fix: bound the token cache, and make peer message ids unique

Two bugs from review, the second in both agents because identity.py is a copy.

The exchanged-token cache grew for the life of the process. It is keyed on
(subject token, scope) and the subject token is minted fresh every turn, so
every turn added keys nothing would ever read again — and expiry was checked on
read but never collected. A slow leak that only shows in a pod up for days,
which is the worst way to find one.

It now drops expired entries first and evicts oldest beyond a bound. Expired
first on purpose: an expired entry is dead weight wherever it sits in the
ordering, so evicting by age alone would keep it while discarding a live one.

The docstring was wrong before the code was: it claimed the cache lasted one
turn. It never did. Corrected rather than quietly made true.

message_id reused the context id, which is the conversation — and a conversation
makes several calls to the same peer, relaying an approval being exactly that.
A peer treating the second call as a replay of the first would surface as a
stale answer rather than an error.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Stéphane Tailland
2026-08-30 08:03:23 +02:00
parent 3c44cc63ea
commit f79b58fc88
8 changed files with 122 additions and 7 deletions
+8 -1
View File
@@ -13,6 +13,7 @@ client is a long-lived token.
from __future__ import annotations
import logging
import uuid
from dataclasses import dataclass
import httpx
@@ -72,7 +73,13 @@ class Delegator:
try:
request = a2a_types.SendMessageRequest(
message=a2a_types.Message(
message_id=f"{context_id}-{specialist.name}",
# Unique per message. The context id is the
# conversation, and a conversation makes several calls to
# the same specialist — relaying an approval is exactly
# that. Reusing an id invites a peer to treat the second
# call as a replay of the first, which surfaces as a
# stale answer rather than as an error.
message_id=f"{context_id}-{specialist.name}-{uuid.uuid4().hex[:8]}",
# Carried across so the specialist keeps one conversation
# per user conversation rather than one per question —
# which is what lets its own approval flow resume.