Skip to content

fix(provider): fail fast on content-filter blocks, cap shared-pool 429 retries, local PAN pre-check - #1511

Closed
ianalitis wants to merge 8 commits into
1jehuang:masterfrom
ianalitis:pr/pan-precheck-content-filter
Closed

ianalitis wants to merge 8 commits into
1jehuang:masterfrom
ianalitis:pr/pan-precheck-content-filter

Conversation

@ianalitis

@ianalitis ianalitis commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

OpenRouter workspace guardrails can block a request with 403 request blocked by content filter: [credit_card]. Their built-in card matcher does not check a Luhn checksum, so ordinary text trips it. In our case it was a shell loop over four consecutive four-digit issue numbers. Two things then made a single false positive expensive:

  1. The provider layer already treats a 403 as final, but the TUI's remote auto-retry did not recognise the block. Every turn resends the full history, so the same blocked text was resent until the operator stopped the session.
  2. When OpenRouter returned a 429 with limit_source: upstream_provider_shared_pool, the retry ladder ran in full, even though OpenRouter had already tried every provider it could route to. On 2026-09-24 we logged 394 such 429s against openrouter/auto, and each one held the whole retry budget.

Change

Commit 1: fix(provider): fail fast on content-filter 403s and cap shared-pool 429 retries

Commit 2: feat(provider): local pre-send PAN check with Luhn for OpenAI-compatible routes

  • New jcode-provider-core::failover::pan_check scans every string in messages before send. It looks for 15- or 16-digit contiguous runs, or 4-4-4-4 and 4-6-5 groups joined by one repeated space or dash. A match needs a valid issuer prefix, the matching brand length and a valid Luhn checksum. A digit run glued to an ASCII letter or digit does not count.
  • On a match the request fails closed before anything leaves the machine. The error names the message index and role, never the value, and keeps the blocked by content filter: [PAN] wording so commit 1's fail-fast path applies.
  • Every base is checked, including loopback: a local endpoint may forward requests remotely without HTTP redirects or system proxies. There is no automatic PAN exemption. JCODE_DISABLE_PAN_CHECK=1 is the explicit escape hatch for a false positive. Separately, parsed loopback bases always use a no-proxy transport on initial sends and retries. Redirects are followed only to other parsed loopback destinations, with a ten-hop limit. Remote redirects stop before dispatch. Client construction errors fail closed, never fall back to a proxy-aware client.
  • Local compatibility: loopback_clean_request_bypasses_proxy_and_follows_local_redirects covers clean POSTs through both 307 and 308 local redirects, without any proxy connection. It fails with the no-redirect client and passes with bounded loopback-only redirects. Card-bearing requests intentionally remain blocked by default, even for local endpoints, because arbitrary local services may forward them remotely. For deliberate synthetic-card work on a trusted local model, launch that process with JCODE_DISABLE_PAN_CHECK=1; this explicit opt-out is covered by explicit_opt_out_allows_local_card_test_data. It is not an automatic exemption and applies to the whole process, including any remote requests.
  • Forwarding regression: loopback_pan_is_blocked_before_a_forwarder_can_receive_it fails on 6ee407fd5 because a connection reaches the local endpoint, then passes with the exemption removed. No external endpoint or literal card fixture is used. Runtime suite after the transport/redirect follow-ups: 147 passed, 1 ignored. Target-crate clippy (--no-deps -- -D warnings) passes; dependency-inclusive clippy remains blocked by the existing jcode-core::stdin_detect range-loop lint.

Validation

  • cargo test -p jcode-provider-core -p jcode-provider-openrouter-runtime: 145 + 142 passed.
  • New tests:
    • content_filter_block_is_never_retried
    • shared_pool_429_retries_at_most_twice
    • pan_pre_send_blocks_a_card_number_without_echoing_it
    • the pan_check unit tests (issue lists, mixed separators, glued digits, bad Luhn, each brand)
    • the TUI remote-event regression for the continuation resend
  • A differential fuzz outside this diff (40k seeded generated cases) compared the extracted Rust functions with an independent Python port and found 0 mismatches. The fuzz catches real divergence: against a simpler regex-only matcher it found 7,170 mismatches.
  • A local scan of 1,500 recent session files found no hits other than published processor test numbers.

Test fixtures build card-shaped values at runtime, so no card-number literal appears in the diff.

@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] No outstanding blocking findings are identified; the established evidence supports the card-check and retry safeguards described in this PR.

Summary

This PR stops automatic resends after content-filter blocks, limits shared-pool 429 retries, and checks outgoing messages for card numbers.

  • Requests with card numbers stop before they reach a provider.
  • Content-filter blocks stop the TUI from retrying the same request.
  • Shared-pool rate limits stop retrying after two retries.

Reviews (9) · Last reviewed commit: "fix(openrouter): allow bounded loopback ..." · Reviewed by Greptile

@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Comments Outside Diff

These findings could not be posted inline.

  • P1 Keep local requests direct crates/jcode-provider-openrouter-runtime/src/openrouter_sse_stream.rs:116 ▶

    If HTTP_PROXY or ALL_PROXY is set without a matching NO_PROXY entry, a PAN-free request to a loopback model now uses a client that can send it through the proxy. A proxy that cannot reach the operator’s local service makes the request fail instead of reaching Ollama or LM Studio. Keep direct loopback transport separate from the card-number check, including on retries.

@ianalitis

Copy link
Copy Markdown
Contributor Author

@greptileai review

@ianalitis

Copy link
Copy Markdown
Contributor Author

@greptileai review

@ianalitis

Copy link
Copy Markdown
Contributor Author

Non-TUI error blocks still blame the key: wire content_filter_block_label into the delivered 403 path?

This branch already handles a content-filter block correctly on one path: terminal_errors.rs calls content_filter_block_label on a stream error, stops retrying, and tells the user the match is in the history. The provider error path in openrouter_sse_stream.rs still does not read the delivered body, so the same block comes back as a credential problem in jcode run and in any client that prints the error block rather than the TUI notice.

Measured with headless jcode run against a loopback OpenAI-compatible stub, on jcode v0.93.10-dev (4ceceaa8b); the arm below is byte-identical on master 02ae3edc0:

  status: 403 Forbidden
  response: {"error": {"message": "Request blocked by content filter: [CREDIT_CARD]", "code": 403}}
Hint: the provider rejected the API key. Check that the key is valid and allowed to use this model, or switch to another provider with /model.

http_status_hint (crates/jcode-provider-openrouter-runtime/src/openrouter_sse_stream.rs:25, called at :255) answers every 401/403 that way, including a loopback endpoint where the block two lines above prints auth: local endpoint (no auth).

That text is newer than this branch: 80b8c0204 (2026-09-29) introduced it, after this branch's merge-base b5a4cde7a (2026-09-23), so nothing here is a regression. A merge does not remove the case either: git merge-tree --write-tree origin/master 6feb8610 (read only, resolved by my local mergiraf driver, tree 8c3905389) keeps both http_status_hint at that call site and pan_pre_send_block, so the merged tree still prints the key blame for a body that names a content filter.

This branch's non-2xx path takes local_endpoint_troubleshooting_hint(&api_base, &model) only (:306), which is what it did at its merge-base, so the parser added here is one call away from covering the case: read the body, and when it names a content filter, say the text is the problem rather than the credential, which is the position content_filter_block_is_never_retried already takes for the pre-send block.

I have that as a small patch against master: names_content_filter(body) plus a CONTENT_FILTER_HINT preferred over the status hint, and the 401 | 403 arm split so a local endpoint stops blaming a hosted key (+71/-1, three tests, cargo test -p jcode-provider-openrouter-runtime --lib 157 passed). Happy to push it as a commit on this branch or as a separate PR, whichever fits the branch.

…29 retries

An OpenRouter guardrail block is decided by text in the conversation, and
every turn resends the full history, so the TUI auto-retry continuation
resent the same blocked request every few seconds (measured 2026-09-25).
Treat 'blocked by content filter' like the #387 model/endpoint mismatch:
clear the retry, stop auto-poke, and name the entity with what to do.
The fail-fast branch moves to remote/terminal_errors.rs so server_events.rs
shrinks under its size budget.

429s that OpenRouter attributes to upstream_provider_shared_pool now retry
at most twice instead of the full configured ladder.

(cherry picked from commit 5feca93)
…ble routes

Gateway guardrails match card shapes without a checksum, so four-digit
issue lists read as cards. Before a request leaves the machine, scan
every string in messages for 15/16 contiguous digits or 4-4-4-4 / 4-6-5
groups with an issuer prefix and a valid Luhn checksum. On a match, fail
closed naming the message index and role, never the value. The error
keeps the 'blocked by content filter: [PAN]' wording so the TUI
fail-fast path stops auto-retry. Loopback bases are exempt and
JCODE_DISABLE_PAN_CHECK=1 is the escape hatch.

Scan of 1,500 recent local session files: no hits other than published
processor test numbers.

(cherry picked from commit e7f83a9)
- Exempt loopback only when the parsed host is exactly localhost or a
  loopback IP, so hosts like localhost.example.com stay checked.
- JCODE_DISABLE_PAN_CHECK opts out only when set to 1.
- Report the message role from a closed set, never request text.
- Stop auto-poke on a content-filter block, including the local one.
- Cap shared-pool retries only for HTTP 429.
A loopback endpoint can redirect or proxy the request body to a remote
host, so the loopback exemption let a card number leave the machine.
Drop it; JCODE_DISABLE_PAN_CHECK=1 remains the explicit escape hatch.
@ianalitis
ianalitis force-pushed the pr/pan-precheck-content-filter branch from 6feb861 to a621564 Compare October 11, 2026 20:43
@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