Skip to content

fix(mcp,openrouter): enforce the filtered MCP child env and report content-filter 403s accurately - #1354

Closed
ianalitis wants to merge 3 commits into
1jehuang:masterfrom
ianalitis:pr/clippy-1.98-lint-drift
Closed

ianalitis wants to merge 3 commits into
1jehuang:masterfrom
ianalitis:pr/clippy-1.98-lint-drift

Conversation

@ianalitis

@ianalitis ianalitis commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Three commits on top of master (a705f842d): two bug fixes and one test-environment fix. Diff is 4 files, +182 / -2.

Commit Change
4c4fd3ec7 fix(mcp): enforce the filtered subprocess environment for MCP children
fdec1e2e5 fix(openrouter): stop reporting a content-filter block as a bad API key
2a306fbe3 fix(test): exempt the macOS-gated macos_computer_use from the token caps

This PR previously also carried a Rust 1.98 clippy/rustfmt sweep and a ratchet refresh. Those commits are dropped: master fixed every one of those sites independently, and Quality Guardrails (the job that runs cargo clippy --all-targets --all-features -- -D warnings on ubuntu-latest, .github/workflows/ci.yml:52) is green on master at a705f842d, 31/31 checks. Details under Scope reduction.

1. MCP children inherited the credentials the filter removed

One of the findings in #771 (this PR does not close that issue; it has other findings).

McpClient::connect_in_dir built the scrubbed environment with mcp_child_env and then applied it with envs(). envs() adds or overrides keys; it never removes them, so the scrubbed map was layered on top of the inherited environment and every credential the filter dropped still reached the MCP child. The filter was enforced against the map, not against the child.

env_clear() now runs before envs(), so a server sees exactly the inherited non-sensitive entries plus the keys its config names.

connect_enforces_child_environment asserts both directions against a real child: a credential named in the config is present with its configured value, and credentials the parent holds but the config does not name are absent. It re-executes itself as an isolated process carrying synthetic inherited secrets rather than mutating the shared test runner's global environment.

2. A content-filter block was reported as a bad API key

OpenRouter answers a blocked prompt with HTTP 403 and a body naming the filter:

{"error":{"message":"Request blocked by content filter: [CREDIT_CARD]"}}

OpenAI-style endpoints report code: "content_filter" instead. Both fell through to the generic 403 hint, so the error block told the reader to check an API key printed two lines below a body that named a content filter (FU-149).

The body markers are now matched first, and the hint says the request text was refused: retrying, or failing over to another lane, resends the same text, so the fix is to change the text rather than the credential.

A 403 from a local endpoint had the same contradiction: the error block prints auth: local endpoint (no auth), and the hint blamed a hosted provider's key. Local endpoints now get a hint that matches that line.

Tests cover the measured guardrail body, the OpenAI-style code, a plain 403 on a hosted endpoint, and a local 403.

3. macos_computer_use failed the token caps on a Mac

macos_computer_use is registered under cfg(target_os = "macos") and its description carries the live-machine safety policy (act only on the requested task, prefer background AX over moving the cursor). On a Mac it exceeds both caps, so cargo test -p jcode-app-core --lib fails on master:

tool_descriptions_stay_under_token_cap: macos_computer_use (~159 tokens)
tool_parameter_descriptions_stay_under_token_cap:
  $.properties.action (~93), $.properties.element (~28)

No CI job runs the app-core lib suite on macOS (the runner only compiles it), so this has stayed green upstream. Each entry is load-bearing: dropping the description entry fails the first test, dropping either parameter entry fails the second. The exemption lines plus their one-line justifications are 5 LOC, so the oversized-test baseline moves 1944 -> 1949 for crates/jcode-app-core/src/tool/tests.rs, the documented "accept size growth from a named change" refresh.

Verification

macOS aarch64-apple-darwin, rustc 1.98.1, at 2a306fbe3:

cargo test -p jcode-base --lib -- mcp::client::tests        # 7 passed
cargo test -p jcode-provider-openrouter-runtime -- openrouter_sse_stream::tests   # 13 passed
cargo test -p jcode-app-core --lib -- token_cap             # 2 passed
cargo fmt --all -- --check                                  # clean
cargo clippy -p jcode-base -p jcode-app-core -p jcode-provider-openrouter-runtime \
  --all-targets --all-features -- -D warnings               # clean
python3 scripts/check_test_size_budget.py                   # rc=0, tracked=49, threshold=1200 LOC

Scope reduction

