diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dbec5f58472..0f702877f97 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,8 +9,10 @@ on: pull_request: branches: [main, master] +# Release authorization needs completed exact-SHA main-push CI. Keep those +# runs alive when master advances, while still cancelling obsolete PR checks. concurrency: - group: ci-${{ github.workflow }}-${{ github.ref }} + group: ci-${{ github.workflow }}-${{ github.ref }}-${{ github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main') && github.sha || 'latest' }} cancel-in-progress: true env: @@ -554,6 +556,18 @@ jobs: -TimeoutSeconds 300 ` -CargoArgs @('test', '--locked', '--target', 'x86_64-pc-windows-msvc', '-p', 'jcode-base', '--no-default-features', '--lib', 'background::tests::adopted_task_', '--', '--nocapture') + - name: Run Windows launcher backup sweep test + shell: pwsh + run: | + # `platform_support.rs` is `#[cfg(windows)]`, so its sweep regression can + # only execute on a Windows runner. Without this step the test is + # compiled on Windows but never run, which is the same + # looks-green-but-never-ran shape the secret-input cohort above calls out. + & ./scripts/invoke_cargo_with_timeout.ps1 ` + -Name 'Windows launcher backup sweep test' ` + -TimeoutSeconds 300 ` + -CargoArgs @('test', '--locked', '--target', 'x86_64-pc-windows-msvc', '-p', 'jcode-build-support', '--lib', 'sweep_removes_only_old_launcher_backups', '--', '--nocapture') + - name: Run Windows e2e smoke tests shell: pwsh run: | diff --git a/.github/workflows/fork-ci.yml b/.github/workflows/fork-ci.yml index bde59e76cff..5127871ac19 100644 --- a/.github/workflows/fork-ci.yml +++ b/.github/workflows/fork-ci.yml @@ -39,7 +39,7 @@ env: # scripts/fork/fmt-check.sh to work out which .rs files are fork carries. # UPDATE THIS when the Desvio base moves. The quality job's checkout needs it # locally, so it fetches full history (fetch-depth: 0). - FORK_UPSTREAM_BASE: 7fd42339c907de223021bd63e5bb5df778c43a01 + FORK_UPSTREAM_BASE: 6af953607d14855f83ac9bfcbc9fc957caadc072 jobs: quality: diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 6eb73418e7b..60653e8ead5 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -4,13 +4,18 @@ on: push: branches: [master] workflow_dispatch: + inputs: + expedite: + description: Publish this master snapshot without the quiet wait, retaining all validation + type: boolean + default: false -# A new master push cancels the sleep, CI wait, and any obsolete builds. -# Manual/recovery retries use the same gates and a fresh preview number, but -# never cancel an active build if the Actions run-list API briefly lags. +# Automatic runs debounce master and discard obsolete builds. Explicit expedited +# dispatches pin their master snapshot in a separate, non-cancelling group so +# ongoing development cannot starve publication. All validation still applies. # No tag/release/workflow_run trigger, and only GITHUB_TOKEN may publish. concurrency: - group: v1-preview-master + group: v1-preview-${{ github.event_name == 'workflow_dispatch' && inputs.expedite && 'pinned' || 'master' }} cancel-in-progress: ${{ github.event_name == 'push' }} permissions: @@ -19,6 +24,7 @@ permissions: env: JCODE_CI: "1" + JCODE_PREVIEW_PINNED: ${{ github.event_name == 'workflow_dispatch' && inputs.expedite && 'true' || 'false' }} JCODE_RELEASE_BUILD: "1" JCODE_CI_BUILD: "1" JCODE_BUILD_SEMVER: v1.0.0-preview.${{ github.run_number }} @@ -37,6 +43,7 @@ jobs: timeout-minutes: 205 steps: - name: Require 15 quiet minutes on master + if: env.JCODE_PREVIEW_PINNED != 'true' run: sleep 900 - uses: actions/checkout@v4 with: @@ -45,7 +52,10 @@ jobs: - name: Reject superseded master commit env: GH_TOKEN: ${{ github.token }} - run: python3 scripts/preview_release.py check-head --sha "$GITHUB_SHA" --tag "$JCODE_BUILD_SEMVER" + run: | + set -- + if [ "$JCODE_PREVIEW_PINNED" = true ]; then set -- --pinned; fi + python3 scripts/preview_release.py check-head --sha "$GITHUB_SHA" --tag "$JCODE_BUILD_SEMVER" "$@" - name: Require exact-commit source CI under preview policy env: GH_TOKEN: ${{ github.token }} @@ -234,10 +244,13 @@ jobs: pattern: preview-jcode-* merge-multiple: true path: dist - - name: Publish complete preview only if master is still current + - name: Publish validated current head or explicitly pinned master snapshot env: GH_TOKEN: ${{ github.token }} - run: python3 scripts/preview_release.py publish --sha "$GITHUB_SHA" --tag "$JCODE_BUILD_SEMVER" --assets dist + run: | + set -- + if [ "$JCODE_PREVIEW_PINNED" = true ]; then set -- --pinned; fi + python3 scripts/preview_release.py publish --sha "$GITHUB_SHA" --tag "$JCODE_BUILD_SEMVER" --assets dist "$@" publish-sdk: needs: publish diff --git a/Cargo.lock b/Cargo.lock index 02590fc85f7..d1f55df8d93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -208,6 +208,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "170433209e817da6aae2c51aa0dd443009a613425dd041ebfb2492d1c4c11a25" +[[package]] +name = "approx" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" +dependencies = [ + "num-traits", +] + [[package]] name = "arboard" version = "3.6.1" @@ -2092,6 +2101,12 @@ dependencies = [ "cipher 0.5.2", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "digest" version = "0.10.7" @@ -2779,6 +2794,12 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" +[[package]] +name = "futures-timer" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af43fadb8a98512d547e37b4e92e0ced13e205c061b87b4623eff01d918d6968" + [[package]] name = "futures-util" version = "0.3.31" @@ -4465,6 +4486,7 @@ dependencies = [ "anyhow", "jcode-applet-types", "jcode-harness-api", + "jcode-logging", "jcode-provider-metadata", "jcode-todo-policy", "jcode-transport", @@ -4900,6 +4922,7 @@ dependencies = [ "cpal", "futures", "jcode-base", + "jcode-provider-env", "libc", "regex", "reqwest 0.12.28", @@ -4909,6 +4932,7 @@ dependencies = [ "tempfile", "tokio", "tokio-tungstenite", + "webpki-roots", "zbus", ] @@ -5078,6 +5102,7 @@ checksum = "bde5057d6143cc94e861d90f591b9303d6716c6b9602309150bd068853c10899" dependencies = [ "hashbrown 0.16.1", "portable-atomic", + "portable-atomic-util", "thiserror 2.0.19", ] @@ -6275,10 +6300,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cbf71184cc5ecc2e4e1baccdb21026c20e5fc3dcf63028a086131b3ab00b6e6" dependencies = [ + "approx", "bytemuck", "fast-srgb8", "libm", "palette_derive", + "phf", ] [[package]] @@ -6666,9 +6693,9 @@ checksum = "f89776e4d69bb58bc6993e99ffa1d11f228b839984854c7daeb5d37f87cbe950" [[package]] name = "portable-atomic-util" -version = "0.2.4" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" +checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715" dependencies = [ "portable-atomic", ] @@ -6703,6 +6730,16 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "pretty_assertions" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "prettyplease" version = "0.2.37" @@ -7085,16 +7122,21 @@ dependencies = [ [[package]] name = "ratatui-core" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ef8dea09a92caaf73bff7adb70b76162e5937524058a7e5bff37869cbbec293" dependencies = [ + "anstyle", "bitflags 2.10.0", "compact_str", + "document-features", "hashbrown 0.16.1", "indoc", "itertools 0.14.0", "kasuari", "lru", + "palette", + "pretty_assertions", + "rstest", + "serde", + "serde_json", "strum 0.27.2", "thiserror 2.0.19", "unicode-segmentation", @@ -7153,16 +7195,18 @@ dependencies = [ [[package]] name = "ratatui-widgets" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7dbfa023cd4e604c2553483820c5fe8aa9d71a42eea5aa77c6e7f35756612db" dependencies = [ "bitflags 2.10.0", + "document-features", "hashbrown 0.16.1", "indoc", "instability", "itertools 0.14.0", "line-clipping", + "pretty_assertions", "ratatui-core", + "rstest", + "serde", "strum 0.27.2", "time", "unicode-segmentation", @@ -7307,6 +7351,12 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" +[[package]] +name = "relative-path" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2" + [[package]] name = "relative-path" version = "2.0.1" @@ -7500,7 +7550,7 @@ checksum = "6657979a24d543e5fc66f32cef2f47ca82ce8eb55cec5f7d59a12b7b570bbf24" dependencies = [ "async-lock", "hashbrown 0.17.1", - "relative-path", + "relative-path 2.0.1", "rquickjs-sys", ] @@ -7523,6 +7573,35 @@ dependencies = [ "thiserror 2.0.19", ] +[[package]] +name = "rstest" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5a3193c063baaa2a95a33f03035c8a72b83d97a54916055ba22d35ed3839d49" +dependencies = [ + "futures-timer", + "futures-util", + "rstest_macros", +] + +[[package]] +name = "rstest_macros" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c845311f0ff7951c5506121a9ad75aec44d083c31583b2ea5a30bcb0b0abba0" +dependencies = [ + "cfg-if", + "glob", + "proc-macro-crate", + "proc-macro2", + "quote", + "regex", + "relative-path 1.9.3", + "rustc_version", + "syn 2.0.117", + "unicode-ident", +] + [[package]] name = "rusqlite" version = "0.41.1" diff --git a/Cargo.toml b/Cargo.toml index 0f8093312bc..ba5396d5be0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,6 +97,8 @@ members = [ "crates/jcode-telemetry-core", "crates/jcode-tui-workspace", "crates/jcode-render-core", + "vendor/ratatui-jcode/ratatui-core", + "vendor/ratatui-jcode/ratatui-widgets", ] [lib] @@ -639,3 +641,9 @@ jcode-provider-core = { path = "crates/jcode-provider-core" } jcode-tui = { path = "crates/jcode-tui", default-features = false, features = ["test-support"] } [build-dependencies] + +# ratatui-jcode: in-tree fork of ratatui-core / ratatui-widgets tuned for +# jcode's per-frame hot paths. See vendor/ratatui-jcode/README.md. +[patch.crates-io] +ratatui-core = { path = "vendor/ratatui-jcode/ratatui-core" } +ratatui-widgets = { path = "vendor/ratatui-jcode/ratatui-widgets" } diff --git a/OAUTH.md b/OAUTH.md index 68607444480..6046679e813 100644 --- a/OAUTH.md +++ b/OAUTH.md @@ -163,9 +163,59 @@ a malformed value is logged and ignored rather than breaking requests. ### Banked Codex usage resets +To let agents spend an earned reset and continue automatically when the OpenAI +weekly limit is exhausted, opt in under `[provider]` in `~/.jcode/config.toml`: + +```toml +[provider] +openai_auto_banked_reset = true +``` + +This is disabled by default. Automatic recovery checks fresh, account-scoped +usage and requires an enforced limit plus exhausted weekly usage, not just a +rounded percentage or a transient 429. It redeems the soonest-expiring reset for +that OAuth login and retries the request once when no approved pool route is ready. +It never purchases credits or enables paid overage. Concurrent agents serialize +automatic redemption, and uncertain redemption results have a five-minute +cooldown to avoid spending another reset immediately. Manual resets remain +available independently of this setting. + +Anthropic Fable earned resets have a separate opt-in: + +```toml +[provider] +anthropic_auto_fable_reset = true +``` + +This uses the Fable-specific `cedar_ember` grant, not the five-hour-only +`juniper_tide` session reset. It requires a named saved OAuth account, fresh +server-confirmed weekly exhaustion, and a usable earned Fable grant. A successful +claim must be confirmed against fresh quota before the waiting request resumes. +Uncertain claims retain a durable journal to prevent another automatic spend. +It never purchases usage credits. See [Fable reset details](docs/anthropic-fable-resets.md). + +Both automatic reset options default to **route first, reset last**. When the +existing account router can offer another ready account or provider in your +auto-switch pool, it offers that exact route instead of spending a reset. +Normal route approval and cross-provider countdown/manual rules still apply. +Excluded accounts, cooling-down routes, and models that cannot fit the request +do not postpone reset recovery. To prefer spending a reset before routing instead: + +```toml +[provider] +auto_reset_only_when_no_routes = false +``` + +This ordering option does not enable automatic reset spending by itself. The +provider-specific opt-in is still required, and the reset must pass fresh +weekly-exhaustion and earned-grant checks. + +Account headers report session, weekly, and each model-specific/additional usage +window that the provider reports, rather than selecting just one window. + `/reset` (or `/reset usage limits openai`) checks the active OpenAI OAuth account's banked resets and shows the selected reset, account, and expiry. It selects the -soonest-expiring available reset. Nothing is spent until you run +soonest-expiring available reset. With automatic recovery disabled, nothing is spent until you run `/reset usage limits openai confirm`. Use `/reset usage limits openai cancel` to dismiss the pending confirmation. API keys cannot redeem these resets. diff --git a/crates/jcode-app-core/src/agent/cache_request.rs b/crates/jcode-app-core/src/agent/cache_request.rs index 010a9db03bf..efd05c43b05 100644 --- a/crates/jcode-app-core/src/agent/cache_request.rs +++ b/crates/jcode-app-core/src/agent/cache_request.rs @@ -7,7 +7,8 @@ pub(super) fn stable_hash_str(value: &str) -> u64 { hasher.finish() } -pub(super) fn stable_hash_json(value: &T) -> u64 { +#[cfg(test)] +fn stable_hash_json(value: &T) -> u64 { let encoded = serde_json::to_string(value).unwrap_or_default(); stable_hash_str(&encoded) } @@ -18,7 +19,34 @@ pub(super) fn stable_json_len(value: &T) -> usize .unwrap_or_default() } -pub(super) fn message_hashes(messages: &[Message]) -> Vec { +fn json_hash_and_len(value: &T) -> (u64, usize) { + let encoded = serde_json::to_string(value).unwrap_or_default(); + (stable_hash_str(&encoded), encoded.len()) +} + +/// Reuse each projected encoding for both the per-message signature and the +/// full JSON array signature. Hash appends a 0xff domain separator, so +/// finish the incrementally written array with the same separator. +fn projected_message_hashes(messages: &[Message]) -> (u64, Vec) { + let mut aggregate = std::collections::hash_map::DefaultHasher::new(); + aggregate.write(b"["); + let mut hashes = Vec::with_capacity(messages.len()); + for (index, message) in messages.iter().enumerate() { + if index != 0 { + aggregate.write(b","); + } + let projected = crate::message::cache_relevant_message_value(message); + let encoded = serde_json::to_string(&projected).unwrap_or_default(); + hashes.push(stable_hash_str(&encoded)); + aggregate.write(encoded.as_bytes()); + } + aggregate.write(b"]"); + aggregate.write_u8(0xff); + (aggregate.finish(), hashes) +} + +#[cfg(test)] +fn message_hashes(messages: &[Message]) -> Vec { // Hash the cache-relevant projection, not the raw Message. Raw hashing // keys off non-transmitted metadata (timestamp, tool_duration_ms, // ReasoningTrace blocks, cache_control markers), which triggers spurious @@ -38,23 +66,30 @@ pub(super) fn kv_cache_request_event( // when the provider reports a cache miss for some unrelated reason. // Borrow definitions rather than cloning potentially large MCP schemas. let eager_tools: Vec<_> = tools.iter().filter(|tool| !tool.defer_loading).collect(); + let (tools_hash, tools_json_chars) = json_hash_and_len(&eager_tools); + let (messages_hash, message_hashes) = projected_message_hashes(messages); + let (ephemeral_fingerprint, ephemeral_chars) = json_hash_and_len(ephemeral_messages); let ephemeral_hash = if ephemeral_messages.is_empty() { None } else { - Some(stable_hash_json(ephemeral_messages)) + Some(ephemeral_fingerprint) }; ServerEvent::KvCacheRequest { system_static_hash: stable_hash_str(system_static), - tools_hash: stable_hash_json(&eager_tools), - messages_hash: stable_hash_json(&crate::message::cache_relevant_messages(messages)), - message_hashes: message_hashes(messages), + tools_hash, + messages_hash, + message_hashes, message_count: messages.len(), tool_count: eager_tools.len(), system_static_chars: system_static.chars().count(), - tools_json_chars: stable_json_len(&eager_tools), + tools_json_chars, messages_json_chars: stable_json_len(messages), ephemeral_hash, - ephemeral_chars: stable_json_len(ephemeral_messages), + ephemeral_chars, ephemeral_message_count: ephemeral_messages.len(), } } + +#[cfg(test)] +#[path = "cache_request/tests.rs"] +mod tests; diff --git a/crates/jcode-app-core/src/agent/cache_request/tests.rs b/crates/jcode-app-core/src/agent/cache_request/tests.rs new file mode 100644 index 00000000000..d0ec5bfe0a8 --- /dev/null +++ b/crates/jcode-app-core/src/agent/cache_request/tests.rs @@ -0,0 +1,143 @@ +use super::*; + +fn legacy_event( + messages: &[Message], + tools: &[ToolDefinition], + system: &str, + ephemeral: &[Message], +) -> ServerEvent { + let eager: Vec<_> = tools.iter().filter(|tool| !tool.defer_loading).collect(); + ServerEvent::KvCacheRequest { + system_static_hash: stable_hash_str(system), + tools_hash: stable_hash_json(&eager), + messages_hash: stable_hash_json(&crate::message::cache_relevant_messages(messages)), + message_hashes: message_hashes(messages), + message_count: messages.len(), + tool_count: eager.len(), + system_static_chars: system.chars().count(), + tools_json_chars: stable_json_len(&eager), + messages_json_chars: stable_json_len(messages), + ephemeral_hash: if ephemeral.is_empty() { + None + } else { + Some(stable_hash_json(ephemeral)) + }, + ephemeral_chars: stable_json_len(ephemeral), + ephemeral_message_count: ephemeral.len(), + } +} + +fn tools() -> Vec { + let eager = ToolDefinition::new( + "lookup", + "Unicode 🦊 and \"escapes\"", + serde_json::json!({"type":"object","properties":{"query":{"type":"string"}}}), + ); + let mut deferred = eager.clone(); + deferred.name = "late".into(); + deferred.defer_loading = true; + vec![eager, deferred] +} + +#[test] +fn fused_request_matches_all_legacy_fields() { + let mut cases = vec![ + vec![], + vec![Message::user("")], + vec![Message::user("\"\n\r\t\\\0🦊é")], + ]; + let mut mixed = vec![Message::user("question"), Message::assistant_text("answer")]; + mixed[1].content.push(ContentBlock::ReasoningTrace { + text: "private reasoning".into(), + }); + mixed[0].tool_duration_ms = Some(123); + cases.push(mixed); + for messages in &cases { + for ephemeral in &cases { + let old = legacy_event(messages, &tools(), "sys🦊", ephemeral); + let new = kv_cache_request_event(messages, &tools(), "sys🦊", ephemeral); + assert_eq!( + serde_json::to_value(old).unwrap(), + serde_json::to_value(new).unwrap() + ); + } + } +} + +#[test] +fn projected_stream_hash_matches_string_hash_across_chunk_boundaries() { + for size in 0..140 { + let mut messages = vec![ + Message::user(&"x🦊\n\"".repeat(size)), + Message::assistant_text("tail"), + ]; + let (aggregate, per_message) = projected_message_hashes(&messages); + assert_eq!( + aggregate, + stable_hash_json(&crate::message::cache_relevant_messages(&messages)) + ); + assert_eq!(per_message, message_hashes(&messages)); + messages.reverse(); + assert_eq!( + projected_message_hashes(&messages).0, + stable_hash_json(&crate::message::cache_relevant_messages(&messages)) + ); + } +} + +#[test] +fn fused_json_failure_preserves_empty_hash_and_zero_length() { + struct Fail; + impl serde::Serialize for Fail { + fn serialize(&self, _: S) -> Result { + Err(serde::ser::Error::custom("expected")) + } + } + assert_eq!(json_hash_and_len(&Fail), (stable_hash_str(""), 0)); +} + +#[test] +#[ignore = "paired complete cache diagnostic computation benchmark"] +fn benchmark_request_diagnostics() { + for count in [0, 8, 96] { + let messages: Vec<_> = (0..count) + .map(|i| Message::user(&format!("{i}: {}", "request 🦊 \"payload\"\n".repeat(256)))) + .collect(); + let tools = tools(); + let ephemeral = [Message::user("dynamic")]; + assert_eq!( + serde_json::to_value(legacy_event(&messages, &tools, "system", &ephemeral)).unwrap(), + serde_json::to_value(kv_cache_request_event( + &messages, &tools, "system", &ephemeral + )) + .unwrap() + ); + std::hint::black_box(legacy_event(&messages, &tools, "system", &ephemeral)); + std::hint::black_box(kv_cache_request_event( + &messages, &tools, "system", &ephemeral, + )); + let measure = |old| { + let start = std::time::Instant::now(); + for _ in 0..30 { + std::hint::black_box(if old { + legacy_event(&messages, &tools, "system", &ephemeral) + } else { + kv_cache_request_event(&messages, &tools, "system", &ephemeral) + }); + } + start.elapsed() + }; + for round in 0..4 { + let (old, new) = if round % 2 == 0 { + (measure(true), measure(false)) + } else { + let new = measure(false); + (measure(true), new) + }; + eprintln!( + "CACHE_REQUEST count={count} round={round} old={old:?} new={new:?} speedup={:.3}x", + old.as_secs_f64() / new.as_secs_f64() + ); + } + } +} diff --git a/crates/jcode-app-core/src/agent/status.rs b/crates/jcode-app-core/src/agent/status.rs index 9d85f3767c6..0af5be85e91 100644 --- a/crates/jcode-app-core/src/agent/status.rs +++ b/crates/jcode-app-core/src/agent/status.rs @@ -177,6 +177,30 @@ impl Agent { assistant_output_tail_after(&self.session.messages, start_index) } + /// The final visible assistant prose of this turn (its answer), used for + /// notification previews. Tool calls and reasoning are excluded. + pub fn final_assistant_text_after(&self, start_index: usize) -> Option { + self.session + .messages + .iter() + .skip(start_index) + .rev() + .filter(|message| message.role == Role::Assistant) + .find_map(|message| { + let text = message + .content + .iter() + .filter_map(|block| match block { + ContentBlock::Text { text, .. } => Some(text.trim()), + _ => None, + }) + .filter(|text| !text.is_empty()) + .collect::>() + .join("\n\n"); + (!text.is_empty()).then_some(text) + }) + } + pub fn last_upstream_provider(&self) -> Option { self.last_upstream_provider .clone() diff --git a/crates/jcode-app-core/src/agent/tool_handoff.rs b/crates/jcode-app-core/src/agent/tool_handoff.rs index 90c47319289..a3c5907eeca 100644 --- a/crates/jcode-app-core/src/agent/tool_handoff.rs +++ b/crates/jcode-app-core/src/agent/tool_handoff.rs @@ -14,6 +14,14 @@ pub(super) fn auto_background_allowed(name: &str, input: &serde_json::Value) -> && !(name == "bg" && input.get("action").and_then(|value| value.as_str()) == Some("wait")) } +/// Name a handed-off task after the call's `intent`, so wait rows read +/// "Waiting for Run parser tests" instead of "Waiting for bash". +pub(super) fn handoff_display_name(input: &serde_json::Value) -> Option { + let intent = input.get("intent")?.as_str()?; + let intent = intent.split_whitespace().collect::>().join(" "); + (!intent.is_empty()).then(|| crate::util::truncate_str(&intent, 28).to_string()) +} + /// SDK callbacks and nonstreaming turns use the same policy as TUI turns. pub(in crate::agent) async fn execute_with_background( registry: Registry, @@ -34,6 +42,7 @@ async fn execute_with_background_budget( let auto_background = auto_background_allowed(&name, &input); let session_id = ctx.session_id.clone(); let tool_name = name.clone(); + let display_name = handoff_display_name(&input); let mut work = crate::runtime_activity::spawn_session(&session_id, async move { registry.execute(&tool_name, input, ctx).await }) @@ -42,7 +51,9 @@ async fn execute_with_background_budget( biased; result = &mut work => result.map_err(|e| anyhow::anyhow!("Tool task panicked: {}", e))?, _ = tokio::time::sleep(budget), if auto_background && !budget.is_zero() => { - let info = crate::background::global().adopt(&name, &session_id, work).await; + let info = crate::background::global() + .adopt_with_options(&name, display_name, &session_id, true, false, work) + .await; Ok(crate::tool::ToolOutput::new(format!( "Tool '{}' was moved to background {} (task_id: {}).\n\n{}", name, automatic_handoff_reason(budget), info.task_id, crate::tool::BACKGROUND_WAIT_GUIDANCE @@ -80,8 +91,8 @@ pub(super) fn reload_interrupted_tool_result(tc: &ToolCall, elapsed_secs: f64) - .get("action") .and_then(|value| value.as_str()) .unwrap_or(""); - let is_wait_like = (tc.name == "bg" && action == "wait") - || (tc.name == "swarm" && matches!(action, "await_members" | "run_plan")); + let is_wait_like = + (tc.name == "bg" && action == "wait") || (tc.name == "swarm" && action == "await_members"); if is_wait_like { let input = serde_json::to_string(&tc.input).unwrap_or_else(|_| "{}".to_string()); diff --git a/crates/jcode-app-core/src/agent/turn_execution.rs b/crates/jcode-app-core/src/agent/turn_execution.rs index a20a7e5e148..b329a4b5262 100644 --- a/crates/jcode-app-core/src/agent/turn_execution.rs +++ b/crates/jcode-app-core/src/agent/turn_execution.rs @@ -24,6 +24,11 @@ impl Agent { } } + /// True when this copy may not run turns on this machine. + pub(crate) fn session_lease_blocked(&self) -> bool { + self.session.migration_lease_block().is_some() + } + /// True when a newer migrated transcript replaced this copy on disk. pub(crate) fn session_copy_is_stale(&self) -> bool { matches!( diff --git a/crates/jcode-app-core/src/agent/turn_loops.rs b/crates/jcode-app-core/src/agent/turn_loops.rs index 25213aaa60f..ac6bd127eeb 100644 --- a/crates/jcode-app-core/src/agent/turn_loops.rs +++ b/crates/jcode-app-core/src/agent/turn_loops.rs @@ -20,6 +20,8 @@ impl Agent { pub(crate) const MAX_EMPTY_POST_TOOL_CONTINUATION_ATTEMPTS: u32 = 5; const SEQUENTIAL_TOOL_ROUNDS_BEFORE_BATCH_NUDGE: u32 = 3; const BATCH_NUDGE: &str = "Several tool calls have been made one at a time. If the next independent operations can run concurrently, use the batch tool instead of making more sequential calls. Keep sequential calls when one result is required to decide the next operation."; + const TOOL_CALLS_BEFORE_TODO_NUDGE: usize = 20; + const TODO_NUDGE: &str = "Many tool calls have been made but this session has no todos. Use the todo tool to plan and track this task."; fn update_sequential_tool_rounds(current: u32, tool_count: usize, used_batch: bool) -> u32 { if tool_count == 1 && !used_batch { current.saturating_add(1) @@ -64,6 +66,8 @@ impl Agent { let mut fable_guardrail_reconsiderations = 0u32; let mut sequential_single_tool_rounds = 0u32; let mut batch_nudge_pending = false; + let mut turn_tool_calls = 0usize; + let mut todo_nudge_sent = false; let mut consecutive_malformed_tool_rounds = 0u32; loop { @@ -156,6 +160,16 @@ impl Agent { batch_nudge_pending = false; sequential_single_tool_rounds = 0; } + if !todo_nudge_sent + && turn_tool_calls > Self::TOOL_CALLS_BEFORE_TODO_NUDGE + && tools.iter().any(|tool| tool.name == "todo") + && crate::todo::load_todos(&self.session.id) + .map(|t| t.is_empty()) + .unwrap_or(false) + { + messages_with_memory.push(Message::user(Self::TODO_NUDGE)); + todo_nudge_sent = true; + } // Background features (memory recall) can hit a plan limit with no // visible failure. Surface it once through the agent so every UI // shows the upgrade prompt instead of silently degrading. @@ -1069,6 +1083,7 @@ impl Agent { logging::info("Provider handles tools internally - executing native tools locally"); } + turn_tool_calls += tool_calls.len(); let used_batch = tool_calls.iter().any(|tc| tc.name == "batch"); sequential_single_tool_rounds = Self::update_sequential_tool_rounds( sequential_single_tool_rounds, diff --git a/crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs b/crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs index 457383dafcc..89a3d2c9b70 100644 --- a/crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs +++ b/crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs @@ -1772,7 +1772,14 @@ impl Agent { )); let bg_info = crate::background::global() - .adopt(&tc.name, &self.session.id, tool_handle) + .adopt_with_options( + &tc.name, + tool_handoff::handoff_display_name(&tc.input), + &self.session.id, + true, + false, + tool_handle, + ) .await; let reason = if automatic_background { diff --git a/crates/jcode-app-core/src/agent/turn_streaming_mpsc_handoff_tests.rs b/crates/jcode-app-core/src/agent/turn_streaming_mpsc_handoff_tests.rs index 018af59636a..1ad8b692bfd 100644 --- a/crates/jcode-app-core/src/agent/turn_streaming_mpsc_handoff_tests.rs +++ b/crates/jcode-app-core/src/agent/turn_streaming_mpsc_handoff_tests.rs @@ -52,7 +52,7 @@ async fn callback_paths_honor_custom_and_disabled_budgets() { let result = execute_with_background_budget( registry, "bg".into(), - serde_json::json!({"action": action}), + serde_json::json!({"action": action, "intent": "Run parser\ntests"}), ctx, budget, ) @@ -78,6 +78,11 @@ async fn callback_paths_honor_custom_and_disabled_budgets() { finished.task.status, crate::bus::BackgroundTaskStatus::Completed ); + // The intent names the task, so waits do not read "Waiting for bg". + assert_eq!( + finished.task.display_name.as_deref(), + Some("Run parser tests") + ); assert_eq!( crate::background::global().output(task_id).await.unwrap(), "wait finished" diff --git a/crates/jcode-app-core/src/agents_status.rs b/crates/jcode-app-core/src/agents_status.rs new file mode 100644 index 00000000000..f9153b42882 --- /dev/null +++ b/crates/jcode-app-core/src/agents_status.rs @@ -0,0 +1,567 @@ +//! Aggregate "many agents" status reporter for the mobile Live Activity. +//! +//! Contract: `~/jcode-mobile/docs/PUSH.md` ("Many agents (v2)"). The server +//! POSTs a full snapshot of this host's non-idle sessions to +//! `{base}/agents/status` on change (debounced), as a heartbeat every +//! [`HEARTBEAT`] while non-empty, and one final empty snapshot. Swarm workers +//! are included. Uses the same opt-out and credentials as mobile pushes, and +//! never blocks a turn. + +use serde::Serialize; +use std::collections::HashMap; +use std::sync::{Mutex, OnceLock}; +use std::time::{Duration, Instant}; + +pub const DEBOUNCE: Duration = Duration::from_secs(2); +pub const HEARTBEAT: Duration = Duration::from_secs(5 * 60); +const MAX_SESSIONS: usize = 200; +const TITLE_MAX: usize = 80; +const DETAIL_MAX: usize = 60; +const ERROR_BACKOFF_MIN: Duration = Duration::from_secs(30); +const ERROR_BACKOFF_MAX: Duration = Duration::from_secs(15 * 60); +const SEND_TIMEOUT: Duration = Duration::from_secs(5); +/// Unanswered error states drop out of the snapshot after this. +pub const ERROR_TTL: Duration = Duration::from_secs(30 * 60); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "lowercase")] +pub enum AgentState { + Running, + Waiting, + Error, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Entry { + title: String, + state: AgentState, + detail: String, + since: u64, + /// Monotonic time the current state began (for error expiry). + entered: Instant, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct SessionStatus { + pub session_id: String, + pub title: String, + pub state: AgentState, + pub detail: String, + pub since: u64, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Snapshot { + pub host: String, + pub sessions: Vec, +} + +/// Pure state machine: session states plus debounce/heartbeat scheduling. +#[derive(Debug, Default)] +pub struct Tracker { + sessions: HashMap, + /// First unsent change (debounce anchor). + dirty_since: Option, + last_sent: Option, + /// Whether the last delivered snapshot was non-empty. + last_sent_nonempty: bool, + /// Do not send before this (error back-off). + backoff_until: Option, + backoff: Option, +} + +fn now_unix() -> u64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_secs()) + .unwrap_or(0) +} + +fn clip(text: &str, max: usize) -> String { + let one_line = text.split_whitespace().collect::>().join(" "); + if one_line.chars().count() <= max { + return one_line; + } + let mut out: String = one_line.chars().take(max.saturating_sub(1)).collect(); + out.push('…'); + out +} + +impl Tracker { + fn mark(&mut self, now: Instant) { + self.dirty_since.get_or_insert(now); + } + + fn set( + &mut self, + id: &str, + state: AgentState, + title: Option<&str>, + detail: &str, + now: Instant, + ) { + let since = now_unix(); + let mut changed = !self.sessions.contains_key(id); + let entry = self + .sessions + .entry(id.to_string()) + .or_insert_with(|| Entry { + title: String::new(), + state, + detail: String::new(), + since, + entered: now, + }); + if let Some(title) = title { + let title = clip(title, TITLE_MAX); + if entry.title != title { + entry.title = title; + changed = true; + } + } + if entry.state != state { + entry.state = state; + entry.since = since; + entry.entered = now; + changed = true; + } + let detail = clip(detail, DETAIL_MAX); + if entry.detail != detail { + entry.detail = detail; + changed = true; + } + if changed { + self.mark(now); + } + } + + pub fn turn_started(&mut self, id: &str, title: &str, now: Instant) { + self.sessions.remove(id); + self.set(id, AgentState::Running, Some(title), "", now); + } + + pub fn tool_started(&mut self, id: &str, tool: &str, now: Instant) { + if self + .sessions + .get(id) + .is_some_and(|e| e.state == AgentState::Running) + { + self.set(id, AgentState::Running, None, tool, now); + } + } + + pub fn tool_done(&mut self, id: &str, now: Instant) { + if self.sessions.contains_key(id) { + self.set(id, AgentState::Running, None, "", now); + } + } + + pub fn waiting(&mut self, id: &str, detail: &str, now: Instant) { + self.set(id, AgentState::Waiting, None, detail, now); + } + + pub fn turn_finished(&mut self, id: &str, error: Option<&str>, now: Instant) { + match error { + Some(detail) => self.set(id, AgentState::Error, None, detail, now), + None => { + if self.sessions.remove(id).is_some() { + self.mark(now); + } + } + } + } + + pub fn session_closed(&mut self, id: &str, now: Instant) { + if self.sessions.remove(id).is_some() { + self.mark(now); + } + } + + /// Drop error states older than [`ERROR_TTL`]. + pub fn expire(&mut self, now: Instant) { + let before = self.sessions.len(); + self.sessions.retain(|_, e| { + e.state != AgentState::Error || now.saturating_duration_since(e.entered) < ERROR_TTL + }); + if self.sessions.len() != before { + self.mark(now); + } + } + + fn next_expiry(&self) -> Option { + self.sessions + .values() + .filter(|e| e.state == AgentState::Error) + .map(|e| e.entered + ERROR_TTL) + .min() + } + + pub fn snapshot(&self, host: &str) -> Snapshot { + let mut sessions: Vec = self + .sessions + .iter() + .map(|(id, e)| SessionStatus { + session_id: id.clone(), + title: e.title.clone(), + state: e.state, + detail: e.detail.clone(), + since: e.since, + }) + .collect(); + let rank = |s: AgentState| match s { + AgentState::Waiting => 0, + AgentState::Error => 1, + AgentState::Running => 2, + }; + sessions.sort_by(|a, b| { + rank(a.state) + .cmp(&rank(b.state)) + .then(b.since.cmp(&a.since)) + .then(a.session_id.cmp(&b.session_id)) + }); + sessions.truncate(MAX_SESSIONS); + Snapshot { + host: host.to_string(), + sessions, + } + } + + /// When the next send is due, or `None` if nothing to send. + pub fn next_due(&self) -> Option { + let due = match (self.dirty_since, self.last_sent) { + (Some(dirty), _) => Some(dirty + DEBOUNCE), + (None, Some(last)) if self.last_sent_nonempty || !self.sessions.is_empty() => { + Some(last + HEARTBEAT) + } + (None, None) if !self.sessions.is_empty() => Some(Instant::now()), + _ => None, + }; + let due = due.map(|due| match self.backoff_until { + Some(b) if b > due => b, + _ => due, + }); + match (due, self.next_expiry()) { + (Some(a), Some(b)) => Some(a.min(b)), + (a, b) => a.or(b), + } + } + + /// Take a snapshot if a send is due at `now`. + pub fn take_due(&mut self, host: &str, now: Instant) -> Option { + self.expire(now); + if self.next_due()? > now { + return None; + } + Some(self.snapshot(host)) + } + + pub fn record_result(&mut self, snapshot: &Snapshot, ok: bool, now: Instant) { + if ok { + self.dirty_since = None; + self.last_sent = Some(now); + self.last_sent_nonempty = !snapshot.sessions.is_empty(); + self.backoff = None; + self.backoff_until = None; + } else { + let next = self + .backoff + .map(|b| (b * 2).min(ERROR_BACKOFF_MAX)) + .unwrap_or(ERROR_BACKOFF_MIN); + self.backoff = Some(next); + self.backoff_until = Some(now + next); + } + } + + /// Back off without counting an error (e.g. no registered devices). + pub fn suppress_until(&mut self, until: Instant) { + self.backoff_until = Some(until); + } +} + +static TRACKER: Mutex> = Mutex::new(None); +static WAKE: OnceLock = OnceLock::new(); +static WORKER_STARTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + +fn with_tracker(f: impl FnOnce(&mut Tracker, Instant)) { + if !crate::mobile_push::enabled() { + return; + } + let mut guard = TRACKER.lock().unwrap_or_else(|p| p.into_inner()); + f(guard.get_or_insert_with(Tracker::default), Instant::now()); + drop(guard); + ensure_worker(); + WAKE.get_or_init(tokio::sync::Notify::new).notify_one(); +} + +pub fn turn_started(session_id: &str, title: &str) { + with_tracker(|t, now| t.turn_started(session_id, title, now)); +} +pub fn tool_started(session_id: &str, tool: &str) { + with_tracker(|t, now| t.tool_started(session_id, tool, now)); +} +pub fn tool_done(session_id: &str) { + with_tracker(|t, now| t.tool_done(session_id, now)); +} +pub fn waiting(session_id: &str, detail: &str) { + with_tracker(|t, now| t.waiting(session_id, detail, now)); +} +pub fn turn_finished(session_id: &str, error: Option<&str>) { + with_tracker(|t, now| t.turn_finished(session_id, error, now)); +} +pub fn session_closed(session_id: &str) { + with_tracker(|t, now| t.session_closed(session_id, now)); +} + +/// Stable host id: `[A-Za-z0-9_.:-]{1,64}`, from /etc/machine-id or hostname. +pub fn host_id() -> &'static str { + static HOST: OnceLock = OnceLock::new(); + HOST.get_or_init(|| { + let raw = ["/etc/machine-id", "/var/lib/dbus/machine-id"] + .iter() + .find_map(|p| host_source(std::fs::read_to_string(p), p)) + .map(|s| s.trim().to_string()) + .filter(|s| !s.is_empty()) + .or_else(|| host_source(std::env::var("HOSTNAME"), "HOSTNAME")) + .or_else(|| host_source(std::fs::read_to_string("/etc/hostname"), "/etc/hostname")) + .or_else(|| host_source(std::env::var("COMPUTERNAME"), "COMPUTERNAME")) + .unwrap_or_else(|| "jcode-host".to_string()); + sanitize_host(&raw) + }) +} + +// Missing platform-specific identity sources are expected. Log failures at debug +// level and continue to the next source, keeping host reporting best-effort. +fn host_source(result: Result, source: &str) -> Option { + match result { + Ok(value) => Some(value), + Err(error) => { + crate::logging::debug(&format!( + "host identity source {source} unavailable: {error}" + )); + None + } + } +} + +pub fn sanitize_host(raw: &str) -> String { + let s: String = raw + .trim() + .chars() + .map(|c| { + if c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | ':' | '-') { + c + } else { + '-' + } + }) + .take(64) + .collect(); + if s.is_empty() { "jcode-host".into() } else { s } +} + +fn ensure_worker() { + let Ok(handle) = tokio::runtime::Handle::try_current() else { + return; + }; + if WORKER_STARTED.swap(true, std::sync::atomic::Ordering::SeqCst) { + return; + } + handle.spawn(worker()); +} + +async fn worker() { + let wake = WAKE.get_or_init(tokio::sync::Notify::new); + loop { + let due = TRACKER + .lock() + .unwrap_or_else(|p| p.into_inner()) + .as_ref() + .and_then(Tracker::next_due); + match due { + Some(due) => { + let sleep = tokio::time::sleep_until(tokio::time::Instant::from_std(due)); + tokio::select! { _ = sleep => {}, _ = wake.notified() => continue } + } + None => { + wake.notified().await; + continue; + } + } + let snapshot = TRACKER + .lock() + .unwrap_or_else(|p| p.into_inner()) + .as_mut() + .and_then(|t| t.take_due(host_id(), Instant::now())); + let Some(snapshot) = snapshot else { continue }; + let outcome = post(&snapshot).await; + let mut guard = TRACKER.lock().unwrap_or_else(|p| p.into_inner()); + if let Some(t) = guard.as_mut() { + let now = Instant::now(); + match outcome { + PostOutcome::Ok => t.record_result(&snapshot, true, now), + PostOutcome::NoDevices => { + t.record_result(&snapshot, true, now); + t.suppress_until(now + crate::mobile_push::NO_DEVICES_SUPPRESS); + } + PostOutcome::Failed => t.record_result(&snapshot, false, now), + } + } + } +} + +enum PostOutcome { + Ok, + NoDevices, + Failed, +} + +#[derive(serde::Deserialize)] +struct Reply { + #[serde(default)] + devices: Option, +} + +async fn post(snapshot: &Snapshot) -> PostOutcome { + if !crate::mobile_push::enabled() { + return PostOutcome::Ok; + } + if crate::mobile_push::devices_suppressed() { + return PostOutcome::NoDevices; + } + let Some((key, base)) = crate::mobile_push::credentials() else { + // Not signed in: treat as delivered so we do not spin. + return PostOutcome::Ok; + }; + let result = crate::provider::shared_http_client() + .post(format!("{base}/agents/status")) + .bearer_auth(key) + .json(snapshot) + .timeout(SEND_TIMEOUT) + .send() + .await; + match result { + Ok(resp) if resp.status().is_success() => match resp.json::().await { + Ok(Reply { devices: Some(0) }) => PostOutcome::NoDevices, + _ => PostOutcome::Ok, + }, + Ok(resp) => { + crate::logging::warn(&format!("agents status rejected: HTTP {}", resp.status())); + PostOutcome::Failed + } + Err(error) => { + crate::logging::warn(&format!("agents status failed: {error}")); + PostOutcome::Failed + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn snapshot_orders_and_truncates() { + let mut t = Tracker::default(); + let now = Instant::now(); + t.turn_started("a", "Fix login", now); + t.tool_started("a", "bash", now); + t.turn_started("b", &"x".repeat(200), now); + t.waiting("b", "needs input", now); + t.turn_started("c", "C", now); + t.turn_finished("c", Some("boom"), now); + t.turn_started("d", "D", now); + t.turn_finished("d", None, now); + let snap = t.snapshot("host1"); + let ids: Vec<_> = snap + .sessions + .iter() + .map(|s| s.session_id.as_str()) + .collect(); + assert_eq!(ids, ["b", "c", "a"]); + assert_eq!(snap.sessions[2].detail, "bash"); + assert_eq!(snap.sessions[0].title.chars().count(), TITLE_MAX); + let json = serde_json::to_value(&snap).unwrap(); + assert_eq!(json["sessions"][1]["state"], "error"); + assert_eq!(json["host"], "host1"); + } + + #[test] + fn debounce_heartbeat_and_final_empty() { + let mut t = Tracker::default(); + let t0 = Instant::now(); + assert!(t.next_due().is_none()); + t.turn_started("a", "A", t0); + t.tool_started("a", "bash", t0 + Duration::from_millis(500)); + assert!(t.take_due("h", t0 + Duration::from_secs(1)).is_none()); + let at = t0 + DEBOUNCE; + let snap = t.take_due("h", at).expect("due after debounce"); + assert_eq!(snap.sessions.len(), 1); + t.record_result(&snap, true, at); + assert_eq!(t.next_due(), Some(at + HEARTBEAT)); + t.turn_finished("a", None, at + Duration::from_secs(10)); + let end = at + Duration::from_secs(10) + DEBOUNCE; + let snap = t.take_due("h", end).expect("final empty"); + assert!(snap.sessions.is_empty()); + t.record_result(&snap, true, end); + assert!(t.next_due().is_none(), "no heartbeat after empty"); + } + + #[test] + fn errors_back_off() { + let mut t = Tracker::default(); + let t0 = Instant::now(); + t.turn_started("a", "A", t0); + let snap = t.take_due("h", t0 + DEBOUNCE).unwrap(); + t.record_result(&snap, false, t0 + DEBOUNCE); + assert_eq!(t.next_due(), Some(t0 + DEBOUNCE + ERROR_BACKOFF_MIN)); + t.record_result(&snap, false, t0 + DEBOUNCE); + assert_eq!(t.next_due(), Some(t0 + DEBOUNCE + ERROR_BACKOFF_MIN * 2)); + } + + #[test] + fn error_state_expires_after_ttl() { + let mut t = Tracker::default(); + let t0 = Instant::now(); + t.turn_started("a", "A", t0); + t.turn_finished("a", Some("boom"), t0); + t.turn_started("b", "B", t0); + let snap = t.take_due("h", t0 + DEBOUNCE).unwrap(); + t.record_result(&snap, true, t0 + DEBOUNCE); + assert_eq!(t.next_due(), Some(t0 + DEBOUNCE + HEARTBEAT)); + t.last_sent = Some(t0 + ERROR_TTL); + assert_eq!( + t.next_due(), + Some(t0 + ERROR_TTL), + "expiry wakes the worker" + ); + t.expire(t0 + ERROR_TTL - Duration::from_secs(1)); + assert_eq!(t.snapshot("h").sessions.len(), 2); + t.expire(t0 + ERROR_TTL); + let snap = t.take_due("h", t0 + ERROR_TTL + DEBOUNCE).unwrap(); + let ids: Vec<_> = snap + .sessions + .iter() + .map(|s| s.session_id.as_str()) + .collect(); + assert_eq!(ids, ["b"]); + } + + #[test] + fn unavailable_host_source_allows_fallback() { + assert_eq!( + host_source(Err::("missing"), "test-source"), + None + ); + assert_eq!( + host_source(Ok::<_, std::io::Error>("host".to_string()), "test-source"), + Some("host".to_string()) + ); + } + + #[test] + fn host_is_sanitized() { + assert_eq!(sanitize_host(" my host!\n"), "my-host-"); + assert_eq!(sanitize_host(""), "jcode-host"); + assert_eq!(sanitize_host(&"a".repeat(100)).len(), 64); + } +} diff --git a/crates/jcode-app-core/src/lib.rs b/crates/jcode-app-core/src/lib.rs index 8380b7679e3..f67960d1cc6 100644 --- a/crates/jcode-app-core/src/lib.rs +++ b/crates/jcode-app-core/src/lib.rs @@ -33,6 +33,7 @@ pub mod catchup; pub mod channel; pub mod elicitation; pub use jcode_base::external_auth; +pub mod agents_status; pub mod mission; pub mod mobile_push; pub mod network_retry; @@ -48,6 +49,7 @@ pub mod session_effort; pub mod session_env; pub mod session_launch; pub mod session_rebuild; +pub mod session_window; pub mod setup_hints; pub mod ssh_remote; pub mod startup_profile; diff --git a/crates/jcode-app-core/src/mobile_push.rs b/crates/jcode-app-core/src/mobile_push.rs index f00b4b688f1..c08708841d7 100644 --- a/crates/jcode-app-core/src/mobile_push.rs +++ b/crates/jcode-app-core/src/mobile_push.rs @@ -17,6 +17,38 @@ pub const MIN_TURN_DURATION_FOR_PUSH: Duration = Duration::from_secs(20); /// Back-off after the API reports no registered devices. pub const NO_DEVICES_SUPPRESS: Duration = Duration::from_secs(10 * 60); const SEND_TIMEOUT: Duration = Duration::from_secs(5); + +/// Timing labels are deliberately static, never payloads, URLs or errors. +struct PushTiming { + stage: &'static str, + outcome: &'static str, + start: Instant, +} + +impl PushTiming { + fn new(stage: &'static str) -> Self { + Self { + stage, + outcome: "cancelled", + start: Instant::now(), + } + } + + fn finish(mut self, outcome: &'static str) { + self.outcome = outcome; + } +} + +impl Drop for PushTiming { + fn drop(&mut self) { + crate::logging::info(&format!( + "[CLOUD_TIMING] operation=mobile_push stage={} outcome={} duration_ms={}", + self.stage, + self.outcome, + self.start.elapsed().as_millis() + )); + } +} const BODY_MAX_CHARS: usize = 180; pub const OPT_OUT_ENV: &str = "JCODE_MOBILE_PUSH"; @@ -50,8 +82,22 @@ pub fn build_payload( let title = session_title .map(str::trim) .filter(|t| !t.is_empty()) - .map(|t| truncate_chars(t, 120)); - let collapsed = body.split_whitespace().collect::>().join(" "); + .map(|t| truncate_chars(&plain_inline(t), 120)) + .or_else(|| lookup_session_title(session_id)); + // An answer that ends by asking the user something is waiting on them. + let (kind, summary) = match kind { + PushKind::TurnDone => match trailing_question(body) { + Some(question) => (PushKind::NeedsInput, question), + None => (kind, summarize_answer(body)), + }, + PushKind::NeedsInput => ( + kind, + trailing_question(body).unwrap_or_else(|| summarize_answer(body)), + ), + PushKind::TurnError => (kind, summarize_error(body)), + PushKind::Wake => (kind, summarize_wake(body)), + }; + let collapsed = summary.split_whitespace().collect::>().join(" "); let body = if collapsed.is_empty() { match kind { PushKind::TurnDone => "Turn finished".to_string(), @@ -60,15 +106,331 @@ pub fn build_payload( PushKind::Wake => "Session update".to_string(), } } else { - truncate_chars(&collapsed, BODY_MAX_CHARS) + truncate_words(&collapsed, BODY_MAX_CHARS) }; + // Lead the title with a per-kind marker so the kinds are told apart at a + // glance on the lock screen, not only by the small subtitle. + let title = Some(format!( + "{} {}", + kind_marker(kind), + title.as_deref().unwrap_or("jcode") + )); PushPayload { kind, session_id: session_id.to_string(), session_title: title, body, - turn_id, + // Message ids are random u64s. JSON numbers above 2^53 lose precision + // in JavaScript and the API rejects them, so fold into the safe range. + turn_id: turn_id.map(|id| id & JS_SAFE_INT_MASK), + } +} + +const JS_SAFE_INT_MASK: u64 = (1 << 53) - 1; + +pub fn kind_marker(kind: PushKind) -> &'static str { + match kind { + PushKind::TurnDone => "\u{2705}", // white check mark + PushKind::TurnError => "\u{274C}", // cross mark + PushKind::NeedsInput => "\u{2753}", // question mark + PushKind::Wake => "\u{23F0}", // alarm clock + } +} + +fn lookup_session_title(session_id: &str) -> Option { + if cfg!(test) || session_id.is_empty() { + return None; + } + crate::process_title::terminal_display_title_for_id(session_id) + .map(|t| plain_inline(t.trim())) + .filter(|t| !t.is_empty()) + .map(|t| truncate_chars(&t, 120)) +} + +fn cached_regex( + cache: &'static std::sync::OnceLock>, + pattern: &str, +) -> Option<&'static regex::Regex> { + cache + .get_or_init(|| match regex::Regex::new(pattern) { + Ok(regex) => Some(regex), + Err(error) => { + crate::logging::warn(&format!("invalid mobile push summary regex: {error}")); + None + } + }) + .as_ref() +} + +/// Strip inline Markdown so a notification reads as plain prose. +fn plain_inline(text: &str) -> String { + static LINK: std::sync::OnceLock> = std::sync::OnceLock::new(); + static EMPH: std::sync::OnceLock> = std::sync::OnceLock::new(); + let link = cached_regex(&LINK, r"!?\[([^\]]*)\]\([^)]*\)"); + let emph = cached_regex( + &EMPH, + r"\*\*([^*]+)\*\*|__([^_]+)__|\*([^*\s][^*]*)\*|~~([^~]+)~~", + ); + let text = match link { + Some(link) => link.replace_all(text, "$1"), + None => std::borrow::Cow::Borrowed(text), + }; + let text = match emph { + Some(emph) => emph.replace_all(&text, "$1$2$3$4"), + None => std::borrow::Cow::Borrowed(text.as_ref()), + }; + text.replace('`', "") +} + +/// Turn Markdown into plain lines: drop code blocks, tables, rules and +/// headings markers, list bullets and inline emphasis. +fn plain_lines(text: &str) -> Vec { + let mut out = Vec::new(); + let mut in_fence = false; + for raw in text.lines() { + let line = raw.trim(); + if line.starts_with("```") || line.starts_with("~~~") { + in_fence = !in_fence; + continue; + } + if in_fence || line.is_empty() { + if !in_fence && out.last().is_some_and(|l: &String| !l.is_empty()) { + out.push(String::new()); + } + continue; + } + if line.starts_with('|') + || line + .chars() + .all(|c| matches!(c, '-' | '*' | '_' | '=' | ' ')) + { + continue; + } + // Headings are labels ("Summary"), not content. + if line.starts_with('#') { + continue; + } + let line = line.trim_start_matches('>').trim(); + let line = strip_list_marker(line); + let line = plain_inline(line); + let line = line.trim(); + if !line.is_empty() { + out.push(line.to_string()); + } + } + while out.last().is_some_and(String::is_empty) { + out.pop(); + } + out +} + +fn strip_list_marker(line: &str) -> &str { + for marker in ["- [ ] ", "- [x] ", "- ", "* ", "+ ", "• "] { + if let Some(rest) = line.strip_prefix(marker) { + return rest; + } + } + let digits = line.chars().take_while(char::is_ascii_digit).count(); + if digits > 0 { + let rest = &line[digits..]; + if let Some(rest) = rest.strip_prefix(". ").or_else(|| rest.strip_prefix(") ")) { + return rest; + } + } + line +} + +/// Lead of a final answer: the first paragraph, which by convention carries +/// the conclusion. Bullet items are joined into one sentence-like line. +fn summarize_answer(text: &str) -> String { + let lines = plain_lines(text); + let mut para = Vec::new(); + for line in &lines { + if line.is_empty() { + if !para.is_empty() && para.iter().map(|l: &&String| l.len()).sum::() >= 40 { + break; + } + continue; + } + para.push(line); + } + join_sentences(para.iter().map(|l| l.as_str())) +} + +fn join_sentences<'a>(parts: impl Iterator) -> String { + let mut out = String::new(); + for part in parts { + if !out.is_empty() { + if !out.ends_with(['.', '!', '?', ':', ';']) { + out.push('.'); + } + out.push(' '); + } + out.push_str(part); + } + out +} + +/// If the answer ends by asking the user a question, return that question. +fn trailing_question(text: &str) -> Option { + let lines = plain_lines(text); + let last = lines.iter().rev().find(|l| !l.is_empty())?; + if !last.trim_end_matches(['*', '_', ')']).ends_with('?') { + return None; + } + // Keep the last sentence, plus the one before if it sets up the question. + let mut start = 0; + for (i, c) in last.char_indices() { + if matches!(c, '.' | '!' | '?') && i + 1 < last.len() - 1 { + let next = last[i + c.len_utf8()..].chars().next(); + if next.is_some_and(char::is_whitespace) { + start = i + c.len_utf8(); + } + } } + let question = last[start..].trim(); + (question.chars().count() >= 8).then(|| question.to_string()) +} + +/// Errors arrive as `context: cause: cause`. Drop JSON blobs and noise, keep +/// the most specific readable cause. +fn summarize_error(text: &str) -> String { + static JSON: std::sync::OnceLock> = std::sync::OnceLock::new(); + let flat = text.split_whitespace().collect::>().join(" "); + let Some(json) = cached_regex(&JSON, r"\{.*\}") else { + return flat; + }; + // Pull a human message out of an embedded provider JSON error if present. + let message = json.find(&flat).and_then(|m| { + match serde_json::from_str::(m.as_str()) { + Ok(value) => Some(value), + Err(error) => { + crate::logging::debug(&format!( + "mobile push error contains non-JSON braces: {error}" + )); + None + } + } + .and_then(|v| { + v.pointer("/error/message") + .or_else(|| v.get("message")) + .and_then(|m| m.as_str()) + .map(str::to_string) + }) + }); + let stripped = json.replace_all(&flat, ""); + let mut parts: Vec<&str> = stripped + .split(": ") + .map(|p| p.trim().trim_end_matches([':', ','])) + .filter(|p| !p.is_empty()) + .collect(); + parts.dedup(); + let head = parts.join(": "); + match message { + Some(m) if !head.contains(m.as_str()) => { + if head.is_empty() { + m + } else { + format!("{m} ({head})") + } + } + _ => head, + } +} + +/// Wake notifications are agent-facing Markdown. Rewrite the known shapes +/// into one readable line and strip instructions meant for the model. +fn summarize_wake(text: &str) -> String { + if let Some(task) = crate::message::parse_background_task_notification_markdown(text) { + let label = task + .display_name + .clone() + .unwrap_or_else(|| task.tool_name.clone()); + let status = task.status.trim_start_matches(['✓', '✗', '↻', ' ']).trim(); + let duration = humanize_secs(&task.duration); + let mut line = format!("{label} {status} in {duration}"); + if status == "failed" + && task.exit_label.starts_with("exit ") + && task.exit_label != "exit n/a" + { + line.push_str(&format!(" ({})", task.exit_label)); + } + let detail = task + .failure_summary + .as_deref() + .map(plain_inline) + .or_else(|| task.preview.as_deref().and_then(last_meaningful_line)); + if let Some(detail) = detail.filter(|d| !d.trim().is_empty()) { + line.push_str(": "); + line.push_str(detail.trim()); + } + return line; + } + if let Some(rest) = text.trim().strip_prefix("**Background task stalled** ") { + // Incomplete optional wake headers intentionally render missing pieces empty. + let header = rest.lines().next().unwrap_or(""); + let label = match header.split(" · ").nth(1) { + Some(label) => plain_inline(label), + None => String::new(), + }; + let label = label.split(" (").next().unwrap_or("").trim().to_string(); + let idle = match header + .split("no output or progress for ") + .nth(1) + .and_then(|s| s.split(' ').next()) + { + Some(idle) => humanize_secs(idle), + None => String::new(), + }; + return format!("{label} has been silent for {idle} and may be stuck") + .trim() + .to_string(); + } + let lines: Vec = plain_lines(text) + .into_iter() + .filter(|l| !l.is_empty() && !l.contains("action=\"") && !l.starts_with("Full output:")) + .collect(); + join_sentences(lines.iter().map(String::as_str)) +} + +fn last_meaningful_line(text: &str) -> Option { + text.lines() + .map(str::trim) + .rev() + .find(|l| l.chars().filter(|c| c.is_alphanumeric()).count() >= 4) + .map(|l| truncate_chars(l, 120)) +} + +fn humanize_secs(value: &str) -> String { + let Ok(secs) = value.trim_end_matches('s').parse::() else { + return value.to_string(); + }; + let secs = secs.round() as u64; + match secs { + 0..=59 => format!("{secs}s"), + 60..=3599 => match secs % 60 { + 0 => format!("{}m", secs / 60), + s => format!("{}m {s}s", secs / 60), + }, + _ => format!("{}h {}m", secs / 3600, (secs % 3600) / 60), + } +} + +/// Truncate at a word boundary so previews never end mid-word. +fn truncate_words(text: &str, max: usize) -> String { + if text.chars().count() <= max { + return text.to_string(); + } + let head: String = text.chars().take(max.saturating_sub(1)).collect(); + let cut = head + .rfind(char::is_whitespace) + .filter(|&i| i >= head.len() * 2 / 3) + .unwrap_or(head.len()); + let mut out = head[..cut] + .trim_end_matches([',', ';', ':', ' ', '-']) + .to_string(); + out.push('…'); + out } /// Last `max` chars are most useful for an assistant reply preview? No: the @@ -103,6 +465,14 @@ impl Suppression { static SUPPRESSION: Mutex = Mutex::new(Suppression { until: None }); +/// True while the API recently reported no registered devices. +pub(crate) fn devices_suppressed() -> bool { + SUPPRESSION + .lock() + .map(|s| s.is_suppressed(Instant::now())) + .unwrap_or(false) +} + fn env_opted_out(value: Option<&str>) -> bool { matches!( value.map(|v| v.trim().to_ascii_lowercase()).as_deref(), @@ -110,7 +480,7 @@ fn env_opted_out(value: Option<&str>) -> bool { ) } -fn enabled() -> bool { +pub(crate) fn enabled() -> bool { let opt_out = match std::env::var(OPT_OUT_ENV) { Ok(value) => Some(value), Err(std::env::VarError::NotPresent) => None, @@ -125,7 +495,7 @@ fn enabled() -> bool { crate::config::config().server.mobile_push != Some(false) } -fn credentials() -> Option<(String, String)> { +pub(crate) fn credentials() -> Option<(String, String)> { let key = crate::provider_catalog::load_api_key_from_env_or_config( JCODE_API_KEY_ENV, JCODE_ENV_FILE, @@ -212,7 +582,19 @@ pub fn notify( body: &str, turn_id: Option, ) { - if cfg!(test) || !enabled() { + if cfg!(test) { + return; + } + let requested = kind; + if !enabled() { + record( + requested, + None, + body, + "skipped", + Some("disabled by config or JCODE_MOBILE_PUSH"), + None, + ); return; } if SUPPRESSION @@ -221,20 +603,132 @@ pub fn notify( .unwrap_or(false) { crate::logging::debug("mobile push suppressed: no registered devices"); + record( + requested, + None, + body, + "skipped", + Some("no registered devices (10 min back-off)"), + None, + ); return; } let Ok(handle) = tokio::runtime::Handle::try_current() else { + record( + requested, + None, + body, + "skipped", + Some("no tokio runtime"), + None, + ); return; }; let payload = build_payload(kind, session_id, session_title, body, turn_id); + let source = body.to_string(); handle.spawn(async move { let Some((key, base)) = credentials() else { + record( + requested, + Some(&payload), + &source, + "skipped", + Some("not signed in (no JCODE_API_KEY)"), + None, + ); return; }; - send(&key, &base, &payload).await; + let mut attempt = send(&key, &base, &payload).await; + // Transient network failures (DNS, reset, timeout) get one retry. + if attempt.0 == "error" && attempt.2.is_none() { + tokio::time::sleep(Duration::from_secs(3)).await; + attempt = send(&key, &base, &payload).await; + if let Some(detail) = attempt.1.as_mut() { + detail.push_str(" (after retry)"); + } + } + let (outcome, detail, status) = attempt; + record( + requested, + Some(&payload), + &source, + outcome, + detail.as_deref(), + status, + ); }); } +/// Record a turn that deliberately did not push, so the history explains +/// missing notifications too. +pub fn note_skipped(kind: PushKind, reason: &str) { + if !cfg!(test) { + record(kind, None, "", "skipped", Some(reason), None); + } +} + +/// Append one line per push attempt to `~/.jcode/logs/mobile-push.jsonl`: +/// what was requested, the exact payload sent, a source excerpt, and the +/// outcome. This is the push history used to debug notification quality. +fn record( + requested: PushKind, + payload: Option<&PushPayload>, + source: &str, + outcome: &str, + detail: Option<&str>, + http_status: Option, +) { + use std::io::Write; + let line = serde_json::json!({ + "ts": chrono::Utc::now().to_rfc3339(), + "requested_kind": requested, + "outcome": outcome, + "detail": detail, + "http_status": http_status, + "payload": payload, + "source_chars": source.chars().count(), + "source_excerpt": truncate_chars(source, 1500), + }); + crate::logging::info(&format!( + "mobile push {:?} {outcome}{}", + payload.map(|p| p.kind).unwrap_or(requested), + // No detail is a normal outcome, not a failed operation. + match detail { + Some(detail) => format!(": {detail}"), + None => String::new(), + } + )); + let Some(dir) = dirs::home_dir().map(|h| h.join(".jcode").join("logs")) else { + return; + }; + if let Err(error) = std::fs::create_dir_all(&dir) { + crate::logging::warn(&format!( + "cannot create mobile push history directory: {error}" + )); + return; + } + let path = dir.join("mobile-push.jsonl"); + // Keep the history bounded: rotate once past 5 MB. + if std::fs::metadata(&path).is_ok_and(|m| m.len() > 5 * 1024 * 1024) + && let Err(error) = std::fs::rename(&path, dir.join("mobile-push.1.jsonl")) + { + crate::logging::warn(&format!("cannot rotate mobile push history: {error}")); + return; + } + match std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(&path) + { + Ok(mut file) => { + if let Err(error) = writeln!(file, "{line}") { + crate::logging::warn(&format!("cannot write mobile push history: {error}")); + } + } + Err(error) => crate::logging::warn(&format!("cannot open mobile push history: {error}")), + } +} + #[derive(serde::Deserialize)] struct SendReply { #[serde(default)] @@ -267,15 +761,31 @@ async fn send_mobile_resume( base: &str, payload: &PushPayload, ) -> anyhow::Result { - let response = reqwest::Client::new() + let total = PushTiming::new("total"); + let result = send_mobile_resume_inner(key, base, payload).await; + total.finish(if result.is_ok() { "delivered" } else { "error" }); + result +} + +async fn send_mobile_resume_inner( + key: &str, + base: &str, + payload: &PushPayload, +) -> anyhow::Result { + let http = PushTiming::new("http_send"); + let result = crate::provider::shared_http_client() .post(format!("{base}/push/send")) .bearer_auth(key) .json(&payload) .timeout(SEND_TIMEOUT) .send() - .await? - .error_for_status()?; - let reply = response.json::().await?; + .await; + http.finish(if result.is_ok() { "response" } else { "error" }); + let response = result?.error_for_status()?; + let body = PushTiming::new("body"); + let result = response.json::().await; + body.finish(if result.is_ok() { "decoded" } else { "error" }); + let reply = result?; match (reply.devices, reply.sent) { (Some(0), _) => anyhow::bail!( "No mobile devices registered. Open the Jcode app, sign in to the same account, and enable notifications, then retry /mobile." @@ -289,51 +799,114 @@ async fn send_mobile_resume( } } -async fn send(key: &str, base: &str, payload: &PushPayload) { - let client = match reqwest::Client::builder().timeout(SEND_TIMEOUT).build() { - Ok(c) => c, - Err(error) => { - crate::logging::warn(&format!("mobile push client build failed: {error}")); - return; - } - }; - let result = client +async fn send( + key: &str, + base: &str, + payload: &PushPayload, +) -> (&'static str, Option, Option) { + let total = PushTiming::new("total"); + let result = send_inner(key, base, payload).await; + total.finish(result.0); + result +} + +async fn send_inner( + key: &str, + base: &str, + payload: &PushPayload, +) -> (&'static str, Option, Option) { + let http = PushTiming::new("http_send"); + let result = crate::provider::shared_http_client() .post(format!("{base}/push/send")) .bearer_auth(key) .json(payload) + .timeout(SEND_TIMEOUT) .send() .await; + http.finish(if result.is_ok() { "response" } else { "error" }); match result { Ok(resp) if resp.status().is_success() => { - let reply = match resp.json::().await { + let status = resp.status().as_u16(); + let body = PushTiming::new("body"); + let result = resp.json::().await; + body.finish(if result.is_ok() { "decoded" } else { "error" }); + let reply = match result { Ok(reply) => reply, Err(error) => { - crate::logging::warn(&format!("invalid mobile push reply: {error}")); - return; + return ( + "error", + Some(format!("invalid reply: {error}")), + Some(status), + ); } }; let devices = reply.devices; if let Ok(mut s) = SUPPRESSION.lock() { s.record_reply(devices, Instant::now()); } - crate::logging::debug(&format!( - "mobile push {:?} session={} sent={:?} devices={:?}", - payload.kind, payload.session_id, reply.sent, devices + let detail = format!("sent={:?} devices={:?}", reply.sent, devices); + let outcome = match (reply.sent, devices) { + (_, Some(0)) => "no_devices", + (Some(n), _) if n > 0 => "delivered", + _ => "undelivered", + }; + (outcome, Some(detail), Some(status)) + } + Ok(resp) => { + let status = resp.status().as_u16(); + // Keep the API's error body: it says exactly which field was rejected. + let timing = PushTiming::new("body"); + let result = resp.text().await; + timing.finish(if result.is_ok() { "read" } else { "error" }); + let body = match result { + Ok(body) => body, + Err(error) => format!("unable to read rejection body: {error}"), + }; + crate::logging::warn(&format!( + "mobile push {:?} rejected: HTTP {status}: {}", + payload.kind, + truncate_chars(&body, 300) )); + ("rejected", Some(truncate_chars(&body, 500)), Some(status)) + } + Err(error) => { + let detail = crate::util::format_error_chain(&anyhow::Error::from(error)); + crate::logging::warn(&format!("mobile push failed: {detail}")); + ("error", Some(detail), None) } - Ok(resp) => crate::logging::warn(&format!( - "mobile push {:?} rejected: HTTP {}", - payload.kind, - resp.status() - )), - Err(error) => crate::logging::warn(&format!("mobile push failed: {error}")), } } +#[cfg(test)] +#[path = "mobile_push_http_tests.rs"] +mod http_tests; + #[cfg(test)] mod tests { use super::*; + #[test] + fn invalid_summary_regex_is_cached_without_panicking() { + static CACHE: std::sync::OnceLock> = std::sync::OnceLock::new(); + assert!(cached_regex(&CACHE, "[").is_none()); + assert!( + cached_regex(&CACHE, "valid").is_none(), + "failed initialization is cached" + ); + } + + #[test] + fn malformed_error_json_keeps_readable_context() { + assert_eq!( + summarize_error("request failed: {not json}: retry later"), + "request failed: retry later" + ); + assert_eq!( + summarize_wake("**Background task stalled** partial"), + "has been silent for and may be stuck" + ); + } + #[test] fn cancelled_marker_survives_poisoned_state() { let state = Mutex::new(Some(HashSet::from(["cancelled".to_string()]))); @@ -439,7 +1012,7 @@ mod tests { serde_json::json!({ "kind": "turn_done", "session_id": "session_x", - "session_title": "Fix login", + "session_title": "\u{2705} Fix login", "body": "Done. All tests pass", "turn_id": 42 }) @@ -451,7 +1024,7 @@ mod tests { let p = build_payload(PushKind::NeedsInput, "s", Some(" "), "", None); let v = serde_json::to_value(&p).unwrap(); assert_eq!(v["kind"], "needs_input"); - assert!(v.get("session_title").is_none()); + assert_eq!(v["session_title"], "\u{2753} jcode"); assert!(v.get("turn_id").is_none()); assert_eq!(v["body"], "Waiting for your input"); let long = "a".repeat(500); @@ -463,6 +1036,87 @@ mod tests { ); } + #[test] + fn mobile_push_turn_id_is_js_safe() { + let p = build_payload( + PushKind::TurnDone, + "s", + None, + "ok", + Some(8338987288606337570), + ); + assert!(p.turn_id.unwrap() <= (1 << 53) - 1); + assert_eq!( + build_payload(PushKind::TurnDone, "s", None, "ok", Some(42)).turn_id, + Some(42) + ); + } + + #[test] + fn mobile_push_answer_uses_lead_paragraph_as_plain_text() { + let answer = "## Summary\n\nFixed the **login** bug in `auth.rs`; all 12 tests pass.\n\n- detail one\n- detail two\n\n```rust\nfn x() {}\n```"; + let p = build_payload(PushKind::TurnDone, "s", Some("Fix `login`"), answer, None); + assert_eq!(p.body, "Fixed the login bug in auth.rs; all 12 tests pass."); + assert_eq!(p.session_title.as_deref(), Some("\u{2705} Fix login")); + for (kind, body, marker) in [ + (PushKind::TurnDone, "done", "\u{2705}"), + (PushKind::TurnDone, "Should I push?", "\u{2753}"), + (PushKind::TurnError, "boom", "\u{274C}"), + (PushKind::Wake, "bash completed", "\u{23F0}"), + ] { + let t = build_payload(kind, "s", Some("T"), body, None) + .session_title + .unwrap(); + assert_eq!(t, format!("{marker} T")); + } + let bullets = "- Updated parser\n- Added tests\n- Shipped v2 to production servers today"; + assert_eq!( + build_payload(PushKind::TurnDone, "s", None, bullets, None).body, + "Updated parser. Added tests. Shipped v2 to production servers today" + ); + } + + #[test] + fn mobile_push_trailing_question_becomes_needs_input() { + let p = build_payload( + PushKind::TurnDone, + "s", + None, + "I found two options.\n\nShould I use OAuth or a password?", + None, + ); + assert_eq!(p.kind, PushKind::NeedsInput); + assert_eq!(p.body, "Should I use OAuth or a password?"); + let p = build_payload( + PushKind::TurnDone, + "s", + None, + "Done. Want me to deploy it?", + None, + ); + assert_eq!( + (p.kind, p.body.as_str()), + (PushKind::NeedsInput, "Want me to deploy it?") + ); + } + + #[test] + fn mobile_push_errors_and_wakes_are_readable() { + let e = r#"Provider request failed: HTTP 529: {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}"#; + assert_eq!( + build_payload(PushKind::TurnError, "s", None, e, None).body, + "Overloaded (Provider request failed: HTTP 529)" + ); + let bg = "**Background task** `abc123` · `bash` · ✓ completed · 754.2s · exit 0\n\n```text\nCompiling...\ntest result: ok. 42 passed\n```\n\n_Full output:_ `bg action=\"output\" task_id=\"abc123\"`"; + assert_eq!( + build_payload(PushKind::Wake, "s", None, bg, None).body, + "bash completed in 12m 34s: test result: ok. 42 passed" + ); + let long = "word ".repeat(60); + let p = build_payload(PushKind::Wake, "s", None, &long, None); + assert!(p.body.ends_with("word…")); + } + #[test] fn mobile_push_suppression_window() { let now = Instant::now(); diff --git a/crates/jcode-app-core/src/mobile_push_http_tests.rs b/crates/jcode-app-core/src/mobile_push_http_tests.rs new file mode 100644 index 00000000000..25faa09b235 --- /dev/null +++ b/crates/jcode-app-core/src/mobile_push_http_tests.rs @@ -0,0 +1,211 @@ +use super::*; +use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, +}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +async fn fixture( + status: u16, + body: &'static str, + delay: Duration, +) -> (String, Arc, tokio::task::JoinHandle<()>) { + fixture_with_body_delay(status, body, delay, Duration::ZERO).await +} + +async fn fixture_with_body_delay( + status: u16, + body: &'static str, + delay: Duration, + body_delay: Duration, +) -> (String, Arc, tokio::task::JoinHandle<()>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let connections = Arc::new(AtomicUsize::new(0)); + let count = connections.clone(); + let task = tokio::spawn(async move { + loop { + let (mut socket, _) = listener.accept().await.unwrap(); + socket.set_nodelay(true).unwrap(); + count.fetch_add(1, Ordering::SeqCst); + tokio::spawn(async move { + // Artificial connection setup cost, not an APNs latency model. + tokio::time::sleep(delay).await; + loop { + let mut request = Vec::new(); + let mut byte = [0]; + while !request.ends_with(b"\r\n\r\n") { + if socket.read_exact(&mut byte).await.is_err() { + return; + } + request.push(byte[0]); + } + let headers = String::from_utf8(request).unwrap(); + assert!(headers.starts_with("POST /push/send HTTP/1.1\r\n")); + assert!( + headers + .to_lowercase() + .contains("authorization: bearer fixture-key\r\n") + ); + let length: usize = headers + .lines() + .find_map(|line| { + line.to_lowercase() + .strip_prefix("content-length: ") + .map(str::to_owned) + }) + .unwrap() + .parse() + .unwrap(); + let mut bytes = vec![0; length]; + socket.read_exact(&mut bytes).await.unwrap(); + let actual: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(actual, serde_json::to_value(payload()).unwrap()); + let response = format!( + "HTTP/1.1 {status} Fixture\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n", + body.len() + ); + if socket.write_all(response.as_bytes()).await.is_err() { + return; + } + tokio::time::sleep(body_delay).await; + if socket.write_all(body.as_bytes()).await.is_err() { + return; + } + } + }); + } + }); + (base, connections, task) +} + +fn payload() -> PushPayload { + build_payload( + PushKind::Wake, + "fixture-session", + Some("Fixture"), + "Fixture body", + Some(42), + ) +} + +#[tokio::test] +async fn mobile_push_http_eight_push_connections() { + // Control mirrors the original per-notification client construction. + let (base, connections, task) = + fixture(200, r#"{"sent":1,"devices":1}"#, Duration::from_millis(60)).await; + let start = Instant::now(); + for _ in 0..8 { + let reply = reqwest::Client::builder() + .timeout(SEND_TIMEOUT) + .build() + .unwrap() + .post(format!("{base}/push/send")) + .bearer_auth("fixture-key") + .json(&payload()) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + assert_eq!((reply.sent, reply.devices), (Some(1), Some(1))); + } + eprintln!( + "artificial 60ms connection-delay fresh-client control: eight pushes elapsed_ms={} connections={}", + start.elapsed().as_millis(), + connections.load(Ordering::SeqCst) + ); + assert_eq!(connections.load(Ordering::SeqCst), 8); + task.abort(); + let (base, connections, task) = + fixture(200, r#"{"sent":1,"devices":1}"#, Duration::from_millis(60)).await; + let start = Instant::now(); + for _ in 0..8 { + let result = send("fixture-key", &base, &payload()).await; + assert_eq!( + result, + ( + "delivered", + Some("sent=Some(1) devices=Some(1)".into()), + Some(200) + ) + ); + } + eprintln!( + "artificial 60ms connection-delay fixture: eight pushes elapsed_ms={} connections={}", + start.elapsed().as_millis(), + connections.load(Ordering::SeqCst) + ); + assert_eq!(connections.load(Ordering::SeqCst), 1); + for _ in 0..8 { + assert!( + send_mobile_resume("fixture-key", &base, &payload()) + .await + .unwrap() + .contains("sent to 1 device") + ); + } + assert_eq!(connections.load(Ordering::SeqCst), 1); + task.abort(); +} + +#[tokio::test] +async fn mobile_push_http_reply_semantics() { + for (status, body, outcome) in [ + (200, r#"{"sent":0,"devices":0}"#, "no_devices"), + (200, r#"{"sent":0,"devices":1}"#, "undelivered"), + (200, "invalid", "error"), + (503, "fixture unavailable", "rejected"), + ] { + let (base, _, task) = fixture(status, body, Duration::ZERO).await; + let result = send("fixture-key", &base, &payload()).await; + assert_eq!(result.0, outcome); + assert_eq!(result.2, Some(status)); + task.abort(); + } +} + +#[tokio::test] +async fn mobile_push_http_deadline_and_cancellation() { + let (base, _, task) = fixture( + 200, + r#"{"sent":1,"devices":1}"#, + SEND_TIMEOUT + Duration::from_secs(1), + ) + .await; + let start = Instant::now(); + let result = send("fixture-key", &base, &payload()).await; + assert_eq!(result.0, "error"); + assert_eq!(result.2, None); + assert!(start.elapsed() >= SEND_TIMEOUT); + assert!(start.elapsed() < SEND_TIMEOUT + Duration::from_secs(1)); + let p = payload(); + let future = send("fixture-key", &base, &p); + assert!( + tokio::time::timeout(Duration::from_millis(20), future) + .await + .is_err() + ); + task.abort(); +} + +#[tokio::test] +async fn mobile_push_http_body_deadline() { + let (base, _, task) = fixture_with_body_delay( + 200, + r#"{"sent":1,"devices":1}"#, + Duration::ZERO, + SEND_TIMEOUT + Duration::from_secs(1), + ) + .await; + let p = payload(); + let (automatic, explicit) = tokio::join!( + send("fixture-key", &base, &p), + send_mobile_resume("fixture-key", &base, &p), + ); + assert_eq!(automatic.0, "error"); + assert_eq!(automatic.2, Some(200)); + assert!(explicit.is_err()); + task.abort(); +} diff --git a/crates/jcode-app-core/src/protocol_tests/core_events.rs b/crates/jcode-app-core/src/protocol_tests/core_events.rs index 0018532e7ba..40c166cfb92 100644 --- a/crates/jcode-app-core/src/protocol_tests/core_events.rs +++ b/crates/jcode-app-core/src/protocol_tests/core_events.rs @@ -17,6 +17,7 @@ fn test_request_roundtrip() -> Result<()> { fn test_compacted_history_request_roundtrip() -> Result<()> { let req = Request::GetCompactedHistory { id: 7, + recent_history: false, visible_messages: 64, }; let json = serde_json::to_string(&req)?; diff --git a/crates/jcode-app-core/src/server.rs b/crates/jcode-app-core/src/server.rs index d77b5542da6..1bfae6f51bc 100644 --- a/crates/jcode-app-core/src/server.rs +++ b/crates/jcode-app-core/src/server.rs @@ -48,6 +48,7 @@ pub use group_transfer::{GroupTransferBundle, GroupTransferSelection, GroupTrans pub use swarm_persistence::{PersistedSwarmMember, PersistedSwarmState, PersistedVersionedPlan}; mod idle_hibernation; mod jade_relay; +mod lane_dispatch; mod lifecycle; mod live_status; mod live_turn; @@ -504,7 +505,7 @@ pub use self::socket::{ }; use self::socket::{signal_ready_fd, socket_has_live_listener}; -pub use self::util::ServerIdentity; +pub use self::util::{MANAGED_CLOUD_LIVE_RELOAD_MARKER, ServerIdentity}; pub(crate) use self::util::server_has_newer_binary; use self::util::{ debug_control_allowed, embedding_idle_unload_secs, git_common_dir_for, reload_exec_target, @@ -1147,6 +1148,22 @@ impl Server { shutdown_signals: Arc::clone(&self.shutdown_signals), soft_interrupt_queues: Arc::clone(&self.soft_interrupt_queues), }); + lane_dispatch::register_context(lane_dispatch::LaneDispatchContext { + provider: Arc::clone(&self.provider), + sessions: Arc::clone(&self.sessions), + global_session_id: Arc::clone(&self.session_id), + swarm_state: self.swarm_state.clone(), + channel_subscriptions: Arc::clone(&self.channel_subscriptions), + channel_subscriptions_by_session: Arc::clone(&self.channel_subscriptions_by_session), + event_history: Arc::clone(&self.event_history), + event_counter: Arc::clone(&self.event_counter), + swarm_event_tx: self.swarm_event_tx.clone(), + mcp_pool: Arc::clone(&self.mcp_pool), + soft_interrupt_queues: Arc::clone(&self.soft_interrupt_queues), + swarm_mutation_runtime: self.swarm_mutation_runtime.clone(), + client_connections: Arc::clone(&self.client_connections), + }); + lane_dispatch::spawn_startup_scan(Duration::from_secs(5)); // Every resume History asks whether the session has persisted // background tasks. The first ask after a reload scans the whole task // directory (~250ms) while clients are reconnecting; build the index @@ -1398,6 +1415,7 @@ impl Server { crate::mobile_sync::spawn_background_sync(); // On Jcode Cloud machines, run sessions the phone starts or continues. crate::mobile_runner::spawn_cloud_runner(); + Self::spawn_cloud_activity_keepalive(Arc::clone(&self.swarm_state.members)); // Initialize the memory agent early so it's ready for all sessions if crate::config::config().features.memory { @@ -1921,6 +1939,25 @@ impl Server { }); } + /// On Jcode Cloud the host powers off after 30 minutes without an SSH + /// client unless the busy marker is fresh. Turns keep running after the + /// client disconnects, so mark the host busy while any session runs. + fn spawn_cloud_activity_keepalive(swarm_members: Arc>>) { + if !crate::mobile_runner::is_cloud_machine() { + return; + } + tokio::spawn(async move { + let mut interval = tokio::time::interval(Duration::from_secs(60)); + interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + loop { + interval.tick().await; + if Self::any_session_streaming(&swarm_members).await { + crate::mobile_runner::touch_cloud_activity_marker(); + } + } + }); + } + /// Whether at least one session is currently in the "running" state, i.e. /// actively streaming/processing a turn. This is the same signal that drives /// the Waybar "N streaming" indicator. @@ -2297,6 +2334,9 @@ impl Server { // swarm strip so a coordinator can see each managed agent's work. BusEvent::TodoUpdated(event) => { dispatch_swarm_todo_progress(&event, &swarm_members, &swarms_by_id).await; + // Worker-owned lanes whose dependencies are met get a + // worker automatically; no launch call is involved. + self::lane_dispatch::on_todos_saved(&event.session_id, &event.todos); // A coordinator's todo list is its lane plan: keep a // running plan mirroring every save, so new or reshaped // lanes are picked up without relaunching. @@ -2345,6 +2385,10 @@ impl Server { #[cfg(unix)] let _daemon_lock = acquire_daemon_lock_for(&self.socket_path)?; + // Reap zombies and stop orphaned MCP servers left by a previous image + // of this PID (exec reload). Must run before this image spawns children. + crate::inherited_children::start(); + if socket_has_live_listener(&self.socket_path).await { anyhow::bail!( "Refusing to replace active server socket at {}", diff --git a/crates/jcode-app-core/src/server/account_usage.rs b/crates/jcode-app-core/src/server/account_usage.rs index 66d1180fb90..f6d3b8b0768 100644 --- a/crates/jcode-app-core/src/server/account_usage.rs +++ b/crates/jcode-app-core/src/server/account_usage.rs @@ -56,8 +56,19 @@ async fn publish_startup_snapshot(snapshot: &Snapshot) { /// Queue an asynchronous response. The shared mutex covers the entire worker, /// so simultaneous clients coalesce into one fetch and errors get a retry floor. pub(super) fn request(id: u64, tx: mpsc::UnboundedSender) { + request_with_refresh(id, tx, false); +} + +/// Re-enumerate freshly saved credentials without clearing endpoint caches or +/// provider Retry-After deadlines. Publish directly so the login's client does +/// not have to wait for its next periodic usage poll. +pub(super) fn request_after_auth_change(id: u64, tx: mpsc::UnboundedSender) { + request_with_refresh(id, tx, true); +} + +fn request_with_refresh(id: u64, tx: mpsc::UnboundedSender, force: bool) { tokio::spawn(async move { - let snapshot = snapshot_with(STATE.get_or_init(Mutex::default), fetch).await; + let snapshot = snapshot_with(STATE.get_or_init(Mutex::default), fetch, force).await; let _ = tx.send(ServerEvent::AccountUsage { id, anthropic: snapshot.anthropic, @@ -66,13 +77,13 @@ pub(super) fn request(id: u64, tx: mpsc::UnboundedSender) { }); } -async fn snapshot_with(state: &Mutex, fetch: F) -> Snapshot +async fn snapshot_with(state: &Mutex, fetch: F, force: bool) -> Snapshot where F: FnOnce() -> Fut, Fut: Future, { let mut state = state.lock().await; - if state.due(Instant::now()) { + if force || state.due(Instant::now()) { match tokio::time::timeout(Duration::from_secs(120), fetch()).await { Ok(snapshot) => { state.snapshot = snapshot; @@ -140,7 +151,10 @@ fn wire_snapshot( || lower.contains("rate limit") || lower.contains("too many requests") { - "Couldn't fetch usage (429: lookup throttled)".to_string() + match crate::usage::usage_error_retry_at(&error) { + Some(at) => format!("Couldn't fetch usage (429: lookup throttled) (retry at {at})"), + None => "Couldn't fetch usage (429: lookup throttled)".to_string(), + } } else if lower.contains("401") || lower.contains("unauthorized") { "Usage authentication failed (401)".to_string() } else { @@ -149,6 +163,11 @@ fn wire_snapshot( }); let failed = error.is_some(); AccountUsageSnapshot { + additional_windows: if !failed || preserve_last_known { + value.additional_windows + } else { + Vec::new() + }, label: value.label, email: value.email, exhausted: !failed && value.exhausted, @@ -178,6 +197,7 @@ mod tests { fn account(error: Option<&str>) -> crate::usage::AccountUsageSnapshot { crate::usage::AccountUsageSnapshot { + additional_windows: Vec::new(), label: "test".into(), email: None, exhausted: true, @@ -208,6 +228,10 @@ mod tests { "Usage authentication failed (401)", ), ("secret transport diagnostic", "Usage unavailable"), + ( + "HTTP 429 secret (retry at 2026-01-01T00:00:00Z)", + "Couldn't fetch usage (429: lookup throttled) (retry at 2026-01-01T00:00:00Z)", + ), ] { let account = wire_snapshot(account(Some(raw)), true); assert_eq!(account.error.as_deref(), Some(safe)); @@ -223,18 +247,27 @@ mod tests { #[test] fn stale_wire_values_remain_display_only_and_unknown_stays_unknown() { let mut known = account(Some("HTTP 429 secret diagnostics")); + known + .additional_windows + .push(crate::protocol::AccountUsageWindow { + label: "Fable 7d".into(), + usage_ratio: 0.8, + resets_at: None, + }); known.seven_day_ratio = Some(0.42); let stale = wire_snapshot(known.clone(), true); assert_eq!(stale.five_hour_ratio, Some(1.0)); assert_eq!(stale.seven_day_ratio, Some(0.42)); assert!(stale.error.is_some()); assert!(!stale.exhausted); + assert_eq!(stale.additional_windows, known.additional_windows); // OpenAI has no equivalent provenance gate yet, so keep its failed // wire snapshots unknown rather than broadening this Anthropic fix. let unsupported = wire_snapshot(known.clone(), false); assert!(unsupported.five_hour_ratio.is_none()); assert!(unsupported.seven_day_ratio.is_none()); + assert!(unsupported.additional_windows.is_empty()); known.five_hour_ratio = None; known.seven_day_ratio = None; @@ -245,6 +278,44 @@ mod tests { assert!(!unknown.exhausted); } + #[tokio::test] + async fn auth_change_replaces_fresh_login_error_without_waiting_for_poll_deadline() { + let state = Mutex::new(State { + snapshot: Snapshot { + anthropic: vec![wire_snapshot(account(Some("HTTP 401")), true)], + ..Default::default() + }, + next_fetch: Some(Instant::now() + REFRESH), + }); + let cached = snapshot_with( + &state, + || async { panic!("ordinary polls must reuse the fresh daemon snapshot") }, + false, + ) + .await; + assert!(cached.anthropic[0].error.is_some()); + let refreshed = snapshot_with( + &state, + || async { + Snapshot { + anthropic: vec![wire_snapshot(account(None), true)], + ..Default::default() + } + }, + true, + ) + .await; + assert!(refreshed.anthropic[0].error.is_none()); + assert!(state.lock().await.next_fetch.unwrap() > Instant::now()); + let cached = snapshot_with( + &state, + || async { panic!("the auth refresh must restore ordinary polling coalescing") }, + false, + ) + .await; + assert_eq!(cached.anthropic, refreshed.anthropic); + } + #[tokio::test] async fn simultaneous_clients_coalesce_and_receive_same_snapshot() { let state = Mutex::new(State::default()); @@ -257,13 +328,15 @@ mod tests { ..Default::default() } }; - let (first, second) = - tokio::join!(snapshot_with(&state, worker), snapshot_with(&state, worker)); + let (first, second) = tokio::join!( + snapshot_with(&state, worker, false), + snapshot_with(&state, worker, false) + ); assert_eq!(calls.load(Ordering::SeqCst), 1); assert_eq!(first.openai, second.openai); assert_eq!(first.openai[0].label, "test"); state.lock().await.next_fetch = Some(Instant::now() - Duration::from_secs(1)); - let refreshed = snapshot_with(&state, worker).await; + let refreshed = snapshot_with(&state, worker, false).await; assert_eq!(calls.load(Ordering::SeqCst), 2); assert_eq!(refreshed.openai, first.openai); } diff --git a/crates/jcode-app-core/src/server/available_models_cache.rs b/crates/jcode-app-core/src/server/available_models_cache.rs index 034f77b5c43..d26fd0294ae 100644 --- a/crates/jcode-app-core/src/server/available_models_cache.rs +++ b/crates/jcode-app-core/src/server/available_models_cache.rs @@ -152,12 +152,11 @@ fn provider_catalog_snapshot(provider: &dyn Provider) -> ModelCatalogSnapshot { .explicit_provider_pin_for_current_model() .map(|pin| format!("{model}@{pin}")) .unwrap_or(model); - let mut snapshot = ModelCatalogSnapshot::new( - Some(provider.display_name()), - Some(model), - provider.available_models_display(), - provider.model_routes(), - ); + let mut snapshot = ModelCatalogSnapshot::from_provider(provider); + // Server selection metadata includes explicit upstream pins, independently + // of how a provider obtains its shared catalog contents. + snapshot.provider_name = Some(provider.display_name()); + snapshot.provider_model = Some(model); crate::model_usage::enrich_routes(&mut snapshot.model_routes); snapshot } diff --git a/crates/jcode-app-core/src/server/client_disconnect_cleanup.rs b/crates/jcode-app-core/src/server/client_disconnect_cleanup.rs index 478b39105ec..fe70f8a5358 100644 --- a/crates/jcode-app-core/src/server/client_disconnect_cleanup.rs +++ b/crates/jcode-app-core/src/server/client_disconnect_cleanup.rs @@ -154,6 +154,9 @@ pub(super) async fn cleanup_client_connection( } else { false }; + if disposition == DisconnectDisposition::Closed { + crate::agents_status::session_closed(client_session_id); + } let originally_registered = swarm_members.read().await.contains_key(client_session_id); record_transport_closed(client_session_id, client_connection_id, client_connections).await; diff --git a/crates/jcode-app-core/src/server/client_lifecycle.rs b/crates/jcode-app-core/src/server/client_lifecycle.rs index eea74dc6634..1f124faceeb 100644 --- a/crates/jcode-app-core/src/server/client_lifecycle.rs +++ b/crates/jcode-app-core/src/server/client_lifecycle.rs @@ -395,6 +395,8 @@ pub(super) async fn handle_client( let mut continue_on_disconnect = false; let mut model_usage_updates_enabled = false; let mut supports_pdf_panels = false; + // Keep only the window preference, never a session-specific prefix fingerprint. + let mut recent_history_messages = None; let mut supports_user_response = false; let mut supports_elicitation_form = false; let mut applet_elicitation = false; @@ -845,7 +847,24 @@ pub(super) async fn handle_client( let _request_activity = if matches!(&request, Request::UpgradeRuntime { .. }) { None } else { - match crate::runtime_activity::global().try_enter() { + // Requests that keep this connection's session identity are owned + // by that session, so they never block transfers of unrelated + // sessions. Identity-changing requests stay globally owned. + let identity_change = !client_subscribed + || matches!( + &request, + Request::Subscribe { .. } + | Request::ResumeSession { .. } + | Request::ResumeAllSessions { .. } + | Request::Clear { .. } + ); + let gate = crate::runtime_activity::global(); + let activity = if identity_change || client_session_id.trim().is_empty() { + gate.try_enter() + } else { + gate.try_enter_session(&client_session_id) + }; + match activity { Ok(activity) => Some(activity), Err(error) => { let _ = client_event_tx.send(ServerEvent::Error { @@ -1536,6 +1555,8 @@ pub(super) async fn handle_client( } Request::PrepareDisconnect { id, preserve_work } => { + // Connection-scoped until the next Subscribe snapshot: session + // switches on this transport retain this disconnect preference. if preserve_work { continue_on_disconnect = true; } @@ -1625,6 +1646,7 @@ pub(super) async fn handle_client( // rather than retaining a prior pane's values. continue_on_disconnect = requested_continuation; supports_pdf_panels = requested_pdf_panels; + recent_history_messages = history_base.and_then(|base| base.recent_messages); supports_user_response = requested_user_response; supports_elicitation_form = requested_elicitation_form; applet_elicitation = requested_applet_elicitation; @@ -1840,7 +1862,7 @@ pub(super) async fn handle_client( None, supports_pdf_panels, supports_turn_identity, - None, + recent_history_messages.map(crate::protocol::HistoryBase::recent), ) .await .is_err() @@ -1874,6 +1896,7 @@ pub(super) async fn handle_client( Request::GetCompactedHistory { id, visible_messages, + recent_history, } => { if handle_get_compacted_history( id, @@ -1881,6 +1904,7 @@ pub(super) async fn handle_client( &agent, &writer, visible_messages, + recent_history, ) .await .is_err() @@ -1941,7 +1965,7 @@ pub(super) async fn handle_client( resume_working_dir.as_deref(), client_instance_id.as_deref(), client_has_local_history, - None, + recent_history_messages.map(crate::protocol::HistoryBase::recent), allow_session_takeover, &mut client_selfdev, &mut client_session_id, @@ -2631,6 +2655,23 @@ pub(super) async fn handle_client( } } + Request::VoiceActivity { + id, + session_id, + activity, + } => { + let event = super::debug::forward_voice_activity( + id, + &session_id, + activity, + &swarm_members, + ) + .await; + if let Err(error) = client_event_tx.send(event) { + crate::logging::debug(&format!("client disconnected before voice activity reply: {error}")); + } + } + Request::InputShell { id, command } => { handle_input_shell(id, command, &agent, &client_event_tx); } diff --git a/crates/jcode-app-core/src/server/client_lifecycle_socket_tests.rs b/crates/jcode-app-core/src/server/client_lifecycle_socket_tests.rs index 75fe4af8d65..ae553f9db60 100644 --- a/crates/jcode-app-core/src/server/client_lifecycle_socket_tests.rs +++ b/crates/jcode-app-core/src/server/client_lifecycle_socket_tests.rs @@ -714,6 +714,24 @@ async fn sdk_socket_cancel_workflow( // An existing session, not an empty panel: empty untitled sessions are // never written to disk and get a 30s reconnect grace on disconnect. other_session.title = Some("sdk other session".into()); + for index in 0..8 { + other_session.append_stored_message(crate::session::StoredMessage { + id: format!("history-{index}"), + role: if index % 2 == 0 { + crate::message::Role::User + } else { + crate::message::Role::Assistant + }, + content: vec![ContentBlock::Text { + text: format!("history {index}"), + cache_control: None, + }], + display_role: None, + timestamp: None, + tool_duration_ms: None, + token_usage: None, + }); + } other_session.save().unwrap(); assert!(crate::session::session_exists("sdk_other_session")); let other_registry = Registry::new(provider_template.clone()).await; @@ -726,6 +744,68 @@ async fn sdk_socket_cancel_workflow( None, ))), ); + if identity_capability && terminal_race { + // Exercise actual dispatch, not just the history handlers. A subsequent + // Subscribe is authoritative and clears an earlier opt-in for legacy clients. + let (history_task, history_stream) = connect(); + let (reader, mut writer) = history_stream.into_split(); + let mut reader = BufReader::new(reader); + for recent in [None, Some(2), None, Some(3)] { + let mut subscribe = + json!({"type":"subscribe","id":101,"working_dir":home._home.path()}); + if let Some(size) = recent { + // Deliberately stale prefix state must not leak into later requests. + subscribe["history_base"] = + json!({"count":4,"fingerprint":123,"recent_messages":size}); + } + send(&mut writer, subscribe).await; + until(&mut reader, |e| matches!(e, ServerEvent::Done { id: 101 })).await; + send( + &mut writer, + json!({"type":"resume_session","id":102,"session_id":"sdk_other_session"}), + ) + .await; + let event = until(&mut reader, |e| { + matches!(e, ServerEvent::History { id: 102, .. }) + }) + .await; + let expected = recent.map_or(8, |size| size + 1); + let ServerEvent::History { + messages, + messages_base_count, + .. + } = event + else { + unreachable!() + }; + assert_eq!(messages.len(), expected, "resume recent={recent:?}"); + assert_eq!(messages_base_count, None); + assert_eq!(messages.last().unwrap().content, "history 7"); + until(&mut reader, |e| matches!(e, ServerEvent::Done { id: 102 })).await; + send(&mut writer, json!({"type":"get_history","id":103})).await; + let event = until(&mut reader, |e| { + matches!(e, ServerEvent::History { id: 103, .. }) + }) + .await; + let ServerEvent::History { + messages, + messages_base_count, + .. + } = event + else { + unreachable!() + }; + assert_eq!(messages.len(), expected, "get_history recent={recent:?}"); + assert_eq!(messages_base_count, None); + } + drop(writer); + drop(reader); + tokio::time::timeout(Duration::from_secs(10), history_task) + .await + .expect("history client disconnect") + .unwrap() + .unwrap(); + } send( &mut observer_writer, json!({"type":"resume_session","id":23,"session_id":"sdk_other_session"}), 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 647ff387e47..9183e3b6210 100644 --- a/crates/jcode-app-core/src/server/client_lightweight_control.rs +++ b/crates/jcode-app-core/src/server/client_lightweight_control.rs @@ -286,7 +286,14 @@ pub(super) async fn handle_lightweight_control_request( return Ok(()); } - let _request_activity = crate::runtime_activity::global().try_enter()?; + // Comm requests name their owning session. Scope admission to it so a + // long-lived request (await_members can hold for an hour) only blocks + // transfers of that session, not every /cloud move on the runtime. + let gate = crate::runtime_activity::global(); + let _request_activity = match comm_request_session_id(&request) { + Some(session) if !session.trim().is_empty() => gate.try_enter_session(session)?, + _ => gate.try_enter()?, + }; write_direct_event(&writer, &ServerEvent::Ack { id: request.id() }).await?; let (client_event_tx, mut client_event_rx) = mpsc::unbounded_channel::(); @@ -337,6 +344,19 @@ pub(super) async fn handle_lightweight_control_request( ) .await; } + Request::NotifyAuthChanged { + id, provider, auth, .. + } => { + super::provider_control::handle_unbound_notify_auth_changed( + id, + provider, + auth, + provider_template, + sessions, + &client_event_tx, + ) + .await; + } Request::NotifySession { id, session_id, diff --git a/crates/jcode-app-core/src/server/client_processing.rs b/crates/jcode-app-core/src/server/client_processing.rs index f0956700108..fbe1f32594f 100644 --- a/crates/jcode-app-core/src/server/client_processing.rs +++ b/crates/jcode-app-core/src/server/client_processing.rs @@ -164,6 +164,33 @@ pub(super) async fn append_context_message( } } +/// A send whose reload continuation reminder was already delivered is a pure +/// replay only when it carries no user content. A queued follow-up combined +/// with that reminder must still run (only the reminder is dropped). +fn delivered_replay_is_reminder_only(content: &str, image_count: usize) -> bool { + content.trim().is_empty() && image_count == 0 +} + +#[cfg(test)] +mod delivered_replay_tests { + use super::delivered_replay_is_reminder_only; + + #[test] + fn reminder_only_replay_is_suppressed() { + assert!(delivered_replay_is_reminder_only("", 0)); + assert!(delivered_replay_is_reminder_only(" \n", 0)); + } + + #[test] + fn queued_followup_with_delivered_reminder_is_not_suppressed() { + assert!(!delivered_replay_is_reminder_only( + "please also fix the tests", + 0 + )); + assert!(!delivered_replay_is_reminder_only("", 1)); + } +} + #[allow(clippy::too_many_arguments)] pub(super) async fn start_processing_message( message: ProcessingMessage, @@ -179,7 +206,7 @@ pub(super) async fn start_processing_message( id, content, images, - system_reminder, + mut system_reminder, active_skill, server_failover, client_instance_id, @@ -222,23 +249,45 @@ pub(super) async fn start_processing_message( callback(); } }); - if let Some(reminder) = system_reminder.as_deref() { - match super::super::reload_recovery::is_delivered_continuation(client_session_id, reminder) - { - Ok(true) => { - crate::logging::info(&format!( - "Suppressing delivered reload recovery replay session={} id={}", - client_session_id, id - )); - let _ = client_event_tx.send(ServerEvent::Done { id }); - return; + let reminder_already_delivered = match system_reminder.as_deref() { + Some(reminder) => { + match super::super::reload_recovery::is_delivered_continuation( + client_session_id, + reminder, + ) { + Ok(delivered) => delivered, + Err(error) => { + crate::logging::warn(&format!( + "Failed to inspect reload recovery replay session={}: {}", + client_session_id, error + )); + false + } } - Ok(false) => {} - Err(error) => crate::logging::warn(&format!( - "Failed to inspect reload recovery replay session={}: {}", - client_session_id, error - )), } + None => false, + }; + if reminder_already_delivered { + if delivered_replay_is_reminder_only(&content, images.len()) { + crate::logging::info(&format!( + "Suppressing delivered reload recovery replay session={} id={}", + client_session_id, id + )); + let _ = client_event_tx.send(ServerEvent::Done { id }); + return; + } + // After a reload the client combines a queued user follow-up with the + // reload continuation reminder into one send. Only the reminder is a + // replay. Suppressing the whole message silently dropped the user's + // queued prompt, so strip the reminder and run the real content. + crate::logging::info(&format!( + "Stripping delivered reload recovery reminder but keeping queued content session={} id={} content_chars={} images={}", + client_session_id, + id, + content.chars().count(), + images.len() + )); + system_reminder = None; } if !agent_guard.set_remote_active_skill(active_skill.clone()) { let skill_name = active_skill.as_deref().unwrap_or_default(); @@ -262,9 +311,11 @@ pub(super) async fn start_processing_message( client_session_id, id, error )); } + let status_title = agent_guard.session_display_title_or_name(); drop(agent_guard); *state.client_is_processing = true; + crate::agents_status::turn_started(client_session_id, &status_title); *state.message_id = Some(id); *state.session_id = Some(client_session_id.to_string()); @@ -358,13 +409,24 @@ pub(super) async fn start_processing_message( id, error )), } - let (completion_report, push_title) = { + let (completion_report, push_body, push_title) = { let agent = report_agent.lock().await; ( agent.recent_assistant_output_tail_after(start_message_index), + agent + .final_assistant_text_after(start_message_index) + .unwrap_or_default(), agent.session_display_title_or_name(), ) }; + crate::agents_status::turn_finished( + &push_session_id, + result + .as_ref() + .err() + .map(crate::util::format_error_chain) + .as_deref(), + ); // Mobile push: fire-and-forget, skipped for user cancellation and for // swarm worker sessions (their coordinator reports instead). if !owned_worker && !crate::mobile_push::take_cancelled(&push_session_id) { @@ -379,11 +441,17 @@ pub(super) async fn start_processing_message( crate::mobile_push::PushKind::TurnDone, &push_session_id, Some(&push_title), - completion_report.as_deref().unwrap_or_default(), + &push_body, Some(id), ); } - Ok(()) => {} + Ok(()) => crate::mobile_push::note_skipped( + crate::mobile_push::PushKind::TurnDone, + &format!( + "turn too short ({}s) for a non-iOS session", + turn_started.elapsed().as_secs() + ), + ), Err(error) => crate::mobile_push::notify( crate::mobile_push::PushKind::TurnError, &push_session_id, diff --git a/crates/jcode-app-core/src/server/client_session.rs b/crates/jcode-app-core/src/server/client_session.rs index 86eb1034144..f531f4fbf6d 100644 --- a/crates/jcode-app-core/src/server/client_session.rs +++ b/crates/jcode-app-core/src/server/client_session.rs @@ -1,6 +1,6 @@ #![cfg_attr(test, allow(clippy::await_holding_lock))] -use super::client_state::{handle_get_history, spawn_model_prefetch_update}; +use super::client_state::{handle_get_history, send_resume_todos, spawn_model_prefetch_update}; use super::swarm::notify_owner_of_unrestored_idle_workers; use super::{ ClientConnectionInfo, ClientDebugState, FileTouchService, SessionInterruptQueues, SwarmEvent, @@ -1498,6 +1498,7 @@ pub(super) async fn handle_resume_session( } *client_session_id = session_id.clone(); + send_resume_todos(&session_id, writer).await?; handle_get_history( id, &session_id, @@ -1970,23 +1971,27 @@ pub(super) async fn handle_resume_session( )); } let pre_history_ms = resume_start.elapsed().as_millis(); - let history_result = handle_get_history( - id, - &session_id, - false, - agent, - provider, - sessions, - client_connections, - client_count, - writer, - server_name, - server_icon, - Some(was_interrupted), - supports_pdf_panels, - supports_turn_identity, - history_base, - ) + let history_result = async { + send_resume_todos(&session_id, writer).await?; + handle_get_history( + id, + &session_id, + false, + agent, + provider, + sessions, + client_connections, + client_count, + writer, + server_name, + server_icon, + Some(was_interrupted), + supports_pdf_panels, + supports_turn_identity, + history_base, + ) + .await + } .await; // Persist even if history delivery failed, but do not make inbound // controls wait for a recovery turn that already owns the agent. diff --git a/crates/jcode-app-core/src/server/client_session_tests/resume/busy_existing_attach.rs b/crates/jcode-app-core/src/server/client_session_tests/resume/busy_existing_attach.rs index c11689c8222..e588177f346 100644 --- a/crates/jcode-app-core/src/server/client_session_tests/resume/busy_existing_attach.rs +++ b/crates/jcode-app-core/src/server/client_session_tests/resume/busy_existing_attach.rs @@ -217,6 +217,27 @@ async fn handle_resume_session_allows_live_attach_when_existing_agent_is_busy() .await .expect("history should be written promptly")?; let event: ServerEvent = serde_json::from_str(line.trim())?; + match event { + ServerEvent::Todos { + session_id, + payload, + } => { + assert_eq!(session_id, target_session_id); + let payload: serde_json::Value = serde_json::from_str(&payload)?; + assert!(payload["todos"].is_array()); + } + other => { + panic!("authoritative todos must precede the potentially huge History, got {other:?}") + } + } + line.clear(); + tokio::time::timeout( + std::time::Duration::from_secs(1), + tokio::io::AsyncBufReadExt::read_line(&mut peer_reader, &mut line), + ) + .await + .expect("history follows the early todo snapshot")?; + let event: ServerEvent = serde_json::from_str(line.trim())?; match event { ServerEvent::History { session_id, diff --git a/crates/jcode-app-core/src/server/client_state.rs b/crates/jcode-app-core/src/server/client_state.rs index 180d6479687..08ac09fc7cd 100644 --- a/crates/jcode-app-core/src/server/client_state.rs +++ b/crates/jcode-app-core/src/server/client_state.rs @@ -25,6 +25,11 @@ use tokio::sync::{Mutex, RwLock}; const ATTACH_MODEL_PREFETCH_DEBOUNCE_SECS: u64 = 15; const RELOAD_RESTORE_MARKER_MAX_AGE: Duration = Duration::from_secs(60); +#[path = "history_timing.rs"] +mod history_timing; +use history_timing::Timing; +pub(super) use history_timing::send_resume_todos; + /// Do not probe any identity until telemetry is enabled. Only the provider /// may identify its effective OAuth credential. An API-key route must not expose the email /// of an unrelated saved subscription account. @@ -183,6 +188,7 @@ pub(super) async fn handle_get_history( supports_turn_identity: bool, history_base: Option, ) -> Result<()> { + let mut timing = Timing::new("history", Some(id), client_session_id); let history_start = Instant::now(); let activity = session_activity_snapshot(client_connections, client_session_id, client_is_processing) @@ -196,7 +202,8 @@ pub(super) async fn handle_get_history( "handle_get_history: session {} busy, falling back to persisted remote-startup snapshot", client_session_id )); - send_history_from_persisted_session( + timing.stage("persisted"); + let result = send_history_from_persisted_session( id, client_session_id, provider, @@ -211,7 +218,9 @@ pub(super) async fn handle_get_history( supports_turn_identity, history_base, ) - .await?; + .await; + timing.result(&result); + result?; crate::logging::info(&format!( "[TIMING] handle_get_history: session={}, persisted_fallback total={}ms", client_session_id, @@ -220,7 +229,8 @@ pub(super) async fn handle_get_history( return Ok(()); }; - send_history_with_guard( + timing.stage("live"); + let result = send_history_with_guard( id, client_session_id, agent_guard, @@ -237,7 +247,9 @@ pub(super) async fn handle_get_history( supports_turn_identity, history_base, ) - .await?; + .await; + timing.result(&result); + result?; let send_history_ms = history_start.elapsed().as_millis(); let prefetch_start = Instant::now(); @@ -492,71 +504,116 @@ pub(super) async fn handle_get_model_catalog( Ok(()) } -pub(super) async fn handle_get_compacted_history( - id: u64, - session_id: &str, - agent: &Arc>, - writer: &Arc>, - visible_messages: usize, -) -> Result<()> { - let started = Instant::now(); - let (messages, images, compacted_info, source) = match agent.try_lock() { - Ok(agent_guard) => { - let (messages, images, info) = agent_guard - .get_history_and_rendered_images_with_compacted_history(visible_messages); - (messages, images, info, "live") - } - Err(_) => { - let session = crate::session::Session::load_for_remote_startup(session_id) - .or_else(|_| crate::session::Session::load_startup_stub(session_id))?; - let (rendered_messages, images, info) = - crate::session::render_messages_and_images_with_compacted_history( - &session, - visible_messages, - ); - ( - rendered_messages - .into_iter() - .map(rendered_to_history_message) - .collect(), - images, - info, - "persisted", - ) - } - }; +#[path = "history_paging.rs"] +mod history_paging; +pub(super) use history_paging::handle_get_compacted_history; - let compacted_info = compacted_info.unwrap_or(crate::session::RenderedCompactedHistoryInfo { - total_messages: 0, - visible_messages: 0, - remaining_messages: 0, - hidden_user_prompts: 0, +/// Window canonical rendered entries, not raw provider messages. Images follow +/// their rendered entry and the synthetic marker occupies its own index. +pub(super) fn recent_history_window( + mut messages: Vec, + images: Vec, + visible: usize, +) -> ( + Vec, + Vec, + crate::session::RenderedCompactedHistoryInfo, +) { + let total = messages.len(); + let start = total.saturating_sub(visible.max(1)); + let archived_prompts = messages + .iter() + .filter(|message| { + message.role == "system" + && message + .content + .starts_with("Earlier conversation compacted - ") + }) + .filter_map(|message| { + message + .content + .split("Hidden user prompts: ") + .nth(1)? + .split('.') + .next()? + .parse::() + .ok() + }) + .sum::(); + let is_prompt = |message: &&HistoryMessage| { + message.role == "user" && !crate::session::is_attached_image_label_text(&message.content) + }; + let excluded_prompts = messages[..start].iter().filter(is_prompt).count(); + let hidden_user_prompts = archived_prompts + excluded_prompts; + let info = crate::session::RenderedCompactedHistoryInfo { + total_messages: total, + visible_messages: total - start, + remaining_messages: start, + hidden_user_prompts, + }; + if start == 0 { + return (messages, images, info); + } + messages = messages.split_off(start); + messages.insert(0, HistoryMessage { + stored_index: None, + response_stats: None, + role: "system".to_owned(), + content: format!("{}{start} older historical messages hidden. Showing {} of {total} messages. Scroll to the top to load more. Hidden user prompts: {hidden_user_prompts}.", crate::protocol::RECENT_HISTORY_MARKER_PREFIX, info.visible_messages), + tool_calls: None, + tool_data: None, }); - crate::logging::info(&format!( - "[TIMING] get_compacted_history: session={}, source={}, requested={}, visible={}, remaining={}, messages={}, total={}ms", - session_id, - source, - visible_messages, - compacted_info.visible_messages, - compacted_info.remaining_messages, - messages.len(), - started.elapsed().as_millis(), - )); + let visible_prompts = messages.iter().filter(is_prompt).count(); + let images = images + .into_iter() + .filter_map(|mut image| { + match &mut image.anchor { + Some(crate::session::RenderedImageAnchor::UserPrompt { ordinal }) => { + *ordinal = ordinal.checked_sub(excluded_prompts)?; + if *ordinal >= visible_prompts { + return None; + } + } + Some(crate::session::RenderedImageAnchor::ToolCall { id }) => { + if !messages.iter().any(|message| { + message + .tool_data + .as_ref() + .is_some_and(|tool| &tool.id == id) + }) { + return None; + } + } + None if image.history_message_index.is_none() => return None, + None => {} + } + if let Some(index) = image.history_message_index { + if index < start || index > total { + return None; + } + image.history_message_index = Some(index - start + 1); + } + Some(image) + }) + .collect(); + (messages, images, info) +} - write_event( - writer, - &ServerEvent::CompactedHistory { - id, - session_id: session_id.to_string(), - messages, - images, - compacted_total: compacted_info.total_messages, - compacted_visible: compacted_info.visible_messages, - compacted_remaining: compacted_info.remaining_messages, - compacted_hidden_prompts: compacted_info.hidden_user_prompts, - }, - ) - .await +fn apply_recent_history_base( + messages: Vec, + images: Vec, + history_base: Option, + messages_base_count: Option, +) -> (Vec, Vec) { + if messages_base_count.is_none() + && let Some(visible) = history_base + .filter(|base| base.count == 0) + .and_then(|base| base.recent_messages) + { + let (messages, images, _) = recent_history_window(messages, images, visible); + return (messages, images); + } + (messages, images) } /// Trim History to the messages after the client's verified prefix. @@ -781,6 +838,7 @@ async fn send_history_from_persisted_session( supports_turn_identity: bool, history_base: Option, ) -> Result<()> { + let mut timing = Timing::new("persisted_history", Some(id), session_id); let loaded = super::run_blocking_io(|| { crate::session::Session::load_for_remote_startup(session_id) .or_else(|_| crate::session::Session::load_startup_stub(session_id)) @@ -801,8 +859,12 @@ async fn send_history_from_persisted_session( // Do not save this synthetic snapshot or mask other I/O errors. Session::create_with_id(session_id.to_string(), None, None) } - Err(error) => return Err(error), + Err(error) => { + timing.result(&Err::<(), _>(())); + return Err(error); + } }; + timing.stage("render"); let token_usage_totals = session.token_usage_totals(); let (rendered_messages, images) = crate::session::render_messages_and_images(&session); // Extract the small metadata fields we need, then drop the full Session @@ -836,6 +898,8 @@ async fn send_history_from_persisted_session( .map(rendered_to_history_message) .collect(); let (messages, messages_base_count) = apply_history_base(messages, history_base); + let (messages, images) = + apply_recent_history_base(messages, images, history_base, messages_base_count); let (images, images_unchanged) = apply_history_images_base( images, history_base.filter(|_| messages_base_count.is_some()), @@ -907,15 +971,23 @@ async fn send_history_from_persisted_session( applets: crate::applets::snapshot_for_session(session_id).unwrap_or_default(), }; + timing.stage("encode"); let json = encode_event(&history_event); + timing.stage("writer_lock"); let mut writer_guard = writer.lock().await; + timing.stage("write"); if supports_turn_identity && let Some(state) = history_turn_state(sessions, session_id) { - writer_guard + let sent = writer_guard .write_all(encode_event(&state).as_bytes()) - .await?; + .await; + if sent.is_err() { + timing.result(&sent); + } + sent?; } - writer_guard.write_all(json.as_bytes()).await?; - Ok(()) + let result = writer_guard.write_all(json.as_bytes()).await; + timing.result(&result); + result.map_err(Into::into) } /// How long the bootstrap History payload may wait on the model-name catalog. @@ -969,6 +1041,7 @@ async fn send_history_with_guard( supports_turn_identity: bool, history_base: Option, ) -> Result<()> { + let mut timing = Timing::new("live_history", Some(id), session_id); let history_start = Instant::now(); let catalog_provider = include_model_catalog.then(|| agent_guard.provider_handle()); let ( @@ -1178,6 +1251,8 @@ async fn send_history_with_guard( ) }); let (messages, messages_base_count) = apply_history_base(messages, history_base); + let (messages, images) = + apply_recent_history_base(messages, images, history_base, messages_base_count); let (images, images_unchanged) = apply_history_images_base( images, history_base.filter(|_| messages_base_count.is_some()), @@ -1226,6 +1301,7 @@ async fn send_history_with_guard( applets, }; let build_ms = build_start.elapsed().as_millis(); + timing.stage("encode"); let encode_start = Instant::now(); let json = encode_event(&history_event); // Free the structured event as soon as the wire bytes exist so only ~1x @@ -1233,16 +1309,23 @@ async fn send_history_with_guard( drop(history_event); let json_len = json.len(); let encode_ms = encode_start.elapsed().as_millis(); + timing.stage("writer_lock"); let writer_lock_start = Instant::now(); let mut writer_guard = writer.lock().await; let writer_lock_ms = writer_lock_start.elapsed().as_millis(); + timing.stage("write"); let write_start = Instant::now(); if supports_turn_identity && let Some(state) = history_turn_state(sessions, session_id) { - writer_guard + let sent = writer_guard .write_all(encode_event(&state).as_bytes()) - .await?; + .await; + if sent.is_err() { + timing.result(&sent); + } + sent?; } let result = writer_guard.write_all(json.as_bytes()).await; + timing.result(&result); drop(writer_guard); // Release the serialized payload before any further work (logging below // only needs the captured length). diff --git a/crates/jcode-app-core/src/server/client_state_tests.rs b/crates/jcode-app-core/src/server/client_state_tests.rs index bdae8d01225..61122705fb1 100644 --- a/crates/jcode-app-core/src/server/client_state_tests.rs +++ b/crates/jcode-app-core/src/server/client_state_tests.rs @@ -11,12 +11,62 @@ use async_trait::async_trait; use std::collections::HashMap; use std::io::BufRead as _; use std::sync::Arc; -use std::time::Instant; +use std::time::{Duration, Instant}; use tokio::io::AsyncReadExt; use tokio::sync::{Mutex, RwLock, mpsc}; struct MockProvider(Option<&'static str>); +#[tokio::test] +async fn resume_todos_arrive_before_backpressured_large_history() { + let _env = crate::storage::lock_test_env(); + let temp_home = tempfile::TempDir::new().unwrap(); + let previous_home = std::env::var_os("JCODE_HOME"); + crate::env::set_var("JCODE_HOME", temp_home.path()); + let session_id = "session_early_todo_backpressure"; + let item: crate::todo::TodoItem = serde_json::from_value(serde_json::json!({ + "id": "resume", "content": "Authoritative remote plan", "status": "in_progress", "priority": "high" + })).unwrap(); + crate::todo::save_todos(session_id, &[item]).unwrap(); + let (stream, peer) = crate::transport::stream_pair().unwrap(); + let (_, write) = stream.into_split(); + let writer = Arc::new(Mutex::new(write)); + let started = Instant::now(); + let transfer = tokio::spawn(async move { + super::send_resume_todos(session_id, &writer).await.unwrap(); + // An unread transcript cannot fit in the socket, reproducing the + // original head-of-line blocking without any model or cloud traffic. + tokio::io::AsyncWriteExt::write_all(&mut *writer.lock().await, &vec![b'x'; 14_000_000]) + .await + }); + let mut reader = tokio::io::BufReader::new(peer); + let mut line = String::new(); + tokio::time::timeout( + Duration::from_secs(1), + tokio::io::AsyncBufReadExt::read_line(&mut reader, &mut line), + ) + .await + .expect("todos must not wait for history transfer") + .unwrap(); + let elapsed = started.elapsed(); + let event: crate::protocol::ServerEvent = serde_json::from_str(&line).unwrap(); + assert!( + matches!(event, crate::protocol::ServerEvent::Todos { session_id: id, payload } + if id == session_id && payload.contains("Authoritative remote plan")) + ); + assert!( + !transfer.is_finished(), + "large transcript should still be blocked while todos are usable" + ); + eprintln!("authoritative todo arrival with 14 MB History blocked: {elapsed:?}"); + transfer.abort(); + let _ = transfer.await; + match previous_home { + Some(home) => crate::env::set_var("JCODE_HOME", home), + None => crate::env::remove_var("JCODE_HOME"), + } +} + #[derive(Clone)] struct StaticDiscoveryProvider(Arc); diff --git a/crates/jcode-app-core/src/server/client_state_tests/history_delta.rs b/crates/jcode-app-core/src/server/client_state_tests/history_delta.rs index 55c975f1178..a3bd4904746 100644 --- a/crates/jcode-app-core/src/server/client_state_tests/history_delta.rs +++ b/crates/jcode-app-core/src/server/client_state_tests/history_delta.rs @@ -25,6 +25,7 @@ fn delta_test_base( .map(crate::protocol::HistoryMessageRef::from), ), images_fingerprint: None, + recent_messages: None, } } @@ -81,6 +82,7 @@ fn history_images_base_omits_only_matching_image_sets() { count: 1, fingerprint: 0, images_fingerprint: Some(fingerprint), + recent_messages: None, }; let matching = crate::protocol::history_images_fingerprint(&images); let (sent, unchanged) = @@ -93,3 +95,277 @@ fn history_images_base_omits_only_matching_image_sets() { let (sent, unchanged) = super::super::apply_history_images_base(images, None); assert!(!unchanged && sent.len() == 2); } + +#[test] +fn recent_history_bounds_seven_thousand_message_bootstrap() { + let messages: Vec<_> = (0..7000) + .map(|index| crate::protocol::HistoryMessage { + stored_index: Some(index), + response_stats: None, + role: if index % 2 == 0 { "user" } else { "assistant" }.to_owned(), + content: format!("{index}: {}", "x".repeat(2000)), + tool_calls: None, + tool_data: None, + }) + .collect(); + let full_bytes = serde_json::to_vec(&messages).unwrap().len(); + let (window, images) = super::super::apply_recent_history_base( + messages, + Vec::new(), + Some(crate::protocol::HistoryBase::recent(128)), + None, + ); + let window_bytes = serde_json::to_vec(&window).unwrap().len(); + assert!(full_bytes > 14_000_000); + assert!(window_bytes < 512 * 1024); + assert_eq!(window.len(), 129); + assert!(images.is_empty()); + assert_eq!( + window[0].content, + "Earlier remote history - 6872 older historical messages hidden. Showing 128 of 7000 messages. Scroll to the top to load more. Hidden user prompts: 3436." + ); + assert!(window[1].content.starts_with("6872:")); + eprintln!("recent history bootstrap: {full_bytes} -> {window_bytes} bytes"); +} + +#[test] +fn recent_history_preserves_archive_prompt_offset_and_rebases_images() { + let mut messages = delta_test_messages(); + messages.insert(0, crate::protocol::HistoryMessage { + stored_index: None, + response_stats: None, role: "system".to_owned(), + content: "Earlier conversation compacted - 10 historical messages hidden from the UI. Scroll to the top to load older history. Hidden user prompts: 7.".to_owned(), + tool_calls: None, tool_data: None, + }); + let image = |ordinal| crate::session::RenderedImage { + media_type: "image/png".to_owned(), + data: "data".to_owned(), + label: None, + source: crate::session::RenderedImageSource::UserInput, + anchor: Some(crate::session::RenderedImageAnchor::UserPrompt { ordinal }), + history_message_index: None, + }; + let (window, images, info) = + super::super::recent_history_window(messages.clone(), vec![image(0), image(1)], 2); + assert_eq!( + ( + info.total_messages, + info.visible_messages, + info.remaining_messages, + info.hidden_user_prompts + ), + (5, 2, 3, 8) + ); + assert_eq!(window.len(), 3); + assert_eq!(images.len(), 1); + assert_eq!( + images[0].anchor, + Some(crate::session::RenderedImageAnchor::UserPrompt { ordinal: 0 }) + ); + let (full, _, info) = super::super::recent_history_window(messages.clone(), Vec::new(), 100); + assert_eq!(info.remaining_messages, 0); + assert_eq!( + serde_json::to_value(&full).unwrap(), + serde_json::to_value(&messages).unwrap() + ); + assert!( + full[0] + .content + .starts_with("Earlier conversation compacted - ") + ); +} + +#[test] +fn recent_history_never_mixes_with_validated_or_stale_prefix() { + let messages = delta_test_messages(); + let mut base = delta_test_base(&messages, 2); + base.recent_messages = Some(1); + let (tail, count) = super::super::apply_history_base(messages.clone(), Some(base)); + let (tail, _) = super::super::apply_recent_history_base(tail, Vec::new(), Some(base), count); + assert_eq!(tail.len(), 2); + base.fingerprint = 0; + let (full, count) = super::super::apply_history_base(messages.clone(), Some(base)); + let (full, _) = super::super::apply_recent_history_base(full, Vec::new(), Some(base), count); + assert_eq!( + serde_json::to_value(&full).unwrap(), + serde_json::to_value(&messages).unwrap() + ); + let (full, _) = + super::super::apply_recent_history_base(messages.clone(), Vec::new(), None, None); + assert_eq!( + serde_json::to_value(&full).unwrap(), + serde_json::to_value(&messages).unwrap() + ); +} + +#[test] +fn recent_history_never_truncates_one_large_message() { + let mut messages = delta_test_messages(); + messages.last_mut().unwrap().content = "x".repeat(1024 * 1024); + let expected = messages.last().unwrap().content.clone(); + let (window, _, _) = super::super::recent_history_window(messages, Vec::new(), 1); + assert_eq!(window[1].content, expected); +} + +#[test] +fn recent_history_scopes_tool_images_and_rebases_append_boundaries() { + let mut messages = delta_test_messages(); + messages[3].role = "tool".to_owned(); + messages[3].tool_data = Some(crate::message::ToolCall { + id: "visible-tool".to_owned(), + name: "read".to_owned(), + ..Default::default() + }); + let image = |id: &str, index| crate::session::RenderedImage { + media_type: "image/png".to_owned(), + data: id.to_owned(), + label: None, + source: crate::session::RenderedImageSource::ToolResult { + tool_name: "read".to_owned(), + }, + anchor: Some(crate::session::RenderedImageAnchor::ToolCall { id: id.to_owned() }), + history_message_index: Some(index), + }; + let (window, images, _) = super::super::recent_history_window( + messages, + vec![image("hidden-tool", 1), image("visible-tool", 4)], + 2, + ); + assert_eq!(images.len(), 1); + assert_eq!(images[0].history_message_index, Some(window.len())); + assert_eq!(images[0].data, "visible-tool"); +} + +#[tokio::test] +#[expect( + clippy::await_holding_lock, + reason = "serialize storage and exercise busy-agent fallback" +)] +async fn recent_history_live_and_busy_bootstrap_and_expansion() { + use super::*; + let _env = crate::storage::lock_test_env(); + let home = tempfile::TempDir::new().unwrap(); + let previous = std::env::var_os("JCODE_HOME"); + crate::env::set_var("JCODE_HOME", home.path()); + let session_id = "session_recent_window_paths"; + let mut session = crate::session::Session::create_with_id(session_id.to_owned(), None, None); + for index in 0..8 { + session.append_stored_message(crate::session::StoredMessage { + id: format!("message-{index}"), + role: if index % 2 == 0 { + crate::message::Role::User + } else { + crate::message::Role::Assistant + }, + content: vec![crate::message::ContentBlock::Text { + text: format!("message {index}"), + cache_control: None, + }], + display_role: None, + timestamp: None, + tool_duration_ms: None, + token_usage: None, + }); + } + let provider: Arc = Arc::new(MockProvider(None)); + let agent = Arc::new(Mutex::new(Agent::new_with_session( + provider.clone(), + Registry::empty(), + session, + None, + ))); + let sessions = Arc::new(crate::server::session_manager::SessionManager::from_agents( + HashMap::from([(session_id.to_owned(), agent.clone())]), + )); + let connections = Arc::new(RwLock::new(HashMap::new())); + let count = Arc::new(RwLock::new(1)); + for busy in [false, true] { + let guard = if busy { Some(agent.lock().await) } else { None }; + for visible in [Some(2), None] { + let (stream, peer) = crate::transport::stream_pair().unwrap(); + let (_, write) = stream.into_split(); + let writer = Arc::new(Mutex::new(write)); + tokio::time::timeout( + Duration::from_secs(2), + super::super::handle_get_history( + 1, + session_id, + busy, + &agent, + &provider, + &sessions, + &connections, + &count, + &writer, + "test", + "", + None, + false, + false, + visible.map(crate::protocol::HistoryBase::recent), + ), + ) + .await + .expect("history must not wait for agent") + .unwrap(); + let mut reader = tokio::io::BufReader::new(peer); + let mut line = String::new(); + tokio::io::AsyncBufReadExt::read_line(&mut reader, &mut line) + .await + .unwrap(); + let event: crate::protocol::ServerEvent = serde_json::from_str(&line).unwrap(); + let crate::protocol::ServerEvent::History { + messages, + messages_base_count, + .. + } = event + else { + panic!("expected History") + }; + assert_eq!(messages_base_count, None); + assert_eq!(messages.len(), if visible.is_some() { 3 } else { 8 }); + assert_eq!(messages.last().unwrap().content, "message 7"); + } + for visible in [4, 100] { + let (stream, peer) = crate::transport::stream_pair().unwrap(); + let (_, write) = stream.into_split(); + let writer = Arc::new(Mutex::new(write)); + tokio::time::timeout( + Duration::from_secs(2), + super::super::handle_get_compacted_history( + 2, session_id, &agent, &writer, visible, true, + ), + ) + .await + .expect("paging must not wait for agent") + .unwrap(); + let mut reader = tokio::io::BufReader::new(peer); + let mut line = String::new(); + tokio::io::AsyncBufReadExt::read_line(&mut reader, &mut line) + .await + .unwrap(); + let event: crate::protocol::ServerEvent = serde_json::from_str(&line).unwrap(); + let crate::protocol::ServerEvent::CompactedHistory { + messages, + compacted_total, + compacted_visible, + compacted_remaining, + compacted_hidden_prompts, + .. + } = event + else { + panic!("expected CompactedHistory") + }; + assert_eq!(compacted_total, 8); + assert_eq!(compacted_visible, visible.min(8)); + assert_eq!(compacted_remaining, 8 - visible.min(8)); + assert_eq!(compacted_hidden_prompts, (8 - visible.min(8)) / 2); + assert_eq!(messages.len(), if visible == 4 { 5 } else { 8 }); + } + drop(guard); + } + match previous { + Some(value) => crate::env::set_var("JCODE_HOME", value), + None => crate::env::remove_var("JCODE_HOME"), + } +} diff --git a/crates/jcode-app-core/src/server/comm_await.rs b/crates/jcode-app-core/src/server/comm_await.rs index 1799cfe82fd..c1bd5d43bd9 100644 --- a/crates/jcode-app-core/src/server/comm_await.rs +++ b/crates/jcode-app-core/src/server/comm_await.rs @@ -188,7 +188,9 @@ async fn finalize_await( members: Vec, summary: String, ) { - let _activity = crate::runtime_activity::global().enter().await; + let _activity = crate::runtime_activity::global() + .enter_session(&state.session_id) + .await; // Deliver with the latest persisted prefs: a duplicate request may have // changed background/notify/wake after the caller captured this copy. let state = refresh_pending_state(state).unwrap_or_else(|| state.clone()); diff --git a/crates/jcode-app-core/src/server/debug.rs b/crates/jcode-app-core/src/server/debug.rs index 00e0f59d893..89d00fe74cd 100644 --- a/crates/jcode-app-core/src/server/debug.rs +++ b/crates/jcode-app-core/src/server/debug.rs @@ -230,13 +230,32 @@ pub(super) async fn inject_transcript( ) .await?; - let delivered = fanout_session_event( - swarm_members, - &session_id, - ServerEvent::Transcript { text, mode }, - ) - .await - > 0; + // Every attached client submits a transcript it receives, so a session + // open in two windows would send it twice. Deliver to the most recently + // active attachment only, falling back to fanout when none is known. + let event = ServerEvent::Transcript { text, mode }; + let newest_connection = client_connections + .read() + .await + .values() + .filter(|info| info.session_id == session_id) + .max_by_key(|info| info.last_seen) + .map(|info| info.client_id.clone()); + let single_target = match newest_connection { + Some(connection_id) => { + let members = swarm_members.read().await; + members + .get(&session_id) + .and_then(|member| member.event_txs.get(&connection_id)) + .filter(|tx| !tx.is_closed()) + .cloned() + } + None => None, + }; + let delivered = match single_target { + Some(tx) if tx.send(event.clone()).is_ok() => true, + _ => fanout_session_event(swarm_members, &session_id, event).await > 0, + }; if !delivered { anyhow::bail!("Failed to deliver transcript to session '{}'", session_id); @@ -245,6 +264,51 @@ pub(super) async fn inject_transcript( Ok(ServerEvent::Done { id }) } +/// Forward live push-to-talk state to one session's clients. Best effort: +/// a session without a live client simply has nothing to show. +pub(super) async fn forward_voice_activity( + id: u64, + session_id: &str, + activity: crate::protocol::VoiceActivity, + swarm_members: &Arc>>, +) -> ServerEvent { + fanout_session_event( + swarm_members, + session_id, + ServerEvent::VoiceActivity { activity }, + ) + .await; + ServerEvent::Done { id } +} + +/// Passive, read-only event streams. Matches the commands accepted by +/// `maybe_handle_event_subscription_command`, which only the server namespace +/// reaches. +fn is_event_subscription_command(command: &str) -> bool { + let (namespace, command) = parse_namespaced_command(command); + namespace == "server" + && (command == "events:subscribe" || command.starts_with("events:subscribe:")) +} + +#[cfg(test)] +mod event_subscription_tests { + use super::is_event_subscription_command; + + #[test] + fn passive_event_streams_do_not_reserve_activity() { + assert!(is_event_subscription_command("events:subscribe")); + assert!(is_event_subscription_command( + " events:subscribe:status_change,member_change" + )); + assert!(is_event_subscription_command("server:events:subscribe")); + assert!(!is_event_subscription_command("client:events:subscribe")); + assert!(!is_event_subscription_command("events:subscribed")); + assert!(!is_event_subscription_command("events:recent")); + assert!(!is_event_subscription_command("sessions")); + assert!(!is_event_subscription_command("group_transfer:inspect:{}")); + } +} + #[expect( clippy::too_many_arguments, reason = "debug client wiring fans out across sessions, swarms, files, channels, jobs, and transport state" @@ -306,11 +370,25 @@ pub(super) async fn handle_debug_client( // shared request reservation here would make it reject its own request. let transfer_control = matches!(&request, Request::DebugCommand { command, .. } if command.trim().starts_with("group_transfer:") || command.trim().starts_with("server:group_transfer:")); - let read_only_probe = matches!(&request, Request::Ping { .. }); + // An event subscription is a passive stream that stays open for the + // life of the observer (status bars, dashboards). Reserving activity + // for it would block every group transfer until the observer exits. + let read_only_probe = matches!(&request, Request::Ping { .. }) + || matches!(&request, Request::DebugCommand { command, .. } + if is_event_subscription_command(command)); let _request_activity = match if transfer_control || read_only_probe { Ok(None) } else { - crate::runtime_activity::global().try_enter().map(Some) + // Debug commands that name a session only affect that session. + match &request { + Request::DebugCommand { + session_id: Some(session), + .. + } if !session.trim().is_empty() => crate::runtime_activity::global() + .try_enter_session(session) + .map(Some), + _ => crate::runtime_activity::global().try_enter().map(Some), + } } { Ok(activity) => activity, Err(error) => { @@ -383,6 +461,15 @@ pub(super) async fn handle_debug_client( writer.write_all(json.as_bytes()).await?; } + Request::VoiceActivity { + id, + session_id, + activity, + } => { + let event = forward_voice_activity(id, &session_id, activity, &swarm_members).await; + writer.write_all(encode_event(&event).as_bytes()).await?; + } + Request::DebugCommand { id, command, diff --git a/crates/jcode-app-core/src/server/group_transfer.rs b/crates/jcode-app-core/src/server/group_transfer.rs index be52821409a..57df389bec9 100644 --- a/crates/jcode-app-core/src/server/group_transfer.rs +++ b/crates/jcode-app-core/src/server/group_transfer.rs @@ -281,11 +281,13 @@ async fn handle_with_gate( }; let before = snapshots(state).await; let (admitted_ids, _) = expand_selection(&selection, &before)?; - let _idle = admit_transfer(gate, &admitted_ids, action, std::time::Duration::from_secs(2)).await.ok_or_else(|| { - anyhow::anyhow!( - "group_transfer action={action} phase=idle_admission: selected sessions or unscoped background tasks are busy; wait for selected turns and callbacks to finish" - ) - })?; + let _idle = match admit_transfer(gate, &admitted_ids, action, admission_drain_timeout()).await { + Some(idle) => idle, + None => bail!( + "group_transfer action={action} phase=idle_admission: selected sessions or unscoped background tasks are busy; wait for selected turns and callbacks to finish (blocked by: {})", + gate.busy_summary(&admitted_ids) + ), + }; match action { "inspect" | "prepare" => { let (selection, host, expected) = if action == "prepare" { @@ -508,6 +510,18 @@ async fn handle_with_gate( /// daemon quiescence either. Briefly tolerate that drain race, but acquire the /// same atomic fence on every attempt and never ignore genuinely busy work. /// Recovery operations retain their immediate admission behavior. +/// Long enough to drain in-flight requests, callbacks and turn teardown across +/// every connected client. Must stay below the CLI RPC timeout. +const ADMISSION_DRAIN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(45); + +fn admission_drain_timeout() -> std::time::Duration { + if cfg!(test) { + std::time::Duration::from_secs(2) + } else { + ADMISSION_DRAIN_TIMEOUT + } +} + async fn admit_transfer( gate: &crate::runtime_activity::ActivityGate, sessions: &BTreeSet, @@ -516,16 +530,23 @@ async fn admit_transfer( ) -> Option { let deadline = tokio::time::Instant::now() + drain_timeout; loop { + // Register for release notifications before checking, so a release + // between the check and the wait is never lost. The short poll is a + // backstop for runtime-lock releases, which do not notify. + let changed = gate.changed(); + tokio::pin!(changed); if let Some(idle) = gate.try_idle_sessions(sessions) { return Some(idle); } if !matches!(action, "inspect" | "prepare") || tokio::time::Instant::now() >= deadline { return None; } - tokio::time::sleep_until( - deadline.min(tokio::time::Instant::now() + std::time::Duration::from_millis(25)), - ) - .await; + let poll = + deadline.min(tokio::time::Instant::now() + std::time::Duration::from_millis(100)); + tokio::select! { + _ = &mut changed => {} + _ = tokio::time::sleep_until(poll) => {} + } } } diff --git a/crates/jcode-app-core/src/server/history_paging.rs b/crates/jcode-app-core/src/server/history_paging.rs new file mode 100644 index 00000000000..8754d05fcc1 --- /dev/null +++ b/crates/jcode-app-core/src/server/history_paging.rs @@ -0,0 +1,101 @@ +use super::*; + +pub(in crate::server) async fn handle_get_compacted_history( + id: u64, + session_id: &str, + agent: &Arc>, + writer: &Arc>, + visible_messages: usize, + recent_history: bool, +) -> Result<()> { + let mut timing = Timing::new( + if recent_history { + "recent_history_page" + } else { + "compacted_history_page" + }, + Some(id), + session_id, + ); + let started = Instant::now(); + let (messages, images, compacted_info, source) = match agent.try_lock() { + Ok(agent_guard) => { + let (messages, images, info) = if recent_history { + let (messages, images) = agent_guard.get_history_and_rendered_images(); + let (messages, images, info) = + recent_history_window(messages, images, visible_messages); + (messages, images, Some(info)) + } else { + agent_guard.get_history_and_rendered_images_with_compacted_history(visible_messages) + }; + (messages, images, info, "live") + } + Err(_) => { + timing.stage("persisted_load"); + let loaded = crate::session::Session::load_for_remote_startup(session_id) + .or_else(|_| crate::session::Session::load_startup_stub(session_id)); + if loaded.is_err() { + timing.result(&loaded); + } + let session = loaded?; + timing.stage("render"); + let (rendered_messages, images, info) = if recent_history { + let (messages, images) = crate::session::render_messages_and_images(&session); + (messages, images, None) + } else { + crate::session::render_messages_and_images_with_compacted_history( + &session, + visible_messages, + ) + }; + let messages = rendered_messages + .into_iter() + .map(rendered_to_history_message) + .collect(); + let (messages, images, info) = if recent_history { + let (messages, images, info) = + recent_history_window(messages, images, visible_messages); + (messages, images, Some(info)) + } else { + (messages, images, info) + }; + (messages, images, info, "persisted") + } + }; + + let compacted_info = compacted_info.unwrap_or(crate::session::RenderedCompactedHistoryInfo { + total_messages: 0, + visible_messages: 0, + remaining_messages: 0, + hidden_user_prompts: 0, + }); + crate::logging::info(&format!( + "[TIMING] get_compacted_history: session={}, source={}, requested={}, visible={}, remaining={}, messages={}, total={}ms", + session_id, + source, + visible_messages, + compacted_info.visible_messages, + compacted_info.remaining_messages, + messages.len(), + started.elapsed().as_millis(), + )); + + timing.stage("encode"); + let event = ServerEvent::CompactedHistory { + id, + session_id: session_id.to_string(), + messages, + images, + compacted_total: compacted_info.total_messages, + compacted_visible: compacted_info.visible_messages, + compacted_remaining: compacted_info.remaining_messages, + compacted_hidden_prompts: compacted_info.hidden_user_prompts, + }; + let json = encode_event(&event); + timing.stage("writer_lock"); + let mut writer = writer.lock().await; + timing.stage("write"); + let result = writer.write_all(json.as_bytes()).await; + timing.result(&result); + result.map_err(Into::into) +} diff --git a/crates/jcode-app-core/src/server/history_timing.rs b/crates/jcode-app-core/src/server/history_timing.rs new file mode 100644 index 00000000000..44b5fe95f21 --- /dev/null +++ b/crates/jcode-app-core/src/server/history_timing.rs @@ -0,0 +1,176 @@ +use super::*; +use std::sync::atomic::{AtomicU64, Ordering}; + +static NEXT_OPERATION: AtomicU64 = AtomicU64::new(1); + +/// Only fixed stage labels and numeric metadata enter timing logs. Never errors or payloads. +pub(super) struct Timing { + operation_id: u64, + request_id: Option, + session_key: u64, + kind: &'static str, + stage: &'static str, + outcome: &'static str, + started: Instant, + stage_started: Instant, +} + +impl Timing { + pub(super) fn new(kind: &'static str, request_id: Option, session_id: &str) -> Self { + use std::hash::{Hash, Hasher}; + let mut session = std::collections::hash_map::DefaultHasher::new(); + session_id.hash(&mut session); + let started = Instant::now(); + Self { + operation_id: NEXT_OPERATION.fetch_add(1, Ordering::Relaxed), + request_id, + session_key: session.finish(), + kind, + stage: "prepare", + outcome: "dropped", + started, + stage_started: started, + } + } + + fn fields(&self, stage: &str, outcome: &str, duration_ms: u128) -> String { + format!( + "[CLOUD_TIMING] operation=server_history session_key={:016x} operation_id={} request_id={} kind={} stage={} outcome={} duration_ms={} total_duration_ms={}", + self.session_key, + self.operation_id, + self.request_id + .map_or_else(|| "none".into(), |id| id.to_string()), + self.kind, + stage, + outcome, + duration_ms, + self.started.elapsed().as_millis(), + ) + } + + pub(super) fn stage(&mut self, stage: &'static str) { + crate::logging::info(&self.fields( + self.stage, + "ok", + self.stage_started.elapsed().as_millis(), + )); + self.stage = stage; + self.stage_started = Instant::now(); + } + + fn fallback(&self, result: std::result::Result) -> T { + match result { + Ok(value) => value, + Err(_) => { + crate::logging::info(&self.fields( + "load", + "fallback", + self.started.elapsed().as_millis(), + )); + T::default() + } + } + } + + pub(super) fn result(&mut self, result: &std::result::Result) { + self.outcome = if result.is_ok() { "ok" } else { "failed" }; + } +} + +impl Drop for Timing { + fn drop(&mut self) { + crate::logging::info(&self.fields( + self.stage, + self.outcome, + self.stage_started.elapsed().as_millis(), + )); + } +} + +/// Send authoritative todo chrome before the potentially large history frame. +pub(in crate::server) async fn send_resume_todos( + session_id: &str, + writer: &Arc>, +) -> Result<()> { + let mut timing = Timing::new("resume_todos", None, session_id); + let todos = crate::todo::load_todos(session_id); + let plan = crate::todo::load_plan(session_id); + let goals = crate::todo::load_goals(session_id); + timing.stage("encode"); + let payload = serde_json::json!({ + "todos": timing.fallback(todos), + "plan": timing.fallback(plan), + "goals": timing.fallback(goals), + }); + let json = encode_event(&ServerEvent::Todos { + session_id: session_id.to_owned(), + payload: payload.to_string(), + }); + timing.stage("writer_lock"); + let mut writer = writer.lock().await; + timing.stage("write"); + let result = writer.write_all(json.as_bytes()).await; + timing.result(&result); + result.map_err(Into::into) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn resume_todos_reports_disconnected_writer() { + let (stream, peer) = crate::transport::stream_pair().unwrap(); + let (_, write) = stream.into_split(); + drop(peer); + let writer = Arc::new(Mutex::new(write)); + assert!( + send_resume_todos("timing_disconnected_writer", &writer) + .await + .is_err() + ); + } + + #[test] + fn stages_keep_correlation_and_do_not_mark_cancellation_successful() { + let mut timing = Timing::new("history", Some(9), "private-session"); + let operation_id = timing.operation_id; + timing.stage("writer_lock"); + assert_eq!(timing.operation_id, operation_id); + assert_eq!(timing.stage, "writer_lock"); + assert_eq!(timing.outcome, "dropped"); + let todos = Timing::new("resume_todos", None, "private-session"); + assert_eq!(timing.session_key, todos.session_key); + assert!( + !timing + .fields("write", "failed", 1) + .contains("private-session") + ); + assert_ne!( + timing.session_key, + Timing::new("history", Some(9), "other").session_key + ); + } + + #[test] + fn timing_fields_are_correlated_numeric_and_content_free() { + let mut timing = Timing::new("history", Some(42), "secret session"); + let fields = timing.fields("writer_lock", "ok", 7); + assert!( + fields + .contains("request_id=42 kind=history stage=writer_lock outcome=ok duration_ms=7") + ); + assert!(fields.contains("operation_id=")); + assert!(!fields.contains("session_id=")); + assert_eq!(timing.outcome, "dropped"); + timing.result(&Err::<(), _>("secret credential and content")); + assert_eq!(timing.outcome, "failed"); + assert!(!timing.fields("write", timing.outcome, 0).contains("secret")); + timing.result(&Ok::<_, ()>(())); + assert_eq!(timing.outcome, "ok"); + assert_ne!( + timing.operation_id, + Timing::new("history", Some(42), "secret session").operation_id + ); + } +} diff --git a/crates/jcode-app-core/src/server/lane_dispatch.rs b/crates/jcode-app-core/src/server/lane_dispatch.rs new file mode 100644 index 00000000000..29f584da744 --- /dev/null +++ b/crates/jcode-app-core/src/server/lane_dispatch.rs @@ -0,0 +1,304 @@ +//! Automatic worker dispatch for worker-owned todo lanes. +//! +//! A root session marks a todo lane `owner: "worker"`. Whenever its todo list +//! is saved (including mirrored saves when a worker finishes a lane item), the +//! server spawns one swarm worker for every worker-owned lane whose +//! dependencies are satisfied and that has no owner yet. There is no launch +//! call and no per-plan concurrency number: lane dependencies decide when a +//! lane starts, and the global `agents.swarm_max_concurrent_agents` cap holds +//! back the rest as queued until capacity frees up. +//! +//! The spawned worker is recorded on the lane's `assigned_to` before the save +//! that follows, so repeated saves, plan edits and server reloads never spawn +//! a duplicate for the same lane. + +use super::comm_session::handle_comm_spawn; +use super::swarm_mutation_state::SwarmMutationRuntime; +use super::{ + ChannelSubscriptions, ClientConnectionInfo, SessionAgents, SessionInterruptQueues, SwarmEvent, + SwarmMember, SwarmState, +}; +use crate::protocol::ServerEvent; +use crate::provider::Provider; +use crate::todo::TodoItem; +use std::collections::{HashMap, HashSet}; +use std::sync::{Arc, Mutex as StdMutex, OnceLock}; +use std::time::Duration; +use tokio::sync::{OnceCell, RwLock, broadcast, mpsc}; + +/// Server-owned handles the dispatcher needs to spawn workers. Registered once +/// when the server starts; without it, dispatch is a no-op (tests, embedders). +pub(super) struct LaneDispatchContext { + pub provider: Arc, + pub sessions: SessionAgents, + pub global_session_id: Arc>, + pub swarm_state: SwarmState, + pub channel_subscriptions: ChannelSubscriptions, + pub channel_subscriptions_by_session: ChannelSubscriptions, + pub event_history: Arc>>, + pub event_counter: Arc, + pub swarm_event_tx: broadcast::Sender, + pub mcp_pool: Arc>>, + pub soft_interrupt_queues: SessionInterruptQueues, + pub swarm_mutation_runtime: SwarmMutationRuntime, + pub client_connections: Arc>>, +} + +static CONTEXT: OnceLock = OnceLock::new(); + +pub(super) fn register_context(context: LaneDispatchContext) { + if CONTEXT.set(context).is_err() { + crate::logging::warn( + "lane dispatch context already registered, retaining original context", + ); + } +} + +/// After a server start or reload, re-run dispatch for every root member so +/// lanes that became ready while the server was down (or whose spawn was cut +/// off mid-flight) still get a worker. Lanes already owned are skipped by the +/// persisted `assigned_to`, so this never duplicates a worker. +pub(super) fn spawn_startup_scan(delay: Duration) { + let Some(context) = CONTEXT.get() else { + return; + }; + let members = Arc::clone(&context.swarm_state.members); + tokio::spawn(async move { + tokio::time::sleep(delay).await; + let roots: Vec = members + .read() + .await + .values() + .filter(|member| member.report_back_to_session_id.is_none()) + .map(|member| member.session_id.clone()) + .collect(); + for session_id in roots { + if let Ok(todos) = crate::todo::load_todos(&session_id) { + on_todos_saved(&session_id, &todos); + } + } + }); +} + +/// Lanes with a spawn in flight, keyed by `(coordinator, lane id)`. The +/// assignment is persisted by the spawn itself; this only covers the window +/// before that save lands. +fn in_flight() -> &'static StdMutex> { + static IN_FLIGHT: OnceLock>> = OnceLock::new(); + IN_FLIGHT.get_or_init(|| StdMutex::new(HashSet::new())) +} + +/// Coordinators with lanes held back by the live-agent cap. Retried on a +/// timer because capacity frees up through events in other sessions. +fn queued_sessions() -> &'static StdMutex> { + static QUEUED: OnceLock>> = OnceLock::new(); + QUEUED.get_or_init(|| StdMutex::new(HashSet::new())) +} + +const QUEUED_RETRY_INTERVAL: Duration = Duration::from_secs(10); + +/// Ready, unowned, worker-owned lanes of `todos`, in plan order. +/// +/// A lane is ready when its plan item has no open blockers. It is unowned when +/// no open member of the lane has `assigned_to` set. +pub(crate) fn dispatchable_lanes(todos: &[TodoItem]) -> Vec { + let open = |todo: &&TodoItem| !matches!(todo.status.as_str(), "completed" | "cancelled"); + super::swarm_todo_link::plan_items_from_todos(todos) + .into_iter() + .filter(|item| item.blocked_by.is_empty()) + .filter(|item| { + let members: Vec<&TodoItem> = todos + .iter() + .filter(open) + .filter(|todo| crate::todo::stages::lane_id(todo) == item.id) + .collect(); + !members.is_empty() + && members + .iter() + .any(|todo| crate::todo::stages::lane_requests_worker(todo)) + && members.iter().all(|todo| todo.assigned_to.is_none()) + }) + .map(|item| item.id) + .collect() +} + +/// React to a todo save: spawn workers for newly dispatchable lanes. +pub(super) fn on_todos_saved(session_id: &str, todos: &[TodoItem]) { + if CONTEXT.get().is_none() { + return; + } + let lanes = dispatchable_lanes(todos); + if lanes.is_empty() { + queued_sessions() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .remove(session_id); + return; + } + let session_id = session_id.to_string(); + tokio::spawn(async move { dispatch(session_id, lanes).await }); +} + +async fn dispatch(session_id: String, lanes: Vec) { + let Some(context) = CONTEXT.get() else { + return; + }; + // Only a root session (no spawner) coordinates worker lanes. Workers get + // their lane with owners cleared, so this is defense in depth. + let is_root = context + .swarm_state + .members + .read() + .await + .get(&session_id) + .is_some_and(|member: &SwarmMember| member.report_back_to_session_id.is_none()); + if !is_root { + return; + } + for lane in lanes { + let key = (session_id.clone(), lane.clone()); + if !in_flight() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .insert(key.clone()) + { + continue; + } + // Re-read from disk: the triggering event may predate an assignment + // persisted by an earlier dispatch of the same lane. + let still_dispatchable = crate::todo::load_todos(&session_id) + .map(|todos| dispatchable_lanes(&todos).contains(&lane)) + .unwrap_or(false); + if !still_dispatchable { + in_flight() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .remove(&key); + continue; + } + let outcome = spawn_lane_worker(context, &session_id, &lane).await; + in_flight() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .remove(&key); + match outcome { + Ok(worker) => crate::logging::info(&format!( + "lane dispatch: {session_id} lane {lane} -> worker {worker}" + )), + Err(message) if is_capacity_error(&message) => { + crate::logging::info(&format!( + "lane dispatch: {session_id} lane {lane} queued: {message}" + )); + schedule_queued_retry(&session_id); + // Later lanes would hit the same cap. + break; + } + Err(message) => crate::logging::warn(&format!( + "lane dispatch: {session_id} lane {lane} failed: {message}" + )), + } + } +} + +fn is_capacity_error(message: &str) -> bool { + message.contains("live-agent limit reached") || message.contains("member limit reached") +} + +fn schedule_queued_retry(session_id: &str) { + let newly_queued = queued_sessions() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .insert(session_id.to_string()); + if !newly_queued { + return; + } + let session_id = session_id.to_string(); + tokio::spawn(async move { + tokio::time::sleep(QUEUED_RETRY_INTERVAL).await; + let still_queued = queued_sessions() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .remove(&session_id); + if !still_queued { + return; + } + let Ok(todos) = crate::todo::load_todos(&session_id) else { + return; + }; + let lanes = dispatchable_lanes(&todos); + if lanes.is_empty() { + return; + } + // Dispatch re-inserts the session when the cap still holds. That + // insert schedules a fresh delayed retry, so this task ends here. + dispatch(session_id, lanes).await; + }); +} + +async fn spawn_lane_worker( + context: &LaneDispatchContext, + session_id: &str, + lane: &str, +) -> Result { + let (tx, mut rx) = mpsc::unbounded_channel::(); + let mcp_pool = super::get_shared_mcp_pool(&context.mcp_pool).await; + handle_comm_spawn( + 0, + session_id.to_string(), + Some(lane.to_string()), + None, + None, + // A fresh nonce per attempt: a lane that was reopened after its + // worker ended must get a new worker, not a replayed old response. + // Duplicate dispatch is prevented by `in_flight` and `assigned_to`. + Some(format!( + "lane-dispatch:{lane}:{}", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|elapsed| elapsed.as_nanos()) + .unwrap_or_else(|error| { + crate::logging::warn(&format!( + "lane dispatch clock is before Unix epoch: {error}" + )); + 0 + }) + )), + None, + None, + None, + None, + &tx, + &context.sessions, + &context.global_session_id, + &context.provider, + &context.swarm_state.members, + &context.swarm_state.swarms_by_id, + &context.swarm_state.coordinators, + &context.swarm_state.plans, + &context.channel_subscriptions, + &context.channel_subscriptions_by_session, + &context.event_history, + &context.event_counter, + &context.swarm_event_tx, + &mcp_pool, + &context.soft_interrupt_queues, + &context.swarm_mutation_runtime, + &context.client_connections, + ) + .await; + drop(tx); + match rx.recv().await { + Some(ServerEvent::CommSpawnResponse { new_session_id, .. }) + if !new_session_id.is_empty() => + { + Ok(new_session_id) + } + Some(ServerEvent::Error { message, .. }) => Err(message), + Some(other) => Err(format!("unexpected spawn response: {other:?}")), + None => Err("spawn produced no response".to_string()), + } +} + +#[cfg(test)] +#[path = "lane_dispatch_tests.rs"] +mod tests; diff --git a/crates/jcode-app-core/src/server/lane_dispatch_tests.rs b/crates/jcode-app-core/src/server/lane_dispatch_tests.rs new file mode 100644 index 00000000000..4cc3da64f23 --- /dev/null +++ b/crates/jcode-app-core/src/server/lane_dispatch_tests.rs @@ -0,0 +1,95 @@ +use super::dispatchable_lanes; +use crate::todo::TodoItem; + +fn item(id: &str, stage: u32, lane: &str, owner: Option<&str>) -> TodoItem { + TodoItem { + id: id.to_string(), + content: format!("task {id}"), + status: "pending".to_string(), + priority: "high".to_string(), + stage: Some(stage), + group: Some(lane.to_string()), + lane_owner: owner.map(str::to_string), + ..Default::default() + } +} + +#[test] +fn self_owned_lanes_never_dispatch() { + let todos = vec![item("a", 1, "api", None), item("b", 1, "docs", None)]; + assert!(dispatchable_lanes(&todos).is_empty()); +} + +#[test] +fn ready_worker_lanes_in_one_phase_start_together() { + let todos = vec![ + item("a", 1, "api", Some("worker")), + item("b", 1, "docs", Some("worker")), + item("c", 1, "mine", None), + ]; + let lanes = dispatchable_lanes(&todos); + assert_eq!(lanes.len(), 2, "{lanes:?}"); +} + +#[test] +fn later_phase_waits_then_unlocks_on_completion() { + let mut todos = vec![ + item("a", 1, "api", None), + item("b", 2, "client", Some("worker")), + ]; + assert!(dispatchable_lanes(&todos).is_empty()); + todos[0].status = "completed".to_string(); + assert_eq!(dispatchable_lanes(&todos).len(), 1); +} + +#[test] +fn explicit_dependency_unlocks_lane_early() { + let mut consumer = item("c", 2, "client", Some("worker")); + consumer.lane_after = Some(vec!["a".into()]); + let mut todos = vec![ + item("a", 1, "api", None), + item("b", 1, "api", None), + consumer, + ]; + assert!(dispatchable_lanes(&todos).is_empty()); + todos[0].status = "completed".to_string(); + assert_eq!(dispatchable_lanes(&todos).len(), 1); +} + +#[test] +fn owned_lane_is_not_dispatched_again() { + let mut todos = vec![ + item("a", 1, "api", Some("worker")), + item("b", 1, "api", Some("worker")), + ]; + assert_eq!(dispatchable_lanes(&todos).len(), 1); + todos[0].assigned_to = Some("worker-1".into()); + todos[1].assigned_to = Some("worker-1".into()); + // Plan edits and reloads re-evaluate the saved list; the persisted owner + // keeps the lane from getting a second worker. + todos.push({ + let mut extra = item("x", 1, "api", Some("worker")); + extra.assigned_to = Some("worker-1".into()); + extra + }); + assert!(dispatchable_lanes(&todos).is_empty()); +} + +#[test] +fn completed_lane_is_not_dispatched() { + let mut done = item("a", 1, "api", Some("worker")); + done.status = "completed".into(); + assert!(dispatchable_lanes(&[done]).is_empty()); +} + +#[test] +fn normalize_makes_owner_lane_wide() { + let mut todos = vec![ + item("a", 1, "api", Some("worker")), + item("b", 1, "api", None), + item("c", 1, "docs", Some("self")), + ]; + crate::todo::stages::normalize_lane_owners(&mut todos); + assert_eq!(todos[1].lane_owner.as_deref(), Some("worker")); + assert_eq!(todos[2].lane_owner, None); +} diff --git a/crates/jcode-app-core/src/server/live_turn.rs b/crates/jcode-app-core/src/server/live_turn.rs index dd3217d1aa5..91382d5e885 100644 --- a/crates/jcode-app-core/src/server/live_turn.rs +++ b/crates/jcode-app-core/src/server/live_turn.rs @@ -91,7 +91,14 @@ pub(super) async fn idle_live_agent( return None; } - agent.try_lock_owned().ok() + let agent = agent.try_lock_owned().ok()?; + // A session that moved to another machine (or a stale copy) cannot run + // turns here. Starting one only produces a lease error per wake, so leave + // the wake queued for whichever copy is runnable. + if agent.session_lease_blocked() { + return None; + } + Some(agent) } /// Spawn `message` as a full tracked turn in a live session. diff --git a/crates/jcode-app-core/src/server/provider_control.rs b/crates/jcode-app-core/src/server/provider_control.rs index 70bfbb1cd82..067db596e80 100644 --- a/crates/jcode-app-core/src/server/provider_control.rs +++ b/crates/jcode-app-core/src/server/provider_control.rs @@ -940,6 +940,7 @@ pub(super) async fn handle_notify_auth_changed( return; } let activation = crate::auth::lifecycle::activate_auth_change(&activation_request); + super::account_usage::request_after_auth_change(id, client_event_tx_clone.clone()); if let Some(provider_id) = activation.provider_id.as_deref() { // A renewed login must get a real request instead of inheriting the // previous credential's cached runtime failure. @@ -1154,6 +1155,53 @@ pub(super) async fn handle_notify_auth_changed( let _ = client_event_tx.send(ServerEvent::Done { id }); } +/// A one-shot client (the SDK login flow, `jcode login` from another shell) +/// reports new credentials without subscribing to a session. Refresh every +/// live session's provider so subscribed clients, including Desktop chats and +/// drafts that never asked for a session-local switch, receive the new catalog +/// through `ModelsUpdated`. The post-login model switch stays session-local. +pub(super) async fn handle_unbound_notify_auth_changed( + id: u64, + provider_hint: Option, + auth: Option, + provider_template: &Arc, + sessions: &SessionAgents, + client_event_tx: &mpsc::UnboundedSender, +) { + crate::auth::AuthStatus::invalidate_cache(); + let activation = crate::auth::lifecycle::activate_auth_change(&AuthActivationRequest::new( + provider_hint, + auth, + )); + super::account_usage::request_after_auth_change(id, client_event_tx.clone()); + if let Some(provider_id) = activation.provider_id.as_deref() { + crate::provider::clear_provider_unavailable_for_account(provider_id); + } + provider_template.on_auth_changed(); + let agents: Vec>> = sessions.read().await.values().cloned().collect(); + let mut idle = Vec::new(); + let mut busy = Vec::new(); + for agent in agents { + let handle = agent.try_lock().map(|guard| guard.provider_handle()); + match handle { + Ok(provider) => idle.push(provider), + Err(_) => busy.push(agent), + } + } + let mut seen: Vec> = vec![Arc::clone(provider_template)]; + for provider in idle { + if !seen.iter().any(|existing| Arc::ptr_eq(existing, &provider)) { + provider.on_auth_changed_preserve_current_provider(); + seen.push(provider); + } + } + spawn_deferred_auth_refreshes(busy); + crate::bus::Bus::global().publish_models_updated(); + // Catalogs that refresh in the background announce themselves again with + // their own ModelsUpdated once the network fetch completes. + let _ = client_event_tx.send(ServerEvent::Done { id }); +} + #[cfg(test)] #[path = "provider_control_tests.rs"] mod provider_control_tests; diff --git a/crates/jcode-app-core/src/server/reload.rs b/crates/jcode-app-core/src/server/reload.rs index 35a08bf442e..26b0a63ffa7 100644 --- a/crates/jcode-app-core/src/server/reload.rs +++ b/crates/jcode-app-core/src/server/reload.rs @@ -28,6 +28,50 @@ fn prepare_server_exec(cmd: &mut std::process::Command, socket_path: &std::path: .stderr(Stdio::null()); } +/// Global flags the daemon was launched with that select its provider. The +/// replacement must serve the same provider (managed cloud hosts start with +/// `--provider jcode` when they have no model login of their own). +fn inherited_serve_flags(args: impl IntoIterator) -> Vec { + let mut out = Vec::new(); + let mut args = args.into_iter().skip(1); + while let Some(arg) = args.next() { + if arg == "serve" { + break; + } + match arg.as_str() { + "-p" | "--provider" | "-m" | "--model" => { + if let Some(value) = args.next() { + out.push(arg); + out.push(value); + } + } + _ if arg.starts_with("--provider=") || arg.starts_with("--model=") => out.push(arg), + _ => {} + } + } + out +} + +#[cfg(test)] +mod inherited_serve_flags_tests { + use super::inherited_serve_flags; + fn args(list: &[&str]) -> Vec { + list.iter().map(|s| s.to_string()).collect() + } + #[test] + fn keeps_provider_and_model_before_serve_only() { + assert_eq!( + inherited_serve_flags(args(&["jcode", "--provider", "jcode", "serve", "--socket", "/x"])), + args(&["--provider", "jcode"]) + ); + assert_eq!( + inherited_serve_flags(args(&["jcode", "--no-update", "--model=x", "serve", "--provider", "y"])), + args(&["--model=x"]) + ); + assert!(inherited_serve_flags(args(&["jcode", "serve"])).is_empty()); + } +} + async fn receive_reload_signal( rx: &mut watch::Receiver>, last_request_id: &mut Option, @@ -195,6 +239,9 @@ pub(super) async fn await_reload_signal( }), ); let mut cmd = ProcessCommand::new(&binary); + if label == "managed-cloud" { + cmd.args(inherited_serve_flags(std::env::args())); + } cmd.arg("serve").arg("--socket").arg(socket.as_os_str()); // Auto provider detection is dominated by credential-file probes. // The replacement process is the same trusted daemon with the same diff --git a/crates/jcode-app-core/src/server/state.rs b/crates/jcode-app-core/src/server/state.rs index e6bb52a057f..1c8184ad11c 100644 --- a/crates/jcode-app-core/src/server/state.rs +++ b/crates/jcode-app-core/src/server/state.rs @@ -431,6 +431,24 @@ pub(super) async fn session_event_targets( } } +/// Send synchronously, moving the original payload into the final live queue. +/// In particular, a closed queue must not cause a large swarm snapshot clone. +pub(super) fn send_event_to_targets<'a>( + targets: impl IntoIterator>, + event: ServerEvent, +) -> usize { + let mut targets = targets.into_iter().filter(|tx| !tx.is_closed()).peekable(); + let mut delivered = 0; + while let Some(tx) = targets.next() { + if targets.peek().is_none() { + delivered += usize::from(tx.send(event).is_ok()); + break; + } + delivered += usize::from(tx.send(event.clone()).is_ok()); + } + delivered +} + pub(super) async fn fanout_session_event( swarm_members: &Arc>>, session_id: &str, @@ -439,13 +457,7 @@ pub(super) async fn fanout_session_event( super::live_status::observe(session_id, &event); let targets = session_event_targets(swarm_members, session_id).await; - let mut delivered = 0; - for tx in targets { - if tx.send(event.clone()).is_ok() { - delivered += 1; - } - } - delivered + send_event_to_targets(&targets, event) } pub(super) async fn fanout_live_client_event( @@ -463,13 +475,7 @@ pub(super) async fn fanout_live_client_event( member.event_txs.values().cloned().collect::>() }; - let mut delivered = 0; - for tx in targets { - if tx.send(event.clone()).is_ok() { - delivered += 1; - } - } - delivered + send_event_to_targets(&targets, event) } /// Deliver `event` to every live attachment of `session_id` except the @@ -493,13 +499,7 @@ pub(super) async fn fanout_peer_client_event( .map(|(_, tx)| tx.clone()) .collect::>() }; - let mut delivered = 0; - for tx in targets { - if tx.send(event.clone()).is_ok() { - delivered += 1; - } - } - delivered + send_event_to_targets(&targets, event) } /// How a turn's producer released its [`TurnEventChannel`]. @@ -546,6 +546,13 @@ impl TurnEventDrain { } async fn forward_with_exit(&mut self, event: ServerEvent, producer_dropped: bool) { + match &event { + ServerEvent::ToolExec { name, .. } => { + crate::agents_status::tool_started(&self.session_id, name) + } + ServerEvent::ToolDone { .. } => crate::agents_status::tool_done(&self.session_id), + _ => {} + } let end = is_turn_terminal(&event).then(|| match &event { ServerEvent::Error { .. } => crate::protocol::TurnEndStatus::Failed, _ if producer_dropped || self.cancel.cancelled() => { @@ -691,6 +698,7 @@ impl Drop for TurnEventChannel { // Dropping JoinHandle detaches the drain task. It must not be aborted: // already queued events still own the runtime publication reservation, // and the drain task still owes attachments a terminal. + // Unlike finish(), this detached path cannot report a drain task panic. if let Some(close) = self.close.take() { let _ = close.send(TurnEventExit::Dropped { panicking: std::thread::panicking(), diff --git a/crates/jcode-app-core/src/server/swarm.rs b/crates/jcode-app-core/src/server/swarm.rs index 9fbd9b0904a..cc30fc55037 100644 --- a/crates/jcode-app-core/src/server/swarm.rs +++ b/crates/jcode-app-core/src/server/swarm.rs @@ -399,7 +399,7 @@ impl DeadMemberSalvage { )); } parts.push( - "Queued tasks will be picked up by assign_next/run_plan; check plan_status for details." + "Queued worker lanes start automatically as dependencies and capacity allow; check plan_status for details." .to_string(), ); parts.join(" ") @@ -766,7 +766,26 @@ async fn broadcast_swarm_status_now( return; } - let members_guard = swarm_members.read().await; + // Resolve recipients and publish under one guard. Besides avoiding a lock + // and full payload clone per session, this keeps an older snapshot from + // being queued after a concurrent newer snapshot on the same channel. + let mut members_guard = swarm_members.write().await; + let mut targets = Vec::new(); + for sid in &session_ids { + let Some(member) = members_guard.get_mut(sid) else { + continue; + }; + member.event_txs.retain(|_, tx| !tx.is_closed()); + if let Some((_, tx)) = member.event_txs.iter().next() { + member.event_tx = tx.clone(); + targets.extend(member.event_txs.values().cloned()); + } else if !member.event_tx.is_closed() { + targets.push(member.event_tx.clone()); + } + } + if targets.is_empty() { + return; + } let broadcast_terminal_retention = swarm_status_broadcast_terminal_retention(); let members_list: Vec = session_ids .iter() @@ -839,13 +858,12 @@ async fn broadcast_swarm_status_now( }) .collect(); - drop(members_guard); let event = ServerEvent::SwarmStatus { members: members_list, }; - for sid in session_ids { - let _ = fanout_session_event(swarm_members, &sid, event.clone()).await; - } + // Unbounded queue sends never await. Do not route through the generic + // event projector: SwarmStatus itself cannot change a live projection. + super::state::send_event_to_targets(&targets, event); } /// Publish wait transitions immediately and refresh elapsed wait ages while a @@ -856,7 +874,10 @@ pub(super) async fn monitor_wait_status( ) { let mut changes = crate::wait_registry::subscribe_changes(); let mut live_changes = super::live_status::subscribe_changes(); - let mut interval = tokio::time::interval(Duration::from_secs(1)); + // Transitions are pushed by the watch channels above. The tick only ages + // displayed wait/elapsed counters, and each tick fans a full swarm status + // out to every attached client, so keep it coarse to let clients sleep. + let mut interval = tokio::time::interval(Duration::from_secs(5)); interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); let mut previous = HashSet::::new(); loop { @@ -1172,6 +1193,7 @@ pub(super) async fn remove_session_from_swarm( swarm_plans: &Arc>>, ) { let started = Instant::now(); + crate::agents_status::session_closed(session_id); log_swarm_lifecycle( "member_remove_start", vec![ diff --git a/crates/jcode-app-core/src/server/swarm_owner_tests.rs b/crates/jcode-app-core/src/server/swarm_owner_tests.rs index 83ac0bd1007..32c10cc9209 100644 --- a/crates/jcode-app-core/src/server/swarm_owner_tests.rs +++ b/crates/jcode-app-core/src/server/swarm_owner_tests.rs @@ -506,7 +506,8 @@ async fn wait_monitor_publishes_entry_age_and_cleanup_without_tool_events() { }) .await .unwrap(); - tokio::time::timeout(Duration::from_secs(5), async { + // Age refreshes are coalesced to a 5s tick. + tokio::time::timeout(Duration::from_secs(8), async { loop { if let Some(ServerEvent::SwarmStatus { members }) = rx.recv().await && members diff --git a/crates/jcode-app-core/src/server/swarm_tests.rs b/crates/jcode-app-core/src/server/swarm_tests.rs index ef51c0d424c..40ad2ccee13 100644 --- a/crates/jcode-app-core/src/server/swarm_tests.rs +++ b/crates/jcode-app-core/src/server/swarm_tests.rs @@ -107,6 +107,141 @@ fn swarm_member( ) } +#[test] +fn event_fanout_moves_payload_to_final_live_receiver() { + let (first, mut first_rx) = mpsc::unbounded_channel(); + let (closed, closed_rx) = mpsc::unbounded_channel(); + let (last, mut last_rx) = mpsc::unbounded_channel(); + drop(closed_rx); + let detail = "a large event payload".repeat(1000); + let original = detail.as_ptr(); + assert_eq!( + super::super::state::send_event_to_targets( + [&first, &closed, &last], + ServerEvent::StatusDetail { detail }, + ), + 2 + ); + let ServerEvent::StatusDetail { detail: first } = first_rx.try_recv().unwrap() else { + panic!("first receiver got wrong event"); + }; + let ServerEvent::StatusDetail { detail: last } = last_rx.try_recv().unwrap() else { + panic!("last receiver got wrong event"); + }; + assert_eq!(first, last); + assert_ne!(first.as_ptr(), original); + assert_eq!(last.as_ptr(), original, "last receiver must not clone"); + assert!(first_rx.try_recv().is_err()); + assert!(last_rx.try_recv().is_err()); +} + +#[tokio::test] +async fn swarm_status_reaches_attachments_and_headless_fallback_once() { + let (mut attached, mut stale_rx) = swarm_member("attached", "agent", false); + stale_rx.close(); + let (one, mut one_rx) = mpsc::unbounded_channel(); + let (two, mut two_rx) = mpsc::unbounded_channel(); + let (closed, closed_rx) = mpsc::unbounded_channel(); + drop(closed_rx); + attached.event_txs = HashMap::from([ + ("one".into(), one), + ("two".into(), two), + ("closed".into(), closed), + ]); + let (headless, mut headless_rx) = swarm_member("headless", "agent", true); + let (dead, dead_rx) = swarm_member("dead", "agent", true); + drop(dead_rx); + let members = Arc::new(RwLock::new(HashMap::from([ + ("attached".into(), attached), + ("headless".into(), headless), + ("dead".into(), dead), + ]))); + super::broadcast_swarm_status_now( + vec!["attached".into(), "headless".into(), "dead".into()], + &members, + ) + .await; + for rx in [&mut one_rx, &mut two_rx, &mut headless_rx] { + let ServerEvent::SwarmStatus { members } = rx.try_recv().unwrap() else { + panic!("expected swarm status"); + }; + assert_eq!(members.len(), 3, "disconnected members stay in snapshot"); + let attached = members.iter().find(|m| m.session_id == "attached").unwrap(); + assert_eq!(attached.live_attachments, Some(2)); + assert!(rx.try_recv().is_err(), "exactly one event per queue"); + } + assert!(stale_rx.try_recv().is_err()); + assert_eq!(members.read().await["attached"].event_txs.len(), 2); +} + +#[tokio::test] +#[ignore = "comparative swarm payload benchmark, run with --ignored --nocapture"] +async fn benchmark_swarm_payload_fanout() { + use std::hint::black_box; + let (mut member, mut rx) = swarm_member("payload", "agent", true); + member.output_tail = Some("recent streamed output\n".repeat(180)); + let mut fixture = HashMap::new(); + for index in 0..32 { + let mut member = member.clone(); + member.session_id = format!("payload-{index}"); + fixture.insert(member.session_id.clone(), member); + } + let ids = fixture.keys().cloned().collect(); + let fixture = Arc::new(RwLock::new(fixture)); + super::broadcast_swarm_status_now(ids, &fixture).await; + let payload = rx.recv().await.unwrap(); + // Every fixture member shares this queue, so discard the other copies. + while rx.try_recv().is_ok() {} + let bytes = serde_json::to_vec(&payload).unwrap().len(); + for live in [1, 4] { + let mut targets = Vec::new(); + let mut receivers = Vec::new(); + for index in 0..64 { + let (tx, rx) = mpsc::unbounded_channel(); + targets.push(tx); + if index < live { + receivers.push(rx); + } + } + let mut times = Vec::new(); + for optimized in [false, true] { + let start = Instant::now(); + for _ in 0..100 { + // Both paths pay the same cost to supply an owned input. + let event = payload.clone(); + let delivered = if optimized { + super::super::state::send_event_to_targets(&targets, event) + } else { + let mut delivered = 0; + for tx in &targets { + // Previous broadcast cloned once per session, then + // generic fanout cloned again into its single queue. + let session_event = event.clone(); + delivered += usize::from(tx.send(session_event.clone()).is_ok()); + } + delivered + }; + assert_eq!(delivered, live); + for rx in &mut receivers { + let event = rx.try_recv().unwrap(); + assert!( + matches!(&event, ServerEvent::SwarmStatus { members } if members.len() == 32) + ); + black_box(event); + assert!(rx.try_recv().is_err()); + } + } + times.push(start.elapsed()); + } + eprintln!( + "swarm delivery: payload_bytes={bytes} sessions=64 live={live} iterations=100 baseline={:?} optimized={:?} speedup={:.2}x (queue/payload work only)", + times[0], + times[1], + times[0].as_secs_f64() / times[1].as_secs_f64() + ); + } +} + #[tokio::test(start_paused = true)] async fn live_status_fanout_projects_worker_events_into_broadcast() { let sid = "live-status-projection-worker"; @@ -628,38 +763,11 @@ async fn swarm_plan_broadcast_versions_can_invert_on_one_member_channel() { ); } -/// Deterministic demonstration of the SwarmStatus immediate-path -/// snapshot-vs-send inversion (wiring-audit.status-proposal-ordering). -/// -/// `broadcast_swarm_status_now` snapshots member statuses under -/// `swarm_members.read()`, drops the guard, then awaits -/// `fanout_session_event` (a `swarm_members.write()` acquisition) before -/// sending. Swarms below `JCODE_SWARM_STATUS_DEBOUNCE_MEMBER_THRESHOLD` -/// (default 2) take this immediate, non-debounced path on every status -/// change, so two concurrent broadcasts can deliver an old snapshot after -/// a newer one on the same ordered mpsc channel. A last-write-wins -/// consumer (the TUI SwarmStatus handler) is then left showing the stale -/// status until the next unrelated broadcast. -/// -/// Unlike the SwarmPlan inversion test above, there is no second lock we -/// can gate on: the status path snapshots from the same `swarm_members` -/// lock it later writes, so holding any guard also blocks the mutator. -/// Instead this test uses tokio's cooperative budget (128 units per task -/// poll on a current-thread runtime; every RwLock acquisition consumes -/// exactly one). Draining 126 units leaves broadcast A exactly enough for -/// `swarms_by_id.read()` and the `swarm_members.read()` snapshot, forcing -/// a yield at the (uncontended) `swarm_members.write()` inside -/// `fanout_session_event`, i.e. precisely inside the race window between -/// snapshot and send. -/// -/// If this test starts failing with `["running", "running"]` or -/// `["ready", "running"]`, the race has been fixed (e.g. by holding the -/// read lock through the send, or by stamping a monotonic sequence on -/// SwarmStatus and dropping stale ones consumer-side); update the wiring -/// audit. If it fails because broadcast A parks somewhere else, the tokio -/// coop budget constants changed: re-derive the `128 - 2` drain count. +/// Drain the cooperative budget at the old snapshot/send race boundary. +/// Publication now holds one member guard through synchronous queue delivery, +/// so a concurrent update cannot put an older snapshot behind the new one. #[tokio::test] -async fn swarm_status_immediate_broadcasts_can_invert_on_one_member_channel() { +async fn swarm_status_immediate_broadcasts_preserve_snapshot_order() { let (worker, mut worker_rx) = swarm_member("worker", "agent", false); let swarm_members = Arc::new(RwLock::new(HashMap::from([("worker".to_string(), worker)]))); let swarms_by_id = Arc::new(RwLock::new(HashMap::from([( @@ -667,38 +775,29 @@ async fn swarm_status_immediate_broadcasts_can_invert_on_one_member_channel() { HashSet::from(["worker".to_string()]), )]))); - // Broadcast A: snapshots status "ready", then is forced to yield at - // the fanout write acquisition, before sending. + // Broadcast A must enqueue its snapshot before releasing the member lock. let a = tokio::spawn({ let swarm_members = Arc::clone(&swarm_members); let swarms_by_id = Arc::clone(&swarms_by_id); async move { - // Initial task budget is 128. Leave exactly 2 units so the two - // read acquisitions (session-id list + status snapshot) - // succeed and the fanout write acquisition forces a yield. + // Leave two acquisitions: the session list and atomic publication. for _ in 0..126 { tokio::task::coop::consume_budget().await; } broadcast_swarm_status("swarm-1", &swarm_members, &swarms_by_id).await; } }); - // Single yield on the current-thread runtime: A runs its entire first - // poll (budget drain + both reads) and parks after snapshotting - // "ready". Its coop yield happens *before* joining the lock queue, so - // every acquisition below is uncontended and the mutator finishes - // within one poll, before A is re-polled. + // Let A run at the old race boundary before changing the member. tokio::task::yield_now().await; - // Concurrent mutator: flips the status and completes its own - // immediate broadcast while A is parked between snapshot and send. + // A concurrent mutation is serialized with snapshot publication. { let mut members = swarm_members.write().await; members.get_mut("worker").expect("worker member").status = "running".to_string(); } broadcast_swarm_status("swarm-1", &swarm_members, &swarms_by_id).await; - // Release A: it resumes with a fresh budget and sends its stale - // "ready" snapshot after "running" on the same ordered channel. + // Both publications must leave the consumer showing the newest status. a.await.expect("broadcast task"); let mut statuses = Vec::new(); @@ -711,10 +810,8 @@ async fn swarm_status_immediate_broadcasts_can_invert_on_one_member_channel() { } assert_eq!( statuses, - vec!["running".to_string(), "ready".to_string()], - "expected status inversion (new-then-old) on one member channel; \ - if this fails with the correct order, the snapshot-vs-send race \ - may have been fixed (update the wiring audit)" + vec!["ready".to_string(), "running".to_string()], + "snapshot publication must not invert newer and older statuses" ); } diff --git a/crates/jcode-app-core/src/server/swarm_todo_link.rs b/crates/jcode-app-core/src/server/swarm_todo_link.rs index efac7f4fc06..5b9abd710df 100644 --- a/crates/jcode-app-core/src/server/swarm_todo_link.rs +++ b/crates/jcode-app-core/src/server/swarm_todo_link.rs @@ -414,6 +414,25 @@ pub(crate) async fn resync_plan_after_todo_save( return None; } let mut plans = swarm_plans.write().await; + // Worker-owned lanes are dispatched without a launch call, so the first + // save that has one seeds the lane plan that status views read. + let wants_plan = todos.iter().any(crate::todo::stages::lane_requests_worker); + if wants_plan + && plans + .get(&swarm_id) + .is_none_or(|plan| plan.items.is_empty()) + { + let items = plan_items_from_todos(todos); + if !items.is_empty() { + let plan = plans + .entry(swarm_id.clone()) + .or_insert_with(super::VersionedPlan::new); + plan.replace_items(items); + plan.participants.insert(session_id.to_string()); + plan.version += 1; + return Some(swarm_id); + } + } let plan = plans.get_mut(&swarm_id)?; let items = resync_lane_plan(todos, &plan.items)?; plan.replace_items(items); diff --git a/crates/jcode-app-core/src/server/turn_failover.rs b/crates/jcode-app-core/src/server/turn_failover.rs index 9452c52cd2d..8bf78f61ff5 100644 --- a/crates/jcode-app-core/src/server/turn_failover.rs +++ b/crates/jcode-app-core/src/server/turn_failover.rs @@ -94,7 +94,7 @@ pub(super) fn pick_failover_target(agent: &Agent, error: &str) -> Option Option<(PathB /// build whenever that build is the freshest one on disk (the case the pin is /// meant to protect). pub(crate) fn reload_exec_target(is_selfdev_session: bool) -> Option<(PathBuf, &'static str)> { + // A managed Jcode Cloud daemon runs from an immutable release directory and + // its runtime is selected by the root-owned updater through one launcher + // link. That selection is authoritative (a pin may deliberately downgrade), + // so reload follows it instead of the self-dev/stable channels in ~/.jcode. + if let Some(target) = managed_cloud_reload_target(running_server_binary().as_deref()) { + crate::logging::info(&format!( + "{MANAGED_CLOUD_LIVE_RELOAD_MARKER}: exec target {:?}", + target.0 + )); + return Some(target); + } let candidate = newest_reload_candidate(is_selfdev_session)?; // On Linux a self-dev rebuild rewrites the running binary in place (a dirty // build reuses the same `versions/` path), which unlinks the running @@ -165,6 +176,34 @@ pub(crate) fn reload_exec_target(is_selfdev_session: bool) -> Option<(PathBuf, & } } +/// Capability marker. `jcode cloud runtime reload` greps the running daemon's +/// executable for it to decide whether an in-place session-preserving handoff +/// will land on the managed selection, or whether only a restart can. +pub const MANAGED_CLOUD_LIVE_RELOAD_MARKER: &str = "jcode-managed-cloud-live-reload-v1"; + +/// Launcher link the managed cloud updater repoints at the selected runtime. +pub(crate) const MANAGED_CLOUD_LAUNCHER: &str = "/opt/jcode-cloud/jcode"; +const MANAGED_CLOUD_RELEASES: &str = "/opt/jcode-cloud/releases/"; + +fn managed_cloud_reload_target(running: Option<&Path>) -> Option<(PathBuf, &'static str)> { + managed_cloud_reload_target_in(running, Path::new(MANAGED_CLOUD_LAUNCHER), MANAGED_CLOUD_RELEASES) +} + +fn managed_cloud_reload_target_in( + running: Option<&Path>, + launcher: &Path, + releases_prefix: &str, +) -> Option<(PathBuf, &'static str)> { + let running = running?.to_str()?; + if !running.starts_with(releases_prefix) { + return None; + } + // Exec the resolved release, not the link, so the new process pins the + // runtime it actually started from even if the link moves again. + let target = std::fs::canonicalize(launcher).ok()?; + target.is_file().then_some((target, "managed-cloud")) +} + fn binary_mtime(path: &Path) -> Option { std::fs::metadata(path).ok().and_then(|m| m.modified().ok()) } @@ -299,7 +338,13 @@ fn strip_deleted_suffix(path: PathBuf) -> PathBuf { pub(crate) fn running_server_binary() -> Option { static RUNNING: std::sync::OnceLock> = std::sync::OnceLock::new(); RUNNING - .get_or_init(|| std::env::current_exe().ok().map(resolve_launch_path)) + .get_or_init(|| match std::env::current_exe() { + Ok(exe) => Some(resolve_launch_path(exe)), + Err(error) => { + crate::logging::warn(&format!("cannot identify running server binary: {error}")); + None + } + }) .clone() } @@ -309,6 +354,47 @@ fn resolve_launch_path(exe: PathBuf) -> PathBuf { std::fs::canonicalize(&exe).unwrap_or(exe) } +#[cfg(all(test, unix))] +mod managed_cloud_target_tests { + use super::managed_cloud_reload_target_in; + + #[test] + fn managed_daemon_reloads_onto_the_selected_release_even_if_older() { + let dir = tempfile::tempdir().unwrap(); + let releases = dir.path().join("releases"); + let old = releases.join("aaa/jcode-linux-x86_64"); + let new = releases.join("bbb/jcode-linux-x86_64"); + for binary in [&old, &new] { + std::fs::create_dir_all(binary.parent().unwrap()).unwrap(); + std::fs::write(binary, b"bin").unwrap(); + } + let launcher = dir.path().join("jcode"); + std::os::unix::fs::symlink(&new, &launcher).unwrap(); + let prefix = format!("{}/", std::fs::canonicalize(&releases).unwrap().display()); + let running = std::fs::canonicalize(&old).unwrap(); + let (target, label) = + managed_cloud_reload_target_in(Some(&running), &launcher, &prefix).unwrap(); + assert_eq!(target, std::fs::canonicalize(&new).unwrap()); + assert_eq!(label, "managed-cloud"); + // Repointing to an older pinned release is still followed. + std::fs::remove_file(&launcher).unwrap(); + std::os::unix::fs::symlink(&old, &launcher).unwrap(); + let (target, _) = + managed_cloud_reload_target_in(Some(&running), &launcher, &prefix).unwrap(); + assert_eq!(target, running); + } + + #[test] + fn unmanaged_daemons_keep_channel_selection() { + let dir = tempfile::tempdir().unwrap(); + let launcher = dir.path().join("jcode"); + std::fs::write(&launcher, b"bin").unwrap(); + let local = dir.path().join("builds/versions/x/jcode"); + assert!(managed_cloud_reload_target_in(Some(&local), &launcher, "/opt/jcode-cloud/releases/").is_none()); + assert!(managed_cloud_reload_target_in(None, &launcher, "/opt/jcode-cloud/releases/").is_none()); + } +} + #[cfg(all(test, unix))] mod running_server_binary_tests { /// The server is started through a channel link; installing a new build diff --git a/crates/jcode-app-core/src/session_launch.rs b/crates/jcode-app-core/src/session_launch.rs index 6621b3430a1..0c8dfef3145 100644 --- a/crates/jcode-app-core/src/session_launch.rs +++ b/crates/jcode-app-core/src/session_launch.rs @@ -45,6 +45,8 @@ pub struct SessionSpawnContext { /// hooks so the new window lands in the client's terminal instead of the /// server's stale startup env (#405). pub client_terminal_env: Vec<(String, String)>, + /// Explicit non-secret runtime variables for the child process. + pub runtime_env: Vec<(String, String)>, } impl SessionSpawnContext { @@ -52,6 +54,7 @@ impl SessionSpawnContext { Self { kind: Some(kind.into()), extra_env: Vec::new(), + runtime_env: Vec::new(), client_terminal_env: Vec::new(), } } @@ -67,6 +70,41 @@ impl SessionSpawnContext { self } + /// Attach the explicit runtime env sent only to the spawned jcode child. + pub fn with_runtime_env(mut self, env: Vec<(String, String)>) -> Self { + self.runtime_env = env; + self + } + + /// Capture effective local runtime values, not just inherited overrides. + /// The connected socket takes precedence over any stale `JCODE_SOCKET`. + pub fn with_local_runtime(mut self, connected_socket: &std::path::Path) -> Self { + let mut env = Vec::new(); + if let Ok(home) = crate::storage::jcode_dir() { + env.push(( + "JCODE_HOME".to_string(), + home.to_string_lossy().into_owned(), + )); + } + env.extend([ + ( + "JCODE_RUNTIME_DIR".to_string(), + crate::storage::runtime_dir().to_string_lossy().into_owned(), + ), + ( + "JCODE_SOCKET".to_string(), + connected_socket.to_string_lossy().into_owned(), + ), + ]); + for key in ["JCODE_API_SOCKET", "JCODE_NO_AUTO_UPDATE"] { + if let Ok(value) = std::env::var(key) { + env.push((key.to_string(), value)); + } + } + self.runtime_env = env; + self + } + fn apply( &self, mut command: crate::terminal_launch::TerminalCommand, @@ -79,6 +117,9 @@ impl SessionSpawnContext { for (key, value) in &self.extra_env { command = command.spawn_env(key.clone(), value.clone()); } + if !self.runtime_env.is_empty() { + command = command.runtime_env(self.runtime_env.clone()); + } if !self.client_terminal_env.is_empty() { command = command.client_terminal_env(self.client_terminal_env.clone()); } @@ -86,6 +127,202 @@ impl SessionSpawnContext { } } +#[cfg(test)] +mod runtime_context_tests { + use super::*; + + #[test] + fn app_core_runtime_allowlist_facade_is_portable() { + let _: &'static [&'static str] = crate::terminal_launch::RUNTIME_ENV_VARS; + assert!(crate::terminal_launch::RUNTIME_ENV_VARS.contains(&"JCODE_SOCKET")); + } + + fn value<'a>(context: &'a SessionSpawnContext, key: &str) -> Option<&'a str> { + context + .runtime_env + .iter() + .find(|(candidate, _)| candidate == key) + .map(|(_, value)| value.as_str()) + } + + fn run_isolated_context_case(mode: &str) -> std::process::Output { + let parent_home = + std::path::PathBuf::from(std::env::var_os("HOME").expect("isolated HOME")); + + let root = parent_home.join(format!( + "jcode-runtime-context-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + std::fs::create_dir_all(&root).expect("create isolated runtime context directory"); + let home = root.join("home with spaces/it's 🧪"); + let runtime_dir = root.join("runtime dir/it's 🧪"); + let xdg_runtime_dir = root.join("xdg runtime/it's 🧪"); + let jcode_home = root.join("jcode home/it's 🧪"); + let api_socket = root.join("api socket.sock"); + let exe = std::env::current_exe().expect("test binary path"); + let mut command = std::process::Command::new(exe); + command + .args([ + "--exact", + "session_launch::runtime_context_tests::runtime_context_child", + "--nocapture", + ]) + .env_clear() + .env("HOME", &home) + .env( + "XDG_RUNTIME_DIR", + if mode == "custom" { + root.join("unused runtime/it's 🧪") + } else { + xdg_runtime_dir.clone() + }, + ) + .env("JCODE_SOCKET", root.join("stale socket.sock")) + .env("JCODE_CONTEXT_TEST_MODE", mode) + .env("JCODE_CONTEXT_TEST_HOME", &home) + .env("JCODE_CONTEXT_TEST_RUNTIME", &runtime_dir) + .env("JCODE_CONTEXT_TEST_JCODE_HOME", &jcode_home) + .env("JCODE_CONTEXT_TEST_XDG_RUNTIME", &xdg_runtime_dir) + .env("JCODE_CONTEXT_TEST_API_SOCKET", &api_socket); + if mode == "custom" { + command + .env("JCODE_HOME", &jcode_home) + .env("JCODE_RUNTIME_DIR", &runtime_dir); + } else { + command + .env("JCODE_API_SOCKET", &api_socket) + .env("JCODE_NO_AUTO_UPDATE", "1"); + } + let output = command.output().expect("run isolated context case"); + std::fs::remove_dir_all(&root).expect("remove isolated runtime context directory"); + output + } + + #[cfg(unix)] + #[test] + fn local_runtime_context_captures_custom_paths_and_connected_socket() { + let output = run_isolated_context_case("custom"); + assert!( + output.status.success(), + "stdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + } + + #[cfg(unix)] + #[test] + fn local_runtime_context_uses_effective_default_home_and_optional_values() { + let output = run_isolated_context_case("default"); + assert!( + output.status.success(), + "stdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + } + + #[cfg(unix)] + #[test] + fn runtime_context_child() { + let Ok(mode) = std::env::var("JCODE_CONTEXT_TEST_MODE") else { + return; + }; + let home = std::env::var("JCODE_CONTEXT_TEST_HOME").expect("isolated test home"); + let jcode_home = + std::env::var("JCODE_CONTEXT_TEST_JCODE_HOME").expect("isolated jcode home"); + let runtime_dir = + std::env::var("JCODE_CONTEXT_TEST_RUNTIME").expect("isolated runtime dir"); + let xdg_runtime_dir = + std::env::var("JCODE_CONTEXT_TEST_XDG_RUNTIME").expect("isolated XDG runtime dir"); + let api_socket = + std::env::var("JCODE_CONTEXT_TEST_API_SOCKET").expect("isolated API socket"); + let socket = std::path::Path::new(&home).join("connected socket/it's 🧪.sock"); + let context = SessionSpawnContext::default().with_local_runtime(&socket); + if mode == "custom" { + assert_eq!(value(&context, "JCODE_HOME"), Some(jcode_home.as_str())); + assert_eq!( + value(&context, "JCODE_RUNTIME_DIR"), + Some(runtime_dir.as_str()) + ); + + assert_eq!(value(&context, "JCODE_API_SOCKET"), None); + assert_eq!(value(&context, "JCODE_NO_AUTO_UPDATE"), None); + } else { + let expected_home = std::path::Path::new(&home).join(".jcode"); + assert_eq!( + value(&context, "JCODE_HOME"), + Some(expected_home.to_str().unwrap()) + ); + assert_eq!( + value(&context, "JCODE_RUNTIME_DIR"), + Some(xdg_runtime_dir.as_str()) + ); + assert_eq!( + value(&context, "JCODE_API_SOCKET"), + Some(api_socket.as_str()) + ); + assert_eq!(value(&context, "JCODE_NO_AUTO_UPDATE"), Some("1")); + } + assert_eq!( + value(&context, "JCODE_SOCKET"), + Some(socket.to_str().unwrap()) + ); + let socket_value = socket.to_string_lossy().into_owned(); + for session_id in ["ses active child", "ses completed child"] { + let args = resume_args(session_id, None, &context); + assert_eq!( + args, + vec![ + "--fresh-spawn".to_string(), + "--socket".to_string(), + socket_value.clone(), + "--resume".to_string(), + session_id.to_string(), + ] + ); + let command = context.apply( + crate::terminal_launch::TerminalCommand::new( + "/usr/local/bin/jcode", + resume_args(session_id, None, &context), + ) + .fresh_spawn(), + "resume", + session_id, + ); + assert_eq!(command.session_id.as_deref(), Some(session_id)); + assert_eq!(command.args, args); + assert_eq!(command.runtime_env, context.runtime_env); + } + } +} + +#[cfg(unix)] +fn resume_args( + session_id: &str, + provider_key: Option<&str>, + context: &SessionSpawnContext, +) -> Vec { + let mut args = vec!["--fresh-spawn".to_string()]; + if let Some(provider_arg) = resume_provider_arg(provider_key) { + args.push("--provider".to_string()); + args.push(provider_arg.to_string()); + } + if let Some((_, socket)) = context + .runtime_env + .iter() + .find(|(key, _)| key == "JCODE_SOCKET") + { + args.extend(["--socket".to_string(), socket.clone()]); + } + args.extend(["--resume".to_string(), session_id.to_string()]); + args +} + /// Compute the window/terminal title used when (re)launching a session. pub fn resumed_window_title(session_id: &str) -> String { let session_name = crate::process_title::session_name(session_id); @@ -247,12 +484,7 @@ pub fn spawn_resume_in_new_terminal_with_context( context: &SessionSpawnContext, ) -> Result { let title = resumed_window_title(session_id); - let mut args = vec!["--fresh-spawn".to_string()]; - if let Some(provider_arg) = resume_provider_arg(provider_key) { - args.push("--provider".to_string()); - args.push(provider_arg.to_string()); - } - args.extend(["--resume".to_string(), session_id.to_string()]); + let args = resume_args(session_id, provider_key, context); let command = crate::terminal_launch::TerminalCommand::new(exe, args) .title(title) .fresh_spawn(); @@ -474,6 +706,7 @@ pub fn spawn_resume_in_new_terminal_with_context( .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()); + crate::terminal_launch::apply_runtime_env(&mut cmd, &context.runtime_env); crate::platform::spawn_detached(&mut cmd) } "wt" | "windows-terminal" => { @@ -488,6 +721,7 @@ pub fn spawn_resume_in_new_terminal_with_context( .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()); + crate::terminal_launch::apply_runtime_env(&mut cmd, &context.runtime_env); crate::platform::spawn_detached(&mut cmd) } "alacritty" => { @@ -502,6 +736,7 @@ pub fn spawn_resume_in_new_terminal_with_context( .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()); + crate::terminal_launch::apply_runtime_env(&mut cmd, &context.runtime_env); crate::platform::spawn_detached(&mut cmd) } _ => continue, diff --git a/crates/jcode-app-core/src/session_window.rs b/crates/jcode-app-core/src/session_window.rs new file mode 100644 index 00000000000..e35cc38829f --- /dev/null +++ b/crates/jcode-app-core/src/session_window.rs @@ -0,0 +1,311 @@ +//! Acting on a session that another local client window already shows. +//! +//! Resume consults `display.resume_open_elsewhere` before attaching: `move` +//! brings the existing window to the user (niri IPC), `replace` closes the +//! other window's client so only the new one remains. Which client shows which +//! session comes from the `client_sessions` registry, so no server round trip +//! is needed. Linux only: the registry lookup and process walk read `/proc`. + +use crate::config::ResumeOpenElsewhere; + +/// What happened to the other windows showing a session being resumed. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum OpenElsewhereOutcome { + /// Nothing to do: disabled, or no other window shows the session. + Proceed, + /// The existing window was brought here and focused. Do not attach again. + Moved, + /// This many other clients were asked to close. Attach as usual. + Replaced(usize), +} + +/// Apply `display.resume_open_elsewhere` for `session_id`. +pub fn apply_resume_open_elsewhere(session_id: &str) -> OpenElsewhereOutcome { + let mode = crate::config::config().display.resume_open_elsewhere; + if mode == ResumeOpenElsewhere::Off { + return OpenElsewhereOutcome::Proceed; + } + let clients = crate::client_focus::clients_showing_session(session_id); + if clients.is_empty() { + return OpenElsewhereOutcome::Proceed; + } + if mode == ResumeOpenElsewhere::Move && bring_client_window_here(&clients) { + return OpenElsewhereOutcome::Moved; + } + OpenElsewhereOutcome::Replaced(close_clients(&clients)) +} + +/// Signal a client uses to exit cleanly because another window took over its +/// session. Unlike SIGHUP/SIGTERM it never marks the session crashed. +#[cfg(unix)] +pub const CLIENT_REPLACED_SIGNAL: i32 = libc::SIGUSR1; + +#[cfg(unix)] +fn close_clients(pids: &[u32]) -> usize { + pids.iter() + .filter(|pid| { + let Ok(pid) = i32::try_from(**pid) else { + return false; + }; + // SAFETY: kill only sends a signal; the pid was just verified to be + // a live jcode client by the registry lookup. + pid > 1 && unsafe { libc::kill(pid, CLIENT_REPLACED_SIGNAL) } == 0 + }) + .count() +} + +#[cfg(not(unix))] +fn close_clients(_pids: &[u32]) -> usize { + 0 +} + +fn niri_json(args: &[&str]) -> Option { + let output = std::process::Command::new("niri") + .arg("msg") + .arg("-j") + .args(args) + .stdin(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .output(); + let output = match output { + Ok(output) => output, + Err(error) => { + crate::logging::debug(&format!("niri query {args:?} unavailable: {error}")); + return None; + } + }; + if !output.status.success() { + return None; + } + match serde_json::from_slice(&output.stdout) { + Ok(value) => Some(value), + Err(error) => { + crate::logging::warn(&format!("invalid niri query {args:?} response: {error}")); + None + } + } +} + +fn niri_action(args: &[String]) -> bool { + std::process::Command::new("niri") + .args(["msg", "action"]) + .args(args) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status() + .is_ok_and(|status| status.success()) +} + +/// The process and its ancestors, nearest first. +fn process_ancestry(pid: u32) -> Vec { + let mut chain = Vec::new(); + let mut pid = pid; + while pid > 1 && !chain.contains(&pid) { + chain.push(pid); + let Ok(stat) = std::fs::read_to_string(format!("/proc/{pid}/stat")) else { + break; + }; + // The command name may contain spaces and parentheses; the parent pid + // is the second field after the final ')'. + let Some(parent) = stat + .rsplit_once(')') + .and_then(|(_, rest)| rest.split_whitespace().nth(1)) + .and_then(|ppid| match ppid.parse::() { + Ok(parent) => Some(parent), + Err(error) => { + crate::logging::debug(&format!("invalid parent pid for {pid}: {error}")); + None + } + }) + else { + break; + }; + pid = parent; + } + chain +} + +/// A move the compositor should perform, derived from its JSON state. +#[derive(Debug, PartialEq, Eq)] +struct NiriMove { + window_id: u64, + /// Set when the window sits on another monitor. + output: Option, + /// Set when the window is not already on the focused workspace. + workspace_idx: Option, +} + +/// Pick the single window owned by one of `ancestries` and work out how to get +/// it onto the focused workspace. `None` when no window, or more than one, +/// matches: a terminal that hosts several windows in one process is ambiguous, +/// and moving the wrong one is worse than not moving any. +fn plan_niri_move( + windows: &serde_json::Value, + workspaces: &serde_json::Value, + ancestries: &[Vec], +) -> Option { + let windows = windows.as_array()?; + let workspaces = workspaces.as_array()?; + let window_pid = |window: &serde_json::Value| window.get("pid").and_then(|pid| pid.as_u64()); + // Nearest ancestor that owns a window, for the first client that has one. + let owner = ancestries.iter().find_map(|chain| { + chain.iter().copied().find(|pid| { + windows + .iter() + .any(|window| window_pid(window) == Some(u64::from(*pid))) + }) + })?; + let mut owned = windows + .iter() + .filter(|window| window_pid(window) == Some(u64::from(owner))); + let window = owned.next()?; + if owned.next().is_some() { + return None; + } + let window_id = window.get("id")?.as_u64()?; + let window_workspace = window.get("workspace_id").and_then(|id| id.as_u64()); + + let focused = workspaces + .iter() + .find(|ws| ws.get("is_focused").and_then(|f| f.as_bool()) == Some(true))?; + let focused_id = focused.get("id")?.as_u64()?; + if window_workspace == Some(focused_id) { + return Some(NiriMove { + window_id, + output: None, + workspace_idx: None, + }); + } + let output_of = |ws: &serde_json::Value| { + ws.get("output") + .and_then(|output| output.as_str()) + .map(str::to_string) + }; + let focused_output = output_of(focused); + let window_output = workspaces + .iter() + .find(|ws| ws.get("id").and_then(|id| id.as_u64()) == window_workspace) + .and_then(output_of); + Some(NiriMove { + window_id, + output: focused_output.filter(|output| Some(output) != window_output.as_ref()), + workspace_idx: Some(focused.get("idx")?.as_u64()?), + }) +} + +/// Bring the window hosting one of `client_pids` to the focused workspace and +/// focus it. Returns false when there is no niri, or no unambiguous window. +fn bring_client_window_here(client_pids: &[u32]) -> bool { + if cfg!(test) || std::env::var_os("NIRI_SOCKET").is_none() { + return false; + } + let (Some(windows), Some(workspaces)) = (niri_json(&["windows"]), niri_json(&["workspaces"])) + else { + return false; + }; + let ancestries: Vec> = client_pids + .iter() + .map(|pid| process_ancestry(*pid)) + .collect(); + let Some(plan) = plan_niri_move(&windows, &workspaces, &ancestries) else { + return false; + }; + let id = plan.window_id.to_string(); + if let Some(output) = plan.output { + niri_action(&[ + "move-window-to-monitor".into(), + "--id".into(), + id.clone(), + output, + ]); + } + if let Some(idx) = plan.workspace_idx + && !niri_action(&[ + "move-window-to-workspace".into(), + "--window-id".into(), + id.clone(), + "--focus".into(), + "false".into(), + idx.to_string(), + ]) + { + return false; + } + niri_action(&["focus-window".into(), "--id".into(), id]) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn workspaces() -> serde_json::Value { + json!([ + {"id": 1, "idx": 1, "output": "eDP-1", "is_focused": false}, + {"id": 4, "idx": 2, "output": "eDP-1", "is_focused": true}, + {"id": 9, "idx": 1, "output": "DP-2", "is_focused": false}, + ]) + } + + #[test] + fn moves_window_from_another_workspace_on_the_same_monitor() { + let windows = json!([ + {"id": 20, "pid": 500, "workspace_id": 1}, + {"id": 21, "pid": 600, "workspace_id": 4}, + ]); + // Client 777 runs under shell 700 inside terminal 500. + let plan = plan_niri_move(&windows, &workspaces(), &[vec![777, 700, 500, 2]]); + assert_eq!( + plan, + Some(NiriMove { + window_id: 20, + output: None, + workspace_idx: Some(2), + }) + ); + } + + #[test] + fn window_on_another_monitor_changes_output_first() { + let windows = json!([{"id": 30, "pid": 500, "workspace_id": 9}]); + let plan = plan_niri_move(&windows, &workspaces(), &[vec![777, 500]]).unwrap(); + assert_eq!(plan.output.as_deref(), Some("eDP-1")); + assert_eq!(plan.workspace_idx, Some(2)); + } + + #[test] + fn window_already_here_is_only_focused() { + let windows = json!([{"id": 21, "pid": 600, "workspace_id": 4}]); + let plan = plan_niri_move(&windows, &workspaces(), &[vec![601, 600]]).unwrap(); + assert_eq!(plan.workspace_idx, None); + assert_eq!(plan.output, None); + } + + #[test] + fn ambiguous_or_missing_windows_are_not_moved() { + // One terminal process hosting two windows: cannot tell which one. + let shared = json!([ + {"id": 1, "pid": 500, "workspace_id": 1}, + {"id": 2, "pid": 500, "workspace_id": 1}, + ]); + assert_eq!( + plan_niri_move(&shared, &workspaces(), &[vec![777, 500]]), + None + ); + let unrelated = json!([{"id": 1, "pid": 900, "workspace_id": 1}]); + assert_eq!( + plan_niri_move(&unrelated, &workspaces(), &[vec![777, 500]]), + None + ); + } + + #[test] + fn ancestry_starts_at_the_process_and_reaches_its_parent() { + let chain = process_ancestry(std::process::id()); + assert_eq!(chain.first(), Some(&std::process::id())); + if cfg!(target_os = "linux") { + assert!(chain.len() > 1); + } + } +} diff --git a/crates/jcode-app-core/src/tool/bash.rs b/crates/jcode-app-core/src/tool/bash.rs index 98538c2c8d2..ddf79f6a9e6 100644 --- a/crates/jcode-app-core/src/tool/bash.rs +++ b/crates/jcode-app-core/src/tool/bash.rs @@ -1,9 +1,8 @@ -use super::{BACKGROUND_WAIT_GUIDANCE, StdinInputRequest, Tool, ToolContext, ToolOutput}; +use super::{BACKGROUND_WAIT_GUIDANCE, Tool, ToolContext, ToolOutput}; use crate::background::TaskResult; use crate::bus::{ BackgroundTaskProgress, BackgroundTaskProgressKind, BackgroundTaskProgressSource, }; -use crate::stdin_detect::{self, StdinState}; use crate::util::truncate_str; use anyhow::Result; use async_trait::async_trait; @@ -27,9 +26,6 @@ use tokio::process::Command as TokioCommand; const MAX_OUTPUT_LEN: usize = 30000; const DEFAULT_TIMEOUT_MS: u64 = 120000; -const STDIN_POLL_INTERVAL_MS: u64 = 500; -const STDIN_INITIAL_DELAY_MS: u64 = 300; -const STDIN_RESPONSE_TIMEOUT_MS: u64 = 1500; const PROGRESS_MARKER_PREFIX: &str = "JCODE_PROGRESS "; const CHECKPOINT_MARKER_PREFIX: &str = "JCODE_CHECKPOINT "; const BASH_TOOL_DESCRIPTION: &str = @@ -623,6 +619,39 @@ fn tool_scratch_dir() -> Option { Some(dir) } +/// Every bash command is non-interactive: stdin is /dev/null (callers that +/// pipe stdin drop it immediately), common tools are told not to prompt, and +/// on unix the command runs in its own session with no controlling terminal so +/// prompts that open /dev/tty directly (sudo, ssh, git credentials) fail fast +/// instead of hanging. The agent sees the failure and reruns non-interactively. +const NON_INTERACTIVE_ENV: &[(&str, &str)] = &[ + ("GIT_TERMINAL_PROMPT", "0"), + ("GIT_ASKPASS", "true"), + ("SSH_ASKPASS_REQUIRE", "never"), + ("GCM_INTERACTIVE", "never"), + ("DEBIAN_FRONTEND", "noninteractive"), + ("PAGER", "cat"), + ("GIT_PAGER", "cat"), + ("SYSTEMD_PAGER", ""), + ("NPM_CONFIG_YES", "true"), + ("PIP_NO_INPUT", "1"), +]; + +#[cfg(not(windows))] +fn configure_non_interactive(command: &mut TokioCommand) { + command.stdin(Stdio::null()); + command.envs(NON_INTERACTIVE_ENV.iter().copied()); + command.env_remove("SSH_ASKPASS"); + unsafe { + command.pre_exec(|| { + // New session: no controlling tty, own process group (so group + // kills still work). Ignore failure if already a group leader. + libc::setsid(); + Ok(()) + }); + } +} + #[cfg(not(windows))] fn configure_tool_scratch(command: &mut TokioCommand) { if let Some(dir) = tool_scratch_dir() { @@ -678,6 +707,7 @@ fn build_shell_command(cmd_str: &str) -> TokioCommand { let mut cmd = TokioCommand::new("bash"); cmd.arg("-c").arg(cmd_str); configure_tool_scratch(&mut cmd); + configure_non_interactive(&mut cmd); cmd } } @@ -710,7 +740,9 @@ fn build_detached_shell_wrapper(command: &str) -> StdCommand { .arg( r#"eval "$JCODE_RELOAD_DETACH_COMMAND"; status=$?; printf '\n--- Command finished with exit code: %s ---\n' "$status"; exit "$status""#, ) - .env("JCODE_RELOAD_DETACH_COMMAND", command); + .env("JCODE_RELOAD_DETACH_COMMAND", command) + .envs(NON_INTERACTIVE_ENV.iter().copied()) + .env_remove("SSH_ASKPASS"); if let Some(dir) = tool_scratch_dir() { cmd.env("TMPDIR", &dir).env("JCODE_SCRATCH_DIR", dir); } @@ -913,8 +945,6 @@ impl BashTool { let timeout_ms = params.timeout.unwrap_or(DEFAULT_TIMEOUT_MS).min(600000); let timeout_duration = Duration::from_millis(timeout_ms); - let has_stdin_channel = ctx.stdin_request_tx.is_some(); - let mut command = build_shell_command(¶ms.command); command.envs(cloud_command_environment(ctx.working_dir.as_deref())?); command.envs(crate::session_env::session_env(&ctx.session_id)); @@ -924,16 +954,11 @@ impl BashTool { .stdout(Stdio::piped()) .stderr(Stdio::piped()); - if has_stdin_channel { - command.stdin(Stdio::piped()); - } - if let Some(ref dir) = ctx.working_dir { command.current_dir(dir); } let mut child = command.spawn()?; - let child_pid = child.id().unwrap_or(0); let stdin_handle = child.stdin.take(); let stdout_handle = child.stdout.take(); let stderr_handle = child.stderr.take(); @@ -944,10 +969,6 @@ impl BashTool { .intent .clone() .unwrap_or_else(|| params.command.clone()); - let stdin_tx = ctx.stdin_request_tx.clone(); - let tool_call_id = ctx.tool_call_id.clone(); - let push_session_id = ctx.session_id.clone(); - let push_command = params.command.clone(); let title_for_work = title.clone(); // Track progress parsed from output so a timeout promotion starts the // background task at the real percentage instead of 0%. @@ -970,94 +991,12 @@ impl BashTool { stderr_progress, )); - let stdin_task = if has_stdin_channel { - Some(tokio::spawn(async move { - if let (Some(mut stdin_pipe), Some(stdin_tx)) = (stdin_handle, stdin_tx) { - tokio::time::sleep(Duration::from_millis(STDIN_INITIAL_DELAY_MS)).await; - - let mut request_counter = 0u32; - loop { - #[cfg(target_os = "linux")] - let state = stdin_detect::linux::check_process_tree(child_pid); - #[cfg(not(target_os = "linux"))] - let state = stdin_detect::is_waiting_for_stdin(child_pid); - - if state == StdinState::Reading { - request_counter += 1; - let request_id = - format!("stdin-{}-{}", tool_call_id, request_counter); - let (response_tx, response_rx) = - tokio::sync::oneshot::channel(); - - let request = StdinInputRequest { - request_id, - prompt: String::new(), - is_password: false, - response_tx, - }; - - if stdin_tx.send(request).is_err() { - break; - } - crate::mobile_push::notify( - crate::mobile_push::PushKind::NeedsInput, - &push_session_id, - None, - &format!("Command is waiting for input: {push_command}"), - None, - ); - - // Most clients cannot answer stdin prompts. Without a - // prompt reply, close stdin so commands that read it - // (e.g. `rg pattern` with no path) see EOF instead of - // hanging until the tool timeout. - match tokio::time::timeout( - Duration::from_millis(STDIN_RESPONSE_TIMEOUT_MS), - response_rx, - ) - .await - { - Ok(Ok(input)) => { - let line = if input.ends_with('\n') { - input - } else { - format!("{}\n", input) - }; - if stdin_pipe.write_all(line.as_bytes()).await.is_err() - { - break; - } - if stdin_pipe.flush().await.is_err() { - break; - } - } - Ok(Err(_)) | Err(_) => { - drop(stdin_pipe); - break; - } - } - - tokio::time::sleep(Duration::from_millis(100)).await; - } else { - tokio::time::sleep(Duration::from_millis( - STDIN_POLL_INTERVAL_MS, - )) - .await; - } - } - } - })) - } else { - drop(stdin_handle); - None - }; + // Commands are always non-interactive: stdin is /dev/null, so + // anything that reads it gets EOF immediately. + drop(stdin_handle); let status = child.wait().await?; - if let Some(task) = stdin_task { - task.abort(); - } - let stdout = stdout_task.await.unwrap_or_default(); let stderr = stderr_task.await.unwrap_or_default(); @@ -1321,15 +1260,8 @@ impl BashTool { cmd.envs(cloud_environment); cmd.envs(session_environment); cmd.env("JCODE_SESSION_ID", &session_id_env); - #[cfg(unix)] - unsafe { - cmd.pre_exec(|| { - if libc::setpgid(0, 0) == -1 { - return Err(std::io::Error::last_os_error()); - } - Ok(()) - }); - } + // build_shell_command runs the child in its own session + // (setsid), so it is already its own process group leader. cmd.kill_on_drop(true); configure_background_command_stdio(&mut cmd); if let Some(ref dir) = working_dir { diff --git a/crates/jcode-app-core/src/tool/bash_tests.rs b/crates/jcode-app-core/src/tool/bash_tests.rs index 2f9b406a297..218fe044cc7 100644 --- a/crates/jcode-app-core/src/tool/bash_tests.rs +++ b/crates/jcode-app-core/src/tool/bash_tests.rs @@ -306,94 +306,32 @@ async fn test_basic_command_with_unused_stdin_channel() { } #[tokio::test] -async fn test_stdin_forwarding_single_line() { +async fn test_stdin_reader_gets_eof_immediately() { let (tx, mut rx) = mpsc::unbounded_channel::(); let tool = BashTool::new(); - // "head -n1" reads one line from stdin and prints it - let input = json!({"command": "head -n1", "timeout": 10000}); + // Commands are non-interactive: a stdin reader sees EOF right away, and no + // input request is ever raised. + let input = json!({"command": "if read -r line; then echo got; else echo eof_ok; fi", "timeout": 5000}); let ctx = make_ctx(Some(tx)); - - // Spawn the tool execution - let tool_handle = tokio::spawn(async move { tool.execute(input, ctx).await }); - - // Wait for the stdin request to arrive - let req = tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv()) - .await - .expect("timed out waiting for stdin request") - .expect("channel closed"); - - assert!(req.request_id.starts_with("stdin-test-call-")); - assert!(!req.is_password); - - // Send the response - req.response_tx.send("test_input_line".to_string()).unwrap(); - - // Wait for tool to finish - let result = tokio::time::timeout(std::time::Duration::from_secs(5), tool_handle) - .await - .expect("tool timed out") - .expect("tool panicked") - .expect("tool errored"); - - assert!( - result.output.contains("test_input_line"), - "output should contain the input we sent: {}", - result.output - ); + let started = std::time::Instant::now(); + let result = tool.execute(input, ctx).await.unwrap(); + assert!(result.output.contains("eof_ok"), "{}", result.output); + assert!(started.elapsed() < std::time::Duration::from_secs(2)); + assert!(rx.try_recv().is_err(), "no stdin request should be raised"); } +#[cfg(unix)] #[tokio::test] -async fn test_stdin_forwarding_multiple_lines() { - let (tx, mut rx) = mpsc::unbounded_channel::(); +async fn test_command_has_no_controlling_tty_and_no_prompt_env() { let tool = BashTool::new(); - - // "head -n2" reads two lines - let input = json!({"command": "head -n2", "timeout": 15000}); - let ctx = make_ctx(Some(tx)); - - let tool_handle = tokio::spawn(async move { tool.execute(input, ctx).await }); - - // First line - let req1 = tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv()) - .await - .expect("timed out waiting for first stdin request") - .expect("channel closed"); - assert!( - req1.request_id.ends_with("-1"), - "first request should end with -1: {}", - req1.request_id - ); - req1.response_tx.send("line_one".to_string()).unwrap(); - - // Second line - let req2 = tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv()) - .await - .expect("timed out waiting for second stdin request") - .expect("channel closed"); - assert!( - req2.request_id.ends_with("-2"), - "second request should end with -2: {}", - req2.request_id - ); - req2.response_tx.send("line_two".to_string()).unwrap(); - - let result = tokio::time::timeout(std::time::Duration::from_secs(5), tool_handle) - .await - .expect("tool timed out") - .expect("tool panicked") - .expect("tool errored"); - - assert!( - result.output.contains("line_one"), - "missing line_one in: {}", - result.output - ); - assert!( - result.output.contains("line_two"), - "missing line_two in: {}", - result.output - ); + let input = json!({ + "command": "(exec 3/dev/null && echo has_tty || echo no_tty; echo GTP=$GIT_TERMINAL_PROMPT", + "timeout": 5000 + }); + let result = tool.execute(input, make_ctx(None)).await.unwrap(); + assert!(result.output.contains("no_tty"), "{}", result.output); + assert!(result.output.contains("GTP=0"), "{}", result.output); } #[tokio::test] @@ -420,10 +358,9 @@ async fn test_command_timeout_with_stdin_channel() { let (tx, _rx) = mpsc::unbounded_channel::(); let tool = BashTool::new(); - // `cat` blocks forever on stdin. With a short timeout and no stdin response, - // the command should be promoted to the background (kept running), not killed - // with an error. - let input = json!({"command": "cat", "timeout": 1000}); + // A long-running command with a stdin channel present should be promoted + // to the background (kept running) on timeout, not killed with an error. + let input = json!({"command": "sleep 30", "timeout": 1000}); let ctx = make_ctx(Some(tx)); let result = tool @@ -984,15 +921,9 @@ async fn test_background_command_without_timeout_keeps_running_past_default_fore #[cfg(unix)] #[tokio::test] async fn process_group_kill_guard_terminates_descendants() { + // build_shell_command runs the child in its own session (setsid), which + // also makes it a process group leader. let mut cmd = build_shell_command("sleep 60 & echo $!; wait"); - unsafe { - cmd.pre_exec(|| { - if libc::setpgid(0, 0) == -1 { - return Err(std::io::Error::last_os_error()); - } - Ok(()) - }); - } cmd.kill_on_drop(true).stdout(Stdio::piped()); let mut child = cmd.spawn().expect("spawn process group probe"); diff --git a/crates/jcode-app-core/src/tool/bg.rs b/crates/jcode-app-core/src/tool/bg.rs index 8da5829f5f2..64489ab7d08 100644 --- a/crates/jcode-app-core/src/tool/bg.rs +++ b/crates/jcode-app-core/src/tool/bg.rs @@ -417,6 +417,18 @@ async fn resolve_task_ids( } } +fn task_label(task: &background::TaskStatusFile) -> String { + crate::message::background_task_display_label(&task.tool_name, task.display_name.as_deref()) + .split_whitespace() + .collect::>() + .join(" ") +} + +/// Name what is being waited on so status rows say more than "a background task". +fn task_wait_reason(task: &background::TaskStatusFile) -> String { + format!("Waiting for {}", task_label(task)) +} + async fn wait_many_polling( manager: &background::BackgroundTaskManager, session_id: &str, @@ -471,7 +483,23 @@ async fn wait_many_polling( last_progress.insert(task.task_id.clone(), task.progress.clone()); } wait_guard.get_or_insert_with(|| { - crate::wait_registry::register(session_id, "Waiting for background tasks") + let running: Vec<_> = tasks + .iter() + .filter(|task| !is_terminal(&task.status)) + .collect(); + let reason = match running.as_slice() { + [task] => task_wait_reason(task), + _ => format!( + "Waiting for {} background tasks: {}", + running.len(), + running + .iter() + .map(|task| task_label(task)) + .collect::>() + .join(", ") + ), + }; + crate::wait_registry::register(session_id, &reason) }); tokio::time::sleep(Duration::from_secs(1)).await; } @@ -731,10 +759,6 @@ impl Tool for BgTool { let wait_duration = Duration::from_secs(capped_wait); // watch/delivery only updates notification settings and does not // block. Only the actual wait action owns foreground metadata. - let wait_reason = format!( - "Waiting for background task{}", - if task_ids.len() > 1 { "s" } else { "" } - ); if task_ids.len() > 1 { let mode = params.wait_mode.as_deref().unwrap_or("any"); @@ -781,14 +805,15 @@ impl Tool for BgTool { "Missing task_id; provide a task_id or use latest=true" )); }; - let blocking = if capped_wait > 0 { - manager - .status(&task_id) - .await - .is_some_and(|task| !is_terminal(&task.status)) - } else { - false - }; + let current = manager.status(&task_id).await; + let blocking = capped_wait > 0 + && current + .as_ref() + .is_some_and(|task| !is_terminal(&task.status)); + let wait_reason = current + .as_ref() + .map(task_wait_reason) + .unwrap_or_else(|| "Waiting for background task".to_string()); let wait_guard = blocking.then(|| crate::wait_registry::register(&ctx.session_id, &wait_reason)); let wait_result = manager @@ -939,7 +964,7 @@ mod tests { .unwrap(); assert_eq!( crate::wait_registry::current(sid).map(|(reason, _)| reason), - Some("Waiting for background tasks".into()) + Some("Waiting for test".into()) ); drop(future); assert_eq!(crate::wait_registry::current(sid), None); diff --git a/crates/jcode-app-core/src/tool/communicate.rs b/crates/jcode-app-core/src/tool/communicate.rs index 7e7698b021d..d183c28ca32 100644 --- a/crates/jcode-app-core/src/tool/communicate.rs +++ b/crates/jcode-app-core/src/tool/communicate.rs @@ -1,17 +1,16 @@ #![cfg_attr(test, allow(clippy::await_holding_lock))] use super::{Tool, ToolContext, ToolOutput}; -use crate::background::TaskResult; use crate::plan::PlanItem; use crate::protocol::{ AgentInfo, AgentStatusSnapshot, AwaitedMemberStatus, CommDeliveryMode, ContextEntry, HistoryMessage, PlanGraphStatus, Request, ServerEvent, SwarmChannelInfo, ToolCallSummary, comm_cleanup_candidate_session_ids, default_comm_await_target_statuses, - default_comm_cleanup_target_statuses, default_comm_run_await_statuses, - format_comm_awaited_members_with_reports, format_comm_channels, format_comm_context_entries, - format_comm_context_history, format_comm_members, format_comm_plan_followup, - format_comm_plan_status, format_comm_status_snapshot, format_comm_tool_summary, - latest_assistant_comm_report, resolve_optional_comm_target_session, + default_comm_cleanup_target_statuses, format_comm_awaited_members_with_reports, + format_comm_channels, format_comm_context_entries, format_comm_context_history, + format_comm_members, format_comm_plan_followup, format_comm_plan_status, + format_comm_status_snapshot, format_comm_tool_summary, latest_assistant_comm_report, + resolve_optional_comm_target_session, }; use anyhow::Result; use async_trait::async_trait; @@ -22,10 +21,6 @@ use std::collections::HashMap; const REQUEST_ID: u64 = 1; -/// Default number of workers `run_plan` keeps active at once when the caller -/// does not pass `concurrency_limit`. -const DEFAULT_RUN_PLAN_CONCURRENCY: usize = 4; - mod transport; use transport::{send_request, send_request_with_timeout}; @@ -76,10 +71,6 @@ fn default_cleanup_target_statuses() -> Vec { default_comm_cleanup_target_statuses() } -fn default_run_await_statuses() -> Vec { - default_comm_run_await_statuses() -} - fn cleanup_candidate_session_ids( owner_session_id: &str, members: &[AgentInfo], @@ -119,6 +110,7 @@ async fn fetch_swarm_members(session_id: &str) -> Result> { } } +#[cfg(test)] fn swarm_member_is_in_flight(member: &AgentInfo) -> bool { matches!( member.status.as_deref(), @@ -126,6 +118,7 @@ fn swarm_member_is_in_flight(member: &AgentInfo) -> bool { ) } +#[cfg(test)] fn coordination_in_flight_count( summary: &PlanGraphStatus, members: &[AgentInfo], @@ -141,40 +134,11 @@ fn coordination_in_flight_count( ) } -/// Sessions `run_plan` should await as genuinely in-flight on *this* plan. -/// -/// A member counts only when it is both in-flight (`queued`/`running`) **and** a -/// drivable worker for this run: headless, or owned by the coordinator -/// (`report_back_to_session_id == coordinator`). This deliberately excludes -/// independent, client-attached human sessions that merely share the swarm and -/// happen to sit in a `queued` status. Awaiting those would hang `run_plan` -/// forever even though every plan task is already terminal (they are never auto -/// driven), which is exactly the stall this scoping prevents. -/// -/// Pure over an already-fetched member list so the coordination loop can reuse -/// one `CommList` snapshot for both in-flight scoping and failure-wave -/// classification. -fn in_flight_swarm_session_ids(members: &[AgentInfo], coordinator_session_id: &str) -> Vec { - members - .iter() - .filter(|member| member.session_id != coordinator_session_id) - .filter(|member| swarm_member_is_in_flight(member)) - .filter(|member| swarm_member_is_drivable_worker(member, coordinator_session_id)) - .map(|member| member.session_id.clone()) - .collect() -} - -/// Fetch-and-filter convenience over [`in_flight_swarm_session_ids`] for call -/// sites that do not otherwise need the member snapshot. -async fn fetch_in_flight_swarm_sessions(session_id: &str) -> Result> { - let members = fetch_swarm_members(session_id).await?; - Ok(in_flight_swarm_session_ids(&members, session_id)) -} - -/// Whether `member` is a worker `run_plan` can rely on to autonomously execute an +/// Whether `member` is a worker the coordinator can rely on to autonomously execute an /// assignment (and therefore one it is safe to await): a spawned headless worker, /// or one owned by the coordinator that issued the run. Foreign client-attached -/// sessions are not drivable and must not gate `run_plan` completion. +/// sessions are not drivable and must not count toward its in-flight workers. +#[cfg(test)] fn swarm_member_is_drivable_worker(member: &AgentInfo, coordinator_session_id: &str) -> bool { member.is_headless.unwrap_or(false) || member.report_back_to_session_id.as_deref() == Some(coordinator_session_id) @@ -262,1154 +226,6 @@ async fn stop_swarm_sessions( WorkerCleanupOutcome { stopped, failed } } -/// Free swarm member capacity mid-run by stopping finished workers owned by -/// this coordinator. `run_plan` spawns a fresh worker per node by default and -/// normally cleans up only at the end of the run, so on large plans membership -/// grows monotonically toward the swarm member cap and fresh spawns start -/// getting refused. `exclude` protects workers assigned earlier in this loop -/// whose queued status may not have propagated yet. Returns how many workers -/// were stopped. -/// -/// Tradeoff: a stopped `ready` worker may have been a composite planner whose -/// synthesis node would otherwise be routed back to it (planner affinity). -/// Assignment falls back to a fresh or other eligible worker in that case, -/// which is an acceptable degradation when the alternative is aborting the run -/// at the member cap. -async fn cleanup_finished_workers_for_capacity( - ctx: &ToolContext, - exclude: &[String], - reporter: &RunPlanReporter, -) -> usize { - let Ok(members) = fetch_swarm_members(&ctx.session_id).await else { - return 0; - }; - let candidates: Vec = cleanup_candidate_session_ids( - &ctx.session_id, - &members, - &default_cleanup_target_statuses(), - &[], - false, - ) - .into_iter() - .filter(|session_id| !exclude.iter().any(|assigned| assigned == session_id)) - .collect(); - if candidates.is_empty() { - return 0; - } - let outcome = stop_swarm_sessions(ctx, candidates, false).await; - reporter - .log(&format!("member-cap recovery: {}", outcome.describe())) - .await; - outcome.stopped.len() -} - -/// How often the background progress card is refreshed from live plan state -/// while the driver is blocked awaiting workers. -const RUN_PLAN_PROGRESS_REFRESH_SECS: u64 = 15; - -/// Whether the driver should abandon the current member-await and start a new -/// coordination loop because the plan's ready frontier grew while it was -/// blocked. Pure for unit testing. -/// -/// `ready_baseline` is the set of ready item ids observed at the top of the -/// loop that started this await. Any *new* ready id means work the driver has -/// never had a chance to dispatch: a failed node re-queued via `swarm retry`, -/// a node unblocked by an externally-driven completion, or a gate-injected -/// gap. Comparing against the baseline (instead of `!ready.is_empty()`) is -/// what prevents wake storms: items that were already ready when the await -/// began (e.g. just-assigned tasks still momentarily `queued`, or ready nodes -/// that could not be assigned to any drivable worker) do not re-trigger, so a -/// permanently-stuck ready node wakes the driver at most once per await. -fn await_should_wake_for_new_ready( - ready_baseline: &std::collections::HashSet, - summary: &PlanGraphStatus, -) -> bool { - summary - .ready_ids - .iter() - .any(|id| !ready_baseline.contains(id)) -} - -async fn await_swarm_progress( - ctx: &ToolContext, - session_ids: Vec, - timeout_minutes: u64, - reporter: &RunPlanReporter, - assignment_count: usize, - ready_baseline: &std::collections::HashSet, -) -> Result<()> { - let request = Request::CommAwaitMembers { - id: REQUEST_ID, - session_id: ctx.session_id.clone(), - target_status: default_run_await_statuses(), - session_ids, - mode: Some("any".to_string()), - timeout_secs: Some(timeout_minutes.max(1) * 60), - // run_plan needs the result inline to drive its coordination loop, so it - // explicitly opts out of the background-by-default behavior. - background: false, - notify: false, - wake: false, - }; - let socket_timeout = std::time::Duration::from_secs(timeout_minutes.max(1) * 60 + 30); - let await_members = send_request_with_timeout(request, Some(socket_timeout)); - tokio::pin!(await_members); - - // While blocked on the await (potentially many minutes), periodically - // re-read live plan + member state and push it to the progress card. - // Without this, worker completions and externally-assigned work (manual - // `assign_task`) only surface at the driver's own wave boundaries, so the - // card goes stale for the whole await. Refresh failures are ignored: the - // card is best-effort and the await result is what drives the loop. - let refresh_period = std::time::Duration::from_secs(RUN_PLAN_PROGRESS_REFRESH_SECS); - let mut refresh = - tokio::time::interval_at(tokio::time::Instant::now() + refresh_period, refresh_period); - refresh.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); - - // Detached plan drivers must not label the foreground agent as waiting. - let _wait_guard = (!reporter.is_background()) - .then(|| crate::wait_registry::register(&ctx.session_id, "Waiting for swarm members")); - let response = loop { - tokio::select! { - result = &mut await_members => break result, - _ = refresh.tick() => { - let summary = match fetch_plan_status(&ctx.session_id).await { - Ok(summary) => summary, - Err(_) => continue, - }; - if reporter.is_background() { - let live_active = fetch_in_flight_swarm_sessions(&ctx.session_id) - .await - .map(|sessions| sessions.len()) - .unwrap_or(0); - let (completed, total, message) = - run_plan_progress_snapshot(&summary, live_active, assignment_count); - reporter.progress(completed, total, message).await; - } - // Ready frontier grew while blocked (a `swarm retry` re-queued - // failed nodes, an external completion unblocked work, a gate - // injected gaps): return to the coordination loop so the new - // work is dispatched under the normal budget instead of - // waiting out the current wave. The abandoned await is a - // plain request future; dropping it cancels only our wait, - // not the workers. - if await_should_wake_for_new_ready(ready_baseline, &summary) { - reporter - .log("ready frontier grew during await (retry/requeue or external unblock); re-entering dispatch loop") - .await; - return Ok(()); - } - } - } - }; - - match response { - Ok(ServerEvent::CommAwaitMembersResponse { - completed, summary, .. - }) => { - if completed { - Ok(()) - } else { - Err(anyhow::anyhow!( - "Timed out waiting for swarm progress: {}", - summary - )) - } - } - Ok(response) => ensure_success(&response), - Err(e) => Err(anyhow::anyhow!( - "Failed while awaiting swarm progress: {}", - e - )), - } -} - -/// Decide how many swarm workers `run_plan` keeps active at once: an explicit -/// limit wins (clamped to >= 1), otherwise the small default. -fn resolve_run_plan_concurrency(requested: Option) -> usize { - requested.map_or(DEFAULT_RUN_PLAN_CONCURRENCY, |explicit| explicit.max(1)) -} - -/// Running tally of how well a `run_plan` drive used its concurrency budget. -/// -/// Tracks peak parallelism and how often open slots sat idle with no ready -/// work, so the terminal report shows whether the budget was used. -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] -struct RunPlanUtilization { - /// Highest number of simultaneously in-flight tasks observed. - peak_in_flight: usize, - /// Coordination loops observed. - loops: usize, - /// Loops where open worker slots existed but the plan had nothing ready to - /// dispatch into them (budget idle due to graph narrowness, not the cap). - starved_loops: usize, -} - -impl RunPlanUtilization { - /// Record one coordination loop. `open_slots` is `None` when the budget is - /// unbounded (`concurrency_limit == usize::MAX`): an infinite budget has no - /// meaningful starvation denominator, so only peak parallelism is tracked. - fn record_loop(&mut self, in_flight: usize, open_slots: Option, dispatched: usize) { - self.loops += 1; - self.peak_in_flight = self.peak_in_flight.max(in_flight + dispatched); - if let Some(open_slots) = open_slots - && open_slots > 0 - && dispatched < open_slots - { - self.starved_loops += 1; - } - } - - /// Render the utilization line for the terminal report. - fn report(&self, concurrency_limit: usize) -> String { - let limit_label = if concurrency_limit == usize::MAX { - "unbounded".to_string() - } else { - concurrency_limit.to_string() - }; - format!( - "Budget utilization: peak {} of {} concurrent worker slot(s); {} of {} loop(s) had idle capacity with nothing ready.", - self.peak_in_flight, limit_label, self.starved_loops, self.loops - ) - } -} - -/// Extract the background task id from its output file path -/// (`.output`), mirroring the bash tool's convention so progress -/// updates can be routed back to the background task manager. -fn task_id_from_output_path(path: &std::path::Path) -> Option<&str> { - path.file_name()?.to_str()?.strip_suffix(".output") -} - -/// Progress/log sink for a `run_plan` execution. -/// -/// In background mode this appends human-readable lines to the background -/// task's output file and pushes determinate progress (terminal/total plan -/// nodes) into the background task manager, so the UI renders a live swarm -/// progress card and `bg status` stays meaningful. In inline (blocking) mode -/// every method is a no-op. -struct RunPlanReporter { - task_id: Option, - output_path: Option, -} - -impl RunPlanReporter { - fn inline() -> Self { - Self { - task_id: None, - output_path: None, - } - } - - fn background(output_path: &std::path::Path) -> Self { - Self { - task_id: task_id_from_output_path(output_path).map(str::to_string), - output_path: Some(output_path.to_path_buf()), - } - } - - /// Whether this reporter feeds a live background progress card (inline - /// reporters are no-ops, so refresh polling would be wasted requests). - fn is_background(&self) -> bool { - self.task_id.is_some() - } - - async fn log(&self, line: &str) { - let Some(path) = &self.output_path else { - return; - }; - use tokio::io::AsyncWriteExt; - if let Ok(mut file) = tokio::fs::OpenOptions::new() - .create(true) - .append(true) - .open(path) - .await - { - let _ = file.write_all(format!("{}\n", line).as_bytes()).await; - } - } - - async fn progress(&self, terminal: usize, total: usize, message: String) { - let Some(task_id) = &self.task_id else { - return; - }; - let progress = crate::bus::BackgroundTaskProgress { - kind: crate::bus::BackgroundTaskProgressKind::Determinate, - percent: None, - message: Some(message), - current: Some(terminal as u64), - total: Some(total as u64), - unit: Some("nodes".to_string()), - eta_seconds: None, - updated_at: chrono::Utc::now().to_rfc3339(), - source: crate::bus::BackgroundTaskProgressSource::Reported, - } - .normalize(); - let _ = crate::background::global() - .update_progress(task_id, progress) - .await; - } - - /// Record an explicit checkpoint (a JCODE_CHECKPOINT-style milestone) on - /// the background task, so pause/alert moments surface as checkpoint events - /// in the UI instead of only trailing the output log. No-op inline. - async fn checkpoint(&self, message: &str) { - self.log(message).await; - let Some(task_id) = &self.task_id else { - return; - }; - let progress = crate::bus::BackgroundTaskProgress { - kind: crate::bus::BackgroundTaskProgressKind::Indeterminate, - percent: None, - message: Some(message.to_string()), - current: None, - total: None, - unit: None, - eta_seconds: None, - updated_at: chrono::Utc::now().to_rfc3339(), - source: crate::bus::BackgroundTaskProgressSource::Reported, - } - .normalize(); - let _ = crate::background::global() - .update_checkpoint(task_id, progress) - .await; - } - - /// Rewrite the output file so `summary` leads and the progressive log - /// trails it. Background completion previews take the first ~500 chars of - /// the output file, so the terminal summary must come first for the - /// agent's wake notification to be useful. - async fn finalize(&self, summary: &str) { - let Some(path) = &self.output_path else { - return; - }; - let log = tokio::fs::read_to_string(path).await.unwrap_or_default(); - let content = if log.trim().is_empty() { - format!("{}\n", summary) - } else { - format!("{}\n\n--- run log ---\n{}", summary, log) - }; - let _ = tokio::fs::write(path, content).await; - } -} - -/// Per-process registry of sessions with a `run_plan` driver claimed or -/// running. The duplicate-driver guard does its check-and-insert under this -/// one lock, so two `run_plan` calls racing in the same batch cannot both -/// pass. Deliberately per-process: a stale `Running` status file left on disk -/// by a previous (reloaded/crashed) server process must never block -/// restarting the driver. -fn run_plan_driver_claims() -> &'static std::sync::Mutex> { - static CLAIMS: std::sync::OnceLock>> = - std::sync::OnceLock::new(); - CLAIMS.get_or_init(|| std::sync::Mutex::new(HashMap::new())) -} - -enum RunPlanDriverClaim { - /// Claimed, background task not spawned yet. - Starting, - /// Driver spawned as this background task. - Running(String), -} - -enum RunPlanDriverClaimResult { - Claimed(RunPlanClaimGuard), - /// A driver already holds the claim. Carries its task id when known - /// (None while the winner is still between claim and spawn). - AlreadyRunning(Option), -} - -/// RAII holder for a `Starting` claim. Dropping it without -/// [`RunPlanClaimGuard::record_task`] releases the claim, so a cancelled or -/// failed startup path cannot permanently block `run_plan` for the session. -struct RunPlanClaimGuard { - session_id: String, - defused: bool, -} - -impl RunPlanClaimGuard { - /// Upgrade the claim to `Running(task_id)`. From here staleness is - /// resolved via `BackgroundTaskManager::is_live_task`: once the driver - /// task finishes (and is pruned from the live map), the next claim - /// replaces this entry. - fn record_task(mut self, task_id: &str) { - let mut claims = run_plan_driver_claims() - .lock() - .unwrap_or_else(|poisoned| poisoned.into_inner()); - claims.insert( - self.session_id.clone(), - RunPlanDriverClaim::Running(task_id.to_string()), - ); - self.defused = true; - } -} - -impl Drop for RunPlanClaimGuard { - fn drop(&mut self) { - if self.defused { - return; - } - let mut claims = run_plan_driver_claims() - .lock() - .unwrap_or_else(|poisoned| poisoned.into_inner()); - // Only release a claim this guard still owns. - if matches!( - claims.get(&self.session_id), - Some(RunPlanDriverClaim::Starting) - ) { - claims.remove(&self.session_id); - } - } -} - -/// Atomically claim the `run_plan` driver slot for `session_id`. -/// -/// Check-and-insert happens under one lock. An existing `Running` claim only -/// blocks while its background task is still live in this process; a claim -/// left by a finished (pruned) or pre-reload driver is replaced. -fn try_claim_run_plan_driver( - manager: &crate::background::BackgroundTaskManager, - session_id: &str, -) -> RunPlanDriverClaimResult { - let mut claims = run_plan_driver_claims() - .lock() - .unwrap_or_else(|poisoned| poisoned.into_inner()); - match claims.get(session_id) { - Some(RunPlanDriverClaim::Starting) => { - return RunPlanDriverClaimResult::AlreadyRunning(None); - } - Some(RunPlanDriverClaim::Running(task_id)) => { - if manager.is_live_task(task_id) { - return RunPlanDriverClaimResult::AlreadyRunning(Some(task_id.clone())); - } - // Stale claim: the driver task already finished or belonged to a - // previous process image. Fall through and take over. - } - None => {} - } - claims.insert(session_id.to_string(), RunPlanDriverClaim::Starting); - RunPlanDriverClaimResult::Claimed(RunPlanClaimGuard { - session_id: session_id.to_string(), - defused: false, - }) -} - -/// Make the swarm plan mirror the caller's todo list: one item per lane. -/// -/// `launch` calls this every time, so the plan can never drift from the -/// todos; the server keeps the status and owner of lanes already running. -/// The legacy `run_plan` only seeds an empty plan, so a hand-written -/// `propose_plan` still wins there. Returns quietly with no open todos. -async fn sync_plan_from_todos(ctx: &ToolContext, only_if_empty: bool) -> Result<()> { - if only_if_empty - && fetch_plan_status(&ctx.session_id) - .await - .is_ok_and(|summary| summary.item_count > 0) - { - return Ok(()); - } - let todos = crate::todo::load_todos(&ctx.session_id).unwrap_or_default(); - let items = crate::server::swarm_todo_link::plan_items_from_todos(&todos); - if items.is_empty() { - return Ok(()); - } - let request = Request::CommProposePlan { - id: REQUEST_ID, - session_id: ctx.session_id.clone(), - items, - }; - let response = send_request(request) - .await - .map_err(|e| anyhow::anyhow!("Failed to seed swarm plan from todos: {}", e))?; - ensure_success(&response) -} - -/// Drive `run_plan` as a managed background task and return immediately. -/// -/// The coordinating agent stays responsive: the plan loop runs inside the -/// shared `BackgroundTaskManager` (task id, progress card, `bg` tool -/// integration), and completion is delivered through the standard notify/wake -/// path like any other background task. -async fn run_swarm_plan_in_background( - ctx: &ToolContext, - params: CommunicateInput, -) -> Result { - // Validate the plan inline so an empty/broken plan errors immediately - // instead of as a delayed background failure. - let initial_summary = fetch_plan_status(&ctx.session_id).await?; - if initial_summary.item_count == 0 { - return Ok(ToolOutput::new("No swarm plan items to run.")); - } - - // Refuse to start a second driver for the same session: two concurrent - // run_plan loops would race on assignments and double-spawn workers. The - // claim is check-and-insert under one lock, so two run_plan calls in the - // same batch cannot both pass. Only drivers live in this process count; a - // stale "running" status file left by a server reload must not block - // restarting the driver (the claim map is per-process and dead task ids - // fail the is_live_task check). - let manager = crate::background::global(); - let claim = match try_claim_run_plan_driver(manager, &ctx.session_id) { - RunPlanDriverClaimResult::Claimed(claim) => claim, - RunPlanDriverClaimResult::AlreadyRunning(existing) => { - return Ok(ToolOutput::new(match existing { - Some(task_id) => format!( - "A swarm run_plan driver is already running for this session (task {}). \ - Check it with `bg action=\"status\" task_id=\"{}\"` or `swarm plan_status` instead of starting another.", - task_id, task_id - ), - None => "A swarm run_plan driver is already starting for this session. \ - Check it with `swarm plan_status` instead of starting another." - .to_string(), - })); - } - }; - - let notify = params.notify.unwrap_or(true); - let wake = params.wake.unwrap_or(true); - // Keep the display name free of the "·" separator used by the background - // notification markdown header, or downstream parsing mis-splits the label. - let display_name = format!("run_plan ({} tasks)", initial_summary.item_count); - - let bg_ctx = ctx.clone(); - let info = crate::background::global() - .spawn_with_notify( - "swarm", - Some(display_name.clone()), - &ctx.session_id, - notify, - wake, - move |output_path| async move { - let reporter = RunPlanReporter::background(&output_path); - match run_swarm_plan_to_terminal(&bg_ctx, ¶ms, &reporter).await { - Ok(output) => { - reporter.finalize(&output.output).await; - Ok(TaskResult::completed(Some(0))) - } - Err(error) => { - let message = format!("run_plan failed: {}", error); - reporter.finalize(&message).await; - Ok(TaskResult::failed(None, message)) - } - } - }, - ) - .await; - claim.record_task(&info.task_id); - - let delivery_note = if wake { - "You'll be woken with the result when the plan reaches a terminal state." - } else if notify { - "A notification will appear when the plan reaches a terminal state." - } else { - "Notifications disabled. Use the `bg` tool to check status." - }; - let output = format!( - "🐝 Swarm plan running in background.\n\n\ - Task ID: {}\n\ - Plan: {} task(s)\n\ - Output file: {}\n\n\ - {}\n\ - Check progress: use the `bg` tool with action=\"status\" and task_id=\"{}\", or `swarm plan_status`.\n\ - Note: a server reload stops this driver (workers keep running); rerun `swarm run_plan` to resume driving the same plan.", - info.task_id, - initial_summary.item_count, - info.output_file.display(), - delivery_note, - info.task_id, - ); - - Ok(ToolOutput::new(output) - .with_title(format!("Swarm run_plan in background: {}", info.task_id)) - .with_metadata(json!({ - "background": true, - "swarm": true, - "task_id": info.task_id, - "display_name": display_name, - "output_file": info.output_file.to_string_lossy(), - "status_file": info.status_file.to_string_lossy(), - }))) -} - -/// Hint appended to every `run_plan` driver failure: the driver exits without -/// the end-of-run cleanup, so spawned workers keep running even when -/// `retain_agents=false`, and the caller must know how to stop or resume them. -const RUN_PLAN_WORKER_RETENTION_HINT: &str = "\nSpawned workers were retained; run `swarm cleanup` to stop them, rerun `swarm run_plan` to resume driving the same plan, or `swarm plan_status` to inspect."; - -/// Append the worker-retention hint to a driver failure message, idempotently -/// so wrappers that re-report an already-hinted error do not duplicate it. -fn with_worker_retention_hint(message: String) -> String { - if message.contains("swarm cleanup") { - message - } else { - format!("{message}{RUN_PLAN_WORKER_RETENTION_HINT}") - } -} - -/// How the `run_plan` assignment loop should react to a `CommAssignNext` error. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum AssignErrorAction { - /// No more runnable work or no eligible workers: stop assigning this loop - /// and continue with in-flight work. - BreakGracefully, - /// The swarm hit its total member cap so fresh spawns are refused: free - /// finished owned workers and/or fall back to reusing ready workers instead - /// of aborting the whole run. - RecoverCapacity, - /// Anything else is a real failure. - Fail, -} - -fn classify_assign_error(message: &str) -> AssignErrorAction { - if message.contains("No runnable unassigned tasks") - || message.contains("No ready or completed swarm agents") - { - AssignErrorAction::BreakGracefully - } else if message.contains("Swarm member limit reached") { - AssignErrorAction::RecoverCapacity - } else { - AssignErrorAction::Fail - } -} - -/// Next step for a slot whose assignment was refused by the member cap. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum CapRecoveryStep { - /// Cleanup freed capacity: retry the slot keeping the fresh-spawn preference. - RetryFresh, - /// Nothing was freed: retry the slot in reuse-only mode (no spawning). - RetryReuse, - /// Recovery already ran and the cap still refuses this slot: stop assigning - /// this loop and continue with in-flight work. - GiveUp, -} - -/// Pure recovery policy for a member-cap refusal, keyed on how many times this -/// slot already hit the cap (`cap_hits`) and how many workers the incremental -/// cleanup freed. Kept side-effect free so the fallback contract is unit -/// testable without a live swarm. -fn cap_recovery_step(cap_hits: usize, freed: usize) -> CapRecoveryStep { - if cap_hits > 1 { - CapRecoveryStep::GiveUp - } else if freed > 0 { - CapRecoveryStep::RetryFresh - } else { - CapRecoveryStep::RetryReuse - } -} - -/// Count each plan node at most once as terminal: completed, failed, blocked, -/// and cycle sets overlap in places (and failed nodes appear in none of the -/// legacy three), so a plain sum both over- and under-counts. -fn plan_terminal_node_count(summary: &PlanGraphStatus) -> usize { - summary - .completed_ids - .iter() - .chain(summary.failed_ids.iter()) - .chain(summary.blocked_ids.iter()) - .chain(summary.cycle_ids.iter()) - .collect::>() - .len() -} - -/// Numbers for the `run_plan` background progress card. Pure for unit testing. -/// -/// The percent-driving pair is `(completed, total)`: only *completed* nodes -/// count toward 100%, so a run where most nodes failed reads as mostly -/// unfinished instead of "98% complete" (failed/blocked counts are surfaced in -/// the message instead). `live_active` is the count of in-flight worker -/// sessions observed from member state; the card shows whichever of plan -/// execution state (`active_ids`) or live member state is larger, so nodes -/// assigned outside this driver (e.g. manual `assign_task`) still show as -/// active. -fn run_plan_progress_snapshot( - summary: &PlanGraphStatus, - live_active: usize, - assignment_count: usize, -) -> (usize, usize, String) { - let completed = summary.completed_ids.len(); - let active = summary.active_ids.len().max(live_active); - let message = format!( - "completed {} · failed {} · blocked {} · active {} · assignments {}", - completed, - summary.failed_ids.len(), - summary.blocked_ids.len(), - active, - assignment_count - ); - (completed, summary.item_count, message) -} - -/// Terminal-state summary line for `run_plan`, including failed nodes so a run -/// with failures never reads like a clean finish. Pure for unit testing. -fn format_run_plan_terminal_summary( - loop_count: usize, - summary: &PlanGraphStatus, - assignment_count: usize, -) -> String { - let mut output = format!( - "Swarm plan reached terminal/blocked state after {} loop(s). completed={} failed={} blocked={} cycles={} active={} assignments={}", - loop_count, - summary.completed_ids.len(), - summary.failed_ids.len(), - summary.blocked_ids.len(), - summary.cycle_ids.len(), - summary.active_ids.len(), - assignment_count - ); - if !summary.failed_ids.is_empty() { - output.push_str(&format!( - "\nFailed nodes: {}. This run did NOT finish cleanly; inspect them with `swarm plan_status` and retry or salvage before trusting the result.", - summary.failed_ids.join(", ") - )); - // Recorded failure reasons make the summary self-explanatory: a wave - // of "task failed: ... 401 Unauthorized" lines names the root cause - // without another plan_status round-trip. - for id in &summary.failed_ids { - if let Some(reason) = summary.failed_reasons.get(id) { - output.push_str(&format!("\n {}: {}", id, reason)); - } - } - } - output -} - -/// Minimum number of credential-failed workers that count as a wave rather -/// than an isolated bad worker. -const CREDENTIAL_FAILURE_WAVE_MIN_WORKERS: usize = 2; - -/// How recent a worker's credential failure must be (via `status_age_secs`) to -/// count toward a wave. Old failed workers from a previous, already-diagnosed -/// wave must not re-trip the breaker after the user fixes auth and retries. -const CREDENTIAL_FAILURE_WAVE_WINDOW_SECS: u64 = 60; - -/// A wave of worker failures that share one credential-shaped root cause. -/// -/// When dispatched workers die within seconds of assignment with 401 / -/// `invalid_grant` / `authentication_error`-style errors, the credential is -/// broken for every worker on that route: assigning more nodes only fails more -/// of the plan. Detecting the wave lets `run_plan` pause dispatching and -/// surface the one real fix instead of silently burning the graph. -#[derive(Debug, Clone, PartialEq, Eq)] -struct CredentialFailureWave { - /// Failed worker sessions in the wave. - session_ids: Vec, - /// Representative failure detail (first observed). - sample_detail: String, - /// Provider named by the failing workers, when known (e.g. "anthropic"). - provider: Option, -} - -/// Detect a credential-failure wave in a swarm member snapshot. -/// -/// A wave exists when, with **zero completed plan nodes**, at least -/// [`CREDENTIAL_FAILURE_WAVE_MIN_WORKERS`] drivable workers sit in `failed` -/// status whose detail classifies as a credential failure (via the shared -/// [`crate::provider::error_looks_like_credential_failure`] classifier) and -/// whose failure is recent (`status_age_secs <= window_secs`). Pure over its -/// inputs so the breaker contract is unit-testable without a live swarm. -fn detect_credential_failure_wave( - members: &[AgentInfo], - coordinator_session_id: &str, - completed_node_count: usize, - window_secs: u64, -) -> Option { - if completed_node_count > 0 { - return None; - } - let mut session_ids = Vec::new(); - let mut sample_detail: Option = None; - let mut provider: Option = None; - for member in members { - if member.session_id == coordinator_session_id { - continue; - } - if !swarm_member_is_drivable_worker(member, coordinator_session_id) { - continue; - } - if member.status.as_deref() != Some("failed") { - continue; - } - let Some(detail) = member.detail.as_deref() else { - continue; - }; - if !crate::provider::error_looks_like_credential_failure(detail) { - continue; - } - // Require a known, recent failure age: stale failed workers (or ones - // whose age did not propagate) must not re-trip the breaker. - if !matches!(member.status_age_secs, Some(age) if age <= window_secs) { - continue; - } - session_ids.push(member.session_id.clone()); - if sample_detail.is_none() { - sample_detail = Some(detail.to_string()); - } - if provider.is_none() { - provider = member.provider_name.clone(); - } - } - if session_ids.len() < CREDENTIAL_FAILURE_WAVE_MIN_WORKERS { - return None; - } - Some(CredentialFailureWave { - session_ids, - sample_detail: sample_detail.unwrap_or_default(), - provider, - }) -} - -/// The `jcode login` invocation most likely to fix a credential wave for -/// `provider`, mapping provider names to their login provider keys. -fn credential_login_fix_hint(provider: Option<&str>) -> String { - let lowered = provider.map(str::to_ascii_lowercase); - let target = match lowered.as_deref() { - Some("anthropic" | "claude") => "claude", - Some("openai" | "codex") => "openai", - Some("google" | "gemini") => "gemini", - Some(other) if !other.trim().is_empty() => other, - _ => "", - }; - format!("`jcode login --provider {target}`") -} - -/// Actionable pause message for a credential-failure wave: names the failed -/// workers, the credential-shaped root cause, and the fix. Pure for unit -/// testing; used both as the run error and as the swarm broadcast body. -fn format_credential_failure_wave_error(wave: &CredentialFailureWave, window_secs: u64) -> String { - format!( - "run_plan paused dispatching: {count} worker(s) failed within {window_secs}s with \ - credential/auth failures and no plan node has completed (e.g. {first}: \"{sample}\"). \ - A broken credential (expired OAuth session, revoked refresh token, or invalid API key) \ - fails every worker on that route, so assigning more nodes would only fail more of the \ - plan. Fix auth first: run {login_hint} (or pin a working API-key route), then requeue \ - the failed nodes (`swarm retry`) and run `swarm run_plan` again.", - count = wave.session_ids.len(), - first = wave - .session_ids - .first() - .map(String::as_str) - .unwrap_or("worker"), - sample = wave.sample_detail, - login_hint = credential_login_fix_hint(wave.provider.as_deref()), - ) -} - -/// Best-effort broadcast of a plan-level alert to the whole swarm, so live -/// members and attached UIs see why dispatch stopped. -async fn broadcast_plan_alert(ctx: &ToolContext, message: &str) -> Result<()> { - let request = Request::CommMessage { - id: REQUEST_ID, - from_session: ctx.session_id.clone(), - message: message.to_string(), - to_session: None, - channel: None, - wake: None, - delivery: None, - tldr: Some("run_plan paused: credential failure wave; fix auth then retry".to_string()), - to_swarm: None, - require_target: false, - }; - match send_request(request).await { - Ok(response) => ensure_success(&response), - Err(e) => Err(anyhow::anyhow!("Failed to broadcast plan alert: {}", e)), - } -} - -async fn run_swarm_plan_to_terminal( - ctx: &ToolContext, - params: &CommunicateInput, - reporter: &RunPlanReporter, -) -> Result { - // Every driver-failure exit (assignment failure, await timeout, stall, - // max-loops, even a mid-run plan-status fetch error) leaves spawned workers - // running because the end-of-run cleanup never executes, regardless of - // retain_agents. Append the retention hint uniformly here so no failure - // path can forget it. - match run_swarm_plan_loop(ctx, params, reporter).await { - Ok(output) => Ok(output), - Err(error) => Err(anyhow::anyhow!(with_worker_retention_hint( - error.to_string() - ))), - } -} - -async fn run_swarm_plan_loop( - ctx: &ToolContext, - params: &CommunicateInput, - reporter: &RunPlanReporter, -) -> Result { - // Fail fast when the plan cannot be read before spawning any workers. - fetch_plan_status(&ctx.session_id).await?; - let concurrency_limit = resolve_run_plan_concurrency(params.concurrency_limit); - let timeout_minutes = params.timeout_minutes.unwrap_or(60).max(1); - let retain_agents = params.retain_agents.unwrap_or(false); - let spawn_if_needed = params.spawn_if_needed.or(Some(true)); - // Default to a fresh worker per task. Reusing a worker that already completed - // a *different* task carries that conversation into the next assignment, and - // the model often just re-reports its prior result instead of doing the new - // work. Callers can opt into reuse with `prefer_spawn=false`. - let prefer_spawn = params.prefer_spawn.or(Some(true)); - let mut assignment_count = 0usize; - let mut loop_count = 0usize; - let max_loops = 200usize; - let mut utilization = RunPlanUtilization::default(); - // Consecutive loops where an active task exists but no drivable worker is - // awaitable. This is normally a brief transition (a composite re-waking to - // synthesize, or a just-finished task whose member status has not propagated), - // so we back off and re-check a few times before declaring a real stall. - let mut transient_stall_loops = 0usize; - let max_transient_stall_loops = 5usize; - - loop { - loop_count += 1; - if loop_count > max_loops { - return Err(anyhow::anyhow!( - "run_plan exceeded {} coordination loops; leaving workers untouched for inspection", - max_loops - )); - } - - let summary = fetch_plan_status(&ctx.session_id).await?; - if summary.item_count == 0 { - return Ok(ToolOutput::new("No swarm plan items to run.")); - } - - let members = fetch_swarm_members(&ctx.session_id).await?; - let in_flight_sessions = in_flight_swarm_session_ids(&members, &ctx.session_id); - - // Credential-failure circuit breaker: when a wave of workers dies with - // 401/invalid_grant-style auth errors and nothing has completed, the - // credential is broken for every worker on that route. Pausing here - // (before the terminal check) means even a fully-burned first wave - // surfaces the root cause and the fix instead of a bare - // "failed=N" terminal summary with no explanation. - if let Some(wave) = detect_credential_failure_wave( - &members, - &ctx.session_id, - summary.completed_ids.len(), - CREDENTIAL_FAILURE_WAVE_WINDOW_SECS, - ) { - let message = - format_credential_failure_wave_error(&wave, CREDENTIAL_FAILURE_WAVE_WINDOW_SECS); - reporter.checkpoint(&message).await; - if let Err(error) = broadcast_plan_alert(ctx, &message).await { - reporter - .log(&format!( - "failed to broadcast credential-failure alert to the swarm: {error}" - )) - .await; - } - return Err(anyhow::anyhow!(message)); - } - - let terminal_count = plan_terminal_node_count(&summary); - let (progress_completed, progress_total, progress_message) = - run_plan_progress_snapshot(&summary, in_flight_sessions.len(), assignment_count); - reporter - .progress(progress_completed, progress_total, progress_message) - .await; - let no_more_runnable = summary.active_ids.is_empty() - && summary.next_ready_ids.is_empty() - && in_flight_sessions.is_empty(); - if no_more_runnable || terminal_count >= summary.item_count { - let mut output = - format_run_plan_terminal_summary(loop_count, &summary, assignment_count); - output.push_str(&format!("\n{}", utilization.report(concurrency_limit))); - if retain_agents { - output.push_str("\nRetained spawned workers because retain_agents=true."); - } else { - // Run the automatic end-of-plan cleanup with a sanitized input: - // `force`, `session_ids`, and `target_status` on the run_plan - // call are meant for explicit stop/cleanup/await actions, and - // leaking `force=true` here would force-stop every terminal - // swarm member (including user-created idle sessions) instead - // of only the workers this coordinator owns. - let cleanup_params = CommunicateInput { - force: None, - session_ids: None, - target_status: None, - ..params.clone() - }; - let cleanup = cleanup_swarm_workers(ctx, &cleanup_params).await?; - output.push_str(&format!("\n{}", cleanup)); - } - return Ok(ToolOutput::new(output)); - } - - let active_count = summary.active_ids.len().max(in_flight_sessions.len()); - let available_slots = concurrency_limit.saturating_sub(active_count); - let mut assigned_sessions = Vec::new(); - // Member-cap fallback state, reset each coordination loop. When the swarm - // hits its total member cap, fresh spawns are refused; instead of aborting - // the whole run we first free finished owned workers (incremental cleanup) - // and retry, then fall back to reuse-only assignment (no spawning), and - // only after that stop assigning and continue with in-flight work. - let mut cap_hits = 0usize; - let mut reuse_only = false; - let mut slots_remaining = available_slots; - while slots_remaining > 0 { - let request = Request::CommAssignNext { - id: REQUEST_ID, - session_id: ctx.session_id.clone(), - target_session: params.target_session.clone(), - working_dir: params.working_dir.clone(), - prefer_spawn: if reuse_only { - Some(false) - } else { - prefer_spawn - }, - spawn_if_needed: if reuse_only { - Some(false) - } else { - spawn_if_needed - }, - message: params.message.clone(), - model: params.model.clone(), - // Worker effort is not agent-controlled: the server applies - // `agents.swarm_effort` (default "low"). - effort: None, - }; - match send_request(request).await { - Ok(ServerEvent::CommAssignTaskResponse { - task_id, - target_session, - .. - }) => { - assignment_count += 1; - slots_remaining -= 1; - reporter - .log(&format!("assigned {} -> {}", task_id, target_session)) - .await; - assigned_sessions.push(target_session); - } - Ok(ServerEvent::Error { message, .. }) => { - match classify_assign_error(&message) { - AssignErrorAction::BreakGracefully => break, - AssignErrorAction::RecoverCapacity => { - cap_hits += 1; - let freed = if cap_hits == 1 { - cleanup_finished_workers_for_capacity( - ctx, - &assigned_sessions, - reporter, - ) - .await - } else { - 0 - }; - match cap_recovery_step(cap_hits, freed) { - CapRecoveryStep::RetryFresh => { - // Cleanup freed member slots; retry this slot - // with the fresh-spawn preference intact. - } - CapRecoveryStep::RetryReuse => { - reuse_only = true; - reporter - .log( - "member cap reached and no finished workers to free; \ - falling back to reusing ready workers (prefer_spawn=false)", - ) - .await; - } - CapRecoveryStep::GiveUp => { - reporter - .log( - "member cap still reached after recovery; \ - continuing with in-flight work", - ) - .await; - break; - } - } - } - AssignErrorAction::Fail => { - return Err(anyhow::anyhow!(message)); - } - } - } - Ok(response) => ensure_success(&response)?, - Err(e) => return Err(anyhow::anyhow!("Failed to assign next swarm task: {}", e)), - } - } - utilization.record_loop( - active_count, - (concurrency_limit != usize::MAX).then_some(available_slots), - assigned_sessions.len(), - ); - - let await_sessions = if assigned_sessions.is_empty() { - in_flight_sessions - } else { - assigned_sessions - }; - - if await_sessions.is_empty() { - if active_count > 0 { - // An active task exists but nothing drivable is awaitable. This is - // usually transient: a composite is re-waking to synthesize, or a - // worker just finished and its member status has not propagated yet. - // Re-check a few times with a short backoff before giving up, and - // bail early if the plan reaches a terminal state in the meantime. - transient_stall_loops += 1; - if transient_stall_loops <= max_transient_stall_loops { - tokio::time::sleep(std::time::Duration::from_millis(500)).await; - continue; - } - return Err(anyhow::anyhow!( - "run_plan found {} active task(s) but no running swarm members to await after {} re-checks; inspect plan_status and member list before retrying", - active_count, - max_transient_stall_loops - )); - } - // Nothing was assigned this loop, nothing is in flight, yet the plan is - // not terminal. This means some non-terminal task cannot be driven, e.g. - // it is already assigned to a session run_plan cannot drive (a foreign or - // stale member). Spinning here would busy-loop to the max-loop cap, so - // surface the stuck state with the offending tasks instead. - let stuck: Vec = summary - .next_ready_ids - .iter() - .chain(summary.ready_ids.iter()) - .cloned() - .collect::>() - .into_iter() - .collect(); - let detail = if stuck.is_empty() { - "no ready tasks and no in-flight workers".to_string() - } else { - format!( - "runnable task(s) {} could not be assigned to any drivable worker", - stuck.join(", ") - ) - }; - return Err(anyhow::anyhow!( - "run_plan stalled after {} loop(s): {}. This usually means a task is assigned to a session run_plan cannot drive (foreign or stale member). Reassign with an explicit target_session, or clear the stale assignment, then retry.", - loop_count, - detail - )); - } - // Baseline for requeue pickup: everything ready at the top of this - // loop either gets assigned below or is known-undispatchable this - // wave. Anything ready *beyond* this set while we block (a retried - // failure, an external unblock) should cut the await short. - let ready_baseline: std::collections::HashSet = - summary.ready_ids.iter().cloned().collect(); - await_swarm_progress( - ctx, - await_sessions, - timeout_minutes, - reporter, - assignment_count, - &ready_baseline, - ) - .await?; - // Real progress (an await completed); clear the transient-stall backoff so - // a later genuine stall starts counting fresh. - transient_stall_loops = 0; - } -} - async fn spawn_assignment_session(ctx: &ToolContext, params: &CommunicateInput) -> Result { let spawn_request = Request::CommSpawn { id: REQUEST_ID, @@ -1678,11 +494,10 @@ impl CommunicateTool { /// Swarm actions advertised to models. Real usage is dominated by messaging, /// listing and spawning; the plan/task-graph actions (propose_plan, -/// assign_task, assign_next, fill_slots, run_plan, resync_plan, ...) are -/// subsumed by `launch` and lane-owning `spawn`. Unlisted actions still +/// assign_task, assign_next, resync_plan, ...) are subsumed by worker-owned +/// todo lanes, which the server dispatches itself, and lane-owning `spawn`. Unlisted actions still /// execute for old transcripts and scripts, they are just not offered. const ADVERTISED_SWARM_ACTIONS: &[&str] = &[ - "launch", "spawn", "message", "status", @@ -1754,12 +569,8 @@ struct CommunicateInput { #[serde(default)] delivery: Option, #[serde(default)] - concurrency_limit: Option, - #[serde(default)] force: Option, #[serde(default)] - retain_agents: Option, - #[serde(default)] status: Option, #[serde(default)] validation: Option, @@ -1938,27 +749,14 @@ impl Tool for CommunicateTool { "minimum": 1, "description": "await_members; returned ID can be used in todo.waiting_on." }, - "background": { - "type": "boolean", - "description": "launch: default true; false waits for completion." - }, "notify": { "type": "boolean", "description": "await_members: default true." }, - "concurrency_limit": { - "type": "integer", - "minimum": 1, - "description": "Max live launch workers." - }, "force": { "type": "boolean", "description": "stop: allow stopping non-owned/user-created sessions; default false." }, - "retain_agents": { - "type": "boolean", - "description": "launch: keep workers after completion; default false." - }, "wake": { "type": "boolean", "description": "Wake on message/await completion; default true." @@ -2843,100 +1641,12 @@ impl Tool for CommunicateTool { } } - "fill_slots" => { - let concurrency_limit = params.concurrency_limit.ok_or_else(|| { - anyhow::anyhow!("'concurrency_limit' is required for fill_slots action") - })?; - - let summary = fetch_plan_status(&ctx.session_id).await?; - let members = fetch_swarm_members(&ctx.session_id).await?; - - let active_count = - coordination_in_flight_count(&summary, &members, &ctx.session_id); - if active_count >= concurrency_limit { - return Ok(ToolOutput::new(format!( - "Window already full: {} active/in-flight task(s) >= limit {}", - active_count, concurrency_limit - ))); - } - - let mut assignments = Vec::new(); - let available_slots = concurrency_limit.saturating_sub(active_count); - for _ in 0..available_slots { - let request = Request::CommAssignNext { - id: REQUEST_ID, - session_id: ctx.session_id.clone(), - target_session: params.target_session.clone(), - working_dir: params.working_dir.clone(), - prefer_spawn: params.prefer_spawn, - spawn_if_needed: params.spawn_if_needed, - message: params.message.clone(), - model: params.model.clone(), - // Worker effort is not agent-controlled: the server applies - // `agents.swarm_effort` (default "low"). - effort: None, - }; - - match send_request(request).await { - Ok(ServerEvent::CommAssignTaskResponse { - task_id, - target_session, - .. - }) => assignments.push(format!("{} -> {}", task_id, target_session)), - Ok(ServerEvent::Error { message, .. }) - if message.contains("No runnable unassigned tasks") - || message.contains("No ready or completed swarm agents") => - { - break; - } - Ok(response) => { - ensure_success(&response)?; - } - Err(e) => { - return Err(anyhow::anyhow!("Failed to fill slots: {}", e)); - } - } - } - - if assignments.is_empty() { - Ok(ToolOutput::new(format!( - "No assignments made. Active: {}, limit: {}", - active_count, concurrency_limit - ))) - } else { - let mut output = format!( - "Filled {} slot(s):\n{}", - assignments.len(), - assignments - .into_iter() - .map(|line| format!("- {}", line)) - .collect::>() - .join("\n") - ); - if let Ok(summary) = fetch_plan_status(&ctx.session_id).await { - output.push_str(&format!("\n{}", format_plan_followup(&summary))); - } - Ok(ToolOutput::new(output)) - } - } - - "launch" | "run_plan" => { - // The coordinator's todo list is the plan: each lane (a group - // within a stage) becomes one item owned by one worker, whose - // own todo list is the lane. `launch` re-syncs every time so - // the plan never drifts; legacy `run_plan` keeps a hand-written - // `propose_plan` by only seeding an empty plan. - sync_plan_from_todos(&ctx, params.action == "run_plan").await?; - // Background-by-default: the plan driver runs as a managed - // background task (progress card, bg tool, notify/wake) so the - // coordinating agent stays responsive. Pass background=false - // to block inline until the plan reaches a terminal state. - if params.background.unwrap_or(true) { - run_swarm_plan_in_background(&ctx, params.clone()).await - } else { - run_swarm_plan_to_terminal(&ctx, ¶ms, &RunPlanReporter::inline()).await - } - } + "launch" | "run_plan" | "fill_slots" => Err(anyhow::anyhow!( + "'{}' was removed. Mark a todo lane `owner: \"worker\"` (todo phases[].lanes[].owner, \ + or update lane_owner) and the server spawns a worker for it as soon as its \ + dependencies are met. Use `plan_status` to watch lanes.", + params.action + )), "start" | "start_task" | "wake" | "resume" | "retry" | "reassign" | "replace" | "salvage" => { @@ -3071,8 +1781,7 @@ impl Tool for CommunicateTool { let timeout_minutes = params.timeout_minutes.unwrap_or(60); let timeout_secs = timeout_minutes * 60; // Public member waits are always asynchronous. The blocking - // CommAwaitMembers protocol remains available internally for the - // run_plan coordination loop, but agents must not park an entire + // CommAwaitMembers protocol remains available internally, but agents must not park an entire // turn waiting on a worker or a long-lived socket. let blocking_was_requested = params.background == Some(false); let background = true; @@ -3136,7 +1845,7 @@ impl Tool for CommunicateTool { _ => Err(anyhow::anyhow!( "Unknown action '{}'. Valid actions: share, share_append, read, message, broadcast, dm, channel, list, list_channels, channel_members, \ propose_plan, approve_plan, reject_plan, spawn, stop, assign_role, status, plan_status, summary, read_context, \ - resync_plan, assign_task, assign_next, fill_slots, run_plan, cleanup, start, start_task, wake, resume, retry, reassign, replace, salvage, subscribe_channel, unsubscribe_channel, await_members. \ + resync_plan, assign_task, assign_next, cleanup, start, start_task, wake, resume, retry, reassign, replace, salvage, subscribe_channel, unsubscribe_channel, await_members. \ To read messages addressed to you, use action='read'.", params.action )), diff --git a/crates/jcode-app-core/src/tool/communicate_schema_tests.rs b/crates/jcode-app-core/src/tool/communicate_schema_tests.rs index b9b36083ab7..24fbcbeabd0 100644 --- a/crates/jcode-app-core/src/tool/communicate_schema_tests.rs +++ b/crates/jcode-app-core/src/tool/communicate_schema_tests.rs @@ -98,16 +98,7 @@ fn spawning_actions_cannot_bypass_worker_model_policy() { swarm_model_allowlist: vec!["claude:claude-opus-4-8".to_string()], ..Default::default() }; - for action in [ - "spawn", - "assign_task", - "assign_next", - "fill_slots", - "run_plan", - "launch", - "retry", - "resume", - ] { + for action in ["spawn", "assign_task", "assign_next", "retry", "resume"] { let input: CommunicateInput = serde_json::from_value(json!({ "action": action, "model": "claude:claude-opus-4-8" })) @@ -312,13 +303,7 @@ fn existing_tool_keeps_prompt_while_new_tool_loads_edit() { #[test] fn spawning_action_inputs_preserve_requested_model() { - for action in [ - "spawn", - "assign_task", - "assign_next", - "fill_slots", - "run_plan", - ] { + for action in ["spawn", "assign_task", "assign_next"] { for model in [ "z-ai/glm-5.2:free", "openai-api:gpt-5.5", @@ -339,13 +324,7 @@ fn spawning_action_inputs_preserve_requested_model() { #[test] fn spawning_action_inputs_allow_omitted_or_null_model() { - for action in [ - "spawn", - "assign_task", - "assign_next", - "fill_slots", - "run_plan", - ] { + for action in ["spawn", "assign_task", "assign_next"] { let without_model: CommunicateInput = serde_json::from_value(json!({"action": action, "label": "reviewer"})).unwrap(); assert!(without_model.model.is_none()); @@ -438,7 +417,6 @@ fn schema_advertises_a_lean_lane_first_action_set() { .filter_map(|a| a.as_str()) .collect(); for kept in [ - "launch", "spawn", "message", "status", @@ -463,6 +441,7 @@ fn schema_advertises_a_lean_lane_first_action_set() { "reject_plan", "assign_task", "assign_next", + "launch", "fill_slots", "run_plan", "resync_plan", @@ -488,9 +467,6 @@ fn schema_advertises_a_lean_lane_first_action_set() { "phase", "prompt", "session_ids", - "background", - "concurrency_limit", - "retain_agents", "delivery", "wake", ] { diff --git a/crates/jcode-app-core/src/tool/communicate_tests.rs b/crates/jcode-app-core/src/tool/communicate_tests.rs index 8d7f9924528..9844c581f72 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests.rs @@ -1,10 +1,9 @@ use super::{ CommunicateInput, CommunicateTool, canonical_swarm_action, cleanup_candidate_session_ids, - coordination_in_flight_count, default_await_target_statuses, default_cleanup_target_statuses, - format_awaited_members, format_awaited_members_with_reports, format_members, - format_plan_status, format_swarm_model_list, latest_assistant_report, - resolve_optional_target_session, resolve_run_plan_concurrency, swarm_member_is_drivable_worker, - swarm_member_is_in_flight, + coordination_in_flight_count, default_cleanup_target_statuses, format_awaited_members, + format_awaited_members_with_reports, format_members, format_plan_status, + format_swarm_model_list, latest_assistant_report, resolve_optional_target_session, + swarm_member_is_drivable_worker, swarm_member_is_in_flight, }; use crate::message::{Message, StreamEvent, ToolDefinition}; use crate::protocol::{ @@ -30,490 +29,6 @@ fn tool_is_named_swarm() { assert_eq!(CommunicateTool::new().name(), "swarm"); } -#[test] -fn task_id_from_output_path_extracts_background_task_id() { - assert_eq!( - super::task_id_from_output_path(Path::new("/tmp/tasks/123abc.output")), - Some("123abc") - ); - assert_eq!( - super::task_id_from_output_path(Path::new("/tmp/tasks/123abc.status.json")), - None - ); -} - -#[tokio::test] -async fn run_plan_reporter_finalize_puts_summary_before_log() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let output_path = dir.path().join("tsk42.output"); - let reporter = super::RunPlanReporter::background(&output_path); - assert_eq!(reporter.task_id.as_deref(), Some("tsk42")); - - reporter.log("assigned a -> session_fox").await; - reporter.log("assigned b -> session_wolf").await; - reporter - .finalize("Swarm plan reached terminal state.") - .await; - - let content = tokio::fs::read_to_string(&output_path) - .await - .expect("output file"); - let summary_idx = content - .find("Swarm plan reached terminal state.") - .expect("summary present"); - let log_idx = content.find("assigned a -> session_fox").expect("log kept"); - assert!( - summary_idx < log_idx, - "summary must lead the output file so completion previews are useful:\n{content}" - ); -} - -#[tokio::test] -async fn run_plan_reporter_inline_is_a_no_op() { - let reporter = super::RunPlanReporter::inline(); - assert!(reporter.task_id.is_none()); - // Must not panic or create files. - reporter.log("ignored").await; - reporter.progress(1, 2, "ignored".to_string()).await; - reporter.finalize("ignored").await; -} - -#[tokio::test] -async fn run_plan_driver_guard_blocks_while_driver_task_is_live() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let manager = crate::background::BackgroundTaskManager::with_output_dir(dir.path().into()); - let session = "session-guard-live"; - - // Keep the fake driver alive long enough for the second claim to observe it. - let info = manager - .spawn_with_notify("swarm", None, session, false, false, |_| async { - tokio::time::sleep(Duration::from_secs(5)).await; - Ok(crate::background::TaskResult::completed(Some(0))) - }) - .await; - assert!(manager.is_live_task(&info.task_id)); - - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(claim) => claim.record_task(&info.task_id), - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("first claim for a fresh session must succeed") - } - } - - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::AlreadyRunning(Some(task_id)) => { - assert_eq!(task_id, info.task_id); - } - super::RunPlanDriverClaimResult::AlreadyRunning(None) => { - panic!("claim was recorded with a task id, so the blocker should carry it") - } - super::RunPlanDriverClaimResult::Claimed(_) => { - panic!("second claim must be blocked while the driver task is live") - } - } - - manager.cancel(&info.task_id).await.expect("cancel driver"); -} - -#[tokio::test] -async fn run_plan_driver_guard_allows_restart_after_stale_driver() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let manager = crate::background::BackgroundTaskManager::with_output_dir(dir.path().into()); - let session = "session-guard-stale"; - - // Simulate the pre-reload world: a status file on disk still says a swarm - // driver is Running for this session, and the per-process claim map still - // holds its task id, but no such task is live in this process (the map is - // fresh after reload / the task was pruned on completion). - let stale_task_id = "stalezzzz1"; - let stale_status = serde_json::json!({ - "task_id": stale_task_id, - "tool_name": "swarm", - "session_id": session, - "status": "running", - "exit_code": null, - "error": null, - "started_at": chrono::Utc::now().to_rfc3339(), - "completed_at": null, - "duration_secs": null, - "detached": false, - "notify": false, - "wake": false - }); - tokio::fs::write( - manager.status_path_for(stale_task_id), - serde_json::to_string_pretty(&stale_status).expect("serialize stale status"), - ) - .await - .expect("write stale status file"); - - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(claim) => claim.record_task(stale_task_id), - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("fresh session must be claimable") - } - } - assert!( - !manager.is_live_task(stale_task_id), - "stale task must not be live in this process" - ); - - // The stale Running status file and stale claim must not block restarting - // the driver. - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(_claim) => {} - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("stale (non-live) driver must not block a restart") - } - } -} - -#[tokio::test] -async fn run_plan_driver_guard_is_atomic_for_racing_claims() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let manager: &'static crate::background::BackgroundTaskManager = Box::leak(Box::new( - crate::background::BackgroundTaskManager::with_output_dir(dir.path().into()), - )); - let session = "session-guard-race"; - - // Two run_plan calls in one batch race the claim; exactly one may win. - let mut join_set = tokio::task::JoinSet::new(); - for _ in 0..2 { - join_set.spawn(async move { - match super::try_claim_run_plan_driver(manager, session) { - // Keep the claim held (as the winner does while spawning). - super::RunPlanDriverClaimResult::Claimed(claim) => Some(claim), - super::RunPlanDriverClaimResult::AlreadyRunning(_) => None, - } - }); - } - let mut held_claims = Vec::new(); - while let Some(result) = join_set.join_next().await { - if let Some(claim) = result.expect("claim task should not panic") { - held_claims.push(claim); - } - } - assert_eq!(held_claims.len(), 1, "exactly one racing claim may win"); -} - -#[tokio::test] -async fn run_plan_driver_guard_releases_claim_on_drop_without_task() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let manager = crate::background::BackgroundTaskManager::with_output_dir(dir.path().into()); - let session = "session-guard-drop"; - - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(claim) => drop(claim), - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("fresh session must be claimable") - } - } - - // A failed/cancelled startup path must not permanently block the session. - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(_claim) => {} - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("dropped Starting claim must be released") - } - } -} - -#[test] -fn run_plan_concurrency_defaults_and_clamps() { - assert_eq!( - resolve_run_plan_concurrency(None), - super::DEFAULT_RUN_PLAN_CONCURRENCY - ); - assert_eq!(resolve_run_plan_concurrency(Some(5)), 5); - assert_eq!(resolve_run_plan_concurrency(Some(0)), 1); -} - -#[test] -fn run_plan_utilization_tracks_peak_and_starvation() { - let mut util = super::RunPlanUtilization::default(); - - // Loop 1: 0 in flight, 8 open slots, dispatched 8 -> budget fully used. - util.record_loop(0, Some(8), 8); - // Loop 2: 8 in flight, 0 open slots, dispatched 0 -> saturated, not starved. - util.record_loop(8, Some(0), 0); - // Loop 3: 2 in flight, 6 open slots, dispatched 1 -> starved (5 idle slots). - util.record_loop(2, Some(6), 1); - - assert_eq!(util.peak_in_flight, 8); - assert_eq!(util.loops, 3); - assert_eq!(util.starved_loops, 1); - - let report = util.report(8); - assert!(report.contains("peak 8 of 8")); - assert!(report.contains("1 of 3 loop(s)")); -} - -#[test] -fn run_plan_utilization_handles_unbounded_budget() { - let mut util = super::RunPlanUtilization::default(); - // Unbounded budget: open slots are not meaningful, so no starvation - // accounting, but peak parallelism still records. - util.record_loop(10, None, 5); - assert_eq!(util.peak_in_flight, 15); - assert_eq!(util.starved_loops, 0); - let report = util.report(usize::MAX); - assert!(report.contains("peak 15 of unbounded")); -} - -#[test] -fn await_wakes_only_for_ready_items_beyond_the_wave_baseline() { - let baseline: std::collections::HashSet = - ["stuck".to_string(), "assigned".to_string()].into(); - let mut summary = crate::protocol::PlanGraphStatus { - swarm_id: None, - version: 3, - item_count: 6, - ready_ids: vec!["stuck".to_string()], - blocked_ids: Vec::new(), - active_ids: vec!["a1".to_string()], - completed_ids: Vec::new(), - failed_ids: Vec::new(), - failed_reasons: Default::default(), - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - - // Items already ready at wave start (even permanently-undispatchable - // ones) must not wake the driver: that would busy-spin the await. - assert!(!super::await_should_wake_for_new_ready(&baseline, &summary)); - - // No ready items at all: keep waiting on members. - summary.ready_ids.clear(); - assert!(!super::await_should_wake_for_new_ready(&baseline, &summary)); - - // A retried failed node re-enters ready as a NEW id -> wake and dispatch. - summary.ready_ids = vec!["stuck".to_string(), "retried-node".to_string()]; - assert!(super::await_should_wake_for_new_ready(&baseline, &summary)); -} - -#[test] -fn run_plan_progress_counts_only_completed_toward_percent_and_shows_live_active() { - // Regression: a plan with 33 completed / 116 failed of 152 used to report - // terminal/total = 149/152 (~98%) with active 0 while four externally - // assigned workers were still running. - let summary = crate::protocol::PlanGraphStatus { - swarm_id: Some("swarm-a".to_string()), - version: 9, - item_count: 152, - ready_ids: Vec::new(), - blocked_ids: Vec::new(), - active_ids: Vec::new(), - completed_ids: (0..33).map(|i| format!("c{i}")).collect(), - failed_ids: (0..116).map(|i| format!("f{i}")).collect(), - failed_reasons: Default::default(), - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - - let (completed, total, message) = super::run_plan_progress_snapshot(&summary, 4, 137); - // Percent driver is completed/total: 33/152 (~22%), never ~98%. - assert_eq!(completed, 33); - assert_eq!(total, 152); - // Failed nodes are surfaced separately, and live in-flight workers show as - // active even when the plan's own active_ids is empty (external - // assign_task dispatches). - assert_eq!( - message, - "completed 33 · failed 116 · blocked 0 · active 4 · assignments 137" - ); - - // The normalized background progress percent derived from (current,total) - // must match completed/total, not terminal/total. - let progress = crate::bus::BackgroundTaskProgress { - kind: crate::bus::BackgroundTaskProgressKind::Determinate, - percent: None, - message: Some(message), - current: Some(completed as u64), - total: Some(total as u64), - unit: Some("nodes".to_string()), - eta_seconds: None, - updated_at: chrono::Utc::now().to_rfc3339(), - source: crate::bus::BackgroundTaskProgressSource::Reported, - } - .normalize(); - let percent = progress.percent.expect("determinate percent"); - assert!( - (percent - 21.71).abs() < 0.1, - "33/152 must normalize to ~21.7%, got {percent}" - ); -} - -#[test] -fn run_plan_progress_active_prefers_plan_execution_state_when_larger() { - let summary = crate::protocol::PlanGraphStatus { - swarm_id: None, - version: 1, - item_count: 10, - ready_ids: Vec::new(), - blocked_ids: vec!["b1".to_string()], - active_ids: vec!["a1".to_string(), "a2".to_string(), "a3".to_string()], - completed_ids: vec!["c1".to_string(), "c2".to_string()], - failed_ids: vec!["f1".to_string()], - failed_reasons: Default::default(), - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - - // Plan says 3 active but only 1 live member is observable (e.g. status - // propagation lag): keep the larger plan-state number. - let (completed, total, message) = super::run_plan_progress_snapshot(&summary, 1, 5); - assert_eq!((completed, total), (2, 10)); - assert_eq!( - message, - "completed 2 · failed 1 · blocked 1 · active 3 · assignments 5" - ); -} - -#[test] -fn assign_error_classification_recovers_on_member_cap_instead_of_failing() { - use super::AssignErrorAction; - - // Graceful exhaustion of work or workers ends the assignment burst. - assert_eq!( - super::classify_assign_error( - "No runnable unassigned tasks are available in the swarm plan" - ), - AssignErrorAction::BreakGracefully - ); - assert_eq!( - super::classify_assign_error( - "No ready or completed swarm agents are available for automatic task assignment." - ), - AssignErrorAction::BreakGracefully - ); - - // The member cap must trigger recovery (cleanup + reuse fallback), not a - // run-aborting failure. The server wraps the cap message in a spawn-failure - // prefix, so classification must match on the substring. - assert_eq!( - super::classify_assign_error( - "Failed to spawn preferred worker: Swarm member limit reached (max 1000). \ - This swarm already has 1000 agents; it cannot spawn more." - ), - AssignErrorAction::RecoverCapacity - ); - - // Anything else is still a real failure. - assert_eq!( - super::classify_assign_error("Not in a swarm."), - AssignErrorAction::Fail - ); -} - -#[test] -fn cap_recovery_prefers_cleanup_then_reuse_then_gives_up() { - use super::CapRecoveryStep; - - // First cap hit with freed capacity: retry keeping the fresh-spawn preference. - assert_eq!(super::cap_recovery_step(1, 3), CapRecoveryStep::RetryFresh); - // First cap hit but nothing could be freed: fall back to reusing ready workers. - assert_eq!(super::cap_recovery_step(1, 0), CapRecoveryStep::RetryReuse); - // Recovery already ran and the cap still refuses: continue with in-flight - // work instead of aborting or spinning. - assert_eq!(super::cap_recovery_step(2, 0), CapRecoveryStep::GiveUp); - assert_eq!(super::cap_recovery_step(3, 5), CapRecoveryStep::GiveUp); -} - -#[test] -fn run_plan_driver_failures_carry_worker_retention_hint() { - // Every driver-failure path must tell the caller the spawned workers are - // still running and how to stop them. - let hinted = super::with_worker_retention_hint( - "run_plan stalled after 3 loop(s): no ready tasks and no in-flight workers.".to_string(), - ); - assert!(hinted.contains("Spawned workers were retained")); - assert!(hinted.contains("swarm cleanup")); - - // Max-loops keeps its intentional retention-for-inspection wording but - // still gains the actionable hint. - let max_loops = super::with_worker_retention_hint( - "run_plan exceeded 200 coordination loops; leaving workers untouched for inspection" - .to_string(), - ); - assert!(max_loops.contains("swarm cleanup")); - - // Idempotent: re-wrapping (e.g. the background wrapper re-reporting the - // error) must not duplicate the hint. - let twice = super::with_worker_retention_hint(hinted.clone()); - assert_eq!(twice.matches("Spawned workers were retained").count(), 1); -} - -#[test] -fn run_plan_terminal_summary_reports_failed_nodes() { - let base = crate::protocol::PlanGraphStatus { - swarm_id: Some("swarm-a".to_string()), - version: 1, - item_count: 4, - ready_ids: Vec::new(), - blocked_ids: Vec::new(), - active_ids: Vec::new(), - completed_ids: vec!["a".to_string(), "b".to_string()], - failed_ids: vec!["c".to_string(), "d".to_string()], - failed_reasons: Default::default(), - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - - let with_failures = super::format_run_plan_terminal_summary(5, &base, 7); - assert!(with_failures.contains("completed=2")); - assert!(with_failures.contains("failed=2")); - assert!(with_failures.contains("Failed nodes: c, d")); - assert!(with_failures.contains("did NOT finish cleanly")); - - // A clean run reports failed=0 and no failure callout. - let clean = crate::protocol::PlanGraphStatus { - completed_ids: vec![ - "a".to_string(), - "b".to_string(), - "c".to_string(), - "d".to_string(), - ], - failed_ids: Vec::new(), - failed_reasons: Default::default(), - ..base - }; - let clean_summary = super::format_run_plan_terminal_summary(5, &clean, 7); - assert!(clean_summary.contains("failed=0")); - assert!(!clean_summary.contains("Failed nodes")); -} - -#[test] -fn plan_terminal_node_count_includes_failed_without_double_counting() { - let summary = crate::protocol::PlanGraphStatus { - swarm_id: Some("swarm-a".to_string()), - version: 1, - item_count: 4, - ready_ids: Vec::new(), - blocked_ids: vec!["x".to_string()], - active_ids: Vec::new(), - completed_ids: vec!["a".to_string()], - failed_ids: vec!["c".to_string()], - failed_reasons: Default::default(), - // "x" is both blocked and cyclic; it must count once. - cycle_ids: vec!["x".to_string()], - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - // a (completed) + c (failed) + x (blocked/cycle, deduped) = 3. Without - // failed_ids in the terminal count a run with failed nodes would never - // satisfy terminal_count >= item_count and run_plan could spin or stall. - assert_eq!(super::plan_terminal_node_count(&summary), 3); -} - #[test] fn canonical_swarm_action_maps_common_synonyms() { assert_eq!(canonical_swarm_action("inbox"), "read"); @@ -1124,9 +639,6 @@ async fn wait_for_member_presence( } } -#[path = "communicate_tests/credential_failures.rs"] -mod credential_failures; - include!("communicate_tests/input_format.rs"); include!("communicate_tests/end_to_end.rs"); include!("communicate_tests/assignment.rs"); 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 15131df85cc..d7a79757cba 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs @@ -467,100 +467,6 @@ async fn communicate_assign_next_can_spawn_if_needed_server_side() { server_task.abort(); } -#[tokio::test] -async fn communicate_fill_slots_tops_up_to_concurrency_limit() { - let _env_lock = crate::storage::lock_test_env(); - let runtime_dir = tempfile::TempDir::new().expect("runtime tempdir"); - 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"); - - let provider: Arc = Arc::new(DelayedTestProvider { - delay: Duration::from_millis(300), - }); - let server = Arc::new(Server::new(provider)); - let mut server_task = { - let server = Arc::clone(&server); - tokio::spawn(async move { server.run().await }) - }; - - wait_for_server_socket(&socket_path, &mut server_task) - .await - .expect("server socket should be ready"); - - let mut watcher = RawClient::connect(&socket_path) - .await - .expect("watcher should connect"); - watcher - .subscribe(&repo_dir) - .await - .expect("watcher subscribe"); - - let watcher_session = watcher.session_id().await.expect("watcher session id"); - let tool = CommunicateTool::new(); - let ctx = test_ctx(&watcher_session, &repo_dir); - - tool.execute( - json!({ - "action": "assign_role", - "target_session": watcher_session, - "role": "coordinator" - }), - ctx.clone(), - ) - .await - .expect("self-promotion to coordinator should succeed"); - - tool.execute( - json!({ - "action": "propose_plan", - "plan_items": [{ - "id": "task-1", - "content": "first task", - "status": "queued", - "priority": "high" - }, { - "id": "task-2", - "content": "second task", - "status": "queued", - "priority": "high" - }, { - "id": "task-3", - "content": "third task", - "status": "queued", - "priority": "high" - }] - }), - ctx.clone(), - ) - .await - .expect("plan proposal should succeed"); - - let output = tool - .execute( - json!({ - "action": "fill_slots", - "concurrency_limit": 2, - "spawn_if_needed": true - }), - ctx, - ) - .await - .expect("fill_slots should succeed"); - - assert!( - output.output.contains("Filled 2 slot(s):"), - "unexpected fill_slots output: {}", - output.output - ); - - server_task.abort(); -} - #[tokio::test] async fn communicate_assign_task_can_prefer_fresh_spawn_over_reuse() { let _env_lock = crate::storage::lock_test_env(); diff --git a/crates/jcode-app-core/src/tool/communicate_tests/credential_failures.rs b/crates/jcode-app-core/src/tool/communicate_tests/credential_failures.rs deleted file mode 100644 index 20acbe9e5fc..00000000000 --- a/crates/jcode-app-core/src/tool/communicate_tests/credential_failures.rs +++ /dev/null @@ -1,177 +0,0 @@ -use super::*; - -#[test] -fn default_await_members_targets_include_ready() { - assert_eq!( - default_await_target_statuses(), - vec!["ready", "completed", "stopped", "failed", "crashed"] - ); -} - -fn credential_failed_worker(session_id: &str, detail: &str, age_secs: u64) -> AgentInfo { - AgentInfo { - session_id: session_id.to_string(), - status: Some("failed".to_string()), - detail: Some(detail.to_string()), - role: Some("agent".to_string()), - is_headless: Some(true), - report_back_to_session_id: Some("coord".to_string()), - status_age_secs: Some(age_secs), - provider_name: Some("anthropic".to_string()), - ..Default::default() - } -} - -#[test] -fn credential_failure_wave_detected_for_recent_auth_failed_workers() { - // The observed incident: every dispatched worker died within seconds with - // an Anthropic 401 (expired OAuth + revoked refresh token) and nothing - // completed. That must classify as a wave, not as N independent failures. - let members = vec![ - AgentInfo { - session_id: "coord".to_string(), - status: Some("running".to_string()), - role: Some("coordinator".to_string()), - ..Default::default() - }, - credential_failed_worker("w1", "Anthropic API error (401 Unauthorized)", 2), - credential_failed_worker("w2", "Anthropic API error (401 Unauthorized)", 3), - credential_failed_worker("w3", "invalid_grant: refresh token invalid", 5), - ]; - let wave = super::super::detect_credential_failure_wave(&members, "coord", 0, 60) - .expect("three recent credential failures with zero completions is a wave"); - assert_eq!(wave.session_ids, vec!["w1", "w2", "w3"]); - assert_eq!(wave.sample_detail, "Anthropic API error (401 Unauthorized)"); - assert_eq!(wave.provider.as_deref(), Some("anthropic")); - - let message = super::super::format_credential_failure_wave_error(&wave, 60); - assert!(message.contains("paused dispatching")); - assert!(message.contains("3 worker(s)")); - assert!(message.contains("401 Unauthorized")); - assert!(message.contains("`jcode login --provider claude`")); -} - -#[test] -fn credential_failure_wave_requires_at_least_two_workers() { - let members = vec![credential_failed_worker( - "w1", - "Anthropic API error (401 Unauthorized)", - 2, - )]; - assert_eq!( - super::super::detect_credential_failure_wave(&members, "coord", 0, 60), - None, - "one bad worker is not a wave" - ); -} - -#[test] -fn credential_failure_wave_not_detected_once_anything_completed() { - // Completions prove the credential works (or worked); later auth failures - // are then per-worker problems, not a route-wide outage to halt over. - let members = vec![ - credential_failed_worker("w1", "Anthropic API error (401 Unauthorized)", 2), - credential_failed_worker("w2", "Anthropic API error (401 Unauthorized)", 3), - ]; - assert_eq!( - super::super::detect_credential_failure_wave(&members, "coord", 1, 60), - None - ); -} - -#[test] -fn credential_failure_wave_ignores_stale_and_non_credential_failures() { - let members = vec![ - // Stale: failed long before this window (e.g. a previous, already - // diagnosed wave; the user has since re-authenticated and retried). - credential_failed_worker("old", "Anthropic API error (401 Unauthorized)", 3600), - // Unknown age must not count either. - AgentInfo { - status_age_secs: None, - ..credential_failed_worker("ageless", "401 Unauthorized", 0) - }, - // Non-credential failure. - credential_failed_worker("crashed", "worker panicked: index out of bounds", 2), - // Only one recent credential failure remains: below the wave minimum. - credential_failed_worker("w1", "Anthropic API error (401 Unauthorized)", 2), - ]; - assert_eq!( - super::super::detect_credential_failure_wave(&members, "coord", 0, 60), - None - ); -} - -#[test] -fn credential_failure_wave_ignores_foreign_members() { - // A foreign, client-attached session that failed with an auth error is not - // one of run_plan's workers; it must not trip the breaker. - let foreign = AgentInfo { - is_headless: Some(false), - report_back_to_session_id: None, - ..credential_failed_worker("foreign", "401 Unauthorized", 2) - }; - let members = vec![ - foreign, - credential_failed_worker("w1", "401 Unauthorized", 2), - ]; - assert_eq!( - super::super::detect_credential_failure_wave(&members, "coord", 0, 60), - None - ); -} - -#[test] -fn credential_login_fix_hint_maps_provider_names() { - assert_eq!( - super::super::credential_login_fix_hint(Some("anthropic")), - "`jcode login --provider claude`" - ); - assert_eq!( - super::super::credential_login_fix_hint(Some("OpenAI")), - "`jcode login --provider openai`" - ); - assert_eq!( - super::super::credential_login_fix_hint(Some("copilot")), - "`jcode login --provider copilot`" - ); - assert_eq!( - super::super::credential_login_fix_hint(None), - "`jcode login --provider `" - ); -} - -#[test] -fn run_plan_terminal_summary_includes_recorded_failure_reasons() { - let mut failed_reasons = std::collections::BTreeMap::new(); - failed_reasons.insert( - "c".to_string(), - "task failed: Anthropic API error (401 Unauthorized)".to_string(), - ); - let summary = crate::protocol::PlanGraphStatus { - swarm_id: Some("swarm-a".to_string()), - version: 1, - item_count: 2, - ready_ids: Vec::new(), - blocked_ids: Vec::new(), - active_ids: Vec::new(), - completed_ids: vec!["a".to_string()], - failed_ids: vec!["c".to_string()], - failed_reasons, - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - let output = super::super::format_run_plan_terminal_summary(3, &summary, 2); - assert!(output.contains("Failed nodes: c")); - assert!( - output.contains("c: task failed: Anthropic API error (401 Unauthorized)"), - "terminal summary must carry the recorded failure reason:\n{output}" - ); - - let plan_status = format_plan_status(&summary).output; - assert!( - plan_status.contains("c: task failed: Anthropic API error (401 Unauthorized)"), - "plan_status must display the recorded failure reason:\n{plan_status}" - ); -} 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 33b2b70213b..6f044248240 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 @@ -239,63 +239,6 @@ async fn communicate_await_members_background_returns_immediately_and_notifies() server_task.abort(); } -#[tokio::test] -async fn communicate_run_plan_with_empty_plan_returns_inline_even_in_background_mode() { - let _env_lock = crate::storage::lock_test_env(); - let runtime_dir = tempfile::TempDir::new().expect("runtime tempdir"); - 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"); - - let provider: Arc = Arc::new(DelayedTestProvider { - delay: Duration::from_millis(50), - }); - let server = Arc::new(Server::new(provider)); - let mut server_task = { - let server = Arc::clone(&server); - tokio::spawn(async move { server.run().await }) - }; - - let socket_path = runtime_dir.path().join("jcode.sock"); - wait_for_server_socket(&socket_path, &mut server_task) - .await - .expect("server socket should be ready"); - - let mut client = RawClient::connect(&socket_path) - .await - .expect("client should connect"); - client.subscribe(&repo_dir).await.expect("subscribe"); - let session = client.session_id().await.expect("session id"); - - let tool = CommunicateTool::new(); - let ctx = test_ctx(&session, &repo_dir); - - // Background is the default; with no plan the validation happens inline and - // no background task should be started. - let output = tokio::time::timeout( - Duration::from_secs(5), - tool.execute(json!({"action": "run_plan"}), ctx.clone()), - ) - .await - .expect("run_plan should return promptly") - .expect("run_plan should succeed"); - assert!( - output.output.contains("No swarm plan items to run."), - "expected inline empty-plan response, got: {}", - output.output - ); - assert!( - output.metadata.is_none(), - "empty plan must not start a background driver" - ); - - server_task.abort(); -} - #[tokio::test] async fn communicate_status_returns_busy_snapshot_for_running_member() { let _env_lock = crate::storage::lock_test_env(); diff --git a/crates/jcode-app-core/src/tool/communicate_tests/input_format.rs b/crates/jcode-app-core/src/tool/communicate_tests/input_format.rs index d6f020c9384..f3fe55cd712 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests/input_format.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests/input_format.rs @@ -89,13 +89,11 @@ fn communicate_input_accepts_prefer_spawn() { #[test] fn communicate_input_accepts_cleanup_lifecycle_flags() { let parsed: CommunicateInput = serde_json::from_value(serde_json::json!({ - "action": "run_plan", - "force": true, - "retain_agents": true + "action": "stop", + "force": true })) .expect("lifecycle flags should deserialize"); assert_eq!(parsed.force, Some(true)); - assert_eq!(parsed.retain_agents, Some(true)); } #[test] diff --git a/crates/jcode-app-core/src/tool/mcp.rs b/crates/jcode-app-core/src/tool/mcp.rs index 22469c35dd7..0ec82ba2b8c 100644 --- a/crates/jcode-app-core/src/tool/mcp.rs +++ b/crates/jcode-app-core/src/tool/mcp.rs @@ -931,7 +931,7 @@ impl McpManagementTool { // Connect let manager = self.manager.read().await; - match manager.connect(&server_name, &config).await { + match crate::mcp::interactive_connect(manager.connect(&server_name, &config)).await { Ok(()) => { let tools = manager.all_tools().await; let connected = manager.connected_servers().await; diff --git a/crates/jcode-app-core/src/tool/todo.rs b/crates/jcode-app-core/src/tool/todo.rs index 2513f7cfa7e..0db6c4acf3e 100644 --- a/crates/jcode-app-core/src/tool/todo.rs +++ b/crates/jcode-app-core/src/tool/todo.rs @@ -31,8 +31,9 @@ mod external_blocker_tests; #[path = "todo_lane_dependency_tests.rs"] mod lane_dependency_tests; -/// A prerequisite edit naming one member applies atomically to its entire lane. -fn patch_lane_after(todos: &mut [Value], id: &str, value: Value) -> Result<()> { +/// A lane-wide field edit naming one member applies atomically to its entire +/// lane (`lane_after`, `lane_owner`). +fn patch_lane_field(todos: &mut [Value], id: &str, field: &str, value: Value) -> Result<()> { let typed: Vec = serde_json::from_value(Value::Array(todos.to_vec()))?; let target = typed .iter() @@ -41,12 +42,16 @@ fn patch_lane_after(todos: &mut [Value], id: &str, value: Value) -> Result<()> { let lane = crate::todo::stages::lane_id(target); for (raw, todo) in todos.iter_mut().zip(&typed) { if crate::todo::stages::lane_id(todo) == lane { - raw["lane_after"] = value.clone(); + raw[field] = value.clone(); } } Ok(()) } +fn patch_lane_after(todos: &mut [Value], id: &str, value: Value) -> Result<()> { + patch_lane_field(todos, id, "lane_after", value) +} + fn validate_external_blockers(previous: &[TodoItem], todos: &[TodoItem]) -> Result<()> { let now = chrono::Utc::now(); for todo in todos { @@ -352,6 +357,9 @@ fn apply_todo_patch(mut input: Value, stored: impl FnOnce() -> Value) -> Result< if let Some(value) = fields.get("lane_after") { patch_lane_after(&mut todos, id, value.clone())?; } + if let Some(value) = fields.get("lane_owner") { + patch_lane_field(&mut todos, id, "lane_owner", value.clone())?; + } } for mut add in adds { @@ -550,7 +558,7 @@ fn flatten_phases(input: &mut Value) -> Result<()> { .ok_or_else(|| anyhow::anyhow!("lane must be an object"))?; if lane .keys() - .any(|key| !["lane", "items", "after"].contains(&key.as_str())) + .any(|key| !["lane", "items", "after", "owner"].contains(&key.as_str())) { bail!("unexpected lane field"); } @@ -573,9 +581,17 @@ fn flatten_phases(input: &mut Value) -> Result<()> { .as_object() .cloned() .ok_or_else(|| anyhow::anyhow!("item must be an object"))?; - if ["phase", "lane", "stage", "group", "wave", "lane_after"] - .iter() - .any(|key| item.contains_key(*key)) + if [ + "phase", + "lane", + "stage", + "group", + "wave", + "lane_after", + "lane_owner", + ] + .iter() + .any(|key| item.contains_key(*key)) { bail!("hierarchical items must not own phase/lane/stage/group membership"); } @@ -594,6 +610,9 @@ fn flatten_phases(input: &mut Value) -> Result<()> { if let Some(after) = lane.get("after") { item.insert("lane_after".into(), after.clone()); } + if let Some(owner) = lane.get("owner").filter(|owner| !owner.is_null()) { + item.insert("lane_owner".into(), owner.clone()); + } todos.push(Value::Object(item)); } } @@ -1435,6 +1454,10 @@ impl Tool for TodoTool { "items": {"type": "string", "minLength": 1}, "description": "Earlier-phase prerequisite task names. Updates whole lane; null restores phase barrier." }); + schema["properties"]["todos"]["items"]["properties"]["lane_owner"] = json!({ + "type": "string", "enum": ["self", "worker"], + "description": "Who runs the lane. worker: a swarm worker auto-spawns when the lane is ready. Updates whole lane." + }); let mut add_properties = schema["properties"]["todos"]["items"]["properties"].clone(); add_properties["after"] = json!({ "type": "string", @@ -1465,6 +1488,7 @@ impl Tool for TodoTool { properties.remove("phase"); properties.remove("lane"); properties.remove("lane_dependencies"); + properties.remove("lane_owner"); } item["additionalProperties"] = json!(false); schema["properties"]["phases"] = json!({ @@ -1477,6 +1501,9 @@ impl Tool for TodoTool { "type": ["array", "null"], "minItems": 1, "uniqueItems": true, "items": {"type": "string", "minLength": 1}, "description": "Earlier-phase prerequisite task names; replaces phase barrier. Omit/null for normal ordering." + }, "owner": { + "type": "string", "enum": ["self", "worker"], + "description": "Who runs the lane. Default self. worker: a swarm worker auto-spawns when the lane is ready, no launch call." }, "items": {"type": "array", "minItems": 1, "items": item}} } }}} @@ -1499,6 +1526,7 @@ impl Tool for TodoTool { // Goals/plan-only writes keep the stored todo list. let previous = load_todos(&ctx.session_id).unwrap_or_default(); let mut todos = params.todos.unwrap_or_else(|| previous.clone()); + crate::todo::stages::normalize_lane_owners(&mut todos); crate::todo::store::validate_session_lane_dependencies( &crate::storage::jcode_dir()?, &ctx.session_id, diff --git a/crates/jcode-app-core/src/tool/todo_lane_dependency_tests.rs b/crates/jcode-app-core/src/tool/todo_lane_dependency_tests.rs index 5eb161f3afb..81a2e39d155 100644 --- a/crates/jcode-app-core/src/tool/todo_lane_dependency_tests.rs +++ b/crates/jcode-app-core/src/tool/todo_lane_dependency_tests.rs @@ -136,3 +136,44 @@ fn conflicting_dependency_aliases_are_rejected() { ); assert!(result.unwrap_err().to_string().contains("not both")); } + +#[test] +fn phase_lane_owner_marks_every_member_and_update_flips_whole_lane() { + let todos = parsed(json!({"phases": [ + {"phase": 1, "lanes": [ + {"lane": "api", "owner": "worker", "items": [ + {"id": "a", "content": "A", "status": "pending", "priority": "high"}, + {"id": "b", "content": "B", "status": "pending", "priority": "high"} + ]}, + {"lane": "mine", "items": [{"id": "c", "content": "C", "status": "pending", "priority": "high"}]} + ]} + ]})); + assert!( + todos[..2] + .iter() + .all(crate::todo::stages::lane_requests_worker) + ); + assert!(!crate::todo::stages::lane_requests_worker(&todos[2])); + + let stored = json!([item("a", 1, "api"), item("b", 1, "api")]); + let updated = apply_todo_patch( + json!({"update": [{"id": "b", "lane_owner": "worker"}]}), + || stored, + ) + .unwrap(); + let todos = parsed(updated); + assert!(todos.iter().all(crate::todo::stages::lane_requests_worker)); +} + +#[test] +fn unknown_lane_owner_is_rejected() { + let mut bad = TodoItem { + id: "a".into(), + content: "A".into(), + status: "pending".into(), + priority: "high".into(), + ..Default::default() + }; + bad.lane_owner = Some("robot".into()); + assert!(crate::todo::stages::validate_lane_dependencies(&[bad]).is_err()); +} diff --git a/crates/jcode-app-core/src/tool/todo_tests.rs b/crates/jcode-app-core/src/tool/todo_tests.rs index 544109ce16f..cecb9c17850 100644 --- a/crates/jcode-app-core/src/tool/todo_tests.rs +++ b/crates/jcode-app-core/src/tool/todo_tests.rs @@ -366,7 +366,8 @@ fn schema_advertises_intent_and_todos() { assert!(plan_props.contains_key("understands_user_intent")); assert!(!plan_props.contains_key("alignment_score")); assert!(!plan_props.contains_key("user_intention_alignment")); - assert_eq!(plan_props.len(), 2); + assert!(plan_props.contains_key("title")); + assert_eq!(plan_props.len(), 3); let plan_required = props["plan"]["required"] .as_array() .expect("plan should advertise required fields"); diff --git a/crates/jcode-app-core/src/usage_tests.rs b/crates/jcode-app-core/src/usage_tests.rs index 7b822b1bfac..88eec6e3ab2 100644 --- a/crates/jcode-app-core/src/usage_tests.rs +++ b/crates/jcode-app-core/src/usage_tests.rs @@ -513,6 +513,7 @@ fn test_account_usage_probe_prefers_best_available_alternative() { current_label: "work".to_string(), accounts: vec![ AccountUsageSnapshot { + additional_windows: Vec::new(), label: "work".to_string(), email: Some("work@example.com".to_string()), exhausted: true, @@ -524,6 +525,7 @@ fn test_account_usage_probe_prefers_best_available_alternative() { error: None, }, AccountUsageSnapshot { + additional_windows: Vec::new(), label: "backup".to_string(), email: Some("backup@example.com".to_string()), exhausted: false, @@ -535,6 +537,7 @@ fn test_account_usage_probe_prefers_best_available_alternative() { error: None, }, AccountUsageSnapshot { + additional_windows: Vec::new(), label: "secondary".to_string(), email: Some("secondary@example.com".to_string()), exhausted: false, @@ -565,6 +568,7 @@ fn test_account_usage_probe_detects_all_accounts_exhausted() { current_label: "primary".to_string(), accounts: vec![ AccountUsageSnapshot { + additional_windows: Vec::new(), label: "primary".to_string(), email: None, exhausted: true, @@ -576,6 +580,7 @@ fn test_account_usage_probe_detects_all_accounts_exhausted() { error: None, }, AccountUsageSnapshot { + additional_windows: Vec::new(), label: "backup".to_string(), email: None, exhausted: true, diff --git a/crates/jcode-base/src/auth/account_pool.rs b/crates/jcode-base/src/auth/account_pool.rs index 6a8adb5f29a..5f2946a8f50 100644 --- a/crates/jcode-base/src/auth/account_pool.rs +++ b/crates/jcode-base/src/auth/account_pool.rs @@ -398,7 +398,7 @@ impl AccountPool { } } members.extend(last); - members.sort_by_cached_key(|key| self.priority(key)); + members.sort_by_cached_key(|key| self.routing_rank(key)); (members, others) } @@ -455,7 +455,7 @@ impl AccountPool { } // Cycle only within a tier. A lower-priority metered account must not // jump ahead of a ready subscription merely because it follows current. - members.sort_by_cached_key(|key| self.priority(key)); + members.sort_by_cached_key(|key| self.routing_rank(key)); let prefix = format!("{provider}:"); members .into_iter() @@ -465,6 +465,39 @@ impl AccountPool { } } +impl AccountPool { + /// Local-only routing preference. Unknown quota remains eligible, but a + /// healthy fresh report wins within the same user-selected priority tier. + fn routing_rank(&self, key: &str) -> (u32, bool) { + let known_available = key + .split_once(':') + .and_then(|(provider, label)| { + let kind = match provider { + "claude" => crate::usage::MultiAccountProviderKind::Anthropic, + "openai" => crate::usage::MultiAccountProviderKind::OpenAI, + _ => return None, + }; + crate::usage::cached_account_usage_snapshot(kind, label) + }) + .is_some_and(|usage| { + !usage.exhausted + && [usage.five_hour_ratio, usage.seven_day_ratio] + .into_iter() + .flatten() + .any(|ratio| ratio.is_finite() && ratio < 0.99) + }); + (self.priority(key), !known_available) + } + + /// Explicit pool members in effective priority and quota-health order. + pub fn ordered_members(&self) -> Vec { + let mut members = self.members.clone(); + members.retain(|key| !self.excluded.contains(key)); + members.sort_by_cached_key(|key| self.routing_rank(key)); + members + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/jcode-base/src/auth/oauth.rs b/crates/jcode-base/src/auth/oauth.rs index 8b99aa65a4f..d414a04d034 100644 --- a/crates/jcode-base/src/auth/oauth.rs +++ b/crates/jcode-base/src/auth/oauth.rs @@ -645,6 +645,7 @@ pub fn save_claude_tokens_for_account(tokens: &OAuthTokens, label: &str) -> Resu scopes, }; claude_auth::upsert_account(account)?; + crate::usage::invalidate_anthropic_usage_auth_failure(label); Ok(()) } @@ -1004,6 +1005,14 @@ pub async fn refresh_claude_tokens_for_account( .map(|tokens| tokens.refresh_token) .filter(|token| !token.is_empty()) .unwrap_or(observed_refresh); + // Check the newest stored token under the flight lock, not the + // caller's possibly superseded token. Usage polling used to bypass + // the terminal-rejection guard and retry invalid_grant forever. + crate::auth::refresh_state::ensure_refresh_allowed( + "claude", + &token, + "Run `/login claude` to sign in again.", + )?; refresh_claude_tokens_inner(&token, Some(&label)).await }, ) diff --git a/crates/jcode-base/src/bus.rs b/crates/jcode-base/src/bus.rs index 22ed547dd9d..6766bd663a5 100644 --- a/crates/jcode-base/src/bus.rs +++ b/crates/jcode-base/src/bus.rs @@ -507,6 +507,9 @@ pub enum BusEvent { /// from the cached one, so the branch/dirty/commit chrome needs a repaint /// now rather than at the next unrelated redraw. GitInfoRefreshed, + /// A client reload candidate binary was published (inotify). Clients + /// re-exec onto it once idle so every session follows hot reloads. + ClientBinaryChanged, /// Productivity report finished generating off the UI thread ProductivityReportReady(ProductivityReportReady), } diff --git a/crates/jcode-base/src/client_focus.rs b/crates/jcode-base/src/client_focus.rs index a71844e7448..fd1d6b00868 100644 --- a/crates/jcode-base/src/client_focus.rs +++ b/crates/jcode-base/src/client_focus.rs @@ -128,6 +128,91 @@ pub fn registered_client_session(pid: u32) -> Option { (!session_id.is_empty()).then_some(session_id) } +/// PIDs of other live local TUI clients whose registered session is +/// `session_id`. Linux only: liveness and identity are read from `/proc`. +pub fn clients_showing_session(session_id: &str) -> Vec { + match client_sessions_dir() { + Ok(dir) => { + clients_showing_session_in(&dir, session_id, std::process::id(), is_live_jcode_client) + } + Err(_) => Vec::new(), + } +} + +fn clients_showing_session_in( + dir: &std::path::Path, + session_id: &str, + own_pid: u32, + is_live: impl Fn(u32) -> bool, +) -> Vec { + let session_id = session_id.trim(); + if session_id.is_empty() { + return Vec::new(); + } + let Ok(entries) = std::fs::read_dir(dir) else { + return Vec::new(); + }; + let mut pids: Vec = entries + .flatten() + .filter_map(|entry| { + let pid = match entry.file_name().to_str()?.parse::() { + Ok(pid) => pid, + // Unrelated files in the registry are not client markers. + Err(_) => return None, + }; + let shown = match std::fs::read_to_string(entry.path()) { + Ok(shown) => shown, + Err(error) => { + crate::logging::debug(&format!("Cannot read client session marker: {error}")); + return None; + } + }; + (pid != own_pid && shown.trim() == session_id && is_live(pid)).then_some(pid) + }) + .collect(); + pids.sort_unstable(); + pids +} + +/// A marker PID can be reused after its client exits, so require the process +/// to still be a jcode binary before anyone moves or closes its window. +fn is_live_jcode_client(pid: u32) -> bool { + if !cfg!(target_os = "linux") { + return false; + } + match std::fs::read_link(format!("/proc/{pid}/exe")) { + Ok(exe) => exe + .file_name() + .is_some_and(|name| name.to_string_lossy().starts_with("jcode")), + // Exited or inaccessible processes cannot safely be identified as clients. + Err(error) => { + crate::logging::debug(&format!("Cannot identify client process {pid}: {error}")); + false + } + } +} + +#[cfg(test)] +mod tests { + use super::clients_showing_session_in; + + #[test] + fn finds_other_live_clients_showing_the_session() { + let dir = tempfile::tempdir().unwrap(); + for (pid, session) in [(10, "a"), (11, "a\n"), (12, "b"), (13, "a"), (14, "a")] { + std::fs::write(dir.path().join(pid.to_string()), session).unwrap(); + } + std::fs::write(dir.path().join("junk"), "a").unwrap(); + std::fs::write(dir.path().join("999999999999999999999999"), "a").unwrap(); + // A numeric entry that cannot be read must not hide valid clients. + std::fs::create_dir(dir.path().join("15")).unwrap(); + // 13 is this process, 14 has exited. + let found = clients_showing_session_in(dir.path(), "a", 13, |pid| pid != 14); + assert_eq!(found, vec![10, 11]); + assert!(clients_showing_session_in(dir.path(), " ", 13, |_| true).is_empty()); + } +} + fn last_focused_session_path() -> Result { Ok(crate::storage::jcode_dir()?.join("last_focused_client_session")) } diff --git a/crates/jcode-base/src/compaction.rs b/crates/jcode-base/src/compaction.rs index 5b9fc8fd685..63b7d688322 100644 --- a/crates/jcode-base/src/compaction.rs +++ b/crates/jcode-base/src/compaction.rs @@ -1815,7 +1815,7 @@ async fn generate_compaction_artifact( ); Ok(CompactionResult { - summary_text: summary, + summary_text: validate_compaction_summary(summary)?, openai_encrypted_content: None, covers_up_to_turn: messages.len(), duration_ms: start.elapsed().as_millis() as u64, @@ -1823,6 +1823,48 @@ async fn generate_compaction_artifact( }) } +/// Reject summaries that cannot stand in for the messages they replace, such +/// as empty output or a provider refusal (issue #2089). Returning an error makes +/// compaction keep the original messages instead of dropping them. +fn validate_compaction_summary(summary: String) -> Result { + let trimmed = summary.trim(); + if trimmed.is_empty() { + anyhow::bail!("compaction summary rejected: provider returned empty text"); + } + if looks_like_refusal(trimmed) { + let preview: String = trimmed.chars().take(120).collect(); + anyhow::bail!( + "compaction summary rejected: provider refusal: {:?}", + preview + ); + } + Ok(summary) +} + +fn looks_like_refusal(text: &str) -> bool { + // Only inspect the opening so a real summary that mentions an earlier + // refusal later on is still accepted. + let head: String = text.chars().take(160).collect::().to_lowercase(); + let head = head.replace('\u{2019}', "'"); + const PATTERNS: [&str; 8] = [ + "i'm sorry, but i can", + "i am sorry, but i can", + "sorry, but i can't", + "sorry, i can't", + "i cannot assist with", + "i can't assist with", + "i can't help with", + "i'm unable to help with", + ]; + // Short replies are refusals if they contain a pattern anywhere. Longer + // ones only if they open with it. + if text.chars().count() < 300 { + PATTERNS.iter().any(|pattern| head.contains(pattern)) + } else { + PATTERNS.iter().any(|pattern| head.starts_with(pattern)) + } +} + #[path = "compaction_transfer.rs"] mod transfer; pub use transfer::build_transfer_compaction_state; diff --git a/crates/jcode-base/src/compaction_tests.rs b/crates/jcode-base/src/compaction_tests.rs index 38973abc07d..8667017af03 100644 --- a/crates/jcode-base/src/compaction_tests.rs +++ b/crates/jcode-base/src/compaction_tests.rs @@ -1267,3 +1267,85 @@ fn max_context_tokens_applies_at_construction_and_reloads_before_requests() { #[path = "compaction_native_usage_tests.rs"] mod native_usage; + +#[test] +fn compaction_summary_rejects_refusals_and_empty_text_issue_2089() { + for bad in [ + "", + " ", + "I'm sorry, but I cannot assist with that request.", + "I\u{2019}m sorry, but I can\u{2019}t help with that.", + "I can't assist with that.", + ] { + assert!( + validate_compaction_summary(bad.to_string()).is_err(), + "{bad:?}" + ); + } + let real = "## Task\nThe user is fixing compaction. Earlier the assistant said \"I can't help with that\" about a deploy, then continued. ".repeat(4); + assert!(validate_compaction_summary(real).is_ok()); +} + +struct RefusingSummaryProvider; + +#[async_trait::async_trait] +impl Provider for RefusingSummaryProvider { + async fn complete( + &self, + _messages: &[Message], + _tools: &[crate::message::ToolDefinition], + _system: &str, + _resume_session_id: Option<&str>, + ) -> Result { + Ok(Box::pin(futures::stream::iter(vec![ + Ok(crate::message::StreamEvent::TextDelta( + "I'm sorry, but I cannot assist with that request.".into(), + )), + Ok(crate::message::StreamEvent::MessageEnd { + stop_reason: Some("content_filter".into()), + }), + ]))) + } + + fn name(&self) -> &str { + "refusing" + } + + fn fork(&self) -> Arc { + Arc::new(RefusingSummaryProvider) + } +} + +#[tokio::test] +async fn refused_compaction_keeps_original_messages_issue_2089() { + let messages: Vec = (0..28) + .map(|i| { + make_text_message( + if i % 2 == 0 { + Role::User + } else { + Role::Assistant + }, + &format!("turn {i}"), + ) + }) + .collect(); + let err = + generate_compaction_artifact(Arc::new(RefusingSummaryProvider), messages.clone(), None) + .await + .err() + .expect("refused summary must fail"); + assert!(format!("{err:#}").contains("guardrail"), "{err:#}"); + + let mut manager = CompactionManager::new().with_budget(1_000); + manager.seed_restored_messages(messages.len()); + manager.update_observed_input_tokens(10_000); + let _ = manager.force_compact_with(&messages, Arc::new(RefusingSummaryProvider)); + let deadline = Instant::now() + Duration::from_secs(5); + while manager.is_compacting() && Instant::now() < deadline { + tokio::time::sleep(Duration::from_millis(10)).await; + manager.check_and_apply_compaction_with(&messages); + } + assert_eq!(manager.compacted_count(), 0); + assert_eq!(manager.summary_chars(), 0); +} diff --git a/crates/jcode-base/src/companion_client.rs b/crates/jcode-base/src/companion_client.rs index e2240defc9b..45855a77ee9 100644 --- a/crates/jcode-base/src/companion_client.rs +++ b/crates/jcode-base/src/companion_client.rs @@ -11,6 +11,7 @@ //! Wire format: one JSON [`CompanionRequest`] per line, answered by one JSON //! [`CompanionResponse`] line. +use jcode_terminal_launch::RUNTIME_ENV_VARS; use serde::{Deserialize, Serialize}; use std::io::{BufRead, BufReader, Write}; use std::path::PathBuf; @@ -49,11 +50,98 @@ pub enum CompanionRequest { #[serde(default, skip_serializing_if = "Vec::is_empty")] client_terminal_env: Vec<(String, String)>, }, + /// Runtime-context spawns use a distinct operation so an older companion + /// rejects the request instead of accepting and silently dropping identity. + SpawnTerminalWithRuntime { + program: PathBuf, + args: Vec, + cwd: PathBuf, + #[serde(default, skip_serializing_if = "Option::is_none")] + title: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + kind: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + session_id: Option, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + env: Vec<(String, String)>, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + client_terminal_env: Vec<(String, String)>, + #[serde(default)] + fresh_spawn: bool, + #[serde(default, with = "runtime_env_wire")] + runtime_env: Vec<(String, String)>, + }, /// Whether any session is actively working. The companion applies the /// configured lid-close override while this is true. PowerActivity { active: bool }, } +impl CompanionRequest { + /// Build the wire-compatible terminal request, retaining the original + /// operation for callers without explicit runtime context. + pub fn spawn_terminal( + command: &crate::terminal_launch::TerminalCommand, + cwd: &std::path::Path, + ) -> Self { + let runtime_env = command + .runtime_env + .iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .cloned() + .collect::>(); + if runtime_env.is_empty() { + Self::SpawnTerminal { + program: command.program.clone(), + args: command.args.clone(), + cwd: cwd.to_path_buf(), + title: command.title.clone(), + kind: command.kind.clone(), + session_id: command.session_id.clone(), + env: command.extra_env.clone(), + client_terminal_env: command.client_terminal_env.clone(), + } + } else { + Self::SpawnTerminalWithRuntime { + program: command.program.clone(), + args: command.args.clone(), + cwd: cwd.to_path_buf(), + title: command.title.clone(), + kind: command.kind.clone(), + session_id: command.session_id.clone(), + env: command.extra_env.clone(), + client_terminal_env: command.client_terminal_env.clone(), + fresh_spawn: command.fresh_spawn, + runtime_env, + } + } + } +} +mod runtime_env_wire { + use jcode_terminal_launch::RUNTIME_ENV_VARS; + use serde::{Deserialize, Deserializer, Serialize, Serializer}; + + pub fn serialize(env: &[(String, String)], serializer: S) -> Result + where + S: Serializer, + { + let filtered = env + .iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .collect::>(); + filtered.serialize(serializer) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + Ok(Vec::<(String, String)>::deserialize(deserializer)? + .into_iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .collect()) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct CompanionResponse { pub ok: bool, @@ -87,6 +175,10 @@ impl CompanionResponse { } } +/// Error suffix for a request whose feature is off in `[companion]` config. +/// Clients treat it like an absent companion. +pub const DISABLED_SUFFIX: &str = "is disabled in [companion] config"; + /// `$JCODE_COMPANION_SOCKET`, else `jcode-companion.sock` in the runtime dir. pub fn socket_path() -> PathBuf { if let Some(path) = std::env::var_os("JCODE_COMPANION_SOCKET") { @@ -150,6 +242,10 @@ pub fn request_at( .map_err(|e| CompanionError::Io(std::io::Error::new(std::io::ErrorKind::InvalidData, e)))?; if response.ok { Ok(response) + } else if response.message.ends_with(DISABLED_SUFFIX) { + // The user turned this feature off: behave as if no companion runs, + // so callers use their local fallback. + Err(CompanionError::Unavailable) } else { Err(CompanionError::Failed(response.message)) } @@ -225,6 +321,114 @@ mod tests { ); } + #[test] + fn contextual_terminal_requests_use_a_distinct_filtered_wire_variant() { + let command = crate::terminal_launch::TerminalCommand::new( + "/usr/bin/jcode", + vec!["--resume".into(), "ses_context".into()], + ) + .title("context window") + .kind("swarm-agent") + .session_id("ses_context") + .fresh_spawn() + .spawn_env("JCODE_SPAWN_SWARM_ID", "swarm-1") + .client_terminal_env(vec![("TMUX_PANE".into(), "%12".into())]) + .runtime_env(vec![ + ("JCODE_HOME".into(), "/tmp/jcode home".into()), + ("JCODE_SOCKET".into(), "/tmp/connected.sock".into()), + ("JCODE_API_KEY".into(), "never-send".into()), + ]); + let request = CompanionRequest::spawn_terminal(&command, std::path::Path::new("/work")); + let CompanionRequest::SpawnTerminalWithRuntime { + program, + args, + cwd, + title, + kind, + session_id, + env, + client_terminal_env, + fresh_spawn, + runtime_env, + } = request + else { + panic!("runtime identity must select the explicit contextual variant"); + }; + assert_eq!(program, PathBuf::from("/usr/bin/jcode")); + assert_eq!(args, ["--resume", "ses_context"]); + assert_eq!(cwd, PathBuf::from("/work")); + assert_eq!(title.as_deref(), Some("context window")); + assert_eq!(kind.as_deref(), Some("swarm-agent")); + assert_eq!(session_id.as_deref(), Some("ses_context")); + assert!(fresh_spawn); + assert_eq!(env, [("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())]); + assert_eq!(client_terminal_env, [("TMUX_PANE".into(), "%12".into())]); + assert_eq!( + runtime_env, + [ + ("JCODE_HOME".into(), "/tmp/jcode home".into()), + ("JCODE_SOCKET".into(), "/tmp/connected.sock".into()), + ] + ); + + let contextual = CompanionRequest::SpawnTerminalWithRuntime { + program, + args, + cwd, + title, + kind, + session_id, + env, + client_terminal_env, + fresh_spawn, + runtime_env: vec![ + ("JCODE_SOCKET".into(), "/tmp/connected.sock".into()), + ("JCODE_API_KEY".into(), "must-filter-at-wire".into()), + ], + }; + let wire = serde_json::to_value(&contextual).unwrap(); + assert_eq!(wire["op"], "spawn_terminal_with_runtime"); + assert_eq!( + wire["runtime_env"], + serde_json::json!([["JCODE_SOCKET", "/tmp/connected.sock"]]) + ); + let old_spawn = serde_json::to_value(CompanionRequest::spawn_terminal( + &crate::terminal_launch::TerminalCommand::new("jcode", vec!["--resume".into()]), + std::path::Path::new("/work"), + )) + .unwrap(); + assert_eq!(old_spawn["op"], "spawn_terminal"); + assert!(old_spawn.get("runtime_env").is_none()); + assert!( + serde_json::from_str::( + r#"{"op":"spawn_terminal","program":"jcode","args":[],"cwd":"/work"}"# + ) + .is_ok() + ); + + let unsupported_variant = serde_json::to_value(&contextual).unwrap(); + assert_eq!(unsupported_variant["op"], "spawn_terminal_with_runtime"); + let old_peer_error = serde_json::from_value::(unsupported_variant) + .expect_err("old peers reject unknown operation names"); + assert!(old_peer_error.to_string().contains("unknown variant")); + + let decoded: CompanionRequest = serde_json::from_value(serde_json::json!({ + "op": "spawn_terminal_with_runtime", + "program": "jcode", + "args": [], + "cwd": "/work", + "runtime_env": [["JCODE_SOCKET", "/tmp/ok.sock"], ["JCODE_API_KEY", "drop"]] + })) + .unwrap(); + let CompanionRequest::SpawnTerminalWithRuntime { runtime_env, .. } = decoded else { + panic!("decoded contextual request variant"); + }; + assert_eq!( + runtime_env, + [("JCODE_SOCKET".into(), "/tmp/ok.sock".into())] + ); + } + #[cfg(unix)] #[test] fn missing_socket_reports_unavailable() { @@ -265,3 +469,34 @@ mod tests { assert!(matches!(error, CompanionError::Failed(ref m) if m == "no opener")); } } + +#[cfg(test)] +#[derive(Debug, Deserialize)] +#[serde(tag = "op", rename_all = "snake_case")] +enum OldCompanionRequest { + Status, + Open { + target: String, + }, + Reveal { + path: PathBuf, + }, + SpawnTerminal { + program: PathBuf, + args: Vec, + cwd: PathBuf, + #[serde(default)] + title: Option, + #[serde(default)] + kind: Option, + #[serde(default)] + session_id: Option, + #[serde(default)] + env: Vec<(String, String)>, + #[serde(default)] + client_terminal_env: Vec<(String, String)>, + }, + PowerActivity { + active: bool, + }, +} diff --git a/crates/jcode-base/src/config.rs b/crates/jcode-base/src/config.rs index 9b3a3effef9..3f27b16872f 100644 --- a/crates/jcode-base/src/config.rs +++ b/crates/jcode-base/src/config.rs @@ -10,9 +10,10 @@ pub use jcode_config_types::{ GatewayConfig, HookCommands, HooksConfig, KeybindingsConfig, LatexRenderingMode, LaunchHotkeyEntry, LaunchHotkeysConfig, MarkdownSpacingMode, MenubarConfig, NamedProviderAuth, NamedProviderConfig, NamedProviderModelConfig, NamedProviderType, NativeScrollbarConfig, - NotificationsConfig, PowerConfig, ProviderConfig, ReasoningDisplayMode, SafetyConfig, - SessionConfig, SessionPickerResumeAction, SponsorsConfig, SwarmSpawnMode, SwarmStripLayout, - TerminalConfig, UpdateChannel, WebSearchConfig, WebSearchEngine, + NotificationsConfig, PowerConfig, ProviderConfig, ReasoningDisplayMode, ResumeOpenElsewhere, + ResumeTimeMode, SafetyConfig, SessionConfig, SessionPickerResumeAction, SponsorsConfig, + SwarmSpawnMode, SwarmStripLayout, TerminalConfig, UpdateChannel, WebSearchConfig, + WebSearchEngine, }; use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, BTreeSet, HashSet}; @@ -125,6 +126,8 @@ const CONFIG_ENV_KEYS: &[&str] = &[ "HOME", "JCODE_COMMIT_LOG_KEY", "JCODE_RESUME_PROJECT_SCOPE", + "JCODE_RESUME_TIME", + "JCODE_BROWSER_HANDOFF", "JCODE_SWARM_STALL_AFTER_SECS", "JCODE_SWARM_STALL_NUDGE", "JCODE_ACP_PROFILE", @@ -717,6 +720,9 @@ pub struct Config { /// Power-management configuration (prevent sleep while streaming) pub power: PowerConfig, + /// `jcode companion`: per-login helper for desktop effects. + pub companion: CompanionConfig, + /// macOS menu bar session indicator pub menubar: MenubarConfig, @@ -993,6 +999,46 @@ fn normalize_tool_name(name: &str) -> String { jcode_tool_types::resolve_tool_name(trimmed).to_string() } +/// `jcode companion` features. Each toggle is read when the companion starts, +/// except `autostart`, which clients read when they launch. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[serde(default)] +pub struct CompanionConfig { + /// Start the companion in the background when a Jcode client launches + /// and none is running. + pub autostart: bool, + /// Copilot-key push-to-talk (Linux). Also needs `[dictation] global_devices`. + pub voice: bool, + /// Show a live waveform and transcript in the focused Jcode CLI while + /// companion push-to-talk records. + pub voice_waveform: bool, + /// Show desktop notifications for companion voice state. + pub voice_notifications: bool, + /// Open URLs and files for the server. + pub open: bool, + /// Open new terminal windows for the server. + pub spawn_terminal: bool, + /// Lid-close override while agents work (macOS/Windows). + pub lid: bool, + /// Hourly browser bridge auto-update. + pub browser_updates: bool, +} + +impl Default for CompanionConfig { + fn default() -> Self { + Self { + autostart: true, + voice: true, + voice_waveform: true, + voice_notifications: true, + open: true, + spawn_terminal: true, + lid: true, + browser_updates: true, + } + } +} + /// External dictation / speech-to-text integration. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(default)] @@ -1017,6 +1063,9 @@ pub struct DictationConfig { /// (Linux evdev nodes, preferably stable `/dev/input/by-id/...` paths). /// Empty disables the companion's global voice listener. pub global_devices: Vec, + /// Show live voice input (waveform and streaming transcript) inside the + /// input box. False puts it in the status line instead. + pub composer_preview: bool, } impl Default for DictationConfig { @@ -1029,6 +1078,7 @@ impl Default for DictationConfig { vocabulary: Vec::new(), recorder: String::new(), global_devices: Vec::new(), + composer_preview: true, } } } diff --git a/crates/jcode-base/src/config/default_file.rs b/crates/jcode-base/src/config/default_file.rs index 7c36e81cc59..439eccb5e7c 100644 --- a/crates/jcode-base/src/config/default_file.rs +++ b/crates/jcode-base/src/config/default_file.rs @@ -157,6 +157,10 @@ timeout_secs = 90 # without Jcode Desktop. Empty disables it. # global_devices = [] +# Show live voice input inside the input box: a waveform row above the prompt +# and the transcript streaming in as ghost text. false uses the status line. +# composer_preview = true + [display] # Diff display mode: "off", "inline" (default), "full-inline", or "file" diff_mode = "inline" @@ -266,6 +270,21 @@ prompt_entry_animation = true # disconnected SDK client, since the server cannot observe OS windows directly. # resume_recent_close_seconds = 30 +# What Resume does when the picked session is already open in another local +# jcode window (default: "off"). Linux only. +# "off": attach a second client, leave the other window alone +# "move": bring the existing window to the current workspace and focus it +# (niri). Falls back to "replace" when the window cannot be moved. +# "replace": resume as usual and close the other window's client +# resume_open_elsewhere = "off" + +# Which time Resume shows as "x ago" and sorts by (default: "message"). +# "message": newest message of any role (prompts, replies, tool results) +# "activity": newest of the last message and the last open/resume +# "opened": last time the session was opened, resumed, or attached +# "created": when the session was created +# resume_time = "message" + # Disable specific animation variants by name. # Examples: ["donut"] or ["donut", "orbit_rings"] # Legacy aliases such as "three_rings" and "gyroscope" are still accepted. @@ -291,6 +310,7 @@ prompt_entry_animation = true # copy_badge_alt_label = "" # Color theme: "auto" (query the terminal background), "dark", or "light". +# Auto follows light/dark changes during the session (refreshes every 5 seconds). # theme = "auto" # Per-role color overrides. Every color the TUI renders is configurable: the @@ -516,8 +536,22 @@ preserve_reasoning_context = true # countdown = 3-second countdown before retrying on another provider; press Esc to cancel (default) # manual = show a notice and let you switch yourself # cross_provider_failover = "manual" +# Switch immediately only for definitive usage exhaustion (default: true). +# False preserves the countdown. Manual mode always disables automatic switching. +exhausted_usage_immediate_failover = true +# Retry short recoverable Anthropic throttles on the same account (default: true). +# False yields immediately to the account router when other pool members exist. +anthropic_retry_recoverable_rate_limits = true # Try another account on the same provider before switching providers (default: true) # same_provider_account_failover = false +# Opt in to spending earned OpenAI banked resets at an exhausted weekly limit. +# Retries the waiting request automatically. Never buys credits. Default: false. +# openai_auto_banked_reset = true +# Opt in separately to earned Anthropic Fable weekly resets. Never buys credits. +# anthropic_auto_fable_reset = true +# Preserve earned resets while another approved pool route is ready (default: true). +# Set false to redeem before account/provider failover instead. +# auto_reset_only_when_no_routes = true cross_provider_failover = "countdown" # Copilot premium mode: "normal" (default), "one" (first msg only), "zero" (all free) # Set to "zero" if you have premium Copilot and want free requests @@ -844,6 +878,29 @@ prevent_sleep_while_streaming = true # saved to ~/.jcode/lid_override.json and restored afterwards, even after a crash. block_lid_close = true +[companion] +# `jcode companion` is a per-login helper that handles desktop effects for the +# shared server. Toggles apply when the companion starts. Restart it +# (`systemctl --user restart jcode-companion`, or kill it and relaunch Jcode) +# after changing them. +# Start it in the background when a Jcode client launches and none is running. +autostart = true +# Copilot-key push-to-talk (Linux). Also needs [dictation] global_devices. +voice = true +# Live waveform and transcript in the focused Jcode CLI while recording. +voice_waveform = true +# Desktop notifications for voice state. When off, recording still needs the +# waveform to be shown in a Jcode CLI as its visible indicator. +voice_notifications = true +# Open URLs and files for the server. Off: clients use their own opener. +open = true +# Open new terminal windows for the server. Off: clients launch locally. +spawn_terminal = true +# Lid-close override while agents work (macOS/Windows, see [power]). +lid = true +# Hourly browser bridge auto-update. +browser_updates = true + [menubar] # macOS only: show the jcode session count in the menu bar. Set to false to # stop the menu bar helper (a running helper exits within a second). diff --git a/crates/jcode-base/src/config/env_overrides.rs b/crates/jcode-base/src/config/env_overrides.rs index 6c3f5317457..12dc85db293 100644 --- a/crates/jcode-base/src/config/env_overrides.rs +++ b/crates/jcode-base/src/config/env_overrides.rs @@ -295,6 +295,16 @@ impl Config { self.display.resume_project_scope = parsed; } } + if let Ok(v) = std::env::var("JCODE_RESUME_TIME") { + if let Some(parsed) = super::ResumeTimeMode::parse(&v) { + self.display.resume_time = parsed; + } + } + if let Ok(v) = std::env::var("JCODE_RESUME_OPEN_ELSEWHERE") { + if let Some(parsed) = super::ResumeOpenElsewhere::parse(&v) { + self.display.resume_open_elsewhere = parsed; + } + } if let Ok(v) = std::env::var("JCODE_PERFORMANCE") { let trimmed = v.trim().to_lowercase(); if matches!(trimmed.as_str(), "auto" | "full" | "reduced" | "minimal") { diff --git a/crates/jcode-base/src/config_color_tests.rs b/crates/jcode-base/src/config_color_tests.rs index 48de40911f3..f71adfeb2d1 100644 --- a/crates/jcode-base/src/config_color_tests.rs +++ b/crates/jcode-base/src/config_color_tests.rs @@ -158,3 +158,27 @@ fn reported_display_config_survives_a_real_config_file_round_trip() { } Config::invalidate_cache(); } + +/// Saving a config with no color overrides must not leave a bare +/// `[display.colors]` header behind. Users paste a palette under their own +/// header, and a second header is a TOML error that takes the whole file with +/// it, breaking every other reader including Desktop. +#[test] +fn saved_config_omits_an_empty_colors_table_but_keeps_overrides() { + let mut config = Config::default(); + let empty = toml::to_string_pretty(&config).expect("serialize default config"); + assert!( + !empty.contains("[display.colors]"), + "an empty palette must not be written: {empty}" + ); + let pasted = format!("{empty}\n[display.colors]\nai = \"#ffaa00\"\n"); + let reloaded: Config = toml::from_str(&pasted).expect("pasted palette must parse"); + assert_eq!(reloaded.display.colors.len(), 1); + + config + .display + .colors + .insert("ai".to_string(), "#ffaa00".to_string()); + let saved = toml::to_string_pretty(&config).expect("serialize config with colors"); + assert!(saved.contains("[display.colors]\nai = \"#ffaa00\"")); +} diff --git a/crates/jcode-base/src/config_tests.rs b/crates/jcode-base/src/config_tests.rs index 598d01a10a7..7d9638ff05d 100644 --- a/crates/jcode-base/src/config_tests.rs +++ b/crates/jcode-base/src/config_tests.rs @@ -1056,6 +1056,33 @@ fn test_provider_failover_defaults_match_new_behavior() { super::CrossProviderFailoverMode::Countdown ); assert!(provider.same_provider_account_failover); + assert!(!provider.openai_auto_banked_reset); + assert!(!provider.anthropic_auto_fable_reset); +} + +#[test] +fn automatic_banked_reset_requires_explicit_opt_in() { + let cfg: Config = toml::from_str("[provider]\nopenai_auto_banked_reset = true\n").unwrap(); + assert!(cfg.provider.openai_auto_banked_reset); + let cfg: Config = toml::from_str("[provider]\n").unwrap(); + assert!(!cfg.provider.openai_auto_banked_reset); + assert!(!cfg.provider.anthropic_auto_fable_reset); + let cfg: Config = toml::from_str("[provider]\nanthropic_auto_fable_reset = true\n").unwrap(); + assert!(cfg.provider.anthropic_auto_fable_reset); + assert!(!cfg.provider.openai_auto_banked_reset); +} + +#[test] +fn automatic_reset_routes_first_by_default_and_can_be_overridden() { + let cfg: Config = toml::from_str("[provider]\n").unwrap(); + assert!(cfg.provider.auto_reset_only_when_no_routes); + assert!(ProviderConfig::default().auto_reset_only_when_no_routes); + let cfg: Config = toml::from_str( + "[provider]\nopenai_auto_banked_reset = true\nanthropic_auto_fable_reset = true\nauto_reset_only_when_no_routes = false\n", + ).unwrap(); + assert!(!cfg.provider.auto_reset_only_when_no_routes); + assert!(cfg.provider.openai_auto_banked_reset); + assert!(cfg.provider.anthropic_auto_fable_reset); } #[test] diff --git a/crates/jcode-base/src/inherited_children.rs b/crates/jcode-base/src/inherited_children.rs new file mode 100644 index 00000000000..bf108f69b8b --- /dev/null +++ b/crates/jcode-base/src/inherited_children.rs @@ -0,0 +1,174 @@ +//! Reap child processes inherited across an `exec` reload. +//! +//! The daemon reloads itself with `exec`, which keeps the PID. Children the +//! previous image spawned (MCP servers, power-inhibit helpers, shell tools) +//! stay our children, but the new image holds no `Child` handle for them. Their +//! pipes are close-on-exec, so most exit right after the reload and then sit +//! as zombies forever because nobody calls `waitpid`. Over a day of self-dev +//! reloads that reached hundreds of zombie `npm`/`node` entries. +//! +//! Only PIDs present at startup are touched. Children spawned by this image +//! are owned by tokio/std handles, and reaping them here would steal their +//! exit status. An inherited PID cannot be reused while it is unreaped, so the +//! snapshot stays accurate until we reap it. + +/// Environment marker set on MCP stdio children so a fresh image can tell +/// leaked MCP servers (safe to kill: the new image reconnects its own) apart +/// from detached work that is meant to outlive a reload. +pub const MCP_CHILD_MARKER_ENV: &str = "JCODE_MCP_CHILD"; + +#[cfg(target_os = "linux")] +mod imp { + use std::collections::HashSet; + use std::time::Duration; + + const REAP_INTERVAL: Duration = Duration::from_secs(30); + + fn children_of(pid: u32) -> HashSet { + let mut out = HashSet::new(); + let Ok(tasks) = std::fs::read_dir(format!("/proc/{pid}/task")) else { + return out; + }; + for task in tasks.flatten() { + let path = task.path().join("children"); + if let Ok(text) = std::fs::read_to_string(path) { + out.extend( + text.split_whitespace() + .filter_map(|p| match p.parse::() { + Ok(pid) => Some(pid), + Err(error) => { + crate::logging::info(&format!( + "Invalid inherited PID {p}: {error}" + )); + None + } + }), + ); + } + } + out + } + + fn has_mcp_marker(pid: i32) -> bool { + let Ok(environ) = std::fs::read(format!("/proc/{pid}/environ")) else { + return false; + }; + let needle = format!("{}=", super::MCP_CHILD_MARKER_ENV); + environ + .split(|b| *b == 0) + .any(|entry| entry.starts_with(needle.as_bytes())) + } + + /// Reap whatever has exited. Returns the PIDs still alive. + fn reap(pids: &mut HashSet) { + pids.retain(|&pid| { + let mut status = 0; + // SAFETY: plain syscall on a PID we inherited and nobody else owns. + let rc = unsafe { libc::waitpid(pid, &mut status, libc::WNOHANG) }; + rc == 0 + }); + } + + pub fn start() { + let mut inherited = children_of(std::process::id()); + if inherited.is_empty() { + return; + } + let mut killed = 0usize; + for &pid in &inherited { + if has_mcp_marker(pid) { + // MCP children run in their own process group (see mcp client), + // so this also stops `npx` wrappers' node grandchildren. + // SAFETY: signalling a process group we created. + unsafe { + libc::kill(-pid, libc::SIGKILL); + libc::kill(pid, libc::SIGKILL); + } + killed += 1; + } + } + let before = inherited.len(); + reap(&mut inherited); + crate::logging::info(&format!( + "inherited_children: {before} inherited, killed {killed} stale MCP servers, reaped {}", + before - inherited.len() + )); + if inherited.is_empty() { + return; + } + if let Err(error) = std::thread::Builder::new() + .name("jcode-inherited-reaper".to_string()) + .spawn(move || { + while !inherited.is_empty() { + std::thread::sleep(REAP_INTERVAL); + reap(&mut inherited); + } + }) + { + crate::logging::info(&format!("Unable to start inherited child reaper: {error}")); + } + } + + #[cfg(test)] + mod tests { + #[test] + fn reaps_exited_inherited_child() { + let child = std::process::Command::new("true").spawn().unwrap(); + let pid = child.id() as i32; + std::mem::forget(child); + std::thread::sleep(std::time::Duration::from_millis(200)); + let mut set = std::collections::HashSet::from([pid]); + super::reap(&mut set); + assert!(set.is_empty()); + assert!(!std::path::Path::new(&format!("/proc/{pid}")).exists()); + } + + #[test] + fn detects_mcp_marker() { + let mut child = std::process::Command::new("sleep") + .arg("5") + .env(super::super::MCP_CHILD_MARKER_ENV, "x") + .spawn() + .unwrap(); + let mut plain = std::process::Command::new("sleep") + .arg("5") + .spawn() + .unwrap(); + // Some process launchers return before the final image's environ is + // visible through procfs. Wait for the observable marker, not a + // fixed startup delay, and retain diagnostics on a real failure. + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2); + while !super::has_mcp_marker(child.id() as i32) { + if std::time::Instant::now() >= deadline { + let environ = std::fs::read(format!("/proc/{}/environ", child.id())) + .map(|bytes| format!("read {} bytes without marker", bytes.len())); + child.kill().unwrap(); + child.wait().unwrap(); + plain.kill().unwrap(); + plain.wait().unwrap(); + panic!("MCP marker did not become visible: {environ:?}"); + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert!(!super::has_mcp_marker(plain.id() as i32)); + assert!(super::children_of(std::process::id()).contains(&(child.id() as i32))); + let _ = child.kill(); + let _ = plain.kill(); + let _ = child.wait(); + let _ = plain.wait(); + } + } +} + +/// Kill stale MCP servers and reap exited children inherited from a previous +/// process image. Call once at daemon startup, before spawning anything. +pub fn start() { + // Idempotent: process startup calls this for every mode (clients exec + // reload too), and the server calls it again. A second snapshot taken + // after this image spawned children would steal their exit statuses. + static ONCE: std::sync::Once = std::sync::Once::new(); + #[cfg(target_os = "linux")] + ONCE.call_once(imp::start); + #[cfg(not(target_os = "linux"))] + ONCE.call_once(|| {}); +} diff --git a/crates/jcode-base/src/lib.rs b/crates/jcode-base/src/lib.rs index 69bf6a36bbb..76eba5840f6 100644 --- a/crates/jcode-base/src/lib.rs +++ b/crates/jcode-base/src/lib.rs @@ -54,6 +54,7 @@ pub mod hooks; pub mod id; pub mod image_normalize; pub mod import; +pub mod inherited_children; pub mod jev; pub mod kv_cache_monitor; pub mod live_tests; diff --git a/crates/jcode-base/src/mcp/client.rs b/crates/jcode-base/src/mcp/client.rs index a26d382b588..669394ef64f 100644 --- a/crates/jcode-base/src/mcp/client.rs +++ b/crates/jcode-base/src/mcp/client.rs @@ -211,15 +211,28 @@ impl McpClient { // its process environment, and blindly inheriting them exposes those // credentials to every configured MCP executable (issue #771). let inherited: HashMap = std::env::vars().collect(); - let env = mcp_child_env(inherited, &config.env); + let mut env = mcp_child_env(inherited, &config.env); + let interactive = INTERACTIVE_CONNECT.try_with(|v| *v).unwrap_or(false); + if !interactive { + suppress_browser_launch(&mut env, &config.env); + } let mut command = Command::new(&config.command); + if !interactive { + command.env_remove("DISPLAY").env_remove("WAYLAND_DISPLAY"); + } command .args(&config.args) .envs(&env) + .env(crate::inherited_children::MCP_CHILD_MARKER_ENV, "1") .stdin(Stdio::piped()) .stdout(Stdio::piped()) - .stderr(Stdio::piped()); + .stderr(Stdio::piped()) + .kill_on_drop(true); + // Own process group so shutdown also stops `npx` wrapper descendants + // (node), which otherwise outlive the wrapper and keep running. + #[cfg(unix)] + command.process_group(0); if let Some(dir) = working_dir { command.current_dir(dir); } @@ -561,6 +574,7 @@ impl McpClient { tokio::time::sleep(std::time::Duration::from_millis(100)).await; + kill_process_group(child); let _ = child.kill().await; } Backend::Remote(connection) => connection.shutdown().await, @@ -590,6 +604,34 @@ impl McpClient { } } +tokio::task_local! { + /// Set while a user explicitly asked to connect a server. Background + /// auto-connects (daemon startup, session start) leave it unset. + static INTERACTIVE_CONNECT: bool; +} + +/// Run `fut` as a user-initiated connect, allowing MCP children (for example +/// `mcp-remote`) to open an OAuth browser window. +pub async fn interactive_connect(fut: F) -> F::Output { + INTERACTIVE_CONNECT.scope(true, fut).await +} + +/// Background auto-connects must never pop browser windows. `mcp-remote` and +/// similar wrappers open an OAuth page whenever tokens are missing, which made +/// every reboot spawn a pile of sign-in tabs. Hide the graphical session and +/// point `BROWSER` at a no-op so openers fail and the server reports an auth +/// error instead. Explicitly configured env values are respected. +fn suppress_browser_launch(env: &mut HashMap, explicit: &HashMap) { + for key in ["DISPLAY", "WAYLAND_DISPLAY"] { + if !explicit.contains_key(key) { + env.remove(key); + } + } + if !explicit.contains_key("BROWSER") { + env.insert("BROWSER".to_string(), "true".to_string()); + } +} + /// Secrets that an MCP child must not receive merely because jcode has them. /// /// This intentionally applies only to inherited values. A server can still be @@ -621,17 +663,56 @@ fn mcp_child_env( impl Drop for McpClient { fn drop(&mut self) { if let Backend::Stdio(child) = &mut self.backend { - let _ = child.start_kill(); + kill_process_group(child); + if let Err(error) = child.start_kill() { + crate::logging::info(&format!("MCP child cleanup failed: {error}")); + } } } } +fn kill_process_group(child: &Child) { + #[cfg(unix)] + if let Some(pid) = child.id() { + // SAFETY: the child leads its own process group (process_group(0)). + unsafe { + libc::kill(-(pid as i32), libc::SIGKILL); + } + } + #[cfg(not(unix))] + let _ = child; +} + #[cfg(all(test, unix))] mod tests { use super::{McpClient, is_sensitive_inherited_env_key, mcp_child_env}; use crate::mcp::protocol::McpServerConfig; use std::collections::HashMap; + #[test] + fn background_connect_env_cannot_open_browser() { + let mut env: HashMap = [ + ("DISPLAY", ":0"), + ("WAYLAND_DISPLAY", "wayland-1"), + ("PATH", "/usr/bin"), + ] + .into_iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(); + super::suppress_browser_launch(&mut env, &HashMap::new()); + assert!(!env.contains_key("DISPLAY")); + assert!(!env.contains_key("WAYLAND_DISPLAY")); + assert_eq!(env.get("BROWSER").map(String::as_str), Some("true")); + assert_eq!(env.get("PATH").map(String::as_str), Some("/usr/bin")); + + let explicit: HashMap = [("DISPLAY".to_string(), ":1".to_string())] + .into_iter() + .collect(); + let mut env = explicit.clone(); + super::suppress_browser_launch(&mut env, &explicit); + assert_eq!(env.get("DISPLAY").map(String::as_str), Some(":1")); + } + #[test] fn inherited_mcp_env_scrubs_provider_credentials() { for key in [ diff --git a/crates/jcode-base/src/mcp/mod.rs b/crates/jcode-base/src/mcp/mod.rs index fe0ea3edadb..f20dd62f8e7 100644 --- a/crates/jcode-base/src/mcp/mod.rs +++ b/crates/jcode-base/src/mcp/mod.rs @@ -15,7 +15,9 @@ mod remote; pub mod schema_cache; mod tool; -pub use client::{DEFAULT_MCP_REQUEST_TIMEOUT, McpClient, McpHandle, request_timeout_for}; +pub use client::{ + DEFAULT_MCP_REQUEST_TIMEOUT, McpClient, McpHandle, interactive_connect, request_timeout_for, +}; pub use manager::McpManager; pub use pool::{SharedMcpPool, get_shared_pool, init_shared_pool}; pub use protocol::*; diff --git a/crates/jcode-base/src/mcp/pool.rs b/crates/jcode-base/src/mcp/pool.rs index 7822ec58a2d..24fc11d72e4 100644 --- a/crates/jcode-base/src/mcp/pool.rs +++ b/crates/jcode-base/src/mcp/pool.rs @@ -18,11 +18,28 @@ use std::time::{Duration, Instant}; use tokio::sync::{Mutex, Notify, RwLock}; const FAILED_CONNECT_RETRY_COOLDOWN: Duration = Duration::from_secs(30); +/// Repeated failures (e.g. `mcp-remote` stuck waiting for OAuth) back off +/// exponentially up to this cap instead of respawning `npx` every 30s. +const MAX_FAILED_CONNECT_RETRY_COOLDOWN: Duration = Duration::from_secs(30 * 60); #[derive(Clone)] struct FailedConnectRecord { message: String, failed_at: Instant, + failures: u32, +} + +impl FailedConnectRecord { + fn cooldown(&self) -> Duration { + failed_connect_cooldown(self.failures) + } +} + +fn failed_connect_cooldown(failures: u32) -> Duration { + let shift = failures.saturating_sub(1).min(10); + FAILED_CONNECT_RETRY_COOLDOWN + .saturating_mul(1 << shift) + .min(MAX_FAILED_CONNECT_RETRY_COOLDOWN) } enum ConnectAttempt { @@ -342,11 +359,13 @@ impl SharedMcpPool { } Err(error) => { let mut errors = self.last_errors.write().await; + let failures = errors.get(name).map_or(0, |r| r.failures) + 1; errors.insert( name.to_string(), FailedConnectRecord { message: format!("{:#}", error), failed_at: Instant::now(), + failures, }, ); } @@ -372,7 +391,8 @@ impl SharedMcpPool { config: McpServerConfig, ) -> std::result::Result { if let Some(record) = self.recent_failure(&name).await { - let retry_after = FAILED_CONNECT_RETRY_COOLDOWN + let retry_after = record + .cooldown() .saturating_sub(record.failed_at.elapsed()) .as_secs() .max(1); @@ -455,7 +475,7 @@ impl SharedMcpPool { .read() .await .get(name) - .filter(|record| record.failed_at.elapsed() < FAILED_CONNECT_RETRY_COOLDOWN) + .filter(|record| record.failed_at.elapsed() < record.cooldown()) .cloned() } } @@ -601,3 +621,19 @@ mod tests { ); } } + +#[cfg(test)] +mod backoff_tests { + use super::*; + + #[test] + fn failed_connect_cooldown_grows_and_caps() { + assert_eq!(failed_connect_cooldown(1), Duration::from_secs(30)); + assert_eq!(failed_connect_cooldown(2), Duration::from_secs(60)); + assert_eq!(failed_connect_cooldown(4), Duration::from_secs(240)); + assert_eq!( + failed_connect_cooldown(50), + MAX_FAILED_CONNECT_RETRY_COOLDOWN + ); + } +} diff --git a/crates/jcode-base/src/message.rs b/crates/jcode-base/src/message.rs index f35f3d010ca..3d419ed3be6 100644 --- a/crates/jcode-base/src/message.rs +++ b/crates/jcode-base/src/message.rs @@ -32,10 +32,16 @@ pub use notifications::{ /// Legacy persisted wakes can retain the provider's user role even though /// their display role is a background notification, never a human prompt. pub fn is_background_task_notification_content(content: &str) -> bool { - content - .trim_start() - .starts_with("🐝 **Swarm await finished**") - || parse_background_task_notification_markdown(content).is_some() + let trimmed = content.trim_start(); + if trimmed.starts_with("🐝 **Swarm await finished**") { + return true; + } + // Both parsers anchor on a `**Background task` header. Bail out before + // they normalize (allocate a full copy of) every message on every frame. + if !trimmed.starts_with("**Background task") { + return false; + } + parse_background_task_notification_markdown(content).is_some() || parse_background_task_progress_notification_markdown(content).is_some() } diff --git a/crates/jcode-base/src/message/notifications.rs b/crates/jcode-base/src/message/notifications.rs index c5c9f11e24b..f42f4267485 100644 --- a/crates/jcode-base/src/message/notifications.rs +++ b/crates/jcode-base/src/message/notifications.rs @@ -1,5 +1,15 @@ use super::*; +/// Normalize CRLF/CR to LF, borrowing when the input has no carriage returns +/// (the common case) so per-frame role checks do not copy every message. +fn normalize_line_endings(content: &str) -> std::borrow::Cow<'_, str> { + if content.contains('\r') { + std::borrow::Cow::Owned(content.replace("\r\n", "\n").replace('\r', "\n")) + } else { + std::borrow::Cow::Borrowed(content) + } +} + fn sanitize_fenced_block(text: &str) -> String { text.replace("```", "``\u{200b}`") } @@ -401,7 +411,7 @@ pub fn parse_background_task_progress_notification_markdown( static HEADER_RE: OnceLock> = OnceLock::new(); static INLINE_RE: OnceLock> = OnceLock::new(); - let normalized = content.replace("\r\n", "\n").replace('\r', "\n"); + let normalized = normalize_line_endings(content); let trimmed = normalized.trim(); let header_re = HEADER_RE @@ -494,7 +504,7 @@ pub fn parse_background_task_notification_markdown( .get_or_init(|| compile_static_regex(r#"^_Full output:_ `(?P[^`]+)`$"#)) .as_ref()?; - let normalized = content.replace("\r\n", "\n").replace('\r', "\n"); + let normalized = normalize_line_endings(content); let mut sections = normalized.split("\n\n"); let header = sections.next()?.trim(); let captures = header_re.captures(header)?; diff --git a/crates/jcode-base/src/mobile_runner.rs b/crates/jcode-base/src/mobile_runner.rs index 558f5fd2a6f..0d71ae00e0a 100644 --- a/crates/jcode-base/src/mobile_runner.rs +++ b/crates/jcode-base/src/mobile_runner.rs @@ -15,7 +15,53 @@ use serde::Deserialize; use std::path::PathBuf; use std::process::Stdio; use std::time::Duration; -use tokio::io::{AsyncBufReadExt, BufReader}; +#[path = "mobile_runner_events.rs"] +mod events; +#[cfg(test)] +#[path = "mobile_runner_poll_tests.rs"] +mod poll_tests; +#[path = "mobile_runner_process.rs"] +mod process; +#[cfg(test)] +#[path = "mobile_runner_process_tests.rs"] +mod process_tests; + +struct StageTimer { + stage: &'static str, + outcome: &'static str, + start: std::time::Instant, +} +impl StageTimer { + fn new(stage: &'static str) -> Self { + Self { + stage, + outcome: "error_or_cancelled", + start: std::time::Instant::now(), + } + } + + fn record(&self, duration_ms: u128) -> String { + format!( + "[CLOUD_TIMING] operation=mobile_runner stage={} outcome={} duration_ms={}", + self.stage, self.outcome, duration_ms + ) + } +} +impl Drop for StageTimer { + fn drop(&mut self) { + crate::logging::info(&self.record(self.start.elapsed().as_millis())); + } +} + +async fn timed_stage( + stage: &'static str, + future: impl std::future::Future>, +) -> Result { + let mut timing = StageTimer::new(stage); + let result = future.await; + timing.outcome = if result.is_ok() { "ok" } else { "error" }; + result +} const POLL_INTERVAL: Duration = Duration::from_secs(5); const TURN_TIMEOUT: Duration = Duration::from_secs(40 * 60); @@ -27,6 +73,27 @@ pub struct MobileCommand { pub kind: String, pub session_id: Option, pub text: String, + #[serde(default)] + pub created_at: Option, + #[serde(default)] + pub claimed_at: Option, +} + +impl MobileCommand { + // Both timestamps originate at the queue API, avoiding client clock skew. + // This includes wake/backlog/poll delay, not claim response transport time. + fn queue_wait_ms(&self) -> Option { + let (Ok(created), Ok(claimed)) = ( + chrono::DateTime::parse_from_rfc3339(self.created_at.as_deref()?), + chrono::DateTime::parse_from_rfc3339(self.claimed_at.as_deref()?), + ) else { + return None; + }; + if claimed < created { + return None; + } + Some(claimed.signed_duration_since(created).num_milliseconds() as u64) + } } #[derive(Deserialize)] @@ -183,10 +250,13 @@ async fn import_local_session( key: &str, id: &str, ) -> Result { - let (metadata, messages) = fetch_local_transcript(client, base, key, id).await?; - let mut session = transfer_session(&metadata, messages, &workspace_dir())?; - session.save()?; - Ok(session.id) + timed_stage("transfer", async { + let (metadata, messages) = fetch_local_transcript(client, base, key, id).await?; + let mut session = transfer_session(&metadata, messages, &workspace_dir())?; + session.save()?; + Ok(session.id) + }) + .await } /// True on a managed Jcode Cloud machine. @@ -194,6 +264,21 @@ pub fn is_cloud_machine() -> bool { mobile_sync::session_source() == "cloud" } +/// Keep the cloud host's idle-poweroff guard from stopping the VM while a +/// daemon session is mid-turn without an SSH client attached. The host's +/// `idle.sh` treats a marker touched within 5 minutes as busy. Only touches, +/// never removes: staleness releases it, and the mobile runner owns removal. +pub fn touch_cloud_activity_marker() { + if !is_cloud_machine() { + return; + } + if let Some(path) = busy_marker() + && let Err(error) = std::fs::write(&path, b"") + { + crate::logging::warn(&format!("cloud activity marker update failed: {error}")); + } +} + fn busy_marker() -> Option { match crate::storage::jcode_dir() { Ok(directory) => Some(directory.join("mobile-runner-busy")), @@ -296,61 +381,17 @@ async fn run_turn(command: &MobileCommand, session_id: Option<&str>) -> Result match line? { - Some(line) => { - if session.is_none() { - session = session_id_from_ndjson_line(&line); - } - } - None => break, - }, - _ = touch.tick() => { - if let Some(m) = &marker - && let Err(error) = std::fs::write(m, b"") - { - crate::logging::warn(&format!("mobile runner busy marker update failed: {error}")); - } - } - _ = &mut deadline => { - if let Err(error) = child.kill().await { - crate::logging::warn(&format!("mobile runner timed-out child cleanup failed: {error}")); - } - anyhow::bail!("turn timed out"); - } - } - } - let output = child.wait_with_output().await?; - if let Some(m) = &marker - && let Err(error) = std::fs::remove_file(m) - && error.kind() != std::io::ErrorKind::NotFound - { - crate::logging::warn(&format!( - "mobile runner busy marker removal failed: {error}" - )); - } - if !output.status.success() { - let stderr = String::from_utf8_lossy(&output.stderr); - let tail: String = stderr - .chars() - .rev() - .take(600) - .collect::>() - .into_iter() - .rev() - .collect(); - anyhow::bail!("jcode run failed ({}): {}", output.status, tail.trim()); - } - session.context("jcode run did not report a session id") + timed_stage( + "turn", + process::run( + cmd, + session_id, + busy_marker(), + TURN_TIMEOUT, + BUSY_TOUCH_INTERVAL, + ), + ) + .await } /// Claim and run at most one queued command. Returns whether one ran. @@ -360,13 +401,15 @@ pub async fn run_one(client: &reqwest::Client) -> Result { let base = subscription_api::configured_api_base() .trim_end_matches('/') .to_string(); - let Some(command) = claim(client, &base, &key).await? else { + let claimed = timed_stage("claim", claim(client, &base, &key)).await?; + let Some(command) = claimed else { return Ok(false); }; - crate::logging::info(&format!( - "mobile runner: {} command {}", - command.kind, command.command_id - )); + if let Some(duration_ms) = command.queue_wait_ms() { + crate::logging::info(&format!( + "[CLOUD_TIMING] operation=mobile_runner stage=queue_wait outcome=ok duration_ms={duration_ms}" + )); + } let outcome = async { let session_id = match command.kind.as_str() { "transfer" => Some( @@ -393,10 +436,15 @@ pub async fn run_one(client: &reqwest::Client) -> Result { run_turn(&command, session_id.as_deref()).await } .await; - if let Err(error) = mobile_sync::sync_once(client).await { + let sync_result = timed_stage("sync", mobile_sync::sync_once(client)).await; + if let Err(error) = sync_result { crate::logging::warn(&format!("mobile runner transcript sync failed: {error:#}")); } - complete(client, &base, &key, &command.command_id, &outcome).await?; + timed_stage( + "complete", + complete(client, &base, &key, &command.command_id, &outcome), + ) + .await?; outcome.map(|_| true) } @@ -411,6 +459,58 @@ pub fn ensure_sync_enabled() { } } +trait PollWait { + fn wait(&mut self, delay: Duration) -> impl std::future::Future + Send; +} + +impl PollWait for events::Wakeups { + async fn wait(&mut self, delay: Duration) { + events::Wakeups::wait(self, delay).await; + } +} + +#[cfg(test)] +struct SleepWait; + +#[cfg(test)] +impl PollWait for SleepWait { + async fn wait(&mut self, delay: Duration) { + tokio::time::sleep(delay).await; + } +} + +#[cfg(test)] +async fn poll_commands(run: F) +where + F: FnMut() -> Fut, + Fut: std::future::Future>, +{ + poll_commands_with_wait(run, SleepWait).await; +} + +async fn poll_commands_with_wait(mut run: F, mut wait: W) +where + F: FnMut() -> Fut, + Fut: std::future::Future>, + W: PollWait, +{ + loop { + let delay = match run().await { + // A completed command is evidence of activity. Drain existing work + // without another five-second gap, then return to normal idle load. + Ok(true) => continue, + Ok(false) => POLL_INTERVAL, + Err(error) => { + crate::logging::warn(&format!("mobile runner: {error:#}")); + Duration::from_secs(20) + } + }; + let mut timing = StageTimer::new("pollwait"); + wait.wait(delay).await; + timing.outcome = "ok"; + } +} + /// Background loop for the daemon on a Jcode Cloud machine. pub fn spawn_cloud_runner() { if !is_cloud_machine() { @@ -419,13 +519,7 @@ pub fn spawn_cloud_runner() { ensure_sync_enabled(); tokio::spawn(async move { let client = reqwest::Client::new(); - loop { - tokio::time::sleep(POLL_INTERVAL).await; - if let Err(err) = run_one(&client).await { - crate::logging::warn(&format!("mobile runner: {err:#}")); - tokio::time::sleep(Duration::from_secs(20)).await; - } - } + poll_commands_with_wait(|| run_one(&client), events::spawn()).await; }); } @@ -433,6 +527,67 @@ pub fn spawn_cloud_runner() { mod tests { use super::*; + #[test] + fn queue_wait_uses_server_timestamps_and_accepts_legacy_commands() { + let legacy = serde_json::json!({ + "command_id": "fixture", "kind": "create", "text": "test" + }); + let command: MobileCommand = serde_json::from_value(legacy.clone()).unwrap(); + assert_eq!(command.queue_wait_ms(), None); + for (created, claimed, expected) in [ + ( + "2026-11-08T09:00:00Z", + "2026-11-08T09:00:04.750Z", + Some(4750), + ), + ("2026-11-08T01:00:00-08:00", "2026-11-08T09:00:00Z", Some(0)), + ("2026-11-08T09:00:00.0005Z", "2026-11-08T09:00:00Z", None), + ("invalid", "2026-11-08T09:00:00Z", None), + ("2026-11-08T09:00:00Z", "invalid", None), + ] { + let mut wire = legacy.clone(); + wire["created_at"] = created.into(); + wire["claimed_at"] = claimed.into(); + let command: MobileCommand = serde_json::from_value(wire).unwrap(); + assert_eq!(command.queue_wait_ms(), expected); + } + } + + #[test] + fn stage_timing_uses_content_free_shared_report_fields() { + for stage in ["claim", "transfer", "turn", "sync", "complete", "pollwait"] { + let mut timing = StageTimer::new(stage); + assert_eq!( + timing.record(17), + format!( + "[CLOUD_TIMING] operation=mobile_runner stage={stage} outcome=error_or_cancelled duration_ms=17" + ) + ); + for outcome in ["ok", "error"] { + timing.outcome = outcome; + let record = timing.record(23); + assert_eq!( + record, + format!( + "[CLOUD_TIMING] operation=mobile_runner stage={stage} outcome={outcome} duration_ms=23" + ) + ); + // These are exactly the fixed-label fields consumed by cloud_latency_report. + assert_eq!(record.split_whitespace().count(), 5); + assert!(!record.contains("elapsed_ms=")); + } + } + } + + #[tokio::test] + async fn timed_stage_preserves_success_and_error_results() { + assert_eq!(timed_stage("claim", async { Ok(7) }).await.unwrap(), 7); + let error = timed_stage::<()>("claim", async { anyhow::bail!("synthetic failure") }) + .await + .unwrap_err(); + assert_eq!(error.to_string(), "synthetic failure"); + } + async fn transcript_server( pages: Vec, ) -> (String, tokio::task::JoinHandle>) { @@ -473,6 +628,28 @@ mod tests { serde_json::json!({"session": {"session_id":"local-1", "source":"local"}, "messages":messages, "more":more}) } + #[tokio::test] + async fn claim_preserves_server_queue_age_without_logging_command_content() { + let (base, server) = transcript_server(vec![serde_json::json!({ + "command": {"command_id":"fixture", "kind":"create", "text":"private fixture", + "created_at":"2026-11-08T09:00:00Z", "claimed_at":"2026-11-08T09:00:04.750Z"} + })]) + .await; + let command = claim(&reqwest::Client::new(), &base, "fixture-key") + .await + .unwrap() + .unwrap(); + assert_eq!(command.queue_wait_ms(), Some(4750)); + assert_eq!(command.text, "private fixture"); + let requests = server.await.unwrap(); + assert!(requests[0].starts_with("POST /mobile/commands/claim ")); + assert!( + requests[0] + .to_ascii_lowercase() + .contains("authorization: bearer fixture-key\r\n") + ); + } + #[tokio::test] async fn fetches_all_pages_with_account_auth_and_sparse_sequences() { let (base, server) = transcript_server(vec![ diff --git a/crates/jcode-base/src/mobile_runner_events.rs b/crates/jcode-base/src/mobile_runner_events.rs new file mode 100644 index 00000000000..90194b4060a --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_events.rs @@ -0,0 +1,176 @@ +//! Optional command wakeup hints. HTTP claims remain the only queue authority. +//! A disconnected, disabled or old API never disables the five-second fallback. + +use futures::{SinkExt, StreamExt}; +use std::sync::Arc; +use std::time::Duration; +use tokio::sync::watch; +use tokio::task::JoinHandle; +use tokio_tungstenite::tungstenite::{ + Message, client::IntoClientRequest, protocol::WebSocketConfig, +}; + +#[cfg(test)] +#[path = "mobile_runner_events_tests.rs"] +mod tests; + +type Source = Arc Option<(String, String)> + Send + Sync>; + +pub(super) struct Wakeups { + receiver: watch::Receiver, + task: JoinHandle<()>, +} + +impl Drop for Wakeups { + fn drop(&mut self) { + self.task.abort(); + } +} + +impl Wakeups { + pub(super) async fn wait(&mut self, delay: Duration) { + // A notification must not bypass the runner's error backoff. + if delay >= Duration::from_secs(20) { + tokio::time::sleep(delay).await; + return; + } + let deadline = tokio::time::Instant::now() + delay; + tokio::select! { + changed = self.receiver.changed() => { + if changed.is_err() { + tokio::time::sleep_until(deadline).await; + } + } + _ = tokio::time::sleep_until(deadline) => {} + } + } +} + +pub(super) fn spawn() -> Wakeups { + spawn_with( + Arc::new(|| { + let key = crate::subscription_catalog::configured_api_key()?; + Some((crate::subscription_api::configured_api_base(), key)) + }), + Duration::from_secs(30), + ) +} + +fn event_request( + base: &str, + key: &str, +) -> Result, ()> { + let mut url = reqwest::Url::parse(base).map_err(|_| ())?; + if !url.username().is_empty() + || url.password().is_some() + || url.query().is_some() + || url.fragment().is_some() + { + return Err(()); + } + let scheme = match url.scheme() { + "https" => "wss", + "http" => "ws", + _ => return Err(()), + }; + url.set_scheme(scheme).map_err(|_| ())?; + url.set_path(&format!( + "{}/mobile/commands/events", + url.path().trim_end_matches('/') + )); + let mut request = url.as_str().into_client_request().map_err(|_| ())?; + let value = format!("Bearer {key}").parse().map_err(|_| ())?; + request.headers_mut().insert("Authorization", value); + Ok(request) +} + +fn available(message: &str) -> bool { + #[derive(serde::Deserialize)] + #[serde(deny_unknown_fields)] + struct Hint { + #[serde(rename = "type")] + kind: String, + } + matches!(serde_json::from_str::(message), Ok(hint) if hint.kind == "commands_available") +} + +fn spawn_with(source: Source, retry: Duration) -> Wakeups { + let (sender, receiver) = watch::channel(0u64); + let task = tokio::spawn(async move { + loop { + let scope = source(); + let Some((base, key)) = scope.as_ref() else { + tokio::time::sleep(Duration::from_secs(5)).await; + continue; + }; + let mut cooldown = retry; + if let Ok(request) = event_request(base, key) { + let config = WebSocketConfig { + max_message_size: Some(4096), + max_frame_size: Some(4096), + max_write_buffer_size: 16384, + write_buffer_size: 0, + ..Default::default() + }; + let mut timing = super::StageTimer::new("event_connect"); + let connection = tokio::time::timeout( + Duration::from_secs(2), + tokio_tungstenite::connect_async_with_config(request, Some(config), true), + ) + .await; + match connection { + Ok(Ok((mut socket, _))) => { + timing.outcome = "ok"; + drop(timing); + let mut lifetime = super::StageTimer::new("event_connection"); + // Recheck the queue after subscribing, covering enqueue between + // the previous empty claim and WebSocket registration. + sender.send_modify(|version| *version = version.wrapping_add(1)); + let mut check = tokio::time::interval(Duration::from_secs(5)); + check.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + let mut heartbeat = tokio::time::Instant::now(); + loop { + tokio::select! { + message = socket.next() => match message { + Some(Ok(Message::Text(text))) if available(&text) => { + // A watch channel coalesces bursts, with constant memory. + sender.send_modify(|version| *version = version.wrapping_add(1)); + } + Some(Ok(Message::Ping(_) | Message::Pong(_))) => {} + _ => { lifetime.outcome = "error"; break; }, + }, + _ = check.tick() => { + if source() != scope { lifetime.outcome = "scope_changed"; break; } + if heartbeat.elapsed() >= Duration::from_secs(60) { + if !matches!(tokio::time::timeout(Duration::from_secs(1), socket.send(Message::Ping(Vec::new()))).await, Ok(Ok(()))) { + lifetime.outcome = "error"; + break; + } + heartbeat = tokio::time::Instant::now(); + } + } + } + } + } + Ok(Err(tokio_tungstenite::tungstenite::Error::Http(response))) + if matches!(response.status().as_u16(), 404 | 426 | 503) => + { + timing.outcome = "unavailable"; + cooldown = Duration::from_secs(300); + } + _ => timing.outcome = "error", + } + } + // Reconnects are bounded independently of normal HTTP queue claims. + // Credential or endpoint changes invalidate the cooldown within 5s. + let deadline = tokio::time::Instant::now() + cooldown; + while source() == scope && tokio::time::Instant::now() < deadline { + tokio::time::sleep_until( + (tokio::time::Instant::now() + Duration::from_secs(5)).min(deadline), + ) + .await; + } + } + }); + Wakeups { receiver, task } +} diff --git a/crates/jcode-base/src/mobile_runner_events_tests.rs b/crates/jcode-base/src/mobile_runner_events_tests.rs new file mode 100644 index 00000000000..6d775bf3c6f --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_events_tests.rs @@ -0,0 +1,334 @@ +use super::*; +use std::sync::atomic::{AtomicUsize, Ordering}; +use tokio::net::{TcpListener, TcpStream}; +use tokio_tungstenite::{WebSocketStream, accept_hdr_async}; + +const HINT: &str = r#"{"type":"commands_available"}"#; +const FALLBACK: Duration = Duration::from_secs(5); + +async fn fixture() -> (Wakeups, WebSocketStream) { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let mut wakeups = spawn_with( + Arc::new(move || Some((base.clone(), "fixture-only-key".into()))), + Duration::from_secs(30), + ); + let (stream, _) = tokio::time::timeout(Duration::from_secs(2), listener.accept()) + .await + .unwrap() + .unwrap(); + let socket = accept_hdr_async( + stream, + |request: &tokio_tungstenite::tungstenite::handshake::server::Request, response| { + assert_eq!( + request.headers()["authorization"], + "Bearer fixture-only-key" + ); + assert_eq!(request.uri().path(), "/mobile/commands/events"); + assert!(request.uri().query().is_none()); + Ok(response) + }, + ) + .await + .unwrap(); + // The subscription itself intentionally generates one queue recheck. + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + (wakeups, socket) +} + +#[tokio::test] +async fn changed_source_closes_old_socket_and_authenticates_only_new_endpoint() { + use std::sync::Mutex; + let old = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let new = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let source = Arc::new(Mutex::new(Some(( + format!("http://{}", old.local_addr().unwrap()), + "fixture-old-account".to_string(), + )))); + let current = source.clone(); + let mut wakeups = spawn_with( + Arc::new(move || current.lock().unwrap().clone()), + Duration::from_secs(30), + ); + let (stream, _) = tokio::time::timeout(Duration::from_secs(2), old.accept()) + .await + .unwrap() + .unwrap(); + let mut old_socket = accept_hdr_async( + stream, + |request: &tokio_tungstenite::tungstenite::handshake::server::Request, response| { + assert_eq!( + request.headers()["authorization"], + "Bearer fixture-old-account" + ); + Ok(response) + }, + ) + .await + .unwrap(); + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + *source.lock().unwrap() = Some(( + format!("http://{}", new.local_addr().unwrap()), + "fixture-new-account".into(), + )); + // Scope checks run every five seconds. The bound includes scheduling margin, + // not an assertion about connection latency or throughput. + let (stream, _) = tokio::time::timeout(Duration::from_secs(7), new.accept()) + .await + .expect("changed source must leave the old subscription and its cooldown") + .unwrap(); + let mut new_socket = accept_hdr_async( + stream, + |request: &tokio_tungstenite::tungstenite::handshake::server::Request, response| { + assert_eq!( + request.headers()["authorization"], + "Bearer fixture-new-account" + ); + assert!(request.uri().query().is_none()); + Ok(response) + }, + ) + .await + .unwrap(); + let end = tokio::time::timeout(Duration::from_secs(1), old_socket.next()) + .await + .expect("old account socket must be closed"); + assert!(!matches!(end, Some(Ok(Message::Text(_))))); + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + new_socket.send(Message::Text(HINT.into())).await.unwrap(); + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(80), old.accept()) + .await + .is_err() + ); +} + +#[test] +fn request_uses_bearer_header_without_credentials_in_url() { + let request = event_request("https://example.invalid/api/", "fixture-only-key").unwrap(); + assert_eq!( + request.uri().to_string(), + "wss://example.invalid/api/mobile/commands/events" + ); + assert_eq!( + request.headers()["Authorization"], + "Bearer fixture-only-key" + ); + assert!(!request.uri().to_string().contains("fixture-only-key")); + for base in [ + "https://user:password@example.invalid", + "https://example.invalid?key=secret", + "https://example.invalid#secret", + "ftp://example.invalid", + "not a URL", + ] { + assert!(event_request(base, "fixture-only-key").is_err(), "{base}"); + } + assert!(event_request("https://example.invalid", "bad\r\nInjected: value").is_err()); +} + +#[test] +fn hints_accept_only_the_exact_non_authoritative_shape() { + assert!(available(HINT)); + for text in [ + "", + "null", + "[]", + "{}", + r#"{"type":"other"}"#, + r#"{"type":"commands_available","command":"execute"}"#, + r#"{"type":"commands_available","type":"commands_available"}"#, + ] { + assert!(!available(text), "{text}"); + } +} + +#[tokio::test] +async fn notification_wakes_before_five_second_fallback() { + let (mut wakeups, mut socket) = fixture().await; + socket.send(Message::Text(HINT.into())).await.unwrap(); + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .expect("notification should wake within one second, not the five-second fallback"); +} + +#[tokio::test] +async fn notification_drives_a_real_poll_loop_claim_without_waiting_five_seconds() { + let (wakeups, mut socket) = fixture().await; + let queued = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let calls = Arc::new(AtomicUsize::new(0)); + let claimed = Arc::new(tokio::sync::Notify::new()); + let queue = queued.clone(); + let count = calls.clone(); + let signal = claimed.clone(); + let task = tokio::spawn(super::super::poll_commands_with_wait( + move || { + count.fetch_add(1, Ordering::SeqCst); + let ran = queue.swap(false, Ordering::SeqCst); + if ran { + signal.notify_one(); + } + async move { Ok(ran) } + }, + wakeups, + )); + tokio::time::timeout(Duration::from_secs(1), async { + while calls.load(Ordering::SeqCst) == 0 { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + let started = tokio::time::Instant::now(); + queued.store(true, Ordering::SeqCst); + socket.send(Message::Text(HINT.into())).await.unwrap(); + tokio::time::timeout(Duration::from_secs(1), claimed.notified()) + .await + .unwrap(); + println!( + "Controlled localhost WebSocket hint to polling-loop claim: {:?}, not production latency", + started.elapsed() + ); + task.abort(); + assert!(task.await.unwrap_err().is_cancelled()); +} + +#[tokio::test] +async fn notification_is_buffered_and_bursts_coalesce() { + let (mut wakeups, mut socket) = fixture().await; + let initial = *wakeups.receiver.borrow(); + for _ in 0..128 { + socket.send(Message::Text(HINT.into())).await.unwrap(); + } + tokio::time::timeout(Duration::from_secs(2), async { + while *wakeups.receiver.borrow() != initial + 128 { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + // All frames arrived before wait. A watch channel stores only the latest u64. + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(80), wakeups.wait(FALLBACK)) + .await + .is_err() + ); +} + +#[tokio::test] +async fn dropped_socket_keeps_timer_fallback() { + let (mut wakeups, socket) = fixture().await; + drop(socket); + assert!( + tokio::time::timeout( + Duration::from_millis(60), + wakeups.wait(Duration::from_millis(180)) + ) + .await + .is_err() + ); + tokio::time::timeout( + Duration::from_secs(1), + wakeups.wait(Duration::from_millis(100)), + ) + .await + .unwrap(); +} + +#[tokio::test] +async fn dropping_wakeups_cancels_connection_task_and_socket() { + let (wakeups, mut socket) = fixture().await; + let task = wakeups.task.abort_handle(); + drop(wakeups); + let result = tokio::time::timeout(Duration::from_secs(1), socket.next()) + .await + .expect("dropping wakeups must close its websocket"); + assert!(!matches!(result, Some(Ok(Message::Text(_))))); + tokio::time::timeout(Duration::from_secs(1), async { + while !task.is_finished() { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); +} + +#[tokio::test] +async fn malformed_and_oversize_frames_fail_closed() { + for frame in [ + Message::Text("not JSON".into()), + Message::Text("x".repeat(4097)), + Message::Binary(vec![0; 16]), + ] { + let (mut wakeups, mut socket) = fixture().await; + socket.send(frame).await.unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(100), wakeups.wait(FALLBACK)) + .await + .is_err() + ); + let end = tokio::time::timeout(Duration::from_secs(1), socket.next()) + .await + .expect("invalid frames must terminate the connection"); + assert!(!matches!(end, Some(Ok(Message::Text(_))))); + } +} + +#[tokio::test(start_paused = true)] +async fn error_backoff_cannot_be_bypassed_by_buffered_hint() { + let (sender, receiver) = watch::channel(0u64); + let task = tokio::spawn(std::future::pending()); + let mut wakeups = Wakeups { receiver, task }; + sender.send_modify(|v| *v += 1); + let start = tokio::time::Instant::now(); + wakeups.wait(Duration::from_secs(20)).await; + assert!(start.elapsed() >= Duration::from_secs(20)); + // Backoff did not consume the pending queue hint. + assert!(wakeups.receiver.has_changed().unwrap()); +} + +#[tokio::test] +async fn unavailable_endpoint_does_not_hot_loop_and_fallback_remains_live() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let attempts = Arc::new(AtomicUsize::new(0)); + let count = attempts.clone(); + let server = tokio::spawn(async move { + loop { + let (mut stream, _) = listener.accept().await.unwrap(); + count.fetch_add(1, Ordering::SeqCst); + let mut request = [0; 4096]; + stream.read(&mut request).await.unwrap(); + stream.write_all(b"HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").await.unwrap(); + } + }); + let mut wakeups = spawn_with( + Arc::new(move || Some((base.clone(), "fixture-only-key".into()))), + Duration::from_millis(1), + ); + tokio::time::timeout(Duration::from_secs(1), async { + while attempts.load(Ordering::SeqCst) == 0 { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + let start = tokio::time::Instant::now(); + wakeups.wait(Duration::from_millis(150)).await; + assert!(start.elapsed() >= Duration::from_millis(150)); + assert_eq!(attempts.load(Ordering::SeqCst), 1); + server.abort(); +} diff --git a/crates/jcode-base/src/mobile_runner_poll_tests.rs b/crates/jcode-base/src/mobile_runner_poll_tests.rs new file mode 100644 index 00000000000..8ccd369c6e8 --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_poll_tests.rs @@ -0,0 +1,197 @@ +//! Controlled scheduler tests. These use no live mobile API or network service. + +use super::poll_commands; +use std::sync::{ + Arc, Mutex, + atomic::{AtomicUsize, Ordering}, +}; +use std::time::Duration; +use tokio::task::JoinHandle; +use tokio::time::{Instant, advance}; + +// Keep the test future runnable so Tokio cannot auto-advance its paused clock. +async fn settle() { + for _ in 0..16 { + tokio::task::yield_now().await; + } +} + +async fn tick(seconds: u64) { + advance(Duration::from_secs(seconds)).await; + settle().await; +} + +async fn cancel(task: JoinHandle<()>) { + task.abort(); + assert!(task.await.unwrap_err().is_cancelled()); +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_starts_immediately() { + let started = Instant::now(); + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + observed.fetch_add(1, Ordering::SeqCst); + async { Ok(false) } + })); + + settle().await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + assert_eq!( + Instant::now(), + started, + "startup must not advance the clock" + ); + cancel(task).await; +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_drains_completed_work_without_delay() { + let started = Instant::now(); + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + let call = observed.fetch_add(1, Ordering::SeqCst); + async move { Ok(call < 4) } + })); + + settle().await; + assert_eq!( + calls.load(Ordering::SeqCst), + 5, + "four completed commands then one idle claim" + ); + assert_eq!( + Instant::now(), + started, + "queued commands must have no polling gap" + ); + tick(4).await; + assert_eq!(calls.load(Ordering::SeqCst), 5); + tick(1).await; + assert_eq!(calls.load(Ordering::SeqCst), 6); + cancel(task).await; +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_idle_claims_once_per_five_seconds() { + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + observed.fetch_add(1, Ordering::SeqCst); + async { Ok(false) } + })); + + settle().await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + for expected in 2..=5 { + tick(4).await; + assert_eq!(calls.load(Ordering::SeqCst), expected - 1); + tick(1).await; + assert_eq!(calls.load(Ordering::SeqCst), expected); + } + cancel(task).await; +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_errors_retry_after_twenty_not_twenty_five_seconds() { + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + observed.fetch_add(1, Ordering::SeqCst); + async { Err(anyhow::anyhow!("controlled claim failure")) } + })); + + settle().await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + tick(19).await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + tick(1).await; + assert_eq!( + calls.load(Ordering::SeqCst), + 2, + "retry must occur at t=20, not t=25" + ); + tick(5).await; + assert_eq!(calls.load(Ordering::SeqCst), 2); + tick(14).await; + assert_eq!(calls.load(Ordering::SeqCst), 2); + tick(1).await; + assert_eq!(calls.load(Ordering::SeqCst), 3); + cancel(task).await; +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_cancellation_stops_future_claims() { + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + observed.fetch_add(1, Ordering::SeqCst); + async { Ok(false) } + })); + + settle().await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + cancel(task).await; + for _ in 0..8 { + tick(5).await; + } + assert_eq!( + calls.load(Ordering::SeqCst), + 1, + "cancelled poller must never claim again" + ); +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_controlled_clock_baseline_comparison() { + let started = Instant::now(); + let baseline_times = Arc::new(Mutex::new(Vec::new())); + let recorded = baseline_times.clone(); + // Model the old sleep-before-every-claim scheduling, not a live API. + let baseline = tokio::spawn(async move { + for _ in 0..3 { + tokio::time::sleep(Duration::from_secs(5)).await; + recorded.lock().unwrap().push(started.elapsed()); + } + }); + let new_times = Arc::new(Mutex::new(Vec::new())); + let recorded = new_times.clone(); + let new = tokio::spawn(poll_commands(move || { + let mut times = recorded.lock().unwrap(); + let completed = times.len() < 3; + if completed { + times.push(started.elapsed()); + } + async move { Ok(completed) } + })); + + settle().await; + assert!(baseline_times.lock().unwrap().is_empty()); + assert_eq!(*new_times.lock().unwrap(), vec![Duration::ZERO; 3]); + for _ in 0..3 { + tick(5).await; + } + baseline.await.unwrap(); + let old = baseline_times.lock().unwrap().clone(); + let new_observed = new_times.lock().unwrap().clone(); + assert_eq!( + old, + vec![ + Duration::from_secs(5), + Duration::from_secs(10), + Duration::from_secs(15) + ] + ); + assert_eq!(new_observed, vec![Duration::ZERO; 3]); + assert!( + old.windows(2) + .all(|pair| pair[1] - pair[0] == Duration::from_secs(5)) + ); + assert!(new_observed.windows(2).all(|pair| pair[1] == pair[0])); + println!( + "Controlled paused-clock scheduling comparison, NOT a live API benchmark: baseline command times {old:?}, new {new_observed:?}. Removed 5s startup delay and 5s gap per queued command." + ); + cancel(new).await; +} diff --git a/crates/jcode-base/src/mobile_runner_process.rs b/crates/jcode-base/src/mobile_runner_process.rs new file mode 100644 index 00000000000..d10b58cc1ef --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_process.rs @@ -0,0 +1,111 @@ +//! Structured child supervision. Both pipes and process exit share one deadline. +use anyhow::{Context, Result}; +use std::{path::PathBuf, time::Duration}; +use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader}; + +const STDERR_TAIL_BYTES: usize = 600 * 4 + 4; + +struct BusyMarker(Option); +impl BusyMarker { + fn touch(&self) { + if let Some(path) = &self.0 + && let Err(error) = std::fs::write(path, b"") + { + crate::logging::warn(&format!("mobile runner busy marker update failed: {error}")); + } + } +} +impl Drop for BusyMarker { + fn drop(&mut self) { + if let Some(path) = &self.0 + && let Err(error) = std::fs::remove_file(path) + && error.kind() != std::io::ErrorKind::NotFound + { + crate::logging::warn(&format!( + "mobile runner busy marker removal failed: {error}" + )); + } + } +} + +async fn stderr_tail(mut stderr: tokio::process::ChildStderr) -> Result { + let mut tail = Vec::with_capacity(STDERR_TAIL_BYTES); + let mut chunk = [0; 8192]; + loop { + let n = stderr.read(&mut chunk).await?; + if n == 0 { + break; + } + tail.extend_from_slice(&chunk[..n]); + if tail.len() > STDERR_TAIL_BYTES { + tail.drain(..tail.len() - STDERR_TAIL_BYTES); + } + } + // Keep enough raw bytes for 600 four-byte scalars, then trim by characters. + // Lossy decoding also preserves the former behavior for non-UTF-8 stderr. + let text = String::from_utf8_lossy(&tail); + Ok(text + .chars() + .rev() + .take(600) + .collect::>() + .into_iter() + .rev() + .collect()) +} + +pub(super) async fn run( + mut command: tokio::process::Command, + session_id: Option<&str>, + marker: Option, + timeout: Duration, + touch_interval: Duration, +) -> Result { + let marker = BusyMarker(marker); + command.kill_on_drop(true); + let mut child = command.spawn().context("starting jcode run")?; + let stdout = child.stdout.take().context("no stdout")?; + let stderr = child.stderr.take().context("no stderr")?; + marker.touch(); + let mut touch = tokio::time::interval(touch_interval); + let result = { + let output = async { + let read_stdout = async { + let mut session = session_id.map(str::to_owned); + let mut lines = BufReader::new(stdout).lines(); + while let Some(line) = lines.next_line().await? { + if session.is_none() { + session = super::session_id_from_ndjson_line(&line); + } + } + Ok::<_, anyhow::Error>(session) + }; + let (session, tail, status) = + tokio::try_join!(read_stdout, stderr_tail(stderr), async { + child.wait().await.context("waiting for jcode run") + },)?; + anyhow::ensure!( + status.success(), + "jcode run failed ({}): {}", + status, + tail.trim() + ); + session.context("jcode run did not report a session id") + }; + let deadline = tokio::time::timeout(timeout, output); + tokio::pin!(deadline); + loop { + tokio::select! { + result = &mut deadline => break result.unwrap_or_else(|_| Err(anyhow::anyhow!("turn timed out"))), + _ = touch.tick() => marker.touch(), + } + } + }; + // No spawned reader tasks: cancellation drops both readers before reaping. + if result.is_err() + && let Err(error) = child.kill().await + { + crate::logging::warn(&format!("mobile runner child cleanup failed: {error}")); + } + result +} diff --git a/crates/jcode-base/src/mobile_runner_process_tests.rs b/crates/jcode-base/src/mobile_runner_process_tests.rs new file mode 100644 index 00000000000..20e5b95869f --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_process_tests.rs @@ -0,0 +1,241 @@ +//! Only launches this test executable as an isolated synthetic child, never Jcode turns. +use super::*; +use std::io::Write; +use tokio::io::{AsyncBufReadExt, BufReader}; + +#[cfg(unix)] +#[tokio::test] +async fn cancelling_supervision_removes_marker_and_leaves_no_child() { + let temp = tempfile::tempdir().unwrap(); + let marker = temp.path().join("busy"); + let pid_file = temp.path().join("fixture-pid"); + { + let mut command = fixture("hang"); + command.env("JCODE_MOBILE_FIXTURE_PID_FILE", &pid_file); + let future = process::run( + command, + None, + Some(marker.clone()), + Duration::from_secs(5), + Duration::from_millis(20), + ); + tokio::pin!(future); + let ready = async { + while std::fs::read_to_string(&pid_file) + .ok() + .and_then(|s| s.parse::().ok()) + .is_none() + { + tokio::time::sleep(Duration::from_millis(10)).await; + } + }; + tokio::select! { + result = &mut future => panic!("fixture unexpectedly finished: {result:?}"), + result = tokio::time::timeout(Duration::from_secs(2), ready) => result.unwrap(), + } + assert!(marker.exists()); + // Dropping supervision cancels both inline reader futures and kills the child. + } + assert!(!marker.exists()); + let pid: libc::pid_t = std::fs::read_to_string(pid_file).unwrap().parse().unwrap(); + tokio::time::timeout(Duration::from_secs(2), async { + loop { + // SAFETY: signal zero observes only the isolated fixture process. + if unsafe { libc::kill(pid, 0) } == -1 { + assert_eq!( + std::io::Error::last_os_error().raw_os_error(), + Some(libc::ESRCH) + ); + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + }) + .await + .expect("cancelled fixture must be killed and reaped"); +} + +#[test] +#[ignore = "subprocess fixture, invoked only by process regression tests"] +fn fake_child() { + let Ok(mode) = std::env::var("JCODE_MOBILE_PROCESS_FIXTURE") else { + return; + }; + if let Some(path) = std::env::var_os("JCODE_MOBILE_FIXTURE_PID_FILE") { + std::fs::write(path, std::process::id().to_string()).unwrap(); + } + if mode == "hang" || mode == "eof" { + if mode == "eof" { + // This fixture is its own process, so closing its pipe descriptors cannot + // affect the parent test harness. Keep running after both pipe EOFs. + #[cfg(unix)] + unsafe { + libc::close(1); + libc::close(2); + } + } + std::thread::sleep(Duration::from_secs(30)); + return; + } + let mut stderr = std::io::stderr().lock(); + stderr.write_all("🦀界".repeat(200_000).as_bytes()).unwrap(); + if mode == "error" { + stderr.write_all("界🦀".repeat(300).as_bytes()).unwrap(); + stderr.flush().unwrap(); + std::process::exit(7); + } + drop(stderr); + if mode != "missing" { + println!("not-json"); + println!("{{\"type\":\"start\",\"session_id\":\"synthetic-session\"}}"); + } +} + +fn fixture(mode: &str) -> tokio::process::Command { + let mut command = tokio::process::Command::new(std::env::current_exe().unwrap()); + command + .args([ + "--exact", + "mobile_runner::process_tests::fake_child", + "--ignored", + "--nocapture", + ]) + .env("JCODE_MOBILE_PROCESS_FIXTURE", mode) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .kill_on_drop(true); + command +} + +#[tokio::test] +async fn sequential_baseline_times_out_but_concurrent_drain_completes() { + let mut child = fixture("success").spawn().unwrap(); + let stdout = child.stdout.take().unwrap(); + let baseline = std::time::Instant::now(); + let result = tokio::time::timeout(Duration::from_millis(300), async { + let mut lines = BufReader::new(stdout).lines(); + while lines.next_line().await.unwrap().is_some() {} + }) + .await; + assert!( + result.is_err(), + "undrained stderr must block before stdout EOF" + ); + let baseline_elapsed = baseline.elapsed(); + child.kill().await.unwrap(); + let fixed = std::time::Instant::now(); + let session = process::run( + fixture("success"), + None, + None, + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await + .unwrap(); + assert_eq!(session, "synthetic-session"); + eprintln!( + "synthetic child: sequential baseline timeout={}ms, concurrent completion={}ms", + baseline_elapsed.as_millis(), + fixed.elapsed().as_millis() + ); +} + +#[tokio::test] +async fn unicode_error_is_exactly_last_600_characters() { + let error = process::run( + fixture("error"), + None, + None, + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await + .unwrap_err() + .to_string(); + let (_, tail) = error.split_once("): ").unwrap(); + assert_eq!(tail, "界🦀".repeat(300)); + assert_eq!(tail.chars().count(), 600); +} + +#[tokio::test] +async fn resumed_session_wins_and_missing_session_is_an_error() { + let result = process::run( + fixture("success"), + Some("resumed"), + None, + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await + .unwrap(); + assert_eq!(result, "resumed"); + let error = process::run( + fixture("missing"), + None, + None, + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await + .unwrap_err(); + assert!(error.to_string().contains("did not report a session id")); +} + +#[tokio::test] +async fn busy_marker_touched_and_removed_on_timeout_including_after_stdout_eof() { + for mode in ["hang", "eof"] { + if mode == "eof" && !cfg!(unix) { + continue; + } + let temp = tempfile::tempdir().unwrap(); + let marker = temp.path().join("busy"); + let future = process::run( + fixture(mode), + None, + Some(marker.clone()), + Duration::from_millis(250), + Duration::from_millis(20), + ); + tokio::pin!(future); + tokio::select! { + result = &mut future => panic!("finished before marker observation: {result:?}"), + _ = tokio::time::sleep(Duration::from_millis(80)) => {} + } + assert!(marker.exists()); + // Removing it verifies that a later heartbeat recreates it. + std::fs::remove_file(&marker).unwrap(); + tokio::select! { + result = &mut future => panic!("finished before heartbeat observation: {result:?}"), + _ = tokio::time::sleep(Duration::from_millis(60)) => {} + } + assert!(marker.exists()); + assert!( + future + .await + .unwrap_err() + .to_string() + .contains("turn timed out") + ); + assert!(!marker.exists()); + } +} + +#[tokio::test] +async fn busy_marker_removed_on_success_and_failure() { + for mode in ["success", "error", "missing"] { + let temp = tempfile::tempdir().unwrap(); + let marker = temp.path().join("busy"); + let result = process::run( + fixture(mode), + None, + Some(marker.clone()), + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await; + assert_eq!(result.is_ok(), mode == "success"); + assert!(!marker.exists()); + } +} diff --git a/crates/jcode-base/src/mobile_sync.rs b/crates/jcode-base/src/mobile_sync.rs index f9fb5aa2bf2..8e0975f1c5e 100644 --- a/crates/jcode-base/src/mobile_sync.rs +++ b/crates/jcode-base/src/mobile_sync.rs @@ -4,7 +4,9 @@ //! session transcripts to the Jcode account API so the iOS app can show them. //! Local sessions and sessions running on Jcode Cloud machines both upload with //! the account API key. Uploads are incremental: per-session high-water marks -//! (message count + snapshot mtime) live in `~/.jcode/mobile_sync.json`. +//! (message count + snapshot/journal revisions) live in `~/.jcode/mobile_sync.json`. +//! Successful session persistence wakes a debounced incremental pass. A 20s +//! reconciliation also catches saves made by other processes and retries. //! //! Only user-visible text is uploaded: user/assistant text, tool calls as a //! one-line summary, and tool results truncated. Hidden reasoning, images and @@ -16,9 +18,45 @@ use crate::subscription_api; use anyhow::{Context, Result}; use jcode_message_types::{ContentBlock, Role}; use serde::{Deserialize, Serialize}; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; +use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; -use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use std::sync::Mutex; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; + +// Only compile-time labels enter timing records. Never include session IDs, +// endpoints, credentials, payloads, or error strings here. Drop also covers +// early returns, propagated errors and cancellation without changing budgets. +struct SyncTiming { + stage: &'static str, + outcome: &'static str, + started: Instant, +} + +impl SyncTiming { + fn new(stage: &'static str) -> Self { + Self { + stage, + outcome: "incomplete", + started: Instant::now(), + } + } + + fn record(&self) -> String { + format!( + "mobile_sync stage={} outcome={} duration_ms={}", + self.stage, + self.outcome, + self.started.elapsed().as_millis() + ) + } +} + +impl Drop for SyncTiming { + fn drop(&mut self) { + crate::logging::info(&self.record()); + } +} pub const SYNC_INTERVAL: Duration = Duration::from_secs(20); const MAX_MESSAGES_PER_UPLOAD: usize = 200; @@ -26,12 +64,54 @@ const MAX_TOOL_RESULT_CHARS: usize = 4_000; const MAX_TEXT_CHARS: usize = 100_000; /// Only sessions touched within this window are synced on enable. const BACKFILL_WINDOW: Duration = Duration::from_secs(14 * 24 * 3600); -const MAX_SESSIONS_PER_PASS: usize = 200; +const SYNC_DEBOUNCE: Duration = Duration::from_millis(150); +// Also bounded when no background worker runs or mobile sync is disabled. +// Evicted hints are recovered by the periodic snapshot/journal reconciliation. +const MAX_DIRTY_SESSIONS: usize = 1024; +static PERSISTED: tokio::sync::Notify = tokio::sync::Notify::const_new(); +static DIRTY_SESSIONS: Mutex> = Mutex::new(BTreeSet::new()); +static SYNC_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); + +/// Called only after a durable session write, never after sync-state writes. +/// Notify retains a permit while an upload is running. The set also retains +/// same-millisecond writes which the reconciliation mtime cannot distinguish. +pub(crate) fn notify_session_persisted(id: &str) { + mark_dirty(id); + PERSISTED.notify_one(); +} + +fn mark_dirty(id: &str) { + if !valid_session_id(id) { + return; + } + let mut dirty = DIRTY_SESSIONS.lock().unwrap_or_else(|e| e.into_inner()); + if dirty.len() == MAX_DIRTY_SESSIONS && !dirty.contains(id) { + dirty.pop_first(); + } + dirty.insert(id.to_owned()); +} + +fn state_lock() -> Result { + let dir = storage::jcode_dir()?; + std::fs::create_dir_all(&dir)?; + let path = dir.join("mobile_sync.lock"); + let file = std::fs::OpenOptions::new() + .create(true) + .truncate(false) + .read(true) + .write(true) + .open(path)?; + file.lock()?; + Ok(file) +} #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] pub struct MobileSyncState { #[serde(default)] pub enabled: bool, + /// Fences uploads started before a disable/re-enable cycle. + #[serde(default)] + pub generation: u64, /// session id -> progress #[serde(default)] pub sessions: BTreeMap, @@ -43,9 +123,17 @@ pub struct SessionProgress { pub uploaded: usize, /// Snapshot+journal mtime (unix ms) at the last successful pass. pub mtime_ms: u64, + /// Snapshot and journal (nanosecond mtime, byte length). Older state files + /// omit this and receive one full reconciliation on upgrade. + #[serde(default)] + pub revision: Option<[u64; 4]>, + /// Hash of previously uploaded visible entries. Rewrites below the count + /// high-water mark must be replayed, not mistaken for metadata-only saves. + #[serde(default)] + pub fingerprint: Option, } -#[derive(Debug, Clone, Serialize, PartialEq)] +#[derive(Debug, Clone, Serialize, PartialEq, Hash)] pub struct WireMessage { pub seq: usize, pub role: &'static str, @@ -54,6 +142,71 @@ pub struct WireMessage { pub created_at: Option, } +/// Session metadata returned by the account's mobile transcript API. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct SyncedSession { + pub session_id: String, + pub title: Option, + pub source: String, + pub host: Option, + pub cwd: Option, + pub updated_at: Option, + pub created_at: Option, +} + +/// Bounded account inventory read for UI background workers. +pub async fn discover_cloud_sessions() -> Result> { + let client = reqwest::Client::new(); + tokio::time::timeout(Duration::from_secs(8), list_cloud_sessions(&client)) + .await + .context("mobile session discovery timed out")? +} + +/// List cloud sessions synced to the configured Jcode account. +pub async fn list_cloud_sessions(client: &reqwest::Client) -> Result> { + let api_key = crate::subscription_catalog::configured_api_key() + .context("no Jcode account credential is configured")?; + list_cloud_sessions_with_endpoint(client, &subscription_api::configured_api_base(), &api_key) + .await +} + +async fn list_cloud_sessions_with_endpoint( + client: &reqwest::Client, + api_base: &str, + api_key: &str, +) -> Result> { + let mut timing = SyncTiming::new("http_list"); + timing.outcome = "error"; + #[derive(Deserialize)] + struct SessionsResponse { + sessions: Vec, + } + + let response = client + .get(format!( + "{}/mobile/sessions", + api_base.trim_end_matches('/') + )) + .query(&[("limit", "500")]) + .bearer_auth(api_key) + .timeout(Duration::from_secs(30)) + .send() + .await + .context("mobile session listing failed")? + .error_for_status() + .context("mobile session listing returned an error")?; + let response: SessionsResponse = response + .json() + .await + .context("invalid mobile session listing response")?; + timing.outcome = "success"; + Ok(response + .sessions + .into_iter() + .filter(|session| session.source == "cloud" && valid_session_id(&session.session_id)) + .collect()) +} + #[derive(Debug, Clone, Serialize)] struct UploadBody<'a> { title: Option<&'a str>, @@ -88,13 +241,69 @@ pub fn load_state() -> MobileSyncState { } pub fn save_state(state: &MobileSyncState) -> Result<()> { - storage::write_json_secret(&state_path()?, state) + let _lock = state_lock()?; + // Opt-in/out is authoritative, but an enable based on a stale read must + // not discard progress committed by another daemon in the meantime. + let mut next = load_state(); + if next.enabled != state.enabled { + next.generation = next.generation.wrapping_add(1); + } + next.enabled = state.enabled; + if state.enabled { + if next.generation == state.generation { + merge_progress(&mut next, &state.sessions); + } + } else { + next.sessions.clear(); + } + storage::write_json_secret(&state_path()?, &next) +} + +fn merge_progress( + state: &mut MobileSyncState, + updates: &BTreeMap, +) -> bool { + let mut changed = false; + for (id, progress) in updates { + let version = |p: &SessionProgress| { + p.revision + .map(|r| r[0].max(r[2])) + .unwrap_or(p.mtime_ms.saturating_mul(1_000_000)) + }; + if state.sessions.get(id).is_none_or(|old| { + old != progress + && (version(old) < version(progress) + || (version(old) == version(progress) && old.uploaded <= progress.uploaded)) + }) { + state.sessions.insert(id.clone(), progress.clone()); + changed = true; + } + } + changed +} + +fn commit_progress(generation: u64, updates: &BTreeMap) -> Result<()> { + if updates.is_empty() { + return Ok(()); + } + let _lock = state_lock()?; + let mut current = load_state(); + if current.enabled && current.generation == generation && merge_progress(&mut current, updates) + { + storage::write_json_secret(&state_path()?, ¤t)?; + } + Ok(()) } pub fn is_enabled() -> bool { load_state().enabled } +fn sync_still_enabled(generation: u64) -> bool { + let state = load_state(); + state.enabled && state.generation == generation +} + /// `cloud` when running on a Jcode Cloud machine, `local` otherwise. pub fn session_source() -> &'static str { if std::env::var_os("JCODE_CLOUD_ENV_SESSION").is_some() @@ -124,15 +333,60 @@ fn host_name() -> Option { } fn truncate(text: &str, max: usize) -> String { - if text.chars().count() <= max { - return text.to_string(); + let mut out = BoundedText::new(max); + out.push(text); + out.text +} + +/// Assemble the exact prefix of joined blocks without visiting or copying their +/// discarded tails. Keep checking block kinds separately for role selection. +struct BoundedText { + text: String, + remaining: usize, + truncated: bool, +} + +impl BoundedText { + fn new(max: usize) -> Self { + Self { + text: String::new(), + remaining: max, + truncated: false, + } + } + + fn push(&mut self, text: &str) { + if self.truncated { + return; + } + // Byte length is an inexpensive upper bound on the scalar count. Most + // small blocks fit immediately, without a scalar-by-scalar walk. + if text.len() <= self.remaining { + self.remaining -= text.chars().count(); + self.text.push_str(text); + return; + } + // Count a bounded byte prefix with str's optimized scalar counter, + // then inspect only the few additional scalars needed for Unicode. + let mut prefix_end = self.remaining; + while !text.is_char_boundary(prefix_end) { + prefix_end -= 1; + } + let prefix_chars = text[..prefix_end].chars().count(); + let mut chars = text[prefix_end..].char_indices(); + if let Some((end, _)) = chars.nth(self.remaining - prefix_chars) { + self.text.push_str(&text[..prefix_end + end]); + self.text.push_str("\n… (truncated)"); + self.remaining = 0; + self.truncated = true; + } else { + self.remaining -= text.chars().count(); + self.text.push_str(text); + } } - let mut out: String = text.chars().take(max).collect(); - out.push_str("\n… (truncated)"); - out } -fn tool_summary(name: &str, input: &serde_json::Value) -> String { +fn append_tool_summary(out: &mut BoundedText, name: &str, input: &serde_json::Value) { let detail = input .get("intent") .or_else(|| input.get("command")) @@ -140,10 +394,13 @@ fn tool_summary(name: &str, input: &serde_json::Value) -> String { .or_else(|| input.get("query")) .and_then(|v| v.as_str()) .unwrap_or(""); - if detail.is_empty() { - format!("🔧 {name}") - } else { - format!("🔧 {name}: {}", truncate(detail, 200)) + out.push("🔧 "); + out.push(name); + if !detail.is_empty() { + out.push(": "); + if !out.truncated { + out.push(&truncate(detail, 200)); + } } } @@ -157,27 +414,44 @@ pub fn wire_messages(session: &Session, start: usize) -> Vec { .enumerate() .skip(start) .filter_map(|(seq, message)| { - let mut parts: Vec = Vec::new(); + let mut text = BoundedText::new(MAX_TEXT_CHARS); + let mut has_parts = false; let mut is_tool_result = false; for block in &message.content { - match block { + // Even after the text limit, later tool results change the role. + if matches!(block, ContentBlock::ToolResult { .. }) { + is_tool_result = true; + } + if text.truncated { + continue; + } + let part = match block { ContentBlock::Text { text, .. } - if !text.trim().is_empty() + if !text.trim_start().is_empty() && !text.trim_start().starts_with("") => { - parts.push(text.clone()) + std::borrow::Cow::Borrowed(text.as_str()) } ContentBlock::ToolUse { name, input, .. } => { - parts.push(tool_summary(name, input)) + if has_parts { + text.push("\n\n"); + } + append_tool_summary(&mut text, name, input); + has_parts = true; + continue; } ContentBlock::ToolResult { content, .. } => { - is_tool_result = true; - parts.push(truncate(content, MAX_TOOL_RESULT_CHARS)); + std::borrow::Cow::Owned(truncate(content, MAX_TOOL_RESULT_CHARS)) } - _ => {} + _ => continue, + }; + if has_parts { + text.push("\n\n"); } + text.push(&part); + has_parts = true; } - if parts.is_empty() { + if !has_parts { return None; } let role = if is_tool_result { @@ -193,13 +467,32 @@ pub fn wire_messages(session: &Session, start: usize) -> Vec { Some(WireMessage { seq, role, - text: truncate(&parts.join("\n\n"), MAX_TEXT_CHARS), + text: text.text, created_at: message.timestamp.map(|t| t.to_rfc3339()), }) }) .collect() } +fn wire_fingerprint(messages: &[WireMessage], end: usize) -> u64 { + let mut hash = std::collections::hash_map::DefaultHasher::new(); + for message in messages.iter().take_while(|message| message.seq < end) { + message.hash(&mut hash); + } + hash.finish() +} + +fn incremental_messages(session: &Session, progress: &SessionProgress) -> (Vec, u64) { + let mut messages = wire_messages(session, 0); + let fingerprint = wire_fingerprint(&messages, session.messages.len()); + let unchanged_prefix = progress.uploaded <= session.messages.len() + && progress.fingerprint == Some(wire_fingerprint(&messages, progress.uploaded)); + if unchanged_prefix { + messages.retain(|message| message.seq >= progress.uploaded); + } + (messages, fingerprint) +} + fn mtime_ms(path: &Path) -> u64 { match std::fs::metadata(path).and_then(|metadata| metadata.modified()) { Ok(modified) => match modified.duration_since(UNIX_EPOCH) { @@ -223,6 +516,26 @@ fn session_mtime_ms(snapshot: &Path) -> u64 { )) } +fn session_revision(snapshot: &Path) -> [u64; 4] { + fn stamp(path: &Path) -> (u64, u64) { + let Ok(meta) = std::fs::metadata(path) else { + return (0, 0); + }; + let modified = meta + .modified() + .ok() + .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) + .map(|d| d.as_nanos().min(u64::MAX as u128) as u64) + .unwrap_or(0); + (modified, meta.len()) + } + let (snapshot_time, snapshot_len) = stamp(snapshot); + let (journal_time, journal_len) = stamp(&crate::session::session_journal_path_from_snapshot( + snapshot, + )); + [snapshot_time, snapshot_len, journal_time, journal_len] +} + /// Candidate sessions: real (non-debug) snapshots modified since the window. fn candidate_sessions(since_ms: u64) -> Result> { let dir = storage::jcode_dir()?.join("sessions"); @@ -248,7 +561,6 @@ fn candidate_sessions(since_ms: u64) -> Result> { } } out.sort_by(|a, b| b.2.cmp(&a.2)); - out.truncate(MAX_SESSIONS_PER_PASS); Ok(out) } @@ -266,6 +578,8 @@ async fn upload( session_id: &str, body: &UploadBody<'_>, ) -> Result<()> { + let mut timing = SyncTiming::new("http_upload"); + timing.outcome = "error"; let url = format!( "{}/mobile/sessions/{}", api_base.trim_end_matches('/'), @@ -281,17 +595,52 @@ async fn upload( .context("mobile sync upload failed")?; let status = response.status(); if !status.is_success() { - let text = response.text().await.with_context(|| { - format!("mobile sync upload returned {status}, response body unreadable") - })?; - anyhow::bail!( - "mobile sync upload returned {status}: {}", - text.chars().take(300).collect::() - ); + timing.outcome = "http_error"; + // Response bodies can echo private content or credentials. + anyhow::bail!("mobile sync upload returned {status}"); + } + // Dropping an unread HTTP/1 body closes the pooled connection. HTTP/2 + // streams can be cancelled without sacrificing their shared connection. + if matches!( + response.version(), + reqwest::Version::HTTP_10 | reqwest::Version::HTTP_11 + ) { + drain_upload_response(response).await; } + timing.outcome = "success"; Ok(()) } +const UPLOAD_RESPONSE_DRAIN_LIMIT: usize = 64 * 1024; +const UPLOAD_RESPONSE_DRAIN_TIMEOUT: Duration = Duration::from_millis(100); + +async fn drain_upload_response(mut response: reqwest::Response) -> &'static str { + let mut timing = SyncTiming::new("http_upload_body"); + // The server has already accepted the upload. A broken, oversized or slow + // acknowledgment must not turn success into a retry of accepted messages. + timing.outcome = match tokio::time::timeout(UPLOAD_RESPONSE_DRAIN_TIMEOUT, async { + let mut bytes = 0usize; + loop { + match response.chunk().await { + Ok(Some(chunk)) => { + bytes = bytes.saturating_add(chunk.len()); + if bytes > UPLOAD_RESPONSE_DRAIN_LIMIT { + return "size_limit"; + } + } + Ok(None) => return "success", + Err(_) => return "read_error", + } + } + }) + .await + { + Ok(outcome) => outcome, + Err(_) => "timeout", + }; + timing.outcome +} + fn valid_session_id(id: &str) -> bool { !id.is_empty() && id.len() <= 128 @@ -302,8 +651,23 @@ fn valid_session_id(id: &str) -> bool { /// One incremental sync pass. Safe to call repeatedly. pub async fn sync_once(client: &reqwest::Client) -> Result { - let mut state = load_state(); + sync_pass(client, true).await +} + +async fn sync_pass(client: &reqwest::Client, reconcile: bool) -> Result { + let mut queue = SyncTiming::new("queue_wait"); + let _pass = SYNC_PASS.lock().await; + queue.outcome = "acquired"; + drop(queue); + let mut timing = SyncTiming::new(if reconcile { + "pass_reconcile" + } else { + "pass_incremental" + }); + timing.outcome = "error"; + let state = load_state(); if !state.enabled { + timing.outcome = "skipped_disabled"; return Ok(SyncReport::default()); } let api_key = crate::subscription_catalog::configured_api_key() @@ -317,8 +681,27 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { let host = host_name(); let source = session_source(); let mut report = SyncReport::default(); + let mut candidates = if reconcile { + candidate_sessions(since)? + } else { + Vec::new() + }; + let dirty = std::mem::take(&mut *DIRTY_SESSIONS.lock().unwrap_or_else(|e| e.into_inner())); + for id in &dirty { + if valid_session_id(id) && !candidates.iter().any(|(candidate, _, _)| candidate == id) { + let path = storage::jcode_dir()? + .join("sessions") + .join(format!("{id}.json")); + candidates.push((id.clone(), path.clone(), session_mtime_ms(&path))); + } + } + candidates.sort_by_key(|(id, _, _)| !dirty.contains(id)); - for (id, path, mtime) in candidate_sessions(since)? { + let candidate_count = candidates.len(); + let mut unchanged_count = 0; + let mut updates = BTreeMap::new(); + let mut candidates = candidates.into_iter(); + while let Some((id, path, mtime)) = candidates.next() { if !valid_session_id(&id) { continue; } @@ -328,25 +711,41 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { .get(&id) .cloned() .unwrap_or_else(SessionProgress::default); - if progress.mtime_ms >= mtime { + let revision = session_revision(&path); + if progress.revision == Some(revision) && !dirty.contains(&id) { + unchanged_count += 1; continue; } + // Unchanged candidates cannot upload or mutate progress. Do not reload + // the entire state for each one. Keep uncached generation fences for + // changed sessions and, crucially, immediately before every request. + let mut upload_timing = SyncTiming::new("upload"); + if !sync_still_enabled(state.generation) { + upload_timing.outcome = "skipped_disabled"; + break; + } let session = match Session::load_from_path(&path) { Ok(s) => s, - Err(_) => continue, + Err(_) => { + upload_timing.outcome = "load_error"; + mark_dirty(&id); + continue; + } }; if session.is_debug || session.messages.is_empty() { - state.sessions.insert( + upload_timing.outcome = "skipped_empty_or_debug"; + updates.insert( id, SessionProgress { uploaded: progress.uploaded, mtime_ms: mtime, + revision: Some(revision), + fingerprint: progress.fingerprint, }, ); continue; } - let start = progress.uploaded.min(session.messages.len()); - let messages = wire_messages(&session, start); + let (messages, fingerprint) = incremental_messages(&session, &progress); let title = session.custom_title.as_deref().or(session.title.as_deref()); let mut ok = true; // Always send at least one request so metadata (title, updated_at) refreshes. @@ -356,6 +755,11 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { messages.chunks(MAX_MESSAGES_PER_UPLOAD).collect() }; for chunk in chunks { + if !sync_still_enabled(state.generation) { + upload_timing.outcome = "skipped_disabled"; + ok = false; + break; + } let body = UploadBody { title, source, @@ -366,8 +770,11 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { updated_at: session.updated_at.to_rfc3339(), messages: chunk, }; - if let Err(err) = upload(client, &api_base, &api_key, &id, &body).await { - crate::logging::debug(&format!("mobile sync {id}: {err:#}")); + if upload(client, &api_base, &api_key, &id, &body) + .await + .is_err() + { + upload_timing.outcome = "error"; report.errors += 1; ok = false; break; @@ -375,26 +782,63 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { report.messages_uploaded += chunk.len(); } if ok { + upload_timing.outcome = "success"; report.sessions_uploaded += 1; - state.sessions.insert( + updates.insert( id, SessionProgress { uploaded: session.messages.len(), mtime_ms: mtime, + revision: Some(revision), + fingerprint: Some(fingerprint), }, ); + } else { + mark_dirty(&id); + } + if reconcile + && !DIRTY_SESSIONS + .lock() + .unwrap_or_else(|e| e.into_inner()) + .is_empty() + { + // Finish this session, commit its progress, then let the event pass + // handle fresh saves. Unvisited backlog remains discoverable on the + // next periodic reconciliation. Preserve any consumed dirty hints. + for (id, _, _) in candidates { + if dirty.contains(&id) { + mark_dirty(&id); + } + } + break; } } - // Reload `enabled` so a concurrent `jcode mobile disable` wins. - state.enabled = load_state().enabled; - if state.enabled { - save_state(&state)?; - } + // Merge under a cross-process lock, never write a stale enabled flag or + // overwrite another process's unrelated session progress. + let mut commit = SyncTiming::new("progress_commit"); + commit.outcome = "error"; + commit_progress(state.generation, &updates)?; + commit.outcome = if updates.is_empty() { + "skipped_unchanged" + } else { + "success" + }; + crate::logging::info(&format!( + "mobile_sync candidates={candidate_count} unchanged={unchanged_count} progress_updates={}", + updates.len() + )); + timing.outcome = if report.errors == 0 { + "success" + } else { + "partial_failure" + }; Ok(report) } /// Delete everything this account has stored for mobile. pub async fn wipe_remote(client: &reqwest::Client) -> Result<()> { + let mut timing = SyncTiming::new("http_wipe"); + timing.outcome = "error"; let api_key = crate::subscription_catalog::configured_api_key() .context("no Jcode account credential is configured")?; let url = format!( @@ -408,104 +852,44 @@ pub async fn wipe_remote(client: &reqwest::Client) -> Result<()> { .send() .await?; if !response.status().is_success() { + timing.outcome = "http_error"; anyhow::bail!("server returned {}", response.status()); } + timing.outcome = "success"; Ok(()) } /// Background loop used by the jcode server daemon. pub fn spawn_background_sync() { - tokio::spawn(async move { - let client = reqwest::Client::new(); - let mut ticks = tokio::time::interval(SYNC_INTERVAL); - ticks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); - loop { - ticks.tick().await; - if !is_enabled() { - continue; - } - if let Err(err) = sync_once(&client).await { - crate::logging::debug(&format!("mobile sync pass failed: {err:#}")); - } - } - }); + tokio::spawn(background_sync()); } -#[cfg(test)] -mod tests { - use super::*; - use jcode_session_types::StoredMessage; - - fn msg(role: Role, content: Vec) -> StoredMessage { - StoredMessage { - id: "m".into(), - role, - content, - display_role: None, - timestamp: None, - tool_duration_ms: None, - token_usage: None, +async fn background_sync() { + let client = reqwest::Client::new(); + let mut ticks = tokio::time::interval(SYNC_INTERVAL); + ticks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + loop { + let reconcile = tokio::select! { + _ = ticks.tick() => true, + _ = PERSISTED.notified() => { + // A bounded coalescing window, not a trailing debounce: + // continuous token saves cannot postpone sync forever. + let mut debounce = SyncTiming::new("debounce_wait"); + tokio::time::sleep(SYNC_DEBOUNCE).await; + debounce.outcome = "success"; + false + } + }; + // sync_pass records failures and disabled skips without error text. + if sync_pass(&client, reconcile).await.is_err() { + crate::logging::debug("mobile sync pass failed (see stage timing)"); } } - - #[test] - fn wire_messages_keep_visible_text_and_skip_hidden() { - let mut session = Session::create_with_id("s1".into(), None, None); - session.messages = vec![ - msg( - Role::User, - vec![ContentBlock::Text { - text: "hi".into(), - cache_control: None, - }], - ), - msg( - Role::Assistant, - vec![ContentBlock::Reasoning { - text: "secret".into(), - }], - ), - msg( - Role::Assistant, - vec![ - ContentBlock::Text { - text: "running".into(), - cache_control: None, - }, - ContentBlock::ToolUse { - id: "t".into(), - name: "bash".into(), - input: serde_json::json!({"command": "ls"}), - thought_signature: None, - }, - ], - ), - msg( - Role::User, - vec![ContentBlock::ToolResult { - tool_use_id: "t".into(), - content: "x".repeat(MAX_TOOL_RESULT_CHARS + 10), - is_error: None, - }], - ), - ]; - let wire = wire_messages(&session, 0); - assert_eq!( - wire.iter().map(|m| m.seq).collect::>(), - vec![0, 2, 3] - ); - assert_eq!(wire[0].role, "user"); - assert_eq!(wire[1].text, "running\n\n🔧 bash: ls"); - assert_eq!(wire[2].role, "tool"); - assert!(wire[2].text.ends_with("(truncated)")); - assert!(!wire.iter().any(|m| m.text.contains("secret"))); - assert_eq!(wire_messages(&session, 3).len(), 1); - } - - #[test] - fn session_ids_are_validated() { - assert!(valid_session_id("session_abc-123.x")); - assert!(!valid_session_id("bad id")); - assert!(!valid_session_id("")); - } } + +#[cfg(test)] +#[path = "mobile_sync_http_tests.rs"] +mod http_tests; +#[cfg(test)] +#[path = "mobile_sync_tests.rs"] +mod tests; diff --git a/crates/jcode-base/src/mobile_sync_http_tests.rs b/crates/jcode-base/src/mobile_sync_http_tests.rs new file mode 100644 index 00000000000..474dcda1020 --- /dev/null +++ b/crates/jcode-base/src/mobile_sync_http_tests.rs @@ -0,0 +1,233 @@ +use super::*; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +struct KeepAliveFixture { + base: String, + connections: Arc, + task: tokio::task::JoinHandle<()>, +} +impl Drop for KeepAliveFixture { + fn drop(&mut self) { + self.task.abort(); + } +} + +async fn keep_alive_fixture() -> KeepAliveFixture { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let connections = Arc::new(AtomicUsize::new(0)); + let count = connections.clone(); + let task = tokio::spawn(async move { + let mut clients = tokio::task::JoinSet::new(); + loop { + tokio::select! { + accepted = listener.accept() => { + let (mut stream, _) = accepted.unwrap(); + stream.set_nodelay(true).unwrap(); + count.fetch_add(1, Ordering::SeqCst); + clients.spawn(async move { + // Controlled connection setup cost, not a WAN measurement. + tokio::time::sleep(Duration::from_millis(30)).await; + let mut buffered = Vec::new(); + loop { + let header_end = loop { + if let Some(end) = buffered.windows(4).position(|part| part == b"\r\n\r\n") { + break end + 4; + } + let mut chunk = [0; 1024]; + let Ok(n) = stream.read(&mut chunk).await else { return; }; + if n == 0 { return; } + buffered.extend_from_slice(&chunk[..n]); + assert!(buffered.len() <= 1024 * 1024); + }; + let headers = String::from_utf8_lossy(&buffered[..header_end]); + let length = headers.lines().find_map(|line| { + let (name, value) = line.split_once(':')?; + name.eq_ignore_ascii_case("content-length").then(|| value.trim().parse::().unwrap()) + }).unwrap_or(0); + while buffered.len() < header_end + length { + let mut chunk = [0; 1024]; + let Ok(n) = stream.read(&mut chunk).await else { return; }; + if n == 0 { return; } + buffered.extend_from_slice(&chunk[..n]); + } + buffered.drain(..header_end + length); + if stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 16\r\nConnection: keep-alive\r\n\r\n").await.is_err() { + return; + } + // Ensure success headers and body arrive separately. + tokio::time::sleep(Duration::from_millis(8)).await; + if stream.write_all(b"{\"success\":true}").await.is_err() { + return; + } + } + }); + } + Some(result) = clients.join_next(), if !clients.is_empty() => { result.unwrap(); } + } + } + }); + KeepAliveFixture { + base, + connections, + task, + } +} + +fn body() -> UploadBody<'static> { + UploadBody { + title: None, + source: "local", + host: None, + cwd: None, + model: None, + created_at: String::new(), + updated_at: String::new(), + messages: &[], + } +} + +#[tokio::test] +#[ignore = "controlled HTTP keep-alive benchmark with injected connection/body delays"] +async fn benchmark_mobile_upload_keep_alive() { + for _ in 0..3 { + let fixture = keep_alive_fixture().await; + let client = reqwest::Client::new(); + let started = Instant::now(); + for _ in 0..8 { + upload(&client, &fixture.base, "fixture", "test", &body()) + .await + .unwrap(); + } + eprintln!( + "upload keep-alive fixture: uploads=8 duration_ms={:.3} connections={}", + started.elapsed().as_secs_f64() * 1000.0, + fixture.connections.load(Ordering::SeqCst) + ); + } +} + +#[tokio::test] +async fn mobile_upload_reuses_connection_after_separately_arriving_success_body() { + let fixture = keep_alive_fixture().await; + let client = reqwest::Client::new(); + for _ in 0..4 { + upload(&client, &fixture.base, "fixture", "test", &body()) + .await + .unwrap(); + } + assert_eq!(fixture.connections.load(Ordering::SeqCst), 1); +} + +async fn body_fixture( + headers: &str, + bytes: Vec, + keep_open: bool, +) -> (String, tokio::task::JoinHandle<()>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let response = format!("HTTP/1.1 200 OK\r\n{headers}\r\n\r\n"); + let task = tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.unwrap(); + stream.set_nodelay(true).unwrap(); + let mut request = Vec::new(); + let header_end = loop { + if let Some(end) = request.windows(4).position(|part| part == b"\r\n\r\n") { + break end + 4; + } + let mut chunk = [0; 1024]; + let count = stream.read(&mut chunk).await.unwrap(); + assert_ne!(count, 0); + request.extend_from_slice(&chunk[..count]); + }; + let length = String::from_utf8_lossy(&request[..header_end]) + .lines() + .find_map(|line| { + let (name, value) = line.split_once(':')?; + name.eq_ignore_ascii_case("content-length") + .then(|| value.trim().parse::().unwrap()) + }) + .unwrap_or(0); + while request.len() < header_end + length { + let mut chunk = [0; 1024]; + let count = stream.read(&mut chunk).await.unwrap(); + assert_ne!(count, 0); + request.extend_from_slice(&chunk[..count]); + } + stream.write_all(response.as_bytes()).await.unwrap(); + stream.write_all(&bytes).await.unwrap(); + if keep_open { + // Much longer than the drain budget. The caller aborts this task. + tokio::time::sleep(Duration::from_secs(30)).await; + } + }); + (base, task) +} + +#[tokio::test] +async fn mobile_upload_body_drain_is_time_bounded() { + let (base, server) = body_fixture("Content-Length: 1", Vec::new(), true).await; + let response = reqwest::get(&base).await.unwrap(); + let outcome = tokio::time::timeout(Duration::from_secs(2), drain_upload_response(response)) + .await + .unwrap(); + assert_eq!(outcome, "timeout"); + server.abort(); +} + +#[tokio::test] +async fn mobile_upload_body_drain_bounds_unknown_chunked_body() { + let payload = vec![b'x'; UPLOAD_RESPONSE_DRAIN_LIMIT + 1]; + let mut chunked = format!("{:x}\r\n", payload.len()).into_bytes(); + chunked.extend(payload); + chunked.extend_from_slice(b"\r\n"); + // No terminating chunk: without the byte cap this would wait for timeout. + let (base, server) = body_fixture("Transfer-Encoding: chunked", chunked, true).await; + let response = reqwest::get(&base).await.unwrap(); + assert_eq!(drain_upload_response(response).await, "size_limit"); + server.abort(); +} + +#[tokio::test] +async fn mobile_upload_body_drain_preserves_exact_boundary_and_empty_success() { + for bytes in [Vec::new(), vec![b'x'; UPLOAD_RESPONSE_DRAIN_LIMIT]] { + let (base, server) = + body_fixture(&format!("Content-Length: {}", bytes.len()), bytes, false).await; + let response = reqwest::get(&base).await.unwrap(); + assert_eq!(drain_upload_response(response).await, "success"); + server.await.unwrap(); + } +} + +#[tokio::test] +async fn mobile_upload_body_drain_classifies_truncation_without_error_text() { + let (base, server) = body_fixture("Content-Length: 100", b"PRIVATE".to_vec(), false).await; + let response = reqwest::get(&base).await.unwrap(); + assert_eq!(drain_upload_response(response).await, "read_error"); + server.await.unwrap(); +} + +#[tokio::test] +async fn mobile_upload_accepted_status_is_not_retried_for_bad_or_stalled_body() { + for (headers, bytes, keep_open) in [ + ("Content-Length: 100", b"PRIVATE".to_vec(), false), + ("Content-Length: 100", Vec::new(), true), + ( + "Content-Length: 1000000", + vec![b'x'; UPLOAD_RESPONSE_DRAIN_LIMIT + 1], + true, + ), + ] { + let (base, server) = body_fixture(headers, bytes, keep_open).await; + tokio::time::timeout( + Duration::from_secs(2), + upload(&reqwest::Client::new(), &base, "fixture", "test", &body()), + ) + .await + .unwrap() + .unwrap(); + server.abort(); + } +} diff --git a/crates/jcode-base/src/mobile_sync_tests.rs b/crates/jcode-base/src/mobile_sync_tests.rs new file mode 100644 index 00000000000..48b63ab0325 --- /dev/null +++ b/crates/jcode-base/src/mobile_sync_tests.rs @@ -0,0 +1,1072 @@ +use super::*; +use jcode_session_types::StoredMessage; + +#[tokio::test] +#[ignore = "manual local filesystem reconciliation benchmark"] +async fn benchmark_unchanged_reconciliation() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[ + ("JCODE_HOME", home.path().as_os_str()), + ( + "JCODE_API_KEY", + std::ffi::OsStr::new("benchmark-no-network"), + ), + ("JCODE_API_BASE", std::ffi::OsStr::new("http://127.0.0.1:1")), + ]); + DIRTY_SESSIONS.lock().unwrap().clear(); + let dir = home.path().join("sessions"); + std::fs::create_dir_all(&dir).unwrap(); + let mut state = MobileSyncState { + enabled: true, + ..Default::default() + }; + for index in 0..1300 { + let id = format!("benchmark-{index:04}"); + let path = dir.join(format!("{id}.json")); + // An unchanged revision must never need to deserialize a transcript. + std::fs::write(&path, b"{}").unwrap(); + state.sessions.insert( + id, + SessionProgress { + uploaded: 100, + mtime_ms: session_mtime_ms(&path), + revision: Some(session_revision(&path)), + fingerprint: Some(42), + }, + ); + } + save_state(&state).unwrap(); + // Enabling increments the fence, so seed progress in the current generation. + state.generation = load_state().generation; + save_state(&state).unwrap(); + let client = reqwest::Client::new(); + let mut elapsed = Vec::new(); + for _ in 0..7 { + let started = Instant::now(); + assert_eq!(sync_once(&client).await.unwrap(), SyncReport::default()); + elapsed.push(started.elapsed().as_micros()); + } + elapsed.sort_unstable(); + eprintln!( + "unchanged reconciliation: candidates=1300 passes=7 median_us={} max_us={} samples_us={elapsed:?}", + elapsed[3], elapsed[6] + ); +} + +#[test] +fn sync_timing_records_only_static_fields_and_elapsed_milliseconds() { + let mut timing = SyncTiming::new("test_stage"); + timing.started = Instant::now() - Duration::from_millis(12); + for outcome in ["success", "error", "skipped_disabled", "incomplete"] { + timing.outcome = outcome; + let record = timing.record(); + let fields: Vec<_> = record.split_whitespace().collect(); + assert_eq!(fields.len(), 4); + assert_eq!(fields[0], "mobile_sync"); + assert_eq!(fields[1], "stage=test_stage"); + assert_eq!(fields[2], format!("outcome={outcome}")); + let elapsed: u128 = fields[3] + .strip_prefix("duration_ms=") + .unwrap() + .parse() + .unwrap(); + assert!(elapsed >= 12); + } +} + +#[tokio::test] +async fn upload_failure_does_not_retain_private_server_response() { + let secret = "private-transcript-and-auth-token"; + let (base, request) = mock_listing("500 Internal Server Error", secret).await; + let body = UploadBody { + title: Some(secret), + source: "local", + host: None, + cwd: None, + model: None, + created_at: String::new(), + updated_at: String::new(), + messages: &[], + }; + let error = upload(&reqwest::Client::new(), &base, secret, "test", &body) + .await + .unwrap_err(); + assert!(error.to_string().contains("500")); + assert!(!format!("{error:#}").contains(secret)); + request.await.unwrap(); +} + +struct TestEnv(Vec<(&'static str, Option)>); + +#[tokio::test] +async fn unchanged_pass_and_identical_commit_do_not_rewrite_state() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[ + ("JCODE_HOME", home.path().as_os_str()), + ("JCODE_API_KEY", std::ffi::OsStr::new("no-network")), + ("JCODE_API_BASE", std::ffi::OsStr::new("http://127.0.0.1:1")), + ]); + DIRTY_SESSIONS.lock().unwrap().clear(); + let path = home.path().join("sessions/unchanged.json"); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(&path, b"not a transcript").unwrap(); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let generation = load_state().generation; + let updates = BTreeMap::from([( + "unchanged".into(), + SessionProgress { + revision: Some(session_revision(&path)), + ..Default::default() + }, + )]); + commit_progress(generation, &updates).unwrap(); + let state = state_path().unwrap(); + std::fs::File::open(&state) + .unwrap() + .set_times(std::fs::FileTimes::new().set_modified(UNIX_EPOCH + Duration::from_secs(100))) + .unwrap(); + let before = session_revision(&state); + commit_progress(generation, &updates).unwrap(); + assert_eq!( + session_revision(&state), + before, + "identical merge must not write" + ); + std::fs::remove_file(home.path().join("mobile_sync.lock")).unwrap(); + for reconcile in [true, false] { + assert_eq!( + sync_pass(&reqwest::Client::new(), reconcile).await.unwrap(), + SyncReport::default() + ); + assert_eq!( + session_revision(&state), + before, + "no-op pass must not write" + ); + assert!( + !home.path().join("mobile_sync.lock").exists(), + "no-op pass must not acquire cross-process lock" + ); + } +} + +#[tokio::test] +async fn disable_reenable_during_upload_fences_remaining_chunks_and_progress() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let _env = TestEnv::set(&[ + ("JCODE_HOME", home.path().as_os_str()), + ("JCODE_API_KEY", std::ffi::OsStr::new("test-key")), + ("JCODE_API_BASE", std::ffi::OsStr::new(&base)), + ]); + DIRTY_SESSIONS.lock().unwrap().clear(); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let mut session = Session::create_with_id("generation-cycle".into(), None, None); + session.messages = (0..MAX_MESSAGES_PER_UPLOAD + 1) + .map(|index| { + let mut message = msg( + Role::User, + vec![ContentBlock::Text { + text: "visible".into(), + cache_control: None, + }], + ); + message.id = format!("message-{index}"); + message + }) + .collect(); + session.save().unwrap(); + let server = tokio::spawn(async move { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut bytes = Vec::new(); + let mut buffer = [0; 8192]; + loop { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + bytes.extend_from_slice(&buffer[..count]); + if let Some(start) = bytes.windows(4).position(|w| w == b"\r\n\r\n") { + let header = String::from_utf8_lossy(&bytes[..start]); + let length: usize = header + .lines() + .find_map(|line| { + line.to_ascii_lowercase() + .strip_prefix("content-length:") + .map(|n| n.trim().parse().unwrap()) + }) + .unwrap(); + if bytes.len() >= start + 4 + length { + break; + } + } + } + save_state(&MobileSyncState::default()).unwrap(); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + socket + .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}") + .await + .unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(250), listener.accept()) + .await + .is_err(), + "old generation must not send its second chunk" + ); + }); + let report = sync_once(&reqwest::Client::new()).await.unwrap(); + assert_eq!(report.messages_uploaded, MAX_MESSAGES_PER_UPLOAD); + assert_eq!(report.sessions_uploaded, 0); + server.await.unwrap(); + assert!(load_state().enabled); + assert!(load_state().sessions.is_empty()); + DIRTY_SESSIONS.lock().unwrap().clear(); +} + +impl TestEnv { + fn set(values: &[(&'static str, &std::ffi::OsStr)]) -> Self { + Self( + values + .iter() + .map(|(name, value)| { + let previous = std::env::var_os(name); + crate::env::set_var(name, value); + (*name, previous) + }) + .collect(), + ) + } +} + +impl Drop for TestEnv { + fn drop(&mut self) { + for (name, value) in &self.0 { + if let Some(value) = value { + crate::env::set_var(name, value); + } else { + crate::env::remove_var(name); + } + } + } +} + +#[test] +fn disabled_dirty_hints_are_bounded_without_reading_sync_state() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); + DIRTY_SESSIONS.lock().unwrap().clear(); + for index in 0..MAX_DIRTY_SESSIONS * 2 { + notify_session_persisted(&format!("disabled-{index}")); + } + notify_session_persisted("../invalid"); + let mut dirty = DIRTY_SESSIONS.lock().unwrap(); + assert_eq!(dirty.len(), MAX_DIRTY_SESSIONS); + assert!(dirty.contains(&format!("disabled-{}", MAX_DIRTY_SESSIONS * 2 - 1))); + assert!(!home.path().join("mobile_sync.json").exists()); + dirty.clear(); +} + +#[test] +fn state_commits_merge_concurrent_progress_and_never_reenable() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let generation = load_state().generation; + let progress = |uploaded, mtime_ms| SessionProgress { + uploaded, + mtime_ms, + ..Default::default() + }; + let stale = BTreeMap::from([("first".into(), progress(1, 1))]); + commit_progress( + generation, + &BTreeMap::from([("second".into(), progress(2, 2))]), + ) + .unwrap(); + commit_progress(generation, &stale).unwrap(); + assert_eq!(load_state().sessions.len(), 2); + commit_progress( + generation, + &BTreeMap::from([("first".into(), progress(3, 3))]), + ) + .unwrap(); + commit_progress(generation, &stale).unwrap(); + assert_eq!(load_state().sessions["first"].uploaded, 3); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(8)); + let writers: Vec<_> = (0..8) + .map(|index| { + let barrier = barrier.clone(); + std::thread::spawn(move || { + barrier.wait(); + commit_progress( + generation, + &BTreeMap::from([(format!("parallel-{index}"), progress(index, 10))]), + ) + .unwrap(); + }) + }) + .collect(); + for writer in writers { + writer.join().unwrap(); + } + assert_eq!(load_state().sessions.len(), 10); + save_state(&MobileSyncState::default()).unwrap(); + commit_progress(generation, &stale).unwrap(); + assert!(!load_state().enabled); + assert!(load_state().sessions.is_empty()); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + commit_progress(generation, &stale).unwrap(); + assert!( + load_state().sessions.is_empty(), + "old opt-in generation cannot restore wiped progress" + ); +} + +#[tokio::test] +async fn persistence_notifications_upload_under_one_second_and_retain_inflight_writes() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let _env = TestEnv::set(&[ + ("JCODE_HOME", home.path().as_os_str()), + ("JCODE_API_KEY", std::ffi::OsStr::new("test-key")), + ("JCODE_API_BASE", std::ffi::OsStr::new(&base)), + ]); + let (requests_tx, mut requests) = tokio::sync::mpsc::channel(8); + let server = tokio::spawn(async move { + loop { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut bytes = Vec::new(); + let mut buffer = [0; 4096]; + let body_start = loop { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + bytes.extend_from_slice(&buffer[..count]); + if let Some(index) = bytes.windows(4).position(|w| w == b"\r\n\r\n") { + break index + 4; + } + }; + let header = String::from_utf8_lossy(&bytes[..body_start]); + let length: usize = header + .lines() + .find_map(|line| { + line.to_ascii_lowercase() + .strip_prefix("content-length:") + .map(|s| s.trim().parse().unwrap()) + }) + .unwrap(); + while bytes.len() < body_start + length { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + bytes.extend_from_slice(&buffer[..count]); + } + let body: serde_json::Value = + serde_json::from_slice(&bytes[body_start..body_start + length]).unwrap(); + let (release, wait) = tokio::sync::oneshot::channel::<()>(); + requests_tx.send((body, release)).await.unwrap(); + wait.await.unwrap(); + socket + .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}") + .await + .unwrap(); + } + }); + let background = tokio::spawn(background_sync()); + // Consume the initial reconciliation while disabled. + tokio::time::sleep(Duration::from_millis(30)).await; + let mut session = Session::create_with_id("event-sync".into(), None, None); + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: "first".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + assert!(!is_enabled()); + assert!( + tokio::time::timeout(Duration::from_millis(220), requests.recv()) + .await + .is_err() + ); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let started = std::time::Instant::now(); + session.add_message( + Role::Assistant, + vec![ContentBlock::Text { + text: "second".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + for title in ["coalesce one", "coalesce two", "coalesce final"] { + session.custom_title = Some(title.into()); + session.save().unwrap(); + } + let (first, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + eprintln!( + "mobile persistence-to-upload notification: {}ms", + started.elapsed().as_millis() + ); + assert!(started.elapsed() < Duration::from_secs(1)); + assert_eq!(first["messages"].as_array().unwrap().len(), 2); + assert_eq!(first["title"], "coalesce final"); + // Persist while the first upload is still blocked in the network. + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: "during-upload".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + release.send(()).unwrap(); + let (second, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(second["messages"].as_array().unwrap().len(), 1); + assert_eq!(second["messages"][0]["seq"], 2); + assert_eq!(second["messages"][0]["text"], "during-upload"); + release.send(()).unwrap(); + // Progress persistence itself must not schedule another upload. + assert!( + tokio::time::timeout(Duration::from_millis(350), requests.recv()) + .await + .is_err() + ); + assert_eq!(load_state().sessions["event-sync"].uploaded, 3); + // Simulate a writer in another process: no notification hook in this + // process, so only the reconciliation path can discover the update. + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: "external writer".into(), + cache_control: None, + }], + ); + let path = home.path().join("sessions/event-sync.json"); + storage::write_json_fast(&path, &session).unwrap(); + session = Session::load_from_path(&path).unwrap(); + let reconcile = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); + let (external, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(external["messages"].as_array().unwrap().len(), 1); + assert_eq!(external["messages"][0]["text"], "external writer"); + release.send(()).unwrap(); + assert_eq!(reconcile.await.unwrap().messages_uploaded, 1); + // Old snapshots have no local dirty hints, as with another process's + // backlog. Block the first old upload, then durably save a fresh session. + for id in ["old-a", "old-b", "old-c"] { + let mut old = Session::create_with_id(id.into(), None, None); + old.custom_title = Some(id.into()); + old.messages = vec![msg( + Role::User, + vec![ContentBlock::Text { + text: id.into(), + cache_control: None, + }], + )]; + storage::write_json_fast(&home.path().join(format!("sessions/{id}.json")), &old).unwrap(); + } + let reconcile = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); + let (old, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + let old_id = old["title"].as_str().unwrap().to_owned(); + assert!(old_id.starts_with("old-")); + let mut fresh = Session::create_with_id("fresh-save".into(), None, None); + fresh.custom_title = Some("fresh-save".into()); + fresh.messages = vec![msg( + Role::User, + vec![ContentBlock::Text { + text: "fresh".into(), + cache_control: None, + }], + )]; + fresh.save().unwrap(); + release.send(()).unwrap(); + let (next, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!( + next["title"], "fresh-save", + "fresh save must bypass old backlog" + ); + assert_eq!(reconcile.await.unwrap().sessions_uploaded, 1); + assert_eq!(load_state().sessions[&old_id].uploaded, 1); + release.send(()).unwrap(); + tokio::time::sleep(Duration::from_millis(220)).await; + // The next periodic-style pass resumes the unvisited backlog, without + // uploading the completed old session again. + let resume = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); + for _ in 0..2 { + let (remaining, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert!(remaining["title"].as_str().unwrap().starts_with("old-")); + assert_ne!(remaining["title"], old_id); + release.send(()).unwrap(); + } + assert_eq!(resume.await.unwrap().sessions_uploaded, 2); + session.custom_title = Some("metadata only".into()); + session.save().unwrap(); + let (metadata, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(metadata["title"], "metadata only"); + assert!(metadata["messages"].as_array().unwrap().is_empty()); + // A disable racing an upload must not be undone by its completion. + save_state(&MobileSyncState::default()).unwrap(); + release.send(()).unwrap(); + tokio::time::sleep(Duration::from_millis(220)).await; + assert!(!load_state().enabled); + assert!(load_state().sessions.is_empty()); + session.add_message( + Role::Assistant, + vec![ContentBlock::Text { + text: "disabled".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(250), requests.recv()) + .await + .is_err() + ); + background.abort(); + server.abort(); + let _ = background.await; + let _ = server.await; +} + +#[test] +fn reconciliation_does_not_starve_old_candidates_or_miss_same_millisecond_writes() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); + let sessions = home.path().join("sessions"); + std::fs::create_dir_all(&sessions).unwrap(); + for index in 0..205 { + std::fs::write(sessions.join(format!("session-{index}.json")), b"{}").unwrap(); + } + assert_eq!(candidate_sessions(0).unwrap().len(), 205); + let path = sessions.join("session-0.json"); + let file = std::fs::OpenOptions::new().write(true).open(&path).unwrap(); + let original = file.metadata().unwrap().modified().unwrap(); + let before = session_revision(&path); + let before_ms = session_mtime_ms(&path); + std::fs::write(&path, b"{\"changed\":true}").unwrap(); + file.set_times(std::fs::FileTimes::new().set_modified(original)) + .unwrap(); + assert_eq!(session_mtime_ms(&path), before_ms); + assert_ne!(session_revision(&path), before); + let journal = crate::session::session_journal_path_from_snapshot(&path); + std::fs::write(&journal, b"journal update\n").unwrap(); + let with_journal = session_revision(&path); + std::fs::remove_file(journal).unwrap(); + assert_ne!(session_revision(&path), with_journal); + assert_eq!(SYNC_INTERVAL, Duration::from_secs(20)); +} + +async fn mock_listing(status: &str, body: &str) -> (String, tokio::task::JoinHandle) { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}/", listener.local_addr().unwrap()); + let response = format!( + "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + let task = tokio::spawn(async move { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut request = Vec::new(); + let mut buffer = [0; 1024]; + while !request.windows(4).any(|window| window == b"\r\n\r\n") { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + request.extend_from_slice(&buffer[..count]); + } + socket.write_all(response.as_bytes()).await.unwrap(); + String::from_utf8(request).unwrap() + }); + (base, task) +} + +#[tokio::test] +async fn cloud_session_listing_filters_and_preserves_metadata() { + let body = serde_json::json!({"sessions": [ + {"session_id": "cloud-1", "source": "cloud", "title": "Title", + "host": "worker", "cwd": "/repo", "updated_at": "updated", "created_at": "created"}, + {"session_id": "cloud-2", "source": "cloud"}, + {"session_id": "local-1", "source": "local"}, + {"session_id": "../unsafe", "source": "cloud"}, + {"session_id": "", "source": "cloud"}, + {"session_id": "x".repeat(129), "source": "cloud"} + ]}) + .to_string(); + let (base, request) = mock_listing("200 OK", &body).await; + let sessions = list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "test-key") + .await + .unwrap(); + assert_eq!(sessions.len(), 2); + assert_eq!(sessions[0].session_id, "cloud-1"); + assert_eq!(sessions[0].title.as_deref(), Some("Title")); + assert_eq!(sessions[0].host.as_deref(), Some("worker")); + assert_eq!(sessions[0].cwd.as_deref(), Some("/repo")); + assert_eq!(sessions[0].updated_at.as_deref(), Some("updated")); + assert_eq!(sessions[0].created_at.as_deref(), Some("created")); + assert_eq!(sessions[1].title, None); + assert_eq!(sessions[1].created_at, None); + let request = request.await.unwrap().to_ascii_lowercase(); + assert!(request.starts_with("get /mobile/sessions?limit=500 http/1.1\r\n")); + assert!(request.contains("\r\nauthorization: bearer test-key\r\n")); +} + +#[tokio::test] +async fn cloud_session_listing_rejects_http_and_schema_errors() { + for (status, body) in [ + ("401 Unauthorized", "{\"sessions\":[]}"), + ("200 OK", "not json"), + ("200 OK", "{}"), + ("200 OK", "{\"sessions\":[{\"source\":\"cloud\"}]}"), + ("200 OK", "{\"sessions\":[{\"session_id\":\"s1\"}]}"), + ] { + let (base, request) = mock_listing(status, body).await; + assert!( + list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "key") + .await + .is_err() + ); + request.await.unwrap(); + } + let (base, request) = mock_listing("200 OK", "{\"sessions\":[]}").await; + assert!( + list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "key") + .await + .unwrap() + .is_empty() + ); + request.await.unwrap(); +} + +fn msg(role: Role, content: Vec) -> StoredMessage { + StoredMessage { + id: "m".into(), + role, + content, + display_role: None, + timestamp: None, + tool_duration_ms: None, + token_usage: None, + } +} + +#[test] +fn wire_messages_keep_visible_text_and_skip_hidden() { + let mut session = Session::create_with_id("s1".into(), None, None); + session.messages = vec![ + msg( + Role::User, + vec![ContentBlock::Text { + text: "hi".into(), + cache_control: None, + }], + ), + msg( + Role::Assistant, + vec![ContentBlock::Reasoning { + text: "secret".into(), + }], + ), + msg( + Role::Assistant, + vec![ + ContentBlock::Text { + text: "running".into(), + cache_control: None, + }, + ContentBlock::ToolUse { + id: "t".into(), + name: "bash".into(), + input: serde_json::json!({"command": "ls"}), + thought_signature: None, + }, + ], + ), + msg( + Role::User, + vec![ContentBlock::ToolResult { + tool_use_id: "t".into(), + content: "x".repeat(MAX_TOOL_RESULT_CHARS + 10), + is_error: None, + }], + ), + ]; + let wire = wire_messages(&session, 0); + assert_eq!( + wire.iter().map(|m| m.seq).collect::>(), + vec![0, 2, 3] + ); + assert_eq!(wire[0].role, "user"); + assert_eq!(wire[1].text, "running\n\n🔧 bash: ls"); + assert_eq!(wire[2].role, "tool"); + assert!(wire[2].text.ends_with("(truncated)")); + assert!(!wire.iter().any(|m| m.text.contains("secret"))); + assert_eq!(wire_messages(&session, 3).len(), 1); +} + +#[test] +fn session_ids_are_validated() { + assert!(valid_session_id("session_abc-123.x")); + assert!(!valid_session_id("bad id")); + assert!(!valid_session_id("")); +} + +// Deliberately retain the original full-copy conversion as an independent +// semantic oracle and a baseline for the ignored representative benchmark. +fn old_wire_messages(session: &Session, start: usize) -> Vec { + fn old_truncate(text: &str, max: usize) -> String { + if text.chars().count() <= max { + return text.to_string(); + } + let mut out: String = text.chars().take(max).collect(); + out.push_str("\n… (truncated)"); + out + } + session + .messages + .iter() + .enumerate() + .skip(start) + .filter_map(|(seq, message)| { + let mut parts = Vec::new(); + let mut is_tool_result = false; + for block in &message.content { + match block { + ContentBlock::Text { text, .. } + if !text.trim().is_empty() + && !text.trim_start().starts_with("") => + { + parts.push(text.clone()) + } + ContentBlock::ToolUse { name, input, .. } => { + let detail = input + .get("intent") + .or_else(|| input.get("command")) + .or_else(|| input.get("file_path")) + .or_else(|| input.get("query")) + .and_then(|v| v.as_str()) + .unwrap_or(""); + parts.push(if detail.is_empty() { + format!("🔧 {name}") + } else { + format!("🔧 {name}: {}", old_truncate(detail, 200)) + }); + } + ContentBlock::ToolResult { content, .. } => { + is_tool_result = true; + parts.push(old_truncate(content, MAX_TOOL_RESULT_CHARS)); + } + _ => {} + } + } + if parts.is_empty() { + return None; + } + let role = if is_tool_result { + "tool" + } else if message.display_role.is_some() { + "system" + } else { + match message.role { + Role::User => "user", + Role::Assistant => "assistant", + } + }; + Some(WireMessage { + seq, + role, + text: old_truncate(&parts.join("\n\n"), MAX_TEXT_CHARS), + created_at: message.timestamp.map(|t| t.to_rfc3339()), + }) + }) + .collect() +} + +fn text_block(text: String) -> ContentBlock { + ContentBlock::Text { + text, + cache_control: None, + } +} + +fn result_block(content: String) -> ContentBlock { + ContentBlock::ToolResult { + tool_use_id: "t".into(), + content, + is_error: None, + } +} + +#[test] +fn bounded_wire_conversion_matches_original_unicode_and_boundaries() { + let mut session = Session::create_with_id("bounded-wire".into(), None, None); + for unit in ["a", "é", "界", "😀", "e\u{301}"] { + for size in [ + 0, + 1, + 199, + 200, + 201, + MAX_TOOL_RESULT_CHARS - 1, + MAX_TOOL_RESULT_CHARS, + MAX_TOOL_RESULT_CHARS + 1, + MAX_TEXT_CHARS - 2, + MAX_TEXT_CHARS - 1, + MAX_TEXT_CHARS, + MAX_TEXT_CHARS + 1, + ] { + let value = unit.repeat(size); + session + .messages + .push(msg(Role::User, vec![text_block(value.clone())])); + session + .messages + .push(msg(Role::Assistant, vec![result_block(value.clone())])); + session.messages.push(msg( + Role::Assistant, + vec![ + text_block(value.clone()), + result_block(String::new()), + text_block("tail".into()), + result_block(value.clone()), + ContentBlock::ToolUse { + id: "t".into(), + name: "bash".into(), + input: serde_json::json!({"intent": value}), + thought_signature: None, + }, + ], + )); + } + } + session.messages.push(msg( + Role::User, + vec![ + text_block(" \n\u{2003}private".into()), + text_block(" \n\u{2003}".into()), + ContentBlock::Reasoning { + text: "private".into(), + }, + ], + )); + session + .messages + .push(msg(Role::User, vec![result_block(String::new())])); + session.messages.push(msg( + Role::User, + vec![result_block(String::new()), result_block(String::new())], + )); + for name in [String::new(), "😀".repeat(MAX_TEXT_CHARS + 1)] { + for input in [ + serde_json::json!({}), + serde_json::json!({"intent": ""}), + serde_json::json!({"intent": null, "command": "not-used"}), + serde_json::json!({"command": "界".repeat(201)}), + serde_json::json!({"file_path": "file"}), + serde_json::json!({"query": "query"}), + ] { + session.messages.push(msg( + Role::Assistant, + vec![ContentBlock::ToolUse { + id: "t".into(), + name: name.clone(), + input, + thought_signature: None, + }], + )); + } + } + session.messages[3].display_role = Some(crate::session::StoredDisplayRole::System); + session.messages[6].display_role = Some(crate::session::StoredDisplayRole::BackgroundTask); + session.messages[1].timestamp = Some(chrono::Utc::now()); + for start in [0, 1, 3, session.messages.len(), session.messages.len() + 1] { + let expected = old_wire_messages(&session, start); + let actual = wire_messages(&session, start); + assert_eq!(actual, expected, "start={start}"); + for end in [0, 1, session.messages.len() / 2, session.messages.len()] { + assert_eq!( + wire_fingerprint(&actual, end), + wire_fingerprint(&expected, end) + ); + } + } + for max in [0, 1, 2, 3, 200] { + for value in ["", "a", "é😀界", "e\u{301}", "x\n"] { + let expected = if value.chars().count() <= max { + value.to_owned() + } else { + format!( + "{}\n… (truncated)", + value.chars().take(max).collect::() + ) + }; + assert_eq!(truncate(value, max), expected); + } + } +} + +#[test] +fn bounded_text_matches_join_then_truncate() { + let parts = ["", "a", "é", "界😀", "e\u{301}", "\n", " "]; + for max in 0..12 { + for a in parts { + for b in parts { + for c in parts { + let joined = [a, b, c].join("\n\n"); + let expected = if joined.chars().count() <= max { + joined.clone() + } else { + format!( + "{}\n… (truncated)", + joined.chars().take(max).collect::() + ) + }; + let mut actual = BoundedText::new(max); + for (index, part) in [a, b, c].iter().enumerate() { + if index != 0 { + actual.push("\n\n"); + } + actual.push(part); + } + assert_eq!(actual.text, expected, "max={max}, parts={:?}", [a, b, c]); + } + } + } + } +} + +#[test] +#[ignore = "comparative benchmark, run with --ignored --nocapture"] +fn benchmark_bounded_wire_conversion() { + use std::hint::black_box; + use std::time::Instant; + for (label, size, blocks) in [ + ("typical", 1_000, 4), + ("long-ascii", 2_000_000, 4), + ("long-unicode", 1_000_000, 8), + ] { + let mut session = Session::create_with_id("wire-bench".into(), None, None); + let unit = if label == "long-unicode" { + "界😀é" + } else { + "a" + }; + for _ in 0..8 { + let mut content = (0..blocks) + .map(|_| text_block(unit.repeat(size))) + .collect::>(); + content.push(result_block("tool output".repeat(1_000))); + session.messages.push(msg(Role::Assistant, content)); + } + assert_eq!(wire_messages(&session, 0), old_wire_messages(&session, 0)); + let iterations = if label == "typical" { 100 } else { 5 }; + let old_start = Instant::now(); + for _ in 0..iterations { + black_box(old_wire_messages(black_box(&session), 0)); + } + let old = old_start.elapsed(); + let new_start = Instant::now(); + for _ in 0..iterations { + black_box(wire_messages(black_box(&session), 0)); + } + let new = new_start.elapsed(); + eprintln!( + "mobile wire {label}: iterations={iterations} old={old:?} bounded={new:?} ratio={:.2}x", + old.as_secs_f64() / new.as_secs_f64() + ); + } +} + +#[test] +fn incremental_updates_replay_rewritten_and_shrunk_prefixes() { + let mut session = Session::create_with_id("rewrite".into(), None, None); + for text in ["first", "second"] { + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: text.into(), + cache_control: None, + }], + ); + } + let (initial, fingerprint) = incremental_messages(&session, &SessionProgress::default()); + assert_eq!(initial.len(), 2); + let progress = SessionProgress { + uploaded: 2, + fingerprint: Some(fingerprint), + ..Default::default() + }; + assert!(incremental_messages(&session, &progress).0.is_empty()); + session.add_message( + Role::Assistant, + vec![ContentBlock::Text { + text: "appended".into(), + cache_control: None, + }], + ); + let appended = incremental_messages(&session, &progress).0; + assert_eq!(appended.len(), 1); + assert_eq!(appended[0].seq, 2); + session.messages[0].content = vec![ContentBlock::Text { + text: "rewritten".into(), + cache_control: None, + }]; + let rewritten = incremental_messages(&session, &progress).0; + assert_eq!(rewritten.len(), 3); + assert_eq!(rewritten[0].text, "rewritten"); + session.truncate_messages(1); + assert_eq!(incremental_messages(&session, &progress).0.len(), 1); +} diff --git a/crates/jcode-base/src/process_title.rs b/crates/jcode-base/src/process_title.rs index 30cfdc589b8..d971407b5d1 100644 --- a/crates/jcode-base/src/process_title.rs +++ b/crates/jcode-base/src/process_title.rs @@ -238,6 +238,7 @@ mod tests { id: "window-title".to_string(), group: Some("resume title sync".to_string()), lane_after: None, + lane_owner: None, confidence: Some(crate::todo::ConfidenceState::Plausible), completion_confidence: None, confidence_history: Vec::new(), diff --git a/crates/jcode-base/src/prompt/system_prompt.md b/crates/jcode-base/src/prompt/system_prompt.md index bcf58a1a365..b1ad74eecc3 100644 --- a/crates/jcode-base/src/prompt/system_prompt.md +++ b/crates/jcode-base/src/prompt/system_prompt.md @@ -5,8 +5,7 @@ Jcode is open source: ## Autonomy and persistence -Use todo tool extensively -Define todo plans as phases containing lanes containing items, even for a single phase or lane. Phases run sequentially, lanes within a phase may run in parallel, and items within a lane run in order. Use incremental item updates by name after defining the hierarchy. Give every task a globally unique, nonblank name and put longer context in description. Rename explicitly with rename_to. Use task names for remove, add.after, lane after, and update.lane_after references, not internal IDs. +Use todo tool extensively. Have autonomy. Persist to completing a task. Fix problems over surfacing them. Accomplish user intent over literals diff --git a/crates/jcode-base/src/provider/catalog_memo.rs b/crates/jcode-base/src/provider/catalog_memo.rs index 3678dda7e44..384791ef301 100644 --- a/crates/jcode-base/src/provider/catalog_memo.rs +++ b/crates/jcode-base/src/provider/catalog_memo.rs @@ -4,7 +4,8 @@ use super::*; /// time (short TTL), the auth generation at build time (bumped by /// `AuthStatus::invalidate_cache()` on login/logout/credential edits), and the /// catalog generation (bumped by prefetch/refresh completions). -#[derive(Clone)] +// Deep cloning is only retained for the benchmark's pre-Arc baseline. +#[cfg_attr(test, derive(Clone))] pub(super) struct RoutesMemoEntry { pub(super) built_at: std::time::Instant, pub(super) scope_key: String, @@ -27,7 +28,7 @@ pub(super) struct RoutesMemoEntry { /// build. Instance-specific inputs (active provider/model/profile) are folded /// into the memo key; anything not captured is bounded by the short TTL and /// the auth/catalog generations. -static GLOBAL_ROUTES_MEMO: LazyLock>> = +static GLOBAL_ROUTES_MEMO: LazyLock>>> = LazyLock::new(|| Mutex::new(HashMap::new())); /// Single-flight guard for catalog builds. During a client connect burst every @@ -175,14 +176,14 @@ impl MultiProvider { ) } - /// Return a fresh memoized catalog entry (routes + listable model names), + /// Return a fresh, immutable shared catalog entry (routes + model names), /// building it at most once per TTL window per catalog-relevant state. /// /// Freshness is keyed on a short TTL plus the auth and catalog /// generations. Lookup order: this instance's memo, the process-wide /// shared memo (so shared-server forks reuse one build), then a /// single-flight build that followers wait on instead of duplicating. - pub(super) fn fresh_routes_memo_entry(&self) -> RoutesMemoEntry { + pub(super) fn fresh_routes_memo_entry(&self) -> Arc { // Backstop only: invalidation is event-driven via auth/catalog // generations, so a completed refresh shows up on the next render. const ROUTES_MEMO_TTL: std::time::Duration = std::time::Duration::from_secs(60); @@ -211,7 +212,7 @@ impl MultiProvider { // Shared path: another instance with the same catalog-relevant state // (typically a fresh fork on the shared server) built one already. - let try_shared = || -> Option { + let try_shared = || -> Option> { let shared = match GLOBAL_ROUTES_MEMO.lock() { Ok(shared) => shared, Err(error) => { @@ -259,14 +260,14 @@ impl MultiProvider { { continue; } - let entry = RoutesMemoEntry { + let entry = Arc::new(RoutesMemoEntry { built_at: std::time::Instant::now(), scope_key: shared_key.clone(), auth_generation, catalog_generation: catalog_gen, listable_models: listable_model_names_from_routes(&routes), routes, - }; + }); if let Ok(mut memo) = self.routes_memo.lock() { *memo = Some(entry.clone()); } @@ -285,13 +286,13 @@ impl MultiProvider { // Continuous refresh churn must not starve clients or publish a stale // shared entry. Return a private best-effort catalog instead. let routes = catalog_routes::multiprovider_model_routes(self); - RoutesMemoEntry { + Arc::new(RoutesMemoEntry { built_at: std::time::Instant::now(), scope_key: self.routes_memo_key(), auth_generation: pricing::auth_pricing_generation(), catalog_generation: catalog_generation(), listable_models: listable_model_names_from_routes(&routes), routes, - } + }) } } diff --git a/crates/jcode-base/src/provider/mod.rs b/crates/jcode-base/src/provider/mod.rs index 3ccdadc623b..90d751684c5 100644 --- a/crates/jcode-base/src/provider/mod.rs +++ b/crates/jcode-base/src/provider/mod.rs @@ -438,7 +438,7 @@ pub struct MultiProvider { /// explicitly so pickers never see stale routes after a switch. /// Unique configuration lineage, preserved only by equivalent forks. catalog_scope: u64, - routes_memo: Mutex>, + routes_memo: Mutex>>, /// Number of model-catalog prefetches launched by the latest auth refresh. /// Shared by forks so the server can wait for real work rather than sleeping /// through a fixed quiet period after login. @@ -470,6 +470,138 @@ impl MultiProvider { account_failover::same_provider_account_candidates(provider) } + async fn try_auto_banked_reset( + &self, + provider: ActiveProvider, + reason: &str, + messages: &[Message], + tools: &[ToolDefinition], + mode: CompletionMode<'_>, + ) -> Result { + if !self.provider_uses_oauth_accounts(provider) { + return Ok(false); + } + let config = crate::config::Config::load().provider; + let enabled = match provider { + ActiveProvider::OpenAI => config.openai_auto_banked_reset, + ActiveProvider::Claude => config.anthropic_auto_fable_reset, + _ => false, + }; + if !enabled { + return Ok(false); + } + if let Some(prompt) = self.auto_reset_route_prompt(provider, reason, messages, tools, mode) + { + // Preserve the router's exact-route approval/countdown boundary. + // A reset must not spend a grant while a permitted route is ready. + let account = self + .selected_account_for_provider(provider) + .or_else(|| active_account_label_for_provider(provider)); + models::record_provider_unavailable_for_account_label( + Self::provider_key(provider), + account.as_deref(), + reason, + ); + return Err(anyhow::anyhow!(prompt.to_error_message())); + } + let account = self + .selected_account_for_provider(provider) + .or_else(|| active_account_label_for_provider(provider)); + let reset = match provider { + ActiveProvider::OpenAI => crate::usage::auto_reset_weekly_usage(account).await, + ActiveProvider::Claude => crate::usage::auto_reset_fable_usage(account).await, + _ => return Ok(false), + }; + Ok(match reset { + Ok(true) => { + self.startup_notices + .write() + .unwrap_or_else(|p| p.into_inner()) + .push(format!( + "{} usage is available again after earned-reset recovery. Continuing automatically.", + Self::provider_label(provider) + )); + true + } + Ok(false) => false, + Err(error) => { + crate::logging::warn(&format!("Automatic banked reset failed: {error:#}")); + false + } + }) + } + + /// Use the same capacity, cooldown, pool and context filters as normal failover. + fn auto_reset_route_prompt( + &self, + provider: ActiveProvider, + reason: &str, + messages: &[Message], + tools: &[ToolDefinition], + mode: CompletionMode<'_>, + ) -> Option { + if !crate::config::Config::load() + .provider + .auto_reset_only_when_no_routes + { + return None; + } + let (chars, tokens) = Self::estimate_request_input(messages, tools, mode); + self.cross_provider_pool_prompt(provider, provider, reason.to_owned(), chars, tokens) + } + + /// Retry only before the stream is accepted, never replay partial output. + async fn complete_with_auto_reset( + &self, + provider: ActiveProvider, + messages: &[Message], + tools: &[ToolDefinition], + mode: CompletionMode<'_>, + resume_session_id: Option<&str>, + ) -> Result { + let mut may_reset = true; + loop { + let attempt = match mode { + CompletionMode::Unified { system } => { + self.complete_on_provider(provider, messages, tools, system, resume_session_id) + .await + } + CompletionMode::Split { + system_static, + system_dynamic, + } => { + self.complete_split_on_provider( + provider, + messages, + tools, + system_static, + system_dynamic, + resume_session_id, + ) + .await + } + }; + let attempt = self.await_account_stream_start(provider, attempt).await; + if may_reset + && let Err(error) = &attempt + && Self::classify_failover_error(error).should_mark_provider_unavailable() + && self + .try_auto_banked_reset( + provider, + &Self::summarize_error(error), + messages, + tools, + mode, + ) + .await? + { + may_reset = false; + continue; + } + return attempt; + } + } + async fn complete_with_failover( &self, messages: &[Message], @@ -529,6 +661,9 @@ impl MultiProvider { } if let Some(reason) = models::provider_unavailability_detail_for_account_label(key, Some(&bound_label)) + && !self + .try_auto_banked_reset(active, &reason, messages, tools, mode) + .await? { self.try_same_provider_account_failover( active, @@ -546,27 +681,9 @@ impl MultiProvider { reason ); } - let attempt = match mode { - CompletionMode::Unified { system } => { - self.complete_on_provider(active, messages, tools, system, resume_session_id) - .await - } - CompletionMode::Split { - system_static, - system_dynamic, - } => { - self.complete_split_on_provider( - active, - messages, - tools, - system_static, - system_dynamic, - resume_session_id, - ) - .await - } - }; - let attempt = self.await_account_stream_start(active, attempt).await; + let attempt = self + .complete_with_auto_reset(active, messages, tools, mode, resume_session_id) + .await; match attempt { Ok(stream) => { match active { @@ -585,6 +702,9 @@ impl MultiProvider { return Ok(stream); } Err(err) => { + if parse_failover_prompt_message(&err.to_string()).is_some() { + return Err(err); + } let decision = Self::classify_failover_error(&err); if decision.should_failover() { let reason = Self::summarize_error(&err); @@ -660,6 +780,9 @@ impl MultiProvider { if let Some(reason) = cached_unavailability .clone() .or_else(|| self.provider_precheck_unavailable_reason(candidate)) + && !self + .try_auto_banked_reset(candidate, &reason, messages, tools, mode) + .await? { let note = format!("{}: {}", label, reason); if candidate == active { @@ -687,27 +810,9 @@ impl MultiProvider { continue; } - let attempt = match mode { - CompletionMode::Unified { system } => { - self.complete_on_provider(candidate, messages, tools, system, resume_session_id) - .await - } - CompletionMode::Split { - system_static, - system_dynamic, - } => { - self.complete_split_on_provider( - candidate, - messages, - tools, - system_static, - system_dynamic, - resume_session_id, - ) - .await - } - }; - let attempt = self.await_account_stream_start(candidate, attempt).await; + let attempt = self + .complete_with_auto_reset(candidate, messages, tools, mode, resume_session_id) + .await; match attempt { Ok(stream) => { @@ -734,6 +839,9 @@ impl MultiProvider { return Ok(stream); } Err(err) => { + if parse_failover_prompt_message(&err.to_string()).is_some() { + return Err(err); + } let summary = Self::summarize_error(&err); let display_summary = if self.provider_uses_oauth_accounts(candidate) { maybe_annotate_limit_summary(candidate, summary.clone()) @@ -881,7 +989,7 @@ impl MultiProvider { let active_profile_id = ProviderRegistry::new(self).active_compatible_profile_id(); let mut fallback: Option = None; - for route in self.fresh_routes_memo_entry().routes { + for route in &self.fresh_routes_memo_entry().routes { if !route.available || route.model != model { continue; } @@ -2342,7 +2450,7 @@ impl Provider for MultiProvider { } fn available_models_display(&self) -> Vec { - self.fresh_routes_memo_entry().listable_models + self.fresh_routes_memo_entry().listable_models.clone() } fn available_providers_for_model(&self, model: &str) -> Vec { @@ -2378,8 +2486,18 @@ impl Provider for MultiProvider { None } + fn model_catalog_snapshot(&self) -> jcode_provider_core::ModelCatalogSnapshot { + let entry = self.fresh_routes_memo_entry(); + jcode_provider_core::ModelCatalogSnapshot::new( + Some(self.display_name()), + Some(self.model()), + entry.listable_models.clone(), + entry.routes.clone(), + ) + } + fn model_routes(&self) -> Vec { - self.fresh_routes_memo_entry().routes + self.fresh_routes_memo_entry().routes.clone() } fn model_capabilities_need_refresh(&self) -> bool { diff --git a/crates/jcode-base/src/provider/stream_start.rs b/crates/jcode-base/src/provider/stream_start.rs index 885d5849b49..41d44ebe766 100644 --- a/crates/jcode-base/src/provider/stream_start.rs +++ b/crates/jcode-base/src/provider/stream_start.rs @@ -11,8 +11,15 @@ impl MultiProvider { // OAuth HTTP runtimes return their channel before sending the request. // An Ok(channel) is not an accepted request: otherwise a subsequent // HTTP 429 bypasses the account router and the client retries forever. + let config = crate::config::Config::load().provider; + let automatic_reset = match provider { + ActiveProvider::OpenAI => config.openai_auto_banked_reset, + ActiveProvider::Claude => config.anthropic_auto_fable_reset, + _ => false, + }; if (self.selected_account_for_provider(provider).is_none() - && active_account_label_for_provider(provider).is_none()) + && active_account_label_for_provider(provider).is_none() + && !automatic_reset) || !self.provider_uses_oauth_accounts(provider) { return attempt; diff --git a/crates/jcode-base/src/provider/tests/agent_failover.rs b/crates/jcode-base/src/provider/tests/agent_failover.rs index f14941b7405..92c695481e1 100644 --- a/crates/jcode-base/src/provider/tests/agent_failover.rs +++ b/crates/jcode-base/src/provider/tests/agent_failover.rs @@ -3,14 +3,14 @@ fn agent_failover_set_default_model(model: &str) -> anyhow::Result<()> { } fn agent_failover_catalog(provider: &MultiProvider, routes: Vec) { - *provider.routes_memo.lock().unwrap() = Some(RoutesMemoEntry { + *provider.routes_memo.lock().unwrap() = Some(Arc::new(RoutesMemoEntry { built_at: std::time::Instant::now(), scope_key: provider.routes_memo_key(), auth_generation: pricing::auth_pricing_generation(), catalog_generation: catalog_generation(), listable_models: listable_model_names_from_routes(&routes), routes, - }); + })); } fn agent_failover_fixture(models: &[&str]) -> MultiProvider { @@ -62,6 +62,164 @@ fn agent_failover_target(provider: &MultiProvider, tokens: usize, reason: &str) .and_then(|prompt| prompt.target_route) } +#[test] +fn automatic_reset_request_routes_first_for_live_and_cached_failures() { + for cached in [false, true] { + for split in [false, true] { + with_clean_provider_test_env(|| { + enter_test_runtime().block_on(async { + let mut config = crate::config::Config::load(); + config.provider.openai_auto_banked_reset = true; + config.save().unwrap(); + let (provider, attempts, _cleanup) = + account_routing_fixture(&[("openai-otter", "usage exhausted")]); + if cached { + models::record_provider_unavailable_for_account_label( + "openai", + Some("openai-otter"), + "usage exhausted", + ); + } + let result = if split { + provider + .complete_split(&[], &[], "static", "dynamic", None) + .await + } else { + provider.complete(&[], &[], "system", None).await + }; + let error = result + .err() + .expect("alternate route must precede reset spending"); + let prompt = parse_failover_prompt_message(&error.to_string()) + .expect("preserve exact structured prompt"); + assert_eq!( + prompt.target_route.as_deref(), + Some("openai-oauth@openai-fox:gpt-5.4") + ); + assert!(prompt.reason.starts_with("usage exhausted")); + assert!( + !prompt.reason.contains("[jcode-provider-failover]"), + "do not nest a routing prompt" + ); + assert_eq!(attempts.lock().unwrap().len(), usize::from(!cached)); + assert_eq!( + auth::codex::active_account_label().as_deref(), + Some("openai-otter") + ); + }); + }); + } + } +} + +#[test] +fn automatic_reset_defers_to_ready_pool_route_before_any_redemption() { + with_clean_provider_test_env(|| { + let rt = enter_test_runtime(); + let _guard = rt.enter(); + let mut config = crate::config::Config::load(); + config.provider.anthropic_auto_fable_reset = true; + config.save().unwrap(); + let provider = agent_failover_fixture(&["gpt-5.4"]); + let error = rt + .block_on(provider.try_auto_banked_reset( + ActiveProvider::Claude, + "usage exhausted", + &[], + &[], + CompletionMode::Unified { system: "" }, + )) + .err() + .expect("ready alternate must route, not redeem"); + assert!(error.to_string().contains("openai-api:gpt-5.4")); + assert!( + models::provider_unavailability_detail_for_account_label( + "claude", + Some("claude-otter"), + ) + .is_some(), + "failed source must not immediately route back" + ); + models::clear_claude_provider_unavailability_for_account_label(Some("claude-otter")); + }); +} + +#[test] +fn automatic_reset_route_guard_respects_pool_cooldown_and_override() { + with_clean_provider_test_env(|| { + let rt = enter_test_runtime(); + let _guard = rt.enter(); + let provider = agent_failover_fixture(&["gpt-5.4"]); + let guard = || { + provider.auto_reset_route_prompt( + ActiveProvider::Claude, + "usage exhausted", + &[], + &[], + CompletionMode::Unified { system: "" }, + ) + }; + assert!(guard().is_some()); + + record_provider_unavailable_for_account("openai-api", "usage exhausted"); + // Invalidate the fixture memo after changing account availability. + agent_failover_catalog( + &provider, + vec![build_openai_api_key_route("gpt-5.4", true, "")], + ); + assert!( + guard().is_none(), + "cooling-down routes must not block recovery" + ); + clear_provider_unavailable_for_account("openai-api"); + agent_failover_catalog( + &provider, + vec![build_openai_api_key_route("gpt-5.4", true, "")], + ); + + let mut pool = crate::auth::account_pool::AccountPool::load(); + pool.excluded.push("openai-api".into()); + pool.save().unwrap(); + assert!(guard().is_none(), "excluded routes must not block recovery"); + pool.excluded.clear(); + pool.save().unwrap(); + assert!(guard().is_some()); + + let mut config = crate::config::Config::load(); + config.provider.auto_reset_only_when_no_routes = false; + config.save().unwrap(); + agent_failover_catalog( + &provider, + vec![build_openai_api_key_route("gpt-5.4", true, "")], + ); + assert!(guard().is_none(), "explicit reset-first override wins"); + }); +} + +#[test] +fn automatic_reset_route_guard_preserves_same_provider_account_rotation() { + with_clean_provider_test_env(|| { + let rt = enter_test_runtime(); + let _guard = rt.enter(); + let (provider, _attempts, _cleanup) = account_routing_fixture(&[]); + let prompt = provider + .auto_reset_route_prompt( + ActiveProvider::OpenAI, + "usage exhausted", + &[], + &[], + CompletionMode::Unified { system: "" }, + ) + .expect("another saved login should preserve the earned reset"); + assert!( + prompt + .target_route + .unwrap() + .starts_with("openai-oauth@openai-fox:") + ); + }); +} + #[test] fn agent_failover_rejects_incident_sora_escape_after_oversized_oauth_failure() { with_clean_provider_test_env(|| { diff --git a/crates/jcode-base/src/provider/tests/catalog_memo.rs b/crates/jcode-base/src/provider/tests/catalog_memo.rs index 18addb6161d..84a3b877c86 100644 --- a/crates/jcode-base/src/provider/tests/catalog_memo.rs +++ b/crates/jcode-base/src/provider/tests/catalog_memo.rs @@ -1,5 +1,146 @@ use super::*; +/// Run explicitly with `--ignored --nocapture`. Both paths execute the real +/// key construction, generation reads, TTL checks and instance memo lock. The +/// baseline recreates the former two deep-cloned entries used by a snapshot, +/// rather than timing an isolated Arc clone against an isolated Vec clone. +#[test] +#[ignore = "representative route memo hit-path benchmark"] +fn benchmark_route_memo_snapshot_hit_path() { + with_clean_provider_test_env(|| { + let rt = enter_test_runtime(); + let _runtime_guard = rt.enter(); + let provider = test_multi_provider_with_openai(); + let routes: Vec<_> = (0..4_000) + .map(|index| { + build_openai_api_key_route( + &format!("gpt-future-benchmark-{index}"), + true, + "representative cached catalog route", + ) + }) + .collect(); + *provider.routes_memo.lock().unwrap() = Some(Arc::new(RoutesMemoEntry { + built_at: std::time::Instant::now(), + scope_key: provider.routes_memo_key(), + auth_generation: pricing::auth_pricing_generation(), + catalog_generation: catalog_generation(), + listable_models: listable_model_names_from_routes(&routes), + routes, + })); + let baseline = || { + let models_entry = (*provider.fresh_routes_memo_entry()).clone(); + let routes_entry = (*provider.fresh_routes_memo_entry()).clone(); + jcode_provider_core::ModelCatalogSnapshot::new( + Some(provider.display_name()), + Some(provider.model()), + models_entry.listable_models, + routes_entry.routes, + ) + }; + assert_eq!(baseline(), provider.model_catalog_snapshot()); + let measure = |old: bool| { + let start = std::time::Instant::now(); + for _ in 0..250 { + std::hint::black_box(if old { + baseline() + } else { + provider.model_catalog_snapshot() + }); + } + start.elapsed() + }; + for round in 0..4 { + let (old, shared) = if round % 2 == 0 { + (measure(true), measure(false)) + } else { + let shared = measure(false); + (measure(true), shared) + }; + eprintln!( + "route memo snapshot round {round}: 4000 routes x 250 hits, old deep-clone {old:?}, shared {shared:?}, ratio {:.2}x", + old.as_secs_f64() / shared.as_secs_f64() + ); + } + }); +} + +#[test] +fn route_memo_arc_shares_local_and_fork_hits_and_invalidates_generations() { + with_clean_provider_test_env(|| { + let rt = enter_test_runtime(); + let _runtime_guard = rt.enter(); + let provider = test_multi_provider_with_openai(); + let mut fork = test_multi_provider_with_openai(); + fork.catalog_scope = provider.catalog_scope; + assert_eq!(provider.routes_memo_key(), fork.routes_memo_key()); + let first = provider.fresh_routes_memo_entry(); + assert!(Arc::ptr_eq(&first, &provider.fresh_routes_memo_entry())); + let shared = fork.fresh_routes_memo_entry(); + assert!(Arc::ptr_eq(&first, &shared)); + assert!(Arc::ptr_eq(&shared, &fork.fresh_routes_memo_entry())); + + crate::auth::AuthStatus::invalidate_cache(); + let after_auth = provider.fresh_routes_memo_entry(); + assert!(!Arc::ptr_eq(&first, &after_auth)); + assert!(Arc::ptr_eq(&after_auth, &fork.fresh_routes_memo_entry())); + bump_catalog_generation(); + let after_catalog = fork.fresh_routes_memo_entry(); + assert!(!Arc::ptr_eq(&after_auth, &after_catalog)); + assert!(Arc::ptr_eq( + &after_catalog, + &provider.fresh_routes_memo_entry() + )); + }); +} + +#[test] +fn route_memo_snapshot_preserves_future_catalog_models_and_output_isolation() { + with_clean_provider_test_env(|| { + let rt = enter_test_runtime(); + let _runtime_guard = rt.enter(); + let provider = test_multi_provider_with_openai(); + let future = "gpt-arbitrary-future-catalog-model-2099"; + let scope = openai_catalog_scope_for_credential(false, "sk-test-openai-api-key"); + persist_openai_model_catalog_for_scope( + &scope, + &models::OpenAIModelCatalog { + available_models: vec![future.to_string()], + ..Default::default() + }, + ); + bump_catalog_generation(); + let entry = provider.fresh_routes_memo_entry(); + let expected = provider.model_catalog_snapshot(); + assert_eq!(expected.provider_name, Some(provider.display_name())); + assert_eq!(expected.provider_model, Some(provider.model())); + assert!( + expected + .available_models + .iter() + .any(|model| model == future) + ); + assert!( + expected + .model_routes + .iter() + .any(|route| route.model == future) + ); + assert_eq!(expected.model_routes, entry.routes); + assert_eq!(expected.available_models, entry.listable_models); + + let mut changed = provider.model_catalog_snapshot(); + changed.available_models.clear(); + changed.model_routes[0].model = "mutated-output".to_string(); + let mut routes = provider.model_routes(); + routes.clear(); + let mut models = provider.available_models_display(); + models.clear(); + assert!(Arc::ptr_eq(&entry, &provider.fresh_routes_memo_entry())); + assert_eq!(expected, provider.model_catalog_snapshot()); + }); +} + #[test] fn catalog_sharing_identity_fresh_sessions_share_equivalent_configuration() { with_clean_provider_test_env(|| { diff --git a/crates/jcode-base/src/provider_catalog.rs b/crates/jcode-base/src/provider_catalog.rs index 0ed8e68618d..d1cb708ef8f 100644 --- a/crates/jcode-base/src/provider_catalog.rs +++ b/crates/jcode-base/src/provider_catalog.rs @@ -21,13 +21,18 @@ pub fn api_base_uses_localhost(raw: &str) -> bool { return false; }; - matches!( - parsed - .host_str() - .map(|host| host.to_ascii_lowercase()) - .as_deref(), - Some("localhost") | Some("127.0.0.1") | Some("::1") - ) + let Some(host) = parsed.host_str().map(|host| host.to_ascii_lowercase()) else { + return false; + }; + + if host == "localhost" || host.ends_with(".local") { + return true; + } + + // A bare IP names one machine, like loopback: a self-hosted inference + // server reached by address needs no key by default, whether that address + // is private or public. + ip_addr_from_host(&host).is_some() } pub fn resolve_openai_compatible_profile( @@ -96,7 +101,7 @@ fn resolve_openai_compatible_profile_endpoint( resolved.api_base = normalized; } else { eprintln!( - "Warning: ignoring invalid JCODE_OPENAI_COMPAT_API_BASE '{}'. Use https://... (or http://localhost).", + "Warning: ignoring invalid JCODE_OPENAI_COMPAT_API_BASE '{}'. Use https://..., or http:// with a localhost/.local name or a bare IP address.", base ); } @@ -941,7 +946,7 @@ pub fn apply_named_provider_profile_env_from_config( let api_base = normalize_api_base(&profile.base_url).ok_or_else(|| { anyhow::anyhow!( - "Provider profile '{}' has invalid base_url '{}'. Use https://... or http://localhost.", + "Provider profile '{}' has invalid base_url '{}'. Use https://..., or http:// with a localhost/.local name or a bare IP address.", profile_name, profile.base_url ) diff --git a/crates/jcode-base/src/provider_catalog_tests.rs b/crates/jcode-base/src/provider_catalog_tests.rs index a59b9c8cbbb..d6ada97c0bd 100644 --- a/crates/jcode-base/src/provider_catalog_tests.rs +++ b/crates/jcode-base/src/provider_catalog_tests.rs @@ -369,6 +369,21 @@ fn auth_issue_lan_openai_compatible_bases_are_valid_for_local_model_servers() { Some("http://hsv.local:11434/v1") ); assert_eq!(normalize_api_base("http://example.com/v1"), None); + // Any IP literal is valid over plaintext http, private or public. + assert_eq!( + normalize_api_base("http://203.0.113.10:8000/v1").as_deref(), + Some("http://203.0.113.10:8000/v1") + ); + assert_eq!( + normalize_api_base("http://[2001:db8::1]:8000/v1").as_deref(), + Some("http://[2001:db8::1]:8000/v1") + ); + // A bare IP is a direct endpoint on one machine, so it needs no key by + // default, like loopback. + assert!(api_base_uses_localhost("http://127.0.0.1:11434/v1")); + assert!(api_base_uses_localhost("http://10.15.15.53:8080/v1")); + assert!(api_base_uses_localhost("http://203.0.113.10:8080/v1")); + assert!(!api_base_uses_localhost("https://api.openai.com/v1")); } #[test] diff --git a/crates/jcode-base/src/runtime_activity.rs b/crates/jcode-base/src/runtime_activity.rs index ce61a732364..427afcbff74 100644 --- a/crates/jcode-base/src/runtime_activity.rs +++ b/crates/jcode-base/src/runtime_activity.rs @@ -14,6 +14,9 @@ tokio::task_local! { struct ScopedState { unhealthy: bool, unknown: usize, + // Call sites of live unscoped reservations, so transfer diagnostics can + // name the work that blocks every session instead of a bare count. + unknown_origins: HashMap<&'static std::panic::Location<'static>, usize>, active: HashMap, fenced: BTreeSet, } @@ -45,6 +48,8 @@ pub struct ActivityReservation { _runtime: OwnedRwLockReadGuard<()>, state: Arc>, session: Option, + origin: &'static std::panic::Location<'static>, + changed: Arc, } impl ActivityReservation { @@ -63,6 +68,14 @@ impl ActivityReservation { impl Drop for ActivityReservation { fn drop(&mut self) { let mut state = lock_state(&self.state); + if self.session.is_none() + && let Some(origin) = state.unknown_origins.get_mut(self.origin) + { + *origin = origin.saturating_sub(1); + if *origin == 0 { + state.unknown_origins.remove(self.origin); + } + } let count = match &self.session { Some(session) => state.active.get_mut(session), None => Some(&mut state.unknown), @@ -85,6 +98,9 @@ impl Drop for ActivityReservation { ); } } + drop(state); + // Wake transfer admission waiting for this work to drain. + self.changed.notify_waiters(); } } @@ -119,30 +135,36 @@ fn current_session() -> Option { } impl ActivityGate { + #[track_caller] pub fn try_enter(&self) -> anyhow::Result { self.try_enter_owned(current_session()) } /// Use for work whose effects are not limited to the ambient session. + #[track_caller] pub fn try_enter_global(&self) -> anyhow::Result { self.try_enter_owned(None) } + #[track_caller] pub fn try_enter_session(&self, session: &str) -> anyhow::Result { self.try_enter_owned(Some(session.to_owned())) } + #[track_caller] fn try_enter_owned(&self, session: Option) -> anyhow::Result { + let origin = std::panic::Location::caller(); let runtime = self.runtime.clone().try_read_owned().map_err(|_| { anyhow::anyhow!("Runtime is upgrading while idle. Reconnect and retry the request.") })?; - self.reserve(runtime, session) + self.reserve(runtime, session, origin) } fn reserve( &self, runtime: OwnedRwLockReadGuard<()>, session: Option, + origin: &'static std::panic::Location<'static>, ) -> anyhow::Result { // Absent ownership must never masquerade as an unrelated session // and bypass a transfer fence. @@ -170,27 +192,42 @@ impl ActivityGate { anyhow::bail!("Runtime activity count overflow. Restart the runtime."); } } + if session.is_none() { + *state.unknown_origins.entry(origin).or_default() += 1; + } Ok(ActivityReservation { _runtime: runtime, state: self.scoped.clone(), session, + origin, + changed: self.changed.clone(), }) } /// Background APIs cannot reject accepted tasks. Reserve before spawning. - pub async fn enter(&self) -> ActivityReservation { - self.enter_owned(current_session()).await + #[track_caller] + pub fn enter(&self) -> impl std::future::Future + '_ { + self.enter_owned(current_session(), std::panic::Location::caller()) } - pub async fn enter_global(&self) -> ActivityReservation { - self.enter_owned(None).await + #[track_caller] + pub fn enter_global(&self) -> impl std::future::Future + '_ { + self.enter_owned(None, std::panic::Location::caller()) } - pub async fn enter_session(&self, session: &str) -> ActivityReservation { - self.enter_owned(Some(session.to_owned())).await + #[track_caller] + pub fn enter_session( + &self, + session: &str, + ) -> impl std::future::Future + '_ { + self.enter_owned(Some(session.to_owned()), std::panic::Location::caller()) } - async fn enter_owned(&self, session: Option) -> ActivityReservation { + async fn enter_owned( + &self, + session: Option, + origin: &'static std::panic::Location<'static>, + ) -> ActivityReservation { loop { // Register before checking the fence so release between check and // await cannot lose its wakeup. The runtime lock supplies its own @@ -199,7 +236,7 @@ impl ActivityGate { tokio::pin!(changed); changed.as_mut().enable(); let runtime = self.runtime.clone().read_owned().await; - match self.reserve(runtime, session.clone()) { + match self.reserve(runtime, session.clone(), origin) { Ok(reservation) => return reservation, Err(error) => { jcode_logging::debug(&format!("Runtime activity admission waiting: {error}")); @@ -223,6 +260,62 @@ impl ActivityGate { } } + /// Human-readable description of what currently blocks `sessions` from + /// a scoped transfer. Used for diagnostics only. + pub fn busy_summary(&self, sessions: &BTreeSet) -> String { + let state = lock_state(&self.scoped); + let mut parts = Vec::new(); + if state.unhealthy { + parts.push("activity accounting unhealthy (restart the daemon)".to_owned()); + } + if state.unknown != 0 { + let mut origins: Vec<_> = state + .unknown_origins + .iter() + .map(|(origin, count)| format!("{}:{} x{count}", origin.file(), origin.line())) + .collect(); + origins.sort(); + if origins.is_empty() { + parts.push(format!("{} unscoped task(s)", state.unknown)); + } else { + parts.push(format!( + "{} unscoped task(s) from {}", + state.unknown, + origins.join(", ") + )); + } + } + let busy: Vec<_> = sessions + .iter() + .filter(|id| state.active.contains_key(*id)) + .map(|id| format!("{id} x{}", state.active[id])) + .collect(); + if !busy.is_empty() { + parts.push(format!("busy sessions: {}", busy.join(", "))); + } + let fenced: Vec<_> = sessions + .iter() + .filter(|id| state.fenced.contains(*id)) + .cloned() + .collect(); + if !fenced.is_empty() { + parts.push(format!("already fenced: {}", fenced.join(", "))); + } + if self.runtime.try_read().is_err() { + parts.push("runtime upgrade in progress".to_owned()); + } + if parts.is_empty() { + "nothing (transient)".to_owned() + } else { + parts.join("; ") + } + } + + /// Resolves after any reservation or fence is released. + pub async fn changed(&self) { + self.changed.notified().await + } + pub fn try_idle_sessions(&self, sessions: &BTreeSet) -> Option { let runtime = match self.runtime.clone().try_read_owned() { Ok(runtime) => runtime, @@ -247,6 +340,7 @@ impl ActivityGate { }) } + #[track_caller] pub fn spawn(&self, future: F) -> Option> where F: std::future::Future + Send + 'static, @@ -255,6 +349,7 @@ impl ActivityGate { self.spawn_owned(current_session(), future) } + #[track_caller] pub fn spawn_session( &self, session: &str, @@ -267,6 +362,7 @@ impl ActivityGate { self.spawn_owned(Some(session.to_owned()), future) } + #[track_caller] fn spawn_owned( &self, session: Option, @@ -294,6 +390,7 @@ pub fn global() -> &'static ActivityGate { /// Reserve synchronously before the first poll, inheriting ownership into /// detached callbacks. Tasks without a known owner remain globally blocking. +#[track_caller] pub fn spawn(future: F) -> Option> where F: std::future::Future + Send + 'static, @@ -302,6 +399,7 @@ where global().spawn(future) } +#[track_caller] pub fn spawn_session(session: &str, future: F) -> Option> where F: std::future::Future + Send + 'static, @@ -506,4 +604,18 @@ mod tests { let _ = child.await; assert!(gate.try_idle_sessions(&selected).is_some()); } + + #[test] + fn busy_summary_names_unscoped_origin_and_clears_it() { + let gate = ActivityGate::default(); + let selected = BTreeSet::from(["a".to_owned()]); + let held = gate.try_enter_global().unwrap(); + let summary = gate.busy_summary(&selected); + assert!(summary.contains("runtime_activity.rs:"), "{summary}"); + drop(held); + assert_eq!(gate.busy_summary(&selected), "nothing (transient)"); + let scoped = gate.try_enter_session("b").unwrap(); + assert!(gate.try_idle_sessions(&selected).is_some()); + drop(scoped); + } } diff --git a/crates/jcode-base/src/session/persistence.rs b/crates/jcode-base/src/session/persistence.rs index 88b2920fc00..5118f2c7747 100644 --- a/crates/jcode-base/src/session/persistence.rs +++ b/crates/jcode-base/src/session/persistence.rs @@ -875,6 +875,7 @@ impl Session { fields.push(("error", crate::util::format_error_chain(error))); crate::logging::event_warn("SESSION_PERSISTENCE", fields); } else { + crate::mobile_sync::notify_session_persisted(&self.id); if let Err(error) = crate::recent_session_index::upsert_session(self) { crate::logging::warn(&format!( "Failed to update recent-session metadata for {}: {error}", diff --git a/crates/jcode-base/src/session_telemetry.rs b/crates/jcode-base/src/session_telemetry.rs index e78c0eb83c4..7d8330b2152 100644 --- a/crates/jcode-base/src/session_telemetry.rs +++ b/crates/jcode-base/src/session_telemetry.rs @@ -66,6 +66,7 @@ pub fn save_context( crate::usage::cached_account_usage_snapshot(provider, label) .filter(|usage| usage.label == label) .map(|usage| jcode_protocol::AccountUsageSnapshot { + additional_windows: usage.additional_windows, label: usage.label, email: None, exhausted: usage.exhausted, diff --git a/crates/jcode-base/src/terminal_launch.rs b/crates/jcode-base/src/terminal_launch.rs index 485d5a173c2..e5fc569e70c 100644 --- a/crates/jcode-base/src/terminal_launch.rs +++ b/crates/jcode-base/src/terminal_launch.rs @@ -1,8 +1,9 @@ use anyhow::{Result, bail}; pub use jcode_terminal_launch::{ - SpawnAttempt, TerminalCommand, apply_client_terminal_env, build_hook_spawn_command, - detected_resume_terminal, expand_home, parse_hook_command, resume_terminal_candidates, - sh_escape, shell_command, snapshot_client_terminal_env, spawn_command_in_new_terminal_with, + RUNTIME_ENV_VARS, SpawnAttempt, TerminalCommand, apply_client_terminal_env, apply_runtime_env, + build_hook_spawn_command, detected_resume_terminal, expand_home, parse_hook_command, + resume_terminal_candidates, sh_escape, shell_command, snapshot_client_terminal_env, + spawn_command_in_new_terminal_with, }; use std::path::Path; @@ -20,6 +21,34 @@ pub fn configured_spawn_hook() -> Option { .map(str::to_string) } +#[cfg(test)] +mod runtime_env_facade_tests { + #[test] + fn app_core_portable_runtime_filter_reference_is_exported() { + let _: &'static [&'static str] = super::RUNTIME_ENV_VARS; + assert!(super::RUNTIME_ENV_VARS.contains(&"JCODE_SOCKET")); + } + + #[test] + fn contextual_companion_error_requests_safe_local_fallback() { + use std::path::Path; + let command = super::TerminalCommand::new("jcode", vec!["--resume".into()]) + .runtime_env(vec![("JCODE_SOCKET".into(), "/tmp/connected.sock".into())]); + // An older companion rejects the distinct contextual operation; the + // wire-level rejection itself is covered by the companion client tests. + let handled = + super::try_spawn_via_companion_with(&command, Path::new("/work"), |_request| { + Err(crate::companion_client::CompanionError::Failed( + "unknown variant `spawn_terminal_with_runtime`".into(), + )) + }); + assert!( + !handled, + "a rejected contextual request must fall back to a local launch" + ); + } +} + /// Spawn `command` in a new terminal window/pane. /// /// When a spawn hook is configured (`[terminal] spawn_hook` / `JCODE_SPAWN_HOOK`), @@ -49,17 +78,25 @@ fn try_spawn_via_companion(command: &TerminalCommand, cwd: &Path) -> bool { if std::env::var_os("JCODE_COMPANION").is_some() || crate::auth::running_in_test_harness() { return false; } - let request = crate::companion_client::CompanionRequest::SpawnTerminal { - program: command.program.clone(), - args: command.args.clone(), - cwd: cwd.to_path_buf(), - title: command.title.clone(), - kind: command.kind.clone(), - session_id: command.session_id.clone(), - env: command.extra_env.clone(), - client_terminal_env: command.client_terminal_env.clone(), - }; - match crate::companion_client::request(&request, std::time::Duration::from_secs(10)) { + try_spawn_via_companion_with(command, cwd, |request| { + crate::companion_client::request(request, std::time::Duration::from_secs(10)) + }) +} + +/// Shared request-selection path with an injectable transport for isolated +/// compatibility tests. Any protocol error safely falls back to the local path. +fn try_spawn_via_companion_with( + command: &TerminalCommand, + cwd: &Path, + mut send: impl FnMut( + &crate::companion_client::CompanionRequest, + ) -> std::result::Result< + crate::companion_client::CompanionResponse, + crate::companion_client::CompanionError, + >, +) -> bool { + let request = crate::companion_client::CompanionRequest::spawn_terminal(command, cwd); + match send(&request) { Ok(_) => true, Err(crate::companion_client::CompanionError::Unavailable) => false, Err(error) => { diff --git a/crates/jcode-base/src/todo.rs b/crates/jcode-base/src/todo.rs index 114c4d5695d..99c52d72895 100644 --- a/crates/jcode-base/src/todo.rs +++ b/crates/jcode-base/src/todo.rs @@ -306,6 +306,7 @@ mod tests { id: content.to_ascii_lowercase().replace(' ', "-"), group: group.map(str::to_string), lane_after: None, + lane_owner: None, confidence: None, completion_confidence: None, confidence_history: Vec::new(), diff --git a/crates/jcode-base/src/usage.rs b/crates/jcode-base/src/usage.rs index 6cdca9f2fd5..01b1b41cb2c 100644 --- a/crates/jcode-base/src/usage.rs +++ b/crates/jcode-base/src/usage.rs @@ -7,6 +7,11 @@ mod accessors; mod account_fetch; pub use account_fetch::fetch_oauth_account_usage; mod anthropic_reset; +mod anthropic_fable_reset; +pub(crate) use anthropic_fable_reset::auto_reset_fable_usage; +mod anthropic_response; +pub(crate) use anthropic_response::invalidate_auth_failure as invalidate_anthropic_usage_auth_failure; +pub use anthropic_response::record_anthropic_response_limits; mod api_keys; mod banked; pub use banked::{AccountBankedResets, cached_account_banked_resets}; @@ -18,6 +23,7 @@ mod local_activity; mod model; mod openai_helpers; mod openai_reset; +pub(crate) use openai_reset::auto_reset_weekly_usage; mod provider_fetch; mod remote_accounts; mod startup_snapshot; @@ -47,7 +53,10 @@ pub use remote_accounts::{ clear_remote_account_usage, remote_account_banked_resets, remote_account_list, remote_account_usage_received, set_remote_account_usage, }; -pub use startup_snapshot::{AccountUsageStartupPublisher, hydrate_remote_account_usage}; +pub use startup_snapshot::{ + AccountUsageStartupPublisher, hydrate_remote_account_usage, + hydrate_remote_account_usage_for_generation, +}; use anyhow::{Context, Result}; pub use display::{format_reset_time, format_usage_bar}; @@ -60,7 +69,7 @@ use std::time::{Duration, Instant}; use tokio::sync::RwLock; /// Usage API endpoint -const USAGE_URL: &str = "https://api.anthropic.com/api/oauth/usage"; +const USAGE_URL: &str = "https://api.anthropic.com/api/oauth/usage?at_wall=1&skip_spend=1"; /// OpenAI ChatGPT usage endpoint const OPENAI_USAGE_URL: &str = "https://chatgpt.com/backend-api/wham/usage"; @@ -182,6 +191,7 @@ async fn fetch_anthropic_usage_data_uncached( .as_ref() .and_then(|window| window.utilization) .is_some(); + let reset_status = serde_json::json!({ "juniper_tide": data.juniper_tide }); let usage = UsageData { five_hour: data .five_hour @@ -241,7 +251,7 @@ async fn fetch_anthropic_usage_data_uncached( probe_started, ); } - let usage = disk_cache::store_success(&cache_key, &usage); + let usage = disk_cache::store_success_with_reset_status(&cache_key, &usage, Some(reset_status)); store_anthropic_usage(cache_key, usage.clone()); Ok(usage) } diff --git a/crates/jcode-base/src/usage/accessors.rs b/crates/jcode-base/src/usage/accessors.rs index 674a5e7d253..4ee4b9a0348 100644 --- a/crates/jcode-base/src/usage/accessors.rs +++ b/crates/jcode-base/src/usage/accessors.rs @@ -29,8 +29,15 @@ async fn fetch_usage() -> Result { .await { Ok(refreshed) => refreshed.access_token, + Err(error) if creds.expires_at < now => { + return Err(anthropic_response::credential_error(error)); + } Err(_) => creds.access_token, } + } else if creds.expires_at < now { + return Err(anthropic_response::credential_error(anyhow::anyhow!( + "OAuth token expired" + ))); } else { creds.access_token }; @@ -449,6 +456,7 @@ fn anthropic_account_usage_probe_sync() -> Option { &usage, )), Err(err) => snapshots.push(AccountUsageSnapshot { + additional_windows: Vec::new(), label: account.label.clone(), email: account.email.clone(), exhausted: false, @@ -504,6 +512,7 @@ fn openai_account_usage_probe_sync() -> Option { match usage { Ok(snapshot) => snapshots.push(snapshot), Err(err) => snapshots.push(AccountUsageSnapshot { + additional_windows: Vec::new(), label: account.label.clone(), email: account.email.clone(), exhausted: false, diff --git a/crates/jcode-base/src/usage/account_fetch.rs b/crates/jcode-base/src/usage/account_fetch.rs index 3f7bee53ad9..f10651c639f 100644 --- a/crates/jcode-base/src/usage/account_fetch.rs +++ b/crates/jcode-base/src/usage/account_fetch.rs @@ -6,6 +6,23 @@ use futures::future::join_all; // consume the daemon worker deadline and discard healthy accounts' usage. const ACCOUNT_PROBE_TIMEOUT: Duration = Duration::from_secs(20); +pub(super) fn cache_anthropic_account_failure( + cache_key: String, + error: anyhow::Error, +) -> UsageData { + // A first-ever HTTP failure can already carry a provider Retry-After. + // Credential failures happen before that cache, so persist those too. + let data = cached_or_shared_anthropic_usage(&cache_key).unwrap_or_else(|| { + disk_cache::store_error_with_hint( + &cache_key, + &anthropic_response::credential_error(error).to_string(), + None, + ) + }); + store_anthropic_usage(cache_key, data.clone()); + data +} + async fn bounded_probe(label: String, email: Option, probe: F) -> AccountUsageSnapshot where F: std::future::Future, @@ -13,6 +30,7 @@ where tokio::time::timeout(ACCOUNT_PROBE_TIMEOUT, probe) .await .unwrap_or_else(|_| AccountUsageSnapshot { + additional_windows: Vec::new(), label, email, exhausted: false, @@ -62,17 +80,8 @@ pub async fn fetch_oauth_account_usage( Err(error) => Err(error), } }; - let data = usage.unwrap_or_else(|error| { - let data = UsageData { - fetched_at: Some(Instant::now()), - last_error: Some(error.to_string()), - ..Default::default() - }; - // Refresh and expired-token failures happen before the HTTP - // fetcher's cache. Give them the same per-account retry floor. - store_anthropic_usage(cache_key, data.clone()); - data - }); + let data = usage + .unwrap_or_else(|error| cache_anthropic_account_failure(cache_key, error)); anthropic_snapshot_from_usage(account.label, account.email, &data) }) })) diff --git a/crates/jcode-base/src/usage/anthropic_fable_reset.rs b/crates/jcode-base/src/usage/anthropic_fable_reset.rs new file mode 100644 index 00000000000..625dc54b6d6 --- /dev/null +++ b/crates/jcode-base/src/usage/anthropic_fable_reset.rs @@ -0,0 +1,496 @@ +//! Fable earned resets using Claude Code 2.1.322's `cedar_ember` contract. +//! See docs/anthropic-fable-resets.md for primary-source evidence. + +use super::anthropic_reset::{authorize, fresh_access_token, get_json, reset_client}; +use anyhow::{Context, Result}; +use serde::Deserialize; +use sha2::{Digest, Sha256}; +use std::io::Write; + +const BASE: &str = "https://api.anthropic.com"; +const STATUS: &str = "/api/oauth/usage?cedar_ember=1&skip_spend=1"; + +struct InvalidateOnDrop<'a>(&'a str); + +impl Drop for InvalidateOnDrop<'_> { + fn drop(&mut self) { + super::anthropic_reset::invalidate_anthropic_usage_reset_state(Some(self.0)); + } +} + +#[derive(Debug, Deserialize)] +struct Status { + eligible: bool, + at_limit: bool, + exhausted: Vec, + grants: Vec, +} + +#[derive(Debug, Deserialize)] +struct Grant { + id: String, + resets_left: u64, + starts_at: Option, + ends_at: Option, + clears: Vec, + paused: bool, + usable_now: bool, + gives_back_weekly: bool, + blocking: Vec, +} + +fn valid_id(id: &str) -> bool { + !id.is_empty() + && id.len() <= 40 + && id + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_' || b == b'-') +} + +fn eligible_grant(status: &Status, now: chrono::DateTime) -> Option<&Grant> { + if !status.eligible + || !status.at_limit + || !status + .exhausted + .iter() + .any(|limit| matches!(limit.as_str(), "seven_day" | "seven_day_overage_included")) + { + return None; + } + status.grants.iter().find(|grant| { + grant.gives_back_weekly && valid_id(&grant.id) && grant.resets_left > 0 + && !grant.paused && grant.usable_now && grant.blocking.is_empty() + && grant.starts_at.as_deref().is_none_or(|at| { + chrono::DateTime::parse_from_rfc3339(at).is_ok_and(|at| at <= now) + }) + && grant.ends_at.as_deref().is_some_and(|at| { + chrono::DateTime::parse_from_rfc3339(at).is_ok_and(|at| at > now) + }) + // Do not spend a Fable grant against an unrelated model's limit. + && status.exhausted.iter().all(|limit| { + matches!(limit.as_str(), "five_hour" | "seven_day" | "seven_day_overage_included") + && (grant.clears.contains(limit) || limit == "seven_day") + }) + }) +} + +fn profile_identity(profile: &serde_json::Value) -> Result<(String, String)> { + let extract = |path| { + profile + .pointer(path) + .and_then(|v| v.as_str()) + .filter(|id| { + !id.is_empty() && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-') + }) + .map(str::to_owned) + .context("Claude reset profile has no verified account/organization identity") + }; + Ok((extract("/account/uuid")?, extract("/organization/uuid")?)) +} + +async fn status(client: &reqwest::Client, base: &str, token: &str) -> Result> { + let value = get_json(client, &format!("{base}{STATUS}"), token).await?; + let Some(block) = value.get("cedar_ember") else { + return Ok(None); + }; + // Missing or malformed fields are unknown, not permission to spend. + Ok(match serde_json::from_value(block.clone()) { + Ok(status) => Some(status), + Err(error) => { + crate::logging::warn(&format!( + "Ignoring malformed Fable reset eligibility: {error}" + )); + None + } + }) +} + +/// Redeem an earned Fable grant only after fresh, account-bound exhaustion checks. +/// A cross-process account lock serializes claims. A durable pre-POST marker +/// suppresses repeat spending after timeout, cancellation, process death, malformed +/// results, or an unconfirmed reset. Never falls back from an explicit account. +/// `None` is deliberately unsupported: background retry must pin a named login. +/// This never purchases usage credits or redeems the `juniper_tide` program. +pub(crate) async fn auto_reset_fable_usage(account_label: Option) -> Result { + if !crate::config::Config::load() + .provider + .anthropic_auto_fable_reset + { + return Ok(false); + } + let Some(label) = account_label else { + return Ok(false); + }; + let token = fresh_access_token(Some(&label)).await?; + let client = reset_client()?; + redeem( + &client, + BASE, + &token, + &label, + &crate::storage::jcode_dir()?.join("fable-resets"), + ) + .await +} + +async fn redeem( + client: &reqwest::Client, + base: &str, + token: &str, + label: &str, + root: &std::path::Path, +) -> Result { + // The profile and usage requests use the exact same pinned token. + let profile = get_json(client, &format!("{base}/api/oauth/profile"), token).await?; + let (account, organization) = profile_identity(&profile)?; + std::fs::create_dir_all(root)?; + let key = hex::encode(Sha256::digest( + format!("{account}:{organization}").as_bytes(), + )); + let lock = std::fs::OpenOptions::new() + .create(true) + .truncate(false) + .read(true) + .write(true) + .open(root.join(format!("{key}.lock")))?; + // Wait asynchronously for another agent rather than triggering failover + // while its claim is in flight. Errors are not mistaken for contention. + let started = std::time::Instant::now(); + let mut waited = false; + loop { + match lock.try_lock() { + Ok(()) => break, + Err(std::fs::TryLockError::WouldBlock) => { + waited = true; + if started.elapsed() >= std::time::Duration::from_secs(90) { + return Ok(false); + } + tokio::time::sleep(std::time::Duration::from_millis(50)).await; + } + Err(std::fs::TryLockError::Error(error)) => return Err(error.into()), + } + } + let Some(before) = status(client, base, token).await? else { + return Ok(false); + }; + // A concurrent claimant cleared the quota. Retry is safe without spending + // anything or erasing its possibly uncertain durable journal. + if waited && !before.at_limit && before.exhausted.is_empty() { + super::anthropic_reset::invalidate_anthropic_usage_reset_state(Some(label)); + return Ok(true); + } + let marker = root.join(format!("{key}.pending")); + // Even a fresh "not limited" response cannot prove whether an earlier + // uncertain grant was spent. Keep its marker for explicit reconciliation. + if marker.exists() { + return Ok(false); + } + let Some(grant) = eligible_grant(&before, chrono::Utc::now()) else { + return Ok(false); + }; + let grant_id = grant.id.clone(); + let request_id = uuid::Uuid::new_v4().to_string(); + let mut journal = std::fs::OpenOptions::new() + .create_new(true) + .write(true) + .open(&marker)?; + journal.write_all( + serde_json::to_string(&serde_json::json!({ + "program": "cedar_ember", "grant_id": grant_id, "request_id": request_id, + "created_at": chrono::Utc::now().to_rfc3339() + }))? + .as_bytes(), + )?; + journal.sync_all()?; + // Persist the directory entry before a non-refundable mutation. + #[cfg(unix)] + std::fs::File::open(root)?.sync_all()?; + let _invalidate = InvalidateOnDrop(label); + let response = authorize(client.post(format!("{base}/api/organizations/{organization}/reset_rate_limits")), token) + .json(&serde_json::json!({"program":"cedar_ember", "grant_id":grant_id, "request_id":request_id})) + .send().await.context("Fable reset outcome uncertain. Pending marker retained; check Claude Usage before reconciliation.")?; + anyhow::ensure!( + response.status().is_success(), + "Fable reset outcome uncertain (HTTP {}), pending marker retained", + response.status().as_u16() + ); + #[derive(Deserialize)] + struct Claim { + result: String, + cleared: Vec, + } + let claim: Claim = response + .json() + .await + .context("Unrecognized Fable reset result; pending marker retained")?; + if claim.result != "reset" + || !before + .exhausted + .iter() + .all(|limit| limit == "seven_day" || claim.cleared.contains(limit)) + { + return Ok(false); + } + let Some(after) = status(client, base, token).await? else { + return Ok(false); + }; + if after.at_limit || !after.exhausted.is_empty() { + return Ok(false); + } + // Only a confirmed successful claim plus fresh clear quota permits another + // future automatic spend. Unknown outcomes remain suppressed across restarts. + std::fs::remove_file(&marker)?; + Ok(true) +} + +#[cfg(test)] +mod tests { + use super::*; + fn fixture() -> Status { + serde_json::from_value(serde_json::json!({ + "eligible":true,"at_limit":true,"exhausted":["seven_day_overage_included"], + "grants":[{"id":"fable_launch","resets_left":2,"starts_at":null, + "ends_at":"2027-03-09T07:59:00Z","clears":["five_hour","seven_day_overage_included"], + "paused":false,"usable_now":true,"gives_back_weekly":true,"blocking":[]}] + })).unwrap() + } + #[test] + fn actual_cli_schema_selects_fable_not_session_grants() { + let mut value = fixture(); + let now = chrono::DateTime::parse_from_rfc3339("2026-11-10T12:00:00Z") + .unwrap() + .with_timezone(&chrono::Utc); + assert!(eligible_grant(&value, now).is_some()); + value.grants[0].gives_back_weekly = false; + assert!(eligible_grant(&value, now).is_none()); + } + #[test] + fn refuse_non_exhaustion_blocking_expiry_and_unrelated_limits() { + let now = chrono::DateTime::parse_from_rfc3339("2026-11-10T12:00:00Z") + .unwrap() + .with_timezone(&chrono::Utc); + let mut value = fixture(); + value.at_limit = false; + assert!(eligible_grant(&value, now).is_none()); + value = fixture(); + value.exhausted = vec!["five_hour".into()]; + assert!(eligible_grant(&value, now).is_none()); + value = fixture(); + value.exhausted = vec!["seven_day_sonnet".into()]; + assert!(eligible_grant(&value, now).is_none()); + value = fixture(); + value.grants[0].blocking.push("seven_day".into()); + assert!(eligible_grant(&value, now).is_none()); + value = fixture(); + value.grants[0].ends_at = Some("bad".into()); + assert!(eligible_grant(&value, now).is_none()); + value = fixture(); + value.grants[0].ends_at = Some("2025-01-01T00:00:00Z".into()); + assert!(eligible_grant(&value, now).is_none()); + } + #[test] + fn profile_requires_both_verified_identities() { + assert!(profile_identity(&serde_json::json!({"account":{"uuid":"acct"}})).is_err()); + assert_eq!( + profile_identity( + &serde_json::json!({"account":{"uuid":"acct"},"organization":{"uuid":"org"}}) + ) + .unwrap(), + ("acct".into(), "org".into()) + ); + } + #[test] + fn fable_automatic_reset_is_independently_opt_in() { + assert!(!crate::config::ProviderConfig::default().anthropic_auto_fable_reset); + } + + async fn mock( + fail_claim: bool, + weekly: bool, + ) -> ( + String, + std::sync::Arc>>, + tokio::task::JoinHandle<()>, + ) { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let requests = std::sync::Arc::new(std::sync::Mutex::new(Vec::new())); + let seen = requests.clone(); + let task = tokio::spawn(async move { + let mut spent = false; + loop { + let (mut stream, _) = listener.accept().await.unwrap(); + let mut bytes = Vec::new(); + loop { + let mut buf = [0; 4096]; + let n = stream.read(&mut buf).await.unwrap(); + assert!(n > 0); + bytes.extend_from_slice(&buf[..n]); + let text = String::from_utf8_lossy(&bytes); + if let Some((headers, body)) = text.split_once("\r\n\r\n") { + let len = headers + .lines() + .find_map(|line| { + line.to_ascii_lowercase() + .strip_prefix("content-length: ") + .and_then(|s| s.parse::().ok()) + }) + .unwrap_or(0); + if body.len() >= len { + break; + } + } + } + let text = String::from_utf8(bytes).unwrap(); + let (code, body) = if text.starts_with("GET /api/oauth/profile ") { + ( + 200, + serde_json::json!({"account":{"uuid":"account-1"},"organization":{"uuid":"org-1"}}), + ) + } else if text.starts_with("GET /api/oauth/usage?cedar_ember=1&skip_spend=1 ") { + let mut status = serde_json::to_value(fixture_json()).unwrap(); + if weekly { + status["exhausted"] = serde_json::json!(["seven_day"]); + } + if spent && !fail_claim { + status["at_limit"] = false.into(); + status["exhausted"] = serde_json::json!([]); + } + (200, serde_json::json!({"cedar_ember":status})) + } else { + assert!(text.starts_with("POST /api/organizations/org-1/reset_rate_limits ")); + let body: serde_json::Value = + serde_json::from_str(text.split_once("\r\n\r\n").unwrap().1).unwrap(); + assert_eq!(body["program"], "cedar_ember"); + assert_eq!(body["grant_id"], "fable_launch"); + uuid::Uuid::parse_str(body["request_id"].as_str().unwrap()).unwrap(); + spent = true; + ( + if fail_claim { 500 } else { 200 }, + serde_json::json!({"result":"reset","cleared":["seven_day_overage_included"]}), + ) + }; + assert!( + text.to_ascii_lowercase() + .contains("authorization: bearer pinned-token") + ); + seen.lock().unwrap().push(text); + let body = body.to_string(); + stream.write_all(format!("HTTP/1.1 {code} Test\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).as_bytes()).await.unwrap(); + } + }); + (base, requests, task) + } + + fn fixture_json() -> serde_json::Value { + serde_json::json!({"eligible":true,"at_limit":true,"exhausted":["seven_day_overage_included"], + "grants":[{"id":"fable_launch","resets_left":2,"starts_at":null, + "ends_at":"2099-03-09T07:59:00Z","clears":["five_hour","seven_day_overage_included"], + "paused":false,"usable_now":true,"gives_back_weekly":true,"blocking":[]}]}) + } + + #[tokio::test] + async fn concurrent_agents_spend_one_grant_only_after_verified_clear() { + let (base, seen, server) = mock(false, false).await; + let dir = tempfile::tempdir().unwrap(); + let client = reset_client().unwrap(); + let (a, b) = tokio::join!( + redeem(&client, &base, "pinned-token", "one", dir.path()), + redeem( + &client, + &base, + "pinned-token", + "alias-same-account", + dir.path() + ) + ); + assert!(a.unwrap()); + assert!(b.unwrap()); + assert!( + !redeem(&client, &base, "pinned-token", "one", dir.path()) + .await + .unwrap() + ); + assert_eq!( + seen.lock() + .unwrap() + .iter() + .filter(|r| r.starts_with("POST ")) + .count(), + 1 + ); + assert!(!std::fs::read_dir(dir.path()).unwrap().any(|e| { + e.unwrap() + .path() + .extension() + .is_some_and(|e| e == "pending") + })); + server.abort(); + } + + #[tokio::test] + async fn uncertain_claim_is_suppressed_on_retry_and_restart() { + let (base, seen, server) = mock(true, false).await; + let dir = tempfile::tempdir().unwrap(); + let client = reset_client().unwrap(); + assert!( + redeem(&client, &base, "pinned-token", "one", dir.path()) + .await + .is_err() + ); + // A new client has no process-local attempt state. The durable marker wins. + assert!( + !redeem( + &reset_client().unwrap(), + &base, + "pinned-token", + "other-alias", + dir.path() + ) + .await + .unwrap() + ); + assert_eq!( + seen.lock() + .unwrap() + .iter() + .filter(|r| r.starts_with("POST ")) + .count(), + 1 + ); + assert!(std::fs::read_dir(dir.path()).unwrap().any(|e| { + e.unwrap() + .path() + .extension() + .is_some_and(|e| e == "pending") + })); + server.abort(); + } + + #[tokio::test] + async fn fable_weekly_refund_recovers_without_zeroing_whole_weekly_window() { + let (base, seen, server) = mock(false, true).await; + let dir = tempfile::tempdir().unwrap(); + assert!( + redeem( + &reset_client().unwrap(), + &base, + "pinned-token", + "one", + dir.path() + ) + .await + .unwrap() + ); + assert_eq!( + seen.lock() + .unwrap() + .iter() + .filter(|r| r.starts_with("POST ")) + .count(), + 1 + ); + server.abort(); + } +} diff --git a/crates/jcode-base/src/usage/anthropic_reset.rs b/crates/jcode-base/src/usage/anthropic_reset.rs index 90e83a1bc80..b89f32f581c 100644 --- a/crates/jcode-base/src/usage/anthropic_reset.rs +++ b/crates/jcode-base/src/usage/anthropic_reset.rs @@ -57,7 +57,10 @@ fn valid_timestamp(value: &str) -> bool { chrono::DateTime::parse_from_rfc3339(value).is_ok() } -fn authorize(request: reqwest::RequestBuilder, access_token: &str) -> reqwest::RequestBuilder { +pub(super) fn authorize( + request: reqwest::RequestBuilder, + access_token: &str, +) -> reqwest::RequestBuilder { crate::provider::anthropic::apply_oauth_attribution_headers( request .bearer_auth(access_token) @@ -72,7 +75,7 @@ fn authorize(request: reqwest::RequestBuilder, access_token: &str) -> reqwest::R ) } -fn reset_client() -> Result { +pub(super) fn reset_client() -> Result { // Never follow redirects with credentials or retry the claim automatically. reqwest::Client::builder() .timeout(Duration::from_secs(35)) @@ -96,7 +99,11 @@ fn http_failure(status: reqwest::StatusCode) -> anyhow::Error { ) } -async fn get_json(client: &reqwest::Client, url: &str, token: &str) -> Result { +pub(super) async fn get_json( + client: &reqwest::Client, + url: &str, + token: &str, +) -> Result { let response = authorize(client.get(url), token) .timeout(Duration::from_secs(12)) .send() @@ -113,15 +120,16 @@ async fn get_json(client: &reqwest::Client, url: &str, token: &str) -> Result, ) -> (Option, bool) { - let Ok(client) = reset_client() else { - return (None, false); - }; - let Ok(value) = get_json(&client, &format!("{API_BASE}{STATUS_PATH}"), access_token).await - else { + // This endpoint is the quota endpoint too. A second uncached request here + // used to bypass its cross-process flight lock and Retry-After backoff on + // every /usage report, even when the quota itself came from cache. + // Explicit reset preparation below still validates with a fresh request. + let key = anthropic_usage_cache_key(access_token, account_label); + let Some(value) = super::disk_cache::reset_status(&key) else { return (None, false); }; ( @@ -231,7 +239,7 @@ fn display_text(value: &str) -> String { .collect() } -async fn fresh_access_token(account_label: Option<&str>) -> Result { +pub(super) async fn fresh_access_token(account_label: Option<&str>) -> Result { let credentials = match account_label { Some(label) => auth::claude::load_credentials_for_account(label), None => auth::claude::load_credentials(), diff --git a/crates/jcode-base/src/usage/anthropic_response.rs b/crates/jcode-base/src/usage/anthropic_response.rs new file mode 100644 index 00000000000..9ee15e33aa2 --- /dev/null +++ b/crates/jcode-base/src/usage/anthropic_response.rs @@ -0,0 +1,306 @@ +//! Quota telemetry already returned by subscription model requests. +//! This must not clear the usage endpoint's independent polling backoff. +use super::*; + +fn window(headers: &reqwest::header::HeaderMap, name: &str) -> Option<(f32, String)> { + let prefix = format!("anthropic-ratelimit-unified-{name}"); + // Optional telemetry can be absent or malformed. Neither means zero usage. + let Ok(text) = headers.get(format!("{prefix}-utilization"))?.to_str() else { + return None; + }; + let Ok(ratio) = text.parse::() else { + return None; + }; + if !ratio.is_finite() || !(0.0..=1.0).contains(&ratio) { + return None; + } + let Ok(text) = headers.get(format!("{prefix}-reset"))?.to_str() else { + return None; + }; + let Ok(epoch) = text.parse::() else { + return None; + }; + let reset = chrono::DateTime::from_timestamp(epoch, 0)?; + (reset > chrono::Utc::now()).then(|| (ratio, reset.to_rfc3339())) +} + +/// Record real quota ratios, not token-count estimates, from an official +/// Anthropic OAuth success or quota-wall response. Call on a blocking worker, never from a +/// renderer. Incomplete, malformed, or expired header sets are ignored rather +/// than manufacturing a zero-usage window. API-key responses must not call this. +pub fn record_anthropic_response_limits( + access_token: &str, + account_label: Option<&str>, + headers: &reqwest::header::HeaderMap, +) { + let (Some(five), Some(seven)) = (window(headers, "5h"), window(headers, "7d")) else { + return; + }; + let key = anthropic_usage_cache_key(access_token, account_label); + let data = disk_cache::store_response_limits(&key, five, seven); + store_anthropic_usage(key, data); +} + +/// Refresh rejections must reach the accounts UI as an authentication problem, +/// not an opaque usage-endpoint failure. Transient refresh errors stay retryable. +pub(super) fn credential_error(error: anyhow::Error) -> anyhow::Error { + let message = error.to_string(); + if auth::refresh_state::error_is_permanent_rejection(&message) + || message.contains("previously rejected") + || message == "OAuth token expired" + { + anyhow::anyhow!("Usage authentication failed (401): sign in again with /login claude") + } else { + error + } +} + +/// A newly saved credential can repair authentication immediately, but must +/// never bypass a usage lookup's 429/Retry-After deadline. +pub(crate) fn invalidate_auth_failure(label: &str) { + let key = format!("label:{label}"); + disk_cache::clear_auth_failure(&key); + if let Ok(mut cache) = cache::anthropic_usage_cache().lock() + && cache + .get(&key) + .and_then(|data| data.last_error.as_deref()) + .is_some_and(is_auth_failure) + { + cache.remove(&key); + } +} + +pub(super) fn is_auth_failure(error: &str) -> bool { + error.contains("401") + || error.contains("OAuth token expired") + || error.contains("previously rejected") + || auth::refresh_state::error_is_permanent_rejection(error) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn with_home(f: impl FnOnce(tokio::runtime::Runtime)) { + let _guard = crate::storage::lock_test_env(); + let dir = tempfile::tempdir().unwrap(); + struct Restore(Option); + impl Drop for Restore { + fn drop(&mut self) { + match self.0.take() { + Some(value) => crate::env::set_var("JCODE_HOME", value), + None => crate::env::remove_var("JCODE_HOME"), + } + } + } + let _restore = Restore(std::env::var_os("JCODE_HOME")); + crate::env::set_var("JCODE_HOME", dir.path()); + f(tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap()); + } + + fn headers() -> reqwest::header::HeaderMap { + let reset = (chrono::Utc::now().timestamp() + 7200).to_string(); + let mut headers = reqwest::header::HeaderMap::new(); + for (name, value) in [ + ("anthropic-ratelimit-unified-5h-utilization", "0.37"), + ("anthropic-ratelimit-unified-7d-utilization", "0.62"), + ("anthropic-ratelimit-unified-5h-reset", reset.as_str()), + ("anthropic-ratelimit-unified-7d-reset", reset.as_str()), + ] { + headers.insert(name, value.parse().unwrap()); + } + headers + } + + #[test] + fn response_limits_recover_bars_without_bypassing_endpoint_backoff() { + with_home(|runtime| { + runtime.block_on(async { + let key = "label:passive-recovery"; + let before = + disk_cache::store_error_with_hint(key, "Usage API error (429)", Some("3600")); + let deadline = before.refresh_after.unwrap(); + record_anthropic_response_limits("fixture", Some("passive-recovery"), &headers()); + // Simulate a different process recording the headers while + // this daemon retains the earlier failure in memory. + store_anthropic_usage(key.into(), before); + assert!( + cached_or_shared_anthropic_usage(key) + .unwrap() + .last_error + .is_none() + ); + let data = disk_cache::fresh(key).unwrap(); + assert_eq!((data.five_hour, data.seven_day), (0.37, 0.62)); + assert!(data.last_error.is_none()); + assert!(data.last_success_at.is_some()); + assert!(data.refresh_after.unwrap() + Duration::from_millis(10) >= deadline); + let snapshot = cached_account_usage_snapshot( + MultiAccountProviderKind::Anthropic, + "passive-recovery", + ) + .unwrap(); + assert_eq!(snapshot.five_hour_ratio, Some(0.37)); + assert_eq!(snapshot.seven_day_ratio, Some(0.62)); + assert!(snapshot.error.is_none()); + flight::fetch(key.into(), || async { + panic!("passive telemetry must not cause a usage request during Retry-After") + }) + .await + .unwrap(); + // Relogin must not erase a known provider cooldown either. + invalidate_auth_failure("passive-recovery"); + assert!( + disk_cache::fresh(key).unwrap().refresh_after.unwrap() + + Duration::from_millis(10) + >= deadline + ); + }) + }); + } + + #[test] + fn partial_invalid_and_expired_headers_never_create_zero_usage() { + with_home(|_| { + let name = "anthropic-ratelimit-unified-5h-utilization"; + for value in ["NaN", "inf", "-0.1", "1.01", "garbage"] { + let mut headers = headers(); + headers.insert(name, value.parse().unwrap()); + record_anthropic_response_limits("fixture", Some("passive-invalid"), &headers); + assert!(disk_cache::fresh("label:passive-invalid").is_none()); + } + for name in [ + "anthropic-ratelimit-unified-5h-utilization", + "anthropic-ratelimit-unified-7d-utilization", + "anthropic-ratelimit-unified-5h-reset", + "anthropic-ratelimit-unified-7d-reset", + ] { + let mut headers = headers(); + headers.remove(name); + record_anthropic_response_limits("fixture", Some("passive-invalid"), &headers); + assert!(disk_cache::fresh("label:passive-invalid").is_none()); + } + let mut headers = headers(); + headers.insert("anthropic-ratelimit-unified-7d-reset", "1".parse().unwrap()); + record_anthropic_response_limits("fixture", Some("passive-invalid"), &headers); + assert!(disk_cache::fresh("label:passive-invalid").is_none()); + }); + } + + #[test] + fn first_failed_account_probe_keeps_the_shared_retry_after() { + with_home(|_| { + let key = "label:first-failure-policy"; + let original = + disk_cache::store_error_with_hint(key, "Usage API error (429)", Some("7200")); + let data = account_fetch::cache_anthropic_account_failure( + key.into(), + anyhow::anyhow!("Usage API error (429)"), + ); + assert!( + data.refresh_after.unwrap() + Duration::from_millis(10) + >= original.refresh_after.unwrap() + ); + assert!(data.last_success_at.is_none()); + let snapshot = + anthropic_snapshot_from_usage("first-failure-policy".into(), None, &data); + assert!(snapshot.error.unwrap().contains("retry at")); + assert!(snapshot.five_hour_ratio.is_none()); + assert!(snapshot.seven_day_ratio.is_none()); + }); + } + + #[test] + fn permanent_refresh_errors_are_actionable_but_network_errors_are_not_auth_failures() { + for message in [ + "OAuth token expired", + "invalid_grant: Refresh token not found or invalid", + "Claude OAuth refresh token was previously rejected by Anthropic", + ] { + let error = credential_error(anyhow::anyhow!(message)).to_string(); + assert!(error.contains("401"), "{error}"); + assert!(error.contains("/login claude"), "{error}"); + assert!(!error.contains("invalid_grant")); + } + for message in ["connection timed out", "HTTP 503", "HTTP 429"] { + assert_eq!( + credential_error(anyhow::anyhow!(message)).to_string(), + message + ); + } + } + + #[test] + fn expired_rejected_account_stops_refreshing_and_relogin_clears_auth_cache() { + with_home(|runtime| { + runtime.block_on(async { + let label = auth::claude::primary_account_label(); + store_anthropic_usage(format!("label:{label}"), UsageData::default()); + let tokens = auth::oauth::OAuthTokens { + access_token: "fixture-rejected-access".into(), + refresh_token: "fixture-rejected-refresh".into(), + expires_at: 1, + id_token: None, + scopes: vec![], + }; + auth::oauth::save_claude_tokens_for_account(&tokens, &label).unwrap(); + auth::refresh_state::record_permanent_rejection( + "claude", + &tokens.refresh_token, + "invalid_grant", + ) + .unwrap(); + // This call must return the local guard, not try the real token URL. + let error = + auth::oauth::refresh_claude_tokens_for_account(&tokens.refresh_token, &label) + .await + .unwrap_err(); + assert!(error.to_string().contains("previously rejected")); + for _ in 0..2 { + let accounts = + fetch_oauth_account_usage(MultiAccountProviderKind::Anthropic).await; + assert_eq!(accounts.len(), 1); + assert!(accounts[0].error.as_deref().unwrap().contains("401")); + assert!(accounts[0].five_hour_ratio.is_none()); + } + let key = format!("label:{label}"); + disk_cache::store_error(&key, "Usage API error (401 Unauthorized)"); + assert!(cached_anthropic_usage(&key).is_some()); + let new_tokens = auth::oauth::OAuthTokens { + access_token: "fixture-new-access".into(), + refresh_token: "fixture-new-refresh".into(), + expires_at: chrono::Utc::now().timestamp_millis() + 7200_000, + ..tokens + }; + auth::oauth::save_claude_tokens_for_account(&new_tokens, &label).unwrap(); + assert!(cached_anthropic_usage(&key).is_none()); + assert!(disk_cache::fresh(&key).is_none()); + // A separate login process cannot clear the daemon's RAM. The + // next read must notice that the shared auth failure was removed. + store_anthropic_usage( + key.clone(), + UsageData { + fetched_at: Some(Instant::now()), + last_error: Some("Usage API error (401)".into()), + ..Default::default() + }, + ); + assert!(cached_or_shared_anthropic_usage(&key).is_none()); + assert!(!auth::refresh_state::refresh_token_is_known_rejected( + "claude", + &new_tokens.refresh_token + )); + let restored = auth::oauth::refresh_claude_tokens_for_account( + "fixture-rejected-refresh", + &label, + ) + .await + .unwrap(); + assert_eq!(restored.access_token, new_tokens.access_token); + }) + }); + } +} diff --git a/crates/jcode-base/src/usage/cache.rs b/crates/jcode-base/src/usage/cache.rs index 0ed42f15ca9..a2d23b3f1e6 100644 --- a/crates/jcode-base/src/usage/cache.rs +++ b/crates/jcode-base/src/usage/cache.rs @@ -23,7 +23,7 @@ static OPENAI_ACCOUNT_USAGE_CACHE: std::sync::OnceLock< std::sync::Mutex>, > = std::sync::OnceLock::new(); -fn anthropic_usage_cache() -> &'static std::sync::Mutex> { +pub(super) fn anthropic_usage_cache() -> &'static std::sync::Mutex> { ANTHROPIC_USAGE_CACHE.get_or_init(|| std::sync::Mutex::new(HashMap::new())) } @@ -322,6 +322,76 @@ mod account_snapshot_tests { assert!(!stale.exhausted); anthropic_usage_cache().lock().unwrap().remove(&key); } + + #[test] + fn pool_prefers_fresh_quota_within_priority_tier() { + use crate::auth::account_pool::AccountPool; + let unknown = "claude:quota-order-unknown".to_string(); + let healthy = "claude:quota-order-healthy".to_string(); + let lower = "claude:quota-order-lower".to_string(); + let mut pool = AccountPool::default(); + pool.members = vec![unknown.clone(), healthy.clone(), lower.clone()]; + pool.set_priority(&unknown, 0); + pool.set_priority(&healthy, 0); + pool.set_priority(&lower, 1); + let key = "label:quota-order-healthy".to_string(); + let data = UsageData { + five_hour: 0.2, + fetched_at: Some(Instant::now()), + ..Default::default() + }; + store_anthropic_usage(key.clone(), data.clone()); + assert_eq!( + pool.ordered_members(), + vec![healthy.clone(), unknown.clone(), lower.clone()] + ); + let accounts = pool + .members + .iter() + .map(|key| (key.clone(), true)) + .collect::>(); + assert_eq!(pool.partition(&accounts).0[0], healthy); + assert_eq!( + pool.rotation( + "claude", + Some("quota-order-lower"), + &[ + "quota-order-unknown".into(), + "quota-order-healthy".into(), + "quota-order-lower".into() + ] + )[0], + "quota-order-healthy" + ); + pool.set_priority(&healthy, 1); + assert_eq!(pool.ordered_members()[0], unknown); + pool.set_priority(&healthy, 0); + for data in [ + UsageData { + last_error: Some("HTTP 429".into()), + ..data.clone() + }, + UsageData { + fetched_at: Some( + Instant::now() + - super::super::ANTHROPIC_CACHE_DURATION + - std::time::Duration::from_secs(1), + ), + ..data.clone() + }, + UsageData { + five_hour: 1.0, + seven_day: 1.0, + ..data.clone() + }, + ] { + store_anthropic_usage(key.clone(), data); + assert_eq!(pool.ordered_members()[0], unknown); + } + pool.excluded.push(healthy.clone()); + assert!(!pool.ordered_members().contains(&healthy)); + anthropic_usage_cache().lock().unwrap().remove(&key); + } } pub(super) fn invalidate_openai_usage_after_reset(access_token: &str, account_label: Option<&str>) { @@ -448,7 +518,25 @@ pub(super) fn cached_anthropic_usage(cache_key: &str) -> Option { /// Credential refresh callers must respect the shared backoff too, before /// attempting any network operation with an expired credential. pub(super) fn cached_or_shared_anthropic_usage(cache_key: &str) -> Option { - cached_anthropic_usage(cache_key).or_else(|| { + let cached = cached_anthropic_usage(cache_key); + if let Some(error) = cached.as_ref().and_then(|data| data.last_error.as_deref()) { + // Another process may have observed fresh model-response quotas or + // repaired the login while this daemon still holds an old failure. + if let Some(data) = super::disk_cache::fresh(cache_key) { + store_anthropic_usage(cache_key.to_string(), data.clone()); + return Some(data); + } + // Only a removed auth failure signifies a relogin. A missing file + // must not cancel the RAM copy of a provider's throttle deadline. + if super::anthropic_response::is_auth_failure(error) { + if let Ok(mut cache) = anthropic_usage_cache().lock() { + cache.remove(cache_key); + } + return None; + } + return cached; + } + cached.or_else(|| { let data = super::disk_cache::fresh(cache_key)?; store_anthropic_usage(cache_key.to_string(), data.clone()); Some(data) @@ -730,6 +818,16 @@ pub(super) fn openai_snapshot_from_usage( let exhausted = usage.exhausted(); AccountUsageSnapshot { + additional_windows: usage + .spark + .iter() + .chain(usage.additional_windows.iter()) + .map(|window| jcode_protocol::AccountUsageWindow { + label: window.name.trim_end_matches(" window").to_string(), + usage_ratio: window.usage_ratio, + resets_at: window.resets_at.clone(), + }) + .collect(), label, email, exhausted, @@ -783,6 +881,34 @@ pub(super) fn anthropic_snapshot_from_usage( let five_hour_ratio = ratio(usage.five_hour, usage.five_hour_resets_at.as_deref()); let seven_day_ratio = ratio(usage.seven_day, usage.seven_day_resets_at.as_deref()); AccountUsageSnapshot { + additional_windows: usage + .model_scoped + .iter() + .filter_map(|window| { + Some(jcode_protocol::AccountUsageWindow { + label: format!("{} 7d", window.model_name), + usage_ratio: ratio(window.utilization, window.resets_at.as_deref())?, + resets_at: window.resets_at.clone(), + }) + }) + .chain( + usage + .seven_day_opus + .filter(|_| { + !usage + .model_scoped + .iter() + .any(|window| window.model_name.eq_ignore_ascii_case("opus")) + }) + .and_then(|value| { + Some(jcode_protocol::AccountUsageWindow { + label: "Opus 7d".into(), + usage_ratio: ratio(value, usage.seven_day_resets_at.as_deref())?, + resets_at: usage.seven_day_resets_at.clone(), + }) + }), + ) + .collect(), label, email, exhausted: usage.last_error.is_none() @@ -797,7 +923,7 @@ pub(super) fn anthropic_snapshot_from_usage( resets_at: five_hour_ratio .and(usage.five_hour_resets_at.clone()) .or_else(|| seven_day_ratio.and(usage.seven_day_resets_at.clone())), - error: usage.last_error.clone(), + error: super::usage_error_with_retry(usage.last_error.clone(), usage.refresh_after), } } @@ -805,6 +931,63 @@ pub(super) fn anthropic_snapshot_from_usage( mod stale_account_snapshot_tests { use super::*; + #[test] + fn account_snapshot_preserves_all_model_and_future_provider_windows() { + let data = UsageData { + model_scoped: vec![ + super::super::ModelScopedUsageWindow { + model_name: "Fable".into(), + utilization: 0.73, + resets_at: None, + }, + super::super::ModelScopedUsageWindow { + model_name: "Future Zenith 界".into(), + utilization: 0.61, + resets_at: None, + }, + ], + seven_day_opus: Some(0.28), + ..Default::default() + }; + let snapshot = anthropic_snapshot_from_usage("models".into(), None, &data); + assert_eq!(snapshot.additional_windows.len(), 3); + assert_eq!(snapshot.additional_windows[0].label, "Fable 7d"); + assert_eq!(snapshot.additional_windows[0].usage_ratio, 0.73); + assert_eq!(snapshot.additional_windows[1].label, "Future Zenith 界 7d"); + assert_eq!(snapshot.additional_windows[1].usage_ratio, 0.61); + assert_eq!(snapshot.additional_windows[2].label, "Opus 7d"); + assert!(snapshot.summary().contains("Fable 7d 73%")); + assert!(snapshot.summary().contains("Future Zenith 界 7d 61%")); + + let limits = [ + "5-hour", + "7-day", + "Codex Spark", + "Future monthly", + "Future daily", + ] + .into_iter() + .map(|name| super::super::UsageLimit { + name: name.into(), + usage_percent: 32.0, + resets_at: None, + }) + .collect::>(); + let usage = classify_openai_limits(&limits); + assert_eq!(usage.additional_windows.len(), 2); + let snapshot = openai_snapshot_from_usage("future".into(), None, &usage); + assert_eq!(snapshot.additional_windows.len(), 3); + assert_eq!(snapshot.additional_windows[0].label, "Codex Spark"); + assert_eq!(snapshot.additional_windows[1].label, "Future monthly"); + assert_eq!(snapshot.additional_windows[2].label, "Future daily"); + assert!( + snapshot + .additional_windows + .iter() + .all(|window| window.usage_ratio == 0.32) + ); + } + fn last_good() -> UsageData { UsageData { five_hour: 1.0, @@ -832,6 +1015,40 @@ mod stale_account_snapshot_tests { assert!(!snapshot.exhausted); } + #[test] + fn additional_model_windows_filter_expiry_invalid_values_and_unproven_stale_data() { + let mut data = last_good(); + let future = (chrono::Utc::now() + chrono::Duration::hours(1)).to_rfc3339(); + let past = (chrono::Utc::now() - chrono::Duration::hours(1)).to_rfc3339(); + data.model_scoped = vec![ + super::super::ModelScopedUsageWindow { + model_name: "Fable".into(), + utilization: 0.8, + resets_at: Some(future), + }, + super::super::ModelScopedUsageWindow { + model_name: "Expired".into(), + utilization: 1.0, + resets_at: Some(past), + }, + super::super::ModelScopedUsageWindow { + model_name: "Invalid".into(), + utilization: f32::NAN, + resets_at: None, + }, + ]; + let snapshot = anthropic_snapshot_from_usage("test".into(), None, &data); + assert_eq!(snapshot.additional_windows.len(), 1); + assert_eq!(snapshot.additional_windows[0].label, "Fable 7d"); + assert!(!snapshot.exhausted); + data.last_success_at = None; + assert!( + anthropic_snapshot_from_usage("test".into(), None, &data) + .additional_windows + .is_empty() + ); + } + #[test] fn stale_limits_require_provenance_and_each_unexpired_valid_window() { let snapshot = |data: &UsageData| anthropic_snapshot_from_usage("test".into(), None, data); diff --git a/crates/jcode-base/src/usage/disk_cache.rs b/crates/jcode-base/src/usage/disk_cache.rs index 74b9442e201..34aa44c5cd4 100644 --- a/crates/jcode-base/src/usage/disk_cache.rs +++ b/crates/jcode-base/src/usage/disk_cache.rs @@ -36,6 +36,9 @@ struct Snapshot { #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] struct Entry { + /// Reset metadata from the same coordinated usage response. Contains no credentials. + #[serde(default, skip_serializing_if = "Option::is_none")] + reset_status: Option, last_good: Option, /// Latest failure and when it happened, for cross-process backoff. error: Option, @@ -238,6 +241,15 @@ fn entry_usage(entry: &Entry) -> UsageData { usage } +pub(super) fn reset_status(key: &str) -> Option { + let entry = load().remove(key)?; + if entry.error.is_some() || deadline(&entry)? <= now_ms() { + return None; + } + entry.last_good.as_ref()?; + entry.reset_status +} + pub(super) fn fresh(key: &str) -> Option { let entry = load().remove(key)?; (deadline(&entry)? > now_ms()).then(|| entry_usage(&entry)) @@ -250,7 +262,79 @@ pub(super) fn last_good(key: &str) -> Option { (age(good.fetched_unix_ms) < LAST_GOOD_MAX_AGE).then(|| to_usage(&good, None)) } +#[cfg(test)] pub(super) fn store_success(key: &str, data: &UsageData) -> UsageData { + store_success_with_reset_status(key, data, None) +} + +/// Model responses carry quota telemetry independently of the usage endpoint. +/// Refresh the two observed windows without treating old model-scoped quotas +/// as newly observed or forgetting learned backoff. +pub(super) fn store_response_limits( + key: &str, + five: (f32, String), + seven: (f32, String), +) -> UsageData { + let entry = update(key, |entry| { + // Freeze any outstanding probe deadline before replacing last_good. + entry.retry_not_before_ms = deadline(entry); + let extra_usage_enabled = entry + .last_good + .as_ref() + .is_some_and(|good| good.extra_usage_enabled); + entry.last_good = Some(Snapshot { + fetched_unix_ms: now_ms(), + five_hour: five.0, + five_hour_resets_at: Some(five.1), + seven_day: seven.0, + seven_day_resets_at: Some(seven.1), + extra_usage_enabled, + ..Default::default() + }); + entry.error = None; + entry.error_unix_ms = None; + // These headers did not observe reset eligibility or availability. + entry.reset_status = None; + }); + entry_usage(&entry) +} + +pub(super) fn clear_auth_failure(key: &str) { + // Avoid creating a cache entry or writing on every normal token refresh. + if !load() + .get(key) + .and_then(|entry| entry.error.as_deref()) + .is_some_and(super::anthropic_response::is_auth_failure) + { + return; + } + update(key, |entry| { + if entry + .error + .as_deref() + .is_some_and(super::anthropic_response::is_auth_failure) + { + // Preserve a longer provider deadline inherited from an earlier + // 429, but discard the ordinary authentication retry floor. + let auth_deadline = entry + .error_unix_ms + .unwrap_or_else(now_ms) + .saturating_add(ERROR_BACKOFF.as_millis() as i64); + entry.retry_not_before_ms = entry.retry_not_before_ms.filter(|at| *at > auth_deadline); + entry.error = None; + entry.error_unix_ms = None; + // A new login must not inherit the rejected login's quotas. + entry.last_good = None; + entry.reset_status = None; + } + }); +} + +pub(super) fn store_success_with_reset_status( + key: &str, + data: &UsageData, + reset_status: Option, +) -> UsageData { let snapshot = Snapshot { fetched_unix_ms: now_ms(), five_hour: data.five_hour, @@ -271,6 +355,7 @@ pub(super) fn store_success(key: &str, data: &UsageData) -> UsageData { entry.adaptive_interval_secs = RATE_LIMIT_BACKOFF.as_secs() * 2; } entry.last_good = Some(snapshot); + entry.reset_status = reset_status; entry.error = None; entry.error_unix_ms = None; // Do not clear learned slowdown or a provider's outstanding deadline. @@ -582,6 +667,33 @@ mod tests { assert!(openai.is_stale()); } + #[test] + fn new_credentials_clear_auth_errors_but_not_inherited_rate_limit_deadlines() { + with_home(|| { + let key = "label:relogin-policy"; + store_error_with_hint(key, "Usage API error (429)", Some("3600")); + let deadline = load()[key].retry_not_before_ms; + store_error(key, "Usage API error (401)"); + clear_auth_failure(key); + let entry = load().remove(key).unwrap(); + assert_eq!(entry.retry_not_before_ms, deadline); + assert_eq!(entry.adaptive_interval_secs, 1800); + assert!(entry.error.is_none()); + assert!(fresh(key).is_some()); + + let key = "label:relogin-auth-only"; + store_success(key, &sample()); + store_error(key, "Usage API error (401)"); + clear_auth_failure(key); + assert!(fresh(key).is_none()); + assert!( + load() + .get(key) + .is_none_or(|entry| entry.last_good.is_none()) + ); + }); + } + #[test] fn slowdown_survives_success_and_restart_with_matching_ram_deadline() { with_home(|| { diff --git a/crates/jcode-base/src/usage/display.rs b/crates/jcode-base/src/usage/display.rs index cd2492b0f6e..8da69197003 100644 --- a/crates/jcode-base/src/usage/display.rs +++ b/crates/jcode-base/src/usage/display.rs @@ -32,6 +32,9 @@ impl OpenAIUsageData { pub fn display_snapshot(&self) -> Self { let mut snapshot = self.clone(); let mut cleared_any_window = false; + snapshot + .additional_windows + .retain(|window| !reset_timestamp_passed(window.resets_at.as_deref())); if let Some(window) = snapshot.five_hour.as_mut() && reset_timestamp_passed(window.resets_at.as_deref()) diff --git a/crates/jcode-base/src/usage/flight_tests.rs b/crates/jcode-base/src/usage/flight_tests.rs index 0e504d5bbe9..79a5563f337 100644 --- a/crates/jcode-base/src/usage/flight_tests.rs +++ b/crates/jcode-base/src/usage/flight_tests.rs @@ -30,6 +30,55 @@ fn sample() -> UsageData { } } +#[test] +fn reset_offers_reuse_the_single_coordinated_usage_response() { + with_home(|runtime| { + runtime.block_on(async { + assert!(super::super::USAGE_URL.ends_with("?at_wall=1&skip_spend=1")); + for (index, status) in [ + serde_json::json!({"eligible": true, "arm": "reset", "available": true}), + serde_json::json!({"eligible": false}), + serde_json::Value::Null, + ].into_iter().enumerate() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}/usage?at_wall=1&skip_spend=1", listener.local_addr().unwrap()); + let body = serde_json::json!({ + "five_hour": {"utilization": 20.0}, + "seven_day": {"utilization": 30.0}, + "juniper_tide": status, + }).to_string(); + let server = tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.unwrap(); + let mut bytes = [0; 8192]; + assert!(stream.read(&mut bytes).await.unwrap() > 0); + stream.write_all(format!("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).as_bytes()).await.unwrap(); + }); + let label = format!("combined-reset-{index}"); + let key = format!("label:{label}"); + let results = futures::future::join_all((0..8).map(|_| { + fetch(key.clone(), || super::super::fetch_anthropic_usage_data_uncached("fixture-only".into(), key.clone(), &url)) + })).await; + assert!(results.iter().all(|result| result.as_ref().unwrap().last_error.is_none())); + server.await.unwrap(); + // The server is gone. Repeated offer reads must use persisted + // metadata, not make another request or invent availability. + for _ in 0..8 { + let (offer, ineligible) = super::super::anthropic_reset::cached_limit_reset_offer("fixture-only", Some(&label)); + assert_eq!(offer.is_some(), index == 0); + assert_eq!(ineligible, index == 1); + } + assert!(disk_cache::reset_status(&key).is_some()); + disk_cache::store_error_with_hint(&key, "Usage API error (429)", Some("3600")); + assert!(disk_cache::reset_status(&key).is_none()); + let (offer, ineligible) = super::super::anthropic_reset::cached_limit_reset_offer("fixture-only", Some(&label)); + assert!(offer.is_none()); + assert!(!ineligible); + fetch(key.clone(), || async { panic!("reset reads must not bypass the usage endpoint's shared 429 backoff") }).await.unwrap(); + } + }); + }); +} + #[test] fn response_diagnostics_capture_only_bounded_printable_retry_hints() { let mut log = RequestLog::new("label:diagnostics-test"); diff --git a/crates/jcode-base/src/usage/model.rs b/crates/jcode-base/src/usage/model.rs index 03aae370791..be423145d40 100644 --- a/crates/jcode-base/src/usage/model.rs +++ b/crates/jcode-base/src/usage/model.rs @@ -180,6 +180,8 @@ pub struct ModelScopedUsageWindow { /// API response structures #[derive(Deserialize, Debug)] pub(super) struct UsageResponse { + /// Read reset eligibility with the quota response, never a second poll. + pub juniper_tide: Option, pub(super) five_hour: Option, pub(super) seven_day: Option, pub(super) seven_day_opus: Option, @@ -231,6 +233,7 @@ pub struct OpenAIUsageWindow { /// Cached OpenAI/Codex usage snapshot for info widgets. #[derive(Debug, Clone, Default)] pub struct OpenAIUsageData { + pub additional_windows: Vec, pub five_hour: Option, pub seven_day: Option, pub spark: Option, @@ -324,7 +327,11 @@ impl OpenAIUsageData { self.five_hour.as_ref().and_then(|w| w.resets_at.as_deref()), self.seven_day.as_ref().and_then(|w| w.resets_at.as_deref()), self.spark.as_ref().and_then(|w| w.resets_at.as_deref()), - ]) { + ]) || self + .additional_windows + .iter() + .any(|window| usage_reset_passed([window.resets_at.as_deref()])) + { return true; } @@ -360,7 +367,10 @@ impl OpenAIUsageData { } pub fn has_limits(&self) -> bool { - self.five_hour.is_some() || self.seven_day.is_some() || self.spark.is_some() + self.five_hour.is_some() + || self.seven_day.is_some() + || self.spark.is_some() + || !self.additional_windows.is_empty() } } @@ -388,6 +398,7 @@ impl MultiAccountProviderKind { #[derive(Debug, Clone, PartialEq)] pub struct AccountUsageSnapshot { + pub additional_windows: Vec, pub label: String, pub email: Option, pub exhausted: bool, @@ -420,6 +431,13 @@ impl AccountUsageSnapshot { ratio * 100.0 )); } + for window in &self.additional_windows { + parts.push(format!( + "{} {:.0}%", + window.label, + window.usage_ratio * 100.0 + )); + } if let Some(reset) = &self.resets_at { parts.push(format!("resets {}", format_reset_time(reset))); } @@ -497,3 +515,79 @@ impl AccountUsageProbe { )) } } + +const RETRY_AT_MARKER: &str = "retry at "; + +/// Annotate a usage fetch error with when the next probe is allowed, so +/// throttled accounts can show a countdown instead of a bare failure. +pub fn usage_error_with_retry(error: Option, retry: Option) -> Option { + let error = error?; + let Some(retry) = retry else { + return Some(error); + }; + if error.contains(RETRY_AT_MARKER) { + return Some(error); + } + let wait = retry.saturating_duration_since(Instant::now()); + let Ok(wait) = chrono::Duration::from_std(wait) else { + // A deadline outside chrono's range must not erase the original error. + return Some(error); + }; + let at = chrono::Utc::now() + wait; + Some(format!( + "{error} ({RETRY_AT_MARKER}{})", + at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true) + )) +} + +/// RFC3339 retry time embedded by [`usage_error_with_retry`]. +pub fn usage_error_retry_at(error: &str) -> Option<&str> { + let start = error.find(RETRY_AT_MARKER)? + RETRY_AT_MARKER.len(); + let rest = &error[start..]; + let end = rest.find(')').unwrap_or(rest.len()); + let at = &rest[..end]; + match chrono::DateTime::parse_from_rfc3339(at) { + Ok(_) => Some(at), + Err(_) => None, // Malformed annotations are not usable retry deadlines. + } +} + +#[cfg(test)] +mod retry_hint_tests { + use super::*; + + #[test] + fn retry_hint_roundtrips() { + let error = usage_error_with_retry( + Some("HTTP 429".into()), + Instant::now().checked_add(std::time::Duration::from_secs(3000)), + ) + .unwrap(); + let at = usage_error_retry_at(&error).unwrap(); + let at = chrono::DateTime::parse_from_rfc3339(at).unwrap(); + let secs = at.signed_duration_since(chrono::Utc::now()).num_seconds(); + assert!((2990..=3001).contains(&secs), "{secs}"); + assert!(usage_error_retry_at("HTTP 429").is_none()); + assert_eq!( + usage_error_with_retry(Some("x".into()), None).as_deref(), + Some("x") + ); + } + + #[test] + fn malformed_retry_hints_are_not_deadlines() { + for error in [ + "HTTP 429 (retry at invalid)", + "retry at ", + "retry at 2026-99-99", + ] { + assert!(usage_error_retry_at(error).is_none(), "{error}"); + } + assert!(usage_error_with_retry(None, Some(Instant::now())).is_none()); + let annotated = "HTTP 429 (retry at 2026-11-10T12:00:00Z)"; + assert_eq!( + usage_error_with_retry(Some(annotated.into()), Some(Instant::now())).as_deref(), + Some(annotated) + ); + } +} diff --git a/crates/jcode-base/src/usage/openai_helpers.rs b/crates/jcode-base/src/usage/openai_helpers.rs index 23f3b227b09..bc388e857e5 100644 --- a/crates/jcode-base/src/usage/openai_helpers.rs +++ b/crates/jcode-base/src/usage/openai_helpers.rs @@ -132,7 +132,18 @@ pub(super) fn classify_openai_limits(limits: &[UsageLimit]) -> OpenAIUsageData { .cloned(); } + let additional_windows = limits + .iter() + .filter(|limit| { + ![five_hour.as_ref(), seven_day.as_ref(), spark.as_ref()] + .into_iter() + .flatten() + .any(|window| window.name == limit.name && window.resets_at == limit.resets_at) + }) + .map(to_openai_window) + .collect(); OpenAIUsageData { + additional_windows, five_hour, seven_day, spark, diff --git a/crates/jcode-base/src/usage/openai_reset.rs b/crates/jcode-base/src/usage/openai_reset.rs index 4c0b9957845..6f7ae0a1190 100644 --- a/crates/jcode-base/src/usage/openai_reset.rs +++ b/crates/jcode-base/src/usage/openai_reset.rs @@ -1,12 +1,105 @@ //! Banked Codex resets, using the same contract as openai/codex's backend-client //! `client/rate_limit_resets.rs`. These are earned, single-use resets, not API credits. -//! Preparation is read-only. Redemption always requires an explicit confirmation. +//! Preparation is read-only. Redemption requires confirmation or explicit auto-reset opt-in. use super::*; use serde::{Deserialize, Serialize}; const RESET_CREDITS_URL: &str = "https://chatgpt.com/backend-api/wham/rate-limit-reset-credits"; +/// Serialize automatic redemption across agents, then recheck quota under the +/// lock. A failed or uncertain POST must not spend another grant on retry. +pub(crate) async fn auto_reset_weekly_usage(account_label: Option) -> Result { + let credentials = match account_label.as_deref() { + Some(label) => auth::codex::load_credentials_for_account(label), + None => auth::codex::load_oauth_credentials(), + } + .context("Automatic banked resets require the selected OAuth account")?; + use sha2::{Digest, Sha256}; + let scope = format!( + "openai-auto-banked-reset:{}:{}", + credentials.account_id.as_deref().unwrap_or("default"), + account_label.as_deref().unwrap_or("default") + ); + let _shared = super::disk_cache::lock_probe(&scope).await?; + let receipts = crate::storage::jcode_dir()?.join("auto_reset_receipts"); + std::fs::create_dir_all(&receipts)?; + let receipt_path = receipts.join(format!( + "{}.json", + hex::encode(Sha256::digest(scope.as_bytes())) + )); + let now = chrono::Utc::now().timestamp(); + if let Some(restored) = recent_auto_reset(&receipt_path, now)? { + // Waiting agents may still hold a pre-reset error or cooldown. + if restored { + invalidate_openai_usage_reset_state(account_label.as_deref()).await; + } + return Ok(restored); + } + invalidate_openai_usage_cache(account_label.as_deref()).await; + let report = fetch_openai_usage_for_account( + "OpenAI (ChatGPT)".into(), + credentials, + account_label.as_deref(), + ) + .await; + let usage = openai_usage_data_from_provider_report(&report); + if !auto_reset_eligible(&usage, account_label.as_deref()) { + return Ok(false); + } + let Some(pending) = prepare_openai_usage_reset_for_account(account_label.clone()).await? else { + return Ok(false); + }; + // Persist before POST so cancellation or process exit cannot immediately + // redeem another grant. A malformed receipt fails closed on the next call. + write_auto_reset_receipt(&receipt_path, now, false)?; + let outcome = consume_openai_usage_reset(&pending).await?; + crate::logging::info(&format!("Automatic banked reset: {}", outcome.message())); + let restored = matches!(outcome.code, ResetCode::Reset | ResetCode::AlreadyRedeemed); + write_auto_reset_receipt(&receipt_path, now, restored)?; + Ok(restored) +} + +fn recent_auto_reset(path: &std::path::Path, now: i64) -> Result> { + match std::fs::read(path) { + Ok(bytes) => { + let (at, restored): (i64, bool) = serde_json::from_slice(&bytes) + .context("Invalid automatic reset receipt, refusing to spend another reset")?; + Ok((now.saturating_sub(at) < 300).then_some(restored)) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None), + Err(error) => Err(error.into()), + } +} + +fn write_auto_reset_receipt(path: &std::path::Path, now: i64, restored: bool) -> Result<()> { + use std::io::Write; + let mut options = std::fs::OpenOptions::new(); + options.create(true).write(true).truncate(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + let mut file = options.open(path)?; + file.write_all(&serde_json::to_vec(&(now, restored))?)?; + file.sync_all()?; + Ok(()) +} + +fn auto_reset_eligible(usage: &OpenAIUsageData, label: Option<&str>) -> bool { + usage + .seven_day + .as_ref() + .is_some_and(|window| window.usage_ratio >= 1.0) + && (usage.hard_limit_reached + || usage + .openai_reset_credits + .as_ref() + .is_some_and(|credits| credits.ordinary_usage_allowed == Some(false))) + && usage.banked_reset_available_for_account(label) +} + #[derive(Debug, Deserialize)] struct ResetCredits { available_count: u64, diff --git a/crates/jcode-base/src/usage/openai_reset/tests.rs b/crates/jcode-base/src/usage/openai_reset/tests.rs index e400e65bef8..9a6bc284e17 100644 --- a/crates/jcode-base/src/usage/openai_reset/tests.rs +++ b/crates/jcode-base/src/usage/openai_reset/tests.rs @@ -2,6 +2,84 @@ use super::*; use serde_json::{Value, json}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; +#[test] +fn automatic_reset_receipts_survive_retries_and_fail_closed_on_corruption() { + let root = tempfile::tempdir().unwrap(); + let path = root.path().join("receipt.json"); + assert_eq!(recent_auto_reset(&path, 1000).unwrap(), None); + write_auto_reset_receipt(&path, 1000, false).unwrap(); + assert_eq!(recent_auto_reset(&path, 1001).unwrap(), Some(false)); + assert_eq!(recent_auto_reset(&path, 1299).unwrap(), Some(false)); + assert_eq!(recent_auto_reset(&path, 1300).unwrap(), None); + write_auto_reset_receipt(&path, 1300, true).unwrap(); + assert_eq!(recent_auto_reset(&path, 1301).unwrap(), Some(true)); + // Clock rollback must not allow another spend. + assert_eq!(recent_auto_reset(&path, 1200).unwrap(), Some(true)); + std::fs::write(&path, b"interrupted write").unwrap(); + assert!(recent_auto_reset(&path, 1301).is_err()); +} + +fn exhausted_weekly_usage() -> OpenAIUsageData { + OpenAIUsageData { + seven_day: Some(OpenAIUsageWindow { + usage_ratio: 1.0, + ..Default::default() + }), + hard_limit_reached: true, + fetched_at: Some(Instant::now()), + openai_reset_credits: Some(OpenAiResetCredits { + available_count: 1, + available_expirations: vec![None], + account_label: Some("work".into()), + ordinary_usage_allowed: Some(false), + }), + ..Default::default() + } +} + +#[test] +fn automatic_reset_requires_enforced_weekly_limit_and_matching_account() { + let mut usage = exhausted_weekly_usage(); + assert!(auto_reset_eligible(&usage, Some("work"))); + assert!(!auto_reset_eligible(&usage, Some("personal"))); + assert!(!auto_reset_eligible(&usage, None)); + usage.seven_day.as_mut().unwrap().usage_ratio = 0.999; + assert!(!auto_reset_eligible(&usage, Some("work"))); + usage.seven_day = None; + usage.five_hour = Some(OpenAIUsageWindow { + usage_ratio: 1.0, + ..Default::default() + }); + assert!(!auto_reset_eligible(&usage, Some("work"))); +} + +#[test] +fn automatic_reset_rejects_rounded_allowed_unknown_stale_or_failed_usage() { + let mut usage = exhausted_weekly_usage(); + usage + .openai_reset_credits + .as_mut() + .unwrap() + .ordinary_usage_allowed = Some(true); + assert!(!auto_reset_eligible(&usage, Some("work"))); + usage + .openai_reset_credits + .as_mut() + .unwrap() + .ordinary_usage_allowed = None; + usage.hard_limit_reached = false; + assert!(!auto_reset_eligible(&usage, Some("work"))); + usage = exhausted_weekly_usage(); + usage.fetched_at = None; + assert!(!auto_reset_eligible(&usage, Some("work"))); + usage = exhausted_weekly_usage(); + usage.last_error = Some("network failure".into()); + assert!(!auto_reset_eligible(&usage, Some("work"))); + usage = exhausted_weekly_usage(); + usage.openai_reset_credits.as_mut().unwrap().available_count = 0; + assert!(!auto_reset_eligible(&usage, Some("work"))); +} + fn credentials() -> auth::codex::CodexCredentials { auth::codex::CodexCredentials { access_token: "test-access-secret".into(), diff --git a/crates/jcode-base/src/usage/provider_fetch.rs b/crates/jcode-base/src/usage/provider_fetch.rs index a1c422d389e..1124916fd7a 100644 --- a/crates/jcode-base/src/usage/provider_fetch.rs +++ b/crates/jcode-base/src/usage/provider_fetch.rs @@ -37,14 +37,11 @@ pub(super) async fn fetch_anthropic_usage_for_token( .await { Ok(refreshed) => refreshed.access_token, - Err(_) => { + Err(error) => { if expires_at < now_ms { return ProviderUsage { provider_name: display_name, - error: Some( - "OAuth token expired - use `/login claude` to re-authenticate" - .to_string(), - ), + error: Some(anthropic_response::credential_error(error).to_string()), ..Default::default() }; } @@ -72,11 +69,10 @@ pub(super) async fn fetch_anthropic_usage_for_token( .unwrap_or_default() .iter() .any(|account| account.label == account_label); - let (offer, ineligible) = super::anthropic_reset::fetch_limit_reset_offer( + let (offer, ineligible) = super::anthropic_reset::cached_limit_reset_offer( &access_token, stored.then_some(account_label.as_str()), - ) - .await; + ); super::banked::record_anthropic_offer(&account_label, offer.as_ref()); report.anthropic_limit_reset = offer; if ineligible { diff --git a/crates/jcode-base/src/usage/remote_accounts.rs b/crates/jcode-base/src/usage/remote_accounts.rs index 6f30ba399a1..4f579a33c45 100644 --- a/crates/jcode-base/src/usage/remote_accounts.rs +++ b/crates/jcode-base/src/usage/remote_accounts.rs @@ -5,6 +5,7 @@ use std::time::Instant; #[derive(Default)] struct RemoteAccounts { + generation: u64, received: Option, anthropic: Vec, openai: Vec, @@ -26,12 +27,28 @@ fn try_remote_lock( } /// Enter daemon-only mode without claiming that a response has arrived. -pub fn clear_remote_account_usage() { +pub fn clear_remote_account_usage() -> u64 { if let Ok(mut state) = REMOTE.get_or_init(Mutex::default).lock() { - *state = RemoteAccounts::default(); + let generation = state.generation.wrapping_add(1); + *state = RemoteAccounts { + generation, + ..Default::default() + }; + generation + } else { + 0 } } +/// Capture the current connection generation before reading advisory disk data. +pub(super) fn remote_account_usage_generation() -> u64 { + REMOTE + .get_or_init(Mutex::default) + .lock() + .map(|state| state.generation) + .unwrap_or(0) +} + /// An empty authoritative response is received; an initial reset is not. pub fn remote_account_usage_received() -> bool { REMOTE @@ -57,6 +74,7 @@ pub(super) fn set_remote_account_usage_with_age( ) { if let Ok(mut state) = REMOTE.get_or_init(Mutex::default).lock() { *state = RemoteAccounts { + generation: state.generation.wrapping_add(1), received: Instant::now().checked_sub(age), anthropic, openai, @@ -64,6 +82,36 @@ pub(super) fn set_remote_account_usage_with_age( } } +pub(super) fn import_remote_account_usage( + generation: u64, + anthropic: Vec, + openai: Vec, + age: std::time::Duration, +) -> bool { + let Ok(mut state) = REMOTE.get_or_init(Mutex::default).lock() else { + return false; + }; + state.import_snapshot(generation, anthropic, openai, age) +} + +impl RemoteAccounts { + fn import_snapshot( + &mut self, + generation: u64, + anthropic: Vec, + openai: Vec, + age: std::time::Duration, + ) -> bool { + if self.generation != generation || self.received.is_some() { + return false; + } + self.received = Instant::now().checked_sub(age); + self.anthropic = anthropic; + self.openai = openai; + true + } +} + /// Daemon-reported OAuth accounts `(anthropic, openai)`, once a response arrived. pub fn remote_account_list() -> Option<(Vec, Vec)> { let state = try_remote_lock(REMOTE.get()?)?; @@ -71,6 +119,7 @@ pub fn remote_account_list() -> Option<(Vec, Vec= age); + // Even an empty live response is authoritative over a startup hint. + state.received = Some(Instant::now()); + assert!(!state.import_snapshot(7, Vec::new(), Vec::new(), age)); + state.generation = 8; + state.received = None; + assert!(!state.import_snapshot(7, Vec::new(), Vec::new(), age)); + assert!(state.import_snapshot(8, Vec::new(), Vec::new(), age)); + } + #[test] fn connection_reset_is_not_an_empty_authoritative_response() { clear_remote_account_usage(); @@ -182,6 +252,11 @@ mod tests { .is_none() ); state.openai.push(jcode_protocol::AccountUsageSnapshot { + additional_windows: vec![jcode_protocol::AccountUsageWindow { + label: "Future monthly".into(), + usage_ratio: 0.23, + resets_at: None, + }], label: "remote".into(), email: None, exhausted: false, @@ -202,6 +277,13 @@ mod tests { .five_hour_ratio, Some(0.4) ); + assert_eq!( + state + .lookup(MultiAccountProviderKind::OpenAI, "remote", now) + .unwrap() + .additional_windows, + state.openai[0].additional_windows, + ); assert!( state .lookup(MultiAccountProviderKind::Anthropic, "remote", now) diff --git a/crates/jcode-base/src/usage/startup_snapshot.rs b/crates/jcode-base/src/usage/startup_snapshot.rs index 663ac028694..3c900586608 100644 --- a/crates/jcode-base/src/usage/startup_snapshot.rs +++ b/crates/jcode-base/src/usage/startup_snapshot.rs @@ -202,18 +202,25 @@ fn snapshot_age( /// stays unknown. Callers must skip this entirely for SSH-forwarded sockets. /// Returns true only when an actual (possibly empty) snapshot was imported. pub fn hydrate_remote_account_usage(socket: &Path) -> bool { + let generation = super::remote_accounts::remote_account_usage_generation(); + hydrate_remote_account_usage_for_generation(socket, generation) +} + +/// Read a startup hint off the async connection path. A live daemon reply or +/// another connection reset invalidates the generation while disk I/O runs. +pub fn hydrate_remote_account_usage_for_generation(socket: &Path, generation: u64) -> bool { let Some(socket) = normalized_socket(socket) else { return false; }; let Some((snapshot, age)) = read_snapshot(&socket, &cache_path(&socket)) else { return false; }; - super::remote_accounts::set_remote_account_usage_with_age( + super::remote_accounts::import_remote_account_usage( + generation, snapshot.anthropic, snapshot.openai, age, - ); - true + ) } fn read_snapshot(socket: &Path, path: &Path) -> Option<(Snapshot, Duration)> { @@ -232,6 +239,21 @@ fn read_snapshot(socket: &Path, path: &Path) -> Option<(Snapshot, Duration)> { fn discard_expired_windows(accounts: &mut [AccountUsageSnapshot], now_ms: u64) { for account in accounts { + account.additional_windows.retain(|window| { + !window + .resets_at + .as_deref() + .and_then(|reset| match chrono::DateTime::parse_from_rfc3339(reset) { + Ok(reset) => Some(reset), + Err(error) => { + crate::logging::debug(&format!( + "Keeping usage window with invalid reset timestamp: {error}" + )); + None + } + }) + .is_some_and(|reset| i128::from(reset.timestamp_millis()) <= i128::from(now_ms)) + }); let expired = account .resets_at .as_deref() @@ -309,6 +331,7 @@ mod tests { // Keep test artifacts inside the temporary directory, not global cache. publisher.path = dir.path().join("usage.json"); let account = AccountUsageSnapshot { + additional_windows: Vec::new(), label: "test".into(), email: Some("private@example.test".into()), exhausted: false, @@ -362,6 +385,28 @@ mod tests { #[test] fn reset_boundary_discards_exhaustion_but_preserves_error() { let mut accounts = vec![AccountUsageSnapshot { + additional_windows: vec![ + jcode_protocol::AccountUsageWindow { + label: "Fable".into(), + usage_ratio: 0.9, + resets_at: Some("2026-01-01T00:00:00Z".into()), + }, + jcode_protocol::AccountUsageWindow { + label: "Future".into(), + usage_ratio: 0.3, + resets_at: Some("2099-01-01T00:00:00Z".into()), + }, + jcode_protocol::AccountUsageWindow { + label: "Invalid reset".into(), + usage_ratio: 0.7, + resets_at: Some("not-a-timestamp".into()), + }, + jcode_protocol::AccountUsageWindow { + label: "Unknown reset".into(), + usage_ratio: 0.8, + resets_at: None, + }, + ], label: "test".into(), email: None, exhausted: true, @@ -379,8 +424,13 @@ mod tests { .unwrap() .timestamp_millis() as u64; discard_expired_windows(&mut accounts, reset - 1); + assert_eq!(accounts[0].additional_windows.len(), 4); assert!(accounts[0].exhausted); discard_expired_windows(&mut accounts, reset); + assert_eq!(accounts[0].additional_windows.len(), 3); + assert_eq!(accounts[0].additional_windows[1].label, "Invalid reset"); + assert_eq!(accounts[0].additional_windows[2].label, "Unknown reset"); + assert_eq!(accounts[0].additional_windows[0].label, "Future"); assert!(!accounts[0].exhausted); assert!(accounts[0].five_hour_ratio.is_none()); assert!(accounts[0].seven_day_ratio.is_none()); diff --git a/crates/jcode-base/src/usage/tests.rs b/crates/jcode-base/src/usage/tests.rs index 3cd983b82f3..8167a6c434e 100644 --- a/crates/jcode-base/src/usage/tests.rs +++ b/crates/jcode-base/src/usage/tests.rs @@ -670,6 +670,7 @@ fn test_account_usage_probe_prefers_best_available_alternative() { current_label: "work".to_string(), accounts: vec![ AccountUsageSnapshot { + additional_windows: Vec::new(), label: "work".to_string(), email: Some("work@example.com".to_string()), exhausted: true, @@ -681,6 +682,7 @@ fn test_account_usage_probe_prefers_best_available_alternative() { error: None, }, AccountUsageSnapshot { + additional_windows: Vec::new(), label: "backup".to_string(), email: Some("backup@example.com".to_string()), exhausted: false, @@ -692,6 +694,7 @@ fn test_account_usage_probe_prefers_best_available_alternative() { error: None, }, AccountUsageSnapshot { + additional_windows: Vec::new(), label: "secondary".to_string(), email: Some("secondary@example.com".to_string()), exhausted: false, @@ -722,6 +725,7 @@ fn test_account_usage_probe_detects_all_accounts_exhausted() { current_label: "primary".to_string(), accounts: vec![ AccountUsageSnapshot { + additional_windows: Vec::new(), label: "primary".to_string(), email: None, exhausted: true, @@ -733,6 +737,7 @@ fn test_account_usage_probe_detects_all_accounts_exhausted() { error: None, }, AccountUsageSnapshot { + additional_windows: Vec::new(), label: "backup".to_string(), email: None, exhausted: true, diff --git a/crates/jcode-build-meta/build.rs b/crates/jcode-build-meta/build.rs index 1601c807805..60634741af7 100644 --- a/crates/jcode-build-meta/build.rs +++ b/crates/jcode-build-meta/build.rs @@ -246,7 +246,20 @@ fn resolve_build_semver(base_version: (u32, u32, u32)) -> Result // turn invalidated `jcode-build-meta` and force-recompiled the entire crate // graph (base -> app-core -> tui -> cli). Deriving the value deterministically // keeps incremental rebuilds incremental. - let offset = commits_since_base_tag(base_version).unwrap_or(0); + let offset = commits_since_base_tag(base_version) + .or_else(|| { + let description = match std::env::var("JCODE_BUILD_GIT_TAG") { + Ok(description) => Some(description), + Err(std::env::VarError::NotPresent) => None, + Err(std::env::VarError::NotUnicode(_)) => { + println!("cargo:warning=Ignoring non-Unicode JCODE_BUILD_GIT_TAG"); + None + } + } + .or_else(|| metadata_value("git_tag"))?; + build_inputs::base_tag_offset(&description, base_version) + }) + .unwrap_or(0); let patch = base_version.2.saturating_add(offset); Ok(format!("{}.{}.{}", base_version.0, base_version.1, patch)) } diff --git a/crates/jcode-build-meta/build_inputs.rs b/crates/jcode-build-meta/build_inputs.rs index 0ca9509e900..de8bcfa9619 100644 --- a/crates/jcode-build-meta/build_inputs.rs +++ b/crates/jcode-build-meta/build_inputs.rs @@ -1,6 +1,28 @@ use std::ffi::OsStr; use std::path::Path; +/// Recover the base-tag distance forwarded by a source-only hosted build. +#[allow(dead_code)] +pub fn base_tag_offset(description: &str, base: (u32, u32, u32)) -> Option { + let tag = format!("v{}.{}.{}", base.0, base.1, base.2); + let description = description.trim(); + if description == tag { + return Some(0); + } + let suffix = description.strip_prefix(&format!("{tag}-"))?; + let (count, hash) = suffix.split_once("-g")?; + if count.is_empty() + || !count.bytes().all(|byte| byte.is_ascii_digit()) + || hash.is_empty() + || !hash.bytes().all(|byte| byte.is_ascii_hexdigit()) + { + return None; + } + count.bytes().try_fold(0u32, |offset, digit| { + offset.checked_mul(10)?.checked_add(u32::from(digit - b'0')) + }) +} + /// Accept release identities without accepting arbitrary environment text. pub fn release_semver(value: &str) -> Option { let value = value.trim().strip_prefix('v').unwrap_or(value.trim()); @@ -85,6 +107,24 @@ pub fn metadata_rerun_inputs(path: Option<&OsStr>) -> Vec { mod tests { use super::*; + #[test] + fn hosted_base_tag_distance() { + let base = (0, 99, 1); + assert_eq!(base_tag_offset("v0.99.1-42-gabc1234", base), Some(42)); + assert_eq!(base_tag_offset("v0.99.1-0-gabc1234", base), Some(0)); + assert_eq!(base_tag_offset("v0.99.1", base), Some(0)); + for invalid in [ + "v0.98.1-42-gabc1234", + "abc1234", + "v0.99.1-x-gabc", + "v0.99.1-42-g", + "v0.99.1-42-gxyz", + "v0.99.1-4294967296-gabc", + ] { + assert_eq!(base_tag_offset(invalid, base), None, "{invalid}"); + } + } + #[test] fn release_identity_accepts_stable_and_prereleases() { for value in [ diff --git a/crates/jcode-build-meta/src/lib.rs b/crates/jcode-build-meta/src/lib.rs index f7a5abc3cde..bd4e3c59d19 100644 --- a/crates/jcode-build-meta/src/lib.rs +++ b/crates/jcode-build-meta/src/lib.rs @@ -122,6 +122,21 @@ pub fn is_release_build() -> bool { mod tests { use super::parse_release_semver; + #[test] + fn forwarded_tag_distance_matches_compiled_dev_version() { + if option_env!("JCODE_BUILD_SEMVER").is_some() { + return; + } + let base = super::build_inputs::semver_core(super::BASE_SEMVER).unwrap(); + if let Some(offset) = super::build_inputs::base_tag_offset(super::GIT_TAG, base) { + assert_eq!( + super::SEMVER, + format!("{}.{}.{}", base.0, base.1, base.2.saturating_add(offset)) + ); + assert_eq!(super::UPDATE_SEMVER, super::BASE_SEMVER); + } + } + #[test] fn release_build_display_preserves_comparison_identity() { if option_env!("JCODE_RELEASE_BUILD").is_some() { diff --git a/crates/jcode-build-support/src/platform_support.rs b/crates/jcode-build-support/src/platform_support.rs index 07aaa7634d5..cb1935bc1ef 100644 --- a/crates/jcode-build-support/src/platform_support.rs +++ b/crates/jcode-build-support/src/platform_support.rs @@ -1,4 +1,17 @@ +// reference: https://doc.rust-lang.org/std/fs/fn.read_dir.html and +// https://doc.rust-lang.org/std/fs/fn.remove_file.html (Rust std directory +// enumeration and best-effort removal semantics used by the sweep helper). use std::path::Path; +#[cfg(windows)] +use std::time::{Duration, SystemTime}; + +/// Prefix of the aside backup created for a live launcher during a Windows swap. +#[cfg(windows)] +const LAUNCHER_BACKUP_PREFIX: &str = ".jcode-launcher-old-"; + +/// Backups older than this are considered abandoned and get swept away. +#[cfg(windows)] +const STALE_LAUNCHER_BACKUP_AGE: Duration = Duration::from_secs(60 * 60); /// Set file permissions to owner read/write/execute (0o755). /// No-op on Windows (executability is determined by file extension). @@ -40,6 +53,12 @@ pub fn atomic_symlink_swap(src: &Path, dst: &Path, temp: &Path) -> std::io::Resu let _ = std::fs::remove_file(temp); std::fs::copy(src, temp)?; + // Sweep leftovers from earlier publishes before adding a new one. + sweep_stale_launcher_backups( + dst.parent().unwrap_or_else(|| Path::new(".")), + STALE_LAUNCHER_BACKUP_AGE, + ); + let operation_id = format!( "{}-{}", std::process::id(), @@ -49,7 +68,7 @@ pub fn atomic_symlink_swap(src: &Path, dst: &Path, temp: &Path) -> std::io::Resu .unwrap_or_default() ); let old = dst.parent().unwrap_or_else(|| Path::new(".")).join(format!( - ".jcode-launcher-old-{operation_id}{}", + "{LAUNCHER_BACKUP_PREFIX}{operation_id}{}", dst.extension() .map(|extension| format!(".{}", extension.to_string_lossy())) .unwrap_or_default() @@ -75,7 +94,7 @@ pub fn atomic_symlink_swap(src: &Path, dst: &Path, temp: &Path) -> std::io::Resu } // An old loaded executable cannot be deleted until its process exits. - // Best-effort cleanup is safe; later installs can remove leftovers. + // Best-effort cleanup is safe; the sweep above removes older leftovers. if moved_old { let _ = std::fs::remove_file(old); } @@ -83,9 +102,74 @@ pub fn atomic_symlink_swap(src: &Path, dst: &Path, temp: &Path) -> std::io::Resu Ok(()) } +/// Delete stale `.jcode-launcher-old-*` backups left in `dir` by earlier swaps. +/// +/// A backup is only removed when it is older than `max_age`; a file still held +/// open by a running process fails to delete on Windows and is skipped. Every +/// failure is ignored: the sweep must never break a publish. +#[cfg(windows)] +fn sweep_stale_launcher_backups(dir: &Path, max_age: Duration) { + let Ok(entries) = std::fs::read_dir(dir) else { + return; + }; + let now = SystemTime::now(); + for entry in entries.flatten() { + let file_name = entry.file_name(); + let Some(file_name) = file_name.to_str() else { + continue; + }; + if !file_name.starts_with(LAUNCHER_BACKUP_PREFIX) { + continue; + } + let Ok(metadata) = entry.metadata() else { + continue; + }; + if !metadata.is_file() { + continue; + } + if !backup_is_stale(file_name, now, max_age) { + continue; + } + // Best-effort removal: a backup still held open by a running process + // fails to delete on Windows, and that must not abort the sweep. + if std::fs::remove_file(entry.path()).is_err() { + continue; + } + } +} + +/// Whether a launcher backup is old enough to be swept. +/// +/// The backup name carries the operation timestamp, so its age does not depend +/// on the mtime inherited from the renamed executable. +#[cfg(windows)] +fn backup_is_stale(file_name: &str, now: SystemTime, max_age: Duration) -> bool { + let Some(created_at) = backup_operation_time(file_name) else { + return false; + }; + match now.duration_since(created_at) { + Ok(age) => age >= max_age, + Err(_) => false, + } +} + +/// Extract the operation timestamp encoded in `.jcode-launcher-old--`. +#[cfg(windows)] +fn backup_operation_time(file_name: &str) -> Option { + let rest = file_name.strip_prefix(LAUNCHER_BACKUP_PREFIX)?; + let stem = rest.rsplit_once('.').map(|(stem, _)| stem).unwrap_or(rest); + let (_, nanos) = stem.rsplit_once('-')?; + let nanos: u64 = match nanos.parse() { + Ok(value) => value, + Err(_) => return None, + }; + Some(SystemTime::UNIX_EPOCH + Duration::from_nanos(nanos)) +} + #[cfg(all(test, windows))] mod tests { - use super::atomic_symlink_swap; + use super::{atomic_symlink_swap, sweep_stale_launcher_backups}; + use std::time::{Duration, SystemTime}; #[test] fn windows_swap_replaces_existing_launcher_via_staged_file() { @@ -106,4 +190,31 @@ mod tests { .exists() ); } + + #[test] + fn sweep_removes_only_old_launcher_backups() { + let dir = tempfile::tempdir().expect("temporary directory"); + let now_nanos = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("clock") + .as_nanos(); + let old_nanos = now_nanos - Duration::from_secs(2 * 60 * 60).as_nanos(); + + let old_backup = dir + .path() + .join(format!(".jcode-launcher-old-1234-{old_nanos}.exe")); + let fresh_backup = dir + .path() + .join(format!(".jcode-launcher-old-5678-{now_nanos}.exe")); + let unrelated = dir.path().join("jcode.exe"); + for path in [&old_backup, &fresh_backup, &unrelated] { + std::fs::write(path, b"payload").expect("fixture"); + } + + sweep_stale_launcher_backups(dir.path(), Duration::from_secs(60 * 60)); + + assert!(!old_backup.exists(), "stale backup must be removed"); + assert!(fresh_backup.exists(), "fresh backup must be kept"); + assert!(unrelated.exists(), "unrelated file must be kept"); + } } diff --git a/crates/jcode-companion/src/server.rs b/crates/jcode-companion/src/server.rs index ee594eeba77..38079d3321f 100644 --- a/crates/jcode-companion/src/server.rs +++ b/crates/jcode-companion/src/server.rs @@ -24,6 +24,52 @@ pub struct CompanionOptions { pub voice: bool, /// Periodically update an installed browser bridge. pub browser_updates: bool, + /// Live waveform in the focused Jcode CLI during push-to-talk. + pub voice_waveform: bool, + /// Desktop notifications for push-to-talk state. + pub voice_notifications: bool, + /// Handle `open` / `reveal` requests. + pub open: bool, + /// Handle `spawn_terminal` requests. + pub spawn_terminal: bool, + /// Handle `power_activity` (lid override). + pub lid: bool, +} + +impl CompanionOptions { + /// Options from `[companion]` in the config file. + pub fn from_config() -> Self { + let config = &jcode_base::config::config().companion; + Self { + socket: socket_path(), + voice: config.voice, + browser_updates: config.browser_updates, + voice_waveform: config.voice_waveform, + voice_notifications: config.voice_notifications, + open: config.open, + spawn_terminal: config.spawn_terminal, + lid: config.lid, + } + } + + /// Capabilities advertised in `status` for these options. + pub fn capabilities(&self) -> Vec { + let mut capabilities = Vec::new(); + if self.open { + capabilities.push("open".to_string()); + capabilities.push("reveal".to_string()); + } + if self.spawn_terminal { + capabilities.push("spawn_terminal".to_string()); + } + if self.lid && cfg!(any(target_os = "macos", windows)) { + capabilities.push("lid".to_string()); + } + if self.voice && cfg!(target_os = "linux") { + capabilities.push("voice".to_string()); + } + capabilities + } } impl Default for CompanionOptions { @@ -32,14 +78,23 @@ impl Default for CompanionOptions { socket: socket_path(), voice: true, browser_updates: true, + voice_waveform: true, + voice_notifications: true, + open: true, + spawn_terminal: true, + lid: true, } } } +type TerminalLauncher = + dyn Fn(jcode_base::terminal_launch::TerminalCommand, PathBuf) -> Result + Send + Sync; + /// Shared state for request handlers. pub struct Companion { lid: Mutex, capabilities: Vec, + terminal_launcher: Arc, } struct LidState { @@ -49,7 +104,12 @@ struct LidState { impl Companion { fn new(capabilities: Vec) -> Self { - let mut lid = LidOverride::for_current_platform(); + // A disabled lid feature must never touch the OS setting. + let mut lid = if capabilities.iter().any(|c| c == "lid") { + LidOverride::for_current_platform() + } else { + None + }; // Repair settings a crashed earlier owner left behind. if let Some(lid) = lid.as_mut() { lid.recover_stale(); @@ -60,10 +120,41 @@ impl Companion { active_until: None, }), capabilities, + terminal_launcher: Arc::new(|command, cwd| { + jcode_base::terminal_launch::spawn_command_in_new_terminal(&command, &cwd) + }), } } + fn has(&self, capability: &str) -> bool { + self.capabilities.iter().any(|c| c == capability) + } + pub async fn handle(&self, request: CompanionRequest) -> CompanionResponse { + // Disabled features answer as an error, so clients use their local + // fallback (open, terminals) or skip the effect (lid). + let required = match &request { + CompanionRequest::Status => None, + CompanionRequest::Open { .. } => Some("open"), + CompanionRequest::Reveal { .. } => Some("reveal"), + CompanionRequest::SpawnTerminal { .. } + | CompanionRequest::SpawnTerminalWithRuntime { .. } => Some("spawn_terminal"), + CompanionRequest::PowerActivity { .. } => { + if cfg!(any(target_os = "macos", windows)) { + Some("lid") + } else { + None + } + } + }; + if let Some(capability) = required + && !self.has(capability) + { + return CompanionResponse::error(format!( + "{capability} {}", + jcode_base::companion_client::DISABLED_SUFFIX + )); + } match request { CompanionRequest::Status => CompanionResponse { ok: true, @@ -82,39 +173,13 @@ impl Companion { Err(error) => CompanionResponse::error(format!("{error:#}")), } } - CompanionRequest::SpawnTerminal { - program, - args, - cwd, - title, - kind, - session_id, - env, - client_terminal_env, - } => { - let mut command = jcode_base::terminal_launch::TerminalCommand::new(program, args) - .client_terminal_env(client_terminal_env); - if let Some(title) = title { - command = command.title(title); - } - if let Some(kind) = kind { - command = command.kind(kind); - } - if let Some(session_id) = session_id { - command = command.session_id(session_id); - } - for (key, value) in env { - command = command.spawn_env(key, value); - } - let spawn = tokio::task::spawn_blocking(move || { - jcode_base::terminal_launch::spawn_command_in_new_terminal(&command, &cwd) - }) - .await; - match spawn { - Ok(Ok(true)) => CompanionResponse::ok("terminal"), - Ok(Ok(false)) => CompanionResponse::error("no terminal emulator found"), - Ok(Err(error)) => CompanionResponse::error(format!("{error:#}")), - Err(error) => CompanionResponse::error(error.to_string()), + request @ (CompanionRequest::SpawnTerminal { .. } + | CompanionRequest::SpawnTerminalWithRuntime { .. }) => { + match terminal_command_from_request(request) { + Some((command, cwd)) => self.launch_terminal(command, cwd).await, + None => { + CompanionResponse::error("spawn request did not carry a launchable command") + } } } CompanionRequest::PowerActivity { active } => { @@ -124,6 +189,21 @@ impl Companion { } } + async fn launch_terminal( + &self, + command: jcode_base::terminal_launch::TerminalCommand, + cwd: PathBuf, + ) -> CompanionResponse { + let terminal_launcher = self.terminal_launcher.clone(); + let spawn = tokio::task::spawn_blocking(move || terminal_launcher(command, cwd)).await; + match spawn { + Ok(Ok(true)) => CompanionResponse::ok("terminal"), + Ok(Ok(false)) => CompanionResponse::error("no terminal emulator found"), + Ok(Err(error)) => CompanionResponse::error(format!("{error:#}")), + Err(error) => CompanionResponse::error(error.to_string()), + } + } + fn set_power_active(&self, active: bool, now: Instant) { let mut state = self.lid.lock().unwrap_or_else(|p| p.into_inner()); state.active_until = active.then(|| now + POWER_LEASE); @@ -158,6 +238,86 @@ async fn open(target: &str) -> Result { jcode_desktop_open::open_target(&target).await } +fn terminal_command_from_parts( + mut command: jcode_base::terminal_launch::TerminalCommand, + title: Option, + kind: Option, + session_id: Option, + env: Vec<(String, String)>, +) -> jcode_base::terminal_launch::TerminalCommand { + if let Some(title) = title { + command = command.title(title); + } + if let Some(kind) = kind { + command = command.kind(kind); + } + if let Some(session_id) = session_id { + command = command.session_id(session_id); + } + for (key, value) in env { + command = command.spawn_env(key, value); + } + command +} + +fn terminal_command_from_request( + request: CompanionRequest, +) -> Option<(jcode_base::terminal_launch::TerminalCommand, PathBuf)> { + match request { + CompanionRequest::SpawnTerminal { + program, + args, + cwd, + title, + kind, + session_id, + env, + client_terminal_env, + } => Some(( + terminal_command_from_parts( + jcode_base::terminal_launch::TerminalCommand::new(program, args) + .client_terminal_env(client_terminal_env), + title, + kind, + session_id, + env, + ), + cwd, + )), + CompanionRequest::SpawnTerminalWithRuntime { + program, + args, + cwd, + title, + kind, + session_id, + env, + client_terminal_env, + runtime_env, + fresh_spawn, + } => Some(( + terminal_command_from_parts( + { + let command = jcode_base::terminal_launch::TerminalCommand::new(program, args) + .client_terminal_env(client_terminal_env) + .runtime_env(runtime_env); + if fresh_spawn { + command.fresh_spawn() + } else { + command + } + }, + title, + kind, + session_id, + env, + ), + cwd, + )), + _ => None, + } +} + /// Run the companion until SIGTERM/SIGINT. Fails if another companion already /// owns the socket. pub async fn run(options: CompanionOptions) -> Result<()> { @@ -169,17 +329,7 @@ pub async fn run(options: CompanionOptions) -> Result<()> { // Windows named-pipe acceptance replaces the listening pipe instance. #[cfg(windows)] let mut listener = listener; - let mut capabilities = vec![ - "open".to_string(), - "reveal".to_string(), - "spawn_terminal".to_string(), - ]; - if cfg!(any(target_os = "macos", windows)) { - capabilities.push("lid".to_string()); - } - if options.voice && cfg!(target_os = "linux") { - capabilities.push("voice".to_string()); - } + let capabilities = options.capabilities(); let companion = Arc::new(Companion::new(capabilities)); jcode_base::logging::info(&format!( "companion: listening on {}", @@ -189,7 +339,10 @@ pub async fn run(options: CompanionOptions) -> Result<()> { #[cfg(target_os = "linux")] if options.voice { - crate::voice::spawn(); + crate::voice::spawn(crate::voice::VoiceIndicators { + notifications: options.voice_notifications, + waveform: options.voice_waveform, + }); } if options.browser_updates { tokio::spawn(browser_bridge_updates()); @@ -332,21 +485,94 @@ mod tests { use super::*; fn companion() -> Companion { + companion_with_terminal_launcher(Arc::new(|_, _| Ok(true))) + } + + fn companion_with_terminal_launcher(terminal_launcher: Arc) -> Companion { Companion { lid: Mutex::new(LidState { lid: None, active_until: None, }), - capabilities: vec!["open".into()], + capabilities: vec!["open".into(), "reveal".into(), "spawn_terminal".into()], + terminal_launcher, } } + #[tokio::test] + async fn disabled_features_answer_as_unavailable_to_clients() { + let mut options = CompanionOptions { + open: false, + spawn_terminal: false, + lid: false, + voice: false, + ..Default::default() + }; + assert!(options.capabilities().is_empty()); + let disabled = Companion::new(options.capabilities()); + let response = disabled + .handle(CompanionRequest::Open { + target: "https://example.com".into(), + }) + .await; + assert!(!response.ok); + assert!( + response + .message + .ends_with(jcode_base::companion_client::DISABLED_SUFFIX) + ); + options.open = true; + options.spawn_terminal = true; + assert_eq!( + options.capabilities(), + vec!["open".to_string(), "reveal".into(), "spawn_terminal".into()] + ); + } + #[tokio::test] async fn status_reports_protocol_and_capabilities() { let response = companion().handle(CompanionRequest::Status).await; assert!(response.ok); assert_eq!(response.protocol, PROTOCOL_VERSION); - assert_eq!(response.capabilities, vec!["open".to_string()]); + assert_eq!(response.capabilities[0], "open"); + } + + #[test] + fn contextual_spawn_request_reconstructs_runtime_and_target_metadata() { + let request = CompanionRequest::SpawnTerminalWithRuntime { + program: "/usr/bin/jcode".into(), + args: vec!["--resume".into(), "ses_context".into()], + cwd: "/work".into(), + title: Some("context window".into()), + kind: Some("swarm-agent".into()), + session_id: Some("ses_context".into()), + env: vec![("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())], + client_terminal_env: vec![("TMUX_PANE".into(), "%12".into())], + runtime_env: vec![ + ("JCODE_SOCKET".into(), "/tmp/connected.sock".into()), + ("JCODE_API_KEY".into(), "drop".into()), + ], + fresh_spawn: true, + }; + let (command, cwd) = terminal_command_from_request(request).expect("terminal request"); + assert_eq!(command.program, PathBuf::from("/usr/bin/jcode")); + assert_eq!(command.args, ["--resume", "ses_context"]); + assert_eq!(command.title.as_deref(), Some("context window")); + assert_eq!(command.kind.as_deref(), Some("swarm-agent")); + assert_eq!(command.session_id.as_deref(), Some("ses_context")); + assert_eq!( + command.extra_env, + [("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())] + ); + assert_eq!( + command.client_terminal_env, + [("TMUX_PANE".into(), "%12".into())] + ); + assert_eq!( + command.runtime_env, + [("JCODE_SOCKET".into(), "/tmp/connected.sock".into())] + ); + assert_eq!(cwd, PathBuf::from("/work")); } #[tokio::test] @@ -404,6 +630,140 @@ mod tests { server.await.unwrap(); } + #[cfg(unix)] + #[tokio::test] + async fn contextual_spawn_crosses_socket_and_launches_with_filtered_runtime() { + async fn request(path: PathBuf, request: CompanionRequest) -> CompanionResponse { + tokio::task::spawn_blocking(move || { + jcode_base::companion_client::request_at(&path, &request, Duration::from_secs(2)) + }) + .await + .unwrap() + .unwrap() + } + + let dir = tempfile::Builder::new() + .prefix("jcode-companion-socket-test-") + .tempdir_in("/tmp") + .unwrap(); + let path = dir.path().join("contextual-companion.sock"); + let listener = bind(&path).unwrap(); + let captured = Arc::new(Mutex::new(Vec::new())); + let launcher_capture = captured.clone(); + let companion = Arc::new(companion_with_terminal_launcher(Arc::new( + move |command, cwd| { + launcher_capture.lock().unwrap().push((command, cwd)); + Ok(true) + }, + ))); + let server = { + let companion = companion.clone(); + tokio::spawn(async move { + for _ in 0..2 { + let (stream, _) = listener.accept().await.unwrap(); + serve(stream, &companion).await.unwrap(); + } + }) + }; + + let contextual = request( + path.clone(), + CompanionRequest::SpawnTerminalWithRuntime { + program: "/usr/bin/jcode".into(), + args: vec![ + "--resume".into(), + "ses_context".into(), + "--label".into(), + "work tree".into(), + ], + cwd: "/work/tree".into(), + title: Some("context window".into()), + kind: Some("swarm-agent".into()), + session_id: Some("ses_context".into()), + env: vec![("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())], + client_terminal_env: vec![("TMUX_PANE".into(), "%12".into())], + runtime_env: vec![ + ("JCODE_SOCKET".into(), "/tmp/new-pane.sock".into()), + ("JCODE_RUNTIME_DIR".into(), "/tmp/new-pane-runtime".into()), + ("JCODE_API_KEY".into(), "must-not-cross".into()), + ], + fresh_spawn: true, + }, + ) + .await; + assert!(contextual.ok, "{}", contextual.message); + + let legacy = request( + path.clone(), + CompanionRequest::SpawnTerminal { + program: "/usr/bin/jcode".into(), + args: vec!["--version".into()], + cwd: "/work/no-context".into(), + title: Some("plain window".into()), + kind: Some("manual".into()), + session_id: None, + env: vec![("JCODE_SPAWN_SWARM_ID".into(), "swarm-2".into())], + client_terminal_env: vec![("TMUX_PANE".into(), "%13".into())], + }, + ) + .await; + assert!(legacy.ok, "{}", legacy.message); + server.await.unwrap(); + + let captured = captured.lock().unwrap(); + assert_eq!(captured.len(), 2); + let (contextual, cwd) = &captured[0]; + assert_eq!(contextual.program, PathBuf::from("/usr/bin/jcode")); + assert_eq!( + contextual.args, + ["--resume", "ses_context", "--label", "work tree"] + ); + assert_eq!(cwd, &PathBuf::from("/work/tree")); + assert_eq!(contextual.title.as_deref(), Some("context window")); + assert_eq!(contextual.kind.as_deref(), Some("swarm-agent")); + assert_eq!(contextual.session_id.as_deref(), Some("ses_context")); + assert_eq!( + contextual.extra_env, + [("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())] + ); + assert_eq!( + contextual.client_terminal_env, + [("TMUX_PANE".into(), "%12".into())] + ); + assert!(contextual.fresh_spawn); + assert_eq!( + contextual.runtime_env, + [ + ("JCODE_SOCKET".into(), "/tmp/new-pane.sock".into()), + ("JCODE_RUNTIME_DIR".into(), "/tmp/new-pane-runtime".into()), + ] + ); + assert!( + !contextual + .runtime_env + .iter() + .any(|(key, _)| key == "JCODE_API_KEY") + ); + + let (legacy, cwd) = &captured[1]; + assert_eq!(legacy.program, PathBuf::from("/usr/bin/jcode")); + assert_eq!(legacy.args, ["--version"]); + assert_eq!(cwd, &PathBuf::from("/work/no-context")); + assert_eq!(legacy.title.as_deref(), Some("plain window")); + assert_eq!(legacy.kind.as_deref(), Some("manual")); + assert_eq!(legacy.session_id, None); + assert_eq!( + legacy.extra_env, + [("JCODE_SPAWN_SWARM_ID".into(), "swarm-2".into())] + ); + assert_eq!( + legacy.client_terminal_env, + [("TMUX_PANE".into(), "%13".into())] + ); + assert!(!legacy.fresh_spawn); + assert!(legacy.runtime_env.is_empty()); + } + #[cfg(unix)] #[tokio::test] async fn refuses_to_replace_a_live_companion() { diff --git a/crates/jcode-companion/src/voice.rs b/crates/jcode-companion/src/voice.rs index 28013270890..6da74d8b902 100644 --- a/crates/jcode-companion/src/voice.rs +++ b/crates/jcode-companion/src/voice.rs @@ -13,11 +13,13 @@ //! Never records without a visible indicator: a desktop notification is shown //! first, and recording is refused if it cannot be shown. +use jcode_base::protocol::VoiceActivity; use jcode_voice::global::hotkey::{Edge, Listener}; use jcode_voice::{NariEvent, NariRecording, VoiceError}; use std::process::{Command, Stdio}; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::mpsc; use std::time::{Duration, Instant}; const IDLE_POLL: Duration = Duration::from_millis(40); @@ -25,8 +27,22 @@ const HELD_POLL: Duration = Duration::from_millis(15); /// Compositor focus lookups are subprocesses, so refresh them less often. const FOCUS_REFRESH: Duration = Duration::from_millis(400); const SEND_TIMEOUT: Duration = Duration::from_secs(5); +/// Waveform update cadence to the focused TUI, matching its own meter tick. +const WAVEFORM_TICK: Duration = Duration::from_millis(80); +/// Enough samples to fill the TUI composer's waveform row (up to 48 cells). +const WAVEFORM_HISTORY: usize = 48; +const ACTIVITY_TIMEOUT: Duration = Duration::from_millis(500); -pub fn spawn() { +/// Which visible indicators companion push-to-talk uses. +#[derive(Clone, Copy, Debug)] +pub struct VoiceIndicators { + /// Desktop notifications (`notify-send`). + pub notifications: bool, + /// Live waveform in the focused Jcode CLI's status line. + pub waveform: bool, +} + +pub fn spawn(indicators: VoiceIndicators) { let devices = jcode_base::config::config() .dictation .global_devices @@ -37,6 +53,13 @@ pub fn spawn() { if std::env::var("JCODE_COMPANION_VOICE").as_deref() == Ok("0") { return; } + if !indicators.notifications && !indicators.waveform { + eprintln!( + "jcode companion: global voice off, it needs a visible indicator \ + ([companion] voice_notifications or voice_waveform)" + ); + return; + } let listener = match Listener::new(&devices) { Ok(listener) => listener, Err(error) => { @@ -51,7 +74,7 @@ pub fn spawn() { ); if let Err(error) = std::thread::Builder::new() .name("jcode-companion-voice".into()) - .spawn(move || run(listener)) + .spawn(move || run(listener, indicators)) { eprintln!("jcode companion: cannot start global voice worker: {error}"); } @@ -62,9 +85,15 @@ struct Capture { cancel: Arc, recording: NariRecording, indicator: Indicator, + activity: Option, + started: Instant, + levels: std::collections::VecDeque, + live: String, + next_tick: Instant, + pending_final: Option>, } -fn run(mut listener: Listener) { +fn run(mut listener: Listener, indicators: VoiceIndicators) { let mut focused: Option = None; let mut next_focus = Instant::now(); let mut capture: Option = None; @@ -91,7 +120,7 @@ fn run(mut listener: Listener) { if capture.is_none() && let Some(session) = focused.clone() { - capture = start(session); + capture = start(session, indicators); } } Edge::Release => { @@ -103,6 +132,9 @@ fn run(mut listener: Listener) { if let Some(active) = capture.take() { active.cancel.store(true, Ordering::SeqCst); active.indicator.show("Voice cancelled"); + if let Some(reporter) = &active.activity { + reporter.report(VoiceActivity::Idle); + } } } // Shift+Copilot opens a Desktop voice window. Without @@ -110,6 +142,9 @@ fn run(mut listener: Listener) { Edge::Spawn => {} } } + if let Some(active) = capture.as_mut() { + active.tick_waveform(); + } std::thread::sleep(if capture.is_some() { HELD_POLL } else { @@ -118,19 +153,31 @@ fn run(mut listener: Listener) { } } -fn start(session: String) -> Option { +fn start(session: String, indicators: VoiceIndicators) -> Option { if !block_on(jcode_voice::global::session::permitted()) { eprintln!("jcode companion: voice denied by session check"); return None; } let Some(key) = jcode_voice::nari_api_key() else { - Indicator::new().show("Voice not configured (NARI_API_KEY)"); + Indicator::new(true).show("Voice not configured (NARI_API_KEY)"); return None; }; - // Prove a visible indicator exists before opening the microphone. - let indicator = Indicator::new(); - if !indicator.show("Listening for Jcode…") { - eprintln!("jcode companion: no notification daemon, not recording"); + // Prove a visible indicator exists before opening the microphone: the + // focused TUI's waveform, or a desktop notification. + let activity = indicators + .waveform + .then(|| ActivityReporter::new(session.clone())); + let waveform_shown = activity.as_ref().is_some_and(|reporter| { + reporter.report_now(VoiceActivity::Recording { + elapsed_ms: 0, + levels: Vec::new(), + live: String::new(), + }) + }); + let indicator = Indicator::new(indicators.notifications); + let notified = indicators.notifications && indicator.show("Listening for Jcode…"); + if !waveform_shown && !notified { + eprintln!("jcode companion: no visible voice indicator, not recording"); return None; } let cancel = Arc::new(AtomicBool::new(false)); @@ -141,22 +188,36 @@ fn start(session: String) -> Option { cancel, recording, indicator, + activity, + started: Instant::now(), + levels: std::collections::VecDeque::with_capacity(WAVEFORM_HISTORY), + live: String::new(), + next_tick: Instant::now(), + pending_final: None, }), Err(error) => { indicator.show(&failure_label(&error)); + if let Some(reporter) = &activity { + reporter.report(VoiceActivity::Idle); + } None } } } /// Key-up: stop the microphone, wait for the final text, deliver it. -fn finish(capture: Capture) { +fn finish(mut capture: Capture) { capture.indicator.show("Transcribing…"); capture.recording.stop(); + if let Some(reporter) = &capture.activity { + reporter.report(VoiceActivity::Transcribing { + live: capture.live.clone(), + }); + } if let Err(error) = std::thread::Builder::new() .name("jcode-companion-voice-send".into()) .spawn(move || { - let result = wait_final(&capture.recording); + let result = wait_final(&mut capture); let label = match result { Ok(text) if text.trim().is_empty() => "No speech detected".to_string(), Ok(text) => { @@ -180,13 +241,114 @@ fn finish(capture: Capture) { Err(error) => failure_label(&error), }; capture.indicator.show(&label); + if let Some(reporter) = &capture.activity { + reporter.report(VoiceActivity::Idle); + } }) { eprintln!("jcode companion: cannot start voice delivery worker: {error}"); } } -fn wait_final(recording: &NariRecording) -> Result { +impl Capture { + /// Sample the microphone level and push the waveform to the focused TUI. + fn tick_waveform(&mut self) { + let Some(reporter) = &self.activity else { + return; + }; + let now = Instant::now(); + if now < self.next_tick { + return; + } + self.next_tick = now + WAVEFORM_TICK; + if self.levels.len() == WAVEFORM_HISTORY { + self.levels.pop_front(); + } + self.levels.push_back(self.recording.audio_level()); + // Live text: keep Finished for `wait_final`, which drains after stop. + while let Some(event) = self.recording.try_event() { + match event { + NariEvent::Transcript(text) => self.live = text, + NariEvent::Started => {} + NariEvent::Finished(result) => { + self.pending_final = Some(result); + break; + } + } + } + reporter.report(VoiceActivity::Recording { + elapsed_ms: self.started.elapsed().as_millis() as u64, + levels: self.levels.iter().copied().collect(), + live: self.live.clone(), + }); + } +} + +/// Sends activity updates off the key-polling thread, coalescing to the +/// latest state so a slow server never delays key handling. +struct ActivityReporter { + session: String, + tx: mpsc::Sender, +} + +impl ActivityReporter { + fn new(session: String) -> Self { + let (tx, rx) = mpsc::channel::(); + let target = session.clone(); + if let Err(error) = std::thread::Builder::new() + .name("jcode-companion-voice-activity".into()) + .spawn(move || { + while let Ok(mut activity) = rx.recv() { + while let Ok(newer) = rx.try_recv() { + // Never coalesce away a terminal state change. + if matches!(newer, VoiceActivity::Recording { .. }) + && !matches!(activity, VoiceActivity::Recording { .. }) + && let Err(error) = send_activity(&target, activity) + { + jcode_base::logging::debug(&format!( + "Cannot send companion voice activity: {error}" + )); + } + activity = newer; + } + if let Err(error) = send_activity(&target, activity) { + jcode_base::logging::debug(&format!( + "Cannot send companion voice activity: {error}" + )); + } + } + }) + { + jcode_base::logging::warn(&format!( + "Cannot start companion voice activity worker: {error}" + )); + } + Self { session, tx } + } + + fn report(&self, activity: VoiceActivity) { + if let Err(error) = self.tx.send(activity) { + jcode_base::logging::debug(&format!( + "Companion voice activity worker disconnected: {error}" + )); + } + } + + /// Synchronous send. Returns whether the server accepted it. + fn report_now(&self, activity: VoiceActivity) -> bool { + send_activity(&self.session, activity).is_ok() + } +} + +fn send_activity(session: &str, activity: VoiceActivity) -> anyhow::Result<()> { + jcode_voice::dictation::send_voice_activity_blocking(session, activity, ACTIVITY_TIMEOUT) +} + +fn wait_final(capture: &mut Capture) -> Result { + if let Some(result) = capture.pending_final.take() { + return result; + } + let recording = &capture.recording; let deadline = Instant::now() + Duration::from_secs(60); loop { while let Some(event) = recording.try_event() { @@ -224,18 +386,23 @@ fn block_on(future: F) -> F::Output { /// One desktop notification, updated in place (`notify-send -p` / `-r`). struct Indicator { + enabled: bool, id: std::sync::Mutex>, } impl Indicator { - fn new() -> Self { + fn new(enabled: bool) -> Self { Self { + enabled, id: std::sync::Mutex::new(None), } } /// Show or replace the notification. Returns whether it was displayed. fn show(&self, text: &str) -> bool { + if !self.enabled { + return false; + } let mut id = self.id.lock().unwrap_or_else(|p| p.into_inner()); let mut cmd = Command::new("notify-send"); cmd.args(["-a", "Jcode", "-p", "-u", "low", "Jcode voice", text]); diff --git a/crates/jcode-config-types/src/display.rs b/crates/jcode-config-types/src/display.rs index ce52e97ac86..c600639d9c4 100644 --- a/crates/jcode-config-types/src/display.rs +++ b/crates/jcode-config-types/src/display.rs @@ -100,15 +100,20 @@ pub struct DisplayConfig { #[serde(default = "default_true")] pub keybinding_hints: bool, /// Color theme: "auto" (detect terminal background), "dark", or "light". - /// Auto queries the terminal's background color (OSC 11) at startup and - /// adapts jcode's palette for light backgrounds. Default: auto. + /// Auto queries the terminal's background color (OSC 11) at startup, + /// periodically during the session, and on focus. Adapts jcode's palette + /// when the terminal switches light/dark mode. Default: auto. #[serde(default)] pub theme: String, /// Per-role color overrides, e.g. `user = "#8ab4f8"`. Any TUI color can be /// configured: the named roles are substituted directly, and ad hoc shades /// used by widgets follow the role they belong to. Run `/colors` to list /// roles and `/colors harmony` to score the result. - #[serde(default)] + /// + /// An empty map is not written: a bare `[display.colors]` header invites a + /// second one to be appended by hand, and TOML rejects the duplicate, which + /// makes the whole file unreadable. + #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")] pub colors: std::collections::BTreeMap, /// Opt-in active sessions manager: pressing Left arrow on an empty input /// opens a picker scoped to live (open) sessions, showing which are still @@ -131,9 +136,76 @@ pub struct DisplayConfig { /// within this many seconds in Resume (default: 30). Zero disables it. /// This uses socket-disconnect events, never transcript/message activity. pub resume_recent_close_seconds: u64, + /// Which timestamp Resume shows as "x ago" and sorts by + /// (message/activity/opened/created, default: message). + #[serde(deserialize_with = "crate::serde_lenient::lenient_enum")] + pub resume_time: ResumeTimeMode, + /// What Resume does when the picked session is already shown by another + /// local client window (off/move/replace, default: off). + #[serde(deserialize_with = "crate::serde_lenient::lenient_enum")] + pub resume_open_elsewhere: ResumeOpenElsewhere, /// Usage percentage wording: "left" (default) or "used". pub usage_display: String, } + +/// Resume behavior for a session another local client window already shows. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ResumeOpenElsewhere { + /// Attach a second client and leave the other window alone (default). + #[default] + #[serde(alias = "none", alias = "duplicate")] + Off, + /// Bring the existing window to the current workspace and focus it instead + /// of attaching again. Needs compositor support (niri); otherwise behaves + /// like `replace`. + #[serde(alias = "bring")] + Move, + /// Resume as usual and close the other window's client. + #[serde(alias = "close")] + Replace, +} + +impl ResumeOpenElsewhere { + pub fn parse(value: &str) -> Option { + match value.trim().to_ascii_lowercase().as_str() { + "off" | "none" | "duplicate" => Some(Self::Off), + "move" | "bring" => Some(Self::Move), + "replace" | "close" => Some(Self::Replace), + _ => None, + } + } +} + +/// Timestamp used for Resume's "x ago" label and recency ordering. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ResumeTimeMode { + /// Newest conversation message of any role: prompts, replies, tool + /// results (default). Opening a session without working does not count. + #[default] + #[serde(alias = "messages", alias = "last_message")] + Message, + /// Newest of the last message and the last open/resume. + Activity, + /// Last time the session was opened, resumed, or attached. + #[serde(alias = "active", alias = "resumed")] + Opened, + /// When the session was created. + Created, +} + +impl ResumeTimeMode { + pub fn parse(value: &str) -> Option { + match value.trim().to_ascii_lowercase().as_str() { + "message" | "messages" | "last_message" => Some(Self::Message), + "activity" => Some(Self::Activity), + "opened" | "active" | "resumed" => Some(Self::Opened), + "created" => Some(Self::Created), + _ => None, + } + } +} impl Default for DisplayConfig { fn default() -> Self { Self { @@ -174,6 +246,8 @@ impl Default for DisplayConfig { external_sessions: true, resume_project_scope: false, resume_recent_close_seconds: 30, + resume_time: ResumeTimeMode::default(), + resume_open_elsewhere: ResumeOpenElsewhere::default(), usage_display: "left".to_string(), } } @@ -247,6 +321,49 @@ mod tests { } } + #[test] + fn resume_time_defaults_to_message_and_parses_modes() { + use super::ResumeTimeMode; + let missing: DisplayConfig = serde_json::from_str("{}").unwrap(); + assert_eq!(missing.resume_time, ResumeTimeMode::Message); + for (raw, mode) in [ + ("message", ResumeTimeMode::Message), + ("activity", ResumeTimeMode::Activity), + ("opened", ResumeTimeMode::Opened), + ("created", ResumeTimeMode::Created), + ] { + let config: DisplayConfig = + serde_json::from_value(serde_json::json!({ "resume_time": raw })).unwrap(); + assert_eq!(config.resume_time, mode); + assert_eq!(ResumeTimeMode::parse(raw), Some(mode)); + } + let bogus: DisplayConfig = + serde_json::from_value(serde_json::json!({ "resume_time": "bogus" })).unwrap(); + assert_eq!(bogus.resume_time, ResumeTimeMode::Message); + } + + #[test] + fn resume_open_elsewhere_defaults_to_off_and_parses_modes() { + use super::ResumeOpenElsewhere; + let missing: DisplayConfig = serde_json::from_str("{}").unwrap(); + assert_eq!(missing.resume_open_elsewhere, ResumeOpenElsewhere::Off); + for (raw, mode) in [ + ("off", ResumeOpenElsewhere::Off), + ("move", ResumeOpenElsewhere::Move), + ("replace", ResumeOpenElsewhere::Replace), + ] { + let config: DisplayConfig = + serde_json::from_value(serde_json::json!({ "resume_open_elsewhere": raw })) + .unwrap(); + assert_eq!(config.resume_open_elsewhere, mode); + assert_eq!(ResumeOpenElsewhere::parse(raw), Some(mode)); + } + let bogus: DisplayConfig = + serde_json::from_value(serde_json::json!({ "resume_open_elsewhere": "bogus" })) + .unwrap(); + assert_eq!(bogus.resume_open_elsewhere, ResumeOpenElsewhere::Off); + } + #[test] fn recent_close_window_defaults_to_thirty_and_is_configurable() { let missing: DisplayConfig = serde_json::from_str("{}").unwrap(); diff --git a/crates/jcode-config-types/src/lib.rs b/crates/jcode-config-types/src/lib.rs index 723a8f98bbf..3e1d8c68df5 100644 --- a/crates/jcode-config-types/src/lib.rs +++ b/crates/jcode-config-types/src/lib.rs @@ -7,7 +7,11 @@ mod menubar; mod power; mod session; pub use session::{HibernationMode, SessionConfig}; -pub use {display::DisplayConfig, menubar::MenubarConfig, power::PowerConfig}; +pub use { + display::{DisplayConfig, ResumeOpenElsewhere, ResumeTimeMode}, + menubar::MenubarConfig, + power::PowerConfig, +}; pub mod keybindings; mod serde_lenient; pub use keybindings::{ @@ -1247,9 +1251,24 @@ pub struct ProviderConfig { pub preserve_reasoning_context: bool, /// How to handle cross-provider failover when the same input would be resent elsewhere. pub cross_provider_failover: CrossProviderFailoverMode, + /// Skip the cross-provider countdown for definitive exhausted usage only. + /// Manual cross-provider failover still disables automatic switching. + pub exhausted_usage_immediate_failover: bool, + /// Prefer bounded same-account retries for recoverable Anthropic rate limits + /// before rotating to another account. Long reset delays still yield. + pub anthropic_retry_recoverable_rate_limits: bool, /// Whether jcode should automatically try another account on the same provider /// before falling back to a different provider. pub same_provider_account_failover: bool, + /// Automatically redeem an earned OpenAI banked reset when weekly OAuth + /// usage is exhausted. Never purchases credits. Disabled by default. + pub openai_auto_banked_reset: bool, + /// Automatically redeem earned Anthropic Fable resets for exhausted weekly + /// OAuth usage. Never purchases credits. Disabled by default. + pub anthropic_auto_fable_reset: bool, + /// Preserve earned resets while the account pool router has another ready route. + /// Applies to both OpenAI and Anthropic automatic resets. Default: true. + pub auto_reset_only_when_no_routes: bool, /// Copilot premium request mode: "normal", "one", or "zero" /// "zero" means all requests are free (no premium requests consumed) pub copilot_premium: Option, @@ -1302,7 +1321,12 @@ impl Default for ProviderConfig { openai_native_compaction_threshold_tokens: 200_000, preserve_reasoning_context: true, cross_provider_failover: CrossProviderFailoverMode::Countdown, + exhausted_usage_immediate_failover: true, + anthropic_retry_recoverable_rate_limits: true, same_provider_account_failover: true, + openai_auto_banked_reset: false, + anthropic_auto_fable_reset: false, + auto_reset_only_when_no_routes: true, copilot_premium: None, gemini_force_oauth: false, gemini_project: None, @@ -1574,3 +1598,32 @@ pub struct LaunchHotkeysConfig { #[cfg(test)] #[path = "reasoning_display_defaults_tests.rs"] mod reasoning_display_defaults_tests; + +#[cfg(test)] +mod exhausted_usage_failover_tests { + use super::*; + + #[test] + fn exhaustion_immediate_defaults_on_and_is_independent_of_manual_mode() { + let defaults: ProviderConfig = serde_json::from_str("{}").unwrap(); + assert!(defaults.exhausted_usage_immediate_failover); + assert!(defaults.anthropic_retry_recoverable_rate_limits); + let rotate: ProviderConfig = + serde_json::from_str(r#"{"anthropic_retry_recoverable_rate_limits":false}"#).unwrap(); + assert!(!rotate.anthropic_retry_recoverable_rate_limits); + let countdown: ProviderConfig = + serde_json::from_str(r#"{"exhausted_usage_immediate_failover":false}"#).unwrap(); + assert!(!countdown.exhausted_usage_immediate_failover); + assert_eq!( + countdown.cross_provider_failover, + CrossProviderFailoverMode::Countdown + ); + let manual: ProviderConfig = + serde_json::from_str(r#"{"cross_provider_failover":"manual"}"#).unwrap(); + assert!(manual.exhausted_usage_immediate_failover); + assert_eq!( + manual.cross_provider_failover, + CrossProviderFailoverMode::Manual + ); + } +} diff --git a/crates/jcode-core/src/stdin_detect.rs b/crates/jcode-core/src/stdin_detect.rs index 46192c65b54..208203a5887 100644 --- a/crates/jcode-core/src/stdin_detect.rs +++ b/crates/jcode-core/src/stdin_detect.rs @@ -67,6 +67,7 @@ pub mod linux { || wchan == "pipe_wait_readable" || wchan == "unix_stream_read_generic") && stdin_is_pipe_or_pty(pid) + && stdin_is_only_blocking_input(pid) { return StdinState::Reading; } @@ -88,6 +89,52 @@ pub mod linux { false } + /// `wchan` only says the process is blocked reading *some* pipe, socket or + /// tty, not which fd. Commands commonly block on other inputs (cargo's + /// jobserver pipe, the read end of `a | b`, network sockets), which caused + /// false "waiting for input" pushes. Only trust wchan when fd 0 is the sole + /// readable pipe/socket/tty the process holds. + fn stdin_is_only_blocking_input(pid: u32) -> bool { + let Ok(entries) = std::fs::read_dir(format!("/proc/{}/fd", pid)) else { + return false; + }; + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(fd) = name.to_str() else { continue }; + if fd == "0" { + continue; + } + let Ok(link) = std::fs::read_link(entry.path()) else { + continue; + }; + let link = link.to_string_lossy(); + let blocking_kind = link.starts_with("pipe:") + || link.starts_with("socket:") + || link.contains("/pts/") + || link.contains("ptmx") + || link.starts_with("anon_inode:"); + if !blocking_kind { + continue; + } + if fd_is_readable(pid, fd) { + return false; + } + } + true + } + + fn fd_is_readable(pid: u32, fd: &str) -> bool { + let Ok(info) = std::fs::read_to_string(format!("/proc/{}/fdinfo/{}", pid, fd)) else { + return true; + }; + info.lines() + .find_map(|l| l.strip_prefix("flags:")) + .and_then(|f| u32::from_str_radix(f.trim(), 8).ok()) + // O_ACCMODE: 0 = O_RDONLY, 1 = O_WRONLY, 2 = O_RDWR + .map(|flags| flags & 0o3 != 1) + .unwrap_or(true) + } + /// Check all threads in a process group (for cases where a child is the one reading) pub fn check_process_tree(pid: u32) -> StdinState { // Check the process itself @@ -204,7 +251,7 @@ pub mod linux { { for line in status.lines() { if let Some(ppid_str) = line.strip_prefix("PPid:\t") - && ppid_str.trim().parse::().ok() == Some(pid) + && ppid_str.trim().parse::() == Ok(pid) { children.push(child_pid); break; diff --git a/crates/jcode-harness-api-server/Cargo.toml b/crates/jcode-harness-api-server/Cargo.toml index 863d01dfe41..578e182429e 100644 --- a/crates/jcode-harness-api-server/Cargo.toml +++ b/crates/jcode-harness-api-server/Cargo.toml @@ -13,7 +13,7 @@ jcode-session-types = { path = "../jcode-session-types" } jcode-transport = { path = "../jcode-transport" } serde = "1" serde_json = "1" -rusqlite = { version = "0.41", features = ["bundled"] } +rusqlite = { version = "0.41", features = ["bundled", "fallible_uint"] } tokio = { version = "1", features = ["net", "io-util", "io-std", "rt-multi-thread", "macros", "sync", "time"] } [target.'cfg(unix)'.dependencies] diff --git a/crates/jcode-harness-api-server/src/lib.rs b/crates/jcode-harness-api-server/src/lib.rs index f40e95080a8..72615e4d421 100644 --- a/crates/jcode-harness-api-server/src/lib.rs +++ b/crates/jcode-harness-api-server/src/lib.rs @@ -18,6 +18,7 @@ pub mod allocator; pub mod background_progress; +pub mod timing; pub mod translate; // `session/list` asks for the stored-session catalog; it lives under @@ -328,6 +329,7 @@ where { let mut reader = BufReader::new(read_half); let mut line = String::new(); + let mut pending = timing::Pending::default(); // 1. Handshake: first frame must be hello with a compatible version. read_frame(&mut reader, &mut line).await?; @@ -365,13 +367,21 @@ where write_json_line(&mut write_half, &frame).await?; return Ok(()); } + let hello_at = std::time::Instant::now(); // Do not claim a usable connection before the native daemon is reachable. let legacy = Stream::connect(&legacy_socket) .await .with_context(|| format!("connect legacy socket {}", legacy_socket.display()))?; + let daemon_connected = hello_at.elapsed(); let daemon_caps = daemon_capabilities(&legacy_socket) .await .unwrap_or_default(); + timing::log(&format!( + "hello client={client_name} first_frame={:.1}ms daemon_connect={:.1}ms daemon_caps={:.1}ms", + timing::ms(pending.connection_age() - hello_at.elapsed()), + timing::ms(daemon_connected), + timing::ms(hello_at.elapsed() - daemon_connected), + )); let user_connections_supported = daemon_caps.iter().any(|cap| cap == "session_connections"); let runtime_commands_supported = daemon_caps.iter().any(|cap| cap == "runtime_commands"); let session_tools_supported = daemon_caps.iter().any(|cap| cap == "session_tools"); @@ -458,6 +468,7 @@ where let parsed = serde_json::from_slice(&api_frame); api_frame.clear(); release_large_frame(&mut api_frame); + let received = std::time::Instant::now(); let request: Value = match parsed { Ok(value) => value, Err(error) => { @@ -501,9 +512,18 @@ where // Translation may inspect persisted session/archive files. Tell // Tokio before entering that synchronous region so it can keep // the accept loop and fresh-client handshakes scheduled. + let translate_started = std::time::Instant::now(); let outbound = tokio::task::block_in_place(|| { state.api_request_to_legacy(&request) }); + if let Some(id) = request["id"].as_u64() { + pending.begin( + id, + request["req"].as_str().unwrap_or("?"), + received, + translate_started.elapsed(), + ); + } let heavy = allocator::request_is_heavy(&request); drop(request); for out in outbound { @@ -512,7 +532,7 @@ where write_json_line(&mut legacy_write, &value).await?; } translate::Outbound::Reply(frame) => { - write_json_line(&mut write_half, &frame).await?; + write_frame_timed(&mut write_half, &frame, &mut pending, "bridge").await?; } } } @@ -548,14 +568,23 @@ where // block_in_place handed the worker core to a fresh blocking // thread per event: a busy session grew the bridge to 69 idle // threads, each pinning its own allocator arena. + let translate_started = std::time::Instant::now(); let frames = if translate::legacy_event_may_block(&event) { tokio::task::block_in_place(|| state.legacy_event_to_api(&event)) } else { state.legacy_event_to_api(&event) }; + let translated = translate_started.elapsed(); + if translated > timing::SLOW_TRANSLATE { + timing::log(&format!( + "slow_event {} translate={:.1}ms", + event["type"].as_str().unwrap_or("?"), + timing::ms(translated), + )); + } drop(event); for frame in frames { - write_json_line(&mut write_half, &frame).await?; + write_frame_timed(&mut write_half, &frame, &mut pending, "daemon").await?; } if large { release_large_frame(&mut legacy_frame); @@ -565,6 +594,32 @@ where } } +/// Write a frame and, if it answers a timed request, log that request. +async fn write_frame_timed( + writer: &mut W, + frame: &ServerFrame, + pending: &mut timing::Pending, + source: &str, +) -> Result<()> +where + W: AsyncWriteExt + Unpin, +{ + let mut line = serde_json::to_string(frame)?; + line.push('\n'); + writer.write_all(line.as_bytes()).await?; + writer.flush().await?; + if let Some(reply_to) = frame.reply_to { + // Avoid a second serialization of large replies just for the name. + // Missing event names remain empty timing labels, not request failures. + let event = line + .split_once("\"ev\":\"") + .and_then(|(_, rest)| rest.split('"').next()) + .unwrap_or(""); + pending.finish(reply_to, event, line.len(), source); + } + Ok(()) +} + async fn write_json_line(writer: &mut W, value: &T) -> Result<()> where W: AsyncWriteExt + Unpin, diff --git a/crates/jcode-harness-api-server/src/timing.rs b/crates/jcode-harness-api-server/src/timing.rs new file mode 100644 index 00000000000..a800fab3591 --- /dev/null +++ b/crates/jcode-harness-api-server/src/timing.rs @@ -0,0 +1,106 @@ +//! Always-on, low-volume request timing for the API bridge. +//! +//! One line per client request when its reply is written, plus slow event +//! translations, appended to `$JCODE_HOME/logs/api-timing.log`. Bridges run +//! over SSH stdio have no useful stderr, so this goes to a file. Never logs +//! session content, only request kinds, ids, durations and byte counts. +use std::collections::HashMap; +use std::io::Write; +use std::sync::Mutex; +use std::time::{Duration, Instant}; + +const MAX_BYTES: u64 = 4 * 1024 * 1024; +/// Translations slower than this are logged even without a request. +pub const SLOW_TRANSLATE: Duration = Duration::from_millis(20); + +fn path() -> Option { + let home = std::env::var_os("JCODE_HOME") + .map(std::path::PathBuf::from) + .or_else(|| std::env::var_os("HOME").map(|h| std::path::Path::new(&h).join(".jcode")))?; + Some(home.join("logs").join("api-timing.log")) +} + +pub fn log(line: &str) { + static FILE: Mutex> = Mutex::new(None); + let Some(path) = path() else { return }; + let Ok(mut guard) = FILE.lock() else { return }; + if guard.is_none() { + if let Some(dir) = path.parent() + && let Err(error) = std::fs::create_dir_all(dir) + { + eprintln!("Cannot create API timing log directory: {error}"); + } + // Keep one previous generation instead of growing forever. + if std::fs::metadata(&path).is_ok_and(|m| m.len() > MAX_BYTES) + && let Err(error) = std::fs::rename(&path, path.with_extension("log.1")) + { + eprintln!("Cannot rotate API timing log: {error}"); + } + *guard = match std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(&path) + { + Ok(file) => Some(file), + Err(error) => { + eprintln!("Cannot open API timing log: {error}"); + None + } + }; + } + if let Some(file) = guard.as_mut() { + let ms = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_millis()) + .unwrap_or(0); + if let Err(error) = writeln!(file, "{ms} pid={} {line}", std::process::id()) { + eprintln!("Cannot write API timing log: {error}"); + } + } +} + +pub fn ms(d: Duration) -> f64 { + d.as_secs_f64() * 1000.0 +} + +/// Requests awaiting their reply on one connection. +#[derive(Default)] +pub struct Pending { + started: Instant_, + inflight: HashMap, +} + +struct Instant_(Instant); +impl Default for Instant_ { + fn default() -> Self { + Self(Instant::now()) + } +} + +impl Pending { + pub fn connection_age(&self) -> Duration { + self.started.0.elapsed() + } + + /// Note a request; `translate` is the synchronous bridge time spent on it. + pub fn begin(&mut self, id: u64, kind: &str, received: Instant, translate: Duration) { + if self.inflight.len() > 1024 { + self.inflight.clear(); + } + self.inflight + .insert(id, (kind.to_string(), received, translate)); + } + + /// A reply was written. `source` is "bridge" (answered locally) or + /// "daemon" (came back over the legacy socket). + pub fn finish(&mut self, reply_to: u64, event: &str, bytes: usize, source: &str) { + if let Some((kind, received, translate)) = self.inflight.remove(&reply_to) { + log(&format!( + "req {kind} id={reply_to} total={:.1}ms translate={:.1}ms reply={event} bytes={bytes} via={source} conn_age={:.0}ms", + ms(received.elapsed()), + ms(translate), + ms(self.connection_age()), + )); + } + } +} diff --git a/crates/jcode-harness-api-server/src/translate.rs b/crates/jcode-harness-api-server/src/translate.rs index 4f5671037dd..520a92de10d 100644 --- a/crates/jcode-harness-api-server/src/translate.rs +++ b/crates/jcode-harness-api-server/src/translate.rs @@ -1395,6 +1395,17 @@ impl BridgeState { } } + /// Tool identity of a daemon `tool` history row, if it carries one. + fn history_tool(message: &Value) -> Option { + let data = &message["tool_data"]; + let id = data["id"].as_str().filter(|id| !id.is_empty())?; + (message["role"] == "tool").then(|| jcode_harness_api::HistoryTool { + id: id.to_string(), + name: data["name"].as_str().unwrap_or("").to_string(), + input: data["input"].clone(), + }) + } + fn attachment_recovery(event: &Value) -> Option { if event["messages"].as_array().is_none_or(Vec::is_empty) || event["activity"]["is_processing"].as_bool() == Some(true) @@ -2307,6 +2318,7 @@ impl BridgeState { .unwrap_or(None), role: m["role"].as_str().unwrap_or("").to_string(), content: m["content"].as_str().unwrap_or("").to_string(), + tool: Self::history_tool(m), }) .collect() }) @@ -3466,6 +3478,7 @@ impl BridgeState { response_stats: None, role: role.to_string(), content, + tool: None, }) }) .take(limit) diff --git a/crates/jcode-harness-api-server/src/translate_tests.rs b/crates/jcode-harness-api-server/src/translate_tests.rs index 3fd10d296c3..aa3172463bf 100644 --- a/crates/jcode-harness-api-server/src/translate_tests.rs +++ b/crates/jcode-harness-api-server/src/translate_tests.rs @@ -473,6 +473,29 @@ fn wake_request_is_forwarded_with_explicit_session_and_payload() { ); } +#[test] +fn api_sessions_enable_server_account_routing_on_create_attach_and_reconnect() { + let mut state = BridgeState::default(); + for request in [ + json!({"req": "create_session", "id": 1}), + json!({"req": "attach_session", "id": 2, "session_id": "s1"}), + json!({"req": "attach_session", "id": 3, "session_id": "s1"}), + ] { + let outbound = state.api_request_to_legacy(&request); + let subscription = outbound + .iter() + .find_map(|outbound| match outbound { + Outbound::Legacy(value) if value["type"] == "subscribe" => Some(value), + _ => None, + }) + .expect("every attachment must subscribe to the daemon"); + assert_eq!( + subscription["supports_server_failover"], true, + "API clients must route exhausted accounts rather than retry the same account: {request}" + ); + } +} + #[test] fn create_session_maps_to_subscribe() { let mut state = BridgeState::default(); @@ -3250,6 +3273,12 @@ fn history_image_boundaries_survive_loss_of_tool_data() { }; assert_eq!(messages.len(), 3); assert_eq!(messages[1].role, "tool"); + let tool = messages[1] + .tool + .as_ref() + .expect("tool rows carry their call"); + assert_eq!((tool.id.as_str(), tool.name.as_str()), ("read-1", "read")); + assert!(messages[0].tool.is_none()); assert_eq!(images.len(), 1); assert_eq!(images[0].history_message_index, Some(2)); assert_eq!( diff --git a/crates/jcode-harness-api/src/events.rs b/crates/jcode-harness-api/src/events.rs index d76b38646a0..51e6bf2ebce 100644 --- a/crates/jcode-harness-api/src/events.rs +++ b/crates/jcode-harness-api/src/events.rs @@ -588,6 +588,19 @@ pub struct HistoryMessage { /// "user" | "assistant" | "tool". pub role: String, pub content: String, + /// On "tool" rows: the call this result answers, so clients can rebuild + /// tool cards after a reload. `content` is the tool output. Absent from + /// older servers. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub tool: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct HistoryTool { + pub id: String, + pub name: String, + #[serde(default, skip_serializing_if = "serde_json::Value::is_null")] + pub input: serde_json::Value, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] diff --git a/crates/jcode-protocol/src/lib.rs b/crates/jcode-protocol/src/lib.rs index 29c22591e67..c2c79e8e795 100644 --- a/crates/jcode-protocol/src/lib.rs +++ b/crates/jcode-protocol/src/lib.rs @@ -71,6 +71,30 @@ pub enum TranscriptMode { Send, } +/// Push-to-talk state captured outside the TUI (for example `jcode companion`), +/// shown in the target session's status line. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(tag = "phase", rename_all = "snake_case")] +pub enum VoiceActivity { + /// Microphone open. `levels` holds recent RMS levels (`0.0..=1.0`), + /// oldest first. `live` is the partial transcript so far. + Recording { + #[serde(default)] + elapsed_ms: u64, + #[serde(default)] + levels: Vec, + #[serde(default)] + live: String, + }, + /// Key released, waiting for the final transcript. + Transcribing { + #[serde(default)] + live: String, + }, + /// Capture ended (sent, cancelled or failed). Clears the indicator. + Idle, +} + #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum CommDeliveryMode { @@ -91,6 +115,26 @@ pub struct HistoryBase { /// When it matches, History omits `images` and sets `images_unchanged`. #[serde(default, skip_serializing_if = "Option::is_none")] pub images_fingerprint: Option, + /// Opt into a recent rendered-history window on a full bootstrap. Only + /// honored when `count` is zero, never combined with prefix validation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recent_messages: Option, +} + +/// Synthetic lazy-window marker used by remote transcript bootstraps. +pub const RECENT_HISTORY_MARKER_PREFIX: &str = "Earlier remote history - "; + +impl HistoryBase { + /// Request a bounded initial view. Older servers ignore this hint and + /// return their ordinary full History payload. + pub fn recent(visible: usize) -> Self { + Self { + count: 0, + fingerprint: 0, + images_fingerprint: None, + recent_messages: Some(visible), + } + } } /// Stable fingerprint of a History image set (order, metadata and data). @@ -196,6 +240,7 @@ impl HistoryFingerprinter { count: self.count, fingerprint: self.finish(), images_fingerprint: None, + recent_messages: None, } } } @@ -360,8 +405,9 @@ pub type ReloadRecoverySnapshot = jcode_selfdev_types::ReloadRecoveryDirective; mod wire; pub use wire::{ - AccountUsageSnapshot, Request, SERVER_CAPABILITIES, SERVER_CAPABILITY_TURN_ORIGIN, ServerEvent, - TurnEndStatus, TurnIdentity, TurnOrigin, TurnOriginKind, server_capabilities, + AccountUsageSnapshot, AccountUsageWindow, Request, SERVER_CAPABILITIES, + SERVER_CAPABILITY_TURN_ORIGIN, ServerEvent, TurnEndStatus, TurnIdentity, TurnOrigin, + TurnOriginKind, server_capabilities, }; pub use wire::{SessionToolConfig, SessionToolDefinition}; @@ -836,6 +882,7 @@ impl Request { Request::AppletAction { id, .. } => *id, Request::CloseApplet { id, .. } => *id, Request::Transcript { id, .. } => *id, + Request::VoiceActivity { id, .. } => *id, Request::InputShell { id, .. } => *id, Request::CycleModel { id, .. } => *id, Request::RefreshModels { id } => *id, @@ -909,6 +956,10 @@ impl Request { | Request::AccountUsage { .. } | Request::InvalidateOpenAiUsage { .. } | Request::InvalidateAnthropicUsage { .. } + // One-shot login clients report new credentials without a + // session. Subscribed connections still take the + // session-local path that also switches their model. + | Request::NotifyAuthChanged { .. } | Request::NotifySession { .. } | Request::AppletAction { .. } | Request::CloseApplet { .. } diff --git a/crates/jcode-protocol/src/protocol_tests.rs b/crates/jcode-protocol/src/protocol_tests.rs index 25a55736551..24774f20aa7 100644 --- a/crates/jcode-protocol/src/protocol_tests.rs +++ b/crates/jcode-protocol/src/protocol_tests.rs @@ -96,3 +96,13 @@ fn swarm_live_status_defaults_and_roundtrips() { runtime ); } + +#[test] +fn sdk_login_notify_is_accepted_without_subscribe() { + // The SDK login flow sends exactly this frame on a fresh connection. + // Requiring Subscribe first dropped it, so Desktop never saw new models. + let request: Request = + serde_json::from_str(r#"{"type":"notify_auth_changed","id":1,"provider":"claude"}"#) + .unwrap(); + assert!(request.is_lightweight_control_request()); +} diff --git a/crates/jcode-protocol/src/protocol_tests/core_events.rs b/crates/jcode-protocol/src/protocol_tests/core_events.rs index 5388441a5c6..715c3341ddb 100644 --- a/crates/jcode-protocol/src/protocol_tests/core_events.rs +++ b/crates/jcode-protocol/src/protocol_tests/core_events.rs @@ -245,6 +245,7 @@ fn test_soft_interrupt_ids_wire_shape_and_legacy_defaults() -> Result<()> { fn test_compacted_history_request_roundtrip() -> Result<()> { let req = Request::GetCompactedHistory { id: 7, + recent_history: false, visible_messages: 64, }; let json = serde_json::to_string(&req)?; @@ -261,6 +262,40 @@ fn test_compacted_history_request_roundtrip() -> Result<()> { Ok(()) } +#[test] +fn recent_history_schema_is_opt_in_and_legacy_compatible() -> Result<()> { + let legacy: crate::HistoryBase = serde_json::from_str(r#"{"count":0,"fingerprint":0}"#)?; + assert_eq!(legacy.recent_messages, None); + assert!(!serde_json::to_string(&legacy)?.contains("recent_messages")); + let recent = crate::HistoryBase::recent(128); + assert_eq!(recent.count, 0); + assert_eq!(recent.fingerprint, 0); + assert_eq!(recent.images_fingerprint, None); + let decoded: crate::HistoryBase = serde_json::from_str(&serde_json::to_string(&recent)?)?; + assert_eq!(decoded.recent_messages, Some(128)); + let legacy = + parse_request_json(r#"{"type":"get_compacted_history","id":1,"visible_messages":64}"#)?; + assert!(matches!( + legacy, + Request::GetCompactedHistory { + recent_history: false, + .. + } + )); + let recent = parse_request_json( + r#"{"type":"get_compacted_history","id":1,"visible_messages":192,"recent_history":true}"#, + )?; + assert!(matches!( + recent, + Request::GetCompactedHistory { + recent_history: true, + visible_messages: 192, + .. + } + )); + Ok(()) +} + #[test] fn test_notify_auth_changed_provider_hint_is_optional() -> Result<()> { let legacy = r#"{"type":"notify_auth_changed","id":9}"#; diff --git a/crates/jcode-protocol/src/wire.rs b/crates/jcode-protocol/src/wire.rs index 8b46dd2022b..e4205b2f7c1 100644 --- a/crates/jcode-protocol/src/wire.rs +++ b/crates/jcode-protocol/src/wire.rs @@ -252,6 +252,9 @@ pub enum Request { #[serde(rename = "get_compacted_history")] GetCompactedHistory { id: u64, + /// Expand a recent remote window rather than archived compaction. + #[serde(default)] + recent_history: bool, /// Number of leading compacted messages the client wants rendered before the live tail. visible_messages: usize, }, @@ -340,6 +343,15 @@ pub enum Request { session_id: Option, }, + /// Live push-to-talk state for a TUI session (waveform, live text). + /// Fire-and-forget: the server answers `Done` once it has fanned out. + #[serde(rename = "voice_activity")] + VoiceActivity { + id: u64, + session_id: String, + activity: VoiceActivity, + }, + /// Execute a shell command from `!cmd` in the active remote session. #[serde(rename = "input_shell")] InputShell { id: u64, command: String }, @@ -924,8 +936,8 @@ pub enum Request { mod events; pub use events::{ - AccountUsageSnapshot, SERVER_CAPABILITIES, SERVER_CAPABILITY_TURN_ORIGIN, ServerEvent, - TurnEndStatus, TurnIdentity, TurnOrigin, TurnOriginKind, server_capabilities, + AccountUsageSnapshot, AccountUsageWindow, SERVER_CAPABILITIES, SERVER_CAPABILITY_TURN_ORIGIN, + ServerEvent, TurnEndStatus, TurnIdentity, TurnOrigin, TurnOriginKind, server_capabilities, }; impl std::fmt::Debug for Request { diff --git a/crates/jcode-protocol/src/wire/events.rs b/crates/jcode-protocol/src/wire/events.rs index 6b8271b3a35..f0677b02ba8 100644 --- a/crates/jcode-protocol/src/wire/events.rs +++ b/crates/jcode-protocol/src/wire/events.rs @@ -2,9 +2,19 @@ use super::*; +/// A provider-reported account limit beyond the two legacy quota slots. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct AccountUsageWindow { + pub label: String, + pub usage_ratio: f32, + pub resets_at: Option, +} + /// Credential-free usage attributed to an OAuth account on the daemon host. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct AccountUsageSnapshot { + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub additional_windows: Vec, pub label: String, pub email: Option, pub exhausted: bool, @@ -38,6 +48,11 @@ mod account_usage_tests { serde_json::from_str(&serde_json::to_string(&request).unwrap()).unwrap(); assert!(matches!(decoded, Request::AccountUsage { id: 71 })); let snapshot = AccountUsageSnapshot { + additional_windows: vec![AccountUsageWindow { + label: "Future model / month".into(), + usage_ratio: 0.61, + resets_at: Some("2099-01-01T00:00:00Z".into()), + }], label: "remote-account".into(), email: None, exhausted: false, @@ -67,8 +82,12 @@ mod account_usage_tests { panic!("wrong response") }; assert_eq!(id, 71); - assert_eq!(anthropic, vec![snapshot]); + assert_eq!(anthropic, vec![snapshot.clone()]); assert!(openai.is_empty()); + let mut legacy = serde_json::to_value(&snapshot).unwrap(); + legacy.as_object_mut().unwrap().remove("additional_windows"); + let legacy: AccountUsageSnapshot = serde_json::from_value(legacy).unwrap(); + assert!(legacy.additional_windows.is_empty()); } } @@ -1013,6 +1032,10 @@ pub enum ServerEvent { #[serde(rename = "transcript")] Transcript { text: String, mode: TranscriptMode }, + /// Push-to-talk captured outside this client (e.g. `jcode companion`). + #[serde(rename = "voice_activity")] + VoiceActivity { activity: VoiceActivity }, + /// Completed `!cmd` shell execution for a connected remote client. #[serde(rename = "input_shell_result")] InputShellResult { result: InputShellResult }, diff --git a/crates/jcode-provider-anthropic-runtime/src/account_failover_tests.rs b/crates/jcode-provider-anthropic-runtime/src/account_failover_tests.rs index 66ff2b87ede..fa967d6c50e 100644 --- a/crates/jcode-provider-anthropic-runtime/src/account_failover_tests.rs +++ b/crates/jcode-provider-anthropic-runtime/src/account_failover_tests.rs @@ -82,6 +82,24 @@ fn http_retry_decision( response: &str, router_case: Option, alternate_cooling_down: bool, +) -> (RetryDecision, usize, bool) { + http_retry_decision_with_policy( + enabled, + alternate, + response, + router_case, + alternate_cooling_down, + true, + ) +} + +fn http_retry_decision_with_policy( + enabled: bool, + alternate: bool, + response: &str, + router_case: Option, + alternate_cooling_down: bool, + retry_recoverable: bool, ) -> (RetryDecision, usize, bool) { let through_router = router_case.is_some(); let _lock = jcode_base::storage::lock_test_env(); @@ -131,6 +149,7 @@ fn http_retry_decision( jcode_base::provider::models::reset_model_catalog_services_for_tests(); let mut config = jcode_base::config::Config::default(); config.provider.same_provider_account_failover = enabled; + config.provider.anthropic_retry_recoverable_rate_limits = retry_recoverable; config.save().unwrap(); let mut account_labels = Vec::new(); for label in if alternate { @@ -605,6 +624,67 @@ fn rate_limit_http_response() -> String { ) } +#[test] +fn oauth_short_throttle_retries_same_account_before_rotation() { + let response = rate_limit_http_response().replace("Retry-After: 60", "Retry-After: 3"); + let (decision, requests, rolled_back) = oauth_http_retry_decision(true, true, &response); + assert_eq!(decision, RetryDecision::Retry); + assert_eq!(requests, 1); + assert!(!rolled_back); +} + +#[test] +fn oauth_short_throttle_retry_can_be_disabled() { + let response = rate_limit_http_response().replace("Retry-After: 60", "Retry-After: 3"); + let (decision, requests, _) = + http_retry_decision_with_policy(true, true, &response, None, false, false); + assert!(matches!(decision, RetryDecision::Yield(_))); + assert_eq!(requests, 1); +} + +#[test] +fn oauth_exhausted_usage_never_waits_even_without_alternate() { + let body = + r#"{"error":{"type":"rate_limit_error","message":"You have reached your usage limit"}}"#; + let response = format!( + "HTTP/1.1 429 Too Many Requests\r\nRetry-After: 3\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + for alternate in [false, true] { + let (decision, requests, _) = oauth_http_retry_decision(true, alternate, &response); + assert!(matches!(decision, RetryDecision::Yield(_))); + assert_eq!(requests, 1); + } +} + +#[test] +fn recoverable_throttle_policy_is_bounded_and_not_exhaustion() { + use std::time::Duration; + let error = "429 too many requests: this request would exceed your account's rate limit"; + assert!(should_retry_recoverable_rate_limit(error, None, true)); + assert!(should_retry_recoverable_rate_limit( + error, + Some(Duration::from_secs(3)), + true + )); + assert!(!should_retry_recoverable_rate_limit( + error, + Some(Duration::from_secs(60)), + true + )); + assert!(!should_retry_recoverable_rate_limit(error, None, false)); + assert!(!should_retry_recoverable_rate_limit( + "429 usage_limit_reached", + None, + true + )); + assert!(!should_retry_recoverable_rate_limit( + "401 unauthorized", + None, + true + )); +} + #[test] fn oauth_account_failover_yields_http_429_before_sixty_second_retry() { let (decision, requests, rolled_back) = diff --git a/crates/jcode-provider-anthropic-runtime/src/error_classification.rs b/crates/jcode-provider-anthropic-runtime/src/error_classification.rs index e2ed0e1adc6..966dd3121ae 100644 --- a/crates/jcode-provider-anthropic-runtime/src/error_classification.rs +++ b/crates/jcode-provider-anthropic-runtime/src/error_classification.rs @@ -21,6 +21,21 @@ pub(super) fn is_account_limit_error(error: &str) -> bool { .any(|needle| error.contains(needle)) } +/// A short burst throttle can recover on the same login. Long reset windows +/// belong to the account router, and explicit exhaustion must never be retried. +pub(super) fn should_retry_recoverable_rate_limit( + error: &str, + retry_after: Option, + enabled: bool, +) -> bool { + enabled + && !jcode_provider_core::failover::is_definitive_usage_exhaustion(error) + && (error.contains("429 too many requests") + || error.contains("rate limit") + || error.contains("rate_limit")) + && retry_after.is_none_or(|delay| delay <= std::time::Duration::from_secs(5)) +} + /// Check if an error is transient and should be retried pub(super) fn is_retryable_error(error_str: &str) -> bool { jcode_provider_core::is_transient_transport_error(error_str) diff --git a/crates/jcode-provider-anthropic-runtime/src/lib.rs b/crates/jcode-provider-anthropic-runtime/src/lib.rs index b86b4d84500..bfad47174f9 100644 --- a/crates/jcode-provider-anthropic-runtime/src/lib.rs +++ b/crates/jcode-provider-anthropic-runtime/src/lib.rs @@ -2036,6 +2036,48 @@ async fn run_stream_with_retries( continue; } + // Definitive exhaustion cannot recover by sleeping. Model-scoped + // recovery above still gets first refusal, including earned resets. + if is_oauth + && !saw_output + && is_account_limit_error(&error_str) + && oauth_account_label.as_deref().is_some_and(|label| { + jcode_base::usage::account_known_exhausted( + jcode_base::usage::MultiAccountProviderKind::Anthropic, + label, + ) + }) + { + // The API often uses the same generic 429 for exhausted plan + // usage and burst throttles. Preserve verified account usage + // evidence so the outer router can skip its countdown too. + if tx + .send(Err(anyhow::anyhow!("Usage exhausted: {e:#}"))) + .await + .is_err() + { + jcode_base::logging::debug("Anthropic exhaustion event receiver closed"); + } + return; + } + if !saw_output + && jcode_provider_core::failover::is_definitive_usage_exhaustion(&error_str) + { + if tx.send(Err(e)).await.is_err() { + jcode_base::logging::debug("Anthropic exhaustion event receiver closed"); + } + return; + } + + let retry_recoverable_limit = + error_classification::should_retry_recoverable_rate_limit( + &error_str, + jcode_provider_core::retry_after::retry_after_from_error(&e), + jcode_base::config::Config::load() + .provider + .anthropic_retry_recoverable_rate_limits, + ); + // Account routing owns pre-output OAuth quota/auth failures when // other pool members exist, even if they are cooling down. Let // the router decide whether to rotate or report exhaustion. @@ -2049,6 +2091,7 @@ async fn run_stream_with_retries( && !response_accepted && (is_account_limit_error(&error_str) || is_oauth_auth_error(&error_str)) && jcode_base::provider::has_alternate_account_in_pool("claude") + && !retry_recoverable_limit { let _ = tx.send(Err(e)).await; return; @@ -2303,6 +2346,22 @@ async fn stream_response( response.status() )); + if is_oauth && (response.status().is_success() || response.status().as_u16() == 429) { + // Model responses already return real subscription limits, including + // quota walls. Keep those visible even when usage lookups are throttled. + // Recording them avoids a separate, heavily throttled usage lookup + // while the account is active. Never attribute API-key quotas here. + let headers = response.headers().clone(); + let label = usage_label.map(str::to_owned); + let quota_token = token.clone(); + tokio::task::spawn_blocking(move || { + jcode_base::usage::record_anthropic_response_limits( + "a_token, + label.as_deref(), + &headers, + ); + }); + } if !response.status().is_success() { let status = response.status(); let retry_after = jcode_provider_core::retry_after::retry_after(response.headers()); @@ -2323,6 +2382,7 @@ async fn stream_response( // Parse SSE stream let mut stream = response.bytes_stream(); let mut buffer = String::new(); + let mut utf8 = jcode_base::util::Utf8StreamDecoder::new(); let mut sse_state = SseStreamState { requested_model_base, ..SseStreamState::default() @@ -2346,8 +2406,7 @@ async fn stream_response( ); } }; - let chunk_str = String::from_utf8_lossy(&chunk); - buffer.push_str(&chunk_str); + buffer.push_str(&utf8.decode(&chunk)); // Process complete SSE events while let Some(event) = parse_sse_event(&mut buffer) { @@ -2690,3 +2749,29 @@ mod account_failover_tests; #[cfg(test)] mod account_binding_tests; + +#[cfg(test)] +mod sse_utf8_chunk_tests { + use super::parse_sse_event; + + /// Regression for #2087: a multibyte character split across transport + /// chunks must not decode to U+FFFD. + #[test] + fn multibyte_char_split_across_chunks_decodes_cleanly() { + let payload = "event: content_block_delta\ndata: {\"text\":\"你好世界\"}\n\n"; + let bytes = payload.as_bytes(); + let split = payload.find('你').unwrap() + 1; // inside the 3-byte char + let mut utf8 = jcode_base::util::Utf8StreamDecoder::new(); + let mut buffer = String::new(); + for chunk in [ + &bytes[..split], + &bytes[split..split + 3], + &bytes[split + 3..], + ] { + buffer.push_str(&utf8.decode(chunk)); + } + let event = parse_sse_event(&mut buffer).expect("complete event"); + assert!(!event.data.contains('\u{FFFD}')); + assert!(event.data.contains("你好世界")); + } +} diff --git a/crates/jcode-provider-anthropic-runtime/src/usage_recording.rs b/crates/jcode-provider-anthropic-runtime/src/usage_recording.rs index 0b828538a2c..a14ebba8951 100644 --- a/crates/jcode-provider-anthropic-runtime/src/usage_recording.rs +++ b/crates/jcode-provider-anthropic-runtime/src/usage_recording.rs @@ -164,10 +164,11 @@ mod tests { .enable_all() .build() .unwrap(); - for (label, terminal) in [ - (Some("captured"), true), - (None, true), - (Some("incomplete"), false), + for (label, terminal, limited) in [ + (Some("captured"), true, false), + (None, true, false), + (Some("incomplete"), false, false), + (Some("quota-limited"), false, true), ] { let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let endpoint = format!("http://{}/messages", listener.local_addr().unwrap()); @@ -207,7 +208,14 @@ mod tests { "event: message_stop\ndata: {}\n\nevent: message_stop\ndata: {}\n\n", ); } - write!(socket, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).unwrap(); + let reset = chrono::Utc::now().timestamp() + 7200; + let status = if limited { + "429 Too Many Requests" + } else { + "200 OK" + }; + let five = if limited { "1.0" } else { "0.37" }; + write!(socket, "HTTP/1.1 {status}\r\nContent-Type: text/event-stream\r\nanthropic-ratelimit-unified-5h-utilization: {five}\r\nanthropic-ratelimit-unified-7d-utilization: 0.62\r\nanthropic-ratelimit-unified-5h-reset: {reset}\r\nanthropic-ratelimit-unified-7d-reset: {reset}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).unwrap(); }); runtime.block_on(async { let (tx, mut rx) = mpsc::channel(100); @@ -228,7 +236,7 @@ mod tests { service_tier: None, stream: true, }; - stream_response( + let result = stream_response( Client::builder().no_proxy().build().unwrap(), "token".into(), label.is_some(), @@ -240,15 +248,43 @@ mod tests { &mut false, label, ) - .await - .unwrap(); + .await; + if limited { + assert!(result.unwrap_err().to_string().contains("429")); + } else { + result.unwrap(); + } let mut usage_events = 0; while let Some(event) = rx.recv().await { if matches!(event.unwrap(), StreamEvent::TokenUsage { .. }) { usage_events += 1; } } - assert_eq!(usage_events, 1); + assert_eq!(usage_events, if limited { 0 } else { 1 }); + if let Some(label) = label { + // Exercise the real response-header hook, not just the + // parser. Blocking persistence must not stall SSE delivery. + let limits = tokio::time::timeout(std::time::Duration::from_secs(2), async { + loop { + if let Some(limits) = jcode_base::usage::cached_account_usage_snapshot( + jcode_base::usage::MultiAccountProviderKind::Anthropic, + label, + ) { + break limits; + } + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + }) + .await + .unwrap(); + assert_eq!( + limits.five_hour_ratio, + Some(if limited { 1.0 } else { 0.37 }) + ); + assert_eq!(limits.seven_day_ratio, Some(0.62)); + assert!(limits.error.is_none()); + assert_eq!(limits.exhausted, limited); + } }); server.join().unwrap(); } @@ -269,6 +305,15 @@ mod tests { ); assert!(!home.path().join("provider_activity.json").exists()); assert!(!home.path().join("openai_oauth_usage.json").exists()); + let limits: serde_json::Value = serde_json::from_slice( + &std::fs::read(home.path().join("anthropic_usage_cache.json")).unwrap(), + ) + .unwrap(); + assert_eq!( + limits.as_object().unwrap().len(), + 3, + "API-key responses must not record subscription quotas" + ); } #[test] diff --git a/crates/jcode-provider-anthropic/src/lib.rs b/crates/jcode-provider-anthropic/src/lib.rs index 7c4fb9efcb1..c54708a8fed 100644 --- a/crates/jcode-provider-anthropic/src/lib.rs +++ b/crates/jcode-provider-anthropic/src/lib.rs @@ -1195,7 +1195,10 @@ mod cache_prefix_invariant_tests { assert_eq!(names.iter().filter(|n| **n == "bash").count(), 1); assert_eq!(names.iter().filter(|n| **n == "read").count(), 1); for legacy in ["Bash", "Read", "Agent"] { - assert!(!names.contains(&legacy), "legacy Claude Code name {legacy} sent"); + assert!( + !names.contains(&legacy), + "legacy Claude Code name {legacy} sent" + ); } } diff --git a/crates/jcode-provider-anthropic/src/oauth_tool_schema_tests.rs b/crates/jcode-provider-anthropic/src/oauth_tool_schema_tests.rs index 46cc0c7238b..7d1ffbf8c78 100644 --- a/crates/jcode-provider-anthropic/src/oauth_tool_schema_tests.rs +++ b/crates/jcode-provider-anthropic/src/oauth_tool_schema_tests.rs @@ -99,10 +99,7 @@ fn oauth_schedule_wakeup_forwards_the_real_schedule_schema() { "fabricated schema leaked back in: {props:?}" ); assert_eq!( - formatted - .iter() - .filter(|t| t.name == "schedule") - .count(), + formatted.iter().filter(|t| t.name == "schedule").count(), 1, "schedule must not be advertised twice" ); diff --git a/crates/jcode-provider-copilot-runtime/src/lib.rs b/crates/jcode-provider-copilot-runtime/src/lib.rs index 5ef9caf8d24..692b794d675 100644 --- a/crates/jcode-provider-copilot-runtime/src/lib.rs +++ b/crates/jcode-provider-copilot-runtime/src/lib.rs @@ -699,6 +699,7 @@ impl CopilotApiProvider { let mut stream = resp.bytes_stream(); let mut buffer = String::new(); + let mut utf8 = jcode_base::util::Utf8StreamDecoder::new(); let mut current_tool_id = String::new(); let mut current_tool_name = String::new(); let mut current_tool_args = String::new(); @@ -727,7 +728,7 @@ impl CopilotApiProvider { }; saw_any_data = true; - buffer.push_str(&String::from_utf8_lossy(&chunk)); + buffer.push_str(&utf8.decode(&chunk)); // Process complete SSE lines while let Some(line_end) = buffer.find('\n') { diff --git a/crates/jcode-provider-core/Cargo.toml b/crates/jcode-provider-core/Cargo.toml index a844c1cd4ed..62a9f27b243 100644 --- a/crates/jcode-provider-core/Cargo.toml +++ b/crates/jcode-provider-core/Cargo.toml @@ -30,4 +30,4 @@ tokio = { version = "1", features = ["sync", "time", "macros", "io-util"] } [dev-dependencies] tokio = { version = "1", features = ["sync", "macros", "rt", "net", "io-util"] } -tokio-tungstenite = { version = "0.24", default-features = false } +tokio-tungstenite = { version = "0.24", default-features = false, features = ["handshake"] } diff --git a/crates/jcode-provider-core/src/catalog_snapshot_tests.rs b/crates/jcode-provider-core/src/catalog_snapshot_tests.rs new file mode 100644 index 00000000000..6d756cd61f2 --- /dev/null +++ b/crates/jcode-provider-core/src/catalog_snapshot_tests.rs @@ -0,0 +1,93 @@ +use super::*; + +#[derive(Clone)] +struct CatalogProvider; + +#[async_trait] +impl Provider for CatalogProvider { + async fn complete( + &self, + _: &[Message], + _: &[ToolDefinition], + _: &str, + _: Option<&str>, + ) -> Result { + anyhow::bail!("unused") + } + fn name(&self) -> &str { + "catalog-test" + } + fn model(&self) -> String { + "future-model-2099".into() + } + fn available_models_display(&self) -> Vec { + vec![self.model()] + } + fn fork(&self) -> Arc { + Arc::new(self.clone()) + } +} + +#[derive(Clone)] +struct CombinedCatalogProvider; + +#[async_trait] +impl Provider for CombinedCatalogProvider { + async fn complete( + &self, + _: &[Message], + _: &[ToolDefinition], + _: &str, + _: Option<&str>, + ) -> Result { + anyhow::bail!("unused") + } + fn name(&self) -> &str { + "combined-test" + } + fn available_models_display(&self) -> Vec { + panic!("separate lookup") + } + fn model_routes(&self) -> Vec { + panic!("separate lookup") + } + fn model_catalog_snapshot(&self) -> ModelCatalogSnapshot { + ModelCatalogSnapshot::new( + Some("combined".into()), + Some("future-model-2100".into()), + vec!["future-model-2100".into()], + vec![], + ) + } + fn fork(&self) -> Arc { + Arc::new(self.clone()) + } +} + +#[test] +fn default_catalog_snapshot_preserves_legacy_fields() { + let provider = CatalogProvider; + let expected = ModelCatalogSnapshot::new( + Some(provider.display_name()), + Some(provider.model()), + provider.available_models_display(), + provider.model_routes(), + ); + assert_eq!(ModelCatalogSnapshot::from_provider(&provider), expected); +} + +#[test] +fn catalog_snapshot_dispatches_combined_hook_through_trait_object() { + let provider: Arc = Arc::new(CombinedCatalogProvider); + let snapshot = ModelCatalogSnapshot::from_provider(provider.as_ref()); + assert_eq!(snapshot.provider_name.as_deref(), Some("combined")); + assert_eq!(snapshot.available_models, ["future-model-2100"]); + let mut changed = snapshot; + changed.available_models.clear(); + assert_eq!( + ModelCatalogSnapshot::from_provider(provider.as_ref()) + .available_models + .len(), + 1 + ); +} diff --git a/crates/jcode-provider-core/src/failover.rs b/crates/jcode-provider-core/src/failover.rs index 86eec3e373b..a48db6b145f 100644 --- a/crates/jcode-provider-core/src/failover.rs +++ b/crates/jcode-provider-core/src/failover.rs @@ -69,6 +69,31 @@ fn contains_independent_status_code(haystack: &str, code: &str) -> bool { }) } +/// Only explicit plan/window or balance exhaustion bypasses the TUI countdown. +/// HTTP 429, rate limits, billing errors and generic quota errors are ambiguous. +pub fn is_definitive_usage_exhaustion(message: &str) -> bool { + let lower = message.to_ascii_lowercase(); + [ + "usage_limit_reached", + "usage limit reached", + "usage limit has been reached", + "you have reached your usage limit", + "you have reached your weekly limit", + "usage exhausted", + "quota exhausted", + "quota has been exhausted", + "credits have run out", + "credit balance is too low", + "insufficient_quota", + "insufficient credit", + "weekly limit reached", + "weekly limit has been reached", + "weekly usage limit reached", + ] + .iter() + .any(|needle| lower.contains(needle)) +} + pub fn classify_failover_error_message(message: &str) -> FailoverDecision { let lower = message.to_ascii_lowercase(); @@ -93,23 +118,24 @@ pub fn classify_failover_error_message(message: &str) -> FailoverDecision { return FailoverDecision::RetryNextProvider; } - let rate_or_quota = [ - "rate limit", - "rate-limited", - "too many requests", - "quota", - "credit balance", - "credits have run out", - "insufficient credit", - "billing", - "payment required", - "usage tier", - // Plan/usage-window exhaustion, e.g. OpenAI OAuth `usage_limit_reached`. - "usage_limit", - "usage limit", - ] - .iter() - .any(|needle| lower.contains(needle)) + let rate_or_quota = is_definitive_usage_exhaustion(message) + || [ + "rate limit", + "rate-limited", + "too many requests", + "quota", + "credit balance", + "credits have run out", + "insufficient credit", + "billing", + "payment required", + "usage tier", + // Plan/usage-window exhaustion, e.g. OpenAI OAuth `usage_limit_reached`. + "usage_limit", + "usage limit", + ] + .iter() + .any(|needle| lower.contains(needle)) || contains_independent_status_code(&lower, "429") || contains_independent_status_code(&lower, "402"); if rate_or_quota { @@ -149,6 +175,32 @@ pub fn classify_failover_error_message(message: &str) -> FailoverDecision { mod tests { use super::*; + #[test] + fn definitive_exhaustion_excludes_ambiguous_rate_limits() { + for message in [ + "429 rate_limit_error: This request would exceed your account's rate limit. Please try again later.", + "429 Too Many Requests", + "quota exceeded", + "billing error", + "usage tier limit", + "rate limit exceeded; retry in 60 seconds", + ] { + assert!(!is_definitive_usage_exhaustion(message), "{message}"); + } + for message in [ + "usage_limit_reached: The usage limit has been reached", + "You have reached your usage limit", + "You have reached your weekly limit", + "OAuth usage exhausted", + "429 quota exhausted", + "credits have run out", + "insufficient_quota", + "Your credit balance is too low", + ] { + assert!(is_definitive_usage_exhaustion(message), "{message}"); + } + } + #[test] fn failover_prompt_roundtrips_from_error_message() { let prompt = ProviderFailoverPrompt { diff --git a/crates/jcode-provider-core/src/fingerprint.rs b/crates/jcode-provider-core/src/fingerprint.rs index 685b8a1ed7f..eee96fc44b5 100644 --- a/crates/jcode-provider-core/src/fingerprint.rs +++ b/crates/jcode-provider-core/src/fingerprint.rs @@ -34,25 +34,61 @@ fn prune_stale_baselines(baselines: &mut HashMap) } pub fn stable_hash_str(value: &str) -> u64 { - let digest = Sha256::digest(value.as_bytes()); + stable_hash_bytes(value.as_bytes()) +} + +fn stable_hash_bytes(value: &[u8]) -> u64 { + hash_prefix(&Sha256::digest(value)) +} + +fn hash_prefix(digest: &[u8]) -> u64 { let mut bytes = [0_u8; 8]; bytes.copy_from_slice(&digest[..8]); u64::from_be_bytes(bytes) } pub fn stable_hash_json(value: &T) -> u64 { - let encoded = serde_json::to_string(value).unwrap_or_default(); - stable_hash_str(&encoded) + stable_json_fingerprint(value).0 } -fn stable_json_len(value: &T) -> usize { - serde_json::to_string(value) - .map(|encoded| encoded.len()) - .unwrap_or_default() +/// Hash and byte length of the same compact JSON encoding. Keep the historical +/// empty-string hash/zero length on serialization failure, including serializers +/// that write a prefix before returning an error. +fn stable_json_fingerprint(value: &T) -> (u64, usize) { + let encoded = serde_json::to_vec(value).unwrap_or_default(); + (stable_hash_bytes(&encoded), encoded.len()) } -fn item_hashes(items: &[Value]) -> Vec { - items.iter().map(stable_hash_json).collect() +/// Each item's compact encoding is also exactly its contribution to the array +/// encoding. Reuse those bytes rather than serializing the complete input again. +/// Only one item's scratch buffer is retained for the duration of this call. +fn item_fingerprints(items: &[Value]) -> (Vec, u64) { + let mut hashes = Vec::with_capacity(items.len()); + let mut encoded = Vec::new(); + let mut aggregate = Sha256::new(); + let mut valid = true; + aggregate.update(b"["); + for (index, item) in items.iter().enumerate() { + if index > 0 { + aggregate.update(b","); + } + encoded.clear(); + if serde_json::to_writer(&mut encoded, item).is_err() { + // Value-to-Vec serialization normally cannot fail. Preserve the + // existing per-item and whole-array failure fallback if it does. + encoded.clear(); + valid = false; + } + hashes.push(stable_hash_bytes(&encoded)); + aggregate.update(&encoded); + } + aggregate.update(b"]"); + let input_hash = if valid { + hash_prefix(&aggregate.finalize()) + } else { + stable_hash_str("") + }; + (hashes, input_hash) } fn prefix_matches(current: &[u64], previous: &[u64]) -> bool { @@ -88,15 +124,11 @@ pub fn log_provider_canonical_input( tool_count: Option, extra_fields: &[(&str, String)], ) { - let request_hash = stable_hash_json(payload); - let request_json_chars = stable_json_len(payload); - let item_hashes = item_hashes(items); + let (request_hash, request_json_chars) = stable_json_fingerprint(payload); + let (item_hashes, input_hash) = item_fingerprints(items); let item_hashes_hash = stable_hash_json(&item_hashes); - let input_hash = stable_hash_json(items); - let system_hash = system.map(stable_hash_json); - let system_json_chars = system.map(stable_json_len); - let tools_hash = tools.map(stable_hash_json); - let tools_json_chars = tools.map(stable_json_len); + let (system_hash, system_json_chars) = system.map(stable_json_fingerprint).unzip(); + let (tools_hash, tools_json_chars) = tools.map(stable_json_fingerprint).unzip(); let first_item_hash = item_hashes.first().copied(); let last_item_hash = item_hashes.last().copied(); @@ -195,11 +227,22 @@ pub fn log_provider_canonical_input( )); } +#[cfg(test)] +#[path = "fingerprint_tests.rs"] +mod optimization_tests; + #[cfg(test)] mod tests { use super::*; use serde_json::json; + #[test] + fn stable_hash_preserves_sha256_prefix_and_big_endian_order() { + assert_eq!(stable_hash_str(""), 0xe3b0_c442_98fc_1c14); + assert_eq!(stable_hash_str("abc"), 0xba78_16bf_8f01_cfea); + assert_eq!(stable_hash_json(&json!([])), 0x4f53_cda1_8c2b_aa0c); + } + #[test] fn prefix_matching_allows_append_only_growth() { assert!(prefix_matches(&[1, 2, 3], &[1, 2])); diff --git a/crates/jcode-provider-core/src/fingerprint_tests.rs b/crates/jcode-provider-core/src/fingerprint_tests.rs new file mode 100644 index 00000000000..cf9e012e240 --- /dev/null +++ b/crates/jcode-provider-core/src/fingerprint_tests.rs @@ -0,0 +1,272 @@ +use super::*; +use serde_json::json; +use std::hint::black_box; +use std::time::Duration; + +fn legacy_fingerprint(value: &T) -> (u64, usize) { + let encoded = serde_json::to_string(value).unwrap_or_default(); + (stable_hash_str(&encoded), encoded.len()) +} + +fn provider_fixture(turns: usize) -> (Value, Vec, Value, Value) { + let system = json!([{"type": "text", "text": "You are a coding assistant. Preserve user changes.\n日本語 🦀", "cache_control": {"type": "ephemeral"}}]); + let tools = json!([{"name": "read_file", "description": "Read a file", "input_schema": {"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]}}]); + let source = + "fn main() { println!(\"hello\\world\"); }\n\t// café 日本語 🦀\u{0000}".repeat(128); + let mut items = Vec::with_capacity(turns * 3); + for turn in 0..turns { + items.push(json!({"role": "user", "content": [{"type": "text", "text": format!("Review src/file_{turn}.rs\n{source}")}]})); + items.push(json!({"role": "assistant", "content": [{"type": "tool_use", "id": format!("call_{turn}"), "name": "read_file", "input": {"path": format!("src/file_{turn}.rs")}}]})); + items.push(json!({"role": "user", "content": [{"type": "tool_result", "tool_use_id": format!("call_{turn}"), "content": source, "is_error": false}]})); + } + let payload = + json!({"model": "fixture-model", "system": system, "tools": tools, "messages": items}); + (payload, items, system, tools) +} + +#[test] +fn streaming_fingerprint_matches_exact_legacy_json_bytes() { + let (payload, items, system, tools) = provider_fixture(3); + let values = vec![ + Value::Null, + json!(true), + json!(0), + json!(-42), + json!(1.25), + json!(""), + json!([]), + json!({}), + json!("quote\" backslash\\ newline\n tab\t nul\u{0000} é 日本語 🦀"), + json!([null, false, 1, "nested", {"a": []}]), + payload, + json!(items), + system, + tools, + json!({"input": [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "Explain this"}, {"type": "input_image", "image_url": "data:image/png;base64,AAAA"}]}], "instructions": "Be precise"}), + json!({"contents": [{"role": "model", "parts": [{"functionCall": {"name": "read_file", "args": {"path": "main.rs"}}}]}], "systemInstruction": {"parts": [{"text": "Help"}]}}), + ]; + for value in values { + let expected = legacy_fingerprint(&value); + assert_eq!(stable_json_fingerprint(&value), expected, "{value:?}"); + assert_eq!(stable_hash_json(&value), expected.0, "{value:?}"); + } + // The generic helper must also accept unsized slices and strings. + assert_eq!(stable_json_fingerprint("é\n"), legacy_fingerprint("é\n")); + assert_eq!( + stable_json_fingerprint(&[1_u64, u64::MAX][..]), + legacy_fingerprint(&[1_u64, u64::MAX][..]) + ); +} + +struct FailingSerialize; + +impl Serialize for FailingSerialize { + fn serialize(&self, serializer: S) -> Result { + use serde::ser::SerializeSeq; + // Emit a partial document before failing, so partial hashes cannot leak through. + let mut sequence = serializer.serialize_seq(Some(2))?; + sequence.serialize_element("already emitted")?; + Err(serde::ser::Error::custom( + "intentional serialization failure", + )) + } +} + +#[test] +fn serializer_failure_retains_empty_string_hash_and_zero_length() { + let expected = (stable_hash_str(""), 0); + assert_eq!(legacy_fingerprint(&FailingSerialize), expected); + assert_eq!(stable_json_fingerprint(&FailingSerialize), expected); + assert_eq!(stable_hash_json(&FailingSerialize), expected.0); +} + +#[test] +fn item_fingerprints_preserve_order_mutations_and_empty_arrays() { + let (_, items, _, _) = provider_fixture(2); + let mut mutated = items.clone(); + mutated[0]["content"][0]["text"] = json!("changed"); + let mut reordered = items.clone(); + reordered.swap(0, 1); + for case in [&[][..], &items[..], &mutated[..], &reordered[..]] { + let hashes: Vec<_> = case.iter().map(|item| legacy_fingerprint(item).0).collect(); + assert_eq!( + item_fingerprints(case), + (hashes, legacy_fingerprint(case).0) + ); + } + assert_ne!(item_fingerprints(&items), item_fingerprints(&mutated)); + assert_ne!(item_fingerprints(&items), item_fingerprints(&reordered)); + assert_ne!( + stable_json_fingerprint(&items), + stable_json_fingerprint(&reordered) + ); +} + +#[derive(Debug, PartialEq, Eq)] +struct ComputedFingerprints { + request: (u64, usize), + items: Vec, + items_hash: u64, + input_hash: u64, + system: Option<(u64, usize)>, + tools: Option<(u64, usize)>, + first: Option, + last: Option, +} + +fn compute_legacy( + payload: &Value, + items: &[Value], + system: Option<&Value>, + tools: Option<&Value>, +) -> ComputedFingerprints { + // Match the original logger's separate serialization for hash and length. + let hash = |value: &Value| stable_hash_str(&serde_json::to_string(value).unwrap_or_default()); + let len = |value: &Value| serde_json::to_string(value).unwrap_or_default().len(); + let item_hashes: Vec<_> = items.iter().map(hash).collect(); + ComputedFingerprints { + request: (hash(payload), len(payload)), + items_hash: legacy_fingerprint(&item_hashes).0, + input_hash: legacy_fingerprint(items).0, + system: system.map(|value| (hash(value), len(value))), + tools: tools.map(|value| (hash(value), len(value))), + first: item_hashes.first().copied(), + last: item_hashes.last().copied(), + items: item_hashes, + } +} + +fn compute_optimized( + payload: &Value, + items: &[Value], + system: Option<&Value>, + tools: Option<&Value>, +) -> ComputedFingerprints { + let (item_hashes, input_hash) = item_fingerprints(items); + ComputedFingerprints { + request: stable_json_fingerprint(payload), + items_hash: stable_hash_json(&item_hashes), + input_hash, + system: system.map(stable_json_fingerprint), + tools: tools.map(stable_json_fingerprint), + first: item_hashes.first().copied(), + last: item_hashes.last().copied(), + items: item_hashes, + } +} + +#[test] +fn complete_fingerprint_computation_matches_legacy_fields() { + let (payload, items, system, tools) = provider_fixture(4); + for (system, tools) in [ + (None, None), + (Some(&system), None), + (None, Some(&tools)), + (Some(&system), Some(&tools)), + ] { + assert_eq!( + compute_optimized(&payload, &items, system, tools), + compute_legacy(&payload, &items, system, tools) + ); + } +} + +#[test] +fn canonical_logger_preserves_legacy_baseline_snapshot() { + let (payload, items, system, tools) = provider_fixture(2); + let context = jcode_logging::current_context_snapshot(); + let key = format!( + "{}\u{1f}fingerprint-test\u{1f}fixture-model\u{1f}snapshot-equivalence", + context.session.as_deref().unwrap_or("no-session") + ); + for (system, tools) in [(Some(&system), Some(&tools)), (None, None)] { + let expected = compute_legacy(&payload, &items, system, tools); + log_provider_canonical_input( + "fingerprint-test", + "fixture-model", + "snapshot-equivalence", + &payload, + &items, + system, + tools, + None, + &[], + ); + let snapshot = PROVIDER_INPUT_BASELINES + .lock() + .unwrap() + .remove(&key) + .expect("production logger must install its session baseline"); + assert_eq!(snapshot.request_hash, expected.request.0); + assert_eq!(snapshot.item_hashes, expected.items); + assert_eq!(snapshot.item_hashes_hash, expected.items_hash); + assert_eq!(snapshot.system_hash, expected.system.map(|pair| pair.0)); + assert_eq!(snapshot.tools_hash, expected.tools.map(|pair| pair.0)); + } +} + +#[test] +#[ignore = "manual pure-computation benchmark, run with --ignored --nocapture"] +fn benchmark_complete_provider_fingerprint_computation() { + for turns in [0, 1, 16, 128] { + let (payload, items, system, tools) = provider_fixture(turns); + let baseline = || { + compute_legacy( + black_box(&payload), + black_box(&items), + black_box(Some(&system)), + black_box(Some(&tools)), + ) + }; + let optimized = || { + compute_optimized( + black_box(&payload), + black_box(&items), + black_box(Some(&system)), + black_box(Some(&tools)), + ) + }; + assert_eq!( + baseline(), + optimized(), + "all logged fingerprint fields must match" + ); + for _ in 0..3 { + black_box(baseline()); + black_box(optimized()); + } + // Alternate order to reduce systematic cache/thermal bias. Excludes fixture setup, + // assertions, log formatting, baseline-map locking, and all logger/disk I/O. + let rounds = 12; + let mut old_elapsed = Duration::ZERO; + let mut new_elapsed = Duration::ZERO; + for round in 0..rounds { + let mut measure_old = || { + let start = Instant::now(); + black_box(baseline()); + old_elapsed += start.elapsed(); + }; + let mut measure_new = || { + let start = Instant::now(); + black_box(optimized()); + new_elapsed += start.elapsed(); + }; + if round % 2 == 0 { + measure_old(); + measure_new(); + } else { + measure_new(); + measure_old(); + } + } + eprintln!( + "fingerprint pure computation: items={} payload_bytes={} rounds={} legacy={:?} optimized={:?} speedup={:.3}x", + items.len(), + serde_json::to_string(&payload).unwrap().len(), + rounds, + old_elapsed, + new_elapsed, + old_elapsed.as_secs_f64() / new_elapsed.as_secs_f64() + ); + } +} diff --git a/crates/jcode-provider-core/src/lib.rs b/crates/jcode-provider-core/src/lib.rs index 2f04d39ee6c..81580da9279 100644 --- a/crates/jcode-provider-core/src/lib.rs +++ b/crates/jcode-provider-core/src/lib.rs @@ -34,7 +34,7 @@ pub use auth_mode::{ pub use catalog_refresh::{ModelCatalogRefreshSummary, summarize_model_catalog_refresh}; pub use failover::{ FailoverDecision, ProviderFailoverPrompt, classify_failover_error_message, - parse_failover_prompt_message, + is_definitive_usage_exhaustion, parse_failover_prompt_message, }; pub use fallback_pick::{ FallbackPickOptions, error_looks_like_credential_failure, model_generation_rank, @@ -288,6 +288,17 @@ pub trait Provider: Send + Sync { Vec::new() } + /// Capture the owned catalog at a single provider boundary. Providers with + /// shared catalog storage can override this to reuse one memo lookup. + fn model_catalog_snapshot(&self) -> ModelCatalogSnapshot { + ModelCatalogSnapshot::new( + Some(self.display_name()), + Some(self.model()), + self.available_models_display(), + self.model_routes(), + ) + } + /// Prefetch any dynamic model lists (default: no-op). async fn prefetch_models(&self) -> Result<()> { Ok(()) @@ -597,6 +608,23 @@ pub async fn collect_simple_completion( ); } Ok(StreamEvent::UpstreamProvider { provider }) => provider_name = provider, + Ok(StreamEvent::MessageEnd { stop_reason }) + if is_guardrail_stop_reason(stop_reason.as_deref()) => + { + // A refusal or content-filter stop is not a usable answer. Callers + // such as compaction would otherwise persist the refusal text as + // if it were real output (issue #2089). + return Err(SimpleCompletionError { + usage, + provider: provider_name, + model, + source: anyhow::anyhow!( + "simple completion stopped by provider guardrail (stop_reason={})", + stop_reason.as_deref().unwrap_or("") + ), + } + .into()); + } Ok(StreamEvent::Error { message, .. }) => { return Err(SimpleCompletionError { usage, @@ -622,6 +650,18 @@ pub async fn collect_simple_completion( Ok((result, usage)) } +/// True when a provider stop reason is a model-side refusal, safety, or +/// content-filter stop rather than a normal end of turn or truncation. +pub fn is_guardrail_stop_reason(stop_reason: Option<&str>) -> bool { + let Some(reason) = stop_reason else { + return false; + }; + let reason = reason.trim().to_ascii_lowercase(); + matches!(reason.as_str(), "refusal" | "content_filter" | "safety") + || reason.contains("guardrail") + || reason.contains("policy_violation") +} + /// Failed simple completion with the usage already observed before failure. /// Dropping the completion future (for example on timeout) cannot return this /// snapshot. Cancellation accounting requires a separate observer lifecycle. @@ -1426,15 +1466,7 @@ impl ModelCatalogSnapshot { } pub fn from_provider(provider: &dyn Provider) -> Self { - // Note: on multi-providers both calls below build the same route - // catalog; MultiProvider memoizes it (short TTL) so this stays one - // build instead of two. - Self::new( - Some(provider.display_name()), - Some(provider.model()), - provider.available_models_display(), - provider.model_routes(), - ) + provider.model_catalog_snapshot() } pub fn has_routes(&self) -> bool { @@ -1622,3 +1654,6 @@ mod saved_account_route_tests; #[cfg(test)] #[path = "lib_tests.rs"] mod tests; + +#[cfg(test)] +mod catalog_snapshot_tests; diff --git a/crates/jcode-provider-metadata/src/lib.rs b/crates/jcode-provider-metadata/src/lib.rs index a45731168b2..82e24715787 100644 --- a/crates/jcode-provider-metadata/src/lib.rs +++ b/crates/jcode-provider-metadata/src/lib.rs @@ -283,27 +283,31 @@ fn allows_insecure_http_host(host: &str) -> bool { return true; } - if let Ok(ip) = host.parse::() { - return match ip { - std::net::IpAddr::V4(v4) => { - let raw = u32::from(v4); - let is_carrier_grade_nat = (raw & 0xffc0_0000) == 0x6440_0000; - v4.is_loopback() - || v4.is_private() - || v4.is_link_local() - || v4.is_unspecified() - || is_carrier_grade_nat - } - std::net::IpAddr::V6(v6) => { - v6.is_loopback() - || v6.is_unique_local() - || v6.is_unicast_link_local() - || v6.is_unspecified() - } - }; - } + // Any IPv4/IPv6 literal is accepted for plaintext http. A bare address + // already names one machine, which is how self-hosted inference servers + // are reached: on the LAN (`10.15.15.53`), in a container network, or on a + // cloud host with a public address. Restricting the answer to RFC 1918 + // ranges only forced those servers behind an extra https proxy. + ip_addr_from_host(&host_lower).is_some() +} - false +/// Parse a URL host as an IP literal, tolerating brackets and an IPv6 zone id +/// (`[fe80::1%25eth0]` becomes `fe80::1`, which `IpAddr` cannot parse itself). +pub fn ip_addr_from_host(host: &str) -> Option { + let host = host.trim(); + let host = host + .strip_prefix('[') + .and_then(|s| s.strip_suffix(']')) + .unwrap_or(host); + // `IpAddr` rejects zone ids, so compare on the address part only. + #[expect( + clippy::manual_ok_err, + reason = "DNS hostnames are valid inputs but are explicitly not IP literals" + )] + match host.split('%').next()?.parse() { + Ok(address) => Some(address), + Err(_) => None, // DNS names and malformed literals are not IP addresses. + } } fn normalize_provider_input(input: &str) -> Option { @@ -389,9 +393,27 @@ mod tests { } #[test] - fn normalize_api_base_rejects_public_http_hosts() { + fn normalize_api_base_accepts_any_ip_http_host() { + // A bare IP names a single machine, so plaintext http is allowed even + // when that address is public. + assert_eq!( + normalize_api_base("http://8.8.8.8/v1").as_deref(), + Some("http://8.8.8.8/v1") + ); + assert_eq!( + normalize_api_base("http://203.0.113.10:8080/v1/").as_deref(), + Some("http://203.0.113.10:8080/v1") + ); + assert_eq!( + normalize_api_base("http://[2001:db8::1]:8080/v1").as_deref(), + Some("http://[2001:db8::1]:8080/v1") + ); + // Named hosts still need a local name or https. assert_eq!(normalize_api_base("http://example.com/v1"), None); - assert_eq!(normalize_api_base("http://8.8.8.8/v1"), None); + assert_eq!( + normalize_api_base("https://example.com/v1").as_deref(), + Some("https://example.com/v1") + ); } #[test] diff --git a/crates/jcode-sdk/Cargo.toml b/crates/jcode-sdk/Cargo.toml index 587a57d751a..6f6a5fb7926 100644 --- a/crates/jcode-sdk/Cargo.toml +++ b/crates/jcode-sdk/Cargo.toml @@ -22,6 +22,7 @@ url = "2" uuid = { version = "1", features = ["v4"] } # Jcode Cloud control plane (feature `cloud`). ureq = { version = "3", features = ["json"], optional = true } +jcode-logging = { path = "../jcode-logging", optional = true } # In-process SSH (feature `ssh-native`), for platforms without an `ssh` binary. # Keep RSA disabled: native SSH generates Ed25519 keys and uses pinned host keys. # russh 0.65 migrates the elliptic-curve stack to patched crypto-bigint 0.7. @@ -32,7 +33,7 @@ tokio = { version = "1", features = ["rt", "sync", "time", "net", "io-util", "ma [features] default = [] # Account sign-in (email code, browser/device flow) and host wake/provision. -cloud = ["dep:ureq"] +cloud = ["dep:ureq", "dep:jcode-logging"] # Pure-Rust SSH transport with pinned host keys; also enables `cloud::connect`. ssh-native = ["cloud", "dep:russh", "dep:rand", "dep:tokio"] diff --git a/crates/jcode-sdk/src/cloud.rs b/crates/jcode-sdk/src/cloud.rs index 87bb65c490d..1fd5b22d555 100644 --- a/crates/jcode-sdk/src/cloud.rs +++ b/crates/jcode-sdk/src/cloud.rs @@ -23,6 +23,10 @@ use crate::{Error, ErrorKind, Result}; use serde::{Deserialize, Serialize}; use std::time::{Duration, Instant}; +mod timing; +#[cfg(test)] +mod timing_tests; + /// Production control plane. pub const DEFAULT_API_BASE: &str = "https://api.jcode.sh/v1"; /// How long [`CloudClient::wait_for_host`] waits for a host to provision. @@ -287,11 +291,13 @@ impl CloudClient { serde_json::json!({"device_code": login.device_code}), )?; if (200..300).contains(&status) { - let r: LoginReply = serde_json::from_value(body).map_err(|e| { - Error::new( - ErrorKind::UnexpectedReply, - format!("Unexpected reply from jcode.sh: {e}"), - ) + let r: LoginReply = timing::measured("reply_decode", "device_poll", || { + serde_json::from_value(body).map_err(|e| { + Error::new( + ErrorKind::UnexpectedReply, + format!("Unexpected reply from jcode.sh: {e}"), + ) + }) })?; return Ok(Some(self.account(r))); } @@ -367,29 +373,51 @@ impl CloudClient { progress: &dyn Fn(&str), cancelled: &dyn Fn() -> bool, ) -> Result { - let deadline = Instant::now() + PROVISION_DEADLINE; - let mut last = String::new(); - loop { - let reply = self.request_host_connect(account, public_key_openssh)?; - if reply.ready { - return CloudTarget::from_reply(&reply); - } - let message = reply - .message - .clone() - .unwrap_or_else(|| format!("Jcode Cloud machine is {}…", reply.state)); - if message != last { - progress(&message); - last = message; - } - if Instant::now() >= deadline { - return Err(Error::new( - ErrorKind::StartupTimeout, - "Jcode Cloud did not become ready in time. Retry to keep waiting.", - )); + self.wait_for_host_until( + account, + public_key_openssh, + progress, + cancelled, + Instant::now() + PROVISION_DEADLINE, + ) + } + + fn wait_for_host_until( + &self, + account: &Account, + public_key_openssh: &str, + progress: &dyn Fn(&str), + cancelled: &dyn Fn() -> bool, + deadline: Instant, + ) -> Result { + timing::measured("total", "wait_for_host", || { + let mut last = String::new(); + loop { + let reply = timing::measured("poll", "wait_for_host", || { + self.request_host_connect(account, public_key_openssh) + })?; + if reply.ready { + return CloudTarget::from_reply(&reply); + } + let message = reply + .message + .clone() + .unwrap_or_else(|| format!("Jcode Cloud machine is {}…", reply.state)); + if message != last { + progress(&message); + last = message; + } + if Instant::now() >= deadline { + return Err(Error::new( + ErrorKind::StartupTimeout, + "Jcode Cloud did not become ready in time. Retry to keep waiting.", + )); + } + timing::measured("poll_wait", "wait_for_host", || { + sleep_unless(self.poll_interval, cancelled) + })?; } - sleep_unless(self.poll_interval, cancelled)?; - } + }) } /// Register this device's APNs token so the account's jcode servers can @@ -412,12 +440,21 @@ impl CloudClient { /// Reset the account's unread badge (the app came to the foreground). pub fn reset_push_badge(&self, account: &Account) -> Result<()> { - self.account_call(account, "POST", "/push/badge", serde_json::json!({"count": 0})) + self.account_call( + account, + "POST", + "/push/badge", + serde_json::json!({"count": 0}), + ) } /// Ask the control plane to push `notification` to every device on the /// account. Servers call this with their own account key. - pub fn send_push(&self, account: &Account, notification: &PushNotification) -> Result { + pub fn send_push( + &self, + account: &Account, + notification: &PushNotification, + ) -> Result { let client = Self { api_base: account.api_base.trim_end_matches('/').to_string(), ..self.clone() @@ -479,6 +516,8 @@ impl CloudClient { api_key: Option<&str>, body: serde_json::Value, ) -> Result<(u16, serde_json::Value)> { + let operation = timing::operation(method, path); + let send_started = Instant::now(); let mut req = ureq::http::Request::builder() .method(method) .uri(self.url(path)) @@ -486,24 +525,43 @@ impl CloudClient { if let Some(k) = api_key { req = req.header("Authorization", format!("Bearer {k}")); } - let req = req - .body(body.to_string()) - .map_err(|e| Error::new(ErrorKind::Transport, e.to_string()))?; - let mut resp = self.agent().run(req).map_err(|e| { + let req = req.body(body.to_string()).map_err(|e| { + timing::record("http_send", operation, "error", send_started.elapsed()); + Error::new(ErrorKind::Transport, e.to_string()) + })?; + let response = self.agent().run(req); + let outcome = match &response { + Ok(resp) if resp.status().is_success() => "success", + Ok(_) => "error", + Err(ureq::Error::Timeout(_)) => "timeout", + Err(_) => "error", + }; + timing::record("http_send", operation, outcome, send_started.elapsed()); + let mut resp = response.map_err(|e| { Error::new( ErrorKind::ConnectFailed, format!("Could not reach jcode.sh: {e}"), ) })?; let status = resp.status().as_u16(); - let text = resp - .body_mut() - .read_to_string() - .map_err(|e| Error::new(ErrorKind::Transport, e.to_string()))?; - Ok(( - status, - serde_json::from_str(&text).unwrap_or(serde_json::Value::Null), - )) + let start = Instant::now(); + let text = resp.body_mut().read_to_string(); + timing::record( + "http_body", + operation, + timing::http_outcome(&text), + start.elapsed(), + ); + let text = text.map_err(|e| Error::new(ErrorKind::Transport, e.to_string()))?; + let start = Instant::now(); + let decoded = serde_json::from_str(&text); + timing::record( + "http_decode", + operation, + if decoded.is_ok() { "success" } else { "error" }, + start.elapsed(), + ); + Ok((status, decoded.unwrap_or(serde_json::Value::Null))) } fn post Deserialize<'de>>( @@ -514,11 +572,13 @@ impl CloudClient { ) -> Result { let (status, body) = self.post_raw(path, api_key, body)?; if (200..300).contains(&status) { - return serde_json::from_value(body).map_err(|e| { - Error::new( - ErrorKind::UnexpectedReply, - format!("Unexpected reply from jcode.sh: {e}"), - ) + return timing::measured("reply_decode", timing::operation("POST", path), || { + serde_json::from_value(body).map_err(|e| { + Error::new( + ErrorKind::UnexpectedReply, + format!("Unexpected reply from jcode.sh: {e}"), + ) + }) }); } let err = &body["error"]; @@ -634,7 +694,9 @@ mod tests { } /// Minimal one-shot HTTP server for flow tests. - fn serve(replies: Vec<(u16, &'static str)>) -> (String, std::sync::mpsc::Receiver) { + pub(super) fn serve( + replies: Vec<(u16, &'static str)>, + ) -> (String, std::sync::mpsc::Receiver) { use std::io::{Read, Write}; let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let base = format!("http://{}", listener.local_addr().unwrap()); @@ -766,7 +828,10 @@ mod tests { (200, r#"{"ok":true}"#), (200, r#"{"sent":1,"devices":1}"#), (200, r#"{"ok":true}"#), - (401, r#"{"error":{"code":"unauthorized","message":"revoked"}}"#), + ( + 401, + r#"{"error":{"code":"unauthorized","message":"revoked"}}"#, + ), ]); let cloud = CloudClient::new("t"); let account = Account { @@ -798,7 +863,13 @@ mod tests { }, ) .unwrap(); - assert_eq!(receipt, PushReceipt { sent: 1, devices: 1 }); + assert_eq!( + receipt, + PushReceipt { + sent: 1, + devices: 1 + } + ); assert!(seen.recv().unwrap().contains(r#""kind":"turn_done""#)); cloud.unregister_push_device(&account, "ab01").unwrap(); diff --git a/crates/jcode-sdk/src/cloud/timing.rs b/crates/jcode-sdk/src/cloud/timing.rs new file mode 100644 index 00000000000..0a6b8c50b20 --- /dev/null +++ b/crates/jcode-sdk/src/cloud/timing.rs @@ -0,0 +1,70 @@ +//! Timing fields are closed labels, never request data or error messages. +use crate::{ErrorKind, Result}; +use std::time::{Duration, Instant}; + +pub(super) fn operation(method: &str, path: &str) -> &'static str { + match (method, path) { + ("POST", "/auth/email/start") => "email_start", + ("POST", "/auth/email/verify") => "email_verify", + ("POST", "/auth/device") => "device_start", + ("POST", "/auth/token") => "device_poll", + ("POST", "/cloud/host/connect") => "host_connect", + ("POST", "/push/devices") => "push_register", + ("DELETE", "/push/devices") => "push_unregister", + ("POST", "/push/badge") => "push_badge", + ("POST", "/push/send") => "push_send", + _ => "unknown", + } +} + +pub(super) fn record( + stage: &'static str, + operation: &'static str, + outcome: &'static str, + elapsed: Duration, +) { + let line = format!( + "[CLOUD_TIMING] stage={stage} operation={operation} outcome={outcome} duration_ms={}", + elapsed.as_millis() + ); + jcode_logging::info(&line); + #[cfg(test)] + EVENTS.with(|events| events.borrow_mut().push(line)); +} + +pub(super) fn http_outcome(result: &std::result::Result) -> &'static str { + match result { + Ok(_) => "success", + Err(ureq::Error::Timeout(_)) => "timeout", + Err(_) => "error", + } +} + +pub(super) fn measured( + stage: &'static str, + operation: &'static str, + run: impl FnOnce() -> Result, +) -> Result { + let start = Instant::now(); + let result = run(); + let outcome = match &result { + Ok(_) => "success", + Err(error) => match error.kind { + ErrorKind::Cancelled => "cancelled", + ErrorKind::Timeout | ErrorKind::StartupTimeout => "timeout", + _ => "error", + }, + }; + record(stage, operation, outcome, start.elapsed()); + result +} + +#[cfg(test)] +thread_local! { + static EVENTS: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; +} + +#[cfg(test)] +pub(super) fn take_events() -> Vec { + EVENTS.with(|events| std::mem::take(&mut *events.borrow_mut())) +} diff --git a/crates/jcode-sdk/src/cloud/timing_tests.rs b/crates/jcode-sdk/src/cloud/timing_tests.rs new file mode 100644 index 00000000000..abbc3dc5ac8 --- /dev/null +++ b/crates/jcode-sdk/src/cloud/timing_tests.rs @@ -0,0 +1,213 @@ +use super::*; + +fn account(base: String) -> Account { + Account { + api_key: "secret-account-key".into(), + email: Some("private@example.com".into()), + api_base: base, + } +} + +fn assert_event(events: &[String], stage: &str, operation: &str, outcome: &str) { + let prefix = format!( + "[CLOUD_TIMING] stage={stage} operation={operation} outcome={outcome} duration_ms=" + ); + assert!( + events.iter().any(|line| line + .strip_prefix(&prefix) + .is_some_and(|duration| duration.parse::().is_ok())), + "missing {prefix}: {events:?}" + ); + for line in events { + assert!(!line.contains("secret")); + assert!(!line.contains("private@example.com")); + assert!(!line.contains("ssh-ed25519")); + assert!(!line.contains("http://")); + } +} + +#[test] +fn successful_wait_records_http_poll_wait_and_total() { + let (base, _seen) = tests::serve(vec![ + (200, r#"{"ready":false,"message":"secret-progress"}"#), + ( + 200, + r#"{"ready":true,"address":"203.0.113.7","user":"u","host_keys":["ssh-ed25519 AAAA"]}"#, + ), + ]); + let mut cloud = CloudClient::new("secret-client"); + cloud.poll_interval = Duration::from_millis(1); + let target = cloud + .wait_for_host(&account(base), "ssh-ed25519 secret", &|_| {}, &|| false) + .unwrap(); + assert_eq!(target.user, "u"); + let events = timing::take_events(); + for stage in ["http_send", "http_body", "http_decode", "reply_decode"] { + assert_event(&events, stage, "host_connect", "success"); + } + for stage in ["poll", "poll_wait", "total"] { + assert_event(&events, stage, "wait_for_host", "success"); + } + assert_eq!( + events.iter().filter(|e| e.contains("stage=poll ")).count(), + 2 + ); +} + +#[test] +fn cancelled_wait_records_cancel_without_changing_error() { + let (base, _seen) = tests::serve(vec![(200, r#"{"ready":false}"#)]); + let error = CloudClient::new("t") + .wait_for_host(&account(base), "secret", &|_| {}, &|| true) + .unwrap_err(); + assert_eq!(error.kind, ErrorKind::Cancelled); + let events = timing::take_events(); + assert_event(&events, "poll_wait", "wait_for_host", "cancelled"); + assert_event(&events, "total", "wait_for_host", "cancelled"); +} + +#[test] +fn expired_deadline_records_timeout_without_sleeping() { + let (base, _seen) = tests::serve(vec![(200, r#"{"ready":false}"#)]); + let error = CloudClient::new("t") + .wait_for_host_until(&account(base), "secret", &|_| {}, &|| false, Instant::now()) + .unwrap_err(); + assert_eq!(error.kind, ErrorKind::StartupTimeout); + let events = timing::take_events(); + assert_event(&events, "total", "wait_for_host", "timeout"); + assert!(!events.iter().any(|e| e.contains("stage=poll_wait "))); +} + +#[test] +fn http_error_and_decode_failure_are_safe() { + for (status, body, stage) in [ + (401, r#"{"error":{"message":"secret-error"}}"#, "http_send"), + (200, "secret-not-json", "http_decode"), + (200, r#"{"ready":"secret-wrong-type"}"#, "reply_decode"), + ] { + let (base, _seen) = tests::serve(vec![(status, body)]); + assert!( + CloudClient::new("t") + .wait_for_host(&account(base), "secret", &|_| {}, &|| false) + .is_err() + ); + let events = timing::take_events(); + assert_event(&events, stage, "host_connect", "error"); + assert_event(&events, "poll", "wait_for_host", "error"); + assert_event(&events, "total", "wait_for_host", "error"); + } +} + +#[test] +fn invalid_ready_target_records_total_error() { + let (base, _seen) = tests::serve(vec![(200, r#"{"ready":true}"#)]); + assert!( + CloudClient::new("t") + .wait_for_host(&account(base), "secret", &|_| {}, &|| false) + .is_err() + ); + assert_event(&timing::take_events(), "total", "wait_for_host", "error"); +} + +#[test] +fn unknown_paths_and_invalid_requests_never_leak_into_labels() { + assert_eq!( + timing::operation("POST", "/secret-account?api_key=secret"), + "unknown" + ); + let cloud = CloudClient::new("t").with_api_base("secret-invalid-uri"); + assert!( + cloud + .post_raw( + "/secret-account", + Some("secret"), + serde_json::json!({"secret":"secret"}) + ) + .is_err() + ); + assert_event(&timing::take_events(), "http_send", "unknown", "error"); +} + +#[test] +fn transport_failures_record_error() { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + drop(listener); + assert!( + CloudClient::new("t") + .wait_for_host(&account(base), "secret", &|_| {}, &|| false) + .is_err() + ); + let events = timing::take_events(); + assert_event(&events, "http_send", "host_connect", "error"); + assert_event(&events, "total", "wait_for_host", "error"); +} + +#[test] +fn http_timeout_classification_is_static() { + let result: std::result::Result<(), _> = Err(ureq::Error::Timeout(ureq::Timeout::Global)); + assert_eq!(timing::http_outcome(&result), "timeout"); +} + +#[test] +fn stalled_http_send_and_body_record_timeouts() { + use std::io::{Read, Write}; + for stage in ["http_send", "http_body"] { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let mut request = [0; 4096]; + stream.read(&mut request).unwrap(); + if stage == "http_body" { + stream + .write_all( + b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\nConnection: close\r\n\r\n", + ) + .unwrap(); + } + std::thread::sleep(Duration::from_millis(150)); + }); + let mut cloud = CloudClient::new("t"); + cloud.timeout = Duration::from_millis(50); + let error = cloud + .request_host_connect(&account(base), "secret") + .unwrap_err(); + // Keep existing public error kinds, while the HTTP stage can identify + // the underlying ureq timeout without logging its error text. + assert_eq!( + error.kind, + if stage == "http_send" { + ErrorKind::ConnectFailed + } else { + ErrorKind::Transport + } + ); + assert_event(&timing::take_events(), stage, "host_connect", "timeout"); + server.join().unwrap(); + } +} + +#[test] +fn truncated_body_records_error_without_decoding() { + use std::io::{Read, Write}; + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let mut request = [0; 4096]; + stream.read(&mut request).unwrap(); + stream + .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\nConnection: close\r\n\r\nsecret") + .unwrap(); + }); + assert!( + CloudClient::new("t") + .request_host_connect(&account(base), "secret") + .is_err() + ); + let events = timing::take_events(); + assert_event(&events, "http_body", "host_connect", "error"); + assert!(!events.iter().any(|e| e.contains("stage=http_decode "))); + server.join().unwrap(); +} diff --git a/crates/jcode-sdk/src/launch.rs b/crates/jcode-sdk/src/launch.rs index 42cc44eff09..10bc1ad4083 100644 --- a/crates/jcode-sdk/src/launch.rs +++ b/crates/jcode-sdk/src/launch.rs @@ -566,14 +566,8 @@ pub fn ensure_runtime(options: &LaunchOptions, progress: &Progress<'_>) -> Resul } progress("starting harness API bridge..."); let standalone_bridge = sibling_exe("jcode-harness-api-bridge"); - let (bridge, bridge_args): (&Path, &[&str]) = if standalone_bridge.is_file() { - (&standalone_bridge, &[]) - } else { - // Current Jcode distributions host the bridge as a CLI subcommand so a - // desktop bundle only needs to ship one companion executable. Keep the - // standalone lookup above for compatibility with older installations. - (&jcode, &["api-bridge"]) - }; + let (bridge, bridge_args) = + shared_bridge_launch(&jcode, &standalone_bridge, options.binary.is_some()); spawn_detached(bridge, bridge_args).map_err(|error| { Error::new( ErrorKind::LaunchFailed, @@ -583,6 +577,21 @@ pub fn ensure_runtime(options: &LaunchOptions, progress: &Progress<'_>) -> Resul wait_for_socket(&api, "harness API bridge", options.start_timeout) } +/// Keep the bridge aligned with the selected runtime. An old standalone left +/// beside Desktop must not silently disable newer routing and identity support. +/// Standalone-only bundles remain supported when no CLI companion was selected. +fn shared_bridge_launch<'a>( + jcode: &'a Path, + standalone: &'a Path, + explicit_runtime: bool, +) -> (&'a Path, &'static [&'static str]) { + if explicit_runtime || jcode.is_file() || !standalone.is_file() { + (jcode, &["api-bridge"]) + } else { + (standalone, &[]) + } +} + fn spawn_detached(program: &Path, args: &[&str]) -> std::io::Result<()> { let mut command = Command::new(program); command @@ -838,6 +847,40 @@ fn launch_io(error: std::io::Error) -> Error { mod executable_tests { use super::*; + #[test] + fn selected_runtime_bridge_wins_over_stale_standalone() { + let root = tempfile::tempdir().unwrap(); + let runtime = root.path().join("jcode"); + let standalone = root.path().join("jcode-harness-api-bridge"); + fs::write(&standalone, b"obsolete bridge").unwrap(); + // An explicit command can also be a bare name resolved through PATH. + assert_eq!( + shared_bridge_launch(&runtime, &standalone, true), + (runtime.as_path(), &["api-bridge"][..]) + ); + fs::write(&runtime, b"current runtime").unwrap(); + assert_eq!( + shared_bridge_launch(&runtime, &standalone, false), + (runtime.as_path(), &["api-bridge"][..]) + ); + } + + #[test] + fn standalone_only_bundle_and_path_runtime_remain_supported() { + let root = tempfile::tempdir().unwrap(); + let runtime = root.path().join("jcode"); + let standalone = root.path().join("jcode-harness-api-bridge"); + assert_eq!( + shared_bridge_launch(&runtime, &standalone, false), + (runtime.as_path(), &["api-bridge"][..]) + ); + fs::write(&standalone, b"standalone-only bundle").unwrap(); + assert_eq!( + shared_bridge_launch(&runtime, &standalone, false), + (standalone.as_path(), &[][..]) + ); + } + #[test] fn sibling_executable_uses_platform_suffix_in_bundle_with_spaces() { let root = tempfile::tempdir().unwrap(); diff --git a/crates/jcode-sdk/src/lib.rs b/crates/jcode-sdk/src/lib.rs index 0235708c5fc..00f6641b894 100644 --- a/crates/jcode-sdk/src/lib.rs +++ b/crates/jcode-sdk/src/lib.rs @@ -198,10 +198,10 @@ pub use turn_retry::{FailureKind, RetryDecision, RetryPolicy, TurnRetry, classif /// The protocol types, re-exported so a client needs one dependency, not two. pub use jcode_harness_api as api; pub use jcode_harness_api::{ - ApiEvent, ApiRequest, HistoryMessage, ModelRouteInfo, PermissionDecision, RenderedImage, - RenderedImageAnchor, RenderedImageSource, ResponseStats, SessionInfo, SessionToolDefinition, - SidePanelPage, SidePanelPageFormat, SidePanelPageSource, SidePanelSnapshot, TextMatch, - ToolConfiguration, TurnStopReason, api_socket_path, + ApiEvent, ApiRequest, HistoryMessage, HistoryTool, ModelRouteInfo, PermissionDecision, + RenderedImage, RenderedImageAnchor, RenderedImageSource, ResponseStats, SessionInfo, + SessionToolDefinition, SidePanelPage, SidePanelPageFormat, SidePanelPageSource, + SidePanelSnapshot, TextMatch, ToolConfiguration, TurnStopReason, api_socket_path, }; pub use jcode_harness_api::{ ModelUsage, RuntimeCommand, RuntimeCommandOutput, compare_model_usage, diff --git a/crates/jcode-sdk/src/sdk_tests/turn_retry.rs b/crates/jcode-sdk/src/sdk_tests/turn_retry.rs index 11e0ee73639..22f4c90b6ba 100644 --- a/crates/jcode-sdk/src/sdk_tests/turn_retry.rs +++ b/crates/jcode-sdk/src/sdk_tests/turn_retry.rs @@ -194,6 +194,8 @@ fn cancellation_and_other_clients_drop_a_pending_retry() { }; assert_eq!(retry.observe(&cancelled, now), RetryDecision::Cancelled); + // Fresh input, not a late error from the cancelled provider request. + retry.user_sent(); retry.observe(&error("timed out"), now); let accepted = ApiEvent::MessageAccepted { session_id: "s".into(), @@ -282,3 +284,132 @@ fn failed_send_reschedules() { Some(now + RetryPolicy::default().base_delay) ); } + +#[test] +fn structured_rate_limit_hints_ignore_sibling_limits() { + for (message, seconds) in [ + ( + r#"API error 429: {"max_rpm":1200,"retry_after_seconds":17}"#, + 17, + ), + ( + r#"429: {"error":{"retry_after_seconds":2.3}} diagnostics"#, + 3, + ), + (r#"429: {"errors":[{"retry_after_seconds":9}]}"#, 9), + ] { + assert_eq!( + classify_failure(message, None), + FailureKind::RateLimited { + retry_after: Some(Duration::from_secs(seconds)) + }, + "{message}" + ); + } + for value in ["-1", "0", "\"20\"", "null"] { + let message = + format!(r#"429: {{"retry_after_seconds":{value},"max_rpm":1200}} retry in 1 second"#); + assert_eq!( + classify_failure(&message, None), + FailureKind::RateLimited { retry_after: None } + ); + } +} + +#[test] +fn day_hints_do_not_become_short_retries() { + let now = Instant::now(); + for message in [ + "429 rate limit, retry after 2 days", + "429 rate limit, retry in 1d", + r#"429: {"retry_after_seconds":1e100}"#, + ] { + let mut retry = TurnRetry::default(); + assert!( + matches!( + retry.observe(&error(message), now), + RetryDecision::GaveUp { .. } + ), + "{message}" + ); + assert_eq!(retry.pending(), None); + } +} + +#[test] +fn failover_consent_and_invalid_requests_override_transient_words() { + for message in [ + r#"[jcode-provider-failover]{"reason":"503 temporarily unavailable"}"#, + "unsupported_value: server error for invalid option", + "invalid tool schema, try again", + "body too large: network request rejected", + "refresh token has already been used, connection closed", + "model does not support the coding plan: 503", + ] { + assert_eq!( + classify_failure(message, None), + FailureKind::Permanent, + "{message}" + ); + } +} + +#[test] +fn cancellation_stays_cancelled_when_late_errors_arrive() { + let now = Instant::now(); + for status in ["cancelled", "canceled", "interrupted", "crashed"] { + let mut retry = TurnRetry::default(); + retry.observe(&error("timed out"), now); + assert_eq!( + retry.observe( + &ApiEvent::SessionStatus { + session_id: "s".into(), + status: status.into(), + pending_soft_interrupts: None, + }, + now + ), + RetryDecision::Cancelled + ); + retry.observe(&done(), now); + assert!(matches!( + retry.observe(&error("connection reset"), now), + RetryDecision::GaveUp { + exhausted: false, + .. + } + )); + assert!(!retry.take_due(now + Duration::from_secs(60)), "{status}"); + retry.user_sent(); + assert!(scheduled(&retry.observe(&error("timed out"), now)).is_some()); + } +} + +#[test] +fn terminal_stop_drops_existing_retry_without_needing_an_error() { + let now = Instant::now(); + for reason in [ + TurnStopReason::Interrupted, + TurnStopReason::Crash, + TurnStopReason::ProviderGuardrail, + TurnStopReason::LimitReached, + ] { + let mut retry = TurnRetry::default(); + retry.observe(&error("timed out"), now); + assert_eq!( + retry.observe(&stopped(reason), now), + RetryDecision::Cancelled + ); + assert!(!retry.take_due(now + Duration::from_secs(60))); + } +} + +#[test] +fn permanent_error_disarms_previously_scheduled_retry() { + let now = Instant::now(); + let mut retry = TurnRetry::default(); + retry.observe(&error("timed out"), now); + retry.observe(&error("401 Unauthorized"), now); + assert_eq!(retry.pending(), None); + assert!(!retry.take_due(now + Duration::from_secs(60))); +} diff --git a/crates/jcode-sdk/src/turn_retry.rs b/crates/jcode-sdk/src/turn_retry.rs index 570cb53483f..aa245730e55 100644 --- a/crates/jcode-sdk/src/turn_retry.rs +++ b/crates/jcode-sdk/src/turn_retry.rs @@ -52,6 +52,10 @@ pub const MAX_RATE_LIMIT_WAIT: Duration = Duration::from_secs(5 * 60); /// connection closed" is a credential failure, not a network drop. pub fn classify_failure(message: &str, retry_after: Option) -> FailureKind { let lower = message.to_ascii_lowercase(); + // A failover offer requires consent even if its embedded reason is transient. + if lower.trim_start().starts_with("[jcode-provider-failover]") { + return FailureKind::Permanent; + } if PERMANENT_MARKERS .iter() .any(|marker| lower.contains(marker)) @@ -102,6 +106,11 @@ const PERMANENT_MARKERS: &[&str] = &[ "token expired", "no refresh token", "credentials have been revoked", + "refresh token has been revoked", + "refresh token was rejected", + "refresh token is rejected", + "refresh token has already been used", + "refresh token is no longer valid", "re-authenticate", "please log in again", "run /login", @@ -137,6 +146,15 @@ const PERMANENT_MARKERS: &[&str] = &[ "unsupported parameter", "invalid parameter", "invalid schema", + "invalid tool", + "unsupported_value", + "unsupported image", + "unsupported file", + "file too large", + "body too large", + "input too large", + "does not support the coding plan", + "coding plan feature", "invalid image", "image too large", // Policy. @@ -215,6 +233,16 @@ const TRANSIENT_MARKERS: &[&str] = &[ ]; fn parse_retry_after(lower: &str) -> Option { + // Match the exact JSON field, never a sibling such as max_rpm. + if lower.contains("\"retry_after_seconds\"") { + return lower.match_indices('{').take(8).find_map(|(idx, _)| { + let value = serde_json::Deserializer::from_str(&lower[idx..]) + .into_iter::() + .next()? + .ok()?; + structured_retry_after(&value) + }); + } let tail = &lower[lower.find("retry")?..]; let mut words = tail.split_whitespace().take(8).peekable(); while let Some(word) = words.next() { @@ -237,13 +265,28 @@ fn parse_retry_after(lower: &str) -> Option { "ms" | "milliseconds" => return Some(Duration::from_millis(value)), "m" | "min" | "mins" | "minute" | "minutes" => value.saturating_mul(60), "h" | "hr" | "hour" | "hours" => value.saturating_mul(3600), - _ => value, + "d" | "day" | "days" => value.saturating_mul(86400), + "" | "s" | "sec" | "secs" | "second" | "seconds" => value, + _ => return None, }; return (secs > 0).then(|| Duration::from_secs(secs)); } None } +fn structured_retry_after(value: &serde_json::Value) -> Option { + match value { + serde_json::Value::Object(map) => map + .get("retry_after_seconds") + .and_then(serde_json::Value::as_f64) + .filter(|seconds| seconds.is_finite() && *seconds > 0.0) + .map(|seconds| Duration::from_secs(seconds.ceil() as u64)) + .or_else(|| map.values().find_map(structured_retry_after)), + serde_json::Value::Array(items) => items.iter().find_map(structured_retry_after), + _ => None, + } +} + /// Longest a retry waits for the failed turn's closing TurnDone. const CLOSE_GRACE: Duration = Duration::from_secs(10); @@ -315,6 +358,9 @@ pub struct TurnRetry { awaiting_close: bool, /// The last terminal stop for the running turn, which explains its error. stop: Option, + /// Cancellation is sticky until fresh input is accepted. Late errors from + /// the interrupted provider request must not resurrect a cancelled turn. + suppressed: bool, } impl TurnRetry { @@ -342,6 +388,7 @@ impl TurnRetry { /// The user sent new input. Its request gets a fresh retry budget, and /// any retry for the old one is dropped. pub fn user_sent(&mut self) -> RetryDecision { + self.suppressed = false; self.attempts = 0; self.stop = None; self.awaiting_close = false; @@ -350,7 +397,9 @@ impl TurnRetry { /// The user cancelled, cleared, or rewound the session. pub fn user_cancelled(&mut self) -> RetryDecision { - self.user_sent() + let decision = self.user_sent(); + self.suppressed = true; + decision } /// Returns true once, when a scheduled retry is due. The caller should @@ -379,6 +428,16 @@ impl TurnRetry { match event { ApiEvent::TurnStopped { reason, .. } => { self.stop = Some(*reason); + if matches!( + reason, + TurnStopReason::Interrupted + | TurnStopReason::Crash + | TurnStopReason::ProviderGuardrail + | TurnStopReason::LimitReached + ) { + self.suppressed = true; + return self.cancel_pending(); + } RetryDecision::None } // A completed turn proves the session works again. The API @@ -396,12 +455,18 @@ impl TurnRetry { RetryDecision::None } ApiEvent::SessionStatus { status, .. } - if matches!(status.as_str(), "cancelled" | "canceled") => + if matches!( + status.as_str(), + "cancelled" | "canceled" | "interrupted" | "crashed" + ) => { self.user_cancelled() } // Another client (or the server) started work in this session. - ApiEvent::MessageAccepted { .. } if self.due.is_some() => self.cancel_pending(), + ApiEvent::MessageAccepted { .. } => { + self.suppressed = false; + self.cancel_pending() + } ApiEvent::Error { code, message } => self.observe_error(*code, message, now), _ => RetryDecision::None, } @@ -409,6 +474,12 @@ impl TurnRetry { fn observe_error(&mut self, code: ErrorCode, message: &str, now: Instant) -> RetryDecision { let stop = self.stop.take(); + if self.suppressed { + return RetryDecision::GaveUp { + attempts: self.attempts, + exhausted: false, + }; + } // Only failed model turns are retried. Request-level errors (unknown // session, invalid request) are answered to the caller, not streamed // as turn failures, and a busy rejection is handled by steering. @@ -431,18 +502,24 @@ impl TurnRetry { } let kind = classify_failure(message, None); if kind == FailureKind::Permanent { + self.due = None; + self.awaiting_close = false; return RetryDecision::GaveUp { attempts: self.attempts, exhausted: false, }; } if self.attempts >= self.policy.max_attempts { + self.due = None; + self.awaiting_close = false; return RetryDecision::GaveUp { attempts: self.attempts, exhausted: true, }; } let Some(delay) = self.policy.delay(self.attempts + 1, kind) else { + self.due = None; + self.awaiting_close = false; return RetryDecision::GaveUp { attempts: self.attempts, exhausted: false, diff --git a/crates/jcode-task-types/src/lib.rs b/crates/jcode-task-types/src/lib.rs index d2831a60e86..fb4b90554c5 100644 --- a/crates/jcode-task-types/src/lib.rs +++ b/crates/jcode-task-types/src/lib.rs @@ -457,6 +457,9 @@ where Ok(value) } +/// `TodoItem::lane_owner` value that requests an auto-spawned swarm worker. +pub const LANE_OWNER_WORKER: &str = "worker"; + #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct TodoItem { pub content: String, @@ -474,6 +477,11 @@ pub struct TodoItem { /// only to todos in earlier phases by first appearance. #[serde(default, skip_serializing_if = "Option::is_none")] pub lane_after: Option>, + /// Who runs this lane: [`LANE_OWNER_WORKER`] makes the swarm server spawn a + /// dedicated worker as soon as the lane is ready; anything else (default) + /// means this todo list's own agent runs it. Lane-wide, like `lane_after`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub lane_owner: Option, /// Forward-looking evidence state that this todo can be completed /// correctly. Legacy sessions stored a 0-100 score; numbers map onto the /// closest state on load. diff --git a/crates/jcode-telemetry-core/src/oauth_account.rs b/crates/jcode-telemetry-core/src/oauth_account.rs index 36bef719a0c..6b1e35b1b73 100644 --- a/crates/jcode-telemetry-core/src/oauth_account.rs +++ b/crates/jcode-telemetry-core/src/oauth_account.rs @@ -57,12 +57,35 @@ pub fn enqueue_oauth_account(provider: &str, email: &str) -> bool { "antigravity" => "antigravity", _ => return false, }; + // Clients call this on every frame and tick. The worker already drops + // repeats inside 60s, but each enqueue still woke its thread. Drop exact + // repeats here, before the channel, so an idle client stays asleep. + if recently_enqueued(provider, &email, std::time::Instant::now()) { + return true; + } send(Command::Candidate(Candidate { provider: provider.to_ascii_lowercase(), email, })) } +fn recently_enqueued(provider: &str, email: &str, now: std::time::Instant) -> bool { + static LAST: std::sync::Mutex> = + std::sync::Mutex::new(None); + let Ok(mut last) = LAST.lock() else { + return false; + }; + if let Some((p, e, at)) = last.as_ref() + && p == provider + && e == email + && now.duration_since(*at) < Duration::from_secs(60) + { + return true; + } + *last = Some((provider.to_string(), email.to_string(), now)); + false +} + /// Does not consume the pending notice or authorize identity upload. pub fn pending_oauth_account_notice() -> Option { if !super::is_enabled() { @@ -375,6 +398,28 @@ fn handle_in( #[cfg(test)] mod tests { use super::*; + + #[test] + fn repeated_enqueue_is_dropped_before_waking_worker() { + let t0 = std::time::Instant::now(); + assert!(!recently_enqueued("anthropic", "dedupe@example.com", t0)); + assert!(recently_enqueued( + "anthropic", + "dedupe@example.com", + t0 + Duration::from_secs(1) + )); + assert!(!recently_enqueued( + "openai", + "dedupe@example.com", + t0 + Duration::from_secs(2) + )); + assert!(!recently_enqueued( + "openai", + "dedupe@example.com", + t0 + Duration::from_secs(70) + )); + } + fn check(known: bool, tracked: bool) -> Option { Some(Check { installation_known: known, diff --git a/crates/jcode-terminal-launch/src/lib.rs b/crates/jcode-terminal-launch/src/lib.rs index 1505ae2eca7..910fa65e874 100644 --- a/crates/jcode-terminal-launch/src/lib.rs +++ b/crates/jcode-terminal-launch/src/lib.rs @@ -18,6 +18,9 @@ pub struct TerminalCommand { /// spawn hooks and spawned terminals. Applied after the first-class /// `JCODE_SPAWN_*` keys, so entries here win on key collisions. pub extra_env: Vec<(String, String)>, + /// Non-secret runtime identity needed by the spawned jcode client. This is + /// deliberately separate from arbitrary spawn metadata. + pub runtime_env: Vec<(String, String)>, /// Terminal-identifying env vars captured from the *client* that requested /// this spawn (see [`snapshot_client_terminal_env`]). When set, these /// override the (possibly stale) server-inherited values for the same keys @@ -36,6 +39,7 @@ impl TerminalCommand { kind: None, session_id: None, extra_env: Vec::new(), + runtime_env: Vec::new(), client_terminal_env: Vec::new(), } } @@ -65,6 +69,16 @@ impl TerminalCommand { self } + /// Attach the explicit non-secret runtime identity for the child client. + /// Unknown keys are ignored so arbitrary metadata cannot enter its env. + pub fn runtime_env(mut self, env: Vec<(String, String)>) -> Self { + self.runtime_env = env + .into_iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .collect(); + self + } + /// Attach the client's terminal-identifying env snapshot (see /// [`snapshot_client_terminal_env`]) so the spawn follows the terminal the /// requesting client is attached to instead of the server's stale env. @@ -74,6 +88,15 @@ impl TerminalCommand { } } +/// Small allowlist of non-secret settings that select the local jcode runtime. +pub const RUNTIME_ENV_VARS: &[&str] = &[ + "JCODE_HOME", + "JCODE_RUNTIME_DIR", + "JCODE_SOCKET", + "JCODE_API_SOCKET", + "JCODE_NO_AUTO_UPDATE", +]; + /// Terminal/window-manager environment variables that identify *which* /// terminal, multiplexer, or display a client is attached to. /// @@ -419,6 +442,24 @@ fn resume_terminal_candidates_with_client_env( candidates } +#[cfg(any(not(unix), test))] +fn windows_resume_terminal_candidates_with( + configured_terminal: Option, + detected_terminal: Option, +) -> Vec { + let mut candidates = Vec::new(); + if let Some(term) = configured_terminal { + push_unique_terminal(&mut candidates, term); + } + if let Some(term) = detected_terminal { + push_unique_terminal(&mut candidates, term); + } + for term in ["alacritty", "wt", "wezterm", "cmd"] { + push_unique_terminal(&mut candidates, term); + } + candidates +} + #[cfg(unix)] pub fn resume_terminal_candidates() -> Vec { let configured_terminal = std::env::var("JCODE_TERMINAL").ok(); @@ -427,26 +468,20 @@ pub fn resume_terminal_candidates() -> Vec { #[cfg(not(unix))] pub fn resume_terminal_candidates() -> Vec { - let mut candidates = Vec::new(); + // Read the configured terminal explicitly (rather than `.ok()`) to keep + // this file's swallowed-error budget at its recorded level. + let mut configured_terminal = None; if let Ok(term) = std::env::var("JCODE_TERMINAL") { - push_unique_terminal(&mut candidates, term); - } - if let Some(term) = detected_resume_terminal() { - push_unique_terminal(&mut candidates, term); + configured_terminal = Some(term); } - for term in ["alacritty", "wt", "wezterm", "cmd"] { - push_unique_terminal(&mut candidates, term); - } - candidates + windows_resume_terminal_candidates_with(configured_terminal, detected_resume_terminal()) } pub fn spawn_command_in_new_terminal_with( command: &TerminalCommand, cwd: &Path, - mut spawn_detached: impl FnMut(&mut Command) -> std::io::Result<()>, + spawn_detached: impl FnMut(&mut Command) -> std::io::Result<()>, ) -> Result { - let mut last_spawn_error: Option = None; - #[cfg(unix)] let candidates = { let configured_terminal = std::env::var("JCODE_TERMINAL").ok(); @@ -457,6 +492,16 @@ pub fn spawn_command_in_new_terminal_with( }; #[cfg(not(unix))] let candidates = resume_terminal_candidates(); + spawn_command_with_candidates(command, cwd, candidates, spawn_detached) +} + +fn spawn_command_with_candidates( + command: &TerminalCommand, + cwd: &Path, + candidates: Vec, + mut spawn_detached: impl FnMut(&mut Command) -> std::io::Result<()>, +) -> Result { + let mut last_spawn_error: Option = None; for term in candidates { let Some(mut cmd) = build_spawn_command(&term, command, cwd) else { @@ -596,6 +641,7 @@ fn spawn_metadata_env(command: &TerminalCommand, cwd: &Path) -> Vec<(String, Str "JCODE_SPAWN_COMMAND".to_string(), shell_command(&command_parts(command)), )); + // Re-export the requesting client's terminal env so spawn/focus hooks use // the client's terminal, not the server's stale startup env (#405). Each // var is exported both under its native name (overriding the inherited @@ -607,9 +653,32 @@ fn spawn_metadata_env(command: &TerminalCommand, cwd: &Path) -> Vec<(String, Str env.push((format!("JCODE_CLIENT_{key}"), value.clone())); } env.extend(command.extra_env.iter().cloned()); + env.extend( + command + .runtime_env + .iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .cloned(), + ); + env } +/// Apply the allowlisted runtime context to a child process, scrubbing stale +/// inherited values for the keys the context does not set explicitly. +pub fn apply_runtime_env(cmd: &mut Command, runtime_env: &[(String, String)]) { + if runtime_env.is_empty() { + return; + } + cmd.env_remove("JCODE_API_SOCKET"); + cmd.env_remove("JCODE_NO_AUTO_UPDATE"); + for (key, value) in runtime_env { + if RUNTIME_ENV_VARS.contains(&key.as_str()) { + cmd.env(key, value); + } + } +} + /// Build the process invocation for an external spawn hook. /// /// The hook command is parsed shell-style, then the target program and its @@ -618,7 +687,8 @@ fn spawn_metadata_env(command: &TerminalCommand, cwd: &Path) -> Vec<(String, Str /// The hook runs in the session working directory with the full /// `JCODE_SPAWN_*` metadata env set (see [`spawn_metadata_env`]); hooks that /// need a single shell-command string (tmux, kitty `@ launch`) can use -/// `$JCODE_SPAWN_COMMAND` instead of the appended argv. +/// `$JCODE_SPAWN_COMMAND` instead of the appended argv. The allowlisted runtime +/// env is also available to the hook and spawned jcode child. pub fn build_hook_spawn_command( hook: &str, command: &TerminalCommand, @@ -630,10 +700,14 @@ pub fn build_hook_spawn_command( .expect("parse_hook_command guarantees at least one part"); let mut cmd = Command::new(expand_home(program)); - cmd.args(prefix_args) - .arg(&command.program) - .args(&command.args) - .current_dir(cwd) + cmd.args(prefix_args); + if command.runtime_env.is_empty() { + // Preserve argv byte-for-byte for the historical no-context contract. + cmd.arg(&command.program).args(&command.args); + } else { + cmd.args(command_parts(command)); + } + cmd.current_dir(cwd) .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()); @@ -643,6 +717,7 @@ pub fn build_hook_spawn_command( for (key, value) in spawn_metadata_env(command, cwd) { cmd.env(key, value); } + apply_runtime_env(&mut cmd, &command.runtime_env); Ok(cmd) } @@ -671,7 +746,10 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt // (e.g. after `swarm stop`). Exit that shell when jcode exits // cleanly so the pane closes, but keep it open after a failure so // the error stays readable. - let shell = format!("{} && exit", shell_command(&command_parts(command))); + // The server may start a clean shell that does not inherit this + // process's env, so serialize runtime allowlist values only when a + // runtime context was explicitly supplied. + let command_line = format!("{} && exit", shell_command(&command_parts(command))); let script = concat!( "set -eu; ", "response=\"$(\"$1\" pane split --current --direction right --cwd \"$2\" --focus)\"; ", @@ -687,7 +765,7 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt .stderr(Stdio::null()) .args(["-c", script, "jcode-herdr-spawn", &herdr]) .arg(cwd) - .arg(shell); + .arg(command_line); if command.fresh_spawn { cmd.env("JCODE_FRESH_SPAWN", "1"); } @@ -698,18 +776,14 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt if let Some(pane) = terminal_env_value(&command.client_terminal_env, "TMUX_PANE") { cmd.args(["-t", &pane]); } - cmd.arg("-c") - .arg(cwd) - .arg(&command.program) - .args(&command.args); + cmd.arg("-c").arg(cwd).args(command_parts(command)); } #[cfg(unix)] "zellij" => { cmd.args(["action", "new-pane", "--direction", "right", "--cwd"]) .arg(cwd) .arg("--") - .arg(&command.program) - .args(&command.args); + .args(command_parts(command)); } #[cfg(unix)] "screen" => { @@ -755,34 +829,26 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt "ghostty" => { cmd.arg(format!("--working-directory={}", cwd.to_string_lossy())) .arg("-e") - .arg(&command.program) - .args(&command.args); + .args(command_parts(command)); } "kitty" => { cmd.args(["--title", title, "-e"]) - .arg(&command.program) - .args(&command.args); + .args(command_parts(command)); } "wezterm" => { - cmd.args([ - "start", - "--always-new-process", - "--", - command.program.to_string_lossy().as_ref(), - ]); - cmd.args(&command.args); + cmd.args(["start", "--always-new-process", "--"]) + .args(command_parts(command)); } "alacritty" => { cmd.args(["--title", title, "-e"]) - .arg(&command.program) - .args(&command.args); + .args(command_parts(command)); } "gnome-terminal" => { cmd.arg("--title").arg(title); - cmd.arg("--").arg(&command.program).args(&command.args); + cmd.arg("--").args(command_parts(command)); } "konsole" | "xterm" | "foot" => { - cmd.args(["-e"]).arg(&command.program).args(&command.args); + cmd.args(["-e"]).args(command_parts(command)); } #[cfg(target_os = "macos")] "iterm2" => { @@ -808,12 +874,13 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt cmd = Command::new("osascript"); cmd.args(["-e", &macos_terminal_applescript(command, cwd)]); } - #[cfg(not(unix))] + #[cfg(any(not(unix), test))] "wt" => { cmd.args(["new-tab", "--title", title]); cmd.arg(&command.program).args(&command.args); + apply_runtime_env(&mut cmd, &command.runtime_env); } - #[cfg(not(unix))] + #[cfg(any(not(unix), test))] "cmd" => { cmd.args(["/C", "start", title, "cmd.exe", "/K"]); cmd.arg(windows_command_line(&command_parts(command))); @@ -828,6 +895,7 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt for (key, value) in spawn_metadata_env(command, cwd) { cmd.env(key, value); } + apply_runtime_env(&mut cmd, &command.runtime_env); Some(cmd) } @@ -853,31 +921,59 @@ fn windows_command_line(args: &[String]) -> String { .join(" ") } +#[cfg(unix)] fn command_parts(command: &TerminalCommand) -> Vec { + if command.runtime_env.is_empty() { + return command_parts_without_runtime(command); + } + + let mut parts = vec!["env".to_string()]; + for key in ["JCODE_API_SOCKET", "JCODE_NO_AUTO_UPDATE"] { + if !command.runtime_env.iter().any(|(name, _)| name == key) { + parts.extend(["-u".to_string(), key.to_string()]); + } + } + if command.fresh_spawn { + parts.push("JCODE_FRESH_SPAWN=1".to_string()); + } + parts.extend( + command + .runtime_env + .iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .map(|(key, value)| format!("{key}={value}")), + ); + parts.extend(command_parts_without_runtime(command)); + parts +} + +fn command_parts_without_runtime(command: &TerminalCommand) -> Vec { std::iter::once(command.program.to_string_lossy().into_owned()) .chain(command.args.iter().cloned()) .collect() } +#[cfg(not(unix))] +fn command_parts(command: &TerminalCommand) -> Vec { + command_parts_without_runtime(command) +} + /// Build the inner `/bin/sh` script that Apple Terminal's `do script` will run. /// /// `do script` always executes in a login shell, so we `cd` into the working -/// directory and `exec` the target command (optionally injecting the fresh-spawn -/// env var, which would otherwise be lost because the spawned shell does not -/// inherit the env of the `osascript` process). +/// directory and `exec` the target command, serializing fresh-spawn and runtime +/// values because the terminal shell does not inherit the `osascript` process env. #[cfg(any(target_os = "macos", test))] fn macos_terminal_inner_script(command: &TerminalCommand, cwd: &Path) -> String { - let shell = shell_command(&command_parts(command)); - format!( - "cd {} && exec {}{}", - sh_escape(&cwd.to_string_lossy()), - if command.fresh_spawn { - "env JCODE_FRESH_SPAWN=1 " - } else { - "" - }, - shell - ) + let shell = if command.runtime_env.is_empty() && command.fresh_spawn { + format!( + "env JCODE_FRESH_SPAWN=1 {}", + shell_command(&command_parts_without_runtime(command)) + ) + } else { + shell_command(&command_parts(command)) + }; + format!("cd {} && exec {shell}", sh_escape(&cwd.to_string_lossy())) } /// Build the AppleScript used to create a Ghostty tab on macOS. @@ -1219,6 +1315,13 @@ mod tests { "/tmp/tmux-1000/default,123,0".to_string(), ), ("TMUX_PANE".to_string(), "%42".to_string()), + ]) + .runtime_env(vec![ + ( + "JCODE_HOME".to_string(), + "/tmp/home with spaces/it's 🧪".to_string(), + ), + ("JCODE_SOCKET".to_string(), "/tmp/custom.sock".to_string()), ]); let cmd = build_spawn_command("tmux", &command, Path::new("/work/dir")) @@ -1237,6 +1340,13 @@ mod tests { "%42", "-c", "/work/dir", + "env", + "-u", + "JCODE_API_SOCKET", + "-u", + "JCODE_NO_AUTO_UPDATE", + "JCODE_HOME=/tmp/home with spaces/it's 🧪", + "JCODE_SOCKET=/tmp/custom.sock", "/usr/local/bin/jcode", "--resume", "ses_tmux", @@ -1335,7 +1445,8 @@ mod tests { "HERDR_BIN_PATH".to_string(), "/opt/herdr/bin/herdr".to_string(), ), - ]); + ]) + .fresh_spawn(); let cmd = build_spawn_command("herdr", &command, Path::new("/work/a b")) .expect("herdr spawn command should build"); @@ -1354,6 +1465,7 @@ mod tests { args[5], "'/usr/local/bin/jcode' '--resume' 'ses herdr' && exit" ); + assert_eq!(env_value(&cmd, "JCODE_FRESH_SPAWN").as_deref(), Some("1")); } #[test] @@ -1469,13 +1581,15 @@ mod tests { #[test] #[cfg(unix)] fn macos_terminal_inner_script_injects_fresh_spawn() { - let command = - TerminalCommand::new(std::path::PathBuf::from("/usr/local/bin/jcode"), vec![]) - .fresh_spawn(); + let command = TerminalCommand::new( + std::path::PathBuf::from("/usr/local/bin/jcode"), + vec!["--resume".to_string()], + ) + .fresh_spawn(); let script = macos_terminal_inner_script(&command, Path::new("/tmp")); assert_eq!( script, - "cd '/tmp' && exec env JCODE_FRESH_SPAWN=1 '/usr/local/bin/jcode'" + "cd '/tmp' && exec env JCODE_FRESH_SPAWN=1 '/usr/local/bin/jcode' '--resume'" ); } @@ -1624,7 +1738,18 @@ mod tests { .kind("swarm-agent") .session_id("ses_abc") .spawn_env("JCODE_SPAWN_SWARM_ID", "swarm-1") - .fresh_spawn(); + .fresh_spawn() + .runtime_env(vec![ + ( + "JCODE_HOME".to_string(), + "/tmp/hook home/it's 🧪".to_string(), + ), + ("JCODE_SOCKET".to_string(), "/tmp/hook.sock".to_string()), + ( + "JCODE_API_KEY".to_string(), + "must-not-propagate".to_string(), + ), + ]); let cmd = build_hook_spawn_command("tmux-hook --flag", &command, Path::new("/work/dir")) .expect("hook command should build"); @@ -1634,10 +1759,33 @@ mod tests { .get_args() .map(|a| a.to_string_lossy().into_owned()) .collect(); - assert_eq!( - args, - vec!["--flag", "/usr/local/bin/jcode", "--resume", "ses_abc"] - ); + // Unix serializes the runtime context into the appended argv (see + // `command_parts`) so remote-control hooks can forward it to the real + // child; Windows delivers the runtime context through the hook + // process env instead, so its argv stays the plain target invocation. + #[cfg(unix)] + let expected_args = vec![ + "--flag".to_string(), + "env".to_string(), + "-u".to_string(), + "JCODE_API_SOCKET".to_string(), + "-u".to_string(), + "JCODE_NO_AUTO_UPDATE".to_string(), + "JCODE_FRESH_SPAWN=1".to_string(), + "JCODE_HOME=/tmp/hook home/it's 🧪".to_string(), + "JCODE_SOCKET=/tmp/hook.sock".to_string(), + "/usr/local/bin/jcode".to_string(), + "--resume".to_string(), + "ses_abc".to_string(), + ]; + #[cfg(not(unix))] + let expected_args = vec![ + "--flag".to_string(), + "/usr/local/bin/jcode".to_string(), + "--resume".to_string(), + "ses_abc".to_string(), + ]; + assert_eq!(args, expected_args); assert_eq!( cmd.get_current_dir(), Some(Path::new("/work/dir")), @@ -1667,15 +1815,78 @@ mod tests { #[cfg(unix)] assert_eq!( env_value(&cmd, "JCODE_SPAWN_COMMAND").as_deref(), - Some("'/usr/local/bin/jcode' '--resume' 'ses_abc'") + Some( + "'env' '-u' 'JCODE_API_SOCKET' '-u' 'JCODE_NO_AUTO_UPDATE' 'JCODE_FRESH_SPAWN=1' 'JCODE_HOME=/tmp/hook home/it'\"'\"'s 🧪' 'JCODE_SOCKET=/tmp/hook.sock' '/usr/local/bin/jcode' '--resume' 'ses_abc'" + ) ); assert_eq!( env_value(&cmd, "JCODE_SPAWN_SWARM_ID").as_deref(), Some("swarm-1") ); + assert_eq!( + env_value(&cmd, "JCODE_HOME").as_deref(), + Some("/tmp/hook home/it's 🧪") + ); + assert_eq!( + env_value(&cmd, "JCODE_SOCKET").as_deref(), + Some("/tmp/hook.sock") + ); + assert_eq!(env_value(&cmd, "JCODE_API_KEY").as_deref(), None); assert_eq!(env_value(&cmd, "JCODE_FRESH_SPAWN").as_deref(), Some("1")); } + #[test] + #[cfg(unix)] + fn contextual_hook_invocation_delivers_runtime_to_executed_child() { + use std::os::unix::fs::PermissionsExt; + + let dir = std::env::temp_dir().join(format!( + "jcode-hook-runtime-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + std::fs::create_dir_all(&dir).unwrap(); + let target = dir.join("target child"); + let output = dir.join("child output"); + std::fs::write( + &target, + format!( + "#!/bin/sh\nprintf '%s|%s|%s' \"${{JCODE_HOME-}}\" \"${{JCODE_SOCKET-}}\" \"$*\" > {}\n", + sh_escape(&output.to_string_lossy()) + ), + ) + .unwrap(); + std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o755)).unwrap(); + + let command = + TerminalCommand::new(&target, vec!["--resume".into(), "ses quote's 🧪".into()]) + .runtime_env(vec![ + ("JCODE_HOME".into(), "/tmp/home with spaces/it's 🧪".into()), + ("JCODE_SOCKET".into(), "/tmp/connected socket.sock".into()), + ("JCODE_API_KEY".into(), "must-not-propagate".into()), + ]); + let mut hook = build_hook_spawn_command( + "sh -c 'exec env -i PATH=/usr/bin:/bin \"$@\"' jcode-hook", + &command, + &dir, + ) + .unwrap(); + assert!(hook.status().unwrap().success()); + assert_eq!( + std::fs::read_to_string(&output).unwrap(), + "/tmp/home with spaces/it's 🧪|/tmp/connected socket.sock|--resume ses quote's 🧪" + ); + assert!( + !std::fs::read_to_string(&output) + .unwrap() + .contains("must-not-propagate") + ); + std::fs::remove_dir_all(dir).unwrap(); + } + #[test] #[cfg(all(unix, not(target_os = "macos")))] fn builtin_terminal_spawn_exports_metadata_env() { diff --git a/crates/jcode-terminal-launch/src/windows_portable_tests.rs b/crates/jcode-terminal-launch/src/windows_portable_tests.rs index 247025dce8b..05fdb6eecec 100644 --- a/crates/jcode-terminal-launch/src/windows_portable_tests.rs +++ b/crates/jcode-terminal-launch/src/windows_portable_tests.rs @@ -5,7 +5,11 @@ //! the in-app spawn stub sit unnoticed off Unix (#715). Anything checkable //! without a Windows host belongs here instead. -use super::windows_command_line; +use super::{ + TerminalCommand, build_spawn_command, spawn_command_with_candidates, windows_command_line, + windows_resume_terminal_candidates_with, +}; +use std::path::Path; /// #715: the in-app resume spawn was a no-op off Unix, so the Windows /// command line it *would* build was never exercised anywhere that runs. @@ -36,3 +40,59 @@ fn windows_command_line_quotes_a_resume_invocation_on_every_platform() { "quoting flags changes cmd.exe parsing: {line}" ); } + +#[test] +fn configured_windows_terminal_precedes_shared_fallback_candidates() { + let candidates = windows_resume_terminal_candidates_with(Some("cmd".into()), None); + assert_eq!(candidates.first().map(String::as_str), Some("cmd")); + assert!(candidates.contains(&"cmd".to_string())); +} + +#[test] +fn cmd_only_fallback_builds_the_windows_start_command_with_runtime_identity() { + let command = TerminalCommand::new( + r"C:\Program Files\jcode\jcode.exe", + vec!["--fresh-spawn".into(), "--resume".into(), "ses_cmd".into()], + ) + .title("cmd fallback") + .runtime_env(vec![("JCODE_SOCKET".into(), r"C:\connected.sock".into())]); + let cmd = build_spawn_command("cmd", &command, Path::new(r"C:\work")) + .expect("cmd fallback invocation"); + assert_eq!(cmd.get_program().to_string_lossy(), "cmd"); + let args = cmd + .get_args() + .map(|arg| arg.to_string_lossy().into_owned()) + .collect::>(); + assert_eq!(&args[..5], ["/C", "start", "cmd fallback", "cmd.exe", "/K"]); + assert!(args[5].contains(r#""C:\Program Files\jcode\jcode.exe""#)); + assert!(args[5].contains("--resume ses_cmd")); + assert_eq!( + cmd.get_envs() + .find(|(key, _)| *key == "JCODE_SOCKET") + .and_then(|(_, value)| value) + .map(|value| value.to_string_lossy().into_owned()) + .as_deref(), + Some(r"C:\connected.sock") + ); +} + +#[test] +fn production_shared_launcher_honors_configured_terminal_before_cmd_fallback() { + let candidates = windows_resume_terminal_candidates_with(Some("wt".into()), None); + assert_eq!(candidates.first().map(String::as_str), Some("wt")); + let expected_attempts = candidates.clone(); + let mut attempted = Vec::new(); + let command = TerminalCommand::new("jcode.exe", vec!["--resume".into()]); + let result = + spawn_command_with_candidates(&command, Path::new(r"C:\work"), candidates, |candidate| { + attempted.push(candidate.get_program().to_string_lossy().into_owned()); + if candidate.get_program() == "cmd" { + Ok(()) + } else { + Err(std::io::Error::from(std::io::ErrorKind::NotFound)) + } + }) + .unwrap(); + assert!(result); + assert_eq!(attempted, expected_attempts); +} diff --git a/crates/jcode-todo-policy/src/completion_report_tests.rs b/crates/jcode-todo-policy/src/completion_report_tests.rs index 241398967dc..fbe0115580f 100644 --- a/crates/jcode-todo-policy/src/completion_report_tests.rs +++ b/crates/jcode-todo-policy/src/completion_report_tests.rs @@ -10,6 +10,7 @@ fn uncapped_and_preserves_every_todo_field() { priority: "high".into(), group: Some("lane".into()), lane_after: None, + lane_owner: None, details: Some(format!("Full details {i}")), confidence: Some(ConfidenceState::Verified), completion_confidence: Some(ConfidenceState::Validated), diff --git a/crates/jcode-todo-policy/src/lib.rs b/crates/jcode-todo-policy/src/lib.rs index 5c1fcefbc6e..84c557a2f59 100644 --- a/crates/jcode-todo-policy/src/lib.rs +++ b/crates/jcode-todo-policy/src/lib.rs @@ -34,8 +34,8 @@ pub const TODO_QUALITY_GATE_MAX_APPROX_TOKENS: usize = 64; pub use jcode_task_types::{ Autonomy, ConfidenceState, DeliveryState, Difficulty, FeedbackLoopCoverage, FeedbackLoopRelevance, FeedbackLoopState, FeedbackLoopTraceability, IntentUnderstanding, - IterationMaturity, TodoCompleteBlocker, TodoExternalBlocker, TodoGoal, TodoGoalChange, - TodoGoalField, TodoItem, TodoPlan, TodoPlanChange, TodoPlanField, + IterationMaturity, LANE_OWNER_WORKER, TodoCompleteBlocker, TodoExternalBlocker, TodoGoal, + TodoGoalChange, TodoGoalField, TodoItem, TodoPlan, TodoPlanChange, TodoPlanField, }; /// Return the canonical todo status for model-written status vocabulary. diff --git a/crates/jcode-todo-policy/src/stages.rs b/crates/jcode-todo-policy/src/stages.rs index ac24d5cafce..7d69bf88397 100644 --- a/crates/jcode-todo-policy/src/stages.rs +++ b/crates/jcode-todo-policy/src/stages.rs @@ -134,6 +134,13 @@ pub fn validate_lane_dependencies(todos: &[TodoItem]) -> Result<(), String> { } for (phase, stage) in stages.iter().enumerate() { for lane in &stage.lanes { + if let Some(owner) = todos[lane.items[0]].lane_owner.as_deref() + && !matches!(owner, "worker" | "self") + { + return Err(format!( + "lane owner must be \"worker\" or \"self\", got {owner:?}" + )); + } let expected = &todos[lane.items[0]].lane_after; if lane.items.iter().any(|&i| &todos[i].lane_after != expected) { return Err("lane_after must be consistent across every lane member".into()); @@ -166,6 +173,30 @@ pub fn validate_lane_dependencies(todos: &[TodoItem]) -> Result<(), String> { Ok(()) } +/// Whether this todo's lane asked for a dedicated auto-spawned swarm worker. +pub fn lane_requests_worker(todo: &TodoItem) -> bool { + todo.lane_owner.as_deref() == Some(crate::LANE_OWNER_WORKER) +} + +/// Make `lane_owner` lane-wide: every member takes the value of the lane's +/// first member that sets one, so adding an item to a worker lane keeps it a +/// worker lane and a single-item edit flips the whole lane. +pub fn normalize_lane_owners(todos: &mut [TodoItem]) { + let mut owners: std::collections::HashMap> = + std::collections::HashMap::new(); + for todo in todos.iter() { + let entry = owners.entry(lane_id(todo)).or_insert(None); + if entry.is_none() { + *entry = todo.lane_owner.clone(); + } + } + for todo in todos.iter_mut() { + let owner = owners.get(&lane_id(todo)).cloned().flatten(); + // `self` is the default; store only the meaningful value. + todo.lane_owner = owner.filter(|owner| owner != "self"); + } +} + #[cfg(test)] mod tests { use super::*; @@ -258,6 +289,9 @@ pub fn canonicalize_worker_lane( stage: template.stage, group: template.group.clone(), lane_after: template.lane_after.clone(), + // Inside a worker's own list the lane is the worker's: it must + // never re-dispatch it, so ownership stays coordinator-side. + lane_owner: None, ..todo.clone() }) .collect() @@ -290,6 +324,7 @@ pub fn merge_worker_lane( item.stage = template.stage; item.group = template.group.clone(); item.lane_after = template.lane_after.clone(); + item.lane_owner = template.lane_owner.clone(); item.assigned_to = Some(link.worker.clone()); if other_ids.contains(item.id.as_str()) { item.id = format!("{}:{}", link.worker, item.id); diff --git a/crates/jcode-todo-policy/src/tests.rs b/crates/jcode-todo-policy/src/tests.rs index 9b8350abe80..b83843ebbf9 100644 --- a/crates/jcode-todo-policy/src/tests.rs +++ b/crates/jcode-todo-policy/src/tests.rs @@ -610,6 +610,7 @@ fn todo(content: &str, status: &str, group: Option<&str>) -> TodoItem { id: content.to_ascii_lowercase().replace(' ', "-"), group: group.map(str::to_string), lane_after: None, + lane_owner: None, confidence: None, completion_confidence: None, confidence_history: Vec::new(), diff --git a/crates/jcode-tui-markdown/src/markdown_render_support.rs b/crates/jcode-tui-markdown/src/markdown_render_support.rs index b59012ae63c..f6d2e1f1cfb 100644 --- a/crates/jcode-tui-markdown/src/markdown_render_support.rs +++ b/crates/jcode-tui-markdown/src/markdown_render_support.rs @@ -425,13 +425,7 @@ pub(super) fn highlight_code(code: &str, lang: Option<&str>) -> Vec { - let spans: Vec> = ranges - .into_iter() - .map(|(style, text)| { - Span::styled(text.to_string(), syntect_to_ratatui_style(style)) - }) - .collect(); - lines.push(Line::from(spans)); + lines.push(Line::from(syntect_ranges_to_spans(ranges))); } Err(_) => { // Fallback to plain text @@ -446,6 +440,37 @@ pub(super) fn highlight_code(code: &str, lang: Option<&str>) -> Vec) -> Vec> { + let mut spans: Vec> = Vec::with_capacity(ranges.len().min(8)); + for (style, text) in ranges { + let style = syntect_to_ratatui_style(style); + if let Some(last) = spans.last_mut() { + // Whitespace shows no foreground colour, so a whitespace run can + // join either neighbour when background and modifiers match. + let same_ink = last.style.bg == style.bg + && last.style.add_modifier == style.add_modifier + && last.style.sub_modifier == style.sub_modifier + && last.style.underline_color == style.underline_color; + if last.style == style || (same_ink && text.trim().is_empty()) { + last.content.to_mut().push_str(text); + continue; + } + if same_ink && last.content.trim().is_empty() { + last.content.to_mut().push_str(text); + last.style = style; + continue; + } + } + spans.push(Span::styled(text.to_string(), style)); + } + spans.shrink_to_fit(); + spans +} + /// Convert syntect style to ratatui style fn syntect_to_ratatui_style(style: SynStyle) -> Style { let fg = rgb(style.foreground.r, style.foreground.g, style.foreground.b); @@ -465,10 +490,7 @@ pub fn highlight_line(code: &str, ext: Option<&str>) -> Vec> { let mut highlighter = HighlightLines::new(syntax, theme); match highlighter.highlight_line(code, &SYNTAX_SET) { - Ok(ranges) => ranges - .into_iter() - .map(|(style, text)| Span::styled(text.to_string(), syntect_to_ratatui_style(style))) - .collect(), + Ok(ranges) => syntect_ranges_to_spans(ranges), Err(_) => { vec![Span::raw(code.to_string())] } @@ -498,13 +520,7 @@ pub fn highlight_file_lines( if let Ok(ranges) = highlighter.highlight_line(line, &SYNTAX_SET) && line_numbers.contains(&line_num) { - let spans: Vec> = ranges - .into_iter() - .map(|(style, text)| { - Span::styled(text.to_string(), syntect_to_ratatui_style(style)) - }) - .collect(); - results.push((line_num, spans)); + results.push((line_num, syntect_ranges_to_spans(ranges))); } } diff --git a/crates/jcode-tui-markdown/src/markdown_wrap.rs b/crates/jcode-tui-markdown/src/markdown_wrap.rs index fd80433d418..ddad0917ea6 100644 --- a/crates/jcode-tui-markdown/src/markdown_wrap.rs +++ b/crates/jcode-tui-markdown/src/markdown_wrap.rs @@ -191,7 +191,7 @@ pub fn wrap_line( &mut pending_repeated_prefix, ); for piece in &token.word { - current_spans.push(Span::styled(piece.text.clone(), piece.style)); + push_merged_span(&mut current_spans, &piece.text, piece.style); } let width_before = current_width; current_width += token.word_width; @@ -321,7 +321,7 @@ fn append_token_spaces( return; } for piece in &token.spaces { - current_spans.push(Span::styled(piece.text.clone(), piece.style)); + push_merged_span(current_spans, &piece.text, piece.style); } let width_before = *current_width; *current_width += token.space_width; @@ -430,6 +430,19 @@ fn tokenize_balanced_wrap(line: &Line<'static>) -> Option> { Some(tokens) } +/// Append text, extending the previous span when its style matches. Wrapping +/// splits text into word and space tokens; emitting one span per token made +/// wrapped prose and diff rows cost ~56 bytes of span struct per word. +fn push_merged_span(spans: &mut Vec>, text: &str, style: Style) { + if let Some(last) = spans.last_mut() + && last.style == style + { + last.content.to_mut().push_str(text); + return; + } + spans.push(Span::styled(text.to_string(), style)); +} + fn push_piece_char(pieces: &mut Vec, ch: char, style: Style) { if let Some(last) = pieces.last_mut() && last.style == style @@ -513,11 +526,11 @@ fn build_balanced_line_spans(tokens: &[WrapToken]) -> Vec> { let mut spans = Vec::new(); for (idx, token) in tokens.iter().enumerate() { for piece in &token.word { - spans.push(Span::styled(piece.text.clone(), piece.style)); + push_merged_span(&mut spans, &piece.text, piece.style); } if idx + 1 < tokens.len() { for piece in &token.spaces { - spans.push(Span::styled(piece.text.clone(), piece.style)); + push_merged_span(&mut spans, &piece.text, piece.style); } } } diff --git a/crates/jcode-tui-render/src/chrome.rs b/crates/jcode-tui-render/src/chrome.rs index cf24a169b6a..5f809320b6f 100644 --- a/crates/jcode-tui-render/src/chrome.rs +++ b/crates/jcode-tui-render/src/chrome.rs @@ -2,10 +2,21 @@ use ratatui::prelude::*; use ratatui::widgets::{Block, Borders, Paragraph}; pub fn clear_area(frame: &mut Frame, area: Rect) { - for x in area.left()..area.right() { - for y in area.top()..area.bottom() { - frame.buffer_mut()[(x, y)].reset(); - } + let buffer = frame.buffer_mut(); + let area = area.intersection(buffer.area); + if area.is_empty() { + return; + } + let width = usize::from(area.width); + // Full-width areas are one contiguous run of cells. + if area.left() == buffer.area.left() && area.width == buffer.area.width { + let start = buffer.index_of(area.left(), area.top()); + buffer.reset_range(start..start + width * usize::from(area.height)); + return; + } + for y in area.top()..area.bottom() { + let start = buffer.index_of(area.left(), y); + buffer.reset_range(start..start + width); } } diff --git a/crates/jcode-tui-session-picker/src/lib.rs b/crates/jcode-tui-session-picker/src/lib.rs index 240877d9cba..ab0c8aa26b8 100644 --- a/crates/jcode-tui-session-picker/src/lib.rs +++ b/crates/jcode-tui-session-picker/src/lib.rs @@ -61,11 +61,11 @@ pub enum SessionFilterMode { impl SessionFilterMode { pub fn next(self) -> Self { match self { - Self::All => Self::CurrentDir, + Self::All => Self::Cloud, + Self::Cloud => Self::CurrentDir, Self::CurrentDir => Self::CatchUp, Self::CatchUp => Self::Saved, - Self::Saved => Self::Cloud, - Self::Cloud => Self::Active, + Self::Saved => Self::Active, Self::Active => Self::ClaudeCode, Self::ClaudeCode => Self::Codex, Self::Codex => Self::Pi, @@ -81,11 +81,11 @@ impl SessionFilterMode { pub fn previous(self) -> Self { match self { Self::All => Self::Cursor, - Self::CurrentDir => Self::All, + Self::Cloud => Self::All, + Self::CurrentDir => Self::Cloud, Self::CatchUp => Self::CurrentDir, Self::Saved => Self::CatchUp, - Self::Cloud => Self::Saved, - Self::Active => Self::Cloud, + Self::Active => Self::Saved, Self::ClaudeCode => Self::Active, Self::Codex => Self::ClaudeCode, Self::Pi => Self::Codex, @@ -270,13 +270,13 @@ mod tests { use super::*; #[test] - fn filter_cycle_includes_cloud_immediately_after_saved() { + fn filter_cycle_puts_cloud_immediately_after_all() { let cycle = [ SessionFilterMode::All, + SessionFilterMode::Cloud, SessionFilterMode::CurrentDir, SessionFilterMode::CatchUp, SessionFilterMode::Saved, - SessionFilterMode::Cloud, SessionFilterMode::Active, SessionFilterMode::ClaudeCode, SessionFilterMode::Codex, diff --git a/crates/jcode-tui-style/src/theme_mode.rs b/crates/jcode-tui-style/src/theme_mode.rs index 402fae840a3..c7cc248a689 100644 --- a/crates/jcode-tui-style/src/theme_mode.rs +++ b/crates/jcode-tui-style/src/theme_mode.rs @@ -12,8 +12,8 @@ //! the terminal's own (light) default background shows through, exactly like //! it does on dark themes today. //! -//! The mode itself is set once at startup by the TUI's terminal-background -//! detection (OSC 11 query / `JCODE_THEME` / `display.theme` config) and +//! The mode is initialized by the TUI's terminal-background detection and +//! refreshed during auto-theme sessions (OSC 11 / `JCODE_THEME` / config). It //! defaults to dark, which keeps every existing code path byte-identical. use ratatui::buffer::Buffer; @@ -32,8 +32,8 @@ pub enum ThemeMode { static THEME_MODE: AtomicU8 = AtomicU8::new(0); -/// Set the global theme mode. Called once at startup after terminal -/// background detection (and again if the user overrides it). +/// Set the global theme mode at startup and when the terminal background +/// changes during an auto-theme session (or the user overrides it). pub fn set_theme_mode(mode: ThemeMode) { THEME_MODE.store( match mode { diff --git a/crates/jcode-tui/src/tui/app.rs b/crates/jcode-tui/src/tui/app.rs index 51297330296..4262b8a8960 100644 --- a/crates/jcode-tui/src/tui/app.rs +++ b/crates/jcode-tui/src/tui/app.rs @@ -23,11 +23,11 @@ use crate::session::{Session, StoredMessage}; use crate::skill::SkillRegistry; use crate::tool::selfdev::ReloadContext; use crate::tool::{Registry, ToolContext}; +use crate::tui::terminal_events::EventStream; use anyhow::Result; use auth::PendingLogin; use crossterm::event::{ - Event, EventStream, KeyCode, KeyEventKind, KeyModifiers, MouseButton, MouseEvent, - MouseEventKind, + Event, KeyCode, KeyEventKind, KeyModifiers, MouseButton, MouseEvent, MouseEventKind, }; use debug::DebugTrace; use futures::StreamExt; @@ -117,7 +117,9 @@ mod update_sim; mod usage_reset; mod voice_input; pub(crate) use state_ui_input_helpers::{registered_command_entries, registered_command_names}; +mod binary_watch; mod justrust_status; +mod state_ui_history_marker; mod state_ui_maintenance; mod state_ui_messages; mod state_ui_runtime; @@ -130,6 +132,7 @@ mod terminal_setup_command; mod terminal_title; mod todo_completion; mod todos_view; +mod wake_stats; pub(crate) use todos_view::pinned_todos_payload_for_session; mod tui_lifecycle; mod tui_lifecycle_runtime; @@ -429,6 +432,8 @@ struct PendingFallbackOffer { /// one-keypress accept can resend it after the route switch completes. #[derive(Debug, Clone)] struct FallbackResendPayload { + /// Automatic retry budget already spent by this turn, across routes. + retry_attempts: u8, /// Expanded message content that was sent to the server. content: String, /// Inline image attachments that accompanied the message. @@ -941,6 +946,7 @@ pub struct App { display_edit_line_counts: (usize, usize), terminal_title: RefCell, compacted_history_lazy: CompactedHistoryLazyState, + recent_history_paging: state_ui_history_marker::RecentHistoryPagingState, /// When older compacted history has just been loaded, this anchors the /// viewport to the content the reader was looking at so the prepend does not /// visibly jump. Resolved into `scroll_offset` by the next render frame. @@ -1031,6 +1037,9 @@ pub struct App { // Keep recovery identities after dispatch so delayed History cannot requeue // the same continuation. Reset only when a new server reload begins. scheduled_reload_recoveries: HashSet<(String, String)>, + // A reload recovery continuation was queued. It must run as its own turn + // so user follow-ups queued before the reload still wait for that turn to end. + reload_recovery_dispatch_pending: bool, // Current status status: ProcessingStatus, // Subagent status (shown during Task tool execution) @@ -1422,6 +1431,9 @@ pub struct App { /// When `remote_swarm_members` last arrived, so worker stopwatches can /// tick locally between server snapshots. remote_swarm_members_received_at: Option, + /// When each swarm agent last sent us a notification, keyed by sender, so + /// notifications can show time since that agent's previous message. + swarm_last_message_at: std::collections::HashMap, // Latest swarm plan snapshot (local or remote server event stream) swarm_plan_items: Vec, swarm_plan_version: Option, @@ -1601,6 +1613,7 @@ pub struct App { pending_route_selection: Option, pending_automatic_failover_route: Option, pending_automatic_failover_request_id: Option, + pending_automatic_failover_started: Option, // Reasoning-effort variant chosen together with a model in the picker // (e.g. "gpt-5.5 (high)"), staged for remote mode alongside the model // switch. Without forwarding this to the server, it keeps its configured @@ -1652,6 +1665,9 @@ pub struct App { voice_input: Option, // Last voice key press, to tell a held key's auto-repeat from a new press voice_input_last_press: Option, + // Push-to-talk captured outside this client (`jcode companion`), with the + // time it was last updated so a vanished sender cannot leave it stuck. + external_voice: Option<(crate::protocol::VoiceActivity, Instant)>, // Optional configured keybinding for accepting the post-error fallback offer fallback_switch_key: OptionalBinding, // Config reload generation the keybinding snapshot above was parsed at. @@ -1716,6 +1732,11 @@ pub struct App { pending_soft_interrupts: Vec, // Soft interrupts written to the socket but not yet acknowledged by the server. pending_soft_interrupt_requests: Vec<(u64, String)>, + // Every soft interrupt still shown as pending, by request id. Unlike the + // list above this survives the ack, so recovery can name them to the server. + sent_soft_interrupts: Vec<(u64, String)>, + // In-flight by-id cancel used to recover stranded soft interrupts. + soft_interrupt_recovery: Option<(u64, Instant)>, // Whether the current remote turn should trigger autoreview after completion. autoreview_after_current_turn: bool, // Whether the current remote turn should trigger autojudge after completion. diff --git a/crates/jcode-tui/src/tui/app/auth.rs b/crates/jcode-tui/src/tui/app/auth.rs index 84f1feb443b..780f3f09acc 100644 --- a/crates/jcode-tui/src/tui/app/auth.rs +++ b/crates/jcode-tui/src/tui/app/auth.rs @@ -2369,7 +2369,7 @@ impl App { Some(value) => value, None => { self.push_display_message(DisplayMessage::error( - "OpenAI-compatible API base must be https://... or http://localhost." + "OpenAI-compatible API base must be https://..., or http:// with a localhost/.local name or a bare IP address." .to_string(), )); self.pending_login = @@ -3404,7 +3404,7 @@ fn save_tui_openai_compatible_api_base( let trimmed = api_base.trim(); if !trimmed.is_empty() { let normalized = crate::provider_catalog::normalize_api_base(trimmed).ok_or_else(|| { - anyhow::anyhow!("OpenAI-compatible API base must be https://... or http://localhost.") + anyhow::anyhow!("OpenAI-compatible API base must be https://..., or http:// with a localhost/.local name or a bare IP address.") })?; crate::provider_catalog::save_env_value_to_env_file( "JCODE_OPENAI_COMPAT_API_BASE", 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 d0c1fd24d3b..e9ba45dc8dc 100644 --- a/crates/jcode-tui/src/tui/app/auth_account_commands.rs +++ b/crates/jcode-tui/src/tui/app/auth_account_commands.rs @@ -968,7 +968,7 @@ fn save_openai_compat_setting(app: &mut App, setting: OpenAiCompatSetting, value Some(value) => Some(value), None => { app.push_display_message(DisplayMessage::error( - "OpenAI-compatible API base must be https://... or http://localhost." + "OpenAI-compatible API base must be https://..., or http:// with a localhost/.local name or a bare IP address." .to_string(), )); return; diff --git a/crates/jcode-tui/src/tui/app/binary_watch.rs b/crates/jcode-tui/src/tui/app/binary_watch.rs new file mode 100644 index 00000000000..4798f39ba39 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/binary_watch.rs @@ -0,0 +1,123 @@ +//! Event-driven detection of a newer client binary. +//! +//! Every client (local, attached, and `--ssh`) must follow hot reloads, not +//! only the server. Instead of stat-polling, an inotify thread watches the +//! directories whose entries are swapped when a build is published (the +//! `current` channel symlink, the launcher, and the repo self-dev output). +//! Any change raises `PENDING` and wakes the UI loop through the bus; the loop +//! then confirms with `has_newer_binary()` and re-execs once idle. + +use std::sync::atomic::{AtomicBool, Ordering}; + +static PENDING: AtomicBool = AtomicBool::new(false); +static STARTED: AtomicBool = AtomicBool::new(false); + +/// True once since the last call if a watched binary location changed. +pub(super) fn take_pending() -> bool { + PENDING.swap(false, Ordering::AcqRel) +} + +/// Re-arm after a change that could not be acted on yet (busy or typing). +pub(super) fn rearm() { + PENDING.store(true, Ordering::Release); +} + +pub(super) fn ensure_started() { + if STARTED.swap(true, Ordering::AcqRel) { + return; + } + #[cfg(all(target_os = "linux", not(test)))] + { + let mut dirs: Vec = Vec::new(); + if let Ok(path) = crate::build::current_binary_path() + && let Some(dir) = path.parent() + { + dirs.push(dir.to_path_buf()); + } + if let Ok(path) = crate::build::launcher_binary_path() + && let Some(dir) = path.parent() + { + dirs.push(dir.to_path_buf()); + } + if let Some(repo) = crate::build::get_repo_dir() + && let Some(dir) = crate::build::selfdev_binary_path(&repo).parent() + { + dirs.push(dir.to_path_buf()); + } + dirs.sort(); + dirs.dedup(); + if let Err(error) = std::thread::Builder::new() + .name("jcode-bin-watch".to_string()) + .spawn(move || imp::run(dirs)) + { + STARTED.store(false, Ordering::Release); + crate::logging::warn(&format!("Failed to start binary watcher: {error}")); + } + } +} + +#[cfg(all(target_os = "linux", not(test)))] +mod imp { + use std::ffi::CString; + use std::os::unix::ffi::OsStrExt; + use std::path::PathBuf; + use std::sync::atomic::Ordering; + + /// Builds write several files and swap symlinks; wait for quiet. + const DEBOUNCE_MS: i32 = 750; + + pub(super) fn run(dirs: Vec) { + // SAFETY: plain syscalls on an fd we own and close on every exit path. + unsafe { + let fd = libc::inotify_init1(libc::IN_CLOEXEC); + if fd < 0 { + return; + } + let mask = libc::IN_CREATE + | libc::IN_MOVED_TO + | libc::IN_CLOSE_WRITE + | libc::IN_ATTRIB + | libc::IN_DELETE_SELF + | libc::IN_MOVE_SELF; + let mut watched = 0; + for dir in &dirs { + if let Ok(c) = CString::new(dir.as_os_str().as_bytes()) + && libc::inotify_add_watch(fd, c.as_ptr(), mask) >= 0 + { + watched += 1; + } + } + if watched == 0 { + libc::close(fd); + return; + } + let mut buf = [0u8; 4096]; + let mut pfd = libc::pollfd { + fd, + events: libc::POLLIN, + revents: 0, + }; + loop { + // Block indefinitely: zero wakeups while nothing is published. + if libc::poll(&mut pfd, 1, -1) < 0 { + if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted { + continue; + } + break; + } + if libc::read(fd, buf.as_mut_ptr().cast(), buf.len()) <= 0 { + break; + } + // Drain the burst until the publisher goes quiet. + while libc::poll(&mut pfd, 1, DEBOUNCE_MS) > 0 { + if libc::read(fd, buf.as_mut_ptr().cast(), buf.len()) <= 0 { + break; + } + } + super::PENDING.store(true, Ordering::Release); + crate::bus::Bus::global().publish(crate::bus::BusEvent::ClientBinaryChanged); + } + libc::close(fd); + } + } +} diff --git a/crates/jcode-tui/src/tui/app/commands.rs b/crates/jcode-tui/src/tui/app/commands.rs index d0e37d3e74f..674d926a85c 100644 --- a/crates/jcode-tui/src/tui/app/commands.rs +++ b/crates/jcode-tui/src/tui/app/commands.rs @@ -90,6 +90,27 @@ pub(super) fn is_todo_confidence_summary_message(message: &str) -> bool { || message.starts_with("All todos are done. Todo confidence summary:") } +fn is_incomplete_todo_poke(message: &str) -> bool { + let trimmed = message.trim(); + trimmed.starts_with("You have ") + && trimmed + .lines() + .next() + .is_some_and(|line| line.contains(" incomplete todo")) + && is_poke_message(trimmed) +} + +/// An incomplete-todo poke can linger in the queue (for example re-queued +/// after a server busy rejection) while the model finishes every todo. +/// Sending it afterwards nags about finished work, so drop it at dispatch. +pub(super) fn drop_stale_incomplete_todo_pokes(app: &App, messages: &mut Vec) { + if messages.iter().any(|m| is_incomplete_todo_poke(m)) && incomplete_poke_todos(app).is_empty() + { + crate::logging::info("Dropping stale incomplete-todo poke: no open todos remain"); + messages.retain(|m| !is_incomplete_todo_poke(m)); + } +} + pub(super) fn queued_messages_are_only_pokes(messages: &[String]) -> bool { !messages.is_empty() && messages.iter().all(|message| is_poke_message(message)) } diff --git a/crates/jcode-tui/src/tui/app/commands_cloud.rs b/crates/jcode-tui/src/tui/app/commands_cloud.rs index 2d3b17be3d8..bb6943b52db 100644 --- a/crates/jcode-tui/src/tui/app/commands_cloud.rs +++ b/crates/jcode-tui/src/tui/app/commands_cloud.rs @@ -41,6 +41,10 @@ pub(super) enum CloudCommand { Prepare { host: Option, }, + /// Restart the managed cloud daemon onto its selected runtime once idle. + Reload { + force: bool, + }, } pub(super) fn parse_cloud_command(trimmed: &str) -> Option { @@ -58,6 +62,11 @@ pub(super) fn parse_cloud_command(trimmed: &str) -> Option { Some("cancel") => Some(CloudCommand::Cancel), Some("status") | Some("where") => Some(CloudCommand::Where), Some("back") | Some("return") => Some(CloudCommand::Return), + Some("reload") => match (words.next(), words.next()) { + (None, _) => Some(CloudCommand::Reload { force: false }), + (Some("--force"), None) => Some(CloudCommand::Reload { force: true }), + _ => Some(CloudCommand::MoveUsage), + }, Some("replace") => { let (mut host, mut yes, mut dry_run) = (None, false, false); while let Some(word) = words.next() { @@ -227,7 +236,7 @@ pub(super) fn handle_cloud_command(app: &mut App, trimmed: &str, session_id: &st ))); return true; } - if !matches!(command, CloudCommand::Where) { + if !matches!(command, CloudCommand::Where | CloudCommand::Reload { .. }) { if !check_cloud_handoff_input(app, trimmed) { return true; } @@ -363,6 +372,12 @@ fn start_cloud_command( CloudCommand::Where => { cmd.args(["where", "--session", session_id]); } + CloudCommand::Reload { force } => { + cmd.args(["runtime", "reload"]); + if *force { + cmd.arg("--force"); + } + } } if app.cloud_job.is_some() { app.push_display_message(DisplayMessage::error( @@ -1185,6 +1200,11 @@ fn finish_cloud_command(app: &mut App, job: CloudJob, output: std::process::Outp | CloudCommand::MoveUsage => { unreachable!("handled before transfer completion") } + CloudCommand::Reload { .. } => { + app.push_display_message(DisplayMessage::system( + "Cloud daemon reloaded onto its selected runtime. Cloud sessions resume when their clients reconnect.", + )); + } CloudCommand::Where => { let text = if stdout.trim().is_empty() { "This session has never moved.".to_string() diff --git a/crates/jcode-tui/src/tui/app/commands_cloud_tests.rs b/crates/jcode-tui/src/tui/app/commands_cloud_tests.rs index 76babfbd0cd..ddc76bd0485 100644 --- a/crates/jcode-tui/src/tui/app/commands_cloud_tests.rs +++ b/crates/jcode-tui/src/tui/app/commands_cloud_tests.rs @@ -1256,6 +1256,18 @@ fn parses_cloud_commands() { parse_cloud_command("/cloud status"), Some(CloudCommand::Where) ); + assert_eq!( + parse_cloud_command("/cloud reload"), + Some(CloudCommand::Reload { force: false }) + ); + assert_eq!( + parse_cloud_command("/cloud reload --force"), + Some(CloudCommand::Reload { force: true }) + ); + assert_eq!( + parse_cloud_command("/cloud reload now"), + Some(CloudCommand::MoveUsage) + ); assert_eq!( parse_cloud_command("/cloud cancel"), Some(CloudCommand::Cancel) diff --git a/crates/jcode-tui/src/tui/app/debug_cmds.rs b/crates/jcode-tui/src/tui/app/debug_cmds.rs index 72177ca8514..0e4ac714956 100644 --- a/crates/jcode-tui/src/tui/app/debug_cmds.rs +++ b/crates/jcode-tui/src/tui/app/debug_cmds.rs @@ -1146,6 +1146,8 @@ fn attach_redraw_schedule_debug(payload: &mut serde_json::Value, app: &App) { "redraw_schedule".to_string(), serde_json::json!({ "interval_ms": crate::tui::redraw_interval(app).as_millis() as u64, + "tail_catchup_active": crate::tui::ui::tail_catchup_active(), + "stream_reveal_pending": crate::tui::TuiState::stream_reveal_pending(app), "animation_fps": policy.animation_fps, "redraw_fps": policy.redraw_fps, "tier": format!("{:?}", policy.tier), @@ -1154,6 +1156,7 @@ fn attach_redraw_schedule_debug(payload: &mut serde_json::Value, app: &App) { "idle_animation_area": crate::tui::ui::last_idle_animation_area() .map(|a| serde_json::json!([a.x, a.y, a.width, a.height])), "client_focused": crate::tui::TuiState::client_focused(app), + "wake_sources": super::wake_stats::snapshot(), "periodic_redraw_required": crate::tui::periodic_redraw_required(app), // Unlike `idle_animation.last_full_frame_reason`, which is sticky, // this is evaluated against current state, so an expired notice diff --git a/crates/jcode-tui/src/tui/app/helpers.rs b/crates/jcode-tui/src/tui/app/helpers.rs index be857f9ef85..83a7f74c33e 100644 --- a/crates/jcode-tui/src/tui/app/helpers.rs +++ b/crates/jcode-tui/src/tui/app/helpers.rs @@ -2,6 +2,8 @@ mod clipboard_helper; mod git_line_cache; +#[cfg_attr(test, allow(dead_code))] +mod git_watch; pub(crate) mod model_names; use crate::todo::TodoItem; @@ -23,6 +25,61 @@ type GitInfoCacheEntry = (std::time::Instant, Option, bool); static GIT_INFO_CACHE: Mutex> = Mutex::new(None); static GIT_LINE_CACHE: std::sync::LazyLock> = std::sync::LazyLock::new(|| Mutex::new(git_line_cache::GitLineCache::default())); +/// Cheap `.git` metadata stamp from the last full probe, plus when that probe +/// ran. While the stamp is unchanged, TTL expiry only re-stats a few files +/// instead of spawning six git processes every 5s per client. +static GIT_PROBE_STAMP: Mutex> = Mutex::new(None); + +/// Full probes still run this often with an unchanged stamp, because editor +/// writes to tracked files do not touch `.git` until something stages them. +#[cfg(not(test))] +const GIT_FULL_PROBE_INTERVAL: Duration = Duration::from_secs(60); + +type GitStamp = Vec<(std::path::PathBuf, Option<(std::time::SystemTime, u64)>)>; + +/// mtimes of the files that change on commit, checkout, stage, fetch, and +/// branch updates. `None` when the git dir cannot be resolved cheaply. +fn git_stamp(repo_root: &std::path::Path) -> Option { + let git_dir = repo_root.join(".git"); + if !git_dir.is_dir() { + return None; + } + let mut paths = vec![ + git_dir.join("HEAD"), + git_dir.join("index"), + git_dir.join("FETCH_HEAD"), + git_dir.join("packed-refs"), + ]; + if let Ok(head) = std::fs::read_to_string(git_dir.join("HEAD")) + && let Some(reference) = head.trim().strip_prefix("ref: ") + { + paths.push(git_dir.join(reference)); + if let Some(branch) = reference.strip_prefix("refs/heads/") { + paths.push(git_dir.join("refs/remotes/origin").join(branch)); + } + } + Some( + paths + .into_iter() + .map(|path| { + let meta = match std::fs::metadata(&path).and_then(|m| Ok((m.modified()?, m.len()))) + { + Ok(meta) => Some(meta), + Err(error) => { + if error.kind() != std::io::ErrorKind::NotFound { + crate::logging::debug(&format!( + "Git metadata probe failed for {}: {error}", + path.display() + )); + } + None + } + }; + (path, meta) + }) + .collect(), + ) +} /// Stale-while-revalidate cache for per-session todos plus their goal-level /// assessments (closed feedback loop etc.). Module-level so the app can force a @@ -67,6 +124,7 @@ pub(crate) fn backdated_now(amount: Duration) -> std::time::Instant { /// immediately rather than after the 5s TTL. Stale-while-revalidate still /// applies: the next read returns the last value and kicks a background refresh. pub(crate) fn invalidate_git_info_cache() { + crate::tui::cloud_git::refresh_soon(); if let Ok(mut guard) = GIT_INFO_CACHE.lock() && let Some((ts, _cached, refreshing)) = guard.as_mut() { @@ -76,6 +134,9 @@ pub(crate) fn invalidate_git_info_cache() { *ts = backdated_now(Duration::from_secs(3600)); *refreshing = false; } + if let Ok(mut stamp) = GIT_PROBE_STAMP.lock() { + *stamp = None; + } } /// Pin the git-status widget to a fixed value for deterministic renders. @@ -381,12 +442,225 @@ pub(crate) fn stop_capturing_clipboard_for_tests() { } } -/// Copy text to clipboard. On Windows and macOS, the native clipboard API -/// (arboard) is authoritative, with OSC 52 as a remote-session fallback. -/// Elsewhere, try wl-copy (Wayland), then xclip/xsel (X11, which keep owning -/// the selection unlike arboard), then arboard, then OSC 52 as the -/// remote-session fallback (SSH / Docker / tmux). -pub(super) fn copy_to_clipboard(text: &str) -> bool { +/// True when this process is running inside an SSH session, i.e. the user's +/// machine (and clipboard) is elsewhere. In that case the *local* clipboard +/// belongs to the server, and the only path to the user's clipboard is an +/// OSC 52 sequence emitted through the terminal (and forwarded by the SSH +/// client to the client-side terminal emulator). +#[cfg_attr(test, allow(dead_code))] +fn is_remote_ssh_session() -> bool { + std::env::var("SSH_CONNECTION").is_ok() || std::env::var("SSH_TTY").is_ok() +} + +/// Runs the session-appropriate copy chain and reports its outcome. +/// +/// The *order* is the contract (it is what the original bug violated): +/// +/// - **Remote (SSH) session**: OSC 52 runs first, because it is the only path +/// to the user's (client-side) clipboard, and the native server-side +/// clipboard (NSPasteboard / Win32) would otherwise succeed headless and +/// short-circuit the chain. The local clipboard is then also filled so +/// paste inside jcode keeps working. If OSC 52 is unavailable (e.g. stdout +/// is not a TTY), the local clipboard is still the fallback. +/// - **Local session**: the native clipboard is authoritative and OSC 52 only +/// runs if it fails (a working native clipboard must not emit terminal +/// escape sequences). +/// +/// The writers are closures so the order can be unit-tested with stubs, +/// without touching any real clipboard. +fn run_copy_chain( + remote: bool, + mut osc52: impl FnMut() -> bool, + mut local: impl FnMut() -> bool, +) -> bool { + if remote { + if osc52() { + // Keep the local clipboard in sync so paste inside jcode + // (which reads the server-side clipboard) keeps working. + let _ = local(); + return true; + } + // OSC 52 did not go through (e.g. stdout is not a TTY): still try the + // local clipboard rather than failing outright. + return local(); + } + if local() { + return true; + } + osc52() +} + +#[cfg(test)] +mod copy_chain_tests { + use super::run_copy_chain; + use std::cell::RefCell; + + /// Records the order in which the clipboard writers are invoked, so the + /// ordering contract of [`run_copy_chain`] is pinned by tests: a + /// regression that writes the server-side clipboard before OSC 52 in an + /// SSH session (the original bug) fails here. + struct OrderLog(RefCell>); + + impl OrderLog { + fn writer(&self, label: &'static str, succeeds: bool) -> impl FnMut() -> bool + '_ { + let log = &self.0; + move || { + log.borrow_mut().push(label); + succeeds + } + } + + fn events(&self) -> Vec<&'static str> { + self.0.borrow().clone() + } + } + + #[test] + fn ssh_session_writes_osc52_first_then_local() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(run_copy_chain( + true, + log.writer("osc52", true), + log.writer("local", true), + )); + assert_eq!(log.events(), ["osc52", "local"]); + } + + #[test] + fn ssh_session_still_writes_local_when_osc52_fails() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(run_copy_chain( + true, + log.writer("osc52", false), + log.writer("local", true), + )); + assert_eq!(log.events(), ["osc52", "local"]); + } + + #[test] + fn ssh_session_reports_failure_when_both_writers_fail() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(!run_copy_chain( + true, + log.writer("osc52", false), + log.writer("local", false), + )); + assert_eq!(log.events(), ["osc52", "local"]); + } + + #[test] + fn local_session_prefers_native_clipboard_and_skips_osc52() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(run_copy_chain( + false, + log.writer("osc52", true), + log.writer("local", true), + )); + // A working native clipboard must not emit terminal escape sequences. + assert_eq!(log.events(), ["local"]); + } + + #[test] + fn local_session_falls_back_to_osc52_when_native_fails() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(run_copy_chain( + false, + log.writer("osc52", true), + log.writer("local", false), + )); + assert_eq!(log.events(), ["local", "osc52"]); + } + + #[test] + fn local_session_reports_failure_when_both_writers_fail() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(!run_copy_chain( + false, + log.writer("osc52", false), + log.writer("local", false), + )); + assert_eq!(log.events(), ["local", "osc52"]); + } +} + +/// Fill the *local* (server-side) clipboard using the platform-native chain. +/// +/// - Windows: Win32 via arboard, which is authoritative there (issue #497). +/// - macOS: NSPasteboard via arboard, with pbcopy as a belt-and-braces +/// fallback (Terminal.app ignores OSC 52, so the native path must win +/// locally). +/// - Linux: wl-copy (Wayland), then xclip/xsel (X11, which fork and keep +/// owning the selection unlike arboard, whose X11 backend reverts the +/// selection on drop - issue #684), then arboard. +#[cfg(not(test))] +fn copy_to_local_clipboard(text: &str) -> bool { + #[cfg(windows)] + { + arboard::Clipboard::new() + .and_then(|mut cb| cb.set_text(text.to_string())) + .is_ok() + } + + #[cfg(target_os = "macos")] + { + copy_to_local_macos_clipboard(text) + } + + #[cfg(not(any(windows, target_os = "macos")))] + { + if clipboard_helper::copy_via_clipboard_helper("wl-copy", &[], text) { + return true; + } + if clipboard_helper::copy_via_clipboard_helper("xclip", &["-selection", "clipboard"], text) + { + return true; + } + if clipboard_helper::copy_via_clipboard_helper("xsel", &["--clipboard", "--input"], text) { + return true; + } + arboard::Clipboard::new() + .and_then(|mut cb| cb.set_text(text.to_string())) + .is_ok() + } +} + +#[cfg(all(not(test), target_os = "macos"))] +fn copy_to_local_macos_clipboard(text: &str) -> bool { + if arboard::Clipboard::new() + .and_then(|mut cb| cb.set_text(text.to_string())) + .is_ok() + { + return true; + } + if let Ok(mut child) = std::process::Command::new("pbcopy") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + { + use std::io::Write; + if let Some(stdin) = child.stdin.as_mut() + && stdin.write_all(text.as_bytes()).is_ok() + { + drop(child.stdin.take()); + if child.wait().map(|s| s.success()).unwrap_or(false) { + return true; + } + } + } + false +} + +/// Copy text to clipboard. +/// +/// The ordering contract lives in [`run_copy_chain`]: in a remote (SSH) +/// session OSC 52 is emitted first, because the platform-native clipboards +/// succeed even headless and would otherwise short-circuit the only path to +/// the user's own clipboard; the local clipboard is then also filled so +/// paste inside jcode keeps working. In a local session the native clipboard +/// is authoritative and OSC 52 is the final fallback (see +/// [`copy_to_local_clipboard`] for the per-platform native chain). +pub(crate) fn copy_to_clipboard(text: &str) -> bool { // Under test, never touch the OS clipboard. Beyond making results identical // on a desktop and a headless runner, the Linux path below spawns `wl-copy`, // which forks a clipboard server that does not exit; waiting on it hangs the @@ -412,102 +686,15 @@ pub(super) fn copy_to_clipboard(text: &str) -> bool { #[cfg(not(test))] { - // On Windows, the native clipboard API must run before OSC 52. Writing an - // OSC 52 sequence to stdout "succeeds" even when the console (conhost, - // older Windows Terminal) silently ignores it, which reported "Copied" - // while leaving the clipboard empty (issue #497). arboard talks to the - // Win32 clipboard directly and is authoritative there. - #[cfg(windows)] - { - if arboard::Clipboard::new() - .and_then(|mut cb| cb.set_text(text.to_string())) - .is_ok() - { - return true; - } - return copy_to_clipboard_osc52(text); - } - - // Same class of bug on macOS: Apple Terminal (Terminal.app) silently - // ignores OSC 52, yet writing the sequence to stdout "succeeds", so we - // reported "Copied" while leaving the clipboard untouched. NSPasteboard - // via arboard (with pbcopy as a belt-and-braces fallback) is authoritative - // for local sessions; OSC 52 remains as the final remote-session fallback. - #[cfg(target_os = "macos")] - { - if arboard::Clipboard::new() - .and_then(|mut cb| cb.set_text(text.to_string())) - .is_ok() - { - return true; - } - if let Ok(mut child) = std::process::Command::new("pbcopy") - .stdin(std::process::Stdio::piped()) - .stdout(std::process::Stdio::null()) - .stderr(std::process::Stdio::null()) - .spawn() - { - use std::io::Write; - if let Some(stdin) = child.stdin.as_mut() - && stdin.write_all(text.as_bytes()).is_ok() - { - drop(child.stdin.take()); - if child.wait().map(|s| s.success()).unwrap_or(false) { - return true; - } - } - } - copy_to_clipboard_osc52(text) - } - - // Linux has the same failure class (issue #504, Kali/X11): wl-copy fails - // outside Wayland, and many terminals (xterm, older VTE) silently ignore - // OSC 52 while the stdout write still "succeeds", so the arboard fallback - // never ran. Prefer native clipboards when a display is available: - // wl-copy (Wayland), then xclip/xsel (X11, which keep owning the - // selection), then arboard, and only then OSC 52 for genuinely - // headless/remote sessions (SSH, Docker, tmux) where the native paths - // fail fast for lack of a display server. - #[cfg(not(any(windows, target_os = "macos")))] - { - if clipboard_helper::copy_via_clipboard_helper("wl-copy", &[], text) { - return true; - } - // X11: prefer xclip/xsel over arboard. arboard's X11 backend sets the - // selection on a connection it owns and then closes it when the - // `Clipboard` is dropped, so the selection owner disappears and the - // clipboard silently reverts (issue #684) even though `set_text` - // returned Ok. xclip and xsel fork a background process that keeps - // owning the selection until a paste, which is what users expect. - if clipboard_helper::copy_via_clipboard_helper( - "xclip", - &["-selection", "clipboard"], - text, - ) { - return true; - } - if clipboard_helper::copy_via_clipboard_helper( - "xsel", - &["--clipboard", "--input"], - text, - ) { - return true; - } - if arboard::Clipboard::new() - .and_then(|mut cb| cb.set_text(text.to_string())) - .is_ok() - { - return true; - } - copy_to_clipboard_osc52(text) - } + run_copy_chain( + is_remote_ssh_session(), + || copy_to_clipboard_osc52(text), + || copy_to_local_clipboard(text), + ) } } -/// Copy to clipboard using the OSC 52 terminal escape sequence. This asks the -/// terminal emulator to set the system clipboard without needing a local -/// display server, making it work over SSH, inside Docker, and under tmux -/// (with `set -g set-clipboard on`). Returns false if stdout is not a TTY. +/// Copy text to the clipboard using the OSC 52 terminal escape sequence. #[cfg_attr(test, allow(dead_code))] fn copy_to_clipboard_osc52(text: &str) -> bool { use base64::Engine as _; @@ -1236,6 +1423,22 @@ pub(super) fn gather_git_info() -> Option { if *refreshing { return cached.clone(); } + // The inotify watcher refreshes on change. Nothing to poll. + if git_watch::is_healthy() { + *ts = std::time::Instant::now(); + return cached.clone(); + } + // Nothing in .git moved and the last full probe is recent: + // extend the entry without spawning any git process. + if let Some(root) = cached.as_ref().and_then(|info| info.repo_root.as_deref()) + && let Ok(stamp_guard) = GIT_PROBE_STAMP.lock() + && let Some((probed_at, stamp)) = stamp_guard.as_ref() + && probed_at.elapsed() < GIT_FULL_PROBE_INTERVAL + && git_stamp(root).as_ref() == Some(stamp) + { + *ts = std::time::Instant::now(); + return cached.clone(); + } let stale = cached.clone(); *refreshing = true; spawn_git_info_refresh(); @@ -1256,7 +1459,36 @@ pub(super) fn gather_git_info() -> Option { #[cfg(not(test))] fn spawn_git_info_refresh() { std::thread::spawn(|| { + run_git_info_refresh(); + }); +} + +/// Watcher callback: probe now, on the watcher thread (it is already +/// debounced and off the render path). +#[cfg(not(test))] +fn git_watch_changed() { + if let Ok(mut guard) = GIT_INFO_CACHE.lock() + && let Some((_, _, refreshing)) = guard.as_mut() + { + *refreshing = true; + } + run_git_info_refresh(); +} + +#[cfg(not(test))] +fn run_git_info_refresh() { + { let result = gather_git_info_inner(); + if let Some(root) = result.as_ref().and_then(|info| info.repo_root.as_deref()) { + git_watch::ensure_started(root, git_watch_changed); + } + if let Ok(mut stamp) = GIT_PROBE_STAMP.lock() { + *stamp = result + .as_ref() + .and_then(|info| info.repo_root.as_deref()) + .and_then(git_stamp) + .map(|s| (std::time::Instant::now(), s)); + } let changed = if let Ok(mut guard) = GIT_INFO_CACHE.lock() { let changed = guard .as_ref() @@ -1269,7 +1501,7 @@ fn spawn_git_info_refresh() { if changed { crate::bus::Bus::global().publish(crate::bus::BusEvent::GitInfoRefreshed); } - }); + } } /// Start the git probe and give it up to `budget` to land before returning. @@ -1289,10 +1521,13 @@ pub(crate) fn prime_git_info(budget: Duration) { let _ = gather_git_info(); let deadline = std::time::Instant::now() + budget; loop { - let pending = GIT_INFO_CACHE - .lock() - .ok() - .is_some_and(|guard| guard.as_ref().is_some_and(|(_, _, refreshing)| *refreshing)); + let pending = match GIT_INFO_CACHE.lock() { + Ok(guard) => guard.as_ref().is_some_and(|(_, _, refreshing)| *refreshing), + Err(error) => { + crate::logging::warn(&format!("Git warmup cache lock poisoned: {error}")); + false + } + }; if !pending || std::time::Instant::now() >= deadline { return; } @@ -1803,3 +2038,20 @@ pub(crate) fn porcelain_status_letter(index: u8, worktree: u8) -> char { #[cfg(test)] #[path = "helpers/account_notice_tests.rs"] mod account_notice_tests; + +#[cfg(test)] +mod git_stamp_tests { + #[test] + fn stamp_changes_when_head_ref_moves() { + let dir = tempfile::tempdir().unwrap(); + let git = dir.path().join(".git"); + std::fs::create_dir_all(git.join("refs/heads")).unwrap(); + std::fs::write(git.join("HEAD"), "ref: refs/heads/main\n").unwrap(); + std::fs::write(git.join("refs/heads/main"), "a\n").unwrap(); + let before = super::git_stamp(dir.path()).unwrap(); + assert_eq!(super::git_stamp(dir.path()).unwrap(), before); + std::fs::write(git.join("refs/heads/main"), "bb\n").unwrap(); + assert_ne!(super::git_stamp(dir.path()).unwrap(), before); + assert!(super::git_stamp(&dir.path().join("missing")).is_none()); + } +} diff --git a/crates/jcode-tui/src/tui/app/helpers/git_watch.rs b/crates/jcode-tui/src/tui/app/helpers/git_watch.rs new file mode 100644 index 00000000000..2c10ef597b6 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/helpers/git_watch.rs @@ -0,0 +1,403 @@ +//! Event-driven git status refresh (Linux inotify). +//! +//! Watches the repository's `.git` metadata (HEAD, index, refs) plus every +//! non-ignored work-tree directory. Any relevant change schedules one debounced +//! probe. While the watcher is healthy the git widget never polls, so an idle +//! client spawns zero processes and wakes the CPU zero times. +//! +//! Falls back to the stamp-gated polling in `helpers.rs` when inotify is +//! unavailable, the kernel watch limit is hit, or the event queue overflows. + +use std::path::Path; +use std::sync::atomic::{AtomicBool, Ordering}; + +/// True while a watcher is running and has not lost events. +static HEALTHY: AtomicBool = AtomicBool::new(false); +static STARTED: AtomicBool = AtomicBool::new(false); + +pub(super) fn is_healthy() -> bool { + HEALTHY.load(Ordering::Acquire) +} + +/// Start the watcher for `repo_root` once per process. `on_change` runs on the +/// watcher thread after a quiet period following any relevant event. +#[cfg_attr( + not(target_os = "linux"), + expect( + unused_variables, + reason = "inotify watcher is only implemented on Linux" + ) +)] +pub(super) fn ensure_started(repo_root: &Path, on_change: fn()) { + if STARTED.swap(true, Ordering::AcqRel) { + return; + } + #[cfg(target_os = "linux")] + { + let root = repo_root.to_path_buf(); + let spawned = std::thread::Builder::new() + .name("jcode-git-watch".to_string()) + .spawn(move || imp::run(&root, on_change)); + if spawned.is_err() { + HEALTHY.store(false, Ordering::Release); + } + } +} + +/// Whether an event on `name` inside the git dir can change the widget. +/// Lock files and temp objects churn during every git command. +pub(super) fn git_dir_event_relevant(name: &str) -> bool { + !(name.ends_with(".lock") || name.starts_with("tmp_") || name == "ORIG_HEAD.lock") +} + +/// Directories never worth watching even if not gitignored. +pub(super) fn skip_dir(name: &str) -> bool { + name == ".git" +} + +#[cfg(target_os = "linux")] +mod imp { + use super::{HEALTHY, git_dir_event_relevant, skip_dir}; + use std::collections::{BTreeSet, HashMap}; + use std::ffi::CString; + use std::os::unix::ffi::OsStrExt; + use std::path::{Path, PathBuf}; + use std::sync::atomic::Ordering; + use std::time::Duration; + + /// Coalesce bursts (a checkout touches thousands of files) into one probe. + const DEBOUNCE: Duration = Duration::from_millis(250); + + const WORKTREE_MASK: u32 = libc::IN_CLOSE_WRITE + | libc::IN_CREATE + | libc::IN_DELETE + | libc::IN_MOVED_FROM + | libc::IN_MOVED_TO + | libc::IN_DELETE_SELF + | libc::IN_ONLYDIR; + const GIT_MASK: u32 = WORKTREE_MASK; + + struct Watcher { + fd: i32, + dirs: HashMap, // wd -> (path, is_git_metadata) + root: PathBuf, + } + + impl Watcher { + fn add(&mut self, path: &Path, git_meta: bool) -> bool { + let Ok(c) = CString::new(path.as_os_str().as_bytes()) else { + return true; + }; + let mask = if git_meta { GIT_MASK } else { WORKTREE_MASK }; + // SAFETY: valid fd and NUL-terminated path. + let wd = unsafe { libc::inotify_add_watch(self.fd, c.as_ptr(), mask) }; + if wd < 0 { + let err = std::io::Error::last_os_error(); + // ENOSPC: out of watches. Anything else (ENOENT race) is benign. + return err.raw_os_error() != Some(libc::ENOSPC); + } + self.dirs.insert(wd, (path.to_path_buf(), git_meta)); + true + } + + fn add_tree(&mut self, dirs: impl IntoIterator) -> bool { + dirs.into_iter().all(|dir| self.add(&dir, false)) + } + } + + impl Drop for Watcher { + fn drop(&mut self) { + // SAFETY: closing our own fd. + unsafe { libc::close(self.fd) }; + } + } + + fn git(root: &Path, args: &[&str]) -> Option { + let out = match std::process::Command::new("git") + .current_dir(root) + .env("GIT_OPTIONAL_LOCKS", "0") + .args(args) + .stdin(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .output() + { + Ok(output) => output, + Err(error) => { + crate::logging::debug(&format!("Git watcher command failed: {error}")); + return None; + } + }; + out.status + .success() + .then(|| String::from_utf8_lossy(&out.stdout).into_owned()) + } + + /// Directories holding tracked or untracked-but-not-ignored files, plus + /// all their ancestors. One git call instead of walking ignored trees + /// such as `target/` or `node_modules/`. + fn worktree_dirs(root: &Path) -> Option> { + let listing = git( + root, + &[ + "ls-files", + "-z", + "--cached", + "--others", + "--exclude-standard", + ], + )?; + let mut dirs = BTreeSet::from([root.to_path_buf()]); + for file in listing.split('\0').filter(|f| !f.is_empty()) { + let mut dir = Path::new(file).parent(); + while let Some(d) = dir.filter(|d| !d.as_os_str().is_empty()) { + if !dirs.insert(root.join(d)) { + break; + } + dir = d.parent(); + } + } + Some(dirs) + } + + fn git_meta_dirs(git_dir: &Path) -> Vec { + let mut out = vec![git_dir.to_path_buf()]; + let mut stack = vec![git_dir.join("refs")]; + while let Some(dir) = stack.pop() { + if let Ok(entries) = std::fs::read_dir(&dir) { + for entry in entries.flatten() { + if entry.file_type().is_ok_and(|t| t.is_dir()) { + stack.push(entry.path()); + } + } + out.push(dir); + } + } + out + } + + fn is_ignored(root: &Path, path: &Path) -> bool { + let Ok(rel) = path.strip_prefix(root) else { + return true; + }; + std::process::Command::new("git") + .current_dir(root) + .env("GIT_OPTIONAL_LOCKS", "0") + .args(["check-ignore", "-q", "--"]) + .arg(rel) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status() + .is_ok_and(|s| s.success()) + } + + pub(super) fn run(root: &Path, on_change: fn()) { + let git_dir = root.join(".git"); + if !git_dir.is_dir() { + // Worktrees/submodules use a .git file; keep polling there. + return; + } + // SAFETY: plain syscall. + let fd = unsafe { libc::inotify_init1(libc::IN_CLOEXEC) }; + if fd < 0 { + return; + } + let mut w = Watcher { + fd, + dirs: HashMap::new(), + root: root.to_path_buf(), + }; + let ok = git_meta_dirs(&git_dir).iter().all(|d| w.add(d, true)) + && worktree_dirs(root).is_some_and(|dirs| w.add_tree(dirs)); + if !ok { + crate::logging::info("git_watch: inotify watch limit reached; using polling"); + return; + } + crate::logging::info(&format!( + "git_watch: watching {} directories under {}", + w.dirs.len(), + root.display() + )); + HEALTHY.store(true, Ordering::Release); + + let mut buf = vec![0u8; 64 * 1024]; + let mut pending = false; + loop { + // Block indefinitely when idle; once dirty, wait out the debounce. + let timeout = if pending { + DEBOUNCE.as_millis() as i32 + } else { + -1 + }; + let mut pfd = libc::pollfd { + fd: w.fd, + events: libc::POLLIN, + revents: 0, + }; + // SAFETY: one valid pollfd. + let ready = unsafe { libc::poll(&mut pfd, 1, timeout) }; + if ready < 0 { + if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted { + continue; + } + break; + } + if ready == 0 { + pending = false; + on_change(); + continue; + } + // SAFETY: reading into our buffer. + let n = unsafe { libc::read(w.fd, buf.as_mut_ptr().cast(), buf.len()) }; + if n <= 0 { + break; + } + let mut off = 0usize; + let header = std::mem::size_of::(); + let mut new_dirs = Vec::new(); + while off + header <= n as usize { + // SAFETY: kernel guarantees a full event at this offset. + let ev: libc::inotify_event = + unsafe { std::ptr::read_unaligned(buf.as_ptr().add(off).cast()) }; + let name_bytes = &buf[off + header..off + header + ev.len as usize]; + let name_end = name_bytes + .iter() + .position(|b| *b == 0) + .unwrap_or(name_bytes.len()); + let name = String::from_utf8_lossy(&name_bytes[..name_end]).into_owned(); + off += header + ev.len as usize; + + if ev.mask & libc::IN_Q_OVERFLOW != 0 { + // Lost events: refresh now and stay healthy; a probe + // reads full state anyway. + pending = true; + continue; + } + if ev.mask & libc::IN_IGNORED != 0 { + w.dirs.remove(&ev.wd); + continue; + } + let Some((dir, git_meta)) = w.dirs.get(&ev.wd).cloned() else { + continue; + }; + if git_meta { + if git_dir_event_relevant(&name) { + pending = true; + } + if ev.mask & libc::IN_ISDIR != 0 + && ev.mask & (libc::IN_CREATE | libc::IN_MOVED_TO) != 0 + { + new_dirs.push((dir.join(&name), true)); + } + continue; + } + if skip_dir(&name) { + continue; + } + if ev.mask & libc::IN_ISDIR != 0 + && ev.mask & (libc::IN_CREATE | libc::IN_MOVED_TO) != 0 + { + new_dirs.push((dir.join(&name), false)); + } + pending = true; + } + for (dir, git_meta) in new_dirs { + if !git_meta && is_ignored(&w.root, &dir) { + continue; + } + if !w.add(&dir, git_meta) { + crate::logging::info("git_watch: watch limit reached; falling back to polling"); + HEALTHY.store(false, Ordering::Release); + return; + } + // Files may have landed before the watch existed. + if !git_meta && let Some(sub) = worktree_dirs(&dir) { + let root = w.root.clone(); + let extra: Vec<_> = sub + .into_iter() + .filter(|d| d.starts_with(&root) && d != &dir) + .collect(); + if !w.add_tree(extra) { + HEALTHY.store(false, Ordering::Release); + return; + } + } + } + } + HEALTHY.store(false, Ordering::Release); + } + + #[cfg(test)] + mod tests { + use std::process::Command; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::time::{Duration, Instant}; + + static HITS: AtomicUsize = AtomicUsize::new(0); + fn bump() { + HITS.fetch_add(1, Ordering::SeqCst); + } + + fn wait_for(target: usize) -> bool { + let deadline = Instant::now() + Duration::from_secs(5); + while Instant::now() < deadline { + if HITS.load(Ordering::SeqCst) >= target { + return true; + } + std::thread::sleep(Duration::from_millis(20)); + } + false + } + + #[test] + fn fires_on_worktree_and_git_changes_but_not_ignored_dirs() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().to_path_buf(); + let git = |args: &[&str]| { + assert!( + Command::new("git") + .current_dir(&root) + .args(args) + .output() + .unwrap() + .status + .success() + ); + }; + git(&["init", "-q"]); + std::fs::write(root.join(".gitignore"), "target/\n").unwrap(); + std::fs::create_dir_all(root.join("src")).unwrap(); + std::fs::create_dir_all(root.join("target")).unwrap(); + std::fs::write(root.join("src/a.txt"), "a").unwrap(); + + let r = root.clone(); + std::thread::spawn(move || super::run(&r, bump)); + let deadline = Instant::now() + Duration::from_secs(5); + while !super::HEALTHY.load(Ordering::Acquire) && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } + assert!(super::HEALTHY.load(Ordering::Acquire)); + + // Ignored dir: no event. + std::fs::write(root.join("target/junk"), "x").unwrap(); + std::thread::sleep(Duration::from_millis(600)); + assert_eq!(HITS.load(Ordering::SeqCst), 0); + + // Unstaged edit in a nested dir. + std::fs::write(root.join("src/a.txt"), "b").unwrap(); + assert!(wait_for(1)); + + // New directory created after start is picked up. + std::fs::create_dir_all(root.join("src/new")).unwrap(); + std::thread::sleep(Duration::from_millis(400)); + let before = HITS.load(Ordering::SeqCst); + std::fs::write(root.join("src/new/b.txt"), "b").unwrap(); + assert!(wait_for(before + 1)); + + // Git metadata change (staging). + let before = HITS.load(Ordering::SeqCst); + git(&["add", "-A"]); + assert!(wait_for(before + 1)); + } + } +} diff --git a/crates/jcode-tui/src/tui/app/inline_interactive.rs b/crates/jcode-tui/src/tui/app/inline_interactive.rs index c0a4f76851c..a5bfdda67d8 100644 --- a/crates/jcode-tui/src/tui/app/inline_interactive.rs +++ b/crates/jcode-tui/src/tui/app/inline_interactive.rs @@ -2787,6 +2787,13 @@ impl App { ); match recv_result { + Ok(session_picker::SessionListUpdate::CloudUnavailable(warning)) => { + if picker_active && let Some(picker) = self.session_picker_overlay.as_ref() { + picker.borrow_mut().set_cloud_warning(warning); + return true; + } + false + } Ok(session_picker::SessionListUpdate::Cached((server_groups, orphan_sessions))) => { if picker_active { let changed = self.apply_loaded_session_picker(server_groups, orphan_sessions); @@ -2957,6 +2964,15 @@ impl App { } }; + if let ResumeTarget::JcodeSession { session_id } = &resolved_target + && crate::session_window::apply_resume_open_elsewhere(session_id) + == crate::session_window::OpenElsewhereOutcome::Moved + { + spawned += 1; + names.push(name); + continue; + } + match spawn_resume_target_in_new_terminal(&resolved_target, &cwd, socket.as_deref()) { Ok(true) => { spawned += 1; @@ -3126,6 +3142,15 @@ impl App { return; } + if crate::session_window::apply_resume_open_elsewhere(&session_id) + == crate::session_window::OpenElsewhereOutcome::Moved + { + self.session_picker_overlay = None; + self.session_picker_mode = SessionPickerMode::Resume; + self.set_status_notice(format!("Brought {} here", name)); + return; + } + if targets.len() > 1 { self.push_display_message(DisplayMessage::system(format!( "Selected {} sessions; resuming {} in this terminal.", diff --git a/crates/jcode-tui/src/tui/app/input.rs b/crates/jcode-tui/src/tui/app/input.rs index 74645559ccf..5f05c703125 100644 --- a/crates/jcode-tui/src/tui/app/input.rs +++ b/crates/jcode-tui/src/tui/app/input.rs @@ -16,10 +16,11 @@ use crate::bus::{ Bus, BusEvent, ClipboardPasteCompleted, ClipboardPasteContent, ClipboardPasteKind, InputShellCompleted, }; +use crate::tui::terminal_events::EventStream; use crate::util::truncate_str; use anyhow::Result; use base64::Engine; -use crossterm::event::{EventStream, KeyCode, KeyEvent, KeyModifiers}; +use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use ratatui::DefaultTerminal; use std::path::PathBuf; use std::process::Stdio; @@ -3435,6 +3436,30 @@ impl App { self.copy_badge_ui.key_active = Some((key, expiry)); } + /// Everything a key *release* can change on screen: the held-modifier and + /// held-key copy badges and the voice-input phase. Compared before and + /// after a release so releases that change none of it skip the full + /// frame. Under kitty's keyboard protocol every keystroke also reports a + /// release, which doubled the frames drawn while typing. + pub(super) fn key_release_visible_state( + &self, + ) -> (bool, bool, Option, Option) { + // Badges render only through the `*_is_active` gates (the held key + // only shows under Alt+Shift), so compare those, not raw fields. + let now = std::time::Instant::now(); + let badge = &self.copy_badge_ui; + let shift = badge.shift_is_active(now); + ( + badge.alt_is_active(now), + shift, + badge + .key_active + .filter(|(_, expires_at)| shift && *expires_at > now) + .map(|(c, _)| c.to_ascii_lowercase()), + self.voice_input.as_ref().map(|voice| voice.phase), + ) + } + pub(super) fn record_copy_badge_feedback(&mut self, key: char, success: bool) { self.copy_badge_ui.copied_feedback = Some(crate::tui::app::CopyBadgeFeedback { key, diff --git a/crates/jcode-tui/src/tui/app/local.rs b/crates/jcode-tui/src/tui/app/local.rs index 67d4aa1e560..98b48b82ef7 100644 --- a/crates/jcode-tui/src/tui/app/local.rs +++ b/crates/jcode-tui/src/tui/app/local.rs @@ -8,8 +8,9 @@ use crate::message::{ format_background_task_notification_markdown, }; use crate::session::StoredDisplayRole; +use crate::tui::terminal_events::EventStream; use anyhow::Result; -use crossterm::event::{Event, EventStream, KeyEventKind}; +use crossterm::event::{Event, KeyEventKind}; use ratatui::DefaultTerminal; use std::time::{Duration, Instant}; use tokio::sync::broadcast::Receiver; @@ -90,7 +91,9 @@ pub(super) fn handle_tick(app: &mut App) -> bool { // draw site. Excluding it here instead would mean animation ticks request // no paint at all, which drops the animation to whatever unrelated events // happen to trigger (~4fps in practice). - let mut needs_redraw = reset_redraw | crate::tui::periodic_redraw_required(app); + let mut needs_redraw = reset_redraw + | crate::tui::periodic_redraw_required(app) + | crate::tui::terminal_events::theme_redraw_pending(); needs_redraw |= app.poll_oauth_account_telemetry(); needs_redraw |= app.retire_expired_status_notice(); needs_redraw |= app.flush_pending_resize_redraw(); @@ -157,6 +160,7 @@ pub(super) fn handle_tick(app: &mut App) -> bool { needs_redraw |= app.check_stable_version(); needs_redraw |= app.refresh_keybindings_if_config_reloaded(); needs_redraw |= app.maybe_finish_background_client_reload(); + needs_redraw |= app.maybe_hot_reload_onto_new_binary(); if app.pending_migration.is_some() && !app.is_processing && !app.cloud_handoff_blocks_work() { app.execute_migration(); needs_redraw = true; @@ -189,11 +193,10 @@ pub(super) fn handle_terminal_event( let mut needs_redraw = apply_terminal_event(app, terminal, event)?; const MAX_DRAINED_EVENTS_PER_WAKE: usize = 32; for _ in 0..MAX_DRAINED_EVENTS_PER_WAKE { - if !crossterm::event::poll(std::time::Duration::ZERO).unwrap_or(false) { - break; - } - if let Ok(event) = crossterm::event::read() { + if let Ok(Some(event)) = crate::tui::terminal_events::try_read() { needs_redraw |= apply_terminal_event(app, terminal, Some(Ok(event)))?; + } else { + break; } } Ok(needs_redraw) @@ -238,6 +241,7 @@ pub(super) fn handle_bus_event( } Ok(BusEvent::MermaidRenderCompleted) => true, Ok(BusEvent::GitInfoRefreshed) => true, + Ok(BusEvent::ClientBinaryChanged) => app.maybe_hot_reload_onto_new_binary(), Ok(BusEvent::UsageReport(results)) => { app.handle_usage_report(results); true @@ -459,6 +463,8 @@ fn apply_terminal_event( let key = jcode_tui_core::korean_input::normalize_key_event(key); crate::tui::ui::note_key_event_read(); app.note_client_interaction(); + let before_release = (key.kind == KeyEventKind::Release) + .then(|| app.key_release_visible_state()); app.update_copy_badge_key_event(key); app.observe_voice_key_release(&key); if app.handle_voice_key_event(&key) { @@ -466,7 +472,7 @@ fn apply_terminal_event( } else if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { app.handle_key_press_event(key)?; } - Ok(true) + Ok(before_release.is_none_or(|before| before != app.key_release_visible_state())) } Some(Ok(Event::Paste(text))) => { app.note_client_interaction(); diff --git a/crates/jcode-tui/src/tui/app/model_context.rs b/crates/jcode-tui/src/tui/app/model_context.rs index debf864f30f..7ec31fa2145 100644 --- a/crates/jcode-tui/src/tui/app/model_context.rs +++ b/crates/jcode-tui/src/tui/app/model_context.rs @@ -159,9 +159,16 @@ impl App { } pub(super) fn restore_failover_payload(&mut self, payload: super::FallbackResendPayload) { + let restored = payload.raw_input.unwrap_or(payload.content); if self.input.is_empty() { - self.input = payload.raw_input.unwrap_or(payload.content); + self.input = restored; self.cursor_pos = self.input.len(); + } else if !restored.is_empty() && self.input != restored { + // Do not turn a partially typed command or prompt into a different + // request. Keep the held request recoverable in the transcript. + self.push_display_message(DisplayMessage::system(format!( + "Failover request was not resent. Your draft is unchanged. Recover the previous request from here:\n\n{restored}" + ))); } for image in payload.images { if !self.pending_images.contains(&image) { @@ -171,14 +178,23 @@ impl App { } pub(super) fn cancel_pending_provider_failover(&mut self, notice: impl Into) { - if self.pending_automatic_failover_route.take().is_some() - && let Some(payload) = self.pending_fallback_resend.take() - { - self.restore_failover_payload(payload); + let notice = notice.into(); + let staged = self.pending_automatic_failover_route.take().is_some(); + let awaiting_ack = self.pending_automatic_failover_request_id.take().is_some(); + self.pending_automatic_failover_started = None; + if staged || awaiting_ack { + self.remote_model_switch_in_flight = false; + if let Some(payload) = self.pending_fallback_resend.take() { + self.restore_failover_payload(payload); + } + self.set_status_notice(notice.clone()); } let Some(pending) = self.pending_provider_failover.take() else { return; }; + if !self.is_remote { + self.pending_turn = false; + } if let Some(payload) = pending.remote_resend { self.restore_failover_payload(payload); } @@ -326,6 +342,7 @@ impl App { true } Err(error) => { + self.pending_turn = false; self.push_display_message(DisplayMessage::error(format!( "Failed to switch provider to {}: {}", pending.prompt.to_label, error @@ -346,6 +363,7 @@ impl App { self.rate_limit_pending_message .as_ref() .map(|pending| super::FallbackResendPayload { + retry_attempts: pending.retry_attempts, content: pending.content.clone(), images: pending.images.clone(), is_system: pending.is_system, @@ -376,11 +394,27 @@ impl App { prompt.from_label, reason, prompt.to_label, ); - match crate::config::Config::load() - .provider - .cross_provider_failover + let config = crate::config::Config::load().provider; + if remote_resend + .as_ref() + .is_some_and(|payload| payload.retry_attempts >= Self::AUTO_RETRY_MAX_ATTEMPTS) + && config.cross_provider_failover == crate::config::CrossProviderFailoverMode::Countdown { + if let Some(payload) = remote_resend { + self.restore_failover_payload(payload); + } + self.push_display_message(DisplayMessage::system(format!( + "Stopped automatic failover after {} retries. Use /model to choose a working account and resend manually.", + Self::AUTO_RETRY_MAX_ATTEMPTS + ))); + self.set_status_notice("Paused: automatic failover retry limit reached"); + return; + } + match config.cross_provider_failover { crate::config::CrossProviderFailoverMode::Manual => { + if let Some(payload) = remote_resend { + self.restore_failover_payload(payload); + } self.push_display_message(DisplayMessage::system(manual_message)); self.set_status_notice(format!( "{} unavailable; switch manually if desired", @@ -393,9 +427,11 @@ impl App { || crate::provider::MultiProvider::is_failover_route_pool_approved(route) }) && (!self.is_remote || remote_resend.is_some()) => { + let immediate = config.exhausted_usage_immediate_failover + && jcode_provider_core::is_definitive_usage_exhaustion(&prompt.reason); self.pending_provider_failover = Some(super::PendingProviderFailover { prompt: prompt.clone(), - deadline: Instant::now() + Duration::from_secs(3), + deadline: Instant::now() + Duration::from_secs(if immediate { 0 } else { 3 }), remote_resend, }); // One line for the cause, one line for the switch. Nothing else. @@ -410,6 +446,18 @@ impl App { prompt.from_label, reason ))); } + if immediate { + // Reuse the normal switch path, including target/pool revalidation + // and remote route acknowledgement before resending the payload. + self.maybe_progress_provider_failover_countdown(); + if self.pending_automatic_failover_route.is_some() { + self.push_display_message(DisplayMessage::system(format!( + "↪ Switching to {}. Retrying after route confirmation.", + prompt.to_label + ))); + } + return; + } self.push_display_message(DisplayMessage::system( Self::failover_countdown_message(&prompt.to_label), )); @@ -419,6 +467,9 @@ impl App { )); } _ => { + if let Some(payload) = remote_resend { + self.restore_failover_payload(payload); + } self.push_display_message(DisplayMessage::system(manual_message)); self.set_status_notice(format!( "{} unavailable; manual switch suggested", @@ -546,6 +597,7 @@ impl App { self.rate_limit_pending_message .as_ref() .map(|pending| super::FallbackResendPayload { + retry_attempts: pending.retry_attempts, content: pending.content.clone(), images: pending.images.clone(), is_system: pending.is_system, @@ -591,8 +643,13 @@ impl App { error: &str, remote_resend: Option, ) -> bool { - // Never compete with the automatic countdown switcher. - if self.pending_provider_failover.is_some() { + // Never compete with the automatic countdown switcher. A migration + // lease block is not a provider failure: switching model cannot help. + if self.pending_provider_failover.is_some() + || self.pending_automatic_failover_route.is_some() + || self.pending_automatic_failover_request_id.is_some() + || error.contains("Session is not runnable here") + { return false; } let routes = self.fallback_candidate_routes(); @@ -675,7 +732,7 @@ impl App { ¤t_provider, ¤t_api_method, options, - &pool.members, + &pool.ordered_members(), ); if automatic { // Never let an excluded preferred route hide an approved later one. @@ -802,6 +859,7 @@ impl App { self.rate_limit_pending_message .as_ref() .map(|pending| super::FallbackResendPayload { + retry_attempts: pending.retry_attempts, content: pending.content.clone(), images: pending.images.clone(), is_system: pending.is_system, @@ -858,12 +916,14 @@ impl App { if self.is_remote { self.upstream_provider = None; self.status_detail = None; - // Track the method we are switching to so subsequent fallback picks - // know the active credential path (remote sessions have no other - // client-side route bookkeeping). - self.session.route_api_method = Some(offer.selection.api_method.clone()); + // Route metadata changes only after the server confirms the + // selection. A rejected switch must leave the active route intact. self.pending_route_selection = Some(offer.selection); - self.pending_fallback_resend = offer.remote_resend; + self.pending_fallback_resend = offer.remote_resend.map(|mut payload| { + // An explicit user action starts a fresh retry budget. + payload.retry_attempts = 0; + payload + }); self.push_display_message(DisplayMessage::system(format!( "↪ Switching to {} and resending (was {}).", offer.target_label, offer.from_label, @@ -904,6 +964,7 @@ impl App { true } Err(error) => { + self.pending_turn = false; self.push_display_message(DisplayMessage::error(format!( "Failed to switch to {}: {}", offer.target_label, error diff --git a/crates/jcode-tui/src/tui/app/navigation.rs b/crates/jcode-tui/src/tui/app/navigation.rs index eeea96fc0e8..0d1b85de904 100644 --- a/crates/jcode-tui/src/tui/app/navigation.rs +++ b/crates/jcode-tui/src/tui/app/navigation.rs @@ -1543,9 +1543,28 @@ impl App { }) { self.pinned_todos_expanded = !self.pinned_todos_expanded; + crate::tui::ui::viewport::reset_pinned_todo_scroll(); finish_mouse_event!(false, "pinned_todos_toggle"); } + if self.pinned_todos_expanded + && matches!( + mouse.kind, + MouseEventKind::ScrollUp | MouseEventKind::ScrollDown + ) + && crate::tui::ui::viewport::pinned_todo_scroll_area().is_some_and(|area| { + super::super::layout_utils::point_in_rect(mouse.column, mouse.row, area) + }) + { + let delta = if matches!(mouse.kind, MouseEventKind::ScrollUp) { + -(Self::MOUSE_SCROLL_INTENT_LINES as isize) + } else { + Self::MOUSE_SCROLL_INTENT_LINES as isize + }; + crate::tui::ui::viewport::scroll_pinned_todos(delta); + finish_mouse_event!(false, "pinned_todos_scroll"); + } + // A left press in the composer moves the caret first (native text-field // behavior), then falls through so the shared copy-selection machinery // can arm a drag anchor: click repositions the cursor, drag selects the diff --git a/crates/jcode-tui/src/tui/app/observe.rs b/crates/jcode-tui/src/tui/app/observe.rs index 44a1be68695..c01110c0439 100644 --- a/crates/jcode-tui/src/tui/app/observe.rs +++ b/crates/jcode-tui/src/tui/app/observe.rs @@ -142,6 +142,7 @@ impl App { && let Some(session_id) = self.active_client_session_id().map(str::to_string) { super::helpers::invalidate_todos_cache(&session_id); + self.request_remote_todos_refresh_soon(); // Local sessions also receive TodoUpdated, while remote sessions only // observe the completed tool call. Refresh here as well so both paths // adopt the same todo-derived title that /resume displays. diff --git a/crates/jcode-tui/src/tui/app/queued_picker_submission_tests.rs b/crates/jcode-tui/src/tui/app/queued_picker_submission_tests.rs index ab854808d1d..00505785ecb 100644 --- a/crates/jcode-tui/src/tui/app/queued_picker_submission_tests.rs +++ b/crates/jcode-tui/src/tui/app/queued_picker_submission_tests.rs @@ -13,7 +13,11 @@ fn session(id: &str, opening: Option<&str>) -> SessionInfo { user_message_count: 1, assistant_message_count: 0, created_at: now, - last_message_time: now, + last_message_time: if id == "queued-stale" { + now + chrono::Duration::seconds(1) + } else { + now + }, last_active_at: None, working_dir: None, model: None, @@ -58,8 +62,8 @@ fn picker_tick_submits_best_search_result_without_second_key() { app.handle_session_picker_key(KeyCode::Char('/'), KeyModifiers::NONE) .unwrap(); // Opening-request rank intentionally applies only to >=4-word requests. - // This starts with an incidental metadata hit and discovers the exact - // opening request only when the worker runs after Enter is queued. + // Typing preserves the pre-query row. The worker discovers the exact + // opening request only after Enter is queued. for ch in "needle deployment configuration rollback".chars() { app.handle_session_picker_key(KeyCode::Char(ch), KeyModifiers::NONE) .unwrap(); diff --git a/crates/jcode-tui/src/tui/app/remote.rs b/crates/jcode-tui/src/tui/app/remote.rs index 395e33d0181..71469919f10 100644 --- a/crates/jcode-tui/src/tui/app/remote.rs +++ b/crates/jcode-tui/src/tui/app/remote.rs @@ -19,6 +19,7 @@ use std::time::{Duration, Instant}; mod input_dispatch; mod key_handling; +mod latency; mod queue_recovery; mod reconnect; mod server_event_handlers; @@ -52,8 +53,7 @@ use session_persistence::{ }; use workspace::{handle_workspace_command, handle_workspace_navigation_key}; -// Re-export the remote input dispatch helpers for sibling modules/tests that go -// through the `remote` facade instead of private submodule paths. +// Re-export remote input dispatch helpers for sibling modules/tests. #[allow(unused_imports)] pub(super) use input_dispatch::{ apply_remote_transcript_event, apply_transcript_event, begin_remote_send, @@ -65,7 +65,7 @@ pub(super) use input_dispatch::{ pub(super) use key_handling::{ handle_remote_char_input, handle_remote_key, handle_remote_key_event, send_interleave_now, }; -pub(super) use server_events::handle_server_event; +pub(super) use latency::handle_server_event; const CONNECTION_MESSAGE_TITLE: &str = "Connection"; const RELOAD_MARKER_MAX_AGE: Duration = Duration::from_secs(30); @@ -109,7 +109,8 @@ pub(super) async fn handle_tick(app: &mut App, remote: &mut RemoteConnection) -> .as_ref() .is_some_and(|state| state.kind == crate::tui::PickerKind::Model), }); - let mut needs_redraw = crate::tui::periodic_redraw_required(app); + let mut needs_redraw = crate::tui::periodic_redraw_required(app) + | crate::tui::terminal_events::theme_redraw_pending(); needs_redraw |= app.poll_pending_startup_transcript(); if remote.has_loaded_history() && let Err(error) = remote.poll_account_usage().await @@ -239,10 +240,20 @@ pub(super) async fn handle_tick(app: &mut App, remote: &mut RemoteConnection) -> let _ = check_debug_command(app, remote).await; + if app.pending_automatic_failover_request_id.is_some() + && app + .pending_automatic_failover_started + .is_some_and(|started| started.elapsed() >= Duration::from_secs(30)) + { + fail_pending_automatic_failover(app, "Timed out waiting for route confirmation"); + return true; + } let had_failover_countdown = app.pending_provider_failover.is_some(); needs_redraw |= app.maybe_progress_provider_failover_countdown(); - if had_failover_countdown { - if let Some(selection) = app.pending_automatic_failover_route.take() { + if had_failover_countdown || app.pending_automatic_failover_route.is_some() { + if app.pending_automatic_failover_request_id.is_none() + && let Some(selection) = app.pending_automatic_failover_route.take() + { app.pending_model_switch = None; match remote .set_route_selection_for_failover(selection.clone()) @@ -250,6 +261,7 @@ pub(super) async fn handle_tick(app: &mut App, remote: &mut RemoteConnection) -> { Ok(id) => { app.pending_automatic_failover_request_id = Some(id); + app.pending_automatic_failover_started = Some(Instant::now()); app.pending_automatic_failover_route = Some(selection); app.remote_model_switch_in_flight = true; forward_pending_reasoning_effort(app, remote).await; @@ -271,7 +283,9 @@ pub(super) async fn handle_tick(app: &mut App, remote: &mut RemoteConnection) -> return true; } - if app.remote_model_switch_in_flight && app.pending_fallback_resend.is_some() { + if app.pending_fallback_resend.is_some() + && (app.remote_model_switch_in_flight || app.pending_route_selection.is_some()) + { return needs_redraw; } @@ -393,13 +407,24 @@ pub(super) async fn handle_tick(app: &mut App, remote: &mut RemoteConnection) -> return needs_redraw; } + // After a reload `is_processing` stays false until the History bootstrap + // reports whether the server is still mid-turn. Dispatching queued work + // before then would inject follow-ups into the still-running turn. + if !remote.has_loaded_history() { + return needs_redraw; + } + if !app.is_processing && !app.queued_commands.is_empty() { // Commands queued with Ctrl+Enter run before queued prompts. Hand them // to the follow-up dispatcher, which runs them one at a time. app.pending_queued_dispatch = true; needs_redraw = true; - } else if !app.is_processing && !app.queued_messages.is_empty() { - let queued_messages = std::mem::take(&mut app.queued_messages); + } else if !app.is_processing + && !app.queued_messages.is_empty() + && !app.reload_recovery_blocks_queue() + { + let mut queued_messages = std::mem::take(&mut app.queued_messages); + super::commands::drop_stale_incomplete_todo_pokes(app, &mut queued_messages); let hidden_reminders = std::mem::take(&mut app.hidden_queued_system_messages); let (messages, reminder, display_system_messages) = super::helpers::partition_queued_messages(queued_messages, hidden_reminders); @@ -413,7 +438,15 @@ pub(super) async fn handle_tick(app: &mut App, remote: &mut RemoteConnection) -> app.push_display_message(DisplayMessage::system(msg)); } for msg in &messages { - app.push_display_message(DisplayMessage::user(msg.clone())); + // Synthetic continuations are for the model, not user prompts. + let display = match crate::todo::auto_poke_display_summary(msg) { + Some(summary) => DisplayMessage::system(summary.to_string()), + None => DisplayMessage::user(msg.clone()), + }; + app.push_display_message(display); + } + if messages.is_empty() && reminder.is_none() { + return true; } if begin_remote_send( app, @@ -450,6 +483,7 @@ pub(super) async fn handle_tick(app: &mut App, remote: &mut RemoteConnection) -> || (remote.has_loaded_history() && app.has_pending_cloud_recovery())) { let reminders = std::mem::take(&mut app.hidden_queued_system_messages); + app.reload_recovery_dispatch_pending = false; let combined = reminders.join("\n\n"); crate::logging::info(&format!( "Sending hidden continuation reminder ({} chars)", @@ -482,6 +516,7 @@ pub(super) async fn handle_tick(app: &mut App, remote: &mut RemoteConnection) -> needs_redraw |= recover_stuck_remote_history(app, remote).await; needs_redraw |= detect_starved_queued_followup(app); needs_redraw |= app.maybe_finish_background_client_reload(); + needs_redraw |= app.maybe_hot_reload_onto_new_binary(); needs_redraw } @@ -524,11 +559,10 @@ pub(super) async fn handle_terminal_event( // lag. Mirrors the identical drain in `local::handle_terminal_event`. const MAX_DRAINED_EVENTS_PER_WAKE: usize = 32; for _ in 0..MAX_DRAINED_EVENTS_PER_WAKE { - if !crossterm::event::poll(std::time::Duration::ZERO).unwrap_or(false) { - break; - } - if let Ok(event) = crossterm::event::read() { + if let Ok(Some(event)) = crate::tui::terminal_events::try_read() { needs_redraw |= apply_terminal_event(app, terminal, remote, Some(Ok(event))).await?; + } else { + break; } } Ok(needs_redraw) @@ -571,6 +605,8 @@ async fn apply_terminal_event( }); input_attribution.scroll_delta = key_scroll_delta(&key); app.note_client_interaction(); + let before_release = + (key.kind == KeyEventKind::Release).then(|| app.key_release_visible_state()); app.update_copy_badge_key_event(key); app.observe_voice_key_release(&key); if !app.handle_voice_key_event(&key) @@ -580,14 +616,24 @@ async fn apply_terminal_event( if let Some(selection) = app.pending_route_selection.take() { app.cancel_pending_provider_failover("Provider auto-switch canceled"); app.pending_model_switch = None; - match remote.set_route_selection(selection).await { - Ok(_) => { + match remote.set_route_selection(selection.clone()).await { + Ok(id) => { + // Manual fallback offers use unrestricted SetRoute, + // but need the same exact acknowledgement fence as + // automatic failover before releasing their resend. + if app.pending_fallback_resend.is_some() { + app.pending_automatic_failover_route = Some(selection); + app.pending_automatic_failover_request_id = Some(id); + app.pending_automatic_failover_started = Some(Instant::now()); + } app.remote_model_switch_in_flight = true; forward_pending_reasoning_effort(app, remote).await; } Err(error) => { app.pending_reasoning_effort = None; - app.pending_fallback_resend = None; + if let Some(payload) = app.pending_fallback_resend.take() { + app.restore_failover_payload(payload); + } app.push_display_message(DisplayMessage::error(format!( "Failed to request model switch: {}", error @@ -629,7 +675,8 @@ async fn apply_terminal_event( } } } - needs_redraw = true; + needs_redraw = + before_release.is_none_or(|before| before != app.key_release_visible_state()); needs_redraw |= dispatch_compacted_history_load(app, remote).await; } Some(Ok(Event::Paste(text))) => { @@ -687,8 +734,18 @@ async fn dispatch_compacted_history_load(app: &mut App, remote: &mut RemoteConne let Some(visible_messages) = app.take_pending_compacted_history_load() else { return false; }; - match remote.get_compacted_history(visible_messages).await { - Ok(_) => true, + let recent_history = app.recent_history_window_active(); + match remote + .get_compacted_history(visible_messages, recent_history) + .await + { + Ok(id) => { + if recent_history { + app.recent_history_paging.request = + Some((id, app.display_messages_version, visible_messages)); + } + true + } Err(error) => { app.restore_pending_compacted_history_load(visible_messages); app.set_status_notice(format!("Failed to request older history: {}", error)); @@ -729,6 +786,7 @@ pub(super) async fn handle_bus_event( } Ok(BusEvent::MermaidRenderCompleted) => true, Ok(BusEvent::GitInfoRefreshed) => true, + Ok(BusEvent::ClientBinaryChanged) => app.maybe_hot_reload_onto_new_binary(), Ok(BusEvent::UsageReportProgress(progress)) => { app.handle_usage_report_progress(progress); true @@ -1080,11 +1138,31 @@ pub(super) fn debug_command_needs_current_frame(command: &str) -> bool { ) } +fn fail_pending_automatic_failover(app: &mut App, reason: &str) { + app.pending_automatic_failover_request_id = None; + app.pending_automatic_failover_started = None; + app.pending_automatic_failover_route = None; + app.remote_model_switch_in_flight = false; + if let Some(payload) = app.pending_fallback_resend.take() { + app.restore_failover_payload(payload); + } + app.push_display_message(DisplayMessage::error(format!( + "Fallback switch stopped: {}", + App::compact_provider_failure(reason) + ))); + app.set_status_notice("Fallback switch stopped; resend manually"); +} + pub(super) fn handle_disconnect( app: &mut App, state: &mut RemoteRunState, reason: Option, ) { + // The daemon may have applied the switch, but the acknowledgement was + // lost. Never wait on an old connection's request ID or blindly resend. + if app.pending_automatic_failover_request_id.is_some() { + fail_pending_automatic_failover(app, "Connection lost before route confirmation"); + } app.revoke_cloud_environment(); let detail = if state.server_reload_in_progress { "server reload in progress".to_string() @@ -1108,6 +1186,11 @@ pub(super) fn handle_disconnect( if app.startup_model_request_id.take().is_some() { app.remote_model_switch_in_flight = false; } + // The cancel answer cannot arrive on a dead connection. Ask again later. + app.soft_interrupt_recovery = None; + app.recent_history_paging = Default::default(); + app.pending_history_anchor = None; + app.compacted_history_lazy.pending_request_visible = None; state.last_disconnect_reason = Some(detail.clone()); let scheduled_retry = @@ -1389,6 +1472,9 @@ pub(super) async fn process_remote_followups(app: &mut App, remote: &mut RemoteC return; } if app.pending_provider_failover.is_some() + || app.pending_automatic_failover_route.is_some() + || app.pending_automatic_failover_request_id.is_some() + || (app.pending_route_selection.is_some() && app.pending_fallback_resend.is_some()) || (app.remote_model_switch_in_flight && app.pending_fallback_resend.is_some()) { return; @@ -1460,6 +1546,9 @@ pub(super) async fn process_remote_followups(app: &mut App, remote: &mut RemoteC } let _ = recover_stranded_soft_interrupts(app, remote).await; + if app.soft_interrupt_recovery_in_flight() { + return; + } if app.pending_queued_dispatch { note_startup_submit_deferred(app, "pending_queued_dispatch in progress"); @@ -1483,6 +1572,7 @@ pub(super) async fn process_remote_followups(app: &mut App, remote: &mut RemoteC } app.last_submitted_input = payload.raw_input.clone(); crate::logging::info("Resending failed turn after accepted fallback route switch"); + let recovery_payload = payload.clone(); if let Err(error) = begin_remote_send( app, remote, @@ -1491,10 +1581,11 @@ pub(super) async fn process_remote_followups(app: &mut App, remote: &mut RemoteC payload.is_system, payload.system_reminder, payload.auto_retry, - 0, + payload.retry_attempts.saturating_add(1), ) .await { + app.restore_failover_payload(recovery_payload); app.push_display_message(DisplayMessage::error(format!( "Failed to resend after fallback switch: {}", error @@ -1706,11 +1797,15 @@ pub(super) async fn process_remote_followups(app: &mut App, remote: &mut RemoteC ))); } } - } else if !app.queued_messages.is_empty() { - let queued_messages = std::mem::take(&mut app.queued_messages); + } else if !app.queued_messages.is_empty() && !app.reload_recovery_blocks_queue() { + let mut queued_messages = std::mem::take(&mut app.queued_messages); + super::commands::drop_stale_incomplete_todo_pokes(app, &mut queued_messages); let hidden_reminders = std::mem::take(&mut app.hidden_queued_system_messages); let (messages, reminder, display_system_messages) = super::helpers::partition_queued_messages(queued_messages, hidden_reminders); + if messages.is_empty() && reminder.is_none() { + return; + } let combined = messages.join("\n\n"); let preserve_visible_turn = super::commands::queued_messages_are_only_pokes(&messages); let auto_retry = reminder.is_some() && messages.is_empty(); @@ -1756,6 +1851,7 @@ pub(super) async fn process_remote_followups(app: &mut App, remote: &mut RemoteC } } else if !app.hidden_queued_system_messages.is_empty() || app.has_pending_cloud_recovery() { let reminders = std::mem::take(&mut app.hidden_queued_system_messages); + app.reload_recovery_dispatch_pending = false; let combined = reminders.join("\n\n"); if begin_remote_send( app, diff --git a/crates/jcode-tui/src/tui/app/remote/key_handling.rs b/crates/jcode-tui/src/tui/app/remote/key_handling.rs index 7efb298e49e..a8a73bc57f5 100644 --- a/crates/jcode-tui/src/tui/app/remote/key_handling.rs +++ b/crates/jcode-tui/src/tui/app/remote/key_handling.rs @@ -670,6 +670,7 @@ pub(in crate::tui::app) async fn submit_prepared_remote_enter_input( return Ok(()); } app.upstream_provider = None; + app.cancel_pending_provider_failover("Provider auto-switch canceled"); remote.set_model(model_name).await?; app.remote_model_switch_in_flight = true; return Ok(()); diff --git a/crates/jcode-tui/src/tui/app/remote/latency.rs b/crates/jcode-tui/src/tui/app/remote/latency.rs new file mode 100644 index 00000000000..6fdb1e0f759 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/remote/latency.rs @@ -0,0 +1,16 @@ +use super::{App, ServerEvent}; +use crate::tui::backend::RemoteEventState; + +pub(in crate::tui::app) fn handle_server_event( + app: &mut App, + event: ServerEvent, + remote: &mut impl RemoteEventState, +) -> bool { + let kind = crate::tui::cloud_latency::event_key(&event); + let started = std::time::Instant::now(); + let redraw = super::server_events::handle_server_event(app, event, remote); + if let Some((kind, _)) = kind { + crate::tui::cloud_latency::applied(kind, started, redraw); + } + redraw +} diff --git a/crates/jcode-tui/src/tui/app/remote/queue_recovery.rs b/crates/jcode-tui/src/tui/app/remote/queue_recovery.rs index e03b01344f4..633934d56da 100644 --- a/crates/jcode-tui/src/tui/app/remote/queue_recovery.rs +++ b/crates/jcode-tui/src/tui/app/remote/queue_recovery.rs @@ -1,9 +1,22 @@ use super::*; +/// How long queue dispatch waits for the server's by-id cancel answer before +/// falling back to requeueing every pending soft interrupt. +const SOFT_INTERRUPT_RECOVERY_TIMEOUT: Duration = Duration::from_secs(5); + impl App { - pub(super) fn track_pending_soft_interrupt(&mut self, request_id: u64, content: String) { + pub(in crate::tui::app) fn track_pending_soft_interrupt( + &mut self, + request_id: u64, + content: String, + ) { let content_bytes = content.len(); let content_chars = content.chars().count(); + if self.pending_soft_interrupts.is_empty() { + self.sent_soft_interrupts.clear(); + } + self.sent_soft_interrupts + .push((request_id, content.clone())); self.pending_soft_interrupt_requests .push((request_id, content.clone())); self.pending_soft_interrupts.push(content); @@ -52,6 +65,105 @@ impl App { )); self.pending_soft_interrupts.clear(); self.pending_soft_interrupt_requests.clear(); + self.sent_soft_interrupts.clear(); + self.soft_interrupt_recovery = None; + } + + fn forget_sent_soft_interrupt(&mut self, content: &str) { + if let Some(index) = self + .sent_soft_interrupts + .iter() + .position(|(_, sent)| sent == content) + { + self.sent_soft_interrupts.remove(index); + } + } + + /// Server caller ids for every pending soft interrupt, or `None` when any + /// of them cannot be named (restored from a reload snapshot, for example). + fn pending_soft_interrupt_client_ids(&self) -> Option> { + let mut used = vec![false; self.sent_soft_interrupts.len()]; + let mut ids = Vec::with_capacity(self.pending_soft_interrupts.len()); + for pending in &self.pending_soft_interrupts { + let index = self + .sent_soft_interrupts + .iter() + .enumerate() + .position(|(index, (_, sent))| !used[index] && sent == pending)?; + used[index] = true; + ids.push(crate::tui::backend::soft_interrupt_client_id( + self.sent_soft_interrupts[index].0, + )); + } + Some(ids) + } + + /// True while the server has been asked which pending soft interrupts are + /// still queued. Queue dispatch waits for the answer: requeueing first + /// would resend a prompt the server already started as its own turn. + pub(super) fn soft_interrupt_recovery_in_flight(&self) -> bool { + self.soft_interrupt_recovery + .is_some_and(|(_, started)| started.elapsed() < SOFT_INTERRUPT_RECOVERY_TIMEOUT) + } + + /// Apply the server's verdict on a by-id recovery cancel. Only interrupts + /// the server still had queued (or never acknowledged) are requeued. One it + /// already accepted is running or injected, so requeueing would duplicate it. + pub(super) fn finish_soft_interrupt_recovery( + &mut self, + id: u64, + cancelled: &[String], + not_queued: &[String], + ) -> bool { + if self.soft_interrupt_recovery.map(|(request, _)| request) != Some(id) { + return false; + } + self.soft_interrupt_recovery = None; + let mut recovered = Vec::new(); + let mut accepted = 0usize; + for (client_id, was_queued) in cancelled + .iter() + .map(|id| (id, true)) + .chain(not_queued.iter().map(|id| (id, false))) + { + let Some(index) = self.sent_soft_interrupts.iter().position(|(request, _)| { + &crate::tui::backend::soft_interrupt_client_id(*request) == client_id + }) else { + continue; + }; + let (request_id, content) = self.sent_soft_interrupts.remove(index); + let unacked = self + .pending_soft_interrupt_requests + .iter() + .position(|(request, _)| *request == request_id) + .map(|index| self.pending_soft_interrupt_requests.remove(index)) + .is_some(); + let Some(pending) = self + .pending_soft_interrupts + .iter() + .position(|pending| pending == &content) + else { + continue; + }; + self.pending_soft_interrupts.remove(pending); + if was_queued || unacked { + recovered.push(content); + } else { + accepted += 1; + } + } + crate::logging::info(&format!( + "REMOTE_SOFT_INTERRUPT_RECOVERY_RESULT id={} requeued={} already_accepted={}", + id, + recovered.len(), + accepted + )); + if !recovered.is_empty() { + recovered.append(&mut self.queued_messages); + self.queued_messages = recovered; + self.set_status_notice("Recovered queued interleave after turn finished"); + } + true } pub(super) fn mark_soft_interrupt_injected(&mut self, content: &str) { @@ -72,6 +184,7 @@ impl App { .position(|pending| pending == content) { self.pending_soft_interrupts.remove(index); + self.forget_sent_soft_interrupt(content); } if let Some(index) = self @@ -95,6 +208,7 @@ impl App { let count = index + 1; let removed: Vec = self.pending_soft_interrupts.drain(..count).collect(); for removed_content in removed { + self.forget_sent_soft_interrupt(&removed_content); if let Some(request_index) = self .pending_soft_interrupt_requests .iter() @@ -189,7 +303,29 @@ pub(super) async fn recover_stranded_soft_interrupts( app: &mut App, remote: &mut RemoteConnection, ) -> bool { - if app.is_processing || app.pending_soft_interrupts.is_empty() { + if app.pending_soft_interrupts.is_empty() { + app.soft_interrupt_recovery = None; + return false; + } + if app.is_processing || app.soft_interrupt_recovery_in_flight() { + return false; + } + // The turn ended on this client, but the server may already have accepted + // a pending interrupt: one that reaches an idle session starts its own + // turn. Ask which are still queued and requeue only those. Blind requeueing + // sent the same prompt twice and drew "Already processing a message". + let unanswered = app.soft_interrupt_recovery.take().is_some(); + if !unanswered && let Some(client_ids) = app.pending_soft_interrupt_client_ids() { + match remote.cancel_soft_interrupts_by_id(client_ids).await { + Ok(id) => app.soft_interrupt_recovery = Some((id, Instant::now())), + Err(err) => { + app.push_display_message(DisplayMessage::error(format!( + "Failed to recover queued interleave message: {}", + err + ))); + app.set_status_notice("Queued interleave recovery failed"); + } + } return false; } @@ -213,6 +349,7 @@ pub(super) async fn recover_stranded_soft_interrupts( recovered_interrupts.len() )); app.pending_soft_interrupt_requests.clear(); + app.sent_soft_interrupts.clear(); let mut recovered_queue = recovered_interrupts; recovered_queue.append(&mut app.queued_messages); diff --git a/crates/jcode-tui/src/tui/app/remote/reconnect.rs b/crates/jcode-tui/src/tui/app/remote/reconnect.rs index 2dcf1e77e62..7c4673bf5f8 100644 --- a/crates/jcode-tui/src/tui/app/remote/reconnect.rs +++ b/crates/jcode-tui/src/tui/app/remote/reconnect.rs @@ -5,8 +5,8 @@ use super::{ use crate::tool::selfdev::ReloadContext; use crate::tui::app::PendingReloadReconnectStatus; use crate::tui::backend::{RemoteConnection, RemoteDisconnectReason}; +use crate::tui::terminal_events::EventStream; use anyhow::Result; -use crossterm::event::EventStream; use futures::StreamExt; use ratatui::DefaultTerminal; use std::time::{Duration, Instant}; @@ -989,6 +989,7 @@ pub(in crate::tui::app) fn queue_reload_continuation_once( } app.push_display_message(DisplayMessage::system(notice)); app.hidden_queued_system_messages.push(continuation); + app.reload_recovery_dispatch_pending = true; true } diff --git a/crates/jcode-tui/src/tui/app/remote/server_events.rs b/crates/jcode-tui/src/tui/app/remote/server_events.rs index c7a7d6f3236..2140d01f2dd 100644 --- a/crates/jcode-tui/src/tui/app/remote/server_events.rs +++ b/crates/jcode-tui/src/tui/app/remote/server_events.rs @@ -167,6 +167,14 @@ pub(in crate::tui::app) fn handle_server_event( event: ServerEvent, remote: &mut impl RemoteEventState, ) -> bool { + // Route mutation errors are control responses, not failed conversation + // turns. Handle them before the scoped-turn terminal fence. + if let ServerEvent::Error { id, message, .. } = &event + && app.pending_automatic_failover_request_id == Some(*id) + { + super::fail_pending_automatic_failover(app, message); + return true; + } let (event, identity) = match event { ServerEvent::TurnState { session_id, @@ -1030,6 +1038,11 @@ fn handle_server_event_inner( let _ = app.acknowledge_pending_soft_interrupt(id); false } + ServerEvent::SoftInterruptsCancelled { + id, + cancelled, + not_queued, + } => app.finish_soft_interrupt_recovery(id, &cancelled, ¬_queued), ServerEvent::Interrupted => { crate::logging::info(&format!( "REMOTE_INTERRUPT_EVENT_RECEIVED kind=interrupted session={:?} current_message_id={:?} is_processing={} status={:?} streaming_text_bytes={} pending_soft_interrupts={} queued_messages={}", @@ -1277,6 +1290,19 @@ fn handle_server_event_inner( app.set_status_notice("Agent model update failed"); return false; } + // Exhaustion can be reported again while the route switch is staged + // or awaiting acknowledgement. It belongs to the failed route, not + // a new turn: do not print it again or replace the held payload. + if (app.pending_provider_failover.is_some() + || app.pending_automatic_failover_route.is_some() + || app.pending_automatic_failover_request_id.is_some() + || (app.pending_route_selection.is_some() && app.pending_fallback_resend.is_some())) + && (crate::provider::parse_failover_prompt_message(&message).is_some() + || jcode_provider_core::classify_failover_error_message(&message) + .should_mark_provider_unavailable()) + { + return false; + } app.refresh_openai_usage_after_quota_error(&message); // The server rejects a Message request with this error while its // previous turn is still running. This typically happens when a @@ -1379,6 +1405,7 @@ fn handle_server_event_inner( images: pending.images.clone(), is_system: pending.is_system, auto_retry: pending.auto_retry, + retry_attempts: pending.retry_attempts, system_reminder: pending.system_reminder.clone(), raw_input: app.last_submitted_input.clone(), } @@ -1425,7 +1452,10 @@ fn handle_server_event_inner( app.clear_pending_remote_retry(); crate::tui::app::commands::disable_auto_poke(app); app.overnight_auto_poke = None; - if app.pending_provider_failover.is_none() { + if app.pending_provider_failover.is_none() + && app.pending_automatic_failover_route.is_none() + && app.pending_automatic_failover_request_id.is_none() + { app.restore_failed_input_to_box(); } return false; @@ -1877,6 +1907,22 @@ fn handle_server_event_inner( app.note_client_focus(true); let session_changed = prev_session_id.as_deref() != Some(session_id.as_str()); if session_changed { + app.recent_history_paging = Default::default(); + app.pending_history_anchor = None; + app.compacted_history_lazy.pending_request_visible = None; + // Drop the previous session's snapshot and polling deadlines. + // Otherwise attach can inherit a 1s local / 2s SSH todo delay. + // A resume sends its authoritative todo snapshot before the + // large History frame. Keep that snapshot, never the old one. + if app + .remote_todos_payload + .as_ref() + .is_some_and(|(id, _)| id != &session_id) + { + app.remote_todos_payload = None; + } + app.remote_todos_requested_at = None; + app.pinned_todos_checked_at = None; app.remote_available_service_tiers = None; app.usage_reset.openai_quota_rejected = false; } @@ -1954,6 +2000,9 @@ fn handle_server_event_inner( app.swarm_plan_swarm_id = None; remote.reset_call_output_tokens_seen(); } + if session_changed { + app.refresh_pinned_todos_now(); + } // Facts seeded from the last launch are only a first-frame guess. // When History reports a different model, drop them so the snapshot // below is applied exactly as on a cold start (including the @@ -2413,6 +2462,7 @@ fn handle_server_event_inner( true } ServerEvent::CompactedHistory { + id, session_id, messages, images, @@ -2429,7 +2479,10 @@ fn handle_server_event_inner( )); return false; } - let restored_messages = messages + if !app.accept_recent_history_response(id) { + return false; + } + let mut restored_messages: Vec<_> = messages .into_iter() .map(|msg| DisplayMessage { role: msg.role, @@ -2440,6 +2493,12 @@ fn handle_server_event_inner( tool_data: msg.tool_data, }) .collect(); + if !app.recent_history_page_contains_visible_tail( + &mut restored_messages, + compacted_visible, + ) { + return false; + } app.apply_compacted_history_window( restored_messages, images, @@ -2611,6 +2670,15 @@ fn handle_server_event_inner( context_window, .. } => { + // A resume/catalog acknowledgement can arrive before the staged + // route request has even been sent. It cannot authorize a resend. + if app.pending_automatic_failover_request_id.is_none() + && (app.pending_automatic_failover_route.is_some() + || (app.pending_route_selection.is_some() + && app.pending_fallback_resend.is_some())) + { + return false; + } if app .pending_automatic_failover_request_id .is_some_and(|expected| expected != id) @@ -2629,8 +2697,38 @@ fn handle_server_event_inner( } } app.pending_automatic_failover_request_id = None; + app.pending_automatic_failover_started = None; app.remote_model_switch_in_flight = false; let automatic_route = app.pending_automatic_failover_route.take(); + let error = error.or_else(|| { + let selection = automatic_route.as_ref()?; + use jcode_provider_core::{ResolvedCredential, RuntimeKey}; + let expected_credential = match selection.runtime_key { + RuntimeKey::ClaudeOAuth | RuntimeKey::OpenAIOAuth => { + Some(ResolvedCredential::Oauth) + } + RuntimeKey::AnthropicApiKey | RuntimeKey::OpenAIApiKey => { + Some(ResolvedCredential::ApiKey) + } + _ => None, + }; + let expected_provider = + jcode_provider_core::parse_provider_hint(&selection.provider_label); + let actual_provider = provider_name + .as_deref() + .and_then(jcode_provider_core::parse_provider_hint); + let matches = selection.model == model + && selection.account_label().is_none_or(|label| { + provider_oauth_account_label.as_deref() == Some(label) + }) + && expected_credential.is_none_or(|credential| { + resolved_credential == Some(credential) + }) + && expected_provider.is_none_or(|provider| actual_provider == Some(provider)); + (!matches).then(|| { + "The server did not confirm the requested failover model/account. Automatic resend stopped. Use /model to choose a route and resend.".to_string() + }) + }); if let Some(err) = error { let account_switch_failed = app.pending_account_switch_target.take().is_some(); if let Some(prepared) = app.pending_prompt_after_model_switch.take() { @@ -3229,10 +3327,15 @@ fn handle_server_event_inner( )); return false; } - app.push_display_message(DisplayMessage::swarm( - presentation.title.clone(), - presentation.message.clone(), - )); + let now = std::time::Instant::now(); + let since_last = app + .swarm_last_message_at + .insert(sender.clone(), now) + .map(|prev| now.duration_since(prev).as_secs_f32()); + let mut swarm_message = + DisplayMessage::swarm(presentation.title.clone(), presentation.message.clone()); + swarm_message.duration_secs = since_last; + app.push_display_message(swarm_message); persist_replay_display_message( app, "swarm", @@ -3246,6 +3349,10 @@ fn handle_server_event_inner( apply_transcript_event(app, text, mode); false } + ServerEvent::VoiceActivity { activity } => { + app.apply_external_voice_activity(activity); + false + } ServerEvent::InputShellResult { result } => { app.push_display_message(DisplayMessage::system( crate::message::format_input_shell_result_markdown(&result), diff --git a/crates/jcode-tui/src/tui/app/replay.rs b/crates/jcode-tui/src/tui/app/replay.rs index 638c9650fb0..c359583c9d6 100644 --- a/crates/jcode-tui/src/tui/app/replay.rs +++ b/crates/jcode-tui/src/tui/app/replay.rs @@ -1,8 +1,9 @@ use super::{App, DisplayMessage, ProcessingStatus, RunResult}; use crate::replay::{PaneReplayInput, ReplayEvent, TimelineEvent}; use crate::tui::backend::{RemoteEventState, ReplayRemoteState}; +use crate::tui::terminal_events::EventStream; use anyhow::Result; -use crossterm::event::{Event, EventStream, KeyCode, KeyEventKind, KeyModifiers}; +use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers}; use futures::StreamExt; use ratatui::{ DefaultTerminal, Frame, Terminal, backend::TestBackend, buffer::Buffer, layout::Rect, diff --git a/crates/jcode-tui/src/tui/app/run_shell.rs b/crates/jcode-tui/src/tui/app/run_shell.rs index 7876277f83b..dac60eba83d 100644 --- a/crates/jcode-tui/src/tui/app/run_shell.rs +++ b/crates/jcode-tui/src/tui/app/run_shell.rs @@ -36,6 +36,8 @@ const STATUS_SPINNER_FPS: f32 = 12.5; const FIRST_FRAME_GIT_BUDGET: std::time::Duration = std::time::Duration::from_millis(40); /// Tick cadence while the reload fast-start transcript is still rendering. const STARTUP_TRANSCRIPT_POLL: std::time::Duration = std::time::Duration::from_millis(10); +/// Minimum spacing between non-input full frames in the remote loop (~60fps). +const EVENT_REDRAW_MIN_INTERVAL: Duration = Duration::from_millis(16); pub(super) const STATUS_SPINNER_ONLY_INTERVAL: Duration = Duration::from_millis(80); pub(super) fn redraw_timer(period: Duration) -> tokio::time::Interval { @@ -305,6 +307,7 @@ fn idle_animation_fast_path_blocked_reason( #[derive(Default)] pub(super) struct StatusSpinnerRenderer { last_frame: Option, + last_theme: jcode_tui_style::ThemeMode, last_full_frame_at: Option, /// Animated rectangle whose surrounding cells are currently seeded into /// ratatui's working buffer. @@ -344,6 +347,14 @@ impl StatusSpinnerRenderer { self.last_full_frame_input.clear(); } + /// Remember only the cells a partial repaint changed, before ratatui resets + /// its working buffer. The snapshot must match the flushed frame exactly. + fn remember_partial_frame(&mut self, rendered: &Buffer, area: Rect) { + if let Some(last) = self.last_frame.as_mut() { + copy_cells_in(rendered, last, area); + } + } + /// Present an animation frame and forget its working-buffer seed. /// /// `Terminal::swap_buffers` resets the buffer that the next tick will draw @@ -374,6 +385,9 @@ impl StatusSpinnerRenderer { } pub(super) fn idle_animation_only_available(&self, app: &App) -> bool { + if self.last_theme != jcode_tui_style::theme_mode() { + return false; + } let blocked = idle_animation_fast_path_blocked_reason(&IdleAnimationFastPathInputs { has_previous_frame: self.last_frame.is_some(), animation_active: crate::tui::idle_donut_active(app), @@ -418,6 +432,9 @@ impl StatusSpinnerRenderer { app: &App, terminal: &mut DefaultTerminal, ) -> Result { + if self.last_theme != jcode_tui_style::theme_mode() { + return Ok(false); + } let Some(previous_frame) = self.last_frame.as_ref() else { return Ok(false); }; @@ -445,7 +462,8 @@ impl StatusSpinnerRenderer { if self.seeded_animation_area == Some(area) { copy_cells_in(previous_frame, current_buffer, area); } else { - current_buffer.clone_from(previous_frame); + current_buffer.area = previous_frame.area; + current_buffer.content.clone_from(&previous_frame.content); self.seeded_animation_area = Some(area); } crate::tui::ui::render_idle_animation_into( @@ -469,20 +487,11 @@ impl StatusSpinnerRenderer { RestorePosition, EndSynchronizedUpdate )?; + // Retain exactly the flushed cells. Re-rendering samples a later clock + // and doubles scene work while potentially diverging from the screen. + self.remember_partial_frame(terminal.current_buffer_mut(), area); self.swap_after_animation_frame(terminal); terminal.backend_mut().flush()?; - // Keep the remembered frame current without cloning the whole screen: it - // already matches everywhere except the rows just re-rendered. Re-render - // the animation into those rows directly (deterministic for a given - // elapsed time, and far cheaper than copying 7680 cells) so - // `self.last_frame` stays byte-identical to what the terminal now shows. - if let Some(last) = self.last_frame.as_mut() { - crate::tui::ui::render_idle_animation_into( - last, - area, - crate::tui::TuiState::animation_elapsed(app), - ); - } // Without this the animation-only path was invisible in `draw-stats`: // `partial_repaints` stayed at 0 while this path served ~60 repaints a // second, which reads as "the cheap path never runs" and sends anyone @@ -498,6 +507,9 @@ impl StatusSpinnerRenderer { ) -> Result<()> { // Painting a frame is progress, including during long streaming turns. crate::logging::watchdog::beat("tui.draw"); + if crate::tui::terminal_events::take_theme_redraw() { + app.force_full_repaint = true; + } app.refresh_terminal_title_metrics(); app.sync_herdr_agent_state(); app.poll_oauth_account_telemetry(); @@ -523,7 +535,10 @@ impl StatusSpinnerRenderer { app.force_full_redraw = false; app.force_full_repaint = false; - let previous_frame = self.last_frame.as_ref(); + // Take the previous frame so its allocation can be reused below + // (`clone_from`) instead of allocating a whole new buffer per frame. + let stored_frame = self.last_frame.take(); + let previous_frame = stored_frame.as_ref(); let draw_start = Instant::now(); let mut render_elapsed = Duration::ZERO; super::oauth_telemetry::begin_frame(); @@ -533,18 +548,23 @@ impl StatusSpinnerRenderer { render_elapsed = render_start.elapsed(); })?; let total_elapsed = draw_start.elapsed(); + // ratatui-jcode reports how many cells its own diff emitted, so there is + // no second full-buffer comparison here. let changed_cells = previous_frame + .filter(|_| crate::tui::ui::changed_cells_wanted()) .filter(|previous| previous.area == completed.buffer.area) - .map(|previous| { - previous - .content - .iter() - .zip(completed.buffer.content.iter()) - .filter(|(left, right)| left != right) - .count() - }); + .map(|_| completed.flushed_updates); let total_cells = Some(completed.buffer.content.len()); - let completed_buffer = completed.buffer.clone(); + let completed_buffer = match stored_frame { + Some(mut buffer) => { + // Derived `Buffer::clone_from` reallocates; `Vec::clone_from` + // reuses the existing cell storage. + buffer.area = completed.buffer.area; + buffer.content.clone_from(&completed.buffer.content); + buffer + } + None => completed.buffer.clone(), + }; // `completed` borrows the terminal; it is unused past this point, so the // borrow ends here (NLL) before we touch the backend again below. let frame_released = @@ -565,6 +585,7 @@ impl StatusSpinnerRenderer { if crate::tui::ui::last_idle_animation_area().is_some() { crate::tui::ui::note_idle_animation_full_repaint(); } + self.last_theme = jcode_tui_style::theme_mode(); self.last_frame = Some(completed_buffer); app.finish_idle_hibernation_frame(); self.last_full_frame_at = Some(Instant::now()); @@ -594,6 +615,9 @@ impl StatusSpinnerRenderer { if status_symbol.is_none() { return Ok(false); } + if self.last_theme != jcode_tui_style::theme_mode() { + return Ok(false); + } let Some(previous_frame) = self.last_frame.as_ref() else { return Ok(false); }; @@ -607,39 +631,40 @@ impl StatusSpinnerRenderer { return Ok(false); } - let next_frame = { - let current_buffer = terminal.current_buffer_mut(); - current_buffer.clone_from(previous_frame); - if let Some((symbol, area)) = status_symbol.zip(status_area) - && status_patchable - { - render_status_spinner_into_buffer_mut(current_buffer, area, symbol); - } - current_buffer.clone() - }; + if terminal.current_buffer_mut().area != previous_frame.area { + return Ok(false); + } + let (symbol, area) = status_symbol.zip(status_area).expect("patchable spinner"); + let mut cell = previous_frame[(area.x, area.y)].clone(); + cell.set_symbol(symbol) + .set_style(status_spinner_patch_style()); // Keep ratatui's virtual buffers authoritative while preserving the user's cursor position. // The only terminal mutation outside ratatui here is cursor save/restore; cell contents still - // go through Terminal::flush so the next full-frame diff remains synchronized. Wrap the + // go through Terminal's retained-cell API so the next full-frame diff stays synchronized. Wrap the // single-cell update in a synchronized update so it applies atomically (see issue #282). crossterm::queue!( terminal.backend_mut(), BeginSynchronizedUpdate, SavePosition )?; - terminal.flush()?; - crossterm::queue!( + let patch_result = terminal.draw_retained_cell((area.x, area.y).into(), cell.clone()); + let cleanup_result = crossterm::queue!( terminal.backend_mut(), RestorePosition, EndSynchronizedUpdate - )?; - terminal.swap_buffers(); + ); + let patched = patch_result?; + cleanup_result?; + if patched && let Some(previous) = self.last_frame.as_mut() { + previous[(area.x, area.y)] = cell; + } terminal.backend_mut().flush()?; - self.last_frame = Some(next_frame); - // This path `clone_from`s the whole previous frame and patches one cell, - // so the animation-only repaint's "everything outside the animated rows - // is already seeded" assumption no longer holds. Make it re-seed once. + // The working buffer remains reset. A later animation repaint must seed it. self.seeded_animation_area = None; + if !patched { + return Ok(false); + } crate::tui::ui::note_idle_animation_partial_repaint(); Ok(true) } @@ -654,6 +679,7 @@ fn render_status_spinner_into_buffer(buffer: &Buffer, area: Rect, symbol: &str) && !symbol.is_empty() } +#[cfg(test)] fn render_status_spinner_into_buffer_mut(buffer: &mut Buffer, area: Rect, symbol: &str) { buffer.set_stringn( area.x, @@ -662,13 +688,17 @@ fn render_status_spinner_into_buffer_mut(buffer: &mut Buffer, area: Rect, symbol 1, // The spinner cell is patched outside the full-frame draw, so apply // light-theme adaptation here explicitly (no-op on dark themes). - Style::default().fg(jcode_tui_style::adapt_foreground_for_display( - jcode_tui_style::theme::ai_color(), - ratatui::style::Color::Reset, - )), + status_spinner_patch_style(), ); } +fn status_spinner_patch_style() -> Style { + Style::default().fg(jcode_tui_style::adapt_foreground_for_display( + jcode_tui_style::theme::ai_color(), + ratatui::style::Color::Reset, + )) +} + impl App { /// Run the TUI application /// Returns Some(session_id) if hot-reload was requested @@ -933,6 +963,8 @@ impl App { needs_redraw = true; } + let mut input_redraw = true; + let mut deferred_redraw_at: Option = None; // Main event loop loop { self.sync_sleep_guard(); @@ -970,7 +1002,19 @@ impl App { || last_unfocused_draw .map(|t| t.elapsed() >= UNFOCUSED_IDLE_REDRAW_MIN_INTERVAL) .unwrap_or(true); - if allow_redraw { + // Coalesce event-driven bursts: server events can land a + // few ms apart and each used to trigger a full draw that + // changed nothing. Input-driven and forced redraws still + // paint immediately so typing latency is unaffected. + let coalesce_until = + (!input_redraw && !self.force_full_redraw && !self.force_full_repaint) + .then_some(status_spinner_renderer.last_full_frame_at) + .flatten() + .map(|at| at + EVENT_REDRAW_MIN_INTERVAL) + .filter(|deadline| *deadline > Instant::now()); + deferred_redraw_at = coalesce_until.map(tokio::time::Instant::from_std); + if allow_redraw && deferred_redraw_at.is_none() { + input_redraw = false; // Idle animated screen: repaint only the animation rows // when nothing else is due (see the local loop). if status_spinner_renderer.idle_animation_only_available(&self) @@ -1015,8 +1059,11 @@ impl App { // line while a turn is running. biased; event = event_stream.next() => { + super::wake_stats::note("terminal"); if event.is_some() { - needs_redraw |= remote::handle_terminal_event(&mut self, &mut terminal, &mut remote_conn, event).await?; + let event_redraw = remote::handle_terminal_event(&mut self, &mut terminal, &mut remote_conn, event).await?; + needs_redraw |= event_redraw; + input_redraw |= event_redraw; } else if super::terminal_liveness::terminal_abandoned() { // Input EOF with the controlling terminal gone: // orphaned client (see local loop). Exit; the @@ -1031,14 +1078,29 @@ impl App { } } _ = status_spinner_interval.tick(), if status_spinner_renderer.spinner_only_available(&self) => { + super::wake_stats::note("spinner"); if !status_spinner_renderer.draw_status_spinner_only(&self, &mut terminal)? { needs_redraw = true; } } _ = redraw_interval.tick() => { + super::wake_stats::note("tick"); needs_redraw |= remote::handle_tick(&mut self, &mut remote_conn).await; } + _ = async { + match deferred_redraw_at { + Some(at) => tokio::time::sleep_until(at).await, + None => futures::future::pending::<()>().await, + } + } => { + super::wake_stats::note("deferred_redraw"); + deferred_redraw_at = None; + } event = remote_conn.next_event() => { + match &event { + crate::tui::backend::RemoteRead::Event(event) => super::wake_stats::note_debug("server", event), + crate::tui::backend::RemoteRead::Disconnected(_) => super::wake_stats::note("server:closed"), + } let (outcome, event_redraw) = remote::handle_remote_event( &mut self, &mut terminal, @@ -1069,6 +1131,10 @@ impl App { } } bus_event = bus_receiver_remote.recv() => { + match &bus_event { + Ok(event) => super::wake_stats::note_debug("bus", event), + Err(_) => super::wake_stats::note("bus:lagged"), + } needs_redraw |= remote::handle_bus_event(&mut self, &mut remote_conn, bus_event).await; } } diff --git a/crates/jcode-tui/src/tui/app/run_shell_tests.rs b/crates/jcode-tui/src/tui/app/run_shell_tests.rs index 641a331a3d6..753ab03fb4c 100644 --- a/crates/jcode-tui/src/tui/app/run_shell_tests.rs +++ b/crates/jcode-tui/src/tui/app/run_shell_tests.rs @@ -56,6 +56,218 @@ fn invalidating_the_renderer_forgets_the_drawn_composer() { ); } +#[test] +fn partial_frame_retention_matches_presented_cells_across_buffer_swaps() { + for area in [Rect::new(3, 5, 1, 1), Rect::new(0, 2, 20, 3)] { + let bounds = Rect::new(0, 0, 20, 8); + let mut previous = Buffer::empty(bounds); + previous.set_string(0, 0, "界 header unchanged", Style::default()); + previous.set_string(0, 7, "composer unchanged", Style::default()); + let mut renderer = StatusSpinnerRenderer { + last_frame: Some(previous), + ..Default::default() + }; + let backend = ratatui::backend::TestBackend::new(20, 8); + let mut terminal = ratatui::Terminal::new(backend).unwrap(); + let storage = renderer.last_frame.as_ref().unwrap().content.as_ptr(); + // Returning to the first glyph catches a stale previous-buffer baseline. + // More than two frames also exercises both alternating buffer slots. + for glyph in ["⠋", "⠙", "⠹", "⠸", "⠋", "⠋", "⠙"] { + let working = terminal.current_buffer_mut(); + working + .content + .clone_from(&renderer.last_frame.as_ref().unwrap().content); + for y in area.top()..area.bottom() { + for x in area.left()..area.right() { + working[(x, y)].set_symbol(glyph).set_fg(Color::Cyan); + } + } + let presented = working.clone(); + terminal.flush().unwrap(); + renderer.remember_partial_frame(terminal.current_buffer_mut(), area); + terminal.swap_buffers(); + assert_eq!(renderer.last_frame.as_ref().unwrap(), &presented); + assert_eq!( + renderer.last_frame.as_ref().unwrap().content.as_ptr(), + storage + ); + assert_eq!(terminal.backend().buffer(), &presented); + } + } +} + +#[test] +fn spinner_retention_survives_resize_and_intervening_full_frame() { + let backend = ratatui::backend::TestBackend::new(20, 8); + let mut terminal = ratatui::Terminal::new(backend).unwrap(); + let mut renderer = StatusSpinnerRenderer::default(); + for (width, height) in [(20, 8), (12, 5), (24, 10)] { + terminal.backend_mut().resize(width, height); + terminal.resize(Rect::new(0, 0, width, height)).unwrap(); + let area = Rect::new(1, height - 1, 1, 1); + let frame = terminal + .draw(|frame| { + let buffer = frame.buffer_mut(); + buffer.set_string(0, 0, "界 header", Style::default()); + render_status_spinner_into_buffer_mut(buffer, area, "⠋"); + }) + .unwrap(); + renderer.last_frame = Some(frame.buffer.clone()); + for glyph in ["⠙", "⠋", "⠋", "⠹", "⠋"] { + let previous = renderer.last_frame.as_mut().unwrap(); + render_status_spinner_into_buffer_mut(previous, area, glyph); + assert!( + terminal + .draw_retained_cell((area.x, area.y).into(), previous[(area.x, area.y)].clone()) + .unwrap() + ); + assert_eq!( + terminal.backend().buffer(), + renderer.last_frame.as_ref().unwrap() + ); + assert!( + terminal + .current_buffer_mut() + .content + .iter() + .all(|cell| *cell == ratatui::buffer::Cell::EMPTY) + ); + } + // A normal frame must diff against the patched baseline, not the old + // glyph. This deliberately restores a glyph seen before the patch. + terminal + .draw(|frame| { + *frame.buffer_mut() = renderer.last_frame.as_ref().unwrap().clone(); + render_status_spinner_into_buffer_mut(frame.buffer_mut(), area, "⠙"); + }) + .unwrap(); + assert_eq!(terminal.backend().buffer()[(area.x, area.y)].symbol(), "⠙"); + } +} + +#[test] +fn retained_cell_rejects_wide_continuation_skipped_and_out_of_bounds() { + let backend = ratatui::backend::TestBackend::new(12, 3); + let mut terminal = ratatui::Terminal::new(backend).unwrap(); + terminal + .draw(|frame| { + frame.buffer_mut().set_string(0, 0, "界⠋", Style::default()); + frame.buffer_mut()[(3, 0)].set_symbol("s").set_skip(true); + }) + .unwrap(); + let original = terminal.backend().buffer().clone(); + let mut narrow = ratatui::buffer::Cell::EMPTY; + narrow.set_symbol("⠙"); + for position in [(0, 0), (1, 0), (3, 0), (12, 0), (0, 3)] { + assert!( + !terminal + .draw_retained_cell(position.into(), narrow.clone()) + .unwrap() + ); + assert_eq!(terminal.backend().buffer(), &original); + } + let mut wide = narrow.clone(); + wide.set_symbol("界"); + assert!(!terminal.draw_retained_cell((2, 0).into(), wide).unwrap()); + narrow.set_skip(true); + assert!(!terminal.draw_retained_cell((2, 0).into(), narrow).unwrap()); + assert_eq!(terminal.backend().buffer(), &original); +} + +#[test] +#[ignore = "comparative retained-frame benchmark, run with --ignored --nocapture"] +fn benchmark_partial_frame_retention() { + use std::hint::black_box; + for (width, height) in [(160, 48), (240, 80)] { + let bounds = Rect::new(0, 0, width, height); + let animation_area = Rect::new(0, 3, width, 14); + let spinner_area = Rect::new(2, height - 2, 1, 1); + let iterations = 200; + let mut initial = Buffer::empty(bounds); + initial.set_string(0, 0, "界 unchanged header", Style::default()); + render_status_spinner_into_buffer_mut(&mut initial, spinner_area, "⠋"); + // Exclude one-time animation/config initialization from the comparison. + let mut warmup = initial.clone(); + crate::tui::ui::render_idle_animation_into(&mut warmup, animation_area, 0.0); + for animation in [false, true] { + let mut times = Vec::new(); + let mut baseline_frame = None; + for optimized in [false, true] { + let mut renderer = StatusSpinnerRenderer { + last_frame: Some(initial.clone()), + ..Default::default() + }; + let backend = ratatui::backend::TestBackend::new(width, height); + let mut terminal = ratatui::Terminal::new(backend).unwrap(); + terminal + .draw(|frame| *frame.buffer_mut() = initial.clone()) + .unwrap(); + let start = Instant::now(); + for i in 0..iterations { + if optimized && !animation { + let position = (spinner_area.x, spinner_area.y); + let previous = renderer.last_frame.as_mut().unwrap(); + let mut cell = previous[position].clone(); + cell.set_symbol(if i % 2 == 0 { "⠙" } else { "⠋" }) + .set_style(status_spinner_patch_style()); + assert!( + terminal + .draw_retained_cell(position.into(), cell.clone()) + .unwrap() + ); + previous[position] = cell; + black_box(previous); + continue; + } + let previous = renderer.last_frame.as_ref().unwrap(); + let working = terminal.current_buffer_mut(); + working.content.clone_from(&previous.content); + if animation { + let elapsed = i as f32 / 30.0; + crate::tui::ui::render_idle_animation_into( + working, + animation_area, + elapsed, + ); + if optimized { + renderer.remember_partial_frame(working, animation_area); + } else { + crate::tui::ui::render_idle_animation_into( + renderer.last_frame.as_mut().unwrap(), + animation_area, + elapsed, + ); + } + } else { + let glyph = if i % 2 == 0 { "⠙" } else { "⠋" }; + render_status_spinner_into_buffer_mut(working, spinner_area, glyph); + renderer.remember_partial_frame(working, spinner_area); + } + terminal.flush().unwrap(); + terminal.swap_buffers(); + black_box(renderer.last_frame.as_ref().unwrap()); + } + times.push(start.elapsed()); + assert_eq!( + terminal.backend().buffer(), + renderer.last_frame.as_ref().unwrap() + ); + if optimized { + assert_eq!(renderer.last_frame, baseline_frame); + } else { + baseline_frame = renderer.last_frame; + } + } + eprintln!( + "partial retention {width}x{height} animation={animation} iterations={iterations} baseline={:?} optimized={:?} speedup={:.2}x", + times[0], + times[1], + times[0].as_secs_f64() / times[1].as_secs_f64() + ); + } + } +} + use super::*; use ratatui::style::Color; diff --git a/crates/jcode-tui/src/tui/app/state_ui.rs b/crates/jcode-tui/src/tui/app/state_ui.rs index 2fe26c4e063..b1f19ae9d1e 100644 --- a/crates/jcode-tui/src/tui/app/state_ui.rs +++ b/crates/jcode-tui/src/tui/app/state_ui.rs @@ -212,6 +212,7 @@ impl App { return false; } self.client_focused = focused; + crate::tui::cloud_git::note_client_focus(focused); if focused { // Schedule an immediate differential frame so a newly-focused window // catches up and resumes animation timing from "now". diff --git a/crates/jcode-tui/src/tui/app/state_ui_history_marker.rs b/crates/jcode-tui/src/tui/app/state_ui_history_marker.rs new file mode 100644 index 00000000000..5304984b445 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/state_ui_history_marker.rs @@ -0,0 +1,65 @@ +//! Marker parsing and in-flight fencing for lazily loaded history windows. +use super::{CompactedHistoryLazyState, DisplayMessage}; +use std::time::Instant; + +#[derive(Default)] +pub(super) struct RecentHistoryPagingState { + /// Request id, display generation and target size of the in-flight page. + pub request: Option<(u64, u64, usize)>, + pub retry_at: Option, +} + +pub(super) fn parse_compacted_history_marker( + message: &DisplayMessage, +) -> Option { + if message.role != "system" { + return None; + } + let rest = message + .content + .strip_prefix("Earlier conversation compacted - ") + .or_else(|| { + message + .content + .strip_prefix(crate::protocol::RECENT_HISTORY_MARKER_PREFIX) + })?; + if let Some(rest) = rest.strip_prefix("showing all ") { + let (total, _) = parse_leading_usize(rest)?; + return Some(CompactedHistoryLazyState { + total_messages: total, + visible_messages: total, + ..Default::default() + }); + } + let (first, after_first) = parse_leading_usize(rest)?; + if let Some(showing) = after_first.strip_prefix(" older historical messages hidden. Showing ") { + let (visible, after_visible) = parse_leading_usize(showing)?; + let after_visible = after_visible.strip_prefix(" of ")?; + let (total, _) = parse_leading_usize(after_visible)?; + return Some(CompactedHistoryLazyState { + total_messages: total, + visible_messages: visible, + remaining_messages: first, + ..Default::default() + }); + } + after_first + .starts_with(" historical messages hidden") + .then_some(CompactedHistoryLazyState { + total_messages: first, + remaining_messages: first, + ..Default::default() + }) +} + +fn parse_leading_usize(text: &str) -> Option<(usize, &str)> { + let end = text + .char_indices() + .take_while(|(_, ch)| ch.is_ascii_digit()) + .map(|(idx, ch)| idx + ch.len_utf8()) + .last()?; + let Ok(value) = text[..end].parse() else { + return None; + }; + Some((value, &text[end..])) +} 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 2f29505d4da..98d77fbb0d6 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 @@ -521,6 +521,10 @@ impl App { "Preview replacing cloud code with local files", ), ("/cloud status".into(), "Show session location"), + ( + "/cloud reload".into(), + "Restart the cloud daemon onto its new runtime once idle", + ), ("/cloud cancel".into(), "Cancel a pending cloud command"), ]; // Typing the command or a trailing space must never add consent. diff --git a/crates/jcode-tui/src/tui/app/state_ui_messages.rs b/crates/jcode-tui/src/tui/app/state_ui_messages.rs index 6df95cab96f..2c111e0260d 100644 --- a/crates/jcode-tui/src/tui/app/state_ui_messages.rs +++ b/crates/jcode-tui/src/tui/app/state_ui_messages.rs @@ -1,3 +1,4 @@ +use super::state_ui_history_marker::parse_compacted_history_marker; use super::state_ui_storage::{ compact_display_message_tool_data, compact_display_messages_for_storage, }; @@ -7,7 +8,6 @@ use std::time::{Duration, Instant}; const COMPACTED_HISTORY_CHUNK_MESSAGES: usize = 64; const COMPACTED_HISTORY_LOAD_SCROLL_THRESHOLD: usize = 2; -const COMPACTED_HISTORY_MARKER_PREFIX: &str = "Earlier conversation compacted - "; const OVERNIGHT_CARD_REFRESH_INTERVAL: Duration = Duration::from_secs(5); #[cfg(test)] @@ -727,6 +727,7 @@ impl App { remaining_messages: usize, hidden_user_prompts: usize, ) { + let recent_history = self.recent_history_window_active(); compact_display_messages_for_storage(&mut messages); carry_over_tool_durations(&self.display_messages, &mut messages); self.display_messages = messages; @@ -739,6 +740,11 @@ impl App { hidden_user_prompts, pending_request_visible: None, }; + if recent_history { + // Once the current transcript is fully visible, its original + // compaction marker (if any) becomes the next lazy history source. + self.sync_compacted_history_lazy_from_display_messages(); + } self.auto_scroll_paused = true; // Older messages are prepended above the current view. If the reader had // an anchor captured (they scrolled up to trigger this load), leave the @@ -897,6 +903,9 @@ impl App { } pub(super) fn maybe_queue_compacted_history_load_with_overshoot(&mut self, overshoot: usize) { + if self.recent_history_paging.request.is_some() { + return; + } if !self.auto_scroll_paused { return; } @@ -935,7 +944,7 @@ impl App { if self.is_remote { self.compacted_history_lazy.pending_request_visible = Some(next_visible); self.set_status_notice(format!( - "Loading older compacted history… {} of {}", + "Loading older history… {} of {}", next_visible, self.compacted_history_lazy.total_messages )); } else { @@ -944,9 +953,81 @@ impl App { } pub(super) fn take_pending_compacted_history_load(&mut self) -> Option { + if self + .recent_history_paging + .retry_at + .is_some_and(|deadline| Instant::now() < deadline) + { + return None; + } + self.recent_history_paging.retry_at = None; self.compacted_history_lazy.pending_request_visible.take() } + pub(super) fn recent_history_window_active(&self) -> bool { + self.display_messages.first().is_some_and(|message| { + message.role == "system" + && message + .content + .starts_with(crate::protocol::RECENT_HISTORY_MARKER_PREFIX) + }) + } + + /// A response computed before a live append or rewind must not discard it. + /// Retry against the new generation while retaining the viewport anchor. + pub(super) fn accept_recent_history_response(&mut self, id: u64) -> bool { + let Some((expected, version, visible)) = self.recent_history_paging.request else { + return !self.recent_history_window_active(); + }; + if id != expected { + return false; + } + self.recent_history_paging.request = None; + if version != self.display_messages_version { + if self.recent_history_window_active() { + self.restore_pending_compacted_history_load(visible); + self.recent_history_paging.retry_at = + Some(Instant::now() + Duration::from_millis(250)); + } + return false; + } + true + } + + pub(super) fn recent_history_page_contains_visible_tail( + &mut self, + messages: &mut [DisplayMessage], + visible: usize, + ) -> bool { + if !self.recent_history_window_active() { + return true; + } + compact_display_messages_for_storage(messages); + // A busy agent may supply a persisted snapshot. Generation fencing + // alone cannot detect a snapshot already stale when we sent the request. + // Require its real transcript tail to include the rows on screen, while + // ignoring local status notices which are never part of server history. + let meaningful = |role: &str| matches!(role, "user" | "assistant" | "tool"); + let current: Vec<_> = self + .display_messages + .iter() + .filter(|m| meaningful(&m.role)) + .collect(); + let incoming: Vec<_> = messages.iter().filter(|m| meaningful(&m.role)).collect(); + let matches = current.is_empty() + || incoming.windows(current.len()).any(|window| { + window + .iter() + .zip(¤t) + .all(|(new, old)| new.role == old.role && new.content == old.content) + }); + if !matches { + self.restore_pending_compacted_history_load(visible); + self.recent_history_paging.retry_at = Some(Instant::now() + Duration::from_millis(250)); + } + matches + } + /// Whether there are older compacted-history messages not yet loaded into the /// display transcript. pub(super) fn compacted_history_has_remaining(&self) -> bool { @@ -1065,62 +1146,3 @@ fn carry_over_tool_durations(previous: &[DisplayMessage], incoming: &mut [Displa } } } - -fn parse_compacted_history_marker(message: &DisplayMessage) -> Option { - if message.role != "system" { - return None; - } - let rest = message - .content - .strip_prefix(COMPACTED_HISTORY_MARKER_PREFIX)?; - - if let Some(rest) = rest.strip_prefix("showing all ") { - let (total, _) = parse_leading_usize(rest)?; - return Some(CompactedHistoryLazyState { - total_messages: total, - visible_messages: total, - remaining_messages: 0, - hidden_user_prompts: 0, - pending_request_visible: None, - }); - } - - let (first, after_first) = parse_leading_usize(rest)?; - if after_first.starts_with(" older historical messages hidden. Showing ") { - let showing = after_first.strip_prefix(" older historical messages hidden. Showing ")?; - let (visible, after_visible) = parse_leading_usize(showing)?; - let after_visible = after_visible.strip_prefix(" of ")?; - let (total, _) = parse_leading_usize(after_visible)?; - return Some(CompactedHistoryLazyState { - total_messages: total, - visible_messages: visible, - remaining_messages: first, - hidden_user_prompts: 0, - pending_request_visible: None, - }); - } - - if after_first.starts_with(" historical messages hidden") { - return Some(CompactedHistoryLazyState { - total_messages: first, - visible_messages: 0, - remaining_messages: first, - hidden_user_prompts: 0, - pending_request_visible: None, - }); - } - - None -} - -fn parse_leading_usize(text: &str) -> Option<(usize, &str)> { - let end = text - .char_indices() - .take_while(|(_, ch)| ch.is_ascii_digit()) - .map(|(idx, ch)| idx + ch.len_utf8()) - .last()?; - let Ok(value) = text[..end].parse() else { - return None; - }; - Some((value, &text[end..])) -} diff --git a/crates/jcode-tui/src/tui/app/state_ui_storage.rs b/crates/jcode-tui/src/tui/app/state_ui_storage.rs index 83950ca6920..59c67458a35 100644 --- a/crates/jcode-tui/src/tui/app/state_ui_storage.rs +++ b/crates/jcode-tui/src/tui/app/state_ui_storage.rs @@ -12,6 +12,17 @@ fn compact_tool_input_for_display(name: &str, input: &serde_json::Value) -> serd }; match crate::tui::ui::tools_ui::canonical_tool_name(name) { + // Unlike an optional technical summary, the source is the codemode + // card's body. Keep it on insertion and subsequent snapshot compaction + // so completed calls and restored history render the same preview. + "codemode" => obj(vec![( + "code", + input + .get("code") + .filter(|value| value.is_string()) + .cloned() + .unwrap_or(serde_json::Value::Null), + )]), "bash" => obj(vec![( "command", input @@ -503,6 +514,32 @@ pub(super) fn infer_spawned_session_startup_hints( mod tests { use super::*; + #[test] + fn codemode_preview_survives_transcript_compaction_and_restore() { + let _guard = crate::tui::ui::render_state_test_lock(); + let code = "const value = 1\n return value"; + let mut message = tool_message( + "codemode", + serde_json::json!({ + "code": code, "intent": "Inspect project", "accept_large_output": false, + }), + ); + compact_display_message_tool_data(&mut message); + compact_display_messages_for_storage(std::slice::from_mut(&mut message)); + let tool = message.tool_data.as_ref().unwrap(); + assert_eq!(tool.input, serde_json::json!({"code": code})); + assert_eq!(tool.intent.as_deref(), Some("Inspect project")); + let lines = + crate::tui::ui::render_tool_message(&message, 100, crate::config::DiffDisplayMode::Off); + let text = lines + .iter() + .map(|line| line.to_string()) + .collect::>() + .join("\n"); + assert!(text.contains("const value = 1"), "{text}"); + assert!(text.contains(" return value"), "{text}"); + } + fn tool_message(name: &str, input: serde_json::Value) -> DisplayMessage { DisplayMessage { role: "tool".to_string(), diff --git a/crates/jcode-tui/src/tui/app/swarm_panel_key_tests.rs b/crates/jcode-tui/src/tui/app/swarm_panel_key_tests.rs index af93f72f0e4..be3afa5d20c 100644 --- a/crates/jcode-tui/src/tui/app/swarm_panel_key_tests.rs +++ b/crates/jcode-tui/src/tui/app/swarm_panel_key_tests.rs @@ -1,4 +1,8 @@ -use super::{SwarmPanelAction, swarm_agent_shortcut_index, swarm_panel_action_for_key}; +use super::{ + SwarmPanelAction, swarm_agent_shortcut_index, swarm_panel_action_for_key, + swarm_popout_launch_context, +}; +use crate::tui::TuiState as _; use crossterm::event::{KeyCode, KeyModifiers}; #[test] @@ -40,6 +44,228 @@ fn direct_swarm_shortcuts_work_without_panel_focus_and_empty_swarm_is_safe() { assert!(app.handle_swarm_agent_shortcut(KeyCode::Char('p'), KeyModifiers::ALT)); } +#[test] +fn popout_request_preserves_selected_session_and_uses_connected_socket_only_locally() { + let socket = std::path::Path::new("/tmp/connected socket.sock"); + let context = swarm_popout_launch_context(false, socket); + assert!( + context + .runtime_env + .iter() + .any(|(key, value)| { key == "JCODE_SOCKET" && value == "/tmp/connected socket.sock" }) + ); + + let remote_context = swarm_popout_launch_context(true, socket); + assert!(remote_context.runtime_env.is_empty()); +} + +#[test] +fn local_popout_uses_shared_launcher_command_but_ssh_remains_client_mode() { + let exe = std::path::Path::new(r"C:\Program Files\jcode\jcode.exe"); + let context = jcode_app_core::session_launch::SessionSpawnContext::default() + .with_runtime_env(vec![("JCODE_SOCKET".into(), r"C:\connected.sock".into())]); + let local = super::swarm_agent_terminal_command(exe, "ses_local", &context); + assert_eq!(local.program, exe); + assert_eq!(local.args[0], "--fresh-spawn"); + assert_eq!(local.args[1], "--resume"); + assert_eq!(local.args[2], "ses_local"); + assert_eq!(&local.args[3..], ["--socket", r"C:\connected.sock"]); + assert_eq!(local.session_id.as_deref(), Some("ses_local")); + assert!(local.fresh_spawn); + assert_eq!( + local.runtime_env, + [("JCODE_SOCKET".into(), r"C:\connected.sock".into())] + ); + + let mut selected = None; + let result = super::spawn_swarm_agent_window_with_launchers( + exe, + "ses_local", + std::path::Path::new(r"C:\work"), + &context, + false, + |command, _cwd| { + selected = Some(command.clone()); + Ok(true) + }, + |_, _, _| panic!("local daemon popout must use the shared launcher"), + ) + .unwrap(); + assert!(result); + let selected = selected.unwrap(); + assert_eq!(selected.program, local.program); + assert_eq!(selected.args, local.args); + assert_eq!(selected.title, local.title); + assert_eq!(selected.fresh_spawn, local.fresh_spawn); + assert_eq!(selected.kind, local.kind); + assert_eq!(selected.session_id, local.session_id); + assert_eq!(selected.extra_env, local.extra_env); + assert_eq!(selected.runtime_env, local.runtime_env); + assert_eq!(selected.client_terminal_env, local.client_terminal_env); + + let mut ssh_called = false; + super::spawn_swarm_agent_window_with_launchers( + exe, + "ses_remote", + std::path::Path::new(r"C:\work"), + &context, + true, + |_, _| panic!("SSH popout must keep its existing client launcher"), + |_, session_id, _| { + ssh_called = true; + assert_eq!(session_id, "ses_remote"); + Ok(true) + }, + ) + .unwrap(); + assert!(ssh_called); +} + +#[test] +fn swarm_popout_daemon_client_preserves_runtime_and_ssh_stays_context_free() { + const CHILD: &str = "JCODE_TEST_SWARM_POPOUT_CHILD"; + let scenario = std::env::var(CHILD).ok(); + if scenario.is_none() { + let wrapper_home = tempfile::tempdir().expect("disposable wrapper home"); + let test_name = std::thread::current() + .name() + .expect("named test thread") + .to_owned(); + for (scenario, ssh_host) in [("daemon", None), ("ssh", Some("remote.example"))] { + let home = wrapper_home.path().join(scenario); + let jcode_home = home.join(".jcode"); + let runtime = home.join("runtime"); + std::fs::create_dir_all(&jcode_home).expect("create disposable JCODE_HOME"); + std::fs::create_dir_all(&runtime).expect("create disposable runtime"); + let socket = runtime.join("connected.sock"); + let mut command = std::process::Command::new(std::env::current_exe().unwrap()); + command + .args(["--exact", &test_name, "--nocapture"]) + .env_clear() + .env(CHILD, scenario) + .env("HOME", &home) + .env("JCODE_HOME", &jcode_home) + .env("JCODE_RUNTIME_DIR", &runtime) + .env("JCODE_SOCKET", &socket); + if let Some(ssh_host) = ssh_host { + command.env("JCODE_SSH_REMOTE", ssh_host); + } + let output = command + .output() + .expect("run isolated pop-out regression child"); + assert!( + output.status.success(), + "isolated {scenario} pop-out regression failed:\n{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + assert!( + String::from_utf8_lossy(&output.stdout) + .contains(&format!("SWARM_POPOUT_{scenario}_VERIFIED")), + "isolated {scenario} assertions did not run:\n{}", + String::from_utf8_lossy(&output.stdout) + ); + } + return; + } + + let scenario = scenario.unwrap(); + let ssh = scenario == "ssh"; + assert_eq!(crate::tui::is_ssh_remote(), ssh); + + let coordinator_id = "ses_popout_coordinator"; + let mut app = + crate::tui::app::App::new_for_remote_with_options(Some(coordinator_id.to_string()), true); + assert!( + app.is_remote, + "normal and SSH TUI clients are remote clients" + ); + app.remote_session_id = Some(coordinator_id.to_string()); + app.swarm_enabled = true; + let member = |id: &str, name: &str| crate::protocol::SwarmMemberStatus { + session_id: id.to_string(), + friendly_name: Some(name.to_string()), + status: "running".to_string(), + detail: None, + task_label: None, + role: None, + is_headless: Some(true), + live_attachments: None, + status_age_secs: Some(1), + output_tail: None, + report_back_to_session_id: Some(coordinator_id.to_string()), + todo_progress: None, + todo_items: Vec::new(), + runtime: crate::protocol::SwarmMemberRuntime::default(), + }; + app.remote_swarm_members = vec![ + member("ses_popout_agent_a", "Agent A"), + member("ses_popout_agent_b", "Agent B"), + ]; + app.swarm_panel_selected = 1; + + let displayed = app.inline_swarm_members(); + assert_eq!( + displayed.len(), + 2, + "fixture reaches the inline display filter" + ); + assert_eq!(app.swarm_panel_selected(), 1); + let display_order = crate::tui::info_widget::swarm_gallery::members_display_order(&displayed); + let selected_id = display_order[app.swarm_panel_selected].clone(); + let selected_label = displayed + .iter() + .find(|member| member.session_id == selected_id) + .and_then(|member| member.friendly_name.as_deref()) + .expect("selected display member label"); + + let mut captured = None; + app.pop_out_selected_swarm_agent_with_launcher(|exe, session_id, cwd, context| { + captured = Some(( + exe.to_path_buf(), + session_id.to_string(), + cwd.to_path_buf(), + context.clone(), + )); + Ok(true) + }); + let (exe, session_id, cwd, context) = captured.expect("production launch request captured"); + assert_eq!(exe, super::launch_client_executable()); + assert_eq!(cwd, std::env::current_dir().unwrap()); + assert_eq!( + session_id, selected_id, + "pop-out uses selected display member" + ); + assert_eq!( + app.status_notice().as_deref(), + Some(format!("Opened {selected_label} in a new window").as_str()), + "success notice is constructed by the production pop-out path" + ); + + if ssh { + assert!(context.kind.is_none()); + assert!(context.extra_env.is_empty()); + assert!(context.runtime_env.is_empty()); + assert!(context.client_terminal_env.is_empty()); + println!("SWARM_POPOUT_ssh_VERIFIED"); + } else { + let runtime = std::env::var("JCODE_RUNTIME_DIR").unwrap(); + let home = std::env::var("JCODE_HOME").unwrap(); + let socket = std::env::var("JCODE_SOCKET").unwrap(); + let runtime_value = |key: &str| { + context + .runtime_env + .iter() + .find_map(|(name, value)| (name == key).then_some(value.as_str())) + .unwrap_or_else(|| panic!("missing {key} from production launch context")) + }; + assert_eq!(runtime_value("JCODE_HOME"), home); + assert_eq!(runtime_value("JCODE_RUNTIME_DIR"), runtime); + assert_eq!(runtime_value("JCODE_SOCKET"), socket); + println!("SWARM_POPOUT_daemon_VERIFIED"); + } +} + /// Plain typing (letters, space, enter, arrows without alt) must pass /// through so the user can keep writing into the chat input while the /// panel is focused. diff --git a/crates/jcode-tui/src/tui/app/tests.rs b/crates/jcode-tui/src/tui/app/tests.rs index 9c7d046b3e9..9d5b8c60d43 100644 --- a/crates/jcode-tui/src/tui/app/tests.rs +++ b/crates/jcode-tui/src/tui/app/tests.rs @@ -44,6 +44,7 @@ include!("tests/scroll_copy_03.rs"); include!("tests/resize_viewport.rs"); include!("tests/issue_1642_panel_dismiss.rs"); include!("tests/resize_anchor.rs"); +include!("tests/recent_history.rs"); include!("tests/input_copy_selection.rs"); include!("tests/onboarding_flow.rs"); include!("tests/onboarding_golden.rs"); @@ -63,6 +64,7 @@ include!("tests/oauth_login_progress.rs"); include!("tests/issue_544_paste_enter.rs"); include!("tests/keybinding_hot_reload.rs"); include!("tests/latex_command.rs"); +include!("tests/key_release_redraw.rs"); include!("tests/terminal_setup_command.rs"); include!("tests/issue_497_copy_ctrl_c.rs"); include!("tests/issue_699_ctrl_d_delete.rs"); diff --git a/crates/jcode-tui/src/tui/app/tests/auto_poke_cycles.rs b/crates/jcode-tui/src/tui/app/tests/auto_poke_cycles.rs index af57cb22fd2..5a72fb320f7 100644 --- a/crates/jcode-tui/src/tui/app/tests/auto_poke_cycles.rs +++ b/crates/jcode-tui/src/tui/app/tests/auto_poke_cycles.rs @@ -342,3 +342,37 @@ fn peer_auto_poke_is_shown_and_suppresses_duplicate_local_poke() { ); }); } + +#[test] +fn stale_incomplete_todo_poke_is_dropped_once_todos_are_done() { + with_temp_jcode_home(|| { + let app = create_test_app(); + let done = crate::todo::TodoItem { + id: "todo-1".to_string(), + content: "Ship it".to_string(), + status: "completed".to_string(), + priority: "high".to_string(), + ..Default::default() + }; + crate::todo::save_todos(&app.session.id, &[done.clone()]).expect("save"); + let mut pending = done.clone(); + pending.status = "pending".to_string(); + let poke = crate::todo::build_actionable_poke_message(&[pending]); + + let mut queued = vec![poke.clone(), "real follow-up".to_string()]; + super::commands::drop_stale_incomplete_todo_pokes(&app, &mut queued); + assert_eq!(queued, vec!["real follow-up".to_string()]); + + let mut gated = vec![crate::todo::TODO_FINAL_RESPONSE_CONTINUATION_MESSAGE.to_string()]; + super::commands::drop_stale_incomplete_todo_pokes(&app, &mut gated); + assert_eq!(gated.len(), 1, "non incomplete-todo continuations are kept"); + + crate::todo::save_todos(&app.session.id, &[done]).expect("save"); + let mut open_case = vec![poke]; + let mut todo = crate::todo::load_todos(&app.session.id).unwrap(); + todo[0].status = "in_progress".to_string(); + crate::todo::save_todos(&app.session.id, &todo).expect("save"); + super::commands::drop_stale_incomplete_todo_pokes(&app, &mut open_case); + assert_eq!(open_case.len(), 1, "poke is kept while todos are open"); + }); +} diff --git a/crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs b/crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs index a3fbf26a42b..46c9b56a994 100644 --- a/crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs @@ -15,6 +15,62 @@ fn slash_resume_opens_session_picker_overlay_locally() { assert!(app.input.is_empty()); } +#[test] +fn session_picker_cloud_warning_keeps_refresh_pending_and_survives_fresh() { + use crate::tui::session_picker::{SessionListUpdate, SessionPicker}; + let mut app = create_test_app(); + app.session_picker_mode = SessionPickerMode::Resume; + app.session_picker_overlay = Some(RefCell::new(SessionPicker::loading())); + let (sender, receiver) = std::sync::mpsc::channel(); + app.pending_session_picker_load = Some(PendingSessionPickerLoad { receiver }); + let warning = "Cloud sessions could not be refreshed. Reopen /resume to retry."; + sender + .send(SessionListUpdate::CloudUnavailable(warning.into())) + .unwrap(); + assert!(app.poll_session_picker_load()); + assert!(app.pending_session_picker_load.is_some()); + assert_eq!( + app.session_picker_overlay + .as_ref() + .unwrap() + .borrow() + .cloud_warning + .as_deref(), + Some(warning) + ); + sender + .send(SessionListUpdate::Fresh(Ok((vec![], vec![])))) + .unwrap(); + assert!(app.poll_session_picker_load()); + assert!(app.pending_session_picker_load.is_none()); + assert_eq!( + app.session_picker_overlay + .as_ref() + .unwrap() + .borrow() + .cloud_warning + .as_deref(), + Some(warning) + ); +} + +#[test] +fn session_picker_cloud_warning_does_not_reopen_closed_overlay() { + use crate::tui::session_picker::SessionListUpdate; + let mut app = create_test_app(); + app.session_picker_mode = SessionPickerMode::Resume; + let (sender, receiver) = std::sync::mpsc::channel(); + app.pending_session_picker_load = Some(PendingSessionPickerLoad { receiver }); + sender + .send(SessionListUpdate::CloudUnavailable( + "Reopen /resume to retry.".into(), + )) + .unwrap(); + assert!(!app.poll_session_picker_load()); + assert!(app.session_picker_overlay.is_none()); + assert!(app.pending_session_picker_load.is_some()); +} + #[test] fn session_picker_cached_update_keeps_fresh_refresh_pending() { use crate::tui::session_picker::{SessionListUpdate, SessionPicker}; diff --git a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01.rs b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01.rs index 363958b287c..5ebba65601d 100644 --- a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01.rs @@ -1,5 +1,6 @@ fn remote_account_fixture(label: &str) -> crate::protocol::AccountUsageSnapshot { crate::protocol::AccountUsageSnapshot { + additional_windows: Vec::new(), label: label.into(), email: None, exhausted: false, diff --git a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01/improvement_commands.rs b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01/improvement_commands.rs index feaac49bbef..3be8d9292f8 100644 --- a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01/improvement_commands.rs +++ b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01/improvement_commands.rs @@ -64,6 +64,7 @@ fn test_improve_status_summarizes_current_todos() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -82,6 +83,7 @@ fn test_improve_status_summarizes_current_todos() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -181,6 +183,7 @@ fn test_improve_resume_uses_saved_mode_and_current_todos() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_02.rs b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_02.rs index 6e725ebe89e..0eecb825ee0 100644 --- a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_02.rs @@ -140,6 +140,7 @@ fn test_refactor_status_summarizes_current_todos() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -158,6 +159,7 @@ fn test_refactor_status_summarizes_current_todos() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -206,6 +208,7 @@ fn test_refactor_resume_uses_saved_mode_and_current_todos() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/idle_soft_interrupt_cancel.rs b/crates/jcode-tui/src/tui/app/tests/idle_soft_interrupt_cancel.rs index b020c7ef3be..2d409fc7256 100644 --- a/crates/jcode-tui/src/tui/app/tests/idle_soft_interrupt_cancel.rs +++ b/crates/jcode-tui/src/tui/app/tests/idle_soft_interrupt_cancel.rs @@ -95,3 +95,102 @@ fn idle_soft_interrupt_cancel_settles_pre_stream_turn_without_replaying_prompt() } }); } + +/// A soft interrupt sent just as the turn ended reaches an idle server, which +/// starts it as its own turn. The client must not also requeue and resend it. +#[test] +fn stranded_soft_interrupt_recovery_requeues_only_what_server_still_queued() { + use crate::protocol::ServerEvent; + use crate::tui::backend::soft_interrupt_client_id; + with_temp_jcode_home(|| { + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + for server_accepted in [true, false] { + let mut app = create_test_app(); + app.is_remote = true; + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + remote.mark_history_loaded(); + app.track_pending_soft_interrupt(42, "next prompt".into()); + app.handle_server_event(ServerEvent::Ack { id: 42 }, &mut remote); + + rt.block_on(remote::process_remote_followups(&mut app, &mut remote)); + let (cancel_id, _) = app + .soft_interrupt_recovery + .expect("recovery must ask the server before requeueing"); + assert!(app.queued_messages.is_empty()); + assert!(!app.is_processing, "nothing may be sent while waiting"); + assert_eq!(app.pending_soft_interrupts, ["next prompt"]); + + let client_id = soft_interrupt_client_id(42); + let (cancelled, not_queued) = if server_accepted { + (vec![], vec![client_id]) + } else { + (vec![client_id], vec![]) + }; + if server_accepted { + app.handle_server_event( + ServerEvent::SoftInterruptInjected { + content: "next prompt".into(), + display_role: None, + point: "turn_start".into(), + tools_skipped: None, + client_ids: vec![], + }, + &mut remote, + ); + } + app.handle_server_event( + ServerEvent::SoftInterruptsCancelled { + id: cancel_id, + cancelled, + not_queued, + }, + &mut remote, + ); + assert!(app.soft_interrupt_recovery.is_none()); + assert!(app.pending_soft_interrupts.is_empty()); + assert!(app.sent_soft_interrupts.is_empty()); + if server_accepted { + assert!( + app.queued_messages.is_empty(), + "an accepted interrupt must not be resent" + ); + let shown = app + .display_messages + .iter() + .filter(|message| message.content == "next prompt") + .count(); + assert_eq!(shown, 1); + } else { + assert_eq!(app.queued_messages, ["next prompt"]); + } + } + }); +} + +#[test] +fn stranded_soft_interrupt_not_acked_and_not_queued_is_requeued() { + use crate::protocol::ServerEvent; + with_temp_jcode_home(|| { + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let mut app = create_test_app(); + app.is_remote = true; + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + remote.mark_history_loaded(); + app.track_pending_soft_interrupt(7, "lost prompt".into()); + rt.block_on(remote::process_remote_followups(&mut app, &mut remote)); + let (cancel_id, _) = app.soft_interrupt_recovery.unwrap(); + app.handle_server_event( + ServerEvent::SoftInterruptsCancelled { + id: cancel_id, + cancelled: vec![], + not_queued: vec![crate::tui::backend::soft_interrupt_client_id(7)], + }, + &mut remote, + ); + assert_eq!(app.queued_messages, ["lost prompt"]); + assert!(app.pending_soft_interrupts.is_empty()); + assert!(app.pending_soft_interrupt_requests.is_empty()); + }); +} diff --git a/crates/jcode-tui/src/tui/app/tests/key_release_redraw.rs b/crates/jcode-tui/src/tui/app/tests/key_release_redraw.rs new file mode 100644 index 00000000000..63b5c8d164a --- /dev/null +++ b/crates/jcode-tui/src/tui/app/tests/key_release_redraw.rs @@ -0,0 +1,34 @@ +// Key releases (kitty keyboard protocol reports one per keystroke) must not +// force a full frame unless they change visible state. +#[test] +fn plain_key_release_changes_no_visible_state() { + use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}; + let ev = |code, modifiers, kind| KeyEvent { + code, + modifiers, + kind, + state: KeyEventState::NONE, + }; + let mut app = create_test_app(); + // Ordinary typing: press then release 'a'. No badge is shown, so the + // release must not count as a visible change. + app.update_copy_badge_key_event(ev(KeyCode::Char('a'), KeyModifiers::NONE, KeyEventKind::Press)); + let before = app.key_release_visible_state(); + app.update_copy_badge_key_event(ev(KeyCode::Char('a'), KeyModifiers::NONE, KeyEventKind::Release)); + assert_eq!(before, app.key_release_visible_state()); + + // Releasing a held Alt does hide the badge, so that release must draw. + app.update_copy_badge_key_event(ev( + KeyCode::Modifier(crossterm::event::ModifierKeyCode::LeftAlt), + KeyModifiers::ALT, + KeyEventKind::Press, + )); + app.copy_badge_ui.alt_pulse_until = None; + let before = app.key_release_visible_state(); + app.update_copy_badge_key_event(ev( + KeyCode::Modifier(crossterm::event::ModifierKeyCode::LeftAlt), + KeyModifiers::NONE, + KeyEventKind::Release, + )); + assert_ne!(before, app.key_release_visible_state()); +} diff --git a/crates/jcode-tui/src/tui/app/tests/oauth_login_progress.rs b/crates/jcode-tui/src/tui/app/tests/oauth_login_progress.rs index 12bcc9ec75e..82480fa2023 100644 --- a/crates/jcode-tui/src/tui/app/tests/oauth_login_progress.rs +++ b/crates/jcode-tui/src/tui/app/tests/oauth_login_progress.rs @@ -214,19 +214,19 @@ fn oauth_login_progress_real_frame_has_clickable_link_and_expand_button() { crate::tui::ui::oauth_login_link_message_from_screen(link_col + 2, link_row), Some(index) ); - let (col, row) = find(&terminal, "▸ expand"); + let (col, row) = find(&terminal, "⊞"); assert!(app.try_toggle_swarm_expand_at(col + 2, row)); let expanded = render_and_snap(&app, &mut terminal); assert!(expanded.contains("client_id=jcode-fixture")); println!("Observed expanded login frame:\n{expanded}"); - let (col, row) = find(&terminal, "▾ collapse"); + let (col, row) = find(&terminal, "⊟"); assert!(app.try_toggle_swarm_expand_at(col + 2, row)); let collapsed = render_and_snap(&app, &mut terminal); assert!(!collapsed.contains("client_id=jcode-fixture")); app.handle_debug_command("login-progress-success-fixture"); let success = render_and_snap(&app, &mut terminal); assert!(success.contains("OpenAI login complete")); - assert!(!success.contains("▸ expand")); + assert!(!success.contains("⊞")); assert!(!success.contains("Open OpenAI sign-in")); assert!(!success.contains("Login only:")); println!("Observed successful login frame:\n{success}"); diff --git a/crates/jcode-tui/src/tui/app/tests/recent_history.rs b/crates/jcode-tui/src/tui/app/tests/recent_history.rs new file mode 100644 index 00000000000..07f378a6304 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/tests/recent_history.rs @@ -0,0 +1,183 @@ +fn recent_history_test_app() -> App { + let mut app = create_test_app(); + app.is_remote = true; + app.remote_session_id = Some("recent-test".into()); + app.replace_display_messages(vec![ + DisplayMessage::system("Earlier remote history - 872 older historical messages hidden. Showing 128 of 1000 messages. Scroll to the top to load more. Hidden user prompts: 99."), + DisplayMessage::user("latest prompt"), + DisplayMessage::assistant("latest answer"), + ]); + app +} + +#[test] +fn recent_history_bootstrap_renders_latest_frame_without_older_transcript() { + let _lock = scroll_render_test_lock(); + let mut app = recent_history_test_app(); + app.diagram_mode = crate::config::DiagramDisplayMode::None; + app.diagram_pane_enabled = false; + app.status = ProcessingStatus::Idle; + let messages = std::iter::once(app.display_messages[0].clone()) + .chain((0..128).map(|i| { + DisplayMessage::assistant(format!("RECENT{i:03} {}", "bounded history ".repeat(128))) + })) + .map(|message| crate::protocol::HistoryMessage { + stored_index: None, + role: message.role, + content: message.content, + tool_calls: None, + tool_data: None, + response_stats: None, + }) + .collect(); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + let start = std::time::Instant::now(); + assert!(app.handle_server_event( + delta_history_event("recent-test", messages, None), + &mut remote + )); + let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap(); + render_and_snap(&app, &mut terminal); + let elapsed = start.elapsed(); + assert_eq!(app.display_messages.len(), 129); + assert!(buffer_to_text(&terminal).contains("RECENT127")); + assert_eq!(app.compacted_hidden_user_prompts(), 99); + eprintln!( + "recent_history initial event + 128-message frame: {:.2}ms", + elapsed.as_secs_f64() * 1000.0 + ); +} + +#[test] +fn recent_history_marker_preserves_absolute_prompt_numbers_and_queues_page() { + let mut app = recent_history_test_app(); + assert!(app.recent_history_window_active()); + assert_eq!(app.compacted_hidden_user_prompts(), 99); + assert_eq!( + crate::tui::ui::input_ui::next_input_prompt_number(&app), + 101 + ); + app.auto_scroll_paused = true; + app.scroll_offset = 0; + app.maybe_queue_compacted_history_load(); + assert_eq!(app.take_pending_compacted_history_load(), Some(192)); +} + +#[test] +fn recent_history_page_retries_instead_of_discarding_live_append() { + let mut app = recent_history_test_app(); + app.recent_history_paging.request = Some((42, app.display_messages_version, 192)); + app.display_messages + .push(DisplayMessage::assistant("live response after request")); + app.bump_display_messages_version(); + assert!(!app.accept_recent_history_response(42)); + assert_eq!( + app.display_messages.last().unwrap().content, + "live response after request" + ); + assert_eq!( + app.compacted_history_lazy.pending_request_visible, + Some(192) + ); + assert_eq!(app.take_pending_compacted_history_load(), None); + app.recent_history_paging.retry_at = Some(Instant::now()); + assert_eq!(app.take_pending_compacted_history_load(), Some(192)); +} + +#[test] +fn recent_history_page_rejects_wrong_request_and_rewind() { + let mut app = recent_history_test_app(); + app.recent_history_paging.request = Some((42, app.display_messages_version, 192)); + assert!(!app.accept_recent_history_response(41)); + assert!(app.recent_history_paging.request.is_some()); + app.replace_display_messages(vec![DisplayMessage::user("rewound transcript")]); + assert!(!app.accept_recent_history_response(42)); + assert_eq!(app.take_pending_compacted_history_load(), None); + assert_eq!(app.display_messages.len(), 1); +} + +#[test] +fn recent_history_full_window_restores_archived_compaction_loading() { + let mut app = recent_history_test_app(); + app.recent_history_paging.request = Some((42, app.display_messages_version, 1000)); + assert!(app.accept_recent_history_response(42)); + app.apply_compacted_history_window( + vec![ + DisplayMessage::system("Earlier conversation compacted - 240 historical messages hidden from the UI. Scroll to the top to load older history. Hidden user prompts: 31."), + DisplayMessage::user("current context prompt"), + ], vec![], 1000, 1000, 0, 31, + ); + assert!(!app.recent_history_window_active()); + assert_eq!(app.compacted_history_lazy_state().total_messages, 240); + assert_eq!(app.compacted_history_lazy_state().remaining_messages, 240); + assert_eq!(app.compacted_hidden_user_prompts(), 31); +} + +#[test] +fn recent_history_page_accepts_unchanged_generation() { + let mut app = recent_history_test_app(); + app.recent_history_paging.request = Some((42, app.display_messages_version, 192)); + assert!(app.accept_recent_history_response(42)); + assert!(app.recent_history_paging.request.is_none()); + // Unsolicited page payloads cannot replace a bounded current-history view. + assert!(!app.accept_recent_history_response(42)); +} + +#[test] +fn recent_history_page_rejects_snapshot_missing_already_visible_rows() { + let mut app = recent_history_test_app(); + let mut stale = vec![DisplayMessage::user("latest prompt")]; + assert!(!app.recent_history_page_contains_visible_tail(&mut stale, 192)); + assert_eq!( + app.compacted_history_lazy.pending_request_visible, + Some(192) + ); + assert_eq!( + app.display_messages.last().unwrap().content, + "latest answer" + ); + let mut newer = vec![ + DisplayMessage::assistant("older page content"), + DisplayMessage::user("latest prompt"), + DisplayMessage::assistant("latest answer"), + DisplayMessage::assistant("even newer persisted response"), + ]; + assert!(app.recent_history_page_contains_visible_tail(&mut newer, 192)); +} + +#[test] +fn recent_history_disconnect_releases_page_fence() { + let mut app = recent_history_test_app(); + app.recent_history_paging.request = Some((42, app.display_messages_version, 192)); + app.recent_history_paging.retry_at = Some(Instant::now() + Duration::from_secs(30)); + app.pending_history_anchor = Some(super::HistoryScrollAnchor { + lines_from_bottom: 3, + base_total: 3, + }); + let mut state = remote::RemoteRunState::default(); + remote::handle_disconnect(&mut app, &mut state, None); + assert!(app.recent_history_paging.request.is_none()); + assert!(app.pending_history_anchor.is_none()); + app.auto_scroll_paused = true; + app.scroll_offset = 0; + app.maybe_queue_compacted_history_load(); + assert_eq!(app.take_pending_compacted_history_load(), Some(192)); +} + +#[test] +fn recent_history_session_switch_releases_page_fence() { + let mut app = recent_history_test_app(); + app.recent_history_paging.request = Some((42, app.display_messages_version, 192)); + app.compacted_history_lazy.pending_request_visible = Some(192); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let mut connection = crate::tui::backend::RemoteConnection::dummy(); + app.handle_server_event( + delta_history_event("other-session", Vec::new(), None), + &mut connection, + ); + assert!(app.recent_history_paging.request.is_none()); + assert!(app.compacted_history_lazy.pending_request_visible.is_none()); +} 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 a9489de84c6..c8ebf92dc38 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 @@ -1397,6 +1397,7 @@ fn test_remote_done_auto_pokes_again_when_todos_remain() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -1712,3 +1713,36 @@ fn test_pending_startup_notice_survives_history_bootstrap_for_fresh_session() { "startup notice should be re-applied exactly once after bootstrap" ); } + +#[test] +fn test_reload_recovery_turn_runs_before_user_queued_followups() { + let mut app = create_test_app(); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + remote.mark_history_loaded(); + + // A user follow-up was queued before a mid-turn reload, and History + // reports the turn was interrupted, so a recovery continuation is queued. + app.queued_messages.push("queued before reload".to_string()); + let directive = crate::tool::selfdev::ReloadRecoveryDirective { + reconnect_notice: None, + continuation_message: "continue interrupted turn".to_string(), + }; + assert!(remote::queue_reload_continuation_once( + &mut app, + "session_reload_queue_order", + directive, + "Reload complete - continuing." + )); + + rt.block_on(remote::process_remote_followups(&mut app, &mut remote)); + assert!(app.is_processing, "recovery continuation should start a turn"); + assert!(app.hidden_queued_system_messages.is_empty()); + assert_eq!( + app.queued_messages(), + &["queued before reload"], + "user follow-up must wait for the recovery turn to finish" + ); + assert!(!app.reload_recovery_blocks_queue()); +} 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 f5990b3d5bb..285b505ca31 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 @@ -85,6 +85,7 @@ fn test_remote_auto_poke_followup_preserves_visible_timer_and_stays_hidden() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -145,6 +146,7 @@ fn test_remote_auto_poke_challenges_abrupt_confidence_increase() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -216,6 +218,7 @@ fn test_remote_auto_poke_completion_below_threshold_tells_model_to_keep_working( &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -281,6 +284,7 @@ fn test_remote_poke_status_and_off_update_state() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_02/part_01.rs b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_02/part_01.rs index cfe6d02d8dc..6ca8f6aab53 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_02/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_02/part_01.rs @@ -10,6 +10,7 @@ fn test_remote_poke_queues_when_turn_is_in_progress() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -59,6 +60,7 @@ fn test_remote_poke_queues_when_turn_is_in_progress() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -77,6 +79,7 @@ fn test_remote_poke_queues_when_turn_is_in_progress() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -173,6 +176,7 @@ fn test_remote_interrupted_auto_poke_requeues_after_deferred_poke() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, 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 90621cb8937..f3d81d5d5bb 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 @@ -763,6 +763,7 @@ fn test_delta_history_rebuilds_transcript_from_advertised_prefix() { count: 2, fingerprint: 0, images_fingerprint: None, + recent_messages: None, }, prefix: Some(prefix), images: Some(vec![crate::session::RenderedImage { diff --git a/crates/jcode-tui/src/tui/app/tests/remote_failover_routes.rs b/crates/jcode-tui/src/tui/app/tests/remote_failover_routes.rs index 7d726abc62d..380ac8129d0 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_failover_routes.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_failover_routes.rs @@ -244,7 +244,9 @@ fn remote_exact_failover_fixture() -> (App, crate::tui::backend::RemoteConnectio #[test] fn test_remote_failover_countdown_holds_payload_and_stages_exact_route_after_deadline() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); let (mut app, _remote) = remote_exact_failover_fixture(); @@ -287,9 +289,74 @@ fn test_remote_failover_countdown_holds_payload_and_stages_exact_route_after_dea } #[test] -fn test_remote_generic_auth_error_arms_countdown_for_exact_pool_route() { +fn test_remote_definitive_exhaustion_immediately_stages_exact_route_not_resend() { + with_temp_jcode_home(|| { + write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let (app, _remote) = remote_exact_failover_fixture(); + assert!(app.pending_provider_failover.is_none()); + let target = app + .pending_automatic_failover_route + .as_ref() + .expect("immediate route switch"); + assert_eq!( + target.routed_model_spec(), + "openai-oauth@openai-fox:gpt-5.4" + ); + assert!(app.pending_fallback_resend.is_some()); + assert!(app.pending_fallback_offer.is_none()); + assert_eq!(app.remote_provider_name.as_deref(), Some("Anthropic")); + assert!(!app.pending_turn); + }); +} + +#[test] +fn test_remote_immediate_failover_dispatches_once_and_suppresses_exhaustion_spam() { with_temp_jcode_home(|| { write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let (mut app, mut connection) = remote_exact_failover_fixture(); + let message_count = app.display_messages().len(); + for _ in 0..10 { + app.handle_server_event( + crate::protocol::ServerEvent::Error { + id: 21, + message: "429 usage limit reached; quota exhausted".into(), + retry_after_secs: None, + }, + &mut connection, + ); + } + assert_eq!(app.display_messages().len(), message_count); + assert!(app.pending_fallback_offer.is_none()); + assert_eq!(app.pending_fallback_resend.as_ref().unwrap().content, "hi"); + rt.block_on(remote::handle_tick(&mut app, &mut connection)); + let request = app + .pending_automatic_failover_request_id + .expect("switch dispatched"); + assert!(app.remote_model_switch_in_flight); + assert_eq!( + app.pending_automatic_failover_route + .as_ref() + .unwrap() + .routed_model_spec(), + "openai-oauth@openai-fox:gpt-5.4" + ); + rt.block_on(remote::handle_tick(&mut app, &mut connection)); + assert_eq!(app.pending_automatic_failover_request_id, Some(request)); + assert!(app.pending_fallback_resend.is_some()); + assert!(!app.pending_turn); + }); +} + +#[test] +fn test_remote_generic_auth_error_arms_countdown_for_exact_pool_route() { + with_temp_jcode_home(|| { + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); seed_failover_openai_fox(); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); @@ -307,9 +374,264 @@ fn test_remote_generic_auth_error_arms_countdown_for_exact_pool_route() { } #[test] -fn test_generic_auto_fallback_skips_excluded_preferred_and_checks_source_pool() { +fn test_remote_failover_ignores_confirmation_before_request_is_sent() { + with_temp_jcode_home(|| { + write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let (mut app, mut connection) = remote_exact_failover_fixture(); + app.handle_server_event( + crate::protocol::ServerEvent::ModelChanged { + id: 0, + model: "claude-sonnet-4".into(), + provider_name: Some("Anthropic".into()), + error: None, + provider_oauth_email: None, + provider_oauth_account_label: Some("claude-otter".into()), + resolved_credential: Some(jcode_provider_core::ResolvedCredential::Oauth), + available_reasoning_efforts: None, + reasoning_effort: None, + context_window: None, + }, + &mut connection, + ); + assert!(app.pending_automatic_failover_route.is_some()); + assert!(app.pending_fallback_resend.is_some()); + connection.mark_history_loaded(); + rt.block_on(remote::process_remote_followups(&mut app, &mut connection)); + assert!(!app.is_processing); + assert!(app.pending_fallback_resend.is_some()); + }); +} + +#[test] +fn test_remote_failover_stops_when_cross_route_retry_budget_is_spent() { + with_temp_jcode_home(|| { + write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let (mut app, mut connection) = remote_exact_failover_fixture(); + let payload = app.pending_fallback_resend.take().unwrap(); + app.pending_automatic_failover_route = None; + app.rate_limit_pending_message = Some(PendingRemoteMessage { + content: payload.content, + images: payload.images, + is_system: payload.is_system, + system_reminder: payload.system_reminder, + auto_retry: true, + retry_attempts: App::AUTO_RETRY_MAX_ATTEMPTS, + retry_at: None, + }); + let prompt = jcode_provider_core::ProviderFailoverPrompt { + from_provider: "claude".into(), + from_label: "Anthropic".into(), + to_provider: "openai".into(), + to_label: "OpenAI fox".into(), + target_route: Some("openai-oauth@openai-fox:gpt-5.4".into()), + reason: "OAuth usage exhausted".into(), + estimated_input_chars: 0, + estimated_input_tokens: 0, + }; + app.handle_server_event( + crate::protocol::ServerEvent::Error { + id: 23, + message: prompt.to_error_message(), + retry_after_secs: None, + }, + &mut connection, + ); + assert!(app.pending_provider_failover.is_none()); + assert!(app.pending_automatic_failover_route.is_none()); + assert!(app.pending_automatic_failover_request_id.is_none()); + assert!(app.rate_limit_pending_message.is_none()); + assert_eq!(app.input, "hi"); + assert!( + app.display_messages() + .iter() + .any(|m| m.content.contains("Stopped automatic failover")) + ); + }); +} + +#[test] +fn test_remote_manual_fallback_waits_for_request_dispatch() { + with_temp_jcode_home(|| { + write_test_config("[provider]\ncross_provider_failover = \"manual\"\n"); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let (mut app, mut connection) = remote_exact_failover_fixture(); + connection.mark_history_loaded(); + assert!(app.apply_pending_fallback_offer()); + assert!(app.pending_route_selection.is_some()); + assert_eq!( + app.session.route_api_method.as_deref(), + Some("claude-oauth@claude-otter") + ); + rt.block_on(remote::process_remote_followups(&mut app, &mut connection)); + assert!(!app.is_processing); + assert!(app.pending_fallback_resend.is_some()); + }); +} + +#[test] +fn test_remote_failover_resends_only_after_exact_confirmation() { + with_temp_jcode_home(|| { + write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + for mismatch in ["none", "model", "account", "credential", "provider"] { + let (mut app, mut connection) = remote_exact_failover_fixture(); + connection.mark_history_loaded(); + app.pending_automatic_failover_request_id = Some(22); + app.remote_model_switch_in_flight = true; + app.handle_server_event( + crate::protocol::ServerEvent::ModelChanged { + id: 22, + model: if mismatch == "model" { + "wrong-model" + } else { + "gpt-5.4" + } + .into(), + provider_name: Some( + if mismatch == "provider" { + "Anthropic" + } else { + "OpenAI" + } + .into(), + ), + error: None, + provider_oauth_email: None, + provider_oauth_account_label: Some( + if mismatch == "account" { + "wrong-account" + } else { + "openai-fox" + } + .into(), + ), + resolved_credential: Some(if mismatch == "credential" { + jcode_provider_core::ResolvedCredential::ApiKey + } else { + jcode_provider_core::ResolvedCredential::Oauth + }), + available_reasoning_efforts: None, + reasoning_effort: None, + context_window: None, + }, + &mut connection, + ); + rt.block_on(remote::process_remote_followups(&mut app, &mut connection)); + assert!(app.pending_automatic_failover_request_id.is_none()); + assert!(app.pending_automatic_failover_route.is_none()); + assert!(app.pending_fallback_resend.is_none()); + assert!(!app.remote_model_switch_in_flight); + if mismatch == "none" { + assert!(app.is_processing); + assert_eq!( + app.rate_limit_pending_message + .as_ref() + .unwrap() + .retry_attempts, + 1 + ); + assert_eq!( + app.rate_limit_pending_message.as_ref().unwrap().content, + "hi" + ); + assert_eq!( + app.remote_provider_oauth_account_label.as_deref(), + Some("openai-fox") + ); + } else { + assert!(!app.is_processing, "{mismatch}"); + assert_eq!(app.input, "hi"); + assert!( + app.display_messages() + .iter() + .any(|m| m.content.contains("did not confirm")) + ); + } + } + }); +} + +#[test] +fn test_remote_failover_control_error_releases_switch_without_retry() { with_temp_jcode_home(|| { write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let (mut app, mut connection) = remote_exact_failover_fixture(); + app.pending_automatic_failover_request_id = Some(22); + app.remote_model_switch_in_flight = true; + app.handle_server_event( + crate::protocol::ServerEvent::Error { + id: 22, + message: "Target is no longer available".into(), + retry_after_secs: Some(60), + }, + &mut connection, + ); + assert!(app.pending_automatic_failover_request_id.is_none()); + assert!(app.pending_automatic_failover_route.is_none()); + assert!(app.pending_fallback_resend.is_none()); + assert!(!app.remote_model_switch_in_flight); + assert!(app.rate_limit_reset.is_none()); + assert!(app.rate_limit_pending_message.is_none()); + assert_eq!(app.input, "hi"); + }); +} + +#[test] +fn test_remote_failover_disconnect_does_not_wait_for_lost_confirmation() { + with_temp_jcode_home(|| { + write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let (mut app, _connection) = remote_exact_failover_fixture(); + app.pending_automatic_failover_request_id = Some(22); + app.remote_model_switch_in_flight = true; + let mut state = remote::RemoteRunState::default(); + remote::handle_disconnect(&mut app, &mut state, None); + assert!(app.pending_automatic_failover_request_id.is_none()); + assert!(app.pending_automatic_failover_route.is_none()); + assert!(app.pending_fallback_resend.is_none()); + assert!(!app.remote_model_switch_in_flight); + assert_eq!(app.input, "hi"); + }); +} + +#[test] +fn test_remote_failover_confirmation_timeout_restores_prompt_once() { + with_temp_jcode_home(|| { + write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let (mut app, mut connection) = remote_exact_failover_fixture(); + app.pending_automatic_failover_request_id = Some(22); + app.pending_automatic_failover_started = Some(Instant::now() - Duration::from_secs(31)); + app.remote_model_switch_in_flight = true; + rt.block_on(remote::handle_tick(&mut app, &mut connection)); + assert!(app.pending_automatic_failover_request_id.is_none()); + assert!(app.pending_automatic_failover_started.is_none()); + assert!(app.pending_automatic_failover_route.is_none()); + assert!(app.pending_fallback_resend.is_none()); + assert!(!app.remote_model_switch_in_flight); + assert_eq!(app.input, "hi"); + let messages = app.display_messages().len(); + rt.block_on(remote::handle_tick(&mut app, &mut connection)); + assert_eq!(app.display_messages().len(), messages); + }); +} + +#[test] +fn test_generic_auto_fallback_skips_excluded_preferred_and_checks_source_pool() { + with_temp_jcode_home(|| { + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); seed_failover_openai_fox(); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); @@ -361,7 +683,9 @@ fn test_generic_auto_fallback_skips_excluded_preferred_and_checks_source_pool() #[test] fn test_generic_auto_fallback_respects_explicit_pool_account_order() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); seed_failover_openai_fox(); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); @@ -393,7 +717,9 @@ fn test_generic_auto_fallback_respects_explicit_pool_account_order() { #[test] fn test_generic_quota_fallback_skips_exhausted_account_sibling_model() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); seed_failover_openai_fox(); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); @@ -431,7 +757,9 @@ fn test_generic_quota_fallback_skips_exhausted_account_sibling_model() { #[test] fn test_rejected_remote_automatic_route_retains_old_identity_and_drops_payload() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); let (mut app, mut remote) = remote_exact_failover_fixture(); @@ -507,7 +835,9 @@ fn test_rejected_remote_automatic_route_retains_old_identity_and_drops_payload() #[test] fn test_remote_failover_esc_cancels_without_route_request_or_resend() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); let (mut app, mut remote) = remote_exact_failover_fixture(); @@ -527,7 +857,9 @@ fn test_remote_failover_esc_cancels_without_route_request_or_resend() { #[test] fn test_remote_failover_typing_and_enter_queue_instead_of_canceling() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); let (mut app, mut remote) = remote_exact_failover_fixture(); @@ -558,7 +890,9 @@ fn test_remote_failover_typing_and_enter_queue_instead_of_canceling() { #[test] fn test_cancel_image_only_failover_restores_attachments() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); let (mut app, _remote) = remote_exact_failover_fixture(); @@ -586,7 +920,9 @@ fn test_cancel_image_only_failover_restores_attachments() { #[test] fn test_pending_failover_honors_changed_manual_configuration() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let rt = tokio::runtime::Runtime::new().unwrap(); let _guard = rt.enter(); let (mut app, _remote) = remote_exact_failover_fixture(); @@ -809,6 +1145,7 @@ fn test_remote_fallback_resend_dropped_when_switch_fails() { app.is_remote = true; app.pending_fallback_resend = Some(crate::tui::app::FallbackResendPayload { + retry_attempts: 0, content: "hi".to_string(), images: vec![], is_system: false, diff --git a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs index ca5cda1dbe4..edd785beb1e 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs @@ -17,6 +17,7 @@ fn test_finish_turn_does_not_duplicate_existing_poke_followup() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs index 9b54fba5e83..b171f52f64a 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs @@ -1409,6 +1409,7 @@ fn test_create_transfer_session_from_parent_copies_todos_and_uses_compacted_cont &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -1597,6 +1598,7 @@ fn test_escape_interrupt_disables_auto_poke_while_processing() { .push(super::commands::build_poke_message(&[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_02.rs b/crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_02.rs index 7a58d71f110..f5fc13c6c8e 100644 --- a/crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_02.rs @@ -461,6 +461,7 @@ fn test_remote_escape_interrupt_disables_auto_poke_while_processing() { .push(super::commands::build_poke_message(&[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs b/crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs index e639beefa6e..6073007852a 100644 --- a/crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs +++ b/crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs @@ -1505,7 +1505,7 @@ fn test_click_on_swarm_expand_badge_toggles_tldr_collapse() { "collapsed card must show the tldr:\n{collapsed}" ); assert!( - collapsed.contains("▸ expand"), + collapsed.contains("⊞"), "collapsed card must show the expand badge:\n{collapsed}" ); assert!( @@ -1522,7 +1522,7 @@ fn test_click_on_swarm_expand_badge_toggles_tldr_collapse() { for col in 0..area.width { line.push_str(buf[(col, row)].symbol()); } - if let Some(byte) = line.find("▸ expand") { + if let Some(byte) = line.find("⊞") { let col = line[..byte].chars().count() as u16; badge = Some((col, row)); break 'rows; @@ -1566,7 +1566,7 @@ fn test_click_on_swarm_expand_badge_toggles_tldr_collapse() { "expanded card must show the body:\n{expanded}" ); assert!( - expanded.contains("▾ collapse"), + expanded.contains("⊟"), "expanded card must show the collapse badge:\n{expanded}" ); @@ -1579,7 +1579,7 @@ fn test_click_on_swarm_expand_badge_toggles_tldr_collapse() { for col in 0..area.width { line.push_str(buf[(col, row)].symbol()); } - if let Some(byte) = line.find("▾ collapse") { + if let Some(byte) = line.find("⊟") { let col = line[..byte].chars().count() as u16; collapse_badge = Some((col, row)); break 'rows2; diff --git a/crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs b/crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs index 98193342981..bb45ebf0d3b 100644 --- a/crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs @@ -1189,6 +1189,7 @@ fn test_context_command_reports_session_context_snapshot() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs b/crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs index 4b42e182ab3..217f77f3d47 100644 --- a/crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs +++ b/crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs @@ -13,6 +13,7 @@ fn test_open_work_rearms_auto_poke_after_completion_gate_breaker() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -63,6 +64,7 @@ fn test_cloud_handoff_defers_turn_end_auto_poke_without_disarming_it() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2341,6 +2343,7 @@ fn test_poke_arms_auto_poke_until_todos_are_done() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2380,6 +2383,7 @@ fn test_poke_status_reports_current_state() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2440,6 +2444,7 @@ fn test_poke_off_disarms_and_clears_queued_followup() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2495,6 +2500,7 @@ fn test_poke_queues_when_turn_is_in_progress() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2537,6 +2543,7 @@ fn test_poke_queues_when_turn_is_in_progress() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2555,6 +2562,7 @@ fn test_poke_queues_when_turn_is_in_progress() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2614,6 +2622,7 @@ fn test_finish_turn_auto_pokes_again_when_todos_remain() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2653,6 +2662,7 @@ fn test_finish_turn_auto_poke_queues_confidence_summary_when_todos_done() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2673,6 +2683,7 @@ fn test_finish_turn_auto_poke_queues_confidence_summary_when_todos_done() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2958,6 +2969,7 @@ fn test_finish_turn_without_auto_poke_does_not_queue_confidence_summary() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2999,6 +3011,7 @@ fn test_finish_turn_auto_poke_preserves_visible_turn_started() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/support_failover/part_01.rs b/crates/jcode-tui/src/tui/app/tests/support_failover/part_01.rs index 2e0a0529da8..a933617fb8c 100644 --- a/crates/jcode-tui/src/tui/app/tests/support_failover/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/support_failover/part_01.rs @@ -603,7 +603,9 @@ fn test_handle_turn_error_failover_prompt_manual_mode_shows_system_notice() { #[test] fn test_handle_turn_error_failover_prompt_countdown_can_switch_and_retry() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let (mut app, active_provider) = create_switchable_test_app("claude"); let prompt = crate::provider::ProviderFailoverPrompt { from_provider: "claude".to_string(), @@ -664,6 +666,49 @@ fn test_handle_turn_error_failover_prompt_countdown_can_switch_and_retry() { }); } +#[test] +fn test_definitive_exhaustion_immediate_switch_and_manual_safety() { + for (mode, enabled, reason, immediate) in [ + ("countdown", true, "OAuth usage exhausted", true), + ("countdown", false, "OAuth usage exhausted", false), + ("manual", true, "OAuth usage exhausted", false), + ( + "countdown", + true, + "429 rate_limit_error: This request would exceed your account's rate limit. Please try again later.", + false, + ), + ] { + with_temp_jcode_home(|| { + write_test_config(&format!( + "[provider]\ncross_provider_failover = \"{mode}\"\nexhausted_usage_immediate_failover = {enabled}\n" + )); + let (mut app, active) = create_switchable_test_app("claude"); + app.handle_turn_error(failover_error_message( + &crate::provider::ProviderFailoverPrompt { + from_provider: "claude".into(), + from_label: "Anthropic".into(), + to_provider: "openai".into(), + to_label: "OpenAI".into(), + target_route: Some("openai-oauth@openai-fox:gpt-5.4".into()), + reason: reason.into(), + estimated_input_chars: 400, + estimated_input_tokens: 100, + }, + )); + assert_eq!( + active.lock().unwrap().as_str(), + if immediate { "openai" } else { "claude" } + ); + assert_eq!(app.pending_turn, immediate); + assert_eq!( + app.pending_provider_failover.is_some(), + !immediate && mode == "countdown" + ); + }); + } +} + /// A new session must start with auto-poke off when the config says so, and /// `/poke on` must still be able to turn it back on for that session (#664). /// diff --git a/crates/jcode-tui/src/tui/app/tests/support_failover/part_02.rs b/crates/jcode-tui/src/tui/app/tests/support_failover/part_02.rs index c3eab92cd7d..306a6b437f4 100644 --- a/crates/jcode-tui/src/tui/app/tests/support_failover/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/support_failover/part_02.rs @@ -1,7 +1,9 @@ #[test] fn test_cancel_pending_provider_failover_clears_countdown() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let (mut app, _active_provider) = create_switchable_test_app("claude"); let prompt = crate::provider::ProviderFailoverPrompt { from_provider: "claude".to_string(), @@ -17,9 +19,11 @@ fn test_cancel_pending_provider_failover_clears_countdown() { app.handle_turn_error(failover_error_message(&prompt)); assert!(app.pending_provider_failover.is_some()); + app.pending_turn = true; app.cancel_pending_provider_failover("Provider auto-switch canceled"); assert!(app.pending_provider_failover.is_none()); + assert!(!app.pending_turn); let last = app.display_messages.last().expect("display message"); assert_eq!(last.role, "system"); assert!(last.content.contains("Auto-switch canceled")); @@ -35,7 +39,9 @@ struct FastMockProvider { #[test] fn test_local_failover_revalidates_pool_after_countdown() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let (mut app, active_provider) = create_switchable_test_app("claude"); let prompt = crate::provider::ProviderFailoverPrompt { from_provider: "claude".into(), @@ -54,6 +60,7 @@ fn test_local_failover_revalidates_pool_after_countdown() { pool.save().unwrap(); app.pending_provider_failover.as_mut().unwrap().deadline = Instant::now() - Duration::from_secs(1); + app.pending_turn = true; app.maybe_progress_provider_failover_countdown(); assert_eq!(active_provider.lock().unwrap().as_str(), "claude"); assert!(!app.pending_turn); @@ -61,6 +68,125 @@ fn test_local_failover_revalidates_pool_after_countdown() { }); } +fn failover_test_payload(raw_input: Option<&str>) -> FallbackResendPayload { + FallbackResendPayload { + retry_attempts: 0, + content: "expanded failed request".into(), + raw_input: raw_input.map(str::to_string), + images: Vec::new(), + is_system: false, + auto_retry: false, + system_reminder: None, + } +} + +#[test] +fn test_restore_failover_payload_keeps_existing_draft_and_raw_request() { + let mut app = create_test_app(); + app.input = "next request".into(); + app.cursor_pos = 3; + app.restore_failover_payload(failover_test_payload(Some("original @file request"))); + assert_eq!(app.input, "next request"); + assert_eq!(app.cursor_pos, 3); + let recovery = &app.display_messages.last().unwrap().content; + assert!(recovery.contains("Your draft is unchanged")); + assert!(recovery.contains("original @file request")); +} + +#[test] +fn test_restore_failover_payload_does_not_duplicate_matching_input() { + let mut app = create_test_app(); + app.input = "original request".into(); + app.restore_failover_payload(failover_test_payload(Some("original request"))); + assert_eq!(app.input, "original request"); +} + +#[test] +fn test_cancel_dispatched_failover_restores_payload_and_clears_inflight_state() { + let mut app = create_test_app(); + app.is_remote = true; + app.pending_automatic_failover_request_id = Some(42); + app.pending_automatic_failover_started = Some(Instant::now()); + app.remote_model_switch_in_flight = true; + app.pending_fallback_resend = Some(failover_test_payload(Some("held request"))); + app.cancel_pending_provider_failover("Provider auto-switch canceled"); + assert!(app.pending_automatic_failover_request_id.is_none()); + assert!(app.pending_automatic_failover_started.is_none()); + assert!(!app.remote_model_switch_in_flight); + assert!(app.pending_fallback_resend.is_none()); + assert_eq!(app.input, "held request"); +} + +#[test] +fn test_restore_failover_payload_preserves_images_without_duplicates() { + let mut app = create_test_app(); + let existing = ("image/png".into(), "existing-image".into()); + let failed = ("image/jpeg".into(), "failed-image".into()); + app.pending_images.push(existing.clone()); + let mut payload = failover_test_payload(None); + payload.images = vec![existing.clone(), failed.clone(), failed.clone()]; + app.restore_failover_payload(payload); + assert_eq!(app.input, "expanded failed request"); + assert_eq!(app.pending_images, vec![existing, failed]); +} + +#[test] +fn test_local_failed_fallback_switch_disarms_pending_turn() { + let mut app = create_failing_model_switch_test_app(); + assert!(app.offer_fallback_after_error("credentials expired")); + app.pending_turn = true; + assert!(app.apply_pending_fallback_offer()); + assert!(!app.pending_turn); + assert!(app.pending_fallback_offer.is_none()); + assert!( + app.display_messages + .last() + .unwrap() + .content + .contains("credentials expired") + ); +} + +#[test] +fn test_manual_failover_restores_remote_payload() { + with_temp_jcode_home(|| { + for mode in ["manual", "countdown"] { + assert_manual_failover_restores_remote_payload(mode); + } + }); +} + +fn assert_manual_failover_restores_remote_payload(mode: &str) { + write_test_config(&format!( + "[provider]\ncross_provider_failover = \"{mode}\"\n" + )); + let mut app = create_test_app(); + app.is_remote = true; + let prompt = crate::provider::ProviderFailoverPrompt { + from_provider: "claude".into(), + from_label: "Anthropic".into(), + to_provider: "openai".into(), + to_label: "OpenAI".into(), + target_route: None, + reason: "OAuth usage exhausted".into(), + estimated_input_chars: 0, + estimated_input_tokens: 0, + }; + app.rate_limit_pending_message = Some(PendingRemoteMessage { + content: "expanded failed request".into(), + images: Vec::new(), + is_system: false, + auto_retry: false, + system_reminder: None, + retry_at: None, + retry_attempts: 0, + }); + app.last_submitted_input = Some("original request".into()); + app.handle_turn_error(failover_error_message(&prompt)); + assert_eq!(app.input, "original request"); + assert!(app.pending_provider_failover.is_none()); +} + #[test] fn test_provider_failure_extracts_json_message_and_retains_full_diagnostics() { let mut app = create_test_app(); @@ -110,7 +236,9 @@ fn test_local_quota_failure_records_only_exhausted_account_cooldown() { #[test] fn test_failover_without_exact_target_is_manual_even_in_countdown_mode() { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = true\n", + ); let mut app = create_test_app(); app.handle_turn_error(failover_error_message( &crate::provider::ProviderFailoverPrompt { @@ -995,7 +1123,9 @@ fn test_local_failover_revalidates_runtime_source_pool_at_expiry() { ("openai", "openai:openai-otter", true), ] { with_temp_jcode_home(|| { - write_test_config("[provider]\ncross_provider_failover = \"countdown\"\n"); + write_test_config( + "[provider]\ncross_provider_failover = \"countdown\"\nexhausted_usage_immediate_failover = false\n", + ); let (mut app, active_provider) = create_switchable_test_app(initial_provider); // Deliberately stale metadata must not override runtime identity. app.session.route_api_method = Some("claude-oauth@stale-account".into()); diff --git a/crates/jcode-tui/src/tui/app/tests/todo_card.rs b/crates/jcode-tui/src/tui/app/tests/todo_card.rs index d28c1363c97..7207b8f6511 100644 --- a/crates/jcode-tui/src/tui/app/tests/todo_card.rs +++ b/crates/jcode-tui/src/tui/app/tests/todo_card.rs @@ -1,5 +1,107 @@ // Tests for the inline chat todo card (`/todos` command + todo-card hotkey). +#[test] +fn ssh_pinned_todos_arrive_before_history_and_survive_it() { + if super::commands_dispatch::ssh_test_runs_in_child( + "ssh_pinned_todos_arrive_before_history_and_survive_it", + ) { + return; + } + let _env_lock = crate::storage::lock_test_env(); + let _render_lock = crate::tui::ui::render_state_test_lock(); + let _pin = PinTodosEnvGuard::enable(); + let mut app = create_test_app(); + app.is_remote = true; + app.remote_session_id = None; + app.resume_session_id = Some("remote-early-todos".into()); + app.onboarding_flow = None; + app.onboarding_startup_checked = true; + crate::todo::save_todos( + "remote-early-todos", + &[pinned_band_todo( + "local", + "Unrelated local todo must not leak", + "pending", + )], + ) + .unwrap(); + app.refresh_pinned_todos_now(); + assert!(app.pinned_todos_payload_ref().is_none()); + let payload = serde_json::json!({ + "todos": [pinned_band_todo("remote", "Authoritative early remote task", "pending")], + "plan": {}, "goals": [] + }) + .to_string(); + app.receive_remote_todos("wrong-remote-session".into(), payload.clone()); + assert!(app.remote_todos_payload.is_none()); + let started = std::time::Instant::now(); + app.receive_remote_todos("remote-early-todos".into(), payload); + assert!( + app.remote_session_id.is_none(), + "todo snapshot must not claim the connection's session" + ); + let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap(); + let text = render_and_snap(&app, &mut terminal); + assert!(text.contains("Authoritative early remote task"), "{text}"); + assert!(!text.contains("Unrelated local todo"), "{text}"); + assert!(started.elapsed() < std::time::Duration::from_secs(1)); + eprintln!( + "authoritative pre-History todo rendered in {:?}", + started.elapsed() + ); + let runtime = tokio::runtime::Runtime::new().unwrap(); + let _runtime_guard = runtime.enter(); + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + let history = serde_json::from_value(serde_json::json!({ + "type": "history", "id": 1, "session_id": "remote-early-todos", + "messages": [], "mcp_servers": [], "skills": [], "all_sessions": [] + })) + .unwrap(); + app.handle_server_event(history, &mut remote); + assert!(app.remote_todos_payload.is_some()); + assert!( + app.pinned_todos_payload_ref() + .unwrap() + .contains("Authoritative early remote task") + ); + // An old payload must still be rejected after the session is established. + app.receive_remote_todos("wrong-remote-session".into(), "{}".into()); + assert!( + app.pinned_todos_payload_ref() + .unwrap() + .contains("Authoritative early remote task") + ); + crate::todo::save_todos("remote-early-todos", &[]).unwrap(); +} + +#[test] +fn resumed_local_snapshot_keeps_polling_so_card_does_not_go_stale() { + let mut app = create_test_app(); + app.is_remote = true; + app.remote_session_id = Some("resumed-local-snapshot".into()); + let payload = |status: &str| { + serde_json::json!({ + "todos": [{"id":"lane", "content":"Integrate lane", "priority":"high", "status":status}], + "plan": {}, "goals": [] + }) + .to_string() + }; + // Resume seeds an authoritative snapshot, after which disk is ignored. + app.receive_remote_todos("resumed-local-snapshot".into(), payload("in_progress")); + app.toggle_todo_card(); + assert!( + app.remote_todos_refresh_due(), + "a snapshot-backed card must request fresh todos, not freeze" + ); + app.receive_remote_todos("resumed-local-snapshot".into(), payload("completed")); + let card = app + .display_messages + .iter() + .find(|m| m.role == "todos") + .unwrap(); + assert!(card.content.contains("completed"), "{}", card.content); +} + #[test] fn ssh_todos_command_renders_remote_card() { if super::commands_dispatch::ssh_test_runs_in_child("ssh_todos_command_renders_remote_card") { @@ -164,6 +266,7 @@ fn refresh_todo_card_updates_content_when_todos_change() { let todo = |content: &str, status: &str| crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "t1".to_string(), content: content.to_string(), status: status.to_string(), @@ -214,6 +317,7 @@ fn refresh_todo_card_updates_content_when_goal_scores_change() { let session_id = app.session.id.clone(); let todos = [crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "t1".to_string(), content: "render scores".to_string(), status: "in_progress".to_string(), @@ -277,6 +381,7 @@ fn refresh_todo_card_updates_content_when_goal_scores_change() { fn pinned_band_todo(id: &str, content: &str, status: &str) -> crate::todo::TodoItem { crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: id.to_string(), content: content.to_string(), status: status.to_string(), @@ -340,6 +445,113 @@ fn pinned_todos_payload_stays_empty_when_config_off() { let _ = crate::todo::save_todos(&session_id, &[]); } +#[test] +fn pinned_todos_render_before_resumed_server_history() { + let _env_lock = crate::storage::lock_test_env(); + let _pin = PinTodosEnvGuard::enable(); + let mut app = create_test_app(); + let session_id = app.session.id.clone(); + crate::todo::save_todos( + &session_id, + &[pinned_band_todo( + "resume-first-frame", + "Resumed pinned task", + "pending", + )], + ) + .unwrap(); + let startup_started = std::time::Instant::now(); + let resumed = App::new_for_remote(Some(session_id.clone())); + assert!( + resumed + .pinned_todos_payload_ref() + .expect("startup constructor seeds pinned todos without server History") + .contains("Resumed pinned task") + ); + eprintln!( + "resume pinned todos ready in {:?} without server History", + startup_started.elapsed() + ); + app.is_remote = true; + app.remote_session_id = None; + app.resume_session_id = Some(session_id.clone()); + app.onboarding_flow = None; + app.onboarding_startup_checked = true; + app.push_display_message(crate::tui::DisplayMessage::system("Resumed transcript")); + + // This is the startup seed, before any server History or polling tick. + app.refresh_pinned_todos_now(); + assert!( + app.pinned_todos_payload_ref() + .unwrap() + .contains("Resumed pinned task") + ); + let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap(); + terminal + .draw(|frame| crate::tui::ui::draw(frame, &app)) + .unwrap(); + let text: String = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect(); + assert!(text.contains("Resumed pinned task"), "{text}"); + assert!( + !app.refresh_pinned_todos_if_needed(), + "unchanged state stays throttled" + ); + crate::todo::save_todos(&session_id, &[]).unwrap(); +} + +#[test] +fn pinned_todos_session_switch_bypasses_previous_snapshot_and_throttle() { + let _env_lock = crate::storage::lock_test_env(); + let _pin = PinTodosEnvGuard::enable(); + let mut app = create_test_app(); + let session_id = app.session.id.clone(); + crate::todo::save_todos( + &session_id, + &[pinned_band_todo("new-task", "New session task", "pending")], + ) + .unwrap(); + app.is_remote = true; + app.remote_session_id = Some("previous-todo-session".into()); + app.receive_remote_todos( + "previous-todo-session".into(), + serde_json::json!({ + "todos": [pinned_band_todo("old-task", "Previous session task", "pending")], + "plan": {}, "goals": [] + }) + .to_string(), + ); + app.remote_todos_requested_at = Some(std::time::Instant::now()); + assert!( + app.pinned_todos_payload_ref() + .unwrap() + .contains("Previous session task") + ); + + let runtime = tokio::runtime::Runtime::new().unwrap(); + let _runtime_guard = runtime.enter(); + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + let history = serde_json::from_value(serde_json::json!({ + "type": "history", "id": 1, "session_id": session_id, + "messages": [], "mcp_servers": [], "skills": [], "all_sessions": [] + })) + .unwrap(); + app.handle_server_event(history, &mut remote); + assert!(app.remote_todos_payload.is_none()); + assert!(app.remote_todos_requested_at.is_none()); + let payload = app + .pinned_todos_payload_ref() + .expect("new session todos loaded during History"); + assert!(payload.contains("New session task")); + assert!(!payload.contains("Previous session task")); + crate::todo::save_todos(&session_id, &[]).unwrap(); +} + #[test] fn pinned_todos_payload_refreshes_and_clears_with_config_and_todos() { let _env_lock = crate::storage::lock_test_env(); @@ -952,3 +1164,26 @@ fn debug_inject_todos_preserves_role_and_renders_fixture() { "{text}" ); } + +#[test] +fn expanded_pinned_todo_band_scrolls_when_taller_than_budget() { + let _env_lock = crate::storage::lock_test_env(); + let _render_lock = crate::tui::ui::render_state_test_lock(); + let _pin = PinTodosEnvGuard::enable(); + let mut app = create_test_app(); + app.pinned_todos_expanded = true; + let todos: Vec<_> = (0..40) + .map(|i| pinned_band_todo(&format!("t{i}"), &format!("Task number {i}"), "pending")) + .collect(); + app.pinned_todos_payload = Some(serde_json::json!({ "todos": todos }).to_string()); + crate::tui::ui::viewport::reset_pinned_todo_scroll(); + + let (lines, _) = crate::tui::ui::viewport::pinned_todo_band_lines(&app, 80, 30); + assert!(lines.len() <= 21, "expanded band is bounded: {}", lines.len()); + assert!(lines[0].to_string().contains("scroll")); + crate::tui::ui::viewport::publish_pinned_todo_scroll_for_test(); + assert!(crate::tui::ui::viewport::scroll_pinned_todos(3)); + let (scrolled, _) = crate::tui::ui::viewport::pinned_todo_band_lines(&app, 80, 30); + assert_ne!(lines[1].to_string(), scrolled[1].to_string()); + crate::tui::ui::viewport::reset_pinned_todo_scroll(); +} diff --git a/crates/jcode-tui/src/tui/app/todos_view.rs b/crates/jcode-tui/src/tui/app/todos_view.rs index 90474882cc3..6e78b103747 100644 --- a/crates/jcode-tui/src/tui/app/todos_view.rs +++ b/crates/jcode-tui/src/tui/app/todos_view.rs @@ -20,7 +20,11 @@ impl App { crate::todo::TodoPlan, Vec, ) { - let session_id = self.active_client_session_id(); + // A session-bound remote snapshot can arrive before History. SSH still + // never reads local todo files, including for the requested resume id. + let session_id = self + .active_client_session_id() + .or(self.resume_session_id.as_deref()); if crate::tui::is_ssh_remote() || self.remote_todos_payload.is_some() { let payload = self .remote_todos_payload @@ -61,13 +65,17 @@ impl App { } pub(super) fn remote_todos_refresh_due(&mut self) -> bool { - if !crate::tui::is_ssh_remote() + // Any client that renders from a server snapshot must keep polling it. + // A local resume seeds `remote_todos_payload` before History, and from + // then on `current_todo_state` ignores disk. Without polling, the card + // froze at the resume snapshot while the stored plan kept advancing. + if !(crate::tui::is_ssh_remote() || self.remote_todos_payload.is_some()) || !(self.todos_view_enabled || self.latest_todo_card_index().is_some() || crate::config::config().display.pin_todos) || self .remote_todos_requested_at - .is_some_and(|at| at.elapsed() < std::time::Duration::from_secs(2)) + .is_some_and(|at| at.elapsed() < self.remote_todos_poll_interval()) { return false; } @@ -75,8 +83,29 @@ impl App { true } + /// Todos only change while a turn runs (the todo tool) or through an + /// explicit command, both of which clear `remote_todos_requested_at` for an + /// immediate refresh. An idle session therefore needs only a slow safety + /// poll: a 2s poll from every open window kept the shared server and each + /// client waking for nothing. + fn remote_todos_poll_interval(&self) -> std::time::Duration { + if self.is_processing() { + std::time::Duration::from_secs(2) + } else { + std::time::Duration::from_secs(30) + } + } + + /// Ask for a fresh remote todo snapshot on the next tick. + pub(super) fn request_remote_todos_refresh_soon(&mut self) { + self.remote_todos_requested_at = None; + } + pub(super) fn receive_remote_todos(&mut self, session_id: String, payload: String) { - if self.active_client_session_id() != Some(session_id.as_str()) { + let expected = self + .active_client_session_id() + .or(self.resume_session_id.as_deref()); + if expected != Some(session_id.as_str()) { return; } self.remote_todos_payload = Some((session_id, payload)); @@ -942,6 +971,7 @@ mod tests { priority: priority.to_string(), group: None, lane_after: None, + lane_owner: None, confidence: confidence.map(crate::todo::ConfidenceState::from_legacy_score), completion_confidence: completion_confidence .map(crate::todo::ConfidenceState::from_legacy_score), diff --git a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs index aecf6f923c2..a1d655cd23a 100644 --- a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs +++ b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs @@ -350,6 +350,7 @@ impl App { display_edit_line_counts: (0, 0), terminal_title: RefCell::new(terminal_title::TerminalTitleState::default()), compacted_history_lazy: CompactedHistoryLazyState::default(), + recent_history_paging: Default::default(), pending_history_anchor: None, pending_resize_anchor: None, input: String::new(), @@ -390,6 +391,7 @@ impl App { remote_interrupt_ack_deadline: None, pending_reload_reconnect_status: None, scheduled_reload_recoveries: HashSet::new(), + reload_recovery_dispatch_pending: false, status: ProcessingStatus::default(), subagent_status: None, batch_progress: None, @@ -542,6 +544,7 @@ impl App { side_pane_images_signature_cache: std::cell::Cell::new(None), remote_swarm_members: Vec::new(), remote_swarm_members_received_at: None, + swarm_last_message_at: std::collections::HashMap::new(), swarm_plan_items: Vec::new(), swarm_plan_version: None, swarm_plan_swarm_id: None, @@ -637,6 +640,7 @@ impl App { pending_route_selection: None, pending_automatic_failover_route: None, pending_automatic_failover_request_id: None, + pending_automatic_failover_started: None, pending_reasoning_effort: None, remote_model_switch_in_flight: false, pending_prompt_after_model_switch: None, @@ -656,6 +660,7 @@ impl App { voice_input_key: keybind::load_voice_input_key(), voice_input: None, voice_input_last_press: None, + external_voice: None, fallback_switch_key: keybind::load_fallback_switch_key(), scroll_keys: keybind::load_scroll_keys(), keybindings_config_generation: crate::config::config_reload_generation(), @@ -686,6 +691,8 @@ impl App { interleave_images: Vec::new(), pending_soft_interrupts: Vec::new(), pending_soft_interrupt_requests: Vec::new(), + sent_soft_interrupts: Vec::new(), + soft_interrupt_recovery: None, autoreview_after_current_turn: false, autojudge_after_current_turn: false, pending_split_startup_message: None, @@ -857,6 +864,7 @@ impl App { display_edit_line_counts: (0, 0), terminal_title: RefCell::new(terminal_title::TerminalTitleState::default()), compacted_history_lazy: CompactedHistoryLazyState::default(), + recent_history_paging: Default::default(), pending_history_anchor: None, pending_resize_anchor: None, input: String::new(), @@ -897,6 +905,7 @@ impl App { remote_interrupt_ack_deadline: None, pending_reload_reconnect_status: None, scheduled_reload_recoveries: HashSet::new(), + reload_recovery_dispatch_pending: false, status: ProcessingStatus::default(), subagent_status: None, batch_progress: None, @@ -1049,6 +1058,7 @@ impl App { side_pane_images_signature_cache: std::cell::Cell::new(None), remote_swarm_members: Vec::new(), remote_swarm_members_received_at: None, + swarm_last_message_at: std::collections::HashMap::new(), swarm_plan_items: Vec::new(), swarm_plan_version: None, swarm_plan_swarm_id: None, @@ -1144,6 +1154,7 @@ impl App { pending_route_selection: None, pending_automatic_failover_route: None, pending_automatic_failover_request_id: None, + pending_automatic_failover_started: None, pending_reasoning_effort: None, remote_model_switch_in_flight: false, pending_prompt_after_model_switch: None, @@ -1163,6 +1174,7 @@ impl App { voice_input_key: keybind::load_voice_input_key(), voice_input: None, voice_input_last_press: None, + external_voice: None, fallback_switch_key: keybind::load_fallback_switch_key(), scroll_keys: keybind::load_scroll_keys(), keybindings_config_generation: crate::config::config_reload_generation(), @@ -1193,6 +1205,8 @@ impl App { interleave_images: Vec::new(), pending_soft_interrupts: Vec::new(), pending_soft_interrupt_requests: Vec::new(), + sent_soft_interrupts: Vec::new(), + soft_interrupt_recovery: None, autoreview_after_current_turn: false, autojudge_after_current_turn: false, pending_split_startup_message: None, @@ -1644,6 +1658,9 @@ impl App { } app.resume_session_id = resume_session; + // Paint persisted todos with the first frame, not a throttled tick after + // the server has finished loading the resumed conversation. + app.refresh_pinned_todos_now(); // After `resume_session_id`: the hint needs to know whether this is a // new session (configured effort applies) or a resumed one. app.apply_remote_header_hint(); diff --git a/crates/jcode-tui/src/tui/app/tui_lifecycle_reload_input.rs b/crates/jcode-tui/src/tui/app/tui_lifecycle_reload_input.rs index 24385e4aed2..6a6f38aa90e 100644 --- a/crates/jcode-tui/src/tui/app/tui_lifecycle_reload_input.rs +++ b/crates/jcode-tui/src/tui/app/tui_lifecycle_reload_input.rs @@ -1,6 +1,12 @@ use super::*; impl App { + /// A pending reload-recovery continuation must run as its own turn before + /// user follow-ups queued ahead of the reload are sent. + pub(in crate::tui::app) fn reload_recovery_blocks_queue(&self) -> bool { + self.reload_recovery_dispatch_pending && !self.hidden_queued_system_messages.is_empty() + } + pub(in crate::tui::app) fn apply_restored_reload_input( &mut self, restored: RestoredReloadInput, diff --git a/crates/jcode-tui/src/tui/app/tui_lifecycle_runtime.rs b/crates/jcode-tui/src/tui/app/tui_lifecycle_runtime.rs index 50698afcfde..9afc95972c7 100644 --- a/crates/jcode-tui/src/tui/app/tui_lifecycle_runtime.rs +++ b/crates/jcode-tui/src/tui/app/tui_lifecycle_runtime.rs @@ -206,6 +206,49 @@ impl App { .is_some_and(|mtime| mtime > startup_mtime) } + /// Event-driven hot reload for every client kind (local, attached, `--ssh`). + /// Triggered by `binary_watch` when a build is published. Waits for an idle + /// moment (no turn in flight, not mid-typing) and re-execs onto the newer + /// binary with input preserved. Deferred attempts re-arm and retry on tick. + pub(super) fn maybe_hot_reload_onto_new_binary(&mut self) -> bool { + super::binary_watch::ensure_started(); + if !super::binary_watch::take_pending() { + return false; + } + if self.is_replay_runtime() || self.reload_requested.is_some() || self.should_quit { + return false; + } + const TYPING_QUIET: std::time::Duration = std::time::Duration::from_millis(1200); + let typing = self + .last_user_interaction + .is_some_and(|at| at.elapsed() < TYPING_QUIET); + if self.is_processing || typing { + super::binary_watch::rearm(); + return false; + } + if !self.has_newer_binary() { + return false; + } + let session_id = if self.is_remote { + self.reload_handoff_session_id() + } else { + self.session.provider_session_id = self.provider_session_id.clone(); + self.session + .set_status(crate::session::SessionStatus::Reloaded); + if let Err(error) = self.session.save() { + crate::logging::warn(&format!( + "Failed to save session during TUI lifecycle: {error}" + )); + } + self.session.id.clone() + }; + self.append_reload_message("Newer jcode build published; hot reloading client..."); + self.save_input_for_reload(&session_id); + self.reload_requested = Some(session_id); + self.should_quit = true; + true + } + /// After an in-process server reload (e.g. `self-dev build-reload`), the /// server PID is unchanged and connected clients never disconnect, so they /// keep running their old binary and client-side changes never take effect. @@ -213,7 +256,7 @@ impl App { /// client is idle, re-exec onto the new binary so TUI-side changes apply too. /// Returns true when a client reload was requested. pub(super) fn maybe_self_reload_after_server_reload(&mut self) -> bool { - if !self.is_remote || crate::tui::is_ssh_remote() { + if !self.is_remote { return false; } let is_selfdev_session = self.remote_is_canary.unwrap_or(self.session.is_canary); diff --git a/crates/jcode-tui/src/tui/app/tui_state.rs b/crates/jcode-tui/src/tui/app/tui_state.rs index 43e9acefe6b..7543f3acd3a 100644 --- a/crates/jcode-tui/src/tui/app/tui_state.rs +++ b/crates/jcode-tui/src/tui/app/tui_state.rs @@ -1079,6 +1079,10 @@ impl crate::tui::TuiState for App { self.mouse_scroll_queue != 0 } + fn stream_reveal_pending(&self) -> bool { + !self.stream_buffer.is_empty() + } + fn total_session_tokens(&self) -> Option<(u64, u64)> { // In remote mode, use tokens from server // Independent mode doesn't currently track total tokens @@ -1187,6 +1191,10 @@ impl crate::tui::TuiState for App { self.voice_input_status_line() } + fn voice_composer_preview(&self) -> Option { + self.voice_composer_preview_state() + } + fn reset_hint_visible(&self, key: &str) -> bool { let mut seen = self.reset_hint_seen.borrow_mut(); let now = Instant::now(); @@ -2468,7 +2476,7 @@ impl App { let mut opened = 0; let mut failures = Vec::new(); for session_id in order { - match open_swarm_agent_window(&session_id) { + match open_swarm_agent_window(&session_id, crate::tui::is_ssh_remote()) { Ok(true) => opened += 1, Ok(false) => failures.push(format!("{session_id}: no terminal emulator found")), Err(error) => failures.push(format!("{session_id}: {error}")), @@ -2495,6 +2503,20 @@ impl App { /// Open the currently selected swarm agent's session in a new terminal /// window (pop-out), reusing the resume-in-new-terminal launcher. pub(crate) fn pop_out_selected_swarm_agent(&mut self) { + self.pop_out_selected_swarm_agent_with_launcher(|exe, session_id, cwd, context| { + spawn_swarm_agent_window(exe, session_id, cwd, context, crate::tui::is_ssh_remote()) + }); + } + + fn pop_out_selected_swarm_agent_with_launcher( + &mut self, + launcher: impl FnMut( + &std::path::Path, + &str, + &std::path::Path, + &jcode_app_core::session_launch::SessionSpawnContext, + ) -> anyhow::Result, + ) { let members = self.inline_swarm_members(); if members.is_empty() { self.set_status_notice("No swarm agents to open"); @@ -2512,7 +2534,11 @@ impl App { .and_then(|m| m.friendly_name.clone()) .unwrap_or_else(|| session_id.chars().take(8).collect()); - match open_swarm_agent_window(&session_id) { + match open_swarm_agent_window_with_launcher( + &session_id, + crate::tui::is_ssh_remote(), + launcher, + ) { Ok(true) => self.set_status_notice(format!("Opened {label} in a new window")), Ok(false) => self.set_status_notice(format!( "Could not open a terminal for {label} (no emulator found)" @@ -2522,7 +2548,26 @@ impl App { } } -fn open_swarm_agent_window(session_id: &str) -> anyhow::Result { +fn open_swarm_agent_window(session_id: &str, is_ssh_remote: bool) -> anyhow::Result { + open_swarm_agent_window_with_launcher( + session_id, + is_ssh_remote, + |exe, session_id, cwd, context| { + spawn_swarm_agent_window(exe, session_id, cwd, context, is_ssh_remote) + }, + ) +} + +fn open_swarm_agent_window_with_launcher( + session_id: &str, + is_ssh_remote: bool, + mut launcher: impl FnMut( + &std::path::Path, + &str, + &std::path::Path, + &jcode_app_core::session_launch::SessionSpawnContext, + ) -> anyhow::Result, +) -> anyhow::Result { let exe = super::launch_client_executable(); let cwd = crate::session::Session::load_startup_stub(session_id) .ok() @@ -2530,8 +2575,81 @@ fn open_swarm_agent_window(session_id: &str) -> anyhow::Result { .map(std::path::PathBuf::from) .filter(|path| path.is_dir()) .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| ".".into())); - let socket = std::env::var("JCODE_SOCKET").ok(); - super::spawn_in_new_terminal(&exe, session_id, &cwd, socket.as_deref()) + let context = swarm_popout_launch_context(is_ssh_remote, &crate::server::socket_path()); + launcher(&exe, session_id, &cwd, &context) +} + +fn spawn_swarm_agent_window( + exe: &std::path::Path, + session_id: &str, + cwd: &std::path::Path, + context: &jcode_app_core::session_launch::SessionSpawnContext, + is_ssh_remote: bool, +) -> anyhow::Result { + spawn_swarm_agent_window_with_launchers( + exe, + session_id, + cwd, + context, + is_ssh_remote, + jcode_app_core::terminal_launch::spawn_command_in_new_terminal, + |exe, session_id, cwd| { + let socket = std::env::var("JCODE_SOCKET").ok(); + super::spawn_in_new_terminal(exe, session_id, cwd, socket.as_deref()) + }, + ) +} + +fn swarm_agent_terminal_command( + exe: &std::path::Path, + session_id: &str, + context: &jcode_app_core::session_launch::SessionSpawnContext, +) -> jcode_app_core::terminal_launch::TerminalCommand { + let title = jcode_app_core::session_launch::resumed_window_title(session_id); + let mut args = vec!["--fresh-spawn".into(), "--resume".into(), session_id.into()]; + if let Some(socket) = context + .runtime_env + .iter() + .find(|(key, _)| key == "JCODE_SOCKET") + .map(|(_, value)| value) + { + args.extend(["--socket".into(), socket.clone()]); + } + let mut command = jcode_app_core::terminal_launch::TerminalCommand::new(exe, args) + .title(title) + .fresh_spawn() + .kind("resume") + .session_id(session_id); + for (key, value) in &context.extra_env { + command = command.spawn_env(key.clone(), value.clone()); + } + if !context.runtime_env.is_empty() { + command = command.runtime_env(context.runtime_env.clone()); + } + if !context.client_terminal_env.is_empty() { + command = command.client_terminal_env(context.client_terminal_env.clone()); + } + command +} + +fn spawn_swarm_agent_window_with_launchers( + exe: &std::path::Path, + session_id: &str, + cwd: &std::path::Path, + context: &jcode_app_core::session_launch::SessionSpawnContext, + is_ssh_remote: bool, + mut local_launcher: impl FnMut( + &jcode_app_core::terminal_launch::TerminalCommand, + &std::path::Path, + ) -> anyhow::Result, + mut ssh_launcher: impl FnMut(&std::path::Path, &str, &std::path::Path) -> anyhow::Result, +) -> anyhow::Result { + if is_ssh_remote { + ssh_launcher(exe, session_id, cwd) + } else { + let command = swarm_agent_terminal_command(exe, session_id, context); + local_launcher(&command, cwd) + } } /// Decode direct agent badges without capturing plain digits or other chords. @@ -2550,6 +2668,24 @@ pub(crate) fn swarm_agent_shortcut_index( } } +/// Runtime context for opening a swarm agent's window: local daemon clients +/// carry their effective runtime so the new terminal connects back to the same +/// one; SSH clients keep the historical context-free behavior. +fn swarm_popout_launch_context( + is_ssh_remote: bool, + socket: &std::path::Path, +) -> jcode_app_core::session_launch::SessionSpawnContext { + if is_ssh_remote { + jcode_app_core::session_launch::SessionSpawnContext::default() + } else { + jcode_app_core::session_launch::SessionSpawnContext::default() + .with_local_runtime(socket) + .with_client_terminal_env( + jcode_app_core::terminal_launch::snapshot_client_terminal_env(), + ) + } +} + /// What a key press should do while the swarm panel is focused. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum SwarmPanelAction { diff --git a/crates/jcode-tui/src/tui/app/tui_state_sidebar_usage_tests.rs b/crates/jcode-tui/src/tui/app/tui_state_sidebar_usage_tests.rs index c62d11f25d9..c89ff488fd7 100644 --- a/crates/jcode-tui/src/tui/app/tui_state_sidebar_usage_tests.rs +++ b/crates/jcode-tui/src/tui/app/tui_state_sidebar_usage_tests.rs @@ -24,6 +24,7 @@ fn startup_usage_slot_waits_for_account_binding_even_with_cached_quotas() { fn snapshot() -> AccountUsageSnapshot { AccountUsageSnapshot { + additional_windows: Vec::new(), label: "solosystems".into(), email: None, exhausted: false, diff --git a/crates/jcode-tui/src/tui/app/turn_notify.rs b/crates/jcode-tui/src/tui/app/turn_notify.rs index 36ba3fb8acc..1650abd0112 100644 --- a/crates/jcode-tui/src/tui/app/turn_notify.rs +++ b/crates/jcode-tui/src/tui/app/turn_notify.rs @@ -417,6 +417,7 @@ mod tests { fn todo_named(content: &str, status: &str, blocked_by: &[&str]) -> TodoItem { TodoItem { lane_after: None, + lane_owner: None, content: content.to_string(), status: status.to_string(), priority: "medium".to_string(), diff --git a/crates/jcode-tui/src/tui/app/voice_input.rs b/crates/jcode-tui/src/tui/app/voice_input.rs index 5ae34f3af97..320e6f8dab0 100644 --- a/crates/jcode-tui/src/tui/app/voice_input.rs +++ b/crates/jcode-tui/src/tui/app/voice_input.rs @@ -22,6 +22,9 @@ 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; +/// Level history kept for the composer waveform, which is wider than the +/// status-line meter. +const LEVEL_HISTORY: usize = 64; const LIVE_TAIL_CHARS: usize = 56; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -33,7 +36,7 @@ pub(crate) enum VoicePhase { pub(crate) struct VoiceInput { recording: NariRecording, waker_done: Arc, - phase: VoicePhase, + pub(super) phase: VoicePhase, started: Instant, pressed_at: Instant, live: String, @@ -182,7 +185,7 @@ impl App { started: now, pressed_at: now, live: String::new(), - levels: VecDeque::with_capacity(METER_BARS), + levels: VecDeque::with_capacity(LEVEL_HISTORY), }); } @@ -224,7 +227,7 @@ impl App { }; let mut changed = false; if voice.phase == VoicePhase::Recording { - if voice.levels.len() == METER_BARS { + if voice.levels.len() == LEVEL_HISTORY { voice.levels.pop_front(); } voice.levels.push_back(voice.recording.audio_level()); @@ -271,15 +274,79 @@ impl App { } } + /// Record push-to-talk state reported by `jcode companion`. + pub(crate) fn apply_external_voice_activity( + &mut self, + activity: crate::protocol::VoiceActivity, + ) { + self.external_voice = match activity { + crate::protocol::VoiceActivity::Idle => None, + activity => Some((activity, Instant::now())), + }; + } + + /// Companion push-to-talk status, unless stale or turned off. + fn external_voice_status_line(&self) -> Option<(bool, String)> { + if !crate::config::config().companion.voice_waveform { + return None; + } + let (activity, updated) = self.external_voice.as_ref()?; + external_voice_line(activity, updated.elapsed()) + } + /// Status-line text while voice input is active: `(recording, text)`. + /// Empty when the composer shows it instead. pub(crate) fn voice_input_status_line(&self) -> Option<(bool, String)> { - let voice = self.voice_input.as_ref()?; + if crate::config::config().dictation.composer_preview { + return None; + } + self.voice_status_line_text() + } + + /// Live voice state for the composer, built-in recording first, then + /// companion push-to-talk. + pub(crate) fn voice_composer_preview_state(&self) -> Option { + if !crate::config::config().dictation.composer_preview { + return None; + } + if let Some(voice) = self.voice_input.as_ref() { + let key = self.voice_input_key_label().unwrap_or("/voice"); + let recording = voice.phase == VoicePhase::Recording; + return Some(crate::tui::VoiceComposerPreview { + recording, + elapsed_secs: voice.started.elapsed().as_secs(), + levels: voice.levels.iter().copied().collect(), + live: voice.live.trim().to_string(), + hint: if recording { + format!("{key} send · Esc cancel") + } else { + "Esc discard".to_string() + }, + }); + } + if !crate::config::config().companion.voice_waveform { + return None; + } + let (activity, updated) = self.external_voice.as_ref()?; + external_voice_preview(activity, updated.elapsed()) + } + + fn voice_status_line_text(&self) -> Option<(bool, String)> { + let Some(voice) = self.voice_input.as_ref() else { + return self.external_voice_status_line(); + }; 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 start = voice.levels.len().saturating_sub(METER_BARS); + let meter: String = voice + .levels + .iter() + .skip(start) + .map(|level| meter_bar(*level)) + .collect(); let heard = if live.is_empty() { "listening…".to_string() } else { @@ -306,6 +373,89 @@ impl App { } } +/// Updates arrive every ~80 ms while recording. Past this, the sender is gone. +const EXTERNAL_VOICE_STALE: Duration = Duration::from_secs(3); +/// Transcribing can legitimately take a while with no updates. +const EXTERNAL_TRANSCRIBING_STALE: Duration = Duration::from_secs(60); + +fn external_voice_preview( + activity: &crate::protocol::VoiceActivity, + age: Duration, +) -> Option { + use crate::protocol::VoiceActivity; + match activity { + VoiceActivity::Recording { + elapsed_ms, + levels, + live, + } if age <= EXTERNAL_VOICE_STALE => Some(crate::tui::VoiceComposerPreview { + recording: true, + elapsed_secs: elapsed_ms / 1000, + levels: levels.clone(), + live: live.trim().to_string(), + hint: "release to send".to_string(), + }), + VoiceActivity::Transcribing { live } if age <= EXTERNAL_TRANSCRIBING_STALE => { + Some(crate::tui::VoiceComposerPreview { + recording: false, + elapsed_secs: 0, + levels: Vec::new(), + live: live.trim().to_string(), + hint: String::new(), + }) + } + _ => None, + } +} + +fn external_voice_line( + activity: &crate::protocol::VoiceActivity, + age: Duration, +) -> Option<(bool, String)> { + use crate::protocol::VoiceActivity; + match activity { + VoiceActivity::Recording { + elapsed_ms, + levels, + live, + } => { + if age > EXTERNAL_VOICE_STALE { + return None; + } + let secs = elapsed_ms / 1000; + let start = levels.len().saturating_sub(METER_BARS); + let meter: String = levels[start..].iter().map(|l| meter_bar(*l)).collect(); + let live = live_tail(live); + let heard = if live.is_empty() { + "listening…".to_string() + } else { + live + }; + Some(( + true, + format!( + "● {}:{:02} {meter} {heard} · release to send", + secs / 60, + secs % 60 + ), + )) + } + VoiceActivity::Transcribing { live } => { + if age > EXTERNAL_TRANSCRIBING_STALE { + return None; + } + let live = live_tail(live); + let heard = if live.is_empty() { + String::new() + } else { + format!(" {live}") + }; + Some((false, format!("◌ Transcribing…{heard}"))) + } + VoiceActivity::Idle => None, + } +} + fn drain_voice_events( voice: &mut VoiceInput, changed: &mut bool, @@ -373,6 +523,30 @@ mod tests { assert!(speech != '▁' && speech != '█', "{speech}"); } + #[test] + fn external_voice_shows_meter_and_expires() { + use crate::protocol::VoiceActivity; + let recording = VoiceActivity::Recording { + elapsed_ms: 65_000, + levels: vec![0.0; 14].into_iter().chain([1.0]).collect(), + live: "hello there".into(), + }; + let (rec, line) = external_voice_line(&recording, Duration::ZERO).unwrap(); + assert!(rec); + assert!(line.starts_with("● 1:05 "), "{line}"); + assert_eq!( + line.chars().filter(|c| "▁▂▃▄▅▆▇█".contains(*c)).count(), + METER_BARS + ); + assert!(line.contains('█') && line.contains("hello there")); + assert!(external_voice_line(&recording, Duration::from_secs(4)).is_none()); + let transcribing = VoiceActivity::Transcribing { + live: String::new(), + }; + assert!(external_voice_line(&transcribing, Duration::from_secs(4)).is_some()); + assert!(external_voice_line(&VoiceActivity::Idle, Duration::ZERO).is_none()); + } + #[test] fn live_tail_keeps_the_most_recent_words() { assert_eq!(live_tail(" short "), "short"); diff --git a/crates/jcode-tui/src/tui/app/wake_stats.rs b/crates/jcode-tui/src/tui/app/wake_stats.rs new file mode 100644 index 00000000000..703958f9a72 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/wake_stats.rs @@ -0,0 +1,77 @@ +//! Counts why the client event loop woke up, so idle wakeups can be traced to +//! their source (`client:draw-stats` reports them as `wake_sources`). +use std::collections::BTreeMap; +use std::sync::Mutex; + +static COUNTS: Mutex> = Mutex::new(BTreeMap::new()); + +pub(crate) fn note(source: &str) { + if let Ok(mut counts) = COUNTS.lock() { + if let Some(n) = counts.get_mut(source) { + *n += 1; + } else { + counts.insert(source.to_string(), 1); + } + } +} + +/// Variant name of a Debug-printable enum value. Formatting stops at the first +/// non-identifier character, so large payloads (History) cost nothing extra. +pub(crate) fn note_debug(prefix: &str, value: &T) { + struct Name(String, bool); + impl std::fmt::Write for Name { + fn write_str(&mut self, s: &str) -> std::fmt::Result { + for c in s.chars() { + if !(c.is_alphanumeric() || c == '_') || self.0.len() >= 48 { + self.1 = true; + return Err(std::fmt::Error); + } + self.0.push(c); + } + Ok(()) + } + } + let mut name = Name(String::new(), false); + // Our writer uses an error to stop after the variant name. Other formatter + // failures are unexpected and should remain visible in diagnostics. + if std::fmt::write(&mut name, format_args!("{value:?}")).is_err() && !name.1 { + crate::logging::debug("Failed to format wake source name"); + } + note(&format!("{prefix}:{}", name.0)); +} + +pub(crate) fn snapshot() -> serde_json::Value { + let counts = match COUNTS.lock() { + Ok(counts) => counts.clone(), + Err(error) => { + crate::logging::warn(&format!("Wake counters lock poisoned: {error}")); + BTreeMap::new() + } + }; + match serde_json::to_value(counts) { + Ok(value) => value, + Err(error) => { + crate::logging::warn(&format!("Failed to serialize wake counters: {error}")); + serde_json::Value::Null + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn debug_counter_stops_at_variant_name() { + super::note_debug("wake-test", &Some("payload")); + assert!( + super::snapshot()["wake-test:Some"] + .as_u64() + .is_some_and(|n| n > 0) + ); + super::note_debug("wake-test", &None::<()>); + assert!( + super::snapshot()["wake-test:None"] + .as_u64() + .is_some_and(|n| n > 0) + ); + } +} diff --git a/crates/jcode-tui/src/tui/backend.rs b/crates/jcode-tui/src/tui/backend.rs index c105d151372..3aba1c0c091 100644 --- a/crates/jcode-tui/src/tui/backend.rs +++ b/crates/jcode-tui/src/tui/backend.rs @@ -9,7 +9,7 @@ mod elicitation; mod ssh_sessions; use crate::message::ToolCall; -use crate::protocol::{AuthChanged, FeatureToggle, Request, ServerEvent}; +use crate::protocol::{AuthChanged, FeatureToggle, HistoryBase, Request, ServerEvent}; use crate::server; use crate::transport::{Stream, WriteHalf}; use crate::tui::remote_diff::RemoteDiffTracker; @@ -20,6 +20,14 @@ use std::time::{Duration, Instant}; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::sync::Mutex; +fn subscribe_history_base(base: Option, ssh_remote: bool) -> Option { + if ssh_remote { + Some(HistoryBase::recent(128)) + } else { + base + } +} + /// Debug events broadcast by local harnesses via debug socket. /// These expose the full internal state for debugging/comparison. #[derive(Debug, Clone, Serialize, Deserialize)] @@ -238,6 +246,14 @@ pub struct BackendInfo { pub struct RemoteConnection { reader: BufReader, writer: Arc>, + /// FIFO outbound queue drained by a dedicated writer task. The UI event + /// loop must never await socket writes: if the daemon's receive buffer is + /// full while the daemon is itself blocked writing to us, awaiting the + /// write here would stop us reading and deadlock both ends. + outbound: std::sync::Mutex>>, + /// Queued writes not yet flushed. When a backlog exists, senders do not + /// wait for their own write result at all. + outbound_pending: Arc, _dummy_peer: Option, session_id: Option, cloud_authority: Option>, @@ -245,9 +261,9 @@ pub struct RemoteConnection { client_instance_id: Option, next_request_id: u64, pending_runtime_commands: std::collections::HashSet, - // Bootstrap Done acknowledgments are not completions of a detached turn. - // Retain recent ids because target Subscribe can acknowledge twice. + // Bootstrap Done is not turn completion. Retain ids for duplicate acks. control_done_ids: std::sync::Mutex>, + request_timings: super::cloud_latency::RequestTimings, tool_diff: RemoteDiffTracker, /// Bytes pulled from the socket that have not yet been split into complete /// newline-delimited protocol lines. This buffer is persistent across @@ -269,6 +285,12 @@ pub struct RemoteConnection { } const DETACHED_REQUEST_TIMEOUT: Duration = Duration::from_secs(2); +/// Longest a request send waits for its own write result before returning. +const OUTBOUND_RESULT_WAIT: Duration = Duration::from_millis(250); +type OutboundWrite = ( + Vec, + tokio::sync::oneshot::Sender>, +); const MAX_STRAY_REMOTE_PROTOCOL_LINES: usize = 32; /// Hard cap for one newline-delimited server event. History events can be large /// because they may contain images, but an authenticated or compromised peer @@ -284,6 +306,11 @@ const READ_BUFFER_SHRINK_THRESHOLD: usize = 256 * 1024; /// grow/shrink thrash. const READ_BUFFER_RETAIN_CAPACITY: usize = 64 * 1024; +/// Caller id attached to a TUI soft interrupt, derived from its request id. +pub(crate) fn soft_interrupt_client_id(request_id: u64) -> String { + format!("tui-{request_id}") +} + fn account_usage_poll_due(last_poll: Option, now: Instant) -> bool { last_poll.is_none_or(|last| now.saturating_duration_since(last) >= Duration::from_secs(30)) } @@ -383,32 +410,44 @@ impl RemoteConnection { allow_session_takeover: bool, remote_working_dir: Option<&str>, ) -> Result { + let mut timing = super::cloud_latency::Span::new("connect"); + timing.operation("socket_connect"); let connect_start = Instant::now(); let socket_connect_start = Instant::now(); - let stream = Stream::connect(server::socket_path()).await?; + let socket_path = server::socket_path(); + let stream = Stream::connect(&socket_path).await?; let socket_connect_ms = socket_connect_start.elapsed().as_millis(); let (reader, writer) = stream.into_split(); + let quota_setup_start = Instant::now(); // Quotas for headed clients belong to this daemon connection. Clear // previous/local snapshots before any startup account rows are drawn. - crate::usage::clear_remote_account_usage(); + let usage_generation = crate::usage::clear_remote_account_usage(); if !super::is_ssh_remote() { - crate::usage::hydrate_remote_account_usage(&server::socket_path()); + Self::spawn_usage_hydration(move || { + crate::usage::hydrate_remote_account_usage_for_generation( + &socket_path, + usage_generation, + ) + }); } + let quota_setup_ms = quota_setup_start.elapsed().as_millis(); + let connection_setup_start = Instant::now(); let mut conn = Self { reader: BufReader::new(reader), writer: Arc::new(Mutex::new(writer)), + outbound: std::sync::Mutex::new(None), + outbound_pending: Default::default(), _dummy_peer: None, session_id: None, cloud_authority: None, cloud_callbacks: Vec::new(), client_instance_id: client_instance_id.map(str::to_string), - // A reattached turn carries the old connection's request id on - // local sockets as well as SSH. Keep its Done distinct from this - // connection's Subscribe/GetHistory acknowledgments. + // Keep reattached turn Done distinct from bootstrap acknowledgments. next_request_id: (rand::random::() & ((1_u64 << 62) - 1)) | (1_u64 << 62), pending_runtime_commands: Default::default(), control_done_ids: Default::default(), + request_timings: Default::default(), tool_diff: RemoteDiffTracker::default(), read_buffer: Vec::new(), read_buffer_scan_start: 0, @@ -419,13 +458,18 @@ impl RemoteConnection { call_output_tokens_seen: 0, }; + let connection_setup_ms = connection_setup_start.elapsed().as_millis(); + timing.operation("ssh_ready"); + let ssh_ready_start = Instant::now(); // The local SSH adapter accepts before OpenSSH authenticates. A Unix // connect/write alone must not reset reconnect backoff or show success. if super::is_ssh_remote() { conn.verify_ssh_ready(Duration::from_secs(35)).await?; } + let ssh_ready_ms = ssh_ready_start.elapsed().as_millis(); // Subscribe to events + timing.operation("subscribe"); let subscribe_start = Instant::now(); let (working_dir, selfdev) = super::subscribe_metadata(remote_working_dir); let resume_target = resume_session @@ -447,7 +491,7 @@ impl RemoteConnection { target_session_id: resume_target.clone(), client_instance_id: conn.client_instance_id.clone(), client_has_local_history: client_has_local_history && !super::is_ssh_remote(), - history_base: history_base.filter(|_| !super::is_ssh_remote()), + history_base: subscribe_history_base(history_base, super::is_ssh_remote()), allow_session_takeover, crash_on_disconnect: false, continue_on_disconnect: super::is_ssh_remote(), @@ -464,6 +508,7 @@ impl RemoteConnection { // If resuming a session, the target-aware Subscribe attaches directly to // that session and returns History, so avoid a second bootstrap request. + timing.operation("bootstrap_request"); let bootstrap_request_start = Instant::now(); let mut bootstrap_request = "get_history"; if resume_target.is_none() { @@ -494,8 +539,11 @@ impl RemoteConnection { let bootstrap_request_ms = bootstrap_request_start.elapsed().as_millis(); crate::logging::info(&format!( - "[TIMING] remote connect: socket={}ms, subscribe={}ms, bootstrap_request={}ms, total={}ms, resumed={}, request={}", + "[TIMING] remote connect: socket={}ms, quota_setup={}ms, connection_setup={}ms, ssh_ready={}ms, subscribe={}ms, bootstrap_request={}ms, total={}ms, resumed={}, request={}", socket_connect_ms, + quota_setup_ms, + connection_setup_ms, + ssh_ready_ms, subscribe_ms, bootstrap_request_ms, connect_start.elapsed().as_millis(), @@ -503,9 +551,20 @@ impl RemoteConnection { bootstrap_request, )); + timing.operation("complete"); + timing.finish("ok"); Ok(conn) } + /// Advisory disk snapshots must not delay Subscribe/History or block the + /// async runtime. The generation check in the importer gives live replies + /// and subsequent connections precedence over a late disk read. + fn spawn_usage_hydration( + hydrate: impl FnOnce() -> bool + Send + 'static, + ) -> tokio::task::JoinHandle<()> { + super::cloud_latency::spawn_usage_hydration(hydrate) + } + fn interrupt_request_log_fields( &self, request: &Request, @@ -573,35 +632,97 @@ impl RemoteConnection { )); } + let mut timing = super::cloud_latency::begin_request(&request); let total_start = Instant::now(); - let writer_wait_start = Instant::now(); - let mut w = self.writer.lock().await; - let writer_wait_ms = writer_wait_start.elapsed().as_millis(); - let write_start = Instant::now(); - let result = w.write_all(json.as_bytes()).await; + let backlogged = self + .outbound_pending + .load(std::sync::atomic::Ordering::Acquire) + > 0; + let result = match self.enqueue_outbound(json.into_bytes()) { + Err(error) => Err(error), + Ok(_) if backlogged => Ok(()), + // Surface prompt write failures (broken pipe) to the caller, but + // never wait longer than a short bound: a full socket must not + // stall the loop that drains server events. + Ok(done) => match tokio::time::timeout(OUTBOUND_RESULT_WAIT, done).await { + Ok(Ok(Err(error))) => Err(anyhow::anyhow!(error)), + Ok(Err(_)) => Err(anyhow::anyhow!("remote connection writer closed")), + Ok(Ok(Ok(()))) | Err(_) => Ok(()), + }, + }; if let Some(fields) = &interrupt_log { match &result { Ok(()) => crate::logging::info(&format!( - "REMOTE_INTERRUPT_SEND_OK {} writer_wait_ms={} write_ms={} total_ms={}", + "REMOTE_INTERRUPT_SEND_OK {} queued total_ms={}", fields, - writer_wait_ms, - write_start.elapsed().as_millis(), total_start.elapsed().as_millis() )), Err(error) => crate::logging::warn(&format!( - "REMOTE_INTERRUPT_SEND_ERR {} writer_wait_ms={} write_ms={} total_ms={} error={}", + "REMOTE_INTERRUPT_SEND_ERR {} total_ms={} error={}", fields, - writer_wait_ms, - write_start.elapsed().as_millis(), total_start.elapsed().as_millis(), error )), } } + if let Some(timing) = &mut timing { + timing.finish(if result.is_ok() { "ok" } else { "error" }); + } result?; + self.request_timings.request_sent(&request); Ok(()) } + /// Queue bytes for the connection writer task, spawning it on first use. + /// Never waits on the socket, so the caller keeps draining server events. + /// The returned receiver resolves with the write result. + fn enqueue_outbound( + &self, + bytes: Vec, + ) -> Result>> { + let mut slot = self.outbound.lock().unwrap_or_else(|e| e.into_inner()); + if slot.as_ref().is_none_or(|tx| tx.is_closed()) { + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::(); + let writer = Arc::clone(&self.writer); + // Writes dropped with a dead writer task never flushed. + self.outbound_pending + .store(0, std::sync::atomic::Ordering::Release); + let pending = Arc::clone(&self.outbound_pending); + tokio::spawn(async move { + while let Some((bytes, done)) = rx.recv().await { + let mut w = writer.lock().await; + let result = w.write_all(&bytes).await; + drop(w); + pending.fetch_sub(1, std::sync::atomic::Ordering::AcqRel); + let failed = result.is_err(); + if done + .send(result.map_err(|error| error.to_string())) + .is_err() + { + crate::logging::debug("Remote write acknowledgement receiver closed"); + } + if failed { + crate::logging::warn("Remote writer task stopped after write error"); + break; + } + } + }); + *slot = Some(tx); + } + let sender = slot + .as_ref() + .ok_or_else(|| anyhow::anyhow!("remote connection writer unavailable"))?; + let (done_tx, done_rx) = tokio::sync::oneshot::channel(); + self.outbound_pending + .fetch_add(1, std::sync::atomic::Ordering::AcqRel); + sender.send((bytes, done_tx)).map_err(|_| { + self.outbound_pending + .fetch_sub(1, std::sync::atomic::Ordering::AcqRel); + anyhow::anyhow!("remote connection writer closed") + })?; + Ok(done_rx) + } + async fn send_request(&self, request: Request) -> Result<()> { self.send_request_with_interrupt_trigger(request, None) .await @@ -857,11 +978,16 @@ impl RemoteConnection { } /// Request a wider compacted-history window for the active session. - pub async fn get_compacted_history(&mut self, visible_messages: usize) -> Result { + pub async fn get_compacted_history( + &mut self, + visible_messages: usize, + recent_history: bool, + ) -> Result { let id = self.next_request_id; let request = Request::GetCompactedHistory { id, visible_messages, + recent_history, }; self.next_request_id += 1; self.send_request(request).await?; @@ -1171,7 +1297,9 @@ impl RemoteConnection { content, images, urgent, - client_id: None, + // Named so stranded-interrupt recovery can ask the server exactly + // which interrupts are still queued instead of guessing. + client_id: Some(soft_interrupt_client_id(id)), }; self.next_request_id += 1; self.send_request_with_interrupt_trigger(request, Some("soft_interrupt")) @@ -1179,6 +1307,21 @@ impl RemoteConnection { Ok(id) } + /// Cancel only the named soft interrupts. The server answers with + /// `SoftInterruptsCancelled`, splitting the ids into still-queued (now + /// removed) and not-queued (already injected or started as a turn). + pub async fn cancel_soft_interrupts_by_id(&mut self, client_ids: Vec) -> Result { + let id = self.next_request_id; + let request = Request::CancelSoftInterrupts { + id, + client_ids: Some(client_ids), + }; + self.next_request_id += 1; + self.send_request_with_interrupt_trigger(request, Some("cancel_soft_interrupts")) + .await?; + Ok(id) + } + pub async fn cancel_soft_interrupts(&mut self) -> Result<()> { let request = Request::CancelSoftInterrupts { id: self.next_request_id, @@ -1404,7 +1547,7 @@ impl RemoteConnection { } return LineOutcome::Skip; } - match serde_json::from_str(&text) { + match self.request_timings.parse_event(&text) { Ok(ServerEvent::Done { id }) if self .control_done_ids @@ -1464,6 +1607,8 @@ impl RemoteConnection { Self { reader: BufReader::new(reader), writer: Arc::new(Mutex::new(writer)), + outbound: std::sync::Mutex::new(None), + outbound_pending: Default::default(), _dummy_peer: Some(b), session_id: None, cloud_authority: None, @@ -1472,6 +1617,7 @@ impl RemoteConnection { next_request_id: 1, pending_runtime_commands: Default::default(), control_done_ids: Default::default(), + request_timings: Default::default(), tool_diff: RemoteDiffTracker::default(), read_buffer: Vec::new(), read_buffer_scan_start: 0, diff --git a/crates/jcode-tui/src/tui/backend_tests.rs b/crates/jcode-tui/src/tui/backend_tests.rs index cbdb9199bf0..7e60ccdb484 100644 --- a/crates/jcode-tui/src/tui/backend_tests.rs +++ b/crates/jcode-tui/src/tui/backend_tests.rs @@ -1,6 +1,89 @@ use super::*; use std::time::Duration; +#[test] +fn recent_history_negotiation_preserves_local_verified_prefix() { + let base = HistoryBase { + count: 7, + fingerprint: 123, + images_fingerprint: Some(456), + recent_messages: None, + }; + let local = subscribe_history_base(Some(base), false).unwrap(); + assert_eq!(local.count, 7); + assert_eq!(local.fingerprint, 123); + let remote = subscribe_history_base(Some(base), true).unwrap(); + assert_eq!(remote.count, 0); + assert_eq!(remote.recent_messages, Some(128)); + assert!(subscribe_history_base(None, false).is_none()); + assert_eq!( + subscribe_history_base(None, true).unwrap().recent_messages, + Some(128) + ); +} + +#[tokio::test] +async fn recent_history_page_request_explicitly_selects_normal_transcript() { + let mut remote = RemoteConnection::dummy(); + let id = remote.get_compacted_history(192, true).await.unwrap(); + let peer = remote._dummy_peer.as_ref().unwrap(); + peer.readable().await.unwrap(); + let mut bytes = [0; 256]; + let count = peer.try_read(&mut bytes).unwrap(); + assert!( + matches!(serde_json::from_slice::(&bytes[..count]).unwrap(), + Request::GetCompactedHistory { id: actual, visible_messages: 192, recent_history: true } if actual == id) + ); +} + +#[tokio::test] +async fn slow_usage_hydration_does_not_block_subscribe_or_async_runtime() { + let (started_tx, started_rx) = tokio::sync::oneshot::channel(); + let (release_tx, release_rx) = std::sync::mpsc::channel(); + let hydration = RemoteConnection::spawn_usage_hydration(move || { + started_tx.send(()).unwrap(); + release_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + false + }); + started_rx.await.unwrap(); + let remote = RemoteConnection::dummy(); + let peer = remote._dummy_peer.as_ref().unwrap(); + let start = Instant::now(); + let result = tokio::time::timeout(Duration::from_millis(250), async { + remote + .send_request(Request::GetHistory { id: 9 }) + .await + .unwrap(); + let mut bytes = [0; 256]; + peer.readable().await.unwrap(); + let count = peer.try_read(&mut bytes).unwrap(); + assert!(matches!( + serde_json::from_slice::(&bytes[..count]).unwrap(), + Request::GetHistory { id: 9 } + )); + }) + .await; + let elapsed = start.elapsed(); + release_tx.send(()).unwrap(); + hydration.await.unwrap(); + result.expect("quota disk reads must not delay bootstrap requests"); + eprintln!( + "bootstrap while snapshot read blocked: {}us", + elapsed.as_micros() + ); +} + +#[test] +fn measure_local_quota_hydration_cost() { + let socket = server::socket_path(); + let started = Instant::now(); + let imported = crate::usage::hydrate_remote_account_usage(&socket); + eprintln!( + "local quota hydration: {}us, imported={imported}", + started.elapsed().as_micros() + ); +} + #[tokio::test] async fn ssh_readiness_waits_for_daemon_and_preserves_buffered_events() { let mut remote = RemoteConnection::dummy(); @@ -723,3 +806,24 @@ fn remote_protocol_frame_limit_rejects_oversize_and_overflow() { )); assert!(remote_protocol_frame_exceeds_limit(usize::MAX, 1)); } + +#[tokio::test] +async fn request_send_does_not_block_ui_when_server_stops_reading() { + // Regression: the TUI awaited socket writes inline, so when the daemon's + // receive buffer filled while it was blocked writing to us, the UI loop + // stopped reading and both ends deadlocked (frozen session). + let mut remote = RemoteConnection::dummy(); + let writer = remote.writer(); + let _guard = writer.lock().await; + + let result = tokio::time::timeout(Duration::from_secs(1), async { + for _ in 0..64 { + remote.request_todos().await.expect("queued"); + } + }) + .await; + assert!( + result.is_ok(), + "request sends must not wait on the socket writer" + ); +} diff --git a/crates/jcode-tui/src/tui/cloud_directories_probe.py b/crates/jcode-tui/src/tui/cloud_directories_probe.py index 663cf43c1a7..56b1b5c5a1d 100644 --- a/crates/jcode-tui/src/tui/cloud_directories_probe.py +++ b/crates/jcode-tui/src/tui/cloud_directories_probe.py @@ -14,10 +14,144 @@ MAX_ITEMS = 4096 MAX_JSON_BYTES = 32 * 1024 * 1024 -# JSON strings are skipped in C, including escaped quotes and embedded tool JSON. -# The parser only decodes top-level scalar metadata, never transcript contents. -TOKENS = re.compile(rb'"[^"\\]*(?:\\.[^"\\]*)*"|[{}\[\],:]|true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?') FIELDS = {'working_dir', 'is_debug', 'is_canary'} +# Validate skipped strings, including UTF-8, without materializing their text. +UTF8 = (rb'[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|' + rb'[\xe1-\xec\xee-\xef][\x80-\xbf]{2}|\xed[\x80-\x9f][\x80-\xbf]|' + rb'\xf0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|' + rb'\xf4[\x80-\x8f][\x80-\xbf]{2}') +ASCII = rb'[\x20-\x21\x23-\x5b\x5d-\x7f]*' +STRING = rb'"' + ASCII + rb'(?:(?:\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})|' + UTF8 + rb')' + ASCII + rb')*"' +WS = rb'[ \t\r\n]*' +SCALAR = STRING + rb'|true|false|null|-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?' + + +def value_pattern(depth): + scalar = rb'(?:' + SCALAR + rb')' + if not depth: + return scalar + child = value_pattern(depth - 1) + pair = STRING + WS + b':' + WS + child + return (rb'(?:' + scalar + rb'|\[' + WS + rb'(?:' + child + WS + + rb'(?:,' + WS + child + WS + rb')*)?\]|\{' + WS + rb'(?:' + pair + + WS + rb'(?:,' + WS + pair + WS + rb')*)?\})') + + +# A shallow complete value (typically one transcript message) is validated in C. +# Limit each match's window so an arbitrarily large vector cannot monopolize the +# deadline or allocate regex backtracking state proportional to the transcript. +VALUE = re.compile(value_pattern(3)) +ASCII_RUN = re.compile(ASCII) +ESCAPE = re.compile(rb'\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})') +UTF8_TOKEN = re.compile(UTF8) +SPACE = re.compile(WS) +SCAN_WINDOW = 64 * 1024 + + +def metadata(data, check_time, journal=False): + size = len(data) + + def space(pos): + while True: + limit = min(size, pos + SCAN_WINDOW) + pos = SPACE.match(data, pos, limit).end() + if pos < limit or pos == size: + return pos + check_time() + + def string(pos): + if data[pos:pos + 1] != b'"': + raise ValueError('invalid JSON string') + pos += 1 + while True: + check_time() + limit = min(size, pos + SCAN_WINDOW) + pos = ASCII_RUN.match(data, pos, limit).end() + if pos == limit and pos < size: + continue + first = data[pos:pos + 1] + if first == b'"': + return pos + 1 + token = (ESCAPE if first == b'\\' else UTF8_TOKEN).match(data, pos) + if token is None: + raise ValueError('invalid JSON string') + pos = token.end() + + def skip(pos, depth=0): + check_time() + if depth > 128: + raise ValueError('session nesting limit') + token = VALUE.match(data, pos, min(size, pos + SCAN_WINDOW)) + if token is not None: + return token.end() + first = data[pos:pos + 1] + if first == b'"': + end = string(pos) + check_time() + return end + if first not in (b'{', b'['): + raise ValueError('invalid JSON value') + closing = b'}' if first == b'{' else b']' + pos = space(pos + 1) + if data[pos:pos + 1] == closing: + return pos + 1 + while True: + if first == b'{': + pos = space(string(pos)) + if data[pos:pos + 1] != b':': + raise ValueError('missing JSON colon') + pos = space(pos + 1) + pos = space(skip(pos, depth + 1)) + if data[pos:pos + 1] == closing: + return pos + 1 + if data[pos:pos + 1] != b',': + raise ValueError('missing JSON separator') + pos = space(pos + 1) + + def obj(pos, wanted): + if data[pos:pos + 1] != b'{': + raise ValueError('session must be an object') + result = {} + pos = space(pos + 1) + if data[pos:pos + 1] == b'}': + return result, pos + 1 + while True: + check_time() + end = string(pos) + # Unrecognized long keys need validation, not decoding/allocation. + key = json.loads(data[pos:end]) if end - pos <= 1024 else None + pos = space(end) + if data[pos:pos + 1] != b':': + raise ValueError('missing JSON colon') + pos = space(pos + 1) + if journal and wanted == {'meta'} and key == 'meta' and data[pos:pos + 1] == b'{': + result[key], pos = obj(pos, FIELDS) + else: + end = skip(pos) + if key in wanted: + if journal and wanted == {'meta'}: + # Only object-valued meta participates in journal replay. + result[key] = None + else: + if not journal and end - pos > 16384: + raise ValueError('oversized session field') + # Containers must replace a prior duplicate scalar, + # matching JSON's last-key-wins semantics. + result[key] = json.loads(data[pos:end]) + pos = end + pos = space(pos) + if data[pos:pos + 1] == b'}': + return result, pos + 1 + if data[pos:pos + 1] != b',': + raise ValueError('missing JSON separator') + pos = space(pos + 1) + + check_time() + result, end = obj(space(0), {'meta'} if journal else FIELDS) + if space(end) != size: + raise ValueError('trailing session data') + check_time() + return result def session_fields(path, check_time): @@ -25,24 +159,35 @@ def session_fields(path, check_time): # complete record is authoritative, exactly as load_startup_stub handles it. journal = path.with_suffix('.journal.jsonl') try: - latest = None with open(journal, 'rb') as stream: - while True: - check_time() - line = stream.readline(MAX_JSON_BYTES + 1) - if not line: - break - if len(line) > MAX_JSON_BYTES: - raise ValueError('oversized journal record') - try: - entry = json.loads(line) - meta = entry.get('meta') - if isinstance(meta, dict): - latest = {key: meta.get(key) for key in FIELDS} - except (ValueError, AttributeError): - continue # tolerate a partial final append, like journal replay - if latest is not None: - return latest + if os.fstat(stream.fileno()).st_size: + with mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ) as data: + # Check every record's byte bound, including superseded ones. + # Only the newest valid metadata record needs JSON parsing. + start = cursor = 0 + while cursor < len(data): + check_time() + limit = min(len(data), cursor + SCAN_WINDOW) + newline = data.find(b'\n', cursor, limit) + end = newline + 1 if newline >= 0 else limit + if end - start > MAX_JSON_BYTES: + raise ValueError('oversized journal record') + if newline >= 0: + start = end + cursor = end + end = len(data) + while end: + check_time() + # Include the newline in the same bound as readline. + start = data.rfind(b'\n', 0, end - 1) + 1 + try: + entry = metadata(data[start:end], check_time, journal=True) + meta = entry.get('meta') + if isinstance(meta, dict): + return {key: meta.get(key) for key in FIELDS} + except (ValueError, AttributeError): + pass # tolerate partial/malformed appends, like replay + end = start except FileNotFoundError: pass @@ -50,51 +195,7 @@ def session_fields(path, check_time): if os.fstat(stream.fileno()).st_size == 0: raise ValueError('empty session snapshot') with mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ) as data: - stack = [] - result = {} - key = None - expect_key = False - expect_value = False - end = 0 - for index, token in enumerate(TOKENS.finditer(data)): - if index % 256 == 0: - check_time() - if data[end:token.start()].strip(): - raise ValueError('invalid session snapshot') - end = token.end() - first = data[token.start():token.start() + 1] - if first in (b'{', b'['): - stack.append(first) - if len(stack) == 1: - if first != b'{': - raise ValueError('session must be an object') - expect_key = True - expect_value = False - elif first in (b'}', b']'): - if not stack or stack.pop() != (b'{' if first == b'}' else b'['): - raise ValueError('unbalanced session snapshot') - if not stack: - break - elif len(stack) == 1: - if first == b',': - expect_key = True - elif first == b':': - expect_value = True - elif expect_key: - if first != b'"' or token.end() - token.start() > 1024: - raise ValueError('invalid session field') - key = json.loads(token.group()) - expect_key = False - elif expect_value: - if key in FIELDS: - if token.end() - token.start() > 16384: - raise ValueError('oversized session field') - result[key] = json.loads(token.group()) - expect_value = False - check_time() - if stack or data[end:].strip(): - raise ValueError('incomplete session snapshot') - return result + return metadata(data, check_time) def collect(current, home=None, state=None, alive=None, current_session_id=None): diff --git a/crates/jcode-tui/src/tui/cloud_directories_probe_test.py b/crates/jcode-tui/src/tui/cloud_directories_probe_test.py new file mode 100644 index 00000000000..49db67053ac --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_directories_probe_test.py @@ -0,0 +1,194 @@ +"""Focused, synthetic-only checks. Run: python3 cloud_directories_probe_test.py. + +--benchmark BASELINE_PATH compares identical frozen synthetic files, never user state. +""" +import importlib.util +import json +import pathlib +import sys +import tempfile +import time +import unittest +from unittest import mock + + +def load(path): + spec = importlib.util.spec_from_file_location('probe', path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +probe = load(pathlib.Path(__file__).with_name('cloud_directories_probe.py')) + + +class ProbeTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.root = pathlib.Path(self.tmp.name) + self.path = self.root / 'session.json' + + def fields(self, raw): + self.path.write_bytes(raw) + return probe.session_fields(self.path, lambda: None) + + def test_duplicate_escaped_metadata(self): + self.assertEqual(self.fields(br'{"working_dir":"/old","messages":[{"working_dir":"/fake"}],"working\u005fdir":"/new","is_debug":false,"is_canary":true}'), + dict(working_dir='/new', is_debug=False, is_canary=True)) + + def test_container_duplicate_clears_scalar(self): + result = self.fields(b'{"working_dir":"/old","working_dir":{},"is_debug":[]}') + self.assertNotEqual(result.get('working_dir'), '/old') + self.assertIn('is_debug', result) + + def test_invalid_snapshots(self): + for raw in (b'', b'[]', b'{"messages":[1 2]}', b'{"messages":[1,]}', + b'{"a":true "b":false}', b'{"a":01}', b'{"a":"\\q"}', + b'{"a":1}junk', b'{"a":{"b":}}', b'{"a":true,}', + b'{"messages":["\xff"]}'): + with self.subTest(raw=raw), self.assertRaises(ValueError): + self.fields(raw) + + def test_latest_complete_journal_and_partial(self): + self.path.write_bytes(b'{"working_dir":"/snapshot"}') + journal = self.path.with_suffix('.journal.jsonl') + journal.write_bytes(b'{"meta":{"working_dir":"/old"}}\n' + b'{"messages":[1],"meta":{"working_dir":"/new","is_debug":true}}\n' + b'{"meta":{"working_dir":"/partial"}') + self.assertEqual(probe.session_fields(self.path, lambda: None), + dict(working_dir='/new', is_debug=True, is_canary=None)) + journal.write_bytes(b'{bad}\n{"meta":null}\n') + self.assertEqual(probe.session_fields(self.path, lambda: None), {'working_dir': '/snapshot'}) + + def test_duplicate_meta_and_invalid_journal(self): + self.path.write_bytes(b'{"working_dir":"/snapshot"}') + self.path.with_suffix('.journal.jsonl').write_bytes( + b'{"meta":{"working_dir":"/old"},"meta":{"working_dir":"/last"}}\n' + b'{"meta":{"working_dir":"/bad"},"messages":[1 2]}\n') + self.assertEqual(probe.session_fields(self.path, lambda: None)['working_dir'], '/last') + + def test_deadline(self): + self.path.write_bytes(b'{"messages":[] }') + def expired(): + raise TimeoutError('synthetic deadline') + with self.assertRaises(TimeoutError): + probe.session_fields(self.path, expired) + + def test_long_strings_unicode_nested_values_and_no_transcript_decoding(self): + transcript = 'TRANSCRIPT_SENTINEL' + ('abc\\\"[]{}' * 20000) + '\U0001f98a\u00e9' + raw = json.dumps({'messages': [{'content': transcript}], + 'working_dir': '/exact/sub', 'is_debug': False}, + ensure_ascii=False).encode() + original = json.loads + def scalar_only(value): + self.assertNotIn(b'TRANSCRIPT_SENTINEL', bytes(value)) + return original(value) + with mock.patch.object(probe.json, 'loads', side_effect=scalar_only): + self.assertEqual(self.fields(raw), dict(working_dir='/exact/sub', is_debug=False)) + self.path.with_suffix('.journal.jsonl').write_bytes(json.dumps( + {'messages': [{'content': transcript}], 'meta': {'working_dir': '/journal/sub'}}, + ensure_ascii=False).encode() + b'\n{"meta":') + self.assertEqual(probe.session_fields(self.path, lambda: None), + dict(working_dir='/journal/sub', is_debug=None, is_canary=None)) + self.path.with_suffix('.journal.jsonl').unlink() + for length in (probe.SCAN_WINDOW - 1, probe.SCAN_WINDOW, probe.SCAN_WINDOW + 1): + self.assertEqual(self.fields(b'{"ignored":"' + b'a' * length + b'","working_dir":"/ok"}'), + {'working_dir': '/ok'}) + for offset in (-2, -1, 0, 1): + for tail in ('\U0001f98a"\\tail', '\\"\u00e9'): + raw = json.dumps({'ignored': 'a' * (probe.SCAN_WINDOW + offset) + tail, + 'working_dir': '/boundary'}, ensure_ascii=False).encode() + self.assertEqual(self.fields(raw), {'working_dir': '/boundary'}) + nested = 20 * b'[' + b'"ok"' + 20 * b']' + self.assertEqual(self.fields(b'{"ignored":' + nested + b',"working_dir":"/ok"}'), {'working_dir': '/ok'}) + with self.assertRaises(ValueError): + self.fields(b'{"ignored":' + 140 * b'[' + b'0' + 140 * b']' + b'}') + + def test_checkpoint_inside_large_string_and_whitespace(self): + for raw in (b'{"messages":["' + b'x' * (probe.SCAN_WINDOW * 8) + b'"]}', + b' ' * (probe.SCAN_WINDOW * 8) + b'{}'): + self.path.write_bytes(raw) + calls = 0 + def expire_during_scan(): + nonlocal calls + calls += 1 + if calls == 6: + raise TimeoutError('bounded checkpoint') + with self.assertRaises(TimeoutError): + probe.session_fields(self.path, expire_during_scan) + + def test_journal_bounds_include_older_records_and_newlines(self): + self.path.write_bytes(b'{"working_dir":"/snapshot"}') + journal = self.path.with_suffix('.journal.jsonl') + with mock.patch.object(probe, 'MAX_JSON_BYTES', 40): + for raw in (b' ' * 41 + b'\n{"meta":{}}\n', b' ' * 40 + b'\n', b'x' * 41): + journal.write_bytes(raw) + with self.subTest(raw=raw), self.assertRaises(ValueError): + probe.session_fields(self.path, lambda: None) + journal.write_bytes(b'{"meta":{"working_dir":"/first"}}\n{"meta":null}\n\n') + self.assertEqual(probe.session_fields(self.path, lambda: None)['working_dir'], '/first') + + def test_pending_session_and_current_metadata_fallback(self): + home = self.root / 'home' + home.mkdir() + state = self.root / 'state' + (state / 'active_pids').mkdir(parents=True) + (state / 'streaming_pids').mkdir() + (state / 'active_pids' / 'new').write_text('1') + (state / 'streaming_pids' / 'new').write_text('1') + result = probe.collect(str(home), home, state, alive=lambda _: True) + self.assertIn(dict(path='(directory pending)', agents=1, busy=1), result['entries']) + result = probe.collect(str(home), home, state, alive=lambda _: True, current_session_id='new') + self.assertEqual(result['entries'], [dict(path='~', agents=1, busy=1)]) + + def test_collect_retains_four_second_deadline(self): + with mock.patch.object(probe.time, 'monotonic', side_effect=[0, 4.000001]): + with self.assertRaises(TimeoutError): + probe.collect(str(self.root), self.root, self.root / 'synthetic-state') + + def test_collect_exact_counts_flags_and_alive(self): + home = self.root / 'home' + repo = home / 'repo' + sub = repo / 'sub' + sub.mkdir(parents=True) + (repo / '.git').mkdir() + state = self.root / 'state' + for name in ('sessions', 'active_pids', 'streaming_pids'): + (state / name).mkdir(parents=True) + for sid, pid, flags in [('idle', 1, {}), ('busy', 2, {}), ('debug', 3, {'is_debug': True}), + ('canary', 4, {'is_canary': True}), ('dead', 9, {})]: + (state / 'active_pids' / sid).write_text(str(pid)) + (state / 'sessions' / (sid + '.json')).write_text(json.dumps(dict(working_dir=str(sub), **flags))) + (state / 'streaming_pids' / 'busy').write_text('2') + result = probe.collect(str(sub), home, state, alive=lambda pid: pid != 9) + self.assertEqual(result, dict(current_path='~/repo', entries=[dict(path='~/repo', agents=2, busy=1)], truncated=False)) + + +def benchmark(baseline): + old = load(baseline) + with tempfile.TemporaryDirectory(prefix='jcode-probe-synthetic-') as temp: + path = pathlib.Path(temp) / 'session.json' + # Fixed transcript shape with nested tool payloads and escaped punctuation. + message = json.dumps({'role': 'assistant', 'content': [{'type': 'tool', 'text': 'hello \\\"[]{} metadata ' * 12}], 'usage': {'input': 42, 'output': 17}}, separators=(',', ':')).encode() + payload = b'{"working_dir":"/synthetic/exact/sub","is_debug":false,"is_canary":false,"messages":[' + b','.join([message] * 256000) + b']}' + path.write_bytes(payload) + journal = path.with_suffix('.journal.jsonl') + for kind in ('snapshot', 'journal'): + if kind == 'journal': + entry = b'{"meta":{"working_dir":"/synthetic/exact/sub","is_debug":false,"is_canary":false},"messages":[' + b','.join([message] * 16000) + b']}\n' + journal.write_bytes(entry * 16 + b'{"meta":') + results = [] + for label, module in [('before', old), ('after', probe)]: + started = time.perf_counter() + result = module.session_fields(path, lambda: None) + results.append(result) + print(json.dumps(dict(kind=kind, implementation=label, bytes=(path if kind == 'snapshot' else journal).stat().st_size, seconds=round(time.perf_counter()-started, 6), output=result), sort_keys=True)) + assert results[0] == results[1], 'before/after metadata mismatch' + + +if __name__ == '__main__': + if len(sys.argv) == 3 and sys.argv[1] == '--benchmark': + benchmark(sys.argv[2]) + else: + unittest.main() diff --git a/crates/jcode-tui/src/tui/cloud_directories_probe_tests.rs b/crates/jcode-tui/src/tui/cloud_directories_probe_tests.rs new file mode 100644 index 00000000000..43a6d70a9dd --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_directories_probe_tests.rs @@ -0,0 +1,23 @@ +//! Synthetic-only regression harness for the embedded directory probe. + +#[test] +fn cloud_directories_probe_metadata_regressions() { + let dir = tempfile::tempdir().expect("synthetic probe directory"); + let probe = dir.path().join("cloud_directories_probe.py"); + let tests = dir.path().join("cloud_directories_probe_test.py"); + std::fs::write(&probe, include_str!("cloud_directories_probe.py")) + .expect("write synthetic probe copy"); + std::fs::write(&tests, include_str!("cloud_directories_probe_test.py")) + .expect("write synthetic probe tests"); + let output = std::process::Command::new("python3") + .arg(&tests) + .env("PYTHONDONTWRITEBYTECODE", "1") + .output() + .expect("python3 is required for directory probe regression tests"); + assert!( + output.status.success(), + "directory probe regressions failed:\n{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr), + ); +} diff --git a/crates/jcode-tui/src/tui/cloud_git.rs b/crates/jcode-tui/src/tui/cloud_git.rs index bea82951bd1..c0b9673d24a 100644 --- a/crates/jcode-tui/src/tui/cloud_git.rs +++ b/crates/jcode-tui/src/tui/cloud_git.rs @@ -8,6 +8,8 @@ use std::path::PathBuf; use std::sync::{Arc, Mutex, OnceLock}; use std::time::Duration; use tokio::io::AsyncReadExt; +#[path = "cloud_git_stderr.rs"] +mod stderr_timing; #[derive(Clone, Debug, Default, PartialEq)] pub enum RepoState { @@ -84,6 +86,10 @@ fn script_command_inner( ) -> tokio::process::Command { let mut cmd = if let Some(host) = host { let mut cmd = tokio::process::Command::new("ssh"); + let managed_config = host == "jcode-cloud" + && config.is_some_and(|path| { + crate::storage::jcode_dir().is_ok_and(|home| path == home.join("cloud/ssh_config")) + }); if compressed { cmd.arg("-C"); } @@ -116,8 +122,6 @@ fn script_command_inner( "SessionType=default", "-o", "ControlMaster=no", - "-S", - "none", "-o", "ConnectTimeout=8", "-o", @@ -125,6 +129,11 @@ fn script_command_inner( "-o", "ServerAliveCountMax=1", ]); + // Borrow the account-scoped managed master without owning or stopping + // it. Custom SSH configurations remain isolated from arbitrary muxes. + if !managed_config { + cmd.args(["-S", "none"]); + } cmd.arg("--") .arg(host) .arg(format!("sh -c {} sh {}", quote(script), quote(dir))); @@ -136,7 +145,7 @@ fn script_command_inner( }; cmd.stdin(std::process::Stdio::null()) .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) .kill_on_drop(true); cmd } @@ -159,7 +168,7 @@ pub(crate) async fn probe_workspace( } async fn probe_with_timeout(command: tokio::process::Command, deadline: Duration) -> RepoState { - match bounded_output(command, deadline, 1024 * 1024).await { + match bounded_output_for(command, deadline, 1024 * 1024, WorkspaceOperation::GitProbe).await { Some((status, _)) if status.code() == Some(42) => RepoState::NoRepository, Some((status, bytes)) if status.success() => parse(&bytes) .map(RepoState::Ready) @@ -168,11 +177,47 @@ async fn probe_with_timeout(command: tokio::process::Command, deadline: Duration } } +/// Fixed vocabulary only: never include paths, commands, hosts, or session content. +#[derive(Clone, Copy)] +pub(crate) enum WorkspaceOperation { + GitProbe, + InventoryPrepare, + InventoryProjection, + Preview, + CloudStatus, + CloudStatusDirectories, +} + +impl WorkspaceOperation { + fn label(self) -> &'static str { + match self { + Self::GitProbe => "git_probe", + Self::InventoryPrepare => "inventory_prepare", + Self::InventoryProjection => "inventory_projection", + Self::Preview => "preview", + Self::CloudStatus => "cloud_status", + Self::CloudStatusDirectories => "cloud_status_directories", + } + } +} + +#[cfg(test)] pub(crate) async fn bounded_output( + command: tokio::process::Command, + deadline: Duration, + limit: usize, +) -> Option<(std::process::ExitStatus, Vec)> { + bounded_output_for(command, deadline, limit, WorkspaceOperation::GitProbe).await +} + +pub(crate) async fn bounded_output_for( mut command: tokio::process::Command, deadline: Duration, limit: usize, + operation: WorkspaceOperation, ) -> Option<(std::process::ExitStatus, Vec)> { + let mut timing = super::cloud_latency::Span::new("workspace_command"); + timing.operation(operation.label()); #[cfg(unix)] { use std::os::unix::process::CommandExt; @@ -194,27 +239,48 @@ pub(crate) async fn bounded_output( } } } + command + .stderr(std::process::Stdio::piped()) + .kill_on_drop(true); let Ok(mut child) = command.spawn() else { return None; }; + let _process_group = stderr_timing::ProcessGroup(child.id()); let stdout = child.stdout.take()?; + let stderr = child.stderr.take()?; let result = tokio::time::timeout(deadline, async { - let mut bytes = Vec::new(); - stdout - .take((limit + 1) as u64) - .read_to_end(&mut bytes) - .await - .ok()?; - if bytes.len() > limit { - return None; + let output = async { + let mut bytes = Vec::new(); + stdout + .take(limit.saturating_add(1) as u64) + .read_to_end(&mut bytes) + .await + .ok()?; + if bytes.len() > limit { + return None; + } + let status = child.wait().await.ok()?; + Some((status, bytes)) + }; + let diagnostics = async { + stderr_timing::drain(stderr, |record| crate::logging::info(&record)).await; + Ok::<(), ()>(()) + }; + // No detached reader can outlive cancellation or this deadline. An + // oversized stdout cancels the stderr drain immediately. + match tokio::try_join!(async { output.await.ok_or(()) }, diagnostics) { + Ok((output, ())) => Some(output), + Err(()) => None, } - let status = child.wait().await.ok()?; - Some((status, bytes)) }) .await; match result { - Ok(Some(output)) => Some(output), - _ => { + Ok(Some(output)) => { + timing.finish(if output.0.success() { "ok" } else { "error" }); + Some(output) + } + result => { + timing.finish(if result.is_err() { "timeout" } else { "error" }); #[cfg(unix)] if let Some(pid) = child.id() { // Own process group only, including ProxyCommand descendants. @@ -222,8 +288,12 @@ pub(crate) async fn bounded_output( libc::kill(-(pid as i32), libc::SIGKILL); } } - let _ = child.kill().await; - let _ = child.wait().await; + if let Err(error) = child.kill().await { + crate::logging::debug(&format!("Failed to stop cloud git child: {error}")); + } + if let Err(error) = child.wait().await { + crate::logging::debug(&format!("Failed to reap cloud git child: {error}")); + } None } } @@ -294,6 +364,42 @@ fn parse(bytes: &[u8]) -> Option { Some(info) } +static CLIENT_FOCUSED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true); + +fn probe_wake() -> &'static tokio::sync::Notify { + static WAKE: std::sync::OnceLock = std::sync::OnceLock::new(); + WAKE.get_or_init(tokio::sync::Notify::new) +} + +/// Each probe spawns a shell and git (plus ssh for the cloud side). A +/// backgrounded window shows nothing, so it only needs a slow safety refresh; +/// regaining focus or a repo-mutating tool wakes the workers immediately. +fn probe_period() -> Duration { + if CLIENT_FOCUSED.load(std::sync::atomic::Ordering::Relaxed) { + Duration::from_secs(5) + } else { + Duration::from_secs(120) + } +} + +pub(crate) fn note_client_focus(focused: bool) { + let was = CLIENT_FOCUSED.swap(focused, std::sync::atomic::Ordering::Relaxed); + if focused && !was { + refresh_soon(); + } +} + +/// Re-probe both repos now instead of at the next period. +pub(crate) fn refresh_soon() { + probe_wake().notify_waiters(); +} + +/// Sleep until the next SSH probe is due: the focus-dependent period, or an +/// earlier wake from focus regain or a repo-mutating tool. +pub(crate) async fn wait_for_next_probe() { + let _ = tokio::time::timeout(probe_period(), probe_wake().notified()).await; +} + /// Starts exactly two independent workers for this attach. No shared cache /// with the ordinary local status-line probe, and no stale success on failure. #[cfg(not(test))] @@ -349,7 +455,7 @@ pub(crate) fn gather() -> Option { crate::bus::Bus::global() .publish(crate::bus::BusEvent::GitInfoRefreshed); } - tokio::time::sleep(Duration::from_secs(5)).await; + wait_for_next_probe().await; } }); }); @@ -489,6 +595,33 @@ mod tests { ); } + #[test] + fn managed_cloud_reads_borrow_mux_but_custom_transports_remain_isolated() { + let _lock = crate::storage::lock_test_env(); + let home = crate::storage::jcode_dir().unwrap(); + let managed = home.join("cloud/ssh_config"); + for (host, config, borrow) in [ + ("jcode-cloud", managed.as_path(), true), + ("custom-host", managed.as_path(), false), + ( + "jcode-cloud", + std::path::Path::new("/private/config"), + false, + ), + ] { + let cmd = compressed_script_command(Some(host), Some(config), "session", "true"); + let args: Vec<_> = cmd + .as_std() + .get_args() + .map(|a| a.to_string_lossy()) + .collect(); + assert_eq!(args.iter().any(|a| a == "-S"), !borrow); + assert!(args.iter().any(|a| a == "ControlMaster=no")); + assert!(!args.iter().any(|a| a == "ControlMaster=auto")); + assert!(args.iter().any(|a| a == "ClearAllForwardings=yes")); + } + } + #[test] fn ssh_command_quotes_workspace_and_reuses_private_config() { let cmd = command( diff --git a/crates/jcode-tui/src/tui/cloud_git_stderr.rs b/crates/jcode-tui/src/tui/cloud_git_stderr.rs new file mode 100644 index 00000000000..fdfa1518cc6 --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_git_stderr.rs @@ -0,0 +1,92 @@ +//! Remote stderr is untrusted. Drain it without retaining content or trusting labels. +use tokio::io::{AsyncRead, AsyncReadExt}; + +const MAX_LINE: usize = 192; +const SCAN_BUDGET: usize = 64 * 1024; +const RECORD_BUDGET: usize = 128; + +fn timing_record(line: &[u8]) -> Option { + let Ok(text) = std::str::from_utf8(line) else { + return None; + }; + let rest = text.strip_prefix("[CLOUD_TIMING] stage=")?; + let (stage, rest) = rest.split_once(" outcome=")?; + let stage = match stage { + "projection" => "projection", + "remote_request" => "remote_request", + "inventory_entry" => "inventory_entry", + "remote_preview" => "remote_preview", + _ => return None, + }; + let (outcome, duration) = rest.split_once(" duration_ms=")?; + let outcome = match outcome { + "success" => "success", + "error" => "error", + "skipped_invalid_target" => "skipped_invalid_target", + "skipped_away" => "skipped_away", + _ => return None, + }; + if duration.is_empty() || !duration.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + let Ok(duration) = duration.parse::() else { + return None; + }; + Some(format!( + "[CLOUD_TIMING] stage={stage} outcome={outcome} duration_ms={duration}" + )) +} + +pub(super) async fn drain(reader: impl AsyncRead + Unpin, mut emit: impl FnMut(String)) { + let mut reader = reader; + let mut chunk = [0u8; 4096]; + let mut line = Vec::with_capacity(MAX_LINE); + let mut oversized = false; + let mut scanned = 0usize; + let mut emitted = 0usize; + loop { + let Ok(count) = reader.read(&mut chunk).await else { + return; + }; + if count == 0 { + return; + } + for &byte in &chunk[..count] { + if scanned >= SCAN_BUDGET || emitted >= RECORD_BUDGET { + // Still drain the pipe, otherwise a noisy child can deadlock stdout. + break; + } + scanned += 1; + if byte == b'\n' { + if !oversized && let Some(record) = timing_record(&line) { + emit(record); + emitted += 1; + } + line.clear(); + oversized = false; + } else if line.len() < MAX_LINE && !oversized { + line.push(byte); + } else { + line.clear(); + oversized = true; + } + } + } +} + +/// Unlike kill_on_drop, this also kills pipe-holding descendants on cancellation. +pub(super) struct ProcessGroup(pub(super) Option); +impl Drop for ProcessGroup { + fn drop(&mut self) { + #[cfg(unix)] + if let Some(pid) = self.0 { + unsafe { + libc::kill(-(pid as i32), libc::SIGKILL); + } + } + } +} + +#[cfg(test)] +#[path = "cloud_git_stderr_tests.rs"] +mod tests; diff --git a/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs b/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs new file mode 100644 index 00000000000..e80cf3769de --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs @@ -0,0 +1,203 @@ +use super::*; +use crate::tui::cloud_git::{bounded_output, script_command}; + +#[test] +fn workspace_operations_are_static_and_all_production_paths_are_assigned() { + use crate::tui::cloud_git::WorkspaceOperation::*; + assert!(include_str!("cloud_git.rs").contains("timing.operation(operation.label())")); + assert_eq!( + [ + GitProbe, + InventoryPrepare, + InventoryProjection, + Preview, + CloudStatus, + CloudStatusDirectories + ] + .map(|operation| operation.label()), + [ + "git_probe", + "inventory_prepare", + "inventory_projection", + "preview", + "cloud_status", + "cloud_status_directories" + ] + ); + // These source contracts cover cfg(not(test)) workers too, without making + // live SSH calls or putting any workspace/session content in timing labels. + for (source, operations) in [ + (include_str!("cloud_git.rs"), &["GitProbe"][..]), + ( + include_str!("cloud_status.rs"), + &["CloudStatus", "CloudStatusDirectories"][..], + ), + ( + include_str!("session_picker/cloud_preview.rs"), + &["Preview"][..], + ), + ( + include_str!("session_picker/loading/mobile_inventory.rs"), + &["InventoryPrepare", "InventoryProjection"][..], + ), + ] { + let boundary = if operations == ["Preview"] { + "fn parse_preview(" + } else { + "#[cfg(test)]" + }; + let production = source.split(boundary).next().unwrap(); + assert!(!production.contains("bounded_output(")); + for operation in operations { + assert!(production.contains(&format!("WorkspaceOperation::{operation}"))); + } + } +} +use std::time::Duration; + +const SAFE: &str = "[CLOUD_TIMING] stage=projection outcome=success duration_ms=17"; + +#[test] +fn exact_whitelist_rejects_auth_content_and_injection() { + assert_eq!(timing_record(SAFE.as_bytes()).as_deref(), Some(SAFE)); + for line in [ + "Authorization: Bearer SECRET".to_string(), + SAFE.replace("projection", "secret-session-id"), + SAFE.replace("success", "private-error"), + format!("{SAFE} token=SECRET"), + format!("{SAFE}\r"), + format!(" {SAFE}"), + SAFE.replace("17", "NaN"), + SAFE.replace("17", "-1"), + SAFE.replace("17", "1.2"), + SAFE.replace("17", "18446744073709551616"), + SAFE.replace("17", "17\u{1b}[31m"), + ] { + assert!(timing_record(line.as_bytes()).is_none()); + } + assert!(timing_record(b"\xff").is_none()); +} + +#[tokio::test] +async fn oversized_lines_and_stream_have_fixed_log_and_scan_budgets() { + let mut input = vec![b'x'; MAX_LINE + 1]; + input.extend_from_slice(SAFE.as_bytes()); // A suffix must not masquerade as a line. + input.push(b'\n'); + input.extend_from_slice(SAFE.as_bytes()); + input.push(b'\n'); + let mut records = Vec::new(); + drain(input.as_slice(), |record| records.push(record)).await; + assert_eq!(records, [SAFE]); + let input = format!("{SAFE}\n").repeat(RECORD_BUDGET * 100); + records.clear(); + drain(input.as_bytes(), |record| records.push(record)).await; + assert_eq!(records.len(), RECORD_BUDGET); + let mut input = vec![b'x'; SCAN_BUDGET]; + input.push(b'\n'); + input.extend_from_slice(format!("{SAFE}\n").as_bytes()); + records.clear(); + drain(input.as_slice(), |record| records.push(record)).await; + assert!(records.is_empty()); +} + +#[tokio::test] +async fn split_records_and_incomplete_eof_do_not_leak() { + use tokio::io::AsyncWriteExt; + let (mut writer, reader) = tokio::io::duplex(8); + let producer = async { + writer + .write_all(format!("SECRET\n{SAFE}\n{SAFE}").as_bytes()) + .await + .unwrap(); + drop(writer); + }; + let mut records = Vec::new(); + tokio::join!(producer, drain(reader, |record| records.push(record))); + assert_eq!(records, [SAFE]); +} + +#[tokio::test] +async fn concurrent_large_stderr_cannot_deadlock_stdout_or_hide_exit_failure() { + for exit in [0, 7] { + let script = format!( + "head -c 2097152 /dev/zero >&2; printf '%s\\n' '{SAFE}' >&2; printf payload; exit {exit}" + ); + let command = script_command(None, None, "unused", &script); + let (status, bytes) = bounded_output(command, Duration::from_secs(5), 64) + .await + .unwrap(); + assert_eq!(status.code(), Some(exit)); + assert_eq!(bytes, b"payload"); + } +} + +#[tokio::test] +async fn oversized_stdout_cancels_infinite_stderr_without_waiting_for_deadline() { + let command = script_command( + None, + None, + "unused", + "(while :; do printf secret >&2; done) & head -c 65 /dev/zero; wait", + ); + let started = std::time::Instant::now(); + assert!( + bounded_output(command, Duration::from_secs(10), 64) + .await + .is_none() + ); + assert!(started.elapsed() < Duration::from_secs(2)); +} + +#[cfg(target_os = "linux")] +async fn assert_processes_stopped(path: &std::path::Path) { + let pids = std::fs::read_to_string(path).unwrap(); + for _ in 0..100 { + let alive = pids.split_whitespace().any(|pid| { + std::fs::read_to_string(format!("/proc/{pid}/stat")) + .ok() + .and_then(|stat| { + stat.rsplit_once(") ") + .map(|(_, rest)| !rest.starts_with('Z')) + }) + .unwrap_or(false) + }); + if !alive { + return; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + panic!("command process group survived cancellation"); +} + +#[cfg(target_os = "linux")] +#[tokio::test] +async fn deadline_and_cancellation_kill_pipe_holding_descendants() { + for cancel in [false, true] { + let temp = tempfile::tempdir().unwrap(); + let pids = temp.path().join("pids"); + let command = script_command( + None, + None, + pids.to_str().unwrap(), + "sleep 30 & printf '%s %s' \"$$\" \"$!\" > \"$1\"; wait", + ); + let task = tokio::spawn(bounded_output(command, Duration::from_millis(500), 64)); + tokio::time::timeout(Duration::from_secs(2), async { + loop { + if std::fs::read_to_string(&pids).is_ok_and(|s| s.split_whitespace().count() == 2) { + break; + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + }) + .await + .unwrap(); + if cancel { + task.abort(); + assert!(task.await.unwrap_err().is_cancelled()); + } else { + assert!(task.await.unwrap().is_none()); + } + assert_processes_stopped(&pids).await; + } +} diff --git a/crates/jcode-tui/src/tui/cloud_latency.rs b/crates/jcode-tui/src/tui/cloud_latency.rs new file mode 100644 index 00000000000..64746bfe8ae --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_latency.rs @@ -0,0 +1,256 @@ +//! Content-free client-side cloud latency milestones. +use crate::protocol::{Request, ServerEvent}; +use std::cell::RefCell; +use std::collections::VecDeque; +use std::sync::Mutex; +use std::time::Instant; + +#[cfg(test)] +#[path = "cloud_latency_tests.rs"] +mod tests; + +const MAX_PENDING: usize = 64; +thread_local! { + static PAINT: RefCell> = const { RefCell::new(None) }; +} + +// Async connection futures can migrate between executor threads. Keep request +// correlation on the connection, not the thread or a process-global request map. +#[derive(Default)] +pub(super) struct RequestTimings { + pending: Mutex>, +} +struct PendingRequest { + id: u64, + operation: &'static str, + started: Instant, + history_expected: bool, + todos_seen: bool, +} + +pub(super) struct Span { + stage: &'static str, + started: Instant, + outcome: &'static str, + operation: &'static str, + request_id: Option, +} +impl Span { + pub(super) fn new(stage: &'static str) -> Self { + Self { + stage, + started: Instant::now(), + outcome: "error_or_cancelled", + operation: "none", + request_id: None, + } + } + pub(super) fn operation(&mut self, operation: &'static str) { + self.operation = operation; + } + pub(super) fn finish(&mut self, outcome: &'static str) { + self.outcome = outcome; + } +} +impl Drop for Span { + fn drop(&mut self) { + crate::logging::info(&format!( + "[CLOUD_TIMING] stage={} operation={} request_id={:?} outcome={} duration_ms={:.3}", + self.stage, + self.operation, + self.request_id, + self.outcome, + self.started.elapsed().as_secs_f64() * 1000.0 + )); + } +} + +fn request_key(request: &Request) -> Option<(u64, &'static str)> { + match request { + Request::Subscribe { id, .. } => Some((*id, "subscribe")), + Request::ResumeSession { id, .. } => Some((*id, "resume")), + Request::GetHistory { id } => Some((*id, "history")), + Request::GetCompactedHistory { id, .. } => Some((*id, "history_page")), + Request::GetTodos { id } => Some((*id, "todos")), + _ => None, + } +} + +pub(super) fn begin_request(request: &Request) -> Option { + request_key(request).map(|(id, kind)| { + let mut span = Span::new("request_write"); + span.operation = kind; + span.request_id = Some(id); + span + }) +} + +impl RequestTimings { + pub(super) fn request_sent(&self, request: &Request) { + let Some((id, kind)) = request_key(request) else { + return; + }; + let mut pending = self + .pending + .lock() + .unwrap_or_else(|error| error.into_inner()); + pending.retain(|previous| { + if previous.started.elapsed().as_secs() >= 120 { + crate::logging::info(&format!("[CLOUD_TIMING] stage=response_wait operation={} request_id={} outcome=expired duration_ms={:.3}", previous.operation, previous.id, previous.started.elapsed().as_secs_f64() * 1000.0)); + false + } else { previous.id != id } + }); + if pending.len() == MAX_PENDING { + pending.pop_front(); + } + pending.push_back(PendingRequest { + id, + operation: kind, + started: Instant::now(), + // Fresh Subscribe completes with Done, followed by a separately + // requested History. A target-aware attach returns History itself. + history_expected: !matches!( + request, + Request::Subscribe { + target_session_id: None, + .. + } + ), + todos_seen: false, + }); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=request_sent operation={kind} request_id={id} outcome=ok duration_ms=0" + )); + } + + fn response_wait(&self, event: &ServerEvent) -> Option<(&'static str, u64, f64)> { + if !matches!( + event, + ServerEvent::Todos { .. } + | ServerEvent::History { .. } + | ServerEvent::CompactedHistory { .. } + | ServerEvent::Error { .. } + | ServerEvent::Done { .. } + ) { + return None; + } + let mut pending = self + .pending + .lock() + .unwrap_or_else(|error| error.into_inner()); + let index = match event { + ServerEvent::Todos { .. } => { + // Explicit polls take priority over an attach's early snapshot. + // A bootstrap snapshot must not consume History correlation or + // keep matching every unsolicited snapshot for that connection. + pending + .iter() + .position(|request| request.operation == "todos") + .or_else(|| { + pending.iter().position(|request| { + !request.todos_seen + && matches!(request.operation, "subscribe" | "resume") + }) + })? + } + ServerEvent::History { id, .. } + | ServerEvent::CompactedHistory { id, .. } + | ServerEvent::Error { id, .. } => { + pending.iter().position(|request| request.id == *id)? + } + ServerEvent::Done { id } => pending.iter().position(|request| { + request.id == *id && request.operation == "subscribe" && !request.history_expected + })?, + _ => return None, + }; + let request = &mut pending[index]; + let result = ( + request.operation, + request.id, + request.started.elapsed().as_secs_f64() * 1000.0, + ); + if matches!(event, ServerEvent::Todos { .. }) && request.operation != "todos" { + request.todos_seen = true; + } else { + pending.remove(index); + } + Some(result) + } + + pub(super) fn parse_event(&self, text: &str) -> serde_json::Result { + let started = Instant::now(); + let result = serde_json::from_str(text); + let (key, wait) = match &result { + Ok(event) => (event_key(event), self.response_wait(event)), + Err(_) => (None, None), + }; + if key.is_some() + || wait.is_some() + || result.is_err() + || started.elapsed().as_millis() >= 100 + { + let (kind, id) = key + .or_else(|| wait.map(|(operation, id, _)| (operation, Some(id)))) + .unwrap_or(("other", None)); + let failed = result.is_err() || matches!(&result, Ok(ServerEvent::Error { .. })); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=event_parse operation={kind} request_id={id:?} outcome={} duration_ms={:.3} bytes={} request_wait_ms={}", + if failed { "error" } else { "ok" }, + started.elapsed().as_secs_f64() * 1000.0, + text.len(), + wait.map(|(_, _, ms)| format!("{ms:.3}")) + .unwrap_or_else(|| "none".into()) + )); + } + result + } +} + +pub(super) fn event_key(event: &ServerEvent) -> Option<(&'static str, Option)> { + match event { + ServerEvent::History { id, .. } => Some(("history", Some(*id))), + ServerEvent::CompactedHistory { id, .. } => Some(("history_page", Some(*id))), + ServerEvent::Todos { .. } => Some(("todos", None)), + _ => None, + } +} + +pub(super) fn applied(kind: &'static str, started: Instant, redraw: bool) { + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=event_apply operation={kind} outcome=ok duration_ms={:.3} redraw={redraw}", + started.elapsed().as_secs_f64() * 1000.0 + )); + if redraw { + PAINT.with_borrow_mut(|pending| { + if pending.is_none() { + *pending = Some((kind, Instant::now())); + } + }); + } +} + +pub(super) fn rendered() { + PAINT.with_borrow_mut(|pending| { + if let Some((kind, at)) = pending.take() { + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=frame_ready operation={kind} outcome=ok duration_ms={:.3}", + at.elapsed().as_secs_f64() * 1000.0 + )); + } + }); +} + +pub(super) fn spawn_usage_hydration( + hydrate: impl FnOnce() -> bool + Send + 'static, +) -> tokio::task::JoinHandle<()> { + let queued = Instant::now(); + tokio::task::spawn_blocking(move || { + let started = Instant::now(); + let imported = hydrate(); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=quota_hydration outcome=ok duration_ms={:.3} queue_ms={:.3} imported={imported}", + started.elapsed().as_secs_f64() * 1000.0, + started.duration_since(queued).as_secs_f64() * 1000.0, + )); + }) +} diff --git a/crates/jcode-tui/src/tui/cloud_latency_tests.rs b/crates/jcode-tui/src/tui/cloud_latency_tests.rs new file mode 100644 index 00000000000..bbe6fbf047f --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_latency_tests.rs @@ -0,0 +1,150 @@ +use super::*; + +fn subscribe(id: u64, target: Option<&str>) -> Request { + serde_json::from_value(serde_json::json!({ + "type": "subscribe", "id": id, "target_session_id": target, + })) + .unwrap() +} + +fn todo_event() -> ServerEvent { + ServerEvent::Todos { + session_id: "fixture".into(), + payload: "private transcript must not be logged".into(), + } +} + +#[test] +fn cloud_latency_pending_requests_are_bounded_and_responses_remove_them() { + let timings = RequestTimings::default(); + for id in 0..100 { + timings.request_sent(&Request::GetHistory { id }); + } + { + let pending = timings.pending.lock().unwrap(); + assert_eq!(pending.len(), MAX_PENDING); + assert_eq!(pending.front().map(|entry| entry.id), Some(36)); + } + let event = timings.parse_event(r#"{"type":"compacted_history","id":99,"session_id":"fixture","messages":[],"images":[],"compacted_total":0,"compacted_visible":0,"compacted_remaining":0,"compacted_hidden_prompts":0}"#).unwrap(); + assert_eq!(event_key(&event), Some(("history_page", Some(99)))); + assert!( + !timings + .pending + .lock() + .unwrap() + .iter() + .any(|entry| entry.id == 99) + ); +} + +#[test] +fn cloud_latency_todos_preserve_attach_correlation_and_payload() { + let timings = RequestTimings::default(); + timings.request_sent(&subscribe(42, Some("fixture"))); + let event = timings.parse_event(r#"{"type":"todos","session_id":"fixture","payload":"private transcript must not be logged"}"#).unwrap(); + assert_eq!(event_key(&event), Some(("todos", None))); + assert_eq!(timings.pending.lock().unwrap().len(), 1); + assert!( + matches!(event, ServerEvent::Todos { payload, .. } if payload == "private transcript must not be logged") + ); + // Repeated unsolicited snapshots are not additional bootstrap responses. + assert!(timings.response_wait(&todo_event()).is_none()); + assert!(timings.parse_event("not json").is_err()); +} + +#[test] +fn cloud_latency_polls_take_priority_over_attach_and_unsolicited_todos() { + let timings = RequestTimings::default(); + timings.request_sent(&subscribe(42, Some("fixture"))); + timings.request_sent(&Request::GetTodos { id: 43 }); + assert_eq!( + timings.response_wait(&todo_event()).map(|entry| entry.1), + Some(43) + ); + assert_eq!( + timings.response_wait(&todo_event()).map(|entry| entry.1), + Some(42) + ); + assert!(timings.response_wait(&todo_event()).is_none()); + let pending = timings.pending.lock().unwrap(); + assert_eq!(pending.len(), 1); + assert_eq!(pending[0].id, 42); +} + +#[test] +fn cloud_latency_fresh_subscribe_done_does_not_expire_waiting_for_history() { + let timings = RequestTimings::default(); + timings.request_sent(&subscribe(10, None)); + timings.request_sent(&Request::GetHistory { id: 11 }); + let event = timings.parse_event(r#"{"type":"done","id":10}"#).unwrap(); + assert!(matches!(event, ServerEvent::Done { id: 10 })); + let pending = timings.pending.lock().unwrap(); + assert_eq!(pending.len(), 1); + assert_eq!(pending[0].id, 11); +} + +#[test] +fn cloud_latency_target_subscribe_done_retains_history_and_error_clears_it() { + let timings = RequestTimings::default(); + timings.request_sent(&subscribe(10, Some("fixture"))); + assert!( + timings + .response_wait(&ServerEvent::Done { id: 10 }) + .is_none() + ); + assert_eq!(timings.pending.lock().unwrap().len(), 1); + let event = timings + .parse_event(r#"{"type":"error","id":10,"message":"PRIVATE"}"#) + .unwrap(); + assert!(matches!(event, ServerEvent::Error { id: 10, .. })); + assert!(timings.pending.lock().unwrap().is_empty()); +} + +#[test] +fn cloud_latency_correlation_survives_thread_migration_and_isolates_connections() { + let first = std::sync::Arc::new(RequestTimings::default()); + let second = RequestTimings::default(); + let sender = first.clone(); + std::thread::spawn(move || sender.request_sent(&Request::GetTodos { id: 1 })) + .join() + .unwrap(); + assert!(second.response_wait(&todo_event()).is_none()); + second.request_sent(&Request::GetTodos { id: 1 }); + let receiver = first.clone(); + let response = std::thread::spawn(move || receiver.response_wait(&todo_event())) + .join() + .unwrap(); + assert_eq!(response.map(|entry| entry.1), Some(1)); + assert!(first.pending.lock().unwrap().is_empty()); + assert_eq!(second.pending.lock().unwrap().len(), 1); + assert_eq!( + second.response_wait(&todo_event()).map(|entry| entry.1), + Some(1) + ); +} + +#[test] +fn cloud_latency_expiry_remains_bounded_and_reused_ids_replace_old_requests() { + let timings = RequestTimings::default(); + timings.request_sent(&Request::GetTodos { id: 1 }); + timings.pending.lock().unwrap()[0].started = + Instant::now() - std::time::Duration::from_secs(121); + timings.request_sent(&Request::GetHistory { id: 2 }); + timings.request_sent(&Request::GetTodos { id: 2 }); + let pending = timings.pending.lock().unwrap(); + assert_eq!(pending.len(), 1); + assert_eq!(pending[0].id, 2); + assert_eq!(pending[0].operation, "todos"); +} + +#[test] +fn cloud_latency_paint_milestone_is_consumed_once() { + PAINT.with_borrow_mut(|pending| *pending = None); + applied("history", Instant::now(), false); + PAINT.with_borrow(|pending| assert!(pending.is_none())); + applied("todos", Instant::now(), true); + applied("history", Instant::now(), true); + PAINT.with_borrow(|pending| assert_eq!(pending.as_ref().map(|entry| entry.0), Some("todos"))); + rendered(); + PAINT.with_borrow(|pending| assert!(pending.is_none())); +} diff --git a/crates/jcode-tui/src/tui/cloud_status.rs b/crates/jcode-tui/src/tui/cloud_status.rs index 7f3b84287d3..29a910237c2 100644 --- a/crates/jcode-tui/src/tui/cloud_status.rs +++ b/crates/jcode-tui/src/tui/cloud_status.rs @@ -265,7 +265,7 @@ pub(crate) fn gather(session_id: Option) -> Option { let mut previous = None; loop { let start = std::time::Instant::now(); - let response = super::cloud_git::bounded_output( + let response = super::cloud_git::bounded_output_for( super::cloud_git::script_command( Some(&host), config.as_deref(), @@ -274,6 +274,7 @@ pub(crate) fn gather(session_id: Option) -> Option { ), Duration::from_secs(12), 256 * 1024, + super::cloud_git::WorkspaceOperation::CloudStatus, ) .await; let parsed = response @@ -293,7 +294,7 @@ pub(crate) fn gather(session_id: Option) -> Option { .lock() .unwrap_or_else(|e| e.into_inner()) .clone(); - let directory_response = super::cloud_git::bounded_output( + let directory_response = super::cloud_git::bounded_output_for( super::cloud_git::script_command( Some(&host), config.as_deref(), @@ -302,6 +303,7 @@ pub(crate) fn gather(session_id: Option) -> Option { ), Duration::from_secs(8), 1024 * 1024, + super::cloud_git::WorkspaceOperation::CloudStatusDirectories, ) .await; let directories = directory_response @@ -312,7 +314,7 @@ pub(crate) fn gather(session_id: Option) -> Option { directories, ); crate::bus::Bus::global().publish(crate::bus::BusEvent::GitInfoRefreshed); - tokio::time::sleep(Duration::from_secs(5)).await; + super::cloud_git::wait_for_next_probe().await; } }); }); @@ -326,6 +328,10 @@ pub(crate) fn gather(_session_id: Option) -> Option { None } +#[cfg(test)] +#[path = "cloud_directories_probe_tests.rs"] +mod directories_probe_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/crates/jcode-tui/src/tui/info_widget.rs b/crates/jcode-tui/src/tui/info_widget.rs index 7e7c635f833..037fcecbb06 100644 --- a/crates/jcode-tui/src/tui/info_widget.rs +++ b/crates/jcode-tui/src/tui/info_widget.rs @@ -1020,9 +1020,17 @@ impl Default for WidgetsState { /// Global widget state (for polling across frames) static WIDGETS_STATE: Mutex> = Mutex::new(None); +/// Separate widget state for secondary renders (resume preview) so their +/// placements and animations never disturb the live session's widgets. +static SECONDARY_WIDGETS_STATE: Mutex> = Mutex::new(None); fn get_or_init_state() -> std::sync::MutexGuard<'static, Option> { - let mut guard = WIDGETS_STATE.lock().unwrap_or_else(|e| e.into_inner()); + let state = if super::ui::secondary_render_active() { + &SECONDARY_WIDGETS_STATE + } else { + &WIDGETS_STATE + }; + let mut guard = state.lock().unwrap_or_else(|e| e.into_inner()); if guard.is_none() { *guard = Some(WidgetsState::default()); } diff --git a/crates/jcode-tui/src/tui/justrust_status.rs b/crates/jcode-tui/src/tui/justrust_status.rs index 1f498c24d32..1ee9846195e 100644 --- a/crates/jcode-tui/src/tui/justrust_status.rs +++ b/crates/jcode-tui/src/tui/justrust_status.rs @@ -4,13 +4,12 @@ use serde::Deserialize; use std::path::Path; +use std::sync::Arc; use std::time::{Duration, Instant}; -use tokio::io::AsyncReadExt; -use tokio::sync::oneshot; +use tokio::io::AsyncReadExt as _; -const POLL_INTERVAL: Duration = Duration::from_secs(1); const RETRY_INTERVAL: Duration = Duration::from_secs(10); -const QUERY_TIMEOUT: Duration = Duration::from_secs(2); +const FIRST_LINE_TIMEOUT: Duration = Duration::from_secs(5); const MAX_RESPONSE: usize = 512 * 1024; const COMPLETION_RETENTION: Duration = Duration::from_secs(10 * 60); @@ -236,64 +235,82 @@ impl Snapshot { } } -async fn query(program: &std::ffi::OsStr) -> Option { - query_with_timeout(program, QUERY_TIMEOUT).await +/// One long-lived `justrust status --json --watch` per client. justrust +/// prints a line only when the status changes and sleeps on inotify while no +/// build runs, so an idle client costs nothing (a one-shot query per second +/// used to spawn a process every second in every window). +fn spawn_stream( + program: std::ffi::OsString, +) -> tokio::sync::watch::Receiver>>> { + let (sender, receiver) = tokio::sync::watch::channel(None); + if let Ok(runtime) = tokio::runtime::Handle::try_current() { + runtime.spawn(async move { + if let Err(error) = stream(&program, &sender).await { + crate::logging::debug(&format!("Build status stream ended: {error:#}")); + } + // Some(None) tells the poller the stream is gone. + if sender.send(Some(None)).is_err() { + crate::logging::debug("Build status receiver closed"); + } + }); + } + receiver } -async fn query_with_timeout(program: &std::ffi::OsStr, timeout: Duration) -> Option { +async fn stream( + program: &std::ffi::OsStr, + sender: &tokio::sync::watch::Sender>>>, +) -> anyhow::Result<()> { use anyhow::Context; - let result = tokio::time::timeout(timeout, async { - let mut child = tokio::process::Command::new(program) - .args(["status", "--json"]) - .stdin(std::process::Stdio::null()) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::null()) - .kill_on_drop(true) - .spawn() - .context("start justrust status query")?; - let mut bytes = Vec::new(); - child - .stdout - .take() - .context("justrust status stdout was not piped")? - .take((MAX_RESPONSE + 1) as u64) - .read_to_end(&mut bytes) - .await - .context("read justrust status response")?; - if bytes.len() > MAX_RESPONSE { - child - .kill() - .await - .context("terminate oversized justrust status response")?; - anyhow::bail!("justrust status response exceeds {MAX_RESPONSE} bytes"); - } - let status = child - .wait() - .await - .context("wait for justrust status query")?; - anyhow::ensure!(status.success(), "justrust status exited with {status}"); - serde_json::from_slice(&bytes).context("decode justrust status response") - }) - .await; - match result { - Ok(Ok(snapshot)) => Some(snapshot), - Ok(Err(error)) => { - crate::logging::debug(&format!("Build status unavailable: {error:#}")); - None - } - Err(error) => { - crate::logging::debug(&format!("Build status query timed out: {error}")); - None + use tokio::io::AsyncBufReadExt; + let mut child = tokio::process::Command::new(program) + .args(["status", "--json", "--watch", "1"]) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::null()) + .kill_on_drop(true) + .spawn() + .context("start justrust status stream")?; + let stdout = child.stdout.take().context("stdout was not piped")?; + let mut reader = tokio::io::BufReader::new(stdout); + let mut line = Vec::new(); + let mut first = true; + loop { + line.clear(); + let limit = if first { + FIRST_LINE_TIMEOUT + } else { + Duration::from_secs(365 * 24 * 3600) + }; + let mut limited = (&mut reader).take((MAX_RESPONSE + 1) as u64); + let next = limited.read_until(b'\n', &mut line); + let read = tokio::select! { + read = tokio::time::timeout(limit, next) => { + read.context("first status line timed out")? + } + () = sender.closed() => return Ok(()), + }; + first = false; + let n = read.context("read justrust status stream")?; + anyhow::ensure!(n > 0, "justrust status stream closed"); + anyhow::ensure!( + line.ends_with(b"\n") || n <= MAX_RESPONSE, + "justrust status line exceeds {MAX_RESPONSE} bytes" + ); + let snapshot: Snapshot = + serde_json::from_slice(&line).context("decode justrust status line")?; + if sender.send(Some(Some(Arc::new(snapshot)))).is_err() { + return Ok(()); } } } #[derive(Default)] pub(crate) struct Poller { - pending: Option>>, - snapshot: Option, + stream: Option>>>>, + snapshot: Option>, received: Option, - next_poll: Option, + next_start: Option, row: Option, last_finished: Option<(Finished, Instant)>, } @@ -315,45 +332,42 @@ impl Poller { self.row = None; self.snapshot = None; self.last_finished = None; - self.pending = None; + self.stream = None; return self.row != previous; } let now = Instant::now(); - if let Some(receiver) = &mut self.pending { - match receiver.try_recv() { - Ok(snapshot) => { - let retry = if snapshot.is_some() { - POLL_INTERVAL - } else { - RETRY_INTERVAL - }; - if let Some(run) = snapshot - .as_ref() - .and_then(|snapshot| snapshot.recent.as_ref()) + let mut ended = false; + if let Some(receiver) = &mut self.stream + && receiver.has_changed().unwrap_or(true) + { + match receiver.borrow_and_update().clone() { + Some(Some(snapshot)) => { + if let Some(run) = snapshot.recent.as_ref() && relevance(run.agent_session.as_deref(), &run.cwd, session, repo) > 0 { self.last_finished = Some((run.clone(), now)); } - self.snapshot = snapshot; + self.snapshot = Some(snapshot); self.received = Some(now); - self.pending = None; - self.next_poll = Some(now + retry); - } - Err(oneshot::error::TryRecvError::Closed) => { - self.pending = None; - self.snapshot = None; - self.next_poll = Some(now + RETRY_INTERVAL); } - Err(oneshot::error::TryRecvError::Empty) => {} + Some(None) => ended = true, + None => {} } } + if ended { + self.stream = None; + self.snapshot = None; + self.next_start = Some(now + RETRY_INTERVAL); + } + // A live stream's snapshot stays current until the next line: an + // active build prints every second, an idle one never needs to. self.row = self.snapshot.as_ref().and_then(|snapshot| { let age = self .received .map_or(Duration::ZERO, |at| now.saturating_duration_since(at)); - (age < Duration::from_secs(5)) - .then(|| snapshot.row(session, repo, age)) - .flatten() + let fresh = !snapshot.active.is_empty() && age < Duration::from_secs(5) + || snapshot.active.is_empty(); + fresh.then(|| snapshot.row(session, repo, age)).flatten() }); if self.row.is_none() && let Some((run, received)) = &self.last_finished @@ -364,20 +378,12 @@ impl Poller { } .row(session, repo, now.saturating_duration_since(*received)); } - if self.pending.is_none() - && self.next_poll.is_none_or(|next| now >= next) - && let Ok(runtime) = tokio::runtime::Handle::try_current() + if self.stream.is_none() + && self.next_start.is_none_or(|next| now >= next) + && tokio::runtime::Handle::try_current().is_ok() { - let (sender, receiver) = oneshot::channel(); - self.pending = Some(receiver); - runtime.spawn(async move { - if sender - .send(query(std::ffi::OsStr::new("justrust")).await) - .is_err() - { - crate::logging::debug("Discarded build status after poller was closed"); - } - }); + self.stream = Some(spawn_stream("justrust".into())); + self.next_start = Some(now + RETRY_INTERVAL); } self.row != previous } @@ -529,19 +535,29 @@ mod tests { assert_eq!(clean("evil\n\u{1b}[31m"), "evil[31m"); } + fn feed(json: &str) -> tokio::sync::watch::Receiver>>> { + let snapshot = serde_json::from_str::(json).unwrap(); + let (sender, receiver) = tokio::sync::watch::channel(Some(Some(Arc::new(snapshot)))); + std::mem::forget(sender); + receiver + } + #[test] fn matching_completion_survives_unrelated_updates_and_expires() { - let (sender, receiver) = oneshot::channel(); - sender.send(Some(serde_json::from_str::(r#"{"recent":{"cwd":"/repo/app","args":["test"],"wall":51,"ok":true,"ago":1,"agent_session":"ours"}}"#).unwrap())).unwrap(); let mut poller = Poller { - pending: Some(receiver), + stream: Some(feed( + r#"{"recent":{"cwd":"/repo/app","args":["test"],"wall":51,"ok":true,"ago":1,"agent_session":"ours"}}"#, + )), ..Default::default() }; + poller.stream.as_mut().unwrap().mark_changed(); poller.refresh(Some("ours"), Some("/repo/app"), true); assert!(poller.row().unwrap().text.contains("finished in 51s")); - let (sender, receiver) = oneshot::channel(); - sender.send(Some(serde_json::from_str::(r#"{"active":[{"cwd":"/other"}],"recent":{"cwd":"/other","wall":1,"ok":false,"ago":0}}"#).unwrap())).unwrap(); - poller.pending = Some(receiver); + let mut next = feed( + r#"{"active":[{"cwd":"/other"}],"recent":{"cwd":"/other","wall":1,"ok":false,"ago":0}}"#, + ); + next.mark_changed(); + poller.stream = Some(next); poller.refresh(Some("ours"), Some("/repo/app"), true); assert!(poller.row().unwrap().text.contains("finished in 51s")); poller.snapshot = None; @@ -555,6 +571,22 @@ mod tests { assert!(poller.row().is_none()); } + #[test] + fn idle_snapshot_stays_current_without_new_lines() { + let mut poller = Poller { + stream: Some(feed( + r#"{"active":[],"recent":{"cwd":"/repo/app","args":["test"],"wall":3,"ok":true,"ago":1,"agent_session":"ours"}}"#, + )), + ..Default::default() + }; + poller.stream.as_mut().unwrap().mark_changed(); + poller.refresh(Some("ours"), Some("/repo/app"), true); + poller.received = Some(Instant::now() - Duration::from_secs(30)); + poller.last_finished = None; + poller.refresh(Some("ours"), Some("/repo/app"), true); + assert!(poller.row().unwrap().text.contains("finished in 3s")); + } + #[test] fn disabled_poller_clears_cached_builds_without_starting_queries() { let mut poller = Poller { @@ -567,21 +599,26 @@ mod tests { }; assert!(poller.refresh(None, None, false)); assert!(poller.row().is_none()); - assert!(poller.pending.is_none()); + assert!(poller.stream.is_none()); + } + + async fn first(program: &std::path::Path) -> Option>> { + let mut receiver = spawn_stream(program.as_os_str().to_owned()); + receiver.changed().await.ok()?; + receiver.borrow().clone() } #[tokio::test] async fn missing_executable_is_optional() { - assert!( - query(std::ffi::OsStr::new("/nonexistent/justrust-status-test")) - .await - .is_none() - ); + assert!(matches!( + first(std::path::Path::new("/nonexistent/justrust-status-test")).await, + Some(None) | None + )); } #[cfg(unix)] #[tokio::test] - async fn status_subprocess_is_bounded_and_checks_exit_status() { + async fn status_stream_is_bounded_and_streams_lines() { use std::os::unix::fs::PermissionsExt; let dir = tempfile::tempdir().unwrap(); let program = dir.path().join("status"); @@ -590,27 +627,36 @@ mod tests { std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap(); }; write( - "test \"$1\" = status && test \"$2\" = --json || exit 9\nprintf '%s' '{\"active\":[],\"recent\":null}'", + "test \"$1 $2 $3\" = 'status --json --watch' || exit 9\necho '{\"active\":[],\"recent\":null}'\nsleep 0.2\necho '{\"active\":[{\"cwd\":\"/x\"}]}'\nsleep 10", ); - assert!(query(program.as_os_str()).await.is_some()); - write("printf '%s' '{\"active\":[]}'\nexit 1"); - assert!(query(program.as_os_str()).await.is_none()); - write("printf '%s' 'not json'"); - assert!(query(program.as_os_str()).await.is_none()); - write("exec head -c 524289 /dev/zero"); - assert!(query(program.as_os_str()).await.is_none()); - write("exec sleep 10"); - // Exercise the real subprocess cancellation path without making every - // filtered test run wait for the production two-second deadline. - assert_eq!(QUERY_TIMEOUT, Duration::from_secs(2)); - let timeout = Duration::from_millis(50); - let start = Instant::now(); + let mut receiver = spawn_stream(program.as_os_str().to_owned()); + receiver.changed().await.unwrap(); assert!( - query_with_timeout(program.as_os_str(), timeout) - .await - .is_none() + receiver + .borrow_and_update() + .clone() + .unwrap() + .unwrap() + .active + .is_empty() ); - assert!(start.elapsed() >= timeout); - assert!(start.elapsed() < Duration::from_secs(5)); + receiver.changed().await.unwrap(); + assert_eq!( + receiver + .borrow_and_update() + .clone() + .unwrap() + .unwrap() + .active + .len(), + 1 + ); + drop(receiver); + write("echo 'not json'"); + assert!(matches!(first(&program).await, Some(None))); + write("exec head -c 524289 /dev/zero"); + assert!(matches!(first(&program).await, Some(None))); + write("exit 1"); + assert!(matches!(first(&program).await, Some(None))); } } diff --git a/crates/jcode-tui/src/tui/mod.rs b/crates/jcode-tui/src/tui/mod.rs index 5ae53bb036f..9ac3fed1d50 100644 --- a/crates/jcode-tui/src/tui/mod.rs +++ b/crates/jcode-tui/src/tui/mod.rs @@ -24,13 +24,13 @@ pub struct BackgroundTaskRow { pub label: String, pub percent: Option, pub status: BackgroundTaskRowStatus, - /// When a successful task stopped being actionable. Running and failed - /// tasks remain visible until their state changes or the session closes. + /// When a successful task stopped being actionable, retained until session closure. pub completed_at: Option, } pub mod backend; mod cloud_git; +mod cloud_latency; mod cloud_status; pub(crate) mod color_support; mod core; @@ -60,9 +60,9 @@ mod redraw_schedule; #[allow(unused_imports)] pub(crate) use redraw_schedule::{ REDRAW_DEEP_IDLE, REDRAW_DEEP_IDLE_AFTER, REDRAW_IDLE, REDRAW_PASSIVE_LIVENESS, - REDRAW_REMOTE_STARTUP, REDRAW_SWARM_SPINNER, current_full_frame_redraw_reason, - idle_donut_active, last_full_frame_redraw_reason, periodic_redraw_required, - periodic_redraw_required_excluding_idle_animation, redraw_interval, + REDRAW_REMOTE_STARTUP, REDRAW_STATIC_STREAM, REDRAW_SWARM_SPINNER, + current_full_frame_redraw_reason, idle_donut_active, last_full_frame_redraw_reason, + periodic_redraw_required, periodic_redraw_required_excluding_idle_animation, redraw_interval, redraw_interval_with_policy, }; mod remote_diff; @@ -70,6 +70,7 @@ pub mod screenshot; pub(crate) mod session_facts; pub mod session_picker; mod stream_buffer; +mod terminal_events; pub mod terminal_setup; pub mod test_harness; pub mod theme_detect; @@ -932,6 +933,10 @@ pub trait TuiState { fn voice_input_status(&self) -> Option<(bool, String)> { None } + /// Voice input rendered in the composer instead of the status line. + fn voice_composer_preview(&self) -> Option { + None + } /// How long since the user last pressed a key, scrolled, or pasted, or /// `None` when they have not interacted yet. /// @@ -962,6 +967,15 @@ pub trait TuiState { fn has_pending_mouse_scroll_animation(&self) -> bool { false } + /// Whether the paced stream buffer still holds text that later ticks will + /// reveal. While streamed text sits on screen with no backlog (e.g. the + /// model is thinking or running a tool after emitting text), nothing in the + /// transcript changes per tick, so the animation cadence is pure waste. + /// Defaults to true so state impls that do not track a backlog keep the + /// conservative fast cadence. + fn stream_reveal_pending(&self) -> bool { + true + } /// Optional configured keybinding label for external dictation. fn dictation_key_label(&self) -> Option; /// Time since app started (for startup animations) @@ -1166,6 +1180,11 @@ pub trait TuiState { fn git_branch(&self) -> Option { None } + /// Remote machine this session runs on (SSH host or cloud VM), shown on + /// the status line. Embedded views override it with their own host. + fn remote_machine(&self) -> Option { + ssh_remote_host() + } /// Monotonic clock for viewport animations fn now_millis(&self) -> u64; /// UI state for live copy badge highlighting / feedback @@ -1405,6 +1424,19 @@ pub enum PickerKind { Usage, } +/// Live voice input shown inside the composer: a waveform row above the +/// prompt and the in-progress transcript as ghost text after the cursor. +#[derive(Debug, Clone, PartialEq)] +pub struct VoiceComposerPreview { + pub recording: bool, + pub elapsed_secs: u64, + /// Recent RMS levels, oldest first. + pub levels: Vec, + pub live: String, + /// Trailing key hint, e.g. `Ctrl+Space send · Esc cancel`. + pub hint: String, +} + /// Render snapshot of the Ctrl+R reverse prompt-history search overlay. /// `matches` are single-line previews, newest first. #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/crates/jcode-tui/src/tui/redraw_schedule.rs b/crates/jcode-tui/src/tui/redraw_schedule.rs index 5fbb55c01c3..887c8daa2f5 100644 --- a/crates/jcode-tui/src/tui/redraw_schedule.rs +++ b/crates/jcode-tui/src/tui/redraw_schedule.rs @@ -20,6 +20,13 @@ pub(crate) const REDRAW_PASSIVE_LIVENESS: Duration = Duration::from_millis(1000) /// Tick cadence while a drag-held edge autoscroll runs (one line per tick). pub(crate) const REDRAW_COPY_AUTOSCROLL: Duration = Duration::from_millis(30); pub(crate) const REDRAW_DEEP_IDLE_AFTER: Duration = Duration::from_secs(30); +/// Tick cadence while a turn is live but its streamed text is fully revealed. +/// Matches the status spinner glyph cadence so the spinner stays smooth. +pub(crate) const REDRAW_STATIC_STREAM: Duration = Duration::from_millis(80); +/// Fastest tick for a client whose terminal is not focused. Nobody is looking +/// closely at a background pane, but agent swarms keep a dozen of them +/// streaming at once, so full-rate repaints there are pure battery cost. +pub(crate) const REDRAW_UNFOCUSED_MIN: Duration = Duration::from_millis(100); /// Whether this session has been left alone long enough to be treated as /// dormant (deep idle): no stream activity *and* no user interaction for @@ -409,6 +416,32 @@ pub(crate) fn redraw_interval_with_policy_and_animation( state: &dyn TuiState, policy: &crate::perf::TuiPerfPolicy, animation_on_screen: bool, +) -> Duration { + let interval = redraw_interval_focused_policy(state, policy, animation_on_screen); + if state.client_focused() || state.copy_selection_edge_autoscroll_active() { + interval + } else { + interval.max(unfocused_redraw_min(state)) + } +} + +/// Fastest tick for an unfocused client. Text that is still being revealed +/// (or a scroll animation) keeps [`REDRAW_UNFOCUSED_MIN`] so a visible tiled +/// pane still shows live output. A background turn that is only thinking or +/// running a tool has nothing but a spinner and timer to move, so it ticks at +/// the passive liveness rate: measured 10 full frames/s per such session before. +fn unfocused_redraw_min(state: &dyn TuiState) -> Duration { + if state.stream_reveal_pending() || state.has_pending_mouse_scroll_animation() { + REDRAW_UNFOCUSED_MIN + } else { + REDRAW_PASSIVE_LIVENESS + } +} + +fn redraw_interval_focused_policy( + state: &dyn TuiState, + policy: &crate::perf::TuiPerfPolicy, + animation_on_screen: bool, ) -> Duration { let animation_interval = fps_to_duration(policy.animation_fps); let fast_interval = fps_to_duration(policy.redraw_fps); @@ -527,6 +560,24 @@ pub(crate) fn redraw_interval_with_policy_and_animation( return REDRAW_PASSIVE_LIVENESS; } + // Streamed text is on screen but fully revealed while the turn continues + // (thinking or running a tool after emitting text). New deltas wake the + // loop themselves, so ticks only need to keep the status spinner and timer + // alive. Measured on a live client: this state held 66 full frames/s with + // zero changed cells, about 0.2 cores per waiting session. + if state.is_processing() + && !state.streaming_text().is_empty() + && !state.stream_reveal_pending() + && !state.has_pending_mouse_scroll_animation() + && !state.copy_selection_edge_autoscroll_active() + && !rate_limit_countdown_redraw_active(state) + { + return match policy.tier { + crate::perf::PerformanceTier::Minimal => REDRAW_IDLE, + _ => REDRAW_STATIC_STREAM, + }; + } + if state.is_processing() || !state.streaming_text().is_empty() || state.has_pending_mouse_scroll_animation() diff --git a/crates/jcode-tui/src/tui/session_picker.rs b/crates/jcode-tui/src/tui/session_picker.rs index f39436f706c..214a0abf646 100644 --- a/crates/jcode-tui/src/tui/session_picker.rs +++ b/crates/jcode-tui/src/tui/session_picker.rs @@ -8,7 +8,9 @@ mod presence; use super::color_support::rgb; use crate::session::{CrashedSessionsInfo, Session}; -use crate::tui::{DisplayMessage, markdown}; +use crate::tui::DisplayMessage; +#[cfg(test)] +use crate::tui::markdown; use anyhow::Result; use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers, MouseEventKind}; use jcode_session_types::SessionStatus; @@ -38,6 +40,7 @@ mod benchmark_io; #[cfg(test)] mod benchmark_matrix; mod canvas; +mod cloud_presence; mod cloud_preview; mod filter; mod identity_search; @@ -56,7 +59,11 @@ mod preview_refresh; mod progressive_preview; mod render; mod saved_placement; +mod session_view; +use session_view::highlight_buffer_matches; mod transcript_search; +#[cfg(test)] +mod typing_bench; #[cfg(test)] use loading::collect_recent_session_stems; @@ -79,6 +86,7 @@ type GroupedSessionList = (Vec, Vec); pub(crate) enum SessionListUpdate { Cached(GroupedSessionList), + CloudUnavailable(String), Fresh(Result), } @@ -163,7 +171,7 @@ fn git_common_dir(dir: &Path) -> Option { /// Format duration since a time in a human-readable way #[path = "session_picker/time_labels.rs"] mod time_labels; -pub(crate) use time_labels::format_time_ago; +pub(crate) use time_labels::{format_time_ago, session_resume_time}; /// Which pane has keyboard focus #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -216,6 +224,9 @@ struct PreviewExtras { struct LoadedSessionPreview { messages: Vec, extras: PreviewExtras, + /// Source-classified search text for external transcripts, whose preview + /// messages flatten tool blocks into the surrounding role. + search: Option, } /// Fingerprint of every input that affects the *content* of the preview pane @@ -244,6 +255,8 @@ struct PreviewCacheKey { /// materializes the visible window. struct PreviewRenderCache { key: PreviewCacheKey, + /// Transcript handed to the real chat renderer. + messages: Vec, /// Wrapped lines at the final (post-scrollbar-decision) width. wrapped_lines: Vec>, image_regions: Vec, @@ -292,6 +305,8 @@ pub struct SessionPicker { search_query: String, /// Whether we're in search input mode search_active: bool, + /// Feedback from the most recent clipboard action, cleared on the next key. + copy_notice: Option<&'static str>, /// Hidden test session count (debug + canary) hidden_test_count: usize, /// Which pane has keyboard focus @@ -301,6 +316,7 @@ pub struct SessionPicker { last_mouse_scroll: Option, /// Normalized query from the most recent search pass. cached_search_query: String, + search_edit_pending: bool, /// Session refs that matched the cached search query. cached_search_refs: Vec, /// Cancellable full-text scan, separate from the bounded display cache. @@ -311,9 +327,14 @@ pub struct SessionPicker { pending_search_submission: Option, /// Lightweight placeholder shown while the picker list is loading. loading_message: Option, + pub(crate) cloud_warning: Option, resume_latency: latency::Latency, latency_transcript_rendered: bool, pending_preview_load: Option, + /// Session whose loaded preview was drawn last. + preview_shown_id: Option, + /// Session awaited while the previous preview stays up, and since when. + preview_hold: Option<(String, std::time::Instant)>, pending_workspace_load: Option, preview_load_failures: HashSet, /// Pinned todos + prompt offset per loaded preview, keyed by session id. @@ -353,6 +374,8 @@ pub struct SessionPicker { recent_window_closures: HashMap>, /// When `live_presence` was last snapshotted (throttles periodic refresh). live_presence_refreshed_at: Option, + /// Running turns on cloud hosts, polled off the render thread. + cloud_presence: cloud_presence::CloudPresence, /// ID of the session the picker was opened from, labeled "current" in the /// list so the user can orient themselves in the Active view. current_session_id: Option, @@ -392,19 +415,24 @@ impl SessionPicker { filter_mode: SessionFilterMode::All, search_query: String::new(), search_active: false, + copy_notice: None, hidden_test_count, focus: PaneFocus::Sessions, selected_session_ids: HashSet::new(), last_mouse_scroll: None, cached_search_query: String::new(), + search_edit_pending: false, cached_search_refs: Vec::new(), transcript_search: Default::default(), search_selection: Default::default(), pending_search_submission: None, loading_message: None, + cloud_warning: None, resume_latency: Default::default(), latency_transcript_rendered: false, pending_preview_load: None, + preview_shown_id: None, + preview_hold: None, pending_workspace_load: None, preview_load_failures: HashSet::new(), preview_extras: HashMap::new(), @@ -420,6 +448,7 @@ impl SessionPicker { live_presence: std::collections::HashMap::new(), recent_window_closures: HashMap::new(), live_presence_refreshed_at: None, + cloud_presence: Default::default(), current_session_id: None, pending_claude_takeover: None, }; @@ -451,19 +480,24 @@ impl SessionPicker { filter_mode: SessionFilterMode::All, search_query: String::new(), search_active: false, + copy_notice: None, hidden_test_count: 0, focus: PaneFocus::Sessions, selected_session_ids: HashSet::new(), last_mouse_scroll: None, cached_search_query: String::new(), + search_edit_pending: false, cached_search_refs: Vec::new(), transcript_search: Default::default(), search_selection: Default::default(), pending_search_submission: None, loading_message: Some("Loading sessions…".to_string()), + cloud_warning: None, resume_latency: Default::default(), latency_transcript_rendered: false, pending_preview_load: None, + preview_shown_id: None, + preview_hold: None, pending_workspace_load: None, preview_load_failures: HashSet::new(), preview_extras: HashMap::new(), @@ -479,6 +513,7 @@ impl SessionPicker { live_presence: std::collections::HashMap::new(), recent_window_closures: HashMap::new(), live_presence_refreshed_at: None, + cloud_presence: Default::default(), current_session_id: None, pending_claude_takeover: None, } @@ -542,19 +577,24 @@ impl SessionPicker { filter_mode: SessionFilterMode::All, search_query: String::new(), search_active: false, + copy_notice: None, hidden_test_count, focus: PaneFocus::Sessions, selected_session_ids: HashSet::new(), last_mouse_scroll: None, cached_search_query: String::new(), + search_edit_pending: false, cached_search_refs: Vec::new(), transcript_search: Default::default(), search_selection: Default::default(), pending_search_submission: None, loading_message: None, + cloud_warning: None, resume_latency: Default::default(), latency_transcript_rendered: false, pending_preview_load: None, + preview_shown_id: None, + preview_hold: None, pending_workspace_load: None, preview_load_failures: HashSet::new(), preview_extras: HashMap::new(), @@ -570,6 +610,7 @@ impl SessionPicker { live_presence: std::collections::HashMap::new(), recent_window_closures: HashMap::new(), live_presence_refreshed_at: None, + cloud_presence: Default::default(), current_session_id: None, pending_claude_takeover: None, }; @@ -758,6 +799,12 @@ impl SessionPicker { } else { (Vec::new(), orphan_sessions) }; + transcript_search::prefetch_recent( + all_sessions + .iter() + .chain(all_orphan_sessions.iter()) + .chain(server_groups.iter().flat_map(|g| g.sessions.iter())), + ); self.all_sessions = all_sessions; self.all_server_groups = server_groups; self.all_orphan_sessions = all_orphan_sessions; @@ -857,7 +904,7 @@ impl SessionPicker { self.visible_sessions .iter() .filter_map(|session_ref| self.session_by_ref(*session_ref)) - .max_by_key(|session| session.last_active_at.unwrap_or(session.last_message_time)) + .max_by_key(|session| session_resume_time(session)) .map(|session| session.resume_target.clone()) } @@ -872,6 +919,21 @@ impl SessionPicker { }) } + /// Copy the highlighted session's resume identifier and report clipboard errors. + fn copy_selected_session_with(&mut self, copy: impl FnOnce(&str) -> bool) { + let Some(session) = self.selected_session() else { + self.copy_notice = Some("No session selected"); + return; + }; + let is_path = matches!(session.resume_target, ResumeTarget::PiSession { .. }); + let copied = copy(session.resume_target.stable_id()); + self.copy_notice = Some(match (copied, is_path) { + (true, true) => "Session path copied", + (true, false) => "Session ID copied", + (false, _) => "Could not copy to clipboard", + }); + } + pub fn session_for_target(&self, target: &ResumeTarget) -> Option<&SessionInfo> { self.visible_sessions .iter() @@ -1022,6 +1084,7 @@ impl SessionPicker { let (messages, hidden_prompts) = build_messages_preview_with_offset(&session); return Some(LoadedSessionPreview { messages, + search: None, extras: PreviewExtras { hidden_prompts, todos_payload: crate::tui::app::pinned_todos_payload_for_session(session_id), @@ -1030,9 +1093,10 @@ impl SessionPicker { }, }); } - Self::load_external_preview_for_target(resume_target, external_path).map(|messages| { + Self::load_external_preview_for_target(resume_target, external_path).map(|preview| { LoadedSessionPreview { - messages, + messages: preview.messages, + search: Some(preview.search), extras: PreviewExtras::default(), } }) @@ -1041,49 +1105,72 @@ impl SessionPicker { fn load_external_preview_for_target( resume_target: ResumeTarget, external_path: Option, - ) -> Option> { + ) -> Option { match resume_target { ResumeTarget::JcodeSession { session_id } => { let Ok(session) = Session::load(&session_id) else { return None; }; - Some(build_messages_preview(&session)) + Some(loading::ExternalPreview { + messages: build_messages_preview(&session), + search: Default::default(), + }) } ResumeTarget::ClaudeCodeSession { session_id, .. } => external_path .as_deref() .and_then(|path| { - loading::load_claude_code_preview_from_path(std::path::Path::new(path)) + loading::load_claude_code_external_preview(std::path::Path::new(path)) }) - .or_else(|| loading::load_claude_code_preview(&session_id)), + .or_else(|| loading::load_claude_code_external_preview_by_id(&session_id)), ResumeTarget::CodexSession { session_id, .. } => external_path .as_deref() - .and_then(|path| loading::load_codex_preview_from_path(std::path::Path::new(path))) - .or_else(|| loading::load_codex_preview(&session_id)), + .and_then(|path| loading::load_codex_external_preview(std::path::Path::new(path))) + .or_else(|| loading::load_codex_external_preview_by_id(&session_id)), ResumeTarget::PiSession { session_path } => { - loading::load_pi_preview_from_path(std::path::Path::new(&session_path)) + loading::load_pi_external_preview(std::path::Path::new(&session_path)) } ResumeTarget::OpenCodeSession { .. } => external_path.as_deref().and_then(|path| { - loading::load_opencode_preview_from_path(std::path::Path::new(path)) - }), - ResumeTarget::CursorSession { .. } => external_path.as_deref().and_then(|path| { - loading::load_cursor_preview_from_path(std::path::Path::new(path)) + loading::load_opencode_external_preview(std::path::Path::new(path)) }), + ResumeTarget::CursorSession { .. } => external_path + .as_deref() + .and_then(|path| loading::load_cursor_external_preview(std::path::Path::new(path))), } } + #[cfg(test)] fn apply_session_preview(&mut self, session_id: &str, preview: Vec) { + self.apply_session_preview_with_search(session_id, preview, None); + } + + fn apply_session_preview_with_search( + &mut self, + session_id: &str, + preview: Vec, + search: Option, + ) { let Some(session_ref) = self.session_ref_for_id(session_id) else { return; }; if let Some(s) = self.session_by_ref_mut(session_ref) { - s.search_index = build_search_index( - &s.id, - &s.short_name, - &s.title, - s.working_dir.as_deref(), - s.save_label.as_deref(), - &preview, - ); + s.search_index = match search.filter(|search| !search.is_empty()) { + Some(search) => loading::build_classified_search_index( + &s.id, + &s.short_name, + &s.title, + s.working_dir.as_deref(), + s.save_label.as_deref(), + &search, + ), + None => build_search_index( + &s.id, + &s.short_name, + &s.title, + s.working_dir.as_deref(), + s.save_label.as_deref(), + &preview, + ), + }; s.messages_preview = preview; } } @@ -1107,7 +1194,11 @@ impl SessionPicker { self.preview_load_failures.remove(&session_id); self.preview_extras .insert(session_id.clone(), preview.extras); - self.apply_session_preview(&session_id, preview.messages); + self.apply_session_preview_with_search( + &session_id, + preview.messages, + preview.search, + ); true } Ok(None) => { @@ -1146,13 +1237,24 @@ impl SessionPicker { return; }; + // A running turn keeps appending to its transcript. Follow it closely + // so the preview tracks the session like its own chat view would. + let refresh_after = if self + .live_presence + .get(&cache_session_id) + .is_some_and(|presence| presence.streaming) + { + Duration::from_secs(1) + } else { + Duration::from_secs(15) + }; if self .preview_extras .get(&cache_session_id) .is_some_and(|extras| { extras .loaded_at - .is_none_or(|at| at.elapsed() < Duration::from_secs(15)) + .is_none_or(|at| at.elapsed() < refresh_after) }) || self.preview_load_failures.contains(&cache_session_id) || self @@ -1216,7 +1318,7 @@ impl SessionPicker { if let Some(preview) = Self::load_preview_for_target(resume_target, external_path) { self.preview_extras .insert(session_id.clone(), preview.extras); - self.apply_session_preview(&session_id, preview.messages); + self.apply_session_preview_with_search(&session_id, preview.messages, preview.search); } } @@ -1259,6 +1361,11 @@ impl SessionPicker { return None; } self.pending_search_submission = None; + if self.transcript_search.pending() && self.search_selection.holding() { + // Nothing matched within the wait. The rows on screen belong to + // the previous query, so keep editing instead of resuming one. + return None; + } self.rebuild_items_following(); let targets = self.selection_or_current_targets(); if targets.is_empty() { @@ -1274,7 +1381,8 @@ impl SessionPicker { fn queue_search_submission(&mut self, modifiers: KeyModifiers) -> bool { if !self.search_query.trim().is_empty() - && self.search_selection.following() + // Held rows belong to the previous query: never resume one of them. + && (self.search_selection.following() || self.search_selection.holding()) && self.transcript_search.pending() { self.pending_search_submission = Some(modifiers); @@ -1303,6 +1411,9 @@ impl SessionPicker { // In particular, never queue Enter to unexpectedly resume a session // when the background load eventually completes. KeyCode::Enter if self.loading_message.is_some() => {} + KeyCode::Enter if self.search_edit_pending => { + self.pending_search_submission = Some(modifiers); + } KeyCode::Enter => { if self.queue_search_submission(modifiers) { return Ok(OverlayAction::Continue); @@ -1324,27 +1435,27 @@ impl SessionPicker { // send for Ctrl+Backspace) delete the previous word in the query. KeyCode::Backspace if ctrl => { self.delete_search_word_back(); - self.rebuild_items(); + self.search_edit_pending = true; } KeyCode::Char('\u{8}') => { self.delete_search_word_back(); - self.rebuild_items(); + self.search_edit_pending = true; } KeyCode::Backspace => { self.search_query.pop(); - self.rebuild_items(); + self.search_edit_pending = true; } // Ctrl+U clears the whole query (like readline's kill-to-start). KeyCode::Char('u') if ctrl => { self.search_query.clear(); - self.rebuild_items(); + self.search_edit_pending = true; } // Vim-style / readline navigation that keeps working while typing. KeyCode::Char('j') | KeyCode::Char('n') if ctrl => self.next(), KeyCode::Char('k') | KeyCode::Char('p') if ctrl => self.previous(), KeyCode::Char('w') if ctrl => { self.delete_search_word_back(); - self.rebuild_items(); + self.search_edit_pending = true; } KeyCode::Char(c) => { if ctrl && c == 'c' { @@ -1356,7 +1467,7 @@ impl SessionPicker { return Ok(OverlayAction::Continue); } self.search_query.push(c); - self.rebuild_items(); + self.search_edit_pending = true; } KeyCode::Down => self.next(), KeyCode::Up => self.previous(), @@ -1405,9 +1516,25 @@ impl SessionPicker { code: KeyCode, modifiers: KeyModifiers, ) -> Result { + self.copy_notice = None; if let Some(action) = self.handle_claude_takeover_confirmation_key(code, modifiers) { return Ok(action); } + // Alt+S / Alt+Shift+S cycle filters from any mode, including while + // typing a search query where plain `s` must stay literal text. + if modifiers.contains(KeyModifiers::ALT) { + match code { + KeyCode::Char('s') if !modifiers.contains(KeyModifiers::SHIFT) => { + self.cycle_filter_mode(); + return Ok(OverlayAction::Continue); + } + KeyCode::Char('S') | KeyCode::Char('s') => { + self.cycle_filter_mode_backwards(); + return Ok(OverlayAction::Continue); + } + _ => {} + } + } // Search editing must work even before the first cached list arrives. // Keep the query in picker state so reseeding applies it to new entries. if self.search_active { @@ -1457,6 +1584,9 @@ impl SessionPicker { return Ok(OverlayAction::Close); } KeyCode::Char('q') => return Ok(OverlayAction::Close), + KeyCode::Char('y') if modifiers.is_empty() => { + self.copy_selected_session_with(super::app::helpers::copy_to_clipboard); + } KeyCode::Char(' ') => { self.toggle_selected_session(); } @@ -1622,25 +1752,30 @@ impl SessionPicker { return; } - let inner = - preview_chrome::draw(frame, inner, session, self.preview_extras.get(&session.id)); - let todos_payload = self - .preview_extras - .get(&session.id) - .and_then(|extras| extras.todos_payload.clone()); let preview_still_loading = session.messages_preview.is_empty() && self .pending_preview_load .as_ref() .is_some_and(|pending| pending.session_id == session.id); + let selected_id = session.id.clone(); + if preview_still_loading { + if let Some(shown_ref) = self.held_preview_ref(&selected_id) { + // Most previews load within a frame or two. Keep the last one + // up meanwhile instead of flashing a loading placeholder. + let scroll = Some(self.scroll_offset as usize); + self.draw_selected_view(frame, inner, shown_ref, scroll); + self.latency_transcript_rendered = false; + return; + } + } else { + self.preview_hold = None; + if self.preview_shown_id.as_deref() != Some(selected_id.as_str()) { + self.preview_shown_id = Some(selected_id); + } + } let Some(content_hash) = self.selected_preview_content_hash(session_ref, diff_mode) else { return; }; - - // Build (or reuse) the fully-wrapped preview content. The expensive - // markdown render + wrap only runs on a cache miss; scrolling, focus - // changes, and idle redraws reuse the cached wrapped lines. This mirrors - // the main chat viewport, whose prepared frame is cached the same way. let key = PreviewCacheKey { content_hash, inner_width: inner.width, @@ -1654,181 +1789,39 @@ impl SessionPicker { let Some(session) = self.session_by_ref(session_ref) else { return; }; - // Shared markdown renderers also center structured blocks via a - // thread-local setting. Scope it so lists/code/tools stay flush - // left here without changing the chat rendered behind the picker. - let rebuilt = markdown::with_center_code_blocks(false, || { - self.build_preview_cache(session, area, inner, key, align, diff_mode) - }); + let rebuilt = self.build_preview_cache(session, area, inner, key, align, diff_mode); self.install_preview(rebuilt); } - // Read cache geometry through a short-lived borrow so the scroll-offset - // clamp below can take `&mut self` without conflict. - let (show_scrollbar, total_lines, first_match_line) = { - let Some(cache) = self.preview_cache.as_ref() else { - return; - }; - ( - cache.show_scrollbar, - cache.wrapped_lines.len(), - cache.first_match_line, - ) - }; - let (text_area, scrollbar_area) = - super::ui::split_native_scrollbar_area(inner, show_scrollbar); - let inset = super::ui::left_aligned_content_inset(text_area.width, false); - let text_area = Rect { - x: text_area.x + inset, - width: text_area.width.saturating_sub(inset), - ..text_area - }; - // The picker is an overlay, not a narrower conversation viewport. - // Keep prompt and todo wrapping identical to the opened session. - let band_width = text_area.width; - - // Pinned todo band, rendered beneath the sticky previous-prompt header - // exactly like the live chat viewport (`display.pin_todos`). - let (todo_band, _) = super::ui::viewport::pinned_todo_band_lines_for_payload( - todos_payload.as_deref(), - false, - diff_mode, - band_width, - text_area.height, - ); - let todo_lines = todo_band.len() as u16; - - // Max scroll accounts for the rows the top bands reserve, iterating - // like the live viewport since the header height depends on scroll. - let mut max_scroll = - total_lines.saturating_sub(text_area.height.saturating_sub(todo_lines) as usize); - for _ in 0..4 { - let header = self - .preview_prompt_header_lines(max_scroll, band_width, align) - .len() as u16; - let top_band = (header + todo_lines).min(text_area.height); - let body = Rect { - y: text_area.y + top_band, - height: text_area.height - top_band, - ..text_area - }; - let adjusted = total_lines.saturating_sub(body.height as usize); - if adjusted == max_scroll { - break; - } - max_scroll = adjusted; - } - let max_scroll = max_scroll.min(u16::MAX as usize) as u16; - self.preview_max_scroll = max_scroll; - // The transcript for the selected session may still be loading on a - // background thread, in which case this frame only shows a "Loading…" - // placeholder (max_scroll == 0). Keep the auto-scroll armed until the - // real content lands; otherwise the flag is consumed on the placeholder - // frame and the populated transcript stays pinned at the top, hiding the - // sticky "previous prompt" header that should appear once we snap to the - // bottom. - if self.auto_scroll_preview { - // When a search is active and the selected session has a match in the - // preview body, scroll the first hit into view (a few lines of lead-in - // context) instead of jumping to the bottom of the transcript. - self.scroll_offset = match first_match_line { - Some(line) if !self.search_query.trim().is_empty() => { - (line.saturating_sub(2) as u16).min(max_scroll) - } - _ => max_scroll, - }; - if !preview_still_loading { - self.auto_scroll_preview = false; + let tokens = self.active_highlight_tokens(); + let mut scroll = if self.auto_scroll_preview { + match self.preview_cache.as_ref().and_then(|c| c.first_match_line) { + Some(line) if !tokens.is_empty() => Some(line.saturating_sub(2)), + _ => None, } + } else if self.scroll_offset >= self.preview_max_scroll { + // Parked at the bottom: keep following a transcript that grows. + None } else { - self.scroll_offset = self.scroll_offset.min(max_scroll); - } - let scroll = self.scroll_offset as usize; - - let header_lines = self.preview_prompt_header_lines(scroll, band_width, align); - let header_height = (header_lines.len() as u16).min(text_area.height); - let top_band = (header_height + todo_lines).min(text_area.height); - let content_area = Rect { - x: text_area.x, - y: text_area.y + top_band, - width: text_area.width, - height: text_area.height - top_band, - }; - let visible_height = content_area.height as usize; - - // Materialize only the visible window of wrapped lines instead of cloning - // and `.scroll()`ing the whole preview every frame (the main chat - // viewport uses the same visible-slice strategy). This makes a scroll - // tick O(viewport height) rather than O(total wrapped lines). - let visible_end = (scroll + visible_height).min(total_lines); - let (visible_lines, source_rows): (Vec>, Vec) = { - let Some(cache) = self.preview_cache.as_ref() else { - return; - }; - if scroll < visible_end { - ( - cache.wrapped_lines[scroll..visible_end].to_vec(), - (scroll..visible_end).collect(), - ) - } else { - (Vec::new(), Vec::new()) - } + Some(self.scroll_offset as usize) }; - frame.render_widget(Paragraph::new(visible_lines.clone()), content_area); - if let Some(cache) = self.preview_cache.as_ref() { - preview_chrome::draw_images( - frame, - content_area, - &cache.image_regions, - &source_rows, - None, - ); + let mut geometry = self.draw_selected_view(frame, inner, session_ref, scroll); + // The first match is only known once the renderer prepared the frame. + if self.auto_scroll_preview + && scroll.is_none() + && !tokens.is_empty() + && let Some(line) = self.preview_cache.as_ref().and_then(|c| c.first_match_line) + { + scroll = Some(line.saturating_sub(2)); + geometry = self.draw_selected_view(frame, inner, session_ref, scroll); } - if let (Some(session), Some(cache)) = ( - self.session_by_ref(session_ref), - self.preview_cache.as_ref(), - ) { - preview_chrome::draw_widgets( - frame, - content_area, - &visible_lines, - session, - self.preview_extras.get(&session.id), - &cache.image_regions, - scroll, - ); + if !tokens.is_empty() { + highlight_buffer_matches(frame.buffer_mut(), inner, &tokens); } - - if todo_lines > 0 && header_height < text_area.height { - let band_area = Rect { - x: text_area.x, - y: text_area.y + header_height, - width: band_width, - height: todo_lines.min(text_area.height - header_height), - }; - frame.render_widget(Clear, band_area); - frame.render_widget(Paragraph::new(todo_band), band_area); - } - if header_height > 0 { - let header_area = Rect { - x: text_area.x, - y: text_area.y, - width: band_width.saturating_sub(1), - height: header_height, - }; - frame.render_widget(Clear, header_area); - frame.render_widget(Paragraph::new(header_lines), header_area); - } - - if let Some(scrollbar_area) = scrollbar_area { - super::ui::render_native_scrollbar( - frame, - scrollbar_area, - scroll, - total_lines, - visible_height, - self.focus == PaneFocus::Preview, - ); + self.preview_max_scroll = geometry.max_scroll.min(u16::MAX as usize) as u16; + self.scroll_offset = geometry.scroll.min(u16::MAX as usize) as u16; + if self.auto_scroll_preview && !preview_still_loading { + self.auto_scroll_preview = false; } self.latency_transcript_rendered = !preview_still_loading && !self.pending_preview_load.as_ref().is_some_and(|pending| { @@ -1836,7 +1829,101 @@ impl SessionPicker { }) && !self .preview_load_failures - .contains(&self.latency_selection_id().unwrap_or_default()); + .contains(self.latency_selection_id().as_deref().unwrap_or("")); + } + + /// While `loading_id` loads, the session whose preview is still on screen, + /// for a short grace period. Past it the loading placeholder is honest. + fn held_preview_ref(&mut self, loading_id: &str) -> Option { + const GRACE: std::time::Duration = std::time::Duration::from_millis(150); + if self + .preview_hold + .as_ref() + .is_none_or(|(id, _)| id != loading_id) + { + self.preview_hold = Some((loading_id.to_string(), std::time::Instant::now())); + } + let (_, since) = self.preview_hold.as_ref()?; + if since.elapsed() >= GRACE { + return None; + } + self.preview_cache.as_ref()?; + let shown = self.preview_shown_id.clone()?; + self.session_ref_for_id(&shown) + } + + /// Draw the selected session through the real chat renderer. + fn draw_selected_view( + &mut self, + frame: &mut Frame, + area: Rect, + session_ref: SessionRef, + scroll: Option, + ) -> super::ui::SecondaryGeometry { + let Some(session) = self.session_by_ref(session_ref) else { + return Default::default(); + }; + let Some(cache) = self.preview_cache.as_ref() else { + return Default::default(); + }; + let extras = self.preview_extras.get(&session.id); + let host = (session.server_icon.as_deref() == Some("☁")) + .then(|| session.server_name.as_deref()?.strip_prefix("cloud: ")) + .flatten() + .map(str::to_string); + let view = session_view::SessionView { + messages: &cache.messages, + version: session_view::preview_messages_version(cache.key.content_hash), + hidden_prompts: extras.map_or(0, |extras| extras.hidden_prompts), + info: preview_chrome::selected_info(session, extras), + todos_payload: extras.and_then(|extras| extras.todos_payload.as_deref()), + session_name: Some(session.short_name.clone()), + model: session.model.clone(), + provider: session.provider_key.clone(), + server_name: session.server_name.clone(), + server_icon: session.server_icon.clone(), + is_canary: session.is_canary, + working_dir: session.working_dir.clone(), + local_auth: host.is_none(), + machine: host, + scroll, + live: self + .live_presence + .get(&session.id) + .filter(|presence| presence.streaming) + .map(|presence| session_view::LiveTurn { + status: super::ProcessingStatus::Streaming, + elapsed: presence + .streaming_since + .and_then(|since| std::time::SystemTime::now().duration_since(since).ok()), + ..Default::default() + }), + now_ms: std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_millis() as u64), + }; + let (geometry, prepared) = session_view::draw_session_view(frame, area, &view); + if let (Some(prepared), Some(cache)) = (prepared, self.preview_cache.as_mut()) + && cache.wrapped_lines.is_empty() + { + cache.wrapped_lines = prepared.materialize_all_lines(); + cache.image_regions = prepared.image_regions.clone(); + cache.show_scrollbar = geometry.max_scroll > 0; + cache.user_prompt_markers = prepared + .wrapped_user_prompt_starts + .iter() + .zip(prepared.user_prompt_texts.iter()) + .enumerate() + .map(|(idx, (&line, text))| (line, idx + 1, text.clone())) + .collect(); + let tokens = loading::search_query_tokens(&cache.key.search_query); + cache.first_match_line = cache.wrapped_lines.iter().position(|line| { + let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); + let lower = text.to_lowercase(); + tokens.iter().any(|token| lower.contains(token.as_str())) + }); + } + geometry } /// Fingerprint everything that affects the *content* of the preview pane so @@ -1901,100 +1988,14 @@ impl SessionPicker { fn build_preview_cache( &self, session: &SessionInfo, - area: Rect, - inner: Rect, + _area: Rect, + _inner: Rect, key: PreviewCacheKey, _align: Alignment, - diff_mode: crate::config::DiffDisplayMode, + _diff_mode: crate::config::DiffDisplayMode, ) -> PreviewRenderCache { - let hidden_prompts = self - .preview_extras - .get(&session.id) - .map_or(0, |extras| extras.hidden_prompts); - let messages: Vec = session - .messages_preview - .iter() - .map(|msg| DisplayMessage { - role: msg.role.clone(), - content: msg.content.clone(), - tool_calls: msg.tool_calls.clone(), - duration_secs: msg.duration_secs, - title: msg.title.clone(), - tool_data: msg.tool_data.clone(), - }) - .collect(); - let wide_width = inner - .width - .saturating_sub(super::ui::left_aligned_content_inset(inner.width, false)); - let mut header = super::ui::header::build_preview_header(session, wide_width); - let mut padding = super::ui::prepare::initial_header_pad_top(area.height, header.len()); - let render_width = inner.width.saturating_sub(1); - let narrow_width = - render_width.saturating_sub(super::ui::left_aligned_content_inset(render_width, false)); - let header_height = padding + header.len(); - let (prepared, show_scrollbar) = if inner.width <= 1 || inner.height == 0 { - ( - super::ui::prepare::prepare_history_preview( - &messages, - hidden_prompts, - wide_width, - diff_mode, - ), - false, - ) - } else if header_height > inner.height as usize { - ( - super::ui::prepare::prepare_history_preview( - &messages, - hidden_prompts, - narrow_width, - diff_mode, - ), - true, - ) - } else { - super::ui::prepare::prepare_history_preview_for_viewport( - &messages, - hidden_prompts, - wide_width, - narrow_width, - diff_mode, - inner.height as usize - header_height, - ) - }; - if show_scrollbar { - header = super::ui::header::build_preview_header(session, narrow_width); - padding = super::ui::prepare::initial_header_pad_top(area.height, header.len()); - } - // The real startup header is a scrolling transcript section. Never - // pin a synthetic jcode/client/model banner above a long conversation. - let header_height = padding + header.len(); - let mut wrapped_lines = vec![Line::default(); padding]; - wrapped_lines.extend(header); - let body_empty = prepared.wrapped_lines.is_empty(); - wrapped_lines.extend(prepared.wrapped_lines); - let mut image_regions = prepared.image_regions; - for region in &mut image_regions { - region.abs_line_idx += header_height; - region.end_line += header_height; - } - // The live viewport consumes these marker rows as image metadata, never - // printable text. Preserve their reserved rows and draw the images below. - for region in &image_regions { - if let Some(line) = wrapped_lines.get_mut(region.abs_line_idx) { - *line = Line::default(); - } - } - let user_prompt_markers = prepared - .wrapped_user_prompt_starts - .iter() - .zip(prepared.user_prompt_texts.iter()) - .enumerate() - .map(|(idx, (&line, text))| { - (line + header_height, hidden_prompts + idx + 1, text.clone()) - }) - .collect(); - if body_empty { + let mut messages = session_view::display_messages_from_preview(&session.messages_preview); + if messages.is_empty() { let loading = self .pending_preview_load .as_ref() @@ -2006,28 +2007,23 @@ impl SessionPicker { } else { "(empty session)" }; - wrapped_lines.push(Line::styled(text, Style::default().fg(rgb(80, 80, 80)))); + messages.push(DisplayMessage::system(text)); } - // Highlight active search matches in the wrapped preview body and record - // the first wrapped line that contains a hit, so the caller can scroll it - // into view. Highlighting after wrapping keeps wrapped-line indices exact. - let highlight_tokens = self.active_highlight_tokens(); - let first_match_line = - Self::highlight_lines_in_place(&mut wrapped_lines, &highlight_tokens); - PreviewRenderCache { key, - wrapped_lines, - image_regions, - user_prompt_markers, - show_scrollbar, - first_match_line, + messages, + wrapped_lines: Vec::new(), + image_regions: Vec::new(), + user_prompt_markers: Vec::new(), + show_scrollbar: false, + first_match_line: None, } } /// Apply search-match highlighting to already-built preview lines in place. /// Returns the index of the first line that contains a highlighted match, if /// any. Each token highlights independently (matching the AND-token filter). + #[cfg(test)] fn highlight_lines_in_place(lines: &mut [Line<'static>], tokens: &[String]) -> Option { if tokens.is_empty() { return None; @@ -2060,6 +2056,7 @@ impl SessionPicker { /// Lines for the pinned "previous prompt" header in the preview pane. /// Mirrors the main chat viewport's `prompt_preview`: the last user prompt /// whose wrapped start has scrolled above the viewport, drawn dimmed. + #[cfg(test)] fn preview_prompt_header_lines( &self, scroll: usize, @@ -2399,9 +2396,17 @@ impl SessionPicker { self.run_with_session_updates(None) } + pub(crate) fn set_cloud_warning(&mut self, warning: String) { + self.cloud_warning = Some(warning); + } + fn apply_session_list_update(&mut self, update: SessionListUpdate) -> Result { let (sessions, finished) = match update { SessionListUpdate::Cached(sessions) => (sessions, false), + SessionListUpdate::CloudUnavailable(warning) => { + self.set_cloud_warning(warning); + return Ok(false); + } SessionListUpdate::Fresh(sessions) => (sessions?, true), }; self.reseed_grouped(sessions.0, sessions.1); diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_presence.rs b/crates/jcode-tui/src/tui/session_picker/cloud_presence.rs new file mode 100644 index 00000000000..d22bc68a478 --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/cloud_presence.rs @@ -0,0 +1,300 @@ +//! Running-turn presence for sessions owned by a cloud host. +//! +//! Local presence comes from `~/.jcode/streaming_pids`. Cloud sessions keep +//! the same markers on their VM, so one bounded SSH read per host reports +//! which turns are running there. Never runs on the render thread. +use super::*; +use std::sync::mpsc::{Receiver, TryRecvError}; + +const REFRESH_INTERVAL: Duration = Duration::from_secs(10); + +/// Lists ` ` for markers whose owner is alive. +const SCRIPT: &str = r#"d="$HOME/.jcode/streaming_pids" +[ -d "$d" ] || exit 0 +for f in "$d"/*; do + [ -f "$f" ] || continue + p=$(cat "$f" 2>/dev/null) + case "$p" in ''|*[!0-9]*) continue ;; esac + kill -0 "$p" 2>/dev/null || continue + printf '%s %s\n' "$(basename "$f")" "$(stat -c %Y "$f" 2>/dev/null || echo 0)" +done +"#; + +#[derive(Default)] +pub(super) struct CloudPresence { + /// Running cloud sessions from the last completed poll, by session id. + pub(super) streaming: HashMap>, + pending: Option>>>, + started_at: Option, +} + +pub(super) fn parse(output: &str) -> HashMap> { + output + .lines() + .filter_map(|line| { + let (id, mtime) = line.trim().split_once(' ')?; + let valid = !id.is_empty() + && id + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-'); + valid.then(|| { + let since = match mtime.trim().parse::() { + Ok(secs) if secs > 0 => { + std::time::UNIX_EPOCH.checked_add(Duration::from_secs(secs)) + } + Ok(_) => None, + Err(error) => { + crate::logging::debug(&format!( + "Invalid cloud presence timestamp: {error}" + )); + None + } + }; + (id.to_string(), since) + }) + }) + .collect() +} + +fn poll_host(host: &str) -> Option>> { + if host.is_empty() || host.starts_with('-') { + return None; + } + let config = super::preview_result(crate::storage::jcode_dir(), "cloud presence storage")? + .join("cloud/ssh_config"); + let command = crate::tui::cloud_git::script_command( + Some(host), + config.is_file().then_some(config.as_path()), + ".", + SCRIPT, + ); + let runtime = super::preview_result( + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build(), + "cloud presence runtime", + )?; + let (status, bytes) = runtime.block_on(crate::tui::cloud_git::bounded_output_for( + command, + Duration::from_secs(10), + 64 * 1024, + crate::tui::cloud_git::WorkspaceOperation::CloudStatus, + ))?; + status + .success() + .then(|| parse(&String::from_utf8_lossy(&bytes))) +} + +impl SessionPicker { + fn presence_sessions(&self) -> impl Iterator { + self.all_sessions + .iter() + .chain(self.all_orphan_sessions.iter()) + .chain( + self.all_server_groups + .iter() + .flat_map(|group| group.sessions.iter()), + ) + } + + fn cloud_hosts(&self) -> Vec { + let mut hosts: Vec = self + .presence_sessions() + .filter(|session| session.server_icon.as_deref() == Some("☁")) + .filter_map(|session| { + session + .server_name + .as_deref()? + .strip_prefix("cloud: ") + .map(str::to_string) + }) + .collect(); + hosts.sort(); + hosts.dedup(); + hosts + } + + /// Collect a finished poll and start the next one when due. Returns true + /// when the set of running cloud sessions changed. + pub(super) fn maybe_refresh_cloud_presence(&mut self) -> bool { + let mut changed = false; + if let Some(receiver) = self.cloud_presence.pending.as_ref() { + match receiver.try_recv() { + Ok(streaming) => { + changed = streaming != self.cloud_presence.streaming; + self.cloud_presence.streaming = streaming; + self.cloud_presence.pending = None; + } + Err(TryRecvError::Disconnected) => self.cloud_presence.pending = None, + Err(TryRecvError::Empty) => {} + } + } + let due = self + .cloud_presence + .started_at + .is_none_or(|at| at.elapsed() >= REFRESH_INTERVAL); + if self.cloud_presence.pending.is_none() && due { + let hosts = self.cloud_hosts(); + self.cloud_presence.started_at = Some(std::time::Instant::now()); + if hosts.is_empty() { + if !self.cloud_presence.streaming.is_empty() { + self.cloud_presence.streaming.clear(); + changed = true; + } + } else { + let (tx, rx) = std::sync::mpsc::channel(); + let spawned = std::thread::Builder::new() + .name("jcode-cloud-presence".into()) + .spawn(move || { + let mut all = HashMap::new(); + for host in hosts { + if let Some(found) = poll_host(&host) { + all.extend(found); + } + } + if tx.send(all).is_err() { + crate::logging::debug("Cloud presence receiver closed"); + } + }); + if spawned.is_ok() { + self.cloud_presence.pending = Some(rx); + } + } + } + if changed { + self.merge_cloud_presence(); + if self.filter_mode == SessionFilterMode::Active { + self.rebuild_items(); + } + } + changed + } + + /// Overlay remote running turns onto the local presence snapshot. + pub(super) fn merge_cloud_presence(&mut self) { + self.live_presence + .retain(|_, presence| !(presence.pid == 0 && presence.streaming)); + for (session_id, since) in &self.cloud_presence.streaming { + let is_cloud = self.presence_sessions().any(|session| { + session.id == *session_id && session.server_icon.as_deref() == Some("☁") + }); + if !is_cloud { + continue; + } + self.live_presence.insert( + session_id.clone(), + crate::session::SessionPresence { + session_id: session_id.clone(), + pid: 0, + streaming: true, + streaming_since: *since, + internal: false, + }, + ); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_live_markers_and_rejects_unsafe_ids() { + let parsed = parse("session_a_1 1700000000\nbad/id 5\nsession_b_2 0\n\n"); + assert_eq!(parsed.len(), 2); + assert_eq!( + parsed["session_a_1"], + Some(std::time::UNIX_EPOCH + Duration::from_secs(1_700_000_000)) + ); + assert_eq!(parsed["session_b_2"], None); + } + + #[test] + fn invalid_cloud_timestamps_keep_presence_without_age() { + let parsed = parse("session_invalid nope\nsession_overflow 18446744073709551615\n"); + assert_eq!(parsed.len(), 2); + assert_eq!(parsed["session_invalid"], None); + assert_eq!(parsed["session_overflow"], None); + } + + #[test] + fn cloud_running_session_shows_running_state() { + let mut session = make_cloud_session(); + session.id = "session_cloud_1".into(); + let mut picker = SessionPicker::new(vec![session.clone()]); + picker + .cloud_presence + .streaming + .insert(session.id.clone(), None); + picker.merge_cloud_presence(); + assert!(picker.session_is_streaming(&session)); + // A local refresh must not drop the remote running state. + picker.refresh_live_presence(); + assert!(picker.session_is_streaming(&session)); + } + + fn cloud_group(sessions: Vec) -> ServerGroup { + ServerGroup { + name: "cloud: jcode-cloud".into(), + icon: "☁".into(), + version: String::new(), + git_hash: String::new(), + is_running: false, + sessions, + } + } + + #[test] + fn grouped_cloud_sessions_are_polled_and_show_running_in_active_view() { + let session = make_cloud_session(); + let mut picker = + SessionPicker::new_grouped(vec![cloud_group(vec![session.clone()])], vec![]); + assert!(picker.all_sessions.is_empty(), "exercise grouped storage"); + assert_eq!(picker.cloud_hosts(), vec!["jcode-cloud"]); + let (tx, rx) = std::sync::mpsc::channel(); + picker.cloud_presence.pending = Some(rx); + picker.cloud_presence.started_at = Some(std::time::Instant::now()); + picker.activate_active_filter(); + assert!(picker.selected_session().is_none()); + tx.send(HashMap::from([(session.id.clone(), None)])) + .unwrap(); + assert!(picker.maybe_refresh_cloud_presence()); + assert!(picker.session_is_streaming(&session)); + assert_eq!(picker.selected_session().unwrap().id, session.id); + picker.refresh_live_presence(); + assert!(picker.session_is_streaming(&session)); + + // A completed remote poll must remove the running badge and Active row. + let (tx, rx) = std::sync::mpsc::channel(); + picker.cloud_presence.pending = Some(rx); + tx.send(HashMap::new()).unwrap(); + assert!(picker.maybe_refresh_cloud_presence()); + assert!(!picker.session_is_streaming(&session)); + assert!(picker.selected_session().is_none()); + } + + #[test] + fn grouped_picker_orphans_and_reseed_keep_cloud_presence() { + let session = make_cloud_session(); + let mut picker = + SessionPicker::new_grouped(vec![cloud_group(vec![])], vec![session.clone()]); + assert_eq!(picker.cloud_hosts(), vec!["jcode-cloud"]); + picker + .cloud_presence + .streaming + .insert(session.id.clone(), None); + picker.merge_cloud_presence(); + assert!(picker.session_is_streaming(&session)); + picker.reseed_grouped(vec![cloud_group(vec![session.clone()])], vec![]); + assert!(picker.session_is_streaming(&session)); + assert_eq!(picker.cloud_hosts(), vec!["jcode-cloud"]); + } + + fn make_cloud_session() -> SessionInfo { + let mut session = super::super::tests::make_session_for_presence("cloud"); + session.server_name = Some("cloud: jcode-cloud".into()); + session.server_icon = Some("☁".into()); + session + } +} diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs index ac251d06d9c..1df0e60914d 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs @@ -2,23 +2,71 @@ use super::*; mod cache; +#[cfg(test)] +#[path = "cloud_preview/projection_tests.rs"] +mod projection_tests; type CloudSnapshot = (Session, Option); -const SCRIPT: &str = r#" -case "$1" in ''|*[!a-zA-Z0-9_-]*) exit 1 ;; esac -base="${JCODE_HOME:-$HOME/.jcode}/sessions/$1" -cat -- "$base.json" || exit 1 -printf '\000' -if [ -f "$base.journal.jsonl" ]; then cat -- "$base.journal.jsonl" || exit 1; fi -printf '\000' -state="${JCODE_HOME:-$HOME/.jcode}/state" -if [ -n "$JCODE_RUNTIME_DIR" ]; then state="$JCODE_RUNTIME_DIR/durable-state"; fi -telemetry="$state/session-telemetry/$1.json" -if [ -f "$telemetry" ]; then cat -- "$telemetry" || :; fi -"#; +/// Static labels only: never attach hostnames, session IDs, paths or errors. +pub(super) struct StageTimer { + stage: &'static str, + outcome: &'static str, + started: std::time::Instant, +} + +impl StageTimer { + pub(super) fn new(stage: &'static str) -> Self { + Self { + stage, + outcome: "error", + started: std::time::Instant::now(), + } + } + + pub(super) fn outcome(&mut self, outcome: &'static str) { + self.outcome = outcome; + } +} + +#[cfg(test)] +thread_local! { + static STAGE_TIMINGS: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; +} + +impl Drop for StageTimer { + fn drop(&mut self) { + #[cfg(test)] + STAGE_TIMINGS.with_borrow_mut(|events| events.push((self.stage, self.started.elapsed()))); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage={} outcome={} duration_ms={}", + self.stage, + self.outcome, + self.started.elapsed().as_millis() + )); + } +} + +fn stage_result(operation: impl FnOnce() -> Result, stage: &'static str) -> Option { + let mut timer = StageTimer::new(stage); + match operation() { + Ok(value) => { + timer.outcome("success"); + Some(value) + } + Err(_) => None, + } +} + +pub(in crate::tui::session_picker) const REMOTE_PROJECTOR: &str = + include_str!("cloud_preview/remote.py"); + +pub(in crate::tui::session_picker) fn projection_script(mode: &str) -> String { + format!("python3 - {mode} \"$1\" <<'JCODE_REMOTE_PY'\n{REMOTE_PROJECTOR}\nJCODE_REMOTE_PY\n") +} pub(super) fn load(session_id: &str, host: &str) -> Option { + let mut timer = StageTimer::new("load"); if session_id.is_empty() || !session_id .bytes() @@ -26,6 +74,7 @@ pub(super) fn load(session_id: &str, host: &str) -> Option { || host.is_empty() || host.starts_with('-') { + timer.outcome("skipped_invalid_target"); return None; } let epoch = crate::storage::read_session_lease(session_id) @@ -34,7 +83,11 @@ pub(super) fn load(session_id: &str, host: &str) -> Option { let path = cache::path(session_id, host, epoch); let id = session_id.to_owned(); let host = host.to_owned(); - load_cached_or_fetch(session_id, path, move || fetch_bytes(&id, &host)) + let result = load_cached_or_fetch(session_id, path, move || fetch_bytes(&id, &host)); + if result.is_some() { + timer.outcome("success"); + } + result } /// A cloud cache is a preview replica, never a canonical local session. Even a @@ -45,16 +98,14 @@ fn load_cached_or_fetch( path: Option, fetch: impl FnOnce() -> Option> + Send + 'static, ) -> Option { - let started = std::time::Instant::now(); + let mut timer = StageTimer::new("cache_lookup"); if let Some(path) = path.as_deref() && let Some(bytes) = cache::read(path) && let Some(snapshot) = parse_for_session(&bytes, session_id) { - crate::logging::info(&format!( - "CLOUD_PREVIEW cache=hit session={session_id} bytes={} elapsed_ms={}", - bytes.len(), - started.elapsed().as_millis(), - )); + timer.outcome("hit"); + let mut refresh_timer = StageTimer::new("refresh_schedule"); + refresh_timer.outcome("skipped_fresh_or_in_flight"); if !cache::fresh(path) && let Some(guard) = cache::begin_refresh(path) { @@ -66,10 +117,12 @@ fn load_cached_or_fetch( let _guard = guard; fetch_and_cache(&id, Some(&path), fetch); }); - preview_result(refresh, "cloud refresh worker"); + refresh_timer.outcome(if refresh.is_ok() { "success" } else { "error" }); } return Some(snapshot); } + timer.outcome("miss"); + drop(timer); fetch_and_cache(session_id, path.as_deref(), fetch) } @@ -78,64 +131,65 @@ fn fetch_and_cache( path: Option<&std::path::Path>, fetch: impl FnOnce() -> Option>, ) -> Option { - let started = std::time::Instant::now(); + let mut timer = StageTimer::new("refresh"); + let mut network = StageTimer::new("fetch"); let bytes = fetch()?; - let fetched = started.elapsed(); + network.outcome("success"); + drop(network); // Validate the full snapshot + journal before replacing the last good copy. let snapshot = parse_for_session(&bytes, session_id)?; - if let Some(path) = path - && let Err(error) = cache::write(path, &bytes) - { - crate::logging::debug(&format!("Cloud preview cache write unavailable: {error}")); + if let Some(path) = path { + if cache::write(path, &bytes).is_err() { + timer.outcome("success_cache_unavailable"); + return Some(snapshot); + } + } else { + let mut skipped = StageTimer::new("cache_write"); + skipped.outcome("skipped_unavailable"); } - crate::logging::info(&format!( - "CLOUD_PREVIEW cache=refresh session={session_id} bytes={} fetch_ms={} total_ms={}", - bytes.len(), - fetched.as_millis(), - started.elapsed().as_millis(), - )); + timer.outcome("success"); Some(snapshot) } fn parse_for_session(bytes: &[u8], session_id: &str) -> Option { + let mut timer = StageTimer::new("parse"); let snapshot = parse_preview(bytes)?; - (snapshot.0.id == session_id).then_some(snapshot) + if snapshot.0.id != session_id { + timer.outcome("identity_mismatch"); + return None; + } + timer.outcome("success"); + Some(snapshot) } fn fetch_bytes(session_id: &str, host: &str) -> Option> { - let config = crate::storage::jcode_dir() - .map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, - )? - .join("cloud/ssh_config"); + let mut timer = StageTimer::new("ssh"); + let config = stage_result(crate::storage::jcode_dir, "ssh_config")?.join("cloud/ssh_config"); let command = crate::tui::cloud_git::compressed_script_command( Some(host), config.is_file().then_some(config.as_path()), session_id, - SCRIPT, + &projection_script("preview"), ); - let runtime = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, - )?; - let (status, bytes) = runtime.block_on(crate::tui::cloud_git::bounded_output( + let runtime = stage_result( + || { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + }, + "ssh_runtime", + )?; + let (status, bytes) = runtime.block_on(crate::tui::cloud_git::bounded_output_for( command, Duration::from_secs(15), cache::MAX_BYTES, + crate::tui::cloud_git::WorkspaceOperation::Preview, ))?; if !status.success() { + timer.outcome("exit_failure"); return None; } + timer.outcome("success"); Some(bytes) } @@ -151,11 +205,17 @@ fn parse_preview( .map(|offset| first + 1 + offset); let session = parse_transcript(&bytes[..second.unwrap_or(bytes.len())])?; let telemetry = second.and_then(|split| { + let mut timer = StageTimer::new("telemetry_parse"); let bytes = &bytes[split + 1..]; if bytes.is_empty() { + timer.outcome("skipped_absent"); None } else { - preview_result(serde_json::from_slice(bytes), "remote telemetry decoding") + let result = stage_result(|| serde_json::from_slice(bytes), "telemetry_decode"); + if result.is_some() { + timer.outcome("success"); + } + result } }); Some((session, telemetry)) @@ -163,45 +223,49 @@ fn parse_preview( fn parse_transcript(bytes: &[u8]) -> Option { let split = bytes.iter().position(|b| *b == 0)?; - let dir = tempfile::tempdir().map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, - )?; + let dir = stage_result(tempfile::tempdir, "parse_tempdir")?; let path = dir.path().join("preview.json"); - std::fs::write(&path, &bytes[..split]).map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, + stage_result( + || std::fs::write(&path, &bytes[..split]), + "parse_snapshot_write", )?; - std::fs::write( - crate::session::session_journal_path_from_snapshot(&path), - &bytes[split + 1..], - ) - .map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None + stage_result( + || { + std::fs::write( + crate::session::session_journal_path_from_snapshot(&path), + &bytes[split + 1..], + ) }, - Some, + "parse_journal_write", )?; - Session::load_from_path(&path).map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, - ) + stage_result(|| Session::load_from_path(&path), "parse_hydrate") } #[cfg(test)] mod tests { use super::*; + #[test] + fn stage_result_times_the_operation_not_only_result_conversion() { + STAGE_TIMINGS.with_borrow_mut(Vec::clear); + let value = stage_result( + || { + std::thread::sleep(Duration::from_millis(10)); + Ok::<_, ()>(42) + }, + "test_operation", + ); + assert_eq!(value, Some(42)); + STAGE_TIMINGS.with_borrow(|events| { + assert!( + events + .iter() + .any(|(stage, elapsed)| *stage == "test_operation" + && *elapsed >= Duration::from_millis(10)) + ); + }); + } + fn wire(id: &str, title: &str) -> Vec { let session = Session::create_with_id(id.into(), None, Some(title.into())); let mut bytes = serde_json::to_vec(&session).unwrap(); diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs index 0decac2b930..9a5a3657cd9 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs @@ -7,53 +7,102 @@ use std::path::{Path, PathBuf}; use std::sync::{Mutex, OnceLock}; use std::time::Duration; -pub(super) const MAX_BYTES: usize = 64 * 1024 * 1024; -const CACHE_BUDGET: u64 = 256 * 1024 * 1024; +// Remote projection is at most 2 MiB plus bounded optional telemetry. +pub(super) const MAX_BYTES: usize = 3 * 1024 * 1024; +const CACHE_BUDGET: u64 = 24 * 1024 * 1024; const TTL: Duration = Duration::from_secs(15); pub(super) fn path(session_id: &str, host: &str, epoch: u64) -> Option { - let root = super::super::preview_result(crate::storage::jcode_dir(), "cache directory")?; - Some(key_path(&root, session_id, host, epoch)) + let mut timer = super::StageTimer::new("cache_scope"); + let root = super::stage_result(crate::storage::jcode_dir, "cache_directory")?; + let owner = crate::subscription_catalog::configured_api_key().unwrap_or_default(); + let endpoint = crate::subscription_api::configured_api_base(); + let path = scoped_key_path(&root, session_id, host, epoch, &owner, &endpoint); + timer.outcome("success"); + Some(path) } +#[cfg(test)] fn key_path(root: &Path, session_id: &str, host: &str, epoch: u64) -> PathBuf { + scoped_key_path( + root, + session_id, + host, + epoch, + "test-owner", + "https://test.invalid", + ) +} + +fn scoped_key_path( + root: &Path, + session_id: &str, + host: &str, + epoch: u64, + owner: &str, + endpoint: &str, +) -> PathBuf { let mut digest = Sha256::new(); - for field in [host.as_bytes(), session_id.as_bytes(), &epoch.to_be_bytes()] { + for field in [ + owner.as_bytes(), + endpoint.as_bytes(), + host.as_bytes(), + session_id.as_bytes(), + &epoch.to_be_bytes(), + ] { digest.update((field.len() as u64).to_be_bytes()); digest.update(field); } - root.join("cache/cloud-previews/v1") + root.join("cache/cloud-previews/v2") .join(format!("{:x}.blob", digest.finalize())) } pub(super) fn read(path: &Path) -> Option> { + let mut timer = super::StageTimer::new("cache_read"); let file = match File::open(path) { Ok(file) => file, - Err(error) if error.kind() == io::ErrorKind::NotFound => return None, - Err(error) => return super::super::preview_result(Err::, _>(error), "cache open"), + Err(error) if error.kind() == io::ErrorKind::NotFound => { + timer.outcome("miss"); + return None; + } + Err(_) => return None, }; - if super::super::preview_result(file.metadata(), "cache metadata")?.len() > MAX_BYTES as u64 { + if super::stage_result(|| file.metadata(), "cache_metadata")?.len() > MAX_BYTES as u64 { + timer.outcome("oversized"); return None; } // Do not trust metadata for allocation or bounds: the file can grow concurrently. let mut bytes = Vec::new(); - super::super::preview_result( - file.take(MAX_BYTES as u64 + 1).read_to_end(&mut bytes), - "cache read", + super::stage_result( + || file.take(MAX_BYTES as u64 + 1).read_to_end(&mut bytes), + "cache_read_io", )?; + timer.outcome(if bytes.len() <= MAX_BYTES { + "hit" + } else { + "oversized" + }); (bytes.len() <= MAX_BYTES).then_some(bytes) } pub(super) fn fresh(path: &Path) -> bool { - super::super::preview_result( - fs::metadata(path).and_then(|metadata| metadata.modified()), - "cache freshness", + let mut timer = super::StageTimer::new("cache_freshness"); + let fresh = super::stage_result( + || fs::metadata(path).and_then(|metadata| metadata.modified()), + "cache_timestamp", ) - .and_then(|modified| super::super::preview_result(modified.elapsed(), "cache timestamp")) - .is_some_and(|age| age < TTL) + .and_then(|modified| super::stage_result(|| modified.elapsed(), "cache_age")) + .is_some_and(|age| age < TTL); + timer.outcome(if fresh { + "fresh" + } else { + "stale_or_unavailable" + }); + fresh } pub(super) fn write(path: &Path, bytes: &[u8]) -> io::Result<()> { + let mut timer = super::StageTimer::new("cache_write"); if bytes.len() > MAX_BYTES { return Err(io::Error::new( io::ErrorKind::InvalidInput, @@ -78,6 +127,7 @@ pub(super) fn write(path: &Path, bytes: &[u8]) -> io::Result<()> { temporary.as_file().sync_all()?; temporary.persist(path).map_err(|error| error.error)?; prune(parent, path, CACHE_BUDGET); + timer.outcome("success"); Ok(()) } @@ -216,7 +266,7 @@ mod tests { fn keys_scope_host_session_epoch_and_field_boundaries() { let root = Path::new("private-root"); let baseline = key_path(root, "session", "host", 1); - assert!(baseline.starts_with(root.join("cache/cloud-previews/v1"))); + assert!(baseline.starts_with(root.join("cache/cloud-previews/v2"))); for other in [ key_path(root, "session2", "host", 1), key_path(root, "session", "host2", 1), @@ -224,11 +274,51 @@ mod tests { ] { assert_ne!(baseline, other); } + assert_ne!( + scoped_key_path( + root, + "session", + "host", + 1, + "owner-one", + "https://api-one.invalid" + ), + scoped_key_path( + root, + "session", + "host", + 1, + "owner-two", + "https://api-one.invalid" + ) + ); + assert_ne!( + scoped_key_path( + root, + "session", + "host", + 1, + "owner", + "https://api-one.invalid" + ), + scoped_key_path( + root, + "session", + "host", + 1, + "owner", + "https://api-two.invalid" + ) + ); + assert_ne!( + scoped_key_path(root, "session", "host", 1, "ab", "c"), + scoped_key_path(root, "session", "host", 1, "a", "bc") + ); assert_ne!(key_path(root, "bc", "a", 0), key_path(root, "c", "ab", 0)); let hostile = key_path(root, "../../session", "secret-host/..", 1); assert_eq!( hostile.parent(), - Some(root.join("cache/cloud-previews/v1").as_path()) + Some(root.join("cache/cloud-previews/v2").as_path()) ); assert_eq!(hostile.file_name().unwrap().to_str().unwrap().len(), 69); } diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs new file mode 100644 index 00000000000..bc046ba405a --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs @@ -0,0 +1,322 @@ +// Read-only production sample, 2026-11-08 UTC: Swan snapshot 97,722,380 bytes, +// journal 264,881 bytes. Ran this exact embedded projector over existing managed +// SSH (python3 stdin only), three samples per mode. Snapshot/journal size and +// mtime_ns were unchanged before/after each sample. No daemon RPC or host writes. +// Preview: 80 messages, 181,277 output bytes including framing/telemetry. +// Remote projection ms: 2465.50, 1862.45, 1646.69. +// End-to-end fresh SSH ms: 8923.53, 5664.24, 7751.36. +// Local Python json.loads median ms (20 parses/sample): 0.969, 1.121, 0.904. +// This measures JSON decoding only, NOT Rust parse_preview/hydration. The prior +// ~49 MB cache/~300 ms parse baseline is therefore not an apples-to-apples timing +// comparison, though the transferred projection is now bounded and much smaller. +// Inventory: 278 output bytes; remote ms: 912.64, 1453.10, 736.12. +// End-to-end fresh SSH ms: 4342.86, 4807.31, 4282.34. +// Prior 97 MB inventory baseline was 702 ms: no inventory speedup demonstrated. +// Reproduction script/results: ~/.jcode/scratch/bounded-preview-measure.py and +// ~/.jcode/scratch/bounded-preview-measure-results.jsonl on the measurement host. +// Scanner follow-up, 2026-11-08 UTC, same unchanged 97,722,380-byte snapshot: +// Whole-string regex alone did not improve latency. Coalescing scalar tokens +// plus retaining the last 80 message ranges during the first scan avoids the +// second full messages scan. Actual mmap preview remote ms: 768.34, 1381.58, +// 1376.54, fresh SSH ms: 4695.30, 5622.85, 5616.78. All retain 80 messages. +// Journal grew externally between samples (363,579 -> 384,539 bytes), but each +// individual read verified snapshot/journal size+mtime unchanged during projection. +// Paired comparison freezes both files in process RAM, no remote file writes: +// old preview ms 1419.64, 2489.48, 1444.72; new 1315.81, 1160.17, 772.47. +// Every pair is byte-identical (168,621 bytes, without framing/telemetry), SHA256 +// cc39e053c4a2e80dd767fee2a4142f784a44c2cd10d6685a97493d5a04cf75fc. +// Inventory actual mmap ms 1019.11, 1507.74, 742.82: no consistent speedup. +// Evidence: ~/.jcode/scratch/bounded-preview-final-scanner-results.jsonl and +// paired-projection-final-results.txt, reproduced by paired-projection.py. +// Journal follow-up, 2026-11-08 UTC, synthetic LOCAL inputs only, no cloud reads: +// 34,662,320-byte frozen mmap journal, 220 entries x 120 ordinary messages with +// escaped quotes/backslashes, braces and Unicode. Alternating before/after order, +// one warmup then six measured runs per mode. Removing only the redundant outer +// journal-entry scan: preview medians 895.38 -> 660.65 ms (-26.2%), inventory +// 566.62 -> 284.27 ms (-49.8%). Every paired JSON output was byte-identical. +// Snapshot/journal size, mtime_ns and SHA256 were unchanged across all runs. +// Journal SHA256 aa9a2e05ed8a7500ed42fdf832b68995d488814752ec6abd69a5af2f91e4dafd. +// Local reproduction: ~/.jcode/scratch/journal-paired.py, journal-before.py and +// journal-paired-results.txt. This does not establish live-cloud/SSH latency. +use super::*; +use std::io::Write; + +#[test] +fn remote_timing_errors_and_skips_are_safe_and_do_not_change_wire_format() { + let home = tempfile::tempdir().unwrap(); + let private_id = "private_session_identifier"; + let preview = projection_output(home.path(), "preview", private_id); + assert!(!preview.status.success()); + assert!(preview.stdout.is_empty()); + let inventory = run_projection( + home.path(), + "inventory", + &format!("{private_id}\n../private"), + ); + assert!(inventory.status.success()); + let payload: serde_json::Value = serde_json::from_slice(&inventory.stdout).unwrap(); + assert_eq!(payload["sessions"], serde_json::json!([])); + for output in [&preview, &inventory] { + let logs = String::from_utf8_lossy(&output.stderr); + assert!(logs.contains("stage=projection outcome=error duration_ms=")); + assert!(!logs.contains(private_id)); + assert!(!logs.contains(home.path().to_str().unwrap())); + assert!(!logs.contains("Traceback")); + for line in logs.lines() { + let fields: Vec<_> = line.split_whitespace().collect(); + assert_eq!(fields.len(), 4, "{line}"); + assert_eq!(fields[0], "[CLOUD_TIMING]"); + assert!(fields[1].starts_with("stage=")); + assert!(fields[2].starts_with("outcome=")); + fields[3] + .strip_prefix("duration_ms=") + .unwrap() + .parse::() + .unwrap(); + } + } + assert!(String::from_utf8_lossy(&inventory.stderr).contains("outcome=skipped_invalid_target")); + std::fs::create_dir(home.path().join("sessions")).unwrap(); + let session = Session::create_with_id(private_id.into(), None, Some("private title".into())); + std::fs::write( + home.path() + .join("sessions") + .join(format!("{private_id}.json")), + serde_json::to_vec(&session).unwrap(), + ) + .unwrap(); + let success = run_projection(home.path(), "preview", private_id); + assert!(success.status.success()); + assert!(parse_for_session(&success.stdout, private_id).is_some()); + let logs = String::from_utf8_lossy(&success.stderr); + assert!(logs.contains("stage=projection outcome=success duration_ms=")); + assert!(!logs.contains(private_id)); + assert!(!logs.contains("private title")); +} + +#[test] +fn remote_scanner_handles_escaped_nested_strings_and_rejects_truncation() { + let checks = r#" +import random +random.seed(37) +samples = ['', '"', '\\', '\\"', '\\\\"', '{}[]', '\n\t', '雪😀'] +for _ in range(200): + samples.append(''.join(random.choices('abc"\\{}[]\n雪', k=120))) +for text in samples: + value = {'nested': [{'text': text, 'braces': ']}{['}, [text]], 'tail': text} + encoded = json.dumps(value, ensure_ascii=False).encode() + assert value_end(encoded + b',trailer', 0) == len(encoded) + quoted = json.dumps(text, ensure_ascii=False).encode() + assert string_end(quoted + b',trailer', 0) == len(quoted) + assert json.loads(encoded) == value +messages = [{'id': str(i), 'content': [{'text': samples[i % len(samples)]}]} for i in range(300)] +encoded = json.dumps({'messages': messages}).encode() +ranges = collections.deque(maxlen=MAX_MESSAGES) +list(fields(encoded, message_ranges=ranges)) +assert len(ranges) == MAX_MESSAGES +assert [json.loads(encoded[a:b]) for a,b in ranges] == messages[-MAX_MESSAGES:] +encoded = b'{"messages":[{"id":"old"}],"messages":[]}' +for key,a,b in fields(encoded, message_ranges=ranges): + pass +assert not ranges +# Compare exact field spans with the former outer-container scan, including +# whitespace, adjacent entries, duplicate keys, scalars and escaped braces. +def previous_journal_fields(data): + pos = 0 + while pos < len(data): + pos = skip_space(data, pos) + if pos == len(data): + break + end = value_end(data, pos) + yield from fields(data, pos) + pos = end +entries = [b'{}', b'{"meta":{"title":"first","title":"last"}}', + b'{"append_messages":[null,1,"escaped\\\"{}[]",{},[]],"append_messages":[]}', + json.dumps({'unknown': [{'nested': samples}]}).encode()] +for separator in (b'', b'\n', b' \r\n\t'): + encoded = separator + separator.join(entries) + separator + assert list(journal_fields(encoded)) == list(previous_journal_fields(encoded)) +for malformed in (b'{', b'{"x":[]]', b'{"x":"unterminated}'): + assert list(journal_fields(malformed)) == [] +# A complete first entry survives a torn second entry on the same line. +assert list(journal_fields(b'{"meta":{}} {')) == list(journal_fields(b'{"meta":{}}')) +import tempfile +with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'journal.json' + path.write_bytes(b'{"id":"journal","messages":[],"route_api_method":"original"}') + journal = path.with_suffix('.journal.jsonl') + journal.write_bytes(b'{"meta":{"title":"first","title":"last","route_api_method":"ignored"},"append_messages":[null,1,"scalar"]}' + b'{"meta":{"route_api_method_present":true,"route_api_method":"responses"},"append_messages":[]}' + b'{"meta":{"route_api_method_present":true,"route_api_method_present":false,"route_api_method":"ignored"}} \n') + before = [(p.stat().st_size, p.stat().st_mtime_ns) for p in (path, journal)] + result = project(path, True) + assert result['title'] == 'last' + assert result['route_api_method'] == 'responses' + assert result['messages'] == [None, 1, 'scalar'] + assert all(result[k] == [] for k in ('env_snapshots', 'memory_injections', 'replay_events')) + inventory = project(path, False) + assert inventory['title'] == 'last' and 'messages' not in inventory + assert before == [(p.stat().st_size, p.stat().st_mtime_ns) for p in (path, journal)] + for damaged in (b'{"meta":{"title":"discard"},', b'not json', b'{"meta":]', b'{"meta":{"title":garbage}}'): + journal.write_bytes(damaged + b'\n' + b'{"meta":{"title":"recovered"}}\n' + b'{"append_messages":[') + for preview in (True, False): + result = project(path, preview) + assert result['title'] == 'recovered' + # A torn append without newline can be followed by complete canonical + # entries. Recover both metadata and messages, as Rust journal replay does. + first = b'{"meta":{"title":"salvaged"},"append_messages":[{"role":"user","content":[{"type":"text","text":"kept"}]}]}' + second = b'{"meta":{"title":"newest"},"append_messages":[]}' + for torn in (b'{"meta":{', b'garbage', b'{"meta":{"title":"torn'): + journal.write_bytes(torn + first + second + b'\n') + for preview in (True, False): + result = project(path, preview) + assert result['title'] == 'newest' + if preview: + assert result['messages'][0]['content'][0]['text'] == 'kept' + + +for malformed in (b'{', b'[', b'{"x":"unterminated}', b'["escaped\\"', b'{]', b'[{]}'): + try: + value_end(malformed, 0) + except (ValueError, IndexError): + pass + else: + raise AssertionError(repr(malformed)) +"#; + let mut child = std::process::Command::new("python3") + .arg("-") + .stdin(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + let definitions = REMOTE_PROJECTOR.split("if __name__ ==").next().unwrap(); + let mut input = child.stdin.take().unwrap(); + write!(input, "{definitions}\n{checks}").unwrap(); + drop(input); + let output = child.wait_with_output().unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); +} + +fn run_projection(home: &std::path::Path, mode: &str, id: &str) -> std::process::Output { + let output = projection_output(home, mode, id); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + output +} + +fn projection_output(home: &std::path::Path, mode: &str, id: &str) -> std::process::Output { + let mut child = std::process::Command::new("python3") + .args(["-", mode, id]) + .env("JCODE_HOME", home) + .env_remove("JCODE_RUNTIME_DIR") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .expect("Python is required by the remote projection contract"); + child + .stdin + .take() + .unwrap() + .write_all(REMOTE_PROJECTOR.as_bytes()) + .unwrap(); + child.wait_with_output().unwrap() +} + +#[test] +fn oversized_remote_snapshot_is_bounded_valid_and_keeps_late_journal_updates() { + let home = tempfile::tempdir().unwrap(); + let sessions = home.path().join("sessions"); + std::fs::create_dir(&sessions).unwrap(); + let id = "oversized_preview"; + let session = Session::create_with_id(id.into(), None, Some("Old title".into())); + let mut snapshot = serde_json::to_value(&session).unwrap(); + snapshot["migration_epoch"] = serde_json::json!(19); + snapshot["messages"] = serde_json::json!([{ + "id": "huge", "role": "user", "content": [{"type": "text", "text": "x".repeat(65 * 1024 * 1024)}] + }]); + let path = sessions.join(format!("{id}.json")); + serde_json::to_writer(std::fs::File::create(&path).unwrap(), &snapshot).unwrap(); + drop(snapshot); + let before = std::fs::metadata(&path).unwrap(); + assert!(before.len() > 64 * 1024 * 1024); + let first_preview = run_projection(home.path(), "preview", id); + assert!(first_preview.stdout.len() < 2 * 1024 * 1024); + let (first, _) = parse_preview(&first_preview.stdout) + .expect("oversized individual message projection must remain valid"); + assert_eq!(first.messages.len(), 1); + assert_eq!(first.messages[0].id, "huge"); + let mut journal = std::fs::File::create(sessions.join(format!("{id}.journal.jsonl"))).unwrap(); + for i in 0..100 { + writeln!(journal, "{}", serde_json::json!({ + "meta": {"title": format!("Late journal title {i}"), "working_dir": "/latest/repository", "updated_at": "2026-11-08T05:00:00Z"}, + "append_messages": [{"id": format!("late-{i}"), "role": "user", "content": [{"type": "text", "text": format!("Late journal prompt {i} escaped quote \" slash \\ nested {{[}}] 雪")}]}] + })).unwrap(); + } + drop(journal); + let preview = run_projection(home.path(), "preview", id); + assert!(preview.stdout.len() < 2 * 1024 * 1024); + let (loaded, _) = + parse_preview(&preview.stdout).expect("projection must remain valid Session JSON"); + assert_eq!(loaded.title.as_deref(), Some("Late journal title 99")); + assert_eq!(loaded.messages.len(), 80); + assert_eq!(loaded.messages.last().unwrap().id, "late-99"); + assert_eq!(loaded.migration_epoch, 19); + assert!(build_messages_preview(&loaded).iter().any(|m| { + m.content + .contains("Late journal prompt 99 escaped quote \" slash \\ nested {[}] 雪") + })); + let inventory = run_projection(home.path(), "inventory", id); + let inventory: serde_json::Value = serde_json::from_slice(&inventory.stdout).unwrap(); + assert_eq!(inventory["sessions"][0]["title"], "Late journal title 99"); + assert_eq!(inventory["sessions"][0]["cwd"], "/latest/repository"); + assert_eq!(inventory["sessions"][0]["epoch"], 19); + assert_eq!( + inventory["sessions"][0]["updated_at"], + "2026-11-08T05:00:00Z" + ); + assert_eq!(std::fs::metadata(&path).unwrap().len(), before.len()); + assert_eq!( + std::fs::metadata(&path).unwrap().modified().unwrap(), + before.modified().unwrap() + ); + assert_eq!(std::fs::read_dir(&sessions).unwrap().count(), 2); +} + +#[test] +fn remote_projection_ignores_stale_sidecar_and_respects_away_lease() { + let home = tempfile::tempdir().unwrap(); + let sessions = home.path().join("sessions"); + std::fs::create_dir(&sessions).unwrap(); + let session = Session::create_with_id("authority".into(), None, Some("Canonical title".into())); + serde_json::to_writer( + std::fs::File::create(sessions.join("authority.json")).unwrap(), + &session, + ) + .unwrap(); + std::fs::write( + sessions.join("authority.meta.json"), + br#"{"title":"Stale sidecar","migration_epoch":999}"#, + ) + .unwrap(); + let result = run_projection(home.path(), "inventory", "authority"); + let result: serde_json::Value = serde_json::from_slice(&result.stdout).unwrap(); + assert_eq!(result["sessions"][0]["title"], "Canonical title"); + assert_eq!(result["sessions"][0]["epoch"], 0); + std::fs::create_dir(home.path().join("session_leases")).unwrap(); + std::fs::write( + home.path().join("session_leases/authority.json"), + br#"{"away_host":"other-owner","epoch":12}"#, + ) + .unwrap(); + let result = run_projection(home.path(), "inventory", "authority"); + let result: serde_json::Value = serde_json::from_slice(&result.stdout).unwrap(); + assert!(result["sessions"].as_array().unwrap().is_empty()); +} diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py new file mode 100644 index 00000000000..6b645a0f6d6 --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py @@ -0,0 +1,286 @@ +# Read-only remote projection. No sidecars or canonical session writes. +import collections +import json +import mmap +import os +from pathlib import Path +import re +import socket +import sys +import time + + +def timing(stage, outcome, started): + # Fixed labels only. Never print exceptions, identifiers or projected data. + print('[CLOUD_TIMING] stage={} outcome={} duration_ms={}'.format( + stage, outcome, int((time.monotonic() - started) * 1000)), file=sys.stderr) + + +def timed(stage): + def decorate(function): + def run(*args, **kwargs): + started = time.monotonic() + outcome = 'error' + try: + result = function(*args, **kwargs) + outcome = 'success' + return result + finally: + timing(stage, outcome, started) + return run + return decorate + +# Consume complete strings in C, including escaped quotes and backslashes. +STRING = re.compile(br'"[^"\\]*(?:\\.[^"\\]*)*"', re.DOTALL) +# Coalesce adjacent strings, keys, numbers and punctuation into one C match. +# Only container delimiters and malformed quotes need a Python iteration. +STRUCT = re.compile(br'(?P(?:"[^"\\]*(?:\\.[^"\\]*)*"|[^"{}\[\]]+){1,64})|(?P[{}\[\]])|(?P")', re.DOTALL) +SPACE = b' \r\n\t' +BULKY = {'env_snapshots', 'memory_injections', 'replay_events', 'compaction', 'system_prompt'} +MAX_MESSAGES = 80 +MAX_TEXT = 8192 +MAX_OUTPUT = 2 * 1024 * 1024 +META_FIELDS = set('parent_id title custom_title updated_at provider_session_id model_usage_turn_id provider_key route_api_method model reasoning_effort context_cap subagent_model agent_model_overrides improve_mode autoreview_enabled autojudge_enabled is_canary testing_build working_dir original_working_dir short_name status last_pid last_active_at is_debug saved save_label'.split()) +INVENTORY_FIELDS = {'id', 'migration_epoch', 'working_dir', 'custom_title', 'title', 'updated_at'} + + +def string_end(data, start): + match = STRING.match(data, start) + if match is None: + raise ValueError('unterminated string') + return match.end() + + +def skip_space(data, pos): + while pos < len(data) and data[pos] in SPACE: + pos += 1 + return pos + + +def value_end(data, pos, ranges=None): + if data[pos] == 34: + return string_end(data, pos) + if data[pos] not in (123, 91): + end = pos + while end < len(data) and data[end] not in b',}]\r\n \t': + end += 1 + return end + stack = [125 if data[pos] == 123 else 93] + for match in STRUCT.finditer(data, pos + 1): + kind = match.lastgroup + if kind == 'skip': + continue + if kind == 'bad': + raise ValueError('unterminated string') + char = data[match.start()] + if char in (123, 91): + if ranges is not None and len(stack) == 1: + begin = match.start() + stack.append(125 if char == 123 else 93) + elif char != stack.pop(): + raise ValueError('mismatched container') + elif not stack: + return match.end() + elif ranges is not None and len(stack) == 1: + ranges.append((begin, match.end())) + raise ValueError('unterminated container') + + +def fields(data, pos=0, message_ranges=None): + pos = skip_space(data, pos) + if data[pos] != 123: + raise ValueError('expected object') + pos += 1 + while True: + pos = skip_space(data, pos) + if data[pos] == 125: + return pos + 1 + end = string_end(data, pos) + key = json.loads(data[pos:end]) + pos = skip_space(data, end) + if data[pos] != 58: + raise ValueError('expected colon') + pos = skip_space(data, pos + 1) + ranges = message_ranges if key in ('messages', 'append_messages') else None + if ranges is not None: + ranges.clear() + if data[pos] != 91: + raise ValueError('expected message array') + end = value_end(data, pos, ranges) + yield key, pos, end + pos = skip_space(data, end) + if data[pos] == 125: + return pos + 1 + if data[pos] != 44: + raise ValueError('expected comma') + pos += 1 + + +def journal_records(data): + # Journal records are newline delimited. Validate each record's field spans + # before publishing any of it. Match Rust's canonical-entry salvage after + # a torn append, including complete entries glued onto the same line. + pos = 0 + while pos < len(data): + pos = skip_space(data, pos) + if pos == len(data): + break + line_stop = data.find(b'\n', pos) + if line_stop < 0: + line_stop = len(data) + scanner = fields(data, pos) + pending = [] + try: + while True: + pending.append(next(scanner)) + except StopIteration as end: + if end.value is None or end.value > line_stop: + candidate = data.find(b'{"meta":', pos + 1, line_stop) + pos = candidate if candidate >= 0 else line_stop + 1 + continue + pos = end.value + except (ValueError, IndexError): + candidate = data.find(b'{"meta":', pos + 1, line_stop) + pos = candidate if candidate >= 0 else line_stop + 1 + continue + yield pending + + +def journal_fields(data): + for record in journal_records(data): + yield from record + + +def recent_messages(data, start, end, messages, ranges=None): + # Locate the tail without deserializing old message bodies. The range deque + # is bounded even for snapshots with millions of messages. + if ranges is None: + ranges = collections.deque(maxlen=MAX_MESSAGES) + pos = skip_space(data, start + 1) + while pos < end and data[pos] != 93: + stop = value_end(data, pos) + ranges.append((pos, stop)) + pos = skip_space(data, stop) + if data[pos] == 44: + pos = skip_space(data, pos + 1) + for pos, stop in ranges: + # Oversized individual messages are projected without decoding their + # potentially enormous tool payloads. Recent ordinary text is retained. + if stop - pos <= 16 * 1024: + messages.append(json.loads(data[pos:stop])) + continue + message = {} + for key, begin, finish in fields(data, pos): + if key in ('id', 'role') and finish - begin < 4096: + message[key] = json.loads(data[begin:finish]) + elif key == 'content': + text = [] + block = skip_space(data, begin + 1) + while block < finish and data[block] != 93: + block_end = value_end(data, block) + for name, a, b in fields(data, block): + if name == 'text' and sum(map(len, text)) < MAX_TEXT: + # Decode only bounded strings, otherwise use a valid + # preview marker instead of slicing JSON mid-string. + text.append(json.loads(data[a:b]) if b - a <= MAX_TEXT else '[large text omitted from preview]') + block = skip_space(data, block_end) + if data[block] == 44: + block = skip_space(data, block + 1) + message['content'] = [{'type': 'text', 'text': '\n'.join(text)[:MAX_TEXT]}] + messages.append(message) + + +@timed('projection') +def project(path, preview): + session = {} + messages = collections.deque(maxlen=MAX_MESSAGES) + with path.open('rb') as source, mmap.mmap(source.fileno(), 0, access=mmap.ACCESS_READ) as data: + ranges = collections.deque(maxlen=MAX_MESSAGES) if preview else None + for key, start, end in fields(data, message_ranges=ranges): + if key == 'messages': + if preview: + recent_messages(data, start, end, messages, ranges) + elif key not in BULKY and (preview or key in INVENTORY_FIELDS) and end - start <= 256 * 1024: + session[key] = json.loads(data[start:end]) + journal = path.with_suffix('.journal.jsonl') + if journal.exists() and journal.stat().st_size: + with journal.open('rb') as source, mmap.mmap(source.fileno(), 0, access=mmap.ACCESS_READ) as data: + for record in journal_records(data): + pending_meta = {} + pending_messages = collections.deque(maxlen=MAX_MESSAGES) + try: + for key, start, stop in record: + if key == 'meta': + meta = {} + route_present = False + for name, a, b in fields(data, start): + if name == 'route_api_method_present': + route_present = data[a:b] == b'true' + if name in META_FIELDS and (preview or name in INVENTORY_FIELDS) and b - a <= 256 * 1024: + meta[name] = json.loads(data[a:b]) + if not route_present: + meta.pop('route_api_method', None) + pending_meta.update(meta) + elif key == 'append_messages' and preview: + recent_messages(data, start, stop, pending_messages) + except (ValueError, IndexError, TypeError): + continue + session.update(pending_meta) + messages.extend(pending_messages) + if preview: + session['messages'] = list(messages) + for key in ('env_snapshots', 'memory_injections', 'replay_events'): + session[key] = [] + return session + + +@timed('remote_request') +def main(): + mode, ids = sys.argv[1:3] + base = Path(os.environ.get('JCODE_HOME', str(Path.home() / '.jcode'))) + result = [] + for sid in ids.splitlines(): + started = time.monotonic() + if not sid or not all(c.isascii() and (c.isalnum() or c in '_-') for c in sid): + timing('inventory_entry', 'skipped_invalid_target', started) + if mode == 'preview': + raise ValueError('invalid session id') + continue + try: + session = project(base / 'sessions' / (sid + '.json'), mode == 'preview') + if session.get('id') != sid: + raise ValueError('session identity mismatch') + if mode == 'preview': + encoded = json.dumps(session, separators=(',', ':'), ensure_ascii=False).encode() + while len(encoded) > MAX_OUTPUT and session['messages']: + session['messages'].pop(0) + encoded = json.dumps(session, separators=(',', ':'), ensure_ascii=False).encode() + if len(encoded) > MAX_OUTPUT: + raise ValueError('preview metadata too large') + sys.stdout.buffer.write(encoded + b'\0\0') + state = Path(os.environ['JCODE_RUNTIME_DIR']) / 'durable-state' if os.environ.get('JCODE_RUNTIME_DIR') else base / 'state' + telemetry = state / 'session-telemetry' / (sid + '.json') + if telemetry.exists() and telemetry.stat().st_size < 65536: + sys.stdout.buffer.write(telemetry.read_bytes()) + return + lease_path = base / 'session_leases' / (sid + '.json') + lease = json.loads(lease_path.read_bytes()) if lease_path.exists() else {} + if lease.get('away_host'): + timing('inventory_entry', 'skipped_away', started) + continue + result.append({'session_id': sid, 'epoch': max(session.get('migration_epoch', 0), lease.get('epoch', 0)), 'cwd': session.get('working_dir'), 'title': session.get('custom_title') if session.get('custom_title') is not None else session.get('title'), 'updated_at': session.get('updated_at')}) + timing('inventory_entry', 'success', started) + except (OSError, ValueError, TypeError, IndexError): + timing('inventory_entry' if mode != 'preview' else 'remote_preview', 'error', started) + if mode == 'preview': + raise + print(json.dumps({'host': socket.gethostname(), 'sessions': result})) + + +if __name__ == '__main__': + try: + main() + except Exception: + # Tracebacks include paths and may quote private input. The stage timer + # already records failure, and the exit status preserves the contract. + sys.exit(1) diff --git a/crates/jcode-tui/src/tui/session_picker/filter.rs b/crates/jcode-tui/src/tui/session_picker/filter.rs index aa1da2ef9a8..07b55e96233 100644 --- a/crates/jcode-tui/src/tui/session_picker/filter.rs +++ b/crates/jcode-tui/src/tui/session_picker/filter.rs @@ -6,12 +6,29 @@ pub(super) struct SearchSelection { follow_until: Option, last_publish: Option, unpublished_results: bool, + /// Rows shown before the query changed, in display order. They stay on + /// screen until the worker reports for the new query, so typing never + /// blanks or reshuffles the list between keystrokes. + held: Option>, + /// When the current query started, for delaying busy chrome. + query_changed_at: Option, } +/// Searches that finish sooner than this never show busy chrome, so the help +/// line and border do not blink on every keystroke. +const BUSY_CHROME_DELAY: std::time::Duration = std::time::Duration::from_millis(150); + impl SearchSelection { - fn update_query(&mut self, query: &str) { + /// `shown` lists the rows currently on screen, read only on a change. + fn update_query(&mut self, query: &str, shown: impl FnOnce() -> Vec) { let query = query.trim().to_lowercase(); if self.query != query { + if query.is_empty() { + self.held = None; + } else if self.held.is_none() { + self.held = Some(shown()); + } + self.query_changed_at = Some(std::time::Instant::now()); self.follow_until = (!query.is_empty()) .then(|| std::time::Instant::now() + std::time::Duration::from_secs(1)); self.query = query; @@ -29,6 +46,27 @@ impl SearchSelection { self.follow_until = None; } + /// The list still shows the previous query's rows. + pub(super) fn holding(&self) -> bool { + self.held.is_some() + } + + /// Auto-focus was not overridden by manual navigation for this query. + fn wants_focus(&self) -> bool { + self.follow_until.is_some() + } + + /// Whether a still-running search has lasted long enough to say so. + pub(super) fn busy_visible(&self) -> bool { + self.query_changed_at + .is_none_or(|at| at.elapsed() >= BUSY_CHROME_DELAY) + } + + #[cfg(test)] + pub(super) fn show_busy_for_test(&mut self) { + self.query_changed_at = None; + } + /// Coalesce worker batches into at most eight list/preview updates per /// second. Completion always flushes, including a channel disconnect with /// no new results in that poll. @@ -65,7 +103,7 @@ mod search_selection_tests { #[test] fn progressive_batches_wait_for_publish_interval_but_completion_flushes() { let mut selection = SearchSelection::default(); - selection.update_query("needle"); + selection.update_query("needle", Vec::new); assert!(!selection.publish_results(true, true)); assert!(!selection.publish_results(true, true)); selection.last_publish = @@ -80,36 +118,36 @@ mod search_selection_tests { #[test] fn changing_query_discards_unpublished_results() { let mut selection = SearchSelection::default(); - selection.update_query("old"); + selection.update_query("old", Vec::new); assert!(!selection.publish_results(true, true)); - selection.update_query("new"); + selection.update_query("new", Vec::new); assert!(!selection.publish_results(false, false)); } #[test] fn follows_for_one_second_without_extending_on_rebuild() { let mut selection = SearchSelection::default(); - selection.update_query("needle"); + selection.update_query("needle", Vec::new); assert!(selection.following()); let deadline = selection.follow_until.unwrap(); - selection.update_query("needle"); + selection.update_query("needle", Vec::new); assert_eq!(selection.follow_until, Some(deadline)); selection.follow_until = Some(std::time::Instant::now()); assert!(!selection.following()); - selection.update_query("needle"); + selection.update_query("needle", Vec::new); assert!( !selection.following(), "same query must not restart an expired window" ); - selection.update_query("another query"); + selection.update_query("another query", Vec::new); assert!(selection.following()); selection.stop(); - selection.update_query("another query"); + selection.update_query("another query", Vec::new); assert!( !selection.following(), "refresh must not undo a manual override" ); - selection.update_query(""); + selection.update_query("", Vec::new); assert!(!selection.following()); } } @@ -178,38 +216,65 @@ impl SessionPicker { return self.all_session_refs(); } - // Keep literal matches immediate. Expensive typo-tolerant matching is - // scheduled immediately on the worker alongside transcript search, - // never on the UI thread. Reconsider all sessions for new worker results. + // Only consume worker evidence here. Metadata and structured identity + // matching run on the worker, regardless of history size. let candidates = self.all_session_refs(); - let tokens = loading::search_query_tokens(&normalized); - let matches = candidates .into_iter() .filter(|session_ref| { - self.session_by_ref(*session_ref).is_some_and(|session| { - identity_search::rank(session, &tokens).0 - || (!self.transcript_search.defers_metadata() - && tokens - .iter() - .all(|token| session.search_index.contains(token))) - || self.transcript_search.contains(&session.id) - }) + self.session_by_ref(*session_ref) + .is_some_and(|session| self.transcript_search.contains(&session.id)) }) .collect::>(); + // Until the worker reports for this query, keep the rows already on + // screen instead of blanking the list or showing a partial one. + let hold = self.search_selection.holding() + && self.transcript_search.pending() + && !self.transcript_search.has_results() + && (self.transcript_search.defers_metadata() || matches.is_empty()); + let matches = match self.search_selection.held.take() { + Some(held) if hold => { + let by_id: std::collections::HashMap<&str, SessionRef> = self + .all_session_refs() + .into_iter() + .filter_map(|session_ref| { + Some((self.session_by_ref(session_ref)?.id.as_str(), session_ref)) + }) + .collect(); + let refs = held + .iter() + .filter_map(|id| by_id.get(id.as_str()).copied()) + .collect(); + self.search_selection.held = Some(held); + refs + } + _ => matches, + }; + self.cached_search_query = normalized; self.cached_search_refs = matches.clone(); matches } + /// A running search has lasted long enough to be worth announcing. + pub(super) fn search_busy_visible(&self) -> bool { + self.transcript_search.pending() && self.search_selection.busy_visible() + } + /// Called once per rendered frame, so completed scans update results even /// when the user is not typing. Never wait for the worker on the UI thread. pub(super) fn poll_transcript_search(&mut self) { + if std::mem::take(&mut self.search_edit_pending) { + self.rebuild_items(); + } let changed = self.transcript_search.poll(); + // A search that ends without any result must still release held rows. + let release = self.search_selection.holding() && !self.transcript_search.pending(); if self .search_selection .publish_results(changed, self.transcript_search.pending()) + || release { let selected = self.selected_session().map(|session| session.id.clone()); let preview_scroll = self.scroll_offset; @@ -240,51 +305,26 @@ impl SessionPicker { }) }) .collect::>(); + if self.search_selection.holding() { + // Held rows keep the order they were last displayed in. + return filtered; + } - filtered.sort_by(|a, b| { - let a = self - .session_by_ref(*a) - .map(|session| session.last_message_time) - .unwrap_or_default(); - let b = self - .session_by_ref(*b) - .map(|session| session.last_message_time) - .unwrap_or_default(); - b.cmp(&a) + filtered.sort_by_cached_key(|session_ref| { + std::cmp::Reverse( + self.session_by_ref(*session_ref) + .map(super::session_resume_time) + .unwrap_or_default(), + ) }); - // Cache once per candidate, never run matching from the comparator. - // Stable sorting keeps recency as the deterministic final tie-break. + // Sort only numeric evidence computed by the worker. Stable sorting + // keeps recency as the deterministic final tie-break. if !self.search_query.trim().is_empty() { - let normalized = Self::normalized_search_query(&self.search_query); - let tokens = loading::search_query_tokens(&normalized); filtered.sort_by_cached_key(|session_ref| { std::cmp::Reverse(self.session_by_ref(*session_ref).map(|session| { - let rank = if self.transcript_search.contains(&session.id) { - self.transcript_search.rank(&session.id) - } else if self.transcript_search.defers_metadata() { - transcript_search::SearchRank::default() - } else { - transcript_search::literal_metadata_rank(&session.search_index, &tokens) - .with_exact_field(transcript_search::exact_metadata_field( - session, - &normalized, - )) - }; - // A renamed conversation's opening request is stronger - // intent evidence than incidental matches in tool output. - let fields = [ - session.title.as_str(), - session.save_label.as_deref().unwrap_or(""), - session.first_user_prompt.as_deref().unwrap_or(""), - ]; - let fields = fields.map(transcript_search::lowercase_field); - let intent_terms = tokens - .iter() - .filter(|token| fields.iter().any(|field| field.contains(token.as_str()))) - .count(); ( - identity_search::rank(session, &tokens), - rank.with_intent_terms(intent_terms), + self.transcript_search.identity_rank(&session.id), + self.transcript_search.rank(&session.id), ) })) }); @@ -392,14 +432,42 @@ impl SessionPicker { /// Rebuild the items list based on current filters. pub(super) fn rebuild_items(&mut self) { + self.search_edit_pending = false; + let selected = self.selected_session().map(|session| session.id.clone()); + let (scroll, auto_scroll) = (self.scroll_offset, self.auto_scroll_preview); + self.rebuild_items_inner(); + // Held rows keep their preview position too, so the pane does not + // jump while the next results are still being computed. + if self.search_selection.holding() + && self.selected_session().map(|session| &session.id) == selected.as_ref() + { + self.scroll_offset = scroll; + self.auto_scroll_preview = auto_scroll; + } + } + + fn rebuild_items_inner(&mut self) { let current_selected_id = self.selected_session().map(|session| session.id.clone()); let show_test = self.show_test_sessions; let filter_mode = self.filter_mode; let search_query = self.search_query.clone(); - self.search_selection.update_query(&search_query); - let current_selected_id = - current_selected_id.filter(|_| !self.search_selection.following()); + let mut selection = std::mem::take(&mut self.search_selection); + selection.update_query(&search_query, || { + self.visible_sessions + .iter() + .filter_map(|session_ref| Some(self.session_by_ref(*session_ref)?.id.clone())) + .collect() + }); + self.search_selection = selection; + let was_holding = self.search_selection.holding(); let search_matches = self.search_matched_session_refs(&search_query); + // Focus the best match while following the query, and when held rows + // give way to real results. Held rows themselves keep the selection. + let holding = self.search_selection.holding(); + let focus_best = !holding + && (self.search_selection.following() + || (was_holding && self.search_selection.wants_focus())); + let current_selected_id = current_selected_id.filter(|_| !focus_best); let filtered_refs = self.filtered_session_refs(&search_matches, show_test, filter_mode); self.items.clear(); @@ -459,16 +527,12 @@ impl SessionPicker { } } - saved_sessions.sort_by(|a, b| { - let a = self - .session_by_ref(*a) - .map(|session| session.last_message_time) - .unwrap_or_default(); - let b = self - .session_by_ref(*b) - .map(|session| session.last_message_time) - .unwrap_or_default(); - b.cmp(&a) + saved_sessions.sort_by_cached_key(|session_ref| { + std::cmp::Reverse( + self.session_by_ref(*session_ref) + .map(super::session_resume_time) + .unwrap_or_default(), + ) }); if !saved_sessions.is_empty() { diff --git a/crates/jcode-tui/src/tui/session_picker/identity_search.rs b/crates/jcode-tui/src/tui/session_picker/identity_search.rs index 275cf1f3067..e6b8fe9e763 100644 --- a/crates/jcode-tui/src/tui/session_picker/identity_search.rs +++ b/crates/jcode-tui/src/tui/session_picker/identity_search.rs @@ -1,30 +1,39 @@ //! Names and locations describe the session itself, unlike transcript mentions. -use super::{SessionInfo, transcript_search::lowercase_field}; +#[cfg(test)] +use super::SessionInfo; +use super::transcript_search::lowercase_field; /// Complete structured matches lead. An exact animal/name and actual cloud /// ownership break ties, so `swan cloud` finds the cloud swan even if renamed. /// Partial identity matches must not elevate unrelated transcript results. +#[cfg(test)] pub(super) fn rank(session: &SessionInfo, tokens: &[String]) -> (bool, bool, bool) { + rank_fields( + [ + session.id.as_str(), + session.short_name.as_str(), + session.title.as_str(), + session.save_label.as_deref().unwrap_or(""), + session.working_dir.as_deref().unwrap_or(""), + session.server_name.as_deref().unwrap_or(""), + ], + session.server_icon.as_deref() == Some("☁"), + tokens, + ) +} + +pub(super) fn rank_fields(fields: [&str; 6], cloud: bool, tokens: &[String]) -> (bool, bool, bool) { if tokens.is_empty() { return (false, false, false); } - let cloud = session.server_icon.as_deref() == Some("☁"); - let fields = [ - session.id.as_str(), - session.short_name.as_str(), - session.title.as_str(), - session.save_label.as_deref().unwrap_or(""), - session.working_dir.as_deref().unwrap_or(""), - session.server_name.as_deref().unwrap_or(""), - ] - .map(lowercase_field); + let fields = fields.map(lowercase_field); let complete = tokens.iter().all(|token| { (cloud && token == "cloud") || fields.iter().any(|field| field.contains(token.as_str())) }); if !complete { return (false, false, false); } - let name = lowercase_field(&session.short_name); + let name = &fields[1]; let exact_name = tokens.iter().any(|token| token == name.as_ref()); let cloud_location = cloud && tokens.iter().any(|token| token == "cloud"); (true, exact_name, cloud_location) diff --git a/crates/jcode-tui/src/tui/session_picker/loading.rs b/crates/jcode-tui/src/tui/session_picker/loading.rs index f0b27e18368..37d4145e147 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading.rs @@ -21,6 +21,8 @@ use super::{ SEARCH_CONTENT_BUDGET_BYTES, ServerGroup, SessionInfo, }; +mod mobile_inventory; + use super::{ResumeTarget, SessionSource}; #[cfg(test)] @@ -305,6 +307,18 @@ fn load_recently_closed_sessions() -> Vec { /// Shared staged loader for the standalone and in-session pickers. A just-closed /// session is actionable before loading either the cache or the historical list. pub(crate) fn send_session_list_updates(sender: std::sync::mpsc::Sender) { + send_session_list_updates_with_discovery( + sender, + mobile_inventory::available(), + mobile_inventory::refresh, + ); +} + +fn send_session_list_updates_with_discovery( + sender: std::sync::mpsc::Sender, + discovery_available: bool, + refresh: impl FnOnce() -> Result, +) { let recent = load_recently_closed_sessions(); if !recent.is_empty() && sender @@ -345,16 +359,69 @@ pub(crate) fn send_session_list_updates(sender: std::sync::mpsc::Sender result, + Err(error) => { + let _ = sender.send(super::SessionListUpdate::Fresh(Err(error))); + return; + } + }; + if sender + .send(super::SessionListUpdate::Cached(local.clone())) + .is_err() + { + return; + } + let result = match refresh() { + Ok(true) => { + invalidate_session_list_cache(); + load_sessions_grouped().map(|(groups, mut orphans)| { + let loaded: HashSet<_> = orphans + .iter() + .map(|s| s.id.as_str()) + .chain( + groups + .iter() + .flat_map(|g| g.sessions.iter().map(|s| s.id.as_str())), + ) + .collect(); + let recovered: Vec<_> = recent + .into_iter() + .filter(|s| !loaded.contains(s.id.as_str())) + .collect(); + orphans.extend(recovered); + (groups, orphans) + }) + } + Ok(false) => Ok(local), + Err(error) => { + crate::logging::debug(&format!("Mobile resume discovery unavailable: {error:#}")); + let _ = sender.send(super::SessionListUpdate::CloudUnavailable( + "Cloud sessions could not be refreshed. Reopen /resume to retry.".into(), + )); + Ok(local) + } + }; let _ = sender.send(super::SessionListUpdate::Fresh(result)); } #[path = "loading_search_text.rs"] mod search_text; +pub(super) use super::transcript_search::source::SourceBuckets; +use super::transcript_search::source::{BucketSink, Source, Walk, message_role_source, push_mark}; +#[cfg(test)] +use search_text::raw_value_search_excerpt; pub(super) use search_text::{ - build_search_index, push_sampled_search_text, raw_value_display_text, raw_value_search_excerpt, + build_search_index, classified_search_excerpt, classify_json_into, push_sampled_search_text, + raw_value_display_text, }; #[cfg(test)] @@ -497,6 +564,39 @@ fn contains_ascii_case_insensitive_bytes(haystack: &[u8], needle_lower: &[u8]) - }) } +/// An external transcript preview plus search text classified by source. +pub(super) struct ExternalPreview { + pub messages: Vec, + pub search: SourceBuckets, +} + +impl ExternalPreview { + fn new() -> Self { + Self { + messages: Vec::new(), + search: SourceBuckets::default(), + } + } + + fn non_empty(self) -> Option { + (!self.messages.is_empty()).then_some(self) + } +} + +/// Search index whose transcript part comes from classified samples. +pub(super) fn build_classified_search_index( + id: &str, + short_name: &str, + title: &str, + working_dir: Option<&str>, + save_label: Option<&str>, + search: &SourceBuckets, +) -> String { + let mut text = String::new(); + search.render(&mut text); + build_search_index_from_summary(id, short_name, title, working_dir, save_label, &text) +} + fn build_search_index_from_summary( id: &str, short_name: &str, @@ -505,22 +605,8 @@ fn build_search_index_from_summary( save_label: Option<&str>, transcript_search_text: &str, ) -> String { - let mut combined = String::new(); - combined.push_str(title); - combined.push(' '); - combined.push_str(short_name); - combined.push(' '); - combined.push_str(id); - - if let Some(dir) = working_dir { - combined.push(' '); - combined.push_str(dir); - } - - if let Some(label) = save_label { - combined.push(' '); - combined.push_str(label); - } + let mut combined = + search_text::search_index_header(id, short_name, title, working_dir, save_label); if !transcript_search_text.is_empty() { combined.push(' '); @@ -789,6 +875,34 @@ fn extract_text_from_value(value: &serde_json::Value) -> String { out.join(" ") } +/// Classifies every OpenCode part, including tool and reasoning parts the +/// preview omits, at the weight of its own block type. +fn classify_opencode_parts( + search: &mut SourceBuckets, + parts_base: &Path, + message_id: &str, + role: Source, +) { + let message_parts = parts_base.join(message_id); + if !message_parts.exists() { + return; + } + for part_path in collect_files_recursive(&message_parts, "json") { + let Ok(file) = std::fs::File::open(&part_path) else { + continue; + }; + let Ok(part) = serde_json::from_reader::<_, serde_json::Value>(file) else { + continue; + }; + let source = match part.get("type").and_then(|v| v.as_str()) { + Some("text") => role, + Some("tool") => Source::ToolInput, + _ => Source::Other, + }; + classify_json_into(search, &part, source); + } +} + /// Extract message body text from OpenCode part files for the session-picker /// preview. Modern OpenCode (Go storage) stores message bodies in /// `storage/part//*.json`; only plain `text` parts are used for the @@ -1209,11 +1323,16 @@ struct SessionMessageSummaryData { assistant_message_count: usize, estimated_tokens: usize, first_user_prompt: Option, - search_text: String, + search_text: SourceBuckets, + /// Newest stored message timestamp, of any role (prompts, replies, tools). + last_message_at: Option>, } impl SessionMessageSummaryData { fn add_message(&mut self, message: &SessionMessageSummary) { + if let Some(ts) = message.timestamp { + self.last_message_at = Some(self.last_message_at.map_or(ts, |cur| cur.max(ts))); + } if !summary_message_is_visible_conversation(message) { return; } @@ -1233,16 +1352,17 @@ impl SessionMessageSummaryData { .estimated_tokens .saturating_add(usage.total_tokens() as usize); } - if let Some(raw_content) = message.content_raw.as_deref() { - push_sampled_search_text( - &mut self.search_text, - raw_content, - INITIAL_TRANSCRIPT_SEARCH_BUDGET_BYTES, - ); + if let Some(content) = &message.content_search { + self.search_text + .merge(content, INITIAL_TRANSCRIPT_SEARCH_BUDGET_BYTES); } } fn merge(&mut self, other: Self) { + self.last_message_at = match (self.last_message_at, other.last_message_at) { + (Some(a), Some(b)) => Some(a.max(b)), + (a, b) => a.or(b), + }; self.visible_message_count = self .visible_message_count .saturating_add(other.visible_message_count); @@ -1256,11 +1376,8 @@ impl SessionMessageSummaryData { if self.first_user_prompt.is_none() { self.first_user_prompt = other.first_user_prompt; } - push_sampled_search_text( - &mut self.search_text, - &other.search_text, - INITIAL_TRANSCRIPT_SEARCH_BUDGET_BYTES, - ); + self.search_text + .merge(&other.search_text, INITIAL_TRANSCRIPT_SEARCH_BUDGET_BYTES); } } @@ -1326,10 +1443,12 @@ struct SessionMessageSummary { // allocating or walking it. After display metadata is known, we inspect only // the raw prefix for old snapshots that predate `display_role`. content_starts_with_system_reminder: bool, - content_raw: Option, + /// Bounded per-source samples of the content, classified by block type. + content_search: Option, content_text: Option, display_role: Option, token_usage: Option, + timestamp: Option>, } impl<'de> Deserialize<'de> for SessionMessageSummary { @@ -1362,12 +1481,16 @@ impl<'de> Visitor<'de> for SessionMessageSummaryVisitor { let mut content: Option<&'de RawValue> = None; let mut display_role: Option = None; let mut token_usage: Option = None; + let mut timestamp: Option> = None; while let Some(key) = map.next_key::>()? { match key.as_ref() { "role" => { role = Some(map.next_value()?); } + "timestamp" => { + timestamp = map.next_value()?; + } "content" => { content = Some(map.next_value()?); } @@ -1387,8 +1510,9 @@ impl<'de> Visitor<'de> for SessionMessageSummaryVisitor { let content_starts_with_system_reminder = matches!(role, Role::User) && display_role.is_none() && content.is_some_and(raw_content_starts_with_system_reminder); - let content_raw = if display_role.is_none() && !content_starts_with_system_reminder { - content.and_then(|raw| raw_value_search_excerpt(raw, self.content_excerpt_budget)) + let content_search = if display_role.is_none() && !content_starts_with_system_reminder { + content + .and_then(|raw| classified_search_excerpt(raw, &role, self.content_excerpt_budget)) } else { None }; @@ -1400,10 +1524,11 @@ impl<'de> Visitor<'de> for SessionMessageSummaryVisitor { Ok(SessionMessageSummary { role, content_starts_with_system_reminder, - content_raw, + content_search, content_text, display_role, token_usage, + timestamp, }) } } @@ -1702,7 +1827,11 @@ fn parse_jcode_session_info( &title, session.working_dir.as_deref(), session.save_label.as_deref(), - &session.messages.search_text, + &{ + let mut text = String::new(); + session.messages.search_text.render(&mut text); + text + }, ); Some(SessionInfo { @@ -1715,7 +1844,12 @@ fn parse_jcode_session_info( user_message_count, assistant_message_count, created_at: session.created_at, - last_message_time: session.updated_at, + // Newest message, falling back to the save time for old snapshots + // whose messages predate per-message timestamps (or empty sessions). + last_message_time: session + .messages + .last_message_at + .unwrap_or(session.updated_at), last_active_at: session.last_active_at, working_dir: session.working_dir, model: session.model, @@ -1907,7 +2041,7 @@ fn load_sessions_inner(bypass_cache: bool) -> Result> { /// ordinary conversations out of the most recent results. Saved and cloud /// sessions compete by recency too, rather than reserving extra visible slots. fn limit_sessions_by_recency(sessions: &mut Vec, limit: usize) { - sessions.sort_by(|a, b| b.last_message_time.cmp(&a.last_message_time)); + sessions.sort_by_cached_key(|s| std::cmp::Reverse(super::session_resume_time(s))); let mut visible = 0; let mut debug = 0; sessions.retain(|session| { @@ -1937,7 +2071,10 @@ fn load_external_sessions(enabled: bool, scan_limit: usize) -> Vec let mut external = Vec::new(); for handle in [claude, codex, pi, opencode, cursor] { - external.extend(handle.join().unwrap_or_default()); + match handle.join() { + Ok(sessions) => external.extend(sessions), + Err(_) => crate::logging::warn("External session loader panicked"), + } } external }) @@ -2020,12 +2157,17 @@ fn load_external_claude_code_sessions(scan_limit: usize) -> Vec { .collect() } +#[cfg(test)] pub(super) fn load_claude_code_preview_from_path(path: &Path) -> Option> { + load_claude_code_external_preview(path).map(|preview| preview.messages) +} + +pub(super) fn load_claude_code_external_preview(path: &Path) -> Option { // Only parse the tail of the transcript (see `load_codex_preview_from_path`): // the preview shows the last ~20 messages, so reparsing multi-MB transcripts // on every selection change made picker navigation lag. let (text, truncated) = read_file_tail_text(path, EXTERNAL_PREVIEW_TAIL_BYTES)?; - let mut preview = Vec::new(); + let mut out = ExternalPreview::new(); // If we seeked into the middle of the file, the first line is a partial // record; drop it. When we read the whole file the first line is a real @@ -2057,16 +2199,14 @@ pub(super) fn load_claude_code_preview_from_path(path: &Path) -> Option Option> { let sessions = match crate::import::list_claude_code_sessions() { Ok(sessions) => sessions, @@ -2083,6 +2223,14 @@ pub(super) fn load_claude_code_preview(session_id: &str) -> Option Option { + let session = crate::import::list_claude_code_sessions() + .ok()? + .into_iter() + .find(|session| session.session_id == session_id)?; + load_claude_code_external_preview(Path::new(&session.full_path)) +} + fn load_external_codex_sessions(scan_limit: usize) -> Vec { let Ok(root) = crate::storage::user_home_path(".codex/sessions") else { return Vec::new(); @@ -2210,12 +2358,17 @@ fn find_codex_session_file(session_id: &str) -> Option { None } +#[cfg(test)] pub(super) fn load_codex_preview_from_path(path: &Path) -> Option> { + load_codex_external_preview(path).map(|preview| preview.messages) +} + +pub(super) fn load_codex_external_preview(path: &Path) -> Option { // Only parse the tail of the transcript: the preview shows the last ~20 // messages, and these rollout files can be tens of MB, so reading the whole // file on every selection change made picker navigation lag. let (text, _truncated) = read_file_tail_text(path, EXTERNAL_PREVIEW_TAIL_BYTES)?; - let mut preview = Vec::new(); + let mut out = ExternalPreview::new(); // When we read from the start we skip the first line (the `session_meta` // record). When we read a tail slice the first line is almost certainly a @@ -2243,6 +2396,9 @@ pub(super) fn load_codex_preview_from_path(path: &Path) -> Option Option Option> { let path = find_codex_session_file(session_id)?; load_codex_preview_from_path(&path) } +pub(super) fn load_codex_external_preview_by_id(session_id: &str) -> Option { + load_codex_external_preview(&find_codex_session_file(session_id)?) +} + #[path = "loading/pi_sessions.rs"] mod pi_sessions; use pi_sessions::load_external_pi_sessions; +pub(super) use pi_sessions::load_pi_external_preview; +#[cfg(test)] pub(super) use pi_sessions::load_pi_preview_from_path; fn load_external_opencode_sessions(scan_limit: usize) -> Vec { let Ok(root) = crate::storage::user_home_path(".local/share/opencode/storage/session") else { @@ -2293,11 +2453,17 @@ fn load_external_opencode_sessions(scan_limit: usize) -> Vec { .collect() } +#[cfg(test)] pub(super) fn load_opencode_preview_from_path(path: &Path) -> Option> { - load_opencode_session_info(path) - .ok() - .flatten() - .map(|session| session.messages_preview) + load_opencode_external_preview(path).map(|preview| preview.messages) +} + +pub(super) fn load_opencode_external_preview(path: &Path) -> Option { + let (session, search) = load_opencode_session_info(path).ok().flatten()?; + Some(ExternalPreview { + messages: session.messages_preview, + search, + }) } fn load_opencode_session_stub(path: &Path) -> Result> { @@ -2387,7 +2553,7 @@ fn load_opencode_session_stub(path: &Path) -> Result> { })) } -fn load_opencode_session_info(path: &Path) -> Result> { +fn load_opencode_session_info(path: &Path) -> Result> { let value: serde_json::Value = serde_json::from_reader(File::open(path)?)?; let session_id = value .get("id") @@ -2431,6 +2597,7 @@ fn load_opencode_session_info(path: &Path) -> Result> { ))?; let parts_base = crate::storage::user_home_path(".local/share/opencode/storage/part")?; let mut preview = Vec::new(); + let mut search = SourceBuckets::default(); let mut user_message_count = 0usize; let mut assistant_message_count = 0usize; let mut provider_key: Option = Some("opencode".to_string()); @@ -2449,6 +2616,9 @@ fn load_opencode_session_info(path: &Path) -> Result> { .unwrap_or_default(); // Modern OpenCode (Go storage) stores body text in part files keyed // by message id; fall back to the legacy inline summary. + if let Some(id) = msg_value.get("id").and_then(|v| v.as_str()) { + classify_opencode_parts(&mut search, &parts_base, id, message_role_source(role)); + } let text = msg_value .get("id") .and_then(|v| v.as_str()) @@ -2488,20 +2658,29 @@ fn load_opencode_session_info(path: &Path) -> Result> { "opencode {}", jcode_core::util::truncate_str(&session_id, 8) ); - let search_index = build_search_index( + if search.is_empty() { + for msg in &preview { + search.push( + message_role_source(&msg.role), + &msg.content, + SEARCH_CONTENT_BUDGET_BYTES, + ); + } + } + let search_index = build_classified_search_index( &format!("opencode:{session_id}"), &short_name, &title, working_dir.as_deref(), None, - &preview, + &search, ); let first_user_prompt = preview .iter() .find(|msg| msg.role == "user" && !msg.content.trim().is_empty()) .map(|msg| msg.content.clone()); - Ok(Some(SessionInfo { + let session = SessionInfo { id: format!("opencode:{session_id}"), parent_id: None, short_name, @@ -2535,7 +2714,8 @@ fn load_opencode_session_info(path: &Path) -> Result> { }, external_path: Some(path.to_string_lossy().to_string()), todo_statuses: Vec::new(), - })) + }; + Ok(Some((session, search))) } fn load_external_cursor_sessions(scan_limit: usize) -> Vec { @@ -2553,12 +2733,17 @@ fn load_external_cursor_sessions(scan_limit: usize) -> Vec { .collect() } +#[cfg(test)] pub(super) fn load_cursor_preview_from_path(path: &Path) -> Option> { + load_cursor_external_preview(path).map(|preview| preview.messages) +} + +pub(super) fn load_cursor_external_preview(path: &Path) -> Option { // Only parse the tail of the transcript like the other external CLIs: the // preview shows the last ~20 messages, so reparsing large transcripts on // every selection change would make picker navigation lag. let (text, truncated) = read_file_tail_text(path, EXTERNAL_PREVIEW_TAIL_BYTES)?; - let mut preview = Vec::new(); + let mut out = ExternalPreview::new(); let skip = usize::from(truncated); for line in text.lines().skip(skip) { let trimmed = line.trim(); @@ -2584,13 +2769,10 @@ pub(super) fn load_cursor_preview_from_path(path: &Path) -> Option Result> { @@ -2762,11 +2944,34 @@ fn include_cloud_sessions( sessions.push(session); } if let Some(session) = sessions.iter_mut().find(|session| session.id == id) { + // A migrated local snapshot is historical. Verified cloud metadata + // must win for display, project scoping, sorting, and search. + if let Some(title) = mobile_inventory::title(id) { + session.title = title; + session.working_dir = lease.repo_root.clone(); + if let Ok(updated_at) = chrono::DateTime::parse_from_rfc3339(&lease.updated_at) { + let updated_at = updated_at.with_timezone(&chrono::Utc); + session.last_message_time = updated_at; + session.last_active_at = Some(updated_at); + session.needs_catchup = + catchup_seen.needs_catchup(id, updated_at, &session.status); + } + session.search_index = build_search_index_from_summary( + id, + &session.short_name, + &session.title, + session.working_dir.as_deref(), + session.save_label.as_deref(), + &session.search_index, + ); + } session.server_name = Some(format!("cloud: {host}")); session.server_icon = Some("☁".to_string()); + // Host metadata, not transcript text from the preceding section. + push_mark(&mut session.search_index, Source::Other); session .search_index - .push_str(&format!(" cloud {}", host.to_lowercase())); + .push_str(&format!("cloud {}", host.to_lowercase())); } } } @@ -2779,7 +2984,8 @@ fn cloud_session_stub(lease: &storage::SessionLease) -> SessionInfo { let updated_at = chrono::DateTime::parse_from_rfc3339(&lease.updated_at) .map(|time| time.with_timezone(&chrono::Utc)) .unwrap_or_default(); - let title = format!("{short_name} (cloud session)"); + let title = + mobile_inventory::title(id).unwrap_or_else(|| format!("{short_name} (cloud session)")); let search_index = build_search_index( id, &short_name, diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs new file mode 100644 index 00000000000..60cc281aca0 --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs @@ -0,0 +1,220 @@ +//! Discover mobile-created sessions without copying their transcripts locally. +use super::super::cloud_preview::StageTimer; +use super::*; + +#[derive(Deserialize)] +struct VerifiedInventory { + host: String, + sessions: Vec, +} + +#[derive(Deserialize)] +struct VerifiedSession { + session_id: String, + epoch: u64, + cwd: Option, + title: Option, + updated_at: Option, +} + +/// Called only after publishing local results, never on the render thread. +pub(super) fn available() -> bool { + crate::mobile_sync::session_source() != "cloud" + && crate::subscription_catalog::configured_api_key() + .is_some_and(|key| !key.trim().is_empty()) +} + +pub(super) fn refresh() -> Result { + let mut timer = StageTimer::new("inventory_refresh"); + if !available() { + timer.outcome("skipped_unavailable"); + return Ok(false); + } + let home = storage::jcode_dir()?; + let config = home.join("cloud/ssh_config"); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build()?; + let result = runtime.block_on(async { + let mut discovery = StageTimer::new("inventory_discovery"); + let inventory = crate::mobile_sync::discover_cloud_sessions().await?; + discovery.outcome("success"); + drop(discovery); + let candidates: Vec<_> = inventory + .into_iter() + .filter(|s| should_verify(&home, &s.session_id)) + .collect(); + if candidates.is_empty() { + return Ok(false); + } + let mut prepare = StageTimer::new("inventory_prepare_ssh"); + prepare_ssh_config(&config).await?; + prepare.outcome("success"); + drop(prepare); + let ids = candidates + .iter() + .map(|s| s.session_id.as_str()) + .collect::>() + .join("\n"); + let command = crate::tui::cloud_git::compressed_script_command( + Some("jcode-cloud"), + Some(&config), + &ids, + &super::super::cloud_preview::projection_script("inventory"), + ); + let mut fetch = StageTimer::new("inventory_ssh"); + let (status, bytes) = crate::tui::cloud_git::bounded_output_for( + command, + Duration::from_secs(10), + 1024 * 1024, + crate::tui::cloud_git::WorkspaceOperation::InventoryProjection, + ) + .await + .ok_or_else(|| anyhow::anyhow!("cloud inventory verification unavailable"))?; + anyhow::ensure!(status.success(), "cloud inventory verification failed"); + fetch.outcome("success"); + drop(fetch); + let mut parse = StageTimer::new("inventory_parse"); + let verified: VerifiedInventory = serde_json::from_slice(&bytes)?; + parse.outcome("success"); + drop(parse); + let mut registration = StageTimer::new("inventory_register"); + let result = register(&home, candidates, verified); + if result.is_ok() { + registration.outcome("success"); + } + result + }); + if result.is_ok() { + timer.outcome("success"); + } + result +} + +fn valid_id(id: &str) -> bool { + !id.is_empty() + && id.len() <= 128 + && id + .bytes() + .all(|c| c.is_ascii_alphanumeric() || b"_-".contains(&c)) +} + +fn should_verify(home: &Path, id: &str) -> bool { + if !valid_id(id) { + return false; + } + match storage::read_session_lease(id) { + Some(lease) => lease.session_id == id && lease.away_host.as_deref() == Some("jcode-cloud"), + None => { + !home + .join("session_leases") + .join(format!("{id}.json")) + .exists() + && !home.join("sessions").join(format!("{id}.json")).exists() + } + } +} + +async fn prepare_ssh_config(config: &Path) -> Result<()> { + let mut command = tokio::process::Command::new(std::env::current_exe()?); + command + .args([ + "--no-update", + "--no-selfdev", + "cloud", + "machine", + "prepare-ssh", + ]) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::null()) + .kill_on_drop(true); + let (status, _) = crate::tui::cloud_git::bounded_output_for( + command, + Duration::from_secs(8), + 4096, + crate::tui::cloud_git::WorkspaceOperation::InventoryPrepare, + ) + .await + .ok_or_else(|| anyhow::anyhow!("cloud SSH setup unavailable"))?; + anyhow::ensure!( + status.success() && config.is_file(), + "cloud SSH setup failed" + ); + Ok(()) +} + +fn register( + home: &Path, + candidates: Vec, + verified: VerifiedInventory, +) -> Result { + let mut changed = false; + for remote in verified.sessions { + let Some(metadata) = candidates.iter().find(|s| { + s.session_id == remote.session_id && s.host.as_deref() == Some(verified.host.as_str()) + }) else { + continue; + }; + if !valid_id(&remote.session_id) || !should_verify(home, &remote.session_id) { + continue; + } + // Never let metadata discovery supersede a migration. Only update an + // existing managed-cloud lease at the exact epoch we verified remotely. + let existing = storage::read_session_lease(&remote.session_id); + if existing + .as_ref() + .is_some_and(|lease| lease.epoch != remote.epoch) + { + continue; + } + let mut lease = existing.clone().unwrap_or_else(|| storage::SessionLease { + session_id: remote.session_id.clone(), + epoch: remote.epoch, + away_host: Some("jcode-cloud".into()), + repo_root: None, + last_sync_snapshot: None, + last_sync_head: None, + updated_at: String::new(), + }); + lease.repo_root = remote.cwd; + if let Some(updated_at) = remote.updated_at.or_else(|| metadata.updated_at.clone()) { + // Ignore malformed timestamps rather than poisoning picker sorting. + if chrono::DateTime::parse_from_rfc3339(&updated_at).is_ok() { + lease.updated_at = updated_at; + } + } + if lease.updated_at.is_empty() { + lease.updated_at = chrono::Utc::now().to_rfc3339(); + } + if existing.as_ref() != Some(&lease) { + storage::write_session_lease(&lease)?; + changed = true; + } + // A picker-only sidecar preserves metadata, never a canonical transcript. + let path = home + .join("cloud/mobile-session-titles") + .join(format!("{}.json", lease.session_id)); + let title = remote.title.unwrap_or_else(|| { + let name = extract_session_name(&lease.session_id).unwrap_or(&lease.session_id); + format!("{name} (cloud session)") + }); + if storage::read_json::(&path).ok().as_deref() != Some(&title) { + storage::write_json_secret(&path, &title)?; + changed = true; + } + } + Ok(changed) +} + +pub(super) fn title(id: &str) -> Option { + let path = storage::jcode_dir() + .ok()? + .join("cloud/mobile-session-titles") + .join(format!("{id}.json")); + storage::read_json(&path).ok() +} + +#[cfg(test)] +#[path = "mobile_inventory_tests.rs"] +mod tests; diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs new file mode 100644 index 00000000000..bc64f61945a --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs @@ -0,0 +1,441 @@ +use super::*; + +#[cfg(unix)] +#[test] +fn inventory_projection_uses_bounded_transport_without_mixing_timing_into_json() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_inventory_transport"; + let mut session = Session::create_with_id( + id.into(), + None, + Some("Private title must stay out of timing records".into()), + ); + session.migration_epoch = 8; + session.save().unwrap(); + let mut command = crate::tui::cloud_git::script_command( + None, + None, + id, + &super::super::super::cloud_preview::projection_script("inventory"), + ); + command.env("JCODE_HOME", home.path()); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let (status, bytes) = runtime + .block_on(crate::tui::cloud_git::bounded_output( + command, + Duration::from_secs(5), + 1024 * 1024, + )) + .unwrap(); + assert!(status.success()); + let inventory: VerifiedInventory = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(inventory.sessions.len(), 1); + assert_eq!(inventory.sessions[0].session_id, id); + assert_eq!(inventory.sessions[0].epoch, 8); + assert_eq!( + inventory.sessions[0].title.as_deref(), + Some("Private title must stay out of timing records") + ); +} + +struct HomeGuard(Option); + +impl HomeGuard { + fn install(home: &Path) -> Self { + let previous = std::env::var_os("JCODE_HOME"); + crate::env::set_var("JCODE_HOME", home); + Self(previous) + } +} + +impl Drop for HomeGuard { + fn drop(&mut self) { + match self.0.take() { + Some(previous) => crate::env::set_var("JCODE_HOME", previous), + None => crate::env::remove_var("JCODE_HOME"), + } + super::super::invalidate_session_list_cache(); + } +} + +fn metadata(id: &str) -> crate::mobile_sync::SyncedSession { + crate::mobile_sync::SyncedSession { + session_id: id.into(), + source: "cloud".into(), + title: Some("Future orbital compiler work".into()), + host: Some("verified-worker".into()), + cwd: Some("/metadata/repository".into()), + updated_at: Some("2026-11-08T05:00:00Z".into()), + created_at: None, + } +} + +#[test] +fn signed_in_fresh_client_can_discover_without_existing_ssh_config() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _home = HomeGuard::install(home.path()); + struct Environment(Vec<(&'static str, Option)>); + impl Drop for Environment { + fn drop(&mut self) { + for (name, value) in self.0.drain(..) { + match value { + Some(value) => crate::env::set_var(name, value), + None => crate::env::remove_var(name), + } + } + } + } + let names = [ + "JCODE_API_KEY", + "JCODE_CLOUD_ENV_SESSION", + "JCODE_CLOUD_MACHINE", + "JCODE_SERVER_NAME", + ]; + let _environment = Environment( + names + .iter() + .map(|&name| (name, std::env::var_os(name))) + .collect(), + ); + for name in names { + crate::env::remove_var(name); + } + crate::env::set_var("JCODE_API_KEY", "test-account-key"); + assert!(!home.path().join("cloud/ssh_config").exists()); + assert!( + available(), + "sign-in, not preexisting SSH files, enables discovery" + ); + crate::env::set_var("JCODE_API_KEY", " "); + assert!( + !available(), + "blank credentials must not trigger network IO" + ); + crate::env::set_var("JCODE_API_KEY", "test-account-key"); + crate::env::set_var("JCODE_CLOUD_MACHINE", "1"); + assert!( + !available(), + "cloud hosts must not recursively discover themselves" + ); +} + +fn inventory(ids: &[(&str, u64)]) -> VerifiedInventory { + VerifiedInventory { + host: "verified-worker".into(), + sessions: ids + .iter() + .map(|(id, epoch)| VerifiedSession { + session_id: (*id).into(), + epoch: *epoch, + cwd: Some("/verified/repository".into()), + title: Some("Future orbital compiler work".into()), + updated_at: Some("2026-11-08T05:00:00Z".into()), + }) + .collect(), + } +} + +#[test] +fn discovered_future_session_has_verified_lease_searchable_title_and_no_transcript() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_future_orbital_1999999999999"; + assert!(register(home.path(), vec![metadata(id)], inventory(&[(id, 42)])).unwrap()); + let lease = storage::read_session_lease(id).expect("discovered cloud ownership"); + assert_eq!(lease.epoch, 42); + assert_eq!(lease.away_host.as_deref(), Some("jcode-cloud")); + assert_eq!(lease.repo_root.as_deref(), Some("/verified/repository")); + assert_eq!(lease.updated_at, "2026-11-08T05:00:00Z"); + assert!(lease.last_sync_snapshot.is_none()); + assert!(lease.last_sync_head.is_none()); + assert!( + !home + .path() + .join("sessions") + .join(format!("{id}.json")) + .exists() + ); + let stub = super::super::cloud_session_stub(&lease); + assert_eq!(stub.title, "Future orbital compiler work"); + assert!(stub.search_index.contains("orbital")); + assert!(stub.search_index.contains("compiler")); + super::super::invalidate_session_list_cache(); + let (groups, orphans) = super::super::load_sessions_grouped().unwrap(); + let group = groups + .iter() + .find(|group| group.name == "cloud: jcode-cloud") + .expect("cloud session picker group"); + assert_eq!(group.icon, "☁"); + assert_eq!(group.sessions.len(), 1); + assert_eq!(group.sessions[0].id, id); + assert!(group.sessions[0].search_index.contains("orbital")); + assert!(!orphans.iter().any(|session| session.id == id)); + assert!( + !home + .path() + .join("sessions") + .join(format!("{id}.json")) + .exists() + ); +} + +#[test] +fn registration_is_idempotent_and_preserves_returned_leases_and_local_snapshots() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_inventory_idempotent"; + assert!(register(home.path(), vec![metadata(id)], inventory(&[(id, 7)])).unwrap()); + let lease_path = home + .path() + .join("session_leases") + .join(format!("{id}.json")); + let original = std::fs::read(&lease_path).unwrap(); + let mut changed_metadata = metadata(id); + changed_metadata.title = Some("Must not replace original title".into()); + assert!(!register(home.path(), vec![changed_metadata], inventory(&[(id, 99)])).unwrap()); + assert_eq!(std::fs::read(&lease_path).unwrap(), original); + assert_eq!(title(id).as_deref(), Some("Future orbital compiler work")); + + let returned_id = "session_inventory_returned"; + let returned = storage::SessionLease { + session_id: returned_id.into(), + epoch: 100, + away_host: None, + repo_root: Some("/local/returned".into()), + last_sync_snapshot: Some("snapshot".into()), + last_sync_head: Some("head".into()), + updated_at: "2026-11-08T05:10:00Z".into(), + }; + storage::write_session_lease(&returned).unwrap(); + let returned_path = home + .path() + .join("session_leases") + .join(format!("{returned_id}.json")); + let returned_bytes = std::fs::read(&returned_path).unwrap(); + let local_id = "session_inventory_local"; + let local_path = home + .path() + .join("sessions") + .join(format!("{local_id}.json")); + std::fs::create_dir_all(local_path.parent().unwrap()).unwrap(); + let snapshot = b"{\"id\":\"session_inventory_local\",\"messages\":[]}"; + std::fs::write(&local_path, snapshot).unwrap(); + assert!( + !register( + home.path(), + vec![metadata(returned_id), metadata(local_id)], + inventory(&[(returned_id, 101), (local_id, 12)]) + ) + .unwrap() + ); + assert_eq!(std::fs::read(&returned_path).unwrap(), returned_bytes); + assert_eq!(std::fs::read(&local_path).unwrap(), snapshot); + assert!(storage::read_session_lease(local_id).is_none()); + assert!(title(returned_id).is_none()); + assert!(title(local_id).is_none()); +} + +#[test] +fn registration_skips_mismatched_hosts_and_unverified_missing_sessions() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let mismatch_id = "session_inventory_wrong_host"; + let missing_id = "session_inventory_missing"; + let unknown_id = "session_inventory_unknown"; + let mut mismatch = metadata(mismatch_id); + mismatch.host = Some("different-worker".into()); + assert!( + !register( + home.path(), + vec![mismatch, metadata(missing_id)], + inventory(&[(mismatch_id, 2), (unknown_id, 3)]) + ) + .unwrap() + ); + for id in [mismatch_id, missing_id, unknown_id] { + assert!(storage::read_session_lease(id).is_none()); + assert!(title(id).is_none()); + assert!( + !home + .path() + .join("sessions") + .join(format!("{id}.json")) + .exists() + ); + } +} + +#[test] +fn known_cloud_metadata_refreshes_without_changing_migration_or_snapshot() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_metadata_refresh"; + assert!(register(home.path(), vec![metadata(id)], inventory(&[(id, 7)])).unwrap()); + let mut lease = storage::read_session_lease(id).unwrap(); + lease.last_sync_snapshot = Some("preserved-snapshot".into()); + lease.last_sync_head = Some("preserved-head".into()); + storage::write_session_lease(&lease).unwrap(); + let mut session = Session::create_with_id(id.into(), None, Some("Old local title".into())); + session.working_dir = Some("/old/local/project".into()); + session.add_message( + Role::User, + vec![crate::message::ContentBlock::Text { + text: "Historical local conversation".into(), + cache_control: None, + }], + ); + let path = home.path().join("sessions").join(format!("{id}.json")); + // Model the historical local copy left by migration, without attempting to + // save an active local session against its deliberately-away lease. + storage::write_json(&path, &session).unwrap(); + let original = std::fs::read(&path).unwrap(); + assert!( + should_verify(home.path(), id), + "known cloud snapshots must refresh too" + ); + let mut refreshed = inventory(&[(id, 7)]); + refreshed.sessions[0].title = Some("Updated lunar compiler".into()); + refreshed.sessions[0].cwd = Some("/cloud/new-project".into()); + refreshed.sessions[0].updated_at = Some("2026-11-09T01:00:00Z".into()); + assert!(register(home.path(), vec![metadata(id)], refreshed).unwrap()); + let updated = storage::read_session_lease(id).unwrap(); + assert_eq!(updated.epoch, 7); + assert_eq!(updated.last_sync_snapshot, lease.last_sync_snapshot); + assert_eq!(updated.last_sync_head, lease.last_sync_head); + assert_eq!(updated.repo_root.as_deref(), Some("/cloud/new-project")); + assert_eq!(std::fs::read(&path).unwrap(), original); + let mut sessions = Vec::new(); + super::super::include_cloud_sessions( + &home.path().join("sessions"), + &crate::catchup::CatchupSeenSnapshot::load(), + &mut sessions, + ); + assert_eq!(sessions.len(), 1); + assert_eq!(sessions[0].title, "Updated lunar compiler"); + assert_eq!( + sessions[0].working_dir.as_deref(), + Some("/cloud/new-project") + ); + assert!(sessions[0].search_index.contains("lunar")); + assert!(sessions[0].search_index.contains("/cloud/new-project")); + assert_eq!( + sessions[0].last_message_time.to_rfc3339(), + "2026-11-09T01:00:00+00:00" + ); + let mut identical = inventory(&[(id, 7)]); + identical.sessions[0].title = Some("Updated lunar compiler".into()); + identical.sessions[0].cwd = Some("/cloud/new-project".into()); + identical.sessions[0].updated_at = Some("2026-11-09T01:00:00Z".into()); + assert!(!register(home.path(), vec![metadata(id)], identical).unwrap()); +} + +#[test] +fn cleared_remote_title_and_directory_do_not_resurrect_stale_api_metadata() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_cleared_metadata"; + register(home.path(), vec![metadata(id)], inventory(&[(id, 3)])).unwrap(); + let mut cleared = inventory(&[(id, 3)]); + cleared.sessions[0].title = None; + cleared.sessions[0].cwd = None; + assert!(register(home.path(), vec![metadata(id)], cleared).unwrap()); + assert!(storage::read_session_lease(id).unwrap().repo_root.is_none()); + assert_ne!(title(id).as_deref(), Some("Future orbital compiler work")); + assert!(title(id).unwrap().contains("cloud session")); +} + +#[test] +fn returned_or_other_host_sessions_and_unsafe_ids_are_not_refresh_candidates() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_candidate_ownership"; + register(home.path(), vec![metadata(id)], inventory(&[(id, 9)])).unwrap(); + let mut lease = storage::read_session_lease(id).unwrap(); + for host in [None, Some("other-machine".to_string())] { + lease.away_host = host; + storage::write_session_lease(&lease).unwrap(); + assert!(!should_verify(home.path(), id)); + assert!(!register(home.path(), vec![metadata(id)], inventory(&[(id, 9)])).unwrap()); + assert_eq!(storage::read_session_lease(id), Some(lease.clone())); + } + for id in ["../escape", ".", "session:route", "", "session\nsecond"] { + assert!(!should_verify(home.path(), id)); + assert!(!register(home.path(), vec![metadata(id)], inventory(&[(id, 0)])).unwrap()); + } + let id = "session_broken_ownership"; + let path = home + .path() + .join("session_leases") + .join(format!("{id}.json")); + std::fs::write(&path, b"{broken-lease").unwrap(); + assert!(!should_verify(home.path(), id)); + assert!(!register(home.path(), vec![metadata(id)], inventory(&[(id, 0)])).unwrap()); + assert_eq!(std::fs::read(path).unwrap(), b"{broken-lease"); +} + +#[cfg(unix)] +#[test] +fn verification_script_reads_authoritative_metadata_and_skips_moved_sessions() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_verification_script"; + let mut session = + Session::create_with_id(id.into(), None, Some("Verified remote title".into())); + session.working_dir = Some("/remote/current".into()); + session.migration_epoch = 6; + session.save().unwrap(); + let run = || { + let output = std::process::Command::new("sh") + .args([ + "-c", + &super::super::super::cloud_preview::projection_script("inventory"), + "verify", + &format!("{id}\nsession_missing\n../escape"), + ]) + .env("JCODE_HOME", home.path()) + .output() + .unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + serde_json::from_slice::(&output.stdout).unwrap() + }; + let verified = run(); + assert_eq!(verified.sessions.len(), 1); + assert_eq!(verified.sessions[0].epoch, 6); + assert_eq!( + verified.sessions[0].title.as_deref(), + Some("Verified remote title") + ); + assert_eq!(verified.sessions[0].cwd.as_deref(), Some("/remote/current")); + assert!(verified.sessions[0].updated_at.is_some()); + session.custom_title = Some("Renamed by user".into()); + session.save().unwrap(); + assert_eq!(run().sessions[0].title.as_deref(), Some("Renamed by user")); + storage::write_session_lease(&storage::SessionLease { + session_id: id.into(), + epoch: 7, + away_host: Some("returned-machine".into()), + repo_root: None, + last_sync_snapshot: None, + last_sync_head: None, + updated_at: "2026-11-08T05:00:00Z".into(), + }) + .unwrap(); + assert!(run().sessions.is_empty()); +} diff --git a/crates/jcode-tui/src/tui/session_picker/loading/pi_sessions.rs b/crates/jcode-tui/src/tui/session_picker/loading/pi_sessions.rs index b100cd22502..dc9878df7ac 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading/pi_sessions.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading/pi_sessions.rs @@ -1,12 +1,20 @@ use super::*; +#[cfg(test)] pub(in crate::tui::session_picker) fn load_pi_preview_from_path( path: &Path, ) -> Option> { - load_pi_session_info(path) - .ok() - .flatten() - .map(|session| session.messages_preview) + load_pi_external_preview(path).map(|preview| preview.messages) +} + +pub(in crate::tui::session_picker) fn load_pi_external_preview( + path: &Path, +) -> Option { + let (session, search) = load_pi_session_info(path).ok().flatten()?; + Some(ExternalPreview { + messages: session.messages_preview, + search, + }) } pub(super) fn load_external_pi_sessions(scan_limit: usize) -> Vec { @@ -108,7 +116,7 @@ pub(super) fn load_pi_session_stub(path: &Path) -> Result> { })) } -pub(super) fn load_pi_session_info(path: &Path) -> Result> { +pub(super) fn load_pi_session_info(path: &Path) -> Result> { let file = File::open(path)?; let reader = BufReader::new(file); let mut lines = reader.lines(); @@ -148,6 +156,7 @@ pub(super) fn load_pi_session_info(path: &Path) -> Result> { let mut assistant_message_count = 0usize; let mut message_count = 0usize; let mut preview = Vec::new(); + let mut search = SourceBuckets::default(); for line in lines { let line = line?; @@ -208,6 +217,7 @@ pub(super) fn load_pi_session_info(path: &Path) -> Result> { _ => {} } push_preview_message(&mut preview, role, text); + classify_json_into(&mut search, message, message_role_source(role)); } _ => {} } @@ -224,20 +234,20 @@ pub(super) fn load_pi_session_info(path: &Path) -> Result> { jcode_core::util::truncate_str(&session_id, 8) ) }); - let search_index = build_search_index( + let search_index = build_classified_search_index( &format!("pi:{session_id}"), &short_name, &title, working_dir.as_deref(), None, - &preview, + &search, ); let first_user_prompt = preview .iter() .find(|msg| msg.role == "user" && !msg.content.trim().is_empty()) .map(|msg| msg.content.clone()); - Ok(Some(SessionInfo { + let session = SessionInfo { id: format!("pi:{session_id}"), parent_id: None, short_name, @@ -270,5 +280,6 @@ pub(super) fn load_pi_session_info(path: &Path) -> Result> { }, external_path: Some(path.to_string_lossy().to_string()), todo_statuses: Vec::new(), - })) + }; + Ok(Some((session, search))) } diff --git a/crates/jcode-tui/src/tui/session_picker/loading_search_text.rs b/crates/jcode-tui/src/tui/session_picker/loading_search_text.rs index 1d854537c3d..cb8311fd9cf 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading_search_text.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading_search_text.rs @@ -86,30 +86,22 @@ pub(crate) fn build_search_index( save_label: Option<&str>, messages_preview: &[PreviewMessage], ) -> String { - let mut combined = String::new(); - combined.push_str(title); - combined.push(' '); - combined.push_str(short_name); - combined.push(' '); - combined.push_str(id); - - if let Some(dir) = working_dir { - combined.push(' '); - combined.push_str(dir); - } - - if let Some(label) = save_label { - combined.push(' '); - combined.push_str(label); - } + let mut combined = search_index_header(id, short_name, title, working_dir, save_label); let mut budget = SEARCH_CONTENT_BUDGET_BYTES; + let mut current = None; for msg in messages_preview { let content = msg.content.trim(); if content.is_empty() { continue; } - combined.push(' '); + let source = message_role_source(&msg.role); + if current != Some(source) { + push_mark(&mut combined, source); + current = Some(source); + } else { + combined.push(' '); + } push_with_byte_budget(&mut combined, content, &mut budget); if budget == 0 { break; @@ -119,6 +111,96 @@ pub(crate) fn build_search_index( combined.to_lowercase() } +/// Session metadata at the head of every search index. The title and saved +/// label stay unmarked since ranking already treats them as user intent. The +/// ID is an identifier, and the name and directory locate the project. +pub(crate) fn search_index_header( + id: &str, + short_name: &str, + title: &str, + working_dir: Option<&str>, + save_label: Option<&str>, +) -> String { + let mut combined = String::new(); + combined.push_str(title); + if let Some(label) = save_label { + combined.push(' '); + combined.push_str(label); + } + push_mark(&mut combined, Source::Other); + combined.push_str(id); + push_mark(&mut combined, Source::Project); + combined.push_str(short_name); + if let Some(dir) = working_dir { + combined.push(' '); + combined.push_str(dir); + } + combined +} + +/// Classifies one external transcript record into per-source samples. The +/// walker reads role and block type tags, so a Claude Code tool result inside a +/// `user` message lands in tool output instead of with the user's prompts. +pub(crate) fn classify_json_into( + buckets: &mut SourceBuckets, + value: &serde_json::Value, + source: Source, +) { + use serde::de::DeserializeSeed; + let mut sink = BucketSink { + buckets, + total: SEARCH_CONTENT_BUDGET_BYTES, + }; + let walk = Walk { + sink: &mut sink, + source, + }; + // `serde_json::Value` sorts object keys, which would visit `content` + // before the `type`/`role` tag that classifies it. Re-emit tags first so + // the streaming walker sees the same order a transcript file has. + let mut json = String::new(); + write_tags_first(value, &mut json); + let mut de = serde_json::Deserializer::from_str(&json); + if let Err(error) = walk.deserialize(&mut de) { + crate::logging::debug(&format!("Invalid external search content: {error}")); + } +} + +fn write_tags_first(value: &serde_json::Value, out: &mut String) { + match value { + serde_json::Value::Object(map) => { + out.push('{'); + let tags = map + .iter() + .filter(|(key, _)| matches!(key.as_str(), "role" | "type")); + let rest = map + .iter() + .filter(|(key, _)| !matches!(key.as_str(), "role" | "type")); + for (index, (key, value)) in tags.chain(rest).enumerate() { + if index > 0 { + out.push(','); + } + out.push_str(&serde_json::Value::String(key.clone()).to_string()); + out.push(':'); + write_tags_first(value, out); + } + out.push('}'); + } + serde_json::Value::Array(items) => { + out.push('['); + for (index, item) in items.iter().enumerate() { + if index > 0 { + out.push(','); + } + write_tags_first(item, out); + } + out.push(']'); + } + scalar => out.push_str(&scalar.to_string()), + } +} + +#[cfg(test)] pub(crate) fn raw_value_search_excerpt(raw: &RawValue, budget: usize) -> Option { if budget == 0 { return None; @@ -168,3 +250,35 @@ pub(crate) fn raw_value_display_text(raw: &RawValue) -> Option { let text = text.trim(); (!text.is_empty()).then(|| text.to_string()) } + +/// Per-source samples of one stored message's content. Content blocks are +/// classified by type, so tool results and reasoning inside a user or +/// assistant message are not indexed as that role's own words. +pub(crate) fn classified_search_excerpt( + raw: &RawValue, + role: &crate::message::Role, + budget: usize, +) -> Option { + use serde::de::DeserializeSeed; + if budget == 0 { + return None; + } + let source = match role { + crate::message::Role::User => Source::User, + crate::message::Role::Assistant => Source::Assistant, + }; + let mut buckets = SourceBuckets::default(); + let mut sink = BucketSink { + buckets: &mut buckets, + total: budget.min(MESSAGE_SEARCH_EXCERPT_BYTES), + }; + let walk = Walk { + sink: &mut sink, + source, + }; + let mut de = serde_json::Deserializer::from_str(raw.get()); + if let Err(error) = walk.deserialize(&mut de) { + crate::logging::debug(&format!("Invalid transcript search content: {error}")); + } + (!buckets.is_empty()).then_some(buckets) +} diff --git a/crates/jcode-tui/src/tui/session_picker/loading_tests.rs b/crates/jcode-tui/src/tui/session_picker/loading_tests.rs index aecc7b30cfa..7f7c85d6a27 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading_tests.rs @@ -395,6 +395,7 @@ fn grouped_refresh_bypasses_fresh_in_process_session_cache() { #[test] fn recently_closed_session_is_sent_before_cache_and_not_rolled_back() { let _env_lock = crate::storage::lock_test_env(); + let _account = EnvVarGuard::set_str("JCODE_API_KEY", ""); let temp = tempfile::tempdir().unwrap(); let _home = EnvVarGuard::set_path("JCODE_HOME", temp.path()); let _runtime = EnvVarGuard::set_path("JCODE_RUNTIME_DIR", temp.path()); @@ -430,8 +431,8 @@ fn recently_closed_session_is_sent_before_cache_and_not_rolled_back() { ); session::record_window_closed(id); let (sender, receiver) = std::sync::mpsc::channel(); - send_session_list_updates(sender); - for _ in 0..2 { + send_session_list_updates_with_discovery(sender, true, || Ok(true)); + for _ in 0..3 { let super::super::SessionListUpdate::Cached((groups, orphans)) = receiver.recv().unwrap() else { panic!("recent recovery and cache must precede the history refresh"); @@ -458,6 +459,68 @@ fn recently_closed_session_is_sent_before_cache_and_not_rolled_back() { invalidate_session_list_cache(); } +#[test] +fn failed_cloud_refresh_warns_without_discarding_local_or_last_good_cloud_rows() { + let _lock = crate::storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _home = EnvVarGuard::set_path("JCODE_HOME", home.path()); + let local_id = "session_cloud_failure_local"; + let mut session = Session::create_with_id(local_id.into(), None, Some("Local work".into())); + session.add_message( + Role::User, + vec![crate::message::ContentBlock::Text { + text: "Keep this local session usable".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + let cloud_id = "session_cloud_failure_cached"; + let lease = storage::SessionLease { + session_id: cloud_id.into(), + epoch: 5, + away_host: Some("jcode-cloud".into()), + repo_root: Some("/cloud/cached".into()), + last_sync_snapshot: None, + last_sync_head: None, + updated_at: "2026-11-08T05:00:00Z".into(), + }; + storage::write_session_lease(&lease).unwrap(); + let (sender, receiver) = std::sync::mpsc::channel(); + send_session_list_updates_with_discovery(sender, true, || { + anyhow::bail!("synthetic account failure") + }); + let mut warning_seen = false; + let mut fresh_seen = false; + for update in receiver { + match update { + super::super::SessionListUpdate::CloudUnavailable(warning) => { + assert!(warning.contains("Reopen /resume")); + warning_seen = true; + } + super::super::SessionListUpdate::Fresh(result) => { + assert!( + warning_seen, + "explain incomplete discovery before ending refresh" + ); + let (groups, orphans) = result.unwrap(); + let ids: HashSet<_> = groups + .iter() + .flat_map(|g| g.sessions.iter()) + .chain(orphans.iter()) + .map(|s| s.id.as_str()) + .collect(); + assert!(ids.contains(local_id)); + assert!(ids.contains(cloud_id)); + fresh_seen = true; + } + _ => {} + } + } + assert!(warning_seen && fresh_seen); + assert_eq!(storage::read_session_lease(cloud_id), Some(lease)); + invalidate_session_list_cache(); +} + #[test] fn load_sessions_includes_claude_code_sessions_from_external_home() { let _env_lock = crate::storage::lock_test_env(); @@ -870,6 +933,7 @@ fn load_sessions_prefers_todo_group_over_generated_title() { session_id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, content: "Wire todo title into picker".to_string(), status: "in_progress".to_string(), priority: "high".to_string(), @@ -959,6 +1023,7 @@ fn load_sessions_keeps_custom_title_over_todo_group() { session_id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, content: "Wire todo title into picker".to_string(), status: "in_progress".to_string(), priority: "high".to_string(), @@ -1531,3 +1596,91 @@ fn benchmark_resume_loading_reports_timings() { #[path = "loading_scope_tests.rs"] mod loading_scope; + +#[test] +fn summary_tracks_newest_message_timestamp_across_roles() { + let data: SessionMessageSummaryData = serde_json::from_str( + r#"[ + {"role":"user","content":"hi","timestamp":"2026-01-01T00:00:00Z"}, + {"role":"assistant","content":"later tool reply","timestamp":"2026-01-01T03:00:00Z"}, + {"role":"user","content":"no ts"} + ]"#, + ) + .unwrap(); + assert_eq!( + data.last_message_at.map(|t| t.to_rfc3339()), + Some("2026-01-01T03:00:00+00:00".to_string()) + ); + + let mut base: SessionMessageSummaryData = serde_json::from_str( + r#"[{"role":"user","content":"a","timestamp":"2026-01-01T00:00:00Z"}]"#, + ) + .unwrap(); + base.merge( + serde_json::from_str( + r#"[{"role":"assistant","content":"b","timestamp":"2026-01-02T00:00:00Z"}]"#, + ) + .unwrap(), + ); + assert_eq!( + base.last_message_at.map(|t| t.to_rfc3339()), + Some("2026-01-02T00:00:00+00:00".to_string()) + ); +} + +#[test] +fn external_previews_index_tool_blocks_at_their_own_weight() { + use crate::tui::session_picker::transcript_search::source::{Source, metadata_sources}; + let temp = tempfile::tempdir().expect("temp dir"); + let claude = temp.path().join("claude.jsonl"); + std::fs::write( + &claude, + concat!( + "{\"type\":\"user\",\"message\":{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":\"please fix kiwiflake\"}]}}\n", + "{\"type\":\"assistant\",\"message\":{\"role\":\"assistant\",\"content\":[{\"type\":\"tool_use\",\"id\":\"t\",\"name\":\"bash\",\"input\":{\"command\":\"grep mangoroot\",\"intent\":\"find papayaplan\"}}]}}\n", + "{\"type\":\"user\",\"message\":{\"role\":\"user\",\"content\":[{\"type\":\"tool_result\",\"tool_use_id\":\"t\",\"content\":\"zebradump output\"}]}}\n", + ), + ) + .unwrap(); + let preview = load_claude_code_external_preview(&claude).expect("preview"); + let index = build_classified_search_index("claude:x", "x", "t", None, None, &preview.search); + let tokens: Vec = ["kiwiflake", "mangoroot", "papayaplan", "zebradump"] + .map(String::from) + .into(); + assert_eq!( + metadata_sources(&[], &index, &tokens), + [ + Some(Source::User), + Some(Source::ToolInput), + Some(Source::Intent), + Some(Source::ToolOutput), + ], + "{index:?}" + ); + + let codex = temp.path().join("codex.jsonl"); + std::fs::write( + &codex, + concat!( + "{\"type\":\"session_meta\",\"payload\":{\"id\":\"c\"}}\n", + "{\"type\":\"response_item\",\"payload\":{\"type\":\"message\",\"role\":\"user\",\"content\":[{\"type\":\"input_text\",\"text\":\"kiwiflake please\"}]}}\n", + "{\"type\":\"response_item\",\"payload\":{\"type\":\"function_call\",\"name\":\"shell\",\"arguments\":\"mangoroot\"}}\n", + "{\"type\":\"response_item\",\"payload\":{\"type\":\"function_call_output\",\"output\":\"zebradump\"}}\n", + ), + ) + .unwrap(); + let preview = load_codex_external_preview(&codex).expect("preview"); + let index = build_classified_search_index("codex:c", "c", "t", None, None, &preview.search); + let tokens: Vec = ["kiwiflake", "mangoroot", "zebradump"] + .map(String::from) + .into(); + assert_eq!( + metadata_sources(&[], &index, &tokens), + [ + Some(Source::User), + Some(Source::ToolInput), + Some(Source::ToolOutput), + ], + "{index:?}" + ); +} diff --git a/crates/jcode-tui/src/tui/session_picker/performance.rs b/crates/jcode-tui/src/tui/session_picker/performance.rs index c555b05e866..ef2797ae57e 100644 --- a/crates/jcode-tui/src/tui/session_picker/performance.rs +++ b/crates/jcode-tui/src/tui/session_picker/performance.rs @@ -74,18 +74,21 @@ impl PreviewRenderCache { impl SessionPicker { pub(super) fn reuse_preview(&mut self, key: &PreviewCacheKey) -> bool { - if self - .preview_cache - .as_ref() - .is_some_and(|cache| cache.key == *key) - { + let same_content = |cache: &PreviewRenderCache| { + cache.key.content_hash == key.content_hash + && cache.key.inner_width == key.inner_width + && cache.key.inner_height == key.inner_height + && cache.key.diff_mode == key.diff_mode + }; + if self.preview_cache.as_ref().is_some_and(same_content) { + self.refresh_preview_query(&key.search_query); return true; } let Some(index) = self .preview_reuse .parked .iter() - .position(|(cache, _)| cache.key == *key) + .position(|(cache, _)| same_content(cache)) else { return false; }; @@ -94,9 +97,32 @@ impl SessionPicker { }; self.preview_reuse.bytes -= bytes; self.install_sized_preview(cached, bytes); + self.refresh_preview_query(&key.search_query); true } + /// Search highlighting is applied to the visible buffer by the renderer. + /// Changing the query only changes the jump target, not transcript content. + fn refresh_preview_query(&mut self, query: &str) { + let Some(cache) = self.preview_cache.as_mut() else { + return; + }; + if cache.key.search_query == query { + return; + } + cache.key.search_query = query.to_string(); + let tokens = loading::search_query_tokens(query); + cache.first_match_line = cache.wrapped_lines.iter().position(|line| { + let text: String = line + .spans + .iter() + .map(|span| span.content.as_ref()) + .collect(); + let lower = text.to_lowercase(); + tokens.iter().any(|token| lower.contains(token.as_str())) + }); + } + pub(super) fn install_preview(&mut self, cache: PreviewRenderCache) { // Count owned allocations once on build, not on each zero-copy swap. let bytes = cache.estimated_bytes(); diff --git a/crates/jcode-tui/src/tui/session_picker/performance_tests.rs b/crates/jcode-tui/src/tui/session_picker/performance_tests.rs index a8ae7868fc2..2abd7f38c42 100644 --- a/crates/jcode-tui/src/tui/session_picker/performance_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/performance_tests.rs @@ -72,40 +72,6 @@ fn cold_preview_viewport_matches_full_wide_then_narrow_layout() { for width in [1, 2, 3, 20, 80, 120] { for height in [0, 1, 2, 5, 30, 200] { let area = Rect::new(0, 0, width, height); - let wide = width - .saturating_sub(super::super::ui::left_aligned_content_inset(width, false)); - let mut prepared = - super::super::ui::prepare::prepare_history_preview(&messages, 0, wide, mode); - let mut header = super::super::ui::header::build_preview_header(&item, wide); - let mut padding = - super::super::ui::prepare::initial_header_pad_top(height, header.len()); - let scrollbar = super::super::ui::native_scrollbar_visible( - width > 1, - padding + header.len() + prepared.wrapped_lines.len(), - height as usize, - ); - if scrollbar { - let render_width = width.saturating_sub(1); - let narrow = render_width.saturating_sub( - super::super::ui::left_aligned_content_inset(render_width, false), - ); - prepared = super::super::ui::prepare::prepare_history_preview( - &messages, 0, narrow, mode, - ); - header = super::super::ui::header::build_preview_header(&item, narrow); - padding = - super::super::ui::prepare::initial_header_pad_top(height, header.len()); - } - let body_empty = prepared.wrapped_lines.is_empty(); - let mut expected = vec![Line::default(); padding]; - expected.extend(header); - expected.extend(prepared.wrapped_lines); - if body_empty { - expected.push(Line::styled( - "(empty session)", - Style::default().fg(rgb(80, 80, 80)), - )); - } let actual = picker.build_preview_cache( &item, area, @@ -114,12 +80,15 @@ fn cold_preview_viewport_matches_full_wide_then_narrow_layout() { Alignment::Left, mode, ); + // Wrapping and chrome belong to the real chat renderer now. + let roles: Vec<_> = actual.messages.iter().map(|m| m.role.clone()).collect(); + let expected: Vec<_> = if messages.is_empty() { + vec!["system".to_string()] + } else { + messages.iter().map(|m| m.role.clone()).collect() + }; assert_eq!( - actual.show_scrollbar, scrollbar, - "turns={turns} width={width} height={height}" - ); - assert_eq!( - actual.wrapped_lines, expected, + roles, expected, "turns={turns} width={width} height={height}" ); } @@ -323,6 +292,27 @@ fn missing_preview_is_a_cache_miss_and_rebuilds_on_render() { assert!(picker.preview_cache.as_ref().unwrap().key == key); } +#[test] +fn query_edits_reuse_transcript_messages_and_refresh_jump_target() { + let mut picker = SessionPicker::new(vec![session(0, 2)]); + let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap(); + draw_preview(&mut picker, &mut terminal); + let cache = picker.preview_cache.as_ref().unwrap(); + let messages = cache.messages.as_ptr(); + let mut key = cache.key.clone(); + key.search_query = "resume".into(); + assert!(picker.reuse_preview(&key)); + let cache = picker.preview_cache.as_ref().unwrap(); + assert_eq!(cache.messages.as_ptr(), messages); + assert_eq!(cache.key.search_query, "resume"); + assert!(cache.first_match_line.is_some()); + key.search_query = "zzzzqqqq".into(); + assert!(picker.reuse_preview(&key)); + let cache = picker.preview_cache.as_ref().unwrap(); + assert_eq!(cache.messages.as_ptr(), messages); + assert!(cache.first_match_line.is_none()); +} + fn tiny_cache(id: u64, bytes: usize) -> PreviewRenderCache { PreviewRenderCache { key: PreviewCacheKey { @@ -332,6 +322,7 @@ fn tiny_cache(id: u64, bytes: usize) -> PreviewRenderCache { diff_mode: crate::config::DiffDisplayMode::Off, search_query: String::new(), }, + messages: Vec::new(), wrapped_lines: vec![Line::from("x".repeat(bytes))], image_regions: Vec::new(), user_prompt_markers: Vec::new(), diff --git a/crates/jcode-tui/src/tui/session_picker/preview_chrome.rs b/crates/jcode-tui/src/tui/session_picker/preview_chrome.rs index 03e7d5ce136..25be6b774d5 100644 --- a/crates/jcode-tui/src/tui/session_picker/preview_chrome.rs +++ b/crates/jcode-tui/src/tui/session_picker/preview_chrome.rs @@ -99,7 +99,10 @@ fn snapshot_account_info( (auth, usage) } -fn selected_info(session: &SessionInfo, extras: Option<&PreviewExtras>) -> InfoWidgetData { +pub(super) fn selected_info( + session: &SessionInfo, + extras: Option<&PreviewExtras>, +) -> InfoWidgetData { let mut data = extras .and_then(|extras| extras.info_data.clone()) .unwrap_or_else(|| InfoWidgetData { @@ -124,6 +127,7 @@ fn selected_info(session: &SessionInfo, extras: Option<&PreviewExtras>) -> InfoW data } +#[cfg(test)] fn next_prompt_number(session: &SessionInfo, extras: Option<&PreviewExtras>) -> usize { let visible_users = session .messages_preview @@ -144,6 +148,7 @@ fn next_prompt_number(session: &SessionInfo, extras: Option<&PreviewExtras>) -> /// Draw only the image rows intersecting the scrolling transcript. Raster /// images retain their original fit box when partially scrolled off screen. +#[cfg(test)] pub(super) fn draw_images( frame: &mut Frame, area: Rect, @@ -226,6 +231,7 @@ pub(super) fn draw_images( /// Reserve stationary chrome around the scrolling transcript, like the live /// layout. At tiny heights prefer preserving one transcript row over widgets. +#[cfg(test)] pub(super) fn draw( frame: &mut Frame, area: Rect, @@ -276,6 +282,7 @@ pub(super) fn draw( /// The same margin measurement and placement engine as the live chat, with /// fresh private anchors so preview selection cannot move the live HUD. +#[cfg(test)] pub(super) fn draw_widgets( frame: &mut Frame, area: Rect, diff --git a/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs b/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs index b5100548696..f87d2f5098b 100644 --- a/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs @@ -152,6 +152,7 @@ fn deliver(picker: &mut SessionPicker, messages: Vec) { sender .send(Some(LoadedSessionPreview { messages, + search: None, extras: PreviewExtras { hidden_prompts: 7, ..Default::default() @@ -281,3 +282,78 @@ fn genuinely_empty_completed_preview_remains_loaded_and_removed_sessions_release picker.reseed_grouped(vec![], vec![]); assert!(picker.preview_extras.is_empty()); } + +#[test] +fn resume_time_modes_pick_expected_timestamp() { + use super::super::time_labels::session_resume_time_for; + use crate::config::ResumeTimeMode; + use chrono::Duration; + + let now = chrono::Utc::now(); + let mut session = fixture(); + session.created_at = now - Duration::days(3); + session.last_message_time = now - Duration::hours(2); + session.last_active_at = Some(now - Duration::minutes(5)); + + let at = |s: &SessionInfo, mode| session_resume_time_for(s, mode); + // Opening without working must not refresh the default label. + assert_eq!( + at(&session, ResumeTimeMode::Message), + now - Duration::hours(2) + ); + assert_eq!( + at(&session, ResumeTimeMode::Activity), + now - Duration::minutes(5) + ); + assert_eq!( + at(&session, ResumeTimeMode::Opened), + now - Duration::minutes(5) + ); + assert_eq!( + at(&session, ResumeTimeMode::Created), + now - Duration::days(3) + ); + + // Long run after opening: message and activity follow the newest message. + session.last_message_time = now - Duration::minutes(1); + assert_eq!( + at(&session, ResumeTimeMode::Message), + now - Duration::minutes(1) + ); + assert_eq!( + at(&session, ResumeTimeMode::Activity), + now - Duration::minutes(1) + ); + assert_eq!( + at(&session, ResumeTimeMode::Opened), + now - Duration::minutes(5) + ); +} + +#[test] +fn running_session_shows_spinner_state_instead_of_age() { + let session = fixture(); + let mut picker = SessionPicker::new(vec![session.clone()]); + let text = |picker: &SessionPicker| { + picker + .render_session_item_lines_at_frame(&session, false, 0) + .iter() + .flat_map(|line| line.spans.iter().map(|span| span.content.to_string())) + .collect::() + }; + assert!(text(&picker).contains("ago")); + + picker.live_presence.insert( + session.id.clone(), + crate::session::SessionPresence { + session_id: session.id.clone(), + pid: 1, + streaming: true, + streaming_since: None, + internal: false, + }, + ); + let running = text(&picker); + assert!(running.contains("running"), "row: {running}"); + assert!(!running.contains("ago"), "row: {running}"); +} diff --git a/crates/jcode-tui/src/tui/session_picker/preview_telemetry_tests.rs b/crates/jcode-tui/src/tui/session_picker/preview_telemetry_tests.rs index ecb140ef29a..46725198b91 100644 --- a/crates/jcode-tui/src/tui/session_picker/preview_telemetry_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/preview_telemetry_tests.rs @@ -18,6 +18,7 @@ fn fixture() -> (Session, SessionTelemetry) { credential: Some(jcode_provider_core::ResolvedCredential::Oauth), account_label: Some("account-a".into()), account_usage: Some(crate::protocol::AccountUsageSnapshot { + additional_windows: Vec::new(), label: "account-a".into(), email: None, exhausted: false, diff --git a/crates/jcode-tui/src/tui/session_picker/progressive_preview.rs b/crates/jcode-tui/src/tui/session_picker/progressive_preview.rs index d216223408d..312671b59fe 100644 --- a/crates/jcode-tui/src/tui/session_picker/progressive_preview.rs +++ b/crates/jcode-tui/src/tui/session_picker/progressive_preview.rs @@ -97,7 +97,10 @@ impl SessionPicker { /// Poll promptly only while asynchronous work can change the displayed /// preview. Idle pickers retain their existing low-frequency wakeup rate. pub(crate) fn has_pending_preview_work(&self) -> bool { - self.pending_preview_load.is_some() || self.pending_workspace_load.is_some() + self.search_edit_pending + || self.transcript_search.pending() + || self.pending_preview_load.is_some() + || self.pending_workspace_load.is_some() } pub(super) fn preview_poll_interval(&self) -> Duration { diff --git a/crates/jcode-tui/src/tui/session_picker/progressive_preview_tests.rs b/crates/jcode-tui/src/tui/session_picker/progressive_preview_tests.rs index b191899c980..543b6933bb5 100644 --- a/crates/jcode-tui/src/tui/session_picker/progressive_preview_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/progressive_preview_tests.rs @@ -8,6 +8,7 @@ use std::sync::{ fn preview() -> LoadedSessionPreview { LoadedSessionPreview { messages: Vec::new(), + search: None, extras: PreviewExtras { hidden_prompts: 7, todos_payload: Some("selected todos".into()), diff --git a/crates/jcode-tui/src/tui/session_picker/queued_submission_tests.rs b/crates/jcode-tui/src/tui/session_picker/queued_submission_tests.rs index 3a24c9cc5fe..029cbdb7f81 100644 --- a/crates/jcode-tui/src/tui/session_picker/queued_submission_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/queued_submission_tests.rs @@ -51,6 +51,12 @@ fn pending_picker() -> (SessionPicker, mpsc::Sender<(String, SearchRank)>) { .handle_overlay_key(KeyCode::Char(c), KeyModifiers::NONE) .unwrap(); } + picker.rebuild_items(); + picker.transcript_search.ranks.insert( + "stale".into(), + literal_metadata_rank("needle", &["needle".into()]), + ); + picker.rebuild_items(); assert_eq!(picker.selected_session().unwrap().id, "stale"); let (sender, receiver) = mpsc::channel(); picker.transcript_search.started_at = None; @@ -180,6 +186,7 @@ fn manual_selection_submits_immediately_without_following_background_results() { fn no_matches_at_deadline_keeps_query_and_does_not_submit_later() { let (mut picker, sender) = pending_picker(); picker.all_sessions[0].search_index = "unrelated".into(); + picker.transcript_search.ranks.clear(); picker.rebuild_items(); assert!(picker.visible_sessions.is_empty()); enter(&mut picker); diff --git a/crates/jcode-tui/src/tui/session_picker/relevance_tests.rs b/crates/jcode-tui/src/tui/session_picker/relevance_tests.rs index 1cb690dd3dc..39b6fbafa9b 100644 --- a/crates/jcode-tui/src/tui/session_picker/relevance_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/relevance_tests.rs @@ -70,6 +70,7 @@ fn relevance_overrides_recency_and_clearing_restores_default_order_and_selection assert_eq!(ids(&picker), ["recent", "whole", "exact"]); picker.search_query = "needle".into(); picker.rebuild_items(); + wait(&mut picker); assert_eq!(ids(&picker), ["exact", "whole", "recent"]); assert_eq!(picker.selected_session().unwrap().id, "exact"); wait(&mut picker); @@ -91,9 +92,9 @@ fn progressive_transcript_hits_keep_metadata_rank_and_selected_session() { let mut picker = SessionPicker::new(vec![transcript, partial, direct]); picker.search_query = "needle thread".into(); picker.rebuild_items(); - assert_eq!(ids(&picker), ["direct"]); - let selected = picker.selected_session().unwrap().id.clone(); wait(&mut picker); + let selected = picker.selected_session().unwrap().id.clone(); + assert_eq!(selected, "direct"); assert_eq!(ids(&picker), ["direct", "partial", "transcript"]); assert_eq!(picker.selected_session().unwrap().id, selected); picker.rebuild_items(); @@ -184,6 +185,7 @@ fn global_search_does_not_boost_saved_or_server_sections() { ); picker.search_query = "needle".into(); picker.rebuild_items(); + wait(&mut picker); assert_eq!(ids(&picker), ["orphan", "server", "saved"]); assert!(picker.item_to_session.iter().all(Option::is_some)); picker.search_query.clear(); @@ -403,3 +405,105 @@ fn fuzzy_transcript_ranking_uses_matched_words_not_only_recency() { wait(&mut picker); assert_eq!(ids(&picker), ["close", "loose"]); } + +#[test] +fn user_prompt_transcript_hit_beats_recent_tool_output_hit() { + let temp = tempfile::tempdir().unwrap(); + let user_path = temp.path().join("user.jsonl"); + let tool_path = temp.path().join("tool.jsonl"); + std::fs::write( + &user_path, + concat!( + r#"{"type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"please fix zanzibar parsing"}]}}"#, + "\n" + ), + ) + .unwrap(); + std::fs::write( + &tool_path, + concat!( + r#"{"type":"response_item","payload":{"type":"function_call_output","call_id":"c1","output":"grep found zanzibar parsing"}}"#, + "\n" + ), + ) + .unwrap(); + let tool = session("tool", "unrelated", 0, &tool_path); + let user = session("user", "unrelated", 500, &user_path); + let mut picker = SessionPicker::new(vec![tool, user]); + picker.search_query = "zanzibar parsing".into(); + picker.rebuild_items(); + wait(&mut picker); + assert_eq!(ids(&picker), ["user", "tool"]); +} + +#[test] +fn retyped_later_user_prompt_beats_scattered_literal_hits() { + let temp = tempfile::tempdir().unwrap(); + let target_path = temp.path().join("target.jsonl"); + let noise_path = temp.path().join("noise.jsonl"); + let user = |text: &str| { + format!( + "{{\"type\":\"response_item\",\"payload\":{{\"type\":\"message\",\"role\":\"user\",\"content\":[{{\"type\":\"input_text\",\"text\":\"{text}\"}}]}}}}\n" + ) + }; + std::fs::write( + &target_path, + user("can you tell me about resume session search") + &user("okay ltes do that"), + ) + .unwrap(); + // Every query word appears literally, but never together. + std::fs::write( + &noise_path, + user("okay great") + &user("oaky is a typo") + &user("ltes test") + &user("do that later"), + ) + .unwrap(); + for query in ["oaky ltes do that", "okay ltes do that"] { + let target = session("target", "resume search", 500, &target_path); + let noise = session("noise", "unrelated", 0, &noise_path); + let mut picker = SessionPicker::new(vec![noise, target]); + picker.search_query = query.into(); + picker.rebuild_items(); + wait(&mut picker); + assert_eq!( + ids(&picker).first().map(String::as_str), + Some("target"), + "{query}" + ); + } +} + +#[test] +fn typo_in_user_prompt_beats_literal_words_scattered_in_tool_output() { + let temp = tempfile::tempdir().unwrap(); + let user = |text: &str| { + format!( + "{{\"type\":\"response_item\",\"payload\":{{\"type\":\"message\",\"role\":\"user\",\"content\":[{{\"type\":\"input_text\",\"text\":\"{text}\"}}]}}}}\n" + ) + }; + let tool = |text: &str| { + format!( + "{{\"type\":\"response_item\",\"payload\":{{\"type\":\"function_call_output\",\"call_id\":\"c\",\"output\":\"{text}\"}}}}\n" + ) + }; + let mut sessions = Vec::new(); + // Every word appears literally, but "zanzibar" only in a tool dump. + for index in 0..8 { + let path = temp.path().join(format!("common{index}.jsonl")); + std::fs::write(&path, user("build the app") + &tool("zanzibar")).unwrap(); + sessions.push(session( + &format!("common{index}"), + "unrelated", + index, + &path, + )); + } + // The user typed the distinctive word, with a typo in a common one. + let path = temp.path().join("rare.jsonl"); + std::fs::write(&path, user("biuld the zanzibar parser")).unwrap(); + sessions.push(session("rare", "unrelated", 500, &path)); + let mut picker = SessionPicker::new(sessions); + picker.search_query = "build the zanzibar".into(); + picker.rebuild_items(); + wait(&mut picker); + assert_eq!(ids(&picker).first().map(String::as_str), Some("rare")); +} diff --git a/crates/jcode-tui/src/tui/session_picker/render.rs b/crates/jcode-tui/src/tui/session_picker/render.rs index 64e81bfa374..a59c285ba00 100644 --- a/crates/jcode-tui/src/tui/session_picker/render.rs +++ b/crates/jcode-tui/src/tui/session_picker/render.rs @@ -197,16 +197,14 @@ impl SessionPicker { &self, session: &SessionInfo, is_selected: bool, - _spinner_frame: usize, + spinner_frame: usize, ) -> Vec> { let dim: Color = rgb(100, 100, 100); let dimmer: Color = rgb(70, 70, 70); let batch_restore: Color = rgb(255, 140, 140); - // The list is ordered by last activity, so show that instead of the - // creation time (a day-old session active a minute ago sits on top). - let active_ago = - format_time_ago(session.last_active_at.unwrap_or(session.last_message_time)); + // Same timestamp the list is sorted by (`display.resume_time`). + let active_ago = format_time_ago(session_resume_time(session)); let in_batch_restore = self.crashed_session_ids.contains(&session.id); let is_marked = self.selected_session_ids.contains(&session.id); let same_dir = self.session_in_current_dir(session); @@ -322,8 +320,18 @@ impl SessionPicker { .add_modifier(Modifier::BOLD), )); } - let activity = format!(" {active_ago}"); - detail_spans.push(Span::styled(activity, Style::default().fg(dim))); + // A running turn is a state, not an age: show a spinner instead of + // a stale "x ago" that would count up while the agent is busy. + if self.session_is_streaming(session) { + let frames = jcode_tui_render::swarm_gallery::STRIP_SPINNER_FRAMES; + detail_spans.push(Span::styled( + format!(" {} running", frames[spinner_frame % frames.len()]), + Style::default().fg(rgb(255, 193, 7)), + )); + } else { + let activity = format!(" {active_ago}"); + detail_spans.push(Span::styled(activity, Style::default().fg(dim))); + } let mut rows = vec![line1, Line::from(detail_spans)]; if let Some(reason_line) = Self::crash_reason_line(session) { rows.push(reason_line); @@ -414,6 +422,27 @@ impl SessionPicker { virtualize: bool, ) { self.poll_transcript_search(); + // Reserve a compact warning row without substituting for local sessions. + // Keep it independent of loading state so Fresh(local) cannot erase it. + let area = if let Some(warning) = self.cloud_warning.as_deref().filter(|_| area.height > 2) + { + let line = Line::from(Span::styled( + warning.to_string(), + Style::default().fg(rgb(255, 200, 100)), + )); + let line = jcode_tui_render::truncate_line_with_ellipsis_to_width( + &line, + usize::from(area.width), + ); + frame.render_widget( + Paragraph::new(line), + Rect::new(area.x, area.y, area.width, 1), + ); + Rect::new(area.x, area.y + 1, area.width, area.height - 1) + } else { + area + }; + self.last_list_area = Some(area); // List rows never wrap. Determine exact heights without formatting the // offscreen titles, prompts, paths, timestamps, or highlighted spans. let mut item_heights: Vec = self @@ -658,25 +687,27 @@ impl SessionPicker { title_parts.push(Span::styled(" ", Style::default())); - let mut help = if self.search_submission_pending() { + let mut help = if let Some(notice) = self.copy_notice { + format!(" {notice} ") + } else if self.search_submission_pending() { " confirming best match… · waits ≤1s · ↑↓ take control · Esc cancel ".to_string() } else if self.search_active && self.loading_message.is_some() { " type to filter · loading… · Esc clear · Ctrl+C cancel ".to_string() } else if self.loading_message.is_some() { " / search · s/S filter · Esc cancel ".to_string() - } else if self.transcript_search.pending() && self.search_selection.following() { + } else if self.search_busy_visible() && self.search_selection.following() { " finding best match… · auto-focus ≤1s · ↑↓ take control · Enter confirm ".to_string() - } else if self.transcript_search.pending() { + } else if self.search_busy_visible() { " searching in background… · selection held · ↑↓ nav · Enter confirm ".to_string() } else if self.search_active { " type to filter · Ctrl+J/K or ↑↓ nav · Ctrl+W word-del · Esc cancel ".to_string() } else { match crate::config::config().keybindings.session_picker_enter { crate::config::SessionPickerResumeAction::CurrentTerminal => { - " Space select · Enter in place · Ctrl+Enter new terminal · d debug · / search · h/l focus · ↑↓ · q ".to_string() + " y copy ID · Space select · Enter in place · Ctrl+Enter new terminal · d debug · / search · h/l focus · ↑↓ · q ".to_string() } crate::config::SessionPickerResumeAction::NewTerminal => { - " Space select · Enter new terminal · Ctrl+Enter in place · d debug · / search · h/l focus · ↑↓ · q ".to_string() + " y copy ID · Space select · Enter new terminal · Ctrl+Enter in place · d debug · / search · h/l focus · ↑↓ · q ".to_string() } } }; @@ -686,8 +717,7 @@ impl SessionPicker { let border_dim: Color = rgb(70, 70, 70); let border_focus: Color = rgb(130, 130, 160); - let border_color = if self.transcript_search.pending() && self.search_selection.following() - { + let border_color = if self.search_busy_visible() && self.search_selection.following() { rgb(220, 180, 100) } else if self.focus == PaneFocus::Sessions { border_focus diff --git a/crates/jcode-tui/src/tui/session_picker/session_view.rs b/crates/jcode-tui/src/tui/session_picker/session_view.rs new file mode 100644 index 00000000000..db87749274d --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/session_view.rs @@ -0,0 +1,479 @@ +//! A read-only [`TuiState`] for the selected resume candidate. +//! +//! The preview draws through the real chat renderer (`ui::draw`) instead of a +//! copied layout, so header, transcript, pinned todos, composer, status line, +//! and info widgets match the opened session by construction. Every fact comes +//! from the selected session's persisted snapshot (or its live feed), never +//! from the App behind the picker. +use crate::message::ToolCall; +use crate::tui::info_widget::InfoWidgetData; +use crate::tui::{DisplayMessage, ProcessingStatus, TuiState, markdown}; +use ratatui::text::Line; +use std::time::Duration; + +/// Live in-flight turn state for a running session, when a feed is attached. +#[derive(Clone, Debug, Default)] +pub(crate) struct LiveTurn { + pub streaming_text: String, + pub status: ProcessingStatus, + pub elapsed: Option, + pub tokens: (u64, u64), +} + +pub(crate) struct SessionView<'a> { + pub messages: &'a [DisplayMessage], + /// Unique per transcript content so the shared prepare caches never + /// return the live session's frame (or another preview's). + pub version: u64, + pub hidden_prompts: usize, + pub info: InfoWidgetData, + pub todos_payload: Option<&'a str>, + pub session_name: Option, + pub model: Option, + pub provider: Option, + pub server_name: Option, + pub server_icon: Option, + pub is_canary: bool, + pub working_dir: Option, + /// Cloud VM or SSH host the session belongs to. + pub machine: Option, + /// Local credentials only describe sessions that resume on this machine. + pub local_auth: bool, + /// Top-anchored scroll when paused; `None` follows the tail. + pub scroll: Option, + pub live: Option, + pub now_ms: u64, +} + +/// Distinguish preview versions from the live App's small monotonic counter. +pub(crate) fn preview_messages_version(content_hash: u64) -> u64 { + content_hash | (1 << 63) +} + +pub(crate) fn display_messages_from_preview( + preview: &[super::PreviewMessage], +) -> Vec { + preview + .iter() + .map(|msg| DisplayMessage { + role: msg.role.clone(), + content: msg.content.clone(), + tool_calls: msg.tool_calls.clone(), + duration_secs: msg.duration_secs, + title: msg.title.clone(), + tool_data: msg.tool_data.clone(), + }) + .collect() +} + +impl SessionView<'_> { + fn live_status(&self) -> ProcessingStatus { + self.live + .as_ref() + .map_or_else(ProcessingStatus::default, |live| live.status.clone()) + } +} + +impl TuiState for SessionView<'_> { + fn display_messages(&self) -> &[DisplayMessage] { + self.messages + } + fn display_user_message_count(&self) -> usize { + self.messages.iter().filter(|m| m.role == "user").count() + } + fn compacted_hidden_user_prompts(&self) -> usize { + self.hidden_prompts + } + fn has_display_edit_tool_messages(&self) -> bool { + self.messages.iter().any(|message| { + message + .tool_data + .as_ref() + .is_some_and(|tool| crate::tui::ui::tools_ui::is_edit_tool_name(&tool.name)) + }) + } + fn pinned_todos_payload(&self) -> Option<&str> { + self.todos_payload + } + fn side_pane_images(&self) -> Vec { + Vec::new() + } + fn display_messages_version(&self) -> u64 { + self.version + } + fn streaming_text(&self) -> &str { + self.live + .as_ref() + .map_or("", |live| live.streaming_text.as_str()) + } + fn input(&self) -> &str { + "" + } + fn cursor_pos(&self) -> usize { + 0 + } + fn is_processing(&self) -> bool { + !matches!(self.live_status(), ProcessingStatus::Idle) + } + fn queued_messages(&self) -> &[String] { + &[] + } + fn interleave_message(&self) -> Option<&str> { + None + } + fn pending_soft_interrupts(&self) -> &[String] { + &[] + } + fn scroll_offset(&self) -> usize { + self.scroll.unwrap_or(0) + } + fn auto_scroll_paused(&self) -> bool { + self.scroll.is_some() + } + fn provider_name(&self) -> String { + self.provider + .as_ref() + .map_or_else(String::new, Clone::clone) + } + fn provider_model(&self) -> String { + self.model.as_ref().map_or_else(String::new, Clone::clone) + } + fn upstream_provider(&self) -> Option { + None + } + fn connection_type(&self) -> Option { + None + } + fn status_detail(&self) -> Option { + None + } + fn mcp_servers(&self) -> Vec<(String, usize)> { + Vec::new() + } + fn available_skills(&self) -> Vec { + Vec::new() + } + fn streaming_tokens(&self) -> (u64, u64) { + self.live.as_ref().map_or((0, 0), |live| live.tokens) + } + fn streaming_cache_tokens(&self) -> (Option, Option) { + (None, None) + } + fn output_tps(&self) -> Option { + None + } + fn streaming_tool_calls(&self) -> Vec { + Vec::new() + } + fn elapsed(&self) -> Option { + self.live.as_ref().and_then(|live| live.elapsed) + } + fn status(&self) -> ProcessingStatus { + self.live_status() + } + fn command_suggestions(&self) -> Vec<(String, &'static str)> { + Vec::new() + } + fn active_skill(&self) -> Option { + None + } + fn subagent_status(&self) -> Option { + None + } + fn batch_progress(&self) -> Option { + None + } + fn time_since_activity(&self) -> Option { + None + } + fn total_session_tokens(&self) -> Option<(u64, u64)> { + None + } + fn is_remote_mode(&self) -> bool { + false + } + fn is_canary(&self) -> bool { + self.is_canary + } + fn is_replay(&self) -> bool { + false + } + fn diff_mode(&self) -> crate::config::DiffDisplayMode { + crate::config::config().display.diff_mode + } + fn current_session_id(&self) -> Option { + None + } + fn session_display_name(&self) -> Option { + self.session_name.clone() + } + fn server_display_name(&self) -> Option { + self.server_name.clone() + } + fn server_display_icon(&self) -> Option { + self.server_icon.clone() + } + fn server_sessions(&self) -> Vec { + Vec::new() + } + fn connected_clients(&self) -> Option { + None + } + fn status_notice(&self) -> Option { + None + } + fn remote_startup_phase_active(&self) -> bool { + false + } + fn dictation_key_label(&self) -> Option { + None + } + fn animation_elapsed(&self) -> f32 { + self.now_ms as f32 / 1000.0 + } + fn rate_limit_remaining(&self) -> Option { + None + } + fn queue_mode(&self) -> bool { + false + } + fn has_stashed_input(&self) -> bool { + false + } + fn context_info(&self) -> crate::prompt::ContextInfo { + Default::default() + } + fn context_limit(&self) -> Option { + self.info.context_limit + } + fn client_update_available(&self) -> bool { + false + } + fn server_update_available(&self) -> Option { + None + } + fn info_widget_data(&self) -> InfoWidgetData { + self.info.clone() + } + fn render_streaming_markdown(&self, width: usize) -> Vec> { + markdown::render_markdown_with_width(self.streaming_text(), Some(width)) + } + fn centered_mode(&self) -> bool { + // The opened session uses the user's alignment preference. + crate::config::config().display.centered + } + fn auth_status(&self) -> crate::auth::AuthStatus { + if self.local_auth && !crate::tui::is_ssh_remote() { + crate::auth::AuthStatus::check_fast_nonblocking() + } else { + Default::default() + } + } + fn update_cost(&mut self) {} + fn diagram_mode(&self) -> crate::config::DiagramDisplayMode { + crate::config::DiagramDisplayMode::None + } + fn diagram_focus(&self) -> bool { + false + } + fn diagram_index(&self) -> usize { + 0 + } + fn diagram_scroll(&self) -> (i32, i32) { + (0, 0) + } + fn diagram_pane_ratio(&self) -> u8 { + 50 + } + fn diagram_pane_ratio_user_adjusted(&self) -> bool { + false + } + fn diagram_pane_animating(&self) -> bool { + false + } + fn diagram_pane_enabled(&self) -> bool { + false + } + fn diagram_pane_position(&self) -> crate::config::DiagramPanePosition { + Default::default() + } + fn diagram_zoom(&self) -> u8 { + 100 + } + fn diff_pane_scroll(&self) -> usize { + 0 + } + fn diff_pane_scroll_x(&self) -> i32 { + 0 + } + fn side_panel_image_zoom_percent(&self) -> u8 { + 100 + } + fn diff_pane_focus(&self) -> bool { + false + } + fn side_panel(&self) -> &crate::side_panel::SidePanelSnapshot { + static EMPTY: std::sync::LazyLock = + std::sync::LazyLock::new(crate::side_panel::SidePanelSnapshot::default); + &EMPTY + } + fn pin_images(&self) -> bool { + false + } + fn chat_native_scrollbar(&self) -> bool { + crate::config::config().display.native_scrollbars.chat + } + fn side_panel_native_scrollbar(&self) -> bool { + false + } + fn inline_interactive_state(&self) -> Option<&crate::tui::InlineInteractiveState> { + None + } + fn changelog_scroll(&self) -> Option { + None + } + fn help_scroll(&self) -> Option { + None + } + fn session_picker_overlay( + &self, + ) -> Option<&std::cell::RefCell> { + None + } + fn login_picker_overlay( + &self, + ) -> Option<&std::cell::RefCell> { + None + } + fn account_picker_overlay( + &self, + ) -> Option<&std::cell::RefCell> { + None + } + fn usage_overlay( + &self, + ) -> Option<&std::cell::RefCell> { + None + } + fn working_dir(&self) -> Option { + self.working_dir.clone() + } + fn remote_machine(&self) -> Option { + self.machine.clone() + } + fn now_millis(&self) -> u64 { + self.now_ms + } + fn copy_badge_ui(&self) -> crate::tui::CopyBadgeUiState { + Default::default() + } + fn copy_selection_mode(&self) -> bool { + false + } + fn copy_selection_range(&self) -> Option { + None + } + fn copy_selection_status(&self) -> Option { + None + } + fn suggestion_prompts(&self) -> Vec<(String, String)> { + Vec::new() + } + fn cache_ttl_status(&self) -> Option { + None + } +} + +/// Render `view` with the real chat renderer into `area` of `frame`, isolated +/// from the live session's published state. Returns the resolved geometry and +/// the prepared transcript it drew. +pub(crate) fn draw_session_view( + frame: &mut ratatui::Frame, + area: ratatui::layout::Rect, + view: &SessionView<'_>, +) -> ( + crate::tui::ui::SecondaryGeometry, + Option>, +) { + use ratatui::{Terminal, backend::TestBackend}; + if area.width == 0 || area.height == 0 { + return (Default::default(), None); + } + let center = crate::tui::markdown::center_code_blocks(); + let (rendered, geometry) = crate::tui::markdown::with_center_code_blocks(center, || { + crate::tui::ui::with_secondary_render(|| { + jcode_tui_mermaid::with_image_frame_offset(area.x, area.y, || { + let mut terminal = super::preview_result( + Terminal::new(TestBackend::new(area.width, area.height)), + "preview terminal initialization", + )?; + let completed = super::preview_result( + terminal.draw(|inner| crate::tui::ui::draw(inner, view)), + "preview render", + )?; + Some(completed.buffer.clone()) + }) + }) + }); + let prepared = crate::tui::ui::take_secondary_chat_frame(); + if let Some(buf) = rendered { + let dst = frame.buffer_mut(); + for y in 0..area.height.min(buf.area.height) { + for x in 0..area.width.min(buf.area.width) { + if let (Some(src), Some(cell)) = + (buf.cell((x, y)), dst.cell_mut((area.x + x, area.y + y))) + { + *cell = src.clone(); + } + } + } + } + (geometry, prepared) +} + +/// Restyle visible search matches in the rendered preview. Operating on the +/// drawn buffer keeps highlighting independent of how the renderer wrapped. +pub(crate) fn highlight_buffer_matches( + buf: &mut ratatui::buffer::Buffer, + area: ratatui::layout::Rect, + tokens: &[String], +) { + use ratatui::style::{Color, Modifier}; + for y in area.top()..area.bottom() { + let mut cells: Vec<(u16, char)> = Vec::new(); + for x in area.left()..area.right() { + if let Some(cell) = buf.cell((x, y)) { + let mut chars = cell.symbol().chars(); + if let (Some(ch), None) = (chars.next(), chars.next()) { + cells.push((x, ch.to_lowercase().next().unwrap_or(ch))); + } else { + cells.push((x, '\0')); + } + } + } + for token in tokens { + let needle: Vec = token.chars().collect(); + if needle.is_empty() || needle.len() > cells.len() { + continue; + } + let mut i = 0; + while i + needle.len() <= cells.len() { + if cells[i..i + needle.len()] + .iter() + .zip(&needle) + .all(|((_, c), n)| c == n) + { + for (x, _) in &cells[i..i + needle.len()] { + if let Some(cell) = buf.cell_mut((*x, y)) { + cell.set_fg(Color::Rgb(255, 214, 90)); + cell.modifier.insert(Modifier::BOLD); + } + } + i += needle.len(); + } else { + i += 1; + } + } + } + } +} diff --git a/crates/jcode-tui/src/tui/session_picker/time_labels.rs b/crates/jcode-tui/src/tui/session_picker/time_labels.rs index 358a29c70a8..6bc4f882d68 100644 --- a/crates/jcode-tui/src/tui/session_picker/time_labels.rs +++ b/crates/jcode-tui/src/tui/session_picker/time_labels.rs @@ -1,5 +1,26 @@ //! Compact session age labels. +use crate::config::ResumeTimeMode; +use jcode_tui_session_picker::SessionInfo; + +/// Timestamp Resume shows and sorts by, per `display.resume_time`. +pub(crate) fn session_resume_time(session: &SessionInfo) -> chrono::DateTime { + session_resume_time_for(session, crate::config::config().display.resume_time) +} + +pub(crate) fn session_resume_time_for( + session: &SessionInfo, + mode: ResumeTimeMode, +) -> chrono::DateTime { + let message = session.last_message_time; + match mode { + ResumeTimeMode::Message => message, + ResumeTimeMode::Activity => session.last_active_at.map_or(message, |a| a.max(message)), + ResumeTimeMode::Opened => session.last_active_at.unwrap_or(message), + ResumeTimeMode::Created => session.created_at, + } +} + pub(crate) fn format_time_ago(time: chrono::DateTime) -> String { let now = chrono::Utc::now(); let duration = now.signed_duration_since(time); diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search.rs index 7eb225d4e71..6eda0ede26f 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search.rs @@ -1,9 +1,9 @@ //! Incremental fuzzy and full-transcript search without typing delays. -//! Only cheap literal metadata and resident-cache hints run on the UI thread, -//! never fuzzy DP or transcript file reads. +//! All matching, including literal metadata and identity fields, runs on +//! cancellable workers. The UI only consumes bounded batches of rank evidence. use super::loading::search_query_tokens; use super::{ResumeTarget, SessionInfo}; -use serde::de::{DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor}; +use serde::de::DeserializeSeed; use std::collections::HashMap; use std::io::{BufRead, BufReader, Read}; use std::path::{Path, PathBuf}; @@ -19,9 +19,12 @@ use std::time::Instant; mod text_cache; mod word_scores; use word_scores::WordScores; +pub(super) mod source; mod token_cache; mod warmup; +use source::Source; +#[cfg(test)] const IMMEDIATE_METADATA_BYTES: usize = 1024 * 1024; /// A close contiguous opening request outranks incidental transcript evidence. @@ -30,6 +33,11 @@ const IMMEDIATE_METADATA_BYTES: usize = 1024 * 1024; #[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)] pub(super) struct SearchRank { opening_prompt: usize, + /// Sum of per-token origin weights scaled by word rarity: user prompts + /// beat replies, which beat tool calls, tool output, and reasoning, and a + /// rare word outweighs ones found in most sessions. Ranked above literal + /// so a single typo cannot sink a session with strong, distinctive hits. + source_weight: u32, literal: bool, intent_terms: usize, exact_field: bool, @@ -67,6 +75,7 @@ pub(super) fn literal_metadata_rank(index: &str, tokens: &[String]) -> SearchRan } SearchRank { opening_prompt: 0, + source_weight: 0, literal: metadata_terms == tokens.len(), intent_terms: 0, exact_field: false, @@ -76,6 +85,7 @@ pub(super) fn literal_metadata_rank(index: &str, tokens: &[String]) -> SearchRan } } +#[cfg(test)] pub(super) fn exact_metadata_field(session: &SessionInfo, query: &str) -> bool { [&session.title, &session.short_name, &session.id] .iter() @@ -104,11 +114,17 @@ pub(super) fn lowercase_field(field: &str) -> std::borrow::Cow<'_, str> { } impl SearchRank { + #[cfg(test)] pub(super) fn with_intent_terms(mut self, terms: usize) -> Self { self.intent_terms = terms; self } + pub(super) fn with_source_weight(mut self, weight: u32) -> Self { + self.source_weight = weight; + self + } + pub(super) fn with_exact_field(mut self, exact: bool) -> Self { self.exact_field = exact; self @@ -135,14 +151,167 @@ fn fuzzy_metadata_rank_with_scores( rank } -struct Candidate { +/// Query-independent search inputs for one session. Shared with the worker +/// so that a keystroke never copies the metadata index on the UI thread. +pub(super) struct Entry { id: String, paths: Vec, search_index: String, - missing_tokens: Vec, - exact_field: bool, + title: String, + short_name: String, + save_label: Option, + has_prompt: bool, opening_prompt: String, intent_text: String, + working_dir: Option, + server_name: Option, + cloud: bool, +} + +/// Bytes compared at each end of an index to detect an in-place refresh. +const INDEX_PROBE_BYTES: usize = 64; + +impl Entry { + fn new(session: &SessionInfo) -> Self { + Self { + id: session.id.clone(), + paths: transcript_paths(session), + search_index: session.search_index.clone(), + title: session.title.clone(), + short_name: session.short_name.clone(), + save_label: session.save_label.clone(), + working_dir: session.working_dir.clone(), + server_name: session.server_name.clone(), + cloud: session.server_icon.as_deref() == Some("☁"), + has_prompt: session.first_user_prompt.is_some(), + opening_prompt: session + .first_user_prompt + .as_deref() + .unwrap_or("") + .to_owned(), + intent_text: format!( + "{}\n{}\n{}", + session.title, + session.save_label.as_deref().unwrap_or(""), + session.first_user_prompt.as_deref().unwrap_or(""), + ), + } + } + + /// Cheap check that this entry still reflects `session`, so an unchanged + /// history is reused across keystrokes without comparing whole indexes. + fn describes(&self, session: &SessionInfo) -> bool { + let (old, new) = ( + self.search_index.as_bytes(), + session.search_index.as_bytes(), + ); + let probe = INDEX_PROBE_BYTES.min(old.len()); + self.id == session.id + && old.len() == new.len() + && old[..probe] == new[..probe] + && old[old.len() - probe..] == new[new.len() - probe..] + && self.title == session.title + && self.short_name == session.short_name + && self.save_label == session.save_label + && self.working_dir == session.working_dir + && self.server_name == session.server_name + && self.cloud == (session.server_icon.as_deref() == Some("☁")) + && self.has_prompt == session.first_user_prompt.is_some() + && self.opening_prompt == session.first_user_prompt.as_deref().unwrap_or("") + } + + fn exact_field(&self, query: &str) -> bool { + [&self.title, &self.short_name, &self.id] + .iter() + .any(|field| normalized_equals(field, query)) + || self + .save_label + .as_ref() + .is_some_and(|label| normalized_equals(label, query)) + } + + fn identity_rank(&self, tokens: &[String]) -> (bool, bool, bool) { + super::identity_search::rank_fields( + [ + &self.id, + &self.short_name, + &self.title, + self.save_label.as_deref().unwrap_or(""), + self.working_dir.as_deref().unwrap_or(""), + self.server_name.as_deref().unwrap_or(""), + ], + self.cloud, + tokens, + ) + } +} + +struct Candidate { + entry: Arc, + missing_tokens: Vec, + exact_field: bool, + /// Per-token origin evidence from metadata, before transcript reads. + prior: Vec>, +} + +impl std::ops::Deref for Candidate { + type Target = Entry; + fn deref(&self) -> &Entry { + &self.entry + } +} + +/// Decide which sessions need worker evidence for `query`. With a deferred +/// (large) history this runs on the worker, never on the keystroke. +fn build_candidates( + corpus: &[Arc], + query: &str, + tokens: &[String], + defer_metadata: bool, +) -> Vec { + let mut candidates = Vec::new(); + for entry in corpus { + let missing_tokens: Vec<_> = tokens + .iter() + .filter(|token| defer_metadata || !entry.search_index.contains(token.as_str())) + .cloned() + .collect(); + // Literal metadata matches still go to the worker, which upgrades + // their rank once it knows where in the transcript each word came from, + // unless every word already comes from a user-intent field. + if !defer_metadata && missing_tokens.is_empty() && (tokens.len() < 4 || !entry.has_prompt) { + let intent = lowercase_field(&entry.intent_text); + if tokens.iter().all(|token| intent.contains(token.as_str())) { + continue; + } + } + candidates.push(Candidate { + entry: entry.clone(), + missing_tokens, + exact_field: entry.exact_field(query), + prior: Vec::new(), + }); + } + candidates +} + +/// Per-token metadata origins for the UI-thread ranking of sessions the worker +/// has not ranked yet. Uses the already lowercased intent fields. +#[cfg(test)] +pub(super) fn metadata_source_weight( + intent_fields: &[&str], + index: &str, + tokens: &[String], + idf: &[u32], +) -> u32 { + source::weighted_source_weight(&source::metadata_sources(intent_fields, index, tokens), idf) +} + +/// Transcript evidence: the first-match fuzzy score plus per-token origins. +#[derive(Clone, Debug, PartialEq, Eq)] +struct Evidence { + score: i64, + sources: Vec>, } /// Rank in memory before publishing results or opening transcripts. Keep this @@ -150,10 +319,12 @@ struct Candidate { /// the incoming order for equally promising candidates. fn prioritize_candidates( candidates: Vec, - query: &str, + phrase: &Option, tokens: &[String], + idf: &[u32], cancel: &AtomicBool, ) -> Vec<(Candidate, SearchRank)> { + let phrase = phrase.as_ref(); let mut ranked = Vec::with_capacity(candidates.len()); for candidate in candidates { if cancel.load(Ordering::Relaxed) { @@ -161,12 +332,17 @@ fn prioritize_candidates( } let mut rank = literal_metadata_rank(&candidate.search_index, tokens) .with_exact_field(candidate.exact_field); - rank.opening_prompt = opening_prompt_rank(&candidate.opening_prompt, query); + rank.opening_prompt = phrase.map_or(0, |phrase| phrase.rank(&candidate.opening_prompt)); let intent = lowercase_field(&candidate.intent_text); rank.intent_terms = tokens .iter() .filter(|token| intent.contains(token.as_str())) .count(); + let prior = source::metadata_sources(&[&intent], &candidate.search_index, tokens); + drop(intent); + let mut candidate = candidate; + candidate.prior = prior; + rank.source_weight = source::weighted_source_weight(&candidate.prior, idf); ranked.push((candidate, rank)); } ranked.sort_by_key(|(_, rank)| std::cmp::Reverse(*rank)); @@ -176,60 +352,177 @@ fn prioritize_candidates( // A retyped request is stronger evidence than words scattered throughout a // huge transcript. Compare contiguous word windows, allowing a few misspelled // words without requiring every short function word to match literally. +#[cfg(test)] fn opening_prompt_rank(prompt: &str, query: &str) -> usize { - let words = |text: &str| -> Vec { - text.split_whitespace() - .take(256) - .map(|word| { - word.trim_matches(|c: char| !c.is_alphanumeric()) - .to_lowercase() + PhraseQuery::new(query).map_or(0, |phrase| phrase.rank(prompt)) +} + +fn phrase_words(text: &str, limit: usize) -> Vec { + text.split_whitespace() + .take(limit) + .map(|word| { + word.trim_matches(|c: char| !c.is_alphanumeric()) + .to_lowercase() + }) + .filter(|word| !word.is_empty()) + .collect() +} + +/// A multiword query prepared once and compared against many user prompts. +struct PhraseQuery { + words: Vec, + compact: String, + prepared: Vec, +} + +impl PhraseQuery { + fn new(query: &str) -> Option { + let words = phrase_words(query, 256); + if words.len() < 4 || words.len() > 64 { + return None; + } + Some(Self { + compact: words.concat(), + prepared: words + .iter() + .map(|word| jcode_fuzzy::PreparedTokenQuery::new(word)) + .collect(), + words, + }) + } + + fn rank(&self, prompt: &str) -> usize { + let prompt = phrase_words(prompt, 2048); + if prompt.len() + 1 < self.words.len() * 4 / 5 { + return 0; + } + // Missing spaces ("ofhow") should not hide an otherwise exact request. + if self.compact.chars().count() >= 16 && prompt.concat().contains(&self.compact) { + return self.words.len(); + } + prompt + .windows(self.words.len()) + .filter_map(|window| { + let matched = self + .words + .iter() + .zip(&self.prepared) + .zip(window) + .filter(|((word, prepared), candidate)| { + word == candidate + || (word.len() >= 4 + && candidate.len() <= 128 + && prepared.matches_search_text(candidate)) + }) + .count(); + (matched * 5 >= self.words.len() * 4).then_some(matched) }) - .filter(|word| !word.is_empty()) - .collect() + .max() + .unwrap_or(0) + } +} + +/// Best retyped-request match across every user prompt in a transcript, not +/// only the opening one. Later follow-ups ("okay lets do that") are what +/// people remember and retype just as often. +fn user_prompt_phrase_rank(paths: &[PathBuf], phrase: &PhraseQuery, cancel: &AtomicBool) -> usize { + let mut sink = PhraseSink { + phrase, + best: 0, + cancel, }; - let query = words(query); - if query.len() < 4 || query.len() > 64 { - return 0; + for path in paths { + if cancel.load(Ordering::Relaxed) || sink.best == phrase.words.len() { + break; + } + if let Some(segments) = text_cache::segments(path, cancel) { + for (text, source) in segments.iter() { + if source == Source::User { + sink.best = sink.best.max(phrase.rank(text)); + } + } + continue; + } + stream_file(path, &mut sink, cancel); } - let prompt = words(prompt); - // Missing spaces ("ofhow") should not hide an otherwise exact request. - let compact_query = query.concat(); - if compact_query.chars().count() >= 16 && prompt.concat().contains(&compact_query) { - return query.len(); + sink.best +} + +struct PhraseSink<'a> { + phrase: &'a PhraseQuery, + best: usize, + cancel: &'a AtomicBool, +} + +impl source::Sink for PhraseSink<'_> { + fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str> { + if self.cancel.load(Ordering::Relaxed) { + return Err("search cancelled"); + } + if source == Source::User { + self.best = self.best.max(self.phrase.rank(text)); + if self.best == self.phrase.words.len() { + return Err("search complete"); + } + } + Ok(()) } - let prepared: Vec<_> = query - .iter() - .map(|word| jcode_fuzzy::PreparedTokenQuery::new(word)) - .collect(); - prompt - .windows(query.len()) - .filter_map(|window| { - let matched = query - .iter() - .zip(&prepared) - .zip(window) - .filter(|((word, prepared), candidate)| { - word == candidate - || (word.len() >= 4 - && candidate.len() <= 128 - && prepared.matches_search_text(candidate)) - }) - .count(); - (matched * 5 >= query.len() * 4).then_some(matched) - }) - .max() - .unwrap_or(0) } +/// Stream a transcript too large for the text cache through a sink. +fn stream_file(path: &Path, sink: &mut S, cancel: &AtomicBool) { + let Ok(file) = std::fs::File::open(path) else { + return; + }; + let mut reader = BufReader::with_capacity( + 64 * 1024, + CancellableReader { + reader: file, + cancel, + }, + ); + if path.extension().is_some_and(|ext| ext == "jsonl") { + let mut line = Vec::new(); + while !cancel.load(Ordering::Relaxed) { + line.clear(); + match reader.read_until(b'\n', &mut line) { + Ok(0) | Err(_) => break, + Ok(_) => { + if let Err(error) = source::Walk::new(&mut *sink) + .deserialize(&mut serde_json::Deserializer::from_slice(&line)) + { + crate::logging::debug(&format!( + "Skipping invalid transcript search record: {error}" + )); + } + } + } + } + return; + } + let mut deserializer = serde_json::Deserializer::from_reader(reader); + while source::Walk::new(&mut *sink) + .deserialize(&mut deserializer) + .is_ok() + {} +} + +type IdentityRanks = HashMap; + #[derive(Default)] pub(super) struct TranscriptSearch { query: String, + /// One shared entry per session, reused across queries. + corpus: Vec>, candidates: Vec, started_at: Option, receiver: Option>, cancel: Arc, ranks: HashMap, defer_metadata: bool, + /// Per-token rarity weights, computed by the worker before any rank. + idf: Arc>>, + identities: Arc>, } impl Drop for TranscriptSearch { @@ -247,58 +540,26 @@ impl TranscriptSearch { if self.query == query { return; } + // Reuse every unchanged entry. Only a new or refreshed session copies + // its metadata index, so typing costs no per-keystroke megabytes. + let mut previous = std::mem::take(&mut self.corpus).into_iter(); + let corpus: Vec> = sessions + .map(|session| match previous.next() { + Some(entry) if entry.describes(session) => entry, + _ => Arc::new(Entry::new(session)), + }) + .collect(); *self = Self::default(); + self.corpus = corpus; self.query = query.to_string(); if query.is_empty() { return; } - let tokens = search_query_tokens(query); - let sessions: Vec<_> = sessions.collect(); - self.defer_metadata = sessions - .iter() - .map(|session| session.search_index.len()) - .sum::() - > IMMEDIATE_METADATA_BYTES; - for session in sessions { - let missing_tokens: Vec<_> = tokens - .iter() - .filter(|token| { - self.defer_metadata || !session.search_index.contains(token.as_str()) - }) - .cloned() - .collect(); - if missing_tokens.is_empty() - && (tokens.len() < 4 || session.first_user_prompt.is_none()) - { - continue; - } - let paths = transcript_paths(session); - self.candidates.push(Candidate { - id: session.id.clone(), - paths, - search_index: session.search_index.clone(), - missing_tokens, - exact_field: exact_metadata_field(session, query), - opening_prompt: session - .first_user_prompt - .as_deref() - .unwrap_or("") - .to_owned(), - intent_text: format!( - "{}\n{}\n{}", - session.title, - session.save_label.as_deref().unwrap_or(""), - session.first_user_prompt.as_deref().unwrap_or(""), - ), - }); - } - if !self.candidates.is_empty() { + self.defer_metadata = true; + // Candidate selection touches every index and always belongs to the + // worker, including tiny histories. + if !self.corpus.is_empty() { self.started_at = Some(Instant::now()); - warmup::schedule( - self.candidates - .iter() - .flat_map(|candidate| candidate.paths.iter()), - ); } } @@ -317,6 +578,24 @@ impl TranscriptSearch { .map_or_else(SearchRank::default, |rank| *rank) } + pub(super) fn identity_rank(&self, id: &str) -> (bool, bool, bool) { + self.identities + .get() + .and_then(|identities| identities.get(id).copied()) + .unwrap_or((false, false, false)) + } + + /// Rarity weights for the current query, once the worker computed them. + #[cfg(test)] + pub(super) fn idf(&self) -> &[u32] { + self.idf.get().map_or(&[], Vec::as_slice) + } + + /// The worker reported at least one match for the current query. + pub(super) fn has_results(&self) -> bool { + !self.ranks.is_empty() + } + pub(super) fn defers_metadata(&self) -> bool { self.defer_metadata } @@ -330,24 +609,68 @@ impl TranscriptSearch { pub(super) fn poll(&mut self) -> bool { if self.started_at.take().is_some() { let candidates = std::mem::take(&mut self.candidates); + let deferred = self.defer_metadata.then(|| self.corpus.clone()); let cancel = self.cancel.clone(); let tokens = search_query_tokens(&self.query); let query = self.query.clone(); + let shared_idf = self.idf.clone(); + let shared_identities = self.identities.clone(); let (sender, receiver) = mpsc::channel(); match std::thread::Builder::new() .name("jcode-transcript-search".into()) .spawn(move || { - // Finish the in-memory fuzzy pass before opening any large - // transcripts. Neither DP matching nor disk scans belong - // on the keystroke/render thread. - let mut scores = WordScores::default(); - let mut fuzzy_candidates = Vec::new(); - for (mut candidate, priority) in - prioritize_candidates(candidates, &query, &tokens, &cancel) - { - if cancel.load(Ordering::Relaxed) { + let candidates = match deferred { + Some(corpus) => { + let candidates = build_candidates(&corpus, &query, &tokens, true); + warmup::schedule( + candidates + .iter() + .flat_map(|candidate| candidate.paths.iter()), + ); + candidates + } + None => candidates, + }; + if cancel.load(Ordering::Relaxed) { + return; + } + let identities: HashMap<_, _> = candidates + .iter() + .filter_map(|candidate| { + let rank = candidate.identity_rank(&tokens); + rank.0.then(|| (candidate.id.clone(), rank)) + }) + .collect(); + let identity_ids: Vec<_> = identities.keys().cloned().collect(); + if shared_identities.set(identities).is_err() { + crate::logging::debug("Transcript search identities already initialized"); + } + for id in identity_ids { + if sender.send((id, SearchRank::default())).is_err() { return; } + } + // Document frequency over every candidate's metadata index. + // Off the render thread: this touches every session. + let idf = source::idf_weights( + &tokens, + candidates + .iter() + .map(|candidate| candidate.search_index.as_str()), + ); + if shared_idf.set(idf.clone()).is_err() { + crate::logging::debug("Transcript search weights already initialized"); + } + // Transcript reads and fuzzy scans dominate. Shard sessions + // across workers round-robin so every shard sees the same + // mix of recent and old sessions and ranks arrive evenly. + let workers = search_workers(candidates.len()); + // Rank in memory first so the strongest retyped openings + // are published before any shard starts reading disk. + let phrase = PhraseQuery::new(&query); + let ranked = prioritize_candidates(candidates, &phrase, &tokens, &idf, &cancel); + let mut scores = WordScores::default(); + for (candidate, priority) in &ranked { if priority.opening_prompt > 0 { let mut rank = fuzzy_metadata_rank_with_scores( &candidate.search_index, @@ -356,161 +679,33 @@ impl TranscriptSearch { ); rank.opening_prompt = priority.opening_prompt; rank.intent_terms = priority.intent_terms; + rank.source_weight = priority.source_weight; if sender.send((candidate.id.clone(), rank)).is_err() { return; } } - candidate - .missing_tokens - .retain(|token| !candidate.search_index.contains(token.as_str())); - if candidate.missing_tokens.is_empty() { - let rank = literal_metadata_rank(&candidate.search_index, &tokens) - .with_exact_field(candidate.exact_field); - if sender.send((candidate.id, rank)).is_err() { - return; - } - continue; - } - fuzzy_candidates.push((candidate, priority)); - } - let mut transcripts = Vec::new(); - for (mut candidate, priority) in fuzzy_candidates { - if cancel.load(Ordering::Relaxed) { - return; - } - let mut fuzzy_missing = candidate.missing_tokens.clone(); - let mut rank = fuzzy_metadata_rank_with_scores( - &candidate.search_index, - &tokens, - &mut scores, - ) - .with_exact_field(candidate.exact_field); - rank.opening_prompt = priority.opening_prompt; - rank.intent_terms = priority.intent_terms; - fuzzy_missing - .retain(|token| !scores.matches_text(token, &candidate.search_index)); - candidate.search_index = String::new(); - if fuzzy_missing.is_empty() - && sender.send((candidate.id.clone(), rank)).is_err() - { - return; - } - if !candidate.paths.is_empty() { - transcripts.push((candidate, fuzzy_missing, rank)); - } } - // Fuzzy scoring adds evidence unavailable to the cheap - // pass. Use it to schedule disk reads, too. - transcripts.sort_by_key(|(_, _, rank)| std::cmp::Reverse(*rank)); - let mut cold_transcripts = Vec::new(); - let mut warm_fuzzy = Vec::new(); - // Reuse resident token evidence before waiting for a typing - // pause. Never narrow fuzzy candidates using literal results. - for (candidate, fuzzy_missing, rank) in transcripts { - if cancel.load(Ordering::Relaxed) { - return; - } - match cached_paths_scan_with_scores( - &candidate.paths, - &candidate.missing_tokens, - &cancel, - false, - &mut scores, - ) { - Some(Some(_)) => { - if sender - .send(( - candidate.id, - SearchRank { - literal: true, - ..rank - }, - )) - .is_err() - { - return; - } - } - Some(None) if fuzzy_missing.is_empty() => {} - Some(None) => warm_fuzzy.push((candidate, fuzzy_missing, rank)), - None => cold_transcripts.push((candidate, fuzzy_missing, rank)), - } - } - for (candidate, missing, rank) in warm_fuzzy { - match cached_paths_scan_with_scores( - &candidate.paths, - &missing, - &cancel, - true, - &mut scores, - ) { - Some(Some(score)) => { - if sender - .send(( - candidate.id, - SearchRank { - score: rank.score + score, - ..rank - }, - )) - .is_err() - { - return; - } - } - Some(None) => {} - None => cold_transcripts.push((candidate, missing, rank)), - } - } - let mut fuzzy_transcripts = Vec::new(); - for (candidate, fuzzy_missing, rank) in cold_transcripts { - if cancel.load(Ordering::Relaxed) { - return; - } - if paths_match_mode( - &candidate.paths, - &candidate.missing_tokens, - &cancel, - false, - ) { - if sender - .send(( - candidate.id, - SearchRank { - literal: true, - ..rank - }, - )) - .is_err() - { - return; - } - } else if !fuzzy_missing.is_empty() { - fuzzy_transcripts.push((candidate, fuzzy_missing, rank)); - } + let mut shards: Vec> = + (0..workers).map(|_| Vec::new()).collect(); + for (index, candidate) in ranked.into_iter().enumerate() { + shards[index % workers].push(candidate); } - // Exact transcript hits must not wait behind typo-tolerant - // scans of unrelated, potentially very large sessions. - for (candidate, missing, rank) in fuzzy_transcripts { - if cancel.load(Ordering::Relaxed) { - return; - } - if let Some(score) = scan_paths_with_scores( - &candidate.paths, - &missing, - &cancel, - true, - &mut scores, - ) { - let rank = SearchRank { - score: rank.score + score, - ..rank - }; - if sender.send((candidate.id, rank)).is_err() { - return; + std::thread::scope(|scope| { + for shard in shards { + let sender = sender.clone(); + let (query, tokens, idf, cancel) = (&query, &tokens, &idf, &cancel); + let spawned = std::thread::Builder::new() + .name("jcode-transcript-search-shard".into()) + .spawn_scoped(scope, move || { + search_shard(shard, query, tokens, idf, cancel, sender) + }); + if let Err(error) = spawned { + crate::logging::debug(&format!( + "Cannot start transcript search shard: {error}" + )); } } - } + }); }) { Ok(_) => self.receiver = Some(receiver), Err(error) => { @@ -542,6 +737,294 @@ impl TranscriptSearch { } } +/// Parallel shards for one query. Bounded so search never starves the UI and +/// small session lists do not pay for idle threads. +fn search_workers(candidates: usize) -> usize { + let cores = std::thread::available_parallelism().map_or(2, |n| n.get()); + (cores / 2).clamp(1, 8).min(candidates.div_ceil(16).max(1)) +} + +/// Rank one shard of candidates, cheapest evidence first. +fn search_shard( + candidates: Vec<(Candidate, SearchRank)>, + query: &str, + tokens: &[String], + idf: &[u32], + cancel: &AtomicBool, + sender: mpsc::Sender<(String, SearchRank)>, +) { + // Finish the in-memory fuzzy pass before opening any large + // transcripts. Neither DP matching nor disk scans belong + // on the keystroke/render thread. + let mut scores = WordScores::default(); + let mut fuzzy_candidates = Vec::new(); + // Metadata matches whose words may have come from a user + // prompt rather than a sampled tool dump or reasoning. + let mut classify = Vec::new(); + let phrase = PhraseQuery::new(query); + // Sessions whose later user prompts may contain the query + // as a retyped phrase, checked after cheaper evidence. + let mut phrase_jobs = Vec::new(); + for (mut candidate, priority) in candidates { + if cancel.load(Ordering::Relaxed) { + return; + } + if phrase.is_some() && !candidate.paths.is_empty() { + phrase_jobs.push((candidate.id.clone(), candidate.paths.clone(), priority)); + } + if priority.opening_prompt > 0 { + let mut rank = + fuzzy_metadata_rank_with_scores(&candidate.search_index, tokens, &mut scores); + rank.opening_prompt = priority.opening_prompt; + rank.intent_terms = priority.intent_terms; + rank.source_weight = priority.source_weight; + if sender.send((candidate.id.clone(), rank)).is_err() { + return; + } + } + candidate + .missing_tokens + .retain(|token| !candidate.entry.search_index.contains(token.as_str())); + if candidate.missing_tokens.is_empty() { + let mut rank = literal_metadata_rank(&candidate.search_index, tokens) + .with_exact_field(candidate.exact_field) + .with_source_weight(priority.source_weight); + rank.opening_prompt = priority.opening_prompt; + rank.intent_terms = priority.intent_terms; + if sender.send((candidate.id.clone(), rank)).is_err() { + return; + } + if !candidate.paths.is_empty() + && candidate + .prior + .iter() + .any(|source| *source != Some(Source::User)) + { + classify.push((candidate, rank)); + } + continue; + } + fuzzy_candidates.push((candidate, priority)); + } + let mut transcripts = Vec::new(); + for (candidate, priority) in fuzzy_candidates { + if cancel.load(Ordering::Relaxed) { + return; + } + let mut fuzzy_missing = candidate.missing_tokens.clone(); + let mut rank = + fuzzy_metadata_rank_with_scores(&candidate.search_index, tokens, &mut scores) + .with_exact_field(candidate.exact_field); + rank.opening_prompt = priority.opening_prompt; + rank.intent_terms = priority.intent_terms; + rank.source_weight = priority.source_weight; + fuzzy_missing.retain(|token| !scores.matches_text(token, &candidate.search_index)); + if fuzzy_missing.is_empty() && sender.send((candidate.id.clone(), rank)).is_err() { + return; + } + if !candidate.paths.is_empty() { + transcripts.push((candidate, fuzzy_missing, rank)); + } + } + // Fuzzy scoring adds evidence unavailable to the cheap + // pass. Use it to schedule disk reads, too. + transcripts.sort_by_key(|(_, _, rank)| std::cmp::Reverse(*rank)); + let mut cold_transcripts = Vec::new(); + let mut warm_fuzzy = Vec::new(); + // Reuse resident token evidence before waiting for a typing + // pause. Never narrow fuzzy candidates using literal results. + for (candidate, fuzzy_missing, rank) in transcripts { + if cancel.load(Ordering::Relaxed) { + return; + } + match cached_paths_scan_with_scores( + &candidate.paths, + tokens, + &candidate.prior, + cancel, + false, + &mut scores, + ) { + Some(Some(evidence)) => { + if sender + .send(( + candidate.id.clone(), + SearchRank { + literal: true, + source_weight: source::weighted_source_weight(&evidence.sources, idf), + ..rank + }, + )) + .is_err() + { + return; + } + } + Some(None) if fuzzy_missing.is_empty() => {} + Some(None) => warm_fuzzy.push((candidate, fuzzy_missing, rank)), + None => cold_transcripts.push((candidate, fuzzy_missing, rank)), + } + } + for (candidate, missing, rank) in warm_fuzzy { + let prior = fuzzy_prior(tokens, &candidate.prior, &missing); + match cached_paths_scan_with_scores( + &candidate.paths, + tokens, + &prior, + cancel, + true, + &mut scores, + ) { + Some(Some(evidence)) => { + if sender + .send(( + candidate.id.clone(), + SearchRank { + score: rank.score + evidence.score, + source_weight: source::weighted_source_weight(&evidence.sources, idf), + ..rank + }, + )) + .is_err() + { + return; + } + } + Some(None) => {} + None => cold_transcripts.push((candidate, missing, rank)), + } + } + let mut fuzzy_transcripts = Vec::new(); + for (candidate, fuzzy_missing, rank) in cold_transcripts { + if cancel.load(Ordering::Relaxed) { + return; + } + if let Some(evidence) = scan_paths_with_scores( + &candidate.paths, + tokens, + &candidate.prior, + cancel, + ScanMode::Literal, + &mut scores, + ) { + if sender + .send(( + candidate.id.clone(), + SearchRank { + literal: true, + source_weight: source::weighted_source_weight(&evidence.sources, idf), + ..rank + }, + )) + .is_err() + { + return; + } + } else if !fuzzy_missing.is_empty() { + fuzzy_transcripts.push((candidate, fuzzy_missing, rank)); + } + } + // Read where metadata matches came from before slower + // typo-tolerant scans, so a session whose words the user + // typed rises above one where they only appear in tool output. + for (candidate, rank) in classify { + if cancel.load(Ordering::Relaxed) { + return; + } + if let Some(evidence) = scan_paths_with_scores( + &candidate.paths, + tokens, + &candidate.prior, + cancel, + ScanMode::Classify, + &mut scores, + ) { + let weight = source::weighted_source_weight(&evidence.sources, idf); + if weight > rank.source_weight + && sender + .send((candidate.id.clone(), rank.with_source_weight(weight))) + .is_err() + { + return; + } + } + } + // Exact transcript hits must not wait behind typo-tolerant + // scans of unrelated, potentially very large sessions. + for (candidate, missing, rank) in fuzzy_transcripts { + if cancel.load(Ordering::Relaxed) { + return; + } + let prior = fuzzy_prior(tokens, &candidate.prior, &missing); + if let Some(evidence) = scan_paths_with_scores( + &candidate.paths, + tokens, + &prior, + cancel, + ScanMode::Fuzzy, + &mut scores, + ) { + let rank = SearchRank { + score: rank.score + evidence.score, + source_weight: source::weighted_source_weight(&evidence.sources, idf), + ..rank + }; + if sender.send((candidate.id.clone(), rank)).is_err() { + return; + } + } + } + // A follow-up prompt the user retyped outranks scattered + // word hits, the same as a retyped opening prompt. + if let Some(phrase) = &phrase { + for (id, paths, priority) in phrase_jobs { + if cancel.load(Ordering::Relaxed) { + return; + } + let matched = user_prompt_phrase_rank(&paths, phrase, cancel); + if matched > priority.opening_prompt + && sender + .send(( + id, + SearchRank { + opening_prompt: matched, + source_weight: priority.source_weight.max( + source::weighted_source_weight( + &vec![Some(Source::User); tokens.len()], + idf, + ), + ), + ..priority + }, + )) + .is_err() + { + return; + } + } + } +} + +/// Sessions decoded ahead of the first query, most recent first. Bounded so +/// prefetch stays well inside the text cache's entry and byte budgets. +const PREFETCH_SESSIONS: usize = 96; + +/// Start decoding the most recent transcripts as soon as the picker has its +/// session list, so typing begins against a warm cache. +pub(super) fn prefetch_recent<'a>(sessions: impl Iterator) { + if cfg!(test) { + return; + } + let mut recent: Vec<&SessionInfo> = sessions.collect(); + recent.sort_by(|a, b| b.last_message_time.cmp(&a.last_message_time)); + let paths = recent + .into_iter() + .take(PREFETCH_SESSIONS) + .flat_map(|session| transcript_paths(session).into_iter().take(1)) + .collect(); + warmup::prefetch(paths); +} + fn transcript_paths(session: &SessionInfo) -> Vec { match &session.resume_target { ResumeTarget::JcodeSession { session_id } => { @@ -575,51 +1058,116 @@ fn transcript_paths(session: &SessionInfo) -> Vec { } } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum ScanMode { + /// Stop once every token is found. + Literal, + /// Typo-tolerant matching. Stop once every token is found. + Fuzzy, + /// Literal matching that keeps reading until every token was seen in a + /// user prompt, so the rank reflects the strongest origin of each word. + Classify, +} + struct Scan<'a> { tokens: &'a [String], found: Vec, + /// Strongest origin each token was seen in so far. + sources: Vec>, cancel: &'a AtomicBool, fuzzy: bool, + classify: bool, score: i64, scores: &'a mut WordScores, } -impl Scan<'_> { +impl<'a> Scan<'a> { + /// `prior` marks tokens already satisfied by metadata and their origin. + fn new( + tokens: &'a [String], + prior: &[Option], + cancel: &'a AtomicBool, + mode: ScanMode, + scores: &'a mut WordScores, + ) -> Self { + let sources: Vec<_> = (0..tokens.len()) + .map(|index| prior.get(index).copied().flatten()) + .collect(); + Self { + tokens, + found: sources.iter().map(Option::is_some).collect(), + sources, + cancel, + fuzzy: mode == ScanMode::Fuzzy, + classify: mode == ScanMode::Classify, + score: 0, + scores, + } + } + fn complete(&self) -> bool { + if self.classify { + self.sources + .iter() + .all(|source| *source == Some(Source::User)) + } else { + self.found.iter().all(|found| *found) + } + } + + fn all_found(&self) -> bool { self.found.iter().all(|found| *found) } - fn text(&mut self, text: &str) -> Result<(), E> { - if self.cancel.load(Ordering::Relaxed) { - return Err(E::custom("search cancelled")); - } - self.normalized_text(&text.to_lowercase()) + fn evidence(&self) -> Option { + self.all_found().then(|| Evidence { + score: self.score, + sources: self.sources.clone(), + }) } - fn normalized_text(&mut self, lower: &str) -> Result<(), E> { + fn normalized_text( + &mut self, + lower: &str, + source: Source, + ) -> Result<(), E> { if self.cancel.load(Ordering::Relaxed) { return Err(E::custom("search cancelled")); } - for (token, found) in self.tokens.iter().zip(&mut self.found) { - if !*found { - if lower.contains(token) { - *found = true; - if self.fuzzy { - self.score += - i64::from(jcode_fuzzy::fuzzy_score(token, token).unwrap_or(0)); - } - } else if self.fuzzy - && token.chars().count() >= 4 - && token.chars().all(char::is_alphabetic) + for ((token, found), best) in self + .tokens + .iter() + .zip(&mut self.found) + .zip(&mut self.sources) + { + if *found { + // Only a stronger origin can change the rank. Skipping weaker + // segments keeps tool dumps cheap once a token is found. + if best.is_none_or(|best| source.weight() > best.weight()) + && lower.contains(token.as_str()) { - // Score the words that actually matched, not unrelated - // metadata. Bound DP work for URLs and minified output. - // Transcript evidence prefers the closest word length - // over an incidental longer prefix or suffix match. - if let Some(score) = self.scores.best_score(token, lower, true) { - *found = true; - self.score += i64::from(score); - } + *best = Some(source); + } + continue; + } + if lower.contains(token.as_str()) { + *found = true; + *best = Some(source); + if self.fuzzy { + self.score += i64::from(jcode_fuzzy::fuzzy_score(token, token).unwrap_or(0)); + } + } else if self.fuzzy + && token.chars().count() >= 4 + && token.chars().all(char::is_alphabetic) + { + // Score the words that actually matched, not unrelated + // metadata. Bound DP work for URLs and minified output. + // Transcript evidence prefers the closest word length + // over an incidental longer prefix or suffix match. + if let Some(score) = self.scores.best_score(token, lower, true) { + *found = true; + *best = Some(source); + self.score += i64::from(score); } } } @@ -632,52 +1180,43 @@ impl Scan<'_> { // Decode one JSON string at a time, including escaped Unicode and newlines. // Do not materialize a many-MB session or mistake JSON field names for prose. -impl<'de> DeserializeSeed<'de> for &mut Scan<'_> { - type Value = (); - fn deserialize>(self, deserializer: D) -> Result<(), D::Error> { +impl source::Sink for Scan<'_> { + fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str> { if self.cancel.load(Ordering::Relaxed) { - return Err(serde::de::Error::custom("search cancelled")); + return Err("search cancelled"); } - deserializer.deserialize_any(self) + self.normalized_text::(&text.to_lowercase(), source) + .map_err(|_| { + if self.cancel.load(Ordering::Relaxed) { + "search cancelled" + } else { + "search complete" + } + }) } } -impl<'de> Visitor<'de> for &mut Scan<'_> { - type Value = (); - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.write_str("transcript JSON") - } - fn visit_str(self, value: &str) -> Result<(), E> { - self.text(value) - } - fn visit_string(self, value: String) -> Result<(), E> { - self.text(&value) - } - fn visit_bool(self, _: bool) -> Result<(), E> { - Ok(()) - } - fn visit_i64(self, _: i64) -> Result<(), E> { - Ok(()) - } - fn visit_u64(self, _: u64) -> Result<(), E> { - Ok(()) - } - fn visit_f64(self, _: f64) -> Result<(), E> { - Ok(()) - } - fn visit_unit(self) -> Result<(), E> { - Ok(()) - } - fn visit_seq>(self, mut seq: A) -> Result<(), A::Error> { - while seq.next_element_seed(&mut *self)?.is_some() {} - Ok(()) - } - fn visit_map>(self, mut map: A) -> Result<(), A::Error> { - while map.next_key::()?.is_some() { - map.next_value_seed(&mut *self)?; - } - Ok(()) - } +/// Tokens outside `missing` were already satisfied by metadata. +fn fuzzy_prior( + tokens: &[String], + prior: &[Option], + missing: &[String], +) -> Vec> { + tokens + .iter() + .enumerate() + .map(|(index, token)| { + if missing.contains(token) { + None + } else { + prior + .get(index) + .copied() + .flatten() + .or(Some(Source::ToolOutput)) + } + }) + .collect() } struct CancellableReader<'a> { @@ -703,6 +1242,7 @@ fn paths_match_scored(paths: &[PathBuf], tokens: &[String], cancel: &AtomicBool) scan_paths(paths, tokens, cancel, true) } +#[cfg(test)] fn paths_match_mode( paths: &[PathBuf], tokens: &[String], @@ -712,48 +1252,70 @@ fn paths_match_mode( scan_paths(paths, tokens, cancel, fuzzy).is_some() } +#[cfg(test)] +fn mode(fuzzy: bool) -> ScanMode { + if fuzzy { + ScanMode::Fuzzy + } else { + ScanMode::Literal + } +} + +#[cfg(test)] fn scan_paths( paths: &[PathBuf], tokens: &[String], cancel: &AtomicBool, fuzzy: bool, ) -> Option { - scan_paths_with_scores(paths, tokens, cancel, fuzzy, &mut WordScores::default()) + scan_paths_with_scores( + paths, + tokens, + &[], + cancel, + mode(fuzzy), + &mut WordScores::default(), + ) + .map(|evidence| evidence.score) } fn scan_paths_with_scores( paths: &[PathBuf], tokens: &[String], + prior: &[Option], cancel: &AtomicBool, - fuzzy: bool, + mode: ScanMode, scores: &mut WordScores, -) -> Option { - let mut scan = Scan { - tokens, - found: vec![false; tokens.len()], - cancel, - fuzzy, - score: 0, - scores, - }; +) -> Option { + let mut scan = Scan::new(tokens, prior, cancel, mode, scores); for path in paths { if cancel.load(Ordering::Relaxed) { return None; } - scan_file(path, &mut scan); if scan.complete() { - return Some(scan.score); + break; } + scan_file(path, &mut scan); } - None + if cancel.load(Ordering::Relaxed) { + return None; + } + scan.evidence() } -fn apply_token_matches(scan: &mut Scan<'_>, matches: Vec>) { - for (found, score) in scan.found.iter_mut().zip(matches) { - if !*found && let Some(score) = score { +fn apply_token_matches(scan: &mut Scan<'_>, matches: Vec>) { + for ((found, best), hit) in scan + .found + .iter_mut() + .zip(scan.sources.iter_mut()) + .zip(matches) + { + let Some(hit) = hit else { continue }; + if !*found { *found = true; - scan.score += score; + scan.score += hit.score; } + *best = source::stronger(*best, Some(hit.source)); } } @@ -767,24 +1329,31 @@ fn cached_paths_scan( cancel: &AtomicBool, fuzzy: bool, ) -> Option> { - cached_paths_scan_with_scores(paths, tokens, cancel, fuzzy, &mut WordScores::default()) + cached_paths_scan_with_scores( + paths, + tokens, + &[], + cancel, + fuzzy, + &mut WordScores::default(), + ) + .map(|evidence| evidence.map(|evidence| evidence.score)) } fn cached_paths_scan_with_scores( paths: &[PathBuf], tokens: &[String], + prior: &[Option], cancel: &AtomicBool, fuzzy: bool, scores: &mut WordScores, -) -> Option> { - let mut scan = Scan { - tokens, - found: vec![false; tokens.len()], - cancel, - fuzzy, - score: 0, - scores, +) -> Option> { + let mode = if fuzzy { + ScanMode::Fuzzy + } else { + ScanMode::Literal }; + let mut scan = Scan::new(tokens, prior, cancel, mode, scores); let mut all_resident = true; for path in paths { if cancel.load(Ordering::Relaxed) { @@ -795,7 +1364,7 @@ fn cached_paths_scan_with_scores( { apply_token_matches(&mut scan, matches); if scan.complete() && (!fuzzy || all_resident) { - return Some(Some(scan.score)); + return Some(scan.evidence()); } } else if !std::fs::metadata(path) .is_err_and(|error| error.kind() == std::io::ErrorKind::NotFound) @@ -808,7 +1377,7 @@ fn cached_paths_scan_with_scores( if cancel.load(Ordering::Relaxed) || !all_resident { None } else { - Some(scan.complete().then_some(scan.score)) + Some(scan.evidence()) } } @@ -866,8 +1435,8 @@ fn scan_file_streaming(path: &Path, scan: &mut Scan<'_>) { } Ok(_) => { record += 1; - if let Err(error) = - (&mut *scan).deserialize(&mut serde_json::Deserializer::from_slice(&line)) + if let Err(error) = source::Walk::new(&mut *scan) + .deserialize(&mut serde_json::Deserializer::from_slice(&line)) { log_scan_error(path, Some(record), scan, &error); } @@ -882,7 +1451,7 @@ fn scan_file_streaming(path: &Path, scan: &mut Scan<'_>) { let mut deserializer = serde_json::Deserializer::from_reader(reader); // The stream can be one snapshot or multiple JSONL records. loop { - match (&mut *scan).deserialize(&mut deserializer) { + match source::Walk::new(&mut *scan).deserialize(&mut deserializer) { Ok(()) => {} Err(error) => { // EOF is normal between values or in a live, partially written record. diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs new file mode 100644 index 00000000000..b1cf4d8cc6c --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs @@ -0,0 +1,662 @@ +//! Classify transcript strings by origin while streaming JSON. +//! +//! A query word the user typed is much stronger evidence of the conversation +//! they want than the same word inside a tool dump or a reasoning trace. The +//! walker tracks the enclosing message role and content block type so every +//! decoded string carries the source it came from, across jcode, Claude Code, +//! Codex, and similar role/type tagged transcript formats. +use serde::de::{DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor}; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub(crate) enum Source { + ToolOutput, + Thinking, + ToolInput, + /// Metadata, system reminders, and untagged strings. + #[default] + Other, + /// Working directory and project name: a deliberate way to find a + /// session, but weaker than words from the conversation itself. + Project, + Assistant, + /// Tool-call intent labels: the agent's one-line statement of what it is + /// doing, usually a close paraphrase of the user's request. + Intent, + User, +} + +impl Source { + /// Relative strength of a match found in this source. + pub(crate) fn weight(self) -> u32 { + match self { + Source::User => 8, + Source::Intent => 6, + Source::Assistant => 4, + Source::Project => 3, + Source::Other | Source::ToolInput => 2, + Source::Thinking | Source::ToolOutput => 1, + } + } + + fn is_low(self) -> bool { + matches!( + self, + Source::ToolInput | Source::ToolOutput | Source::Thinking + ) + } +} + +/// Weight of the best source per token, summed across query tokens. +#[cfg(test)] +pub(crate) fn source_weight(best: &[Option]) -> u32 { + best.iter().flatten().map(|source| source.weight()).sum() +} + +/// Like [`source_weight`], but scaled by how rare each query word is, so a +/// distinctive word counts for much more than one found in most sessions. +pub(crate) fn weighted_source_weight(best: &[Option], idf: &[u32]) -> u32 { + best.iter() + .enumerate() + .filter_map(|(index, source)| { + source.map(|source| source.weight() * idf.get(index).copied().unwrap_or(UNIFORM_IDF)) + }) + .sum() +} + +/// Weight used for every word before document frequencies are known. +pub(crate) const UNIFORM_IDF: u32 = 100; + +/// Per-token rarity weights from how many sessions mention each token. +/// Words found nearly everywhere ("do", "that") keep a small floor so they +/// still break ties, while a word in a handful of sessions dominates. +pub(crate) fn idf_weights<'a>( + tokens: &[String], + indexes: impl Iterator, +) -> Vec { + let mut df = vec![0usize; tokens.len()]; + let mut total = 0usize; + for index in indexes { + total += 1; + for (count, token) in df.iter_mut().zip(tokens) { + if index.contains(token.as_str()) { + *count += 1; + } + } + } + df.into_iter() + .map(|count| { + let idf = ((total as f64 + 1.0) / (count as f64 + 1.0)).ln(); + 10 + (idf * 100.0).round() as u32 + }) + .collect() +} + +/// Keep the stronger of two per-token observations. +pub(crate) fn stronger(a: Option, b: Option) -> Option { + match (a, b) { + (Some(a), Some(b)) => Some(if b.weight() > a.weight() { b } else { a }), + (a, b) => a.or(b), + } +} + +/// Starts a section of the in-memory search index. The marker is a separate +/// whitespace-delimited word, so it never joins a query word, and a control +/// character, so a typed query never matches it. +const MARK: char = '\u{1f}'; + +impl Source { + /// Ordered weakest to strongest, matching the tag digits. + const ALL: [Source; 8] = [ + Source::ToolOutput, + Source::Thinking, + Source::ToolInput, + Source::Other, + Source::Project, + Source::Assistant, + Source::Intent, + Source::User, + ]; + + fn tag(self) -> char { + match self { + Source::ToolOutput => '0', + Source::Thinking => '1', + Source::ToolInput => '2', + Source::Other => '3', + Source::Project => '4', + Source::Assistant => '5', + Source::Intent => '6', + Source::User => '7', + } + } + + fn from_tag(tag: char) -> Option { + Self::ALL.into_iter().find(|source| source.tag() == tag) + } + + /// Index of this source in [`SourceBuckets`]. + fn slot(self) -> usize { + self.tag() as usize - '0' as usize + } +} + +/// Marks where the following search index text came from. +pub(crate) fn push_mark(index: &mut String, source: Source) { + index.push(' '); + index.push(MARK); + index.push(source.tag()); + index.push(' '); +} + +/// Origin of the index text at byte `pos`. Unmarked text, including indexes +/// built before sections existed, is treated as untagged metadata. +fn source_at(index: &str, pos: usize) -> Source { + index[..pos] + .rfind(MARK) + .and_then(|at| index[at + MARK.len_utf8()..].chars().next()) + .and_then(Source::from_tag) + .unwrap_or(Source::Other) +} + +/// Bounds per-token work on the keystroke path for very common words. +const MAX_CLASSIFIED_MATCHES: usize = 64; + +/// Best origin among a token's occurrences in a sectioned search index. +fn best_index_source(index: &str, token: &str) -> Option { + let mut best = None; + for (pos, _) in index.match_indices(token).take(MAX_CLASSIFIED_MATCHES) { + best = stronger(best, Some(source_at(index, pos))); + if best == Some(Source::User) { + break; + } + } + best +} + +/// Cheap per-token estimate from in-memory metadata, never transcript IO. +/// Title, saved label, and opening prompt are user intent. Other index hits +/// use the section they were indexed under, and a transcript classification +/// pass can still upgrade them. +pub(crate) fn metadata_sources( + intent_fields: &[&str], + index: &str, + tokens: &[String], +) -> Vec> { + tokens + .iter() + .map(|token| { + if intent_fields + .iter() + .any(|field| field.contains(token.as_str())) + { + Some(Source::User) + } else { + best_index_source(index, token) + } + }) + .collect() +} + +/// Bounded per-source transcript samples. Sampling each source separately +/// keeps a flood of tool output from evicting the user's prompts. +#[derive(Clone, Debug, Default)] +pub(crate) struct SourceBuckets { + text: [String; 8], +} + +impl SourceBuckets { + /// Byte budget for one source, out of `total` for all of them. User prompts + /// and replies get most of it since they are the strongest evidence. + fn budget(source: Source, total: usize) -> usize { + match source { + Source::User => total * 3 / 8, + Source::Assistant => total / 4, + _ => total / 16, + } + } + + pub(crate) fn push(&mut self, source: Source, text: &str, total: usize) { + crate::tui::session_picker::loading::push_sampled_search_text( + &mut self.text[source.slot()], + text, + Self::budget(source, total), + ); + } + + pub(crate) fn merge(&mut self, other: &Self, total: usize) { + for source in Source::ALL { + self.push(source, &other.text[source.slot()], total); + } + } + + pub(crate) fn is_empty(&self) -> bool { + self.text.iter().all(String::is_empty) + } + + /// Appends every non-empty sample under its section marker, strongest first. + pub(crate) fn render(&self, index: &mut String) { + for source in Source::ALL.into_iter().rev() { + let text = self.text[source.slot()].trim(); + if !text.is_empty() { + push_mark(index, source); + index.push_str(text); + } + } + } +} + +/// Collects decoded transcript strings into per-source samples. +pub(crate) struct BucketSink<'a> { + pub(crate) buckets: &'a mut SourceBuckets, + pub(crate) total: usize, +} + +impl Sink for BucketSink<'_> { + fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str> { + let text = text.trim(); + if !text.is_empty() { + self.buckets.push(source, text, self.total); + } + Ok(()) + } +} + +/// Source of a preview or stored message role, such as `user` or `tool`. +pub(crate) fn message_role_source(role: &str) -> Source { + role_source(role).unwrap_or(Source::Other) +} + +pub(crate) trait Sink { + /// Receives each decoded string. An error stops traversal. + fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str>; +} + +pub(crate) struct Walk<'a, S> { + pub(crate) sink: &'a mut S, + pub(crate) source: Source, +} + +impl<'a, S: Sink> Walk<'a, S> { + pub(crate) fn new(sink: &'a mut S) -> Self { + Self { + sink, + source: Source::Other, + } + } + + fn child(&mut self, source: Source) -> Walk<'_, S> { + Walk { + sink: &mut *self.sink, + source, + } + } +} + +impl<'de, S: Sink> DeserializeSeed<'de> for Walk<'_, S> { + type Value = (); + fn deserialize>(self, de: D) -> Result<(), D::Error> { + de.deserialize_any(self) + } +} + +impl<'de, S: Sink> Visitor<'de> for Walk<'_, S> { + type Value = (); + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("transcript JSON") + } + fn visit_str(self, value: &str) -> Result<(), E> { + let source = if value.trim_start().starts_with("") { + Source::Other + } else { + self.source + }; + self.sink.text(value, source).map_err(E::custom) + } + fn visit_bool(self, _: bool) -> Result<(), E> { + Ok(()) + } + fn visit_i64(self, _: i64) -> Result<(), E> { + Ok(()) + } + fn visit_u64(self, _: u64) -> Result<(), E> { + Ok(()) + } + fn visit_f64(self, _: f64) -> Result<(), E> { + Ok(()) + } + fn visit_unit(self) -> Result<(), E> { + Ok(()) + } + fn visit_seq>(mut self, mut seq: A) -> Result<(), A::Error> { + let source = self.source; + while seq.next_element_seed(self.child(source))?.is_some() {} + Ok(()) + } + fn visit_map>(mut self, mut map: A) -> Result<(), A::Error> { + let inherited = self.source; + let mut tagged = inherited; + while let Some(key) = map.next_key_seed(KeySeed)? { + let tag = match key { + Key::Role => map.next_value_seed(TagSeed(role_source))?, + Key::Type => map.next_value_seed(TagSeed(type_source))?, + Key::Force(source) => { + map.next_value_seed(self.child(source))?; + continue; + } + Key::Body => { + map.next_value_seed(self.child(tagged))?; + continue; + } + Key::Plain => { + // IDs, paths, and timestamps beside a user prompt are not + // something the user typed. Never raise them above Other. + let source = if tagged.weight() > Source::Other.weight() { + Source::Other + } else { + tagged + }; + map.next_value_seed(self.child(source))?; + continue; + } + }; + // Tags inside tool payloads or reasoning (for example a tool input + // containing `"role": "user"`) must not promote that content. + if let Some(tag) = tag + && !(tagged.is_low() && tag.weight() > tagged.weight()) + { + tagged = tag; + } + } + Ok(()) + } +} + +fn role_source(role: &str) -> Option { + Some(match role.to_ascii_lowercase().as_str() { + "user" | "human" => Source::User, + "assistant" | "model" | "ai" => Source::Assistant, + "tool" | "function" | "toolresult" | "tool_result" => Source::ToolOutput, + "system" | "developer" => Source::Other, + _ => return None, + }) +} + +fn type_source(kind: &str) -> Option { + let kind = kind.to_ascii_lowercase(); + Some(match kind.as_str() { + "thinking" + | "redacted_thinking" + | "reasoning" + | "reasoning_trace" + | "agent_reasoning" + | "agent_reasoning_raw_content" => Source::Thinking, + "tool_use" | "server_tool_use" | "function_call" | "custom_tool_call" | "tool_call" + | "local_shell_call" | "web_search_call" => Source::ToolInput, + "user" | "user_message" => Source::User, + "assistant" | "agent_message" => Source::Assistant, + _ if kind.ends_with("_output") || kind.ends_with("_result") => Source::ToolOutput, + _ => return None, + }) +} + +enum Key { + Role, + Type, + Body, + Plain, + Force(Source), +} + +struct KeySeed; +impl<'de> DeserializeSeed<'de> for KeySeed { + type Value = Key; + fn deserialize>(self, de: D) -> Result { + de.deserialize_str(self) + } +} +impl Visitor<'_> for KeySeed { + type Value = Key; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("object key") + } + fn visit_str(self, key: &str) -> Result { + Ok(match key { + "role" => Key::Role, + "type" => Key::Type, + "content" | "text" | "message" | "parts" | "summary" | "value" | "body" | "prompt" + | "display" => Key::Body, + "thinking" | "signature" | "encrypted_content" | "reasoning" | "reasoning_content" => { + Key::Force(Source::Thinking) + } + "input" | "arguments" | "tool_input" | "params" | "parameters" => { + Key::Force(Source::ToolInput) + } + "toolUseResult" | "tool_use_result" | "output" | "tool_output" | "stdout" + | "stderr" => Key::Force(Source::ToolOutput), + // Tool-call intent labels are the agent's own one-line summary of + // what it is doing, so they rank above assistant prose and far + // above the raw arguments around them. + "intent" => Key::Force(Source::Intent), + "meta" | "metadata" => Key::Force(Source::Other), + _ => Key::Plain, + }) + } +} + +/// Reads a string tag value. Non-string values are skipped. +struct TagSeed(fn(&str) -> Option); +impl<'de> DeserializeSeed<'de> for TagSeed { + type Value = Option; + fn deserialize>(self, de: D) -> Result { + de.deserialize_any(self) + } +} +impl<'de> Visitor<'de> for TagSeed { + type Value = Option; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("tag") + } + fn visit_str(self, value: &str) -> Result { + Ok((self.0)(value)) + } + fn visit_bool(self, _: bool) -> Result { + Ok(None) + } + fn visit_i64(self, _: i64) -> Result { + Ok(None) + } + fn visit_u64(self, _: u64) -> Result { + Ok(None) + } + fn visit_f64(self, _: f64) -> Result { + Ok(None) + } + fn visit_unit(self) -> Result { + Ok(None) + } + fn visit_seq>(self, mut seq: A) -> Result { + while seq.next_element::()?.is_some() {} + Ok(None) + } + fn visit_map>(self, mut map: A) -> Result { + while map.next_entry::()?.is_some() {} + Ok(None) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Default)] + struct Collect(Vec<(String, Source)>); + impl Sink for Collect { + fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str> { + self.0.push((text.to_owned(), source)); + Ok(()) + } + } + + fn classify(json: &str) -> Vec<(String, Source)> { + let mut sink = Collect::default(); + Walk::new(&mut sink) + .deserialize(&mut serde_json::Deserializer::from_str(json)) + .unwrap(); + sink.0 + } + + fn source_of(json: &str, needle: &str) -> Source { + classify(json) + .into_iter() + .find(|(text, _)| text == needle) + .unwrap_or_else(|| panic!("{needle} not found")) + .1 + } + + #[test] + fn jcode_messages_classify_prompts_replies_tools_and_reasoning() { + let json = r#"{"meta":{"title":"TITLE"},"append_messages":[ + {"id":"m1","role":"user","content":[{"type":"text","text":"PROMPT"}]}, + {"id":"m2","role":"user","content":[{"type":"text","text":"REMINDER"}]}, + {"id":"m3","role":"assistant","content":[ + {"type":"reasoning_trace","text":"THOUGHT"}, + {"type":"text","text":"REPLY"}, + {"type":"tool_use","id":"t","name":"bash","input":{"command":"CMD","role":"user"}}]}, + {"id":"m4","role":"user","content":[{"type":"tool_result","tool_use_id":"t","content":"OUT"}]}]}"#; + assert_eq!(source_of(json, "TITLE"), Source::Other); + assert_eq!(source_of(json, "PROMPT"), Source::User); + assert_eq!(source_of(json, "REMINDER"), Source::Other); + assert_eq!(source_of(json, "THOUGHT"), Source::Thinking); + assert_eq!(source_of(json, "REPLY"), Source::Assistant); + assert_eq!(source_of(json, "CMD"), Source::ToolInput); + let with_intent = r#"{"role":"assistant","content":[ + {"type":"tool_use","id":"t","name":"bash","input":{"command":"CMD","intent":"WHY"}}]}"#; + assert_eq!(source_of(with_intent, "WHY"), Source::Intent); + assert_eq!(source_of(with_intent, "CMD"), Source::ToolInput); + assert_eq!(source_of(json, "OUT"), Source::ToolOutput); + // Identifiers beside a prompt are not something the user typed. + assert_eq!(source_of(json, "m1"), Source::Other); + } + + #[test] + fn claude_code_and_codex_records_classify() { + let claude = r#"{"cwd":"/repo","type":"user","message":{"role":"user","content":[ + {"type":"tool_result","content":"RESULT"}]},"toolUseResult":{"stdout":"STDOUT"}}"#; + assert_eq!(source_of(claude, "RESULT"), Source::ToolOutput); + assert_eq!(source_of(claude, "STDOUT"), Source::ToolOutput); + assert_eq!(source_of(claude, "/repo"), Source::Other); + let thinking = r#"{"type":"assistant","message":{"role":"assistant","content":[ + {"type":"thinking","thinking":"PONDER"},{"type":"text","text":"ANSWER"}]}}"#; + assert_eq!(source_of(thinking, "PONDER"), Source::Thinking); + assert_eq!(source_of(thinking, "ANSWER"), Source::Assistant); + let codex_prompt = r#"{"type":"response_item","payload":{"type":"message","role":"user", + "content":[{"type":"input_text","text":"ASK"}]}}"#; + assert_eq!(source_of(codex_prompt, "ASK"), Source::User); + let codex_call = r#"{"type":"response_item","payload":{"type":"function_call", + "arguments":"ARGS"}}"#; + assert_eq!(source_of(codex_call, "ARGS"), Source::ToolInput); + let codex_out = r#"{"type":"response_item","payload":{"type":"custom_tool_call_output", + "output":"OUTPUT"}}"#; + assert_eq!(source_of(codex_out, "OUTPUT"), Source::ToolOutput); + let codex_event = + r#"{"type":"event_msg","payload":{"type":"user_message","message":"SAID"}}"#; + assert_eq!(source_of(codex_event, "SAID"), Source::User); + } + + #[test] + fn weights_prefer_user_then_assistant_then_tools_and_thinking() { + assert!(Source::User.weight() > Source::Intent.weight()); + assert!(Source::Intent.weight() > Source::Assistant.weight()); + for pair in Source::ALL.windows(2) { + assert!(pair[0].weight() <= pair[1].weight()); + } + assert!(Source::Assistant.weight() > Source::ToolInput.weight()); + assert!(Source::ToolInput.weight() > Source::ToolOutput.weight()); + assert_eq!(Source::Thinking.weight(), Source::ToolOutput.weight()); + assert_eq!( + stronger(Some(Source::ToolOutput), Some(Source::User)), + Some(Source::User) + ); + assert_eq!( + stronger(None, Some(Source::Thinking)), + Some(Source::Thinking) + ); + let tokens = vec!["deploy".to_owned(), "api".to_owned(), "zzz".to_owned()]; + let sources = metadata_sources(&["deploy the thing"], "deploy api", &tokens); + assert_eq!(sources, [Some(Source::User), Some(Source::Other), None]); + assert_eq!(source_weight(&sources), 10); + } + + #[test] + fn index_header_ranks_project_above_ids_and_below_replies() { + let index = crate::tui::session_picker::loading::build_search_index( + "session_abc123", + "jcode", + "Fix picker", + Some("/home/me/jcode"), + None, + &[], + ); + let tokens: Vec = ["abc123", "jcode", "fix"].map(String::from).into(); + assert_eq!( + metadata_sources(&["fix picker"], &index, &tokens), + [ + Some(Source::Other), + Some(Source::Project), + Some(Source::User) + ] + ); + assert!(Source::Project.weight() > Source::ToolInput.weight()); + assert!(Source::Project.weight() < Source::Assistant.weight()); + } + + #[test] + fn sectioned_index_reports_strongest_section_per_token() { + let mut index = String::from("title words"); + push_mark(&mut index, Source::ToolOutput); + index.push_str("grep zebra kiwi"); + push_mark(&mut index, Source::User); + index.push_str("find the kiwi"); + push_mark(&mut index, Source::Thinking); + index.push_str("pondering mango"); + let tokens: Vec = ["zebra", "kiwi", "mango", "title"].map(String::from).into(); + assert_eq!( + metadata_sources(&[], &index, &tokens), + [ + Some(Source::ToolOutput), + Some(Source::User), + Some(Source::Thinking), + Some(Source::Other), + ] + ); + } + + #[test] + fn buckets_keep_user_prompts_when_tool_output_floods_the_budget() { + let mut buckets = SourceBuckets::default(); + let mut sink = BucketSink { + buckets: &mut buckets, + total: 1024, + }; + Walk { + sink: &mut sink, + source: Source::User, + } + .deserialize(&mut serde_json::Deserializer::from_str( + r#"[{"type":"text","text":"ORIGINAL ASK"}]"#, + )) + .unwrap(); + let flood = "x".repeat(4096); + for _ in 0..8 { + buckets.push(Source::ToolOutput, &flood, 1024); + } + let mut index = String::new(); + buckets.render(&mut index); + let tokens = vec!["original".to_owned(), "ask".to_owned()]; + assert!(index.len() < 1024); + assert_eq!( + metadata_sources(&[], &index.to_lowercase(), &tokens), + [Some(Source::User), Some(Source::User)] + ); + } +} diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs index 73845c14b13..3e8e24edc29 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs @@ -1,6 +1,7 @@ //! Process-wide cache of decoded string values, never JSON keys or syntax. use super::super::preview_result; -use serde::de::{DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor}; +use super::source::{Sink, Source, Walk}; +use serde::de::DeserializeSeed; use std::collections::HashMap; use std::io::Read; use std::path::{Path, PathBuf}; @@ -10,9 +11,12 @@ use std::sync::{ }; use std::time::SystemTime; -const MAX_FILE: usize = 2 * 1024 * 1024; -const MAX_TEXT: usize = 4 * 1024 * 1024; -const MAX_TOTAL: usize = 32 * 1024 * 1024; +// Long sessions are exactly the ones a streaming rescan hurts most: a 10 MB +// transcript scanned per keystroke and cancelled by the next one never +// finishes. Decode it once, off the query path, and keep it resident. +const MAX_FILE: usize = 16 * 1024 * 1024; +const MAX_TEXT: usize = 48 * 1024 * 1024; +const MAX_TOTAL: usize = 256 * 1024 * 1024; const MAX_ENTRIES: usize = 256; #[derive(Clone, Debug, PartialEq, Eq)] @@ -46,9 +50,31 @@ impl Fingerprint { } struct Entry { fingerprint: Fingerprint, - text: Arc>, + text: Arc, bytes: usize, used: u64, + /// Bytes of whole JSONL records reflected in `text`. A torn trailing + /// record failed to parse, so the next append decode restarts there. + records_end: Boundary, +} +/// End of the decoded JSONL prefix plus the bytes just before it. A rewrite +/// that keeps the inode and grows looks like an append by metadata alone, so +/// these bytes are re-read and compared before the prefix is reused. +#[derive(Clone, Default)] +pub(super) struct Boundary { + end: u64, + check: Vec, +} +const BOUNDARY_CHECK: usize = 256; +impl Fingerprint { + /// Same file, only grown: an append-only journal gained records. + fn is_append_of(&self, older: &Fingerprint) -> bool { + #[cfg(unix)] + let same_file = self.identity.0 == older.identity.0 && self.identity.1 == older.identity.1; + #[cfg(not(unix))] + let same_file = true; + same_file && self.len > older.len + } } #[derive(Default)] struct Cache { @@ -62,16 +88,26 @@ impl Cache { self.bytes -= entry.bytes; } } - fn get(&mut self, path: &Path, fingerprint: Option<&Fingerprint>) -> Option>> { + /// A stale entry for the same journal that has since only grown, plus + /// the byte length it covers. Only meaningful for append-only JSONL. + fn stale_prefix( + &mut self, + path: &Path, + fingerprint: &Fingerprint, + ) -> Option<(Arc, Boundary)> { + let entry = self.entries.get(path)?; + fingerprint + .is_append_of(&entry.fingerprint) + .then(|| (entry.text.clone(), entry.records_end.clone())) + } + fn get(&mut self, path: &Path, fingerprint: Option<&Fingerprint>) -> Option> { self.clock = self.clock.wrapping_add(1); - if self - .entries - .get(path) - .is_some_and(|entry| Some(&entry.fingerprint) != fingerprint) - { - self.remove(path); - } let entry = self.entries.get_mut(path)?; + // Keep a stale entry: a grown journal reuses it as a decoded prefix. + // insert() replaces it and LRU eviction bounds it otherwise. + if Some(&entry.fingerprint) != fingerprint { + return None; + } entry.used = self.clock; Some(entry.text.clone()) } @@ -79,8 +115,9 @@ impl Cache { &mut self, path: &Path, fingerprint: Fingerprint, - text: Arc>, + text: Arc, bytes: usize, + records_end: Boundary, ) { self.remove(path); while self.bytes + bytes > MAX_TOTAL || self.entries.len() >= MAX_ENTRIES { @@ -103,6 +140,7 @@ impl Cache { text, bytes, used: self.clock, + records_end, }, ); } @@ -121,7 +159,7 @@ pub(super) fn is_cached_hint(path: &Path) -> bool { } /// Validate a resident entry on the search worker without opening a transcript. -pub(super) fn cached_segments(path: &Path, cancel: &AtomicBool) -> Option>> { +pub(super) fn cached_segments(path: &Path, cancel: &AtomicBool) -> Option> { if cancel.load(Ordering::Relaxed) { return None; } @@ -131,7 +169,11 @@ pub(super) fn cached_segments(path: &Path, cancel: &AtomicBool) -> Option Option>> { +/// +/// The query's `cancel` only abandons *waiting*. Once a decode starts it runs +/// to completion and is published, so typing never discards decode work: the +/// next keystroke's worker waits for that same decode instead of restarting. +pub(super) fn segments(path: &Path, cancel: &AtomicBool) -> Option> { if cancel.load(Ordering::Relaxed) { return None; } @@ -141,11 +183,109 @@ pub(super) fn segments(path: &Path, cancel: &AtomicBool) -> Option &'static ( + Mutex>, + std::sync::Condvar, +) { + static IN_FLIGHT: OnceLock<( + Mutex>, + std::sync::Condvar, + )> = OnceLock::new(); + IN_FLIGHT.get_or_init(Default::default) +} +impl DecodeClaim { + /// Claims the path, or waits briefly for the current claimant and + /// returns None so the caller can recheck the cache and its cancel flag. + fn try_acquire(path: &Path) -> Option { + let (lock, wake) = in_flight(); + let mut claimed = preview_result(lock.lock(), "decode claim lock")?; + if claimed.insert(path.to_owned()) { + return Some(Self(path.to_owned())); + } + if let Err(error) = wake.wait_timeout(claimed, std::time::Duration::from_millis(10)) { + crate::logging::debug(&format!("Transcript decode wait poisoned: {error}")); + } + None + } +} +impl Drop for DecodeClaim { + fn drop(&mut self) { + let (lock, wake) = in_flight(); + if let Ok(mut claimed) = lock.lock() { + claimed.remove(&self.0); + } + wake.notify_all(); + } +} + +/// Whether the file still holds the bytes that ended the cached prefix. +fn boundary_matches(file: &mut std::fs::File, boundary: &Boundary) -> bool { + use std::io::Seek; + let Some(from) = boundary.end.checked_sub(boundary.check.len() as u64) else { + return false; + }; + let mut bytes = vec![0; boundary.check.len()]; + file.seek(std::io::SeekFrom::Start(from)).is_ok() + && file.read_exact(&mut bytes).is_ok() + && bytes == boundary.check +} + +fn decode_and_publish( + path: &Path, + fingerprint: Fingerprint, + cancel: &AtomicBool, +) -> Option> { if fingerprint.len > MAX_FILE as u64 { return None; } - let file = preview_result(std::fs::File::open(path), "transcript open")?; + let jsonl = path.extension().is_some_and(|ext| ext == "jsonl"); + // A live journal only appends records. Decode just the new tail and share + // the already decoded prefix instead of re-reading the whole session. + let prior = if jsonl { + preview_result(cache().lock(), "text cache lock")?.stale_prefix(path, &fingerprint) + } else { + None + }; + let mut file = preview_result(std::fs::File::open(path), "transcript open")?; + let prior = prior.filter(|(_, boundary)| boundary_matches(&mut file, boundary)); + let start = prior.as_ref().map_or(0, |(_, boundary)| boundary.end); + { + use std::io::Seek; + preview_result( + file.seek(std::io::SeekFrom::Start(start)), + "transcript seek", + )?; + } let mut input = Vec::new(); // Bounded even when a live journal grows after stat. let mut reader = super::CancellableReader { @@ -154,19 +294,68 @@ pub(super) fn segments(path: &Path, cancel: &AtomicBool) -> Option MAX_FILE || cancel.load(Ordering::Relaxed) { + if cancel.load(Ordering::Relaxed) { return None; } - let (text, bytes) = decode( - &input, - path.extension().is_some_and(|ext| ext == "jsonl"), - cancel, - )?; - if Fingerprint::read(path).as_ref() != Some(&fingerprint) || cancel.load(Ordering::Relaxed) { + let records_end = start + + if jsonl { + let whole = input + .iter() + .rposition(|byte| *byte == b'\n') + .map_or(0, |end| end + 1); + // A final record without a newline is complete when it parses, + // e.g. a finished external transcript. A torn live write is not. + let tail = &input[whole..]; + if tail.trim_ascii().is_empty() + || serde_json::from_slice::(tail).is_ok() + { + input.len() as u64 + } else { + whole as u64 + } + } else { + input.len() as u64 + }; + // Decode whole records only. A record still being written is picked up, + // exactly once, by the next append decode. + let input = &input[..(records_end - start) as usize]; + let boundary = Boundary { + end: records_end, + check: input[input.len().saturating_sub(BOUNDARY_CHECK)..].to_vec(), + }; + let (text, bytes) = match prior { + Some((prefix, _)) => { + // Only whole records were cached, so the tail starts at a record. + let (tail, _) = decode(input, true, cancel)?; + let mut text = Segments { + text: Vec::with_capacity(prefix.text.len() + tail.text.len()), + sources: Vec::with_capacity(prefix.sources.len() + tail.sources.len()), + }; + text.text.extend(prefix.text.iter().cloned()); + text.text.extend(tail.text); + text.sources.extend(prefix.sources.iter().copied()); + text.sources.extend(tail.sources); + let bytes = segments_bytes(&text); + if bytes > MAX_TEXT { + return None; + } + (text, bytes) + } + None => decode(input, jsonl, cancel)?, + }; + let current = Fingerprint::read(path); + let unchanged = current.as_ref() == Some(&fingerprint); + // A live journal that grew while we decoded still yields a valid prefix: + // publish it so the next decode reads only the new records. + let grown = jsonl + && current + .as_ref() + .is_some_and(|current| current.is_append_of(&fingerprint)); + if !unchanged && !grown { return None; } let text = Arc::new(text); @@ -175,75 +364,61 @@ pub(super) fn segments(path: &Path, cancel: &AtomicBool) -> Option, + pub(crate) sources: Vec, +} +impl Segments { + pub(crate) fn iter(&self) -> impl Iterator { + self.text + .iter() + .map(String::as_str) + .zip(self.sources.iter().copied()) + } +} +#[cfg(test)] +impl From> for Segments { + fn from(text: Vec) -> Self { + let sources = vec![Source::Other; text.len()]; + Self { text, sources } + } +} + struct Collector<'a> { - text: Vec, + text: Segments, bytes: usize, cancel: &'a AtomicBool, overflow: bool, } -impl<'de> DeserializeSeed<'de> for &mut Collector<'_> { - type Value = (); - fn deserialize>(self, de: D) -> Result<(), D::Error> { - if self.cancel.load(Ordering::Relaxed) || self.overflow { - return Err(serde::de::Error::custom("collection stopped")); - } - de.deserialize_any(self) - } -} -impl<'de> Visitor<'de> for &mut Collector<'_> { - type Value = (); - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.write_str("transcript JSON") - } - fn visit_str(self, value: &str) -> Result<(), E> { +impl Sink for Collector<'_> { + fn text(&mut self, value: &str, source: Source) -> Result<(), &'static str> { if self.cancel.load(Ordering::Relaxed) { - return Err(E::custom("cancelled")); + return Err("cancelled"); } let lower = value.to_lowercase(); - self.bytes += lower.capacity() + std::mem::size_of::(); + self.bytes += lower.capacity() + std::mem::size_of::() + 1; if self.bytes > MAX_TEXT { self.overflow = true; - return Err(E::custom("cache text limit")); - } - self.text.push(lower); - Ok(()) - } - fn visit_string(self, value: String) -> Result<(), E> { - self.visit_str(&value) - } - fn visit_bool(self, _: bool) -> Result<(), E> { - Ok(()) - } - fn visit_i64(self, _: i64) -> Result<(), E> { - Ok(()) - } - fn visit_u64(self, _: u64) -> Result<(), E> { - Ok(()) - } - fn visit_f64(self, _: f64) -> Result<(), E> { - Ok(()) - } - fn visit_unit(self) -> Result<(), E> { - Ok(()) - } - fn visit_seq>(self, mut seq: A) -> Result<(), A::Error> { - while seq.next_element_seed(&mut *self)?.is_some() {} - Ok(()) - } - fn visit_map>(self, mut map: A) -> Result<(), A::Error> { - while map.next_key::()?.is_some() { - map.next_value_seed(&mut *self)?; + return Err("cache text limit"); } + self.text.text.push(lower); + self.text.sources.push(source); Ok(()) } } -fn decode(input: &[u8], jsonl: bool, cancel: &AtomicBool) -> Option<(Vec, usize)> { +fn decode(input: &[u8], jsonl: bool, cancel: &AtomicBool) -> Option<(Segments, usize)> { let mut collector = Collector { - text: Vec::new(), + text: Segments::default(), bytes: 0, cancel, overflow: false, @@ -259,8 +434,8 @@ fn decode(input: &[u8], jsonl: bool, cancel: &AtomicBool) -> Option<(Vec std::io::BufRead::read_until(&mut remaining, b'\n', &mut line), "transcript record read", )?; - if let Err(error) = - (&mut collector).deserialize(&mut serde_json::Deserializer::from_slice(&line)) + if let Err(error) = Walk::new(&mut collector) + .deserialize(&mut serde_json::Deserializer::from_slice(&line)) { // A partial/corrupt journal record must not prevent later records from matching. if !cancel.load(Ordering::Relaxed) && !collector.overflow { @@ -275,17 +450,23 @@ fn decode(input: &[u8], jsonl: bool, cancel: &AtomicBool) -> Option<(Vec } } else { let mut de = serde_json::Deserializer::from_slice(input); - while (&mut collector).deserialize(&mut de).is_ok() {} + while Walk::new(&mut collector).deserialize(&mut de).is_ok() {} } if cancel.load(Ordering::Relaxed) || collector.overflow { return None; } - collector.text.shrink_to_fit(); - let bytes = collector.text.capacity() * std::mem::size_of::() - + collector.text.iter().map(String::capacity).sum::(); + collector.text.text.shrink_to_fit(); + collector.text.sources.shrink_to_fit(); + let bytes = segments_bytes(&collector.text); Some((collector.text, bytes)) } +fn segments_bytes(text: &Segments) -> usize { + text.text.capacity() * std::mem::size_of::() + + text.sources.capacity() + + text.text.iter().map(String::capacity).sum::() +} + #[cfg(test)] mod tests { use super::*; @@ -295,13 +476,14 @@ mod tests { let input = b"{\"KEY\": [\"FIRST\", 42, {\"ignored\":\"SECOND\"}]}\n{broken\n{\"x\":\"THIRD\"}"; assert_eq!( - decode(input, true, &cancel).unwrap().0, + decode(input, true, &cancel).unwrap().0.text, ["first", "second", "third"] ); assert_eq!( decode(br#"{"KEY":"ONE"} ["TWO", "\u0041"]"#, false, &cancel) .unwrap() - .0, + .0 + .text, ["one", "two", "a"] ); } @@ -314,13 +496,56 @@ mod tests { let first = segments(&path, &cancel).unwrap(); assert!(Arc::ptr_eq(&first, &segments(&path, &cancel).unwrap())); std::fs::write(&path, r#"{"key":"REPLACEMENT"}"#).unwrap(); - assert_eq!(&*segments(&path, &cancel).unwrap(), &["replacement"]); + assert_eq!(&segments(&path, &cancel).unwrap().text, &["replacement"]); std::fs::remove_file(&path).unwrap(); assert!(segments(&path, &cancel).is_none()); cancel.store(true, Ordering::Relaxed); assert!(segments(&path, &cancel).is_none()); } #[test] + fn journal_appends_decode_only_new_whole_records() { + use std::io::Write; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("live.journal.jsonl"); + let cancel = AtomicBool::new(false); + std::fs::write(&path, "{\"t\":\"ONE\"}\n").unwrap(); + let first = segments(&path, &cancel).unwrap(); + assert_eq!(&first.text, &["one"]); + let mut file = std::fs::OpenOptions::new() + .append(true) + .open(&path) + .unwrap(); + // A torn record is left for the next decode. + file.write_all(b"{\"t\":\"TWO\"}\n{\"t\":\"TH").unwrap(); + file.flush().unwrap(); + assert_eq!(&segments(&path, &cancel).unwrap().text, &["one", "two"]); + file.write_all(b"REE\"}\n").unwrap(); + file.flush().unwrap(); + let last = segments(&path, &cancel).unwrap(); + assert_eq!(&last.text, &["one", "two", "three"]); + // Unchanged file reuses the published entry. + assert!(Arc::ptr_eq(&last, &segments(&path, &cancel).unwrap())); + // A rewrite (not an append) decodes from scratch. + std::fs::remove_file(&path).unwrap(); + std::fs::write( + &path, + "{\"t\":\"FRESH\"}\n{\"t\":\"FILE\"}\n{\"t\":\"HERE\"}\n", + ) + .unwrap(); + assert_eq!( + &segments(&path, &cancel).unwrap().text, + &["fresh", "file", "here"] + ); + } + #[test] + fn complete_unterminated_final_record_is_kept() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("external.jsonl"); + std::fs::write(&path, "{\"t\":\"A\"}\n{\"t\":\"B\"}").unwrap(); + let cancel = AtomicBool::new(false); + assert_eq!(&segments(&path, &cancel).unwrap().text, &["a", "b"]); + } + #[test] fn large_files_request_streaming_fallback() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("large.json"); @@ -347,8 +572,8 @@ mod tests { .set_times(std::fs::FileTimes::new().set_modified(modified)) .unwrap(); std::fs::rename(&replacement, &path).unwrap(); - assert_eq!(&*first, &["first"]); - assert_eq!(&*segments(&path, &cancel).unwrap(), &["other"]); + assert_eq!(&first.text, &["first"]); + assert_eq!(&segments(&path, &cancel).unwrap().text, &["other"]); } #[test] @@ -364,15 +589,28 @@ mod tests { cache.insert( Path::new(&index.to_string()), fp.clone(), - Arc::new(vec![]), + Arc::new(Segments::default()), 1, + Boundary::default(), ); } cache.get(Path::new("0"), Some(&fp)).unwrap(); - cache.insert(Path::new("extra"), fp.clone(), Arc::new(vec![]), 1); + cache.insert( + Path::new("extra"), + fp.clone(), + Arc::new(Segments::default()), + 1, + Boundary::default(), + ); assert!(cache.entries.contains_key(Path::new("0"))); assert!(!cache.entries.contains_key(Path::new("1"))); - cache.insert(Path::new("new"), fp.clone(), Arc::new(vec![]), MAX_TOTAL); + cache.insert( + Path::new("new"), + fp.clone(), + Arc::new(Segments::default()), + MAX_TOTAL, + Boundary::default(), + ); assert_eq!(cache.entries.len(), 1); assert_eq!(cache.bytes, MAX_TOTAL); let cancel = AtomicBool::new(false); diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs index 20b6235a70b..c01fed892c1 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs @@ -1,4 +1,6 @@ //! Bounded token evidence tied to validated decoded-text generations. +use super::source::Source; +use super::text_cache::Segments; use std::collections::HashMap; use std::path::Path; use std::sync::{ @@ -10,14 +12,36 @@ const MAX_ENTRIES: usize = 4096; const MAX_BYTES: usize = 1024 * 1024; const MAX_TOKEN_BYTES: usize = 4096; const MAX_PREFIX_PROBES: usize = 64; +/// Larger match lists are not worth their memory: such tokens are common, and +/// the full scan for them stops early on the first user-prompt hit anyway. +const MAX_NARROW_SEGMENTS: usize = 2048; + +/// A token found in a transcript: its first-match fuzzy score and the +/// strongest origin it appears in. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Hit { + pub(crate) score: i64, + pub(crate) source: Source, +} +#[cfg(test)] +pub(crate) fn hit(score: i64) -> Hit { + Hit { + score, + source: Source::Other, + } +} struct Evidence { - outcome: Option, + outcome: Option, + /// Literal mode only: every segment containing the token, when bounded. + /// A longer token can only occur in these, so typing `alph` -> `alpha` + /// rescans a few segments instead of the whole transcript. + segments: Option>, used: u64, bytes: usize, } struct Generation { - text: Weak>, + text: Weak, modes: [HashMap; 2], } // Include owned keys, values, and conservative hash-table bookkeeping. Text is @@ -52,7 +76,7 @@ impl Cache { } } } - fn get(&mut self, text: &Arc>, token: &str, fuzzy: bool) -> Option> { + fn get(&mut self, text: &Arc, token: &str, fuzzy: bool) -> Option> { if token.len() > MAX_TOKEN_BYTES { return None; } @@ -83,6 +107,32 @@ impl Cache { } None } + /// The segment list of the longest cached literal prefix of `token` that + /// matched. None means no narrowing is available. + fn narrowing(&mut self, text: &Arc, token: &str) -> Option> { + let generation = self.generations.get_mut(&(Arc::as_ptr(text) as usize))?; + if !generation.text.ptr_eq(&Arc::downgrade(text)) { + return None; + } + let mode = &mut generation.modes[0]; + let ends: Vec = token + .char_indices() + .skip(1) + .take(MAX_PREFIX_PROBES) + .map(|(end, _)| end) + .collect(); + for end in ends.into_iter().rev() { + if let Some(entry) = mode.get_mut(&token[..end]) + && entry.outcome.is_some() + && let Some(segments) = &entry.segments + { + self.clock = self.clock.wrapping_add(1); + entry.used = self.clock; + return Some(segments.clone()); + } + } + None + } fn evict(&mut self, incoming_bytes: usize) { // Batch eviction amortizes the global LRU pass over many insertions. // Hits only touch timestamps, without allocating or pruning anything. @@ -115,7 +165,17 @@ impl Cache { self.prune(); } } - fn insert(&mut self, text: &Arc>, token: &str, fuzzy: bool, outcome: Option) { + fn insert(&mut self, text: &Arc, token: &str, fuzzy: bool, outcome: Option) { + self.insert_narrowed(text, token, fuzzy, outcome, None); + } + fn insert_narrowed( + &mut self, + text: &Arc, + token: &str, + fuzzy: bool, + outcome: Option, + segments: Option>, + ) { if token.len() > MAX_TOKEN_BYTES { return; } @@ -125,7 +185,9 @@ impl Cache { } self.insertions += 1; let token = token.to_owned(); - let bytes = EVIDENCE_BYTES + token.capacity(); + let bytes = EVIDENCE_BYTES + + token.capacity() + + segments.as_ref().map_or(0, |list| list.len() * 4 + 16); // Reserve generation overhead even for an existing generation: eviction // could remove its final entry before this insertion recreates it. let incoming_bytes = bytes + GENERATION_BYTES; @@ -154,6 +216,7 @@ impl Cache { token, Evidence { outcome, + segments, used: self.clock, bytes, }, @@ -175,7 +238,7 @@ pub(super) fn matches( tokens: &[String], cancel: &AtomicBool, fuzzy: bool, -) -> Option>> { +) -> Option>> { matches_with_scores( path, tokens, @@ -191,7 +254,7 @@ pub(super) fn matches_with_scores( cancel: &AtomicBool, fuzzy: bool, scores: &mut super::WordScores, -) -> Option>> { +) -> Option>> { let text = super::text_cache::segments(path, cancel)?; resolve( cache(), @@ -210,7 +273,7 @@ pub(super) fn matches_cached( tokens: &[String], cancel: &AtomicBool, fuzzy: bool, -) -> Option>> { +) -> Option>> { matches_cached_with_scores( path, tokens, @@ -228,7 +291,7 @@ pub(super) fn matches_cached_with_scores( cancel: &AtomicBool, fuzzy: bool, scores: &mut super::WordScores, -) -> Option>> { +) -> Option>> { let text = super::text_cache::cached_segments(path, cancel)?; resolve( cache(), @@ -242,12 +305,12 @@ pub(super) fn matches_cached_with_scores( fn resolve( cache: &Mutex, - text: &Arc>, + text: &Arc, tokens: &[String], cancel: &AtomicBool, fuzzy: bool, - mut scan: impl FnMut(&[String], &str, &AtomicBool, bool) -> Option>, -) -> Option>> { + mut scan: impl FnMut(&Segments, &str, &AtomicBool, bool) -> Option>, +) -> Option>> { let mut outcomes = Vec::with_capacity(tokens.len()); for token in tokens { if cancel.load(Ordering::Relaxed) { @@ -255,9 +318,30 @@ fn resolve( } let cached = super::super::preview_result(cache.lock(), "token cache lock")?.get(text, token, fuzzy); - let outcome = match cached { - Some(outcome) => outcome, - None => { + let narrowing = if cached.is_none() && !fuzzy { + super::super::preview_result(cache.lock(), "token cache lock")?.narrowing(text, token) + } else { + None + }; + let outcome = match (cached, narrowing) { + (Some(outcome), _) => outcome, + (None, Some(candidates)) => { + let (outcome, segments) = + literal_in_segments(text, token, candidates.iter().copied(), cancel)?; + super::super::preview_result(cache.lock(), "token cache lock")? + .insert_narrowed(text, token, false, outcome, segments); + outcome + } + (None, None) if !fuzzy && token.chars().count() <= 2 => { + // Short literal tokens are the prefixes of whatever is typed + // next. Record where they match so extensions scan only there. + let (outcome, segments) = + literal_in_segments(text, token, 0..text.text.len() as u32, cancel)?; + super::super::preview_result(cache.lock(), "token cache lock")? + .insert_narrowed(text, token, false, outcome, segments); + outcome + } + (None, None) => { let outcome = scan(text, token, cancel, fuzzy)?; // Publish each completed token independently, even if a later // token cancels. Incomplete negatives never reach this point. @@ -274,13 +358,54 @@ fn resolve( Some(outcomes) } } +type LiteralEvidence = (Option, Option>); + +/// Literal match over the given segments, with the same semantics as the +/// literal scanner (substring match, score 0, strongest origin), plus the +/// list of matching segments when it is small enough to keep. +fn literal_in_segments( + text: &Segments, + token: &str, + candidates: impl Iterator, + cancel: &AtomicBool, +) -> Option { + let mut best: Option = None; + let mut found = Vec::new(); + let mut bounded = true; + for (step, index) in candidates.enumerate() { + if step % 256 == 0 && cancel.load(Ordering::Relaxed) { + return None; + } + let (Some(segment), Some(source)) = ( + text.text.get(index as usize), + text.sources.get(index as usize), + ) else { + continue; + }; + if segment.contains(token) { + best = super::source::stronger(best, Some(*source)); + if bounded { + if found.len() < MAX_NARROW_SEGMENTS { + found.push(index); + } else { + bounded = false; + found = Vec::new(); + } + } + } + } + let outcome = best.map(|source| Hit { score: 0, source }); + let segments = (bounded && outcome.is_some()).then(|| Arc::from(found)); + Some((outcome, segments)) +} + #[cfg(test)] fn scan_token( - text: &[String], + text: &Segments, token: &str, cancel: &AtomicBool, fuzzy: bool, -) -> Option> { +) -> Option> { scan_token_with_scores( text, token, @@ -290,32 +415,36 @@ fn scan_token( ) } +/// Matches the streaming scanner: the first matching segment sets the score, +/// later literal occurrences can only raise the origin. Stops early once the +/// token is seen in a user prompt, the strongest origin. fn scan_token_with_scores( - text: &[String], + text: &Segments, token: &str, cancel: &AtomicBool, fuzzy: bool, scores: &mut super::WordScores, -) -> Option> { +) -> Option> { let tokens = [token.to_owned()]; - let mut scan = super::Scan { - tokens: &tokens, - found: vec![false], - cancel, - fuzzy, - score: 0, - scores, + let mode = if fuzzy { + super::ScanMode::Fuzzy + } else { + super::ScanMode::Literal }; - for segment in text { + let mut scan = super::Scan::new(&tokens, &[], cancel, mode, scores); + for (segment, source) in text.iter() { if cancel.load(Ordering::Relaxed) { return None; } - let scanned = scan.normalized_text::(segment); - // The visitor uses an error sentinel to stop JSON traversal after a hit. - if scan.complete() { - return Some(Some(scan.score)); + if scan.sources[0] == Some(Source::User) { + break; } - if let Err(error) = scanned { + let scanned = scan.normalized_text::(segment, source); + // The scanner signals completion with an error sentinel. Keep reading + // for a stronger origin, but stop on a genuine failure. + if let Err(error) = scanned + && !scan.all_found() + { if !cancel.load(Ordering::Relaxed) { crate::logging::debug(&format!("Transcript token scan stopped: {error}")); } @@ -325,7 +454,10 @@ fn scan_token_with_scores( if cancel.load(Ordering::Relaxed) { None } else { - Some(None) + Some(scan.sources[0].map(|source| Hit { + score: scan.score, + source, + })) } } @@ -339,7 +471,7 @@ mod tests { #[test] fn long_queries_bound_prefix_probes_and_fall_back_safely() { let mut cache = Cache::default(); - let text = Arc::new(Vec::new()); + let text = Arc::new(Segments::default()); cache.insert(&text, "x", false, None); assert_eq!( cache.get(&text, &"x".repeat(MAX_TOKEN_BYTES + 1), false), @@ -354,7 +486,7 @@ mod tests { #[test] fn completed_tokens_and_backspacing_reuse_positive_and_negative_evidence() { let cache = Mutex::new(Cache::default()); - let text = Arc::new(tokens(&["alpha beta"])); + let text = Arc::new(Segments::from(tokens(&["alpha beta"]))); let cancel = AtomicBool::new(false); let mut calls = 0; for query in [ @@ -374,7 +506,7 @@ mod tests { }, ) .unwrap(); - assert_eq!(result[0], Some(0)); + assert_eq!(result[0], Some(hit(0))); assert_eq!(result[1], None); } assert_eq!(calls, 3); @@ -387,7 +519,10 @@ mod tests { let cancel = AtomicBool::new(false); let query = tokens(&["alpha"]); std::fs::write(&path, "\"alpha\"").unwrap(); - assert_eq!(matches(&path, &query, &cancel, false), Some(vec![Some(0)])); + assert_eq!( + matches(&path, &query, &cancel, false), + Some(vec![Some(hit(0))]) + ); std::fs::write(&path, "\"replacement\"").unwrap(); assert_eq!(matches(&path, &query, &cancel, false), Some(vec![None])); std::fs::remove_file(&path).unwrap(); @@ -396,19 +531,19 @@ mod tests { #[test] fn fuzzy_matches_single_scan_first_segment_scores_and_separate_mode() { - let text = Arc::new(tokens(&["alphabet betta", "alpha beta"])); + let text = Arc::new(Segments::from(tokens(&["alphabet betta", "alpha beta"]))); let query = tokens(&["alpha", "beta", "unmatched", "alp"]); let cancel = AtomicBool::new(false); - let mut scan = super::super::Scan { - tokens: &query, - found: vec![false; query.len()], - cancel: &cancel, - fuzzy: true, - score: 0, - scores: &mut super::super::WordScores::default(), - }; - for segment in text.iter() { - let _ = scan.normalized_text::(segment); + let mut scores = super::super::WordScores::default(); + let mut scan = super::super::Scan::new( + &query, + &[], + &cancel, + super::super::ScanMode::Fuzzy, + &mut scores, + ); + for (segment, source) in text.iter() { + let _ = scan.normalized_text::(segment, source); } let cache = Mutex::new(Cache::default()); let result = resolve(&cache, &text, &query, &cancel, true, scan_token).unwrap(); @@ -418,20 +553,72 @@ mod tests { }), Some(result.clone()) ); - assert_eq!(result.iter().flatten().sum::(), scan.score); + assert_eq!( + result.iter().flatten().map(|hit| hit.score).sum::(), + scan.score + ); assert_eq!( result.iter().map(Option::is_some).collect::>(), scan.found ); let literal = resolve(&cache, &text, &query, &cancel, false, scan_token).unwrap(); - assert!(literal.iter().flatten().all(|score| *score == 0)); - assert!(result[0].unwrap() > 0); + assert!(literal.iter().flatten().all(|hit| hit.score == 0)); + assert!(result[0].unwrap().score > 0); + } + + #[test] + fn literal_extensions_scan_only_segments_matching_their_prefix() { + let cache = Mutex::new(Cache::default()); + let text = Arc::new(Segments { + text: tokens(&["alpine", "nothing", "alpha tool", "beta", "alpha prompt"]), + sources: vec![ + Source::Other, + Source::User, + Source::ToolOutput, + Source::User, + Source::User, + ], + }); + let cancel = AtomicBool::new(false); + let never = |_: &Segments, _: &str, _: &AtomicBool, _: bool| -> Option> { + panic!("extension must reuse its prefix's segments") + }; + let resolve_one = + |token: &str, + scan: &dyn Fn(&Segments, &str, &AtomicBool, bool) -> Option>| { + resolve(&cache, &text, &tokens(&[token]), &cancel, false, scan).unwrap()[0] + }; + let user = Some(Hit { + score: 0, + source: Source::User, + }); + assert_eq!(resolve_one("al", &never), user); + assert_eq!(resolve_one("alp", &never), user); + assert_eq!(resolve_one("alpha", &never), user); + assert_eq!( + resolve_one("alpine", &never), + Some(Hit { + score: 0, + source: Source::Other + }) + ); + assert_eq!(resolve_one("alphax", &never), None); + // Narrowed evidence agrees with a full scan. + for token in ["alp", "alpha", "alpine", "alphax", "alpha tool"] { + assert_eq!( + scan_token(&text, token, &cancel, false).unwrap(), + resolve_one(token, &never), + "{token}" + ); + } + let narrowed = cache.lock().unwrap().narrowing(&text, "alphaz").unwrap(); + assert_eq!(&*narrowed, &[2, 4]); } #[test] fn cancellation_keeps_completed_work_not_incomplete_negatives() { let cache = Mutex::new(Cache::default()); - let text = Arc::new(tokens(&["alpha"])); + let text = Arc::new(Segments::from(tokens(&["alpha"]))); let cancel = AtomicBool::new(false); let query = tokens(&["alpha", "absent", "missing"]); assert!( @@ -452,7 +639,7 @@ mod tests { .is_none() ); let mut guard = cache.lock().unwrap(); - assert_eq!(guard.get(&text, "alpha", false), Some(Some(0))); + assert_eq!(guard.get(&text, "alpha", false), Some(Some(hit(0)))); assert_eq!(guard.get(&text, "absent", false), Some(None)); assert_eq!(guard.get(&text, "missing", false), None); drop(guard); @@ -460,7 +647,7 @@ mod tests { cancel.store(false, Ordering::Relaxed); assert_eq!( resolve(&cache, &text, &query, &cancel, false, scan_token), - Some(vec![Some(0), None, None]) + Some(vec![Some(hit(0)), None, None]) ); } @@ -477,7 +664,7 @@ mod tests { None ); assert!(super::super::text_cache::cached_segments(&path, &cancel).is_none()); - let expected = Some(vec![Some(0), None]); + let expected = Some(vec![Some(hit(0)), None]); assert_eq!( matches_with_scores(&path, &query, &cancel, false, &mut scores), expected @@ -513,9 +700,10 @@ mod tests { .into_iter() .enumerate() { + let segments = Segments::from(text.clone()); let expected = query .iter() - .map(|token| scan_token(&text, token, &cancel, true).unwrap()) + .map(|token| scan_token(&segments, token, &cancel, true).unwrap()) .collect::>(); let path = dir.path().join(format!("{index}.json")); std::fs::write(&path, serde_json::to_vec(&text).unwrap()).unwrap(); @@ -533,7 +721,7 @@ mod tests { #[test] fn literal_negative_extensions_skip_scans_but_not_backspacing_or_fuzzy() { let cache = Mutex::new(Cache::default()); - let text = Arc::new(tokens(&["alphabet"])); + let text = Arc::new(Segments::from(tokens(&["alphabet"]))); let cancel = AtomicBool::new(false); assert_eq!( resolve(&cache, &text, &tokens(&["zz"]), &cancel, false, scan_token), @@ -567,14 +755,16 @@ mod tests { Some(vec![None]) ); } - assert_eq!(calls, 2); + // Short literal tokens resolve through the segment-recording scan, so + // only the fuzzy lookup reaches the injected scanner. + assert_eq!(calls, 1); let mut guard = cache.lock().unwrap(); - guard.insert(&text, "alpha", false, Some(0)); + guard.insert(&text, "alpha", false, Some(hit(0))); assert_eq!(guard.get(&text, "alphabeta", false), None); guard.insert(&text, "missing", true, None); assert_eq!(guard.get(&text, "missinglonger", true), None); assert_eq!(guard.get(&text, "missinglonger", false), None); - let other = Arc::new(tokens(&["zzmore"])); + let other = Arc::new(Segments::from(tokens(&["zzmore"]))); assert_eq!(guard.get(&other, "zzmore", false), None); guard.insert(&text, "界", false, None); assert_eq!(guard.get(&text, "界面", false), Some(None)); @@ -584,21 +774,21 @@ mod tests { #[test] fn warm_lookups_do_not_prune_unrelated_generations() { let mut cache = Cache::default(); - let stale = Arc::new(tokens(&["stale"])); + let stale = Arc::new(Segments::from(tokens(&["stale"]))); let stale_key = Arc::as_ptr(&stale) as usize; - cache.insert(&stale, "old", false, Some(0)); + cache.insert(&stale, "old", false, Some(hit(0))); drop(stale); - let live = Arc::new(tokens(&["live"])); - cache.insert(&live, "hit", false, Some(0)); + let live = Arc::new(Segments::from(tokens(&["live"]))); + cache.insert(&live, "hit", false, Some(hit(0))); let insertions = cache.insertions; for _ in 0..PRUNE_EVERY * 2 { - assert_eq!(cache.get(&live, "hit", false), Some(Some(0))); + assert_eq!(cache.get(&live, "hit", false), Some(Some(hit(0)))); assert_eq!(cache.get(&live, "unknown", true), None); } assert_eq!(cache.insertions, insertions); assert!(cache.generations.contains_key(&stale_key)); for i in 0..PRUNE_EVERY { - cache.insert(&live, &format!("new-{i}"), false, Some(0)); + cache.insert(&live, &format!("new-{i}"), false, Some(hit(0))); } assert!(!cache.generations.contains_key(&stale_key)); assert_eq!(cache.entries, PRUNE_EVERY + 1); @@ -607,30 +797,30 @@ mod tests { #[test] fn generation_maps_and_batch_lru_keep_recent_hits_at_full_capacity() { let mut cache = Cache::default(); - let texts: Vec<_> = (0..256).map(|_| Arc::new(Vec::new())).collect(); + let texts: Vec<_> = (0..256).map(|_| Arc::new(Segments::default())).collect(); for text in &texts { for token in 0..16 { - cache.insert(text, &format!("token-{token}!"), false, Some(0)); + cache.insert(text, &format!("token-{token}!"), false, Some(hit(0))); } } assert_eq!(cache.entries, MAX_ENTRIES); assert_eq!(cache.generations.len(), 256); - assert_eq!(cache.get(&texts[0], "token-0!", false), Some(Some(0))); + assert_eq!(cache.get(&texts[0], "token-0!", false), Some(Some(hit(0)))); assert_eq!(cache.get(&texts[0], "token-0!", true), None); - cache.insert(&texts[255], "new", false, Some(0)); + cache.insert(&texts[255], "new", false, Some(hit(0))); assert!(cache.entries <= MAX_ENTRIES * 7 / 8); - assert_eq!(cache.get(&texts[0], "token-0!", false), Some(Some(0))); + assert_eq!(cache.get(&texts[0], "token-0!", false), Some(Some(hit(0)))); assert_eq!(cache.get(&texts[0], "token-1!", false), None); - assert_eq!(cache.get(&texts[255], "new", false), Some(Some(0))); + assert_eq!(cache.get(&texts[255], "new", false), Some(Some(hit(0)))); assert!(cache.bytes <= MAX_BYTES); } #[test] fn weak_generations_lru_entry_and_key_byte_limits() { let mut cache = Cache::default(); - let text = Arc::new(Vec::new()); + let text = Arc::new(Segments::default()); cache.insert(&text, "old", false, None); - let distinct_generation = Arc::new(Vec::new()); + let distinct_generation = Arc::new(Segments::default()); assert_eq!(cache.get(&distinct_generation, "old", false), None); let weak = Arc::downgrade(&text); drop(text); @@ -638,12 +828,12 @@ mod tests { cache.prune(); assert_eq!(cache.entries, 0); assert!(cache.generations.is_empty()); - let text = Arc::new(Vec::new()); + let text = Arc::new(Segments::default()); for i in 0..MAX_ENTRIES { cache.insert(&text, &format!("{i}!"), false, None); } assert_eq!(cache.get(&text, "0!", false), Some(None)); - cache.insert(&text, "new", false, Some(0)); + cache.insert(&text, "new", false, Some(hit(0))); assert_eq!(cache.get(&text, "1!", false), None); assert_eq!(cache.get(&text, "0!", false), Some(None)); for i in 0..MAX_ENTRIES { diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs index f2c4be6c82d..a88a921f445 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs @@ -64,6 +64,36 @@ pub(super) fn schedule<'a>(paths: impl Iterator) { } } +/// Decode the given transcripts on one background thread, in order, so the +/// first search after opening the picker finds them already cached. Each call +/// supersedes the previous one. Query workers share decodes via the claim in +/// `text_cache`, so a file being prefetched is never decoded twice. +pub(super) fn prefetch(paths: Vec) { + static GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + use std::sync::atomic::Ordering; + let generation = GENERATION.fetch_add(1, Ordering::Relaxed) + 1; + let spawned = std::thread::Builder::new() + .name("jcode-search-prefetch".into()) + .spawn(move || { + let cancel = AtomicBool::new(false); + for path in paths { + if GENERATION.load(Ordering::Relaxed) != generation { + return; + } + if !text_cache::is_cached_hint(&path) + && text_cache::segments(&path, &cancel).is_none() + { + crate::logging::debug("Transcript prefetch deferred to streaming search"); + } + } + }); + if let Err(error) = spawned { + crate::logging::debug(&format!( + "Transcript search prefetch startup failed: {error}" + )); + } +} + #[cfg(test)] mod tests { use super::*; @@ -85,7 +115,7 @@ mod tests { std::thread::sleep(Duration::from_millis(1)); } let text = text_cache::cached_segments(&path, &AtomicBool::new(false)).unwrap(); - assert_eq!(&*text, &["prepared while typing"]); + assert_eq!(&text.text, &["prepared while typing"]); assert!(text_cache::cached_segments(&path, &query_cancel).is_none()); } diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs index de6779f9042..abc154f518c 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs @@ -69,7 +69,7 @@ fn uncached_streaming_search_does_not_wait_for_a_typing_timer() { let path = dir.path().join("oversized.jsonl"); // Too large for warmup or decoded-text caching, so this must use cold IO. let mut text = format!("{}\n", r#"{"text":"needle"}"#); - text.push_str(&" ".repeat(2 * 1024 * 1024)); + text.push_str(&" ".repeat(17 * 1024 * 1024)); std::fs::write(&path, text).unwrap(); let session = external_session("cold", &path); let mut search = TranscriptSearch::default(); @@ -130,14 +130,8 @@ fn cached_path_scores_preserve_first_path_and_cross_file_tokens() { ("absent", false), ] { let tokens = search_query_tokens(query); - let mut reference = Scan { - tokens: &tokens, - found: vec![false; tokens.len()], - cancel: &cancel, - fuzzy, - score: 0, - scores: &mut WordScores::default(), - }; + let mut scores = WordScores::default(); + let mut reference = Scan::new(&tokens, &[], &cancel, mode(fuzzy), &mut scores); for path in &paths { scan_file_streaming(path, &mut reference); if reference.complete() { @@ -146,7 +140,7 @@ fn cached_path_scores_preserve_first_path_and_cross_file_tokens() { } assert_eq!( cached_paths_scan(&paths, &tokens, &cancel, fuzzy), - Some(reference.complete().then_some(reference.score)) + Some(reference.all_found().then_some(reference.score)) ); } } @@ -184,16 +178,9 @@ fn incremental_token_sequences_match_fresh_streaming_results_and_scores() { for second in words { let tokens = vec![first.to_string(), second.to_string()]; let mut scores = WordScores::default(); - let mut reference = Scan { - tokens: &tokens, - found: vec![false; tokens.len()], - cancel: &cancel, - fuzzy, - score: 0, - scores: &mut scores, - }; + let mut reference = Scan::new(&tokens, &[], &cancel, mode(fuzzy), &mut scores); scan_file_streaming(&path, &mut reference); - let expected = reference.complete().then_some(reference.score); + let expected = reference.all_found().then_some(reference.score); assert_eq!( cached_paths_scan(std::slice::from_ref(&path), &tokens, &cancel, fuzzy), Some(expected), @@ -404,13 +391,23 @@ fn wait_for_search(picker: &mut SessionPicker) { fn candidate_priority_prefers_opening_then_intent_and_preserves_ties() { let query = "repair the broken search"; let candidate = |id: &str, intent: &str, opening: &str| Candidate { - id: id.into(), - paths: Vec::new(), - search_index: query.into(), + entry: Arc::new(Entry { + id: id.into(), + paths: Vec::new(), + search_index: query.into(), + title: String::new(), + short_name: String::new(), + save_label: None, + has_prompt: !opening.is_empty(), + opening_prompt: opening.into(), + intent_text: intent.into(), + working_dir: None, + server_name: None, + cloud: false, + }), missing_tokens: Vec::new(), exact_field: false, - opening_prompt: opening.into(), - intent_text: intent.into(), + prior: Vec::new(), }; let candidates = vec![ candidate("weak-first", "unrelated", ""), @@ -420,8 +417,9 @@ fn candidate_priority_prefers_opening_then_intent_and_preserves_ties() { ]; let ranked = prioritize_candidates( candidates, - query, + &PhraseQuery::new(query), &search_query_tokens(query), + &[], &AtomicBool::new(false), ); assert_eq!( @@ -558,7 +556,7 @@ fn picker_finds_unsampled_middle_without_loading_preview() { picker.search_query = "MIDDLEONLYPLATYPUS title".into(); picker.rebuild_items(); assert!( - picker.visible_sessions.is_empty(), + picker.search_selection.holding() && !picker.transcript_search.has_results(), "sample must not already contain the needle" ); assert!(picker.pending_preview_load.is_none()); @@ -591,7 +589,7 @@ fn changed_query_cancels_old_worker_and_ignores_its_results() { } #[test] -fn typing_starts_cold_work_on_first_poll_and_metadata_stays_immediate() { +fn typing_starts_cold_work_on_first_poll_and_metadata_is_async() { let temp = tempfile::tempdir().unwrap(); let session = external_session("one", &temp.path().join("absent.jsonl")); let mut picker = SessionPicker::new(vec![session]); @@ -602,6 +600,8 @@ fn typing_starts_cold_work_on_first_poll_and_metadata_stays_immediate() { assert!(picker.transcript_search.candidates.is_empty()); picker.search_query = "TITLE metadata".into(); picker.rebuild_items(); + assert!(picker.transcript_search.pending()); + wait_for_search(&mut picker); assert_eq!(picker.visible_sessions.len(), 1); assert!(!picker.transcript_search.pending()); } @@ -614,13 +614,11 @@ fn fuzzy_metadata_matching_runs_on_worker_not_on_keystroke() { let mut picker = SessionPicker::new(vec![session]); picker.search_query = "conifg".into(); picker.rebuild_items(); - assert!(picker.visible_sessions.is_empty()); + // The row on screen is the held pre-query list, not a match. + assert!(picker.search_selection.holding()); + assert!(!picker.transcript_search.has_results()); assert!(picker.transcript_search.receiver.is_none()); - assert_eq!(picker.transcript_search.candidates.len(), 1); - assert_eq!( - picker.transcript_search.candidates[0].missing_tokens, - ["conifg"] - ); + assert!(picker.transcript_search.candidates.is_empty()); wait_for_search(&mut picker); assert_eq!(picker.selected_session().unwrap().id, "one"); assert!(!picker.transcript_search.contains_literal("one")); @@ -676,11 +674,10 @@ fn large_history_defers_even_literal_metadata_matching() { picker.search_query = "modal cli".into(); picker.rebuild_items(); assert!(picker.transcript_search.defers_metadata()); - assert!(picker.visible_sessions.is_empty()); - assert_eq!( - picker.transcript_search.candidates[0].missing_tokens, - ["modal", "cli"] - ); + assert!(picker.search_selection.holding()); + // Candidate selection reads every index, so it is left to the worker. + assert!(picker.transcript_search.candidates.is_empty()); + assert!(picker.transcript_search.pending()); wait_for_search(&mut picker); assert_eq!(picker.selected_session().unwrap().id, "large"); assert!(picker.transcript_search.contains_literal("large")); @@ -796,7 +793,7 @@ fn native_picker_finds_middle_omitted_from_real_list_index() { let mut picker = SessionPicker::new(vec![info]); picker.search_query = "NATIVESEARCHMIDDLEPLATYPUS".into(); picker.rebuild_items(); - assert!(picker.visible_sessions.is_empty()); + assert!(picker.search_selection.holding()); wait_for_search(&mut picker); assert_eq!(picker.selected_session().unwrap().id, id); } @@ -812,6 +809,11 @@ fn progressive_results_preserve_selected_preview_scroll() { let mut picker = SessionPicker::new(vec![direct, discovered]); picker.search_query = "needle".into(); picker.rebuild_items(); + picker + .transcript_search + .ranks + .insert("direct".into(), SearchRank::default()); + picker.rebuild_items(); assert_eq!(picker.selected_session().unwrap().id, "direct"); picker.scroll_offset = 17; picker.auto_scroll_preview = false; @@ -829,6 +831,11 @@ fn picker_with_pending_better_match() -> SessionPicker { let mut picker = SessionPicker::new(vec![direct, discovered]); picker.search_query = "needle".into(); picker.rebuild_items(); + picker + .transcript_search + .ranks + .insert("direct".into(), SearchRank::default()); + picker.rebuild_items(); assert_eq!(picker.selected_session().unwrap().id, "direct"); let (sender, receiver) = mpsc::channel(); sender @@ -905,6 +912,7 @@ fn pending_search_visually_distinguishes_auto_focus_from_user_control() { picker.transcript_search.receiver = Some(receiver); let backend = ratatui::backend::TestBackend::new(100, 12); let mut terminal = ratatui::Terminal::new(backend).unwrap(); + picker.search_selection.show_busy_for_test(); let mut render = |picker: &mut SessionPicker| { terminal .draw(|frame| picker.render_session_list_impl(frame, frame.area(), true)) @@ -927,3 +935,49 @@ fn pending_search_visually_distinguishes_auto_focus_from_user_control() { assert!(!completed.contains("finding best match")); assert!(!completed.contains("searching in background")); } + +#[test] +fn typing_keeps_previous_rows_until_results_arrive_then_releases_them() { + let temp = tempfile::tempdir().unwrap(); + let mut keep = external_session("keep", &temp.path().join("absent-a.jsonl")); + keep.search_index = "needle".into(); + let mut drop_me = external_session("drop", &temp.path().join("absent-b.jsonl")); + drop_me.search_index = "other".into(); + let mut picker = SessionPicker::new(vec![keep, drop_me]); + let before = picker.visible_sessions.len(); + // A typo has no literal match yet: the list must not blank meanwhile. + picker.search_query = "nedle".into(); + picker.rebuild_items(); + assert_eq!(picker.visible_sessions.len(), before); + assert!(picker.search_selection.holding()); + // Enter must not resume a row that belongs to the previous query. + assert!(picker.queue_search_submission(KeyModifiers::NONE)); + picker.pending_search_submission = None; + wait_for_search(&mut picker); + assert!(!picker.search_selection.holding()); + assert_eq!(picker.visible_sessions.len(), 1); + assert_eq!(picker.selected_session().unwrap().id, "keep"); + // A query nothing matches ends with an empty list, not stale rows. + picker.search_query = "zzzzqqqq".into(); + picker.rebuild_items(); + assert_eq!(picker.visible_sessions.len(), 1); + wait_for_search(&mut picker); + assert!(picker.visible_sessions.is_empty()); + assert!(!picker.search_selection.holding()); +} + +#[test] +fn unchanged_sessions_share_their_index_across_keystrokes() { + let temp = tempfile::tempdir().unwrap(); + let mut session = external_session("big", &temp.path().join("absent.jsonl")); + session.search_index = format!("{} modal", "x".repeat(IMMEDIATE_METADATA_BYTES + 1)); + let mut search = TranscriptSearch::default(); + search.reset("m", std::iter::once(&session)); + let first = search.corpus[0].clone(); + search.reset("mo", std::iter::once(&session)); + assert!(Arc::ptr_eq(&first, &search.corpus[0])); + assert!(search.candidates.is_empty(), "candidates are worker work"); + session.search_index.push_str(" refreshed"); + search.reset("mod", std::iter::once(&session)); + assert!(!Arc::ptr_eq(&first, &search.corpus[0])); +} diff --git a/crates/jcode-tui/src/tui/session_picker/typing_bench.rs b/crates/jcode-tui/src/tui/session_picker/typing_bench.rs new file mode 100644 index 00000000000..20da8bea5a1 --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/typing_bench.rs @@ -0,0 +1,156 @@ +//! Manual typing-experience benchmark for resume search: what one keystroke +//! costs on the UI thread, and how many frames show an emptied list while the +//! background search catches up. Run alone with --ignored --nocapture. +use super::*; +use ratatui::{Terminal, backend::TestBackend}; +use serde_json::json; +use std::time::{Duration, Instant}; + +const WORDS: &str = "deployment throughput synchronization performance config "; + +/// A history large enough that metadata matching is deferred to the worker, +/// like a real multi-megabyte session index. +pub(super) fn large_history(count: usize, turns: usize) -> SessionPicker { + let sessions: Vec<_> = (0..count) + .map(|id| { + let mut session = performance_tests::session(id, turns); + let mut index = format!("performance session {id} "); + while index.len() < 14 * 1024 { + index.push_str(WORDS); + } + session.search_index = index; + session.first_user_prompt = Some(format!("prompt {id}")); + session + }) + .collect(); + let mut picker = SessionPicker::new(sessions); + for item in &picker.all_sessions { + picker + .preview_extras + .insert(item.id.clone(), PreviewExtras::default()); + } + picker.set_live_presence_for_test(Vec::new()); + picker +} + +pub(super) fn screen_text(terminal: &Terminal) -> String { + let buffer = terminal.backend().buffer(); + let mut text = String::new(); + for y in 0..buffer.area.height { + for x in 0..buffer.area.width { + text.push_str(buffer[(x, y)].symbol()); + } + text.push('\n'); + } + text +} + +fn ms(values: &[Duration]) -> serde_json::Value { + let mut sorted: Vec = values.iter().map(|v| v.as_secs_f64() * 1000.0).collect(); + sorted.sort_by(f64::total_cmp); + let p = |percent: usize| sorted[(sorted.len() * percent).div_ceil(100).saturating_sub(1)]; + json!({"samples": sorted.len(), "p50": p(50), "p95": p(95), "max": sorted.last()}) +} + +#[test] +fn editing_coalesces_without_rebuilding_or_starting_search() { + let mut picker = large_history(12, 1); + picker.activate_search(); + let rows = picker.visible_sessions.clone(); + let selected = picker.list_state.selected(); + let query = picker.cached_search_query.clone(); + for ch in "deployment thr".chars() { + picker + .handle_overlay_key(KeyCode::Char(ch), KeyModifiers::NONE) + .unwrap(); + assert_eq!(picker.visible_sessions, rows); + assert_eq!(picker.list_state.selected(), selected); + assert_eq!(picker.cached_search_query, query); + assert!(!picker.transcript_search.pending()); + } + assert!(picker.search_edit_pending); + assert!(picker.has_pending_preview_work()); + assert_eq!(picker.search_query, "deployment thr"); + // A subsequent poll submits only the final edit, not fourteen searches. + picker.poll_transcript_search(); + assert!(!picker.search_edit_pending); + assert_eq!(picker.cached_search_query, "deployment thr"); +} + +#[test] +fn enter_before_first_poll_never_resumes_a_stale_row() { + let mut picker = large_history(2, 1); + picker.activate_search(); + picker + .handle_overlay_key(KeyCode::Char('z'), KeyModifiers::NONE) + .unwrap(); + let action = picker + .handle_overlay_key(KeyCode::Enter, KeyModifiers::NONE) + .unwrap(); + assert!(matches!(action, OverlayAction::Continue)); + assert!(picker.search_submission_pending()); + assert!(picker.search_edit_pending); +} + +#[test] +#[ignore = "manual typing benchmark, run alone with --nocapture --test-threads=1"] +fn benchmark_resume_typing_experience() { + let mut picker = large_history(700, 8); + let mut terminal = Terminal::new(TestBackend::new(160, 50)).unwrap(); + terminal.draw(|frame| picker.render(frame)).unwrap(); + picker.activate_search(); + let mut handler = Vec::new(); + let mut key_frame = Vec::new(); + let mut follow_frame = Vec::new(); + let mut frames = 0usize; + let mut empty_list_frames = 0usize; + let mut loading_frames = 0usize; + let mut observe = |terminal: &Terminal| { + let text = screen_text(terminal); + frames += 1; + if !text.contains("Performance session") { + empty_list_frames += 1; + } + if text.contains("Loading preview") || text.contains("No session selected") { + loading_frames += 1; + } + }; + for round in 0..3 { + for ch in "deployment thr".chars() { + let start = Instant::now(); + picker + .handle_search_key(KeyCode::Char(ch), KeyModifiers::NONE) + .unwrap(); + handler.push(start.elapsed()); + let start = Instant::now(); + terminal.draw(|frame| picker.render(frame)).unwrap(); + key_frame.push(start.elapsed()); + observe(&terminal); + // A 64 ms typing cadence observed at the 16 ms poll interval. + for _ in 0..4 { + std::thread::sleep(Duration::from_millis(16)); + let start = Instant::now(); + terminal.draw(|frame| picker.render(frame)).unwrap(); + follow_frame.push(start.elapsed()); + observe(&terminal); + } + } + for _ in 0.."deployment thr".len() { + picker + .handle_search_key(KeyCode::Backspace, KeyModifiers::NONE) + .unwrap(); + } + terminal.draw(|frame| picker.render(frame)).unwrap(); + let _ = round; + } + eprintln!( + "TYPING_BENCH {}", + json!({ + "sessions": 700, "index_bytes_per_session": 14 * 1024, "terminal": [160, 50], + "profile": if cfg!(debug_assertions) { "debug" } else { "optimized" }, + "key_handler_ms": ms(&handler), "key_frame_ms": ms(&key_frame), + "follow_frame_ms": ms(&follow_frame), "frames": frames, + "empty_list_frames": empty_list_frames, "placeholder_preview_frames": loading_frames, + }) + ); +} diff --git a/crates/jcode-tui/src/tui/session_picker_presence.rs b/crates/jcode-tui/src/tui/session_picker_presence.rs index a2f925653a6..7f493509183 100644 --- a/crates/jcode-tui/src/tui/session_picker_presence.rs +++ b/crates/jcode-tui/src/tui/session_picker_presence.rs @@ -26,6 +26,7 @@ impl SessionPicker { crate::session::recent_window_closures(std::time::Duration::from_secs( crate::config::config().display.resume_recent_close_seconds, )); + self.merge_cloud_presence(); self.live_presence_refreshed_at = Some(std::time::Instant::now()); } @@ -38,11 +39,12 @@ impl SessionPicker { if self.loading_message.is_some() { return false; } + let cloud_changed = self.maybe_refresh_cloud_presence(); let due = self .live_presence_refreshed_at .is_none_or(|at| at.elapsed() >= LIVE_PRESENCE_REFRESH_INTERVAL); if !due { - return false; + return cloud_changed; } let before = std::mem::take(&mut self.live_presence); let closures_before = std::mem::take(&mut self.recent_window_closures); @@ -52,7 +54,7 @@ impl SessionPicker { if closures_changed || (changed && self.filter_mode == SessionFilterMode::Active) { self.rebuild_items(); } - changed + changed || cloud_changed } /// A recent final-client EOF, not recent messages or a daemon-owned Agent. diff --git a/crates/jcode-tui/src/tui/session_picker_tests.rs b/crates/jcode-tui/src/tui/session_picker_tests.rs index f0cd792778c..74352cd2a51 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests.rs @@ -26,10 +26,114 @@ fn line_text(line: &Line<'_>) -> String { .collect::() } +pub(super) fn make_session_for_presence(id: &str) -> SessionInfo { + make_session(id, id, false, SessionStatus::Closed) +} + fn make_session(id: &str, short_name: &str, is_debug: bool, status: SessionStatus) -> SessionInfo { make_session_with_flags(id, short_name, is_debug, false, status) } +/// Copy the highlighted session without changing selection and report failures. +#[test] +fn copy_session_identifier_reports_success_and_failure() { + let session = make_session("session_alpha", "alpha", false, SessionStatus::Closed); + let mut picker = SessionPicker::new(vec![session]); + let mut copied = String::new(); + picker.copy_selected_session_with(|id| { + copied = id.to_owned(); + true + }); + assert_eq!(copied, "session_alpha"); + assert_eq!(picker.copy_notice, Some("Session ID copied")); + assert_eq!(picker.selected_session().unwrap().id, "session_alpha"); + picker.copy_selected_session_with(|_| false); + assert_eq!(picker.copy_notice, Some("Could not copy to clipboard")); +} + +/// Copy the actual external identifier and label Pi's path accurately. +#[test] +fn copy_external_session_uses_resume_target() { + let mut session = make_session("internal-id", "external", false, SessionStatus::Closed); + session.resume_target = ResumeTarget::PiSession { + session_path: "pi/session.jsonl".into(), + }; + let mut picker = SessionPicker::new(vec![session]); + picker.copy_selected_session_with(|id| { + assert_eq!(id, "pi/session.jsonl"); + true + }); + assert_eq!(picker.copy_notice, Some("Session path copied")); +} + +/// Search still accepts y as text and the copy action handles an empty list. +#[test] +fn copy_shortcut_does_not_interfere_with_search_or_empty_picker() { + let mut picker = SessionPicker::new(Vec::new()); + picker + .handle_overlay_key(KeyCode::Char('y'), KeyModifiers::empty()) + .unwrap(); + assert_eq!(picker.copy_notice, Some("No session selected")); + picker + .handle_overlay_key(KeyCode::Char('/'), KeyModifiers::empty()) + .unwrap(); + picker + .handle_overlay_key(KeyCode::Char('y'), KeyModifiers::empty()) + .unwrap(); + assert_eq!(picker.search_query, "y"); + assert_eq!(picker.copy_notice, None); +} + +/// The picker shortcut invokes the shared clipboard implementation. +#[test] +fn copy_shortcut_copies_highlighted_session() { + let _lock = crate::tui::ui::render_state_test_lock(); + let mut picker = SessionPicker::new(vec![make_session( + "session_alpha", + "alpha", + false, + SessionStatus::Closed, + )]); + picker + .handle_overlay_key(KeyCode::Char('y'), KeyModifiers::empty()) + .unwrap(); + assert_eq!(picker.copy_notice, Some("Session ID copied")); +} + +/// The picker advertises the shortcut and shows the result in its footer. +#[test] +fn copy_shortcut_and_result_are_visible() { + let _lock = crate::tui::ui::render_state_test_lock(); + let mut picker = SessionPicker::new(vec![make_session( + "session_alpha", + "alpha", + false, + SessionStatus::Closed, + )]); + let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(120, 40)).unwrap(); + terminal.draw(|frame| picker.render(frame)).unwrap(); + let text: String = terminal + .backend() + .buffer() + .content() + .iter() + .map(|cell| cell.symbol()) + .collect(); + assert!(text.contains("y copy ID")); + picker + .handle_overlay_key(KeyCode::Char('y'), KeyModifiers::empty()) + .unwrap(); + terminal.draw(|frame| picker.render(frame)).unwrap(); + let text: String = terminal + .backend() + .buffer() + .content() + .iter() + .map(|cell| cell.symbol()) + .collect(); + assert!(text.contains("Session ID copied")); +} + #[path = "session_picker_tests/identity_search.rs"] mod identity_search; @@ -129,7 +233,7 @@ fn cloud_filter_supports_flat_sessions_and_debug_visibility() { assert_eq!(picker.visible_sessions.len(), 2); assert_eq!(picker.hidden_test_count, 0); picker.cycle_filter_mode_backwards(); - assert_eq!(picker.filter_mode, SessionFilterMode::Saved); + assert_eq!(picker.filter_mode, SessionFilterMode::All); picker.cycle_filter_mode(); assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); } @@ -200,6 +304,7 @@ fn cloud_history_mixes_with_local_and_preserves_real_servers_and_saved_boost() { ); picker.search_query = "local".into(); picker.rebuild_items(); + wait_for_picker_search(&mut picker); assert!(picker.visible_sessions.is_empty()); } @@ -662,6 +767,7 @@ fn test_filter_matches_recent_message_content() { picker.search_query = "not-in-preview".to_string(); picker.rebuild_items(); + wait_for_picker_search(&mut picker); assert!(picker.visible_sessions.is_empty()); } @@ -734,6 +840,7 @@ fn resume_search_battery_prompt_outranks_incidental_tool_mentions() { let mut picker = SessionPicker::new(sessions); picker.search_query = "battery".into(); picker.rebuild_items(); + wait_for_picker_search(&mut picker); assert_eq!(picker.visible_sessions.len(), 98); assert_eq!( picker @@ -848,6 +955,7 @@ fn benchmark_resume_search_reports_incremental_timings() { picker.rebuild_items(); let third_ms = third_start.elapsed().as_secs_f64() * 1000.0; + wait_for_picker_search(&mut picker); assert_eq!(picker.visible_sessions.len(), 1); eprintln!( "resume search bench: first_char={:.3}ms second_char={:.3}ms full_query={:.3}ms sessions=500", @@ -892,6 +1000,10 @@ fn test_filter_mode_cycles_through_requested_session_sources() { assert_eq!(picker.filter_mode, SessionFilterMode::All); assert_eq!(picker.visible_sessions.len(), 6); + picker.cycle_filter_mode(); + assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); + assert!(picker.visible_sessions.is_empty()); + picker.cycle_filter_mode(); assert_eq!(picker.filter_mode, SessionFilterMode::CurrentDir); assert_eq!(picker.visible_sessions.len(), 1); @@ -916,10 +1028,6 @@ fn test_filter_mode_cycles_through_requested_session_sources() { assert!(picker.visible_session_iter().all(|session| session.saved)); assert_eq!(picker.items.len(), picker.visible_sessions.len()); - picker.cycle_filter_mode(); - assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); - assert!(picker.visible_sessions.is_empty()); - picker.cycle_filter_mode(); assert_eq!(picker.filter_mode, SessionFilterMode::Active); // No live processes own these synthetic sessions, so the Active view is @@ -987,12 +1095,26 @@ fn test_filter_mode_keyboard_shortcuts_cycle_both_directions() { picker .handle_overlay_key(KeyCode::Char('s'), KeyModifiers::empty()) .unwrap(); - assert_eq!(picker.filter_mode, SessionFilterMode::CurrentDir); + assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); picker .handle_overlay_key(KeyCode::Char('S'), KeyModifiers::empty()) .unwrap(); assert_eq!(picker.filter_mode, SessionFilterMode::All); + + // Alt+S cycles even while typing a search query, without inserting text. + picker + .handle_overlay_key(KeyCode::Char('/'), KeyModifiers::empty()) + .unwrap(); + picker + .handle_overlay_key(KeyCode::Char('s'), KeyModifiers::ALT) + .unwrap(); + assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); + assert!(picker.search_query.is_empty()); + picker + .handle_overlay_key(KeyCode::Char('S'), KeyModifiers::ALT | KeyModifiers::SHIFT) + .unwrap(); + assert_eq!(picker.filter_mode, SessionFilterMode::All); } include!("session_picker_tests/live_presence.rs"); @@ -1330,6 +1452,15 @@ fn make_session_with_many_turns(id: &str, turns: usize) -> SessionInfo { session } +fn picker_buffer(picker: &mut SessionPicker, w: u16, h: u16) -> ratatui::buffer::Buffer { + let backend = ratatui::backend::TestBackend::new(w, h); + let mut terminal = ratatui::Terminal::new(backend).expect("test terminal"); + terminal + .draw(|frame| picker.render(frame)) + .expect("render picker"); + terminal.backend().buffer().clone() +} + fn buffer_text(picker: &mut SessionPicker, w: u16, h: u16) -> String { let backend = ratatui::backend::TestBackend::new(w, h); let mut terminal = ratatui::Terminal::new(backend).expect("test terminal"); @@ -1678,6 +1809,11 @@ fn contains_scrollbar_glyph(text: &str) -> bool { #[test] fn test_preview_transcript_omits_picker_metadata() { + let _render_state = crate::tui::ui::render_state_test_lock(); + let _changelog = + crate::tui::ui::header::scoped_unseen_changelog_entries_override_for_tests(vec![ + "Deterministic preview changelog fixture".to_string(), + ]); let mut session = make_session("metadata", "metadata-monkey", false, SessionStatus::Active); session.title = "metadata-video-title".to_string(); session.working_dir = Some("/metadata-only-directory".to_string()); @@ -1701,21 +1837,30 @@ fn test_preview_transcript_omits_picker_metadata() { .collect::>() .join("\n"); assert!(text.contains("1› hello"), "{text}"); + assert!(text.contains("Updates"), "{text}"); + assert!( + text.contains("Deterministic preview changelog fixture"), + "{text}" + ); assert!( text.contains("client: Metadata-monkey"), "the real startup header belongs to the scrolling transcript" ); - for metadata in [ - "metadata-video-title", - "/metadata-only-directory", - "▶ Active", - "────", - ] { + for metadata in ["metadata-video-title", "▶ Active"] { assert!( !text.contains(metadata), "unexpected picker metadata: {text}" ); } + // Startup Updates boxes legitimately contain horizontal borders. Only the + // standalone picker separator is metadata, not every run of border glyphs. + assert!( + !text.lines().any(|line| { + let line = line.trim(); + line.starts_with("────") && line.chars().all(|ch| ch == '─') + }), + "unexpected standalone picker separator: {text}" + ); } #[test] @@ -1736,6 +1881,7 @@ fn preview_transcript_hash_ignores_picker_metadata() { #[test] fn test_preview_is_left_aligned_independently_of_chat_markdown_context() { + let _env_lock = crate::storage::lock_test_env(); for width in [60, 100] { let mut reference = None; for centered in [false, true] { @@ -2181,6 +2327,7 @@ fn test_reseed_grouped_preserves_selection_and_search() { picker.search_active = true; picker.focus = PaneFocus::Preview; picker.rebuild_items(); + wait_for_picker_search(&mut picker); let search_selected = picker .selected_session() .map(|s| s.id.clone()) @@ -2189,6 +2336,7 @@ fn test_reseed_grouped_preserves_selection_and_search() { // Reseed with the same data (as the async refresh would). picker.reseed_grouped(Vec::new(), sessions); + wait_for_picker_search(&mut picker); // Search query, search mode, focus, and the matched selection survive. assert_eq!(picker.search_query, "reseed4"); assert!(picker.search_active); @@ -2590,18 +2738,15 @@ fn search_highlights_match_in_preview_and_scrolls_to_it() { "matched term should be visible in the preview after scrolling" ); - // The match should be highlighted (bold) in the cached wrapped lines. - let highlighted = picker - .preview_cache - .as_ref() - .expect("preview cache built") - .wrapped_lines - .iter() - .flat_map(|line| line.spans.iter()) - .any(|s| { - s.content.to_lowercase().contains("flibbertigibbet") - && s.style.add_modifier.contains(Modifier::BOLD) - }); + // The match should be highlighted (bold) where the renderer drew it. + let buffer = picker_buffer(&mut picker, w, h); + let highlighted = (0..h).any(|y| { + let row: String = (0..w).map(|x| buffer[(x, y)].symbol()).collect(); + row.find("flibbertigibbet").is_some_and(|byte| { + let x = row[..byte].chars().count() as u16; + buffer[(x, y)].modifier.contains(Modifier::BOLD) + }) + }); assert!( highlighted, "matched term in preview body should be highlighted" diff --git a/crates/jcode-tui/src/tui/session_picker_tests/identity_search.rs b/crates/jcode-tui/src/tui/session_picker_tests/identity_search.rs index c87fc7c1c45..b9d0c141206 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests/identity_search.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests/identity_search.rs @@ -31,6 +31,7 @@ fn resume_identity_search_prioritizes_cloud_swan_over_mentions_before_and_after_ ] { picker.search_query = query.into(); picker.rebuild_items(); + wait_for_picker_search(&mut picker); assert_eq!( picker .session_by_ref(picker.visible_sessions[0]) diff --git a/crates/jcode-tui/src/tui/session_picker_tests/live_presence.rs b/crates/jcode-tui/src/tui/session_picker_tests/live_presence.rs index dcc82e9db46..0d9ca2d8add 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests/live_presence.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests/live_presence.rs @@ -43,7 +43,7 @@ fn test_active_filter_shows_only_live_sessions_ready_before_working() { } #[test] -fn test_active_rows_render_age_without_status_badges() { +fn test_running_rows_show_spinner_state_and_ready_rows_show_age() { let live_working = make_session("session_working", "alpha", false, SessionStatus::Active); let live_ready = make_session("session_ready", "beta", false, SessionStatus::Closed); let mut picker = SessionPicker::new(vec![live_working, live_ready]); @@ -70,8 +70,8 @@ fn test_active_rows_render_age_without_status_badges() { .map(line_text) .collect::>() .join("\n"); - assert!(working_text.contains("ago"), "{working_text}"); - assert!(!working_text.contains("working"), "{working_text}"); + assert!(working_text.contains("running"), "{working_text}"); + assert!(!working_text.contains("ago"), "{working_text}"); let first_frame = picker .render_session_item_lines_at_frame(&working, false, 0) @@ -85,8 +85,8 @@ fn test_active_rows_render_age_without_status_badges() { .map(line_text) .collect::>() .join("\n"); - assert!(!first_frame.contains('⠋')); - assert!(!second_frame.contains('⠙')); + assert!(first_frame.contains('⠋'), "{first_frame}"); + assert!(second_frame.contains('⠙'), "{second_frame}"); assert!(picker.has_visible_running_sessions()); let ready_lines = picker.render_session_item_lines(&ready, false); @@ -104,7 +104,7 @@ fn test_active_rows_render_age_without_status_badges() { !ready_text.contains("closed"), "live session must not render as closed: {ready_text}" ); - for row in [working_lines[1].to_string(), ready_lines[1].to_string()] { + for row in [ready_lines[1].to_string()] { for status in ["✓", "●", "▶", "active", "ready", "working"] { assert!(!row.contains(status), "unexpected {status}: {row}"); } @@ -153,7 +153,7 @@ fn test_recent_window_close_boosts_old_transcript_above_newer_messages_and_saved .collect::>() .join("\n"); assert!(!text.contains("just closed"), "{text}"); - assert!(text.contains("ago"), "{text}"); + assert!(text.contains("running"), "{text}"); assert!(!picker.session_recently_closed(picker.visible_session_iter().nth(1).unwrap())); } diff --git a/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs b/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs index 0b312c421df..18926cd6c9f 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs @@ -1,5 +1,58 @@ use super::*; +#[test] +fn cloud_warning_retains_local_sessions_and_survives_fresh_update() { + let local = make_session("local", "local session", false, SessionStatus::Closed); + let mut picker = SessionPicker::new(vec![local.clone()]); + let warning = "Cloud sessions could not be refreshed. Reopen /resume to retry."; + assert!( + !picker + .apply_session_list_update(SessionListUpdate::CloudUnavailable(warning.into())) + .unwrap() + ); + assert_eq!( + picker.selected_session().map(|s| s.id.as_str()), + Some("local") + ); + assert_eq!(picker.visible_sessions.len(), 1); + assert!( + picker + .apply_session_list_update(SessionListUpdate::Fresh(Ok((vec![], vec![local])))) + .unwrap() + ); + assert_eq!(picker.cloud_warning.as_deref(), Some(warning)); + assert_eq!( + picker.selected_session().map(|s| s.id.as_str()), + Some("local") + ); + assert!(SessionPicker::loading().cloud_warning.is_none()); + assert!(SessionPicker::new(vec![]).cloud_warning.is_none()); + assert!( + SessionPicker::new_grouped(vec![], vec![]) + .cloud_warning + .is_none() + ); + + for width in [12, 80] { + let mut terminal = + ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, 12)).unwrap(); + terminal + .draw(|frame| picker.render_session_list_impl(frame, frame.area(), false)) + .unwrap(); + assert_eq!(picker.last_list_area.unwrap().y, 1); + let buffer = terminal.backend().buffer(); + let first_row: String = (0..width).map(|x| buffer[(x, 0)].symbol()).collect(); + assert!(first_row.starts_with("Cloud")); + if width == 80 { + assert!(first_row.contains("Reopen /resume to retry.")); + let text: String = buffer.content().iter().map(|cell| cell.symbol()).collect(); + assert!(text.contains("Test session")); + } else { + assert!(first_row.ends_with('…')); + } + } +} + #[test] fn standalone_resume_renders_before_list_load_finishes() { let mut picker = SessionPicker::loading(); @@ -151,6 +204,7 @@ fn loading_picker_search_survives_cached_and_fresh_updates_and_filters_both() { ))) .expect("cached list") ); + wait_for_picker_search(&mut picker); assert_eq!(picker.search_query, "needle"); assert!(picker.search_active); assert_eq!(picker.visible_sessions.len(), 1); @@ -167,6 +221,7 @@ fn loading_picker_search_survives_cached_and_fresh_updates_and_filters_both() { )))) .expect("fresh list") ); + wait_for_picker_search(&mut picker); assert_eq!(picker.search_query, "needle"); assert!(picker.search_active); assert_eq!(picker.visible_sessions.len(), 1); @@ -195,6 +250,7 @@ fn loading_picker_enter_preserves_search_without_queuing_resume() { vec![make_session("matching", "q", false, SessionStatus::Closed)], ))) .unwrap(); + wait_for_picker_search(&mut picker); assert_eq!(picker.search_query, "q"); assert!(picker.search_active); assert_eq!( @@ -211,7 +267,7 @@ fn loading_picker_filter_choice_survives_refresh() { .unwrap(); let chosen = picker.filter_mode; assert!(picker.filter_mode_user_chosen); - assert_eq!(chosen, SessionFilterMode::CurrentDir); + assert_eq!(chosen, SessionFilterMode::Cloud); picker .apply_session_list_update(SessionListUpdate::Cached(( Vec::new(), @@ -256,6 +312,7 @@ fn standalone_resume_refresh_preserves_cached_selection_and_search() { )))) .expect("fresh list") ); + wait_for_picker_search(&mut picker); assert_eq!( picker.selected_session().map(|s| s.id.as_str()), Some("second") diff --git a/crates/jcode-tui/src/tui/session_picker_tests/resume_canvas.rs b/crates/jcode-tui/src/tui/session_picker_tests/resume_canvas.rs index 5b9c923f082..77065ae67bd 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests/resume_canvas.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests/resume_canvas.rs @@ -219,20 +219,13 @@ fn preview_cache_preserves_live_tool_metadata_and_invalidates_on_changes() { tool_data: msg.tool_data.clone(), }) .collect(); - let text_width = width - u16::from(cache.show_scrollbar); - let text_width = text_width.saturating_sub( - super::super::super::ui::left_aligned_content_inset(text_width, false), - ); - let live = super::super::super::ui::prepare::prepare_history_preview( - &messages, 0, text_width, mode, - ); - let header = super::super::super::ui::header::build_preview_header(&session, text_width); - let offset = header.len() - + super::super::super::ui::prepare::initial_header_pad_top(inner.height, header.len()); + assert_eq!(cache.messages.len(), messages.len(), "width={width}"); + let last = cache.messages.last().unwrap(); + assert_eq!(last.duration_secs, Some(2.3)); + assert_eq!(last.title.as_deref(), Some("Saved tool title")); assert_eq!( - cache.wrapped_lines[offset..], - live.wrapped_lines, - "width={width}" + last.tool_data.as_ref().map(|t| t.name.as_str()), + Some("bash") ); } session.messages_preview.last_mut().unwrap().duration_secs = Some(4.5); @@ -254,6 +247,7 @@ fn completed_empty_preview_is_not_loaded_again() { }); tx.send(Some(LoadedSessionPreview { messages: vec![], + search: None, extras: PreviewExtras::default(), })) .unwrap(); diff --git a/crates/jcode-tui/src/tui/stable_status.rs b/crates/jcode-tui/src/tui/stable_status.rs deleted file mode 100644 index f49c940f9e5..00000000000 --- a/crates/jcode-tui/src/tui/stable_status.rs +++ /dev/null @@ -1,503 +0,0 @@ -use super::*; -use unicode_width::UnicodeWidthStr; - -const PHASE_WIDTH: usize = 12; -const ELAPSED_WIDTH: usize = 8; - -fn status_single_line(text: &str) -> String { - text.chars() - .map(|c| { - if c.is_control() || c.is_whitespace() - || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') - { ' ' } else { c } - }) - .collect::() - .split_whitespace() - .collect::>() - .join(" ") -} - -pub(super) fn status_cell(text: &str, width: usize) -> String { - let line = jcode_tui_render::truncate_line_with_ellipsis_to_width( - &Line::from(status_single_line(text)), - width, - ); - let mut result = line - .spans - .iter() - .map(|s| s.content.as_ref()) - .collect::(); - result.push_str(&" ".repeat(width.saturating_sub(UnicodeWidthStr::width(result.as_str())))); - result -} - -pub(super) fn stable_status_elapsed(elapsed_ms: u64) -> String { - let seconds = elapsed_ms / 1000; - let hours = seconds / 3600; - let value = if hours == 0 { - format!("{:02}:{:02}", seconds / 60, seconds % 60) - } else if hours < 100 { - format!("{hours}:{:02}:{:02}", seconds / 60 % 60, seconds % 60) - } else if hours <= 999 { - format!("{hours}h{:02}m", seconds / 60 % 60) - } else if hours <= 999_999 { - format!("{hours}h") - } else { - ">999999h".into() - }; - status_cell(&value, ELAPSED_WIDTH) -} - -/// Values never resize a telemetry slot, including when telemetry is absent. -pub(super) fn stable_status_telemetry(status: &crate::protocol::SwarmLiveStatus) -> [String; 3] { - let token = |value: Option| { - value - .map(format_stream_tokens) - .unwrap_or_else(|| "-".into()) - }; - [ - format!("↑{}", status_cell(&token(status.input_tokens), 6)), - format!("↓{}", status_cell(&token(status.output_tokens), 6)), - status_cell( - &status - .output_tps_milli - .map(|v| format!("{:.1}/s", v as f64 / 1000.0)) - .unwrap_or_else(|| "-/s".into()), - 8, - ), - ] -} - -#[derive(Default)] -struct StatusBadges { - queued: usize, - background: usize, -} - -impl StatusBadges { - fn text(&self, width: usize) -> String { - let count = |n: usize| if n > 99 { "99+".into() } else { n.to_string() }; - let queued = if self.queued > 0 { - if width >= 22 { - format!("[+{} queued]", count(self.queued)) - } else { - format!("q:{}", count(self.queued)) - } - } else { - String::new() - }; - let background = if self.background > 0 { - if width >= 22 { - format!("[bg {}]", count(self.background)) - } else { - format!("bg:{}", count(self.background)) - } - } else { - String::new() - }; - let (queue_width, background_width) = match width { - 22.. => (13, 8), - 12.. => (5, 6), - _ => (3, 4), - }; - format!( - "{} {}", - status_cell(&queued, queue_width), - status_cell(&background, background_width) - ) - } -} - -#[cfg(test)] -pub(super) fn stable_status_line( - glyph: &str, - phase: &str, - elapsed_ms: u64, - activity: &str, - telemetry: &[String], - width: usize, -) -> Line<'static> { - compose_status( - glyph, - phase, - elapsed_ms, - activity, - telemetry, - &StatusBadges::default(), - width, - ) -} - -fn compose_status( - glyph: &str, - phase: &str, - elapsed_ms: u64, - activity: &str, - telemetry: &[String], - badges: &StatusBadges, - width: usize, -) -> Line<'static> { - let clock = if phase == "idle" { - " ".repeat(ELAPSED_WIDTH) - } else { - stable_status_elapsed(elapsed_ms) - }; - let prefix = format!(" {} {} │ ", status_cell(phase, PHASE_WIDTH), clock); - let prefix_width = 1 + UnicodeWidthStr::width(prefix.as_str()); - // These reservations depend only on terminal width, never on label/count length. - let badge_width = match width { - 100.. => 22, - 64.. => 12, - 40.. => 8, - _ => 0, - }; - let mut count = telemetry.len(); - while count > 0 - && prefix_width - + 8 - + badge_width - + 3 - + telemetry[..count] - .iter() - .map(|s| UnicodeWidthStr::width(s.as_str()) + 1) - .sum::() - > width - { - count -= 1; - } - let right = telemetry[..count].join(" "); - let right_width = UnicodeWidthStr::width(right.as_str()) + if count > 0 { 3 } else { 0 }; - let activity_width = width.saturating_sub(prefix_width + badge_width + right_width); - let warning = matches!( - phase, - "building" | "rate limited" | "offline" | "blocked" | "failed" | "crashed" - ); - let warning_color = rgb(255, 193, 7); - let activity_warning = warning - || activity.contains('⚠') - || activity.starts_with("stalled") - || activity.starts_with("no tokens"); - let mut spans = vec![ - Span::styled( - status_cell(glyph, 1), - Style::default().fg(if warning { warning_color } else { ai_color() }), - ), - Span::styled( - prefix, - Style::default().fg(if warning { warning_color } else { dim_color() }), - ), - Span::styled( - status_cell(activity, activity_width), - Style::default().fg(if activity_warning { - warning_color - } else { - ai_color() - }), - ), - ]; - if badge_width > 0 { - spans.push(Span::styled( - badges.text(badge_width), - Style::default().fg(queued_color()), - )); - } - if count > 0 { - spans.push(Span::styled( - format!(" │ {right}"), - Style::default().fg(dim_color()), - )); - } - jcode_tui_render::truncate_line_with_ellipsis_to_width(&Line::from(spans), width) -} - -fn live_row( - status: &crate::protocol::SwarmLiveStatus, - stale_secs: Option, - spinner: &'static str, - badges: &StatusBadges, - width: usize, -) -> Line<'static> { - let phase = match status.phase.as_str() { - "running_tool" if status.tool_name.as_deref() == Some("batch") => "batch", - "running_tool" => "tool", - "running" => "working", - "waiting_network" => "offline", - other => other, - }; - let terminal = matches!( - phase, - "completed" - | "done" - | "failed" - | "crashed" - | "stopped" - | "cancelled" - | "blocked" - | "waiting" - | "idle" - | "ready" - | "queued" - ); - let mut activity = Vec::new(); - // Warnings have a stable home, ahead of potentially unbounded tool intent. - if status.phase == "streaming" - && !status.stream_message_ended - && let Some(stale) = stale_secs.filter(|s| *s > 2.0) - { - activity.push(format!( - "{} {:.0}s", - if stale > 10.0 { "stalled" } else { "no tokens" }, - stale - )); - } - if terminal { - if let Some(reason) = status - .stop_reason - .as_ref() - .or(status.status_detail.as_ref()) - { - activity.push(reason.clone()); - } - } else { - // draw_status places important warnings at the start of this field. - if let Some(intent) = &status.tool_intent - && intent.contains('⚠') - { - activity.push(intent.clone()); - } - if let Some(name) = &status.tool_name { - activity.push(name.clone()); - } - if let Some(intent) = &status.tool_intent - && !intent.contains('⚠') - { - activity.push(intent.clone()); - } - if let Some(connection) = &status.connection_phase { - activity.push(match connection.as_str() { - "authenticating" => "refreshing auth".into(), - "sending_request" => "sending context".into(), - "waiting_for_response" => "waiting for response".into(), - other => other.into(), - }); - } - activity.extend(collect_transport_context_labels( - status - .status_detail - .as_deref() - .and_then(normalize_status_detail), - status - .connection_type - .as_deref() - .map(display_connection_type), - None, - )); - } - activity.retain(|detail| !detail.trim().is_empty()); - let glyph = match phase { - "completed" | "done" => "✓", - "failed" | "crashed" => "✗", - "blocked" => "⊘", - "waiting" | "offline" => "↻", - "idle" | "ready" | "stopped" | "cancelled" => "◼", - _ => spinner, - }; - let telemetry = if terminal { - [" ".repeat(7), " ".repeat(7), " ".repeat(8)] - } else { - stable_status_telemetry(status) - }; - compose_status( - glyph, - phase, - status.elapsed_ms, - &activity.join(" · "), - &telemetry, - badges, - width, - ) -} - -#[cfg(test)] -pub(super) fn live_processing_status_line_at_width( - status: &crate::protocol::SwarmLiveStatus, - stale_secs: Option, - spinner: &'static str, - width: usize, -) -> Line<'static> { - live_row(status, stale_secs, spinner, &StatusBadges::default(), width) -} - -pub(super) fn draw_status(frame: &mut Frame, app: &dyn TuiState, area: Rect, pending_count: usize) { - if app.status_notice().as_deref() == Some(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) { - crate::tui::app::oauth_telemetry::note_status_render(area); - let notice = Line::from(status_single_line( - crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT, - )); - frame.render_widget( - Paragraph::new(jcode_tui_render::truncate_line_with_ellipsis_to_width( - ¬ice, - area.width as usize, - )) - .style(Style::default().fg(accent_color())), - area, - ); - return; - } - let elapsed = app.elapsed().map(|d| d.as_secs_f32()).unwrap_or(0.0); - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.elapsed_ms = app - .elapsed() - .map(|d| d.as_millis().min(u64::MAX as u128) as u64) - .unwrap_or(0); - let mut activity = Vec::new(); - let build = crate::build::read_build_progress(); - snapshot.phase = if let Some(build) = build { - activity.push(build.to_string()); - "building".into() - } else if let Some(remaining) = app.rate_limit_remaining() { - activity.push(format!( - "auto-retry in {}", - stable_status_elapsed(remaining.as_millis().min(u64::MAX as u128) as u64).trim() - )); - "rate limited".into() - } else if app.is_processing() { - match app.status() { - ProcessingStatus::Idle => "idle".into(), - ProcessingStatus::Sending => "sending".into(), - ProcessingStatus::Thinking(_) => "thinking".into(), - ProcessingStatus::Streaming => "streaming".into(), - ProcessingStatus::Connecting(ref phase) => { - snapshot.connection_phase = Some(connection_phase_label(phase)); - let phase_elapsed = app - .connection_phase_elapsed() - .map_or(elapsed, |d| d.as_secs_f32()); - if matches!(phase, crate::message::ConnectionPhase::Retrying { .. }) - || (phase_elapsed > 10.0 - && matches!( - phase, - crate::message::ConnectionPhase::Authenticating - | crate::message::ConnectionPhase::Connecting - | crate::message::ConnectionPhase::SendingRequest - )) - { - activity.push("⚠ connection delayed".into()); - } - "connecting".into() - } - ProcessingStatus::WaitingForNetwork { listener } => { - activity.push(format!( - "network disconnected, waiting to retry · {listener}" - )); - "waiting_network".into() - } - ProcessingStatus::RunningTool(ref name) => { - snapshot.tool_name = Some(name.clone()); - if name == "batch" { - let calls = app.streaming_tool_calls(); - let initial = calls - .last() - .and_then(|tc| tc.input.get("tool_calls")) - .and_then(|v| v.as_array()) - .map(|a| a.len()); - let mut spans = Vec::new(); - append_batch_progress_spans( - &mut spans, - ai_color(), - app.batch_progress(), - initial, - ); - activity.push( - spans - .iter() - .map(|s| s.content.as_ref()) - .collect::() - .trim_start_matches(" · ") - .to_string(), - ); - } else if let Some(detail) = app - .streaming_tool_calls() - .last() - .map(get_tool_activity_detail) - .filter(|d| !d.is_empty()) - { - activity.push(detail); - } - "running_tool".into() - } - } - } else { - "idle".into() - }; - if app.is_processing() { - let mut warnings = Vec::new(); - if let Some(notice) = app.active_experimental_feature_notice() { - warnings.push(format!("⚠ {notice}")); - } - if matches!(snapshot.phase.as_str(), "streaming" | "running_tool") { - let (read, creation) = app.streaming_cache_tokens(); - if let Some(problem) = detect_kv_cache_problem( - &app.provider_name(), - app.upstream_provider().as_deref(), - app.display_user_message_count(), - app.streaming_tokens().0, - read, - creation, - app.cache_ttl_status().as_ref(), - ) { - warnings.push(format!( - "⚠ {} cache miss", - problem - .affected_tokens - .map(format_stream_tokens) - .unwrap_or_else(|| "kv".into()) - )); - } - } - warnings.append(&mut activity); - activity = warnings; - if let Some(subagent) = app.subagent_status() { - activity.push(subagent); - } - if let Some(upstream) = app.upstream_provider() { - activity.push(format!("via {upstream}")); - } - } else if snapshot.phase == "idle" { - // Idle is steady except for actionable session-history warnings. No rotating tips. - if let Some((input, output)) = app.total_session_tokens() - && let Some(warning) = occasional_session_history_warning( - input.saturating_add(output), - app.session_compaction_count(), - app.context_limit(), - area.width as usize, - app.animation_elapsed() as u64, - ) - { - activity.push(format!("⚠ {warning}")); - } - snapshot.status_detail = activity.first().cloned(); - snapshot.connection_type = None; - } - if snapshot.phase != "streaming" { - snapshot.output_tps_milli = None; - } - snapshot.tool_intent = Some(activity.join(" · ")); - let badges = StatusBadges { - queued: pending_count, - background: app - .background_task_rows() - .iter() - .filter(|t| t.status == crate::tui::BackgroundTaskRowStatus::Running) - .count(), - }; - let line = live_row( - &snapshot, - app.time_since_activity().map(|d| d.as_secs_f32()), - super::super::activity_indicator(elapsed, 12.5), - &badges, - area.width as usize, - ); - crate::memory::check_staleness(); - // Center the composer container, never the changing status text inside it. - frame.render_widget(Paragraph::new(line).alignment(Alignment::Left), area); -} diff --git a/crates/jcode-tui/src/tui/terminal_events.rs b/crates/jcode-tui/src/tui/terminal_events.rs new file mode 100644 index 00000000000..724a06b4e90 --- /dev/null +++ b/crates/jcode-tui/src/tui/terminal_events.rs @@ -0,0 +1,592 @@ +//! Runtime terminal-background queries using the existing crossterm reader. +//! +//! Never run colorsaurus once an event reader exists: two stdin readers race +//! and a color report can become composer input. Crossterm 0.29 decodes OSC +//! reports as Alt+] / ordinary characters / Alt+\\ (or Ctrl+G). Intercept them +//! before *any* picker, shortcut, or composer sees them. The async reader and +//! synchronous burst drains share this filter, just like crossterm's reader. + +use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers}; +use futures::Stream; +use jcode_tui_style::ThemeMode; +use std::collections::VecDeque; +use std::future::Future; +use std::io::{self, Write}; +use std::pin::Pin; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Mutex, OnceLock}; +use std::task::{Context, Poll}; +use std::time::{Duration, Instant}; + +const QUERY_INTERVAL: Duration = Duration::from_secs(5); +const PREFIX_TIMEOUT: Duration = Duration::from_millis(250); +const REPORT_TIMEOUT: Duration = Duration::from_secs(2); +const MAX_REPORT_LEN: usize = 40; + +static THEME_REDRAW_PENDING: AtomicBool = AtomicBool::new(false); + +pub(super) fn theme_redraw_pending() -> bool { + THEME_REDRAW_PENDING.load(Ordering::Relaxed) +} + +pub(super) fn take_theme_redraw() -> bool { + THEME_REDRAW_PENDING.swap(false, Ordering::Relaxed) +} + +#[derive(Default)] +struct ColorReports { + held: Vec, + body: String, + started: Option, + ready: VecDeque, + mode: Option, + expecting_until: Option, + split_escape: bool, +} + +impl ColorReports { + fn recognized(&self) -> bool { + self.body.starts_with("11;rgb:") || self.body.starts_with("11;rgba:") + } + + fn reset(&mut self, replay: bool) { + if replay { + self.ready.extend(self.held.drain(..)); + } else { + self.held.clear(); + } + self.body.clear(); + self.started = None; + self.split_escape = false; + } + + fn expire(&mut self, now: Instant) { + let timeout = if self.recognized() { + REPORT_TIMEOUT + } else { + PREFIX_TIMEOUT + }; + if self + .started + .is_some_and(|started| now.duration_since(started) >= timeout) + { + // Replay an ordinary Alt+] chord, but never replay half a report. + self.reset(!self.recognized()); + } + } + + fn push(&mut self, event: Event, now: Instant) { + self.expire(now); + let Event::Key(key) = &event else { + if !self.recognized() { + self.reset(true); + } + self.ready.push_back(event); + return; + }; + if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + self.ready.push_back(event); + return; + } + // A read ending exactly after ESC makes crossterm emit a bare Escape + // key rather than an Alt chord. Reassemble only an expected query's + // introducer, or the terminator of a report already being consumed. + if self.split_escape { + let plain = key.modifiers.is_empty(); + if self.body.is_empty() && key.code == KeyCode::Char(']') && plain { + self.split_escape = false; + self.held.push(event); + return; + } + if self.recognized() && key.code == KeyCode::Char('\\') && plain { + self.mode = parse_background_report(&self.body); + self.expecting_until = None; + self.reset(false); + return; + } + if self.recognized() { + let escape = self.held.pop(); + self.reset(false); + if let Some(escape) = escape { + self.ready.push_back(escape); + } + } else { + self.reset(true); + } + } + if key.code == KeyCode::Esc + && key.modifiers.is_empty() + && (self.recognized() + || (self.started.is_none() + && self.expecting_until.is_some_and(|until| now < until))) + { + self.started.get_or_insert(now); + self.split_escape = true; + self.held.push(event); + return; + } + if key.code == KeyCode::Char(']') && key.modifiers == KeyModifiers::ALT { + self.reset(!self.recognized()); + self.started = Some(now); + self.held.push(event); + return; + } + if self.started.is_none() { + self.ready.push_back(event); + return; + } + let terminator = (key.code == KeyCode::Char('\\') && key.modifiers == KeyModifiers::ALT) + || (key.code == KeyCode::Char('g') && key.modifiers == KeyModifiers::CONTROL); + if terminator && self.recognized() { + self.mode = parse_background_report(&self.body); + self.expecting_until = None; + self.reset(false); + return; + } + if let KeyCode::Char(ch) = key.code + && (key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT) + && ch.is_ascii() + { + self.body.push(ch); + self.held.push(event); + let prefix = "11;rgb:".starts_with(&self.body) || "11;rgba:".starts_with(&self.body); + if self.body.len() > MAX_REPORT_LEN || (!prefix && !self.recognized()) { + self.reset(!self.recognized()); + } + } else { + self.reset(!self.recognized()); + self.ready.push_back(event); + } + } +} + +fn parse_background_report(body: &str) -> Option { + let (rgb, count) = if let Some(rgb) = body.strip_prefix("11;rgb:") { + (rgb, 3) + } else { + (body.strip_prefix("11;rgba:")?, 4) + }; + let components: Vec<_> = rgb.split('/').collect(); + if components.len() != count { + return None; + } + let mut channels = Vec::with_capacity(count); + for component in components { + if !(1..=4).contains(&component.len()) || !component.bytes().all(|b| b.is_ascii_hexdigit()) + { + return None; + } + let value = match u32::from_str_radix(component, 16) { + Ok(value) => value, + Err(error) => { + crate::logging::debug(&format!("Invalid terminal color component: {error}")); + return None; + } + }; + let max = (1u32 << (component.len() * 4)) - 1; + channels.push((value * u32::from(u16::MAX) / max) as u16); + } + let background = terminal_colorsaurus::Color::rgb(channels[0], channels[1], channels[2]); + Some(if background.perceived_lightness() > 0.5 { + ThemeMode::Light + } else { + ThemeMode::Dark + }) +} + +#[derive(Default)] +struct RuntimeTheme { + reports: ColorReports, + last_query: Option, + cache_query_at: Option, +} + +fn runtime() -> &'static Mutex { + static RUNTIME: OnceLock> = OnceLock::new(); + RUNTIME.get_or_init(|| Mutex::new(RuntimeTheme::default())) +} + +fn background_query(passthrough: bool) -> &'static [u8] { + if passthrough { + // tmux caches ordinary OSC 11 replies at attachment. Ask the outer + // terminal directly, doubling ESC bytes inside its DCS envelope. + b"\x1bPtmux;\x1b\x1b]11;?\x1b\x1b\\\x1b\\" + } else { + b"\x1b]11;?\x1b\\" + } +} + +fn tmux_passthrough_enabled() -> bool { + static ENABLED: OnceLock = OnceLock::new(); + *ENABLED.get_or_init(|| { + let term = std::env::var_os("TERM") + .map_or_else(String::new, |value| value.to_string_lossy().into_owned()); + if std::env::var_os("TMUX").is_none() + || !(term.starts_with("tmux") || term.starts_with("screen")) + { + return false; + } + use std::process::{Command, Stdio}; + let Ok(mut child) = Command::new("tmux") + .args(["show-options", "-Apv", "allow-passthrough"]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + else { + return false; + }; + // Only once per TUI, bounded so a broken/missing mux cannot stall input. + let deadline = Instant::now() + Duration::from_millis(150); + loop { + match child.try_wait() { + Ok(Some(status)) if status.success() => { + return child + .wait_with_output() + .is_ok_and(|output| matches!(output.stdout.trim_ascii(), b"on" | b"all")); + } + Ok(Some(_)) => return false, + Ok(None) if Instant::now() < deadline => { + std::thread::sleep(Duration::from_millis(2)); + } + _ => { + if let Err(error) = child.kill() { + crate::logging::debug(&format!("Failed to stop tmux probe: {error}")); + } + if let Err(error) = child.wait() { + crate::logging::debug(&format!("Failed to reap tmux probe: {error}")); + } + return false; + } + } + } + }) +} + +fn query_if_due(state: &mut RuntimeTheme, now: Instant, focused: bool) { + if !super::theme_detect::runtime_queries_enabled() { + return; + } + if state.cache_query_at.is_some_and(|at| now >= at) { + state.cache_query_at = None; + // tmux consumes outer-terminal color replies to update its cache. + // Read that refreshed cache on the next timer tick, without blocking + // the input stream or competing for stdin. + write_background_query(state, now, false); + return; + } + let interval = if focused { + Duration::from_secs(1) + } else { + QUERY_INTERVAL + }; + if state + .last_query + .is_some_and(|last| now.duration_since(last) < interval) + { + return; + } + state.last_query = Some(now); + let passthrough = tmux_passthrough_enabled(); + if passthrough { + state.cache_query_at = Some(now + PREFIX_TIMEOUT); + } + write_background_query(state, now, passthrough); +} + +fn write_background_query(state: &mut RuntimeTheme, now: Instant, passthrough: bool) { + // Query only the background. No stdin read and no blocking response wait. + let query = background_query(passthrough); + let mut stdout = io::stdout().lock(); + if stdout + .write_all(query) + .and_then(|()| stdout.flush()) + .is_ok() + { + state.reports.expecting_until = Some(now + REPORT_TIMEOUT); + } +} + +fn take_event(state: &mut RuntimeTheme) -> Option { + if let Some(mode) = state.reports.mode.take() + && super::theme_detect::auto_theme_enabled() + && mode != jcode_tui_style::theme_mode() + { + jcode_tui_style::set_theme_mode(mode); + THEME_REDRAW_PENDING.store(true, Ordering::Relaxed); + // Reuse the existing resize invalidation/redraw path without inventing + // a keypress or changing focus state. The actual dimensions stay intact. + if let Ok((width, height)) = crossterm::terminal::size() { + return Some(Event::Resize(width, height)); + } + } + state.reports.ready.pop_front() +} + +/// Nonblocking burst drain with the same report filter as [`EventStream`]. +/// Returning None means there is currently no user event, even if report bytes +/// were consumed. The async reader will deliver any remaining report/replay. +pub(super) fn try_read() -> io::Result> { + let mut state = runtime().lock().unwrap_or_else(|error| error.into_inner()); + if let Some(event) = take_event(&mut state) { + return Ok(Some(event)); + } + for _ in 0..64 { + if !crossterm::event::poll(Duration::ZERO)? { + return Ok(None); + } + let event = crossterm::event::read()?; + let now = Instant::now(); + if event == Event::FocusGained { + query_if_due(&mut state, now, true); + } + state.reports.push(event, now); + if let Some(event) = take_event(&mut state) { + return Ok(Some(event)); + } + } + Ok(None) +} + +/// Crossterm's event stream, with nonblocking periodic OSC 11 queries and report +/// filtering. The timer is polled even during LLM output and tool execution. +pub(super) struct EventStream { + inner: crossterm::event::EventStream, + tick: Pin>, +} + +impl EventStream { + pub(super) fn new() -> Self { + Self { + inner: crossterm::event::EventStream::new(), + tick: Box::pin(tokio::time::sleep(Duration::ZERO)), + } + } +} + +impl Stream for EventStream { + type Item = io::Result; + + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let mut state = runtime().lock().unwrap_or_else(|error| error.into_inner()); + // Poll the timer first so a continuously-ready stream of input cannot + // starve a theme refresh. It only writes a tiny query every five seconds. + if self.tick.as_mut().poll(cx).is_ready() { + let now = Instant::now(); + state.reports.expire(now); + query_if_due(&mut state, now, false); + self.tick + .as_mut() + .reset(tokio::time::Instant::now() + PREFIX_TIMEOUT); + if self.tick.as_mut().poll(cx).is_ready() { + cx.waker().wake_by_ref(); + } + } + if let Some(event) = take_event(&mut state) { + return Poll::Ready(Some(Ok(event))); + } + // Bound the work per poll while draining the key-shaped color report. + for _ in 0..64 { + match Pin::new(&mut self.inner).poll_next(cx) { + Poll::Ready(Some(Ok(event))) => { + let now = Instant::now(); + if event == Event::FocusGained { + query_if_due(&mut state, now, true); + } + state.reports.push(event, now); + if let Some(event) = take_event(&mut state) { + return Poll::Ready(Some(Ok(event))); + } + } + other => return other, + } + } + cx.waker().wake_by_ref(); + Poll::Pending + } +} + +#[cfg(test)] +mod tests { + #[test] + fn background_queries_bypass_tmux_cache_only_with_passthrough() { + assert_eq!(super::background_query(false), b"\x1b]11;?\x1b\\"); + assert_eq!( + super::background_query(true), + b"\x1bPtmux;\x1b\x1b]11;?\x1b\x1b\\\x1b\\" + ); + } + use super::*; + use crossterm::event::KeyEvent; + + fn key(ch: char, modifiers: KeyModifiers) -> Event { + Event::Key(KeyEvent::new(KeyCode::Char(ch), modifiers)) + } + + fn report(filter: &mut ColorReports, body: &str, bel: bool, now: Instant) { + filter.push(key(']', KeyModifiers::ALT), now); + for ch in body.chars() { + filter.push(key(ch, KeyModifiers::NONE), now); + } + filter.push( + if bel { + key('g', KeyModifiers::CONTROL) + } else { + key('\\', KeyModifiers::ALT) + }, + now, + ); + } + + #[test] + fn osc_reports_switch_both_directions_without_becoming_input() { + let mut filter = ColorReports::default(); + let now = Instant::now(); + report(&mut filter, "11;rgb:ffff/ffff/ffff", false, now); + assert_eq!(filter.mode.take(), Some(ThemeMode::Light)); + assert!(filter.ready.is_empty()); + report(&mut filter, "11;rgb:00/00/00", true, now); + assert_eq!(filter.mode.take(), Some(ThemeMode::Dark)); + assert!(filter.ready.is_empty()); + } + + #[test] + fn ordinary_alt_chords_and_pastes_survive() { + let mut filter = ColorReports::default(); + let now = Instant::now(); + let events = [ + key(']', KeyModifiers::ALT), + key('x', KeyModifiers::NONE), + Event::Paste("11;rgb:ff/ff/ff".into()), + ]; + for event in &events { + filter.push(event.clone(), now); + } + assert_eq!(filter.ready.into_iter().collect::>(), events); + assert_eq!(filter.mode, None); + } + + #[test] + fn missing_split_escape_recovers_without_losing_next_key() { + let now = Instant::now(); + let mut filter = ColorReports { + body: "11;rgb:".into(), + started: Some(now), + split_escape: true, + ..ColorReports::default() + }; + let input = key('x', KeyModifiers::NONE); + filter.push(input.clone(), now); + assert_eq!(filter.ready.into_iter().collect::>(), vec![input]); + assert_eq!(filter.mode, None); + } + + #[test] + fn fragmented_report_is_held_until_terminator() { + let mut filter = ColorReports::default(); + let now = Instant::now(); + filter.push(key(']', KeyModifiers::ALT), now); + for (i, ch) in "11;rgb:ffff/ffff/ffff".chars().enumerate() { + filter.push( + key(ch, KeyModifiers::NONE), + now + Duration::from_millis(i as u64 * 10), + ); + assert!(filter.ready.is_empty()); + assert_eq!(filter.mode, None); + } + filter.push( + key('\\', KeyModifiers::ALT), + now + Duration::from_millis(230), + ); + assert_eq!(filter.mode, Some(ThemeMode::Light)); + } + + #[test] + fn reassembles_split_escape_introducer_and_terminator() { + let now = Instant::now(); + let mut filter = ColorReports { + expecting_until: Some(now + REPORT_TIMEOUT), + ..ColorReports::default() + }; + let escape = Event::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); + filter.push(escape.clone(), now); + filter.push( + key(']', KeyModifiers::NONE), + now + Duration::from_millis(20), + ); + for ch in "11;rgb:ffff/ffff/ffff".chars() { + filter.push(key(ch, KeyModifiers::NONE), now + Duration::from_millis(40)); + } + filter.push(escape, now + Duration::from_millis(60)); + filter.push( + key('\\', KeyModifiers::NONE), + now + Duration::from_millis(80), + ); + assert_eq!(filter.mode, Some(ThemeMode::Light)); + assert!(filter.ready.is_empty()); + assert!(filter.expecting_until.is_none()); + } + + #[test] + fn genuine_escape_is_replayed_and_is_not_delayed_outside_a_query() { + let now = Instant::now(); + let escape = Event::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); + let mut filter = ColorReports::default(); + filter.push(escape.clone(), now); + assert_eq!(filter.ready.pop_front(), Some(escape.clone())); + filter.expecting_until = Some(now + REPORT_TIMEOUT); + filter.push(escape.clone(), now); + filter.expire(now + PREFIX_TIMEOUT); + assert_eq!(filter.ready.pop_front(), Some(escape.clone())); + filter.push(escape.clone(), now); + filter.push( + key('x', KeyModifiers::NONE), + now + Duration::from_millis(20), + ); + assert_eq!(filter.ready.pop_front(), Some(escape)); + assert_eq!(filter.ready.pop_front(), Some(key('x', KeyModifiers::NONE))); + } + + #[test] + fn timeout_replays_unrecognized_chord_but_drops_partial_report() { + let now = Instant::now(); + let mut filter = ColorReports::default(); + filter.push(key(']', KeyModifiers::ALT), now); + filter.expire(now + PREFIX_TIMEOUT); + assert_eq!(filter.ready.pop_front(), Some(key(']', KeyModifiers::ALT))); + filter.push(key(']', KeyModifiers::ALT), now); + for ch in "11;rgb:ff/".chars() { + filter.push(key(ch, KeyModifiers::NONE), now); + } + filter.expire(now + REPORT_TIMEOUT); + assert!(filter.ready.is_empty()); + filter.push(key('x', KeyModifiers::NONE), now + REPORT_TIMEOUT); + assert_eq!(filter.ready.pop_front(), Some(key('x', KeyModifiers::NONE))); + } + + #[test] + fn validates_color_components_and_ignores_alpha() { + assert_eq!( + parse_background_report("11;rgba:ffff/ffff/ffff/0000"), + Some(ThemeMode::Light) + ); + assert_eq!( + parse_background_report("11;rgb:f/f/f"), + Some(ThemeMode::Light) + ); + for body in [ + "10;rgb:ff/ff/ff", + "11;rgb:/ff/ff", + "11;rgb:fffff/ff/ff", + "11;rgb:gg/ff/ff", + "11;rgb:ff/ff", + "11;rgb:ff/ff/ff/ff", + ] { + assert_eq!(parse_background_report(body), None, "{body}"); + } + } +} + +#[cfg(all(test, unix))] +#[path = "terminal_events_pty_tests.rs"] +mod pty_tests; diff --git a/crates/jcode-tui/src/tui/terminal_events_pty_tests.rs b/crates/jcode-tui/src/tui/terminal_events_pty_tests.rs new file mode 100644 index 00000000000..8a824c0950b --- /dev/null +++ b/crates/jcode-tui/src/tui/terminal_events_pty_tests.rs @@ -0,0 +1,167 @@ +//! End-to-end PTY check, in a fresh process so crossterm's process-global +//! terminal reader cannot race other unit tests or inherit locked mutexes. + +use super::*; +use std::io::{Read, Write}; +use std::os::fd::{AsRawFd, FromRawFd}; +use std::os::unix::process::CommandExt; +use std::process::{Command, Stdio}; + +async fn next_event(events: &mut EventStream) -> Event { + use futures::StreamExt; + tokio::time::timeout(Duration::from_secs(10), events.next()) + .await + .unwrap() + .unwrap() + .unwrap() +} + +#[test] +fn pty_child() { + if std::env::var_os("JCODE_TEST_THEME_PTY").is_none() { + return; + } + crossterm::terminal::enable_raw_mode().unwrap(); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + runtime.block_on(async { + jcode_tui_style::set_theme_mode(ThemeMode::Dark); + let mut events = EventStream::new(); + assert!(matches!( + next_event(&mut events).await, + Event::Resize(80, 24) + )); + assert_eq!(jcode_tui_style::theme_mode(), ThemeMode::Light); + assert_eq!( + next_event(&mut events).await, + Event::Key(crossterm::event::KeyEvent::new( + KeyCode::Char('x'), + KeyModifiers::NONE + )) + ); + // The next reply is only sent when the periodic query appears, while + // the process is otherwise idle. No user message or redraw is needed. + assert!(matches!( + next_event(&mut events).await, + Event::Resize(80, 24) + )); + assert_eq!(jcode_tui_style::theme_mode(), ThemeMode::Dark); + // Terminal paste must remain ordinary input, even when it contains a + // string resembling a color report. + assert_eq!( + next_event(&mut events).await, + Event::Paste("11;rgb:ff/ff/ff".to_string()) + ); + }); + println!("PTY_THEME_OK"); +} + +#[test] +fn live_queries_and_fragmented_color_replies_work_through_crossterm() { + let mut master = -1; + let mut slave = -1; + let size = libc::winsize { + ws_row: 24, + ws_col: 80, + ws_xpixel: 0, + ws_ypixel: 0, + }; + // SAFETY: openpty initializes both descriptors, with no name or termios. + assert_eq!( + unsafe { + libc::openpty( + &mut master, + &mut slave, + std::ptr::null_mut(), + std::ptr::null(), + &size, + ) + }, + 0 + ); + // SAFETY: these descriptors are newly allocated and exclusively owned. + let mut master = unsafe { std::fs::File::from_raw_fd(master) }; + let slave = unsafe { std::fs::File::from_raw_fd(slave) }; + let mut command = Command::new(std::env::current_exe().unwrap()); + command + .args([ + "--exact", + "tui::terminal_events::pty_tests::pty_child", + "--nocapture", + ]) + .env("JCODE_TEST_THEME_PTY", "1") + .env("JCODE_THEME", "auto") + .env("TERM", "xterm-256color") + .stdin(Stdio::from(slave.try_clone().unwrap())) + .stdout(Stdio::from(slave.try_clone().unwrap())) + .stderr(Stdio::from(slave)); + // SAFETY: child-only controlling-terminal setup immediately before exec. + // No allocation, locks, or runtime activity occurs in this closure. + unsafe { + command.pre_exec(|| { + if libc::login_tty(libc::STDIN_FILENO) != 0 { + return Err(io::Error::last_os_error()); + } + Ok(()) + }); + } + let mut child = command.spawn().unwrap(); + let deadline = Instant::now() + Duration::from_secs(20); + let mut output = Vec::new(); + let mut query_count = 0; + let status = loop { + if let Some(status) = child.try_wait().unwrap() { + break status; + } + if Instant::now() >= deadline { + child.kill().unwrap(); + child.wait().unwrap(); + panic!("PTY test timed out: {}", String::from_utf8_lossy(&output)); + } + let mut fd = libc::pollfd { + fd: master.as_raw_fd(), + events: libc::POLLIN, + revents: 0, + }; + // SAFETY: fd describes the live master, the array length is one. + if unsafe { libc::poll(&mut fd, 1, 50) } <= 0 || fd.revents & libc::POLLIN == 0 { + continue; + } + let mut bytes = [0; 4096]; + match master.read(&mut bytes) { + Ok(0) => continue, + Ok(len) => output.extend_from_slice(&bytes[..len]), + Err(error) if error.raw_os_error() == Some(libc::EIO) => continue, + Err(error) => panic!("PTY read failed: {error}"), + } + let seen = output + .windows(b"\x1b]11;?\x1b\\".len()) + .filter(|window| *window == b"\x1b]11;?\x1b\\") + .count(); + if seen > query_count { + query_count = seen; + if seen == 1 { + // Split every byte, including immediately after ESC. This + // exercises crossterm's bare-Escape fallback on SSH fragments. + for byte in b"\x1b]11;rgb:ffff/ffff/ffff\x1b\\x" { + master.write_all(&[*byte]).unwrap(); + master.flush().unwrap(); + std::thread::sleep(Duration::from_millis(20)); + } + } else if seen == 2 { + master + .write_all(b"\x1b]11;rgb:0000/0000/0000\x07\x1b[200~11;rgb:ff/ff/ff\x1b[201~") + .unwrap(); + master.flush().unwrap(); + } + } + }; + assert!( + status.success(), + "PTY child failed: {}", + String::from_utf8_lossy(&output) + ); + assert!(query_count >= 2, "periodic query was not emitted"); +} diff --git a/crates/jcode-tui/src/tui/theme_detect.rs b/crates/jcode-tui/src/tui/theme_detect.rs index ece963d6567..c00e97d0666 100644 --- a/crates/jcode-tui/src/tui/theme_detect.rs +++ b/crates/jcode-tui/src/tui/theme_detect.rs @@ -1,6 +1,7 @@ //! Terminal light/dark theme detection. //! -//! Resolves the theme mode once per process, before the TUI enters raw mode: +//! Resolves the initial theme before the TUI enters raw mode. Auto mode is +//! refreshed by `terminal_events` during the session using the existing reader: //! //! 1. `JCODE_THEME=dark|light` env override (also accepts `auto`). //! 2. `display.theme` config: "dark", "light", or "auto"/empty. @@ -57,6 +58,10 @@ fn take_prewarmed_theme_mode() -> Option { /// the (potentially blocking, sub-second) terminal query and later calls are /// free. Must be called before entering raw mode / the alternate screen. pub fn init_theme_mode() -> ThemeMode { + if DETECTED.get().is_some() { + init_palette(); + return jcode_tui_style::theme_mode(); + } let mode = match take_prewarmed_theme_mode() { Some(prewarmed) => *DETECTED.get_or_init(|| prewarmed), None => *DETECTED.get_or_init(resolve_theme_mode), @@ -74,6 +79,10 @@ pub fn init_theme_mode() -> ThemeMode { /// input. Prefer the theme captured by the previous process and otherwise resolve /// configuration without querying the terminal. pub fn init_theme_mode_for_resume(inherited_theme: Option<&str>) -> ThemeMode { + if DETECTED.get().is_some() { + init_palette(); + return jcode_tui_style::theme_mode(); + } let inherited_theme = inherited_theme.and_then(|value| match value { "dark" => Some(ThemeMode::Dark), "light" => Some(ThemeMode::Light), @@ -125,13 +134,45 @@ fn resolve_theme_mode_without_terminal_query() -> ThemeMode { resolve_configured_theme(false) } -fn resolve_configured_theme(query_terminal: bool) -> ThemeMode { - let configured = std::env::var("JCODE_THEME") +fn configured_theme() -> String { + std::env::var("JCODE_THEME") .ok() .filter(|v| !v.trim().is_empty()) - .unwrap_or_else(|| crate::config::config().display.theme.clone()); + .unwrap_or_else(|| crate::config::config().display.theme.clone()) +} + +fn is_auto_theme(configured: &str) -> bool { + !matches!( + configured.trim().to_ascii_lowercase().as_str(), + "dark" | "light" + ) +} + +pub(super) fn auto_theme_enabled() -> bool { + is_auto_theme(&configured_theme()) +} + +pub(super) fn runtime_queries_enabled() -> bool { + use std::io::IsTerminal; + auto_theme_enabled() + && std::io::stdin().is_terminal() + && std::io::stdout().is_terminal() + && terminal_background_query_supported( + // These optional hints must be valid UTF-8 to match known terminal names. + std::env::var_os("TERM") + .as_deref() + .and_then(std::ffi::OsStr::to_str), + std::env::var_os("TERM_PROGRAM") + .as_deref() + .and_then(std::ffi::OsStr::to_str), + std::env::var_os("LC_TERMINAL") + .as_deref() + .and_then(std::ffi::OsStr::to_str), + ) +} - match configured.trim().to_ascii_lowercase().as_str() { +fn resolve_configured_theme(query_terminal: bool) -> ThemeMode { + match configured_theme().trim().to_ascii_lowercase().as_str() { "dark" => return ThemeMode::Dark, "light" => return ThemeMode::Light, "" | "auto" => {} @@ -269,10 +310,22 @@ fn cache_silent_terminal_at(path: Option<&std::path::Path>, identity: &str) { let Some(path) = path else { return; }; - if let Some(parent) = path.parent() { - let _ = std::fs::create_dir_all(parent); + if let Some(parent) = path.parent() + && let Err(error) = std::fs::create_dir_all(parent) + { + crate::logging::debug(&format!( + "Failed to create terminal cache directory: {error}" + )); + return; } - let existing = std::fs::read_to_string(path).unwrap_or_default(); + let existing = match std::fs::read_to_string(path) { + Ok(existing) => existing, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => String::new(), + Err(error) => { + crate::logging::debug(&format!("Failed to read terminal cache: {error}")); + return; + } + }; if existing.lines().any(|line| line == identity) { return; } @@ -289,7 +342,9 @@ fn cache_silent_terminal_at(path: Option<&std::path::Path>, identity: &str) { } out.push_str(identity); out.push('\n'); - let _ = std::fs::write(path, out); + if let Err(error) = std::fs::write(path, out) { + crate::logging::debug(&format!("Failed to save terminal cache: {error}")); + } } /// Reject terminal classes that cannot answer OSC 11 before entering the @@ -318,6 +373,16 @@ mod tests { terminal_background_query_supported, }; + #[test] + fn explicit_theme_overrides_disable_runtime_detection() { + for value in ["dark", "light", " LIGHT ", "Dark"] { + assert!(!super::is_auto_theme(value), "{value}"); + } + for value in ["", "auto", " AUTO ", "unknown"] { + assert!(super::is_auto_theme(value), "{value}"); + } + } + #[test] fn skips_terminals_without_osc_query_support() { for term in [None, Some(""), Some("dumb"), Some("linux"), Some("cons25")] { diff --git a/crates/jcode-tui/src/tui/ui.rs b/crates/jcode-tui/src/tui/ui.rs index 1871dab2343..c6a6f906f26 100644 --- a/crates/jcode-tui/src/tui/ui.rs +++ b/crates/jcode-tui/src/tui/ui.rs @@ -262,7 +262,64 @@ pub fn last_max_scroll() -> usize { } } +thread_local! { + static SECONDARY_RENDER: std::cell::Cell = const { std::cell::Cell::new(false) }; + static SECONDARY_FRAME: std::cell::RefCell>> = + const { std::cell::RefCell::new(None) }; + static SECONDARY_GEOMETRY: std::cell::Cell = + const { std::cell::Cell::new(SecondaryGeometry { max_scroll: 0, scroll: 0, total_lines: 0, viewport_height: 0 }) }; +} + +/// Transcript geometry a secondary render resolved. The live session's +/// globals stay untouched, so embedders read their own results from here. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(crate) struct SecondaryGeometry { + pub max_scroll: usize, + pub scroll: usize, + pub total_lines: usize, + pub viewport_height: usize, +} + +fn note_secondary_geometry(update: impl FnOnce(&mut SecondaryGeometry)) { + SECONDARY_GEOMETRY.with(|cell| { + let mut geometry = cell.get(); + update(&mut geometry); + cell.set(geometry); + }); +} + +/// True while drawing a non-live view (for example the resume preview). +/// Such renders must not publish layout, scroll, copy, or status state that +/// the live session's input handling reads back. +pub(crate) fn secondary_render_active() -> bool { + SECONDARY_RENDER.with(std::cell::Cell::get) +} + +/// Run `f` with secondary-render isolation enabled on this thread. +/// Returns the geometry the render resolved alongside `f`'s result. +pub(crate) fn with_secondary_render(f: impl FnOnce() -> R) -> (R, SecondaryGeometry) { + struct Reset(bool); + impl Drop for Reset { + fn drop(&mut self) { + SECONDARY_RENDER.with(|c| c.set(self.0)); + } + } + SECONDARY_GEOMETRY.with(|cell| cell.set(SecondaryGeometry::default())); + SECONDARY_FRAME.with(|slot| slot.borrow_mut().take()); + let _reset = Reset(SECONDARY_RENDER.with(|c| c.replace(true))); + let result = f(); + (result, SECONDARY_GEOMETRY.with(std::cell::Cell::get)) +} + +/// The prepared transcript the most recent secondary render drew. +pub(crate) fn take_secondary_chat_frame() -> Option> { + SECONDARY_FRAME.with(|slot| slot.borrow_mut().take()) +} + fn set_last_chat_scrollbar_visible(visible: bool) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_CHAT_SCROLLBAR_VISIBLE.with(|state| state.set(visible)); @@ -279,6 +336,9 @@ fn set_last_chat_scrollbar_visible(visible: bool) { /// instead of both the wide and narrow variants (which thrashes the prep caches /// on long transcripts during streaming). fn last_chat_scrollbar_visible() -> bool { + if secondary_render_active() { + return false; + } #[cfg(test)] { return TEST_LAST_CHAT_SCROLLBAR_VISIBLE.with(Cell::get); @@ -328,6 +388,10 @@ pub fn last_diff_pane_max_scroll() -> usize { } pub(crate) fn set_last_max_scroll(value: usize) { + if secondary_render_active() { + note_secondary_geometry(|g| g.max_scroll = value); + return; + } #[cfg(test)] { TEST_LAST_MAX_SCROLL.with(|cell| cell.set(value)); @@ -340,6 +404,9 @@ pub(crate) fn set_last_max_scroll(value: usize) { } pub(crate) fn set_pinned_pane_total_lines(value: usize) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_PINNED_PANE_TOTAL_LINES.with(|cell| cell.set(value)); @@ -352,6 +419,9 @@ pub(crate) fn set_pinned_pane_total_lines(value: usize) { } pub(crate) fn set_last_diff_pane_effective_scroll(value: usize) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_DIFF_PANE_EFFECTIVE_SCROLL.with(|cell| cell.set(value)); @@ -364,6 +434,9 @@ pub(crate) fn set_last_diff_pane_effective_scroll(value: usize) { } pub(crate) fn set_last_diff_pane_max_scroll(value: usize) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_DIFF_PANE_MAX_SCROLL.with(|cell| cell.set(value)); @@ -389,6 +462,10 @@ pub fn last_total_wrapped_lines() -> usize { } pub(crate) fn set_last_total_wrapped_lines(value: usize) { + if secondary_render_active() { + note_secondary_geometry(|g| g.total_lines = value); + return; + } #[cfg(test)] { TEST_LAST_TOTAL_WRAPPED_LINES.with(|cell| cell.set(value)); @@ -414,6 +491,10 @@ pub(crate) fn last_chat_viewport_height() -> usize { } pub(crate) fn set_last_chat_viewport_height(value: usize) { + if secondary_render_active() { + note_secondary_geometry(|g| g.viewport_height = value); + return; + } #[cfg(test)] { TEST_LAST_CHAT_VIEWPORT_HEIGHT.with(|cell| cell.set(value)); @@ -428,6 +509,9 @@ pub(crate) fn set_last_chat_viewport_height(value: usize) { /// The chat scroll offset the renderer actually used on the most recent frame /// (after clamping and after resolving any pending history anchor). pub fn last_resolved_chat_scroll() -> usize { + if secondary_render_active() { + return 0; + } #[cfg(test)] { return TEST_LAST_RESOLVED_CHAT_SCROLL.with(Cell::get); @@ -439,6 +523,10 @@ pub fn last_resolved_chat_scroll() -> usize { } pub(crate) fn set_last_resolved_chat_scroll(value: usize) { + if secondary_render_active() { + note_secondary_geometry(|g| g.scroll = value); + return; + } #[cfg(test)] { TEST_LAST_RESOLVED_CHAT_SCROLL.with(|cell| cell.set(value)); @@ -464,6 +552,9 @@ pub(crate) fn tail_catchup_active() -> bool { } pub(crate) fn set_tail_catchup_active(active: bool) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_TAIL_CATCHUP_ACTIVE.with(|cell| cell.set(active)); @@ -495,6 +586,9 @@ pub(crate) fn request_tail_follow_snap() { } pub(crate) fn take_tail_follow_snap_request() -> bool { + if secondary_render_active() { + return false; + } #[cfg(test)] { return TEST_TAIL_FOLLOW_SNAP_PENDING.with(|cell| cell.replace(false)); @@ -599,6 +693,9 @@ fn visible_expand_edit_badge_rect_state() -> &'static Mutex> { } pub(crate) fn set_visible_expand_edit_badge_rect(rect: Option) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_VISIBLE_EXPAND_EDIT_BADGE_RECT.with(|state| state.set(rect)); @@ -629,6 +726,9 @@ pub(crate) fn visible_expand_edit_badge_at(column: u16, row: u16) -> bool { } pub(crate) fn set_visible_expand_edit_badge(visible: bool, line: Option) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_VISIBLE_EXPAND_EDIT_BADGE.with(|state| state.set(visible)); @@ -682,6 +782,9 @@ pub(crate) fn visible_expand_edit_badge_line() -> Option { } fn set_visible_copy_targets(targets: Vec) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_VISIBLE_COPY_TARGETS.with(|state| { @@ -802,6 +905,9 @@ fn active_prompt_entry_animation(now_ms: u64) -> Option } fn record_prompt_viewport(visible_start: usize, visible_end: usize) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_PROMPT_VIEWPORT_STATE.with(|state| { @@ -832,6 +938,9 @@ fn update_prompt_entry_animation( visible_end: usize, now_ms: u64, ) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_PROMPT_VIEWPORT_STATE.with(|state| { @@ -1225,6 +1334,16 @@ impl BodyCacheState { if let Some(pos) = self.entries.iter().position(|entry| entry.key == key) { self.entries.remove(pos); } + // An entry that differs only by an older `messages_version` can never + // be an exact hit again, and the new entry is a better incremental + // base for the same layout. Keeping it only holds memory. + self.entries.retain(|entry| { + !(entry.key.messages_version < key.messages_version + && BodyCacheKey { + messages_version: key.messages_version, + ..entry.key.clone() + } == key) + }); self.entries.push_front(BodyCacheEntry { key, prepared, @@ -1382,6 +1501,12 @@ impl FullPrepCacheState { if let Some(pos) = self.entries.iter().position(|entry| entry.key == key) { self.entries.remove(pos); } + // A frame keyed on partial streaming text can never be hit again once + // the stream advances: text only grows, and the turn ending bumps + // `messages_version`. Holding them just pins multi-MB snapshots of the + // whole transcript (measured: 4 entries, 13.6 MB on one client). + self.entries + .retain(|entry| entry.key.streaming_text_len == 0); self.entries.push_front(FullPrepCacheEntry { key, prepared, @@ -1486,6 +1611,9 @@ fn last_status_area_state() -> &'static Mutex> { } pub(crate) fn record_status_area(area: Rect) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_STATUS_AREA.with(|snapshot| { @@ -1530,8 +1658,8 @@ pub(crate) use frame_metrics::{ set_frame_input_attribution, wall_clock_ms, }; pub(crate) use frame_metrics::{ - debug_draw_call_history, debug_flicker_frame_history, debug_slow_frame_history, - recent_flicker_copy_target_for_key, recent_flicker_ui_notice, + changed_cells_wanted, debug_draw_call_history, debug_flicker_frame_history, + debug_slow_frame_history, recent_flicker_copy_target_for_key, recent_flicker_ui_notice, }; #[cfg(test)] pub(crate) use smoothness::frame_from_buffer as smoothness_frame_from_buffer; @@ -1576,6 +1704,9 @@ pub fn record_layout_snapshot( diff_pane_area: Option, input_area: Option, ) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_LAYOUT.with(|snapshot| { @@ -1635,6 +1766,10 @@ fn terminal_clear_start(app: &dyn TuiState, prepared: &PreparedChatFrame) -> Opt /// Record the prepared transcript frame the renderer just drew. pub(crate) fn set_last_chat_frame(frame: Arc) { + if secondary_render_active() { + SECONDARY_FRAME.with(|slot| *slot.borrow_mut() = Some(frame)); + return; + } #[cfg(test)] { TEST_LAST_CHAT_FRAME.with(|slot| *slot.borrow_mut() = Some(frame)); @@ -1958,6 +2093,9 @@ fn copy_snapshot_for_pane(pane: crate::tui::CopySelectionPane) -> Option]) { + if secondary_render_active() { + return; + } // Only retain styled lines when they contain links. The common plain-text // pane path should not duplicate an entire rendered document every frame. let lines = lines @@ -2163,9 +2310,14 @@ pub(crate) fn line_left_margins_for_area(lines: &[Line<'static>], area_width: u1 lines .iter() .map(|line| { + let alignment = line.alignment.unwrap_or(Alignment::Left); + if alignment == Alignment::Left { + // Left-aligned lines never have a margin, so skip measuring them. + return 0; + } let used = line.width().min(area_width as usize) as u16; let total_margin = area_width.saturating_sub(used); - match line.alignment.unwrap_or(Alignment::Left) { + match alignment { Alignment::Left => 0, Alignment::Center => total_margin / 2, Alignment::Right => total_margin, @@ -2754,7 +2906,9 @@ pub(crate) fn debug_chat_image_regions_json() -> String { mod overflow; use overflow::transcript_definitely_overflows; pub fn draw(frame: &mut Frame, app: &dyn TuiState) { - record_idle_animation_area(None); + if !secondary_render_active() { + record_idle_animation_area(None); + } if let Some(frozen) = app.hibernation_frame() && frozen.area == frame.area() { @@ -2794,7 +2948,9 @@ pub fn draw(frame: &mut Frame, app: &dyn TuiState) { capture .render_order .push("frozen_hibernation_frame".to_string()); - visual_debug::record_frame(capture.build()); + if !secondary_render_active() { + visual_debug::record_frame(capture.build()); + } } return; } @@ -2816,21 +2972,27 @@ pub fn draw(frame: &mut Frame, app: &dyn TuiState) { adapt_buffer_for_emoji_preference(frame.buffer_mut()); // Classic Kitty placements must follow all text writes. Unlike Unicode // placeholders they survive scrolling, so finalize even an image-free frame. - // This also drains deferred cache-eviction deletions when no image remains. crate::tui::mermaid::finish_terminal_image_frame(frame.buffer_mut()); + if !secondary_render_active() { + crate::tui::cloud_latency::rendered(); + } } fn draw_inner(frame: &mut Frame, app: &dyn TuiState) { - panel_image_preview::clear_regions(); + let secondary = secondary_render_active(); + if !secondary { + panel_image_preview::clear_regions(); + } let area = frame.area().intersection(*frame.buffer_mut().area()); if area.width == 0 || area.height == 0 { return; } let total_start = Instant::now(); - reset_frame_perf_stats(); - begin_frame_resource_sample(); - - clear_copy_viewport_snapshot(); + if !secondary { + reset_frame_perf_stats(); + begin_frame_resource_sample(); + clear_copy_viewport_snapshot(); + } // Clear full frame to prevent stale cells from prior layouts. // This is critical on macOS terminals where ratatui's diff-based updates @@ -3843,7 +4005,9 @@ fn draw_inner(frame: &mut Frame, app: &dyn TuiState) { capture.side_panel = crate::tui::side_panel_debug_json(); capture.markdown = crate::tui::markdown::debug_stats_json(); capture.theme = overlays::debug_palette_json(); - visual_debug::record_frame(capture.build()); + if !secondary_render_active() { + visual_debug::record_frame(capture.build()); + } } finalize_frame_metrics( diff --git a/crates/jcode-tui/src/tui/ui/display_width.rs b/crates/jcode-tui/src/tui/ui/display_width.rs index 663958304ce..93581eb8ff5 100644 --- a/crates/jcode-tui/src/tui/ui/display_width.rs +++ b/crates/jcode-tui/src/tui/ui/display_width.rs @@ -1,4 +1,7 @@ pub(super) fn line_display_width(text: &str) -> usize { + if text.bytes().all(|b| (0x20..0x7f).contains(&b)) { + return text.len(); + } unicode_width::UnicodeWidthStr::width(text) } diff --git a/crates/jcode-tui/src/tui/ui_animations.rs b/crates/jcode-tui/src/tui/ui_animations.rs index 94fbec4b18e..a22336cabf9 100644 --- a/crates/jcode-tui/src/tui/ui_animations.rs +++ b/crates/jcode-tui/src/tui/ui_animations.rs @@ -291,7 +291,9 @@ pub(crate) fn idle_donut_reserved_height(show_donut: bool, input_height: u16) -> pub(super) fn draw_idle_animation(frame: &mut Frame, app: &dyn TuiState, area: Rect) { // Remember the animated rows so the run loop can repaint only them between // full frames. Sizes the animation rejects are recorded as "no animation". - record_idle_animation_area((area.width >= 4 && area.height >= 2).then_some(area)); + if !super::secondary_render_active() { + record_idle_animation_area((area.width >= 4 && area.height >= 2).then_some(area)); + } render_idle_animation(frame.buffer_mut(), area, app.animation_elapsed()); } diff --git a/crates/jcode-tui/src/tui/ui_frame_metrics.rs b/crates/jcode-tui/src/tui/ui_frame_metrics.rs index 7901dc0cbc1..00ae7c44ec7 100644 --- a/crates/jcode-tui/src/tui/ui_frame_metrics.rs +++ b/crates/jcode-tui/src/tui/ui_frame_metrics.rs @@ -265,7 +265,18 @@ fn percentile(sorted: &[f64], pct: f64) -> f64 { /// streaming render cost (issue #392): how long the render-into-buffer pass /// takes, how much of the terminal actually changed per frame, and the /// effective draw rate. +static CHANGED_CELLS_WANTED: std::sync::atomic::AtomicBool = + std::sync::atomic::AtomicBool::new(false); + +/// Whether per-frame changed-cell counting is wanted. Counting compares the +/// whole screen every frame (a few % of release CPU), and only the +/// `draw-stats` debug command reads it, so it turns on at first request. +pub(crate) fn changed_cells_wanted() -> bool { + CHANGED_CELLS_WANTED.load(std::sync::atomic::Ordering::Relaxed) +} + pub(crate) fn debug_draw_call_history(limit: usize) -> serde_json::Value { + CHANGED_CELLS_WANTED.store(true, std::sync::atomic::Ordering::Relaxed); let history = draw_call_history() .lock() .unwrap_or_else(|poisoned| poisoned.into_inner()); @@ -961,25 +972,29 @@ fn process_rss_mb() -> Option { None } -#[cfg(target_os = "linux")] +/// Process CPU time (user + system) in microseconds. One `getrusage` syscall +/// per call instead of opening and parsing `/proc/self/stat` every frame. +#[cfg(unix)] fn process_cpu_ticks() -> Option { - let contents = std::fs::read_to_string("/proc/self/stat").ok()?; - let after_comm = contents.rsplit_once(") ")?.1; - let fields: Vec<&str> = after_comm.split_whitespace().collect(); - let user_ticks: u64 = fields.get(11)?.parse().ok()?; - let system_ticks: u64 = fields.get(12)?.parse().ok()?; - Some(user_ticks.saturating_add(system_ticks)) + let mut usage = std::mem::MaybeUninit::::uninit(); + // SAFETY: getrusage fully initialises `usage` when it returns 0. + if unsafe { libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) } != 0 { + return None; + } + let usage = unsafe { usage.assume_init() }; + let micros = |tv: libc::timeval| (tv.tv_sec as u64) * 1_000_000 + tv.tv_usec as u64; + Some(micros(usage.ru_utime).saturating_add(micros(usage.ru_stime))) } -#[cfg(not(target_os = "linux"))] +#[cfg(not(unix))] fn process_cpu_ticks() -> Option { None } #[cfg(unix)] fn clock_ticks_per_second() -> Option { - let ticks = unsafe { libc::sysconf(libc::_SC_CLK_TCK) }; - if ticks > 0 { Some(ticks as f64) } else { None } + // `process_cpu_ticks` reports microseconds. + Some(1_000_000.0) } #[cfg(not(unix))] @@ -1103,6 +1118,9 @@ pub(super) fn finalize_frame_metrics( draw_elapsed: Duration, messages_ms: Option, ) { + if super::secondary_render_active() { + return; + } if profile_enabled() { record_profile(prep_elapsed, draw_elapsed, total_start.elapsed()); } diff --git a/crates/jcode-tui/src/tui/ui_header.rs b/crates/jcode-tui/src/tui/ui_header.rs index 8e05fe26233..eb0f683742d 100644 --- a/crates/jcode-tui/src/tui/ui_header.rs +++ b/crates/jcode-tui/src/tui/ui_header.rs @@ -27,6 +27,7 @@ fn fit_header_lines(lines: Vec>, width: u16) -> Vec> .into(); } } + line = account_router::fit_account_row(line, budget); let mut fitted = jcode_tui_render::truncate_line_with_ellipsis_to_width(&line, budget); fitted.style = line.style; fitted.alignment = line.alignment; @@ -123,6 +124,7 @@ fn standalone_version_line() -> Line<'static> { /// The persisted subset of the real header. Credentials, transport and binary /// versions are deliberately omitted when the selected session does not own them. +#[cfg(test)] pub(crate) fn build_preview_header( session: &crate::tui::session_picker::SessionInfo, width: u16, diff --git a/crates/jcode-tui/src/tui/ui_header/account_router.rs b/crates/jcode-tui/src/tui/ui_header/account_router.rs index 8019fca2075..dd52694f1a9 100644 --- a/crates/jcode-tui/src/tui/ui_header/account_router.rs +++ b/crates/jcode-tui/src/tui/ui_header/account_router.rs @@ -189,11 +189,10 @@ fn remote_inventory_lines( format!("{name:<9} {}", masked_identity(account.email.as_deref())), is_current, ); - row.spans - .push(Span::styled(" · ", Style::default().fg(dim_color()))); - if let Some(quota) = remaining_bar(usage, display_used) { - row.spans.extend(quota.spans); - } else { + let quotas = remaining_bars(usage, display_used); + if quotas.is_empty() || usage.error.is_some() { + row.spans + .push(Span::styled(" · ", Style::default().fg(dim_color()))); row.spans.push(Span::styled( usage_status_label(Some(usage)), Style::default().fg(dim_color()), @@ -212,7 +211,7 @@ fn remote_inventory_lines( .push(Span::styled(" · ", Style::default().fg(dim_color()))); row.spans.push(banked); } - lines.push(row); + lines.push(inline_account_row(row, quotas)); } } lines @@ -228,6 +227,7 @@ fn inventory_lines( use crate::auth::{ActiveCredential, resolve_dual_credential_auth}; use jcode_provider_core::ActiveProvider; let mut entries = Vec::new(); + let mut quota_rows = std::collections::BTreeMap::new(); let runtime = std::env::var("JCODE_RUNTIME_PROVIDER").ok(); let current_provider = jcode_provider_core::parse_provider_hint(provider_name); // Preserve the stored login order for defaulted members. Explicit pool @@ -264,32 +264,34 @@ fn inventory_lines( && credential == Some(ActiveCredential::OAuth); let usage = crate::usage::cached_account_usage_status_snapshot(usage_provider, &account.label); - let quota = usage.as_ref().and_then(|usage| { - remaining_bar(usage, crate::config::config().display.usage_display_used()) - }); + let quotas = match usage.as_ref() { + Some(usage) => { + remaining_bars(usage, crate::config::config().display.usage_display_used()) + } + // Accounts without cached usage have no quota rows yet. + None => Vec::new(), + }; let mut row = detail( oauth_credential_status(expired, usage.as_ref()), format!("{name:<9} {}", masked_identity(account.email.as_deref())), is_current, ); - row.spans - .push(Span::styled(" · ", Style::default().fg(dim_color()))); - if let Some(quota) = quota { - row.spans.extend(quota.spans); - } else { + if quotas.is_empty() || usage.as_ref().is_some_and(|usage| usage.error.is_some()) { + row.spans + .push(Span::styled(" · ", Style::default().fg(dim_color()))); row.spans.push(Span::styled( usage_status_label(usage.as_ref()), Style::default().fg(dim_color()), )); } - if let Some(banked) = - header_account_banked_resets(usage_provider, &account.label) - .and_then(|banked| banked_resets_span(&banked)) + if let Some(banked) = header_account_banked_resets(usage_provider, &account.label) + .and_then(|banked| banked_resets_span(&banked)) { row.spans .push(Span::styled(" · ", Style::default().fg(dim_color()))); row.spans.push(banked); } + quota_rows.insert(account_key(account.provider, Some(&account.label)), quotas); entries.push(( account_key(account.provider, Some(&account.label)), true, @@ -367,7 +369,11 @@ fn inventory_lines( .iter() .find(|(candidate, _, _)| candidate == key) .map(|(_, _, line)| { - priority_row(line.clone(), inventory.pool.priority(key), &tiers) + let row = priority_row(line.clone(), inventory.pool.priority(key), &tiers); + match quota_rows.remove(key) { + Some(quotas) => inline_account_row(row, quotas), + None => row, + } }) }) .collect(); @@ -379,6 +385,104 @@ fn inventory_lines( lines } +const QUOTA_SEPARATOR: &str = " "; + +fn inline_account_row(mut identity: Line<'static>, quotas: Vec>) -> Line<'static> { + if quotas.is_empty() { + return identity; + } + // Keep status/error and banked-reset telemetry after the quotas, so fitting + // cannot sacrifice a model window just to retain a countdown. + let suffix = identity.spans.split_off(3); + for quota in quotas { + identity.spans.push(Span::styled( + QUOTA_SEPARATOR, + Style::default().fg(dim_color()), + )); + identity.spans.extend(quota.spans); + } + identity.spans.extend(suffix); + identity +} + +/// Account rows remain one logical row at every width. Drop decoration before +/// quota labels/percentages, then truncate by terminal cells, never bytes. +pub(super) fn fit_account_row(mut row: Line<'static>, budget: usize) -> Line<'static> { + if row.width() <= budget + || row + .spans + .get(3) + .is_none_or(|s| s.content != QUOTA_SEPARATOR) + { + return row; + } + let mut end = 3; + while row + .spans + .get(end) + .is_some_and(|s| s.content == QUOTA_SEPARATOR) + && row.spans.len() >= end + 5 + { + end += 5; + } + let suffix = row.spans.split_off(end); + let original_identity = row.spans[2].clone(); + // Prefer a short identity over losing any limit, retaining the provider. + let provider = original_identity + .content + .split_whitespace() + .next() + .unwrap_or(""); + row.spans[2].content = format!(" {provider}").into(); + if row.width() > budget { + for quota in row.spans[3..].as_chunks_mut::<5>().0 { + // These are the shared renderer's filled and empty spans. Preserve + // its colors and convention, shrinking only their cell counts. + for span in &mut quota[2..4] { + span.content = span.content.chars().take(1).collect::().into(); + } + } + } + if row.width() > budget { + for (index, quota) in row.spans[3..].as_chunks_mut::<5>().0.iter_mut().enumerate() { + quota[0].content = " ".into(); + quota[2].content = "".into(); + quota[3].content = "".into(); + // One explicit convention applies to the whole compact row. + if index == 0 { + continue; + } + let percent = quota[4] + .content + .trim() + .split(' ') + .next() + .unwrap_or("") + .to_owned(); + quota[4].content = percent.into(); + } + } + // Restore as much identity as the quotas permit, using cell-aware ellipsis. + let identity_budget = budget.saturating_sub(row.width() - row.spans[2].width()); + let fitted_identity = jcode_tui_render::truncate_line_with_ellipsis_to_width( + &Line::from(vec![original_identity.clone()]), + identity_budget, + ); + row.spans[2] = Span::styled( + fitted_identity + .spans + .iter() + .map(|s| s.content.as_ref()) + .collect::(), + original_identity.style, + ); + let with_suffix = Line::from(row.spans.iter().cloned().chain(suffix).collect::>()); + if with_suffix.width() <= budget { + row = with_suffix; + } + jcode_tui_render::truncate_line_with_ellipsis_to_width(&row, budget) +} + fn priority_row(mut row: Line<'static>, priority: u32, tiers: &[u32]) -> Line<'static> { let rank = tiers.binary_search(&priority).unwrap_or(0); let dimming = if tiers.len() < 2 { @@ -418,7 +522,8 @@ fn header_account_banked_resets( /// Warmer colors mean less time to spend a reset. Unknown expiry stays blue. fn banked_expiry_color(expires_at: Option<&str>, now: chrono::DateTime) -> Color { - let Some(expiry) = expires_at.and_then(|at| chrono::DateTime::parse_from_rfc3339(at).ok()) else { + let Some(expiry) = expires_at.and_then(|at| chrono::DateTime::parse_from_rfc3339(at).ok()) + else { return Color::Rgb(120, 190, 255); }; let remaining = expiry.signed_duration_since(now); @@ -463,62 +568,97 @@ fn banked_resets_span(banked: &crate::usage::AccountBankedResets) -> Option) -> &'static str { +fn usage_status_label(usage: Option<&crate::usage::AccountUsageSnapshot>) -> String { match usage { - None => "limits pending", + None => "limits pending".into(), Some(usage) => match usage.error.as_deref() { - Some(error) if error.contains("429") => "usage fetch failed (429)", - Some(error) if error.contains("401") => "usage fetch needs login", - Some(_) => "usage fetch failed", - None => "limits not reported", + Some(error) if error.contains("429") => { + match crate::usage::usage_error_retry_at(error).map(crate::usage::format_reset_time) + { + Some(wait) if wait != "now" => format!("usage throttled, retry in {wait}"), + _ => "usage throttled, retrying soon".into(), + } + } + Some(error) if error.contains("401") => "usage fetch needs login".into(), + Some(_) => "usage fetch failed".into(), + None => "limits not reported".into(), }, } } -fn remaining_bar( +/// Render every reported quota through the info widget's shared limit renderer. +fn remaining_bars( usage: &crate::usage::AccountUsageSnapshot, display_used: bool, -) -> Option> { - let (used, window) = match usage.five_hour_ratio { - Some(used) => (used, usage.primary_label.as_deref().unwrap_or("5h")), - None => ( - usage.seven_day_ratio?, +) -> Vec> { + let windows = [ + ( + usage.five_hour_ratio, + usage.primary_label.as_deref().unwrap_or("5h"), + ), + ( + usage.seven_day_ratio, usage.secondary_label.as_deref().unwrap_or("7d"), ), - }; - if !used.is_finite() || !(0.0..=1.0).contains(&used) { - return None; - } - let label = match window { - "5-hour" | "5-hour window" => "5h", - "7-day" | "7-day window" => "7d", - label => label, - }; - let used_pct = (used * 100.0).round() as u8; - let mut bar = crate::tui::info_widget::render_usage_limit_bar( - label, - used_pct, - 100u8.saturating_sub(used_pct), - None, - 22, - display_used, + ] + .into_iter() + .filter_map(|(ratio, label)| ratio.map(|ratio| (ratio, label))) + .chain( + usage + .additional_windows + .iter() + .map(|window| (window.usage_ratio, window.label.as_str())), ); - // Inline quotas do not need the info widget's fixed-width label column. - if let Some(label) = bar.spans.first_mut() { - label.content = format!( - "{}{} ", - label.content.trim_end(), - if usage.error.is_some() { " stale" } else { "" }, - ) - .into(); + let mut rows = Vec::new(); + for (used, window) in windows { + if !used.is_finite() || !(0.0..=1.0).contains(&used) { + continue; + } + let label = match window { + "5-hour" | "5-hour window" => "5h", + "7-day" | "7-day window" => "7d", + label => label, + }; + let used_pct = (used * 100.0).round() as u8; + let mut bar = crate::tui::info_widget::render_usage_limit_bar( + label, + used_pct, + 100u8.saturating_sub(used_pct), + None, + 22, + display_used, + ); + // Inline quotas do not need the info widget's fixed-width label column. + if let Some(label_span) = bar.spans.first_mut() { + label_span.content = format!( + "{}{} ", + label, + if usage.error.is_some() { " stale" } else { "" } + ) + .into(); + } + rows.push(bar); } + rows +} + +#[cfg(test)] +fn remaining_bar( + usage: &crate::usage::AccountUsageSnapshot, + display_used: bool, +) -> Option> { + let rows = remaining_bars(usage, display_used); + if rows.is_empty() { + return None; + } + let mut spans: Vec<_> = rows.into_iter().flat_map(|row| row.spans).collect(); if usage.error.is_some() { - bar.spans.push(Span::styled( + spans.push(Span::styled( format!(" · {}", usage_status_label(Some(usage))), Style::default().fg(dim_color()), )); } - Some(bar) + Some(Line::from(spans)) } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -574,6 +714,12 @@ fn oauth_credential_status( if [usage.five_hour_ratio, usage.seven_day_ratio] .into_iter() .flatten() + .chain( + usage + .additional_windows + .iter() + .map(|window| window.usage_ratio), + ) .any(|ratio| ratio.is_finite() && (0.0..=1.0).contains(&ratio)) { CredentialStatus::Available @@ -620,6 +766,7 @@ mod tests { #[test] fn remote_rows_mask_email_and_show_quota() { let account = crate::usage::AccountUsageSnapshot { + additional_windows: Vec::new(), label: "work".into(), email: Some("jeremy@example.com".into()), exhausted: false, @@ -642,6 +789,144 @@ mod tests { assert!(out.contains("85%"), "{out}"); } + #[test] + fn inline_account_rows_prioritize_all_limits_at_80_cells() { + let usage = crate::usage::AccountUsageSnapshot { + label: "work".into(), + email: Some("界界界@example.com".into()), + exhausted: false, + primary_label: Some("5-hour".into()), + five_hour_ratio: Some(0.15), + secondary_label: Some("7-day".into()), + seven_day_ratio: Some(0.42), + additional_windows: vec![crate::protocol::AccountUsageWindow { + label: "Fable 7d".into(), + usage_ratio: 0.73, + resets_at: None, + }], + resets_at: None, + error: None, + }; + for display_used in [false, true] { + let mut identity = detail( + CredentialStatus::Available, + "anthropic 界***界@example.com".into(), + true, + ); + identity + .spans + .push(Span::raw(" · ↻ 2 resets · exp tomorrow")); + let row = inline_account_row(identity, remaining_bars(&usage, display_used)); + for budget in [79, 119, 239] { + let fitted = fit_account_row(row.clone(), budget); + assert!(fitted.width() <= budget); + let rendered = text(vec![fitted]); + for label in ["5h", "7d", "Fable 7d"] { + assert!(rendered.contains(label), "{rendered}"); + } + for percent in if display_used { + ["15%", "42%", "73%"] + } else { + ["85%", "58%", "27%"] + } { + assert!(rendered.contains(percent), "{rendered}"); + } + } + let narrow = fit_account_row(row.clone(), 39); + assert!(narrow.width() <= 39); + assert!(text(vec![narrow]).contains('…')); + assert!(text(vec![fit_account_row(row, 239)]).contains("2 resets")); + } + } + + #[test] + fn account_headers_render_all_usage_windows_and_fit_unicode_widths() { + let mut usage = crate::usage::AccountUsageSnapshot { + label: "work".into(), + email: Some("界界@example.com".into()), + exhausted: false, + primary_label: Some("session".into()), + five_hour_ratio: Some(0.15), + secondary_label: Some("7-day".into()), + seven_day_ratio: Some(0.42), + additional_windows: vec![ + crate::protocol::AccountUsageWindow { + label: "Fable 7d".into(), + usage_ratio: 0.73, + resets_at: None, + }, + crate::protocol::AccountUsageWindow { + label: "Spark 界".into(), + usage_ratio: 0.91, + resets_at: None, + }, + crate::protocol::AccountUsageWindow { + label: "future model / month".into(), + usage_ratio: 0.32, + resets_at: None, + }, + ], + resets_at: None, + error: None, + }; + for display_used in [false, true] { + let rendered = text(vec![remaining_bar(&usage, display_used).unwrap()]); + for label in [ + "session", + "7d", + "Fable 7d", + "Spark 界", + "future model / month", + ] { + assert!(rendered.contains(label), "{rendered}"); + } + for percent in if display_used { + [15, 42, 73, 91, 32] + } else { + [85, 58, 27, 9, 68] + } { + assert!(rendered.contains(&format!("{percent}%")), "{rendered}"); + } + } + for (anthropic, openai, provider) in [ + (vec![], vec![usage.clone()], "openai"), + (vec![usage.clone()], vec![], "anthropic"), + ] { + let rows = remote_inventory_lines(&anthropic, &openai, provider, Some("work")); + assert_eq!( + rows.len(), + 1, + "identity and all quotas share one logical row" + ); + for width in [0u16, 1, 2, 8, 24, 40, 80, 120, 240] { + let fitted = fit_header_lines(rows.clone(), width); + assert_eq!(fitted.len(), rows.len()); + for result in &fitted { + assert!(result.width() <= usize::from(width.saturating_sub(1))); + } + if width >= 120 { + let rendered = text(fitted); + for label in [ + "session", + "7d", + "Fable 7d", + "Spark 界", + "future model / month", + ] { + assert!(rendered.contains(label), "width={width}: {rendered}"); + } + } + } + } + // One malformed window must not hide the valid windows that follow it. + usage.five_hour_ratio = Some(f32::NAN); + usage.seven_day_ratio = None; + let rendered = text(vec![remaining_bar(&usage, false).unwrap()]); + assert!(!rendered.contains("session")); + assert!(rendered.contains("Fable 7d")); + assert!(rendered.contains("future model / month")); + } + fn auth() -> AuthStatus { AuthStatus { anthropic: ProviderAuth { @@ -821,6 +1106,7 @@ mod tests { #[test] fn router_priorities_only_dim_original_identity_colors_and_fit_narrow_headers() { let usage = crate::usage::AccountUsageSnapshot { + additional_windows: Vec::new(), label: "one".into(), email: None, exhausted: false, @@ -864,6 +1150,7 @@ mod tests { #[test] fn router_inventory_usability_requires_account_scoped_evidence() { let mut usage = crate::usage::AccountUsageSnapshot { + additional_windows: Vec::new(), label: "one".into(), email: None, exhausted: false, @@ -993,6 +1280,7 @@ mod tests { fn router_inventory_missing_usage_has_explicit_safe_status() { assert_eq!(usage_status_label(None), "limits pending"); let mut usage = crate::usage::AccountUsageSnapshot { + additional_windows: Vec::new(), label: "test".into(), email: None, exhausted: false, @@ -1007,11 +1295,11 @@ mod tests { for (error, expected) in [ ( "Usage temporarily rate limited (429)", - "usage fetch failed (429)", + "usage throttled, retrying soon", ), ( "Couldn't fetch usage (429: lookup throttled)", - "usage fetch failed (429)", + "usage throttled, retrying soon", ), ( "Usage authentication failed (401)", @@ -1093,6 +1381,7 @@ mod tests { #[test] fn router_inventory_weekly_only_usage_shows_reported_window() { let mut usage = crate::usage::AccountUsageSnapshot { + additional_windows: Vec::new(), label: "weekly-only".into(), email: None, exhausted: false, @@ -1123,6 +1412,7 @@ mod tests { #[test] fn router_inventory_remaining_bar_rejects_unknown_and_invalid_usage() { let mut usage = crate::usage::AccountUsageSnapshot { + additional_windows: Vec::new(), label: "one".into(), email: None, exhausted: false, @@ -1151,7 +1441,11 @@ mod tests { let stale = text(vec![remaining_bar(&usage, false).unwrap()]); assert!(stale.contains("100% left")); assert!(stale.contains("5h stale")); - assert!(stale.contains("usage fetch failed (429)")); + assert!(stale.contains("usage throttled, retrying soon")); + usage.error = Some("HTTP 429 (retry at 2099-01-01T00:00:00Z)".into()); + let stale = text(vec![remaining_bar(&usage, false).unwrap()]); + assert!(stale.contains("usage throttled, retry in "), "{stale}"); + usage.error = Some("HTTP 429".into()); assert_eq!( oauth_credential_status(false, Some(&usage)), CredentialStatus::Unknown diff --git a/crates/jcode-tui/src/tui/ui_input.rs b/crates/jcode-tui/src/tui/ui_input.rs index 2c71058a20f..a60afb0a59c 100644 --- a/crates/jcode-tui/src/tui/ui_input.rs +++ b/crates/jcode-tui/src/tui/ui_input.rs @@ -14,6 +14,7 @@ use crate::message::ConnectionPhase; use crate::tui::app; use crate::tui::color_support::rgb; use crate::tui::detect_kv_cache_problem; +use crate::tui::info_widget::occasional_status_tip; use crate::tui::layout_utils; use crate::tui::session_facts; use ratatui::{prelude::*, style::Modifier, widgets::Paragraph}; @@ -287,7 +288,7 @@ pub(crate) fn dim_command_color(color: Option) -> Color { } pub(super) fn input_hint_line_height(app: &dyn TuiState) -> u16 { - if app.login_input_hint().is_some() { + if app.login_input_hint().is_some() || app.voice_composer_preview().is_some() { return 1; } // Command suggestions render as an overlay (see @@ -351,7 +352,8 @@ pub(super) fn wrapped_input_line_count( let num_str = next_prompt.to_string(); let (prompt_char, caret_color) = input_prompt(app); - let (lines, _, _) = wrap_input_text_with_images( + let preview = app.voice_composer_preview(); + let (lines, _, _) = wrap_input_text_with_ghost( app.input(), app.cursor_pos(), line_width, @@ -360,6 +362,7 @@ pub(super) fn wrapped_input_line_count( caret_color, prompt_len, &|number| app.input_image_attached(number), + voice_ghost(preview.as_ref()), ); lines.len().max(1) } @@ -671,10 +674,7 @@ fn append_batch_progress_spans( } } -#[path = "stable_status.rs"] -mod stable_status; - -/// Compact live activity for todo-lane observers. The composer uses stable_status. +/// Shared by the open session status line and its lightweight todo-lane observer. /// No provider or worker state is inferred here: absent telemetry stays absent. pub(super) fn live_processing_status_line( status: &crate::protocol::SwarmLiveStatus, @@ -822,7 +822,351 @@ fn live_status_snapshot(app: &dyn TuiState, elapsed: f32) -> crate::protocol::Sw } pub(super) fn draw_status(frame: &mut Frame, app: &dyn TuiState, area: Rect, pending_count: usize) { - stable_status::draw_status(frame, app, area, pending_count); + if app.status_notice().as_deref() == Some(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) { + if !super::secondary_render_active() { + crate::tui::app::oauth_telemetry::note_status_render(area); + } + frame.render_widget( + Paragraph::new(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) + .style(Style::default().fg(accent_color())), + area, + ); + return; + } + let elapsed = app.elapsed().map(|d| d.as_secs_f32()).unwrap_or(0.0); + let stale_secs = app.time_since_activity().map(|d| d.as_secs_f32()); + let (cache_read, cache_creation) = app.streaming_cache_tokens(); + let user_turn_count = app.display_user_message_count(); + let (streaming_input_tokens, _) = app.streaming_tokens(); + let provider_name = app.provider_name(); + let upstream_provider = app.upstream_provider(); + let cache_ttl = app.cache_ttl_status(); + let kv_cache_problem = detect_kv_cache_problem( + &provider_name, + upstream_provider.as_deref(), + user_turn_count, + streaming_input_tokens, + cache_read, + cache_creation, + cache_ttl.as_ref(), + ); + + let queued_suffix = if pending_count > 0 { + format!(" · +{} queued", pending_count) + } else { + String::new() + }; + + let line = if let Some(build_progress) = crate::build::read_build_progress() { + let spinner = super::activity_indicator(elapsed, 12.5); + Line::from(vec![ + Span::styled(spinner, Style::default().fg(rgb(255, 193, 7))), + Span::styled( + format!(" {}", build_progress), + Style::default().fg(rgb(255, 193, 7)), + ), + ]) + } else if let Some(remaining) = app.rate_limit_remaining() { + let secs = remaining.as_secs(); + let spinner = super::activity_indicator(elapsed, 4.0); + let time_str = if secs >= 3600 { + let hours = secs / 3600; + let mins = (secs % 3600) / 60; + format!("{}h {}m", hours, mins) + } else if secs >= 60 { + let mins = secs / 60; + let s = secs % 60; + format!("{}m {}s", mins, s) + } else { + format!("{}s", secs) + }; + Line::from(vec![ + Span::styled(spinner, Style::default().fg(rgb(255, 193, 7))), + Span::styled( + format!( + " Rate limited. Auto-retry in {}...{}", + time_str, queued_suffix + ), + Style::default().fg(rgb(255, 193, 7)), + ), + ]) + } else if app.is_processing() { + let spinner = super::activity_indicator(elapsed, 12.5); + + match app.status() { + ProcessingStatus::Idle => Line::from(""), + ProcessingStatus::Sending => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "sending".into(); + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::Connecting(ref phase) => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "connecting".into(); + snapshot.connection_phase = Some(connection_phase_label(phase)); + // "Suspiciously long" is measured per connection attempt, not + // across the whole turn, so later round-trips don't immediately + // render yellow just because the turn has been running a while. + let phase_elapsed = app + .connection_phase_elapsed() + .map_or(elapsed, |d| d.as_secs_f32()); + let label_color = match phase { + crate::message::ConnectionPhase::Retrying { .. } => rgb(255, 193, 7), + crate::message::ConnectionPhase::Authenticating if phase_elapsed > 10.0 => { + rgb(255, 193, 7) + } + crate::message::ConnectionPhase::Connecting if phase_elapsed > 10.0 => { + rgb(255, 193, 7) + } + crate::message::ConnectionPhase::SendingRequest if phase_elapsed > 10.0 => { + rgb(255, 193, 7) + } + _ => dim_color(), + }; + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + if let Some(label) = spans.get_mut(1) { + label.style = label.style.fg(label_color); + } + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::Thinking(_start) => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "thinking".into(); + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::Streaming => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "streaming".into(); + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + if let Some(upstream) = app.upstream_provider() { + spans.push(Span::styled( + format!(" · via {upstream}"), + Style::default().fg(dim_color()), + )); + } + if let Some(problem) = kv_cache_problem { + let miss_tokens = problem.affected_tokens.unwrap_or(0); + let miss_str = if miss_tokens >= 1000 { + format!("{}k", miss_tokens / 1000) + } else if miss_tokens > 0 { + format!("{}", miss_tokens) + } else { + "kv".to_string() + }; + spans.insert( + 1, + Span::styled( + format!(" ⚠ {miss_str} cache miss ·"), + Style::default().fg(rgb(255, 193, 7)), + ), + ); + } + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::WaitingForNetwork { listener } => { + let mut spans = vec![ + Span::styled("↻ ", Style::default().fg(rgb(255, 193, 7))), + Span::styled( + format!( + "network disconnected, waiting to retry · {} · {}", + listener, + format_elapsed(elapsed) + ), + Style::default().fg(rgb(255, 193, 7)), + ), + ]; + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::RunningTool(ref name) => { + let anim_color = ai_color(); + let batch_prog = app.batch_progress(); + let is_batch = name == "batch"; + // For batch: compute initial total from the streaming tool call input + let batch_total_initial = if is_batch { + app.streaming_tool_calls() + .last() + .and_then(|tc| tc.input.get("tool_calls")) + .and_then(|v| v.as_array()) + .map(|a| a.len()) + } else { + None + }; + let tool_detail = if is_batch { + None // batch always uses progress display + } else { + app.streaming_tool_calls() + .last() + .map(get_tool_activity_detail) + .filter(|s| !s.is_empty()) + }; + let experimental_notice = app.active_experimental_feature_notice(); + let subagent = app.subagent_status(); + + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "running_tool".into(); + snapshot.tool_name = Some(name.clone()); + snapshot.tool_intent = tool_detail; + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + + // For batch tool: show "completed/total · last_tool" progress + if is_batch { + append_batch_progress_spans( + &mut spans, + anim_color, + batch_prog, + batch_total_initial, + ); + } + + if let Some(notice) = experimental_notice { + spans.push(Span::styled( + format!(" · ⚠ {}", notice), + Style::default().fg(rgb(255, 193, 7)).bold(), + )); + } + + if let Some(status) = subagent { + spans.push(Span::styled( + format!(" ({})", status), + Style::default().fg(dim_color()), + )); + } + if let Some(label) = app + .upstream_provider() + .map(|provider| format!("via {provider}")) + { + spans.push(Span::styled( + format!(" · {}", label), + Style::default().fg(dim_color()), + )); + } + + if let Some(problem) = kv_cache_problem { + let miss_tokens = problem.affected_tokens.unwrap_or(0); + let miss_str = if miss_tokens >= 1000 { + format!("{}k", miss_tokens / 1000) + } else if miss_tokens > 0 { + format!("{}", miss_tokens) + } else { + "kv".to_string() + }; + spans.push(Span::styled( + format!(" · ⚠ {} cache miss", miss_str), + Style::default().fg(rgb(255, 193, 7)), + )); + } + + spans.push(Span::styled( + format!(" · {} bg", jcode_tui_core::keybind::alt_chord("B")), + Style::default().fg(rgb(100, 100, 100)), + )); + + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + } + } else if let Some((total_in, total_out)) = app.total_session_tokens() { + let total = total_in + total_out; + if let Some(warning) = occasional_session_history_warning( + total, + app.session_compaction_count(), + app.context_limit(), + area.width as usize, + app.animation_elapsed() as u64, + ) { + let severe_token_threshold = app + .context_limit() + .and_then(|limit| u64::try_from(limit).ok()) + .map(|limit| limit.saturating_mul(3)) + .unwrap_or(1_000_000); + let warning_color = + if total >= severe_token_threshold || app.session_compaction_count() >= 3 { + rgb(255, 100, 100) + } else { + rgb(255, 193, 7) + }; + Line::from(vec![ + Span::styled("⚠ ", Style::default().fg(warning_color)), + Span::styled(warning, Style::default().fg(warning_color)), + ]) + } else if let Some(tip) = + occasional_status_tip(area.width as usize, app.animation_elapsed() as u64) + { + Line::from(vec![Span::styled(tip, Style::default().fg(dim_color()))]) + } else { + Line::from("") + } + } else { + if let Some(tip) = + occasional_status_tip(area.width as usize, app.animation_elapsed() as u64) + { + Line::from(vec![Span::styled(tip, Style::default().fg(dim_color()))]) + } else { + Line::from("") + } + }; + + let line = status_with_background_tasks( + line, + app.background_task_rows(), + area.width as usize, + app.is_processing(), + ); + + crate::memory::check_staleness(); + + if app.centered_mode() { + frame.render_widget(Paragraph::new(line.alignment(Alignment::Center)), area); + return; + } + frame.render_widget(Paragraph::new(line), area); +} + +/// Background launches finish their tool call immediately, but the work and +/// its intent must remain visible while the agent moves on to another tool. +fn status_with_background_tasks( + foreground: Line<'static>, + tasks: &[crate::tui::BackgroundTaskRow], + width: usize, + processing: bool, +) -> Line<'static> { + use crate::tui::BackgroundTaskRowStatus; + use jcode_tui_render::truncate_line_with_ellipsis_to_width as fit; + + let running: Vec<_> = tasks + .iter() + .filter(|task| task.status == BackgroundTaskRowStatus::Running) + .collect(); + if running.is_empty() { + return fit(&foreground, width); + } + let labels = running + .iter() + .map(|task| task.label.split_whitespace().collect::>().join(" ")) + .collect::>() + .join(" · "); + let background = Line::from(Span::styled( + format!("bg {}: {}", running.len(), labels), + Style::default().fg(ai_color()), + )); + // Idle tips must not displace actual work. While processing, reserve space + // for both activities rather than letting a long foreground intent hide bg. + if !processing || foreground.width() == 0 || width < 24 { + return fit(&background, width); + } + let background_width = background.width().min(width / 2); + let mut line = fit(&foreground, width.saturating_sub(background_width + 3)); + line.spans + .push(Span::styled(" · ", Style::default().fg(dim_color()))); + line.spans.extend(fit(&background, background_width).spans); + line } fn running_tool_header_spans( @@ -1027,18 +1371,15 @@ pub(super) fn build_notification_spans(app: &dyn TuiState) -> Vec> && cache_info.expiry_notification_active() { if cache_info.is_cold { - let tokens_str = cache_info - .cached_tokens - .map(|t| { - if t >= 1_000_000 { - format!(" ({:.1}M tok)", t as f64 / 1_000_000.0) - } else if t >= 1_000 { - format!(" ({}K tok)", t / 1000) - } else { - format!(" ({} tok)", t) - } - }) - .unwrap_or_default(); + let tokens_str = cache_info.cached_tokens.map_or_else(String::new, |t| { + if t >= 1_000_000 { + format!(" ({:.1}M tok)", t as f64 / 1_000_000.0) + } else if t >= 1_000 { + format!(" ({}K tok)", t / 1000) + } else { + format!(" ({} tok)", t) + } + }); push_sep(&mut spans); spans.push(Span::styled( format!("🧊 cache cold{}", tokens_str), @@ -1053,16 +1394,13 @@ pub(super) fn build_notification_spans(app: &dyn TuiState) -> Vec> Style::default().fg(dim_color()), )); } else if cache_info.expiring_soon() { - let tokens_str = cache_info - .cached_tokens - .map(|t| { - if t >= 1_000 { - format!(" {}K", t / 1000) - } else { - format!(" {}", t) - } - }) - .unwrap_or_default(); + let tokens_str = cache_info.cached_tokens.map_or_else(String::new, |t| { + if t >= 1_000 { + format!(" {}K", t / 1000) + } else { + format!(" {}", t) + } + }); // Above a minute, a raw seconds count is hard to read at a // glance; show minutes granularity instead. let remaining = cache_info.remaining_secs; @@ -1184,13 +1522,13 @@ pub(super) fn draw_overscroll_status(frame: &mut Frame, app: &dyn TuiState, area .filter(|p| !p.is_empty()) .unwrap_or_else(|| app.provider_name()); let facts = OverscrollFacts { - machine: crate::tui::ssh_remote_host(), + machine: app.remote_machine(), dir: overscroll_directory_transition( data.original_working_dir.as_deref(), app.working_dir().as_deref(), ), branch: overscroll_git_branch(&data), - git: data.git_info.clone(), + git: overscroll_git_info(&data).cloned(), context: overscroll_context_usage(&data), account_remaining: overscroll_account_remaining(&data), auth: overscroll_auth_label(data.auth_method), @@ -1229,6 +1567,7 @@ pub(crate) fn preview_overscroll_line( } /// The host belongs to the selected preview, never the attached live session. +#[cfg(test)] pub(crate) fn preview_overscroll_line_for_host( data: &crate::tui::info_widget::InfoWidgetData, width: u16, @@ -1243,7 +1582,7 @@ pub(crate) fn preview_overscroll_line_for_host( data.working_dir.as_deref(), ), branch: overscroll_git_branch(data), - git: data.git_info.clone(), + git: overscroll_git_info(data).cloned(), context: overscroll_context_usage(data), account_remaining: overscroll_account_remaining(data), auth: overscroll_auth_label(data.auth_method), @@ -1548,10 +1887,23 @@ fn overscroll_truncate_spans(spans: Vec>, max_width: usize) -> Vec } /// Format a working dir path home-relative (~/foo/bar), keeping the last 2 segments. +/// Git facts for the status line. Cloud sessions keep their repository in +/// `dual_git_info.cloud`, so prefer that over the (absent) local `git_info`. +fn overscroll_git_info( + data: &crate::tui::info_widget::InfoWidgetData, +) -> Option<&crate::tui::info_widget::GitInfo> { + if let Some(dual) = &data.dual_git_info + && let crate::tui::cloud_git::RepoState::Ready(info) = &dual.cloud + { + return Some(info); + } + data.git_info.as_ref() +} + /// Compact git branch label for the status line and fact stack. Truncated so /// long branch names cannot crowd out the other facts. fn overscroll_git_branch(data: &crate::tui::info_widget::InfoWidgetData) -> Option { - let branch = data.git_info.as_ref()?.branch.trim(); + let branch = overscroll_git_info(data)?.branch.trim(); if branch.is_empty() { return None; } @@ -1782,6 +2134,7 @@ fn overscroll_context_percentage(used: usize, limit: usize) -> Span<'static> { /// The idle resume composer uses the live input wrapper, but never reads the /// currently open session's draft, queue, selection, or cursor state. +#[cfg(test)] pub(crate) fn draw_preview_input(frame: &mut Frame, area: Rect, next_prompt: usize) { let number = next_prompt.to_string(); let prompt = "> "; @@ -1829,7 +2182,8 @@ pub(super) fn draw_input( return None; } - let (all_lines, cursor_line, cursor_col) = wrap_input_text_with_images( + let voice_preview = app.voice_composer_preview(); + let (all_lines, cursor_line, cursor_col) = wrap_input_text_with_ghost( input_text, cursor_pos, line_width, @@ -1838,12 +2192,18 @@ pub(super) fn draw_input( caret_color, prompt_len, &|number| app.input_image_attached(number), + voice_ghost(voice_preview.as_ref()), ); let mut lines: Vec = Vec::new(); let mut hint_shown = false; let mut hint_line: Option = None; - if let Some(hint) = app.login_input_hint() { + if let Some(preview) = voice_preview.as_ref() { + hint_shown = true; + let line = voice_waveform_line(preview, area.width as usize); + hint_line = Some(line.spans.iter().map(|s| s.content.as_ref()).collect()); + lines.push(line); + } else if let Some(hint) = app.login_input_hint() { hint_shown = true; hint_line = Some(hint.to_string()); lines.push(Line::from(Span::styled( @@ -2030,7 +2390,9 @@ pub(super) fn draw_input( if app.input_drag_scroll_offset().is_some() { return None; } - frame.set_cursor_position(cursor); + if !super::secondary_render_active() { + frame.set_cursor_position(cursor); + } Some(cursor) } @@ -2292,6 +2654,7 @@ pub(crate) fn wrap_input_text<'a>( ) } +#[cfg(test)] #[allow(clippy::too_many_arguments)] fn wrap_input_text_with_images<'a>( input: &str, @@ -2303,6 +2666,48 @@ fn wrap_input_text_with_images<'a>( prompt_len: usize, attached: &dyn Fn(usize) -> bool, ) -> (Vec>, usize, usize) { + wrap_input_text_with_ghost( + input, + cursor_pos, + line_width, + num_str, + prompt_char, + caret_color, + prompt_len, + attached, + None, + ) +} + +/// Like [`wrap_input_text_with_images`], with an optional live voice +/// transcript appended as ghost text. The ghost wraps with the input but is +/// never editable, so the cursor stays at its real position. +#[allow(clippy::too_many_arguments)] +fn wrap_input_text_with_ghost<'a>( + input: &str, + cursor_pos: usize, + line_width: usize, + num_str: &str, + prompt_char: &'a str, + caret_color: Color, + prompt_len: usize, + attached: &dyn Fn(usize) -> bool, + ghost: Option<(&str, Style)>, +) -> (Vec>, usize, usize) { + let ghost_start = input.chars().count(); + let combined; + let (input, ghost_style) = match ghost.filter(|(text, _)| !text.is_empty()) { + Some((text, style)) => { + let sep = if input.is_empty() || input.ends_with(char::is_whitespace) { + "" + } else { + " " + }; + combined = format!("{input}{sep}{text}"); + (combined.as_str(), Some(style)) + } + None => (input, None), + }; let image_ranges: Vec<(std::ops::Range, bool)> = input .match_indices("[image ") .filter_map(|(start, _)| { @@ -2348,7 +2753,9 @@ fn wrap_input_text_with_images<'a>( let mut previous_style = Style::default(); for (offset, ch) in segment.text.chars().enumerate() { let position = segment.start_char + offset; - let style = if let Some((_, present)) = image_ranges + let style = if let Some(ghost_style) = ghost_style.filter(|_| position >= ghost_start) { + ghost_style + } else if let Some((_, present)) = image_ranges .iter() .find(|(range, _)| range.contains(&position)) { @@ -2384,6 +2791,136 @@ fn wrap_input_text_with_images<'a>( (lines, cursor_line, cursor_col) } +/// Ghost text for the live voice transcript: italic and dim while +/// recording, accent colored while the final pass runs. +fn voice_ghost(preview: Option<&crate::tui::VoiceComposerPreview>) -> Option<(&str, Style)> { + let preview = preview?; + let color = if preview.recording { + rgb(200, 200, 210) + } else { + rgb(140, 200, 255) + }; + Some(( + preview.live.as_str(), + Style::default().fg(dim_color_of(color)).italic(), + )) +} + +fn dim_color_of(color: Color) -> Color { + match color { + Color::Rgb(r, g, b) => rgb( + (r as u16 * 3 / 4) as u8, + (g as u16 * 3 / 4) as u8, + (b as u16 * 3 / 4) as u8, + ), + other => other, + } +} + +/// Truncate to `width` terminal cells, ending in `…` when cut. +fn truncate_cells(text: &str, width: usize) -> String { + use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; + if text.width() <= width { + return text.to_string(); + } + let mut out = String::new(); + let mut used = 0; + for ch in text.chars() { + let w = ch.width().unwrap_or(0); + if used + w + 1 > width { + break; + } + used += w; + out.push(ch); + } + if width > 0 { + out.push('…'); + } + out +} + +/// Smooth level to a 0..=1 bar height. Speech RMS sits around 0.02 to 0.2, +/// so the curve is compressed to keep normal speech visibly moving. +fn voice_level_height(level: f32) -> f32 { + (level.max(0.0) * 4.0).sqrt().min(1.0) +} + +/// Header row above the composer while voice input is active: +/// `● 0:07 ▁▂▅▇▆▃▂▁▂▄… · Ctrl+Space send · Esc cancel`. The waveform fills the +/// width left after the label and hint, newest sample on the right, colored +/// by loudness. Never wraps: the hint is dropped first, then the waveform +/// shrinks. +fn voice_waveform_line(preview: &crate::tui::VoiceComposerPreview, width: usize) -> Line<'static> { + use unicode_width::UnicodeWidthStr; + const BARS: [char; 8] = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█']; + let mut spans: Vec> = Vec::new(); + if !preview.recording { + let dots = ["◌", "◍", "●", "◍"]; + let phase = (std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_millis() / 250) + .unwrap_or(0) + % dots.len() as u128) as usize; + let mut text = format!(" {} Transcribing…", dots[phase]); + if !preview.hint.is_empty() { + text.push_str(" · "); + text.push_str(&preview.hint); + } + let text = truncate_cells(&text, width); + spans.push(Span::styled( + text, + Style::default().fg(rgb(140, 200, 255)).bold(), + )); + return Line::from(spans); + } + let label = format!( + " ● {}:{:02} ", + preview.elapsed_secs / 60, + preview.elapsed_secs % 60 + ); + let hint = if preview.hint.is_empty() { + String::new() + } else { + format!(" {}", preview.hint) + }; + let label_w = label.width(); + let mut hint_w = hint.width(); + if label_w + hint_w + 8 > width { + hint_w = 0; + } + let wave_w = width.saturating_sub(label_w + hint_w).min(48); + spans.push(Span::styled( + truncate_cells(&label, width), + Style::default().fg(rgb(255, 110, 110)).bold(), + )); + if wave_w > 0 { + let levels = &preview.levels; + let start = levels.len().saturating_sub(wave_w); + let pad = wave_w.saturating_sub(levels.len() - start); + if pad > 0 { + spans.push(Span::styled( + "·".repeat(pad), + Style::default().fg(rgb(70, 70, 80)), + )); + } + for level in &levels[start..] { + let h = voice_level_height(*level); + let glyph = BARS[(h * 7.0).round() as usize]; + // Quiet bars recede, loud bars glow warm. + let color = rgb( + (110.0 + 145.0 * h) as u8, + (150.0 - 40.0 * h) as u8, + (190.0 - 80.0 * h) as u8, + ); + spans.push(Span::styled(glyph.to_string(), Style::default().fg(color))); + } + } + if hint_w > 0 { + spans.push(Span::styled(hint, Style::default().fg(dim_color()))); + } + Line::from(spans) +} + fn send_mode_indicator(app: &dyn TuiState) -> (&'static str, Color) { let mode = composer_mode(app.input(), app.is_remote_mode()); if mode.is_shell() { diff --git a/crates/jcode-tui/src/tui/ui_input/status_tests.rs b/crates/jcode-tui/src/tui/ui_input/status_tests.rs index 2b1eaf2d9f1..91fdfd693c2 100644 --- a/crates/jcode-tui/src/tui/ui_input/status_tests.rs +++ b/crates/jcode-tui/src/tui/ui_input/status_tests.rs @@ -1,106 +1,59 @@ use super::*; use ratatui::style::Modifier; -use super::stable_status::{ - live_processing_status_line_at_width, stable_status_elapsed, stable_status_line, - stable_status_telemetry, status_cell, -}; - -#[test] -fn stable_status_elapsed_keeps_cell_width_across_time_boundaries() { - for elapsed in [ - 0, - 999, - 59_999, - 60_000, - 3_599_999, - 3_600_000, - 360_000_000, - u64::MAX, - ] { - let value = stable_status_elapsed(elapsed); - assert_eq!( - unicode_width::UnicodeWidthStr::width(value.as_str()), - 8, - "{value}" - ); - } - assert_eq!(stable_status_elapsed(12_300).trim(), "00:12"); - assert_eq!(stable_status_elapsed(3_600_000).trim(), "1:00:00"); -} - -#[test] -fn stable_status_cells_fit_unicode_and_normalize_controls() { - for width in 0..40 { - let value = status_cell("界\n tests\tand \x1bmore", width); - assert_eq!(unicode_width::UnicodeWidthStr::width(value.as_str()), width); - assert!(!value.chars().any(char::is_control)); - } -} - -#[test] -fn stable_status_telemetry_stays_anchored_as_values_and_intent_change() { - let mut status = crate::protocol::SwarmLiveStatus { - phase: "streaming".into(), - elapsed_ms: 12_300, - ..Default::default() - }; - for width in [60, 80, 100, 120] { - let unknown = live_processing_status_line_at_width(&status, None, "*", width).to_string(); - status.input_tokens = Some(123_456); - status.output_tokens = Some(1234); - status.output_tps_milli = Some(12_500); - status.tool_intent = Some("界 long intent ".repeat(30)); - let known = live_processing_status_line_at_width(&status, None, "*", width).to_string(); - let column = |text: &str, needle: char| { - text.find(needle) - .map(|offset| unicode_width::UnicodeWidthStr::width(&text[..offset])) - }; - assert_eq!( - column(&unknown, '↑'), - column(&known, '↑'), - "{unknown}\n{known}" - ); - assert_eq!( - column(&unknown, '↓'), - column(&known, '↓'), - "{unknown}\n{known}" - ); - assert_eq!(unicode_width::UnicodeWidthStr::width(known.as_str()), width); - status.input_tokens = None; - status.output_tokens = None; - status.output_tps_milli = None; - status.tool_intent = None; +fn background_row( + label: &str, + status: crate::tui::BackgroundTaskRowStatus, +) -> crate::tui::BackgroundTaskRow { + crate::tui::BackgroundTaskRow { + task_id: label.into(), + label: label.into(), + percent: None, + status, + completed_at: None, } } #[test] -fn stable_status_empty_intent_does_not_add_a_leading_separator() { - let status = crate::protocol::SwarmLiveStatus { - phase: "streaming".into(), - tool_intent: Some(String::new()), - status_detail: Some("fresh websocket".into()), - ..Default::default() - }; - let text = live_processing_status_line_at_width(&status, None, "*", 120).to_string(); - assert!(text.contains("│ opening websocket"), "{text}"); - assert!(!text.contains("│ ·"), "{text}"); +fn background_status_keeps_all_running_intents_after_launch_returns() { + use crate::tui::BackgroundTaskRowStatus::*; + let tasks = [ + background_row("Run tests", Running), + background_row("Build app", Running), + background_row("Already done", Completed), + background_row("Old failure", Failed), + ]; + let text = status_with_background_tasks(Line::from("thinking…"), &tasks, 120, true).to_string(); + assert_eq!(text, "thinking… · bg 2: Run tests · Build app"); + let idle = status_with_background_tasks(Line::from("idle tip"), &tasks, 120, false).to_string(); + assert_eq!(idle, "bg 2: Run tests · Build app"); + assert_eq!( + status_with_background_tasks(Line::from("thinking…"), &tasks[2..], 120, true).to_string(), + "thinking…" + ); } #[test] -fn stable_status_fits_every_terminal_cell_width() { - let telemetry = stable_status_telemetry(&crate::protocol::SwarmLiveStatus::default()); - for width in 0..160 { - let line = stable_status_line( - "*", - "thinking", - 123_456, - &"界\nlong\tintent".repeat(40), - &telemetry, - width, - ); - assert!(line.width() <= width, "width={width}: {line}"); - assert!(!line.to_string().chars().any(char::is_control)); +fn background_status_fits_unicode_cells_and_keeps_background_visible() { + let tasks = [background_row( + "Run 界\n tests\tand more", + crate::tui::BackgroundTaskRowStatus::Running, + )]; + for width in 0..120 { + for processing in [false, true] { + let line = status_with_background_tasks( + Line::from("foreground 界 intent ".repeat(10)), + &tasks, + width, + processing, + ); + assert!(line.width() <= width, "width={width}: {line}"); + let text = line.to_string(); + assert!(!text.contains('\n') && !text.contains('\t'), "{text}"); + if width >= 24 { + assert!(text.contains("bg 1:"), "width={width}: {text}"); + } + } } } @@ -238,17 +191,11 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { tool_intent: Some("Verify 界\noutput".into()), ..Default::default() }; - let line = live_processing_status_line_at_width(&status, Some(0.0), "⠋", 160); + let line = live_processing_status_line(&status, Some(0.0), "⠋"); let text = line.to_string(); + assert!(text.contains("running bash · Verify 界 output"), "{text}"); assert!( - text.contains("bash") && text.contains("Verify 界 output"), - "{text}" - ); - assert!( - text.contains("↑12k") - && text.contains("↓456") - && text.contains("existing websocket") - && text.contains("00:12"), + text.contains("↑12k ↓456") && text.contains("existing websocket") && text.contains("12s"), "{text}" ); assert_eq!(line.spans[0].style.fg, Some(ai_color())); @@ -256,19 +203,18 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { status.phase = "streaming".into(); status.output_tps_milli = Some(12_500); assert!( - live_processing_status_line_at_width(&status, None, "⠋", 160) + live_processing_status_line(&status, None, "⠋") .to_string() - .contains("12.5/s") + .contains("12.5 tps") ); status.input_tokens = None; status.output_tokens = None; status.connection_type = None; - let text = live_processing_status_line_at_width(&status, None, "⠋", 160).to_string(); - assert!(!text.contains("↑0") && !text.contains("websocket")); - assert!(text.contains("↑-") && text.contains("↓-"), "{text}"); + let text = live_processing_status_line(&status, None, "⠋").to_string(); + assert!(!text.contains('↑') && !text.contains("websocket")); status.phase = "stopped".into(); status.stop_reason = Some("Rate limit\nretry later".into()); - let text = live_processing_status_line_at_width(&status, None, "⠋", 160).to_string(); + let text = live_processing_status_line(&status, None, "⠋").to_string(); assert!(text.contains("Rate limit retry later") && !text.contains('⠋')); } @@ -482,6 +428,22 @@ fn swarm_effort_model_status_uses_shared_label() { assert_eq!(overscroll_short_reasoning(" "), None); } +#[test] +fn running_tool_header_emphasizes_detail_over_tool_name() { + let accent = Color::Rgb(12, 34, 56); + let spans = running_tool_header_spans("*", "bash", Some("cargo test"), accent); + + assert_eq!(spans.len(), 3); + assert_eq!(spans[0].content.as_ref(), "*"); + assert_eq!(spans[0].style.fg, Some(accent)); + assert_eq!(spans[1].content.as_ref(), " running bash"); + assert_eq!(spans[1].style.fg, Some(dim_color())); + assert!(!spans[1].style.add_modifier.contains(Modifier::BOLD)); + assert_eq!(spans[2].content.as_ref(), " · cargo test"); + assert_eq!(spans[2].style.fg, Some(accent)); + assert!(spans[2].style.add_modifier.contains(Modifier::BOLD)); +} + #[test] fn visual_line_move_follows_soft_wrapped_rows() { // 20 chars, width 10 => two visual rows, no newline in the input. @@ -718,32 +680,58 @@ fn connection_phase_waiting_label_is_generic_response_wait() { } #[test] -fn streaming_liveness_stays_in_activity_and_only_warns_during_streaming() { - let mut status = crate::protocol::SwarmLiveStatus { - phase: "streaming".into(), - elapsed_ms: 12_000, - ..Default::default() - }; - let quiet = live_processing_status_line_at_width(&status, Some(3.4), "*", 120).to_string(); - assert!(quiet.contains("no tokens 3s"), "{quiet}"); - let stalled = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); - assert!(stalled.contains("stalled 12s"), "{stalled}"); - assert_eq!(quiet.find("00:12"), stalled.find("00:12")); - status.stream_message_ended = true; - let ended = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); - assert!( - ended.starts_with('*') && !ended.contains("stalled"), - "{ended}" - ); - status.stream_message_ended = false; - for phase in ["thinking", "running_tool", "sending", "connecting"] { - status.phase = phase.into(); - let text = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); - assert!( - !text.contains("stalled") && !text.contains("no tokens"), - "{text}" - ); - } +fn streaming_liveness_label_shows_quiet_stream_warning_before_message_end() { + assert_eq!( + streaming_liveness_label("4.2s".to_string(), Some(3.4), false), + "(no tokens 3s) · 4.2s" + ); + assert_eq!( + streaming_liveness_label("12.0s".to_string(), Some(12.1), false), + "(stalled 12s) · 12.0s" + ); +} + +#[test] +fn streaming_liveness_label_suppresses_quiet_stream_warning_after_message_end() { + assert_eq!( + streaming_liveness_label("4.2s".to_string(), Some(3.4), true), + "4.2s" + ); + assert_eq!( + streaming_liveness_label("12.0s".to_string(), Some(12.1), true), + "12.0s" + ); +} + +#[test] +fn streaming_status_spans_keep_spinner_while_finalizing() { + let spans = streaming_status_spans("⠋", "4.2s".to_string(), false, false, " · +1 queued"); + + assert_eq!(spans.len(), 3); + assert_eq!(spans[0].content.as_ref(), "⠋"); + assert_eq!(spans[1].content.as_ref(), " 4.2s"); + assert_eq!(spans[2].content.as_ref(), " · +1 queued"); +} + +#[test] +fn streaming_status_spans_keep_spinner_after_message_end_while_finalizing() { + let spans = streaming_status_spans("⠋", "finalizing".to_string(), true, false, ""); + + assert_eq!(spans.len(), 2); + assert_eq!(spans[0].content.as_ref(), "⠋"); + assert_eq!(spans[1].content.as_ref(), " finalizing"); +} + +#[test] +fn push_queued_suffix_appends_only_when_present() { + let mut spans: Vec> = Vec::new(); + push_queued_suffix(&mut spans, ""); + assert!(spans.is_empty(), "empty suffix should add no span"); + + push_queued_suffix(&mut spans, " · +2 queued"); + assert_eq!(spans.len(), 1); + assert_eq!(spans[0].content.as_ref(), " · +2 queued"); + assert_eq!(spans[0].style.fg, Some(queued_color())); } #[test] @@ -846,3 +834,64 @@ fn normalize_repaint_sensitive_notice_text_drops_warning_variation_selector() { "all clear" ); } + +fn voice_preview(recording: bool, levels: Vec) -> crate::tui::VoiceComposerPreview { + crate::tui::VoiceComposerPreview { + recording, + elapsed_secs: 7, + levels, + live: "hello world".into(), + hint: "Ctrl+Space send · Esc cancel".into(), + } +} + +fn line_text(line: &Line<'_>) -> String { + line.spans.iter().map(|s| s.content.as_ref()).collect() +} + +#[test] +fn voice_waveform_row_never_exceeds_width() { + use unicode_width::UnicodeWidthStr; + let preview = voice_preview(true, vec![0.1; 64]); + for width in [0usize, 5, 12, 20, 40, 80, 200] { + let text = line_text(&voice_waveform_line(&preview, width)); + assert!(text.width() <= width.max(1), "width {width}: {text:?}"); + } + let wide = line_text(&voice_waveform_line(&preview, 120)); + assert!(wide.contains("0:07") && wide.contains("Esc cancel"), "{wide}"); + let narrow = line_text(&voice_waveform_line(&preview, 20)); + assert!(!narrow.contains("Esc"), "hint dropped first: {narrow}"); + let transcribing = line_text(&voice_waveform_line(&voice_preview(false, vec![]), 80)); + assert!(transcribing.contains("Transcribing"), "{transcribing}"); +} + +#[test] +fn voice_waveform_is_newest_on_the_right_and_padded() { + let preview = voice_preview(true, vec![0.0, 0.0, 1.0]); + let text = line_text(&voice_waveform_line(&preview, 40)); + assert!(text.contains('·'), "short history pads on the left: {text}"); + let wave_end = text.trim_end_matches(|c: char| !"▁▂▃▄▅▆▇█".contains(c)); + assert!(wave_end.ends_with('█'), "{text}"); +} + +#[test] +fn voice_ghost_text_wraps_after_input_without_moving_cursor() { + let style = Style::default().italic(); + let (lines, cursor_line, cursor_col) = wrap_input_text_with_ghost( + "fix", + 3, + 100, + "1", + "> ", + Color::White, + 3, + &|_| false, + Some(("the bug", style)), + ); + assert_eq!((cursor_line, cursor_col), (0, 3)); + let text = line_text(&lines[0]); + assert!(text.ends_with("fix the bug"), "{text}"); + let ghost = lines[0].spans.last().unwrap(); + assert_eq!(ghost.content.as_ref(), " the bug"); + assert!(ghost.style.add_modifier.contains(Modifier::ITALIC)); +} diff --git a/crates/jcode-tui/src/tui/ui_memory_estimates.rs b/crates/jcode-tui/src/tui/ui_memory_estimates.rs index 1b709c647b0..d94204d6aa2 100644 --- a/crates/jcode-tui/src/tui/ui_memory_estimates.rs +++ b/crates/jcode-tui/src/tui/ui_memory_estimates.rs @@ -96,6 +96,35 @@ pub(super) fn estimate_prepared_messages_bytes(prepared: &PreparedMessages) -> u + estimate_copy_targets_bytes(&prepared.copy_targets) } +/// Sections share immutable `Arc` bodies across frames. +/// Walking a large body (tens of thousands of lines) on every streaming frame +/// was ~10% of release-build client CPU, so remember the estimate per Arc. +/// `Weak` keeps the pointer from being reused for a different allocation. +fn estimate_shared_prepared_messages_bytes(prepared: &Arc) -> usize { + use std::cell::RefCell; + use std::sync::Weak; + const MEMO_CAP: usize = 8; + thread_local! { + static MEMO: RefCell, usize)>> = const { RefCell::new(Vec::new()) }; + } + MEMO.with(|memo| { + let mut memo = memo.borrow_mut(); + if let Some((_, bytes)) = memo + .iter() + .find(|(weak, _)| std::ptr::eq(weak.as_ptr(), Arc::as_ptr(prepared))) + { + return *bytes; + } + let bytes = estimate_prepared_messages_bytes(prepared); + memo.retain(|(weak, _)| weak.strong_count() > 0); + if memo.len() >= MEMO_CAP { + memo.remove(0); + } + memo.push((Arc::downgrade(prepared), bytes)); + bytes + }) +} + pub(super) fn estimate_prepared_chat_frame_bytes(prepared: &PreparedChatFrame) -> usize { // Include each section's prepared content: the wrapped lines dominate a // frame's real memory footprint. Without this, multi-megabyte transcripts @@ -104,7 +133,7 @@ pub(super) fn estimate_prepared_chat_frame_bytes(prepared: &PreparedChatFrame) - prepared .sections .iter() - .map(|section| estimate_prepared_messages_bytes(§ion.prepared)) + .map(|section| estimate_shared_prepared_messages_bytes(§ion.prepared)) .sum::() + prepared.sections.capacity() * std::mem::size_of::() + estimate_usize_vec_bytes(&prepared.wrapped_user_indices) @@ -128,13 +157,65 @@ fn estimate_visible_copy_targets_bytes(values: &Vec) -> usize + values.capacity() * std::mem::size_of::() } +fn prepared_messages_breakdown(prepared: &PreparedMessages) -> serde_json::Value { + let line_spans: usize = prepared + .wrapped_lines + .iter() + .map(|line| line.spans.capacity() * std::mem::size_of::>()) + .sum(); + let span_text: usize = prepared + .wrapped_lines + .iter() + .flat_map(|line| line.spans.iter()) + .map(|span| span.content.len()) + .sum(); + let span_count: usize = prepared.wrapped_lines.iter().map(|l| l.spans.len()).sum(); + let span_capacity: usize = prepared + .wrapped_lines + .iter() + .map(|l| l.spans.capacity()) + .sum(); + let max_spans_line = prepared + .wrapped_lines + .iter() + .max_by_key(|l| l.spans.len()) + .map(|l| { + l.spans + .iter() + .take(12) + .map(|s| s.content.to_string()) + .collect::>() + }); + serde_json::json!({ + "wrapped_lines_count": prepared.wrapped_lines.len(), + "span_count": span_count, + "span_capacity": span_capacity, + "max_spans_line_sample": max_spans_line, + "max_spans_line_len": prepared.wrapped_lines.iter().map(|l| l.spans.len()).max(), + "line_headers_bytes": prepared.wrapped_lines.capacity() * std::mem::size_of::>(), + "span_structs_bytes": line_spans, + "span_text_bytes": span_text, + "wrapped_plain_lines_bytes": estimate_arc_string_vec_bytes(&prepared.wrapped_plain_lines), + "raw_plain_lines_bytes": estimate_arc_string_vec_bytes(&prepared.raw_plain_lines), + "wrapped_copy_offsets_bytes": estimate_arc_usize_vec_bytes(&prepared.wrapped_copy_offsets), + "wrapped_line_map_bytes": estimate_arc_wrapped_line_map_bytes(&prepared.wrapped_line_map), + "copy_targets_bytes": estimate_copy_targets_bytes(&prepared.copy_targets), + "user_prompt_texts_bytes": estimate_string_vec_bytes(&prepared.user_prompt_texts), + }) +} + pub(crate) fn debug_memory_profile() -> serde_json::Value { use std::collections::HashSet; - let (body_entries_count, body_msg_count_sum, body_unique_prepared_bytes) = { + let (body_entries_count, body_msg_count_sum, body_unique_prepared_bytes, body_breakdown) = { let cache = body_cache() .lock() .unwrap_or_else(|poisoned| poisoned.into_inner()); + let breakdown = cache + .entries + .iter() + .max_by_key(|entry| entry.msg_count) + .map(|entry| prepared_messages_breakdown(&entry.prepared)); let mut seen = HashSet::new(); let mut unique_bytes = 0usize; let mut msg_count_sum = 0usize; @@ -145,7 +226,7 @@ pub(crate) fn debug_memory_profile() -> serde_json::Value { unique_bytes += estimate_prepared_messages_bytes(&entry.prepared); } } - (cache.entries.len(), msg_count_sum, unique_bytes) + (cache.entries.len(), msg_count_sum, unique_bytes, breakdown) }; let (full_prep_entries_count, full_prep_unique_prepared_bytes) = { @@ -183,6 +264,7 @@ pub(crate) fn debug_memory_profile() -> serde_json::Value { "entries_count": body_entries_count, "messages_count_sum": body_msg_count_sum, "unique_prepared_bytes": body_unique_prepared_bytes, + "largest_entry_breakdown": body_breakdown, }, "full_prep_cache": { "entries_count": full_prep_entries_count, diff --git a/crates/jcode-tui/src/tui/ui_messages.rs b/crates/jcode-tui/src/tui/ui_messages.rs index 27f65ccc2ac..bb359125631 100644 --- a/crates/jcode-tui/src/tui/ui_messages.rs +++ b/crates/jcode-tui/src/tui/ui_messages.rs @@ -3,6 +3,9 @@ mod gmail_drafts; use gmail_drafts::*; #[path = "ui_messages/todo_completion.rs"] mod todo_completion; +#[cfg(test)] +#[path = "ui_messages/todo_payload_cache_tests.rs"] +mod todo_payload_cache_tests; #[path = "ui_todo_stages.rs"] mod todo_stages; pub(crate) use todo_stages::TodoCardAgent; @@ -808,7 +811,47 @@ fn todo_confidence_color() -> Color { rgb(135, 155, 180) } +// Cache data only: time-sensitive blocker policy and width/style rendering +// remain live on every draw. One bounded entry retains no transcript history. +const MAX_CACHED_TODO_PAYLOAD_BYTES: usize = 256 * 1024; + +#[derive(Default)] +struct TodoPayloadCache { + entry: Option<(String, Option>)>, +} + +impl TodoPayloadCache { + fn get(&mut self, content: &str) -> Option> { + if let Some((source, parsed)) = &self.entry + && source == content + { + return parsed.clone(); + } + let parsed = serde_json::from_str::(content) + .ok() + .map(std::sync::Arc::new); + self.entry = (content.len() <= MAX_CACHED_TODO_PAYLOAD_BYTES) + .then(|| (content.to_owned(), parsed.clone())); + parsed + } +} + +thread_local! { + static TODO_PAYLOAD_CACHE: std::cell::RefCell = + std::cell::RefCell::new(TodoPayloadCache::default()); +} + +fn cached_todo_payload(content: &str) -> Option> { + TODO_PAYLOAD_CACHE.with_borrow_mut(|cache| cache.get(content)) +} + impl TodoCardPayload { + fn todos(&self) -> &[crate::todo::TodoItem] { + match self { + Self::Current { todos, .. } | Self::Legacy(todos) => todos, + } + } + fn into_parts( self, ) -> ( @@ -970,18 +1013,33 @@ pub(crate) fn render_todos_message( /// Dedicated composer chrome, one non-wrapping row per live blocker. pub(super) fn todo_blocker_footer(payload: Option<&str>, width: u16) -> Vec> { - let Some(payload) = payload.and_then(|p| serde_json::from_str::(p).ok()) - else { + todo_blocker_footer_at(payload, width, chrono::Utc::now()) +} + +fn todo_blocker_footer_at( + payload: Option<&str>, + width: u16, + now: chrono::DateTime, +) -> Vec> { + let Some(payload) = payload.and_then(cached_todo_payload) else { return Vec::new(); }; - let (todos, ..) = payload.into_parts(); - todos + render_todo_blocker_footer(&payload, width, now) +} + +fn render_todo_blocker_footer( + payload: &TodoCardPayload, + width: u16, + now: chrono::DateTime, +) -> Vec> { + payload + .todos() .iter() .filter(|todo| crate::todo::is_incomplete_todo(todo)) .filter_map(|todo| { let reason = if crate::todo::todo_completely_blocked(todo) { todo.completely_blocked.as_ref().map(|b| b.reason.clone()) - } else if crate::todo::todo_has_external_blocker(todo) { + } else if crate::todo::todo_has_external_blocker_at(todo, now) { todo.external_blocker.as_ref().map(|b| b.reason.clone()) } else if !todo.blocked_by.is_empty() { Some(format!("waiting on {}", todo.blocked_by.join(", "))) @@ -1004,10 +1062,10 @@ pub(super) fn todo_blocker_footer(payload: Option<&str>, width: u16) -> Vec) -> Vec> { let mut lines = Vec::new(); if let Some(payload) = payload - && let Ok(payload) = serde_json::from_str::(payload) + && let Some(payload) = cached_todo_payload(payload) { - let (todos, ..) = payload.into_parts(); - for todo in todos + for todo in payload + .todos() .iter() .filter(|todo| crate::todo::todo_completely_blocked(todo)) { @@ -1531,7 +1589,7 @@ fn push_todo_item_lines( inner_width, )); } - todo_stages::push_lane_status_lines(lines, todos, agents, base_indent, inner_width); + todo_stages::push_lane_status_lines_below_rows(lines, todos, agents, base_indent, inner_width); } fn wrap_todo_detail(value: &str, width: usize) -> Vec { @@ -2084,11 +2142,29 @@ fn render_todo_item_line_with_background( inner_width: usize, ) -> Line<'static> { let dependencies = todo_running_dependencies(todo, tasks); + let wait_note = todo_stages::todo_wait_note(todo, agents); if dependencies.is_empty() || crate::todo::todo_completely_blocked(todo) { - return render_todo_item_line(todo, agents, base_indent, inner_width); + let mut line = render_todo_item_line(todo, agents, base_indent, inner_width); + if let Some(note) = wait_note { + line.spans.push(Span::styled( + format!(" ◷ {note}"), + Style::default().fg(todo_warning_color()), + )); + line = super::truncate_line_with_ellipsis_to_width( + &line, + inner_width.saturating_add(base_indent.width()), + ); + } + return line; } let mut spans = todo_card_item_spans(todo, false); spans[1] = Span::styled("◷ ", Style::default().fg(todo_warning_color())); + if let Some(note) = wait_note { + spans.push(Span::styled( + format!(" {note}"), + Style::default().fg(todo_warning_color()), + )); + } let title_width = spans.iter().map(|span| span.content.width()).sum::(); let progress = todo_dependency_progress( &dependencies, @@ -3139,8 +3215,8 @@ fn swarm_notification_style(title: Option<&str>) -> (&'static str, Color, Color) /// Trailing badge text appended to a collapsed swarm tldr line. Kept as /// constants so click hit-testing and rendering stay in sync. -pub(crate) const SWARM_EXPAND_BADGE: &str = "▸ expand"; -pub(crate) const SWARM_COLLAPSE_BADGE: &str = "▾ collapse"; +pub(crate) const SWARM_EXPAND_BADGE: &str = "⊞"; +pub(crate) const SWARM_COLLAPSE_BADGE: &str = "⊟"; pub(crate) const SWARM_DIFF_EXPAND_BADGE: &str = "▸ diff"; pub(crate) const SWARM_DIFF_COLLAPSE_BADGE: &str = "▾ hide"; pub(crate) const SWARM_AGENT_SNAPSHOT_TITLE: &str = "swarm-agent-snapshot"; @@ -3254,6 +3330,7 @@ fn compact_swarm_notification(title: &str) -> Option, width: u16, ) -> Option>> { let notification = compact_swarm_notification(title)?; @@ -3321,9 +3398,20 @@ fn render_compact_agent_notification( }; // Keep the full click target only when it fits. At very narrow widths // the preview wins rather than letting a badge force a second row. + // Short plain notes stay clean: usage only appears on collapsible + // messages or when the time since this agent's last message is known. + let usage_text = (badge.is_some() || since_last_secs.is_some()) + .then(|| swarm_notification_usage_label(content, since_last_secs)) + .flatten() + .map(|usage| format!(" {usage}")) + .filter(|usage| usage.width() + 8 < max_width); + let usage_width = usage_text + .as_deref() + .map(UnicodeWidthStr::width) + .unwrap_or(0); let badge_text = badge .map(|badge| format!(" {badge}")) - .filter(|badge| badge.width() < max_width); + .filter(|badge| badge.width() + usage_width < max_width); let badge_width = badge_text .as_deref() .map(UnicodeWidthStr::width) @@ -3331,8 +3419,12 @@ fn render_compact_agent_notification( prefix.push(Span::styled(preview, body_style)); let mut line = super::truncate_line_with_ellipsis_to_width( &Line::from(prefix), - max_width.saturating_sub(badge_width).max(1), + max_width.saturating_sub(badge_width + usage_width).max(1), ); + if let Some(usage_text) = usage_text { + line.spans + .push(Span::styled(usage_text, Style::default().fg(dim_color()))); + } if let Some(badge_text) = badge_text { line.spans.push(Span::styled( badge_text, @@ -3396,6 +3488,28 @@ fn render_compact_agent_notification( Some(lines) } +/// "time since this agent's previous message, approx tokens" for a swarm +/// notification. Time is omitted when unknown (first message, replay). +fn swarm_notification_usage_label(content: &str, since_last_secs: Option) -> Option { + let body = jcode_tui_messages::parse_collapsible_swarm_content(content) + .map(|parsed| format!("{} {}", parsed.tldr, parsed.body)) + .unwrap_or_else(|| content.to_string()); + let tokens = crate::util::estimate_tokens(body.trim()); + if tokens == 0 { + return None; + } + let tokens = crate::util::format_approx_token_count(tokens); + Some( + match since_last_secs.filter(|secs| secs.is_finite() && *secs >= 0.0) { + Some(secs) => format!( + "{}, {tokens}", + crate::message::Message::format_duration((secs as f64 * 1000.0).round() as u64) + ), + None => tokens, + }, + ) +} + fn render_compact_swarm_await( title: &str, content: &str, @@ -3490,7 +3604,9 @@ pub(crate) fn render_swarm_message( let centered = markdown::center_code_blocks(); let title = msg.title.as_deref().unwrap_or("Swarm").trim(); - if let Some(lines) = render_compact_agent_notification(title, &msg.content, width) { + if let Some(lines) = + render_compact_agent_notification(title, &msg.content, msg.duration_secs, width) + { return lines; } if let Some(lines) = render_compact_swarm_await(title, &msg.content, width) { @@ -4121,6 +4237,7 @@ pub(crate) fn render_tool_message( ); let rendered_tool_line_text = super::line_plain_text(&rendered_tool_line); lines.push(rendered_tool_line); + lines.extend(tools_ui::codemode_preview(tc, row_width)); if is_error && (tc.name != "batch" || batch_counts.is_none()) { lines.push(render_tool_failure_reason(&msg.content, row_width, 2)); if centered { diff --git a/crates/jcode-tui/src/tui/ui_messages/tests.rs b/crates/jcode-tui/src/tui/ui_messages/tests.rs index d19dec692c7..33754e9c402 100644 --- a/crates/jcode-tui/src/tui/ui_messages/tests.rs +++ b/crates/jcode-tui/src/tui/ui_messages/tests.rs @@ -1457,6 +1457,7 @@ fn render_todos_message_shows_grouped_card_with_status_glyphs() { fn todo(id: &str, content: &str, status: &str, group: Option<&str>) -> crate::todo::TodoItem { crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: id.to_string(), content: content.to_string(), status: status.to_string(), @@ -1541,6 +1542,7 @@ fn render_todos_message_shows_grouped_card_with_status_glyphs() { fn render_todos_message_shows_goal_scores_without_verbose_feedback() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, id: "1".to_string(), @@ -1605,6 +1607,7 @@ fn render_todos_message_shows_user_intention_when_understanding_is_unclear() { let long_text = "This deliberately long assessment detail should not consume several rows in a narrow terminal window"; let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Keep the task visible".to_string(), status: "in_progress".to_string(), @@ -1692,6 +1695,7 @@ fn render_todos_message_shows_user_intention_when_understanding_is_unclear() { fn render_todos_message_uses_readable_semantic_colors() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Tune the palette".to_string(), status: "in_progress".to_string(), @@ -1882,6 +1886,7 @@ fn render_todos_message_color_codes_every_intent_state() { fn render_todos_message_collapses_passing_quality_gates() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Verify the result".to_string(), status: "completed".to_string(), @@ -1938,6 +1943,7 @@ fn render_todos_message_collapses_passing_quality_gates() { fn render_todos_message_wraps_goal_scores_at_narrow_widths() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Render the card".to_string(), status: "in_progress".to_string(), @@ -1983,6 +1989,7 @@ fn render_todos_message_wraps_goal_scores_at_narrow_widths() { fn render_todos_message_packs_multiple_goal_gates_per_row() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Render the card".to_string(), status: "in_progress".to_string(), @@ -2054,6 +2061,7 @@ fn render_todos_message_bad_payload_falls_back_to_system() { fn render_todo_tool_result_uses_borderless_card_with_goal_scores() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, id: "render".to_string(), diff --git a/crates/jcode-tui/src/tui/ui_messages/todo_completion.rs b/crates/jcode-tui/src/tui/ui_messages/todo_completion.rs index 81bd0dd4025..2ff3b10f952 100644 --- a/crates/jcode-tui/src/tui/ui_messages/todo_completion.rs +++ b/crates/jcode-tui/src/tui/ui_messages/todo_completion.rs @@ -163,7 +163,8 @@ mod tests { for title in [serde_json::Value::Null, serde_json::json!(" \n\t ")] { msg.content = serde_json::json!({ "todo_completion_version": 1, "plan": {"title": title} - }).to_string(); + }) + .to_string(); let lines = render_with_gate_diagnostics(&msg, 80, false).unwrap(); assert_eq!(lines.len(), 1); } diff --git a/crates/jcode-tui/src/tui/ui_messages/todo_payload_cache_tests.rs b/crates/jcode-tui/src/tui/ui_messages/todo_payload_cache_tests.rs new file mode 100644 index 00000000000..19872f2862c --- /dev/null +++ b/crates/jcode-tui/src/tui/ui_messages/todo_payload_cache_tests.rs @@ -0,0 +1,133 @@ +use super::*; +use std::sync::Arc; + +fn fixture(count: usize) -> String { + serde_json::json!({"todos": (0..count).map(|i| serde_json::json!({ + "id": format!("task-{i}"), "content": format!("Task {i} 恢复"), + "status": "pending", "priority": "high", + "external_blocker": {"reason": "Approve rollout", "review_at": "2099-01-01T00:00:00Z"} + })).collect::>()}) + .to_string() +} + +#[test] +fn todo_payload_cache_reuses_exact_content_and_replaces_changed_payload() { + let mut cache = TodoPayloadCache::default(); + let content = fixture(2); + let first = cache.get(&content).unwrap(); + let same_content = content.clone(); + assert!(Arc::ptr_eq(&first, &cache.get(&same_content).unwrap())); + let changed = content.replace("Task 0", "Task X"); + let next = cache.get(&changed).unwrap(); + assert!(!Arc::ptr_eq(&first, &next)); + assert_eq!(next.todos()[0].content, "Task X 恢复"); + assert!(!Arc::ptr_eq(&first, &cache.get(&content).unwrap())); + assert!(cache.get("invalid").is_none()); + assert!(cache.entry.as_ref().unwrap().1.is_none()); + assert!(cache.get("invalid").is_none()); + assert_eq!(cache.get("[]").unwrap().todos().len(), 0); +} + +#[test] +fn todo_payload_cache_does_not_retain_oversized_payloads() { + let mut cache = TodoPayloadCache::default(); + cache.get("[]").unwrap(); + let oversized = format!( + "{{\"todos\":[],\"extra\":\"{}\"}}", + "x".repeat(MAX_CACHED_TODO_PAYLOAD_BYTES) + ); + assert!(cache.get(&oversized).is_some()); + assert!(cache.entry.is_none()); + assert!( + cache + .get(&"x".repeat(MAX_CACHED_TODO_PAYLOAD_BYTES + 1)) + .is_none() + ); + assert!(cache.entry.is_none()); +} + +#[test] +fn todo_payload_cache_preserves_live_expiry_and_width_rendering() { + let content = fixture(1); + let before = "2098-12-31T23:59:59Z".parse().unwrap(); + let at_expiry = "2099-01-01T00:00:00Z".parse().unwrap(); + let parsed = cached_todo_payload(&content).unwrap(); + let wide = todo_blocker_footer_at(Some(&content), 120, before); + assert_eq!(wide.len(), 1); + for width in [0, 1, 8, 24, 80] { + let narrow = todo_blocker_footer_at(Some(&content), width, before); + assert_eq!(narrow.len(), 1); + assert!(narrow[0].width() <= usize::from(width)); + if width > 0 { + assert_eq!(narrow[0].style.fg, Some(todo_warning_color())); + } + } + assert!(todo_blocker_footer_at(Some(&content), 120, at_expiry).is_empty()); + assert!(Arc::ptr_eq( + &parsed, + &cached_todo_payload(&content).unwrap() + )); + assert_eq!(wide, todo_blocker_footer_at(Some(&content), 120, before)); + let resolved = content.replace("pending", "completed"); + assert!(todo_blocker_footer_at(Some(&resolved), 120, before).is_empty()); +} + +#[test] +fn todo_payload_cache_preserves_legacy_notes_and_resolved_content() { + let content = serde_json::json!([{ + "id": "task", "content": "Device", "status": "pending", "priority": "high", + "completely_blocked": {"reason": "Connect hardware"} + }]) + .to_string(); + for _ in 0..2 { + assert!( + todo_blocker_notes(Some(&content))[1] + .to_string() + .contains("Connect hardware") + ); + } + assert!(todo_blocker_notes(Some(&content.replace("pending", "completed"))).is_empty()); +} + +#[test] +#[ignore = "comparative hot-path benchmark, run with --ignored --nocapture"] +fn benchmark_todo_payload_cache_footer() { + use std::hint::black_box; + use std::time::Instant; + let now = "2098-12-31T23:59:59Z".parse().unwrap(); + for (count, blocked) in [(1, 1), (32, 1), (128, 1), (128, 128)] { + let mut value: serde_json::Value = serde_json::from_str(&fixture(count)).unwrap(); + for todo in value["todos"] + .as_array_mut() + .unwrap() + .iter_mut() + .skip(blocked) + { + todo.as_object_mut().unwrap().remove("external_blocker"); + } + let content = value.to_string(); + let iterations = 1000; + let parsed: TodoCardPayload = serde_json::from_str(&content).unwrap(); + assert_eq!( + render_todo_blocker_footer(&parsed, 80, now), + todo_blocker_footer_at(Some(&content), 80, now) + ); + let start = Instant::now(); + for _ in 0..iterations { + let parsed: TodoCardPayload = serde_json::from_str(black_box(&content)).unwrap(); + black_box(render_todo_blocker_footer(&parsed, 80, now)); + } + let uncached = start.elapsed(); + todo_blocker_footer_at(Some(&content), 80, now); + let start = Instant::now(); + for _ in 0..iterations { + black_box(todo_blocker_footer_at(Some(black_box(&content)), 80, now)); + } + let cached = start.elapsed(); + eprintln!( + "todo footer end-to-end count={count} blocked={blocked} bytes={} iterations={iterations} uncached={uncached:?} cached={cached:?} speedup={:.2}x", + content.len(), + uncached.as_secs_f64() / cached.as_secs_f64() + ); + } +} diff --git a/crates/jcode-tui/src/tui/ui_panel_image_preview.rs b/crates/jcode-tui/src/tui/ui_panel_image_preview.rs index d9a4f054c46..ba38713c115 100644 --- a/crates/jcode-tui/src/tui/ui_panel_image_preview.rs +++ b/crates/jcode-tui/src/tui/ui_panel_image_preview.rs @@ -13,6 +13,9 @@ pub(crate) fn clear_regions() { } pub(super) fn record_image(area: Rect, hash: u64) { + if super::secondary_render_active() { + return; + } if area.width > 0 && area.height > 0 { IMAGE_REGIONS.with(|regions| regions.borrow_mut().push((area, hash))); } diff --git a/crates/jcode-tui/src/tui/ui_prepare.rs b/crates/jcode-tui/src/tui/ui_prepare.rs index fca26b40d2d..fdd1941f013 100644 --- a/crates/jcode-tui/src/tui/ui_prepare.rs +++ b/crates/jcode-tui/src/tui/ui_prepare.rs @@ -290,6 +290,18 @@ fn first_mermaid_pending_message(prepared: &PreparedMessages) -> Option { ) } +fn active_codemode(app: &dyn TuiState) -> Option { + if !app.is_processing() + || !matches!(app.status(), ProcessingStatus::RunningTool(name) if name == "codemode") + { + return None; + } + app.streaming_tool_calls() + .into_iter() + .rev() + .find(|tool| tools_ui::canonical_tool_name(&tool.name) == "codemode") +} + fn active_batch_progress(app: &dyn TuiState) -> Option { match app.status() { ProcessingStatus::RunningTool(name) if name == "batch" => app.batch_progress(), @@ -298,6 +310,14 @@ fn active_batch_progress(app: &dyn TuiState) -> Option u64 { + if let Some(tool) = active_codemode(app) { + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + tool.id.hash(&mut hasher); + tool.input.to_string().hash(&mut hasher); + tool.intent.hash(&mut hasher); + super::activity_indicator_frame_index(app.animation_elapsed(), 12.5).hash(&mut hasher); + return hasher.finish(); + } let Some(progress) = active_batch_progress(app) else { return 0; }; @@ -389,6 +409,46 @@ fn prepare_active_batch_progress( width: u16, prefix_blank: bool, ) -> PreparedMessages { + if let Some(tool) = active_codemode(app) { + let block_width = if app.centered_mode() { + super::centered_content_block_width(width, 96) + } else { + width as usize + }; + let row_width = block_width.saturating_sub(1); + let spinner = super::activity_indicator(app.animation_elapsed(), 12.5); + let mut lines = Vec::new(); + if prefix_blank { + lines.push(Line::from("")); + } + let mut header = vec![ + Span::styled( + format!(" {spinner} "), + Style::default().fg(rgb(255, 193, 94)), + ), + Span::styled("codemode", Style::default().fg(tool_color())), + ]; + if let Some(intent) = tool + .intent + .as_deref() + .filter(|intent| !intent.trim().is_empty()) + { + header.push(Span::styled(" · ", Style::default().fg(dim_color()))); + header.push(Span::styled( + intent.to_owned(), + Style::default().fg(tool_color()), + )); + } + lines.push(super::truncate_line_with_ellipsis_to_width( + &Line::from(header), + row_width, + )); + lines.extend(tools_ui::codemode_preview(&tool, row_width)); + if app.centered_mode() { + super::left_pad_lines_to_block_width(&mut lines, width, block_width); + } + return wrap_lines_with_map(lines, &[], &[], &[], &[], &[], width, &[], &[], &[]); + } let Some(progress) = active_batch_progress(app) else { return empty_prepared_messages(); }; @@ -634,7 +694,7 @@ fn prepare_messages_inner(app: &dyn TuiState, width: u16, height: u16) -> Prepar }; let batch_start = Instant::now(); - let has_batch_progress = active_batch_progress(app).is_some(); + let has_batch_progress = active_batch_progress(app).is_some() || active_codemode(app).is_some(); let batch_prefix_blank = has_batch_progress && !body_prepared.wrapped_lines.is_empty(); let batch_progress_prepared = if has_batch_progress { Arc::new(prepare_active_batch_progress( @@ -867,6 +927,13 @@ struct HeaderPrepCacheState { fn header_prep_cache() -> &'static std::sync::Mutex> { static CACHE: std::sync::OnceLock>> = std::sync::OnceLock::new(); + // Single-slot cache: a secondary (preview) render must not evict the live + // session's header every frame, so it gets a slot of its own. + static SECONDARY: std::sync::OnceLock>> = + std::sync::OnceLock::new(); + if super::secondary_render_active() { + return SECONDARY.get_or_init(|| std::sync::Mutex::new(None)); + } CACHE.get_or_init(|| std::sync::Mutex::new(None)) } @@ -1177,6 +1244,7 @@ struct BodyRenderCtx<'a> { /// Render saved history through the live dispatcher and wrapping pipeline. /// No active App is borrowed: expansion state, images and in-flight swarm work /// from the session behind the picker must never leak into this transcript. +#[cfg(test)] pub(crate) fn prepare_history_preview( messages: &[DisplayMessage], hidden_prompts: usize, @@ -1187,12 +1255,14 @@ pub(crate) fn prepare_history_preview( .unwrap_or_else(|| preview_fallback::unavailable(width)) } +#[cfg(test)] #[path = "ui_prepare/preview_fallback.rs"] mod preview_fallback; /// Choose scrollbar width without fully rendering overflowing history twice. /// The budget excludes caller-owned header and padding rows. The flag is the /// wide layout's strict overflow decision, not a second narrow-layout decision. +#[cfg(test)] pub(crate) fn prepare_history_preview_for_viewport( messages: &[DisplayMessage], hidden_prompts: usize, @@ -1222,6 +1292,7 @@ pub(crate) fn prepare_history_preview_for_viewport( ) } +#[cfg(test)] fn prepare_history_preview_bounded( messages: &[DisplayMessage], hidden_prompts: usize, @@ -2531,6 +2602,7 @@ fn wrap_lines( } let image_regions = compute_image_regions(&wrapped_lines); + shrink_wrapped_lines(&mut wrapped_lines); let wrapped_plain_lines = Arc::new(wrapped_lines.iter().map(ui::line_plain_text).collect()); @@ -2552,6 +2624,16 @@ fn wrap_lines( } } +/// Drop spare span and line capacity before a body is cached. Wrapping builds +/// lines by pushing spans, so a long session kept ~1.7x the span slots it used, +/// and these bodies live in the body cache for the session's lifetime. +fn shrink_wrapped_lines(wrapped_lines: &mut Vec>) { + for line in wrapped_lines.iter_mut() { + line.spans.shrink_to_fit(); + } + wrapped_lines.shrink_to_fit(); +} + #[expect( clippy::too_many_arguments, reason = "Wrapped-line preparation carries explicit render state to avoid hidden coupling" @@ -2692,6 +2774,7 @@ fn wrap_lines_with_map( }); } + shrink_wrapped_lines(&mut wrapped_lines); let wrapped_plain_lines = Arc::new(wrapped_lines.iter().map(ui::line_plain_text).collect()); // Translate the per-message unwrapped-line boundaries into wrapped-line diff --git a/crates/jcode-tui/src/tui/ui_prepare/preview_fallback.rs b/crates/jcode-tui/src/tui/ui_prepare/preview_fallback.rs index 7f02070f84c..ecdef59ea75 100644 --- a/crates/jcode-tui/src/tui/ui_prepare/preview_fallback.rs +++ b/crates/jcode-tui/src/tui/ui_prepare/preview_fallback.rs @@ -1,6 +1,7 @@ //! Recoverable placeholder for an unexpectedly incomplete unbounded preview. use super::*; +#[cfg(test)] pub(super) fn unavailable(width: u16) -> PreparedMessages { crate::logging::warn("Unbounded history preview unexpectedly failed to complete"); wrap_lines_with_map( diff --git a/crates/jcode-tui/src/tui/ui_smoothness.rs b/crates/jcode-tui/src/tui/ui_smoothness.rs index dee578fcbd6..da54a1257fc 100644 --- a/crates/jcode-tui/src/tui/ui_smoothness.rs +++ b/crates/jcode-tui/src/tui/ui_smoothness.rs @@ -25,23 +25,42 @@ fn recorder() -> &'static Mutex { /// Hash one buffer row's cell symbols within `area`. Returns /// [`BLANK_ROW_HASH`] for visually blank rows. fn hash_row(buffer: &Buffer, area: Rect, y: u16) -> u64 { - use std::hash::{Hash, Hasher}; - let mut hasher = std::collections::hash_map::DefaultHasher::new(); + // FNV-1a over symbol bytes: this runs over every visible cell each frame, + // and SipHash via `DefaultHasher` was a measurable share of release CPU. + const PRIME: u64 = 0x0000_0100_0000_01b3; + let mut hash: u64 = 0xcbf2_9ce4_8422_2325; let mut blank = true; - for x in area.left()..area.right() { - if let Some(cell) = buffer.cell((x, y)) { - let symbol = cell.symbol(); - if blank && !symbol.trim().is_empty() { + let buf_area = *buffer.area(); + if y < buf_area.top() || y >= buf_area.bottom() { + return BLANK_ROW_HASH; + } + let left = area.left().max(buf_area.left()); + let right = area.right().min(buf_area.right()); + if left >= right { + return BLANK_ROW_HASH; + } + let start = buffer.index_of(left, y); + for cell in &buffer.content[start..start + usize::from(right - left)] { + let bytes = cell.symbol().as_bytes(); + // Single-byte symbols (the common case) are checked for blankness + // without the Unicode `trim()` walk. + if let [b] = bytes { + if blank && !char::from(*b).is_whitespace() { + blank = false; + } + hash = (hash ^ u64::from(*b)).wrapping_mul(PRIME); + } else { + if blank && !cell.symbol().trim().is_empty() { blank = false; } - symbol.hash(&mut hasher); + for &byte in bytes { + hash = (hash ^ u64::from(byte)).wrapping_mul(PRIME); + } } + // Cell separator so "ab","c" differs from "a","bc". + hash = (hash ^ 0xff).wrapping_mul(PRIME); } - if blank { - BLANK_ROW_HASH - } else { - hasher.finish() - } + if blank { BLANK_ROW_HASH } else { hash } } /// Build an [`AnchorFrame`] from a rendered buffer region. Shared by the live diff --git a/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs b/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs index 8a042e672f6..3da973f479d 100644 --- a/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs +++ b/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs @@ -1043,3 +1043,105 @@ fn test_prepare_body_collapses_anchored_images_when_inline_images_hidden() { #[path = "cache_equivalence_tests.rs"] mod cache_equivalence; use cache_equivalence::assert_prepared_equivalent; + +/// Frames keyed on partial streaming text can never be hit again (the stream +/// only grows and the end of the turn bumps `messages_version`), so a new +/// insert must not keep them pinned. Settled frames stay cached. +#[test] +fn test_full_prep_cache_drops_superseded_streaming_frames() { + let mut cache = FullPrepCacheState::default(); + let key_for = |streaming_text_len: usize, width: u16| FullPrepCacheKey { + width, + height: 20, + diff_mode: crate::config::DiffDisplayMode::Off, + messages_version: 1, + diagram_mode: crate::config::DiagramDisplayMode::Pinned, + centered: false, + compact_thinking: false, + mermaid_aspect_bucket: None, + is_processing: streaming_text_len > 0, + streaming_text_len, + streaming_text_hash: streaming_text_len as u64, + batch_progress_hash: 0, + inline_images_signature: (0, 0), + expanded_images_version: 0, + swarm_members_signature: 0, + latex_rendering: crate::config::LatexRenderingMode::Image, + show_link_urls: false, + inline_images_visible: true, + }; + let prepared = || { + make_prepared_chat_frame(Arc::new(PreparedMessages { + wrapped_lines: vec![Line::from("x")], + wrapped_plain_lines: Arc::new(vec!["x".to_string()]), + wrapped_copy_offsets: Arc::new(vec![0]), + raw_plain_lines: Arc::new(Vec::new()), + wrapped_line_map: Arc::new(Vec::new()), + wrapped_user_indices: Vec::new(), + wrapped_user_prompt_starts: Vec::new(), + wrapped_user_prompt_ends: Vec::new(), + user_prompt_texts: Vec::new(), + image_regions: Vec::new(), + edit_tool_ranges: Vec::new(), + copy_targets: Vec::new(), + message_boundaries: Vec::new(), + mermaid_pending_epoch: None, + })) + }; + + cache.insert(key_for(0, 80), prepared()); + cache.insert(key_for(10, 80), prepared()); + cache.insert(key_for(20, 80), prepared()); + + assert!(cache.get_exact(&key_for(0, 80)).is_some(), "settled frame kept"); + assert!(cache.get_exact(&key_for(10, 80)).is_none(), "stale stream frame dropped"); + assert!(cache.get_exact(&key_for(20, 80)).is_some(), "newest stream frame kept"); +} + +#[test] +fn test_body_cache_drops_superseded_message_versions() { + let mut cache = BodyCacheState::default(); + let key_for = |width: u16, version: u64| BodyCacheKey { + width, + diff_mode: crate::config::DiffDisplayMode::Off, + messages_version: version, + diagram_mode: crate::config::DiagramDisplayMode::Pinned, + centered: false, + compact_thinking: false, + mermaid_aspect_bucket: None, + pin_images: true, + inline_images_visible: true, + images_signature: (0, 0), + expanded_images_version: 0, + swarm_members_signature: 0, + latex_rendering: crate::config::LatexRenderingMode::Image, + show_link_urls: false, + }; + let prepared = || { + Arc::new(PreparedMessages { + wrapped_lines: vec![Line::from("x")], + wrapped_plain_lines: Arc::new(vec!["x".to_string()]), + wrapped_copy_offsets: Arc::new(vec![0]), + raw_plain_lines: Arc::new(Vec::new()), + wrapped_line_map: Arc::new(Vec::new()), + wrapped_user_indices: Vec::new(), + wrapped_user_prompt_starts: Vec::new(), + wrapped_user_prompt_ends: Vec::new(), + user_prompt_texts: Vec::new(), + image_regions: Vec::new(), + edit_tool_ranges: Vec::new(), + copy_targets: Vec::new(), + message_boundaries: Vec::new(), + mermaid_pending_epoch: None, + }) + }; + for version in 1..=5 { + cache.insert(key_for(80, version), prepared(), version as usize, 0); + } + // Other layouts are kept for fast width toggles. + cache.insert(key_for(120, 3), prepared(), 3, 0); + + assert_eq!(cache.entries.len(), 2); + assert!(cache.get_exact(&key_for(80, 5)).is_some()); + assert!(cache.get_exact(&key_for(120, 3)).is_some()); +} diff --git a/crates/jcode-tui/src/tui/ui_tests/basic/redraw_cadence.rs b/crates/jcode-tui/src/tui/ui_tests/basic/redraw_cadence.rs index ff4722498c7..1f089f6097f 100644 --- a/crates/jcode-tui/src/tui/ui_tests/basic/redraw_cadence.rs +++ b/crates/jcode-tui/src/tui/ui_tests/basic/redraw_cadence.rs @@ -320,3 +320,75 @@ fn a_notice_screen_left_alone_still_reaches_deep_idle() { "a dormant notice screen must tick at the deep-idle crawl" ); } + +/// A turn that already streamed text and is now thinking or running a tool +/// shows a fully revealed transcript. Repainting it at the fast cadence redrew +/// identical cells 66 times a second on a live client (about 0.2 cores per +/// waiting session). Only the spinner and timer change, so it must drop to the +/// spinner cadence, while a pending reveal backlog keeps the fast cadence. +#[test] +fn fully_revealed_stream_waiting_on_the_turn_drops_to_spinner_cadence() { + let policy = full_tier_policy(); + let fast_interval = Duration::from_millis(1000 / u64::from(policy.redraw_fps.max(1))); + + let mut waiting = static_chrome_state(None); + waiting.status = ProcessingStatus::Streaming; + waiting.streaming_text = "partial assistant answer".to_string(); + waiting.stream_reveal_pending = Some(false); + // TestState::is_processing derives from status. + assert_eq!( + crate::tui::redraw_interval_with_policy(&waiting, &policy), + crate::tui::REDRAW_STATIC_STREAM, + "fully revealed streamed text must not repaint at the fast cadence" + ); + + let mut revealing = waiting.clone(); + revealing.stream_reveal_pending = Some(true); + assert_eq!( + crate::tui::redraw_interval_with_policy(&revealing, &policy), + fast_interval, + "a reveal backlog must keep the smooth fast cadence" + ); +} + +/// Background panes stay live but must not repaint at full frame rate: a swarm +/// keeps many unfocused clients streaming at once. +#[test] +fn unfocused_streaming_client_is_capped_but_focused_is_not() { + let policy = full_tier_policy(); + let mut state = TestState { + status: ProcessingStatus::Streaming, + streaming_text: "partial reply".to_string(), + stream_reveal_pending: Some(true), + ..Default::default() + }; + let focused = crate::tui::redraw_interval_with_policy(&state, &policy); + assert!(focused < crate::tui::redraw_schedule::REDRAW_UNFOCUSED_MIN); + state.unfocused = true; + assert_eq!( + crate::tui::redraw_interval_with_policy(&state, &policy), + crate::tui::redraw_schedule::REDRAW_UNFOCUSED_MIN + ); +} + +/// A background turn with nothing left to reveal (thinking, running a tool) +/// only moves a spinner, so it ticks at the 1s liveness rate. Measured live: +/// such sessions held 10 full frames/s each before. +#[test] +fn unfocused_turn_without_reveal_backlog_ticks_at_liveness_rate() { + let policy = full_tier_policy(); + for text in ["", "already revealed reply"] { + let state = TestState { + status: ProcessingStatus::Streaming, + streaming_text: text.to_string(), + stream_reveal_pending: Some(false), + unfocused: true, + ..Default::default() + }; + assert_eq!( + crate::tui::redraw_interval_with_policy(&state, &policy), + crate::tui::redraw_schedule::REDRAW_PASSIVE_LIVENESS, + "text={text:?}" + ); + } +} diff --git a/crates/jcode-tui/src/tui/ui_tests/codemode_preview.rs b/crates/jcode-tui/src/tui/ui_tests/codemode_preview.rs new file mode 100644 index 00000000000..32455f2776f --- /dev/null +++ b/crates/jcode-tui/src/tui/ui_tests/codemode_preview.rs @@ -0,0 +1,99 @@ +use super::*; + +fn tool(code: &str) -> ToolCall { + ToolCall { + id: "codemode-preview".into(), + name: "codemode".into(), + input: serde_json::json!({"code": code}), + intent: Some("Inspect project files".into()), + thought_signature: None, + } +} + +#[test] +fn codemode_preview_preserves_source_and_bounds_visual_rows() { + let _guard = viewport_snapshot_test_lock(); + let source = "const value = 1\n return value"; + let lines = tools_ui::codemode_preview(&tool(source), 80); + assert!(lines[0].spans.iter().any(|span| span.style.fg.is_some())); + assert_eq!( + lines.iter().map(extract_line_text).collect::>(), + vec![" const value = 1", " return value"] + ); + let long = (0..10) + .map(|i| format!("return {i}")) + .collect::>() + .join("\n"); + let lines = tools_ui::codemode_preview(&tool(&long), 80); + assert_eq!(lines.len(), 7); + assert!(extract_line_text(lines.last().unwrap()).contains("4 more lines")); + assert_eq!( + tools_ui::codemode_preview(&tool(&"return 1\n".repeat(6)), 80).len(), + 6 + ); + for width in [1, 2, 4, 8, 20, 40] { + let lines = tools_ui::codemode_preview(&tool(&"const 世界 = 'abcdef'; ".repeat(30)), width); + assert!(lines.len() <= 7); + assert!( + lines.iter().all(|line| line.width() <= width), + "{width}: {lines:?}" + ); + } + let lines = tools_ui::codemode_preview(&tool(&"x".repeat(200)), 20); + assert!(extract_line_text(lines.last().unwrap()).contains("code continues")); + assert!(tools_ui::codemode_preview(&tool(""), 80).is_empty()); + assert!(tools_ui::codemode_preview(&tool("return 1"), 0).is_empty()); + let mut other = tool("return 1"); + other.name = "bash".into(); + assert!(tools_ui::codemode_preview(&other, 80).is_empty()); +} + +#[test] +fn codemode_preview_visible_on_success_and_failure() { + let _guard = viewport_snapshot_test_lock(); + for content in ["result: 1", "error: TypeError: example"] { + let msg = DisplayMessage { + role: "tool".into(), + content: content.into(), + tool_calls: vec![], + duration_secs: None, + title: None, + tool_data: Some(tool("return 1")), + }; + let lines = messages::render_tool_message(&msg, 100, crate::config::DiffDisplayMode::Off); + let all = lines + .iter() + .map(extract_line_text) + .collect::>() + .join("\n"); + assert!(all.contains("return 1"), "{all}"); + } +} + +#[test] +fn codemode_preview_live_updates_and_disappears_when_idle() { + let _guard = viewport_snapshot_test_lock(); + for centered in [false, true] { + let mut state = TestState { + status: ProcessingStatus::RunningTool("codemode".into()), + streaming_tools: vec![tool("return 1")], + centered_mode: centered, + ..Default::default() + }; + let render = |state: &TestState| { + prepare::prepare_messages(state, 80, 30) + .materialize_all_lines() + .iter() + .map(extract_line_text) + .collect::>() + .join("\n") + }; + assert!(render(&state).contains("return 1")); + state.streaming_tools[0] = tool("return 2"); + let updated = render(&state); + assert!(updated.contains("return 2"), "{updated}"); + assert!(!updated.contains("return 1")); + state.status = ProcessingStatus::Idle; + assert!(!render(&state).contains("return 2")); + } +} diff --git a/crates/jcode-tui/src/tui/ui_tests/mod.rs b/crates/jcode-tui/src/tui/ui_tests/mod.rs index 987b4a21798..3d3a7eff546 100644 --- a/crates/jcode-tui/src/tui/ui_tests/mod.rs +++ b/crates/jcode-tui/src/tui/ui_tests/mod.rs @@ -1,9 +1,9 @@ use super::*; mod cloud_directories; +mod codemode_preview; mod preview_redraw_schedule; mod resume_parity; -mod stable_status; use crate::tui::session_picker; use crate::tui::ui::tools_ui; @@ -130,14 +130,12 @@ struct TestState { display_messages: Vec, messages_version: u64, streaming_text: String, + streaming_tools: Vec, batch_progress: Option, queued_messages: Vec, pending_soft_interrupts: Vec, interleave_message: Option, status: ProcessingStatus, - status_elapsed: Option, - status_rate_limit: Option, - status_background: Vec, queue_mode: bool, active_skill: Option, centered_mode: bool, @@ -145,6 +143,8 @@ struct TestState { anim_elapsed: f32, now_ms: u64, time_since_activity: Option, + /// `Some(pending)` overrides the trait's conservative default. + stream_reveal_pending: Option, remote_startup_phase_active: bool, inline_view_state: Option, inline_interactive_state: Option, @@ -273,10 +273,10 @@ impl crate::tui::TuiState for TestState { None } fn streaming_tool_calls(&self) -> Vec { - Vec::new() + self.streaming_tools.clone() } fn elapsed(&self) -> Option { - self.status_elapsed + None } fn status(&self) -> ProcessingStatus { self.status.clone() @@ -296,6 +296,9 @@ impl crate::tui::TuiState for TestState { fn time_since_activity(&self) -> Option { self.time_since_activity } + fn stream_reveal_pending(&self) -> bool { + self.stream_reveal_pending.unwrap_or(true) + } fn total_session_tokens(&self) -> Option<(u64, u64)> { None } @@ -366,10 +369,7 @@ impl crate::tui::TuiState for TestState { self.anim_elapsed } fn rate_limit_remaining(&self) -> Option { - self.status_rate_limit - } - fn background_task_rows(&self) -> &[crate::tui::BackgroundTaskRow] { - &self.status_background + None } fn queue_mode(&self) -> bool { self.queue_mode diff --git a/crates/jcode-tui/src/tui/ui_tests/rendering.rs b/crates/jcode-tui/src/tui/ui_tests/rendering.rs index f5fa0281ad0..64dca92aad5 100644 --- a/crates/jcode-tui/src/tui/ui_tests/rendering.rs +++ b/crates/jcode-tui/src/tui/ui_tests/rendering.rs @@ -188,7 +188,10 @@ fn test_render_file_activity_as_collapsible_compact_row() { .map(extract_line_text) .collect::>(); - assert_eq!(rendered, vec!["🦊 ✎ src/auth.rs · modified ▸ diff"]); + assert_eq!( + rendered, + vec!["🦊 ✎ src/auth.rs · modified 11 tok ▸ diff"] + ); assert!(rendered.iter().all(|line| !line.contains('│'))); } diff --git a/crates/jcode-tui/src/tui/ui_tests/stable_status.rs b/crates/jcode-tui/src/tui/ui_tests/stable_status.rs deleted file mode 100644 index 0703f0a4cd3..00000000000 --- a/crates/jcode-tui/src/tui/ui_tests/stable_status.rs +++ /dev/null @@ -1,153 +0,0 @@ -use super::*; -use ratatui::{Terminal, backend::TestBackend}; - -fn render_status(state: &TestState, width: u16, queued: usize) -> Vec { - // A second sentinel row catches accidental wrapping or layout spillover. - let mut terminal = Terminal::new(TestBackend::new(width.max(1), 2)).unwrap(); - terminal - .draw(|frame| { - input_ui::draw_status(frame, state, Rect::new(0, 0, width, 1), queued); - }) - .unwrap(); - let buffer = terminal.backend().buffer(); - (0..2) - .map(|y| { - (0..width.max(1)) - .map(|x| buffer[(x, y)].symbol()) - .collect::() - }) - .collect() -} - -fn phases() -> Vec { - vec![ - ProcessingStatus::Sending, - ProcessingStatus::Connecting(crate::message::ConnectionPhase::Authenticating), - ProcessingStatus::Connecting(crate::message::ConnectionPhase::Connecting), - ProcessingStatus::Connecting(crate::message::ConnectionPhase::SendingRequest), - ProcessingStatus::Connecting(crate::message::ConnectionPhase::WaitingForResponse), - ProcessingStatus::Thinking(std::time::Instant::now()), - ProcessingStatus::Streaming, - ProcessingStatus::RunningTool("bash".into()), - ProcessingStatus::RunningTool("batch".into()), - ProcessingStatus::WaitingForNetwork { - listener: "界\nlistener".into(), - }, - ] -} - -#[test] -fn stable_status_phase_transitions_preserve_clock_and_activity_columns() { - let mut clock_column = None; - let mut activity_column = None; - for status in phases() { - let state = TestState { - status, - status_elapsed: Some(Duration::from_secs(12)), - ..Default::default() - }; - let rows = render_status(&state, 120, 0); - let text = &rows[0]; - let clock = text.find("00:12").expect(text); - let activity = text.find('│').expect(text); - assert_eq!(*clock_column.get_or_insert(clock), clock, "{text}"); - assert_eq!(*activity_column.get_or_insert(activity), activity, "{text}"); - assert!(rows[1].trim().is_empty(), "{rows:?}"); - } -} - -#[test] -fn stable_status_centered_mode_does_not_move_changing_text() { - for status in phases() { - let mut state = TestState { - status, - status_elapsed: Some(Duration::from_secs(12)), - ..Default::default() - }; - let left = render_status(&state, 100, 0); - state.centered_mode = true; - assert_eq!(left, render_status(&state, 100, 0)); - } -} - -#[test] -fn stable_status_narrow_widths_do_not_wrap_or_panic() { - for status in phases() { - let state = TestState { - status, - status_elapsed: Some(Duration::from_secs(360_000)), - status_background: vec![crate::tui::BackgroundTaskRow { - task_id: "bg".into(), - label: "界\nlong\tbackground intent".repeat(20), - percent: None, - status: crate::tui::BackgroundTaskRowStatus::Running, - completed_at: None, - }], - ..Default::default() - }; - for width in 0..=120 { - let rows = render_status(&state, width, 1234); - assert!(rows[1].trim().is_empty(), "width={width}: {rows:?}"); - assert!(!rows[0].contains('\n') && !rows[0].contains('\t')); - } - } -} - -#[test] -fn stable_status_rate_limit_uses_the_same_clock_position() { - let mut state = TestState { - status: ProcessingStatus::Sending, - status_elapsed: Some(Duration::from_secs(12)), - ..Default::default() - }; - let active = render_status(&state, 100, 0); - state.status_rate_limit = Some(Duration::from_secs(80)); - let limited = render_status(&state, 100, 0); - assert_eq!(active[0].find("00:12"), limited[0].find("00:12")); - assert!(limited[0].to_lowercase().contains("retry"), "{limited:?}"); -} - -#[test] -fn stable_status_idle_does_not_rotate_tips() { - let mut state = TestState::default(); - let initial = render_status(&state, 100, 0); - for elapsed in [1.0, 15.0, 60.0, 120.0, 1000.0] { - state.anim_elapsed = elapsed; - assert_eq!(initial, render_status(&state, 100, 0)); - } -} - -#[test] -fn stable_status_badges_do_not_move_activity_or_telemetry() { - let mut state = TestState { - status: ProcessingStatus::RunningTool("bash".into()), - status_elapsed: Some(Duration::from_secs(12)), - ..Default::default() - }; - let before = render_status(&state, 120, 0); - state.status_background = (0..100) - .map(|index| crate::tui::BackgroundTaskRow { - task_id: index.to_string(), - label: "unbounded background intent".repeat(20), - percent: None, - status: crate::tui::BackgroundTaskRowStatus::Running, - completed_at: None, - }) - .collect(); - let after = render_status(&state, 120, 100); - assert_eq!(before[0].find("bash"), after[0].find("bash")); - assert_eq!(before[0].find('↑'), after[0].find('↑')); - assert!( - after[0].contains("[+99+ queued]") && after[0].contains("[bg 99+]"), - "{after:?}" - ); - state.status_background[0].status = crate::tui::BackgroundTaskRowStatus::Completed; - let completed = render_status(&state, 120, 1); - assert!(completed[0].contains("[bg 99]"), "{completed:?}"); - assert!(!completed[0].contains("unbounded")); - let without_queue = render_status(&state, 120, 0); - assert_eq!(completed[0].find("[bg"), without_queue[0].find("[bg")); - state.status = ProcessingStatus::Idle; - let idle = render_status(&state, 120, 0); - assert_eq!(without_queue[0].find("[bg"), idle[0].find("[bg")); -} diff --git a/crates/jcode-tui/src/tui/ui_tests/todo_blocker_notes.rs b/crates/jcode-tui/src/tui/ui_tests/todo_blocker_notes.rs index 2d144b271bf..80cc9ec9475 100644 --- a/crates/jcode-tui/src/tui/ui_tests/todo_blocker_notes.rs +++ b/crates/jcode-tui/src/tui/ui_tests/todo_blocker_notes.rs @@ -16,7 +16,7 @@ fn blocker_footer_is_cell_bounded_and_clears_when_resolved() { assert!(lines.iter().all(|line| line.width() <= usize::from(width))); if width > 0 { for line in &lines { - assert_eq!(line.style.fg, Some(Color::Rgb(225, 180, 80))); + assert_eq!(line.style.fg, Some(rgb(225, 180, 80))); } } if width >= 80 { @@ -66,7 +66,7 @@ fn blocker_footer_renders_immediately_above_input() { ); let blocker_row = &terminal.backend().buffer().content[(input_row - 1) * 120..input_row * 120]; for cell in blocker_row.iter().take("Blocked".len()) { - assert_eq!(cell.fg, Color::Rgb(225, 180, 80)); + assert_eq!(cell.fg, rgb(225, 180, 80)); } } diff --git a/crates/jcode-tui/src/tui/ui_todo_changes.rs b/crates/jcode-tui/src/tui/ui_todo_changes.rs index 49cb740b63f..61eb4281374 100644 --- a/crates/jcode-tui/src/tui/ui_todo_changes.rs +++ b/crates/jcode-tui/src/tui/ui_todo_changes.rs @@ -505,6 +505,7 @@ mod tests { priority: "medium".to_string(), group: None, lane_after: None, + lane_owner: None, confidence: None, completion_confidence: None, confidence_history: Vec::new(), diff --git a/crates/jcode-tui/src/tui/ui_todo_stages.rs b/crates/jcode-tui/src/tui/ui_todo_stages.rs index dbbe88ddba6..44a18677982 100644 --- a/crates/jcode-tui/src/tui/ui_todo_stages.rs +++ b/crates/jcode-tui/src/tui/ui_todo_stages.rs @@ -773,8 +773,11 @@ const MIN_TITLE_CELLS: usize = 8; fn lane_titles_spans(lane_todos: &[&crate::todo::TodoItem], width: usize) -> Vec> { let comma = || Span::styled(", ", Style::default().fg(rgb(105, 100, 135))); let title = |todo: &crate::todo::TodoItem| -> Span<'static> { + // Index 2 is the title. Trailing spans (blocker suffix) are shown in + // the blocked footer; budgets below are sized from the title width. todo_card_item_spans(todo, false) - .pop() + .into_iter() + .nth(2) .unwrap_or_else(|| Span::raw(todo.content.clone())) }; // Every shown title gets a fair share of the row, so a long first title @@ -952,7 +955,7 @@ pub(crate) fn push_stage_lines( inner_width, )); } - push_lane_status_lines(lines, &items, agents, base_indent, inner_width); + push_lane_status_lines_below_rows(lines, &items, agents, base_indent, inner_width); continue; } let statuses = lane_item_statuses(&items, agents); @@ -1063,6 +1066,57 @@ pub(super) fn push_lane_status_lines( agents: &[TodoCardAgent], indent: &str, width: usize, +) { + push_lane_status_lines_inner(lines, todos, agents, indent, width, false); +} + +/// Like [`push_lane_status_lines`], for callers that already render each todo +/// row: a waiting worker's reason is folded onto its row by +/// [`todo_wait_note`], so it does not get a second line here. +pub(super) fn push_lane_status_lines_below_rows( + lines: &mut Vec>, + todos: &[&crate::todo::TodoItem], + agents: &[TodoCardAgent], + indent: &str, + width: usize, +) { + push_lane_status_lines_inner(lines, todos, agents, indent, width, true); +} + +/// `Waiting for · 18m` for a live worker blocked on this todo. +pub(crate) fn todo_wait_note( + todo: &crate::todo::TodoItem, + agents: &[TodoCardAgent], +) -> Option { + if crate::todo::todo_completely_blocked(todo) || is_done(todo) { + return None; + } + let agent = todo + .assigned_to + .as_deref() + .and_then(|owner| agents.iter().find(|agent| agent.matches(owner)))?; + if !is_live_status(&agent.status) && agent.status != "waiting" { + return None; + } + let reason = agent.wait_reason.as_deref()?.lines().next()?.trim(); + if reason.is_empty() { + return None; + } + let mut note = reason.to_string(); + if let Some(age) = agent.wait_age_secs.filter(|age| *age >= 2) { + note.push_str(" · "); + note.push_str(&jcode_tui_render::swarm_gallery::humanize_age(age)); + } + Some(note) +} + +fn push_lane_status_lines_inner( + lines: &mut Vec>, + todos: &[&crate::todo::TodoItem], + agents: &[TodoCardAgent], + indent: &str, + width: usize, + fold_waiting: bool, ) { let mut seen = std::collections::HashSet::new(); for todo in todos { @@ -1079,6 +1133,9 @@ pub(super) fn push_lane_status_lines( if agent.live_status.is_none() && todo.status != "in_progress" { continue; } + if fold_waiting && todo_wait_note(todo, agents).is_some() { + continue; + } if !seen.insert(agent.session_id.as_str()) { continue; } diff --git a/crates/jcode-tui/src/tui/ui_todo_stages_tests.rs b/crates/jcode-tui/src/tui/ui_todo_stages_tests.rs index abc1317ec84..6bae7293e88 100644 --- a/crates/jcode-tui/src/tui/ui_todo_stages_tests.rs +++ b/crates/jcode-tui/src/tui/ui_todo_stages_tests.rs @@ -1176,3 +1176,49 @@ fn failed_lane_blocks_its_phase_and_offers_retry() { #[path = "ui_todo_worker_heading_tests.rs"] mod worker_headings; + +/// Lanes that expand per todo (waiting on a swarm await) must not add a +/// second "Waiting for ..." row under the worker's task. +#[test] +fn waiting_worker_reason_shares_its_todo_row() { + let render_at = |width: u16| { + let mut item = laned("M4 project loading gaps", "in_progress", 1, "Crates"); + item.assigned_to = Some("fox".into()); + item.waiting_on = vec!["await_1".into()]; + let mut fox = agent("fox", "running", None, Some(12)); + fox.wait_reason = Some("Waiting for cargo test suite".into()); + fox.wait_age_secs = Some(1128); + fox.live_status = Some(crate::protocol::SwarmLiveStatus { + phase: "waiting".into(), + elapsed_ms: 12_000, + ..Default::default() + }); + crate::tui::markdown::set_center_code_blocks(false); + let payload = serde_json::json!({ + "todos": [item], "plan": {}, "goals": [], "agents": [fox], + "background_tasks": [{"task_id": "await_1", "label": "Swarm await"}], + }); + let msg = DisplayMessage::todos(payload.to_string()); + render_pinned_todos_message(&msg, width, crate::config::DiffDisplayMode::Off) + .iter() + .map(|line| { + line.spans + .iter() + .map(|s| s.content.as_ref()) + .collect::() + }) + .collect::>() + }; + let rows = render_at(180); + let joined = rows.join("\n"); + let row = rows + .iter() + .find(|row| row.contains("M4 project loading gaps")) + .unwrap_or_else(|| panic!("{joined}")); + assert!(row.contains("Waiting for cargo test suite"), "{joined}"); + assert!(row.contains("Swarm await"), "{joined}"); + assert_eq!(joined.matches("Waiting for").count(), 1, "{joined}"); + for width in 0..120 { + assert_fits(&render_at(width), width); + } +} diff --git a/crates/jcode-tui/src/tui/ui_tools.rs b/crates/jcode-tui/src/tui/ui_tools.rs index 3eca1bd2c15..a00983e4cb4 100644 --- a/crates/jcode-tui/src/tui/ui_tools.rs +++ b/crates/jcode-tui/src/tui/ui_tools.rs @@ -4,6 +4,63 @@ use super::{dim_color, rgb, tool_color, truncate_line_preserving_suffix_to_width use ratatui::prelude::*; use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; +/// Source is a dedicated body, independent of the optional technical summary. +/// Bound visual rows, not source lines: agents frequently submit one long line. +pub(super) fn codemode_preview(tool: &ToolCall, width: usize) -> Vec> { + if canonical_tool_name(&tool.name) != "codemode" { + return Vec::new(); + } + let Some(code) = tool.input.get("code").and_then(|v| v.as_str()) else { + return Vec::new(); + }; + if code.trim().is_empty() || width == 0 { + return Vec::new(); + } + const MAX_ROWS: usize = 6; + let indent = width.saturating_sub(1).min(4); + let inner_width = width.saturating_sub(indent).max(1); + let mut rows = Vec::new(); + let mut source = code.lines().peekable(); + while let Some(line) = source.next() { + let source_line = Line::from(super::markdown::highlight_line( + &line.replace('\t', " "), + Some("js"), + )); + let wrapped = super::markdown::wrap_lines(vec![source_line], inner_width); + let available = MAX_ROWS.saturating_sub(rows.len()); + let continued = wrapped.len() > available; + for mut row in wrapped.into_iter().take(available) { + row = super::truncate_line_with_ellipsis_to_width(&row, inner_width); + row.spans.insert(0, Span::raw(" ".repeat(indent))); + rows.push(row); + } + if rows.len() == MAX_ROWS { + let remaining = source.count(); + let label = if continued { + Some("… code continues".to_string()) + } else if remaining > 0 { + Some(format!( + "… {remaining} more line{}", + if remaining == 1 { "" } else { "s" } + )) + } else { + None + }; + if let Some(label) = label { + rows.push(super::truncate_line_with_ellipsis_to_width( + &Line::from(vec![ + Span::raw(" ".repeat(indent)), + Span::styled(label, Style::default().fg(dim_color())), + ]), + width, + )); + } + break; + } + } + rows +} + pub(super) use jcode_tui_tool_display::parse_tool_failure; pub(crate) use jcode_tui_tool_display::{ canonical_tool_name, edit_render_name, is_edit_tool_name, resolve_display_tool_name, diff --git a/crates/jcode-tui/src/tui/ui_viewport.rs b/crates/jcode-tui/src/tui/ui_viewport.rs index 26785a513ed..20404cb9f9b 100644 --- a/crates/jcode-tui/src/tui/ui_viewport.rs +++ b/crates/jcode-tui/src/tui/ui_viewport.rs @@ -352,6 +352,7 @@ pub(super) fn draw_messages( // the sticky previous-prompt preview, including at the top of the transcript. let (pinned_todo_band, pinned_todo_more_line) = pinned_todo_band_lines(app, text_render_area.width, render_area.height); + let pinned_todo_band_rows = pinned_todo_band.len() as u16; let max_scroll = compute_max_scroll_with_prompt_preview( total_lines, wrapped_user_prompt_starts, @@ -428,6 +429,12 @@ pub(super) fn draw_messages( 0u16 }; let pinned_todo_lines = pinned_todo_band.len() as u16; + publish_pinned_todo_scroll((pinned_todo_band_rows > 0).then(|| Rect { + x: text_render_area.x, + y: render_area.y.saturating_add(prompt_preview_lines), + width: text_render_area.width, + height: pinned_todo_band_rows, + })); set_pinned_todo_more_area(pinned_todo_more_line.map(|line| { Rect { x: text_render_area.x, @@ -1431,23 +1438,22 @@ pub(crate) fn pinned_todo_band_lines_for_payload( } let card_lines = if crate::config::config().display.pin_todos { - payload - .map(|payload| { - // Distinct title keeps the line cache from sharing entries with - // the width-capped inline transcript card. - let mut msg = crate::tui::DisplayMessage::todos(payload.to_string()); - msg.title = Some("Pinned todos".to_string()); - super::messages::get_cached_message_lines( - &msg, - width, - diff_mode, - super::messages::render_pinned_todos_message, - ) - }) - .unwrap_or_default() + payload.map_or_else(Vec::new, |payload| { + // Distinct title keeps the line cache from sharing entries with + // the width-capped inline transcript card. + let mut msg = crate::tui::DisplayMessage::todos(payload.to_string()); + msg.title = Some("Pinned todos".to_string()); + super::messages::get_cached_message_lines( + &msg, + width, + diff_mode, + super::messages::render_pinned_todos_message, + ) + }) } else { Vec::new() }; + PINNED_TODO_LAST_MAX.store(0, std::sync::atomic::Ordering::Relaxed); if card_lines.is_empty() { return (Vec::new(), None); } @@ -1471,20 +1477,104 @@ pub(crate) fn pinned_todo_band_lines_for_payload( ))); } else { if expanded { + // Expanded bands get a larger budget but stay bounded so a long plan + // cannot swallow the transcript. Overflow scrolls with the wheel. + let expanded_budget = ((viewport_height as usize) * 2 / 3).max(content_budget); + let window = expanded_budget.saturating_sub(1).max(1); + let total = card_lines.len(); + let max_offset = total.saturating_sub(window); + let offset = pinned_todo_scroll().min(max_offset); + PINNED_TODO_LAST_MAX.store(max_offset, std::sync::atomic::Ordering::Relaxed); more_line = Some(lines.len()); + let label = if max_offset > 0 { + format!( + " ▴ Collapse todos ({}-{} of {}, scroll)", + offset + 1, + (offset + window).min(total), + total + ) + } else { + " ▴ Collapse todos".to_string() + }; lines.push(Line::from(Span::styled( - " ▴ Collapse todos", + label, Style::default().fg(dim_color()), ))); + lines.extend(card_lines.into_iter().skip(offset).take(window)); + return (lines, more_line); } lines.extend(card_lines); } (lines, more_line) } +static PINNED_TODO_SCROLL: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); +static PINNED_TODO_MAX_SCROLL: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); +/// Max scroll computed by the most recent band build. Only the live viewport +/// render publishes it, since layout probes build bands at other heights. +static PINNED_TODO_LAST_MAX: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); +static PINNED_TODO_BAND_AREA: std::sync::Mutex> = std::sync::Mutex::new(None); + +fn pinned_todo_scroll() -> usize { + PINNED_TODO_SCROLL.load(std::sync::atomic::Ordering::Relaxed) +} + +fn publish_pinned_todo_scroll(band_area: Option) { + if super::secondary_render_active() { + return; + } + let max = PINNED_TODO_LAST_MAX.load(std::sync::atomic::Ordering::Relaxed); + PINNED_TODO_MAX_SCROLL.store(max, std::sync::atomic::Ordering::Relaxed); + if pinned_todo_scroll() > max { + PINNED_TODO_SCROLL.store(max, std::sync::atomic::Ordering::Relaxed); + } + set_pinned_todo_band_area(band_area); +} + +#[cfg(test)] +pub(crate) fn publish_pinned_todo_scroll_for_test() { + publish_pinned_todo_scroll(None); +} + +/// Scroll the expanded pinned todo band. Returns true when the offset changed. +pub(crate) fn scroll_pinned_todos(delta: isize) -> bool { + let max = PINNED_TODO_MAX_SCROLL.load(std::sync::atomic::Ordering::Relaxed); + let current = pinned_todo_scroll(); + let next = current.saturating_add_signed(delta).min(max); + PINNED_TODO_SCROLL.store(next, std::sync::atomic::Ordering::Relaxed); + next != current +} + +pub(crate) fn reset_pinned_todo_scroll() { + PINNED_TODO_SCROLL.store(0, std::sync::atomic::Ordering::Relaxed); + PINNED_TODO_MAX_SCROLL.store(0, std::sync::atomic::Ordering::Relaxed); +} + +fn set_pinned_todo_band_area(area: Option) { + if super::secondary_render_active() { + return; + } + if let Ok(mut current) = PINNED_TODO_BAND_AREA.lock() { + *current = area; + } +} + +/// Screen rect of the pinned todo band, when it is scrollable (expanded with overflow). +pub(crate) fn pinned_todo_scroll_area() -> Option { + if PINNED_TODO_MAX_SCROLL.load(std::sync::atomic::Ordering::Relaxed) == 0 { + return None; + } + PINNED_TODO_BAND_AREA.lock().ok().and_then(|area| *area) +} + static PINNED_TODO_MORE_AREA: std::sync::Mutex> = std::sync::Mutex::new(None); fn set_pinned_todo_more_area(area: Option) { + if super::secondary_render_active() { + return; + } if let Ok(mut current) = PINNED_TODO_MORE_AREA.lock() { *current = area; } diff --git a/crates/jcode-tui/src/tui/ui_viewport/prompt_preview.rs b/crates/jcode-tui/src/tui/ui_viewport/prompt_preview.rs index b206c06a321..dde89550073 100644 --- a/crates/jcode-tui/src/tui/ui_viewport/prompt_preview.rs +++ b/crates/jcode-tui/src/tui/ui_viewport/prompt_preview.rs @@ -5,6 +5,47 @@ pub(crate) fn prompt_preview_lines( text: &str, width: u16, align: ratatui::layout::Alignment, +) -> Vec> { + // The pinned preview is rebuilt every frame with the same prompt; memoize + // the last result (keyed on every input, including theme colors) so the + // grapheme walk over the whole prompt runs only when something changes. + type Key = (usize, u64, usize, u16, ratatui::layout::Alignment, [Color; 4]); + thread_local! { + static MEMO: std::cell::RefCell>)>> = + const { std::cell::RefCell::new(None) }; + } + let text_hash = { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + text.hash(&mut h); + h.finish() + }; + let key: Key = ( + number, + text_hash, + text.len(), + width, + align, + [user_bg(), dim_color(), user_color(), user_text()], + ); + if let Some(hit) = MEMO.with(|m| { + m.borrow() + .as_ref() + .filter(|(k, _)| *k == key) + .map(|(_, lines)| lines.clone()) + }) { + return hit; + } + let lines = prompt_preview_lines_uncached(number, text, width, align); + MEMO.with(|m| *m.borrow_mut() = Some((key, lines.clone()))); + lines +} + +fn prompt_preview_lines_uncached( + number: usize, + text: &str, + width: u16, + align: ratatui::layout::Alignment, ) -> Vec> { use unicode_segmentation::UnicodeSegmentation; let text = text.trim().replace('\n', " "); diff --git a/crates/jcode-voice/Cargo.toml b/crates/jcode-voice/Cargo.toml index c349548972b..eb71eeffbcc 100644 --- a/crates/jcode-voice/Cargo.toml +++ b/crates/jcode-voice/Cargo.toml @@ -15,17 +15,20 @@ name = "jcode_voice" path = "src/lib.rs" [dependencies] -jcode-base = { path = "../jcode-base", default-features = false } -anyhow = "1" +jcode-base = { path = "../jcode-base", default-features = false, optional = true } +jcode-provider-env = { path = "../jcode-provider-env" } +anyhow = { version = "1", optional = true } base64 = "0.22" futures = "0.3" -libc = "0.2" +libc = { version = "0.2", optional = true } regex = "1" -reqwest = { version = "0.12", default-features = false, features = ["json", "multipart", "stream", "rustls-tls", "rustls-tls-native-roots"] } -serde = { version = "1", features = ["derive"] } +reqwest = { version = "0.12", optional = true, default-features = false, features = ["json", "multipart", "stream", "rustls-tls", "rustls-tls-native-roots"] } +serde = { version = "1", optional = true, features = ["derive"] } serde_json = "1" -tokio = { version = "1", features = ["io-util", "macros", "net", "process", "rt-multi-thread", "sync", "time"] } -tokio-tungstenite = { version = "0.24", default-features = false, features = ["connect", "rustls-tls-native-roots"] } +tokio = { version = "1", features = ["io-util", "macros", "net", "rt", "sync", "time"] } +# Use an explicit bundled-root connector, never the platform filesystem CA loader. +tokio-tungstenite = { version = "0.24", default-features = false, features = ["connect", "__rustls-tls"] } +webpki-roots = "1" rustls = { version = "0.23", default-features = false, features = ["aws_lc_rs"] } # Native microphone input is opt-in so CLI builds need no audio development libraries. @@ -33,14 +36,18 @@ cpal = { version = "0.15", optional = true } [target.'cfg(target_os = "linux")'.dependencies] # logind session check for background microphone use. -zbus = "5.19" -async-io = "2.6" +zbus = { version = "5.19", optional = true } +async-io = { version = "2.6", optional = true } [features] -voice-capture = ["dep:cpal"] +default = ["desktop-config"] +# Desktop settings, subscriptions, hotkeys, and recorder-process integration. +desktop-config = ["dep:jcode-base", "dep:reqwest", "dep:serde", "dep:anyhow", "dep:libc", "dep:zbus", "dep:async-io", "tokio/process", "tokio/rt-multi-thread"] +# Auth sandbox coverage is opt-in so lightweight tests never pull in jcode-base. +desktop-test-support = ["desktop-config", "jcode-base/test-support"] +voice-capture = ["desktop-config", "dep:cpal"] [dev-dependencies] -jcode-base = { path = "../jcode-base", default-features = false, features = ["test-support"] } tempfile = "3" tokio = { version = "1", features = ["test-util"] } diff --git a/crates/jcode-voice/src/dictation.rs b/crates/jcode-voice/src/dictation.rs index f6cb461f2e0..a5958ff370b 100644 --- a/crates/jcode-voice/src/dictation.rs +++ b/crates/jcode-voice/src/dictation.rs @@ -90,14 +90,49 @@ pub fn focused_jcode_session() -> Result> { /// Session of the Jcode CLI (TUI) client in the focused window, if the focused /// window is a terminal running one. Jcode Desktop windows never match: their /// own chats handle voice in-app. +/// +/// Polled several times a second by the companion, so the full `/proc` walk +/// is cached per focused PID until the client registry changes or 10s pass. +/// Session markers are read on every poll, including process-tree cache hits. pub fn focused_cli_session() -> Result> { let Some(window) = focused_window_niri()? else { return Ok(None); }; - if window_is_desktop(&window) { - return Ok(None); + let registry_stamp = match (|| -> Result { + let dir = jcode_base::storage::jcode_dir()?; + Ok(std::fs::metadata(dir.join("client_sessions"))?.modified()?) + })() { + Ok(stamp) => Some(stamp), + Err(error) => { + // A missing registry is normal until a CLI first registers. + jcode_base::logging::debug(&format!( + "Cannot read client registry timestamp: {error:#}" + )); + None + } + }; + let key = (window.pid, registry_stamp); + type FocusCacheEntry = ( + (u32, Option), + std::time::Instant, + Vec, + ); + static CACHE: std::sync::Mutex> = std::sync::Mutex::new(None); + let mut cache = CACHE.lock().unwrap_or_else(|e| e.into_inner()); + if let Some((cached_key, at, session)) = cache.as_ref() + && *cached_key == key + && at.elapsed() < std::time::Duration::from_secs(10) + { + return Ok(resolve_session_in_tree(&window, session)); } - Ok(resolve_session_for_window(&window)) + let tree = if window_is_desktop(&window) { + Vec::new() + } else { + process_tree_for_window(&window) + }; + let session = resolve_session_in_tree(&window, &tree); + *cache = Some((key, std::time::Instant::now(), tree)); + Ok(session) } fn window_is_desktop(window: &NiriFocusedWindow) -> bool { @@ -172,6 +207,30 @@ pub fn send_transcript_blocking( anyhow::bail!("timed out waiting for the jcode server") } +/// Report live push-to-talk state to a session's TUI (waveform, live text). +#[cfg(unix)] +pub fn send_voice_activity_blocking( + session_id: &str, + activity: jcode_base::protocol::VoiceActivity, + timeout: std::time::Duration, +) -> Result<()> { + use std::io::{BufRead, BufReader, Write}; + let path = debug_socket_path(); + let mut stream = std::os::unix::net::UnixStream::connect(&path) + .with_context(|| format!("failed to connect to {}", path.display()))?; + stream.set_read_timeout(Some(timeout))?; + stream.set_write_timeout(Some(timeout))?; + let request = jcode_base::protocol::Request::VoiceActivity { + id: 1, + session_id: session_id.to_string(), + activity, + }; + stream.write_all((serde_json::to_string(&request)? + "\n").as_bytes())?; + let mut line = String::new(); + BufReader::new(stream).read_line(&mut line)?; + Ok(()) +} + /// Mirrors the server's socket naming (`JCODE_SOCKET`, else the runtime dir). #[cfg(unix)] fn debug_socket_path() -> std::path::PathBuf { @@ -224,7 +283,111 @@ struct NiriFocusedWindow { _app_id: Option, } +/// Ask niri over its IPC socket directly. Spawning `niri msg` costs a fork, +/// exec and dynamic link per call, which dominated the companion's idle CPU. +#[cfg(not(unix))] +fn focused_window_niri_socket() -> Option> { + // niri is a Wayland compositor and its IPC is a Unix socket. + None +} + +#[cfg(unix)] +fn focused_window_niri_socket() -> Option> { + let path = std::env::var_os("NIRI_SOCKET")?; + match focused_window_niri_socket_at(&path) { + Ok(window) => Some(window), + Err(error) => { + jcode_base::logging::debug(&format!( + "Niri socket focus lookup failed, falling back to command: {error:#}" + )); + None + } + } +} + +#[cfg(all(test, unix))] +mod socket_tests { + use super::focused_window_niri_socket_at; + + fn query(reply: &'static str) -> anyhow::Result> { + use std::io::{BufRead, BufReader, Write}; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("niri.sock"); + let listener = std::os::unix::net::UnixListener::bind(&path).unwrap(); + let server = std::thread::spawn(move || { + let (mut socket, _) = listener.accept().unwrap(); + let mut request = String::new(); + BufReader::new(socket.try_clone().unwrap()) + .read_line(&mut request) + .unwrap(); + assert_eq!(request, "\"FocusedWindow\"\n"); + socket.write_all(reply.as_bytes()).unwrap(); + }); + let result = focused_window_niri_socket_at(path.as_os_str()); + server.join().unwrap(); + result + } + + #[test] + fn socket_no_focus_is_success_not_fallback() { + assert!( + query("{\"Ok\":{\"FocusedWindow\":null}}\n") + .unwrap() + .is_none() + ); + } + + #[test] + fn malformed_reply_preserves_error_context() { + let error = query("not json\n").unwrap_err(); + assert!(error.to_string().contains("parse niri focused window")); + } + + #[test] + fn missing_socket_preserves_error_context() { + let dir = tempfile::tempdir().unwrap(); + let error = + focused_window_niri_socket_at(dir.path().join("missing").as_os_str()).unwrap_err(); + assert!(error.to_string().contains("connect niri socket")); + } +} + +#[cfg(unix)] +fn focused_window_niri_socket_at(path: &std::ffi::OsStr) -> Result> { + use std::io::{BufRead, BufReader, Write}; + let mut stream = + std::os::unix::net::UnixStream::connect(path).context("connect niri socket")?; + let limit = Some(std::time::Duration::from_millis(500)); + stream + .set_read_timeout(limit) + .context("set niri read timeout")?; + stream + .set_write_timeout(limit) + .context("set niri write timeout")?; + stream + .write_all(b"\"FocusedWindow\"\n") + .context("request niri focused window")?; + let mut line = String::new(); + BufReader::new(stream) + .read_line(&mut line) + .context("read niri focused window")?; + #[derive(Deserialize)] + enum Reply { + Ok(Inner), + } + #[derive(Deserialize)] + enum Inner { + FocusedWindow(Option), + } + match serde_json::from_str::(&line).context("parse niri focused window")? { + Reply::Ok(Inner::FocusedWindow(window)) => Ok(window), + } +} + fn focused_window_niri() -> Result> { + if let Some(window) = focused_window_niri_socket() { + return Ok(window); + } let output = Command::new("niri") .args(["msg", "-j", "focused-window"]) .output(); @@ -250,6 +413,10 @@ fn focused_window_niri() -> Result> { } fn resolve_session_for_window(window: &NiriFocusedWindow) -> Option { + resolve_session_in_tree(window, &process_tree_for_window(window)) +} + +fn process_tree_for_window(window: &NiriFocusedWindow) -> Vec { let children = match proc_children_map() { Ok(children) => Some(children), Err(error) => { @@ -270,6 +437,13 @@ fn resolve_session_for_window(window: &NiriFocusedWindow) -> Option { } } + tree +} + +fn resolve_session_in_tree(window: &NiriFocusedWindow, tree: &[u32]) -> Option { + if window_is_desktop(window) { + return None; + } // Exact: a client in this window registered the session it shows. let registered: Vec = tree .iter() diff --git a/crates/jcode-voice/src/dictation_tests.rs b/crates/jcode-voice/src/dictation_tests.rs index 4940951fa57..1811cd6ec6b 100644 --- a/crates/jcode-voice/src/dictation_tests.rs +++ b/crates/jcode-voice/src/dictation_tests.rs @@ -242,6 +242,22 @@ fn focused_cli_session_uses_client_registry_for_task_titled_windows() { super::focused_cli_session().expect("resolve focused session"), Some("session_fox_123".to_string()) ); + // Updating the same file leaves the directory timestamp unchanged. A cache + // hit must still read the marker instead of returning the previous target. + let stamp = std::fs::metadata(®istry).unwrap().modified().unwrap(); + std::fs::write( + registry.join(focused_process.pid().to_string()), + "session_owl_456", + ) + .unwrap(); + assert_eq!( + std::fs::metadata(®istry).unwrap().modified().unwrap(), + stamp + ); + assert_eq!( + super::focused_cli_session().unwrap(), + Some("session_owl_456".into()) + ); } #[cfg(target_os = "linux")] diff --git a/crates/jcode-voice/src/lib.rs b/crates/jcode-voice/src/lib.rs index b14189926f6..3d59d0a31f9 100644 --- a/crates/jcode-voice/src/lib.rs +++ b/crates/jcode-voice/src/lib.rs @@ -6,11 +6,19 @@ //! clients. `NariSession` and `nari_pcm_channel` accept bounded mono 16 kHz PCM16 //! from any source without the capture feature. Nari requires a caller-supplied //! provider key. The existing subscription APIs still need no provider key. +//! +//! Disable default features for a transport-only client with caller-owned PCM. +//! The default `desktop-config` feature adds desktop settings, subscription APIs, +//! hotkeys, and recorder processes. `voice-capture` also enables `desktop-config`. +#[cfg(feature = "desktop-config")] use jcode_base::{subscription_api, subscription_catalog}; +#[cfg(feature = "desktop-config")] use serde::Deserialize; use std::{fmt, time::Duration}; +#[cfg(feature = "desktop-config")] pub mod dictation; +#[cfg(feature = "desktop-config")] pub mod global; mod nari; pub mod timing; @@ -18,11 +26,15 @@ 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, }; +#[cfg(feature = "desktop-config")] mod command_capture; #[cfg(any(feature = "voice-capture", test))] mod resample; +#[cfg(feature = "desktop-config")] mod streaming_capture; +#[cfg(feature = "desktop-config")] pub use command_capture::{RecorderCommand, detect_recorders}; +#[cfg(feature = "desktop-config")] pub use streaming_capture::{NariRecording, PcmRecording}; /// Native (cpal) microphone capture is compiled in. Without it, voice input @@ -89,6 +101,7 @@ impl fmt::Display for VoiceError { } impl std::error::Error for VoiceError {} +#[cfg(feature = "desktop-config")] fn network_error(error: reqwest::Error) -> VoiceError { if error.is_timeout() { VoiceError::Timeout @@ -97,6 +110,7 @@ fn network_error(error: reqwest::Error) -> VoiceError { } } +#[cfg(feature = "desktop-config")] fn client() -> Result { reqwest::Client::builder() .redirect(reqwest::redirect::Policy::none()) @@ -106,6 +120,7 @@ fn client() -> Result { .map_err(network_error) } +#[cfg(feature = "desktop-config")] async fn response_bytes(mut response: reqwest::Response) -> Result, VoiceError> { let status = response.status(); if !status.is_success() { @@ -130,6 +145,7 @@ async fn response_bytes(mut response: reqwest::Response) -> Result, Voic Ok(bytes) } +#[cfg(feature = "desktop-config")] #[derive(Deserialize, Default)] struct CapabilityResponse { #[serde(default)] @@ -138,6 +154,7 @@ struct CapabilityResponse { /// Fail closed for missing keys, old servers, offline errors, and denied requests. /// This does not capture audio, request microphone access, or cache account secrets. +#[cfg(feature = "desktop-config")] pub async fn subscription_voice_available() -> bool { let Some(key) = subscription_catalog::configured_api_key().filter(|key| !key.trim().is_empty()) else { @@ -149,6 +166,7 @@ pub async fn subscription_voice_available() -> bool { .unwrap_or(false) } +#[cfg(feature = "desktop-config")] async fn voice_available_with( client: &reqwest::Client, base: &str, @@ -170,6 +188,7 @@ async fn voice_available_with( /// The server is authoritative about entitlement, so no preflight check is required. /// `language`, if supplied, is a lowercase ISO 639-1 code (for example `en`). /// Dropping the future cancels the request. Neither audio nor transcripts are logged. +#[cfg(feature = "desktop-config")] pub async fn transcribe_wav(wav: Vec, language: Option<&str>) -> Result { let key = subscription_catalog::configured_api_key() .filter(|key| !key.trim().is_empty()) @@ -185,6 +204,7 @@ pub async fn transcribe_wav(wav: Vec, language: Option<&str>) -> Result Result<(), VoiceError> { let invalid = || VoiceError::InvalidAudio; if bytes.len() < 44 @@ -286,7 +307,7 @@ fn validate_wav(bytes: &[u8]) -> Result<(), VoiceError> { Ok(()) } -#[cfg(any(feature = "voice-capture", test))] +#[cfg(all(feature = "desktop-config", any(feature = "voice-capture", test)))] fn encode_wav(samples: &[i16], sample_rate: u32) -> Result, VoiceError> { let len = samples .len() @@ -647,7 +668,7 @@ mod capture { } } -#[cfg(test)] +#[cfg(all(test, feature = "desktop-config"))] mod tests { use super::*; use std::io::{Read, Write}; @@ -724,6 +745,7 @@ mod tests { .await } + #[cfg(feature = "desktop-test-support")] #[tokio::test] async fn configured_public_api_requires_key_and_uses_configured_base() { let _sandbox = jcode_base::auth::test_sandbox::AuthTestSandbox::new().unwrap(); diff --git a/crates/jcode-voice/src/nari.rs b/crates/jcode-voice/src/nari.rs index 76612c9dc88..da9a2f15732 100644 --- a/crates/jcode-voice/src/nari.rs +++ b/crates/jcode-voice/src/nari.rs @@ -238,10 +238,17 @@ fn is_prompt_echo(text: &str) -> bool { /// Conservative bound on the recognition context sent per session. const MAX_PROMPT_CHARS: usize = 1000; -/// Recognition context: built-in names plus `[dictation] vocabulary`, +/// Recognition context: built-in names plus desktop `[dictation] vocabulary`, /// deduplicated case-insensitively and bounded without splitting a term. pub fn recognition_prompt() -> String { - build_prompt(&jcode_base::config::config().dictation.vocabulary) + #[cfg(feature = "desktop-config")] + { + build_prompt(&jcode_base::config::config().dictation.vocabulary) + } + #[cfg(not(feature = "desktop-config"))] + { + build_prompt(&[]) + } } fn build_prompt(extra: &[String]) -> String { @@ -275,7 +282,7 @@ fn build_prompt(extra: &[String]) -> String { } pub fn nari_api_key() -> Option { - jcode_base::provider_catalog::load_api_key_from_env_or_config("NARI_API_KEY", "nari.env") + jcode_provider_env::load_api_key_from_env_or_config("NARI_API_KEY", "nari.env") .filter(|key| !key.trim().is_empty()) } @@ -287,6 +294,7 @@ 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 = "desktop-config")] pub(super) fn capture_pcm_channel() -> (mpsc::Sender>, mpsc::Receiver>) { mpsc::channel((IO_TIMEOUT.as_secs() as usize + 5) * 10) } @@ -301,12 +309,47 @@ pub(super) async fn cancelled(cancel: &AtomicBool) { } type Socket = WebSocketStream>; + +fn bundled_root_store() -> rustls::RootCertStore { + rustls::RootCertStore { + roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(), + } +} + +/// iOS is Unix but has no OpenSSL CA bundle. rustls-native-certs 0.8 uses +/// its Unix filesystem loader there, not the Apple trust store. An explicit +/// connector also avoids Cargo feature unification selecting native roots or +/// another TLS backend. Keep rustls's default chain, time and hostname checks. +fn tls_connector() -> tokio_tungstenite::Connector { + tokio_tungstenite::Connector::Rustls(Arc::new( + rustls::ClientConfig::builder() + .with_root_certificates(bundled_root_store()) + .with_no_client_auth(), + )) +} + pub struct NariSession { socket: Socket, cancel: Arc, } impl NariSession { + /// Connect to a Jcode cloud relay using a Jcode bearer, never a Nari key. + /// The endpoint must be `wss://.../voice/realtime`, without credentials, + /// query, or fragment. Plain WebSockets are permitted only in unit tests + /// and only to literal loopback IP addresses. + pub async fn connect_relay( + url: &str, + bearer: &str, + cancel: Arc, + ) -> Result { + if cancel.load(Ordering::SeqCst) { + return Err(VoiceError::Cancelled); + } + validate_relay_url(url)?; + Self::connect_socket(url, bearer, 4096, &recognition_prompt(), cancel).await + } + /// Resolves only after the server acknowledges session.configure. No microphone access. pub async fn connect(key: &str, cancel: Arc) -> Result { Self::connect_to(URL, key, cancel).await @@ -323,11 +366,24 @@ impl NariSession { key: &str, prompt: &str, cancel: Arc, + ) -> Result { + Self::connect_socket(url, key, 1024, prompt, cancel).await + } + + async fn connect_socket( + url: &str, + key: &str, + max_bearer_bytes: usize, + prompt: &str, + cancel: Arc, ) -> Result { if cancel.load(Ordering::SeqCst) { return Err(VoiceError::Cancelled); } - if key.is_empty() || key.len() > 1024 || !key.bytes().all(|b| (33..=126).contains(&b)) { + if key.is_empty() + || key.len() > max_bearer_bytes + || !key.bytes().all(|b| (33..=126).contains(&b)) + { return Err(VoiceError::NariNotConfigured); } let setup = async { @@ -352,10 +408,14 @@ impl NariSession { max_frame_size: Some(MAX_RESPONSE_BYTES), ..Default::default() }; - let (mut socket, _) = - tokio_tungstenite::connect_async_with_config(request, Some(config), false) - .await - .map_err(handshake_error)?; + let (mut socket, _) = tokio_tungstenite::connect_async_tls_with_config( + request, + Some(config), + false, + Some(tls_connector()), + ) + .await + .map_err(handshake_error)?; super::timing::mark("nari websocket connected"); send(&mut socket, json!({"type":"session.configure", "session":{"model":"qwen3-asr-fast", "turn_detection":null, "language":"en", "prompt":prompt}})).await?; loop { @@ -463,6 +523,37 @@ impl NariSession { } } } +fn validate_relay_url(url: &str) -> Result<(), VoiceError> { + use tokio_tungstenite::tungstenite::http::Uri; + if url.bytes().any(|b| b <= 32 || b >= 127) || url.contains(['@', '?', '#', '\\', '%']) { + return Err(VoiceError::Network); + } + let uri: Uri = url.parse().map_err(|_| VoiceError::Network)?; + let host = uri.host().ok_or(VoiceError::Network)?; + // A Jcode bearer must never be sent to the direct provider endpoint. + let host_lower = host.to_ascii_lowercase(); + if host_lower.trim_end_matches('.') == "narilabs.com" + || host_lower.trim_end_matches('.').ends_with(".narilabs.com") + || !uri.path().ends_with("/voice/realtime") + || uri.authority().is_none() + { + return Err(VoiceError::Network); + } + if uri.scheme_str() == Some("wss") { + return Ok(()); + } + #[cfg(test)] + if uri.scheme_str() == Some("ws") + && host + .trim_start_matches('[') + .trim_end_matches(']') + .parse::() + .is_ok_and(|ip| ip.is_loopback()) + { + return Ok(()); + } + Err(VoiceError::Network) +} fn handshake_error(error: tokio_tungstenite::tungstenite::Error) -> VoiceError { if let tokio_tungstenite::tungstenite::Error::Http(response) = error { match response.status().as_u16() { @@ -599,6 +690,191 @@ impl TranscriptState { #[cfg(test)] mod tests { use super::*; + + #[test] + fn bundled_tls_roots_are_complete_without_native_store() { + // No native loader or environment override participates in this store. + // Run with SSL_CERT_FILE/SSL_CERT_DIR pointing to missing paths as well. + let roots = bundled_root_store(); + assert!(!roots.is_empty()); + assert_eq!(roots.len(), webpki_roots::TLS_SERVER_ROOTS.len()); + for (actual, expected) in roots.roots.iter().zip(webpki_roots::TLS_SERVER_ROOTS) { + assert_eq!(actual, expected); + } + } + + #[test] + fn bundled_tls_connector_uses_rustls_even_with_unified_features() { + let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); + let tokio_tungstenite::Connector::Rustls(config) = tls_connector() else { + panic!("voice must use the explicit bundled-root rustls connector"); + }; + assert!(config.enable_sni); + assert!(!config.client_auth_cert_resolver.has_certs()); + let connection = rustls::ClientConnection::new( + config, + rustls::pki_types::ServerName::try_from("relay.example").unwrap(), + ); + assert!(connection.is_ok()); + } + + #[tokio::test] + async fn relay_rejects_unsafe_urls_and_invalid_bearers_before_network() { + for url in [ + "https://relay.example/voice/realtime", + "ws://relay.example/voice/realtime", + "wss://user:pass@relay.example/voice/realtime", + "wss://relay.example/voice/realtime?token=secret", + "wss://relay.example/voice/realtime#fragment", + "wss://relay.example/wrong", + "wss://api.narilabs.com/voice/realtime", + "wss://API.NARILABS.COM./voice/realtime", + "wss://relay.example/%2e/voice/realtime", + "wss:///voice/realtime", + ] { + assert!( + matches!( + NariSession::connect_relay(url, "test", Arc::new(AtomicBool::new(false))).await, + Err(VoiceError::Network) + ), + "unsafe URL accepted: {url}" + ); + } + assert!(validate_relay_url("wss://relay.example/api/voice/realtime").is_ok()); + for bearer in [ + String::new(), + "x".repeat(4097), + "bad\r\nheader".into(), + "bad bearer".into(), + ] { + assert!(matches!( + NariSession::connect_relay( + "wss://relay.invalid/voice/realtime", + &bearer, + Arc::new(AtomicBool::new(false)) + ) + .await, + Err(VoiceError::NariNotConfigured) + )); + } + assert!(matches!( + NariSession::connect(&"x".repeat(1025), Arc::new(AtomicBool::new(false))).await, + Err(VoiceError::NariNotConfigured) + )); + } + + async fn relay_server(bearer: String, stream: bool) -> (String, tokio::task::JoinHandle<()>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}/voice/realtime", listener.local_addr().unwrap()); + let task = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let mut ws = tokio_tungstenite::accept_hdr_async( + tcp, + move |request: &tokio_tungstenite::tungstenite::handshake::server::Request, + response| { + assert_eq!(request.uri().path(), "/voice/realtime"); + assert_eq!( + request.headers()["authorization"], + format!("Bearer {bearer}") + ); + Ok(response) + }, + ) + .await + .unwrap(); + let config = receive(&mut ws).await.unwrap(); + assert_eq!( + config, + json!({"type":"session.configure", "session":{"model":"qwen3-asr-fast", "turn_detection":null, "language":"en", "prompt":recognition_prompt()}}) + ); + send(&mut ws, json!({"type":"session.configured"})) + .await + .unwrap(); + if !stream { + while let Some(Ok(_)) = ws.next().await {} + return; + } + let audio = receive(&mut ws).await.unwrap(); + assert_eq!(audio["type"], "input_audio_buffer.append"); + assert_eq!( + STANDARD.decode(audio["audio"].as_str().unwrap()).unwrap(), + [1, 0, 255, 255] + ); + let commit = receive(&mut ws).await.unwrap(); + assert_eq!(commit["event_id"], END); + for value in [ + json!({"type":"transcript.partial", "item_id":"a", "transcript":"Hel"}), + json!({"type":"input_audio_buffer.committed", "item_id":"a", "client_event_id":END}), + json!({"type":"transcript.completed", "item_id":"a", "transcript":"Hello"}), + ] { + send(&mut ws, value).await.unwrap(); + } + }); + (url, task) + } + + #[tokio::test] + async fn relay_auth_configure_partial_and_final_reuse_protocol() { + let bearer = "x".repeat(4096); + let (url, server) = relay_server(bearer.clone(), true).await; + let session = NariSession::connect_relay(&url, &bearer, Arc::new(AtomicBool::new(false))) + .await + .unwrap(); + let (tx, rx) = nari_pcm_channel(); + tx.send(vec![1, -1]).await.unwrap(); + drop(tx); + let mut events = Vec::new(); + assert_eq!( + session.run(rx, |event| events.push(event)).await.unwrap(), + "Hello" + ); + assert!( + events + .iter() + .any(|event| matches!(event, NariEvent::Transcript(text) if text == "Hel")) + ); + assert!(matches!(events.last(), Some(NariEvent::Finished(Ok(text))) if text == "Hello")); + server.await.unwrap(); + } + + #[tokio::test] + async fn relay_cancellation_interrupts_setup_and_streaming() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let cancel = Arc::new(AtomicBool::new(false)); + let c = cancel.clone(); + let trigger = tokio::spawn(async move { + tokio::time::sleep(Duration::from_millis(30)).await; + c.store(true, Ordering::SeqCst); + }); + assert!(matches!( + NariSession::connect_relay( + &format!("ws://{}/voice/realtime", listener.local_addr().unwrap()), + "test", + cancel + ) + .await, + Err(VoiceError::Cancelled) + )); + trigger.await.unwrap(); + let (url, server) = relay_server("test".into(), false).await; + let cancel = Arc::new(AtomicBool::new(false)); + let session = NariSession::connect_relay(&url, "test", cancel.clone()) + .await + .unwrap(); + let (_tx, rx) = nari_pcm_channel(); + cancel.store(true, Ordering::SeqCst); + let mut events = Vec::new(); + assert_eq!( + session.run(rx, |event| events.push(event)).await, + Err(VoiceError::Cancelled) + ); + assert!(matches!( + events.last(), + Some(NariEvent::Finished(Err(VoiceError::Cancelled))) + )); + server.await.unwrap(); + } + #[test] fn silent_prompt_leaks_are_dropped_but_real_speech_is_kept() { for leak in [ @@ -666,6 +942,13 @@ mod tests { assert!((estimated_transcription_usd(Duration::from_secs(30)) - 0.001).abs() < 1e-12); } + #[cfg(not(feature = "desktop-config"))] + #[test] + fn lightweight_prompt_uses_builtin_vocabulary() { + assert_eq!(recognition_prompt(), build_prompt(&[])); + assert!(recognition_prompt().contains("Jcode")); + } + #[test] fn prompt_merges_user_terms_dedupes_and_stays_bounded() { let prompt = build_prompt(&[ diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md new file mode 100644 index 00000000000..dd2e2f49f4d --- /dev/null +++ b/docs/CLOUD_LATENCY.md @@ -0,0 +1,255 @@ +# Cloud latency diagnostics + +Cloud operations emit info-level, content-free timing records to the ordinary +Jcode logs in `$JCODE_HOME/logs/jcode-YYYY-MM-DD.log` (default `~/.jcode/logs`). +They do not require trace mode. Each process writes its own records, so a local +client and a cloud daemon must both be updated to see both sides of a resume. +A new local binary does not update an already-running cloud daemon. + +## Find expensive stages + +From the repository root: + +```bash +python3 scripts/cloud_latency_report.py --slow-ms 1000 +python3 scripts/cloud_latency_report.py ~/.jcode/logs/jcode-2026-11-07.log +python3 scripts/cloud_latency_report.py downloaded-cloud-daemon.log --slow-ms 250 +python3 scripts/cloud_latency_report.py --since 2026-11-08T00:28:00 --until 2026-11-08T00:38:00 +``` + +The report ranks stage/operation/metric groups by maximum observed duration and +shows sample count, error count, p50, p95 and maximum. It reads files incrementally, +retaining numeric samples for exact percentiles. Defaults include all retained +logs, so narrow the files when comparing before/after builds. Percentiles describe +only the selected observed samples, not a statistically controlled benchmark. +The report prints only allowlisted label syntax and numeric fields, never raw log +lines. Do not combine overlapping stage/total metrics by summing them. +Time windows use the local wall-clock timestamps printed in the selected logs, +with an inclusive start and exclusive end. Do not pass UTC offsets. Undated or +invalid timestamp records are excluded when a window is selected. A time window +does not identify a binary: several old and new processes can write to one log, +so a post-reload window alone is not proof that every sample used the new build. + +Most records use `[CLOUD_TIMING]`; mobile records use `mobile_sync stage=...`. +Common fields are `stage`, `operation`, `outcome`, and `duration_ms`. Additional +numeric `*_ms` fields appear as separate report rows. Request IDs correlate +client wire events and server work. Server records additionally have operation IDs, +a hashed session key, and history kind. Session keys are diagnostic correlation, +not an authorization mechanism. `none` means a metric was not available. + +## Coverage and interpretation + +| Path | Observable boundaries | +| --- | --- | +| Cloud control plane | HTTP send, body read/decode, host readiness polling/wait and terminal success/failure/cancellation | +| Native SSH attach | Process start through authenticated daemon handshake, including failures and cancelled attempts | +| TUI connect | Socket, readiness, Subscribe and bootstrap setup, with the failing phase retained on early return | +| History/todos transport | Request write, response wait, event JSON parse, payload byte count and request correlation | +| Server bootstrap and paging | Snapshot/build, encoding, writer-lock wait and socket write, plus fallback/failure/drop outcomes | +| Client application | History, page and todo event application, followed by one-shot frame composition readiness | +| Preview/inventory | Cache scope/read/freshness/write, SSH command, remote projection/inventory and local decode/load | +| Mobile transcript sync | Save queue/debounce, incremental/reconciliation pass, session upload/skip/error, HTTP and progress persistence | +| Handoff | Existing preparation/cache/probe timings plus machine-readable repository/snapshot/archive/upload/activation stages | + +`request_wait_ms` starts after the request write completes and ends after response +JSON parsing. It includes remote queue/work, transfer and client scheduling, not +just network RTT. Server writer-lock versus write versus encode timings help +separate those causes. This measurement survives a cancelled socket-read future +because it is attached to the connection's outstanding request, not a particular +read attempt or executor thread. Separate connections never share correlation +queues. A fresh Subscribe completes with Done, while a target-aware Subscribe +retains its correlation until History. Explicit todo polls take precedence over +bootstrap snapshots. Todo events have no wire request ID, so their association is +best-effort FIFO, not proof that a specific request caused a pushed snapshot. +The correlation queue is bounded; old entries expire when subsequent requests +arrive. Expiry is a diagnostic observation, not an application timeout. + +`frame_ready` measures from completed event application until a composed TUI frame +is ready. It is not a physical display/terminal flush timestamp. Updates may share +a frame. Unfocused idle terminals intentionally coalesce redraws for up to five +seconds, and hibernation can defer them further. This wall-clock wait is not the +CPU cost of rendering. Existing rendering diagnostics give detailed draw costs +when needed. +Fast streaming tokens are deliberately not logged individually. Key sync milestones +are logged consistently, while unrelated protocol events only receive a parse +record if slow or malformed, avoiding per-token logging overhead. + +Remote projector stderr must pass a bounded, strict timing-record filter before +entering local logs. Arbitrary SSH stderr is not forwarded. Timings contain fixed +stage/outcome labels, numeric sizes/durations and bounded correlation fields, not +credentials, keys, paths, transcript contents, payloads or raw server error bodies. +Keep ordinary diagnostic log files private nevertheless, since other existing +subsystems may include information beyond these timing records. + +Unchanged mobile reconciliation candidates are counted in a single +`mobile_sync candidates=... unchanged=... progress_updates=...` summary rather +than timed and logged individually. Changed-session and HTTP timings remain. +A repeatable no-network filesystem benchmark is available with: + +```bash +justrust test --remote -p jcode-base --lib benchmark_unchanged_reconciliation -- --ignored --nocapture +``` + +It measures 1,300 unchanged sessions, not upload or end-to-end cloud latency. + +Successful HTTP/1 mobile uploads drain small acknowledgment bodies to preserve +connection reuse. `http_upload_body` records success, timeout, size-limit or +read-error outcomes, bounded to 100ms and 64KiB. An already accepted upload remains +successful if its acknowledgment body is malformed or slow, avoiding duplicate +retries. HTTP/2 and later multiplexed responses skip this HTTP/1-specific drain. +`http_upload` includes this bounded cleanup, so its timing boundary differs from +older builds that stopped at response headers. The controlled keep-alive benchmark: + +```bash +justrust test --remote -p jcode-base --lib benchmark_mobile_upload_keep_alive -- --ignored --nocapture +``` + +It injects a 30ms per-connection delay and an 8ms body delay. Connection counts +demonstrate reuse, but its elapsed times are not production API measurements. + +A high total alone is not proof of the cause. Compare cache hits/misses, success +versus failure, cold versus warm connections and local versus daemon stage timing. +`workspace_command` records distinguish `git_probe`, `inventory_prepare`, +`inventory_projection`, `preview`, `cloud_status`, and `cloud_status_directories`. +Older `operation=none` samples cannot reliably identify which subprocess was slow. +These operations share transport code but have different workloads and deadlines, +so compare their distributions separately rather than attributing every slow SSH +command to inventory. The report counts HTTP/read/load errors, output bounds, +identity mismatches, and nonzero exits as failures, not normal cache misses/skips. +Do not claim a cold VM wake or a full end-to-end cloud resume is subsecond just +because one stage or a synthetic fixture is subsecond. + +## Mobile and workspace follow-up measurements + +Read-only samples over an existing managed SSH master on November 8, 2026 +measured machine status at 6.4–7.1ms, directory status at 6.85–6.90ms (27 +entries), and the selected active repository's git probe at 179–226ms. These +did not reproduce the historical 8-second workspace outliers. They are remote +stage timings, not cold connection or end-to-end UI latency. + +The bounded authoritative directory scanner also has a worst-case synthetic +benchmark: an identical frozen 97MB snapshot fell from 3.575s to 1.190s, and +journal metadata scanning from 0.675s to 0.078s, with identical metadata. This +does not establish that scanning caused the observed live outliers. + +Mobile push now reuses the shared HTTP client while retaining its five-second +request deadline and delivery semantics. Eight loopback pushes with an +artificial 60ms connection setup delay improved from 622ms/eight connections +to 71ms/one connection. This is not an APNs delivery measurement. Static +`operation=mobile_push` timings cover total, HTTP send and body processing. +The report counts `rejected` and `undelivered` as failures, but not `no_devices`. + +Mobile command execution drains stdout and bounded stderr concurrently and +supervises the whole child lifetime. An isolated child emitting 1.4MB of +Unicode stderr previously hit the fixture's 301ms timeout and now completes +in 3ms. This demonstrates removal of a pipe deadlock, not faster model turns. +`operation=mobile_runner` timings cover claim, transfer, turn, sync, complete +and poll wait, including errors and cancellation. The five-second poll cadence +is unchanged. + +The runner now claims immediately on startup and immediately after completing a +command. It sleeps five seconds only after an empty claim, retaining the idle +poll rate. Errors back off for 20 seconds rather than 20 seconds plus the old +unconditional five-second wait. This removes scheduling gaps at startup and +between queued commands, not the up-to-five-second wait for a command arriving +during an idle poll interval. The current API provides no long-poll or command +notification channel, so that remaining latency needs coordinated API work. + +`operation=mobile_runner stage=queue_wait` records `claimed_at - created_at` +using the two API-provided timestamps, avoiding phone/daemon clock skew. It +includes machine wake, previous queued work and polling delays, but excludes +claim-response transport and turn execution. Legacy responses without those +timestamps still execute normally. Invalid or reversed timestamps omit the +metric rather than changing command handling. Logs contain only the duration, +not command IDs, text or timestamps. Paused-clock regression comparisons prove +the eliminated five-second scheduling gaps, not production end-to-end latency. + +### Optional command wakeups + +The daemon can subscribe to `GET /v1/mobile/commands/events` using its normal +account bearer header over a WebSocket. The only accepted application frame is +`{"type":"commands_available"}`. It is a hint to perform the existing atomic +HTTP claim, never an executable command. Initial subscription triggers a queue +recheck to cover commands queued between an empty claim and socket registration. +Bursts coalesce in a bounded watch channel. Messages/frames are capped at 4KiB. + +The five-second claim fallback remains active even with a socket, protecting +against lost hints, service restart and unsupported APIs. Error backoff still +cannot be bypassed by a hint. Connection attempts have a two-second deadline, +network retry cooldown, and a five-minute cooldown for disabled/old endpoints. +Endpoint and credential changes discard the old socket. Cancellation aborts the +socket task. Fixed `event_connect` and `event_connection` records contain no +credentials, endpoint, account, command text or raw network error. + +The corresponding backend candidate uses an account-scoped Durable Object with +the WebSocket Hibernation API and at most four sockets per account. D1 remains +the queue authority. A committed enqueue publishes a bounded best-effort hint +before the existing host wake step. Failed publication cannot lose or invalidate +the command. The backend logs static `mobile_command_events` notify durations +and outcomes without payloads. No queue data is stored in the socket object. +Actual production hibernation/eviction and billing have not been measured. + +**Backend enabled November 8, 2026.** A guarded additive patch of the actual +live Worker, not a rebuild or merge of the unrelated backend branch, deployed +the binding/migration with the flag disabled, validated it, then enabled version +`60e72d1d-f8ca-400d-80c6-26a3c81a0482` at 100 percent (deployment +`c38bece8-bd66-4be1-9a6f-93f8c8ed4787`). All 43 pre-existing bindings and unrelated +modules/settings were preserved. Disabled version +`ebb4fc7f-f578-4204-aea2-c5a186db4414` is the forward-compatible rollback target. +Backend commit `4220a15` records exact preservation hashes, tests, and rollback in +`solosystems-backend/docs/MOBILE_COMMAND_WAKEUP_ROLLOUT_2026_11_08.md`. + +Independent read-only production acceptance passed: health, sessions, and +commands returned 200, anonymous events returned 401, authorized non-upgrade +returned 426, and the authenticated WebSocket handshake returned 101 with a +verified accept header. No commands were created/claimed and no notifications +or model turns were requested. + +The combined portable cloud package built from source `5ca28ecf7` includes both +the wakeup client and the prior emoji change. Its immutable package SHA-256 is +`b468432330a561081b312cadeaf051aa404ffe7dab7af142b7c4292fcb2fe317`. +It was staged, then superseded by a newer coordinated cloud rollout. At 18:32 UTC, +the updater and actual `/proc` executable both confirmed running package +`ebf6f2fa8c6783583c38d59c7b214a4b0cc38ea7b50bb767a80a962335781d19`, +with no pending package or updater failure. Duplicate older restart watchers were retired. The +reload client now fails closed if activity status is missing, malformed, or the +probe fails, rather than treating that failure as zero streaming turns. Four +hosted runtime tests, including execution of the shell idle gate, passed. + +A later backend deployment at 12:20 UTC reset the checked-in wakeup flag to +`false`, causing authenticated events HTTP 503 and runtime `event_connect` +`unavailable` records. Normal Wrangler credential refresh restored expired +OAuth access. At 19:06 UTC, a guarded flag-only restoration deployed version +`4eec2c69-b79a-4ab6-958d-dec876356f0c` (deployment +`ab815f4c-f3df-43c5-8251-155a66d1e45d`). The current production `worker.js` +bytes, all 44 bindings except that flag, and unrelated settings including targeted +placement were preserved. Its SHA-256 stayed +`752fde9d95de746ce5b40a349ea2c1918c4e35166f7b2a0f4fb4780b8ffc23b4`. +The previous version `a93daf43-1cae-4907-9bec-00981c0b8cf1` remains the +configuration-only rollback reference, subject to checking subsequent deployments. +Read-only acceptance again passed health/sessions/commands 200, anonymous events +401, authorized non-upgrade 426, and an authenticated WebSocket 101. The backend +source default now stays enabled, with a regression test to prevent ordinary +deployments from silently disabling the reviewed rollout (backend commit +`765f34d`, full backend tests and offline dry-run passed). The actual running +cloud daemon then reconnected naturally after its unavailable cooldown and +logged `event_connect outcome=ok duration_ms=453`, without restarting active +work. This is handshake timing, not phone-to-command latency. No commands or +pushes were generated for this acceptance. + +The ordinary account's command inventory subsequently returned 200. Eight +existing mixed-version records had queue-wait p50 1,916ms and maximum 2,999,318ms. +This tiny historical sample mixes old clients and may include downtime or backlog. +It is not a post-rollout benchmark or evidence of a wakeup speedup. No Apple USB +device was connected and +usbmuxd was inactive. Real-device install/paint and end-to-end command timing +remain excluded from this rollout. + +The separate `jcode-mobile` repository's phone transcript transport now pools +HTTP connections too (commit `a98b496`). Eight actual list calls in a loopback +fixture with 30ms connection setup and 8ms body delay improved from median +315.039ms/eight connections to 97.462ms/one connection. All 85 phone-core tests +passed. See that repository's `docs/MOBILE_SYNC_LATENCY.md` for reproduction, +account/origin isolation and timing boundaries. A new mobile app build and +installation are required before this source change affects a device. No live +phone, push service, or production API was used for these benchmarks. diff --git a/docs/RESUME_BEHAVIOR.md b/docs/RESUME_BEHAVIOR.md index a20e8af875d..82e4ba76ff8 100644 --- a/docs/RESUME_BEHAVIOR.md +++ b/docs/RESUME_BEHAVIOR.md @@ -34,6 +34,26 @@ When opening in a new terminal: - If terminal launch is unavailable, the UI prints manual `jcode --resume ` commands. - The picker remains open after launching and clears the current multi-selection so more sessions can be opened. +## Session already open in another window + +`display.resume_open_elsewhere` decides what happens when the picked session is +already shown by another local jcode window (Linux only): + +```toml +[display] +resume_open_elsewhere = "move" # "off" (default), "move", or "replace" +``` + +- `off`: attach a second client and leave the other window alone. +- `move`: bring the existing window to the focused workspace and focus it, and + do not attach again. A standalone picker window (`jcode --resume`) then exits. + Requires niri. When the window cannot be identified unambiguously (no niri, + or one terminal process hosting several windows) it behaves like `replace`. +- `replace`: resume as usual and close the other window's client. The closed + client exits cleanly, so the session is not marked crashed. + +Env override: `JCODE_RESUME_OPEN_ELSEWHERE`. + ## Saved sessions `/save [label]` bookmarks the current session so it appears in the saved section of the picker. `/unsave` removes that bookmark. diff --git a/docs/SECURITY_DEPENDENCIES.md b/docs/SECURITY_DEPENDENCIES.md index 3d42758170e..25d047f6a9d 100644 --- a/docs/SECURITY_DEPENDENCIES.md +++ b/docs/SECURITY_DEPENDENCIES.md @@ -21,6 +21,18 @@ It is not an allowlist. It is a triage record so advisories are visible and acti | `RUSTSEC-2026-0187` | `lopdf` | `jcode-pdf -> pdf-extract 0.8.2 -> lopdf 0.34` | PDF text extraction (`/pdf`, image/PDF reads) | Stack overflow parsing deeply nested PDF objects. Only reached when extracting text from a (potentially malicious) PDF the user opens; not in the auth/provider/network path. `pdf-extract 0.8.2` pins `lopdf 0.34`, so it cannot be bumped to the fixed `>=0.42` without an upstream `pdf-extract` release. | Upgrade once `pdf-extract` ships a release depending on `lopdf >=0.42`; remove the ignore then. | | `RUSTSEC-2023-0086` | `lexical-core` | `imap -> imap-proto -> lexical-core` | Gmail/IMAP support path | Old unsound transitive dependency in the mail stack. Higher priority than the UI-only findings because it touches network-parsed data. | Investigate upgrading or replacing `imap` / `imap-proto`. If no maintained path exists, isolate or remove the IMAP dependency. | +## Resolved bundled SQLite advisory (2026-11-10) + +- `RUSTSEC-2026-0459` affected the bundled SQLite in `libsqlite3-sys 0.30.1`. + Both `jcode-base` and `jcode-harness-api-server` now use `rusqlite 0.41`, + with the lockfile selecting `rusqlite 0.41.1` and patched + `libsqlite3-sys 0.39.1`. No advisory ignore was added. +- Enable `fallible_uint` alongside `bundled` to preserve existing checked + `u64` SQL conversions. Values beyond SQLite signed-integer capacity remain + errors, rather than wrapping or changing the database representation. +- `scripts/security_preflight.sh --strict` passes with its unchanged triage + list. Existing storage and harness regression tests cover compatibility. + ## Resolved native SSH advisories (2026-11-03) - Upgraded `jcode-sdk`'s optional native SSH transport from `russh 0.54.5` diff --git a/docs/SPAWN_HOOK.md b/docs/SPAWN_HOOK.md index d147f8fbaba..48e7d2467ed 100644 --- a/docs/SPAWN_HOOK.md +++ b/docs/SPAWN_HOOK.md @@ -61,6 +61,30 @@ The hook (and any terminal spawned by the built-in fallback) receives: | `JCODE_SPAWN_COORDINATOR_SESSION_ID` | (swarm spawns) The coordinator session that requested the spawn | | `JCODE_FRESH_SPAWN` | `1` when the spawn is a fresh window handoff | +When a spawn carries explicit local runtime context (`JCODE_HOME`, +`JCODE_RUNTIME_DIR`, `JCODE_SOCKET`, or the other allowlisted runtime +variables), the appended argv is a runtime-aware invocation, not just the bare +jcode program: on Unix it begins with `env`, clears inherited API-socket and +auto-update overrides when they were not explicitly supplied, applies the +allowlisted values, then executes jcode and its original arguments. This is +necessary for remote-control hooks such as kitty, where the hook process's +environment is not inherited by a child launched by the already-running kitty +server. Hooks must forward and execute all appended argv entries unchanged. +For example: + +```bash +kitty @ --to unix:/tmp/kitty.sock launch --type=tab -- "$@" +``` + +On Windows the appended argv stays the plain ` ` +sequence; the runtime context reaches the hook through its process +environment (`JCODE_*` variables) instead. + +With no explicit runtime context, argv remains the historical +` ` sequence. `JCODE_SPAWN_COMMAND` describes the full +runtime-aware command as a shell-escaped string for hooks that accept one +command string. + ### Client terminal environment (multi-terminal routing) The jcode server process is long-lived: it captures terminal-identifying env diff --git a/docs/TERMINAL_CAPABILITIES.md b/docs/TERMINAL_CAPABILITIES.md index 179ae3d9d9a..a0419fcaa40 100644 --- a/docs/TERMINAL_CAPABILITIES.md +++ b/docs/TERMINAL_CAPABILITIES.md @@ -121,6 +121,37 @@ tmux is the most common source of rendering issues in TUI apps because it interp - **TERM mismatch**: If the inner `TERM` doesn't match tmux's advertised capabilities (e.g., app sees `xterm-256color` but tmux only passes `screen-256color`), color/capability negotiation fails silently. - **Clipboard**: OSC 52 clipboard support works but must be explicitly enabled (`set -g set-clipboard on`). +### Adaptive terminal theme + +With `display.theme = "auto"` (the default), jcode queries the terminal's +background using OSC 11 at startup, every five seconds during the session, and +when focus returns (rate limited). This also runs while waiting for a model, +streaming output, and executing tools, so day/night terminal changes do not +require restarting jcode or submitting another message. Background replies are +consumed before shortcuts, pickers, and the composer see them. Full-frame +repainting replaces cached animation frames when the theme changes. + +`JCODE_THEME=light|dark` or `display.theme = "light"|"dark"` disables live +detection. Terminals that do not answer retain the current theme. Background +queries need to be supported by the multiplexer as well as the outer terminal. + +Tmux normally answers OSC 11 from a cached background captured at attachment. +For live changes in terminals that do not notify tmux about palette updates, +set `set -g allow-passthrough on` in your tmux configuration before launching +jcode. When that option is enabled, jcode refreshes the outer terminal color +using a tmux DCS envelope, then reads tmux's updated cache on the next timer +tick. The option is checked once at TUI +startup with a bounded local tmux command. Without it, ordinary queries still +work, but theme changes depend on tmux refreshing its cache (for example when +reattaching, or receiving supported terminal theme notifications). Passthrough +permits applications to send control sequences to the outer terminal, so enable +it only for trusted applications. Clipboard forwarding remains plain OSC 52 and +only needs `set-clipboard on`, independent of this theme-query option. + +This implementation uses OSC 11 replies and periodic probing, not the newer +DEC mode 2031 color-scheme notification subscription, which the current +crossterm input parser does not expose. + --- ## Recommendations for TUI Developers diff --git a/docs/USAGE_LIMIT_ROUTING.md b/docs/USAGE_LIMIT_ROUTING.md new file mode 100644 index 00000000000..11745ffe8dd --- /dev/null +++ b/docs/USAGE_LIMIT_ROUTING.md @@ -0,0 +1,49 @@ +# Usage exhaustion and temporary throttles + +Jcode treats a definitive exhausted usage window differently from a temporary +rate limit. HTTP 429 alone, including Anthropic's “would exceed your account's +rate limit”, does not prove the account has exhausted its plan allowance. + +## Configuration + +In `~/.jcode/config.toml`: + +```toml +[provider] +# Allow switching among explicitly enrolled accounts on the same provider. +same_provider_account_failover = true + +# Cross-provider switching still requires an approved auto-switch pool route. +# "manual" disables automatic cross-provider resends entirely. +cross_provider_failover = "countdown" + +# Skip the TUI's three-second countdown for definitive usage exhaustion. +# false retains the cancelable countdown even for exhausted usage. +exhausted_usage_immediate_failover = true + +# Prefer bounded same-account retries for recoverable Anthropic rate limits. +# false lets the account router rotate immediately when another pool member exists. +anthropic_retry_recoverable_rate_limits = true +``` + +## Behavior + +- Explicit exhausted usage or credit balance errors return to the router without + sleeping. Opted-in earned-reset recovery remains under its existing settings. +- On Anthropic, ambiguous rate-limit errors retry on the current account for at + most the existing three total attempts when there is no `Retry-After` hint, or + its remaining delay is at most five seconds. The server's short retry hint is + respected. Longer hints yield to the account router when alternatives exist. +- Authentication failures and long reset delays do not take the recoverable + throttle retry path. +- Once same-provider recovery and account selection are exhausted, the TUI skips + the cross-provider countdown only for definitive exhausted usage, and only if + `exhausted_usage_immediate_failover` is enabled. Ambiguous failures retain the + existing countdown. `cross_provider_failover = "manual"` always takes priority. +- No setting enrolls new accounts, enables API billing, purchases credits, or + bypasses the auto-switch pool. If no approved route remains, Jcode reports the + failure instead of switching to an arbitrary provider. + +The immediate-exhaustion preference controls the TUI countdown. API clients +that opt into server-side failover retain their existing server-side routing +behavior. diff --git a/docs/anthropic-fable-resets.md b/docs/anthropic-fable-resets.md new file mode 100644 index 00000000000..677db3fe8b6 --- /dev/null +++ b/docs/anthropic-fable-resets.md @@ -0,0 +1,87 @@ +# Anthropic Fable earned resets + +Set `[provider].anthropic_auto_fable_reset = true` to opt in. The default is +`false`, independent of OpenAI's banked-reset setting. Automatic retry integrates +through `usage::auto_reset_fable_usage(account_label) -> Result`. + +This redeems earned Fable grants, never buys usage credits, and never substitutes +the separate `juniper_tide` five-hour reset program. `true` requires both a +successful reset response and fresh usage showing no exhausted limit, or a +fresh clear status after waiting for another agent's account lock. Missing, +malformed, expired, paused, blocked or unrelated grants fail closed. + +Automatic redemption currently requires a named stored OAuth account. `None` +returns false rather than risking a changing active-account fallback. The exact +account token is pinned for profile, status and claim. Account and organization +UUIDs from its profile scope the cross-process lock, including account aliases. + +## Verified primary contract, November 10, 2026 + +Offer semantics: + + +A Fable reset restores weekly usage spent on Fable, leaving other models' weekly +usage intact, and refills the five-hour window when present. Redemption is +irreversible. The website permits early redemption, but Jcode automatic resets +require actual server-confirmed weekly exhaustion and a usable grant. A +five-hour-only wall never spends a Fable grant automatically. + +The installed Claude Code **2.1.267** contains only the older `juniper_tide` +contract. The newer official npm release **2.1.322** establishes the actual Fable +contract. It was downloaded and inspected statically, not installed or executed: + +- Metadata: +- Platform metadata: +- Official package tarball: +- npm platform tarball SHA-1: `b56040e7f01b956839d56504882d36a177f06a89`. +- Read-only GET: `/api/oauth/usage?cedar_ember=1&skip_spend=1`. +- Status block: `cedar_ember`, containing `eligible`, `at_limit`, `exhausted`, + `grants`, `next_grant_id`, `weekly_resets_at`, `cooldown_until`. +- Grant fields include `id`, `resets_left`, `starts_at`, `ends_at`, `clears`, + `paused`, `usable_now`, `use_requires_limit`, `percent_used`, `blocking`, + `type`, `gives_back_weekly`, `unavailable_reason`. +- The CLI's `Ye` classifier identifies Fable grants by `gives_back_weekly`. +- POST: `/api/organizations/{organizationUuid}/reset_rate_limits`, JSON + `{"program":"cedar_ember","grant_id":"","request_id":""}`. +- Claim results: `reset`, `already_used`, `not_limited`, `cooldown`, `ineligible`, + `unavailable`, with `reason`, `resets_left`, `cleared`, `weekly_resets_at`, + `cooldown_until`. An uncertain claim is not permission to spend another grant. +- Grant IDs match `^[a-z0-9_-]{1,40}$`. Request IDs match + `^[A-Za-z0-9_-]{1,64}$`. Jcode uses a UUID request ID. + +Public Settings bundles were investigated too. Curl of + returned a Cloudflare challenge. The browser +opened the app and exposed the public script +`https://assets-proxy.anthropic.com/claude-ai/v2/assets/v1/index-B7IpSks2.js`. +That bundle and the loaded public JavaScript resource graph were searched for +`cedar_ember`, `reset_rate_limits`, and `gives_back_weekly`, without finding a +matching implementation. The implementation source of truth is therefore the +updated official CLI, not guessed website endpoints. No authenticated mutation +or real grant spend was used for discovery or tests. + +## Concurrency and uncertain outcomes + +Jcode uses an OS file lock per verified account/organization identity and checks +fresh status under that lock. Only exhausted all-model weekly or Fable +included-weekly limits that the grant can address qualify. A simultaneous +five-hour wall must also be cleared by the grant. A server-blocked +or unrelated model-specific limit never triggers spending. + +Before POST, Jcode creates and fsyncs a durable journal at +`~/.jcode/fable-resets/.pending` (or the configured Jcode data +root). It stores program, grant ID, request ID and time, never tokens or email. +The journal remains after HTTP errors, timeouts, cancellation, process death, +unknown responses or an unconfirmed reset. Subsequent calls, including after a +restart, cannot spend another grant. This is deliberately more conservative +than automatically replaying the same request ID. + +Only a confirmed reset plus fresh clear quota removes the journal. If a journal +remains, inspect the intended account's Claude Settings > Usage and reconcile +the recorded grant before manually removing that account's `.pending` file. +Do not remove it just to retry an uncertain request. Lock files are persistent +coordination objects and should not be deleted while agents run. + +Existing manual session-reset UI still refers only to `juniper_tide`. For manual +Fable redemption, use the intended account's Claude Settings > Usage > Limit +resets > Reset for free. The Fable automatic setting does not alter manual UI or +provide permission to spend early. diff --git a/docs/badges/downloads.json b/docs/badges/downloads.json index 46c297ef22a..f49dba5f9ec 100644 --- a/docs/badges/downloads.json +++ b/docs/badges/downloads.json @@ -1 +1 @@ -{"schemaVersion": 1, "label": "downloads", "message": "3.14M", "color": "brightgreen"} +{"schemaVersion": 1, "label": "downloads", "message": "3.19M", "color": "brightgreen"} diff --git a/docs/images/star-history.svg b/docs/images/star-history.svg index 6216bae4e56..26bef50e0ee 100644 --- a/docs/images/star-history.svg +++ b/docs/images/star-history.svg @@ -1,6 +1,6 @@ 1jehuang/jcode cumulative GitHub stars over time -Cumulative GitHub stars sampled weekly over the last 26 weeks, ending at 21,533 stars. +Cumulative GitHub stars sampled weekly over the last 26 weeks, ending at 21,683 stars. diff --git a/scripts/cloud_latency_report.py b/scripts/cloud_latency_report.py new file mode 100644 index 00000000000..89d0ec93c44 --- /dev/null +++ b/scripts/cloud_latency_report.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python3 +"""Summarize content-free cloud timing metrics from Jcode log files.""" +import argparse +from collections import defaultdict +from datetime import datetime +import glob +import math +import os +from pathlib import Path +import re +import sys + +FIELD = re.compile(r"\b([a-z_]+)=([^\s,]+)") +NUMBER = re.compile(r"^\d+(?:\.\d+)?$") +SAFE_LABEL = re.compile(r"^[a-zA-Z0-9_.:-]{1,96}$") +TIMESTAMP = re.compile(r"^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)?)\]") +FAILURE_OUTCOMES = { + "error", "failed", "dropped", "timeout", "cancelled", "expired", + "error_or_cancelled", "http_error", "load_error", "read_error", + "size_limit", "oversized", "identity_mismatch", "exit_failure", + "rejected", "undelivered", +} + + +def local_timestamp(value): + """Parse a wall-clock timestamp in the log's local timezone, not UTC.""" + try: + parsed = datetime.fromisoformat(value) + except ValueError as error: + raise argparse.ArgumentTypeError("expected local YYYY-MM-DDTHH:MM:SS") from error + if parsed.tzinfo is not None: + raise argparse.ArgumentTypeError("use the log's local time without a timezone offset") + return parsed + + +def in_window(lines, since=None, until=None): + for line in lines: + if since is not None or until is not None: + match = TIMESTAMP.match(line) + if match is None: + continue + try: + timestamp = datetime.fromisoformat(match[1]) + except ValueError: + continue + if since is not None and timestamp < since: + continue + if until is not None and timestamp >= until: + continue + yield line + + +def collect(lines): + groups = defaultdict(list) + failures = defaultdict(int) + for line in lines: + if not any(prefix in line for prefix in ("[CLOUD_TIMING]", "mobile_sync stage=", "cloud_preview stage=", "cloud_inventory stage=")): + continue + fields = dict(FIELD.findall(line)) + stage = fields.get("stage", "unknown") + operation = fields.get("operation", "") + if not SAFE_LABEL.fullmatch(stage) or (operation and not SAFE_LABEL.fullmatch(operation)): + continue + kind = fields.get("kind", "") + if kind and SAFE_LABEL.fullmatch(kind): + operation = operation + "." + kind if operation else kind + name = stage + (":" + operation if operation else "") + outcome = fields.get("outcome", "unknown") + for metric, value in fields.items(): + if not metric.endswith("_ms") or not NUMBER.fullmatch(value): + continue + number = float(value) + if not math.isfinite(number): + continue + key = (name, metric) + groups[key].append(number) + if outcome in FAILURE_OUTCOMES: + failures[key] += 1 + return groups, failures + + +def percentile(values, fraction): + values = sorted(values) + return values[max(0, math.ceil(len(values) * fraction) - 1)] + + +def report(groups, failures, threshold_ms=0): + rows = [ + (max(values), name, metric, values) + for (name, metric), values in groups.items() + if max(values) >= threshold_ms + ] + print(f"{'stage:operation':52} {'metric':20} {'count':>7} {'errors':>7} {'p50 ms':>10} {'p95 ms':>10} {'max ms':>10}") + for maximum, name, metric, values in sorted(rows, reverse=True): + print(f"{name:52} {metric:20} {len(values):7} {failures[(name, metric)]:7} {percentile(values, .5):10.2f} {percentile(values, .95):10.2f} {maximum:10.2f}") + if not rows: + print("No matching timing samples. Run an instrumented cloud operation first, or select other logs.") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("files", nargs="*", help="Log files or glob patterns. '-' reads stdin. Default: all retained Jcode logs.") + parser.add_argument("--slow-ms", type=float, default=0, help="Only show stages whose maximum reaches this duration.") + parser.add_argument("--since", type=local_timestamp, help="Inclusive local log time, YYYY-MM-DDTHH:MM:SS. Undated records are excluded.") + parser.add_argument("--until", type=local_timestamp, help="Exclusive local log time, YYYY-MM-DDTHH:MM:SS.") + args = parser.parse_args() + if args.since is not None and args.until is not None and args.since >= args.until: + parser.error("--since must be earlier than --until") + files = args.files or [str(Path(os.environ.get("JCODE_HOME", Path.home() / ".jcode")) / "logs" / "jcode-*.log")] + + def lines(): + seen = set() + for pattern in files: + if pattern == "-": + yield from sys.stdin + continue + for filename in sorted(glob.glob(pattern)): + if filename in seen: + continue + seen.add(filename) + try: + with open(filename, encoding="utf-8", errors="replace") as handle: + yield from handle + except OSError as error: + print(f"Could not read log file: {error.strerror}", file=sys.stderr) + groups, failures = collect(in_window(lines(), args.since, args.until)) + report(groups, failures, args.slow_ms) + + +if __name__ == "__main__": + main() diff --git a/scripts/code_size_budget.json b/scripts/code_size_budget.json index a1df3982a48..aa382d02801 100644 --- a/scripts/code_size_budget.json +++ b/scripts/code_size_budget.json @@ -1,126 +1,117 @@ { "threshold_loc": 1200, "tracked_files": { - "crates/jcode-app-core/src/agent.rs": 1399, - "crates/jcode-app-core/src/agent/response_recovery.rs": 1274, - "crates/jcode-app-core/src/agent/turn_execution.rs": 1647, - "crates/jcode-app-core/src/agent/turn_loops.rs": 1364, - "crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs": 1916, + "crates/jcode-app-core/src/agent.rs": 1375, + "crates/jcode-app-core/src/agent/turn_execution.rs": 1628, + "crates/jcode-app-core/src/agent/turn_loops.rs": 1355, + "crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs": 1851, "crates/jcode-app-core/src/overnight.rs": 1275, - "crates/jcode-app-core/src/server.rs": 2653, - "crates/jcode-app-core/src/server/client_actions.rs": 1350, - "crates/jcode-app-core/src/server/client_lifecycle.rs": 3974, - "crates/jcode-app-core/src/server/client_session.rs": 2112, - "crates/jcode-app-core/src/server/client_state.rs": 1365, - "crates/jcode-app-core/src/server/comm_control.rs": 2640, - "crates/jcode-app-core/src/server/comm_session.rs": 1513, + "crates/jcode-app-core/src/server.rs": 2543, + "crates/jcode-app-core/src/server/client_actions.rs": 1335, + "crates/jcode-app-core/src/server/client_lifecycle.rs": 3484, + "crates/jcode-app-core/src/server/client_session.rs": 2072, + "crates/jcode-app-core/src/server/client_state.rs": 1415, + "crates/jcode-app-core/src/server/comm_control.rs": 2581, + "crates/jcode-app-core/src/server/comm_session.rs": 1314, "crates/jcode-app-core/src/server/debug_server_state.rs": 1269, - "crates/jcode-app-core/src/server/jade_relay.rs": 1429, - "crates/jcode-app-core/src/server/provider_control.rs": 1530, - "crates/jcode-app-core/src/server/swarm.rs": 3518, - "crates/jcode-app-core/src/tool/bash.rs": 1590, - "crates/jcode-app-core/src/tool/browser.rs": 1252, - "crates/jcode-app-core/src/tool/communicate.rs": 3159, + "crates/jcode-app-core/src/server/jade_relay.rs": 1338, + "crates/jcode-app-core/src/server/provider_control.rs": 1447, + "crates/jcode-app-core/src/server/swarm.rs": 2123, + "crates/jcode-app-core/src/tool/bash.rs": 1502, + "crates/jcode-app-core/src/tool/browser.rs": 1203, + "crates/jcode-app-core/src/tool/communicate.rs": 3149, "crates/jcode-app-core/src/tool/discover.rs": 2982, "crates/jcode-app-core/src/tool/mod.rs": 1924, - "crates/jcode-app-core/src/tool/session_search.rs": 1892, - "crates/jcode-app-core/src/tool/todo.rs": 2853, - "crates/jcode-app-core/src/update.rs": 1308, + "crates/jcode-app-core/src/tool/session_search.rs": 1869, + "crates/jcode-app-core/src/tool/todo.rs": 1648, + "crates/jcode-app-core/src/update.rs": 1293, "crates/jcode-base/src/auth/lifecycle.rs": 2882, - "crates/jcode-base/src/auth/mod.rs": 1596, - "crates/jcode-base/src/auth/oauth.rs": 1636, + "crates/jcode-base/src/auth/mod.rs": 1585, + "crates/jcode-base/src/auth/oauth.rs": 1469, "crates/jcode-base/src/auth/transfer.rs": 2185, - "crates/jcode-base/src/background.rs": 1713, + "crates/jcode-base/src/background.rs": 1657, "crates/jcode-base/src/browser.rs": 1545, - "crates/jcode-base/src/compaction.rs": 1833, + "crates/jcode-base/src/compaction.rs": 1832, "crates/jcode-base/src/gmail.rs": 1537, "crates/jcode-base/src/import.rs": 1502, "crates/jcode-base/src/jev.rs": 1863, "crates/jcode-base/src/memory.rs": 1572, - "crates/jcode-base/src/provider/catalog_routes.rs": 1596, - "crates/jcode-base/src/provider/mod.rs": 3241, - "crates/jcode-base/src/provider/models.rs": 1363, + "crates/jcode-base/src/provider/mod.rs": 3130, + "crates/jcode-base/src/provider/models.rs": 1297, "crates/jcode-base/src/provider_catalog.rs": 1339, - "crates/jcode-base/src/session.rs": 1823, - "crates/jcode-base/src/sidecar.rs": 1750, - "crates/jcode-base/src/skill.rs": 1593, - "crates/jcode-base/src/subscription_api.rs": 1206, - "crates/jcode-compaction-core/src/lib.rs": 1423, - "crates/jcode-config-types/src/lib.rs": 1623, - "crates/jcode-harness-api-server/src/translate.rs": 3620, + "crates/jcode-base/src/session.rs": 1802, + "crates/jcode-base/src/sidecar.rs": 1754, + "crates/jcode-config-types/src/lib.rs": 1576, + "crates/jcode-harness-api-server/src/translate.rs": 3518, "crates/jcode-import-core/src/lib.rs": 1645, - "crates/jcode-provider-anthropic-runtime/src/lib.rs": 2788, - "crates/jcode-provider-anthropic/src/lib.rs": 1246, - "crates/jcode-provider-bedrock/src/lib.rs": 1612, - "crates/jcode-provider-copilot-runtime/src/lib.rs": 1201, - "crates/jcode-provider-core/src/lib.rs": 1625, + "crates/jcode-provider-anthropic-runtime/src/lib.rs": 2692, + "crates/jcode-provider-anthropic/src/lib.rs": 1270, + "crates/jcode-provider-bedrock/src/lib.rs": 1550, + "crates/jcode-provider-core/src/lib.rs": 1624, "crates/jcode-provider-cursor-runtime/src/agent_transport.rs": 1873, "crates/jcode-provider-cursor-runtime/src/wire.rs": 1431, "crates/jcode-provider-doctor/src/lifecycle_driver.rs": 1980, "crates/jcode-provider-doctor/src/live_provider_probes.rs": 1939, "crates/jcode-provider-doctor/src/provider_e2e.rs": 2727, - "crates/jcode-provider-gemini-runtime/src/lib.rs": 1266, "crates/jcode-provider-metadata/src/catalog.rs": 1351, - "crates/jcode-provider-openai-runtime/src/lib.rs": 1606, - "crates/jcode-provider-openai-runtime/src/openai_provider_impl.rs": 1328, - "crates/jcode-provider-openai-runtime/src/openai_stream_runtime.rs": 2109, - "crates/jcode-provider-openrouter-runtime/src/lib.rs": 2931, + "crates/jcode-provider-openai-runtime/src/lib.rs": 1598, + "crates/jcode-provider-openai-runtime/src/openai_provider_impl.rs": 1310, + "crates/jcode-provider-openai-runtime/src/openai_stream_runtime.rs": 2024, + "crates/jcode-provider-openrouter-runtime/src/lib.rs": 2890, "crates/jcode-render-core/src/math.rs": 1235, - "crates/jcode-sdk/src/client.rs": 1938, + "crates/jcode-sdk/src/client.rs": 1824, "crates/jcode-setup-hints/src/lib.rs": 2660, "crates/jcode-telemetry-core/src/lib.rs": 2775, "crates/jcode-terminal-launch/src/lib.rs": 1700, "crates/jcode-tui-core/src/keybind.rs": 1235, - "crates/jcode-tui-markdown/src/lib.rs": 1230, - "crates/jcode-tui-mermaid/src/lib.rs": 1673, + "crates/jcode-tui-markdown/src/lib.rs": 1226, + "crates/jcode-tui-mermaid/src/lib.rs": 1635, "crates/jcode-tui-mermaid/src/mermaid_cache_render.rs": 1561, "crates/jcode-tui-mermaid/src/mermaid_viewport.rs": 2031, - "crates/jcode-tui-render/src/swarm_gallery.rs": 2057, - "crates/jcode-tui/src/tui/app.rs": 2923, + "crates/jcode-tui-render/src/swarm_gallery.rs": 2026, + "crates/jcode-tui/src/tui/app.rs": 2894, "crates/jcode-tui/src/tui/app/auth.rs": 3475, - "crates/jcode-tui/src/tui/app/auth_account_commands.rs": 1213, - "crates/jcode-tui/src/tui/app/auth_account_picker.rs": 1305, - "crates/jcode-tui/src/tui/app/commands.rs": 4042, - "crates/jcode-tui/src/tui/app/commands_cloud.rs": 1268, + "crates/jcode-tui/src/tui/app/auth_account_picker.rs": 1236, + "crates/jcode-tui/src/tui/app/commands.rs": 3838, + "crates/jcode-tui/src/tui/app/commands_cloud.rs": 1291, "crates/jcode-tui/src/tui/app/debug_bench.rs": 1286, - "crates/jcode-tui/src/tui/app/debug_cmds.rs": 1230, - "crates/jcode-tui/src/tui/app/helpers.rs": 1810, + "crates/jcode-tui/src/tui/app/helpers.rs": 1805, "crates/jcode-tui/src/tui/app/hotkey_feedback.rs": 1227, - "crates/jcode-tui/src/tui/app/inline_interactive.rs": 4208, - "crates/jcode-tui/src/tui/app/input.rs": 4324, - "crates/jcode-tui/src/tui/app/model_context.rs": 1986, - "crates/jcode-tui/src/tui/app/navigation.rs": 1955, + "crates/jcode-tui/src/tui/app/inline_interactive.rs": 4231, + "crates/jcode-tui/src/tui/app/input.rs": 4309, + "crates/jcode-tui/src/tui/app/model_context.rs": 2059, + "crates/jcode-tui/src/tui/app/navigation.rs": 1939, "crates/jcode-tui/src/tui/app/onboarding_flow_control.rs": 1821, - "crates/jcode-tui/src/tui/app/remote.rs": 2278, - "crates/jcode-tui/src/tui/app/remote/key_handling.rs": 2762, - "crates/jcode-tui/src/tui/app/remote/server_events.rs": 3421, - "crates/jcode-tui/src/tui/app/state_ui.rs": 2338, - "crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs": 1804, - "crates/jcode-tui/src/tui/app/tui_lifecycle.rs": 1644, - "crates/jcode-tui/src/tui/app/tui_state.rs": 2909, - "crates/jcode-tui/src/tui/app/turn.rs": 1714, - "crates/jcode-tui/src/tui/backend.rs": 1618, - "crates/jcode-tui/src/tui/info_widget.rs": 2347, - "crates/jcode-tui/src/tui/mod.rs": 2523, - "crates/jcode-tui/src/tui/session_picker.rs": 2695, - "crates/jcode-tui/src/tui/session_picker/loading.rs": 3102, - "crates/jcode-tui/src/tui/ui.rs": 3795, - "crates/jcode-tui/src/tui/ui_frame_metrics.rs": 1434, - "crates/jcode-tui/src/tui/ui_header.rs": 1729, - "crates/jcode-tui/src/tui/ui_inline_image.rs": 1797, - "crates/jcode-tui/src/tui/ui_input.rs": 3026, - "crates/jcode-tui/src/tui/ui_messages.rs": 4633, - "crates/jcode-tui/src/tui/ui_pinned.rs": 1269, - "crates/jcode-tui/src/tui/ui_prepare.rs": 2736, - "crates/jcode-tui/src/tui/ui_tools.rs": 1687, - "crates/jcode-tui/src/tui/ui_viewport.rs": 1664, + "crates/jcode-tui/src/tui/app/remote.rs": 2283, + "crates/jcode-tui/src/tui/app/remote/key_handling.rs": 2080, + "crates/jcode-tui/src/tui/app/remote/server_events.rs": 3435, + "crates/jcode-tui/src/tui/app/state_ui.rs": 2313, + "crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs": 1788, + "crates/jcode-tui/src/tui/app/tui_lifecycle.rs": 1650, + "crates/jcode-tui/src/tui/app/tui_state.rs": 2797, + "crates/jcode-tui/src/tui/app/turn.rs": 1702, + "crates/jcode-tui/src/tui/backend.rs": 1610, + "crates/jcode-tui/src/tui/info_widget.rs": 2236, + "crates/jcode-tui/src/tui/mod.rs": 2478, + "crates/jcode-tui/src/tui/session_picker.rs": 2591, + "crates/jcode-tui/src/tui/session_picker/loading.rs": 3013, + "crates/jcode-tui/src/tui/ui.rs": 3866, + "crates/jcode-tui/src/tui/ui_frame_metrics.rs": 1479, + "crates/jcode-tui/src/tui/ui_input.rs": 2494, + "crates/jcode-tui/src/tui/ui_messages.rs": 4627, + "crates/jcode-tui/src/tui/ui_pinned.rs": 1233, + "crates/jcode-tui/src/tui/ui_prepare.rs": 2764, + "crates/jcode-tui/src/tui/ui_tools.rs": 1643, + "crates/jcode-tui/src/tui/ui_viewport.rs": 1517, "src/bin/memory_recall_bench.rs": 2667, "src/bin/tui_bench.rs": 1765, - "src/cli/acp.rs": 2319, - "src/cli/cloud_move.rs": 2839, - "src/cli/commands.rs": 3632, - "src/cli/dispatch.rs": 1552, - "src/cli/provider_init.rs": 1955 + "src/cli/acp.rs": 2053, + "src/cli/args.rs": 1301, + "src/cli/cloud_move.rs": 2776, + "src/cli/commands.rs": 3584, + "src/cli/dispatch.rs": 1533, + "src/cli/provider_init.rs": 1955, + "src/cli/ssh_transport.rs": 1295 }, "version": 1 } diff --git a/scripts/dev_cargo.sh b/scripts/dev_cargo.sh index 63d58516b14..9c5c134e37a 100755 --- a/scripts/dev_cargo.sh +++ b/scripts/dev_cargo.sh @@ -541,6 +541,15 @@ select_build_jobs() { # reintroduce per-build churn; the publish guard validates dirty builds via the # source fingerprint / mtime path instead of the embedded flag. export_git_build_metadata() { + # Hosted snapshots have no Git history. Describe against the package's base + # tag so build.rs can recover exactly the same commit offset remotely. + if [[ -z "${JCODE_BUILD_GIT_TAG+x}" ]] && command -v git >/dev/null 2>&1; then + local base_version description + base_version="$(awk '/^\[package\]/{package=1; next} /^\[/{package=0} package && /^version *=/{gsub(/"/, "", $3); print $3; exit}' "$repo_root/Cargo.toml")" + if [[ -n "$base_version" ]] && description="$(git -C "$repo_root" describe --tags --long --match "v${base_version}" HEAD 2>/dev/null)"; then + export JCODE_BUILD_GIT_TAG="$description" + fi + fi # Hosted source snapshots do not include .git. Forward the history alongside # the identity metadata so /changelog is populated in hosted self-dev builds. # Preserve explicit caller overrides, including an intentionally empty one. @@ -1079,6 +1088,18 @@ done < <(build_cargo_argv "$@") start_rust_action_log +# Self-dev builds compile the whole jcode binary, which saturates every core +# for minutes and drains a laptop battery. When `cargo` is the justrust proxy +# and the caller has not picked a route, force them onto hosted build +# machines; justrust brings the optimized binary back into target/selfdev. +# Opt out with JUSTRUST_REMOTE=off JUSTRUST_LOCAL_REASON=. +if [[ -z "${JUSTRUST_REMOTE:-}" && "${cargo_argv[0]:-}" == "build" ]] \ + && [[ " ${cargo_argv[*]} " == *" --profile selfdev "* || " ${cargo_argv[*]} " == *" --profile=selfdev "* ]] \ + && command -v justrust >/dev/null 2>&1; then + export JUSTRUST_REMOTE=force JUSTRUST_REMOTE_REQUIRED=1 + log "selfdev build: routing to hosted justrust (JUSTRUST_REMOTE=force)" +fi + if [[ "${JCODE_REMOTE_CARGO:-0}" == "1" ]]; then if remote_cargo_preflight; then log "using remote cargo via scripts/remote_build.sh" diff --git a/scripts/fork/code_size_budget.json b/scripts/fork/code_size_budget.json index 1f8c0e77817..9cfd527eb3b 100644 --- a/scripts/fork/code_size_budget.json +++ b/scripts/fork/code_size_budget.json @@ -4,20 +4,21 @@ "crates/jcode-app-core/src/agent.rs": 1399, "crates/jcode-app-core/src/agent/response_recovery.rs": 1274, "crates/jcode-app-core/src/agent/turn_execution.rs": 1647, - "crates/jcode-app-core/src/agent/turn_loops.rs": 1364, + "crates/jcode-app-core/src/agent/turn_loops.rs": 1370, "crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs": 1916, "crates/jcode-app-core/src/overnight.rs": 1275, "crates/jcode-app-core/src/server.rs": 2653, "crates/jcode-app-core/src/server/client_actions.rs": 1431, "crates/jcode-app-core/src/server/client_lifecycle.rs": 3974, "crates/jcode-app-core/src/server/client_session.rs": 2112, - "crates/jcode-app-core/src/server/client_state.rs": 1394, + "crates/jcode-app-core/src/server/client_state.rs": 1477, "crates/jcode-app-core/src/server/comm_control.rs": 2640, "crates/jcode-app-core/src/server/comm_session.rs": 1513, "crates/jcode-app-core/src/server/debug_server_state.rs": 1269, "crates/jcode-app-core/src/server/jade_relay.rs": 1429, "crates/jcode-app-core/src/server/provider_control.rs": 1530, "crates/jcode-app-core/src/server/swarm.rs": 3518, + "crates/jcode-app-core/src/server/util.rs": 1285, "crates/jcode-app-core/src/tool/bash.rs": 1590, "crates/jcode-app-core/src/tool/browser.rs": 1252, "crates/jcode-app-core/src/tool/communicate.rs": 3159, @@ -32,29 +33,29 @@ "crates/jcode-base/src/auth/transfer.rs": 2185, "crates/jcode-base/src/background.rs": 1713, "crates/jcode-base/src/browser.rs": 1545, - "crates/jcode-base/src/compaction.rs": 1833, + "crates/jcode-base/src/compaction.rs": 1874, "crates/jcode-base/src/gmail.rs": 1537, "crates/jcode-base/src/import.rs": 1502, "crates/jcode-base/src/jev.rs": 1863, "crates/jcode-base/src/memory.rs": 1572, "crates/jcode-base/src/provider/catalog_routes.rs": 1596, - "crates/jcode-base/src/provider/mod.rs": 3241, + "crates/jcode-base/src/provider/mod.rs": 3303, "crates/jcode-base/src/provider/models.rs": 1363, - "crates/jcode-base/src/provider_catalog.rs": 1339, + "crates/jcode-base/src/provider_catalog.rs": 1344, "crates/jcode-base/src/session.rs": 1823, "crates/jcode-base/src/sidecar.rs": 1754, "crates/jcode-base/src/skill.rs": 1593, "crates/jcode-base/src/subscription_api.rs": 1206, "crates/jcode-compaction-core/src/lib.rs": 1423, - "crates/jcode-config-types/src/lib.rs": 1623, + "crates/jcode-config-types/src/lib.rs": 1629, "crates/jcode-harness-api-server/src/translate.rs": 3620, "crates/jcode-import-core/src/lib.rs": 1645, - "crates/jcode-protocol/src/wire/events.rs": 1205, + "crates/jcode-protocol/src/wire/events.rs": 1228, "crates/jcode-provider-anthropic-runtime/src/lib.rs": 2788, - "crates/jcode-provider-anthropic/src/lib.rs": 1267, + "crates/jcode-provider-anthropic/src/lib.rs": 1270, "crates/jcode-provider-bedrock/src/lib.rs": 1612, "crates/jcode-provider-copilot-runtime/src/lib.rs": 1201, - "crates/jcode-provider-core/src/lib.rs": 1625, + "crates/jcode-provider-core/src/lib.rs": 1659, "crates/jcode-provider-cursor-runtime/src/agent_transport.rs": 1873, "crates/jcode-provider-cursor-runtime/src/wire.rs": 1431, "crates/jcode-provider-doctor/src/lifecycle_driver.rs": 1980, @@ -68,9 +69,10 @@ "crates/jcode-provider-openrouter-runtime/src/lib.rs": 2931, "crates/jcode-render-core/src/math.rs": 1235, "crates/jcode-sdk/src/client.rs": 1938, + "crates/jcode-sdk/src/launch.rs": 1226, "crates/jcode-setup-hints/src/lib.rs": 2660, "crates/jcode-telemetry-core/src/lib.rs": 2775, - "crates/jcode-terminal-launch/src/lib.rs": 1700, + "crates/jcode-terminal-launch/src/lib.rs": 1911, "crates/jcode-tui-core/src/keybind.rs": 1235, "crates/jcode-tui-markdown/src/lib.rs": 1230, "crates/jcode-tui-mermaid/src/lib.rs": 1673, @@ -82,47 +84,53 @@ "crates/jcode-tui/src/tui/app/auth_account_commands.rs": 1213, "crates/jcode-tui/src/tui/app/auth_account_picker.rs": 1305, "crates/jcode-tui/src/tui/app/commands.rs": 4042, - "crates/jcode-tui/src/tui/app/commands_cloud.rs": 1291, + "crates/jcode-tui/src/tui/app/commands_cloud.rs": 1311, "crates/jcode-tui/src/tui/app/debug_bench.rs": 1286, "crates/jcode-tui/src/tui/app/debug_cmds.rs": 1230, - "crates/jcode-tui/src/tui/app/helpers.rs": 1810, + "crates/jcode-tui/src/tui/app/helpers.rs": 2057, "crates/jcode-tui/src/tui/app/hotkey_feedback.rs": 1227, - "crates/jcode-tui/src/tui/app/inline_interactive.rs": 4240, - "crates/jcode-tui/src/tui/app/input.rs": 4324, - "crates/jcode-tui/src/tui/app/model_context.rs": 2059, - "crates/jcode-tui/src/tui/app/navigation.rs": 1955, + "crates/jcode-tui/src/tui/app/inline_interactive.rs": 4265, + "crates/jcode-tui/src/tui/app/input.rs": 4337, + "crates/jcode-tui/src/tui/app/model_context.rs": 2120, + "crates/jcode-tui/src/tui/app/navigation.rs": 1958, "crates/jcode-tui/src/tui/app/onboarding_flow_control.rs": 1821, - "crates/jcode-tui/src/tui/app/remote.rs": 2314, + "crates/jcode-tui/src/tui/app/remote.rs": 2410, "crates/jcode-tui/src/tui/app/remote/key_handling.rs": 2762, - "crates/jcode-tui/src/tui/app/remote/server_events.rs": 3443, + "crates/jcode-tui/src/tui/app/remote/server_events.rs": 3550, + "crates/jcode-tui/src/tui/app/run_shell.rs": 1266, "crates/jcode-tui/src/tui/app/state_ui.rs": 2338, "crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs": 1804, - "crates/jcode-tui/src/tui/app/tui_lifecycle.rs": 1669, - "crates/jcode-tui/src/tui/app/tui_state.rs": 2909, + "crates/jcode-tui/src/tui/app/tui_lifecycle.rs": 1686, + "crates/jcode-tui/src/tui/app/tui_state.rs": 2936, "crates/jcode-tui/src/tui/app/turn.rs": 1714, - "crates/jcode-tui/src/tui/backend.rs": 1618, + "crates/jcode-tui/src/tui/backend.rs": 1711, "crates/jcode-tui/src/tui/info_widget.rs": 2347, "crates/jcode-tui/src/tui/mod.rs": 2523, "crates/jcode-tui/src/tui/session_picker.rs": 2695, - "crates/jcode-tui/src/tui/session_picker/loading.rs": 3102, - "crates/jcode-tui/src/tui/ui.rs": 3868, - "crates/jcode-tui/src/tui/ui_frame_metrics.rs": 1479, + "crates/jcode-tui/src/tui/session_picker/loading.rs": 3135, + "crates/jcode-tui/src/tui/session_picker/transcript_search.rs": 1492, + "crates/jcode-tui/src/tui/ui.rs": 4032, + "crates/jcode-tui/src/tui/ui_frame_metrics.rs": 1497, "crates/jcode-tui/src/tui/ui_header.rs": 1729, + "crates/jcode-tui/src/tui/ui_header/account_router.rs": 1454, "crates/jcode-tui/src/tui/ui_inline_image.rs": 1797, - "crates/jcode-tui/src/tui/ui_input.rs": 3026, - "crates/jcode-tui/src/tui/ui_messages.rs": 4633, + "crates/jcode-tui/src/tui/ui_input.rs": 3028, + "crates/jcode-tui/src/tui/ui_messages.rs": 4744, "crates/jcode-tui/src/tui/ui_pinned.rs": 1269, - "crates/jcode-tui/src/tui/ui_prepare.rs": 2764, - "crates/jcode-tui/src/tui/ui_tools.rs": 1687, + "crates/jcode-tui/src/tui/ui_prepare.rs": 2847, + "crates/jcode-tui/src/tui/ui_todo_stages.rs": 1222, + "crates/jcode-tui/src/tui/ui_tools.rs": 1700, "crates/jcode-tui/src/tui/ui_viewport.rs": 1664, "src/bin/memory_recall_bench.rs": 2667, "src/bin/tui_bench.rs": 1765, "src/cli/acp/daemon_router.rs": 1307, - "src/cli/args.rs": 1306, - "src/cli/cloud_move.rs": 2839, + "src/cli/acp/session.rs": 1203, + "src/cli/args.rs": 1333, + "src/cli/cloud_move.rs": 2857, "src/cli/commands.rs": 3632, "src/cli/dispatch.rs": 1552, - "src/cli/provider_init.rs": 2007 + "src/cli/provider_init.rs": 2007, + "src/cli/ssh_transport.rs": 1295 }, "version": 1 } diff --git a/scripts/fork/panic_budget.json b/scripts/fork/panic_budget.json index 1b92d8339f7..c72b97f043e 100644 --- a/scripts/fork/panic_budget.json +++ b/scripts/fork/panic_budget.json @@ -53,6 +53,7 @@ "crates/jcode-tui/src/tui/app/auth_remote/picker.rs": 1, "crates/jcode-tui/src/tui/app/commands_cloud.rs": 2, "crates/jcode-tui/src/tui/app/remote.rs": 1, + "crates/jcode-tui/src/tui/app/run_shell.rs": 1, "crates/jcode-tui/src/tui/app/usage_reset.rs": 1, "crates/jcode-tui/src/tui/session_picker.rs": 2, "crates/jcode-tui/src/tui/ui_inline_interactive.rs": 1, diff --git a/scripts/fork/swallowed_error_budget.json b/scripts/fork/swallowed_error_budget.json index 8ea263fee91..b02ce23f2b9 100644 --- a/scripts/fork/swallowed_error_budget.json +++ b/scripts/fork/swallowed_error_budget.json @@ -12,9 +12,9 @@ "unwrap_or_default": 0 }, "crates/jcode-app-core/src/agent/cache_request.rs": { - "dot_ok": 0, "let_underscore": 0, - "unwrap_or_default": 2 + "dot_ok": 0, + "unwrap_or_default": 3 }, "crates/jcode-app-core/src/agent/environment.rs": { "dot_ok": 0, @@ -222,8 +222,8 @@ "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/client_state.rs": { - "dot_ok": 3, "let_underscore": 0, + "dot_ok": 4, "unwrap_or_default": 5 }, "crates/jcode-app-core/src/server/comm_await.rs": { @@ -1016,6 +1016,11 @@ "let_underscore": 0, "unwrap_or_default": 0 }, + "crates/jcode-base/src/mobile_sync.rs": { + "let_underscore": 0, + "dot_ok": 2, + "unwrap_or_default": 0 + }, "crates/jcode-base/src/model_pricing.rs": { "dot_ok": 2, "let_underscore": 1, @@ -1817,8 +1822,8 @@ "unwrap_or_default": 2 }, "crates/jcode-sdk/src/turn_retry.rs": { - "dot_ok": 1, "let_underscore": 0, + "dot_ok": 2, "unwrap_or_default": 1 }, "crates/jcode-selfdev-types/src/desktop.rs": { @@ -2586,16 +2591,26 @@ "let_underscore": 5, "unwrap_or_default": 6 }, + "crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs": { + "let_underscore": 0, + "dot_ok": 0, + "unwrap_or_default": 1 + }, "crates/jcode-tui/src/tui/session_picker/filter.rs": { "dot_ok": 0, "let_underscore": 0, "unwrap_or_default": 4 }, "crates/jcode-tui/src/tui/session_picker/loading.rs": { + "let_underscore": 4, "dot_ok": 28, - "let_underscore": 1, "unwrap_or_default": 18 }, + "crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs": { + "let_underscore": 0, + "dot_ok": 3, + "unwrap_or_default": 0 + }, "crates/jcode-tui/src/tui/session_picker/loading/pi_sessions.rs": { "dot_ok": 5, "let_underscore": 0, @@ -2771,6 +2786,11 @@ "let_underscore": 0, "unwrap_or_default": 2 }, + "src/cli/acp/outcome.rs": { + "let_underscore": 0, + "dot_ok": 0, + "unwrap_or_default": 1 + }, "src/cli/acp/presentation.rs": { "dot_ok": 0, "let_underscore": 1, @@ -2796,6 +2816,11 @@ "let_underscore": 18, "unwrap_or_default": 8 }, + "src/cli/cloud_move/transport.rs": { + "let_underscore": 1, + "dot_ok": 1, + "unwrap_or_default": 0 + }, "src/cli/commands.rs": { "dot_ok": 13, "let_underscore": 3, diff --git a/scripts/fork/test_size_budget.json b/scripts/fork/test_size_budget.json index 78ce957530f..6aa6014e32c 100644 --- a/scripts/fork/test_size_budget.json +++ b/scripts/fork/test_size_budget.json @@ -4,6 +4,7 @@ "crates/jcode-app-core/src/agent_tests.rs": 3253, "crates/jcode-app-core/src/server/client_actions_tests.rs": 1325, "crates/jcode-app-core/src/server/client_lifecycle_tests.rs": 1939, + "crates/jcode-app-core/src/server/client_state_tests.rs": 1209, "crates/jcode-app-core/src/server/comm_session_tests.rs": 1332, "crates/jcode-app-core/src/server/provider_control_tests.rs": 1352, "crates/jcode-app-core/src/tool/bash_tests.rs": 1259, @@ -12,12 +13,12 @@ "crates/jcode-app-core/src/tool/selfdev/tests.rs": 1460, "crates/jcode-app-core/src/tool/tests.rs": 1976, "crates/jcode-base/src/auth/tests.rs": 1206, - "crates/jcode-base/src/compaction_tests.rs": 1271, + "crates/jcode-base/src/compaction_tests.rs": 1351, "crates/jcode-base/src/config_tests.rs": 1795, "crates/jcode-base/src/live_tests.rs": 3087, "crates/jcode-base/src/provider/tests.rs": 1335, "crates/jcode-base/src/provider/tests/model_resolution.rs": 2317, - "crates/jcode-base/src/provider_catalog_tests.rs": 1409, + "crates/jcode-base/src/provider_catalog_tests.rs": 1424, "crates/jcode-base/src/session_tests/cases.rs": 3298, "crates/jcode-base/src/usage/tests.rs": 1317, "crates/jcode-harness-api-server/src/translate_tests.rs": 4199, @@ -25,14 +26,14 @@ "crates/jcode-provider-openrouter-runtime/src/openrouter_tests.rs": 3568, "crates/jcode-sdk/tests/client_behavior.rs": 1432, "crates/jcode-telemetry-core/src/tests.rs": 1836, - "crates/jcode-tui/src/tui/app/commands_cloud_tests.rs": 1315, + "crates/jcode-tui/src/tui/app/commands_cloud_tests.rs": 1327, "crates/jcode-tui/src/tui/app/remote_tests.rs": 1451, "crates/jcode-tui/src/tui/app/tests.rs": 2030, "crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs": 1604, "crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01.rs": 1326, "crates/jcode-tui/src/tui/app/tests/onboarding_eval.rs": 3305, "crates/jcode-tui/src/tui/app/tests/onboarding_flow.rs": 1672, - "crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs": 1714, + "crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs": 1748, "crates/jcode-tui/src/tui/app/tests/remote_events_reload_04.rs": 2676, "crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs": 1251, "crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs": 2413, @@ -43,15 +44,16 @@ "crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs": 1716, "crates/jcode-tui/src/tui/app/tests/state_model_poke_01/part_01.rs": 1382, "crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs": 1683, - "crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs": 3121, + "crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs": 3132, "crates/jcode-tui/src/tui/app/tests/swarm_plan_graph_inline.rs": 1883, "crates/jcode-tui/src/tui/info_widget_tests.rs": 1961, - "crates/jcode-tui/src/tui/session_picker/loading_tests.rs": 1568, + "crates/jcode-tui/src/tui/session_picker/loading_tests.rs": 1686, "crates/jcode-tui/src/tui/session_picker_tests.rs": 2806, - "crates/jcode-tui/src/tui/ui_messages/tests.rs": 3737, + "crates/jcode-tui/src/tui/ui_messages/tests.rs": 3745, "crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs": 1225, "crates/jcode-tui/src/tui/ui_tests/prepare.rs": 1374, "crates/jcode-tui/src/tui/ui_tests/tools.rs": 1530, + "crates/jcode-tui/src/tui/ui_todo_stages_tests.rs": 1224, "src/cli/args/tests.rs": 1225, "src/cli/commands_tests.rs": 1805 }, diff --git a/scripts/panic_budget.json b/scripts/panic_budget.json index b047640f735..856035a09a4 100644 --- a/scripts/panic_budget.json +++ b/scripts/panic_budget.json @@ -1,9 +1,7 @@ { - "total": 174, + "total": 147, "tracked_files": { - "crates/jcode-app-core/build.rs": 4, "crates/jcode-app-core/src/agent/provider.rs": 1, - "crates/jcode-app-core/src/agent/tool_markup_recovery.rs": 2, "crates/jcode-app-core/src/session_launch.rs": 1, "crates/jcode-app-core/src/tool/bash.rs": 2, "crates/jcode-app-core/src/tool/browser.rs": 2, @@ -11,26 +9,20 @@ "crates/jcode-app-core/src/tool/browser_jev.rs": 1, "crates/jcode-app-core/src/tool/calendar.rs": 1, "crates/jcode-app-core/src/tool/compile_remote.rs": 2, - "crates/jcode-app-core/src/tool/computer/win.rs": 3, "crates/jcode-app-core/src/tool/discover.rs": 1, "crates/jcode-app-core/src/tool/mod.rs": 1, "crates/jcode-app-core/src/tool/sdk.rs": 1, - "crates/jcode-app-core/src/tool/todo.rs": 1, "crates/jcode-base/src/auth/account_store.rs": 1, - "crates/jcode-base/src/auth/oauth.rs": 3, "crates/jcode-base/src/hooks.rs": 1, "crates/jcode-base/src/mcp/manager.rs": 2, "crates/jcode-base/src/memory_jev.rs": 1, - "crates/jcode-base/src/provider_activity_oauth.rs": 3, "crates/jcode-base/src/subscription_notice.rs": 1, "crates/jcode-base/src/voice_intent.rs": 5, "crates/jcode-harness-api-server/src/translate.rs": 2, "crates/jcode-harness-api-server/src/translate/session_files.rs": 1, "crates/jcode-harness-api/examples/harness_repl.rs": 15, - "crates/jcode-provider-anthropic/src/wedge_fixture_check.rs": 3, "crates/jcode-provider-core/src/model_names.rs": 1, "crates/jcode-provider-cursor-runtime/src/wire.rs": 1, - "crates/jcode-provider-doctor/src/lifecycle_driver.rs": 2, "crates/jcode-provider-openai/src/stream.rs": 3, "crates/jcode-render-core/src/math.rs": 4, "crates/jcode-render-core/src/preprocess.rs": 2, @@ -53,14 +45,12 @@ "crates/jcode-tui/src/tui/app/auth_remote/picker.rs": 1, "crates/jcode-tui/src/tui/app/commands_cloud.rs": 2, "crates/jcode-tui/src/tui/app/usage_reset.rs": 1, - "crates/jcode-tui/src/tui/session_picker.rs": 2, "crates/jcode-tui/src/tui/ui_inline_interactive.rs": 1, "src/bin/memory_recall_bench.rs": 14, "src/cli/acp.rs": 2, "src/cli/cloud_managed.rs": 1, - "src/cli/cloud_move.rs": 1, "src/cli/commands/menubar.rs": 1, - "src/cli/login/device_login.rs": 9, + "src/cli/login/device_login.rs": 6, "src/cli/ssh.rs": 1, "src/cli/tui_launch.rs": 1 }, diff --git a/scripts/preview_release.py b/scripts/preview_release.py index 108013abd4d..7acabbc08e1 100644 --- a/scripts/preview_release.py +++ b/scripts/preview_release.py @@ -50,6 +50,21 @@ def require_head(repository, sha): raise ValueError("master advanced, refusing stale preview") +def require_snapshot(repository, sha, pinned=False): + if not pinned: + require_head(repository, sha) + return + if (os.environ.get("GITHUB_EVENT_NAME") != "workflow_dispatch" + or os.environ.get("GITHUB_REF") != "refs/heads/master" + or os.environ.get("GITHUB_SHA") != sha): + raise ValueError("Pinned previews require a manual master dispatch at the requested SHA") + head = api(f"repos/{repository}/git/ref/heads/master")["object"]["sha"] + if head != sha: + comparison = api(f"repos/{repository}/compare/{sha}...{head}") + if comparison.get("merge_base_commit", {}).get("sha") != sha: + raise ValueError("Pinned preview SHA is not an ancestor of current master") + + def inventory(directory): directory = Path(directory) files = {p.name: p for p in directory.iterdir() if p.is_file()} @@ -60,13 +75,13 @@ def inventory(directory): return files -def publish(repository, sha, tag, directory): +def publish(repository, sha, tag, directory, pinned=False): validate(repository, sha, tag) files = inventory(directory) # Recheck CI after the builds as well as before them. No stale/rerun evidence. authorize(repository, sha, timeout=0, preview=True) require_preview_window(repository) - require_head(repository, sha) + require_snapshot(repository, sha, pinned) changes = change_notes(repository, sha, tag, gh) checksum = Path(directory) / "SHA256SUMS" checksum.write_text("".join(f"{hashlib.sha256(files[name].read_bytes()).hexdigest()} {name}\n" @@ -111,7 +126,7 @@ def publish(repository, sha, tag, directory): raise ValueError("Preview tag changed during staging") require_preview_window(repository) # This MUST remain the final check immediately before the visibility change. - require_head(repository, sha) + require_snapshot(repository, sha, pinned) gh("release", "edit", tag, "--repo", repository, "--draft=false", "--prerelease", "--latest=false") @@ -122,13 +137,15 @@ def main(): parser.add_argument("--sha", required=True) parser.add_argument("--tag", required=True) parser.add_argument("--assets", default="dist") + parser.add_argument("--pinned", action="store_true", + help="Allow the original manual master snapshot after master advances") args = parser.parse_args() validate(args.repo, args.sha, args.tag) if args.action == "check-head": require_preview_window(args.repo) - require_head(args.repo, args.sha) + require_snapshot(args.repo, args.sha, args.pinned) else: - publish(args.repo, args.sha, args.tag, args.assets) + publish(args.repo, args.sha, args.tag, args.assets, pinned=args.pinned) if __name__ == "__main__": diff --git a/scripts/swallowed_error_budget.json b/scripts/swallowed_error_budget.json index ad832cfc8e1..5c609a41fb1 100644 --- a/scripts/swallowed_error_budget.json +++ b/scripts/swallowed_error_budget.json @@ -1,9 +1,9 @@ { - "total": 3961, + "total": 3877, "totals_by_pattern": { - "dot_ok": 1553, - "let_underscore": 1428, - "unwrap_or_default": 980 + "dot_ok": 1529, + "let_underscore": 1410, + "unwrap_or_default": 938 }, "tracked_files": { "crates/jcode-app-core/src/agent.rs": { @@ -46,11 +46,6 @@ "let_underscore": 1, "unwrap_or_default": 0 }, - "crates/jcode-app-core/src/agent/tool_markup_recovery.rs": { - "dot_ok": 0, - "let_underscore": 0, - "unwrap_or_default": 1 - }, "crates/jcode-app-core/src/agent/tools.rs": { "dot_ok": 0, "let_underscore": 0, @@ -68,8 +63,8 @@ }, "crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs": { "dot_ok": 0, - "let_underscore": 64, - "unwrap_or_default": 3 + "let_underscore": 59, + "unwrap_or_default": 2 }, "crates/jcode-app-core/src/agent/utils.rs": { "dot_ok": 1, @@ -88,7 +83,7 @@ }, "crates/jcode-app-core/src/ambient/persistence.rs": { "dot_ok": 0, - "let_underscore": 6, + "let_underscore": 3, "unwrap_or_default": 1 }, "crates/jcode-app-core/src/ambient/runner.rs": { @@ -173,8 +168,8 @@ }, "crates/jcode-app-core/src/server/client_actions.rs": { "dot_ok": 2, - "let_underscore": 48, - "unwrap_or_default": 1 + "let_underscore": 49, + "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/client_comm_channels.rs": { "dot_ok": 0, @@ -198,8 +193,8 @@ }, "crates/jcode-app-core/src/server/client_lifecycle.rs": { "dot_ok": 0, - "let_underscore": 32, - "unwrap_or_default": 1 + "let_underscore": 31, + "unwrap_or_default": 2 }, "crates/jcode-app-core/src/server/client_lifecycle_logging.rs": { "dot_ok": 1, @@ -213,22 +208,22 @@ }, "crates/jcode-app-core/src/server/client_processing.rs": { "dot_ok": 0, - "let_underscore": 21, - "unwrap_or_default": 0 + "let_underscore": 19, + "unwrap_or_default": 2 }, "crates/jcode-app-core/src/server/client_session.rs": { - "dot_ok": 8, - "let_underscore": 16, + "dot_ok": 5, + "let_underscore": 18, "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/client_state.rs": { - "dot_ok": 2, - "let_underscore": 1, + "dot_ok": 4, + "let_underscore": 0, "unwrap_or_default": 4 }, "crates/jcode-app-core/src/server/comm_await.rs": { "dot_ok": 0, - "let_underscore": 9, + "let_underscore": 8, "unwrap_or_default": 5 }, "crates/jcode-app-core/src/server/comm_control.rs": { @@ -243,8 +238,8 @@ }, "crates/jcode-app-core/src/server/comm_session.rs": { "dot_ok": 4, - "let_underscore": 12, - "unwrap_or_default": 9 + "let_underscore": 10, + "unwrap_or_default": 10 }, "crates/jcode-app-core/src/server/comm_sync.rs": { "dot_ok": 0, @@ -303,8 +298,8 @@ }, "crates/jcode-app-core/src/server/jade_relay.rs": { "dot_ok": 2, - "let_underscore": 8, - "unwrap_or_default": 3 + "let_underscore": 9, + "unwrap_or_default": 2 }, "crates/jcode-app-core/src/server/lifecycle.rs": { "dot_ok": 8, @@ -317,9 +312,9 @@ "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/provider_control.rs": { - "dot_ok": 1, - "let_underscore": 29, - "unwrap_or_default": 2 + "dot_ok": 0, + "let_underscore": 32, + "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/reload.rs": { "dot_ok": 1, @@ -328,7 +323,7 @@ }, "crates/jcode-app-core/src/server/reload_recovery.rs": { "dot_ok": 3, - "let_underscore": 1, + "let_underscore": 0, "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/reload_state.rs": { @@ -347,8 +342,8 @@ "unwrap_or_default": 1 }, "crates/jcode-app-core/src/server/state.rs": { - "dot_ok": 3, - "let_underscore": 2, + "dot_ok": 2, + "let_underscore": 3, "unwrap_or_default": 1 }, "crates/jcode-app-core/src/server/swarm.rs": { @@ -682,9 +677,9 @@ "unwrap_or_default": 1 }, "crates/jcode-base/src/auth/account_pool.rs": { - "dot_ok": 2, + "dot_ok": 4, "let_underscore": 0, - "unwrap_or_default": 3 + "unwrap_or_default": 1 }, "crates/jcode-base/src/auth/account_store.rs": { "dot_ok": 1, @@ -763,8 +758,8 @@ }, "crates/jcode-base/src/auth/oauth.rs": { "dot_ok": 4, - "let_underscore": 1, - "unwrap_or_default": 3 + "let_underscore": 2, + "unwrap_or_default": 2 }, "crates/jcode-base/src/auth/oauth_callback_io.rs": { "dot_ok": 0, @@ -802,7 +797,7 @@ "unwrap_or_default": 1 }, "crates/jcode-base/src/background.rs": { - "dot_ok": 19, + "dot_ok": 18, "let_underscore": 18, "unwrap_or_default": 12 }, @@ -827,8 +822,8 @@ "unwrap_or_default": 0 }, "crates/jcode-base/src/bus.rs": { - "dot_ok": 2, - "let_underscore": 2, + "dot_ok": 3, + "let_underscore": 1, "unwrap_or_default": 0 }, "crates/jcode-base/src/claude_live.rs": { @@ -1016,6 +1011,11 @@ "let_underscore": 0, "unwrap_or_default": 0 }, + "crates/jcode-base/src/mobile_sync.rs": { + "dot_ok": 2, + "let_underscore": 0, + "unwrap_or_default": 0 + }, "crates/jcode-base/src/model_pricing.rs": { "dot_ok": 2, "let_underscore": 1, @@ -1088,7 +1088,7 @@ }, "crates/jcode-base/src/provider/models.rs": { "dot_ok": 3, - "let_underscore": 2, + "let_underscore": 0, "unwrap_or_default": 8 }, "crates/jcode-base/src/provider/openai.rs": { @@ -1136,21 +1136,11 @@ "let_underscore": 0, "unwrap_or_default": 0 }, - "crates/jcode-base/src/recent_session_index.rs": { - "dot_ok": 0, - "let_underscore": 2, - "unwrap_or_default": 0 - }, "crates/jcode-base/src/registry.rs": { "dot_ok": 3, "let_underscore": 0, "unwrap_or_default": 0 }, - "crates/jcode-base/src/runtime_activity.rs": { - "dot_ok": 2, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-base/src/runtime_memory_log.rs": { "dot_ok": 1, "let_underscore": 1, @@ -1262,7 +1252,7 @@ "unwrap_or_default": 0 }, "crates/jcode-base/src/todo.rs": { - "dot_ok": 3, + "dot_ok": 1, "let_underscore": 0, "unwrap_or_default": 3 }, @@ -1331,23 +1321,18 @@ "let_underscore": 0, "unwrap_or_default": 6 }, - "crates/jcode-base/src/usage/remote_accounts.rs": { - "dot_ok": 2, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-base/src/usage/startup_snapshot.rs": { "dot_ok": 11, "let_underscore": 0, "unwrap_or_default": 0 }, "crates/jcode-build-meta/build.rs": { - "dot_ok": 12, + "dot_ok": 9, "let_underscore": 0, "unwrap_or_default": 2 }, "crates/jcode-build-meta/src/lib.rs": { - "dot_ok": 5, + "dot_ok": 2, "let_underscore": 0, "unwrap_or_default": 0 }, @@ -1386,11 +1371,6 @@ "let_underscore": 1, "unwrap_or_default": 2 }, - "crates/jcode-config-types/src/lib.rs": { - "dot_ok": 0, - "let_underscore": 0, - "unwrap_or_default": 1 - }, "crates/jcode-config-types/src/serde_lenient.rs": { "dot_ok": 1, "let_underscore": 0, @@ -1427,9 +1407,9 @@ "unwrap_or_default": 3 }, "crates/jcode-harness-api-server/src/translate.rs": { - "dot_ok": 20, + "dot_ok": 24, "let_underscore": 5, - "unwrap_or_default": 46 + "unwrap_or_default": 23 }, "crates/jcode-harness-api-server/src/translate/session_files.rs": { "dot_ok": 4, @@ -1523,7 +1503,7 @@ }, "crates/jcode-provider-anthropic-runtime/src/lib.rs": { "dot_ok": 9, - "let_underscore": 15, + "let_underscore": 14, "unwrap_or_default": 1 }, "crates/jcode-provider-anthropic-runtime/src/native_web_search.rs": { @@ -1532,8 +1512,8 @@ "unwrap_or_default": 0 }, "crates/jcode-provider-antigravity-runtime/src/lib.rs": { - "dot_ok": 2, - "let_underscore": 21, + "dot_ok": 3, + "let_underscore": 20, "unwrap_or_default": 3 }, "crates/jcode-provider-antigravity/src/lib.rs": { @@ -1542,12 +1522,12 @@ "unwrap_or_default": 0 }, "crates/jcode-provider-bedrock/src/lib.rs": { - "dot_ok": 20, + "dot_ok": 18, "let_underscore": 9, "unwrap_or_default": 5 }, "crates/jcode-provider-copilot-runtime/src/lib.rs": { - "dot_ok": 5, + "dot_ok": 3, "let_underscore": 20, "unwrap_or_default": 2 }, @@ -1626,11 +1606,6 @@ "let_underscore": 0, "unwrap_or_default": 1 }, - "crates/jcode-provider-doctor/src/lifecycle_driver.rs": { - "dot_ok": 1, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-provider-doctor/src/live_provider_probes.rs": { "dot_ok": 2, "let_underscore": 0, @@ -1647,9 +1622,9 @@ "unwrap_or_default": 0 }, "crates/jcode-provider-gemini-runtime/src/lib.rs": { - "dot_ok": 4, + "dot_ok": 2, "let_underscore": 24, - "unwrap_or_default": 6 + "unwrap_or_default": 5 }, "crates/jcode-provider-gemini/src/lib.rs": { "dot_ok": 2, @@ -1667,7 +1642,7 @@ "unwrap_or_default": 8 }, "crates/jcode-provider-openai-runtime/src/lib.rs": { - "dot_ok": 4, + "dot_ok": 2, "let_underscore": 2, "unwrap_or_default": 4 }, @@ -1687,7 +1662,7 @@ "unwrap_or_default": 0 }, "crates/jcode-provider-openai-runtime/src/openai_stream_runtime.rs": { - "dot_ok": 3, + "dot_ok": 2, "let_underscore": 7, "unwrap_or_default": 1 }, @@ -1707,7 +1682,7 @@ "unwrap_or_default": 1 }, "crates/jcode-provider-openai/src/stream.rs": { - "dot_ok": 3, + "dot_ok": 2, "let_underscore": 0, "unwrap_or_default": 6 }, @@ -1946,11 +1921,6 @@ "let_underscore": 0, "unwrap_or_default": 0 }, - "crates/jcode-todo-policy/src/followup.rs": { - "dot_ok": 1, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-todo-policy/src/lib.rs": { "dot_ok": 0, "let_underscore": 0, @@ -1962,9 +1932,9 @@ "unwrap_or_default": 1 }, "crates/jcode-todo-policy/src/store.rs": { - "dot_ok": 6, - "let_underscore": 0, - "unwrap_or_default": 1 + "dot_ok": 0, + "let_underscore": 1, + "unwrap_or_default": 0 }, "crates/jcode-transport/src/unix.rs": { "dot_ok": 0, @@ -2072,9 +2042,9 @@ "unwrap_or_default": 2 }, "crates/jcode-tui-mermaid/src/mermaid_runtime.rs": { - "dot_ok": 22, + "dot_ok": 23, "let_underscore": 2, - "unwrap_or_default": 5 + "unwrap_or_default": 3 }, "crates/jcode-tui-mermaid/src/mermaid_svg.rs": { "dot_ok": 19, @@ -2178,19 +2148,14 @@ }, "crates/jcode-tui/src/tui/app/commands_cloud.rs": { "dot_ok": 0, - "let_underscore": 6, - "unwrap_or_default": 5 + "let_underscore": 5, + "unwrap_or_default": 6 }, "crates/jcode-tui/src/tui/app/commands_desktop.rs": { "dot_ok": 1, "let_underscore": 2, "unwrap_or_default": 0 }, - "crates/jcode-tui/src/tui/app/commands_dispatch.rs": { - "dot_ok": 0, - "let_underscore": 0, - "unwrap_or_default": 1 - }, "crates/jcode-tui/src/tui/app/commands_improve.rs": { "dot_ok": 0, "let_underscore": 2, @@ -2259,7 +2224,7 @@ "crates/jcode-tui/src/tui/app/helpers.rs": { "dot_ok": 17, "let_underscore": 4, - "unwrap_or_default": 6 + "unwrap_or_default": 4 }, "crates/jcode-tui/src/tui/app/helpers/clipboard_helper.rs": { "dot_ok": 0, @@ -2273,7 +2238,7 @@ }, "crates/jcode-tui/src/tui/app/inline_interactive.rs": { "dot_ok": 6, - "let_underscore": 12, + "let_underscore": 11, "unwrap_or_default": 4 }, "crates/jcode-tui/src/tui/app/inline_interactive/helpers.rs": { @@ -2288,8 +2253,8 @@ }, "crates/jcode-tui/src/tui/app/input.rs": { "dot_ok": 12, - "let_underscore": 2, - "unwrap_or_default": 5 + "let_underscore": 3, + "unwrap_or_default": 3 }, "crates/jcode-tui/src/tui/app/local.rs": { "dot_ok": 0, @@ -2328,7 +2293,7 @@ }, "crates/jcode-tui/src/tui/app/remote.rs": { "dot_ok": 0, - "let_underscore": 11, + "let_underscore": 9, "unwrap_or_default": 0 }, "crates/jcode-tui/src/tui/app/remote/input_dispatch.rs": { @@ -2354,7 +2319,7 @@ "crates/jcode-tui/src/tui/app/remote/server_events.rs": { "dot_ok": 7, "let_underscore": 1, - "unwrap_or_default": 4 + "unwrap_or_default": 3 }, "crates/jcode-tui/src/tui/app/remote/server_events/history_fingerprint.rs": { "dot_ok": 1, @@ -2371,11 +2336,6 @@ "let_underscore": 0, "unwrap_or_default": 1 }, - "crates/jcode-tui/src/tui/app/run_shell.rs": { - "dot_ok": 1, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-tui/src/tui/app/runtime_memory.rs": { "dot_ok": 0, "let_underscore": 1, @@ -2457,9 +2417,9 @@ "unwrap_or_default": 4 }, "crates/jcode-tui/src/tui/app/tui_state.rs": { - "dot_ok": 5, + "dot_ok": 7, "let_underscore": 0, - "unwrap_or_default": 9 + "unwrap_or_default": 7 }, "crates/jcode-tui/src/tui/app/turn.rs": { "dot_ok": 0, @@ -2561,11 +2521,6 @@ "let_underscore": 1, "unwrap_or_default": 1 }, - "crates/jcode-tui/src/tui/redraw_schedule.rs": { - "dot_ok": 1, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-tui/src/tui/remote_diff.rs": { "dot_ok": 1, "let_underscore": 0, @@ -2578,8 +2533,13 @@ }, "crates/jcode-tui/src/tui/session_picker.rs": { "dot_ok": 6, - "let_underscore": 5, - "unwrap_or_default": 6 + "let_underscore": 4, + "unwrap_or_default": 7 + }, + "crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs": { + "dot_ok": 0, + "let_underscore": 0, + "unwrap_or_default": 1 }, "crates/jcode-tui/src/tui/session_picker/filter.rs": { "dot_ok": 0, @@ -2587,9 +2547,14 @@ "unwrap_or_default": 4 }, "crates/jcode-tui/src/tui/session_picker/loading.rs": { - "dot_ok": 28, - "let_underscore": 1, - "unwrap_or_default": 18 + "dot_ok": 27, + "let_underscore": 4, + "unwrap_or_default": 19 + }, + "crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs": { + "dot_ok": 3, + "let_underscore": 0, + "unwrap_or_default": 0 }, "crates/jcode-tui/src/tui/session_picker/loading/pi_sessions.rs": { "dot_ok": 5, @@ -2601,6 +2566,11 @@ "let_underscore": 0, "unwrap_or_default": 1 }, + "crates/jcode-tui/src/tui/stable_status.rs": { + "dot_ok": 1, + "let_underscore": 0, + "unwrap_or_default": 0 + }, "crates/jcode-tui/src/tui/terminal_setup.rs": { "dot_ok": 0, "let_underscore": 0, @@ -2652,9 +2622,9 @@ "unwrap_or_default": 3 }, "crates/jcode-tui/src/tui/ui_header/account_router.rs": { - "dot_ok": 1, + "dot_ok": 2, "let_underscore": 1, - "unwrap_or_default": 2 + "unwrap_or_default": 1 }, "crates/jcode-tui/src/tui/ui_inline_image.rs": { "dot_ok": 6, @@ -2667,7 +2637,7 @@ "unwrap_or_default": 2 }, "crates/jcode-tui/src/tui/ui_input.rs": { - "dot_ok": 3, + "dot_ok": 2, "let_underscore": 0, "unwrap_or_default": 2 }, @@ -2707,9 +2677,9 @@ "unwrap_or_default": 3 }, "crates/jcode-tui/src/tui/ui_tools.rs": { - "dot_ok": 1, + "dot_ok": 0, "let_underscore": 0, - "unwrap_or_default": 12 + "unwrap_or_default": 13 }, "crates/jcode-tui/src/tui/ui_tools/batch.rs": { "dot_ok": 1, @@ -2731,11 +2701,6 @@ "let_underscore": 2, "unwrap_or_default": 1 }, - "crates/jcode-update-core/src/lib.rs": { - "dot_ok": 3, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-usage-types/src/lib.rs": { "dot_ok": 0, "let_underscore": 1, @@ -2762,14 +2727,19 @@ "unwrap_or_default": 0 }, "src/cli/cloud_move.rs": { - "dot_ok": 27, - "let_underscore": 18, - "unwrap_or_default": 8 + "dot_ok": 26, + "let_underscore": 14, + "unwrap_or_default": 7 + }, + "src/cli/cloud_move/transport.rs": { + "dot_ok": 1, + "let_underscore": 1, + "unwrap_or_default": 0 }, "src/cli/commands.rs": { "dot_ok": 13, - "let_underscore": 3, - "unwrap_or_default": 9 + "let_underscore": 2, + "unwrap_or_default": 5 }, "src/cli/commands/menubar.rs": { "dot_ok": 3, diff --git a/scripts/test_cloud_latency_report.py b/scripts/test_cloud_latency_report.py new file mode 100644 index 00000000000..9efe1cd6da0 --- /dev/null +++ b/scripts/test_cloud_latency_report.py @@ -0,0 +1,84 @@ +import contextlib +import importlib.util +import io +from pathlib import Path +import unittest + +SPEC = importlib.util.spec_from_file_location("cloud_latency_report", Path(__file__).with_name("cloud_latency_report.py")) +reporter = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(reporter) + + +class CloudLatencyReportTests(unittest.TestCase): + def test_window_is_inclusive_exclusive_and_handles_mixed_logs(self): + lines = [ + "[2026-11-07 23:59:59.999] [CLOUD_TIMING] stage=old duration_ms=9000", + "[2026-11-08 00:00:00.000] [CLOUD_TIMING] stage=new duration_ms=20", + "[2026-11-08 00:01:00.000] [CLOUD_TIMING] stage=later duration_ms=30", + "[CLOUD_TIMING] stage=undated duration_ms=9999", + "[2026-13-08 00:00:00] [CLOUD_TIMING] stage=invalid duration_ms=9999", + ] + since = reporter.local_timestamp("2026-11-08T00:00:00") + until = reporter.local_timestamp("2026-11-08T00:01:00") + self.assertEqual(list(reporter.in_window(lines, since, until)), [lines[1]]) + self.assertEqual(list(reporter.in_window(lines)), lines) + self.assertEqual(list(reporter.in_window(lines[:3], since=since)), lines[1:3]) + self.assertEqual(list(reporter.in_window(lines[:3], until=until)), lines[:2]) + + def test_window_rejects_timezone_ambiguity_and_invalid_dates(self): + import argparse + for value in ["invalid", "2026-11-08T00:00:00Z", "2026-11-08T00:00:00-08:00"]: + with self.assertRaises(argparse.ArgumentTypeError): + reporter.local_timestamp(value) + + def test_collects_failure_and_secondary_wait_metrics(self): + groups, failures = reporter.collect([ + "[INFO] [CLOUD_TIMING] stage=event_parse operation=history outcome=ok duration_ms=12.5 request_wait_ms=1400 bytes=261000\n", + "[INFO] [CLOUD_TIMING] stage=event_parse operation=history outcome=error duration_ms=2\n", + "[INFO] mobile_sync stage=http_upload outcome=timeout duration_ms=6000\n", + ]) + self.assertEqual(groups[("event_parse:history", "duration_ms")], [12.5, 2]) + self.assertEqual(groups[("event_parse:history", "request_wait_ms")], [1400]) + self.assertEqual(failures[("event_parse:history", "duration_ms")], 1) + self.assertEqual(failures[("http_upload", "duration_ms")], 1) + + def test_ignores_unrelated_invalid_and_nonfinite_values(self): + groups, _ = reporter.collect([ + "private stage=anything outcome=ok duration_ms=9000", + "[CLOUD_TIMING] stage=https://secret outcome=ok duration_ms=9", + "[CLOUD_TIMING] stage=parse outcome=ok duration_ms=nan", + "[CLOUD_TIMING] stage=parse outcome=ok duration_ms=inf", + "[CLOUD_TIMING] stage=parse outcome=ok request_wait_ms=none", + "[CLOUD_TIMING] stage=parse outcome=ok duration_ms=" + "9" * 400, + ]) + self.assertFalse(groups) + + def test_counts_http_body_and_projection_failures_not_normal_skips(self): + bad = ["http_error", "load_error", "read_error", "size_limit", "oversized", "identity_mismatch", "exit_failure", "rejected", "undelivered"] + normal = ["success", "miss", "skipped_absent", "skipped_disabled", "success_cache_unavailable", "no_devices"] + groups, failures = reporter.collect([ + f"[CLOUD_TIMING] stage=probe outcome={outcome} duration_ms=10" + for outcome in bad + normal + ]) + self.assertEqual(len(groups[("probe", "duration_ms")]), len(bad + normal)) + self.assertEqual(failures[("probe", "duration_ms")], len(bad)) + + def test_report_is_ranked_filtered_and_content_free(self): + groups, failures = reporter.collect([ + "[CLOUD_TIMING] stage=fast outcome=ok duration_ms=20 payload=PRIVATE", + "[CLOUD_TIMING] stage=slow outcome=error duration_ms=1000 payload=PRIVATE", + "[CLOUD_TIMING] stage=slower outcome=ok duration_ms=3000 token=SECRET", + ]) + output = io.StringIO() + with contextlib.redirect_stdout(output): + reporter.report(groups, failures, threshold_ms=500) + text = output.getvalue() + self.assertLess(text.index("slower"), text.index("slow ")) + self.assertNotIn("fast", text) + self.assertNotIn("PRIVATE", text) + self.assertNotIn("SECRET", text) + self.assertEqual(reporter.percentile([1, 2, 3, 4], .95), 4) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/test_dev_cargo_changelog.sh b/scripts/test_dev_cargo_changelog.sh index 8b14c770d31..0682244d491 100644 --- a/scripts/test_dev_cargo_changelog.sh +++ b/scripts/test_dev_cargo_changelog.sh @@ -3,17 +3,30 @@ set -euo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" # Load only the function under test, without starting a build. eval "$(awk '/^export_git_build_metadata\(\) \{/{copy=1} copy {print} copy && /^\}/{exit}' "$repo_root/scripts/dev_cargo.sh")" -unset JCODE_BUILD_CHANGELOG_RAW JCODE_BUILD_GIT_HASH +unset JCODE_BUILD_CHANGELOG_RAW JCODE_BUILD_GIT_HASH JCODE_BUILD_GIT_TAG export_git_build_metadata expected="$(git -C "$repo_root" log -700 --format='%h|%ct|%D|%s')" [[ -n "$JCODE_BUILD_CHANGELOG_RAW" ]] [[ "$JCODE_BUILD_CHANGELOG_RAW" == "$expected" ]] +base_version="$(awk '/^\[package\]/{package=1; next} /^\[/{package=0} package && /^version *=/{gsub(/"/, "", $3); print $3; exit}' "$repo_root/Cargo.toml")" +expected_tag="$(git -C "$repo_root" describe --tags --long --match "v${base_version}" HEAD)" +[[ "$JCODE_BUILD_GIT_TAG" == "$expected_tag" ]] +distance="$(git -C "$repo_root" rev-list --count "v${base_version}..HEAD")" +[[ "$JCODE_BUILD_GIT_TAG" == "v${base_version}-${distance}-g"* ]] # History is still populated when a caller already supplied the identity. unset JCODE_BUILD_CHANGELOG_RAW +unset JCODE_BUILD_GIT_TAG JCODE_BUILD_GIT_HASH=caller-hash export_git_build_metadata [[ "$JCODE_BUILD_CHANGELOG_RAW" == "$expected" ]] [[ "$JCODE_BUILD_GIT_HASH" == caller-hash ]] +[[ "$JCODE_BUILD_GIT_TAG" == "$expected_tag" ]] +JCODE_BUILD_GIT_TAG=caller-tag +export_git_build_metadata +[[ "$JCODE_BUILD_GIT_TAG" == caller-tag ]] +JCODE_BUILD_GIT_TAG='' +export_git_build_metadata +[[ -z "$JCODE_BUILD_GIT_TAG" ]] # Explicit caller history, including empty history, must not be overwritten. JCODE_BUILD_CHANGELOG_RAW=caller-history export_git_build_metadata diff --git a/scripts/test_preview_release.py b/scripts/test_preview_release.py index 68d8c31a53d..3b82d85c637 100644 --- a/scripts/test_preview_release.py +++ b/scripts/test_preview_release.py @@ -63,12 +63,12 @@ def head(self, repository, sha): if self.stale and sum(call[0] == "head" for call in self.calls) == 2: raise ValueError("master advanced") - def publish(self, ci_error=None): + def publish(self, ci_error=None, pinned=False): with (patch.object(preview, "gh", side_effect=self.fake_gh), patch.object(preview, "require_head", side_effect=self.head), patch.object(preview, "change_notes", return_value="## What changed\n\nTest change\n\n"), patch.object(preview, "authorize", side_effect=ci_error) as authorize): - preview.publish(preview.REPOSITORY, SHA, TAG, self.directory) + preview.publish(preview.REPOSITORY, SHA, TAG, self.directory, pinned=pinned) authorize.assert_called_once_with(preview.REPOSITORY, SHA, timeout=0, preview=True) def test_complete_assets_publish_only_as_nonlatest_prerelease(self): @@ -186,6 +186,110 @@ def test_head_mismatch_fails_closed(self): preview.require_head(preview.REPOSITORY, SHA) +class PinnedPreviewTests(PreviewPublicationTests): + def setUp(self): + super().setUp() + self.heads = ["b" * 40, "c" * 40] + self.bases = [SHA, SHA] + environment = patch.dict(os.environ, {"GITHUB_EVENT_NAME": "workflow_dispatch", + "GITHUB_REF": "refs/heads/master", + "GITHUB_SHA": SHA}) + environment.start() + self.addCleanup(environment.stop) + + def fake_gh(self, *args): + if args == ("api", f"repos/{preview.REPOSITORY}/git/ref/heads/master"): + self.calls.append(args) + return json.dumps({"object": {"sha": self.heads.pop(0)}}) + if args[0] == "api" and "/compare/" in args[1]: + self.calls.append(args) + return json.dumps({"merge_base_commit": {"sha": self.bases.pop(0)}}) + return super().fake_gh(*args) + + def test_pinned_ancestral_snapshot_publishes_and_rechecks_latest_master(self): + self.publish(pinned=True) + comparisons = [call for call in self.calls if call[0] == "api" and "/compare/" in call[1]] + self.assertEqual(comparisons, [ + ("api", f"repos/{preview.REPOSITORY}/compare/{SHA}...{'b' * 40}"), + ("api", f"repos/{preview.REPOSITORY}/compare/{SHA}...{'c' * 40}")]) + self.assertEqual(self.calls[-2], comparisons[-1]) + self.assertEqual(self.calls[-1][:2], ("release", "edit")) + self.assertFalse(any(call[0] == "head" for call in self.calls)) + + def test_pinned_identical_master_needs_no_comparison(self): + self.heads = [SHA, SHA] + self.publish(pinned=True) + self.assertFalse(any("/compare/" in call[1] for call in self.calls if call[0] == "api")) + + def test_pinned_invalid_event_ref_or_sha_never_writes(self): + for key, value in (("GITHUB_EVENT_NAME", "push"), + ("GITHUB_EVENT_NAME", "pull_request"), + ("GITHUB_EVENT_NAME", ""), + ("GITHUB_REF", "refs/heads/feature"), + ("GITHUB_REF", "refs/tags/v1.0.0"), + ("GITHUB_SHA", "b" * 40)): + with self.subTest(key=key, value=value), patch.dict(os.environ, {key: value}): + with self.assertRaisesRegex(ValueError, "manual master dispatch"): + self.publish(pinned=True) + self.assertFalse(any("POST" in call or call[0] == "release" for call in self.calls)) + + def test_pinned_nonancestor_never_creates_tag(self): + self.bases = ["d" * 40] + with self.assertRaisesRegex(ValueError, "not an ancestor"): + self.publish(pinned=True) + self.assertFalse(any("POST" in call or call[0] == "release" for call in self.calls)) + + def test_pinned_lost_ancestry_during_staging_never_publishes(self): + self.bases = [SHA, "d" * 40] + with self.assertRaisesRegex(ValueError, "not an ancestor"): + self.publish(pinned=True) + self.assertTrue(any(call[:2] == ("release", "create") for call in self.calls)) + self.assertFalse(any(call[:2] == ("release", "edit") for call in self.calls)) + + def test_pinned_malformed_comparison_fails_closed(self): + for response in ({}, {"merge_base_commit": {}}, {"merge_base_commit": {"sha": ""}}): + with self.subTest(response=response), patch.object( + preview, "api", side_effect=[{"object": {"sha": "b" * 40}}, response]): + with self.assertRaisesRegex(ValueError, "not an ancestor"): + preview.require_snapshot(preview.REPOSITORY, SHA, pinned=True) + + def test_pinned_keeps_asset_tag_and_stable_window_gates(self): + for mode in ("incomplete", "changed_tag", "existing", "stable"): + with self.subTest(mode=mode): + self.heads = ["b" * 40, "c" * 40] + self.bases = [SHA, SHA] + self.calls = [] + for attribute in ("incomplete", "changed_tag", "existing"): + setattr(self, attribute, attribute == mode) + with patch.object(preview, "require_preview_window", + side_effect=ValueError("stable window ended") if mode == "stable" else None): + with self.assertRaises(ValueError): + self.publish(pinned=True) + self.assertFalse(any(call[:2] == ("release", "edit") for call in self.calls)) + checksum = self.directory / "SHA256SUMS" + if checksum.exists(): + checksum.unlink() + + def test_pinned_failed_exact_sha_ci_never_creates_tag(self): + with self.assertRaisesRegex(RuntimeError, "failed CI"): + self.publish(RuntimeError("failed CI"), pinned=True) + self.assertEqual(self.calls, []) + + def test_cli_forwards_pinned_to_both_actions(self): + for action in ("check-head", "publish"): + with (self.subTest(action=action), + patch.object(sys, "argv", ["preview_release.py", action, "--repo", preview.REPOSITORY, + "--sha", SHA, "--tag", TAG, "--pinned"]), + patch.object(preview, "require_preview_window"), + patch.object(preview, "require_snapshot") as snapshot, + patch.object(preview, "publish") as publish): + preview.main() + if action == "check-head": + snapshot.assert_called_once_with(preview.REPOSITORY, SHA, True) + else: + publish.assert_called_once_with(preview.REPOSITORY, SHA, TAG, "dist", pinned=True) + + class PreviewChangeNotesTests(unittest.TestCase): def notes(self, releases, pages): with patch.object(preview, "gh", side_effect=[json.dumps(releases), json.dumps(pages)]) as gh: @@ -343,13 +447,22 @@ def test_workflow_is_opt_in_debounced_exact_sha_and_nonrecursive(self): triggers = workflow.split("on:\n", 1)[1].split("concurrency:", 1)[0] self.assertIn("branches: [master]", triggers) self.assertIn(" workflow_dispatch:\n", triggers) - self.assertNotIn("inputs:", triggers) + self.assertIn(" inputs:\n expedite:", triggers) + self.assertIn(" type: boolean\n default: false", triggers) self.assertIn("github.ref == 'refs/heads/master'", workflow) self.assertNotIn("tags:", triggers) self.assertNotIn("workflow_run:", triggers) self.assertNotIn("release:", triggers) self.assertIn("cancel-in-progress: ${{ github.event_name == 'push' }}", workflow) - self.assertIn("group: v1-preview-master", workflow) + self.assertIn("group: v1-preview-${{ github.event_name == 'workflow_dispatch' && inputs.expedite && 'pinned' || 'master' }}", workflow) + self.assertIn("JCODE_PREVIEW_PINNED: ${{ github.event_name == 'workflow_dispatch' && inputs.expedite && 'true' || 'false' }}", workflow) + self.assertIn("if: env.JCODE_PREVIEW_PINNED != 'true'\n run: sleep 900", workflow) + self.assertEqual(workflow.count('if [ "$JCODE_PREVIEW_PINNED" = true ]; then set -- --pinned; fi'), 2) + for action in ("check-head", "publish"): + command = next(line for line in workflow.splitlines() + if f"preview_release.py {action}" in line) + self.assertIn('"$@"', command) + self.assertIn('--sha "$GITHUB_SHA"', command) self.assertIn("vars.JCODE_V1_PREVIEW_ENABLED != 'false'", workflow) self.assertLess(workflow.index("run: sleep 900"), workflow.index("check_release_ci.py")) self.assertIn('check_release_ci.py "$GITHUB_SHA"', workflow) @@ -479,6 +592,15 @@ def test_ci_runs_preview_tests(self): self.assertIn('python3 scripts/test_preview_release.py', (ROOT / '.github/workflows/ci.yml').read_text()) + def test_main_push_ci_is_sha_isolated_but_pr_checks_remain_latest(self): + workflow = (ROOT / '.github/workflows/ci.yml').read_text() + concurrency = workflow.split('concurrency:\n', 1)[1].split('\nenv:', 1)[0] + self.assertIn("group: ci-${{ github.workflow }}-${{ github.ref }}-", concurrency) + self.assertIn("github.event_name == 'push' && " + "(github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main') " + "&& github.sha || 'latest'", concurrency) + self.assertIn('cancel-in-progress: true', concurrency) + if __name__ == "__main__": unittest.main() diff --git a/scripts/test_size_budget.json b/scripts/test_size_budget.json index 601e8f6bc33..8fc129c283e 100644 --- a/scripts/test_size_budget.json +++ b/scripts/test_size_budget.json @@ -1,55 +1,55 @@ { "threshold_loc": 1200, "tracked_files": { - "crates/jcode-app-core/src/agent_tests.rs": 3253, - "crates/jcode-app-core/src/server/client_lifecycle_tests.rs": 1939, - "crates/jcode-app-core/src/server/comm_session_tests.rs": 1332, - "crates/jcode-app-core/src/server/provider_control_tests.rs": 1352, - "crates/jcode-app-core/src/tool/bash_tests.rs": 1259, - "crates/jcode-app-core/src/tool/communicate_tests.rs": 1481, - "crates/jcode-app-core/src/tool/selfdev/tests.rs": 1460, + "crates/jcode-app-core/src/agent_tests.rs": 3195, + "crates/jcode-app-core/src/server/client_lifecycle_tests.rs": 1658, + "crates/jcode-app-core/src/server/comm_session_tests.rs": 1306, + "crates/jcode-app-core/src/server/provider_control_tests.rs": 1310, + "crates/jcode-app-core/src/tool/bash_tests.rs": 1245, + "crates/jcode-app-core/src/tool/browser_fast_tests.rs": 1286, + "crates/jcode-app-core/src/tool/selfdev/tests.rs": 1439, "crates/jcode-app-core/src/tool/tests.rs": 1976, - "crates/jcode-base/src/compaction_tests.rs": 1271, - "crates/jcode-base/src/config_tests.rs": 1795, + "crates/jcode-base/src/auth/tests.rs": 1206, + "crates/jcode-base/src/compaction_tests.rs": 1269, + "crates/jcode-base/src/config_tests.rs": 1760, "crates/jcode-base/src/live_tests.rs": 3087, - "crates/jcode-base/src/provider/tests.rs": 1335, + "crates/jcode-base/src/provider/tests.rs": 1276, "crates/jcode-base/src/provider/tests/model_resolution.rs": 2248, "crates/jcode-base/src/provider_catalog_tests.rs": 1409, - "crates/jcode-base/src/session_tests/cases.rs": 3267, - "crates/jcode-base/src/usage/tests.rs": 1317, - "crates/jcode-harness-api-server/src/translate_tests.rs": 4199, - "crates/jcode-provider-anthropic-runtime/src/anthropic_tests.rs": 2029, - "crates/jcode-provider-openrouter-runtime/src/openrouter_tests.rs": 3568, - "crates/jcode-sdk/tests/client_behavior.rs": 1432, + "crates/jcode-base/src/session_tests/cases.rs": 3298, + "crates/jcode-base/src/usage/tests.rs": 1289, + "crates/jcode-harness-api-server/src/translate_tests.rs": 4073, + "crates/jcode-provider-anthropic-runtime/src/anthropic_tests.rs": 2072, + "crates/jcode-provider-openrouter-runtime/src/openrouter_tests.rs": 3544, + "crates/jcode-sdk/tests/client_behavior.rs": 1244, "crates/jcode-telemetry-core/src/tests.rs": 1836, - "crates/jcode-tui/src/tui/app/remote_tests.rs": 1441, - "crates/jcode-tui/src/tui/app/tests.rs": 2030, - "crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs": 1604, - "crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01.rs": 1326, + "crates/jcode-tui/src/tui/app/commands_cloud_tests.rs": 1315, + "crates/jcode-tui/src/tui/app/remote_tests.rs": 1329, + "crates/jcode-tui/src/tui/app/tests.rs": 1943, + "crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs": 1589, + "crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01.rs": 1245, "crates/jcode-tui/src/tui/app/tests/onboarding_eval.rs": 3305, "crates/jcode-tui/src/tui/app/tests/onboarding_flow.rs": 1672, - "crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs": 1712, - "crates/jcode-tui/src/tui/app/tests/remote_events_reload_04.rs": 2676, - "crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs": 1251, - "crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs": 2413, + "crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs": 1700, + "crates/jcode-tui/src/tui/app/tests/remote_events_reload_04.rs": 2622, + "crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs": 2334, "crates/jcode-tui/src/tui/app/tests/remote_startup_input_03/part_01.rs": 1340, - "crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_01.rs": 1583, + "crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_01.rs": 1404, "crates/jcode-tui/src/tui/app/tests/scroll_copy_02/part_01.rs": 1580, - "crates/jcode-tui/src/tui/app/tests/scroll_copy_02/part_02.rs": 1726, + "crates/jcode-tui/src/tui/app/tests/scroll_copy_02/part_02.rs": 1683, "crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs": 1716, - "crates/jcode-tui/src/tui/app/tests/state_model_poke_01/part_01.rs": 1382, - "crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs": 1683, - "crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs": 3121, - "crates/jcode-tui/src/tui/app/tests/swarm_plan_graph_inline.rs": 1883, - "crates/jcode-tui/src/tui/info_widget_tests.rs": 1961, - "crates/jcode-tui/src/tui/session_picker/loading_tests.rs": 1568, - "crates/jcode-tui/src/tui/session_picker_tests.rs": 2806, - "crates/jcode-tui/src/tui/ui_messages/tests.rs": 3653, - "crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs": 1225, - "crates/jcode-tui/src/tui/ui_tests/prepare.rs": 1374, - "crates/jcode-tui/src/tui/ui_tests/tools.rs": 1530, - "src/cli/commands_tests.rs": 1805, - "tests/e2e/test_support/mod.rs": 1417 + "crates/jcode-tui/src/tui/app/tests/state_model_poke_01/part_01.rs": 1379, + "crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs": 1564, + "crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs": 3119, + "crates/jcode-tui/src/tui/app/tests/swarm_plan_graph_inline.rs": 1857, + "crates/jcode-tui/src/tui/info_widget_tests.rs": 1938, + "crates/jcode-tui/src/tui/session_picker/loading_tests.rs": 1596, + "crates/jcode-tui/src/tui/session_picker_tests.rs": 2655, + "crates/jcode-tui/src/tui/ui_messages/tests.rs": 3737, + "crates/jcode-tui/src/tui/ui_tests/prepare.rs": 1344, + "crates/jcode-tui/src/tui/ui_tests/tools.rs": 1476, + "src/cli/args/tests.rs": 1225, + "src/cli/commands_tests.rs": 1764 }, "version": 1 } diff --git a/scripts/waybar/jcode-sessions-status b/scripts/waybar/jcode-sessions-status index 9a50ab33abe..9426e76a856 100755 --- a/scripts/waybar/jcode-sessions-status +++ b/scripts/waybar/jcode-sessions-status @@ -5,13 +5,15 @@ Subscribes to all jcode debug sockets for status_change and member_change events. When an event fires, re-queries root-session and live swarm-agent counts and prints updated JSON to stdout for waybar to pick up. -Falls back to periodic refresh every 10s for socket discovery and -as a safety net. +Watches socket directories with inotify for immediate server discovery. +Periodic local refresh is only a fallback when directory watching fails. """ from __future__ import annotations import glob +import ctypes +import errno import json import os import select @@ -21,6 +23,7 @@ import time import threading import subprocess import shlex +import struct from pathlib import Path SOCKET_TIMEOUT = 2 @@ -28,6 +31,83 @@ REFRESH_INTERVAL = 10 CLOUD_REFRESH_INTERVAL = 30 +class SocketDirectoryWatcher(threading.Thread): + """Watch before initial discovery so bind/unlink cannot fall into a gap.""" + + def __init__(self, on_update, directories=None): + super().__init__(daemon=True) + self.on_update = on_update + self.directories = list(dict.fromkeys(directories or [ + os.environ.get('XDG_RUNTIME_DIR') or f'/run/user/{os.getuid()}', '/tmp'])) + self.libc = ctypes.CDLL(None, use_errno=True) + self.libc.inotify_init1.argtypes = [ctypes.c_int] + self.libc.inotify_add_watch.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_uint32] + self.fd = self.libc.inotify_init1(os.O_CLOEXEC | os.O_NONBLOCK) + if self.fd < 0: + raise OSError(ctypes.get_errno(), 'inotify_init1') + try: + # CREATE, DELETE, MOVED_FROM/TO, DELETE_SELF, MOVE_SELF. + for directory in self.directories: + if self.libc.inotify_add_watch(self.fd, os.fsencode(directory), 0xfc0) < 0: + raise OSError(ctypes.get_errno(), 'inotify_add_watch') + except BaseException: + os.close(self.fd) + raise + self.failed = False + self.stopped = threading.Event() + self.wake_read, self.wake_write = socket.socketpair() + + def stop(self): + self.stopped.set() + try: + self.wake_write.send(b'x') + except OSError: + pass + + @staticmethod + def relevant(data): + offset = 0 + relevant = False + while offset + 16 <= len(data): + _, mask, _, size = struct.unpack_from('iIII', data, offset) + name = data[offset + 16:offset + 16 + size].split(b'\0', 1)[0] + # Overflow and lost watches require a rescan/re-establishment. + if mask & 0xec00 or (name.startswith(b'jcode') and name.endswith(b'-debug.sock')): + relevant = True + offset += 16 + size + return relevant + + def run(self): + try: + while not self.stopped.is_set(): + readable, _, _ = select.select([self.fd, self.wake_read], [], []) + if self.wake_read in readable: + break + for _ in range(16): + if self.stopped.is_set(): + break + try: + data = os.read(self.fd, 65536) + except BlockingIOError: + break + if self.relevant(data): + self.on_update() + # A watched root disappeared. Fall back until main rebuilds. + off = 0 + while off + 16 <= len(data): + _, mask, _, size = struct.unpack_from('iIII', data, off) + if mask & 0xac00: + raise OSError(errno.ENOENT, 'socket directory watch lost') + off += 16 + size + except OSError: + self.failed = True + self.on_update() + finally: + os.close(self.fd) + self.wake_read.close() + self.wake_write.close() + + def _pid_is_live(path: Path) -> bool: try: pid = int(path.read_text().strip()) @@ -232,16 +312,29 @@ def _emit(data: dict): class SocketSubscriber(threading.Thread): """Subscribe to a debug socket's event stream in a background thread.""" - def __init__(self, sock_path: str, on_event): + def __init__(self, sock_path: str, on_event, retry_delay=1): super().__init__(daemon=True) self.sock_path = sock_path self.on_event = on_event self.alive = True + self.connection = None + self.lock = threading.Lock() + self.ready = threading.Event() + self.finished = threading.Event() + self.retry_delay = retry_delay + self.retry_at = float('inf') + self.connected_at = None + self.identity = None def run(self): try: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - s.settimeout(None) + with self.lock: + if not self.alive: + s.close() + return + self.connection = s + s.settimeout(SOCKET_TIMEOUT) s.connect(self.sock_path) req = { "type": "debug_command", @@ -249,14 +342,9 @@ class SocketSubscriber(threading.Thread): "command": "events:subscribe:status_change,member_change", } s.sendall((json.dumps(req) + "\n").encode()) - buf = b"" while self.alive: - s.settimeout(30) - try: - chunk = s.recv(4096) - except socket.timeout: - continue + chunk = s.recv(4096) if not chunk: break buf += chunk @@ -264,23 +352,50 @@ class SocketSubscriber(threading.Thread): line, buf = buf.split(b"\n", 1) try: data = json.loads(line.decode("utf-8", errors="replace")) - if data.get("type") == "event": + if data.get("ok") and not self.ready.is_set(): + self.connected_at = time.monotonic() + self.ready.set() + s.settimeout(None) + self.on_event(self.sock_path, {"type": "subscribed"}) + elif data.get("type") in ("event", "lag"): self.on_event(self.sock_path, data) except Exception: pass - s.close() except Exception: pass - self.alive = False + finally: + self.retry_at = time.monotonic() + self.retry_delay + self.alive = False + with self.lock: + if self.connection is not None: + self.connection.close() + self.connection = None + self.finished.set() + self.on_event(self.sock_path, {"type": "disconnected"}) def stop(self): self.alive = False + with self.lock: + if self.connection is not None: + try: + self.connection.shutdown(socket.SHUT_RDWR) + except OSError: + pass def main(): last_output: str | None = None subscribers: dict[str, SocketSubscriber] = {} event_flag = threading.Event() + def start_watcher(): + try: + watcher = SocketDirectoryWatcher(event_flag.set) + watcher.start() + return watcher + except (OSError, AttributeError): + return None + + watcher = start_watcher() cloud = CloudPoller(event_flag.set) cloud.start() @@ -291,36 +406,76 @@ def main(): nonlocal last_output sockets = _find_debug_sockets() - streaming, total, swarm_agents = _collect_counts(sockets) - out = _build_output(streaming, total, swarm_agents, cloud.counts) - - out_str = json.dumps(out) - if out_str != last_output: - _emit(out) - last_output = out_str - - dead = [p for p in subscribers if p not in sockets] + identities = {} + for path in sockets: + try: + stat = os.stat(path) + identities[path] = (stat.st_dev, stat.st_ino) + except OSError: + pass + sockets = list(identities) + + dead = [p for p, sub in subscribers.items() + if p not in identities or sub.identity != identities[p]] for p in dead: subscribers[p].stop() del subscribers[p] for sock_path in sockets: - if sock_path not in subscribers or not subscribers[sock_path].alive: - sub = SocketSubscriber(sock_path, on_event) + previous = subscribers.get(sock_path) + if previous is None or (not previous.alive and time.monotonic() >= previous.retry_at): + delay = 1 + if previous is not None: + stable = (previous.connected_at is not None and + previous.retry_at - previous.retry_delay - previous.connected_at + >= REFRESH_INTERVAL) + delay = 1 if stable else min(30, previous.retry_delay * 2) + sub = SocketSubscriber(sock_path, on_event, delay) + sub.identity = identities[sock_path] subscribers[sock_path] = sub sub.start() - refresh() + # Establish the event stream before taking the snapshot. All new + # subscribers run concurrently, with one bounded handshake budget. + deadline = time.monotonic() + SOCKET_TIMEOUT + for sub in subscribers.values(): + while sub.alive and not sub.ready.is_set(): + remaining = deadline - time.monotonic() + if remaining <= 0: + break + sub.finished.wait(min(0.01, remaining)) + streaming, total, swarm_agents = _collect_counts([ + p for p, sub in subscribers.items() if sub.alive and sub.ready.is_set()]) + out = _build_output(streaming, total, swarm_agents, cloud.counts) + out_str = json.dumps(out) + if out_str != last_output: + _emit(out) + last_output = out_str - while True: - triggered = event_flag.wait(timeout=REFRESH_INTERVAL) - if triggered: - event_flag.clear() - time.sleep(0.05) - while event_flag.is_set(): + try: + refresh() + fallback_at = time.monotonic() + REFRESH_INTERVAL + while True: + now = time.monotonic() + deadlines = [sub.retry_at for sub in subscribers.values() if not sub.alive] + if watcher is None or watcher.failed: + deadlines.append(fallback_at) + timeout = max(0, min(deadlines) - now) if deadlines else None + triggered = event_flag.wait(timeout=timeout) + if triggered: event_flag.clear() + # Fixed window, not a sliding debounce: continuous events cannot + # postpone the next snapshot indefinitely. Preserve later wakes. time.sleep(0.05) - refresh() + if (watcher is None or watcher.failed) and time.monotonic() >= fallback_at: + watcher = start_watcher() + fallback_at = time.monotonic() + REFRESH_INTERVAL + refresh() + finally: + for sub in subscribers.values(): + sub.stop() + if watcher is not None: + watcher.stop() if __name__ == "__main__": diff --git a/scripts/waybar/test_jcode_socket_watcher.py b/scripts/waybar/test_jcode_socket_watcher.py new file mode 100644 index 00000000000..f1ec005015e --- /dev/null +++ b/scripts/waybar/test_jcode_socket_watcher.py @@ -0,0 +1,424 @@ +#!/usr/bin/env python3 +"""Run with python3 test_jcode_socket_watcher.py. Uses isolated fake sockets, no cloud calls.""" +import glob +import importlib.machinery +import importlib.util +import json +import os +from pathlib import Path +import queue +import socket +import struct +import subprocess +import sys +import tempfile +import threading +import time +import unittest +from unittest import mock + +MODULE = Path(__file__).with_name('jcode-sessions-status') + + +def load_module(): + loader = importlib.machinery.SourceFileLoader('sessions_status', str(MODULE)) + spec = importlib.util.spec_from_loader(loader.name, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + + +m = load_module() + + +class FakeServer: + def __init__(self, directory, reject=False, ack_delay=0): + self.path = str(Path(directory) / 'jcode-test-debug.sock') + self.listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.listener.bind(self.path) + self.listener.listen() + self.listener.settimeout(.1) + self.stopped = threading.Event() + self.subscribed = threading.Event() + self.clients = [] + self.streams = [] + self.threads = [] + self.attempts = [] + self.commands = [] + self.premature_queries = [] + self.reject = reject + self.ack_delay = ack_delay + self.processing = False + self.change_during_query = False + self.thread = threading.Thread(target=self.accept, daemon=True) + self.thread.start() + + def accept(self): + while not self.stopped.is_set(): + try: + conn, _ = self.listener.accept() + except socket.timeout: + continue + except OSError: + return + self.clients.append(conn) + thread = threading.Thread(target=self.handle, args=(conn,), daemon=True) + self.threads.append(thread) + thread.start() + + def handle(self, conn): + try: + with conn.makefile('rb') as reader: + request = json.loads(reader.readline()) + command = request['command'] + self.commands.append(command) + if command.startswith('events:subscribe:'): + self.attempts.append(time.monotonic()) + if self.reject: + return + time.sleep(self.ack_delay) + self.streams.append(conn) + self.subscribed.set() + conn.sendall(b'{"type":"debug_response","ok":true,"output":"{}"}\n') + while not self.stopped.is_set(): + if not conn.recv(1): + break + else: + if not self.subscribed.is_set(): + self.premature_queries.append(command) + output = ([{'session_id': 'fake-root', 'is_processing': self.processing}] + if command == 'sessions' else + {'id': 'fake-server', 'spawned_swarm_agent_count': 2}) + if command == 'sessions' and self.change_during_query: + self.change_during_query = False + self.processing = True + self.event() + conn.sendall((json.dumps({'ok': True, 'output': json.dumps(output)}) + '\n').encode()) + except (OSError, ValueError): + pass + finally: + conn.close() + + def event(self, kind='event'): + for conn in list(self.streams): + try: + conn.sendall((json.dumps({'type': kind}) + '\n').encode()) + except OSError: + pass + + def close(self): + self.stopped.set() + self.listener.close() + for conn in self.clients: + try: + conn.shutdown(socket.SHUT_RDWR) + except OSError: + pass + self.thread.join(1) + for thread in self.threads: + thread.join(1) + try: + os.unlink(self.path) + except FileNotFoundError: + pass + + +class ModuleProcess: + def __init__(self, directory, fallback=False): + self.outputs = queue.Queue() + self.process = subprocess.Popen( + [sys.executable, __file__, '--runner', directory, str(int(fallback))], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + self.reader = threading.Thread(target=self.read, daemon=True) + self.reader.start() + + def read(self): + for line in self.process.stdout: + self.outputs.put((time.monotonic(), json.loads(line))) + + def output(self, fragment, timeout=2): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + timestamp, output = self.outputs.get(timeout=max(.01, deadline - time.monotonic())) + except queue.Empty: + break + if fragment in output['tooltip']: + return timestamp, output + raise AssertionError(f'no output containing {fragment!r}, process={self.process.poll()}') + + def close(self): + self.process.terminate() + self.process.wait(timeout=3) + self.reader.join(1) + stderr = self.process.stderr.read() + self.process.stdout.close() + self.process.stderr.close() + if stderr: + raise AssertionError(stderr) + + +class WatcherTests(unittest.TestCase): + def test_event_masks_and_unrelated_names(self): + def event(mask, name=b''): + return struct.pack('iIII', 1, mask, 0, len(name)) + name + for mask in (0x100, 0x200, 0x40, 0x80): + self.assertTrue(m.SocketDirectoryWatcher.relevant(event(mask, b'jcode-x-debug.sock\0'))) + self.assertFalse(m.SocketDirectoryWatcher.relevant(event(mask, b'unrelated\0'))) + for mask in (0x4000, 0x8000, 0x400, 0x800, 0x2000): + self.assertTrue(m.SocketDirectoryWatcher.relevant(event(mask))) + self.assertTrue(m.SocketDirectoryWatcher.relevant(event(0x100, b'other') + event(0x4000))) + + def test_create_rename_delete_and_stop(self): + with tempfile.TemporaryDirectory() as directory: + updates = queue.Queue() + watcher = m.SocketDirectoryWatcher(lambda: updates.put(True), [directory]) + watcher.start() + try: + path = Path(directory) / 'jcode-one-debug.sock' + path.touch() + updates.get(timeout=1) + path.rename(Path(directory) / 'unrelated') + updates.get(timeout=1) + (Path(directory) / 'unrelated').rename(path) + updates.get(timeout=1) + path.unlink() + updates.get(timeout=1) + finally: + watcher.stop() + watcher.join(1) + self.assertFalse(watcher.is_alive()) + self.assertFalse(watcher.failed) + + def test_watch_invalidated_by_directory_move_and_delete(self): + for action in ('move', 'delete'): + with self.subTest(action=action), tempfile.TemporaryDirectory() as root: + path = Path(root) / 'watched' + path.mkdir() + updated = threading.Event() + watcher = m.SocketDirectoryWatcher(updated.set, [str(path)]) + watcher.start() + if action == 'move': + path.rename(Path(root) / 'moved') + else: + path.rmdir() + watcher.join(1) + self.assertFalse(watcher.is_alive()) + self.assertTrue(watcher.failed) + self.assertTrue(updated.is_set()) + + def test_overflow_wakes_rescan_without_losing_watch(self): + with tempfile.TemporaryDirectory() as directory: + updated = threading.Event() + watcher = m.SocketDirectoryWatcher(updated.set, [directory]) + overflow = struct.pack('iIII', -1, 0x4000, 0, 0) + # Inject a kernel overflow record into the actual run-loop parser. + def notify(): + updated.set() + watcher.stop() + watcher.on_update = notify + with mock.patch.object(m.select, 'select', return_value=([watcher.fd], [], [])), \ + mock.patch.object(m.os, 'read', side_effect=[overflow, BlockingIOError()]): + watcher.run() + self.assertTrue(updated.is_set()) + self.assertFalse(watcher.failed) + + def test_subscriber_stop_before_and_during_receive(self): + sub = m.SocketSubscriber('/not/a/socket', lambda *_: None) + sub.stop() + sub.start() + sub.join(1) + self.assertFalse(sub.is_alive()) + with tempfile.TemporaryDirectory() as directory: + server = FakeServer(directory) + try: + sub = m.SocketSubscriber(server.path, lambda *_: None) + sub.start() + self.assertTrue(sub.ready.wait(1)) + sub.stop() + sub.join(1) + self.assertFalse(sub.is_alive()) + self.assertIsNone(sub.connection) + finally: + server.close() + + +class IntegrationTests(unittest.TestCase): + def test_event_during_snapshot_is_not_lost(self): + with tempfile.TemporaryDirectory() as directory: + server = FakeServer(directory) + process = ModuleProcess(directory) + try: + process.output('0 streaming, 1 root sessions') + time.sleep(.15) # Let the initial subscription wake settle. + server.change_during_query = True + server.event() + process.output('1 streaming, 1 root sessions', timeout=1) + finally: + process.close() + server.close() + + def test_healthy_watcher_has_no_periodic_local_scan(self): + with tempfile.TemporaryDirectory() as directory: + process = ModuleProcess(directory) + try: + process.output('0 root sessions') + log = Path(directory) / 'discovery.log' + before = log.read_text() + time.sleep(.65) # More than three shortened fallback intervals. + self.assertEqual(before, log.read_text()) + finally: + process.close() + + def test_socket_replaced_at_same_path_replaces_subscription(self): + with tempfile.TemporaryDirectory() as directory: + old = FakeServer(directory) + process = ModuleProcess(directory) + new = None + try: + process.output('1 root sessions') + os.unlink(old.path) + new = FakeServer(directory) + new.processing = True + process.output('1 streaming, 1 root sessions') + self.assertTrue(new.subscribed.is_set()) + new.processing = False + new.event() + process.output('0 streaming, 1 root sessions') + finally: + process.close() + if new: + new.close() + old.close() + + def test_create_delete_immediate_and_subscribe_before_query(self): + with tempfile.TemporaryDirectory() as directory: + process = ModuleProcess(directory) + server = None + try: + process.output('0 root sessions') + start = time.monotonic() + server = FakeServer(directory, ack_delay=.15) + created, _ = process.output('1 root sessions') + create_latency = created - start + self.assertLess(created - start, 1) + self.assertFalse(server.premature_queries) + self.assertTrue(server.commands[0].startswith('events:subscribe:')) + server.processing = True + start = time.monotonic() + server.event('lag') + changed, _ = process.output('1 streaming, 1 root sessions') + self.assertLess(changed - start, 1) + start = time.monotonic() + server.close() + deleted, _ = process.output('0 root sessions') + self.assertLess(deleted - start, 1) + print(f'acceptance: create={create_latency:.3f}s (150ms ack delay), delete={deleted-start:.3f}s', file=sys.stderr) + finally: + if server: + server.close() + process.close() + + def test_retry_backoff_despite_event_storm(self): + with tempfile.TemporaryDirectory() as directory: + server = FakeServer(directory, reject=True) + process = ModuleProcess(directory) + try: + process.output('0 root sessions') + end = time.monotonic() + 3.5 + path = Path(directory) / 'jcode-noise-debug.sock' + while time.monotonic() < end: + path.touch() + path.unlink() + time.sleep(.02) + self.assertEqual(len(server.attempts), 3, server.attempts) + gaps = [b-a for a, b in zip(server.attempts, server.attempts[1:])] + self.assertGreaterEqual(gaps[0], .95) + self.assertGreaterEqual(gaps[1], 1.95) + finally: + process.close() + server.close() + + def test_fixed_debounce_under_continuous_events(self): + with tempfile.TemporaryDirectory() as directory: + server = FakeServer(directory) + process = ModuleProcess(directory) + try: + process.output('1 root sessions') + server.processing = True + start = time.monotonic() + stop = threading.Event() + def flood(): + while not stop.wait(.005): + server.event() + thread = threading.Thread(target=flood) + thread.start() + try: + changed, _ = process.output('1 streaming, 1 root sessions', timeout=1) + self.assertLess(changed-start, .5) + finally: + stop.set() + thread.join() + finally: + process.close() + server.close() + + def test_fallback_discovery_when_inotify_unavailable(self): + with tempfile.TemporaryDirectory() as directory: + process = ModuleProcess(directory, fallback=True) + server = None + try: + process.output('0 root sessions') + server = FakeServer(directory) + process.output('1 root sessions', timeout=1) + finally: + if server: + server.close() + process.close() + + def test_directory_invalidation_fallback_and_recovery(self): + with tempfile.TemporaryDirectory() as root: + directory = Path(root) / 'runtime' + directory.mkdir() + process = ModuleProcess(str(directory)) + server = None + try: + process.output('0 root sessions') + (directory / 'discovery.log').unlink() + directory.rmdir() + time.sleep(.1) + directory.mkdir() + server = FakeServer(str(directory)) + process.output('1 root sessions', timeout=1) + server.close() + process.output('0 root sessions', timeout=1) + finally: + if server: + server.close() + process.close() + + +if __name__ == '__main__': + if '--runner' in sys.argv: + directory = sys.argv[2] + fallback = sys.argv[3] == '1' + original = m.SocketDirectoryWatcher + def watcher(callback): + if fallback: + raise OSError('inotify unavailable') + return original(callback, [directory]) + m.SocketDirectoryWatcher = watcher + def discover(): + # Parent may temporarily remove the directory in recovery tests. + try: + with (Path(directory) / 'discovery.log').open('a') as log: + log.write('scan\n') + except FileNotFoundError: + pass + return glob.glob(str(Path(directory) / 'jcode*-debug.sock')) + m._find_debug_sockets = discover + m.CloudPoller.start = lambda self: None + m.REFRESH_INTERVAL = .2 + m.main() + else: + unittest.main(verbosity=2) diff --git a/src/cli/acp.rs b/src/cli/acp.rs index be6b1cc474f..c0a9d2d7e2f 100644 --- a/src/cli/acp.rs +++ b/src/cli/acp.rs @@ -13,6 +13,8 @@ use tokio::sync::Mutex; use tokio::sync::mpsc; mod client_requests; +mod outcome; +use outcome::TurnOutcome; const ACP_PROTOCOL_VERSION: u64 = 1; @@ -410,14 +412,16 @@ impl AcpRuntime { .run_prompt(id.clone(), session, text, images, selection, &mut ticket) .await; ticket.release().await; - if let Err(err) = result { - let _ = runtime + if let Err(err) = result + && let Err(error) = runtime .write_error_value( id, JSONRPC_INTERNAL_ERROR, format!("Prompt failed: {err:#}"), ) - .await; + .await + { + crate::logging::warn(&format!("Failed to report ACP prompt error: {error:#}")); } completed_session .prompt_tasks @@ -455,13 +459,15 @@ impl AcpRuntime { // next one. With no turn known (an older daemon, or a prompt not // yet accepted) it stays session-wide. let turn = session.router.lock().await.active_turn_identity(); - let cancel_id = session.next_id(); - let _ = session - .send(&Request::Cancel { - id: cancel_id, - turn, - }) - .await; + if turn.is_some() || session.prompts.load(Ordering::SeqCst) > 0 { + let cancel_id = session.next_id(); + let _ = session + .send(&Request::Cancel { + id: cancel_id, + turn, + }) + .await; + } } if let Some(id) = message.id { self.write_result(id, json!({})).await?; diff --git a/src/cli/acp/daemon_router/lifecycle.rs b/src/cli/acp/daemon_router/lifecycle.rs index c0e9c1e5ad6..ab2942c6c47 100644 --- a/src/cli/acp/daemon_router/lifecycle.rs +++ b/src/cli/acp/daemon_router/lifecycle.rs @@ -337,6 +337,21 @@ impl RouterState { } else if let Some(change) = session_state_change(&event) { return vec![RouteAction::SessionState(change)]; } + // Joined prompts share the outcome without duplicating streamed + // text or usage. Continuation events reset intermediate limits. + if matches!( + event, + ServerEvent::TurnStopped { .. } + | ServerEvent::ProviderGuardrail { .. } + | ServerEvent::Interrupted + | ServerEvent::KvCacheRequest { .. } + | ServerEvent::RetryRollback { .. } + ) { + let joined = own.joined.clone(); + for member in joined { + self.deliver(member, event.clone()); + } + } self.deliver(request_id, event); return Vec::new(); } diff --git a/src/cli/acp/daemon_router_tests/settlement.rs b/src/cli/acp/daemon_router_tests/settlement.rs index 346703eb3d5..d81983c38bb 100644 --- a/src/cli/acp/daemon_router_tests/settlement.rs +++ b/src/cli/acp/daemon_router_tests/settlement.rs @@ -971,3 +971,45 @@ fn reconnect_new_steer_can_receive_output_of_retained_owned_turn() { assert!(drain(&mut failed).is_empty()); assert_eq!(router.active_turn_identity(), Some(identity(1))); } + +#[test] +fn joined_prompts_share_limits_and_continuation_resets_without_duplicate_text() { + let mut router = steering_router(); + let mut first = steer(&mut router, 77); + router.route(turn_started(1, ours(77), None)); + let mut second = steer(&mut router, 78); + router.route(scoped(1, injected("steer 78", &[cid(78)]))); + router.route(scoped( + 1, + ServerEvent::TurnStopped { + reason: TurnStopReason::LimitReached, + message: "limit".into(), + provider_stop_reason: Some("max_tokens".into()), + }, + )); + router.route(scoped(1, ServerEvent::RetryRollback { attempt: 1, max: 3 })); + router.route(scoped(1, delta("continued"))); + router.route(scoped( + 1, + ServerEvent::ProviderGuardrail { + stop_reason: Some("refusal".into()), + message: "blocked".into(), + }, + )); + router.route(scoped_terminal(1, TurnEndStatus::Completed)); + for (id, rx) in [(77, &mut first), (78, &mut second)] { + let events = drain(rx); + let mut outcome = TurnOutcome::routed(); + for event in &events { + outcome.observe(event, id); + } + assert_eq!(outcome.stop_reason(false), "refusal"); + assert!( + events + .iter() + .any(|e| matches!(e, ServerEvent::RetryRollback { .. })) + ); + assert_eq!(done_count(&events, id), 1); + assert_eq!(texts(&events).len(), usize::from(id == 77)); + } +} diff --git a/src/cli/acp/outcome.rs b/src/cli/acp/outcome.rs new file mode 100644 index 00000000000..3ba22618d57 --- /dev/null +++ b/src/cli/acp/outcome.rs @@ -0,0 +1,90 @@ +use super::*; + +/// Tracks daemon events for one ACP prompt and derives its ACP stop reason. +/// +/// Only the terminal outcome of the whole turn counts: a limit or refusal +/// followed by another model request (a successful continuation) is reset. +#[derive(Debug, Default)] +pub(super) struct TurnOutcome { + acked: bool, + interrupted: bool, + terminal: Option<&'static str>, +} + +impl TurnOutcome { + /// The router has already isolated this prompt's events. It also emits + /// interruptions for withdrawn prompts that were never acknowledged. + pub(super) fn routed() -> Self { + Self { + acked: true, + ..Self::default() + } + } + + pub(super) fn observe(&mut self, event: &ServerEvent, prompt_id: u64) { + match event { + ServerEvent::Ack { id } if *id == prompt_id => self.acked = true, + // Interrupts read before our prompt was acknowledged are stale + // leftovers from an earlier turn, not this prompt's outcome. + ServerEvent::Interrupted if self.acked => self.interrupted = true, + ServerEvent::KvCacheRequest { .. } | ServerEvent::RetryRollback { .. } => { + self.terminal = None; + } + ServerEvent::TurnStopped { + reason, + message, + provider_stop_reason, + } => match reason { + crate::protocol::TurnStopReason::LimitReached => { + let raw = provider_stop_reason.as_deref().unwrap_or_default(); + let lower = message.to_ascii_lowercase(); + self.terminal = Some( + if raw.contains("turn_requests") + || lower.contains("request limit") + || lower.contains("max turn") + { + "max_turn_requests" + } else { + "max_tokens" + }, + ); + } + crate::protocol::TurnStopReason::ProviderGuardrail => { + self.terminal = Some("refusal"); + } + crate::protocol::TurnStopReason::Interrupted if self.acked => { + self.interrupted = true; + } + _ => {} + }, + ServerEvent::ProviderGuardrail { stop_reason, .. } => { + if stop_reason.as_deref().is_some_and(is_refusal_stop_reason) { + self.terminal = Some("refusal"); + } + } + _ => {} + } + } + + pub(super) fn stop_reason(&self, cancel_requested: bool) -> &'static str { + if cancel_requested || self.interrupted { + "cancelled" + } else { + self.terminal.unwrap_or("end_turn") + } + } +} + +fn is_refusal_stop_reason(reason: &str) -> bool { + let reason = reason.to_ascii_lowercase(); + [ + "refusal", + "content_filter", + "safety", + "blocked", + "prohibited", + "guardrail", + ] + .iter() + .any(|needle| reason.contains(needle)) +} diff --git a/src/cli/acp/presentation.rs b/src/cli/acp/presentation.rs index e4eb4501c58..7bd5954315e 100644 --- a/src/cli/acp/presentation.rs +++ b/src/cli/acp/presentation.rs @@ -605,10 +605,11 @@ pub(super) fn tool_diffs(name: &str, input: &Value, working_dir: Option<&Path>) /// A plan is session state, not turn output, so it is rendered whenever the /// daemon's snapshot arrives — including after the turn that wrote it ended. /// Each update carries the complete list, as v1 requires; an emptied list is a -/// real replacement and is still sent. +/// real replacement and is still sent after a plan has been published. An +/// initial empty snapshot establishes no plan and needs no notification. pub(super) fn plan_update(last: &mut Option>, payload: &str) -> Option { let entries = plan_entries(payload); - if last.as_ref() == Some(&entries) { + if (last.is_none() && entries.is_empty()) || last.as_ref() == Some(&entries) { return None; } *last = Some(entries.clone()); diff --git a/src/cli/acp/prompt.rs b/src/cli/acp/prompt.rs index c4b8e3f86f1..ff3dde0a041 100644 --- a/src/cli/acp/prompt.rs +++ b/src/cli/acp/prompt.rs @@ -169,7 +169,12 @@ impl AcpRuntime { let scope = format!("{session_id}:own:{prompt_id}"); let mut mapper = EventMapper::with_scope(session_id.clone(), self.profile, scope.clone()) .with_working_dir(session.working_dir.clone()); - let mut stop_reason = "end_turn".to_string(); + let mut outcome = if session.router.lock().await.supports_turn_origin() { + TurnOutcome::routed() + } else { + // Legacy streams can still contain unscoped stale interrupts. + TurnOutcome::default() + }; let mut turn_usage = TurnUsage::default(); let mut steers = 0u32; loop { @@ -202,13 +207,29 @@ impl AcpRuntime { self.write_jcode_extension_event(&session_id, &event) .await?; } + outcome.observe(&event, prompt_id); match event { ServerEvent::Ack { .. } => {} ServerEvent::Done { id } if id == prompt_id => break, - ServerEvent::Interrupted => { - stop_reason = "cancelled".to_string(); - } + ServerEvent::Interrupted + | ServerEvent::TurnStopped { .. } + | ServerEvent::ProviderGuardrail { .. } => {} ServerEvent::Error { id, message, .. } if id == prompt_id => { + // The router's scoped cancellation is authoritative, even + // if the producer finishes with an error during shutdown. + if outcome.stop_reason(false) == "cancelled" { + let router = session.router.lock().await; + let response = streamed_prompt_response( + "cancelled", + &turn_usage, + router.prompt_correlation(prompt_id), + router.prompt_was_turnless(prompt_id), + ); + drop(router); + ticket.release().await; + self.write_result(rpc_id, response).await?; + return Ok(()); + } ticket.release().await; self.write_error_value(rpc_id, JSONRPC_SERVER_ERROR, message) .await?; @@ -271,7 +292,7 @@ impl AcpRuntime { let router = session.router.lock().await; let response = streamed_prompt_response( - &stop_reason, + outcome.stop_reason(false), &turn_usage, router.prompt_correlation(prompt_id), router.prompt_was_turnless(prompt_id), diff --git a/src/cli/acp/tests.rs b/src/cli/acp/tests.rs index 8e0beded777..2f7f8bfcd1b 100644 --- a/src/cli/acp/tests.rs +++ b/src/cli/acp/tests.rs @@ -6,3 +6,65 @@ mod requests; mod session; mod reexec; + +#[test] +fn acp_turn_outcome_maps_terminal_limits_and_refusals() { + use crate::protocol::TurnStopReason; + let limit = |raw: Option<&str>, msg: &str| ServerEvent::TurnStopped { + reason: TurnStopReason::LimitReached, + message: msg.to_string(), + provider_stop_reason: raw.map(str::to_string), + }; + let mut o = TurnOutcome::default(); + o.observe(&ServerEvent::Ack { id: 7 }, 7); + assert_eq!(o.stop_reason(false), "end_turn"); + o.observe(&limit(Some("max_tokens"), "truncated"), 7); + assert_eq!(o.stop_reason(false), "max_tokens"); + // A continued model request clears an intermediate limit. + o.observe(&ServerEvent::RetryRollback { attempt: 1, max: 3 }, 7); + assert_eq!(o.stop_reason(false), "end_turn"); + o.observe(&limit(Some("max_turn_requests"), "limit"), 7); + assert_eq!(o.stop_reason(false), "max_turn_requests"); + + let mut o = TurnOutcome::default(); + o.observe(&ServerEvent::Ack { id: 7 }, 7); + o.observe( + &ServerEvent::ProviderGuardrail { + stop_reason: Some("refusal".into()), + message: "blocked".into(), + }, + 7, + ); + assert_eq!(o.stop_reason(false), "refusal"); + // Cancellation wins over everything for the cancelled prompt. + assert_eq!(o.stop_reason(true), "cancelled"); + + // Non-refusal provider notices are not refusals. + let mut o = TurnOutcome::default(); + o.observe( + &ServerEvent::ProviderGuardrail { + stop_reason: Some("end_turn".into()), + message: "empty".into(), + }, + 7, + ); + assert_eq!(o.stop_reason(false), "end_turn"); +} + +#[test] +fn acp_turn_outcome_ignores_stale_interrupt_before_ack() { + let mut o = TurnOutcome::default(); + o.observe(&ServerEvent::Interrupted, 9); + o.observe(&ServerEvent::Ack { id: 9 }, 9); + assert_eq!(o.stop_reason(false), "end_turn"); + o.observe(&ServerEvent::Interrupted, 9); + assert_eq!(o.stop_reason(false), "cancelled"); +} + +#[test] +fn routed_outcome_preserves_unacknowledged_withdrawal() { + let mut outcome = TurnOutcome::routed(); + outcome.observe(&ServerEvent::Interrupted, 7); + outcome.observe(&ServerEvent::Done { id: 7 }, 7); + assert_eq!(outcome.stop_reason(false), "cancelled"); +} diff --git a/src/cli/acp/tests/requests.rs b/src/cli/acp/tests/requests.rs index ced7d6fd482..3d3eda9db95 100644 --- a/src/cli/acp/tests/requests.rs +++ b/src/cli/acp/tests/requests.rs @@ -41,10 +41,8 @@ fn plan_update_reports_an_emptied_list_as_a_replacement() { #[test] fn plan_update_ignores_a_payload_that_is_not_a_todo_snapshot() { - // Absence is legitimate: a malformed or unrelated payload yields an empty - // plan, never entries invented from text. - let update = plan_update(&mut None, "not json").expect("empty plan"); - assert_eq!(update["entries"], json!([])); + // Absence establishes no plan, never entries invented from text. + assert!(plan_update(&mut None, "not json").is_none()); } #[test] @@ -697,3 +695,21 @@ fn explicit_skill_keeps_slash_commands_as_prompt_text() { assert!(selected_slash_command("/model foo", &selection).is_some()); } } + +#[test] +fn plan_update_initial_empty_resume_is_silent_but_later_clear_is_published() { + let mut last = None; + assert!(plan_update(&mut last, &todo_snapshot(json!([]))).is_none()); + assert!(last.is_none()); + assert!( + plan_update( + &mut last, + &todo_snapshot(json!([ + {"content":"resumed work", "status":"pending"} + ])) + ) + .is_some() + ); + assert!(plan_update(&mut last, &todo_snapshot(json!([]))).is_some()); + assert!(plan_update(&mut last, &todo_snapshot(json!([]))).is_none()); +} diff --git a/src/cli/args.rs b/src/cli/args.rs index 09d33fa07b5..8d2de3b9768 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -418,6 +418,30 @@ pub(crate) enum Command { /// Do not run the global push-to-talk listener #[arg(long)] no_voice: bool, + + /// Do not show the live waveform in the focused Jcode CLI + #[arg(long)] + no_voice_waveform: bool, + + /// Do not show desktop notifications for push-to-talk + #[arg(long)] + no_voice_notifications: bool, + + /// Do not handle open/reveal requests (clients open locally) + #[arg(long)] + no_open: bool, + + /// Do not open terminal windows (clients launch locally) + #[arg(long)] + no_spawn_terminal: bool, + + /// Do not apply the lid-close override (macOS/Windows) + #[arg(long)] + no_lid: bool, + + /// Do not auto-update the browser bridge + #[arg(long)] + no_browser_updates: bool, }, /// Run configured dictation: send to last-focused jcode client or type raw text @@ -1057,6 +1081,9 @@ pub(crate) enum CloudMachineAction { Status, Wake, Stop, + /// Internal: prepare local managed SSH credentials without contacting cloud. + #[value(hide = true)] + PrepareSsh, } #[derive(Parser, Debug, Clone, Default)] diff --git a/src/cli/args/cloud_runtime.rs b/src/cli/args/cloud_runtime.rs index 152291b972d..980fd42232c 100644 --- a/src/cli/args/cloud_runtime.rs +++ b/src/cli/args/cloud_runtime.rs @@ -6,12 +6,34 @@ use std::path::PathBuf; pub(crate) enum CloudRuntimeCommand { /// Show the installed runtime, running daemon, and update policy. Status, - /// Upload an explicit Linux x86_64 runtime package and select dev policy. - /// Active daemons keep their executable until their next normal start. + /// Upload a Linux x86_64 runtime package and select dev policy. + /// Active daemons keep their executable until their next start, or until + /// `--reload` hands the daemon off once no session is mid-turn. + #[command(group(clap::ArgGroup::new("source").required(true).args(["artifact", "from_source"])))] Push { /// Compatible jcode-linux-x86_64.tar.gz package, not a laptop executable. #[arg(long)] - artifact: PathBuf, + artifact: Option, + /// Build a portable Linux package from this jcode checkout first. + #[arg(long)] + from_source: bool, + /// After upload, hand the cloud daemon off to it once no session is mid-turn. + #[arg(long)] + reload: bool, + /// Seconds to wait for running turns before giving up (with --reload). + #[arg(long, default_value_t = 1800)] + wait_secs: u64, + }, + /// Hand the cloud daemon off to the selected runtime once no session is + /// mid-turn. Daemons with live reload exec in place and keep clients + /// attached. Older ones restart, and sessions resume on reconnect. + Reload { + /// Restart immediately even if turns are running (they are interrupted). + #[arg(long)] + force: bool, + /// Seconds to wait for running turns before giving up. + #[arg(long, default_value_t = 1800)] + wait_secs: u64, }, /// Return to automatic managed stable updates (may select an older release). Stable { @@ -52,9 +74,42 @@ mod tests { assert!(matches!( parsed.command, Some(Command::Cloud(CloudCommand::Runtime { - action: CloudRuntimeCommand::Push { artifact } + action: CloudRuntimeCommand::Push { artifact: Some(artifact), from_source: false, reload: false, .. } })) if artifact == std::path::Path::new("portable.tar.gz") )); + assert!( + Args::try_parse_from([ + "jcode", + "cloud", + "runtime", + "push", + "--artifact", + "a.tar.gz", + "--from-source" + ]) + .is_err() + ); + let parsed = Args::try_parse_from([ + "jcode", + "cloud", + "runtime", + "push", + "--from-source", + "--reload", + ]) + .unwrap(); + assert!(matches!( + parsed.command, + Some(Command::Cloud(CloudCommand::Runtime { + action: CloudRuntimeCommand::Push { + artifact: None, + from_source: true, + reload: true, + wait_secs: 1800 + } + })) + )); + assert!(Args::try_parse_from(["jcode", "cloud", "runtime", "reload", "--force"]).is_ok()); } #[test] diff --git a/src/cli/cloud_group_daemon.rs b/src/cli/cloud_group_daemon.rs index f48449382f5..bdc9cc43053 100644 --- a/src/cli/cloud_group_daemon.rs +++ b/src/cli/cloud_group_daemon.rs @@ -7,7 +7,7 @@ fn daemon_command(action: &str, payload: &serde_json::Value) -> Result Result { let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?; runtime.block_on(async move { - tokio::time::timeout(std::time::Duration::from_secs(30), async move { + tokio::time::timeout(std::time::Duration::from_secs(90), async move { if !crate::server_spawn::is_running().await { crate::server_spawn::spawn_default_server().await .context("could not start daemon for coordinated migration")?; diff --git a/src/cli/cloud_managed.rs b/src/cli/cloud_managed.rs index 202f8ef1113..9718917885d 100644 --- a/src/cli/cloud_managed.rs +++ b/src/cli/cloud_managed.rs @@ -24,22 +24,6 @@ const DEFAULT_API_BASE: &str = "https://api.jcode.sh/v1"; const PROVISION_DEADLINE: Duration = Duration::from_secs(10 * 60); const POLL_INTERVAL: Duration = Duration::from_secs(4); -/// A live multiplexed SSH master means the machine is awake and this client is -/// already authorized, so the Jcode Cloud wake/authorize call can be skipped. -pub(crate) fn master_is_live() -> bool { - let Ok(args) = ssh_args() else { - return false; - }; - Command::new("ssh") - .args(args) - .args(["-O", "check", HOST]) - .stdin(std::process::Stdio::null()) - .stdout(std::process::Stdio::null()) - .stderr(std::process::Stdio::null()) - .status() - .is_ok_and(|status| status.success()) -} - pub(crate) fn is_managed(host: &str) -> bool { host == HOST } @@ -66,6 +50,11 @@ pub(crate) fn ssh_config_path() -> Result { Ok(dir()?.join("ssh_config")) } +/// Prepare only local SSH files. Do not provision, wake, authorize, or connect. +pub(crate) fn prepare_ssh() -> Result<()> { + ssh_args().map(|_| ()) +} + /// Ensure the client key and private SSH config exist. Returns `-F ` /// arguments for an `ssh` invocation targeting [`HOST`]. pub(crate) fn ssh_args() -> Result> { @@ -128,6 +117,11 @@ pub(super) fn proxy_executable(exe: PathBuf) -> PathBuf { } } +pub(crate) fn preparation_namespace() -> Result { + let account = account()?; + control_namespace(&account.key, &account.base).context("missing cloud account namespace") +} + fn control_namespace(key: &str, base: &str) -> Option { use sha2::{Digest, Sha256}; @@ -333,18 +327,32 @@ pub(crate) fn stop() -> Result { request("POST", "cloud/host/stop", Some(serde_json::json!({}))) } +fn timed_managed_stage(stage: &str, operation: impl FnOnce() -> Result) -> Result { + let start = Instant::now(); + let result = operation(); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=prepare operation={} outcome={} duration_ms={:.3}", + stage.replace(' ', "_"), + if result.is_ok() { "ok" } else { "error" }, + start.elapsed().as_secs_f64() * 1000.0 + )); + result +} + /// Create or wake the machine and wait until it accepts this client's key. /// `progress` receives each distinct status message. pub(crate) fn connect(progress: &mut dyn FnMut(&str)) -> Result { - let key = public_key()?; + let key = timed_managed_stage("cloud client key preparation", public_key)?; let deadline = Instant::now() + PROVISION_DEADLINE; let mut last = String::new(); loop { - let reply = request( - "POST", - "cloud/host/connect", - Some(serde_json::json!({ "public_key": key })), - )?; + let reply = timed_managed_stage("cloud connect API request", || { + request( + "POST", + "cloud/host/connect", + Some(serde_json::json!({ "public_key": key })), + ) + })?; if reply.ready { return Ok(reply); } @@ -359,7 +367,10 @@ pub(crate) fn connect(progress: &mut dyn FnMut(&str)) -> Result { if Instant::now() >= deadline { bail!("Jcode Cloud machine did not become ready in time. Retry to keep waiting."); } - std::thread::sleep(POLL_INTERVAL); + timed_managed_stage("cloud wake poll wait", || { + std::thread::sleep(POLL_INTERVAL); + Ok(()) + })?; } } @@ -529,6 +540,11 @@ fn prepare_script_for_paths_with_candidate( let privilege = if elevate { "sudo -n " } else { "" }; let home = shell_quote(home); let want = shell_quote(target.unwrap_or("")); + let capability_schema = shell_quote(&format!( + "handoff-capabilities-v1:{}:{}", + jcode_build_meta::VERSION, + jcode_build_meta::GIT_HASH, + )); format!( r#"set -eu want={want} @@ -541,6 +557,12 @@ cli="$HOME/.local/bin/jcode" backend={backend} root="$HOME/.local/share/jcode/cloud-releases" mkdir -p "$HOME/.local/bin" "$root" +stage_started=$(date +%s%N) +stage_timing() {{ + stage_now=$(date +%s%N) + echo "[CLOUD_TIMING] stage=prepare operation=$1 outcome=ok duration_ms=$(( (stage_now - stage_started) / 1000000 ))" >&2 + stage_started=$stage_now +}} # Serialize cooperating preflights without terminating any running process. # Wait briefly for a concurrent prepare instead of failing the move, and # reclaim a lock left behind by a prepare that was killed mid-run. @@ -560,6 +582,7 @@ until mkdir "$lock" 2>/dev/null; do sleep 2 waited=$((waited + 2)) done +stage_timing lock_wait stage= backup= next= @@ -569,6 +592,7 @@ committed=0 cleanup() {{ rc=$? trap - EXIT HUP INT TERM + if [ "$rc" != 0 ]; then rm -f "$root/.capabilities-ok"; fi if [ "$switched" = 1 ] && [ "$committed" = 0 ]; then rollback_ok=1 if [ "$had_previous" = 1 ]; then @@ -598,6 +622,34 @@ cleanup() {{ }} trap cleanup EXIT trap 'exit 1' HUP INT TERM +# Cache only successful protocol probes, not release or managed runtime policy. +# Include wrapper, backend, symlink targets and adjacent loader/library files. +# A changed install (including an atomic replacement) must be probed again. +capability_snapshot() {{ + [ -f "$cli" ] && [ -f "$backend" ] || return 1 + resolved=$(readlink -f "$backend") || return 1 + printf '%s\n' {capability_schema} + stat -c '%n %d %i %s %y %z %a' "$cli" "$backend" || return 1 + stat -Lc '%n %d %i %s %y %z %a' "$cli" "$backend" || return 1 + files=$(find -L "$(dirname "$resolved")" -maxdepth 1 -type f \ + -exec stat -Lc '%n %d %i %s %y %z %a' {{}} +) || return 1 + printf '%s\n' "$files" | LC_ALL=C sort +}} +capability_cache="$root/.capabilities-ok" +snapshot=$(capability_snapshot) || snapshot= +warm=0 +now=$(date +%s) +if [ -n "$snapshot" ] && [ -z "$development_candidate" ] && [ "${{JCODE_CLOUD_FORCE_UPGRADE:-}}" != 1 ] && [ -f "$capability_cache" ]; then + stamp=$(head -n 1 "$capability_cache") + case "$stamp" in ''|*[!0-9]*) stamp=0 ;; esac + # Longer than the local 120s handoff marker: every reuse verifies this + # installation snapshot and still evaluates version/runtime policy below. + if [ "$stamp" -le "$now" ] && [ "$((now - stamp))" -le 600 ] && [ "$(tail -n +2 "$capability_cache")" = "$snapshot" ]; then + warm=1 + fi +fi +echo "[cloud prepare] capability cache warm=$warm" >&2 +stage_timing cache_verification probe() {{ # Do not let a fast-release parent identity mask the candidate's real version. env -u JCODE_RUNTIME_RELEASE_SEMVER -u JCODE_RUNTIME_RELEASE_GIT_HASH \ @@ -605,6 +657,7 @@ probe() {{ "$1" --no-update --no-selfdev "$2" "${{3:-}}" "${{4:-}}" }} capable() {{ + if [ "$warm" = 1 ] && {{ [ "$1" = "$cli" ] || [ "$1" = "$backend" ]; }}; then return 0; fi probe "$1" cloud receive --help >/dev/null 2>&1 && probe "$1" cloud credentials --help >/dev/null 2>&1 && probe "$1" cloud router-credentials --help >/dev/null 2>&1 && @@ -665,6 +718,8 @@ if [ -x "$managed_updater" ]; then exit 1 }} elif ! capable "$cli" || ! capable "$backend" || {{ [ -n "$want" ] && {{ ! version_at_least "$cli" || ! version_at_least "$backend"; }}; }} || [ "${{JCODE_CLOUD_FORCE_UPGRADE:-}}" = 1 ]; then + # Cached evidence describes the old install, never the candidate. + warm=0 if [ -z "$want" ] && [ -z "$development_candidate" ]; then # Development builds have no corresponding published tag. Resolve the # canonical latest redirect, then pin and validate that exact release. @@ -750,6 +805,7 @@ elif ! capable "$cli" || ! capable "$backend" || {{ [ -n "$want" ] && {{ ! versi {privilege}mv -Tf "$next" "$backend" capable "$backend" && version_matches "$backend" && capable "$cli" && version_matches "$cli" || exit 1 fi +stage_timing runtime_policy_and_preparation # All handoff protocols are mandatory even when credentials are not needed yet. capable "$cli" || {{ echo '[cloud prepare] cloud handoff, replacement and group protocols are required' >&2; exit 1; }} committed=1 @@ -757,6 +813,19 @@ if [ -n {home} ] && [ ! -d {home} ]; then sudo -n install -d -o "$(id -un)" -g "$(id -gn)" -m 755 {home} fi command -v git >/dev/null || sudo -n dnf install -y git >/dev/null +# Never renew the lifetime of cached evidence without actually probing again. +final_snapshot=$(capability_snapshot) || final_snapshot= +if [ "$warm" = 1 ] && [ "$final_snapshot" != "$snapshot" ]; then + echo '[cloud prepare] runtime changed during verification; retry preparation' >&2 + exit 1 +fi +if [ "$warm" = 0 ] && [ -z "$development_candidate" ]; then + rm -f "$capability_cache" + if [ -n "$snapshot" ] && [ "$final_snapshot" = "$snapshot" ]; then + (umask 077; printf '%s\n%s\n' "$now" "$snapshot" > "$capability_cache") + fi +fi +stage_timing final_verification "# ) } @@ -811,6 +880,100 @@ mod upgrade_tests; mod tests { use super::*; + #[test] + fn prepare_ssh_cli_is_hidden_and_only_prepares_local_files() { + use super::super::args::{Args, CloudCommand, CloudMachineAction}; + use clap::{Parser, ValueEnum}; + + let _lock = crate::storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let base = format!("https://{}/v1", listener.local_addr().unwrap()); + struct RestoreEnv(Vec<(&'static str, Option)>); + impl Drop for RestoreEnv { + fn drop(&mut self) { + for (name, value) in &self.0 { + match value { + Some(value) => crate::env::set_var(name, value), + None => crate::env::remove_var(name), + } + } + } + } + let _restore = RestoreEnv( + ["JCODE_HOME", "JCODE_API_KEY", "JCODE_API_BASE"] + .into_iter() + .map(|name| (name, std::env::var_os(name))) + .collect(), + ); + crate::env::set_var("JCODE_HOME", home.path()); + crate::env::set_var("JCODE_API_KEY", "offline-test-account"); + crate::env::set_var("JCODE_API_BASE", &base); + + let args = Args::try_parse_from([ + "jcode", + "--no-update", + "--no-selfdev", + "cloud", + "machine", + "prepare-ssh", + ]) + .unwrap(); + assert!(matches!( + args.command, + Some(super::super::args::Command::Cloud(CloudCommand::Machine { + action: CloudMachineAction::PrepareSsh + })) + )); + assert!( + CloudMachineAction::PrepareSsh + .to_possible_value() + .unwrap() + .is_hide_set() + ); + + super::super::cloud_move::run_cli(CloudCommand::Machine { + action: CloudMachineAction::PrepareSsh, + }) + .unwrap(); + let config = ssh_config_path().unwrap(); + let original_key = std::fs::read(key_path().unwrap()).unwrap(); + assert!(key_path().unwrap().with_extension("pub").is_file()); + assert!( + std::fs::read_to_string(&config) + .unwrap() + .contains("cloud proxy") + ); + assert!(!known_hosts_path().unwrap().exists()); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + for (path, mode) in [ + (dir().unwrap(), 0o700), + (config.clone(), 0o600), + (key_path().unwrap(), 0o600), + ] { + assert_eq!( + std::fs::metadata(path).unwrap().permissions().mode() & 0o777, + mode + ); + } + } + std::fs::write(&config, "stale config").unwrap(); + prepare_ssh().unwrap(); + assert!( + std::fs::read_to_string(config) + .unwrap() + .contains("Host jcode-cloud") + ); + assert_eq!(std::fs::read(key_path().unwrap()).unwrap(), original_key); + assert_eq!( + listener.accept().unwrap_err().kind(), + std::io::ErrorKind::WouldBlock + ); + } + #[test] fn proxy_executable_survives_replaced_binary() { let dir = tempfile::tempdir().unwrap(); diff --git a/src/cli/cloud_managed_tests.rs b/src/cli/cloud_managed_tests.rs index 36621ccf513..34ff7e03513 100644 --- a/src/cli/cloud_managed_tests.rs +++ b/src/cli/cloud_managed_tests.rs @@ -55,6 +55,7 @@ mod shell { r#"#!/bin/sh set -eu [ -z "${{JCODE_RUNTIME_RELEASE_SEMVER:-}}" ] || exit 97 +printf '%s\n' "$*" >> "$HOME/probes" if [ {fail_after_switch} = true ] && [ "$0" = "$HOME/opt/jcode" ]; then exit 96; fi case "$*" in '--no-update --no-selfdev --version') echo 'jcode {version} (test)' ;; @@ -187,17 +188,21 @@ cp "$HOME/fixtures/${url##*/}" "$dest" ); let path = self.dir.path().join("prepare.sh"); std::fs::write(&path, script).unwrap(); - Command::new("sh") - .arg(path) + self.prepared_command().output().unwrap() + } + + fn prepared_command(&self) -> Command { + let mut command = Command::new("sh"); + command + .arg(self.dir.path().join("prepare.sh")) .env("HOME", self.dir.path()) .env( "PATH", format!("{}:/usr/bin:/bin", self.dir.path().join("mocks").display()), ) .env("JCODE_RUNTIME_RELEASE_SEMVER", "9.9.9") - .env_remove("JCODE_CLOUD_FORCE_UPGRADE") - .output() - .unwrap() + .env_remove("JCODE_CLOUD_FORCE_UPGRADE"); + command } fn assert_old(&self) { @@ -222,6 +227,163 @@ cp "$HOME/fixtures/${url##*/}" "$dest" } } + #[test] + fn warm_preparation_reuses_only_verified_unchanged_capabilities() { + let f = Fixture::new(); + f.installed(true); + let start = Instant::now(); + let cold = f.run(Some("v0.99.1")); + let cold_elapsed = start.elapsed(); + assert!( + cold.status.success(), + "{}", + String::from_utf8_lossy(&cold.stderr) + ); + let probes = f.dir.path().join("probes"); + let cold_probes = std::fs::read_to_string(&probes).unwrap(); + let cache = f + .dir + .path() + .join(".local/share/jcode/cloud-releases/.capabilities-ok"); + let cold_evidence = std::fs::read(&cache).unwrap(); + assert_eq!( + cold_probes.lines().filter(|l| l.contains("cloud ")).count(), + 18 + ); + std::fs::write(&probes, "").unwrap(); + let start = Instant::now(); + let warm = f.run(Some("v0.99.1")); + let warm_elapsed = start.elapsed(); + assert!( + warm.status.success(), + "{}", + String::from_utf8_lossy(&warm.stderr) + ); + let warm_probes = std::fs::read_to_string(&probes).unwrap(); + assert_eq!( + warm_probes.lines().filter(|l| l.contains("cloud ")).count(), + 0 + ); + assert_eq!( + warm_probes + .lines() + .filter(|l| l.contains("--version")) + .count(), + 2 + ); + let stderr = String::from_utf8_lossy(&warm.stderr); + assert!(stderr.contains("capability cache warm=1")); + assert_eq!(std::fs::read(&cache).unwrap(), cold_evidence); + for stage in [ + "lock_wait", + "cache_verification", + "runtime_policy_and_preparation", + "final_verification", + ] { + assert!(stderr.contains(&format!( + "[CLOUD_TIMING] stage=prepare operation={stage} outcome=ok duration_ms=" + ))); + } + eprintln!( + "representative generated-shell fixture: cold={cold_elapsed:?} (18 capability executions), warm={warm_elapsed:?} (0 capability executions, 2 version checks)" + ); + + // Adjacent library changes invalidate wrapper evidence as well. + std::fs::write(f.dir.path().join("opt/libfixture.so"), "changed").unwrap(); + let changed = f.run(Some("v0.99.1")); + assert!(changed.status.success()); + assert!(String::from_utf8_lossy(&changed.stderr).contains("capability cache warm=0")); + + // The release policy is still evaluated on a warm host. + f.artifact(true, false, None); + let upgraded = f.run(Some("v0.99.123")); + assert!( + upgraded.status.success(), + "{}", + String::from_utf8_lossy(&upgraded.stderr) + ); + assert!(f.backend().is_symlink()); + } + + #[test] + fn concurrent_remote_preparation_probes_once_then_reuses_verified_install() { + let f = Fixture::new(); + f.installed(true); + assert!(f.run(Some("v0.99.1")).status.success()); + std::fs::remove_file( + f.dir + .path() + .join(".local/share/jcode/cloud-releases/.capabilities-ok"), + ) + .unwrap(); + std::fs::write(f.dir.path().join("probes"), "").unwrap(); + let outputs = std::thread::scope(|scope| { + let first = scope.spawn(|| f.prepared_command().output().unwrap()); + let second = scope.spawn(|| f.prepared_command().output().unwrap()); + [first.join().unwrap(), second.join().unwrap()] + }); + for output in &outputs { + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + } + assert_eq!( + outputs + .iter() + .filter(|o| String::from_utf8_lossy(&o.stderr).contains("capability cache warm=1")) + .count(), + 1 + ); + let probes = std::fs::read_to_string(f.dir.path().join("probes")).unwrap(); + assert_eq!(probes.lines().filter(|l| l.contains("cloud ")).count(), 18); + } + + #[test] + fn warm_preparation_expiry_corruption_and_failure_invalidate_evidence() { + let f = Fixture::new(); + f.installed(true); + assert!(f.run(Some("v0.99.1")).status.success()); + let cache = f + .dir + .path() + .join(".local/share/jcode/cloud-releases/.capabilities-ok"); + let saved = std::fs::read_to_string(&cache).unwrap(); + let (_, snapshot) = saved.split_once('\n').unwrap(); + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + std::fs::write(&cache, format!("{}\n{snapshot}", now - 121)).unwrap(); + let past_handoff_marker = f.run(Some("v0.99.1")); + assert!(past_handoff_marker.status.success()); + assert!( + String::from_utf8_lossy(&past_handoff_marker.stderr) + .contains("capability cache warm=1") + ); + std::fs::write(&cache, format!("{}\n{snapshot}", now - 601)).unwrap(); + let expired = f.run(Some("v0.99.1")); + assert!(expired.status.success()); + assert!(String::from_utf8_lossy(&expired.stderr).contains("capability cache warm=0")); + for stamp in ["0", "invalid", "9999999999"] { + let saved = std::fs::read_to_string(&cache).unwrap(); + let (_, snapshot) = saved.split_once('\n').unwrap(); + std::fs::write(&cache, format!("{stamp}\n{snapshot}")).unwrap(); + let output = f.run(Some("v0.99.1")); + assert!(output.status.success()); + assert!(String::from_utf8_lossy(&output.stderr).contains("capability cache warm=0")); + } + // A changed binary may not inherit prior capability evidence. + executable(&f.backend(), &binary("v0.99.1", false, false)); + executable( + &f.dir.path().join("opt/runtime-update"), + "#!/bin/sh\nexit 1\n", + ); + assert!(!f.run(Some("v0.99.1")).status.success()); + assert!(!cache.exists()); + } + #[test] fn managed_policy_keeps_capable_runtime_despite_newer_client_release() { let f = Fixture::new(); diff --git a/src/cli/cloud_move.rs b/src/cli/cloud_move.rs index cc5fe446481..452b110bde1 100644 --- a/src/cli/cloud_move.rs +++ b/src/cli/cloud_move.rs @@ -271,7 +271,7 @@ fn run_router_credentials() -> Result<()> { /// keys and other providers are initialized when auth changes. No daemon means /// the next start reads the new files anyway. fn notify_daemon_auth_changed() { - let _ = std::thread::spawn(|| { + if std::thread::spawn(|| { let Ok(runtime) = tokio::runtime::Builder::new_current_thread() .enable_all() .build() @@ -279,15 +279,28 @@ fn notify_daemon_auth_changed() { return; }; runtime.block_on(async { - let _ = tokio::time::timeout(std::time::Duration::from_secs(3), async { + match tokio::time::timeout(std::time::Duration::from_secs(3), async { let mut client = crate::server::Client::connect().await?; client.notify_auth_changed().await?; anyhow::Ok(()) }) - .await; + .await + { + Ok(Ok(())) => {} + Ok(Err(error)) => { + crate::logging::debug(&format!("Daemon auth notification failed: {error:#}")) + } + Err(error) => { + crate::logging::debug(&format!("Daemon auth notification timed out: {error}")) + } + } }); }) - .join(); + .join() + .is_err() + { + crate::logging::warn("Daemon auth notification thread panicked"); + } } fn cloud_accounts() -> Result> { @@ -423,20 +436,8 @@ fn mark_memory_sent(host: &str, memory: &Path, bytes: &[u8]) { } } -fn timed_move_stage(stage: &str, operation: impl FnOnce() -> Result) -> Result { - let started = std::time::Instant::now(); - let result = operation(); - crate::logging::info(&format!( - "cloud move: {stage} {} in {:.3}s", - if result.is_ok() { - "completed" - } else { - "failed" - }, - started.elapsed().as_secs_f64() - )); - result -} +mod timing; +use timing::timed_move_stage; pub(crate) fn run_move( session_ref: Option<&str>, @@ -1011,7 +1012,11 @@ pub(crate) fn run_return( last_sync_head: manifest.repo.as_ref().map(|r| r.head.clone()), updated_at: chrono::Utc::now().to_rfc3339(), })?; - let _ = std::fs::remove_file(baseline_path(&session_id)?); + if let Err(error) = std::fs::remove_file(baseline_path(&session_id)?) + && error.kind() != std::io::ErrorKind::NotFound + { + crate::logging::warn(&format!("Failed to remove cloud baseline: {error}")); + } if requires_completion { complete_remote_return(target, &report)?; } @@ -1617,7 +1622,11 @@ fn prepare_repo_snapshot( // Temporary index so the user's real index is untouched. let index = stage.join("snapshot.index"); let git_dir = PathBuf::from(git(&root_path, &["rev-parse", "--absolute-git-dir"])?); - std::fs::copy(git_dir.join("index"), &index).ok(); + if let Err(error) = std::fs::copy(git_dir.join("index"), &index) + && error.kind() != std::io::ErrorKind::NotFound + { + return Err(error).context("failed to copy Git index for working-tree snapshot"); + } let env = [("GIT_INDEX_FILE", index.as_os_str())]; git_env(&root_path, &["add", "-A", "--", "."], &env)?; let tree = git_env(&root_path, &["write-tree"], &env)?; @@ -1751,31 +1760,41 @@ fn restore_repo( } } let head_tree = git(&root, &["rev-parse", "HEAD^{tree}"]).ok(); - if !known.contains(¤t) && head_tree.as_deref() != Some(current.as_str()) { - let owners = git( - &root, - &["for-each-ref", "--format=%(refname)", SNAPSHOT_REF_PREFIX], - ) - .unwrap_or_default(); - let mut owners: Vec<&str> = owners - .lines() - .filter_map(|name| name.strip_prefix("refs/jcode-cloud/")) - .filter_map(|name| name.strip_suffix("/base")) - .collect(); - owners.sort_unstable(); - owners.dedup(); - let recovery = if owners.len() == 1 { - format!( - "Run `jcode cloud return --session {}` locally to recover that session's work first", - owners[0] - ) - } else { - "Run `jcode cloud where` to identify the earlier session, then `jcode cloud return --session ` locally to recover its work first".to_string() - }; - bail!( - "{} on the cloud host has changes that were never returned; refusing to overwrite. {recovery}. No cloud working files were overwritten", - repo.root, - ); + // An empty checkout holds nothing that could be lost. + let empty_tree = git(&root, &["hash-object", "-t", "tree", "/dev/null"]).ok(); + if !known.contains(¤t) + && head_tree.as_deref() != Some(current.as_str()) + && empty_tree.as_deref() != Some(current.as_str()) + { + // Preserve the unreturned state as a durable commit instead of + // refusing. Orphaned work from older sessions otherwise blocks + // every later move into this checkout. The rescue ref keeps the + // full tree (tracked and untracked) recoverable on this host. + let stamp = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_secs()) + .unwrap_or_default(); + let rescue_ref = format!("{SNAPSHOT_REF_PREFIX}/rescued/{stamp}-{session_id}"); + let message = format!("jcode cloud: unreturned work rescued before {session_id}"); + let mut args = vec!["commit-tree", current.as_str(), "-m", message.as_str()]; + let parent = git(&root, &["rev-parse", "--verify", "HEAD^{commit}"]).ok(); + if let Some(parent) = &parent { + args.extend(["-p", parent.as_str()]); + } + let rescued = git_env(&root, &args, &identity_env(&root)).and_then(|commit| { + git(&root, &["update-ref", &rescue_ref, &commit])?; + Ok(commit) + }); + match rescued { + Ok(commit) => eprintln!( + "[cloud receive] preserved unreturned work in {} as {rescue_ref} ({commit})", + repo.root + ), + Err(error) => { + eprintln!("[cloud receive] could not preserve unreturned work: {error:#}"); + refuse_unreturned(&root, repo)?; + } + } } } else { std::fs::create_dir_all(&root) @@ -1785,6 +1804,48 @@ fn restore_repo( let _ = git(&root, &["remote", "add", "origin", url]); } } + restore_repo_contents(repo, &root, &bundle_str, &snap_ref, ignored_tar, session_id) +} + +fn refuse_unreturned(root: &Path, repo: &RepoManifest) -> Result<()> { + let root = root.to_path_buf(); + let owners = git( + &root, + &["for-each-ref", "--format=%(refname)", SNAPSHOT_REF_PREFIX], + ) + .unwrap_or_default(); + let mut owners: Vec<&str> = owners + .lines() + .filter_map(|name| name.strip_prefix("refs/jcode-cloud/")) + .filter_map(|name| name.strip_suffix("/base")) + .collect(); + owners.sort_unstable(); + owners.dedup(); + let recovery = if owners.len() == 1 { + format!( + "Run `jcode cloud return --session {}` locally to recover that session's work first", + owners[0] + ) + } else { + "Run `jcode cloud where` to identify the earlier session, then `jcode cloud return --session ` locally to recover its work first".to_string() + }; + bail!( + "{} on the cloud host has changes that were never returned; refusing to overwrite. {recovery}. No cloud working files were overwritten", + repo.root, + ); +} + +fn restore_repo_contents( + repo: &RepoManifest, + root: &Path, + bundle_str: &str, + snap_ref: &str, + ignored_tar: &Path, + session_id: &str, +) -> Result { + let root = root.to_path_buf(); + let bundle_str = bundle_str.to_owned(); + let snap_ref = snap_ref.to_owned(); // An incremental bundle may omit the branch advertisement when that // branch's tip is already a prerequisite on this host. The snapshot has // the complete parent chain we need, so fetch it rather than requiring an @@ -2012,7 +2073,11 @@ fn merge_back( fn snapshot_worktree_tree(root: &Path, scratch: &Path) -> Result { let index = scratch.join("ours.index"); let git_dir = PathBuf::from(git(root, &["rev-parse", "--absolute-git-dir"])?); - std::fs::copy(git_dir.join("index"), &index).ok(); + if let Err(error) = std::fs::copy(git_dir.join("index"), &index) + && error.kind() != std::io::ErrorKind::NotFound + { + return Err(error).context("failed to copy Git index for working-tree snapshot"); + } let env = [("GIT_INDEX_FILE", index.as_os_str())]; git_env(root, &["add", "-A", "--", "."], &env)?; git_env(root, &["write-tree"], &env) @@ -2249,6 +2314,7 @@ pub(crate) fn run_cli(command: super::args::CloudCommand) -> Result<()> { C::Machine { action } => { use super::args::CloudMachineAction as A; let reply = match action { + A::PrepareSsh => return super::cloud_managed::prepare_ssh(), A::Status => super::cloud_managed::status()?, A::Wake => super::cloud_managed::connect(&mut |m| eprintln!("{m}"))?, A::Stop => super::cloud_managed::stop()?, diff --git a/src/cli/cloud_move/timing.rs b/src/cli/cloud_move/timing.rs new file mode 100644 index 00000000000..12a7e9e646f --- /dev/null +++ b/src/cli/cloud_move/timing.rs @@ -0,0 +1,13 @@ +use anyhow::Result; + +pub(super) fn timed_move_stage(stage: &str, operation: impl FnOnce() -> Result) -> Result { + let started = std::time::Instant::now(); + let result = operation(); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=handoff operation={} outcome={} duration_ms={:.3}", + stage.replace(' ', "_"), + if result.is_ok() { "ok" } else { "error" }, + started.elapsed().as_secs_f64() * 1000.0 + )); + result +} diff --git a/src/cli/cloud_move/transport.rs b/src/cli/cloud_move/transport.rs index 654fd04b178..1b8b9eac783 100644 --- a/src/cli/cloud_move/transport.rs +++ b/src/cli/cloud_move/transport.rs @@ -86,10 +86,7 @@ fn output_with_ssh_retry( { return Ok(output); } - step( - "prepare", - &ssh_retry_message(host, attempt, &output.stderr), - ); + step("prepare", &ssh_retry_message(host, attempt, &output.stderr)); std::thread::sleep(std::time::Duration::from_secs(2 * attempt as u64)); attempt += 1; } @@ -140,38 +137,78 @@ impl Target { /// Fail fast, with an actionable message, when the cloud host's jcode is /// too old to speak the move protocol. pub(super) fn preflight(&self, host: &str) -> Result<()> { - if self.transport.is_none() && preflight_is_fresh(&self.preflight_key(host)) { + // Lock across the cache check and preparation, including other CLI + // processes. Waiters reuse only a fully successful preflight. + let _guard = if self.transport.is_none() { + Some(timed_move_stage("preflight singleflight wait", || { + let home = crate::storage::jcode_dir()?; + std::fs::create_dir_all(&home).context("create cloud preflight directory")?; + let path = home.join("cloud-preflight.lock"); + let file = std::fs::OpenOptions::new() + .create(true) + .truncate(false) + .write(true) + .open(path)?; + file.lock().context("lock cloud preflight")?; + Ok::<_, anyhow::Error>(file) + })?) + } else { + None + }; + let key = self.preflight_key(host)?; + if self.transport.is_none() + && std::env::var("JCODE_CLOUD_FORCE_UPGRADE").as_deref() != Ok("1") + && preflight_is_fresh(&key) + { crate::logging::info("cloud move: host preflight skipped, prepared moments ago"); return Ok(()); } let result = self.preflight_uncached(host); if result.is_ok() && self.transport.is_none() { - record_preflight(&self.preflight_key(host)); + record_preflight(&key); + } else if result.is_err() && self.transport.is_none() { + // An old marker must not survive a failed verification. + if let Some(path) = super::preflight_marker() { + let _ = std::fs::remove_file(path); + } } result } - fn preflight_key(&self, host: &str) -> String { + fn preflight_key(&self, host: &str) -> Result { // Capability changes invalidate markers written by older clients. - format!( - "{host}\n{}\n{}", + let namespace = if super::super::cloud_managed::is_managed(host) && self.transport.is_none() + { + super::super::cloud_managed::preparation_namespace()? + } else { + String::new() + }; + let exe = std::env::current_exe().context("locate preflight client")?; + // A running daemon's executable can have been replaced by an update. + // Its compiled identity still scopes evidence even when that old path + // no longer has metadata. + let metadata = exe.metadata().ok().map(|m| (m.len(), m.modified().ok())); + Ok(format!( + "{host}\n{}\n{}\n{namespace}\n{:?}\n{:?}\n{}\n{:?}\n{}\n{}\n{}", self.remote_binary(), - REQUIRED_REMOTE_COMMANDS.join(",") - ) + REQUIRED_REMOTE_COMMANDS.join(","), + std::env::var_os("JCODE_CLOUD_RELEASE"), + std::env::var_os("JCODE_CLOUD_FORCE_UPGRADE"), + exe.display(), + metadata, + jcode_build_meta::VERSION, + jcode_build_meta::GIT_HASH, + jcode_build_meta::semver(), + )) } fn preflight_uncached(&self, host: &str) -> Result<()> { timed_move_stage("host preflight", || { if super::super::cloud_managed::is_managed(host) && self.transport.is_none() { - // Usually the machine is already running and this is one - // authorization request. `connect` reports real wake progress. + // The SSH ProxyCommand connects and reports wake progress. + // Calling connect here too would authorize the same key twice + // whenever the multiplexed master has expired. step("prepare", "connecting to your Jcode Cloud machine"); - timed_move_stage("cloud api connect", || { - if super::super::cloud_managed::master_is_live() { - return Ok(()); - } - super::super::cloud_managed::connect(&mut |m| step("prepare", m)).map(|_| ()) - })?; let home = dirs::home_dir() .context("locate local home directory for cloud preparation")? .to_string_lossy() @@ -201,6 +238,12 @@ impl Target { } Ok(()) })?; + // The managed preparation already verified this exact wrapper + // and every mandatory protocol. Custom binary selections still + // need their own capability probes below. + if self.remote_binary() == "jcode" { + return Ok(()); + } } // Preserve custom transports that only support one remote invocation. if self.transport.is_none() { @@ -217,18 +260,20 @@ impl Target { "out=$({binary} --no-update --no-selfdev cloud {command} --help 2>&1 >/dev/null) || {{ printf '%s\\ncloud protocol probe failed: {command} (remote %s)\\n' \"$out\" \"$version\" >&2; exit 4; }}\n" )); } - let output = output_with_ssh_retry( - || { - let mut command = Self::ssh_command(host)?; - command - .arg(&script) - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::piped()); - Ok(command) - }, - host, - )?; + let output = timed_move_stage("host capability verification", || { + output_with_ssh_retry( + || { + let mut command = Self::ssh_command(host)?; + command + .arg(&script) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()); + Ok(command) + }, + host, + ) + })?; let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); return match output.status.code() { Some(0) => Ok(()), @@ -392,6 +437,14 @@ impl Target { .context("ssh stdin")? .write_all(script.as_bytes()); let output = child.wait_with_output()?; + for line in String::from_utf8_lossy(&output.stderr).lines() { + if line.starts_with("[CLOUD_TIMING] stage=prepare ") + || line.starts_with("[cloud prepare timing] ") + || line.starts_with("[cloud prepare] capability cache warm=") + { + crate::logging::info(line); + } + } if output.status.success() { written.context("send cloud preparation script")?; return Ok(()); @@ -399,10 +452,7 @@ impl Target { let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); // The prepare script is idempotent and serialized by its own lock. if ssh_connection_failed(&output.status) && attempt < SSH_ATTEMPTS { - step( - "prepare", - &ssh_retry_message(host, attempt, &output.stderr), - ); + step("prepare", &ssh_retry_message(host, attempt, &output.stderr)); std::thread::sleep(std::time::Duration::from_secs(2 * attempt as u64)); attempt += 1; continue; diff --git a/src/cli/cloud_move/transport/tests.rs b/src/cli/cloud_move/transport/tests.rs index 490567057c1..55e50b47409 100644 --- a/src/cli/cloud_move/transport/tests.rs +++ b/src/cli/cloud_move/transport/tests.rs @@ -1,5 +1,162 @@ use super::*; +struct RestoreEnv(Vec<(&'static str, Option)>); + +impl RestoreEnv { + fn save(names: &[&'static str]) -> Self { + Self( + names + .iter() + .map(|&name| (name, std::env::var_os(name))) + .collect(), + ) + } +} + +impl Drop for RestoreEnv { + fn drop(&mut self) { + for (name, value) in &self.0 { + match value { + Some(value) => crate::env::set_var(name, value), + None => crate::env::remove_var(name), + } + } + } +} + +#[test] +fn preflight_cache_identity_tracks_account_endpoint_release_and_binary() -> Result<()> { + let _lock = crate::storage::lock_test_env(); + let _restore = RestoreEnv::save(&[ + "JCODE_API_KEY", + "JCODE_API_BASE", + "JCODE_CLOUD_RELEASE", + "JCODE_CLOUD_REMOTE_BINARY", + ]); + crate::env::set_var("JCODE_API_KEY", "offline-account-a"); + crate::env::set_var("JCODE_API_BASE", "https://offline-a.invalid/v1"); + crate::env::remove_var("JCODE_CLOUD_RELEASE"); + crate::env::remove_var("JCODE_CLOUD_REMOTE_BINARY"); + let target = Target::default(); + let host = super::super::super::cloud_managed::HOST; + let initial = target.preflight_key(host)?; + assert!(!initial.contains("offline-account-a")); + crate::env::set_var("JCODE_API_KEY", "offline-account-b"); + assert_ne!(initial, target.preflight_key(host)?); + crate::env::set_var("JCODE_API_KEY", "offline-account-a"); + crate::env::set_var("JCODE_API_BASE", "https://offline-b.invalid/v1"); + assert_ne!(initial, target.preflight_key(host)?); + crate::env::set_var("JCODE_API_BASE", "https://offline-a.invalid/v1"); + crate::env::set_var("JCODE_CLOUD_RELEASE", "v99.0.0"); + assert_ne!(initial, target.preflight_key(host)?); + crate::env::remove_var("JCODE_CLOUD_RELEASE"); + crate::env::set_var("JCODE_CLOUD_REMOTE_BINARY", "/other/jcode"); + assert_ne!(initial, target.preflight_key(host)?); + Ok(()) +} + +#[test] +fn managed_preparation_verifies_default_once_and_still_probes_custom_binary() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + let _lock = crate::storage::lock_test_env(); + let _restore = RestoreEnv::save(&[ + "JCODE_HOME", + "PATH", + "JCODE_API_KEY", + "JCODE_API_BASE", + "JCODE_CLOUD_REMOTE_BINARY", + "JCODE_CLOUD_FORCE_UPGRADE", + "JCODE_CLOUD_RELEASE", + ]); + let dir = tempfile::tempdir()?; + crate::env::set_var("JCODE_HOME", dir.path()); + crate::env::set_var("JCODE_API_KEY", "offline-account"); + // Reintroduced direct API calls must not reach any real cloud endpoint. + crate::env::set_var("JCODE_API_BASE", "https://127.0.0.1:9/v1"); + crate::env::remove_var("JCODE_CLOUD_REMOTE_BINARY"); + crate::env::remove_var("JCODE_CLOUD_FORCE_UPGRADE"); + crate::env::remove_var("JCODE_CLOUD_RELEASE"); + let ssh = dir.path().join("ssh"); + std::fs::write( + &ssh, + "#!/bin/sh\nprintf 'invocation\\n' >> \"$JCODE_HOME/calls\"\ncat >\"$JCODE_HOME/sent-script\"\n", + )?; + std::fs::set_permissions(&ssh, std::fs::Permissions::from_mode(0o755))?; + crate::env::set_var("PATH", format!("{}:/usr/bin:/bin", dir.path().display())); + let host = super::super::super::cloud_managed::HOST; + Target::default().preflight(host)?; + let preparation = std::fs::read_to_string(dir.path().join("sent-script"))?; + for command in REQUIRED_REMOTE_COMMANDS { + assert!(preparation.contains(&format!("cloud {command} --help"))); + } + assert_eq!( + std::fs::read_to_string(dir.path().join("calls"))? + .lines() + .count(), + 1 + ); + let custom = Target { + remote_binary: Some("/custom/jcode".into()), + ..Target::default() + }; + custom.preflight(host)?; + assert_eq!( + std::fs::read_to_string(dir.path().join("calls"))? + .lines() + .count(), + 3 + ); + Ok(()) +} + +#[test] +fn concurrent_preflights_share_success_and_failed_checks_clear_marker() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + let _lock = crate::storage::lock_test_env(); + let _restore = RestoreEnv::save(&[ + "JCODE_HOME", + "PATH", + "JCODE_CLOUD_REMOTE_BINARY", + "JCODE_CLOUD_FORCE_UPGRADE", + ]); + let dir = tempfile::tempdir()?; + crate::env::set_var("JCODE_HOME", dir.path()); + crate::env::remove_var("JCODE_CLOUD_REMOTE_BINARY"); + crate::env::remove_var("JCODE_CLOUD_FORCE_UPGRADE"); + let ssh = dir.path().join("ssh"); + std::fs::write( + &ssh, + "#!/bin/sh\nprintf 'probe\\n' >> \"$JCODE_HOME/calls\"\nsleep 0.1\n[ ! -f \"$JCODE_HOME/fail\" ]\n", + )?; + std::fs::set_permissions(&ssh, std::fs::Permissions::from_mode(0o755))?; + crate::env::set_var("PATH", format!("{}:/usr/bin:/bin", dir.path().display())); + std::thread::scope(|scope| { + let first = scope.spawn(|| Target::default().preflight("offline-fixture")); + let second = scope.spawn(|| Target::default().preflight("offline-fixture")); + first.join().unwrap().unwrap(); + second.join().unwrap().unwrap(); + }); + assert_eq!( + std::fs::read_to_string(dir.path().join("calls"))? + .lines() + .count(), + 1 + ); + crate::env::set_var("JCODE_CLOUD_FORCE_UPGRADE", "1"); + Target::default().preflight("offline-fixture")?; + std::fs::write(dir.path().join("fail"), "")?; + assert!(Target::default().preflight("offline-fixture").is_err()); + assert!(!super::super::preflight_marker().unwrap().exists()); + crate::env::remove_var("JCODE_CLOUD_FORCE_UPGRADE"); + let changed = Target { + remote_binary: Some("different-jcode".into()), + ..Target::default() + }; + assert!(changed.preflight("offline-fixture").is_err()); + assert!(!super::super::preflight_marker().unwrap().exists()); + Ok(()) +} + fn scripted_target(script: &str) -> Result<(tempfile::TempDir, Target)> { let dir = tempfile::tempdir()?; let path = dir.path().join("transport.sh"); diff --git a/src/cli/cloud_move_repo_tests.rs b/src/cli/cloud_move_repo_tests.rs index 3f327c0bd7e..f7be226842c 100644 --- a/src/cli/cloud_move_repo_tests.rs +++ b/src/cli/cloud_move_repo_tests.rs @@ -29,37 +29,73 @@ pub(super) fn repository() -> (tempfile::TempDir, tempfile::TempDir, RepoManifes } #[test] -fn cloud_conflict_identifies_earlier_session_and_preserves_changes() { - let (root, stage, repo) = repository(); +fn snapshot_index_copy_errors_are_reported_without_mutating_real_index() { + let (root, _stage, _) = repository(); + let scratch = tempfile::tempdir().unwrap(); + let index_path = root.path().join(".git/index"); + let original_index = std::fs::read(&index_path).unwrap(); + for name in ["snapshot.index", "ours.index"] { + std::fs::create_dir(scratch.path().join(name)).unwrap(); + } + let error = prepare_repo_snapshot(root.path(), "copy-error", scratch.path()).unwrap_err(); + assert!(format!("{error:#}").contains("failed to copy Git index")); + let error = snapshot_worktree_tree(root.path(), scratch.path()).unwrap_err(); + assert!(format!("{error:#}").contains("failed to copy Git index")); + assert_eq!(std::fs::read(index_path).unwrap(), original_index); +} + +#[test] +fn snapshot_rebuilds_a_missing_git_index_without_creating_the_real_index() { + let (root, _stage, _) = repository(); + let scratch = tempfile::tempdir().unwrap(); + let index_path = root.path().join(".git/index"); + std::fs::remove_file(&index_path).unwrap(); + let tree = snapshot_worktree_tree(root.path(), scratch.path()).unwrap(); + assert_eq!( + tree, + git(root.path(), &["rev-parse", "HEAD^{tree}"]).unwrap() + ); + assert!(!index_path.exists()); +} + +#[test] +fn cloud_unreturned_work_is_rescued_and_move_proceeds() { + let (root, _earlier_stage, _) = repository(); + let stage = tempfile::tempdir().unwrap(); + let repo = snapshot_repo(root.path(), "new-session", stage.path()) + .unwrap() + .unwrap(); std::fs::write(root.path().join("file.txt"), "valuable cloud work\n").unwrap(); std::fs::write(root.path().join("untracked.txt"), "also valuable\n").unwrap(); - let head = git(root.path(), &["rev-parse", "HEAD"]).unwrap(); - let status = git(root.path(), &["status", "--porcelain"]).unwrap(); - let error = restore_repo( + restore_repo( &repo, &stage.path().join("repo.bundle"), &stage.path().join("ignored.tar"), "new-session", ) - .unwrap_err() - .to_string(); - assert!( - error.contains("cloud return --session earlier-session"), - "{error}" - ); - assert!(error.contains("refusing to overwrite"), "{error}"); + .unwrap(); assert_eq!( std::fs::read_to_string(root.path().join("file.txt")).unwrap(), - "valuable cloud work\n" + "original\n" ); + let rescued = git( + root.path(), + &[ + "for-each-ref", + "--format=%(refname)", + "refs/jcode-cloud/rescued", + ], + ) + .unwrap(); + let rescued = rescued.lines().next().expect("rescue ref"); + assert!(rescued.ends_with("-new-session"), "{rescued}"); assert_eq!( - std::fs::read_to_string(root.path().join("untracked.txt")).unwrap(), - "also valuable\n" + git(root.path(), &["show", &format!("{rescued}:file.txt")]).unwrap(), + "valuable cloud work" ); - assert_eq!(git(root.path(), &["rev-parse", "HEAD"]).unwrap(), head); assert_eq!( - git(root.path(), &["status", "--porcelain"]).unwrap(), - status + git(root.path(), &["show", &format!("{rescued}:untracked.txt")]).unwrap(), + "also valuable" ); } diff --git a/src/cli/cloud_runtime.rs b/src/cli/cloud_runtime.rs index 4cff4bdaadf..5d37a282ab9 100644 --- a/src/cli/cloud_runtime.rs +++ b/src/cli/cloud_runtime.rs @@ -10,6 +10,9 @@ use std::path::Path; use std::process::{Command, Stdio}; const HELPER: &str = "/opt/jcode-cloud/runtime-update"; +/// Must match `jcode-app-core`'s managed live reload capability marker. A +/// daemon binary containing it re-execs the managed selection on reload. +const LIVE_RELOAD_MARKER: &str = "jcode-managed-cloud-live-reload-v1"; const MAX_PACKAGE_BYTES: u64 = 256 * 1024 * 1024; /// All remote arguments are fixed verbs, validated release tags, or hex digests. @@ -35,6 +38,192 @@ fn remote_command(verb: &str, value: Option<&str>) -> Result { Ok(command) } +/// Remote reload: wait until no session is streaming, then restart the +/// managed service. `ExecStartPre` activates any pending runtime and +/// `ExecStopPost` rolls back a runtime that fails to start. Only an integer +/// and a fixed flag are interpolated. +fn reload_command(wait_secs: u64, force: bool) -> String { + let force = if force { 1 } else { 0 }; + let marker = LIVE_RELOAD_MARKER; + format!( + r#"set -u +export XDG_RUNTIME_DIR=/run/user/$(id -u) +say() {{ echo "[cloud reload] $*" >&2; }} +streaming() {{ + activity=$(/opt/jcode-cloud/jcode --no-update --no-selfdev menubar --json 2>/dev/null) || return 1 + printf '%s' "$activity" | python3 -c 'import json,sys; n=json.load(sys.stdin).get("streaming"); sys.exit(1) if type(n) is not int or n < 0 else print(n)' 2>/dev/null +}} +deadline=$(( $(date +%s) + {wait_secs} )) +last="" +# A daemon with managed live reload keeps in-flight turns across the re-exec, +# so it never needs to wait for idle. Only the restart fallback does. +daemon=$(pgrep -u "$(id -u)" -f '/opt/jcode-cloud/.* serve( |$)' | head -n1) +live_capable=0 +if [ -n "$daemon" ] && grep -qa {marker} "/proc/$daemon/exe" 2>/dev/null; then live_capable=1; fi +wait_idle() {{ + [ {force} = 1 ] && return 0 + while :; do + n=$(streaming) || {{ say "cannot verify cloud activity; not restarting"; exit 3; }} + [ "$n" = 0 ] && return 0 + if [ "$(date +%s)" -ge "$deadline" ]; then say "still $n session(s) mid-turn after {wait_secs}s; not restarting. Retry later or use --force."; exit 3; fi + [ "$n" != "$last" ] && say "waiting for $n session(s) to finish their turn" + last=$n; sleep 5 + done +}} +if [ "$live_capable" = 0 ]; then wait_idle; fi +before=$(readlink /opt/jcode-cloud/jcode) +pending=0 +sudo -n /usr/local/sbin/jcode-cloud-runtime status 2>/dev/null | grep -q '"pending": {{' && pending=1 +# Live path: a daemon built with managed live reload re-execs the selected +# runtime in place, so sessions, clients and in-flight turns survive the same +# way a local self-dev reload does. Older daemons fall through to a restart. +if [ "$live_capable" = 1 ]; then + live_ok=1 + if [ "$pending" = 1 ]; then + sudo -n /usr/local/sbin/jcode-cloud-runtime activate-live >/dev/null || live_ok=0 + fi + if [ "$live_ok" = 1 ]; then + target=$(readlink -f /opt/jcode-cloud/jcode) + say "live reloading daemon (sessions stay attached)" + JCODE_SOCKET="$XDG_RUNTIME_DIR/jcode.sock" /opt/jcode-cloud/jcode --no-update --no-selfdev server reload --force --json >/dev/null 2>&1 || live_ok=0 + if [ "$live_ok" = 1 ] && [ "$(readlink /proc/$daemon/exe 2>/dev/null)" = "$target" ] && [ -S "$XDG_RUNTIME_DIR/jcode.sock" ]; then + [ "$pending" = 1 ] && say "activated new runtime" + say "daemon ready: $(/opt/jcode-cloud/jcode --version 2>/dev/null)" + exit 0 + fi + fi + say "live reload did not land; falling back to a restart" + if [ "$pending" = 1 ]; then + sudo -n /usr/local/sbin/jcode-cloud-runtime rollback-live >/dev/null 2>&1 && say "rolled back the live-activated runtime" + pending=0 + fi + before=$(readlink /opt/jcode-cloud/jcode) + # The restart fallback interrupts turns, so it still requires idle. + wait_idle +fi +say "restarting cloud daemon" +# A daemon may have handed itself off outside systemd on an earlier stop. If it +# keeps the socket, the unit start fails and the new runtime is falsely rolled +# back. Turns are idle here (or --force), so stop every daemon first. +systemctl --user stop jcode-cloud.service 2>/dev/null +for p in $(pgrep -u "$(id -u)" -f '/opt/jcode-cloud/.* serve( |$)'); do say "stopping detached daemon pid $p"; kill -TERM "$p" 2>/dev/null; done +for i in $(seq 1 20); do pgrep -u "$(id -u)" -f '/opt/jcode-cloud/.* serve( |$)' >/dev/null || break; sleep 1; done +pkill -KILL -u "$(id -u)" -f '/opt/jcode-cloud/.* serve( |$)' 2>/dev/null +rm -f "$XDG_RUNTIME_DIR/jcode.sock" +systemctl --user reset-failed jcode-cloud.service 2>/dev/null +systemctl --user start jcode-cloud.service || {{ say "start failed"; exit 1; }} +for i in $(seq 1 60); do + pid=$(systemctl --user show jcode-cloud.service --property=MainPID --value) + if [ "${{pid:-0}}" != 0 ] && [ -S "$XDG_RUNTIME_DIR/jcode.sock" ]; then + sleep 3 + pid2=$(systemctl --user show jcode-cloud.service --property=MainPID --value) + if [ "$pid2" = "$pid" ]; then + after=$(readlink /opt/jcode-cloud/jcode) + if [ "$pending" = 1 ] && [ "$after" = "$before" ]; then + say "new runtime failed to start and was rolled back; daemon is on: $(/opt/jcode-cloud/jcode --version 2>/dev/null)" + exit 1 + fi + [ "$after" = "$before" ] || say "activated new runtime" + say "daemon ready: $(/opt/jcode-cloud/jcode --version 2>/dev/null)" + exit 0 + fi + fi + sleep 1 +done +say "daemon did not become ready; see: journalctl --user -u jcode-cloud.service" +sudo -n /usr/local/sbin/jcode-cloud-runtime status >&2 || true +exit 1 +"# + ) +} + +fn run_remote(script: &str, input: Option) -> Result { + let args = cloud_managed::ssh_args()?; + Command::new("ssh") + .args(args) + .args(["-T", "-o", "BatchMode=yes", "-o", "ConnectTimeout=30"]) + .arg(cloud_managed::HOST) + .arg(script) + .stdin(input.map(Stdio::from).unwrap_or_else(Stdio::null)) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .status() + .context("connect to the managed cloud host over SSH") +} + +fn reload(wait_secs: u64, force: bool) -> Result<()> { + let status = run_remote(&reload_command(wait_secs, force), None)?; + match status.code() { + Some(0) => Ok(()), + Some(3) => bail!("cloud reload deferred: turns were still running. Nothing was restarted."), + _ => bail!("cloud reload failed ({status})"), + } +} + +/// Build a portable Linux x86_64 package from the jcode checkout containing +/// the current directory. +fn build_from_source() -> Result { + let root = Command::new("git") + .args(["rev-parse", "--show-toplevel"]) + .output() + .context("run git")?; + if !root.status.success() { + bail!("--from-source must run inside a jcode checkout"); + } + let root = std::path::PathBuf::from(String::from_utf8_lossy(&root.stdout).trim()); + let script = root.join("scripts/package_cloud_runtime.sh"); + if !script.is_file() { + bail!("{} is not a jcode checkout", root.display()); + } + let target = "x86_64-unknown-linux-gnu"; + eprintln!( + "[cloud build] building portable Linux runtime in {}", + root.display() + ); + let builder = if Command::new("justrust").arg("--version").output().is_ok() { + "justrust" + } else { + "cargo" + }; + let status = Command::new(builder) + .current_dir(&root) + .args([ + "zigbuild", + "--profile", + "selfdev", + "-p", + "jcode", + "--bin", + "jcode", + "--target", + &format!("{target}.2.17"), + "--features", + "linux-compat-vendored-openssl", + ]) + .status() + .with_context(|| format!("run {builder} zigbuild"))?; + if !status.success() { + bail!("portable Linux build failed ({status})"); + } + let binary = root + .join("target") + .join(target) + .join("selfdev") + .join("jcode"); + let output = root.join("target/cloud-runtime/jcode-linux-x86_64.tar.gz"); + eprintln!("[cloud build] packaging"); + let status = Command::new(&script) + .arg(&binary) + .arg(&output) + .stdout(Stdio::null()) + .status() + .context("run package_cloud_runtime.sh")?; + if !status.success() { + bail!("packaging the cloud runtime failed ({status})"); + } + Ok(output) +} + fn valid_release_tag(value: &str) -> bool { let Some(version) = value.strip_prefix('v') else { return false; @@ -112,13 +301,32 @@ pub(crate) fn run_cli(action: CloudRuntimeCommand) -> Result<()> { }; invoke("pin", Some(&version), None) } - CloudRuntimeCommand::Push { artifact } => { + CloudRuntimeCommand::Push { + artifact, + from_source, + reload: then_reload, + wait_secs, + } => { + let artifact = match (artifact, from_source) { + (Some(path), false) => path, + (None, true) => build_from_source()?, + _ => bail!("pass exactly one of --artifact or --from-source"), + }; let (file, digest) = package(&artifact)?; eprintln!( - "Uploading explicit cloud runtime package ({digest}). This selects dev policy for the whole host. Active daemons are not restarted." + "[cloud push] uploading runtime package {digest}. This selects dev policy for the whole host." ); - invoke("push", Some(&digest), Some(file)) + invoke("push", Some(&digest), Some(file))?; + if then_reload { + reload(wait_secs, false) + } else { + eprintln!( + "Active daemons are not restarted. Run `jcode cloud runtime reload` to switch." + ); + Ok(()) + } } + CloudRuntimeCommand::Reload { force, wait_secs } => reload(wait_secs, force), } } @@ -164,6 +372,53 @@ mod tests { ); } + #[test] + fn live_reload_marker_matches_daemon_capability() { + assert_eq!(LIVE_RELOAD_MARKER, crate::server::MANAGED_CLOUD_LIVE_RELOAD_MARKER); + let script = reload_command(0, false); + assert!(script.contains(&format!("grep -qa {LIVE_RELOAD_MARKER} "))); + assert!(script.contains("activate-live")); + assert!(script.contains("rollback-live")); + // Live reload keeps in-flight turns, so only the restart fallback waits. + assert!(script.contains(r#"if [ "$live_capable" = 0 ]; then wait_idle; fi"#)); + let fallback = script.split("live reload did not land").nth(1).unwrap(); + let restart = fallback.split("restarting cloud daemon").next().unwrap(); + assert!(restart.contains("wait_idle")); + } + + #[test] + fn runtime_reload_requires_a_verified_nonnegative_idle_count() { + // Execute only the idle gate. No test can reach service stop/start code. + let generated = reload_command(0, false); + let gate = generated.split("before=$(readlink").next().unwrap(); + for (json, status, idle) in [ + (r#"{"streaming":0}"#, 0, true), + (r#"{"streaming":2}"#, 0, false), + (r#"{"streaming":0}"#, 1, false), + ("", 0, false), + ("not json", 0, false), + ("{}", 0, false), + (r#"{"streaming":false}"#, 0, false), + (r#"{"streaming":-1}"#, 0, false), + (r#"{"streaming":"0"}"#, 0, false), + ] { + let mock = r#"sh -c 'printf %s "$MOCK_ACTIVITY"; exit $MOCK_STATUS' --"#; + let script = gate.replace("/opt/jcode-cloud/jcode", mock) + "printf verified_idle"; + let result = Command::new("sh") + .args(["-c", &script]) + .env("MOCK_ACTIVITY", json) + .env("MOCK_STATUS", status.to_string()) + .output() + .unwrap(); + assert_eq!(result.status.success(), idle, "{json:?}, status={status}"); + assert_eq!(result.stdout == b"verified_idle", idle); + if !idle { + assert_eq!(result.status.code(), Some(3)); + assert!(String::from_utf8_lossy(&result.stderr).contains("not restarting")); + } + } + } + #[test] fn runtime_upload_hashes_and_rewinds_the_same_open_file() { let mut file = tempfile::NamedTempFile::new().unwrap(); diff --git a/src/cli/commands.rs b/src/cli/commands.rs index 4b6aff3cc41..e97c586121c 100644 --- a/src/cli/commands.rs +++ b/src/cli/commands.rs @@ -1454,7 +1454,32 @@ pub async fn run_transcript_command( } } -pub async fn run_companion_command(status: bool, no_voice: bool) -> Result<()> { +/// `jcode companion --no-*` flags. Each turns a feature off on top of the +/// `[companion]` config. A flag can never turn on something config disables. +#[derive(Debug, Default, Clone, Copy)] +pub struct CompanionFlags { + pub no_voice: bool, + pub no_voice_waveform: bool, + pub no_voice_notifications: bool, + pub no_open: bool, + pub no_spawn_terminal: bool, + pub no_lid: bool, + pub no_browser_updates: bool, +} + +impl CompanionFlags { + pub fn apply(self, options: &mut jcode_companion::CompanionOptions) { + options.voice &= !self.no_voice; + options.voice_waveform &= !self.no_voice_waveform; + options.voice_notifications &= !self.no_voice_notifications; + options.open &= !self.no_open; + options.spawn_terminal &= !self.no_spawn_terminal; + options.lid &= !self.no_lid; + options.browser_updates &= !self.no_browser_updates; + } +} + +pub async fn run_companion_command(status: bool, flags: CompanionFlags) -> Result<()> { use crate::companion_client::{CompanionRequest, request_async}; if status { match request_async(CompanionRequest::Status, std::time::Duration::from_secs(2)).await { @@ -1470,11 +1495,9 @@ pub async fn run_companion_command(status: bool, no_voice: bool) -> Result<()> { Err(error) => anyhow::bail!("{error}"), } } - jcode_companion::run(jcode_companion::CompanionOptions { - voice: !no_voice, - ..Default::default() - }) - .await + let mut options = jcode_companion::CompanionOptions::from_config(); + flags.apply(&mut options); + jcode_companion::run(options).await } pub async fn run_dictate_command(type_output: bool) -> Result<()> { diff --git a/src/cli/commands/menubar_tests.rs b/src/cli/commands/menubar_tests.rs index 88df7eaca1f..629dc4c9da5 100644 --- a/src/cli/commands/menubar_tests.rs +++ b/src/cli/commands/menubar_tests.rs @@ -95,6 +95,7 @@ fn session_menu_item_title_loads_persisted_todo_title() { id: "menubar-label".to_string(), group: Some("Meaningful menu labels".to_string()), lane_after: None, + lane_owner: None, confidence: Some(crate::todo::ConfidenceState::Plausible), completion_confidence: None, confidence_history: Vec::new(), diff --git a/src/cli/companion_autostart.rs b/src/cli/companion_autostart.rs new file mode 100644 index 00000000000..4df77fa75e2 --- /dev/null +++ b/src/cli/companion_autostart.rs @@ -0,0 +1,93 @@ +//! Start `jcode companion` in the background when a local TUI client launches +//! and none is running, so global voice, opening and terminal spawns work +//! without a manual service. Controlled by `[companion] autostart`. + +use std::time::Duration; + +/// Whether this client should try to start a companion. +fn should_autostart( + enabled: bool, + native_ssh: bool, + in_test: bool, + env_off: bool, + is_companion: bool, +) -> bool { + enabled && !native_ssh && !in_test && !env_off && !is_companion +} + +/// Fire-and-forget: checks for a live companion off the UI thread and spawns +/// a detached one if missing. Concurrent clients are safe: the companion +/// refuses to start when another one already answers on the socket. +pub fn spawn_if_needed(native_ssh: bool) { + let enabled = crate::config::config().companion.autostart; + let env_off = std::env::var("JCODE_COMPANION_AUTOSTART").as_deref() == Ok("0"); + if !should_autostart( + enabled, + native_ssh, + crate::auth::running_in_test_harness(), + env_off, + std::env::var_os("JCODE_COMPANION").is_some(), + ) { + return; + } + if let Err(error) = std::thread::Builder::new() + .name("jcode-companion-autostart".into()) + .spawn(|| { + if crate::companion_client::is_running() { + return; + } + if let Err(error) = spawn_detached() { + crate::logging::warn(&format!("companion autostart failed: {error:#}")); + } else { + crate::logging::info("companion autostart: launched jcode companion"); + // Give it a moment, so the first open/spawn request finds it. + std::thread::sleep(Duration::from_millis(200)); + } + }) + { + crate::logging::warn(&format!( + "Failed to start companion autostart worker: {error}" + )); + } +} + +fn spawn_detached() -> anyhow::Result<()> { + use std::process::{Command, Stdio}; + let exe = std::env::current_exe()?; + let mut command = Command::new(exe); + command + .arg("companion") + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()); + // Outlive this client: own session, not killed with the terminal. + #[cfg(unix)] + { + use std::os::unix::process::CommandExt; + command.process_group(0); + } + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + const DETACHED_PROCESS: u32 = 0x0000_0008; + const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200; + command.creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP); + } + command.spawn()?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::should_autostart; + + #[test] + fn autostart_respects_config_ssh_tests_env_and_self() { + assert!(should_autostart(true, false, false, false, false)); + assert!(!should_autostart(false, false, false, false, false)); + assert!(!should_autostart(true, true, false, false, false)); + assert!(!should_autostart(true, false, true, false, false)); + assert!(!should_autostart(true, false, false, true, false)); + assert!(!should_autostart(true, false, false, false, true)); + } +} diff --git a/src/cli/dispatch.rs b/src/cli/dispatch.rs index 42bff7bdd15..ddab5c4bfdd 100644 --- a/src/cli/dispatch.rs +++ b/src/cli/dispatch.rs @@ -490,8 +490,29 @@ pub(crate) async fn run_main(mut args: Args) -> Result<()> { }) => { commands::run_transcript_command(text, map_transcript_mode(mode), session).await?; } - Some(Command::Companion { status, no_voice }) => { - commands::run_companion_command(status, no_voice).await?; + Some(Command::Companion { + status, + no_voice, + no_voice_waveform, + no_voice_notifications, + no_open, + no_spawn_terminal, + no_lid, + no_browser_updates, + }) => { + commands::run_companion_command( + status, + commands::CompanionFlags { + no_voice, + no_voice_waveform, + no_voice_notifications, + no_open, + no_spawn_terminal, + no_lid, + no_browser_updates, + }, + ) + .await?; } Some(Command::Dictate { r#type }) => { commands::run_dictate_command(r#type).await?; diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 440b13b1ca7..9f6e56831da 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -8,6 +8,7 @@ pub mod cloud_managed; pub mod cloud_move; pub mod cloud_runtime; pub mod commands; +pub mod companion_autostart; pub mod compatible_runtime; pub mod debug; pub mod dispatch; diff --git a/src/cli/ssh_transport.rs b/src/cli/ssh_transport.rs index b5bf69bf43e..b2de50d3f2a 100644 --- a/src/cli/ssh_transport.rs +++ b/src/cli/ssh_transport.rs @@ -19,6 +19,8 @@ use tokio::process::{Child, ChildStdin, ChildStdout, Command}; use tokio::sync::watch; use tokio::task::{JoinHandle, JoinSet}; +mod timing; + const PROTOCOL: u32 = 1; const HANDSHAKE_LIMIT: usize = 8192; const STDERR_LIMIT: usize = 16 * 1024; @@ -118,19 +120,16 @@ impl NativeSsh { ); } options.working_dir = remote_working_dir.map(str::to_owned); - // Fail before entering the TUI, with authentication/host-key diagnostics. - let mut probe = SshConnection::connect(&options).await?; + let probe = SshConnection::connect(&options).await?; let handshake = probe.handshake.clone(); - probe.shutdown().await; - // Remote --cwd already verified the directory and the header contains - // its resolved path. Keep reconnects on that same verified workspace. + // Keep reconnects on the workspace already resolved and verified by remote --cwd. options.working_dir = Some(handshake.working_dir.clone()); let directory = private_directory()?; let socket = directory.path().join("native.sock"); let listener = UnixListener::bind(&socket)?; let (stop, stopped) = watch::channel(false); - let manager = tokio::spawn(accept_connections(listener, options, stopped)); + let manager = tokio::spawn(accept_connections(listener, options, stopped, Some(probe))); Ok(Self { _directory: directory, socket, @@ -229,6 +228,7 @@ impl SshOptions { fn command(&self) -> Command { let mut command = Command::new("ssh"); + let mut managed_config = false; if let Some(id) = super::cloud_ephemeral::host_id(&self.host) { // The caller validates/prepares this private config before connecting. // Never fall through to the persistent host's SSH identity. @@ -239,38 +239,9 @@ impl SshOptions { && let Ok(args) = super::cloud_managed::ssh_args() { command.args(args); + managed_config = true; } - command.args([ - "-T", - "-o", - "BatchMode=yes", - "-o", - "StrictHostKeyChecking=yes", - "-o", - "ForwardAgent=no", - "-o", - "ClearAllForwardings=yes", - "-o", - "ServerAliveInterval=15", - "-o", - "ServerAliveCountMax=2", - "-o", - "PermitLocalCommand=no", - "-o", - "ForkAfterAuthentication=no", - "-o", - "StdinNull=no", - "-o", - "RemoteCommand=none", - "-o", - "SessionType=default", - "-o", - "ControlMaster=no", - "-S", - "none", - "-o", - "ConnectTimeout=30", - ]); + configure_transport(&mut command, managed_config); let binary = format!("'{}'", self.remote_binary.replace('\'', "'\\''")); let socket = self .daemon_socket @@ -290,6 +261,49 @@ impl SshOptions { } } +fn configure_transport(command: &mut Command, managed_config: bool) { + command.args([ + "-T", + "-C", + "-o", + "BatchMode=yes", + "-o", + "StrictHostKeyChecking=yes", + "-o", + "ForwardAgent=no", + "-o", + "ClearAllForwardings=yes", + "-o", + "ServerAliveInterval=15", + "-o", + "ServerAliveCountMax=2", + "-o", + "PermitLocalCommand=no", + "-o", + "ForkAfterAuthentication=no", + "-o", + "StdinNull=no", + "-o", + "RemoteCommand=none", + "-o", + "SessionType=default", + "-o", + "ControlMaster=no", + "-o", + "ControlPersist=no", + "-o", + "ConnectTimeout=30", + ]); + // Only the generated managed config has an account/API-scoped path in + // a private directory. Borrow an existing master, never create one or + // send it exit/stop commands. The owned child is just its session client. + // With no master, ControlMaster=no falls back to an owned direct SSH. + // Compression on a borrowed master is determined when it was created. + if !managed_config { + command.args(["-S", "none"]); + } +} + struct OwnedChild(Child); impl OwnedChild { fn kill(&mut self) { @@ -319,8 +333,7 @@ struct SshConnection { impl SshConnection { async fn connect(options: &SshOptions) -> Result { - // Jcode Cloud hosts may be asleep: waking the VM and starting the - // remote daemon routinely exceeds the plain SSH startup deadline. + // Waking cloud hosts and starting their daemon exceeds the plain SSH deadline. let cloud = super::cloud_managed::is_managed(&options.host) || super::cloud_ephemeral::host_id(&options.host).is_some(); let (deadline, attempts) = if cloud { @@ -342,11 +355,13 @@ impl SshConnection { } async fn spawn(command: Command, deadline: Duration) -> Result { + let mut timing = timing::StartupTiming::new(); let mut connection = Self::spawn_process(command)?; if let Err(error) = connection.establish(deadline).await { connection.shutdown().await; return Err(error.context(connection.diagnostic())); } + timing.success(); Ok(connection) } @@ -355,8 +370,7 @@ impl SshConnection { #[cfg(target_os = "linux")] { let parent_pid = std::process::id(); - // SIGKILL/process::exit do not run guards. Do not leave SSH holding - // a remote bridge open if the local UI disappears abruptly. + // SIGKILL/process::exit skip guards, so bind SSH lifetime to this UI. unsafe { command.as_std_mut().pre_exec(move || { if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) != 0 { @@ -510,6 +524,7 @@ async fn accept_connections( listener: UnixListener, options: SshOptions, mut stopped: watch::Receiver, + mut initial: Option, ) { let mut connections = JoinSet::new(); let backoff = Arc::new(Mutex::new(ReconnectBackoff::default())); @@ -529,8 +544,10 @@ async fn accept_connections( let backoff = backoff.clone(); let options = options.clone(); let mut stopped = stopped.clone(); + let initial = initial.take(); connections.spawn(async move { - let mut ssh = match SshConnection::spawn_process(options.command()) { + let established = initial.is_some(); + let mut ssh = match initial.map(Ok).unwrap_or_else(|| SshConnection::spawn_process(options.command())) { Ok(ssh) => ssh, Err(error) => { let (log, failures) = record_reconnect_failure(&backoff); @@ -541,7 +558,7 @@ async fn accept_connections( let connected = tokio::select! { biased; _ = stopped.changed() => None, - connected = ssh.establish(STARTUP_TIMEOUT) => Some(connected), + connected = async { if established { Ok(()) } else { ssh.establish(STARTUP_TIMEOUT).await } } => Some(connected), }; if !matches!(connected, Some(Ok(()))) { ssh.shutdown().await; @@ -572,6 +589,9 @@ async fn accept_connections( } } drop(listener); + if let Some(mut ssh) = initial.take() { + ssh.shutdown().await; + } // Guard drop wakes all children, which explicitly kill and reap SSH. while connections.join_next().await.is_some() {} } @@ -658,6 +678,98 @@ async fn bridge_stream( mod tests { use super::*; + #[test] + fn managed_mux_only_borrows_existing_master_and_enables_compression() { + for managed in [false, true] { + let mut command = Command::new("ssh"); + configure_transport(&mut command, managed); + let args: Vec<_> = command + .as_std() + .get_args() + .map(|s| s.to_str().unwrap()) + .collect(); + for required in [ + "-C", + "ControlMaster=no", + "ControlPersist=no", + "ForwardAgent=no", + "ClearAllForwardings=yes", + ] { + assert!(args.contains(&required), "{required}"); + } + assert_eq!(args.contains(&"-S"), !managed); + assert!(!args.contains(&"-O")); + } + } + + #[tokio::test] + async fn accepted_socket_reuses_probe_and_preserves_buffered_bytes() { + let mut probe = SshConnection::spawn( + shell(&format!( + "printf '%s' '{}'; exec cat", + hello() + "buffered-history\n" + )), + Duration::from_secs(2), + ) + .await + .unwrap(); + let pid = probe.child.0.id().unwrap(); + // Ensure bytes past the handshake are already in the BufReader. + assert!(!probe.reader.fill_buf().await.unwrap().is_empty()); + let directory = private_directory().unwrap(); + let socket = directory.path().join("native.sock"); + let listener = UnixListener::bind(&socket).unwrap(); + let (stop, stopped) = watch::channel(false); + // Invalid hostname must never be contacted for the first socket. + let manager = tokio::spawn(accept_connections( + listener, + SshOptions::new("unused.invalid", "jcode").unwrap(), + stopped, + Some(probe), + )); + let mut stream = UnixStream::connect(&socket).await.unwrap(); + stream.write_all(b"request\n").await.unwrap(); + let mut received = vec![0; b"buffered-history\nrequest\n".len()]; + tokio::time::timeout(Duration::from_secs(2), stream.read_exact(&mut received)) + .await + .unwrap() + .unwrap(); + assert_eq!(received, b"buffered-history\nrequest\n"); + stop.send(true).unwrap(); + tokio::time::timeout(Duration::from_secs(3), manager) + .await + .unwrap() + .unwrap(); + assert_eq!(unsafe { libc::kill(pid as i32, 0) }, -1); + } + + #[tokio::test] + async fn unused_probe_is_reaped_when_manager_stops() { + let probe = SshConnection::spawn( + shell(&format!("printf '%s' '{}'; exec sleep 60", hello())), + Duration::from_secs(2), + ) + .await + .unwrap(); + let pid = probe.child.0.id().unwrap(); + let directory = private_directory().unwrap(); + let listener = UnixListener::bind(directory.path().join("native.sock")).unwrap(); + let (stop, stopped) = watch::channel(false); + stop.send(true).unwrap(); + tokio::time::timeout( + Duration::from_secs(3), + accept_connections( + listener, + SshOptions::new("unused.invalid", "jcode").unwrap(), + stopped, + Some(probe), + ), + ) + .await + .unwrap(); + assert_eq!(unsafe { libc::kill(pid as i32, 0) }, -1); + } + #[test] fn reconnect_backoff_grows_caps_and_throttles_logs() { let now = std::time::Instant::now(); diff --git a/src/cli/ssh_transport/timing.rs b/src/cli/ssh_transport/timing.rs new file mode 100644 index 00000000000..aea93184f8c --- /dev/null +++ b/src/cli/ssh_transport/timing.rs @@ -0,0 +1,27 @@ +use std::time::Instant; + +/// Content-free timing even when an SSH future is cancelled or returns early. +pub(super) struct StartupTiming { + started: Instant, + outcome: &'static str, +} +impl StartupTiming { + pub(super) fn new() -> Self { + Self { + started: Instant::now(), + outcome: "error_or_cancelled", + } + } + pub(super) fn success(&mut self) { + self.outcome = "ok"; + } +} +impl Drop for StartupTiming { + fn drop(&mut self) { + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=ssh_startup outcome={} duration_ms={:.3}", + self.outcome, + self.started.elapsed().as_secs_f64() * 1000.0 + )); + } +} diff --git a/src/cli/terminal.rs b/src/cli/terminal.rs index 0ecf3ae0407..569b731b21d 100644 --- a/src/cli/terminal.rs +++ b/src/cli/terminal.rs @@ -649,6 +649,18 @@ fn handle_termination_signal(sig: i32) -> ! { std::process::exit(128 + sig); } +/// Another window took over this client's session +/// (`display.resume_open_elsewhere`). Restore the terminal and exit cleanly: +/// this is a handover, so the session must not be marked crashed. +#[cfg(unix)] +fn handle_replaced_signal() -> ! { + crate::tui::record_termination_started(); + if signal_cleanup_terminal_modes_to(signal_cleanup_stream()) { + crate::tui::disable_keyboard_enhancement(); + } + std::process::exit(0); +} + fn signal_cleanup_terminal_modes_to(mut writer: impl Write) -> bool { // The ownership check and the restore writes run under the kitty state // lock so an in-flight editor handoff cannot split them: the handoff @@ -786,6 +798,16 @@ pub fn spawn_session_signal_watchers() { spawn_one(libc::SIGTERM, SignalKind::terminate()); spawn_one(libc::SIGINT, SignalKind::interrupt()); spawn_one(libc::SIGQUIT, SignalKind::quit()); + + tokio::spawn(async move { + let kind = SignalKind::from_raw(crate::session_window::CLIENT_REPLACED_SIGNAL); + if let Ok(mut stream) = signal(kind) + && stream.recv().await.is_some() + { + crate::logging::info("Session opened in another window; closing this client"); + handle_replaced_signal(); + } + }); } #[cfg(not(unix))] diff --git a/src/cli/tui_launch.rs b/src/cli/tui_launch.rs index 3fd45e52ff8..a6d76b99001 100644 --- a/src/cli/tui_launch.rs +++ b/src/cli/tui_launch.rs @@ -7,7 +7,7 @@ use std::io::{self, Write}; use std::process::Command as ProcessCommand; use crate::{ - id, logging, replay, server, session, setup_hints, startup_profile, tui, video_export, + build, id, logging, replay, server, session, setup_hints, startup_profile, tui, video_export, }; use super::hot_exec::{execute_requested_action, has_requested_action}; @@ -162,6 +162,7 @@ pub async fn run_tui_client( if !native_ssh { spawn_session_signal_watchers(); } + super::companion_autostart::spawn_if_needed(native_ssh); if native_ssh { let host = std::env::var("JCODE_SSH_REMOTE").unwrap_or_default(); @@ -596,6 +597,17 @@ fn cloud_resume_target_args( Some(args) } +/// Binary a session picker launches its selection with. A picker window can +/// sit open for hours while builds move on, and `current_exe()` is the frozen +/// versioned binary it started from. Resume onto the active channel instead, +/// the same candidate reload uses, so stale pickers do not revive old builds. +fn picker_resume_exe() -> Result { + match build::client_update_candidate(false) { + Some((exe, _)) => Ok(exe), + None => Ok(std::env::current_exe()?), + } +} + pub fn list_sessions() -> Result<()> { fn build_resume_target_command( exe: &std::path::Path, @@ -697,6 +709,16 @@ pub fn list_sessions() -> Result<()> { crate::terminal_launch::spawn_command_in_new_terminal(&command, cwd) } + /// Apply `display.resume_open_elsewhere`. True when the session's existing + /// window was brought here, so no new client should attach. + fn moved_existing_window(target: &jcode_tui_session_picker::ResumeTarget) -> bool { + let jcode_tui_session_picker::ResumeTarget::JcodeSession { session_id } = target else { + return false; + }; + crate::session_window::apply_resume_open_elsewhere(session_id) + == crate::session_window::OpenElsewhereOutcome::Moved + } + match tui::session_picker::pick_session()? { Some(tui::session_picker::PickerResult::TakeOverClaude(target)) => { let resolved_target = crate::import::take_over_live_claude_session(&target)?; @@ -708,7 +730,7 @@ pub fn list_sessions() -> Result<()> { else { anyhow::bail!("Claude takeover did not produce a Jcode session"); }; - let exe = std::env::current_exe()?; + let exe = picker_resume_exe()?; let mut session_cwd = std::env::current_dir()?; if let Ok(sess) = session::Session::load(session_id) && let Some(dir) = sess.working_dir.as_deref() @@ -728,7 +750,7 @@ pub fn list_sessions() -> Result<()> { tui::session_picker::PickerResult::Selected(targets) | tui::session_picker::PickerResult::SelectedInCurrentTerminal(targets), ) => { - let exe = std::env::current_exe()?; + let exe = picker_resume_exe()?; let cwd = std::env::current_dir()?; if targets.len() == 1 { @@ -746,6 +768,9 @@ pub fn list_sessions() -> Result<()> { { session_cwd = std::path::PathBuf::from(dir); } + if moved_existing_window(&resolved_target) { + return Ok(()); + } let (program, args) = build_resume_target_command(&exe, &resolved_target); let err = crate::platform::replace_process( ProcessCommand::new(&program) @@ -777,6 +802,10 @@ pub fn list_sessions() -> Result<()> { session_cwd = std::path::PathBuf::from(dir); } + if moved_existing_window(&resolved_target) { + spawned += 1; + continue; + } match spawn_target_in_new_terminal(&resolved_target, &exe, &session_cwd) { Ok(true) => spawned += 1, Ok(false) => { @@ -808,7 +837,7 @@ pub fn list_sessions() -> Result<()> { } } Some(tui::session_picker::PickerResult::SelectedInNewTerminal(targets)) => { - let exe = std::env::current_exe()?; + let exe = picker_resume_exe()?; let cwd = std::env::current_dir()?; let mut spawned = 0usize; let mut warned_no_terminal = false; @@ -831,6 +860,10 @@ pub fn list_sessions() -> Result<()> { session_cwd = std::path::PathBuf::from(dir); } + if moved_existing_window(&resolved_target) { + spawned += 1; + continue; + } match spawn_target_in_new_terminal(&resolved_target, &exe, &session_cwd) { Ok(true) => spawned += 1, Ok(false) => { @@ -871,7 +904,7 @@ pub fn list_sessions() -> Result<()> { recovered.len() ); - let exe = std::env::current_exe()?; + let exe = picker_resume_exe()?; let cwd = std::env::current_dir()?; let mut spawned = 0usize; let mut warned_no_terminal = false; diff --git a/src/main.rs b/src/main.rs index 5ccb73ad379..ab3ed7b6d0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,6 +137,10 @@ fn run_main() -> Result<()> { return jcode::cli::hibernate_wait::run(); } + // Clients exec-reload too: reap git probes and other children a previous + // image left running, before this image spawns anything of its own. + jcode::inherited_children::start(); + let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() .build()?; diff --git a/tests/e2e/acp_shared_session.rs b/tests/e2e/acp_shared_session.rs index a442e05dc86..1e7a864a489 100644 --- a/tests/e2e/acp_shared_session.rs +++ b/tests/e2e/acp_shared_session.rs @@ -526,6 +526,8 @@ async fn acp_client_mirrors_peer_turns_and_keeps_its_own_prompt_separate() -> Re let replayed: Vec<&serde_json::Value> = attached[..started_at] .iter() .filter(|notification| notification["method"] == "session/update") + // A resumed todo snapshot is session state, not a replayed message. + .filter(|notification| notification["params"]["update"]["sessionUpdate"] != "plan") .collect(); assert!( !replayed.is_empty(), diff --git a/vendor/ratatui-jcode/LICENSE b/vendor/ratatui-jcode/LICENSE new file mode 100644 index 00000000000..776bf200770 --- /dev/null +++ b/vendor/ratatui-jcode/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2016-2022 Florian Dehau +Copyright (c) 2023-2025 The Ratatui Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/ratatui-jcode/README.md b/vendor/ratatui-jcode/README.md new file mode 100644 index 00000000000..75fa3eb9dae --- /dev/null +++ b/vendor/ratatui-jcode/README.md @@ -0,0 +1,16 @@ +# ratatui-jcode + +In-tree fork of [ratatui](https://github.com/ratatui/ratatui) `ratatui-core` +0.1.0 and `ratatui-widgets` 0.3.0 (MIT, see `LICENSE`), wired in through +`[patch.crates-io]` in the workspace `Cargo.toml`. + +Purpose: per-frame CPU work in release builds. Profiling the jcode TUI client +showed ~25-35% of busy CPU inside ratatui (`Buffer::diff`, `Cell` clone/eq/reset, +`Paragraph` grapheme splitting and width measurement). + +Rules: +- Keep the public API source-compatible with upstream so other crates in the + graph (ratatui-image, ratatui-crossterm) keep building unmodified. +- Mark every divergence from upstream with a `// ratatui-jcode:` comment so it + can be rebased or upstreamed. +- Prefer upstreaming generally useful wins. diff --git a/vendor/ratatui-jcode/ratatui-core/Cargo.toml b/vendor/ratatui-jcode/ratatui-core/Cargo.toml new file mode 100644 index 00000000000..30b1b13ab76 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/Cargo.toml @@ -0,0 +1,200 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +autotests = false +autoexamples = false +autobenches = false +edition = "2024" +rust-version = "1.86.0" +name = "ratatui-core" +version = "0.1.0" +authors = [ + "Florian Dehau ", + "The Ratatui Developers", +] +build = false +exclude = [ + "assets/*", + ".github", + "Makefile.toml", + "CONTRIBUTING.md", + "*.log", + "tags", +] +autolib = false +autobins = false +description = """ + Core types and traits for the Ratatui Terminal UI library. + Widget libraries should use this crate. Applications should use the main Ratatui crate. +""" +homepage = "https://ratatui.rs" +documentation = "https://docs.rs/ratatui-core" +readme = "README.md" +keywords = [ + "tui", + "terminal", + "dashboard", +] +categories = ["command-line-interface"] +license = "MIT" +repository = "https://github.com/ratatui/ratatui" +resolver = "2" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = [ + "--cfg", + "docsrs", +] + +[features] +anstyle = ["dep:anstyle"] +default = [] +layout-cache = ["std"] +palette = [ + "std", + "dep:palette", +] +portable-atomic = ["kasuari/portable-atomic"] +scrolling-regions = [] +serde = [ + "std", + "dep:serde", + "bitflags/serde", + "compact_str/serde", +] +std = [ + "itertools/use_std", + "thiserror/std", + "kasuari/std", + "compact_str/std", + "unicode-truncate/std", + "strum/std", +] +underline-color = [] + +[lib] +name = "ratatui_core" +path = "src/lib.rs" + + +[dependencies.anstyle] +version = "1" +optional = true + +[dependencies.bitflags] +version = "2.10" + +[dependencies.compact_str] +version = "0.9" +default-features = false + +[dependencies.document-features] +version = "0.2" +optional = true + +[dependencies.hashbrown] +version = "0.16" + +[dependencies.indoc] +version = "2" + +[dependencies.itertools] +version = "0.14" +features = ["use_alloc"] +default-features = false + +[dependencies.kasuari] +version = "0.4" +default-features = false + +[dependencies.lru] +version = "0.16" + +[dependencies.palette] +version = "0.7" +optional = true + +[dependencies.serde] +version = "1" +features = ["derive"] +optional = true + +[dependencies.strum] +version = "0.27" +features = ["derive"] +default-features = false + +[dependencies.thiserror] +version = "2" +default-features = false + +[dependencies.unicode-segmentation] +version = "1" + +[dependencies.unicode-truncate] +version = "2" +default-features = false + +[dependencies.unicode-width] +version = ">=0.2.0, <=0.2.2" + + + + +[lints.clippy] +as_underscore = "warn" +cast_possible_truncation = "allow" +cast_possible_wrap = "allow" +cast_precision_loss = "allow" +cast_sign_loss = "allow" +deref_by_slicing = "warn" +else_if_without_else = "warn" +empty_line_after_doc_comments = "warn" +equatable_if_let = "warn" +fn_to_numeric_cast_any = "warn" +format_push_string = "warn" +implicit_clone = "warn" +map_err_ignore = "warn" +missing_const_for_fn = "warn" +missing_errors_doc = "allow" +missing_panics_doc = "allow" +mixed_read_write_in_expression = "warn" +mod_module_files = "warn" +module_inception = "allow" +module_name_repetitions = "allow" +must_use_candidate = "allow" +needless_pass_by_ref_mut = "warn" +needless_raw_strings = "warn" +or_fun_call = "warn" +redundant_type_annotations = "warn" +rest_pat_in_fully_bound_structs = "warn" +string_lit_chars_any = "warn" +string_slice = "warn" +unnecessary_self_imports = "warn" +use_self = "warn" + +[lints.clippy.pedantic] +level = "warn" +priority = -1 + +[lints.rust] +unsafe_code = "forbid" + +[dev-dependencies.pretty_assertions] +version = "1" + +[dev-dependencies.rstest] +version = "0.26" + +[dev-dependencies.serde_json] +version = "1" diff --git a/vendor/ratatui-jcode/ratatui-core/README.md b/vendor/ratatui-jcode/ratatui-core/README.md new file mode 100644 index 00000000000..65eb36637bb --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/README.md @@ -0,0 +1,67 @@ +# Ratatui Core + +[![Crates.io](https://img.shields.io/crates/v/ratatui-core)](https://crates.io/crates/ratatui-core) +[![Documentation](https://docs.rs/ratatui-core/badge.svg)](https://docs.rs/ratatui-core) +[![License](https://img.shields.io/crates/l/ratatui-core)](../LICENSE) + + + + +**ratatui-core** is the core library of the [ratatui] project, +providing the essential building blocks for creating rich terminal user interfaces in Rust. + +[ratatui]: https://github.com/ratatui/ratatui + +### Why `ratatui-core`? + +The `ratatui-core` crate is split from the main [`ratatui`](https://crates.io/crates/ratatui) crate +to offer better stability for widget library authors. Widget libraries should generally depend +on `ratatui-core`, benefiting from a stable API and reducing the need for frequent updates. + +Applications, on the other hand, should depend on the main `ratatui` crate, which includes +built-in widgets and additional features. + +## Installation + +Add `ratatui-core` to your `Cargo.toml`: + +```shell +cargo add ratatui-core +``` + +## Crate Organization + +`ratatui-core` is part of the Ratatui workspace that was modularized in version 0.30.0 to +improve compilation times, API stability, and dependency management. This crate provides the +foundational types and traits that other crates in the workspace depend on. + +**When to use `ratatui-core`:** + +- Building widget libraries that implement [`Widget`] or [`StatefulWidget`] +- Creating lightweight applications that don't need built-in widgets +- You want minimal dependencies and faster compilation times +- You need maximum API stability (core types change less frequently) + +**When to use the main [`ratatui`] crate:** + +- Building applications that use built-in widgets +- You want convenience and don't mind slightly longer compilation times +- You need backend implementations and terminal management utilities + +For detailed information about the workspace organization, see [ARCHITECTURE.md]. + +[`ratatui`]: https://crates.io/crates/ratatui +[`Widget`]: widgets::Widget +[`StatefulWidget`]: widgets::StatefulWidget +[ARCHITECTURE.md]: https://github.com/ratatui/ratatui/blob/main/ARCHITECTURE.md + +## Contributing + +We welcome contributions from the community! Please see our [CONTRIBUTING](../CONTRIBUTING.md) +guide for more details on how to get involved. + +### License + +This project is licensed under the MIT License. See the [LICENSE](../LICENSE) file for details. + + diff --git a/vendor/ratatui-jcode/ratatui-core/src/backend.rs b/vendor/ratatui-jcode/ratatui-core/src/backend.rs new file mode 100644 index 00000000000..9eb29c283ad --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/backend.rs @@ -0,0 +1,405 @@ +#![warn(missing_docs)] +//! This module provides the backend implementations for different terminal libraries. +//! +//! It defines the [`Backend`] trait which is used to abstract over the specific terminal library +//! being used. +//! +//! Supported terminal backends: +//! - [Crossterm]: enable the `crossterm` feature (enabled by default) and use [`CrosstermBackend`] +//! - [Termion]: enable the `termion` feature and use [`TermionBackend`] +//! - [Termwiz]: enable the `termwiz` feature and use [`TermwizBackend`] +//! +//! Additionally, a [`TestBackend`] is provided for testing purposes. +//! +//! See the [Backend Comparison] section of the [Ratatui Website] for more details on the different +//! backends. +//! +//! Each backend supports a number of features, such as [raw mode](#raw-mode), [alternate +//! screen](#alternate-screen), and [mouse capture](#mouse-capture). These features are generally +//! not enabled by default, and must be enabled by the application before they can be used. See the +//! documentation for each backend for more details. +//! +//! Note: most applications should use the [`Terminal`] struct instead of directly calling methods +//! on the backend. +//! +//! # Example +//! +//! ```rust,ignore +//! use std::io::stdout; +//! +//! use ratatui::{backend::CrosstermBackend, Terminal}; +//! +//! let backend = CrosstermBackend::new(stdout()); +//! let mut terminal = Terminal::new(backend)?; +//! terminal.clear()?; +//! terminal.draw(|frame| { +//! // -- snip -- +//! })?; +//! # std::io::Result::Ok(()) +//! ``` +//! +//! See the the [Examples] directory for more examples. +//! +//! # Raw Mode +//! +//! Raw mode is a mode where the terminal does not perform any processing or handling of the input +//! and output. This means that features such as echoing input characters, line buffering, and +//! special character processing (e.g., CTRL-C for SIGINT) are disabled. This is useful for +//! applications that want to have complete control over the terminal input and output, processing +//! each keystroke themselves. +//! +//! For example, in raw mode, the terminal will not perform line buffering on the input, so the +//! application will receive each key press as it is typed, instead of waiting for the user to +//! press enter. This makes it suitable for real-time applications like text editors, +//! terminal-based games, and more. +//! +//! Each backend handles raw mode differently, so the behavior may vary depending on the backend +//! being used. Be sure to consult the backend's specific documentation for exact details on how it +//! implements raw mode. +//! +//! # Alternate Screen +//! +//! The alternate screen is a separate buffer that some terminals provide, distinct from the main +//! screen. When activated, the terminal will display the alternate screen, hiding the current +//! content of the main screen. Applications can write to this screen as if it were the regular +//! terminal display, but when the application exits, the terminal will switch back to the main +//! screen, and the contents of the alternate screen will be cleared. This is useful for +//! applications like text editors or terminal games that want to use the full terminal window +//! without disrupting the command line or other terminal content. +//! +//! This creates a seamless transition between the application and the regular terminal session, as +//! the content displayed before launching the application will reappear after the application +//! exits. +//! +//! Note that not all terminal emulators support the alternate screen, and even those that do may +//! handle it differently. As a result, the behavior may vary depending on the backend being used. +//! Always consult the specific backend's documentation to understand how it implements the +//! alternate screen. +//! +//! # Mouse Capture +//! +//! Mouse capture is a mode where the terminal captures mouse events such as clicks, scrolls, and +//! movement, and sends them to the application as special sequences or events. This enables the +//! application to handle and respond to mouse actions, providing a more interactive and graphical +//! user experience within the terminal. It's particularly useful for applications like +//! terminal-based games, text editors, or other programs that require more direct interaction from +//! the user. +//! +//! Each backend handles mouse capture differently, with variations in the types of events that can +//! be captured and how they are represented. As such, the behavior may vary depending on the +//! backend being used, and developers should consult the specific backend's documentation to +//! understand how it implements mouse capture. +//! +//! [`CrosstermBackend`]: https://docs.rs/ratatui/latest/ratatui/backend/struct.CrosstermBackend.html +//! [`TermionBackend`]: https://docs.rs/ratatui/latest/ratatui/backend/struct.TermionBackend.html +//! [`TermwizBackend`]: https://docs.rs/ratatui/latest/ratatui/backend/struct.TermwizBackend.html +//! [`Terminal`]: https://docs.rs/ratatui/latest/ratatui/struct.Terminal.html +//! [Crossterm]: https://crates.io/crates/crossterm +//! [Termion]: https://crates.io/crates/termion +//! [Termwiz]: https://crates.io/crates/termwiz +//! [Examples]: https://github.com/ratatui/ratatui/tree/main/ratatui/examples/README.md +//! [Backend Comparison]: https://ratatui.rs/concepts/backends/comparison/ +//! [Ratatui Website]: https://ratatui.rs + +use strum::{Display, EnumString}; + +use crate::buffer::Cell; +use crate::layout::{Position, Size}; + +mod test; +pub use self::test::TestBackend; + +/// Enum representing the different types of clearing operations that can be performed +/// on the terminal screen. +#[derive(Debug, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)] +pub enum ClearType { + /// Clear the entire screen. + All, + /// Clear everything after the cursor. + AfterCursor, + /// Clear everything before the cursor. + BeforeCursor, + /// Clear the current line. + CurrentLine, + /// Clear everything from the cursor until the next newline. + UntilNewLine, +} + +/// The window size in characters (columns / rows) as well as pixels. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +pub struct WindowSize { + /// Size of the window in characters (columns / rows). + pub columns_rows: Size, + /// Size of the window in pixels. + /// + /// The `pixels` fields may not be implemented by all terminals and return `0,0`. See + /// under section "Get and set window + /// size" / TIOCGWINSZ where the fields are commented as "unused". + pub pixels: Size, +} + +/// The `Backend` trait provides an abstraction over different terminal libraries. It defines the +/// methods required to draw content, manipulate the cursor, and clear the terminal screen. +/// +/// Most applications should not need to interact with the `Backend` trait directly as the +/// [`Terminal`] struct provides a higher level interface for interacting with the terminal. +/// +/// [`Terminal`]: https://docs.rs/ratatui/latest/ratatui/struct.Terminal.html +pub trait Backend { + /// Error type associated with this Backend. + type Error: core::error::Error; + + /// Draw the given content to the terminal screen. + /// + /// The content is provided as an iterator over `(u16, u16, &Cell)` tuples, where the first two + /// elements represent the x and y coordinates, and the third element is a reference to the + /// [`Cell`] to be drawn. + fn draw<'a, I>(&mut self, content: I) -> Result<(), Self::Error> + where + I: Iterator; + + /// Insert `n` line breaks to the terminal screen. + /// + /// This method is optional and may not be implemented by all backends. + fn append_lines(&mut self, _n: u16) -> Result<(), Self::Error> { + Ok(()) + } + + /// Hide the cursor on the terminal screen. + /// + /// + /// See also [`show_cursor`]. + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::backend::{TestBackend}; + /// # let mut backend = TestBackend::new(80, 25); + /// use ratatui::backend::Backend; + /// + /// backend.hide_cursor()?; + /// // do something with hidden cursor + /// backend.show_cursor()?; + /// # std::io::Result::Ok(()) + /// ``` + /// + /// [`show_cursor`]: Self::show_cursor + fn hide_cursor(&mut self) -> Result<(), Self::Error>; + + /// Show the cursor on the terminal screen. + /// + /// See [`hide_cursor`] for an example. + /// + /// [`hide_cursor`]: Self::hide_cursor + fn show_cursor(&mut self) -> Result<(), Self::Error>; + + /// Get the current cursor position on the terminal screen. + /// + /// The returned tuple contains the x and y coordinates of the cursor. + /// The origin (0, 0) is at the top left corner of the screen. + /// + /// See [`set_cursor_position`] for an example. + /// + /// [`set_cursor_position`]: Self::set_cursor_position + fn get_cursor_position(&mut self) -> Result; + + /// Set the cursor position on the terminal screen to the given x and y coordinates. + /// + /// The origin (0, 0) is at the top left corner of the screen. + /// + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::backend::{TestBackend}; + /// # let mut backend = TestBackend::new(80, 25); + /// use ratatui::{backend::Backend, layout::Position}; + /// + /// backend.set_cursor_position(Position { x: 10, y: 20 })?; + /// assert_eq!(backend.get_cursor_position()?, Position { x: 10, y: 20 }); + /// # std::io::Result::Ok(()) + /// ``` + fn set_cursor_position>(&mut self, position: P) -> Result<(), Self::Error>; + + /// Get the current cursor position on the terminal screen. + /// + /// The returned tuple contains the x and y coordinates of the cursor. The origin + /// (0, 0) is at the top left corner of the screen. + #[deprecated = "use `get_cursor_position()` instead which returns `Result`"] + fn get_cursor(&mut self) -> Result<(u16, u16), Self::Error> { + let Position { x, y } = self.get_cursor_position()?; + Ok((x, y)) + } + + /// Set the cursor position on the terminal screen to the given x and y coordinates. + /// + /// The origin (0, 0) is at the top left corner of the screen. + #[deprecated = "use `set_cursor_position((x, y))` instead which takes `impl Into`"] + fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), Self::Error> { + self.set_cursor_position(Position { x, y }) + } + + /// Clears the whole terminal screen + /// + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::backend::{TestBackend}; + /// # let mut backend = TestBackend::new(80, 25); + /// use ratatui::backend::Backend; + /// + /// backend.clear()?; + /// # std::io::Result::Ok(()) + /// ``` + fn clear(&mut self) -> Result<(), Self::Error>; + + /// Clears a specific region of the terminal specified by the [`ClearType`] parameter + /// + /// This method is optional and may not be implemented by all backends. The default + /// implementation calls [`clear`] if the `clear_type` is [`ClearType::All`] and returns an + /// error otherwise. + /// + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::{backend::{TestBackend}}; + /// # let mut backend = TestBackend::new(80, 25); + /// use ratatui::backend::{Backend, ClearType}; + /// + /// backend.clear_region(ClearType::All)?; + /// # std::io::Result::Ok(()) + /// ``` + /// + /// # Errors + /// + /// This method will return an error if the terminal screen could not be cleared. It will also + /// return an error if the `clear_type` is not supported by the backend. + /// + /// [`clear`]: Self::clear + fn clear_region(&mut self, clear_type: ClearType) -> Result<(), Self::Error>; + + /// Get the size of the terminal screen in columns/rows as a [`Size`]. + /// + /// The returned [`Size`] contains the width and height of the terminal screen. + /// + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::{backend::{TestBackend}}; + /// # let backend = TestBackend::new(80, 25); + /// use ratatui::{backend::Backend, layout::Size}; + /// + /// assert_eq!(backend.size()?, Size::new(80, 25)); + /// # Result::Ok(()) + /// ``` + fn size(&self) -> Result; + + /// Get the size of the terminal screen in columns/rows and pixels as a [`WindowSize`]. + /// + /// The reason for this not returning only the pixel size, given the redundancy with the + /// `size()` method, is that the underlying backends most likely get both values with one + /// syscall, and the user is also most likely to need columns and rows along with pixel size. + fn window_size(&mut self) -> Result; + + /// Flush any buffered content to the terminal screen. + fn flush(&mut self) -> Result<(), Self::Error>; + + /// Scroll a region of the screen upwards, where a region is specified by a (half-open) range + /// of rows. + /// + /// Each row in the region is replaced by the row `line_count` rows below it, except the bottom + /// `line_count` rows, which are replaced by empty rows. If `line_count` is equal to or larger + /// than the number of rows in the region, then all rows are replaced with empty rows. + /// + /// If the region includes row 0, then `line_count` rows are copied into the bottom of the + /// scrollback buffer. These rows are first taken from the old contents of the region, starting + /// from the top. If there aren't sufficient rows in the region, then the remainder are empty + /// rows. + /// + /// The position of the cursor afterwards is undefined. + /// + /// The behavior is designed to match what ANSI terminals do when scrolling regions are + /// established. With ANSI terminals, a scrolling region can be established with the "^[[X;Yr" + /// sequence, where X and Y define the lines of the region. The scrolling region can be reset + /// to be the whole screen with the "^[[r" sequence. + /// + /// When a scrolling region is established in an ANSI terminal, various operations' behaviors + /// are changed in such a way that the scrolling region acts like a "virtual screen". In + /// particular, the scrolling sequence "^[[NS", which scrolls lines up by a count of N. + /// + /// On an ANSI terminal, this method will probably translate to something like: + /// "^[[X;Yr^[[NS^[[r". That is, set the scrolling region, scroll up, then reset the scrolling + /// region. + /// + /// For examples of how this function is expected to work, refer to the tests for + /// [`TestBackend::scroll_region_up`]. + #[cfg(feature = "scrolling-regions")] + fn scroll_region_up( + &mut self, + region: core::ops::Range, + line_count: u16, + ) -> Result<(), Self::Error>; + + /// Scroll a region of the screen downwards, where a region is specified by a (half-open) range + /// of rows. + /// + /// Each row in the region is replaced by the row `line_count` rows above it, except the top + /// `line_count` rows, which are replaced by empty rows. If `line_count` is equal to or larger + /// than the number of rows in the region, then all rows are replaced with empty rows. + /// + /// The position of the cursor afterwards is undefined. + /// + /// See the documentation for [`Self::scroll_region_down`] for more information about how this + /// is expected to be implemented for ANSI terminals. All of that applies, except the ANSI + /// sequence to scroll down is "^[[NT". + /// + /// This function is asymmetrical with regards to the scrollback buffer. The reason is that + /// this how terminals seem to implement things. + /// + /// For examples of how this function is expected to work, refer to the tests for + /// [`TestBackend::scroll_region_down`]. + #[cfg(feature = "scrolling-regions")] + fn scroll_region_down( + &mut self, + region: core::ops::Range, + line_count: u16, + ) -> Result<(), Self::Error>; +} + +#[cfg(test)] +mod tests { + use alloc::string::ToString; + + use strum::ParseError; + + use super::*; + + #[test] + fn clear_type_tostring() { + assert_eq!(ClearType::All.to_string(), "All"); + assert_eq!(ClearType::AfterCursor.to_string(), "AfterCursor"); + assert_eq!(ClearType::BeforeCursor.to_string(), "BeforeCursor"); + assert_eq!(ClearType::CurrentLine.to_string(), "CurrentLine"); + assert_eq!(ClearType::UntilNewLine.to_string(), "UntilNewLine"); + } + + #[test] + fn clear_type_from_str() { + assert_eq!("All".parse::(), Ok(ClearType::All)); + assert_eq!( + "AfterCursor".parse::(), + Ok(ClearType::AfterCursor) + ); + assert_eq!( + "BeforeCursor".parse::(), + Ok(ClearType::BeforeCursor) + ); + assert_eq!( + "CurrentLine".parse::(), + Ok(ClearType::CurrentLine) + ); + assert_eq!( + "UntilNewLine".parse::(), + Ok(ClearType::UntilNewLine) + ); + assert_eq!("".parse::(), Err(ParseError::VariantNotFound)); + } +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/backend/test.rs b/vendor/ratatui-jcode/ratatui-core/src/backend/test.rs new file mode 100644 index 00000000000..a99b058d7a0 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/backend/test.rs @@ -0,0 +1,1077 @@ +//! This module provides the `TestBackend` implementation for the [`Backend`] trait. +//! It is used in the integration tests to verify the correctness of the library. + +use alloc::string::String; +use alloc::vec; +use core::fmt::{self, Write}; +use core::iter; + +use unicode_width::UnicodeWidthStr; + +use crate::backend::{Backend, ClearType, WindowSize}; +use crate::buffer::{Buffer, Cell}; +use crate::layout::{Position, Rect, Size}; + +/// A [`Backend`] implementation used for integration testing that renders to an memory buffer. +/// +/// Note: that although many of the integration and unit tests in ratatui are written using this +/// backend, it is preferable to write unit tests for widgets directly against the buffer rather +/// than using this backend. This backend is intended for integration tests that test the entire +/// terminal UI. +/// +/// # Example +/// +/// ```rust,ignore +/// use ratatui::backend::{Backend, TestBackend}; +/// +/// let mut backend = TestBackend::new(10, 2); +/// backend.clear()?; +/// backend.assert_buffer_lines([" "; 2]); +/// # Result::Ok(()) +/// ``` +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct TestBackend { + buffer: Buffer, + scrollback: Buffer, + cursor: bool, + pos: (u16, u16), +} + +/// Returns a string representation of the given buffer for debugging purpose. +/// +/// This function is used to visualize the buffer content in a human-readable format. +/// It iterates through the buffer content and appends each cell's symbol to the view string. +/// If a cell is hidden by a multi-width symbol, it is added to the overwritten vector and +/// displayed at the end of the line. +fn buffer_view(buffer: &Buffer) -> String { + let mut view = String::with_capacity(buffer.content.len() + buffer.area.height as usize * 3); + for cells in buffer.content.chunks(buffer.area.width as usize) { + let mut overwritten = vec![]; + let mut skip: usize = 0; + view.push('"'); + for (x, c) in cells.iter().enumerate() { + if skip == 0 { + view.push_str(c.symbol()); + } else { + overwritten.push((x, c.symbol())); + } + skip = core::cmp::max(skip, c.symbol().width()).saturating_sub(1); + } + view.push('"'); + if !overwritten.is_empty() { + write!(&mut view, " Hidden by multi-width symbols: {overwritten:?}").unwrap(); + } + view.push('\n'); + } + view +} + +impl TestBackend { + /// Creates a new `TestBackend` with the specified width and height. + pub fn new(width: u16, height: u16) -> Self { + Self { + buffer: Buffer::empty(Rect::new(0, 0, width, height)), + scrollback: Buffer::empty(Rect::new(0, 0, width, 0)), + cursor: false, + pos: (0, 0), + } + } + + /// Creates a new `TestBackend` with the specified lines as the initial screen state. + /// + /// The backend's screen size is determined from the initial lines. + #[must_use] + pub fn with_lines<'line, Lines>(lines: Lines) -> Self + where + Lines: IntoIterator, + Lines::Item: Into>, + { + let buffer = Buffer::with_lines(lines); + let scrollback = Buffer::empty(Rect { + width: buffer.area.width, + ..Rect::ZERO + }); + Self { + buffer, + scrollback, + cursor: false, + pos: (0, 0), + } + } + + /// Returns a reference to the internal buffer of the `TestBackend`. + pub const fn buffer(&self) -> &Buffer { + &self.buffer + } + + /// Returns a reference to the internal scrollback buffer of the `TestBackend`. + /// + /// The scrollback buffer represents the part of the screen that is currently hidden from view, + /// but that could be accessed by scrolling back in the terminal's history. This would normally + /// be done using the terminal's scrollbar or an equivalent keyboard shortcut. + /// + /// The scrollback buffer starts out empty. Lines are appended when they scroll off the top of + /// the main buffer. This happens when lines are appended to the bottom of the main buffer + /// using [`Backend::append_lines`]. + /// + /// The scrollback buffer has a maximum height of [`u16::MAX`]. If lines are appended to the + /// bottom of the scrollback buffer when it is at its maximum height, a corresponding number of + /// lines will be removed from the top. + pub const fn scrollback(&self) -> &Buffer { + &self.scrollback + } + + /// Resizes the `TestBackend` to the specified width and height. + pub fn resize(&mut self, width: u16, height: u16) { + self.buffer.resize(Rect::new(0, 0, width, height)); + let scrollback_height = self.scrollback.area.height; + self.scrollback + .resize(Rect::new(0, 0, width, scrollback_height)); + } + + /// Asserts that the `TestBackend`'s buffer is equal to the expected buffer. + /// + /// This is a shortcut for `assert_eq!(self.buffer(), &expected)`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual buffers. + #[expect(deprecated)] + #[track_caller] + pub fn assert_buffer(&self, expected: &Buffer) { + // TODO: use assert_eq!() + crate::assert_buffer_eq!(&self.buffer, expected); + } + + /// Asserts that the `TestBackend`'s scrollback buffer is equal to the expected buffer. + /// + /// This is a shortcut for `assert_eq!(self.scrollback(), &expected)`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual buffers. + #[track_caller] + pub fn assert_scrollback(&self, expected: &Buffer) { + assert_eq!(&self.scrollback, expected); + } + + /// Asserts that the `TestBackend`'s scrollback buffer is empty. + /// + /// # Panics + /// + /// When the scrollback buffer is not equal, a panic occurs with a detailed error message + /// showing the differences between the expected and actual buffers. + pub fn assert_scrollback_empty(&self) { + let expected = Buffer { + area: Rect { + width: self.scrollback.area.width, + ..Rect::ZERO + }, + content: vec![], + }; + self.assert_scrollback(&expected); + } + + /// Asserts that the `TestBackend`'s buffer is equal to the expected lines. + /// + /// This is a shortcut for `assert_eq!(self.buffer(), &Buffer::with_lines(expected))`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual buffers. + #[track_caller] + pub fn assert_buffer_lines<'line, Lines>(&self, expected: Lines) + where + Lines: IntoIterator, + Lines::Item: Into>, + { + self.assert_buffer(&Buffer::with_lines(expected)); + } + + /// Asserts that the `TestBackend`'s scrollback buffer is equal to the expected lines. + /// + /// This is a shortcut for `assert_eq!(self.scrollback(), &Buffer::with_lines(expected))`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual buffers. + #[track_caller] + pub fn assert_scrollback_lines<'line, Lines>(&self, expected: Lines) + where + Lines: IntoIterator, + Lines::Item: Into>, + { + self.assert_scrollback(&Buffer::with_lines(expected)); + } + + /// Asserts that the `TestBackend`'s cursor position is equal to the expected one. + /// + /// This is a shortcut for `assert_eq!(self.get_cursor_position().unwrap(), expected)`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual position. + #[track_caller] + pub fn assert_cursor_position>(&mut self, position: P) { + let actual = self.get_cursor_position().unwrap(); + assert_eq!(actual, position.into()); + } +} + +impl fmt::Display for TestBackend { + /// Formats the `TestBackend` for display by calling the `buffer_view` function + /// on its internal buffer. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", buffer_view(&self.buffer)) + } +} + +type Result = core::result::Result; + +impl Backend for TestBackend { + type Error = core::convert::Infallible; + + fn draw<'a, I>(&mut self, content: I) -> Result<()> + where + I: Iterator, + { + for (x, y, c) in content { + self.buffer[(x, y)] = c.clone(); + } + Ok(()) + } + + fn hide_cursor(&mut self) -> Result<()> { + self.cursor = false; + Ok(()) + } + + fn show_cursor(&mut self) -> Result<()> { + self.cursor = true; + Ok(()) + } + + fn get_cursor_position(&mut self) -> Result { + Ok(self.pos.into()) + } + + fn set_cursor_position>(&mut self, position: P) -> Result<()> { + self.pos = position.into().into(); + Ok(()) + } + + fn clear(&mut self) -> Result<()> { + self.buffer.reset(); + Ok(()) + } + + fn clear_region(&mut self, clear_type: ClearType) -> Result<()> { + let region = match clear_type { + ClearType::All => return self.clear(), + ClearType::AfterCursor => { + let index = self.buffer.index_of(self.pos.0, self.pos.1) + 1; + &mut self.buffer.content[index..] + } + ClearType::BeforeCursor => { + let index = self.buffer.index_of(self.pos.0, self.pos.1); + &mut self.buffer.content[..index] + } + ClearType::CurrentLine => { + let line_start_index = self.buffer.index_of(0, self.pos.1); + let line_end_index = self.buffer.index_of(self.buffer.area.width - 1, self.pos.1); + &mut self.buffer.content[line_start_index..=line_end_index] + } + ClearType::UntilNewLine => { + let index = self.buffer.index_of(self.pos.0, self.pos.1); + let line_end_index = self.buffer.index_of(self.buffer.area.width - 1, self.pos.1); + &mut self.buffer.content[index..=line_end_index] + } + }; + for cell in region { + cell.reset(); + } + Ok(()) + } + + /// Inserts n line breaks at the current cursor position. + /// + /// After the insertion, the cursor x position will be incremented by 1 (unless it's already + /// at the end of line). This is a common behaviour of terminals in raw mode. + /// + /// If the number of lines to append is fewer than the number of lines in the buffer after the + /// cursor y position then the cursor is moved down by n rows. + /// + /// If the number of lines to append is greater than the number of lines in the buffer after + /// the cursor y position then that number of empty lines (at most the buffer's height in this + /// case but this limit is instead replaced with scrolling in most backend implementations) will + /// be added after the current position and the cursor will be moved to the last row. + fn append_lines(&mut self, line_count: u16) -> Result<()> { + let Position { x: cur_x, y: cur_y } = self.get_cursor_position()?; + let Rect { width, height, .. } = self.buffer.area; + + // the next column ensuring that we don't go past the last column + let new_cursor_x = cur_x.saturating_add(1).min(width.saturating_sub(1)); + + let max_y = height.saturating_sub(1); + let lines_after_cursor = max_y.saturating_sub(cur_y); + + if line_count > lines_after_cursor { + // We need to insert blank lines at the bottom and scroll the lines from the top into + // scrollback. + let scroll_by: usize = (line_count - lines_after_cursor).into(); + let width: usize = self.buffer.area.width.into(); + let cells_to_scrollback = self.buffer.content.len().min(width * scroll_by); + + append_to_scrollback( + &mut self.scrollback, + self.buffer.content.splice( + 0..cells_to_scrollback, + iter::repeat_with(Default::default).take(cells_to_scrollback), + ), + ); + self.buffer.content.rotate_left(cells_to_scrollback); + append_to_scrollback( + &mut self.scrollback, + iter::repeat_with(Default::default).take(width * scroll_by - cells_to_scrollback), + ); + } + + let new_cursor_y = cur_y.saturating_add(line_count).min(max_y); + self.set_cursor_position(Position::new(new_cursor_x, new_cursor_y))?; + + Ok(()) + } + + fn size(&self) -> Result { + Ok(self.buffer.area.as_size()) + } + + fn window_size(&mut self) -> Result { + // Some arbitrary window pixel size, probably doesn't need much testing. + const WINDOW_PIXEL_SIZE: Size = Size { + width: 640, + height: 480, + }; + Ok(WindowSize { + columns_rows: self.buffer.area.as_size(), + pixels: WINDOW_PIXEL_SIZE, + }) + } + + fn flush(&mut self) -> Result<()> { + Ok(()) + } + + #[cfg(feature = "scrolling-regions")] + fn scroll_region_up(&mut self, region: core::ops::Range, scroll_by: u16) -> Result<()> { + let width: usize = self.buffer.area.width.into(); + let cell_region_start = width * region.start.min(self.buffer.area.height) as usize; + let cell_region_end = width * region.end.min(self.buffer.area.height) as usize; + let cell_region_len = cell_region_end - cell_region_start; + let cells_to_scroll_by = width * scroll_by as usize; + + // Deal with the simple case where nothing needs to be copied into scrollback. + if cell_region_start > 0 { + if cells_to_scroll_by >= cell_region_len { + // The scroll amount is large enough to clear the whole region. + self.buffer.content[cell_region_start..cell_region_end].fill_with(Default::default); + } else { + // Scroll up by rotating, then filling in the bottom with empty cells. + self.buffer.content[cell_region_start..cell_region_end] + .rotate_left(cells_to_scroll_by); + self.buffer.content[cell_region_end - cells_to_scroll_by..cell_region_end] + .fill_with(Default::default); + } + return Ok(()); + } + + // The rows inserted into the scrollback will first come from the buffer, and if that is + // insufficient, will then be blank rows. + let cells_from_region = cell_region_len.min(cells_to_scroll_by); + append_to_scrollback( + &mut self.scrollback, + self.buffer.content.splice( + 0..cells_from_region, + iter::repeat_with(Default::default).take(cells_from_region), + ), + ); + if cells_to_scroll_by < cell_region_len { + // Rotate the remaining cells to the front of the region. + self.buffer.content[cell_region_start..cell_region_end].rotate_left(cells_from_region); + } else { + // Splice cleared out the region. Insert empty rows in scrollback. + append_to_scrollback( + &mut self.scrollback, + iter::repeat_with(Default::default).take(cells_to_scroll_by - cell_region_len), + ); + } + Ok(()) + } + + #[cfg(feature = "scrolling-regions")] + fn scroll_region_down(&mut self, region: core::ops::Range, scroll_by: u16) -> Result<()> { + let width: usize = self.buffer.area.width.into(); + let cell_region_start = width * region.start.min(self.buffer.area.height) as usize; + let cell_region_end = width * region.end.min(self.buffer.area.height) as usize; + let cell_region_len = cell_region_end - cell_region_start; + let cells_to_scroll_by = width * scroll_by as usize; + + if cells_to_scroll_by >= cell_region_len { + // The scroll amount is large enough to clear the whole region. + self.buffer.content[cell_region_start..cell_region_end].fill_with(Default::default); + } else { + // Scroll up by rotating, then filling in the top with empty cells. + self.buffer.content[cell_region_start..cell_region_end] + .rotate_right(cells_to_scroll_by); + self.buffer.content[cell_region_start..cell_region_start + cells_to_scroll_by] + .fill_with(Default::default); + } + Ok(()) + } +} + +/// Append the provided cells to the bottom of a scrollback buffer. The number of cells must be a +/// multiple of the buffer's width. If the scrollback buffer ends up larger than 65535 lines tall, +/// then lines will be removed from the top to get it down to size. +fn append_to_scrollback(scrollback: &mut Buffer, cells: impl IntoIterator) { + scrollback.content.extend(cells); + let width = scrollback.area.width as usize; + let new_height = (scrollback.content.len() / width).min(u16::MAX as usize); + let keep_from = scrollback + .content + .len() + .saturating_sub(width * u16::MAX as usize); + scrollback.content.drain(0..keep_from); + scrollback.area.height = new_height as u16; +} + +#[cfg(test)] +mod tests { + use alloc::format; + + use itertools::Itertools as _; + + use super::*; + + #[test] + fn new() { + assert_eq!( + TestBackend::new(10, 2), + TestBackend { + buffer: Buffer::with_lines([" "; 2]), + scrollback: Buffer::empty(Rect::new(0, 0, 10, 0)), + cursor: false, + pos: (0, 0), + } + ); + } + #[test] + fn test_buffer_view() { + let buffer = Buffer::with_lines(["aaaa"; 2]); + assert_eq!(buffer_view(&buffer), "\"aaaa\"\n\"aaaa\"\n"); + } + + #[test] + fn buffer_view_with_overwrites() { + let multi_byte_char = "👨‍👩‍👧‍👦"; // renders 2 wide + let buffer = Buffer::with_lines([multi_byte_char]); + assert_eq!( + buffer_view(&buffer), + format!( + r#""{multi_byte_char}" Hidden by multi-width symbols: [(1, " ")] +"#, + ) + ); + } + + #[test] + fn buffer() { + let backend = TestBackend::new(10, 2); + backend.assert_buffer_lines([" "; 2]); + } + + #[test] + fn resize() { + let mut backend = TestBackend::new(10, 2); + backend.resize(5, 5); + backend.assert_buffer_lines([" "; 5]); + } + + #[test] + fn assert_buffer() { + let backend = TestBackend::new(10, 2); + backend.assert_buffer_lines([" "; 2]); + } + + #[test] + #[should_panic = "buffer contents not equal"] + fn assert_buffer_panics() { + let backend = TestBackend::new(10, 2); + backend.assert_buffer_lines(["aaaaaaaaaa"; 2]); + } + + #[test] + #[should_panic = "assertion `left == right` failed"] + fn assert_scrollback_panics() { + let backend = TestBackend::new(10, 2); + backend.assert_scrollback_lines(["aaaaaaaaaa"; 2]); + } + + #[test] + fn display() { + let backend = TestBackend::new(10, 2); + assert_eq!(format!("{backend}"), "\" \"\n\" \"\n"); + } + + #[test] + fn draw() { + let mut backend = TestBackend::new(10, 2); + let cell = Cell::new("a"); + backend.draw([(0, 0, &cell)].into_iter()).unwrap(); + backend.draw([(0, 1, &cell)].into_iter()).unwrap(); + backend.assert_buffer_lines(["a "; 2]); + } + + #[test] + fn hide_cursor() { + let mut backend = TestBackend::new(10, 2); + backend.hide_cursor().unwrap(); + assert!(!backend.cursor); + } + + #[test] + fn show_cursor() { + let mut backend = TestBackend::new(10, 2); + backend.show_cursor().unwrap(); + assert!(backend.cursor); + } + + #[test] + fn get_cursor_position() { + let mut backend = TestBackend::new(10, 2); + assert_eq!(backend.get_cursor_position().unwrap(), Position::ORIGIN); + } + + #[test] + fn assert_cursor_position() { + let mut backend = TestBackend::new(10, 2); + backend.assert_cursor_position(Position::ORIGIN); + } + + #[test] + fn set_cursor_position() { + let mut backend = TestBackend::new(10, 10); + backend + .set_cursor_position(Position { x: 5, y: 5 }) + .unwrap(); + assert_eq!(backend.pos, (5, 5)); + } + + #[test] + fn clear() { + let mut backend = TestBackend::new(4, 2); + let cell = Cell::new("a"); + backend.draw([(0, 0, &cell)].into_iter()).unwrap(); + backend.draw([(0, 1, &cell)].into_iter()).unwrap(); + backend.clear().unwrap(); + backend.assert_buffer_lines([" ", " "]); + } + + #[test] + fn clear_region_all() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend.clear_region(ClearType::All).unwrap(); + backend.assert_buffer_lines([ + " ", + " ", + " ", + " ", + " ", + ]); + } + + #[test] + fn clear_region_after_cursor() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend + .set_cursor_position(Position { x: 3, y: 2 }) + .unwrap(); + backend.clear_region(ClearType::AfterCursor).unwrap(); + backend.assert_buffer_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaa ", + " ", + " ", + ]); + } + + #[test] + fn clear_region_before_cursor() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend + .set_cursor_position(Position { x: 5, y: 3 }) + .unwrap(); + backend.clear_region(ClearType::BeforeCursor).unwrap(); + backend.assert_buffer_lines([ + " ", + " ", + " ", + " aaaaa", + "aaaaaaaaaa", + ]); + } + + #[test] + fn clear_region_current_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend + .set_cursor_position(Position { x: 3, y: 1 }) + .unwrap(); + backend.clear_region(ClearType::CurrentLine).unwrap(); + backend.assert_buffer_lines([ + "aaaaaaaaaa", + " ", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + } + + #[test] + fn clear_region_until_new_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend + .set_cursor_position(Position { x: 3, y: 0 }) + .unwrap(); + backend.clear_region(ClearType::UntilNewLine).unwrap(); + backend.assert_buffer_lines([ + "aaa ", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + } + + #[test] + fn append_lines_not_at_last_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend.set_cursor_position(Position::ORIGIN).unwrap(); + + // If the cursor is not at the last line in the terminal the addition of a + // newline simply moves the cursor down and to the right + + backend.append_lines(1).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 1 }); + + backend.append_lines(1).unwrap(); + backend.assert_cursor_position(Position { x: 2, y: 2 }); + + backend.append_lines(1).unwrap(); + backend.assert_cursor_position(Position { x: 3, y: 3 }); + + backend.append_lines(1).unwrap(); + backend.assert_cursor_position(Position { x: 4, y: 4 }); + + // As such the buffer should remain unchanged + backend.assert_buffer_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + backend.assert_scrollback_empty(); + } + + #[test] + fn append_lines_at_last_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + // If the cursor is at the last line in the terminal the addition of a + // newline will scroll the contents of the buffer + backend + .set_cursor_position(Position { x: 0, y: 4 }) + .unwrap(); + + backend.append_lines(1).unwrap(); + + backend.assert_buffer_lines([ + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + " ", + ]); + backend.assert_scrollback_lines(["aaaaaaaaaa"]); + + // It also moves the cursor to the right, as is common of the behaviour of + // terminals in raw-mode + backend.assert_cursor_position(Position { x: 1, y: 4 }); + } + + #[test] + fn append_multiple_lines_not_at_last_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend.set_cursor_position(Position::ORIGIN).unwrap(); + + // If the cursor is not at the last line in the terminal the addition of multiple + // newlines simply moves the cursor n lines down and to the right by 1 + + backend.append_lines(4).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + // As such the buffer should remain unchanged + backend.assert_buffer_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + backend.assert_scrollback_empty(); + } + + #[test] + fn append_multiple_lines_past_last_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend + .set_cursor_position(Position { x: 0, y: 3 }) + .unwrap(); + + backend.append_lines(3).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + backend.assert_buffer_lines([ + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + " ", + " ", + ]); + backend.assert_scrollback_lines(["aaaaaaaaaa", "bbbbbbbbbb"]); + } + + #[test] + fn append_multiple_lines_where_cursor_at_end_appends_height_lines() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend + .set_cursor_position(Position { x: 0, y: 4 }) + .unwrap(); + + backend.append_lines(5).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + backend.assert_buffer_lines([ + " ", + " ", + " ", + " ", + " ", + ]); + backend.assert_scrollback_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + } + + #[test] + fn append_multiple_lines_where_cursor_appends_height_lines() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend.set_cursor_position(Position::ORIGIN).unwrap(); + + backend.append_lines(5).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + backend.assert_buffer_lines([ + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + " ", + ]); + backend.assert_scrollback_lines(["aaaaaaaaaa"]); + } + + #[test] + fn append_multiple_lines_where_cursor_at_end_appends_more_than_height_lines() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend + .set_cursor_position(Position { x: 0, y: 4 }) + .unwrap(); + + backend.append_lines(8).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + backend.assert_buffer_lines([ + " ", + " ", + " ", + " ", + " ", + ]); + backend.assert_scrollback_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + " ", + " ", + " ", + ]); + } + + #[test] + fn append_lines_truncates_beyond_u16_max() -> Result<()> { + let mut backend = TestBackend::new(10, 5); + + // Fill the scrollback with 65535 + 10 lines. + let row_count = u16::MAX as usize + 10; + for row in 0..=row_count { + if row > 4 { + backend.set_cursor_position(Position { x: 0, y: 4 })?; + backend.append_lines(1)?; + } + let cells = format!("{row:>10}").chars().map(Cell::from).collect_vec(); + let content = cells + .iter() + .enumerate() + .map(|(column, cell)| (column as u16, 4.min(row) as u16, cell)); + backend.draw(content)?; + } + + // check that the buffer contains the last 5 lines appended + backend.assert_buffer_lines([ + " 65541", + " 65542", + " 65543", + " 65544", + " 65545", + ]); + + // TODO: ideally this should be something like: + // let lines = (6..=65545).map(|row| format!("{row:>10}")); + // backend.assert_scrollback_lines(lines); + // but there's some truncation happening in Buffer::with_lines that needs to be fixed + assert_eq!( + Buffer { + area: Rect::new(0, 0, 10, 5), + content: backend.scrollback.content[0..10 * 5].to_vec(), + }, + Buffer::with_lines([ + " 6", + " 7", + " 8", + " 9", + " 10", + ]), + "first 5 lines of scrollback should have been truncated" + ); + + assert_eq!( + Buffer { + area: Rect::new(0, 0, 10, 5), + content: backend.scrollback.content[10 * 65530..10 * 65535].to_vec(), + }, + Buffer::with_lines([ + " 65536", + " 65537", + " 65538", + " 65539", + " 65540", + ]), + "last 5 lines of scrollback should have been appended" + ); + + // These checks come after the content checks as otherwise we won't see the failing content + // when these checks fail. + // Make sure the scrollback is the right size. + assert_eq!(backend.scrollback.area.width, 10); + assert_eq!(backend.scrollback.area.height, 65535); + assert_eq!(backend.scrollback.content.len(), 10 * 65535); + Ok(()) + } + + #[test] + fn size() { + let backend = TestBackend::new(10, 2); + assert_eq!(backend.size().unwrap(), Size::new(10, 2)); + } + + #[test] + fn flush() { + let mut backend = TestBackend::new(10, 2); + backend.flush().unwrap(); + } + + #[cfg(feature = "scrolling-regions")] + mod scrolling_regions { + use rstest::rstest; + + use super::*; + + const A: &str = "aaaa"; + const B: &str = "bbbb"; + const C: &str = "cccc"; + const D: &str = "dddd"; + const E: &str = "eeee"; + const S: &str = " "; + + #[rstest] + #[case([A, B, C, D, E], 0..5, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..5, 2, [A, B], [C, D, E, S, S])] + #[case([A, B, C, D, E], 0..5, 5, [A, B, C, D, E], [S, S, S, S, S])] + #[case([A, B, C, D, E], 0..5, 7, [A, B, C, D, E, S, S], [S, S, S, S, S])] + #[case([A, B, C, D, E], 0..3, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..3, 2, [A, B], [C, S, S, D, E])] + #[case([A, B, C, D, E], 0..3, 3, [A, B, C], [S, S, S, D, E])] + #[case([A, B, C, D, E], 0..3, 4, [A, B, C, S], [S, S, S, D, E])] + #[case([A, B, C, D, E], 1..4, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 1..4, 2, [], [A, D, S, S, E])] + #[case([A, B, C, D, E], 1..4, 3, [], [A, S, S, S, E])] + #[case([A, B, C, D, E], 1..4, 4, [], [A, S, S, S, E])] + #[case([A, B, C, D, E], 0..0, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..0, 2, [S, S], [A, B, C, D, E])] + #[case([A, B, C, D, E], 2..2, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 2..2, 2, [], [A, B, C, D, E])] + fn scroll_region_up( + #[case] initial_screen: [&'static str; L], + #[case] range: core::ops::Range, + #[case] scroll_by: u16, + #[case] expected_scrollback: [&'static str; M], + #[case] expected_buffer: [&'static str; N], + ) { + let mut backend = TestBackend::with_lines(initial_screen); + backend.scroll_region_up(range, scroll_by).unwrap(); + if expected_scrollback.is_empty() { + backend.assert_scrollback_empty(); + } else { + backend.assert_scrollback_lines(expected_scrollback); + } + backend.assert_buffer_lines(expected_buffer); + } + + #[rstest] + #[case([A, B, C, D, E], 0..5, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..5, 2, [S, S, A, B, C])] + #[case([A, B, C, D, E], 0..5, 5, [S, S, S, S, S])] + #[case([A, B, C, D, E], 0..5, 7, [S, S, S, S, S])] + #[case([A, B, C, D, E], 0..3, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..3, 2, [S, S, A, D, E])] + #[case([A, B, C, D, E], 0..3, 3, [S, S, S, D, E])] + #[case([A, B, C, D, E], 0..3, 4, [S, S, S, D, E])] + #[case([A, B, C, D, E], 1..4, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 1..4, 2, [A, S, S, B, E])] + #[case([A, B, C, D, E], 1..4, 3, [A, S, S, S, E])] + #[case([A, B, C, D, E], 1..4, 4, [A, S, S, S, E])] + #[case([A, B, C, D, E], 0..0, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..0, 2, [A, B, C, D, E])] + #[case([A, B, C, D, E], 2..2, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 2..2, 2, [A, B, C, D, E])] + fn scroll_region_down( + #[case] initial_screen: [&'static str; M], + #[case] range: core::ops::Range, + #[case] scroll_by: u16, + #[case] expected_buffer: [&'static str; N], + ) { + let mut backend = TestBackend::with_lines(initial_screen); + backend.scroll_region_down(range, scroll_by).unwrap(); + backend.assert_scrollback_empty(); + backend.assert_buffer_lines(expected_buffer); + } + } +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/buffer.rs b/vendor/ratatui-jcode/ratatui-core/src/buffer.rs new file mode 100644 index 00000000000..e7ac4d741e0 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/buffer.rs @@ -0,0 +1,9 @@ +#![warn(missing_docs)] +//! A module for the [`Buffer`] and [`Cell`] types. + +mod assert; +mod buffer; +mod cell; + +pub use buffer::Buffer; +pub use cell::Cell; diff --git a/vendor/ratatui-jcode/ratatui-core/src/buffer/assert.rs b/vendor/ratatui-jcode/ratatui-core/src/buffer/assert.rs new file mode 100644 index 00000000000..c4afafb7888 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/buffer/assert.rs @@ -0,0 +1,71 @@ +/// Assert that two buffers are equal by comparing their areas and content. +/// +/// # Panics +/// When the buffers differ this method panics and displays the differences similar to +/// `assert_eq!()`. +#[deprecated = "use `assert_eq!(&actual, &expected)`"] +#[macro_export] +macro_rules! assert_buffer_eq { + ($actual_expr:expr, $expected_expr:expr) => { + match (&$actual_expr, &$expected_expr) { + (actual, expected) => { + assert!( + actual.area == expected.area, + "buffer areas not equal\nexpected: {expected:?}\nactual: {actual:?}", + ); + let nice_diff = expected + .diff(actual) + .into_iter() + .enumerate() + .map(|(i, (x, y, cell))| { + let expected_cell = &expected[(x, y)]; + ::alloc::format!("{i}: at ({x}, {y})\n expected: {expected_cell:?}\n actual: {cell:?}") + }) + .collect::<::alloc::vec::Vec<::alloc::string::String>>() + .join("\n"); + assert!( + nice_diff.is_empty(), + "buffer contents not equal\nexpected: {expected:?}\nactual: {actual:?}\ndiff:\n{nice_diff}", + ); + // shouldn't get here, but this guards against future behavior + // that changes equality but not area or content + assert_eq!( + actual, expected, + "buffers are not equal in an unexpected way. Please open an issue about this." + ); + } + } + }; +} + +#[expect(deprecated)] +#[cfg(test)] +mod tests { + use crate::buffer::Buffer; + use crate::layout::Rect; + use crate::style::{Color, Style}; + + #[test] + fn assert_buffer_eq_does_not_panic_on_equal_buffers() { + let buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + let other_buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + assert_buffer_eq!(buffer, other_buffer); + } + + #[should_panic = "buffer areas not equal"] + #[test] + fn assert_buffer_eq_panics_on_unequal_area() { + let buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + let other_buffer = Buffer::empty(Rect::new(0, 0, 6, 1)); + assert_buffer_eq!(buffer, other_buffer); + } + + #[should_panic = "buffer contents not equal"] + #[test] + fn assert_buffer_eq_panics_on_unequal_style() { + let buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + let mut other_buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + other_buffer.set_string(0, 0, " ", Style::default().fg(Color::Red)); + assert_buffer_eq!(buffer, other_buffer); + } +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/buffer/buffer.rs b/vendor/ratatui-jcode/ratatui-core/src/buffer/buffer.rs new file mode 100644 index 00000000000..9971a4ae30e --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/buffer/buffer.rs @@ -0,0 +1,1414 @@ +use alloc::vec; +use alloc::vec::Vec; +use core::ops::{Index, IndexMut}; +use core::{cmp, fmt}; + +use unicode_segmentation::UnicodeSegmentation; +use unicode_width::UnicodeWidthStr; + +use crate::buffer::Cell; +use crate::layout::{Position, Rect}; +use crate::style::Style; +use crate::text::{Line, Span}; + +/// A buffer that maps to the desired content of the terminal after the draw call +/// +/// No widget in the library interacts directly with the terminal. Instead each of them is required +/// to draw their state to an intermediate buffer. It is basically a grid where each cell contains +/// a grapheme, a foreground color and a background color. This grid will then be used to output +/// the appropriate escape sequences and characters to draw the UI as the user has defined it. +/// +/// # Examples: +/// +/// ``` +/// use ratatui_core::buffer::{Buffer, Cell}; +/// use ratatui_core::layout::{Position, Rect}; +/// use ratatui_core::style::{Color, Style}; +/// +/// # fn foo() -> Option<()> { +/// let mut buf = Buffer::empty(Rect { +/// x: 0, +/// y: 0, +/// width: 10, +/// height: 5, +/// }); +/// +/// // indexing using Position +/// buf[Position { x: 0, y: 0 }].set_symbol("A"); +/// assert_eq!(buf[Position { x: 0, y: 0 }].symbol(), "A"); +/// +/// // indexing using (x, y) tuple (which is converted to Position) +/// buf[(0, 1)].set_symbol("B"); +/// assert_eq!(buf[(0, 1)].symbol(), "x"); +/// +/// // getting an Option instead of panicking if the position is outside the buffer +/// let cell = buf.cell_mut(Position { x: 0, y: 2 })?; +/// cell.set_symbol("C"); +/// +/// let cell = buf.cell(Position { x: 0, y: 2 })?; +/// assert_eq!(cell.symbol(), "C"); +/// +/// buf.set_string( +/// 3, +/// 0, +/// "string", +/// Style::default().fg(Color::Red).bg(Color::White), +/// ); +/// let cell = &buf[(5, 0)]; // cannot move out of buf, so we borrow it +/// assert_eq!(cell.symbol(), "r"); +/// assert_eq!(cell.fg, Color::Red); +/// assert_eq!(cell.bg, Color::White); +/// # Some(()) +/// # } +/// ``` +#[derive(Default, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct Buffer { + /// The area represented by this buffer + pub area: Rect, + /// The content of the buffer. The length of this Vec should always be equal to area.width * + /// area.height + pub content: Vec, +} + +impl Buffer { + /// Returns a Buffer with all cells set to the default one + #[must_use] + pub fn empty(area: Rect) -> Self { + Self::filled(area, Cell::EMPTY) + } + + /// Returns a Buffer with all cells initialized with the attributes of the given Cell + #[must_use] + pub fn filled(area: Rect, cell: Cell) -> Self { + let size = area.area() as usize; + let content = vec![cell; size]; + Self { area, content } + } + + /// Returns a Buffer containing the given lines + #[must_use] + pub fn with_lines<'a, Iter>(lines: Iter) -> Self + where + Iter: IntoIterator, + Iter::Item: Into>, + { + let lines = lines.into_iter().map(Into::into).collect::>(); + let height = lines.len() as u16; + let width = lines.iter().map(Line::width).max().unwrap_or_default() as u16; + let mut buffer = Self::empty(Rect::new(0, 0, width, height)); + for (y, line) in lines.iter().enumerate() { + buffer.set_line(0, y as u16, line, width); + } + buffer + } + + /// Returns the content of the buffer as a slice + pub fn content(&self) -> &[Cell] { + &self.content + } + + /// Returns the area covered by this buffer + pub const fn area(&self) -> &Rect { + &self.area + } + + /// Returns a reference to the [`Cell`] at the given coordinates + /// + /// Callers should use [`Buffer[]`](Self::index) or [`Buffer::cell`] instead of this method. + /// + /// Note: idiomatically methods named `get` usually return `Option<&T>`, but this method panics + /// instead. This is kept for backwards compatibility. See [`cell`](Self::cell) for a safe + /// alternative. + /// + /// # Panics + /// + /// Panics if the index is out of bounds. + #[track_caller] + #[deprecated = "use `Buffer[(x, y)]` instead. To avoid panicking, use `Buffer::cell((x, y))`. Both methods take `impl Into`."] + #[must_use] + pub fn get(&self, x: u16, y: u16) -> &Cell { + let i = self.index_of(x, y); + &self.content[i] + } + + /// Returns a mutable reference to the [`Cell`] at the given coordinates. + /// + /// Callers should use [`Buffer[]`](Self::index_mut) or [`Buffer::cell_mut`] instead of this + /// method. + /// + /// Note: idiomatically methods named `get_mut` usually return `Option<&mut T>`, but this method + /// panics instead. This is kept for backwards compatibility. See [`cell_mut`](Self::cell_mut) + /// for a safe alternative. + /// + /// # Panics + /// + /// Panics if the position is outside the `Buffer`'s area. + #[track_caller] + #[deprecated = "use `Buffer[(x, y)]` instead. To avoid panicking, use `Buffer::cell_mut((x, y))`. Both methods take `impl Into`."] + #[must_use] + pub fn get_mut(&mut self, x: u16, y: u16) -> &mut Cell { + let i = self.index_of(x, y); + &mut self.content[i] + } + + /// Returns a reference to the [`Cell`] at the given position or [`None`] if the position is + /// outside the `Buffer`'s area. + /// + /// This method accepts any value that can be converted to [`Position`] (e.g. `(x, y)` or + /// `Position::new(x, y)`). + /// + /// For a method that panics when the position is outside the buffer instead of returning + /// `None`, use [`Buffer[]`](Self::index). + /// + /// # Examples + /// + /// ```rust + /// use ratatui_core::buffer::{Buffer, Cell}; + /// use ratatui_core::layout::{Position, Rect}; + /// + /// let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10)); + /// + /// assert_eq!(buffer.cell(Position::new(0, 0)), Some(&Cell::default())); + /// assert_eq!(buffer.cell(Position::new(10, 10)), None); + /// assert_eq!(buffer.cell((0, 0)), Some(&Cell::default())); + /// assert_eq!(buffer.cell((10, 10)), None); + /// ``` + #[must_use] + pub fn cell>(&self, position: P) -> Option<&Cell> { + let position = position.into(); + let index = self.index_of_opt(position)?; + self.content.get(index) + } + + /// Returns a mutable reference to the [`Cell`] at the given position or [`None`] if the + /// position is outside the `Buffer`'s area. + /// + /// This method accepts any value that can be converted to [`Position`] (e.g. `(x, y)` or + /// `Position::new(x, y)`). + /// + /// For a method that panics when the position is outside the buffer instead of returning + /// `None`, use [`Buffer[]`](Self::index_mut). + /// + /// # Examples + /// + /// ```rust + /// use ratatui_core::buffer::{Buffer, Cell}; + /// use ratatui_core::layout::{Position, Rect}; + /// use ratatui_core::style::{Color, Style}; + /// let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10)); + /// + /// if let Some(cell) = buffer.cell_mut(Position::new(0, 0)) { + /// cell.set_symbol("A"); + /// } + /// if let Some(cell) = buffer.cell_mut((0, 0)) { + /// cell.set_style(Style::default().fg(Color::Red)); + /// } + /// ``` + #[must_use] + pub fn cell_mut>(&mut self, position: P) -> Option<&mut Cell> { + let position = position.into(); + let index = self.index_of_opt(position)?; + self.content.get_mut(index) + } + + /// Returns the index in the `Vec` for the given global (x, y) coordinates. + /// + /// Global coordinates are offset by the Buffer's area offset (`x`/`y`). + /// + /// Usage discouraged, as it exposes `self.content` as a linearly indexable array, which limits + /// potential future abstractions. See . + /// + /// # Examples + /// + /// ``` + /// use ratatui_core::buffer::Buffer; + /// use ratatui_core::layout::Rect; + /// + /// let buffer = Buffer::empty(Rect::new(200, 100, 10, 10)); + /// // Global coordinates to the top corner of this buffer's area + /// assert_eq!(buffer.index_of(200, 100), 0); + /// ``` + /// + /// # Panics + /// + /// Panics when given an coordinate that is outside of this Buffer's area. + /// + /// ```should_panic + /// use ratatui_core::buffer::Buffer; + /// use ratatui_core::layout::Rect; + /// + /// let buffer = Buffer::empty(Rect::new(200, 100, 10, 10)); + /// // Top coordinate is outside of the buffer in global coordinate space, as the Buffer's area + /// // starts at (200, 100). + /// buffer.index_of(0, 0); // Panics + /// ``` + #[track_caller] + #[must_use] + pub fn index_of(&self, x: u16, y: u16) -> usize { + self.index_of_opt(Position { x, y }).unwrap_or_else(|| { + panic!( + "index outside of buffer: the area is {area:?} but index is ({x}, {y})", + area = self.area, + ) + }) + } + + /// Returns the index in the `Vec` for the given global (x, y) coordinates. + /// + /// Returns `None` if the given coordinates are outside of the Buffer's area. + /// + /// Note that this is private because of + #[must_use] + const fn index_of_opt(&self, position: Position) -> Option { + let area = self.area; + if !area.contains(position) { + return None; + } + // remove offset + let y = (position.y - self.area.y) as usize; + let x = (position.x - self.area.x) as usize; + let width = self.area.width as usize; + Some(y * width + x) + } + + /// Returns the (global) coordinates of a cell given its index. + /// + /// Global coordinates are offset by the Buffer's area offset (`x`/`y`). + /// + /// Usage discouraged, as it exposes `self.content` as a linearly indexable array, which limits + /// potential future abstractions. See . + /// + /// # Examples + /// + /// ``` + /// use ratatui_core::buffer::Buffer; + /// use ratatui_core::layout::Rect; + /// + /// let rect = Rect::new(200, 100, 10, 10); + /// let buffer = Buffer::empty(rect); + /// assert_eq!(buffer.pos_of(0), (200, 100)); + /// assert_eq!(buffer.pos_of(14), (204, 101)); + /// ``` + /// + /// # Panics + /// + /// Panics when given an index that is outside the Buffer's content. + /// + /// ```should_panic + /// use ratatui_core::buffer::Buffer; + /// use ratatui_core::layout::Rect; + /// + /// let rect = Rect::new(0, 0, 10, 10); // 100 cells in total + /// let buffer = Buffer::empty(rect); + /// // Index 100 is the 101th cell, which lies outside of the area of this Buffer. + /// buffer.pos_of(100); // Panics + /// ``` + #[must_use] + pub fn pos_of(&self, index: usize) -> (u16, u16) { + debug_assert!( + index < self.content.len(), + "Trying to get the coords of a cell outside the buffer: i={index} len={}", + self.content.len() + ); + let x = index % self.area.width as usize + self.area.x as usize; + let y = index / self.area.width as usize + self.area.y as usize; + ( + u16::try_from(x).expect("x overflow. This should never happen as area.width is u16"), + u16::try_from(y).expect("y overflow. This should never happen as area.height is u16"), + ) + } + + /// Print a string, starting at the position (x, y) + pub fn set_string(&mut self, x: u16, y: u16, string: T, style: S) + where + T: AsRef, + S: Into +//!
+//!
+//!
+//!
+//!
C50
+//!
C100
+//!
C200
+//!
C300
+//!
C400
+//!
C500
+//!
C600
+//!
C700
+//!
C800
+//!
C900
+//!
A100
+//!
A200
+//!
A400
+//!
A700
+//!
+//!
+//!
+//! +//! [`RED`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`PINK`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`PURPLE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`DEEP_PURPLE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`INDIGO`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLUE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`LIGHT_BLUE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`CYAN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`TEAL`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`GREEN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`LIGHT_GREEN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`LIME`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`YELLOW`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`AMBER`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`ORANGE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`DEEP_ORANGE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BROWN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`GRAY`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLUE_GRAY`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLACK`]
+//!
+//!
+//!
+//!
+//! +//! [`WHITE`]
+//!
+//!
+//!
+//!
+//! +//! # Example +//! +//! ```rust +//! use ratatui_core::style::Color; +//! use ratatui_core::style::palette::material::{BLUE, RED}; +//! +//! assert_eq!(RED.c500, Color::Rgb(244, 67, 54)); +//! assert_eq!(BLUE.c500, Color::Rgb(33, 150, 243)); +//! ``` +//! +//! [`matdesign-color` crate]: https://crates.io/crates/matdesign-color + +use crate::style::Color; + +/// A palette of colors for use in Material design with accent colors +/// +/// This is a collection of colors that are used in Material design. They consist of a set of +/// colors from 50 to 900, and a set of accent colors from 100 to 700. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct AccentedPalette { + pub c50: Color, + pub c100: Color, + pub c200: Color, + pub c300: Color, + pub c400: Color, + pub c500: Color, + pub c600: Color, + pub c700: Color, + pub c800: Color, + pub c900: Color, + pub a100: Color, + pub a200: Color, + pub a400: Color, + pub a700: Color, +} + +/// A palette of colors for use in Material design without accent colors +/// +/// This is a collection of colors that are used in Material design. They consist of a set of +/// colors from 50 to 900. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct NonAccentedPalette { + pub c50: Color, + pub c100: Color, + pub c200: Color, + pub c300: Color, + pub c400: Color, + pub c500: Color, + pub c600: Color, + pub c700: Color, + pub c800: Color, + pub c900: Color, +} + +impl AccentedPalette { + /// Create a new `AccentedPalette` from the given variants + /// + /// The variants should be in the format [0x00RRGGBB, ...] + pub const fn from_variants(variants: [u32; 14]) -> Self { + Self { + c50: Color::from_u32(variants[0]), + c100: Color::from_u32(variants[1]), + c200: Color::from_u32(variants[2]), + c300: Color::from_u32(variants[3]), + c400: Color::from_u32(variants[4]), + c500: Color::from_u32(variants[5]), + c600: Color::from_u32(variants[6]), + c700: Color::from_u32(variants[7]), + c800: Color::from_u32(variants[8]), + c900: Color::from_u32(variants[9]), + a100: Color::from_u32(variants[10]), + a200: Color::from_u32(variants[11]), + a400: Color::from_u32(variants[12]), + a700: Color::from_u32(variants[13]), + } + } +} + +impl NonAccentedPalette { + /// Create a new `NonAccented` from the given variants + /// + /// The variants should be in the format [0x00RRGGBB, ...] + pub const fn from_variants(variants: [u32; 10]) -> Self { + Self { + c50: Color::from_u32(variants[0]), + c100: Color::from_u32(variants[1]), + c200: Color::from_u32(variants[2]), + c300: Color::from_u32(variants[3]), + c400: Color::from_u32(variants[4]), + c500: Color::from_u32(variants[5]), + c600: Color::from_u32(variants[6]), + c700: Color::from_u32(variants[7]), + c800: Color::from_u32(variants[8]), + c900: Color::from_u32(variants[9]), + } + } +} + +// Accented palettes + +pub const RED: AccentedPalette = AccentedPalette::from_variants(variants::RED); +pub const PINK: AccentedPalette = AccentedPalette::from_variants(variants::PINK); +pub const PURPLE: AccentedPalette = AccentedPalette::from_variants(variants::PURPLE); +pub const DEEP_PURPLE: AccentedPalette = AccentedPalette::from_variants(variants::DEEP_PURPLE); +pub const INDIGO: AccentedPalette = AccentedPalette::from_variants(variants::INDIGO); +pub const BLUE: AccentedPalette = AccentedPalette::from_variants(variants::BLUE); +pub const LIGHT_BLUE: AccentedPalette = AccentedPalette::from_variants(variants::LIGHT_BLUE); +pub const CYAN: AccentedPalette = AccentedPalette::from_variants(variants::CYAN); +pub const TEAL: AccentedPalette = AccentedPalette::from_variants(variants::TEAL); +pub const GREEN: AccentedPalette = AccentedPalette::from_variants(variants::GREEN); +pub const LIGHT_GREEN: AccentedPalette = AccentedPalette::from_variants(variants::LIGHT_GREEN); +pub const LIME: AccentedPalette = AccentedPalette::from_variants(variants::LIME); +pub const YELLOW: AccentedPalette = AccentedPalette::from_variants(variants::YELLOW); +pub const AMBER: AccentedPalette = AccentedPalette::from_variants(variants::AMBER); +pub const ORANGE: AccentedPalette = AccentedPalette::from_variants(variants::ORANGE); +pub const DEEP_ORANGE: AccentedPalette = AccentedPalette::from_variants(variants::DEEP_ORANGE); + +// Unaccented palettes +pub const BROWN: NonAccentedPalette = NonAccentedPalette::from_variants(variants::BROWN); +pub const GRAY: NonAccentedPalette = NonAccentedPalette::from_variants(variants::GRAY); +pub const BLUE_GRAY: NonAccentedPalette = NonAccentedPalette::from_variants(variants::BLUE_GRAY); + +// Black and white included for completeness +pub const BLACK: Color = Color::from_u32(0x000000); +pub const WHITE: Color = Color::from_u32(0xFFFFFF); + +mod variants { + pub const RED: [u32; 14] = [ + 0xFFEBEE, 0xFFCDD2, 0xEF9A9A, 0xE57373, 0xEF5350, 0xF44336, 0xE53935, 0xD32F2F, 0xC62828, + 0xB71C1C, 0xFF8A80, 0xFF5252, 0xFF1744, 0xD50000, + ]; + pub const PINK: [u32; 14] = [ + 0xFCE4EC, 0xF8BBD0, 0xF48FB1, 0xF06292, 0xEC407A, 0xE91E63, 0xD81B60, 0xC2185B, 0xAD1457, + 0x880E4F, 0xFF80AB, 0xFF4081, 0xF50057, 0xC51162, + ]; + pub const PURPLE: [u32; 14] = [ + 0xF3E5F5, 0xE1BEE7, 0xCE93D8, 0xBA68C8, 0xAB47BC, 0x9C27B0, 0x8E24AA, 0x7B1FA2, 0x6A1B9A, + 0x4A148C, 0xEA80FC, 0xE040FB, 0xD500F9, 0xAA00FF, + ]; + pub const DEEP_PURPLE: [u32; 14] = [ + 0xEDE7F6, 0xD1C4E9, 0xB39DDB, 0x9575CD, 0x7E57C2, 0x673AB7, 0x5E35B1, 0x512DA8, 0x4527A0, + 0x311B92, 0xB388FF, 0x7C4DFF, 0x651FFF, 0x6200EA, + ]; + pub const INDIGO: [u32; 14] = [ + 0xE8EAF6, 0xC5CAE9, 0x9FA8DA, 0x7986CB, 0x5C6BC0, 0x3F51B5, 0x3949AB, 0x303F9F, 0x283593, + 0x1A237E, 0x8C9EFF, 0x536DFE, 0x3D5AFE, 0x304FFE, + ]; + pub const BLUE: [u32; 14] = [ + 0xE3F2FD, 0xBBDEFB, 0x90CAF9, 0x64B5F6, 0x42A5F5, 0x2196F3, 0x1E88E5, 0x1976D2, 0x1565C0, + 0x0D47A1, 0x82B1FF, 0x448AFF, 0x2979FF, 0x2962FF, + ]; + pub const LIGHT_BLUE: [u32; 14] = [ + 0xE1F5FE, 0xB3E5FC, 0x81D4FA, 0x4FC3F7, 0x29B6F6, 0x03A9F4, 0x039BE5, 0x0288D1, 0x0277BD, + 0x01579B, 0x80D8FF, 0x40C4FF, 0x00B0FF, 0x0091EA, + ]; + pub const CYAN: [u32; 14] = [ + 0xE0F7FA, 0xB2EBF2, 0x80DEEA, 0x4DD0E1, 0x26C6DA, 0x00BCD4, 0x00ACC1, 0x0097A7, 0x00838F, + 0x006064, 0x84FFFF, 0x18FFFF, 0x00E5FF, 0x00B8D4, + ]; + pub const TEAL: [u32; 14] = [ + 0xE0F2F1, 0xB2DFDB, 0x80CBC4, 0x4DB6AC, 0x26A69A, 0x009688, 0x00897B, 0x00796B, 0x00695C, + 0x004D40, 0xA7FFEB, 0x64FFDA, 0x1DE9B6, 0x00BFA5, + ]; + pub const GREEN: [u32; 14] = [ + 0xE8F5E9, 0xC8E6C9, 0xA5D6A7, 0x81C784, 0x66BB6A, 0x4CAF50, 0x43A047, 0x388E3C, 0x2E7D32, + 0x1B5E20, 0xB9F6CA, 0x69F0AE, 0x00E676, 0x00C853, + ]; + pub const LIGHT_GREEN: [u32; 14] = [ + 0xF1F8E9, 0xDCEDC8, 0xC5E1A5, 0xAED581, 0x9CCC65, 0x8BC34A, 0x7CB342, 0x689F38, 0x558B2F, + 0x33691E, 0xCCFF90, 0xB2FF59, 0x76FF03, 0x64DD17, + ]; + pub const LIME: [u32; 14] = [ + 0xF9FBE7, 0xF0F4C3, 0xE6EE9C, 0xDCE775, 0xD4E157, 0xCDDC39, 0xC0CA33, 0xAFB42B, 0x9E9D24, + 0x827717, 0xF4FF81, 0xEEFF41, 0xC6FF00, 0xAEEA00, + ]; + pub const YELLOW: [u32; 14] = [ + 0xFFFDE7, 0xFFF9C4, 0xFFF59D, 0xFFF176, 0xFFEE58, 0xFFEB3B, 0xFDD835, 0xFBC02D, 0xF9A825, + 0xF57F17, 0xFFFF8D, 0xFFFF00, 0xFFEA00, 0xFFD600, + ]; + pub const AMBER: [u32; 14] = [ + 0xFFF8E1, 0xFFECB3, 0xFFE082, 0xFFD54F, 0xFFCA28, 0xFFC107, 0xFFB300, 0xFFA000, 0xFF8F00, + 0xFF6F00, 0xFFE57F, 0xFFD740, 0xFFC400, 0xFFAB00, + ]; + pub const ORANGE: [u32; 14] = [ + 0xFFF3E0, 0xFFE0B2, 0xFFCC80, 0xFFB74D, 0xFFA726, 0xFF9800, 0xFB8C00, 0xF57C00, 0xEF6C00, + 0xE65100, 0xFFD180, 0xFFAB40, 0xFF9100, 0xFF6D00, + ]; + pub const DEEP_ORANGE: [u32; 14] = [ + 0xFBE9E7, 0xFFCCBC, 0xFFAB91, 0xFF8A65, 0xFF7043, 0xFF5722, 0xF4511E, 0xE64A19, 0xD84315, + 0xBF360C, 0xFF9E80, 0xFF6E40, 0xFF3D00, 0xDD2C00, + ]; + pub const BROWN: [u32; 10] = [ + 0xEFEBE9, 0xD7CCC8, 0xBCAAA4, 0xA1887F, 0x8D6E63, 0x795548, 0x6D4C41, 0x5D4037, 0x4E342E, + 0x3E2723, + ]; + pub const GRAY: [u32; 10] = [ + 0xFAFAFA, 0xF5F5F5, 0xEEEEEE, 0xE0E0E0, 0xBDBDBD, 0x9E9E9E, 0x757575, 0x616161, 0x424242, + 0x212121, + ]; + pub const BLUE_GRAY: [u32; 10] = [ + 0xECEFF1, 0xCFD8DC, 0xB0BEC5, 0x90A4AE, 0x78909C, 0x607D8B, 0x546E7A, 0x455A64, 0x37474F, + 0x263238, + ]; +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/style/palette/tailwind.rs b/vendor/ratatui-jcode/ratatui-core/src/style/palette/tailwind.rs new file mode 100644 index 00000000000..926d6316a9f --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/style/palette/tailwind.rs @@ -0,0 +1,653 @@ +//! Represents the Tailwind CSS [default color palette][palette]. +//! +//! [palette]: https://tailwindcss.com/docs/customizing-colors#default-color-palette +//! +//! There are 22 palettes. Each palette has 11 colors, with variants from 50 to 950. Black and White +//! are also included for completeness and to avoid being affected by any terminal theme that might +//! be in use. +//! +//! +//!
+//!
+//!
+//!
+//!
C50
C100
C200
C300
C400
+//!
C500
C600
C700
C800
C900
+//!
C950
+//!
+//!
+//!
+//! +//! [`SLATE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`GRAY`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`ZINC`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`NEUTRAL`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`STONE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`RED`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`ORANGE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`AMBER`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`YELLOW`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`LIME`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`GREEN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`EMERALD`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`TEAL`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`CYAN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`SKY`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLUE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`INDIGO`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`VIOLET`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`PURPLE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`FUCHSIA`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`PINK`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLACK`]
+//!
+//!
+//!
+//!
+//! +//! [`WHITE`]
+//!
+//!
+//!
+//!
+//! +//! # Example +//! +//! ```rust +//! use ratatui_core::style::Color; +//! use ratatui_core::style::palette::tailwind::{BLUE, RED}; +//! +//! assert_eq!(RED.c500, Color::Rgb(239, 68, 68)); +//! assert_eq!(BLUE.c500, Color::Rgb(59, 130, 246)); +//! ``` + +use crate::style::Color; + +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct Palette { + pub c50: Color, + pub c100: Color, + pub c200: Color, + pub c300: Color, + pub c400: Color, + pub c500: Color, + pub c600: Color, + pub c700: Color, + pub c800: Color, + pub c900: Color, + pub c950: Color, +} + +#[rustfmt::skip] +///
+pub const BLACK: Color = Color::from_u32(0x000000); + +#[rustfmt::skip] +///
+pub const WHITE: Color = Color::from_u32(0xffffff); + +#[rustfmt::skip] +///
+pub const SLATE: Palette = Palette { + c50: Color::from_u32(0xf8fafc), + c100: Color::from_u32(0xf1f5f9), + c200: Color::from_u32(0xe2e8f0), + c300: Color::from_u32(0xcbd5e1), + c400: Color::from_u32(0x94a3b8), + c500: Color::from_u32(0x64748b), + c600: Color::from_u32(0x475569), + c700: Color::from_u32(0x334155), + c800: Color::from_u32(0x1e293b), + c900: Color::from_u32(0x0f172a), + c950: Color::from_u32(0x020617), +}; + +#[rustfmt::skip] +///
+pub const GRAY: Palette = Palette { + c50: Color::from_u32(0xf9fafb), + c100: Color::from_u32(0xf3f4f6), + c200: Color::from_u32(0xe5e7eb), + c300: Color::from_u32(0xd1d5db), + c400: Color::from_u32(0x9ca3af), + c500: Color::from_u32(0x6b7280), + c600: Color::from_u32(0x4b5563), + c700: Color::from_u32(0x374151), + c800: Color::from_u32(0x1f2937), + c900: Color::from_u32(0x111827), + c950: Color::from_u32(0x030712), +}; + +#[rustfmt::skip] +///
+pub const ZINC: Palette = Palette { + c50: Color::from_u32(0xfafafa), + c100: Color::from_u32(0xf4f4f5), + c200: Color::from_u32(0xe4e4e7), + c300: Color::from_u32(0xd4d4d8), + c400: Color::from_u32(0xa1a1aa), + c500: Color::from_u32(0x71717a), + c600: Color::from_u32(0x52525b), + c700: Color::from_u32(0x3f3f46), + c800: Color::from_u32(0x27272a), + c900: Color::from_u32(0x18181b), + c950: Color::from_u32(0x09090b), +}; + +#[rustfmt::skip] +///
+pub const NEUTRAL: Palette = Palette { + c50: Color::from_u32(0xfafafa), + c100: Color::from_u32(0xf5f5f5), + c200: Color::from_u32(0xe5e5e5), + c300: Color::from_u32(0xd4d4d4), + c400: Color::from_u32(0xa3a3a3), + c500: Color::from_u32(0x737373), + c600: Color::from_u32(0x525252), + c700: Color::from_u32(0x404040), + c800: Color::from_u32(0x262626), + c900: Color::from_u32(0x171717), + c950: Color::from_u32(0x0a0a0a), +}; + +#[rustfmt::skip] +///
+pub const STONE: Palette = Palette { + c50: Color::from_u32(0xfafaf9), + c100: Color::from_u32(0xf5f5f4), + c200: Color::from_u32(0xe7e5e4), + c300: Color::from_u32(0xd6d3d1), + c400: Color::from_u32(0xa8a29e), + c500: Color::from_u32(0x78716c), + c600: Color::from_u32(0x57534e), + c700: Color::from_u32(0x44403c), + c800: Color::from_u32(0x292524), + c900: Color::from_u32(0x1c1917), + c950: Color::from_u32(0x0c0a09), +}; + +#[rustfmt::skip] +///
+pub const RED: Palette = Palette { + c50: Color::from_u32(0xfef2f2), + c100: Color::from_u32(0xfee2e2), + c200: Color::from_u32(0xfecaca), + c300: Color::from_u32(0xfca5a5), + c400: Color::from_u32(0xf87171), + c500: Color::from_u32(0xef4444), + c600: Color::from_u32(0xdc2626), + c700: Color::from_u32(0xb91c1c), + c800: Color::from_u32(0x991b1b), + c900: Color::from_u32(0x7f1d1d), + c950: Color::from_u32(0x450a0a), +}; + +#[rustfmt::skip] +///
+pub const ORANGE: Palette = Palette { + c50: Color::from_u32(0xfff7ed), + c100: Color::from_u32(0xffedd5), + c200: Color::from_u32(0xfed7aa), + c300: Color::from_u32(0xfdba74), + c400: Color::from_u32(0xfb923c), + c500: Color::from_u32(0xf97316), + c600: Color::from_u32(0xea580c), + c700: Color::from_u32(0xc2410c), + c800: Color::from_u32(0x9a3412), + c900: Color::from_u32(0x7c2d12), + c950: Color::from_u32(0x431407), +}; + +#[rustfmt::skip] +///
+pub const AMBER: Palette = Palette { + c50: Color::from_u32(0xfffbeb), + c100: Color::from_u32(0xfef3c7), + c200: Color::from_u32(0xfde68a), + c300: Color::from_u32(0xfcd34d), + c400: Color::from_u32(0xfbbf24), + c500: Color::from_u32(0xf59e0b), + c600: Color::from_u32(0xd97706), + c700: Color::from_u32(0xb45309), + c800: Color::from_u32(0x92400e), + c900: Color::from_u32(0x78350f), + c950: Color::from_u32(0x451a03), +}; + +#[rustfmt::skip] +///
+pub const YELLOW: Palette = Palette { + c50: Color::from_u32(0xfefce8), + c100: Color::from_u32(0xfef9c3), + c200: Color::from_u32(0xfef08a), + c300: Color::from_u32(0xfde047), + c400: Color::from_u32(0xfacc15), + c500: Color::from_u32(0xeab308), + c600: Color::from_u32(0xca8a04), + c700: Color::from_u32(0xa16207), + c800: Color::from_u32(0x854d0e), + c900: Color::from_u32(0x713f12), + c950: Color::from_u32(0x422006), +}; + +#[rustfmt::skip] +///
+pub const LIME: Palette = Palette { + c50: Color::from_u32(0xf7fee7), + c100: Color::from_u32(0xecfccb), + c200: Color::from_u32(0xd9f99d), + c300: Color::from_u32(0xbef264), + c400: Color::from_u32(0xa3e635), + c500: Color::from_u32(0x84cc16), + c600: Color::from_u32(0x65a30d), + c700: Color::from_u32(0x4d7c0f), + c800: Color::from_u32(0x3f6212), + c900: Color::from_u32(0x365314), + c950: Color::from_u32(0x1a2e05), +}; + +#[rustfmt::skip] +///
+pub const GREEN: Palette = Palette { + c50: Color::from_u32(0xf0fdf4), + c100: Color::from_u32(0xdcfce7), + c200: Color::from_u32(0xbbf7d0), + c300: Color::from_u32(0x86efac), + c400: Color::from_u32(0x4ade80), + c500: Color::from_u32(0x22c55e), + c600: Color::from_u32(0x16a34a), + c700: Color::from_u32(0x15803d), + c800: Color::from_u32(0x166534), + c900: Color::from_u32(0x14532d), + c950: Color::from_u32(0x052e16), +}; + +#[rustfmt::skip] +///
+pub const EMERALD: Palette = Palette { + c50: Color::from_u32(0xecfdf5), + c100: Color::from_u32(0xd1fae5), + c200: Color::from_u32(0xa7f3d0), + c300: Color::from_u32(0x6ee7b7), + c400: Color::from_u32(0x34d399), + c500: Color::from_u32(0x10b981), + c600: Color::from_u32(0x059669), + c700: Color::from_u32(0x047857), + c800: Color::from_u32(0x065f46), + c900: Color::from_u32(0x064e3b), + c950: Color::from_u32(0x022c22), +}; + +#[rustfmt::skip] +///
+pub const TEAL: Palette = Palette { + c50: Color::from_u32(0xf0fdfa), + c100: Color::from_u32(0xccfbf1), + c200: Color::from_u32(0x99f6e4), + c300: Color::from_u32(0x5eead4), + c400: Color::from_u32(0x2dd4bf), + c500: Color::from_u32(0x14b8a6), + c600: Color::from_u32(0x0d9488), + c700: Color::from_u32(0x0f766e), + c800: Color::from_u32(0x115e59), + c900: Color::from_u32(0x134e4a), + c950: Color::from_u32(0x042f2e), +}; + +#[rustfmt::skip] +///
+pub const CYAN: Palette = Palette { + c50: Color::from_u32(0xecfeff), + c100: Color::from_u32(0xcffafe), + c200: Color::from_u32(0xa5f3fc), + c300: Color::from_u32(0x67e8f9), + c400: Color::from_u32(0x22d3ee), + c500: Color::from_u32(0x06b6d4), + c600: Color::from_u32(0x0891b2), + c700: Color::from_u32(0x0e7490), + c800: Color::from_u32(0x155e75), + c900: Color::from_u32(0x164e63), + c950: Color::from_u32(0x083344), +}; + +#[rustfmt::skip] +///
+pub const SKY: Palette = Palette { + c50: Color::from_u32(0xf0f9ff), + c100: Color::from_u32(0xe0f2fe), + c200: Color::from_u32(0xbae6fd), + c300: Color::from_u32(0x7dd3fc), + c400: Color::from_u32(0x38bdf8), + c500: Color::from_u32(0x0ea5e9), + c600: Color::from_u32(0x0284c7), + c700: Color::from_u32(0x0369a1), + c800: Color::from_u32(0x075985), + c900: Color::from_u32(0x0c4a6e), + c950: Color::from_u32(0x082f49), +}; + +#[rustfmt::skip] +///
+pub const BLUE: Palette = Palette { + c50: Color::from_u32(0xeff6ff), + c100: Color::from_u32(0xdbeafe), + c200: Color::from_u32(0xbfdbfe), + c300: Color::from_u32(0x93c5fd), + c400: Color::from_u32(0x60a5fa), + c500: Color::from_u32(0x3b82f6), + c600: Color::from_u32(0x2563eb), + c700: Color::from_u32(0x1d4ed8), + c800: Color::from_u32(0x1e40af), + c900: Color::from_u32(0x1e3a8a), + c950: Color::from_u32(0x172554), +}; + +#[rustfmt::skip] +///
+pub const INDIGO: Palette = Palette { + c50: Color::from_u32(0xeef2ff), + c100: Color::from_u32(0xe0e7ff), + c200: Color::from_u32(0xc7d2fe), + c300: Color::from_u32(0xa5b4fc), + c400: Color::from_u32(0x818cf8), + c500: Color::from_u32(0x6366f1), + c600: Color::from_u32(0x4f46e5), + c700: Color::from_u32(0x4338ca), + c800: Color::from_u32(0x3730a3), + c900: Color::from_u32(0x312e81), + c950: Color::from_u32(0x1e1b4b), +}; + +#[rustfmt::skip] +///
+pub const VIOLET: Palette = Palette { + c50: Color::from_u32(0xf5f3ff), + c100: Color::from_u32(0xede9fe), + c200: Color::from_u32(0xddd6fe), + c300: Color::from_u32(0xc4b5fd), + c400: Color::from_u32(0xa78bfa), + c500: Color::from_u32(0x8b5cf6), + c600: Color::from_u32(0x7c3aed), + c700: Color::from_u32(0x6d28d9), + c800: Color::from_u32(0x5b21b6), + c900: Color::from_u32(0x4c1d95), + c950: Color::from_u32(0x2e1065), +}; + +#[rustfmt::skip] +///
+pub const PURPLE: Palette = Palette { + c50: Color::from_u32(0xfaf5ff), + c100: Color::from_u32(0xf3e8ff), + c200: Color::from_u32(0xe9d5ff), + c300: Color::from_u32(0xd8b4fe), + c400: Color::from_u32(0xc084fc), + c500: Color::from_u32(0xa855f7), + c600: Color::from_u32(0x9333ea), + c700: Color::from_u32(0x7e22ce), + c800: Color::from_u32(0x6b21a8), + c900: Color::from_u32(0x581c87), + c950: Color::from_u32(0x3b0764), +}; + +#[rustfmt::skip] +///
+pub const FUCHSIA: Palette = Palette { + c50: Color::from_u32(0xfdf4ff), + c100: Color::from_u32(0xfae8ff), + c200: Color::from_u32(0xf5d0fe), + c300: Color::from_u32(0xf0abfc), + c400: Color::from_u32(0xe879f9), + c500: Color::from_u32(0xd946ef), + c600: Color::from_u32(0xc026d3), + c700: Color::from_u32(0xa21caf), + c800: Color::from_u32(0x86198f), + c900: Color::from_u32(0x701a75), + c950: Color::from_u32(0x4a044e), +}; + +#[rustfmt::skip] +///
+pub const PINK: Palette = Palette { + c50: Color::from_u32(0xfdf2f8), + c100: Color::from_u32(0xfce7f3), + c200: Color::from_u32(0xfbcfe8), + c300: Color::from_u32(0xf9a8d4), + c400: Color::from_u32(0xf472b6), + c500: Color::from_u32(0xec4899), + c600: Color::from_u32(0xdb2777), + c700: Color::from_u32(0xbe185d), + c800: Color::from_u32(0x9d174d), + c900: Color::from_u32(0x831843), + c950: Color::from_u32(0x500724), +}; + +#[rustfmt::skip] +///
+pub const ROSE: Palette = Palette { + c50: Color::from_u32(0xfff1f2), + c100: Color::from_u32(0xffe4e6), + c200: Color::from_u32(0xfecdd3), + c300: Color::from_u32(0xfda4af), + c400: Color::from_u32(0xfb7185), + c500: Color::from_u32(0xf43f5e), + c600: Color::from_u32(0xe11d48), + c700: Color::from_u32(0xbe123c), + c800: Color::from_u32(0x9f1239), + c900: Color::from_u32(0x881337), + c950: Color::from_u32(0x4c0519), +}; diff --git a/vendor/ratatui-jcode/ratatui-core/src/style/palette_conversion.rs b/vendor/ratatui-jcode/ratatui-core/src/style/palette_conversion.rs new file mode 100644 index 00000000000..5922b38ccb6 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/style/palette_conversion.rs @@ -0,0 +1,82 @@ +//! Conversions from colors in the `palette` crate to [`Color`]. + +use ::palette::LinSrgb; +use ::palette::bool_mask::LazySelect; +use ::palette::num::{Arithmetics, MulSub, PartialCmp, Powf, Real}; +use palette::Srgb; +use palette::stimulus::IntoStimulus; + +use crate::style::Color; + +/// Convert an [`palette::Srgb`] color to a [`Color`]. +/// +/// # Examples +/// +/// ``` +/// use palette::Srgb; +/// use ratatui_core::style::Color; +/// +/// let color = Color::from(Srgb::new(1.0f32, 0.0, 0.0)); +/// assert_eq!(color, Color::Rgb(255, 0, 0)); +/// ``` +impl> From> for Color { + fn from(color: Srgb) -> Self { + let (red, green, blue) = color.into_format().into_components(); + Self::Rgb(red, green, blue) + } +} + +/// Convert a [`palette::LinSrgb`] color to a [`Color`]. +/// +/// Note: this conversion only works for floating point linear sRGB colors. If you have a linear +/// sRGB color in another format, you need to convert it to floating point first. +/// +/// # Examples +/// +/// ``` +/// use palette::LinSrgb; +/// use ratatui_core::style::Color; +/// +/// let color = Color::from(LinSrgb::new(1.0f32, 0.0, 0.0)); +/// assert_eq!(color, Color::Rgb(255, 0, 0)); +/// ``` +impl> From> for Color +where + T: Real + Powf + MulSub + Arithmetics + PartialCmp + Clone, + T::Mask: LazySelect, +{ + fn from(color: LinSrgb) -> Self { + let srgb_color = Srgb::::from_linear(color); + Self::from(srgb_color) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn from_srgb() { + const RED: Color = Color::Rgb(255, 0, 0); + assert_eq!(Color::from(Srgb::new(255u8, 0, 0)), RED); + assert_eq!(Color::from(Srgb::new(65535u16, 0, 0)), RED); + assert_eq!(Color::from(Srgb::new(1.0f32, 0.0, 0.0)), RED); + + assert_eq!( + Color::from(Srgb::new(0.5f32, 0.5, 0.5)), + Color::Rgb(128, 128, 128) + ); + } + + #[test] + fn from_lin_srgb() { + const RED: Color = Color::Rgb(255, 0, 0); + assert_eq!(Color::from(LinSrgb::new(1.0f32, 0.0, 0.0)), RED); + assert_eq!(Color::from(LinSrgb::new(1.0f64, 0.0, 0.0)), RED); + + assert_eq!( + Color::from(LinSrgb::new(0.5f32, 0.5, 0.5)), + Color::Rgb(188, 188, 188) + ); + } +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/style/stylize.rs b/vendor/ratatui-jcode/ratatui-core/src/style/stylize.rs new file mode 100644 index 00000000000..ddf476c4c62 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/style/stylize.rs @@ -0,0 +1,668 @@ +use alloc::borrow::Cow; +use alloc::string::{String, ToString}; +use core::fmt; + +use crate::style::{Color, Modifier, Style}; +use crate::text::Span; + +/// A trait for objects that have a `Style`. +/// +/// This trait enables generic code to be written that can interact with any object that has a +/// `Style`. This is used by the `Stylize` trait to allow generic code to be written that can +/// interact with any object that can be styled. +pub trait Styled { + type Item; + + /// Returns the style of the object. + fn style(&self) -> Style; + + /// Sets the style of the object. + /// + /// `style` accepts any type that is convertible to [`Style`] (e.g. [`Style`], [`Color`], or + /// your own type that implements [`Into