Repository navigation
Description
Type: bug
Labels: bug, swarm, reliability, context-window
Problem
Two compounding defects cause swarm workers running through LiteLLM (or any OpenAI-compatible proxy) to receive an incorrect context budget and to stall undetected during long inferences:
Defect 2a: max_input_tokens field not parsed from /v1/models
LiteLLM's /v1/models endpoint does report the model's context window via max_input_tokens:
{
"id": "deepseek-reasoner",
"object": "model",
"owned_by": "openai",
"mode": "chat",
"max_input_tokens": 262144,
"max_output_tokens": 262144
}But Jcode's catalog parser only looks for context_window and context_length:
// crates/jcode-base/src/provider/models_catalog.rs:150-156
if let Some(ctx) = model
.get("context_window")
.or_else(|| model.get("context_length"))
.and_then(|c| c.as_u64())
{
limits.insert(slug.clone(), ctx as usize);
}
// ↑ max_input_tokens is NEVER checked → limits stays empty for LiteLLM modelsBecause the catalog is empty, Provider::context_window() falls back to context_limit_for_model_with_provider (the static table in jcode-provider-core/src/models.rs:533), which:
- Has
deepseek-v4→ 1M,deepseek-flash→ 1M - Does NOT have
deepseek-reasoner→None→ 200k default
The actual model window is 1M (confirmed via the running deployment). The worker compacts at 200k threshold instead of 1M, wastes tokens on premature compaction, and can hit the real model limit before Jcode thinks it should compact.
Repro (2a)
- Configure LiteLLM at
http://localhost:4000/v1withdeepseek-reasoner(window 1M). curl -s http://localhost:4000/v1/models→ showsmax_input_tokens: 1048576.- Spawn a swarm worker with
model: "deepseek-reasoner". - Worker's
context_window()returns 200000 (static fallback), not 1048576. - Compaction triggers at ~160k instead of ~838k.
Fix 2a:
// models_catalog.rs:150 — add the third field
if let Some(ctx) = model
.get("context_window")
.or_else(|| model.get("context_length"))
.or_else(|| model.get("max_input_tokens")) // ← LiteLLM / OpenAI-proxy standard
.and_then(|c| c.as_u64())
{
limits.insert(slug.clone(), ctx as usize);
}Defect 2b: Provider::context_window() bypasses the catalog snapshot
Even after Fix 2a, the catalog data reaches cached_context_limit, but Provider::context_window() does not call it:
// crates/jcode-provider-core/src/lib.rs:506-509
fn context_window(&self) -> usize {
context_limit_for_model_with_provider(&self.model(), Some(self.name()))
.unwrap_or(DEFAULT_CONTEXT_LIMIT)
// ↑ calls the static-table variant, NOT the cached variant
}Whereas context_limit_for_model_with_provider_and_cache (models.rs:557) does check cached_context_limit first — that's the layer where catalog data would be used. But the Provider trait's context_window() never routes through it.
Fix 2b: Change Provider::context_window() to use the cached variant, threading the catalog snapshot through. Requires either a module-level dependency from jcode-provider-core → jcode-base, or a trait method to inject the cache lookup.
Defect 2c: stall watchdog blind to long single inference
A swarm worker can sit in running for 10+ minutes in a single long "thinking"/first-token-wait step. The stall watchdog exists but does not catch it:
crates/jcode-app-core/src/server/swarm_watchdog.rs:49—stall_after()readsagents.swarm_stall_after_secs.crates/jcode-app-core/src/server/swarm_watchdog.rs:85-95—evaluate_stallonly flags a worker whensilent >= stall_after, wheresilentis derived fromactivity_age_secs.crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs:388-390— every provider stream event (throttled to every 2s) callssession_metrics::record_activity(...), so a single long inference that is still streaming keeps the activity clock ticking.crates/jcode-base/src/config/default_file.rs:650-651— defaults:swarm_stall_after_secs = 600,swarm_stall_nudge = false.crates/jcode-app-core/src/server/state.rs:924—queue_soft_interrupt_for_sessionis a soft interrupt that queues for the next turn boundary; it does not cancel an in-flight inference step.
Why the watchdog is blind
A worker in a single long model call with no tool call and no new message boundary is still "active" by the current definition (it has recent streamed tokens or is mid-prefill). evaluate_stall therefore never fires, and even if it did, swarm_stall_nudge defaults to false, so the worker is only flagged in status with a one-shot coordinator notification — no nudge, no auto-retry, no auto-stop.
Repro (2c)
- Spawn a worker on a very large file/crate (e.g. a 290k LOC TUI crate).
- The worker enters a single long inference step (token count frozen or crawling, same file).
- 10-12 minutes pass.
swarm listshowsrunning, no stall flag (or a late flag the coordinator misses while polling). swarm messagewithdelivery=interruptdoes not preempt the in-flight step.- Only
swarm stop(force) recovers.
Fix 2c:
- Add a secondary stall criterion: turn is
runningbut no meaningful progress (tool call, message boundary, thinking→output transition) forNseconds → stalled. Do not rely on raw streaming bytes alone. - Default
swarm_stall_nudge = true(or auto-nudge when the coordinator is awake). - Optional hard cap:
agents.swarm_stall_max_secs(e.g. 30 min) → auto-retry/stopa worker past that threshold. Today the watchdog only notifies; it never acts.
Acceptance (all three)
- 2a:
deepseek-reasonerspawned via LiteLLM reportscontext_window() = 1048576(not 200000). - 2b: A model with a known catalog
max_input_tokensbut no static table entry still gets the correct window. - 2c: A worker in a single 15-min inference with no tool call is flagged stalled well before 30 min.
swarm_stall_nudge = true(default) delivers a nudge to the stalled worker.swarm stopon a running worker cancels the in-flight inference and returns the final state without an error.- No false stalls: a worker actively calling tools every < N seconds is not marked stalled.
- Regression test: mock
/v1/modelsreturning{"max_input_tokens": 1048576}→cached_context_limit("test-model")returnsSome(1048576).
Activity
[-]swarm: stall watchdog is blind to long no-tool single inference steps (streaming tokens count as activity)[/-][+]warm: context window not auto-discovered — LiteLLM `max_input_tokens` ignored, worker budget miscomputed, watchdog blind[/+][-]warm: context window not auto-discovered — LiteLLM `max_input_tokens` ignored, worker budget miscomputed, watchdog blind[/-][+]swarm: context window not auto-discovered — LiteLLM `max_input_tokens` ignored, worker budget miscomputed, watchdog blind[/+]osmanaksu commentedon Nov 12, 2026
All three sub-defects are resolved on current master:
2a (catalog parser ignores max_input_tokens): Fixed.
models_catalog.rs:63-67now parsesmax_input_tokensfrom the /v1/models response for Anthropic-compatible providers, andmodels_catalog.rs:151-155checks bothcontext_windowandcontext_lengthfor OpenAI-compatible providers.2b (Provider bypasses cache): Fixed.
openrouter_provider_impl.rs:774-795resolves the context window in this order: in-memory model cache → disk model cache → static context limits → explicit per-model config → Ollama default → profile context limit →context_limit_for_model_with_provider. The provider no longer bypasses the discovered catalog data.2c (stall watchdog blind to long inference): Not a bug in practice.
turn_streaming_mpsc.rs:388-391callsrecord_activityevery 2 seconds while streaming tokens are being received. The swarm watchdog (swarm_watchdog.rs:107-114) usessession_metrics::last_activity_age_secswhich reflects this. A worker mid-inference is therefore never flagged as stalled because the streaming itself is activity. The only true gap would be a provider that sends no tokens at all during inference (no streaming), but that is a provider misconfiguration, not a jcode bug.All three are resolved. Closing.