The branch originally had 9 commits on top of its base: 6 fix(lint) commits, a test-environment fix (answering_no_on_continue_prompt, which master also carries), and these two fixes; the token-cap fix was added after the rebase. I re-checked whether the dropped commits were still unique value, and they are not:

  • Reverting the 11 files they touch to master's content (browser_fast.rs, compile_remote/source.rs, discover.rs, memory.rs, todo.rs, allocator.rs, translate.rs, launch.rs, structured.rs, linux_env.rs, mermaid_content.rs) and re-running clippy with -D warnings across the affected crates stayed clean (rc=0), with those crates re-Checking/Compiling in the same run, so the run was not a cache hit.
  • Per-site checks agree: master already carries the onboarding_flow.rs test fix, the lifecycle.rs type, the PathBuf and Suggestions changes, google.rs's is_multiple_of, scratch_maintenance.rs:225's call, and part_01.rs:1822's let press =.
  • The authoritative check is CI, not my machine: master's Quality Guardrails job passes the exact gate CI: cargo clippy -D warnings fails on stable 1.98 at 12 sites that are unchanged on master #1348 was filed against, on Linux, at a705f842d.

So #1348 and #1294 were stale rather than open work, and both are now closed with the evidence above (#1348 as no-longer-reproducible, #1294 as fixed by the upstream tail expression, verified with the exact macOS repro command from the report). The ratchet refresh (check_code_size_budget.py and the three ratchets behind it) is also unrelated to these fixes and is not needed: every ratchet step in Quality Guardrails passes on master and on this branch.

@ianalitis

Copy link
Copy Markdown
Contributor Author

Scoping note: the one-line jcode-terminal-launch fix here is the focused version of the same change that the broad fork PR #1295 (currently conflicting) carries at its head. #1294 is the issue for that lint, and this PR closes it as part of a lint pass rather than as a standalone change.

@greptile-apps

greptile-apps Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] No outstanding findings require changes before merging.

Summary

This PR makes MCP child processes use the filtered environment and improves error hints for blocked prompts and local servers.

  • MCP servers start with only the environment they are meant to receive.
  • Blocked prompts get content-filter guidance instead of an API-key warning.
  • Local 401 and 403 errors point users to the local server's settings.
  • Prompt-size checks now exempt the macOS-only safety tool.

Reviews (9) · Last reviewed commit: "fix(test): exempt the macOS-gated macos_..." · Reviewed by Greptile

This was referenced Sep 23, 2026
@ianalitis

Copy link
Copy Markdown
Contributor Author

Rebased onto origin/master (8870f19) and extended with the macOS clippy/fmt fixes the 0.88 merge surfaced (see the new commit message). cargo clippy --workspace --all-targets --all-features -D warnings and cargo fmt --check are clean on macOS; the three lib tests that failed on macOS (rejects_non_utf8_git_paths, the two tool-description token caps) now pass.

@ianalitis
ianalitis force-pushed the pr/clippy-1.98-lint-drift branch from 0dcd875 to 07db086 Compare September 25, 2026 00:24
Comment thread crates/jcode-app-core/src/tool/tests.rs Resolved
@greptile-apps

This comment has been minimized.

@ianalitis

Copy link
Copy Markdown
Contributor Author

The red checks on this PR are not from the change: every failing job (Build & Test on all three OSes, Quality Guardrails, Windows Cross-Target Check) stops at the Configure SSH for cargo git dependencies step before compiling, because repository secrets are not exposed to workflows on fork PRs. Format, TypeScript SDK and the other secret-free jobs pass. Locally on macOS, cargo fmt --all -- --check and cargo clippy --workspace --all-targets --all-features -- -D warnings are clean on the head (07db0864d). A maintainer re-run from a branch with access to the secret (or pull_request_target for that step) should show the real result.

@ianalitis

Copy link
Copy Markdown
Contributor Author

@greptile review

@ianalitis

ianalitis commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Ratchet and red-CI attribution for head 4ceceaa8b

Reproduced locally against this branch and against a clean git archive of origin/master (04c7d2b04), so the deltas are measured rather than assumed:

Gate master 04c7d2b04 this branch 4ceceaa8b
python3 scripts/check_code_size_budget.py 45 violations, exit 1 44, exit 1
python3 scripts/check_test_size_budget.py 10 violations, exit 1 10, exit 1
cargo fmt --all -- --check exit 1 (3 hunks, 2 files) exit 0

