Skip to content

fix: cold gateway metadata triggers premature compaction on resumed prompts #1541

Closed

Description

@JAORMX

Bug

The first prompt after resuming a conversation following a server restart can compact its history using the 128,000-token unknown-model fallback, before asynchronous model discovery restores the model's actual context window.

Observed with stacklok-gateway and gpt-5.6-sol (1,050,000-token context window, displayed as approximately 1.1M). The configured default, gpt-5.6-terra, also has a large context window. Switching to a smaller default is not necessary to reproduce this bug: the reproduced inference model remains gpt-5.6-sol.

Opening the resumed conversation does not trigger compaction. Sending the first prompt does. This affects actual engine compaction, not only the footer display.

Impact

At the default 80% threshold:

  • Actual model window: 1,050,000 → approximately 840,000-token compaction threshold.
  • Temporary fallback: 128,000 → approximately 102,400-token compaction threshold.

A conversation comfortably within the actual model limit is unnecessarily summarized. Metadata healing afterward does not restore the pre-compaction active history. The reproducer also observes the compaction archive event; this is not a claim that all archived history is deleted.

Deterministic offline reproduction

An exploratory reproducer passed against commit f1d83593c using:

  • Two real app.Build instances to exercise restart.
  • Durable JSONL session storage.
  • A custom stacklok-gateway registration.
  • A local TLS httptest model-listing endpoint advertising a 1,050,000-token window.
  • The real per-session engine factory, context-window resolver, and compaction path.
  • Mock inference, with no real gateway requests or model spend.
  • A synchronization barrier holding the second Build's asynchronous /models response, rather than a sleep-based race.

Procedure:

  1. Start the first composition and let live metadata load.
  2. Establish and persist a completed Sol conversation with approximately 150k estimated history tokens: above the fallback threshold but below the real threshold.
  3. Restart composition using the same durable store.
  4. Resume the session and send a prompt while live metadata refresh is held.
  5. Capture the actual engine window, inference model, compaction events, and archived history.
  6. Release model discovery and observe metadata healing.
  7. Compare with a restarted control where discovery finishes before the resumed prompt.

Results:

Scenario Engine window History estimate Compaction events
Original composition, metadata loaded 1,050,000 150,231 0
Resumed, metadata loaded 1,050,000 150,250 0
Resumed, metadata refresh held 128,000 approximately 150k before compaction 1

The cold run retained gpt-5.6-sol as the inference model, archived 33 pre-compaction messages, and reduced request history to 178 estimated tokens using the test compactor. The client-facing window was provisional (0) before discovery; the engine nevertheless used 128000. After releasing /models, the echoed window healed to 1050000.

The disposable reproducer is local, not committed or attached to this issue:

  • .scratch/premature-compaction-repro-20260915-01/zz_premature_compaction_repro_test.go
  • .scratch/premature-compaction-repro-20260915-01/overlay.json
  • .scratch/premature-compaction-repro-20260915-01/run.log

It runs as a Go test overlay against ./internal/app, selecting TestDisposablePrematureCompactionAfterGatewayRestart. These paths document the investigation artifacts, not files available in a repository checkout.

Root cause

Custom-provider context metadata is live-only and held in memory. Catalog lookup uses the exact provider/model namespace: canonical OpenAI or OpenRouter catalog entries do not automatically supply limits for a custom stacklok-gateway entry.

After restart:

  1. The custom provider's initial catalog seed has no context-window metadata.
  2. Live discovery runs asynchronously.
  3. The correctly rehydrated engine evaluates its context-window resolver on the first resumed prompt.
  4. The resolver converts an unknown window into 128k, without distinguishing provisional discovery from a settled unknown value for compaction purposes.
  5. maybeCompact runs before the provider request, modifying active history before metadata arrives.

Relevant locations at the investigated revision:

  • internal/app/livemeta.go:104 — provider-keyed metadata seeding.
  • internal/app/modellister.go:460 — custom-provider seed without context metadata.
  • internal/app/livemeta.go:310 — window-resolution precedence.
  • internal/app/livemeta.go:348 — engine's 128k unknown-window fallback.
  • internal/app/build.go:2628 — asynchronous refresh startup.
  • internal/app/build.go:3113 — per-session resolver construction.
  • engine/agent/loop.go:1617 — first-turn request/compaction ordering.
  • engine/agent/loop.go:2749 — compaction threshold evaluation.

This reproduces the failure mechanism, not the exact discovery timing of the original live incident.

Expected behavior / acceptance criteria

  • A resumed conversation must not be compacted against a transient 128k fallback merely because discovery of its actual model window is still pending.
  • Add an offline restart regression covering a custom provider with a large live-only window, delayed discovery, durable session recovery, and the first resumed prompt.
  • The regression must assert actual compaction/archive behavior and selected model identity, not just the UI window echo.
  • A refresh-ready control below the actual threshold must not compact; normal compaction above a resolved threshold must remain functional.
  • Define bounded behavior for discovery failure/unavailability: no indefinite wait, silent assumption of a large window, or blind oversized inference request.
  • Preserve explicit operator context-window overrides and honest behavior for genuinely unknown models.

Fix direction (design not yet selected)

Protect the first compaction decision by distinguishing provisional metadata from a resolved limit. Options include bounded metadata resolution before driving the resumed request, or a suitably validated durable last-known metadata cache. A footer-only refresh is insufficient. Do not automatically equate every OpenAI-compatible gateway with the canonical OpenAI catalog.

Existing operational mitigation: an explicit context-window override or an exact provider/model entry in operator models.context_windows takes precedence over live discovery. Scope overrides carefully to the models they actually describe.

No production fix has been implemented.

Activity

  1. self-assigned this
    on Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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