Skip to content

fix(training-agent): match report_usage signal records to stored activations - #7773

Merged
bokelley merged 1 commit into
mainfrom
fix/report-usage-activation-lookup
Sep 30, 2026
Merged

bokelley merged 1 commit into
mainfrom
fix/report-usage-activation-lookup

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Root cause

handleReportUsage (server/src/training-agent/task-handlers.ts) validated a signal usage record with session.signalActivations.get(record.signal_agent_segment_id), a lookup by the bare segment ID. activate_signal never stores an activation under that key. It uses ${segment}:${destination}, and since #7769 also ${segment}:${destination}#${seat} for a destination with a seat account. So the lookup could never match, and usage for every activated signal came back as SIGNAL_NOT_FOUND ("not found in session. Use activate_signal first."). #7769 noted this under "Out of scope, noticed".

Fix

  • A new module-level helper, signalActivationMatches(state, segmentId, dest?), matches a stored activation by its recorded segment. When a destination is passed, it also matches the destination type, destination ID and seat account. activate_signal's replace and deactivate path now uses the helper too, in place of its inline copy of the same check. The two tasks therefore share one definition of which activation is which, and neither rebuilds the map key. That also covers sessions persisted under the older key shapes.
  • report_usage accepts a signal record when the record's account session holds a live activation of that segment.
  • The error stays the same. Unknown, deactivated and other-account segments all get the same SIGNAL_NOT_FOUND with the same message and field. That's what the uniform-response rule for SIGNAL_NOT_FOUND in enums/error-code.json requires. A test checks that a deactivated signal's response is identical to the response for a never-activated one.
  • Account isolation doesn't change. Each record already resolves its own session from record.account, the same sessionKeyFromArgs derivation activate_signal uses. The lookup only searches that session, so one account's usage can't match another account's activation.

Spec ambiguity: several destinations, please review

A report-usage record (account/report-usage-request.json, docs/accounts/tasks/report_usage.mdx) identifies a signal only by account and signal_agent_segment_id. It has no destination, deployment or seat field. So the spec never lets a record pick one activation out of several destinations, and it doesn't say what should happen when a segment is live on more than one.

I went with what seemed least surprising: any live deployment of the segment in the record's account satisfies the record. The usage counts against the signal, not against one deployment. The record is rejected only after every destination has been deactivated. I didn't invent a non-schema destination hint. This decision is written up in a code comment at the lookup.

Out of scope, noticed

  • This change doesn't add pricing_option_id checks for signal records. A mismatch against the activation's or the catalog's pricing option is still accepted, as before. The creative path does check this, but adding it for signals would be a new rejection, and that deserves its own change.
  • The multi-tenant router serves report_usage only on /sales and /creative (tenants/tool-catalog.ts), and activate_signal only on /signals. The schema says signal_agent_segment_id is "Required for signals agents", but the /signals tenant doesn't advertise report_usage. Sessions are keyed by account, not by tenant, so signal usage reported to /sales does see activations made on /signals. The legacy single-server surface exposes both tools. Whether to add report_usage to /signals is a separate catalog, capability and drift-test decision.

No schema, task-definition or normative-doc changes, so there's no changeset. node scripts/check-changeset-protocol-scope.cjs origin/main passes.

Tests

New in server/tests/unit/training-agent.test.ts, under report_usage handler › signal usage against activate_signal activations:

Test Without fix With fix
accepts usage for a signal activated to an agent destination fail pass
accepts usage for a signal activated to a platform seat fail pass
accepts usage for an activation persisted under the legacy account-less key fail pass
matches the signal while any destination is live and rejects it once every destination is deactivated (agent plus two seats on one platform) fail pass
rejects a deactivated signal the same way as one that was never activated fail pass
never matches another account's activation of the same signal (mixed-account batch, then swapped) fail pass

All six fail on main. Each one first asserts that usage is accepted for an activation that exists, and that is the bug.

Local runs:

  • npx vitest run --config server/vitest.config.ts src/training-agent tests/unit/training-agent tests/unit/comply-test-controller.test.ts tests/unit/storyboards.test.ts: 39 files, 1532 tests passed
  • npm run typecheck: passed
  • node --test tests/test-coverage-registration.test.cjs: 3/3 passed
  • Pre-commit server-unit shards: passed

Storyboards (dist/compliance/latest)

These are the storyboards from grep -rln report_usage static/compliance/source, plus the signals storyboards that cover activate_signal, whose deactivation matcher now goes through the shared helper.

Tenant Storyboard Before (main) After
creative creative/billing_out_of_band 4P 4P
creative creative_ad_server 8P 8P
sales media_buy_seller/billing_finality_delivery 9P 9P
sales media_buy_seller/revenue_share_pricing 9P 9P
signals schema_validation_signals 5P 5P
signals signal_marketplace 8P 8P
signals signal_owned 5P 5P
signals signals_baseline 4P 4P

None of these storyboards sends a signal usage record. The report_usage storyboards cover creative and media-buy usage, and schema_validation_signals mentions report_usage only in narrative. The results are the same before and after, so the unit tests above are the only direct regression guard.

🤖 Generated with Claude Code

…vations

report_usage validated signal_agent_segment_id with
session.signalActivations.get(segmentId), but activate_signal stores
activations under `${segment}:${destination}` (and, since #7769,
`${segment}:${destination}#${seat}`). The bare-segment lookup never
matched, so usage for any activated signal was rejected with
SIGNAL_NOT_FOUND.

report_usage now matches stored activations by their recorded segment
through the same signalActivationMatches helper activate_signal uses for
replacement and deactivation. A usage record identifies a signal only by
account and segment (the schema has no destination field), so any live
deployment of the segment in the record's account session satisfies it.
Unknown, deactivated and other-account segments keep the same uniform
SIGNAL_NOT_FOUND.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@aao-secretariat aao-secretariat Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ladon verdict: Approve

Approve — clean training-agent bugfix, no blocking or medium findings.

This PR fixes handleReportUsage in server/src/training-agent/task-handlers.ts, replacing an impossible bare-segment map lookup with iteration that matches on the recorded segmentId via the shared signalActivationMatches helper. Account isolation is preserved through per-record session resolution, and six new regression tests cover the changed behavior.

Checks performed:

  • Both changed files reviewed (task-handlers.ts + training-agent.test.ts).
  • Changeset scope: correctly none — server app code only, no protocol surface (static/schemas/source/, docs/reference/, dist/**) touched.
  • No schema↔docs coherence concerns (no schema changes).
  • No new external I/O or operational-readiness gaps.

Decision table: no critical/high/medium findings; gated_paths is false (row 2 N/A); high_risk is false; no no-auto-approve team match. review_decision is REVIEW_REQUIRED but gated_paths is false, so the hard gate does not apply. Falls through to row 9 → approve.

@bokelley
bokelley merged commit 1c66c53 into main Sep 30, 2026
53 checks passed
@bokelley
bokelley deleted the fix/report-usage-activation-lookup branch September 30, 2026 05:05
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