Skip to content

Keep the context length a load was given instead of resetting it to Auto - #9841

Merged
danielhanchen merged 5 commits into
mainfrom
ctx-pin-survives-load
Aug 27, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
ctx-pin-survives-load

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The report

Setting Context Length explicitly and loading works once, then silently reverts to Auto on every load after it. Reported as "it seems like its ignoring my Context Length and always stop at Auto (8,192)", with the slider pushed to 262,144.

On a model that fits no GPU subset, Auto resolves to the host-offload fallback of 8,192. So a user who asked for 262,144 gets 8,192, and nothing on screen says why.

The backend was not at fault

It launches -c 262144 for every shape tested: a 400 GB MoE on one card, on two cards, on no GPU at all, with manual or auto layers, and with -ncmoe passed as an extra argument. The route passes max_seq_length through verbatim and the Pydantic bound is 1048576. 8,192 comes out only when the request arrives asking for Auto.

What actually happened

The frontend threw the pin away the moment the load succeeded. All four post-load writers derived what to keep from resolveManualAutoCtxPin, which answers a narrower question:

gpuMemoryMode === "manual" && gpuLayers < 0 && (customContextLength ?? 0) > 0

The Context Length control is offered in every GPU Memory mode, and resolveLoadMaxSeqLength honors a pin in every mode, so the survival rule was narrower than the send rule. On Default, the pin became null the instant the load completed. customContextLength is the first field of the settings panel's React key, so that flip remounted the editor, which re-seeded from the cleared config and rendered the literal "Auto". The next load sent 0.

The user's first load really did run at 262,144. It is the second one that drops to 8,192, which is why it reads as "always stops at Auto".

The four writers also disagreed with each other, which is the clearest sign this was a bug and not a policy:

site what it wrote
auto-load path kept the pin
compare path kept the pin
interactive path dropped it
status refresh dropped it unconditionally, on every refresh

So even the two paths that kept the pin lost it on the next status refresh.

The fix

resolveLoadedCtxPin replaces the predicate with the question actually being asked: what context did this load request? Positive keeps it, 0 means Auto and clears it. GPU memory mode is not a parameter.

The backend already publishes exactly this value as requested_context_length, and the status path was reading it and then feeding it into the mode filter that discarded it. All four writers now source the n_ctx their load put on the wire, so they agree. resolveManualAutoCtxPin had no callers left and is retired; the send-side resolveFitMaxSeqLength it was named after stays.

Widening the pin exposed a window that was harmless while it was almost always null: a status refresh can land with modelLoading still true, and a stale poll would then plant the outgoing model's context on the model coming in. The pin now takes the same seedLoadParams rule the file already applies to every other load parameter, leaving performLoad to own the field mid-load. A model change still carries nothing across, because the value is sourced from the status describing the active load and never from previous state.

No backend change. git status touches studio/frontend only.

Tests

New studio/frontend/tests/context-pin-survives-load.test.ts, covering the pin surviving a completed load in all four mode and layer combinations, the panel not remounting to Auto, the next load still sending 262,144, an Auto load still clearing the pin and staying Auto, a model change carrying nothing across, the mid-load window, all four writers, and the retirement.

  • With the change: 8 of 8 pass.
  • With studio/frontend/src reverted: 0 of 8 pass.
  • Full frontend suite: 5656 passed, 0 failed.
  • npm run typecheck clean. Lint diffed against the base on the five changed files: no new findings.

Follow-up, not in this PR

Tensor parallel silently reduces an explicit context by a much larger margin, as low as a fifth of what was asked for, announced only as a log line. It can be reached without touching the Tensor Parallel toggle, including through an inherited LLAMA_ARG_SPLIT_MODE=tensor. This fix makes that reduction visible for the first time, since the pin now holds the requested value, but the existing capacity copy names the post-reduction number and reads as a warning about the future rather than a statement about what already happened. That deserves its own change.

Setting Context Length explicitly and loading works once, then silently reverts
to Auto on every load after it. On a model that fits no GPU subset, Auto
resolves to the host-offload fallback of 8192, so a user who asked for 262144
gets 8192 and no indication why.

The backend was never at fault. It launches `-c 262144` for every shape I
tested: a 400 GB MoE on one card, on two, on none, with manual or auto layers,
with `-ncmoe` extras. The route passes `max_seq_length` through verbatim. 8192
appears only when the request arrives asking for Auto.

