Commit Graph

28 Commits

Author SHA1 Message Date
Teknium
584f6e5dd1 fix(gateway): the app's turn on an old conversation reopens the row the chat's /new ended
Review of #144840 (F4): the detached conversation kept running on a row stamped session_reset, so
in_place:false compression was refused (parent already ended), listings showed it ended, and a
background completion addressed to it was dropped as pinned to a gone session. The drain's claim now
reopens it, as an API or local turn reopens its own row (reopen_api_session).
2026-10-27 17:04:02 -07:00
Teknium
29abd70464 fix(gateway): a chat's old conversation stays usable from the apps, and its replies stop reaching the chat
Teknium's ruling: "I dont want to lock out desktop / mobile app users from a session. Please just
make it so that telegram doesn't get responses from A if they are on a /new'd convo".

When a messaging chat names another conversation (/new, /resume, /branch, a handoff, its next
inbound), every other live session on its route used to go read-only (session_superseded, cold
mounts since 7ba53f4756, handed-off chats since #144708, warm views in this PR's first commit).
Now they DETACH instead (gateway/session_detached.py): the live session moves off the chat's route
onto a key naming the conversation (agent:<ns>:detached:<sid>), pinned on a copy of its source
(SessionSource.detached_route, wire-invisible). Its turns then run on the ordinary messaging path:

- build_session_key answers the pinned key, so the running slot, agent cache, busy guard and the
  agent a Stop reaches are the conversation's own, never the chat's;
- SessionStore.get_or_create_session resolves it to the conversation itself (its compression tip),
  never creating or persisting a route;
- _delivery_adapter_for answers the profile's LocalSessionAdapter, the local sessions' delivery:
  sends publish nothing, viewers read the canonical event stream (local_viewer_turn covers it, so
  status, notices and approvals go there too). session_local_recovery.local_adapter creates it on
  first use, since a messaging-only gateway has none until a local session restores.

The key has no platform slot, so a key parser (_parse_session_key) finds no chat to deliver to.
The chat naming the conversation again (/resume) moves it back onto its route (name_lane), and
its next turn delivers to the chat again. A cold open of a row the lane moved past mounts detached.
/new, /resume and /branch name the lane at once (_name_lane), not at the chat's next message, and
_publish_route does on every publish, so a warm view detaches the moment its chat moves on.
History mounts keep their own read-only gate (require_history_access). LiveSession.superseded,
history_mount and handed_off are gone (with route_recovery.mount/moved_local, which only fed them);
LiveSession.detached says what it means.
2026-10-27 17:04:02 -07:00
unsupportedpastels
22b297098f fix(gateway): answer voice and fast reads on a history mount; promote the mount in place on API bind
Addresses independent review findings on #143601.

Refs #143547
2026-10-25 11:39:45 -04:00
unsupportedpastels
bc629f94e0 fix(gateway): let API binding replace a history mount; check full delete scope
Addresses Greptile review on #143601.

Refs #143547
2026-10-25 11:39:45 -04:00
unsupportedpastels
4e3e66896d fix(gateway): give a bound caller back an API route the heal already forked
Review F2 (P2): the unused-session exemption never matched the fork main's
stale-route heal leaves, because the heal's turn ran in the minted session, so
routes forked before the fix kept answering 409 admission_conflict on every
run. The route is derived from the caller's id, so a holder outside its
compression lineage with no binding of its own is a heal replacement: bind
and restore take the route back (logging the replacement's id) and the
replacement keeps its transcript. A holder with its own binding still refuses.

Refs #143547
2026-10-25 11:38:48 -04:00
unsupportedpastels
439e874e2f fix(gateway): keep finalized bound API routes; hold the owner lock across /new
Addresses maintainer review on #143591.

Refs #143547
2026-10-25 11:38:48 -04:00
unsupportedpastels
c4004c5a87 fix(gateway): stop only the stopped turn and settle stuck admission heads
A Stop that arrived after its run finished interrupted the next turn's agent.
A queued head that kept failing its preclaim check with admission_conflict
paused the drain until restart; it now settles with a definite refusal, and a
route left on an unused session by the stale-route heal is re-pointed to its
caller.

Rows LC-47, LC-31, LC-45.

Refs #143547
2026-10-25 11:38:48 -04:00
unsupportedpastels
8efcf48806 fix(gateway): refuse a chain successor whose walk lost a fence before its bind
A Responses/Runs continuation looks up its chain's live generation before
the binding transaction. If housekeeping reclaims the swept root's sweep
marker in between, keep_passed_fences pinned nothing and returned quietly,
so the successor was bound with its walk unpinned. The next continuation
of the same previous_response_id found the root free, recreated it and ran
there: the chain split as root, successor, root.

- keep_passed_fences now reports whether every generation the walk passed
  still holds a marker, and pins them only then.
- bind_api_steps checks it in the write that binds the successor and
  refuses with chain_moved when the walk no longer holds. The transaction
  rolls back, so nothing is bound or pinned.
- The Responses and Runs doors answer chain_moved as a retryable 503
  (Retry-After: 1). The retry looks the chain up again. The root is free
  by then, so the chain stays on it.

Refs #134889 (LC-25)
Refs #140762
Refs #140731
2026-10-18 01:47:07 -04:00
David Dudok de Wit
f62b8f2003 fix(state): let a swept caller-named API session id age out
A housekeeping sweep keeps a permanent retired-session marker only for an
id a generation walk depends on. _derived_api_sessions picked those API
ids by stored source, and every API session is stored as api_server,
including an id the caller names through POST /api/sessions or
X-Hermes-Session-Id. So once a prune swept such an id, it was refused for
good: POST /api/sessions answered 409 session_deleted and every chat with
that X-Hermes-Session-Id answered 409 not_found, as after a user delete.

- At the sweep, match the derived API ids themselves: every hosted-room
  member (bot_room), and the api-<16 hex> ids that _derive_chat_session_id
  mints for a header-less chat and implicit_api_session mints for a
  chat's or chain's next generation. The id carries its derivation, so
  this also holds for sessions an older release bound, whose binding
  receipts record no provenance. A caller-named id now ages out at the
  horizon; a user delete stays permanent.
- Any id can root a Responses/Runs chain, so a sweep can't recognize one.
  When a continuation moves past a swept stored session to its next
  generation, the bind of that successor makes the passed markers
  permanent (keep_passed_fences, in the binding's write). The Responses
  and /v1/runs doors pass the root as walked_from.
- The API adapter's memory registry is keyed by session id: a manager
  checked in more than the retry horizon ago is shut down at checkout,
  so a session reusing a swept id never gets its predecessor's recall.

Refs #134889
Fixes #140731

(cherry picked from commit 23d83c2b13370423661a8332db00e2fd20c28e8f)
2026-10-18 01:47:07 -04:00
unsupportedpastels
f88003f53a fix(api): continue a deleted Responses or Runs chain on a new session
With the deletion fence permanent, a new request on a retained chain whose
transcript was deleted answered 409 `not_found` forever: Responses
`previous_response_id`, a named Responses `conversation` (unusable for good),
and `/v1/runs` `previous_response_id`. On main these worked past the horizon
only by recreating the deleted transcript id.

The stored session id now goes through `continued_api_session`: the stored id
while it is live, else its next generation, the same walk the header-less
chat uses. The history still comes from the response store. An exact
Responses retry stays pinned to the generation holding its identity, and an
explicit `session_id` is never moved.

Refs #134889 (GW-12)
Refs #140018

Co-authored-by: David Dudok de Wit <5354424+dokterdok@users.noreply.github.com>
2026-10-17 13:16:34 -04:00
unsupportedpastels
96d44275c8 fix(gateway): move a deleted room member or A2A context to a new session
With the deletion fence permanent, two derived ids were refused forever once
their session was deleted: a hosted-room member's `room_<digest>` (fixed for
the life of the room) and an A2A peer context's local identity. On main both
came back after the 7-day horizon by recreating the deleted id.

Both now take the first generation of their id that is not retired, the way
a deleted Bot Chat or header-less chat is replaced. The room member's
binding receipt records its generation, so a restart restores and verifies
it. A room retry is replayed by its run Idempotency-Key, never by this id.
An A2A exact retry of an input the deleted session already took stays pinned
to that retired session and is refused there, never run again.

Refs #134889 (GW-12)
Refs #140018

Co-authored-by: David Dudok de Wit <5354424+dokterdok@users.noreply.github.com>
2026-10-17 13:16:34 -04:00
unsupportedpastels
8fbe279f5e refactor(gateway): one generation walk for derived session ids
The header-less chat walk (`implicit_api_session`) repeated the Bot Chat
creation walk (`_bot_chat_creation_id`). Both now call
`hermes_state_mutation_retirement.first_generation`: the first generation of
a derived id that is not retired, with an optional pin that keeps an exact
retry on the retired generation holding its identity, and an optional
"spent" check (a renamed Bot Chat). No behaviour change; the hosted-room and
A2A paths use it next.

Refs #134889 (GW-12)
2026-10-17 13:16:34 -04:00
unsupportedpastels
24f07b3f13 fix(api): give a new header-less chat a new session after a delete
A header-less Chat Completions turn derives its session id from the
system prompt and first user message, so every turn of an Open
WebUI-style conversation lands on one session. With the retired-session
marker now permanent, a deleted session's derived id is refused for
good: a NEW conversation that opens the same way (same system prompt,
"hi") was answered not_found forever, where main served it again once
the marker expired after 7 days.

The chat door now asks the owning profile's store which session the
derived id names (implicit_api_session): the derived id while it is not
retired, else the first generation of the same opening,
api-<sha256(id, n)>, that is not retired, the way a deleted Bot Chat is
replaced by its next generation. It reads durable rows only, so the
follow-up turns of that conversation, which derive the same id, land on
the same new session, across restarts. The fence itself is unchanged:
the deleted id is never recreated. The lookup runs before either
response path starts, behind the same error boundary as the run: a
store that cannot answer gets the OpenAI JSON error, not aiohttp's
plain-text 500.

An exact retry stays on the generation that holds its request id's
terminal identity, so it replays its original answer (or conflicts on a
changed payload) as before. An explicit X-Hermes-Session-Id never comes
here and stays refused. A key spent on another session is still refused
by the admission's one-target guard on the new generation.

Refs #134889 (GW-12)
Refs #140208
Refs #140018
2026-10-17 13:16:34 -04:00
Teknium
9585f789bf docs(api): bind_api_steps docstring says caller_key is registered on adoption
The docstring said caller_key "is never registered as the conversation's
declared key", but on adoption the binding transaction inserts it into the
declared-key registry and stamps it into the binding receipt, which the
key-only follow-up relies on. Reword to match. No behaviour change.

Review finding by @unsupportedpastels on #140184
Refs #134889 (LC-13)
2026-10-16 19:21:03 -07:00
Teknium
42bee65cec fix(api): adopt pre-binding API sessions under the caller's declared key
Earlier releases stored the declared X-Hermes-Session-Key (e.g. webui:<id>)
in sessions.session_key and kept no declared-key registry. On main those rows
409 on every /v1/runs:

- with an explicit session_id, bind_api_steps got no declared_key (only a
  key that SELECTED the conversation is passed), so the cherry-picked
  equality check could never match on the issue's own request;
- with the key alone, declared_api_session found no registry entry and the
  run went to a fresh run_id session, losing the conversation.

The binding now also receives the request's validated header key
(caller_key) for adoption only. A row stamped with exactly that key is
rewritten to the canonical route and the key is registered for it in the
same transaction (refused if the registry names another session), so later
key-only requests reach the same row. declared_api_session falls back to
the legacy row lookup when the registry has no entry. Any other stored key
still conflicts, and a refused caller never rewrites the row.

Test: tests/gateway/test_api_runs_legacy_declared_key.py drives the real
/v1/runs HTTP door against legacy-shaped rows (red on main: 409).

Refs #139835
Refs #134889 (LC-13)
Refs #140138
2026-10-16 19:21:02 -07:00
KoNit-K
9e82766ad8 fix(api): adopt legacy declared session keys
(cherry picked from commit 7c9384097732d22e6fb539a940386c2d282ca277)
2026-10-16 19:21:02 -07:00
Teknium
11f4978b76 chore(api): waive BLE001 on the admission step driver (exceptions are re-thrown)
run_steps / run_steps_off_loop catch a yielded write's exception only to throw it back into the
step generator, which re-raises it (or cleans up and re-raises). Nothing is swallowed.

(cherry picked from commit bc5ad10abec9390434a41dc5e8e0f08c582dcdb8)
2026-10-10 22:19:45 -07:00
Teknium
47b24028dd fix(api): API admission writes run off the owner loop in admission order
admit_api_turn was synchronous and every HTTP door (Chat Completions, Responses, /v1/runs,
/api/sessions/{id}/chat via run_api_turn) called it from its aiohttp coroutine, so
bind_api_session's binding transaction, admit_session_input and the refused-image release ran
SQLite writes on the shared owner loop. A writer held elsewhere (up to _WRITE_PATIENCE_S) froze
every session, timer, Stop and approval on the gateway, the same N14 hazard _submit already
avoids with tracked_write(ordered=True).

Admission is now a write-step generator (admit_api_steps -> bind_api_steps): each SQLite write is
yielded and the async door (admit_api_turn_async / run_steps_off_loop) runs it through
tracked_write(ordered=True), so writes leave the loop in admission order. The drive is one
tracked, shielded task, serialized per authority, so the Responses lookup->admission stays one
step among API doors (simultaneous observers still converge), retirement joins it, and a caller
cancelled mid-write hands the committed row its drain. Both write transactions re-check the
admission gate inside the transaction (a drain that began while the write waited still wins).
admit_api_turn keeps its sync shape for in-process callers by driving the same steps inline.

Test: tests/gateway/test_api_admission_off_loop.py::test_admission_waits_for_the_writer_off_the_owner_loop[chat|responses|runs]

Review: dokterdok N32
(cherry picked from commit 8811b07e65e1ea773c34c95980a051e38c522d03)
2026-10-10 22:19:45 -07:00
David Dudok de Wit
73472130cb fix(api): restore declared conversations at their current physical tip
Refresh a declared API route from any genuine compression ancestor while retaining its logical admission identity.

Reported-by: itsflownium (https://github.com/NousResearch/hermes-agent/pull/106742#issuecomment-6050777316)
Reported-by: greptile-apps (https://github.com/NousResearch/hermes-agent/pull/106742#issuecomment-6041818267)

Validation: tests/gateway/test_api_authority_continuity.py, tests/gateway/test_api_authority_retry.py

Implementation and additional regression probes were AI-assisted with GPT-6 Astra.

(cherry picked from commit 16bdc091692fa8361400933c3ae2e27d37f3d344)
2026-10-08 09:10:27 -07:00
Teknium
1a8f3bdf69 fix(state): deleting a session also removes its launch policy and API binding receipts
The deletion fence scrubbed admission payloads and worker results but left
gateway.local_policy.v1:<id> (whose request_json holds a cron fire's job
and raw prompt, gateway/session_cron.py) and gateway.api.binding.v1 /
gateway.api.conversation.v1 receipts in state_meta forever. Every boot's
recover_local_sessions then walked the orphan policy and logged
"Local session ... paused: storage_unavailable", and a declared API
conversation key kept resolving to the deleted id.

retire_sessions (every delete/prune/sweep path) now calls
hermes_state_local.retire_local_receipts: a policy goes when its current
physical target is deleted (a pruned reset ancestor whose session lives on
keeps it); an API binding and any declared key pointing at it go with the
id. The API prefixes move beside POLICY_PREFIX so the state layer owns them.
commit_local_session and bind_api_session refuse a retired id with
not_found, so a create/cron/API retry cannot resurrect a deleted session
under its deterministic id.

Review: https://github.com/NousResearch/hermes-agent/pull/106742 (@itsflownium, Persistence: delete fence leaves policy/binding receipts)
2026-10-08 01:39:21 -07:00
Teknium
16a151078a fix(api): compressed API conversations keep accepting follow-ups
With `compression.in_place=false` the TurnRunner advances the routing entry to the
compression child, while the API binding (and the durable FIFO) stays on the root.
`restore_api_session` treated that expected route change as a foreign takeover, so
the next request on the same `X-Hermes-Session-Key`, and any follower already
queued behind the compressing turn (via `check_api_turn` in the drain), failed
with `admission_conflict` and the conversation was dead.

The root remains the queue identity; a route that sits on the root's own
compression lineage is accepted as-is, and a cold restore resumes the route at the
live compression tip. A route moved to an unrelated session (/new, another
binding) still refuses with `admission_conflict`.

Review: https://github.com/NousResearch/hermes-agent/pull/106742#discussion_r4213427120
2026-10-07 20:47:56 -07:00
teknium1
7c7e56db93 fix(gateway): a /p/<profile>/ API session runs under the profile whose authority admitted it
Under multiplex the launch profile's api_server listener receives every
/p/<name>/ request and the routed authority admits the turn, but the
turn's runtime home comes from the RoutingIdentity pinned on the session
source. bind_api_session built that source with no profile, so
resolve_identity placed every API session in the launch profile:
alpha's turn ran with default's config, model and provider key
(tests/e2e/core/tenancy/test_two_tenant_gateway.py: 'alpha was answered
by someone else').

The bound source now carries the authority's served profile name and a
pinned shared-listener identity (transport = launch profile,
runtime = served profile), persisted as SessionEntry.transport_profile
so a restart restores the same placement; the session key lands in the
agent:<profile> namespace instead of agent:main.
2026-09-24 03:57:14 -07:00
teknium1
dc3ccba3e9 fix(gateway): stamp API, cron and reset routing entries with the store clock too
Same aware-vs-naive boundary as the local producer: bind_api_session, cron session creation and the prepared local reset published aware UTC SessionEntry timestamps into the routing index, so one such entry aborted the naive-cutoff recovery sweep for every later route.

Findings: F29
2026-09-12 10:24:03 -07:00
Teknium
f48d09563b fix: preserve API content identity history and terminal outcomes 2026-09-10 05:16:53 -07:00
Teknium
06a9516c16 fix: preserve hosted API source during peer persistence 2026-09-08 18:43:50 -07:00
Teknium
6b278ca4f0 feat: reserve hosted API identities through the authority 2026-09-08 18:01:06 -07:00
Teknium
dea4f0c568 feat: run bound API turns on the shared cached agent 2026-09-08 18:01:06 -07:00
Teknium
76532ae133 feat: restore server-bound API session identities 2026-09-08 18:01:06 -07:00