Skip to content

feat(context)!: deterministic minimum-first layer allocator - #87

Open
LukasParke wants to merge 16 commits into
mattapperson:mainfrom
LukasParke:lukeparke/openrouter-deterministic-allocator
Open

feat(context)!: deterministic minimum-first layer allocator#87
LukasParke wants to merge 16 commits into
mattapperson:mainfrom
LukasParke:lukeparke/openrouter-deterministic-allocator

Conversation

@LukasParke

Copy link
Copy Markdown
Collaborator

What

  • satisfy declared layer minimums from the full available context window
  • scale minimums proportionally only when they overcommit that window
  • distribute a deterministic 25% discretionary remainder by capped headroom
  • cap omitted/auto layer budgets at 2,000 tokens
  • remove the unused historyBudget result field

Why

The prior 60/40 split could violate declared minimums and treated auto as infinite headroom, producing unstable allocations. Deterministic block sizes also keep prompt-cache bands byte-stable.

This ports item 17 from fork/port/openrouter-fixes commits d8fb4f34 and 59497c48, plus a review fix that conserves fractional residual budgets.

Test plan

  • context and full core suites pass (1,484 pass, 5 live skips)
  • allocator/adversarial boundary tests
  • context/core typechecks
  • root lint
  • export-tag check
  • clean-context Pi review; fixed fractional min/mixed-cap conservation

Breaking changes

  • omitted/auto budgets now cap at 2,000 tokens
  • allocation policy changes from 60/40 to minimum-first plus a 25% discretionary pool
  • allocateBudgets() no longer returns historyBudget

Dependency

Semantically follows compaction items #80 and runtime folding #85. The branch currently includes those prerequisites and must be rebased through main after they merge.

Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Port item 15 from fork/port/openrouter-fixes commits 3d65b3e and 135ac3f: compaction records, pure projection, and pressure helpers. Model-call folding/events remain item 16.

Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Port item 16 from fork/port/openrouter-fixes commits 1761e6b and b83bdd9: fold in raw index space across execution/preview paths and emit context_pressure only on real threshold crossings.

Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
@vercel

vercel Bot commented Aug 13, 2026

Copy link
Copy Markdown

@LukasParke is attempting to deploy a commit to the Matt Apperson's projects Team on Vercel.

A member of the Team first needs to authorize it.

@LukasParke

Copy link
Copy Markdown
Collaborator Author

Agent: Code CI/structural/DCO are green where configured; remaining external red checks are compat with an empty upstream OPENROUTER_API_KEY and Vercel authorization. Where core CI is red, the only failure is the unrelated timing-sensitive executeRunCode retry-backoff test; affected local/full suites passed. I cannot rerun upstream Actions without admin rights.

Replaces the 60/40 layer/history split and its infinite-headroom
special cases: minimums are guaranteed first (proportionally scaled
when overcommitted), the remainder distributes proportional to
declared caps, and 'auto'/omitted budgets get a fixed 2000-token cap
instead of an unbounded share of the window.

Deterministic block sizes are the precondition for byte-stable
rendered layer blocks — which is what makes the prompt-cache anchoring
in this repo actually hold across turns when layer budgets are in
play. Same layers + same policy now always yields the same allocation
regardless of window size.

BREAKING CHANGE: layers with 'auto' or omitted budgets previously
received up to 60% of the post-reserve window (~74k tokens on the
128k default); they now receive a fixed 2000-token cap. Declare an
explicit budget (number or {min,max}) for layers that need more. The
allocator's historyBudget return field is removed (it had no readers).

Part of the ce6d44e9 port from OpenRouter's fork.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
The ported allocator (faithfully to its fork source) scaled declared
minimums against the 25% layer pool rather than the full available
budget, so {min: 10000} at a 32k window received 7000. Minimums are
now satisfied first out of the full post-reserve window (proportional
scale-down only when they overcommit it), and the LAYER_POOL_SHARE
ration applies to the discretionary remainder only. The documented
"minimums first" guarantee now holds numerically.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
…behavior

Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
@LukasParke
LukasParke force-pushed the lukeparke/openrouter-deterministic-allocator branch from 1ebd469 to 6fa44da Compare August 13, 2026 07:46
Expose the pure retry-delay calculation for deterministic assertions instead of proxying global setTimeout, which captured sibling test timers under CI concurrency.

Signed-off-by: Luke Parke <5702154+LukasParke@users.noreply.github.com>
@LukasParke

Copy link
Copy Markdown
Collaborator Author

Agent: Rewrote the two unsigned commits with identical tree content and valid SSH/DCO signatures, then replaced global timer interception with deterministic retry-delay unit assertions. DCO and Structural gate pass on the new history; CI rerun is in progress.

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.

1 participant