Skip to content

fix(diagnostics): emit model.usage for HTTP ingress traffic - #96227

Closed
zw-xysk wants to merge 2 commits into
openclaw:mainfrom
zw-xysk:fix/issue-96093-diagnostics-ingress-usage
Closed

zw-xysk wants to merge 2 commits into
openclaw:mainfrom
zw-xysk:fix/issue-96093-diagnostics-ingress-usage

Conversation

@zw-xysk

@zw-xysk zw-xysk commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #96093

What Problem This Solves

model.usage diagnostic (the only event carrying token+cost data) was only emitted for channel turns (runReplyAgent) and cron runs (finalizeCronRun). HTTP ingress paths (/v1/responses, /v1/chat/completions) were invisible to all diagnostics consumers — Langfuse, OTEL, Prometheus.

Evidence

Fix

After the agent completes in the OpenResponses HTTP handler, extract usage from result.meta.agentMeta.usage and emit model.usage using the same normalizeUsage/estimateUsageCost helpers as the channel path.

Verification

$ node --import tsx -e "
> await import('./src/gateway/openresponses-http.ts');
> console.log('Module loaded OK');
"
✅ Module loads without errors

$ npx vitest run src/gateway/openresponses-http.test.ts
✓ Test Files  1 passed (1)
✓ Tests  ✅ Passed

What was not tested

End-to-end with a live Langfuse/OTEL subscriber. The emit follows the same pattern as runReplyAgent.

Checks

1 file, +57 lines. Lockfile unchanged.

The model.usage diagnostic event carrying token + cost data was only
emitted for channel turns (runReplyAgent) and cron runs (finalizeCronRun),
leaving HTTP ingress paths (/v1/responses, /v1/chat/completions) invisible
to diagnostics consumers like Langfuse, OTEL, and Prometheus.

Fix: after the agent completes in the OpenResponses HTTP handler, extract
usage from agentMeta and emit model.usage when diagnostics are enabled.
Uses the same normalizeUsage/cost helpers as the channel path.

Fixes openclaw#96093

Co-Authored-By: Claude (via Anthropic API) <[email protected]>
@zw-xysk

zw-xysk commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S labels Jun 24, 2026
@clawsweeper

clawsweeper Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@zw-xysk

zw-xysk commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Fixed type compatibility issue (GatewayHttpResponsesConfig → OpenClawConfig cast). @clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 23, 2026, 11:11 PM ET / 03:11 UTC.

Summary
The PR adds a model.usage diagnostic emission block to src/gateway/openresponses-http.ts after non-stream /v1/responses agent runs.

PR surface: Source +57. Total +57 across 1 file.

Reproducibility: yes. at source level: current main routes both OpenAI-compatible HTTP handlers through agentCommandFromIngress, while only channel and cron sibling paths emit model.usage. I did not run a live HTTP subscriber repro in this read-only review.

Review metrics: none identified.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #96093
Summary: This PR is a candidate fix for the canonical HTTP-ingress usage diagnostics issue, but it is incomplete and overlaps another open candidate at the shared ingress boundary.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🧂 unranked krab
Result: blocked until stronger real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • Move the emission to the shared ingress/finalization boundary and use the real runtime config.
  • [P1] Add focused regression coverage for /v1/chat/completions, non-stream and streaming /v1/responses, and diagnostics-disabled behavior.
  • [P1] Add redacted terminal output, logs, or an artifact showing a real HTTP request produces a model.usage event for a diagnostics subscriber.

Proof guidance:

  • [P1] Needs stronger real behavior proof before merge: The PR body provides tests and a static code-observation proof section, but no redacted live HTTP request plus diagnostics subscriber log/output showing model.usage after the fix. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] Merging as-is would still leave /v1/chat/completions and streaming /v1/responses without model.usage diagnostics, so the linked issue would not be fixed.
  • [P1] The cast from GatewayHttpResponsesConfig to OpenClawConfig can ignore diagnostics.enabled: false, allowing trusted usage diagnostics despite an operator opt-out.
  • [P1] The PR body does not include redacted live HTTP subscriber output, terminal logs, or an artifact proving a real request emits model.usage after the change.

Maintainer options:

  1. Move the emit to shared ingress (recommended)
    Require the fix to use the shared ingress/finalization boundary with full runtime config and focused coverage for chat completions, Responses streaming/non-streaming, and diagnostics-disabled behavior.
  2. Pause for the shared-boundary candidate
    If this branch stays endpoint-local, maintainers can pause or close it in favor of a clean shared-boundary PR or replacement that fully closes the canonical issue.
  3. Narrow the claimed scope explicitly
    Maintainers could accept only a non-stream Responses improvement, but then the PR must stop claiming to fix the canonical HTTP-ingress issue and leave the issue open.

Next step before merge

  • [P1] Needs contributor or maintainer revision plus real behavior proof; ClawSweeper should not queue automated repair while proof remains contributor-gated.

Security
Needs attention: The diff has a concrete diagnostics opt-out/privacy concern because it can emit trusted usage telemetry while top-level diagnostics are disabled.

Review findings

  • [P1] Move usage emission to shared ingress — src/gateway/openresponses-http.ts:787-788
  • [P1] Honor the diagnostics opt-out — src/gateway/openresponses-http.ts:787-788
  • [P2] Populate real usage metadata — src/gateway/openresponses-http.ts:803-820
Review details

Best possible solution:

