Skip to content

fix(attribution): self-register consumer agents so consumer_agent_id is captured - #38

Merged
JulioMCruz merged 1 commit into
mainfrom
fix/consumer-agent-attribution
Jun 26, 2026
Merged

fix(attribution): self-register consumer agents so consumer_agent_id is captured#38
JulioMCruz merged 1 commit into
mainfrom
fix/consumer-agent-attribution

Conversation

@JulioMCruz

Copy link
Copy Markdown
Contributor

Closes the "consumer_agent_id empty from the baked plugin" gap.

Root cause

getAccessContext resolved x-agent-id against the agents table and nulled it out when the agent had no row there (a guard against FK-violation 500s on the consumer_agent_id REFERENCES agents(id) columns in knowledge_attributions / knowledge_usage_events / x402_receipts). Provisioned agents send KNOWLEDGE_AGENT_ID + KNOWLEDGE_AGENT_WALLET but are never inserted into the Knowledge agents table, so their consumer attribution was always dropped.

Fix

An unknown x-agent-id that also presents a wallet is now self-registered as a consumer row (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.

  • No wallet → still treated as a public consumer (agentId null), so anonymous callers cannot spam the table.
  • Disabled agent → agentId null (never attribute to it).

Tests

+4 getAccessContext unit tests (self-register / no-wallet / active / disabled). Full suite 209 pass.

…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).
@JulioMCruz
JulioMCruz merged commit ef68566 into main Jun 26, 2026
1 check passed
@JulioMCruz
JulioMCruz deleted the fix/consumer-agent-attribution branch June 26, 2026 04:16
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