Correction: this comment first said 46/45 for the code-size counts; those were off by one. The measured counts are above.

  • Code-size: this branch's set is a strict subset of master's. The only file master lists and this branch does not is crates/jcode-tui/src/tui/app/onboarding_flow_control.rs, tracked at 1766 LOC: master sits at 1767, and this branch's reflow is back under the baseline, so the script omits the file. Two files appear on both lists with different sizes: crates/jcode-base/src/auth/lifecycle.rs (branch 3098 vs master 3093, +5 for the clippy-1.99 type alias) and crates/jcode-provider-openai-runtime/src/lib.rs (branch 1525 vs master 1526, -1). Both are already over their baselines on master, so neither adds a violation.
  • Test-size: the 10-file sets match. Before 4ceceaa8b this branch had 11, because the file-local #![allow(clippy::await_holding_lock)] added by 7a5e1c393 pushed agent_tests.rs to 3023 against its tracked 3021. Removing it puts the file back on 3021, so it is absent from both lists: the module that declares it (agent.rs:1306, #[cfg(test)] #[path = "agent_tests.rs"] mod tests;) already carries #![cfg_attr(test, allow(clippy::await_holding_lock))] on master, lint levels are inherited by child modules, and cargo clippy -p jcode-app-core --all-targets --all-features -- -D warnings is clean without it. crates/jcode-app-core/src/tool/tests.rs is 15 LOC larger than master's on both lists (1956 vs 1941), already red either way.

So Quality Guardrails stays red on this PR for exactly the ratchets that already fail on master; this branch adds no violation and removes one. The job fails at the oversized-file step, which runs before the oversized-test step, so its log only ever shows the code-size gate.

Master reds this PR clears

Master job Master failure at 04c7d2b04 In this branch
Format cargo fmt --all -- --check exit 1: jcode-provider-openai-runtime/src/lib.rs:966, onboarding_flow_control.rs:633, onboarding_flow_control.rs:874 branch tree is fmt-clean (whole-tree check)
Build & Test (ubuntu-latest) tui::app::tests::answering_no_on_continue_prompt_lands_on_regular_session panicked at crates/jcode-tui/src/tui/app/tests/onboarding_flow.rs:660 7571a3e64 asserts the rendered body plus onboarding_flow_active() / onboarding_welcome_kind() == Suggestions instead of the ambient-auth accessors
Build & Test (macos-latest) Warning budget exceeded: current=2 baseline=0 the two counted sites (use std::path::PathBuf; in state_ui_input_helpers.rs, the onboarding Suggestions site in onboarding_flow.rs) are removed/absorbed

Build & Test (windows-latest) is green on master and on this branch.

Overlap with #1772

#1772 (fix(ci): restore the Format job on master) is the same three rustfmt hunks in the same two files. This branch already carries a fmt-clean tree and touches both files, so #1772 looks redundant once this lands. I'd rather you decide than assume: say the word and I'll close #1772 with a pointer to this PR.

@ianalitis

Copy link
Copy Markdown
Contributor Author

Run 37942238270 on 4ceceaa8b: one red left, and it is inherited

Job master 04c7d2b04 4ceceaa8b
Build & Test (ubuntu-latest) failure success
Build & Test (macos-latest) failure success
Format failure success
Build & Test (windows-latest) success success
Quality Guardrails failure failure

Quality Guardrails is the only red, and its job log (job 113859859345) fails at the code-size step with exit 1 on the 44 files already over their tracked baselines on master (45 there; the extra one is onboarding_flow_control.rs, which this branch brings back under). Nothing here adds a violation.

Independent check of the ubuntu failure this PR targets, on the branch, locally:

scripts/dev_cargo.sh test -p jcode-tui --lib answering_no_on_continue_prompt_lands_on_regular_session
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2498 filtered out

So the merge blocker is a guardrail that is already red on master rather than anything this branch does.

@ianalitis

Copy link
Copy Markdown
Contributor Author

#1772 overlap, re-verified against its current head

Both PRs carry byte-identical patch bodies for the same two files: @@ -633,10 +633,7 @@ and @@ -874,19 +871,19 @@ in crates/jcode-tui/src/tui/app/onboarding_flow_control.rs (14 insertions / 17 deletions each), plus the same single crates/jcode-provider-openai-runtime/src/lib.rs hunk. Whole-file content matches for lib.rs on both branches (da0d98c8aa01).

It differs for onboarding_flow_control.rs only because of base drift: #1772 (b6f21a4d0) sits on a6ba7844f, three commits behind master. Master's 4a9be63e7 ("onboarding: drop starter prompt cards, land on the regular new-session screen") deleted the starter-prompt-card body from that file, so #1772's copy still carries code master no longer has and merging it as-is would reintroduce it.

Conclusion unchanged: this PR carries #1772's rustfmt hunks unchanged, on current master, and passes Format. #1772 therefore adds nothing and would need a rebase first. Closing it is your call.

