Skip to content

swarm: context window not auto-discovered — LiteLLM max_input_tokens ignored, worker budget miscomputed, watchdog blind #2149

Closed

Description

@osmanaksu

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 models

Because 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)

  1. Configure LiteLLM at http://localhost:4000/v1 with deepseek-reasoner (window 1M).
  2. curl -s http://localhost:4000/v1/models → shows max_input_tokens: 1048576.
  3. Spawn a swarm worker with model: "deepseek-reasoner".
  4. Worker's context_window() returns 200000 (static fallback), not 1048576.
  5. 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() reads agents.swarm_stall_after_secs.
  • crates/jcode-app-core/src/server/swarm_watchdog.rs:85-95 — evaluate_stall only flags a worker when silent >= stall_after, where silent is derived from activity_age_secs.
  • crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs:388-390 — every provider stream event (throttled to every 2s) calls session_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_session is 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)

  1. Spawn a worker on a very large file/crate (e.g. a 290k LOC TUI crate).
  2. The worker enters a single long inference step (token count frozen or crawling, same file).
  3. 10-12 minutes pass. swarm list shows running, no stall flag (or a late flag the coordinator misses while polling).
  4. swarm message with delivery=interrupt does not preempt the in-flight step.
  5. Only swarm stop (force) recovers.

Fix 2c:

  1. Add a secondary stall criterion: turn is running but no meaningful progress (tool call, message boundary, thinking→output transition) for N seconds → stalled. Do not rely on raw streaming bytes alone.
  2. Default swarm_stall_nudge = true (or auto-nudge when the coordinator is awake).
  3. Optional hard cap: agents.swarm_stall_max_secs (e.g. 30 min) → auto-retry/stop a worker past that threshold. Today the watchdog only notifies; it never acts.

Acceptance (all three)

  • 2a: deepseek-reasoner spawned via LiteLLM reports context_window() = 1048576 (not 200000).
  • 2b: A model with a known catalog max_input_tokens but 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 stop on 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/models returning {"max_input_tokens": 1048576} → cached_context_limit("test-model") returns Some(1048576).

Activity

  1. changed the title [-]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[/+] on Nov 12, 2026
  2. changed the title [-]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[/+] on Nov 12, 2026
  3. osmanaksu commented on Nov 12, 2026

    @osmanaksu
    Author

    All three sub-defects are resolved on current master:

    2a (catalog parser ignores max_input_tokens): Fixed. models_catalog.rs:63-67 now parses max_input_tokens from the /v1/models response for Anthropic-compatible providers, and models_catalog.rs:151-155 checks both context_window and context_length for OpenAI-compatible providers.

    2b (Provider bypasses cache): Fixed. openrouter_provider_impl.rs:774-795 resolves 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-391 calls record_activity every 2 seconds while streaming tokens are being received. The swarm watchdog (swarm_watchdog.rs:107-114) uses session_metrics::last_activity_age_secs which 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions