Skip to content

fix(stats): keep sub-millisecond precision in avg tool duration - #1267

Merged
edenreich merged 2 commits into
mainfrom
fix/issue-1266
Sep 22, 2026
Merged

edenreich merged 2 commits into
mainfrom
fix/issue-1266

Conversation

@inference-gateway-maintainer

Copy link
Copy Markdown
Contributor

Resolves #1266

Summary

infer stats (and every other surface that renders tool aggregates) truncated the
mean tool duration to whole milliseconds, so any average under 1ms rendered as
"0ms". The mean is now stored as fractional milliseconds and a shared formatter
falls back to microseconds below 1ms, matching the µs formatting the TUI already
uses for tool-call durations.

Changes

  • internal/platform/telemetry/stats.go: ToolStat.AvgMs is now float64 (the
    avg_ms JSON key is unchanged; values may now be fractional, which also fixes
    infer stats --format json showing 0 for sub-ms averages), toolStats()
    drops the whole-ms truncation, and a new telemetry.FormatAvg renders
    microseconds below 1ms and whole milliseconds above, next to the existing
    FormatFailRate so every surface agrees.
  • cmd/stats/stats.go: the Avg column renders via telemetry.FormatAvg.
  • internal/presentation/shortcuts/stats.go: the /stats markdown table and
    vertical views render via telemetry.FormatAvg.
  • internal/insights/insights.go: the insights digest tool totals and the report
    table render via telemetry.FormatAvg (digest header now reads
    "calls/failures/avg" since the value carries its own unit).
  • internal/platform/telemetry/stats_test.go (new): table test for FormatAvg
    plus a regression test that a 0.4ms mean survives aggregation un-truncated.

Note: avg_ms in infer stats --format json can now be a fractional number
instead of an integer; in-repo consumers are unaffected.

ToolStat.AvgMs truncated the mean duration to whole milliseconds, so averages under 1ms rendered as 0ms in infer stats, /stats and the insights digest. Store the mean as fractional milliseconds and add telemetry.FormatAvg which falls back to microseconds below 1ms, matching the TUI duration formatting convention.
Comment thread internal/platform/telemetry/stats.go Resolved
@edenreich

Copy link
Copy Markdown
Contributor

@infer /model ollama_cloud/glm-5.3 Can you review this PR? is it ready for merge? use /ponytail:ponytail /ponytail:ponytail-review /go

@inference-gateway-maintainer

inference-gateway-maintainer Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Model: ollama_cloud/glm-5.3 · View Job

Todos

Pull Request

#1267

✅ Infer Result: Success

All checks pass locally and in CI. Let me close out the plan and deliver the review.

Review complete. All findings verified - here is the full review.


Review of PR #1267 - ready for merge: yes

Verdict: approve. The only thing blocking the merge is the required review decision itself (MERGEABLE, BLOCKED solely on REVIEW_REQUIRED). All 12 CI checks pass on head 2beff20 (test, lint, vet, e2e, CodeQL, build, build-macos, postgres), and I re-ran the affected packages locally: internal/platform/telemetry, internal/insights, internal/presentation/shortcuts (plus cmd/stats) all green.

Correctness (fixes the reported bug at the root)

  • The root cause was the truncation in toolStats() (int64(durSum/count*1000)), not the rendering. This PR fixes it there, so every surface - infer stats text table, /stats markdown table, /stats vertical, insights digest, insights report, and --format json (now fractional avg_ms instead of 0) - is fixed in one place. No sibling caller left broken.
  • Verified by grep that AvgMs has no other consumers: the only remaining uses are the ones switched to FormatAvg plus insights_test.go:104 (AvgMs: 47 - untyped constant, compiles fine as float64) and the new test. avg_ms as a JSON key appears nowhere else in the repo (no e2e/integration parser, no docs reference).
  • The regression test (TestToolStatsKeepsSubMsPrecision) reproduces the exact issue scenario: a 0.4ms mean previously truncated to 0ms; now survives aggregation. Table test covers the formatter boundaries (0, sub-ms, 0.999, 1, 47.3, 1250.6).

ponytail-review (over-engineering pass)

Lean already. Ship.

  • No stdlib reuse was skipped: the existing µs formatters (agent/infrastructure FormatDuration, presentation result_view, cmd/traces tracetree) all live in packages platform/telemetry cannot import - agent infrastructure would be an upward import, presentation is depguard-blocked. The 4-line FormatAvg next to the existing FormatFailRate in the package all four surfaces already import is the correct minimum and prevents drift.
  • No speculative flexibility, no new dependencies, no extra files beyond one small test file. Struct doc comment updated to document the fractional contract.

Go idioms

Table-driven tests with t.Run subtests, colocated _test.go, no new interfaces, plain structs. The != 0.4 exact float comparison is deterministic here (exact constants through IEEE 754 arithmetic) and passes, so no tolerance needed. Header change from (calls/failures/avg ms) to (calls/failures/avg) is right since the value now carries its own unit.

Non-blocking notes

  • The second commit, "Apply suggestion from @edenreich", is not a Conventional Commit. Use a squash merge (or rebase) so semantic-release derives the release from the single fix(stats): commit - the fix commit itself is correctly typed and lowercase.
  • Cosmetic nano-edge, no action needed: FormatAvg rounds via %.0f, so an average in [0.9995, 1)ms renders as 1000µs rather than 1ms (still accurate, vanishingly rare).
  • The screenshot embedded in [BUG] The stats cmd is not accurate when mentioning avg speed 0ms #1266 could not be downloaded (the attachment redirects to an S3 domain outside the fetch allow-list); the issue text described the symptom fully, so nothing was lost.
  • Docs check: docs/ contains no mention of the stats "Avg" column or avg_ms, so the fractional-JSON note in the PR description is sufficient - no docs ticket needed.

