fix(attribution): self-register consumer agents so consumer_agent_id is captured - #38
Merged
Merged
Conversation
…is captured getAccessContext nulled out any x-agent-id not already in the agents table (to avoid FK-violation 500s on usage/attribution/receipt writes). Provisioned agents send KNOWLEDGE_AGENT_ID + KNOWLEDGE_AGENT_WALLET but were never rows in the Knowledge agents table, so their consumer_agent_id always landed empty. Now: an unknown x-agent-id that ALSO presents a wallet is self-registered as a 'consumer' agents row (id + wallet in metadata, ON CONFLICT DO NOTHING) and kept — agents-in-the-market become first-class consumers on their first paid query. No wallet, or a disabled agent → still treated as a public consumer (agentId null) so FK writes never 500. +4 getAccessContext unit tests (209 pass).
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.
Closes the "consumer_agent_id empty from the baked plugin" gap.
Root cause
getAccessContextresolvedx-agent-idagainst theagentstable and nulled it out when the agent had no row there (a guard against FK-violation 500s on theconsumer_agent_id REFERENCES agents(id)columns inknowledge_attributions/knowledge_usage_events/x402_receipts). Provisioned agents sendKNOWLEDGE_AGENT_ID+KNOWLEDGE_AGENT_WALLETbut are never inserted into the Knowledgeagentstable, so their consumer attribution was always dropped.Fix
An unknown
x-agent-idthat also presents a wallet is now self-registered as aconsumerrow (id,display_name=id,metadata.wallet,ON CONFLICT DO NOTHING) and kept — so consumer usage + earnings attribution is captured. Agents-in-the-market become first-class consumers on their first paid query.agentIdnull), so anonymous callers cannot spam the table.agentIdnull (never attribute to it).Tests
+4
getAccessContextunit tests (self-register / no-wallet / active / disabled). Full suite 209 pass.