Skip to content

fix: return and fence auxiliary usage results - #1904

Merged
jbeda merged 25 commits into
mainfrom
impl/auxiliary-usage-results
Oct 4, 2026
Merged

jbeda merged 25 commits into
mainfrom
impl/auxiliary-usage-results

Conversation

@jbeda

@jbeda jbeda commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • persist auxiliary hook, permission, reflection, and utility model usage in purpose-specific ledgers
  • fence parent-owned detached reviewer usage and preserve explicit reflection usage through lifecycle errors
  • retain exact composition-selected provider/model attribution, including nested utility cascade compaction

Verification

  • task lint
  • task docs
  • task test
  • task test:race
  • go run ./cmd/mecademo
  • task ac-trace-strict

Review

  • Final panel review: no ship blockers, important findings, or advisories.

@jbeda
jbeda force-pushed the impl/auxiliary-usage-results branch 3 times, most recently from d2213a3 to b45be2a Compare September 30, 2026 18:52
@kantord

kantord commented Oct 2, 2026

Copy link
Copy Markdown
Member

AI review

This review was generated by an AI-assisted code review. Please validate findings against the code and intended contract before acting on them.

Panel review — auxiliary and contextual guardrail usage

Fixed point: origin/main (160d9f014d42c3e524d9ccae028d125e12baec0b)
Diff: 173 files, 5,373 insertions, 855 deletions, 21 commits

Spec — does this implement what was asked?

Sources: docs/acceptance/auxiliary-token-usage.md; docs/acceptance/contextual-guardrail-usage-accounting.md

  • [blocker · Wrong] Automatic compaction records returned usage directly, bypassing the run ownership-admission fence. After Run.OwnershipLost() clears admission, an in-flight compaction can still mutate the stale session ledger at engine/agent/loop.go:3165-3169. This contradicts the contract that no accounting path persists after ownership loss and late auxiliary results are dropped (docs/acceptance/contextual-guardrail-usage-accounting.md:28-32). Route this through recordCompleteAuxiliaryUsageWhileActive and add a lease-loss race test.

Standards — does this follow project conventions?

Sources read: AGENTS.md, applicable rule files, docs/development-process.md, ADRs 0307, 0350, 0354, 0363, and 0371.

  • [important · Violation] The auxiliary-usage reporter is installed around every PermissionPolicy.Evaluate call whenever a run exists (engine/agent/dispatch.go:1097-1105), including ordinary evaluations. ADR 0371 and the acceptance contract restrict it to the main-session path-escape evaluation; generic paths must not arm this reporter (docs/adr/0371-contextual-guardrail-usage-accounting.md:20, docs/acceptance/contextual-guardrail-usage-accounting.md:21,31). Install it only around the path-escape checker call.

Test adequacy — do the tests independently prove the contract?

  • [important] Contextual-usage tests establish the guardrail bucket but do not assert that main/run-visible usage remains unchanged. For example, engine/agent/contextual_guardrail_usage_test.go:103-115 checks guardrail and router totals but not Session.Usage or UsageKindMain. A regression that both records guardrail usage and folds it into main usage would pass, despite AC1.1 requiring main accounting remain unchanged (docs/acceptance/contextual-guardrail-usage-accounting.md:42). Add explicit main-usage assertions across action, inbound, and substitution-floor paths.

Domain — specialist reviewers

  • [blocker · High] Automatic compaction bypasses run ownership fencing at engine/agent/loop.go:3165-3169. A lease-loss transition revokes ownership under the service lock (internal/adapter/server/service.go:8148-8156), but compaction directly calls sess.RecordAuxiliaryUsage. Use the existing run-owned admission helper and add a blocked-compaction/ownership-loss test proving neither ledger mutation nor save admission occurs.
    Sources: software-architect

  • [important · Medium] engine/agent/auxiliary_usage.go:17-22 and internal/app/contextual_reviewer.go:487-492 duplicate provider/model-attribution normalization already represented in the session layer (engine/session/usage.go:81-88). Consolidate it behind one canonical helper only if it can remain internal and avoid expanding the public engine API.
    Sources: code-duplication-reviewer

Panel passes

  • secure-code-reviewer: no concrete security or authority regression found.
  • library-reuse-reviewer: Lean already.

Summary

  • Spec axis: 1 finding — 1 blocker.
  • Standards axis: 1 finding — 1 important.
  • Test adequacy axis: 1 finding — 1 important.
  • Domain axis: 1 blocker, 1 important.
  • The compaction ownership defect is independently confirmed by the Spec and Domain axes; it remains two findings because the axes are intentionally orthogonal.

Most important single issue: engine/agent/loop.go:3169 can admit compaction usage after ownership loss, violating the stale-session accounting fence.

PANEL: ship_blockers=2 important=3 advisory=0 reviewer_failures=0

jbeda and others added 19 commits October 3, 2026 21:15
jbeda and others added 4 commits October 3, 2026 21:19
Supersede reflection session writes while retaining returned usage and bounded diagnostics. Defer durable ownership to #2069.

Co-authored-by: Mecatl <[email protected]>
Keep returned reflection usage and bounded diagnostics without accounting-only locks, reloads or saves. Consolidate utility usage conversion and preserve nested attribution.

Co-authored-by: Mecatl <[email protected]>
Preserve early clean-result availability while holding guardrail usage in the private assessment until canonical dispatcher drain. Cover ordered attribution with held siblings.

Co-authored-by: Mecatl <[email protected]>
@jbeda
jbeda force-pushed the impl/auxiliary-usage-results branch from e39b627 to 525626a Compare October 4, 2026 04:50
@jbeda

jbeda commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up on the AI-assisted panel review, checked against the current PR head:

  • Automatic compaction / lease loss: Not taking the suggested run-admission change. Compaction may update the old in-memory Session if an in-flight provider call finishes after lease loss, but the service invalidates its local mutation capability before cancellation, so a later save is not admitted. We explicitly accept losing usage returned after ownership loss; we will not reload/replay or write to a successor session to recover it. The reported Run.OwnershipLost() mechanism does not exist in this implementation. The run-local auxiliary admission fence is for completed/late run results, not a backend lease-token fence.
  • Permission evaluations: The engine consumes PermissionResult.Usage on each evaluation, as the result contract requires, including re-evaluations. Ordinary policies return no checker usage; only the main-session path-escape policy can produce it. This is not a generic hook reporter installed for every call.
  • Main-usage test gap: Addressed in db7f8adef: action/inbound checks now use nonzero main-model usage and assert it is unchanged; the substitution-floor test asserts the returned review usage does not enter the main or router bucket.
  • Attribution normalization: The cited internal/app/contextual_reviewer.go copy no longer exists. The remaining small duplication between agent and session was considered, but consolidating it across those packages would expand the engine's exported API or add a package solely for this helper. Leaving it as-is for this PR.

Separately, f65b36499 fixes the disabled-guardrails reviewer regression found during human review and removes the measured per-tool reporter allocation regression. Focused tests, task lint, task test, task test:race, and the offline demo passed locally; the current PR checks report no failures.

@jbeda
jbeda merged commit d7541d6 into main Oct 4, 2026
39 checks passed
@jbeda
jbeda deleted the impl/auxiliary-usage-results branch October 4, 2026 23:49
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Oct 7, 2026
2 tasks
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.

2 participants