No code changes were made in this run. Approve and merge.

Model: ollama_cloud/glm-5.3 · Exit Code: 0 · Duration: 3m 40s

Tokens: 334,565 in · 13,065 out · 347,630 total (7 requests)

Tool calls: 29 total · 100% success rate

Traces
Session: 1790101316-a0499705

session (headless, success)                                                                        3m32s
├── chat ollama_cloud/glm-5.3                                                                       8.0s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       8.0s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     8.0s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                             8.0s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 8.0s
├── execute_tool Read call_mocg3sz4                                                                333µs
├── execute_tool Read call_s5e6ak4z                                                                163µs
├── execute_tool Bash call_br72srln                                                                  4ms
├── execute_tool Bash call_02629fmf                                                                  5ms
├── execute_tool Read call_kjdujvef                                                                116µs
├── chat ollama_cloud/glm-5.3                                                                      15.8s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                      15.8s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    15.8s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                            15.8s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                15.8s
├── execute_tool Read call_hhmj1wvo                                                                297µs
├── execute_tool Read call_5j9u25o5                                                                576µs
├── execute_tool Bash call_9n0s27eb                                                                824ms
├── execute_tool TodoWrite call_y1vqga6r                                                            80µs
├── execute_tool Bash call_ecn6yd4t                                                                  4ms
├── chat ollama_cloud/glm-5.3                                                                       7.2s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                       7.2s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]     7.2s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                             7.2s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                 7.2s
├── execute_tool Memory call_b4w7w0lz                                                              147µs
├── execute_tool Grep call_i5xkup7f                                                                140ms
├── execute_tool Grep call_tip37fwz                                                                127ms
├── execute_tool Read call_m2m4fvhf                                                                231µs
├── execute_tool Grep call_0chcx9ra                                                                144ms
├── execute_tool Read call_2692rhz4                                                                344µs
├── execute_tool Memory call_2ccvww8h                                                               96µs
├── chat ollama_cloud/glm-5.3                                                                      23.5s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                      23.5s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    23.5s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                            23.5s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                23.5s
├── execute_tool Read call_xd6x7bbs                                                                261µs
├── execute_tool WebFetch call_aqzcng2w                                                            249ms [error: tool_error]
├── execute_tool Bash call_qotc5p5z                                                                 1.2s
├── execute_tool Bash call_lzrw97x8                                                                 1.0s
├── execute_tool Grep call_j3yipwv7                                                                 21ms
├── execute_tool Read call_1rukx931                                                                161µs
├── chat ollama_cloud/glm-5.3                                                                      17.9s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                      17.9s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    17.9s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                            17.9s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                17.9s
├── execute_tool TodoWrite call_edgdg851                                                            76µs
├── execute_tool Read call_rvyg0s9m                                                                160µs
├── execute_tool Bash call_twtglbra                                                                865ms
├── execute_tool Bash call_er6ucrkl                                                                1m29s
├── chat ollama_cloud/glm-5.3                                                                      14.0s
│   ╰── POST /v1/chat/completions [inference-gateway, server]                                      14.0s
│       ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    13.9s
│           ╰── POST /proxy/:provider/*path [inference-gateway, server]                            13.9s
│               ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                13.9s
├── execute_tool TodoWrite call_ie1q5e3r                                                            83µs
╰── chat ollama_cloud/glm-5.3                                                                      33.5s
    ╰── POST /v1/chat/completions [inference-gateway, server]                                      33.4s
        ╰── POST 127.0.0.1:8080/proxy/ollama_cloud/chat/completions [inference-gateway, client]    33.4s
            ╰── POST /proxy/:provider/*path [inference-gateway, server]                            33.4s
                ╰── POST ollama.com/v1/chat/completions [inference-gateway, client]                33.4s
Stats
Tool Calls

╭───────────┬───────┬───────┬─────────╮
│ Tool      │ Calls │ Fail% │ Avg     │
├───────────┼───────┼───────┼─────────┤
│ Read      │ 10    │ 0%    │ 0ms     │
│ Bash      │ 8     │ 0%    │ 11575ms │
│ Grep      │ 4     │ 0%    │ 108ms   │
│ TodoWrite │ 3     │ 0%    │ 0ms     │
│ Memory    │ 2     │ 0%    │ 0ms     │
│ WebFetch  │ 1     │ 100%  │ 249ms   │
╰───────────┴───────┴───────┴─────────╯

Token Usage

╭──────────────────────┬────────┬────────┬────────────┬────────┬──────╮
│ Model                │ Prompt │ Cached │ Completion │ Total  │ Cost │
├──────────────────────┼────────┼────────┼────────────┼────────┼──────┤
│ ollama_cloud/glm-5.3 │ 334565 │ 240640 │ 13065      │ 347630 │ -    │
╰──────────────────────┴────────┴────────┴────────────┴────────┴──────╯

Sessions

╭───────────┬──────────┬──────────╮
│ Execution │ Mode     │ Sessions │
├───────────┼──────────┼──────────┤
│ headless  │ headless │ 1        │
╰───────────┴──────────┴──────────╯

Triggered by edenreich · Infer Action · OSS

@edenreich
edenreich merged commit d0a8fb6 into main Sep 22, 2026
1 check passed
@edenreich
edenreich deleted the fix/issue-1266 branch September 22, 2026 18:26
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.206.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] The stats cmd is not accurate when mentioning avg speed 0ms

1 participant