The frontend discarded the pin the moment the load succeeded. All four
post-load writers derived what to keep from `resolveManualAutoCtxPin`, which
answers a narrower question: it returns the pin only under Manual GPU memory
with auto layers, and null in every other mode. The Context Length control is
offered in every mode, and `resolveLoadMaxSeqLength` honors a pin in every mode,
so the survival rule was narrower than the send rule. On Default the pin became
null the instant the load completed; `customContextLength` is the first field of
the settings panel's React key, so the flip remounted the editor, it re-seeded
from the cleared config and rendered "Auto", and the next load sent 0.

The four writers also disagreed with each other, which is the clearest sign this
was a bug rather than a policy: the auto-load and compare paths kept the pin,
the interactive path dropped it, and the status path dropped it unconditionally
on every refresh, so even the two that kept it lost it moments later.

`resolveLoadedCtxPin` replaces the predicate with the question actually being
asked: what context did this load request? Positive means keep it, 0 means Auto
and clears it. GPU memory mode is not a parameter. The backend already publishes
exactly this as `requested_context_length`, which the status path was reading
and then feeding into the mode filter that threw it away.
`resolveManualAutoCtxPin` had no callers left and is retired; the send-side
`resolveFitMaxSeqLength` it was named after stays.

Widening the pin exposed a window that was harmless while it was almost always
null: a status refresh can land with `modelLoading` still true, and a stale poll
would then plant the outgoing model's context on the model coming in. The pin
now takes the same `seedLoadParams` rule the file already applies to every other
load parameter, leaving performLoad to own the field mid-load. A model change
still carries nothing across, because the value is sourced from the status
describing the active load and never from previous state.

No backend change.

Tests cover the pin surviving in all four mode and layer combinations, the panel
not remounting to Auto, the next load still sending the user's number, an Auto
load still clearing the pin, a model change carrying nothing across, the
mid-load window, and all four writers. All 8 fail without the source change.
…d one

`test_chat_autoload_failure_gate.py` slices `autoLoadSmallestModel` out of
chat-adapter.ts and evaluates it against a stub preamble. Retiring
`resolveManualAutoCtxPin` left its stub behind and gave the region a
`resolveLoadedCtxPin` the harness had never heard of, so every scenario in the
file failed.

The stub mirrors the real predicate instead of returning a constant. The sliced
region stores its result as the load's context pin, so a stub that always
answered null would make all 71 scenarios agree with a bug in it.

The guard that caught this named the missing symbol directly rather than letting
the scenarios fail as wrong-model assertions, which is why the cause was obvious
from the failure text alone.
Comments only. The four post-load writers each repeated the same sentence about
a later Apply reverting the pin; that now lives once, at the predicate, and each
site says only the fact that is not visible locally: the value it pins is the
one that went on the wire, so the pin cannot disagree with the launched -c.

The forensic detail in preset-policy.ts (the reported 262144, the React-key
remount, the 8192 fallback) is the history of the bug rather than the state of
the code, and the test file's header already carries it. The paragraph in the
applier arguing about what the old filter did or did not guarantee goes the same
way; the load-bearing fact inside it survives in three lines.

