Repository navigation
fix(training-agent): match report_usage signal records to stored activations - #7773
Merged
Merged
Conversation
…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]>
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
handleReportUsage(server/src/training-agent/task-handlers.ts) validated a signal usage record withsession.signalActivations.get(record.signal_agent_segment_id), a lookup by the bare segment ID.activate_signalnever 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 asSIGNAL_NOT_FOUND("not found in session. Use activate_signal first."). #7769 noted this under "Out of scope, noticed".Fix
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_usageaccepts a signal record when the record's account session holds a live activation of that segment.SIGNAL_NOT_FOUNDwith the same message andfield. That's what the uniform-response rule forSIGNAL_NOT_FOUNDinenums/error-code.jsonrequires. A test checks that a deactivated signal's response is identical to the response for a never-activated one.record.account, the samesessionKeyFromArgsderivationactivate_signaluses. 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 byaccountandsignal_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
pricing_option_idchecks 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.report_usageonly on/salesand/creative(tenants/tool-catalog.ts), andactivate_signalonly on/signals. The schema sayssignal_agent_segment_idis "Required for signals agents", but the/signalstenant doesn't advertisereport_usage. Sessions are keyed by account, not by tenant, so signal usage reported to/salesdoes see activations made on/signals. The legacy single-server surface exposes both tools. Whether to addreport_usageto/signalsis 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/mainpasses.Tests
New in
server/tests/unit/training-agent.test.ts, underreport_usage handler›signal usage against activate_signal activations: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 passednpm run typecheck: passednode --test tests/test-coverage-registration.test.cjs: 3/3 passedStoryboards (
dist/compliance/latest)These are the storyboards from
grep -rln report_usage static/compliance/source, plus the signals storyboards that coveractivate_signal, whose deactivation matcher now goes through the shared helper.main)creative/billing_out_of_bandcreative_ad_servermedia_buy_seller/billing_finality_deliverymedia_buy_seller/revenue_share_pricingschema_validation_signalssignal_marketplacesignal_ownedsignals_baselineNone of these storyboards sends a signal usage record. The
report_usagestoryboards cover creative and media-buy usage, andschema_validation_signalsmentionsreport_usageonly 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