# Complete - m-provider-usage-attribution-hot-path/02+01_attempt_usage_emission

## Completion Time

2026-07-31

## Summary

Completed actual-provider attempt usage emission in two review loops with a final PASS verdict.

## Loop History

| Plan | Review | Verdict | Notes |
|------|--------|---------|-------|
| `plan_cloud_G10_0.log` | `code_review_cloud_G10_0.log` | FAIL | The first implementation snapshotted runtime-enabled non-streaming tunnel usage before parsing the buffered provider JSON body. |
| `plan_cloud_G06_1.log` | `code_review_cloud_G06_1.log` | PASS | Finalized body-only Chat/Responses usage at terminal and error boundaries and verified provider-switch attribution without a synthetic `USAGE` frame. |

## Implementation/Cleanup

- Separated request-terminal accounting from provider-attributed attempt usage and retained exactly one terminal increment per HTTP request.
- Bound actual provider, served model, node, response mode, and attribution policy to each direct, pool, retry, fallback, and recovery attempt.
- Finalized buffered non-streaming Chat/Responses JSON before terminal, provider-error, and source-error usage snapshots while leaving passthrough response bytes unchanged.
- Added deterministic body-only endpoint and provider-switch regression evidence without requiring a provider tunnel `USAGE` frame.

## Final Verification

- `GOCACHE=/tmp/iop-s1-provider-usage-attribution-review-gocache GOTMPDIR=/config/workspace/iop-s1/.verify-provider-usage-review/gotmp go test -count=1 ./apps/edge/internal/openai -run 'NonStreamingTunnelAttempt|ProviderSwitchEmitsUsage'` - PASS; `iop/apps/edge/internal/openai`.
- `GOCACHE=/tmp/iop-s1-provider-usage-attribution-review-gocache GOTMPDIR=/config/workspace/iop-s1/.verify-provider-usage-review/gotmp go test -race -count=1 ./apps/edge/internal/openai -run 'NonStreamingTunnelAttemptFinalizesBodyUsage|ProviderSwitchEmitsUsagePerActualAttemptAndOneRequest'` - PASS; `iop/apps/edge/internal/openai`.
- `GOCACHE=/tmp/iop-s1-provider-usage-attribution-review-gocache GOTMPDIR=/config/workspace/iop-s1/.verify-provider-usage-review/gotmp go test -count=1 ./packages/go/config ./apps/edge/internal/service ./apps/edge/internal/openai` - PASS; all three packages passed.
- `GOCACHE=/tmp/iop-s1-provider-usage-attribution-review-gocache GOTMPDIR=/config/workspace/iop-s1/.verify-provider-usage-review/gotmp go test -race -count=1 ./apps/edge/internal/openai` - PASS.
- `test -z "$(gofmt -l apps/edge/internal/openai/provider_observation.go apps/edge/internal/openai/stream_gate_runtime.go apps/edge/internal/openai/usage_attribution_attempt_test.go)"` and `git diff --check` - PASS; no output.
- `rg --sort path -n 'route_model|usage_attribution|provider_id|served_model|terminal counter' agent-contract/outer/openai-compatible-api.md agent-spec/input/openai-compatible-surface.md` - PASS; the contract and living spec retain the actual-provider attempt and exactly-once request-terminal semantics.

## Roadmap Completion

- Milestone: `agent-roadmap/phase/operational-observability-provider-management/milestones/provider-usage-attribution-hot-path.md`
- Milestone link: [Milestone document](agent-roadmap/phase/operational-observability-provider-management/milestones/provider-usage-attribution-hot-path.md)
- Completed task ids:
  - `group-policy`: PASS; evidence=`agent-task/archive/2026/07/m-provider-usage-attribution-hot-path/02+01_attempt_usage_emission/plan_cloud_G06_1.log`, `agent-task/archive/2026/07/m-provider-usage-attribution-hot-path/02+01_attempt_usage_emission/code_review_cloud_G06_1.log`; verification=`go test -count=1 ./apps/edge/internal/openai -run 'UsageAttributionPolicy|UsageRecorder|UsageMetricsLabels'`.
  - `dispatch-binding`: PASS; evidence=`agent-task/archive/2026/07/m-provider-usage-attribution-hot-path/02+01_attempt_usage_emission/plan_cloud_G06_1.log`, `agent-task/archive/2026/07/m-provider-usage-attribution-hot-path/02+01_attempt_usage_emission/code_review_cloud_G06_1.log`; verification=`go test -count=1 ./packages/go/config ./apps/edge/internal/service ./apps/edge/internal/openai`.
  - `attempt-usage`: PASS; evidence=`agent-task/archive/2026/07/m-provider-usage-attribution-hot-path/02+01_attempt_usage_emission/plan_cloud_G06_1.log`, `agent-task/archive/2026/07/m-provider-usage-attribution-hot-path/02+01_attempt_usage_emission/code_review_cloud_G06_1.log`; verification=`go test -race -count=1 ./apps/edge/internal/openai -run 'NonStreamingTunnelAttemptFinalizesBodyUsage|ProviderSwitchEmitsUsagePerActualAttemptAndOneRequest'`.
- Not completed task ids: None.

## Remaining Nits

- None.

## Follow-up Work

- None.