@ianalitis

Copy link
Copy Markdown
Contributor Author

Current master 02ae3edc0: four failing steps, and this branch removes each one

The earlier table used the merge base (04c7d2b04). Re-measured against current master: run 38003764282 on 02ae3edc0, the parent of master's head 56d6a8ce4, whose only difference is docs/images/star-history.svg.

Failing step on master 02ae3edc0 master this branch hunk that removes it
Format -> Check formatting failure success rustfmt hunks in onboarding_flow_control.rs + jcode-provider-openai-runtime/src/lib.rs (3 diffs -> 0, on a clean git archive of 02ae3edc0)
Quality Guardrails -> Check formatting failure success same drift
Build & Test (ubuntu-latest) -> Run TUI library tests (Linux only) failure success tests/onboarding_flow.rs: replaces the credential-dependent assertion
Build & Test (macos-latest) -> Enforce warning budget (Linux, macOS) failure success the two warnings this branch deletes

Each row is the same line of code, not a coincidence.

ubuntu, job 114067723709: master fails on exactly the assertion this branch rewrites.

thread 'tui::app::tests::answering_no_on_continue_prompt_lands_on_regular_session' panicked at crates/jcode-tui/src/tui/app/tests/onboarding_flow.rs:660:9:
assertion failed: app.suggestion_prompts().is_empty() || !app.onboarding_welcome_active()

This branch replaces that line with matches!(app.onboarding_welcome_kind(), OnboardingWelcomeKind::Suggestions), because both old helpers read AuthStatus::check_fast(), which is why the test only failed on the Linux runner. Verified on this branch locally: test tui::app::tests::answering_no_on_continue_prompt_lands_on_regular_session ... ok, 1 passed; 0 failed; 2498 filtered out.

macos, job 114067723668: master's warning budget names exactly the two warnings this branch deletes.

warning: unused import: `std::path::PathBuf`
warning: variant `Suggestions` is never constructed
Warning budget exceeded: current=2 baseline=0

state_ui_input_helpers.rs drops the unused std::path::PathBuf import; onboarding_flow.rs marks the dead Suggestions variant #[allow(dead_code)].

Every guardrail budget is already red on master

Quality Guardrails on master stops at step 6 (Check formatting) and steps 7-19 come back skipped, so none of the budget steps below have ever run there. Measured locally on a clean git archive of 02ae3edc0, using master's own scripts/*_budget.json:

budget step master 02ae3edc0 02ae3edc0 + this branch
Enforce oversized-file ratchet fail, 50 items fail, 49 items
Enforce test-file size budget fail fail, identical item list
Enforce panic-prone budget fail (166 -> 167) fail, identical
Enforce swallowed-error budget fail, 11 items fail, identical item list
Enforce wildcard re-export budget pass (17) pass (17)

The branch changes none of these and improves one of them: onboarding_flow_control.rs is 1767 lines on master against a 1766 baseline (an item), and 1764 lines here (no item). Fixing the format step is what lets the budget steps run for the first time, which is why master's existing backlog becomes visible on this PR. Refreshing those baselines is a maintainer call; this PR deliberately does not touch any scripts/*_budget.json.

Net: this branch clears every step that fails on master today, and leaves the guardrail steps red exactly as red as master already is.

@ianalitis

Copy link
Copy Markdown
Contributor Author

Head c9712a336: every CI job is green except Quality Guardrails → Enforce oversized-file ratchet, and that step is not this branch's.

Measured on a clean origin/master (56d6a8ce4) checkout and on this branch:

tree scripts/check_code_size_budget.py
origin/master exit 1, 50 regressions
this branch exit 1, 44 regressions

The 44 are a strict subset of master's by file (on branch but not master: []), and scripts/code_size_budget.json is byte-identical to master's, so the baseline was not touched here. master's own Quality Guardrails never reaches the ratchet because it stops earlier at Check formatting (run 38003764282), which is why an inherited failure looks new on a branch that fixes formatting.

Two of the 44 are files this PR edits, and both already regressed on master: crates/jcode-base/src/auth/lifecycle.rs gains 5 net lines (a type_complexity alias; the file is 325 lines over its baseline before this change) and crates/jcode-provider-openai-runtime/src/lib.rs loses 1.

The three ratchets behind it (check_test_size_budget.py, check_panic_budget.py, check_swallowed_error_budget.py) are skipped once the first one fails, and all three also fail on master's tree. So all four need a baseline refresh or real file splits, which is a different change from lint drift; happy to open that separately.

