Skip to content

feat(runtime): stream live model output rate and calibrate token estimates - #68

Merged
locez merged 2 commits into
mainfrom
fix/runtime-token-calibration
Sep 23, 2026
Merged

locez merged 2 commits into
mainfrom
fix/runtime-token-calibration

Conversation

@locez

@locez locez commented Sep 23, 2026

Copy link
Copy Markdown
Owner

Why

  • A streaming turn showed no live throughput, and long text deltas accumulated a second event-by-event copy in retained run results.
  • Request token budgets stayed on a fixed bytes-per-token ratio even after the provider reported real input usage for the same request.

What changed

  • merry-llm: every provider attempt is received through receive_model_stream, which keeps semantic events ordered and bounded while publishing latest-only output progress (UTF-8 bytes over the first-to-last receive interval, at most every 100 ms). Progress counts text, tool-call fragments, and reasoning without retaining text, and downgrades timing quality for hidden or summarized reasoning and consumer-limited delivery instead of reporting a false rate.
  • merry-core: new ModelOutputRate, OutputTokenSource, and OutputTimingQuality, exposed as runtime/journal events, plus is_transient() so live-only observations (text deltas and rate samples) stay out of retained run results and replay.
  • merry-runtime: one rate per attempt from provider usage or the calibrated estimate, delivered on a watch-based latest-only channel beside the ordered semantic journal and reset at step and compaction boundaries. Session state learns a bounded provider/model/stable-prefix-scoped input-token calibration from complete request estimates plus matching provider usage: underestimation applies immediately, excess headroom is released gradually, and the calibration persists with the session document.
  • merry-cli / sdks/python: the TUI status line shows the last measurable sample (~N tok/s when approximate); the Python SDK exposes typed ModelOutputRate events and keeps transient updates out of result.events.

Rates are client receive-side observations, never a server decode clock, and provider usage does not imply that hidden reasoning timing was observed.

Verification

  • cargo fmt --all --check - clean
  • cargo clippy --all-targets --all-features -- -D warnings - clean
  • cargo test --all --no-fail-fast - every target passes except four host-dependent sandbox suites that need nested bwrap, a root-owned /etc/ssh/ssh_config, or a writable /root; none of them touch the changed modules
  • sdks/python: ruff check, ty check, pytest (91 passed), uv build - all clean

…mates

Why:
- A streaming turn showed no live throughput, and long text deltas were kept
  as a second event-by-event copy in retained run results.
- Request token budgets relied on a fixed bytes-per-token ratio even after the
  provider reported real input usage for the same request.

What changed:
- merry-llm: receive every provider attempt through `receive_model_stream`,
  which keeps semantic events ordered and bounded while publishing latest-only
  output progress (UTF-8 bytes over the first-to-last receive interval, at most
  every 100 ms). Progress counts text, tool-call fragments, and reasoning
  without retaining text, and downgrades timing quality for hidden or
  summarized reasoning and consumer-limited delivery instead of reporting a
  false rate.
- merry-core: add `ModelOutputRate`, `OutputTokenSource`, and
  `OutputTimingQuality`, expose them as runtime/journal events, and add
  `is_transient()` so live-only observations (text deltas, rate samples) are
  excluded from retained run results and replay.
- merry-runtime: reduce provider usage or the calibrated estimate into one rate
  per attempt, deliver it through a watch-based latest-only channel beside the
  ordered semantic journal, and reset it at step and compaction boundaries.
  Session state learns a bounded, provider/model/stable-prefix-scoped input
  token calibration from complete request estimates plus matching provider
  usage: underestimation applies immediately and excess headroom is released
  gradually. The calibration persists with the session document.
- merry-cli and sdks/python: show the last measurable sample in the TUI status
  line (`~N tok/s` when approximate) and expose typed `ModelOutputRate` events,
  keeping transient updates out of `result.events`.

Verification:
- cargo fmt --all --check
- cargo clippy --all-targets --all-features -- -D warnings
- cargo test --all --no-fail-fast: every target passes except four
  host-dependent sandbox suites that need nested bwrap, a root-owned
  /etc/ssh/ssh_config, or a writable /root.
- sdks/python: ruff check, ty check, pytest (91 passed), uv build
Apply Ruff formatting to the output rate parser, typed values, and tests so the Python SDK CI gate can proceed. Python ASTs are unchanged; no runtime behavior or checks are modified.

Verified on Linux with Python 3.14: uv sync --locked; test-utils extension build; Ruff formatting and lint; ty; all 91 Python tests; example and probe compilation; wheel and source archive build; git diff --check.

Refs: #68
@locez
locez merged commit 50ce8a5 into main Sep 23, 2026
8 checks passed
@locez
locez deleted the fix/runtime-token-calibration branch September 23, 2026 17:06
@locez locez linked an issue Sep 23, 2026 that may be closed by this pull request
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.

ctx 窗口估算不准

1 participant