Skip to content

feat(runtime): charge context deltas at the ratio the provider measurement established (RR7) - #127

Merged
lg2m merged 1 commit into
feat/eng-868-rr6-reactive-overflowfrom
feat/eng-869-rr7-estimate-calibration
Sep 22, 2026
Merged

lg2m merged 1 commit into
feat/eng-868-rr6-reactive-overflowfrom
feat/eng-869-rr7-estimate-calibration

Conversation

@lg2m

@lg2m lg2m commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #126 (RR6). RR7 of docs/plans/run-reliability.md; audit finding R02 (d), deferred from F04. Closes ENG-869.

Behaviour

The measured-occupancy chain (C6) already seeds each request from the provider's reported input_tokens, but it charged every byte delta at the fixed 4 bytes/token. On a code-heavy transcript (~3.1 B/t) each appended turn was under-charged by ~22 %, so the estimate drifted low across a long run until the provider rejected the request — the case RR6 now recovers, but better not to get there.

adjust_measured_tokens now charges and credits at the ratio the measurement itself implies — calibrated_bytes_per_token(measured_tokens, measured_bytes): rounded to nearest, clamped to 2–6 B/t, defaulting to 4 below 2 000 measured tokens (noise floor where the system prompt dominates). The run loop calibrates once against the whole measured request and applies each component's delta at that ratio (adjust_measured_tokens_at).

Unchanged: the raw 4 B/t estimate for an unmeasured first request (nothing to calibrate against), the compatibility rules for reuse, and the storage backstop. No schema or protocol change.

Verification

  • deltas_are_charged_at_the_ratio_the_measurement_established: 3 B/t code, 5 B/t prose, both directions, clamp bounds, noise floor.
  • calibration_holds_the_estimate_within_ten_percent_on_a_code_heavy_transcript — the plan's acceptance criterion: eight turns at 3.1 B/t, worst error < 10 %; the default would be 22 % under on every delta.
  • Existing measured-chain and checkpoint-seam tests pass unchanged; cargo test -p qq-core --lib all green; workspace clippy -D warnings and fmt clean.

Docs

architecture.md § resolved model (occupancy reuse paragraph); ledger entry.

…ement established (RR7)

The measured-occupancy chain seeds from the provider's reported input
tokens but charged every byte delta at the fixed four bytes per token. On
a code-heavy transcript (~3.1 B/t) each appended turn was under-charged by
about 22 %, so the estimate drifted low across a long run until the
provider rejected the request.

adjust_measured_tokens now charges and credits at the ratio the measurement
itself implies (measured bytes over measured tokens, rounded to nearest,
clamped to 2-6, defaulting to 4 below 2 000 measured tokens). The run loop
calibrates once against the whole measured request and applies each
component's delta at that ratio. The raw byte estimate for an unmeasured
first request is unchanged; RR6 covers a provider rejection of it.

Tests: deltas_are_charged_at_the_ratio_the_measurement_established;
calibration_holds_the_estimate_within_ten_percent_on_a_code_heavy_transcript
(eight turns at 3.1 B/t, worst error under 10 % where the default is 22 %
under per delta); existing chain and checkpoint tests unchanged.

Closes ENG-869.
@lg2m
lg2m added this pull request to stack #129 September 22, 2026 15:39
@lg2m
lg2m merged commit a92498d into main Sep 22, 2026
4 checks passed
@lg2m
lg2m deleted the feat/eng-869-rr7-estimate-calibration branch September 22, 2026 16:48
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