Everything else on this head passes: Format, Build & Test on ubuntu/macos/windows, Windows Cross-Target Check, TypeScript SDK, Release Automation, Setup Friction Eval, and every Enforce warning budget step including macOS, where master currently fails it.

@ianalitis

Copy link
Copy Markdown
Contributor Author

Merge-order check against #1511

pr/pan-precheck-content-filter (#1511) and this branch both edit
crates/jcode-provider-openrouter-runtime/src/openrouter_sse_stream.rs, from the
same base blob 3e01785bd. The hunks are complementary rather than duplicative:

behaviour #1511 this branch
content-filter 403 should_retry fails fast, no retry hint selection: names_content_filter -> CONTENT_FILTER_HINT
local 403 - 401 | 403 if is_local arm, hosted key not blamed
pre-send local PAN check (issuer prefix + Luhn) -

Checked with git merge-tree --write-tree <this head> 6feb8610e (their head, with
the local mergiraf merge driver disabled so plain 3-way merge decides): rc 0, one
tree, no CONFLICT line, and the merged file carries both helpers. So the two can
land in either order without a rebase on this file.

One follow-up, not a blocker: names_content_filter here and
failover::content_filter_block_label there detect the same markers, so they could
collapse into a single helper once both are in.

`McpClient::connect_in_dir` built the scrubbed environment with `mcp_child_env`
(issue #771: a credential must be opted into a server through its config) and
then applied it with `envs()`. `envs()` adds or overrides keys; it never removes
them, so the scrubbed map was layered on top of the inherited environment and
every credential the filter dropped still reached the MCP child. The filter was
only enforced against the map, not against the child.

`env_clear()` now runs before `envs()`, so a server sees exactly the inherited
non-sensitive entries plus the keys its config names.

The new test asserts both directions against a real child: a credential named in
the config is present with its configured value, and credentials the parent
holds but the config does not name are absent. It re-executes itself as an
isolated process carrying synthetic inherited secrets rather than mutating the
shared test runner's global environment.
OpenRouter answers a blocked prompt with HTTP 403 and a body naming the filter:
`{"error":{"message":"Request blocked by content filter: [CREDIT_CARD]"}}`.
OpenAI-style endpoints report `code: "content_filter"` instead. Both fell
through to the generic 403 hint, so the error block told the reader to check an
API key two lines below a body that named a content filter (FU-149).

Match the body markers first, and answer with a hint that says the request text
was refused: retrying, or failing over to another lane, resends the same text,
so the fix is to change the text rather than the credential.

A 403 from a local endpoint had the same contradiction: the error block prints
`auth: local endpoint (no auth)`, and the hint blamed a hosted provider's key.
Local endpoints now get a hint that matches that line.

Tests cover the measured guardrail body, the OpenAI-style code, a plain 403 on a
hosted endpoint, and a local 403.
macos_computer_use is registered under cfg(target_os = "macos") and its
description carries the live-machine safety policy (act only on the requested
task, prefer background AX over moving the cursor). On a Mac it exceeds both
caps, so `cargo test -p jcode-app-core --lib` fails on master:

  tool_descriptions_stay_under_token_cap: macos_computer_use (~159 tokens)
  tool_parameter_descriptions_stay_under_token_cap:
    $.properties.action (~93), $.properties.element (~28)

No CI job runs the app-core lib suite on macOS (the runner only compiles it),
so this has stayed green upstream. Each entry is load-bearing there: dropping
the description entry fails the first test, dropping either parameter entry
fails the second.

The exemption lines plus their one-line justifications are 5 LOC, so the
oversized-test baseline moves 1944 -> 1949 for
crates/jcode-app-core/src/tool/tests.rs, the documented "accept size growth
from a named change" refresh.
@ianalitis
ianalitis force-pushed the pr/clippy-1.98-lint-drift branch from c9712a3 to 2a306fb Compare October 11, 2026 17:49
@ianalitis ianalitis changed the title fix(lint): clear clippy 1.98 and rustfmt drift, repair the dev-bins bench build fix(mcp,openrouter): enforce the filtered MCP child env and report content-filter 403s accurately Oct 11, 2026
xenking added a commit to xenking/jcode that referenced this pull request Nov 22, 2026
Clear inherited MCP subprocess variables before applying the filtered configuration. Distinguish content-filter refusals and unauthenticated endpoint policies from hosted API-key failures.

Port of 1jehuang#1354

Upstream-PR: 1jehuang#1354
Co-authored-by: Iraklis Analitis <[email protected]>
@ianalitis ianalitis closed this by deleting the head repository Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant