Skip to content

fix(provider): name the active profile in logs, History and /usage, not the openrouter slot - #1288

Merged
1jehuang merged 1826 commits into
1jehuang:masterfrom
ningfangbin:fix/openai-compatible-profile-identity
Sep 19, 2026
Merged

1jehuang merged 1826 commits into
1jehuang:masterfrom
ningfangbin:fix/openai-compatible-profile-identity

Conversation

@ningfangbin

@ningfangbin ningfangbin commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1286

Problem

The openrouter provider is a multiplexing slot: it serves the public OpenRouter aggregator and every direct OpenAI-compatible profile (deepseek, nvidia-nim, zai, ...). Provider::name() is the stable machine id for the class (OpenRouter for the slot, openrouter for a concrete runtime instance), while display_name() resolves the active profile (DeepSeek). Surfaces that read the class id where a profile identity belongs reported DeepSeek sessions as OpenRouter:

  • every log line was tagged prv:OpenRouter / prv:openrouter
  • both History fallback paths could report the class name
  • /usage attributed spend to an openrouter bucket on hosts where OpenRouter is not configured and was never used (the ledger key is derived from that provider name)
  • KV-cache telemetry printed provider=OpenRouter for model=deepseek-flash

Per the contract clarified in #691 and the Provider trait docs, name() stays the machine id; the fix belongs in the surfaces that read it.

Changes

File Change
crates/jcode-app-core/src/agent/environment.rs log context (prv:) uses display_name()
crates/jcode-app-core/src/server/client_state.rs History provider name: the persisted-session path now maps both persisted shapes of a direct OpenAI-compatible profile (deepseek and openai-compatible:deepseek) to the profile's display name and falls back to display_name(); the handle_get_model_catalog busy-agent fallback (a second emission path into the same field) now does too. Import-source codes (opencode, claude-code, openai-codex) share the field but are not profile routes, so they stay verbatim
crates/jcode-base/src/provider_activity.rs source_key_for_provider_label keeps an already-prefixed openai-compatible:<id> label instead of falling through contains("openai") to openai:api-key
crates/jcode-tui/src/tui/app.rs KV-cache telemetry keeps the canonical provider family in provider= (the baseline identity the TTL/expiry classifier keys on) and adds a label= field with the profile, instead of overwriting the identity with a label

Provider::name() is untouched, so routing, auth and restore decisions are unchanged.

Evidence

Real-binary A/B on one machine, throwaway sandboxes per path.

Local run (jcode run -p deepseek -m deepseek-flash "hi"):

binary version log prefix
pristine master v0.84.75-dev (168358c3d) [ses:…|prv:openrouter|mod:deepseek]
this branch v0.84.57-dev (419b4fc97) [ses:…|prv:DeepSeek|mod:deepseek]

Daemon path (serve + a debug session, i.e. the multiplexing slot path that produced the real logs):

binary version log prefix
pristine master v0.84.75-dev (168358c3d) prv:OpenRouter|mod:deepseek ×14
this branch v0.84.57-dev (419b4fc97) prv:DeepSeek|mod:deepseek ×14

Both runs use two throwaway JCODE_HOME sandboxes and the same command (jcode run -p deepseek -m deepseek-flash "hi" for the first table; serve plus a debug session for the second), so the two prefixes never mix in one log file.

Release-binary evidence for the ledger half: on a host where jcode auth status reports openrouter not_configured, no OpenRouter credential exists and no OpenRouter model was ever selected, provider_activity.json still carried an openrouter bucket with real spend while the same host's logs held 503k+ prv:OpenRouter|mod:deepseek lines.

Tests

Every changed surface is pinned by a test that fails when its fix is reverted (verified by reverting each hunk and re-running):

  • agent::environment::tests::log_context_names_the_profile_not_the_transport_slot — new
  • server::client_state::tests::history_provider_name_maps_a_compatible_profile_key_to_its_label — new
  • server::client_state::tests::history_provider_name_maps_a_bare_compatible_profile_key — new (reverting the bare-key mapping fails it with left: Some("deepseek") / right: Some("DeepSeek"))
  • server::client_state::tests::history_provider_name_keeps_import_source_codes_verbatim — new
  • server::client_state::tests::history_provider_name_falls_back_to_the_profile_label — new
  • server::client_state::tests::source_key_mapping_keeps_an_already_prefixed_profile_key — new
  • tui::app::tests::kv_cache_baseline_identity_stays_canonical_while_the_label_is_the_profile — new (fails with left: "DeepSeek" / right: "openrouter" if the baseline stores the display label)
  • the existing handle_get_model_catalog_* tests now guard the busy fallback: the shared mock's display_name() differs from its name(), so reaching for name() fails the assertion
  • the existing history_provider_name_preserves_unknown_runtime_profile test moved from opencode-go (itself a catalog profile, i.e. no longer "unknown") to remote-catalog

