Files
hermes-agent/gateway/platforms/api_server_response_identity.py
Teknium feccfcca8f fix(api): settle Responses identities from their admission's terminal outcome
A Responses Idempotency-Key identity settled only when its observer stored
the idem: replay record. A streaming request whose client disconnected
before the final SSE frame still completed canonically, but nobody was left
to write that record, so its response_keys row kept settled_at NULL for
good: it never counted toward the identity bound, survived reopen and
eviction, and DELETE /v1/responses/<id> answered 404 because the row had no
response_id. Each row is small, but the producer is ongoing (four
disconnected streams with a bound of 2 all stayed).

Round 9 already settled such rows from the home's state.db on store open.
That reconcile now also runs before every bound decision (put/eviction) and
before a public delete, independently of any HTTP observer. It settles only
an identity whose canonical admission is terminal (live row or retired
tombstone), stamps it with the current time (open keeps acceptance time for
migrated rows), and records its deterministic response id so delete reaches
it; the id derivation moved to api_server_response_identity so the route and
the store share it. Queued, started and unknown admissions keep their
identity untouched, and an aged-out or deleted key is still refused with
admission_conflict rather than run again.

Review: https://github.com/NousResearch/hermes-agent/pull/106742#pullrequestreview-5475590057
2026-10-10 05:52:03 -07:00

32 lines
1.4 KiB
Python

"""Stable presentation identity for a durable Responses admission.
The model label, creation time and compact ID/index ordering survive response-body eviction.
Item IDs derive from response identity and occurrence; no additional transcript/output copy is retained.
"""
import hashlib
import json
_PREFIXES = {'function_call': 'fc', 'function_call_output': 'fco', 'reasoning': 'rs', 'message': 'msg'}
def durable_response_id(request_key):
"""The public response id of an Idempotency-Key request; the store derives it again for an
identity whose replay record was never written."""
return 'resp_' + hashlib.sha256(request_key.encode()).hexdigest()[:28]
def next_item_id(response_id, counters, kind, *, call_id=None, commentary=False):
# The final message is distinct from any live commentary, which is not part of the answer.
key = (kind, call_id, bool(commentary))
occurrence = counters.get(key, 0)
counters[key] = occurrence + 1
digest = hashlib.sha256(json.dumps([response_id, *key, occurrence], separators=(',', ':')).encode()).hexdigest()
return _PREFIXES[kind] + '_' + digest[:24]
def identify_output(items, response_id):
counters = {}
return [dict(item, id=next_item_id(response_id, counters, item['type'],
call_id=item.get('call_id'), commentary=item.get('phase') == 'commentary'))
for item in items]