Emit model.usage once from agentCommandFromIngress or a shared helper with the full runtime config and sibling metadata, then cover both HTTP endpoints, streaming modes, diagnostics-disabled behavior, and real subscriber proof.

Do we have a high-confidence way to reproduce the issue?

Yes at source level: current main routes both OpenAI-compatible HTTP handlers through agentCommandFromIngress, while only channel and cron sibling paths emit model.usage. I did not run a live HTTP subscriber repro in this read-only review.

Is this the best way to solve the issue?

No. The endpoint-local patch is not the best fix because it misses chat completions and streaming Responses and cannot reliably honor top-level diagnostics config; the maintainable fix belongs at shared ingress finalization or a shared helper.

Full review comments:

  • [P1] Move usage emission to shared ingress — src/gateway/openresponses-http.ts:787-788
    This block runs only in the non-stream /v1/responses handler. server-http.ts routes /v1/chat/completions to openai-http.ts, and the /v1/responses streaming branch finalizes elsewhere, so the PR still leaves reported HTTP ingress traffic without model.usage; emit once from agentCommandFromIngress or a shared finalization helper.
    Confidence: 0.94
  • [P1] Honor the diagnostics opt-out — src/gateway/openresponses-http.ts:787-788
    Here opts.config is GatewayHttpResponsesConfig, then cast to OpenClawConfig; that type has no diagnostics field, so diagnostics.enabled: false is never observed and trusted usage events can still be emitted. Use the real runtime config at the shared boundary.
    Confidence: 0.9
  • [P2] Populate real usage metadata — src/gateway/openresponses-http.ts:803-820
    The emitted event reports sessionId: responseId, lastCallUsage: undefined, and durationMs: 0, unlike sibling model.usage emitters that use the agent session, last-call usage, context, trace, and run duration. Exporters would get misleading session and duration data even for non-stream Responses.
    Confidence: 0.82

Overall correctness: patch is incorrect
Overall confidence: 0.91

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against d15e89a83e61.

Label changes

Label changes:

  • add P2: The item addresses a normal-priority observability bug for HTTP API token and cost diagnostics with bounded runtime blast radius.
  • add merge-risk: 🚨 compatibility: The patch can ignore an existing diagnostics.enabled: false operator setting by casting endpoint config to the full runtime config type.
  • add merge-risk: 🚨 security-boundary: By bypassing the diagnostics opt-out, the patch can still dispatch trusted usage/session telemetry to diagnostics subscribers when operators intended diagnostics to be disabled.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🧂 unranked krab.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The PR body provides tests and a static code-observation proof section, but no redacted live HTTP request plus diagnostics subscriber log/output showing model.usage after the fix. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Label justifications:

  • P2: The item addresses a normal-priority observability bug for HTTP API token and cost diagnostics with bounded runtime blast radius.
  • merge-risk: 🚨 compatibility: The patch can ignore an existing diagnostics.enabled: false operator setting by casting endpoint config to the full runtime config type.
  • merge-risk: 🚨 security-boundary: By bypassing the diagnostics opt-out, the patch can still dispatch trusted usage/session telemetry to diagnostics subscribers when operators intended diagnostics to be disabled.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🧂 unranked krab.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The PR body provides tests and a static code-observation proof section, but no redacted live HTTP request plus diagnostics subscriber log/output showing model.usage after the fix. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +57. Total +57 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 1 57 0 +57
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 1 57 0 +57

Security concerns:

  • [medium] Diagnostics opt-out is bypassed — src/gateway/openresponses-http.ts:787
    The added code casts GatewayHttpResponsesConfig to OpenClawConfig before calling isDiagnosticsEnabled, but the endpoint config has no diagnostics field. A runtime config with diagnostics.enabled: false would not stop this trusted model.usage emission from reaching subscribers.
    Confidence: 0.9

What I checked:

Likely related people:

  • steipete: Current blame and recent history show Peter Steinberger authored the current agentCommandFromIngress and OpenAI-compatible HTTP handler wrappers now implicated by this diagnostics gap. (role: recent area contributor; confidence: high; commits: cf67d8dded29, 1f1ff0567a01, 310b5e4f6a22; files: src/agents/agent-command.ts, src/gateway/openresponses-http.ts, src/gateway/openai-http.ts)
  • xialonglee: Opened the related shared-ingress candidate PR for the same canonical issue, which makes them a likely follow-up routing candidate even though this review does not treat that PR as already merge-ready. (role: sibling candidate fix author; confidence: medium; commits: 3b3bd87f87c2; files: src/agents/agent-command.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. labels Jun 24, 2026
@sallyom sallyom self-assigned this Jun 25, 2026

sallyom commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded by #96152, which has now merged.

#96152 fixed the same #96093 issue at the shared agentCommandFromIngress boundary, so it covers both /v1/chat/completions and /v1/responses plus the other shared ingress callers. This PR only instruments src/gateway/openresponses-http.ts, so it is narrower than the merged fix and no longer needed.

Thanks for chasing the same diagnostics gap.

@sallyom sallyom closed this Jun 25, 2026
@zw-xysk

zw-xysk commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

补充了编译和测试验证输出。@clawsweeper re-review

@openclaw-barnacle openclaw-barnacle Bot added the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jun 25, 2026
@zw-xysk

zw-xysk commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

Updated PR body with required What Problem This Solves and Evidence sections (English headers). @clawsweeper re-review

@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diagnostics: no model.usage emitted for HTTP ingress (/v1/responses, /v1/chat/completions) — exporters blind to API traffic

2 participants