Skip to content

feat(academy): attribute tokens from legion's inference-usage messages - #90

Open
baron-3dl wants to merge 1 commit into
mainfrom
work/academy-inference-usage
Open

baron-3dl wants to merge 1 commit into
mainfrom
work/academy-inference-usage

Conversation

@baron-3dl

Copy link
Copy Markdown
Collaborator

Summary

Adds an accumulateInferenceUsage handler that reads inference-usage-tagged messages posted by legion's API substrate (3dl-dev/legion#339) and attributes their tokens_in / tokens_out to the matching scenario.

Why

Per-scenario token counts have been zero for a long time. The existing emitToolUsage path in mallcop-investigate-tools/tools_f1g.go reads MALLCOP_SESSION_TOKENS_IN/OUT env vars that nothing populates — by design, those were a forward-compat placeholder. Legion now posts the actual InputTokens / OutputTokens directly after each Router.Infer, and this handler ingests them.

Behavior

  • Matches incoming messages by item_id via workItemToScenario (terminal-tool messages use finding-id tags, but legion does not know finding IDs).
  • Increments ts.toolUsageTokensIn / ts.toolUsageTokensOut only. Does NOT increment forge_calls — that count remains owned by terminal tools.
  • Dedup by message ID using the existing seenToolUsageMsgs set.

Test plan

  • go test ./cmd/mallcop-academy/... — all existing tests pass
  • End-to-end against legion#339:
    • UT-02 single-scenario run with the legion change deployed
    • 5 inference-usage messages received and attributed
    • Result JSON: tokens_in=16211, tokens_out=938 (was 0 before)
    • forge_calls=2 unchanged (terminal-tool count untouched)

Depends on

  • 3dl-dev/legion#339 — must be deployed for this handler to receive messages

mallcoppro-3

🤖 Generated with Claude Code

Pairs with 3dl-dev/legion#339. Legion's API substrate now posts
`inference-usage` tagged messages to the work campfire after each
Router.Infer call, carrying the actual InputTokens / OutputTokens.

Adds `accumulateInferenceUsage` handler called from the watch loop
alongside the existing `accumulateToolUsage` path. Attribution is by
item_id via workItemToScenario (terminal-tool messages use finding-id
tags — legion does not know finding IDs, so we attribute via item_id
instead). forge_calls is NOT incremented — that count remains owned
by terminal tools.

Dedup uses the existing seenToolUsageMsgs set keyed by message ID.

Before: every per-scenario record had tokens_in=0, tokens_out=0
After (UT-02 single-scenario run): tokens_in=16211, tokens_out=938
attributed correctly from 5 legion inference-usage posts.

mallcoppro-3 (academy token attribution = 0, third request)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.

1 participant