Validation:

  • focused suites: jcode-base provider_activity 13/13, jcode-app-core history + log-context 5/5, jcode-tui kv_cache 13/13
  • full jcode-app-core --lib: 1225 passed / 24 failed on the branch vs 1223 passed / 23 failed on pristine master; the failure-name sets are identical apart from tool::bash::tests::test_background_command_respects_timeout, which passes when run alone (load-sensitive flake, unrelated)

An independent reviewer with clean context re-ran the suites, re-derived every revert failure, and reported no blocker; the two should-fix items it raised (a second History emission path, and overstated wording about name()'s casing) are addressed above.

Greptile's P2 was valid and is fixed: storing the rendered profile label in the KV-cache baseline disabled KvCacheMissReason::Expired for direct profiles, because cache_ttl_for_provider_model recognises provider families (openrouter), not labels (NVIDIA NIM). The baseline now keeps the canonical identity and the log line gained the separate label= field.

Tooling note

Developed with jcode running deepseek-v4-flash: the fix, the regression tests and the A/B evidence above come from that session, plus a second clean-context jcode subagent that reviewed the diff independently and found the second History emission path now fixed here.

CI notes

Two pre-existing guardrails are red on master independent of this PR, in files it does not touch:

  • cargo fmt --all --check also wants to reformat 9 files none of which are in this diff (jcode-harness-api-server/src/translate.rs, translate_tests.rs, jcode-protocol/src/wire.rs, jcode-provider-core/src/lib.rs, jcode-sdk/src/lib.rs, jcode-tui-markdown/src/lib.rs, jcode-tui/src/tui/app/remote/server_events.rs, jcode-tui/src/tui/backend.rs, src/cli/acp.rs). Only the two files this PR changes were formatted.
  • scripts/check_code_size_budget.py and scripts/check_test_size_budget.py already exit non-zero on pristine master (verified in a clean checkout of origin/master). This PR adds 2 lines to crates/jcode-tui/src/tui/app.rs, which is already over its recorded budget on master (2563 → 2565 LOC); the failure set is otherwise byte-identical.
  • clippy -D warnings also trips on a crate this PR does not touch: crates/jcode-render-core/src/markdown.rs:426 (clippy::collapsible_if), so clippy cannot currently be used as a signal for this diff.

Known limitations (deliberate scope)

1jehuang and others added 30 commits August 25, 2026 22:39
jcode and others added 22 commits September 12, 2026 04:01
# Conflicts:
#	crates/jcode-harness-api-server/src/translate_tests.rs
#	crates/jcode-harness-api/src/lib.rs
#	sdk/typescript/src/protocol.ts
…ibutors-20260913

Welcome pull requests from all contributors
@greptile-apps

greptile-apps Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

Safe to merge.

Findings

  1. P2 Keep TTL identity canonical ▶
Fix with agent prompt
### Issue 1
crates/jcode-tui/src/tui/app.rs:2470-2472
For direct OpenAI-compatible profiles, this saves a presentation label such as `NVIDIA NIM` as the KV-cache baseline provider. The expiration check later passes that value to TTL classification, which recognizes the canonical `openrouter` family but not `NVIDIA NIM`; after 301 seconds the miss is no longer labeled `Expired`. This is a non-blocking telemetry regression: idle-cache diagnostics lose the expiration reason for these profiles. Keep a canonical TTL provider identity separate from the rendered profile label.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This update keeps canonical provider identities for KV-cache TTL classification while using profile display names in user-facing diagnostics and related attribution paths.

Reviews (2) · Last reviewed commit: "fix(provider): name the active profile i..."

Comment on lines +2446 to +2450
.unwrap_or_else(|| self.provider.display_name())
} else {
self.provider.name().to_string()
// The profile the session talks to, not the transport slot that
// multiplexes every direct OpenAI-compatible profile (#1286).
self.provider.display_name()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Keep TTL identity canonical

For direct OpenAI-compatible profiles, this saves a presentation label such as NVIDIA NIM as the KV-cache baseline provider. The expiration check later passes that value to TTL classification, which recognizes the canonical openrouter family but not NVIDIA NIM; after 301 seconds the miss is no longer labeled Expired. This is a non-blocking telemetry regression: idle-cache diagnostics lose the expiration reason for these profiles. Keep a canonical TTL provider identity separate from the rendered profile label.

Artifacts

Rust harness for NVIDIA NIM TTL classification

  • The authored executable harness selects the repository's real NVIDIA NIM profile and compares its prior canonical provider value with its changed display-label value, proving the differing expiration decisions.

NVIDIA NIM TTL classification harness output

  • Captured output from running the Rust harness shows `openrouter` is expired at 301 seconds while `NVIDIA NIM` has no TTL and is not expired, confirming the regression.

View artifacts

T-Rex Ran code and verified through T-Rex

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-tui/src/tui/app.rs
Line: 2446-2450

Comment:
**Keep TTL identity canonical**

For direct OpenAI-compatible profiles, this saves a presentation label such as `NVIDIA NIM` as the KV-cache baseline provider. The expiration check later passes that value to TTL classification, which recognizes the canonical `openrouter` family but not `NVIDIA NIM`; after 301 seconds the miss is no longer labeled `Expired`. This is a non-blocking telemetry regression: idle-cache diagnostics lose the expiration reason for these profiles. Keep a canonical TTL provider identity separate from the rendered profile label.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

…ot the openrouter slot

The OpenRouter provider is a multiplexing slot: `Provider::name()` reports the
provider *class* (`OpenRouter` for the slot, `openrouter` for a concrete
runtime instance) while `display_name()` resolves the profile the session
actually talks to (DeepSeek, NVIDIA NIM, ...). Surfaces that read the class id
where a profile identity belongs therefore reported DeepSeek sessions as
OpenRouter:

- the log prefix tagged every line `prv:OpenRouter` / `prv:openrouter`
- both `History` emission paths could report the class name: the
  persisted-session path fell back to `provider.name()`, and
  `handle_get_model_catalog`'s busy-agent fallback always did
- `/usage` attributed spend to an `openrouter` bucket on hosts where OpenRouter
  is not configured and was never used (the ledger key is derived from that
  provider name)
- KV-cache telemetry printed `provider=OpenRouter` for a DeepSeek model

Report the profile-aware label instead. A direct OpenAI-compatible profile is
persisted both bare (`deepseek`, the session vocabulary) and as its source key
(`openai-compatible:deepseek`), so both shapes now report the profile's display
name; the existing "unknown runtime profile" test moved to a genuinely unknown
key (`remote-catalog`) because `opencode-go` is itself a catalog profile.
Import-source codes (`opencode`, `claude-code`, `openai-codex`) share the same
field but are not profile routes, so they stay verbatim. An already-prefixed key
also survives `source_key_for_provider_label`, which used to fall through
`contains("openai")` and bill `openai:api-key`.

KV-cache telemetry keeps the canonical family id in `provider=` (the baseline
identity the TTL/expiry classifier keys on) and adds a separate `label=` field
with the profile, so diagnostics stay correct for both machines and people.

`Provider::name()` keeps its documented contract as the stable machine-facing
identifier, so routing, auth and restore decisions are untouched.

Known limitations (deliberate scope): `@provider`-pinned models still persist
`provider_key = "openrouter"` (route/restore identity, overlaps 1jehuang#1116);
structured telemetry keeps the class id per the `name()` contract; and the
ledger write has no end-to-end test because it is TUI-side and spawns a writer
thread.

Closes 1jehuang#1286
@ningfangbin
ningfangbin force-pushed the fix/openai-compatible-profile-identity branch from 419b4fc to 1b07253 Compare September 17, 2026 09:34
@ningfangbin

Copy link
Copy Markdown
Contributor Author

Reviewed the Greptile P2 and fixed it in 1b07253.

KV-cache TTL identity (valid): kv_cache_provider_name() had started returning the rendered profile label (DeepSeek, NVIDIA NIM), which is the value stored in KvCacheBaseline.provider and later passed to cache_ttl_for_provider_model. That classifier matches provider families (openrouter, openai, anthropic, gemini, ...), so direct OpenAI-compatible profiles silently lost KvCacheMissReason::Expired.

Fix: the baseline identity is canonical again (provider.name() / the server-provided provider name), and the human-facing label moved to a separate field in the log line:

KV_CACHE_USAGE: turn=… call=… provider=openrouter label=DeepSeek upstream=None model=deepseek-flash …

provider= keeps what the TTL/expiry classifier and the baseline comparison key on; label= is what a person reading the diagnostic wants (issue #1286 symptom 3). New regression test tui::app::tests::kv_cache_baseline_identity_stays_canonical_while_the_label_is_the_profile pins both halves and fails with left: "DeepSeek" / right: "openrouter" if the baseline goes back to storing a label.

@1jehuang
1jehuang merged commit 140b6d1 into 1jehuang:master Sep 19, 2026
1 check passed
@1jehuang

Copy link
Copy Markdown
Owner

Merged into master. The combined candidate passed history-provider, model-catalog, log-context, provider-activity and KV-cache identity tests. Real isolated CLI calls using the OpenAI-compatible profile returned History provider OpenAI-compatible, produced profile-aware log prefixes, and produced zero incorrect OpenRouter labels. Direct-profile mappings are covered by the focused tests.

Integrated with original PR ancestry and contributor attribution preserved, applying only the actual contribution rather than the divergent old upstream tree. Validation is local targeted testing and isolated public-interface acceptance, not a claim that hosted CI is green.

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.

Direct OpenAI-compatible providers are reported as openrouter in logs and in /usage spend

3 participants