Kept: why the predicate takes no GPU memory mode, since the signature reads as
arbitrarily narrow without it and that narrowness was the bug; the mid-load
window note, since an editor simplifying it back to an unconditional write would
reintroduce the stale-poll hazard; and the harness stub note, since a stub doing
real work invites replacement with a constant that would make all 71 scenarios
agree with a bug.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2fdea2bd69

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1769 to +1770
const keepCustomCtx = resolveLoadedCtxPin(
loadResponse.is_gguf ? loadMaxSeqLength : null,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve Auto across same-model reloads

When Context Length is Auto under a custom or modified inference preset, reloading the current GGUF still produces a positive loadMaxSeqLength because resolveLoadMaxSeqLength substitutes ggufContextLength for the null setting. Treating that resolved wire value as an explicit pin changes the control from Auto to the current numeric context after any same-model reload (for example, when applying another model setting), so later GPU-memory changes can no longer auto-resize the context. Preserve whether loadCustomContextLength was explicitly set rather than inferring that from the resolved request value.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct, and it was a regression this PR introduced. Fixed in 598b600.

Confirmed before fixing. resolveLoadMaxSeqLength returns ggufContextLength on a same-model reload, so with Context Length on Auto the wire value is the model's native length under any preset source that is not builtin-default. Measured against a 262144-context model: builtin-default sends 0 and Auto survives; custom and modified send 262144 and Auto became a numeric pin. Deriving intent from the resolved request was the error.

The pin now comes from the user's setting. resolveLoadedCtxPin(requestedContextLength) is now resolveExplicitCtxPin(customContextLength), renamed rather than left with a name that invites feeding the wire value back in, and the three in-app writers pass their own per-model setting. resolveLoadMaxSeqLength is untouched: the send rule was never wrong, only the attempt to invert it.

That surfaced a second case of the same shape. When layers are pinned on the same model, the load substitutes the resolved context for Auto, or the reload would send 0 and llama.cpp's fit-off branch would read it as the native length. That is the app protecting the load rather than the user choosing a length, so the pin is captured before the substitution, with a test asserting that source ordering.

The status path cannot read the setting, and a backend flag would not fix it: requested_n_ctx is set from the max_seq_length the frontend sent, and the frontend sends the same number for an explicit 262144 and for an Auto reload of a 262144-context model, so the backend cannot tell them apart either. The new seed therefore acts only on unambiguous evidence and, where the echo is ambiguous, defers to the record written while watching the load rather than overwriting it in either direction.

Tests 8 to 10, including Auto surviving a same-model reload across all three preset sources and a guard that no writer feeds a wire value into the pin resolver. Reverting either half fails the new tests; reverting both fails three. Full frontend suite 5658 passed, typecheck clean, and the source-slicing autoload tests still pass at 71.

The first version of this fix inferred "the user pinned a context" from "the
request carried a positive n_ctx". That inference is wrong.
`resolveLoadMaxSeqLength` does not send 0 for Auto in every case: a same-model
reload returns `ggufContextLength`, so with Context Length on Auto under a custom
or modified preset the wire value is the model's native length. Measured with a
262144-context model: builtin-default sends 0 and Auto survives, custom and
modified send 262144 and Auto is converted into a numeric pin. After that, any
same-model reload, including applying an unrelated setting, silently froze the
context and GPU-memory changes could no longer resize it. That is the mirror of
the bug this PR set out to fix.

The pin now comes from the user's setting rather than the resolved value.
`resolveLoadedCtxPin(requestedContextLength)` becomes
`resolveExplicitCtxPin(customContextLength)`: the body is unchanged but the
argument's meaning is not, so it is renamed rather than left with a name that
invites feeding the wire value back in. The three in-app writers pass their own
per-model setting. `resolveLoadMaxSeqLength` is untouched; the send rule was
never wrong, only the attempt to invert it.

That surfaced a second instance of the same class. performLoad substitutes the
resolved context for Auto when layers are pinned on the same model, or the reload
would send 0 and llama.cpp's fit-off branch would read that as the native length.
That is the app protecting the load, not the user choosing a length, so the pin
is captured before it. A test asserts the capture precedes the clamp in source
order.

The status path cannot use the same source, and a backend flag would not help:
`requested_n_ctx` is set from the `max_seq_length` the frontend sent, and the
frontend sends the same number for an explicit 262144 and for an Auto reload of
a 262144-context model, so the backend cannot distinguish them either. A new
field would be the frontend's own state round-tripped through the server. The
new `resolveCtxPinSeed` therefore acts only on unambiguous evidence: it writes
nothing mid-load, clears on a non-GGUF load or an explicit 0, and on a model
change adopts the arriving model's saved setting only when it matches what the
server reports, which is the rule the batch sizes next door already use. Where
the echo is genuinely ambiguous, a positive value on the same model, it defers
to the record this tab wrote while watching its own load. The cost is that
another client re-pinning the same model to a different length looks like an
Auto reload; keeping Auto working is worth it.

Tests go from 8 to 10. Two existing ones changed contract deliberately: the
"writers pin the n_ctx their load actually sent" title stated the regression
itself and is renamed, and two source-regex anchors became behavioural
assertions now that the status path has a seam. No assertion was loosened, and a
new guard asserts no writer feeds a wire value into the pin resolver at all.
Reverting either half of the fix fails the new tests; reverting both fails three.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 598b600df2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +67 to +72
if (!modelChanged) {
// Same model, ambiguous echo. What is in the store came from watching this
// tab's own load, which knew what the user actually asked for; an echo that
// an Auto reload produces just as readily is not grounds to overwrite it,
// in either direction.
return {};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reconcile positive context changes on same-model polls

When another tab or API client reloads the same model/variant with a different positive context length, modelChanged remains false and this branch ignores the new requested_context_length. The local customContextLength and loadedCustomContextLength therefore continue describing the previous server invocation even though ggufContextLength is updated; applying any unrelated setting later resends the stale pin and silently reverts the other client's context choice. Even if the positive echo cannot distinguish explicit from Auto, a value that differs from the current loaded pin must invalidate or reconcile that baseline.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct, fixed in fac3ca8. I had documented this as an accepted gap, on the grounds that the echo cannot distinguish explicit from Auto. That reasoning covered failing to NOTICE the other client; it does not cover resending the stale pin on the next Apply and taking their context back, which is what you point out actually happens.

A contradicted baseline is now dropped. The echo is still not adopted, since it genuinely cannot say whether a human chose the new value, so the control falls back to Auto and the next Apply agrees with the server rather than fighting it. Checked the cases that could have made clearing wrong: an agreeing echo changes nothing, which is what keeps an explicit pin alive across ordinary polls; with no pin recorded there is no baseline to contradict, so an Auto reload under a custom preset still cannot manufacture one; and our own pinned model reloaded on Auto reports the resolved context, which IS the pin, so it matches and nothing moves.

Test asserts all four of those shapes plus the mid-load window. It fails without the source change.

Comment on lines +78 to +83
return remembered != null && remembered > 0 && remembered === incoming
? {
customContextLength: remembered,
loadedCustomContextLength: remembered,
}
: CLEAR;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve unambiguous pins during resident hydration

When a fresh tab hydrates a resident Manual-memory/Auto-layers GGUF that was loaded with an explicit context but was not saved as a remembered config, this branch clears the pin because remembered is null. In that placement mode, however, resolveFitMaxSeqLength always sends 0 for Auto, so a positive requested_context_length is unambiguous evidence of an explicit pin; clearing it makes the UI claim Auto and causes the next unrelated Apply to reload with 0. Pass the reported GPU mode/layers into this resolver and retain positive values for this unambiguous case.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct, fixed in fac3ca8. I verified the premise rather than taking it on trust, and the test pins it directly: resolveFitMaxSeqLength(true, "manual", -1, null, 4096) is 0 and the same call with a pin returns the pin, so under Manual memory with Auto layers Auto is 0 on the wire without exception and a positive echo cannot have come from it.

That echo is now adopted, and read ahead of both other branches rather than inside them: it is better evidence than a saved config, and on a model change it describes the model that arrived instead of the store's leftovers, so it fixes the hydration case you describe and the cross-model one at the same time.

One detail worth naming. The caller passes the raw status.gpu_memory_mode and status.gpu_layers, not the normalised values computed a few lines below, because those null out layers when the mode is not manual, which would read as "no layers reported" rather than as Auto and would silently disable the rule. A source assertion pins that.

Scoped to that mode only: pinned layers do not send through resolveFitMaxSeqLength, and Default memory is where the original bug lived, so the echo stays ambiguous in both and is not adopted. Tests cover both negatives, plus Auto still reporting 0 and still clearing.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 598b600df2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The status seed treated every positive `requested_context_length` as evidence of
nothing, on the grounds that an Auto same-model reload reports one too. That is
right in general and wrong in two specific places.

An echo that CONTRADICTS the pin this tab recorded means another tab or an API
client reloaded the same model at a different context. The pin then describes an
invocation that is not running, and the next unrelated Apply resends it and
silently takes back the other client's choice. Keeping the record was defensible
while the cost was only failing to notice; actively reverting someone else is
not. The contradicted baseline is now dropped. The echo is still not adopted
here, because it still cannot say whether a human chose the new value, so the
control falls back to Auto and the next Apply agrees with the server instead of
fighting it. An agreeing echo changes nothing, which is what keeps an explicit
pin alive across ordinary polls, and our own pinned model reloaded on Auto
reports the resolved context, which IS the pin, so it matches and nothing moves.

Under Manual memory with Auto layers there is no ambiguity to reason around at
all. That placement sends its pin through `resolveFitMaxSeqLength`, which
answers `customContextLength > 0 ? it : 0`, so Auto is 0 on the wire without
exception and a positive echo cannot have come from it. Clearing there made a
fresh tab hydrating a resident pinned server claim Auto, and the next Apply
reload at 0. That echo is now adopted, ahead of both other branches: it is
better evidence than a saved config, and on a model change it describes the
model that arrived rather than the store's leftovers.

The caller passes the raw `status.gpu_memory_mode` and `status.gpu_layers`, not
the normalised values computed below it, which null out layers off manual and
would read as "not reported" rather than as Auto.

All four invariants still hold and the ten existing tests are untouched: an
explicit context survives in every mode, Auto stays Auto across a same-model
reload under every preset source, a model change carries nothing across, and a
poll landing mid-load plants nothing. The two new tests fail without the source
change; the ten existing ones pass either way.
@danielhanchen

Copy link
Copy Markdown
Member Author

Confirmed the pin was being dropped after a completed load in use-chat-model-runtime.ts and apply-inference-status-to-store.ts, so the next load silently went out as Auto. Will get this reviewed.

@danielhanchen
danielhanchen merged commit 8702247 into main Aug 27, 2026
35 of 41 checks passed
@danielhanchen
danielhanchen deleted the ctx-pin-survives-load branch August 27, 2026 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant