Skip to content

The agent that grows with you

feat: route revision-checked session metadata through gateway

Teknium17ddbe4407a6
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index 9640b4639d4..605da532904 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -33,6 +33,7 @@ class AuthorityConnection:
ref = SessionRef(self.actor.profile_id, params.get('session_id', ''))
handlers = {'session.create': self.create, 'ping': self.ping, 'runtime.describe': self.describe,
'session.list': self.list_sessions, 'session.info': self.info,
+ 'session.mutate': self.mutate,
'session.resume': self.resume, 'prompt.submit': self.submit,
'prompt.receipt': self.receipt, 'prompt.cancel': self.cancel,
'session.interrupt': self.interrupt, 'session.events.since': self.events_since,
@@ -134,6 +135,17 @@ class AuthorityConnection:
request_id, ref, {'text': params.get('text')}, 'queue'))
return asdict(receipt)
+ async def mutate(self, ref, params):
+ from hermes_state_runtime import mutate_runtime_session
+ self.authority.authorize(self.actor, ref, 'session:control')
+ self.authority._require_admission_open()
+ if set(params) != {'session_id', 'request_id', 'expected_revision', 'operation', 'payload'}:
+ raise RuntimeStoreError('invalid_params')
+ return mutate_runtime_session(
+ self.authority.db, epoch=self.authority.epoch, principal_id=self.actor.subject,
+ session_id=ref.session_id, request_id=params['request_id'],
+ expected_revision=params['expected_revision'], operation=params['operation'], payload=params['payload'])
+
async def receipt(self, ref, params):
return asdict(await self.authority.receipt(self.actor, ref, params.get('admission_id')))
diff --git a/tests/gateway/test_session_mutation_rpc.py b/tests/gateway/test_session_mutation_rpc.py
new file mode 100644
index 00000000000..9e2265001fc
--- /dev/null
+++ b/tests/gateway/test_session_mutation_rpc.py
@@ -0,0 +1,47 @@
+"""Competing viewers must not overwrite a newer metadata revision."""
+from types import SimpleNamespace
+
+import pytest
+
+from gateway.session_authority import LiveSession, SessionAuthority
+from gateway.session_controls import AuthorityConnection
+from hermes_state import SessionDB
+from hermes_state_runtime import begin_runtime_epoch
+
+
+async def test_mutation_rpc_fences_retries_and_competing_viewers(tmp_path):
+ with SessionDB(db_path=tmp_path / 'state.db') as db:
+ db.create_session('s', source='test')
+ epoch = begin_runtime_epoch(db, instance_id='current')
+ runner = SimpleNamespace(_draining=False)
+ authority = SessionAuthority(runner, profile_id='owned', instance_id='current', db=db, epoch=epoch)
+ authority.sessions['s'] = LiveSession(None, 'route')
+ owner = AuthorityConnection(authority, object(), {'user_id': 'owner'})
+ viewer = AuthorityConnection(authority, object(), {'user_id': 'reader', 'capabilities': ['session:read']})
+ request = {'id': 1, 'method': 'session.mutate', 'params': {
+ 'session_id': 's', 'request_id': 'rename-1', 'expected_revision': db.get_session('s')['runtime_revision'],
+ 'operation': 'rename', 'payload': {'title': 'Owned title'}}}
+ try:
+ denied = await viewer.dispatch(request)
+ assert denied['error']['message'] == 'permission_denied', denied
+ first = await owner.dispatch(request)
+ assert first['result']['title'] == 'Owned title', first
+ revision = db.get_session('s')['runtime_revision']
+ assert await owner.dispatch(request) == first
+ assert db.get_session('s')['runtime_revision'] == revision
+ competing = {**request, 'params': {**request['params'], 'request_id': 'rename-2',
+ 'payload': {'title': 'Stale title'}}}
+ conflict = await owner.dispatch(competing)
+ assert conflict['error']['message'] == 'revision_conflict', conflict
+ assert db.get_session('s')['title'] == 'Owned title'
+ archived = await owner.dispatch({**request, 'params': {**request['params'],
+ 'request_id': 'archive-1', 'expected_revision': revision,
+ 'operation': 'archive', 'payload': {'archived': True}}})
+ assert archived['result']['archived'] is True, archived
+ runner._draining = True
+ stopped = await owner.dispatch(request)
+ assert stopped['error']['message'] == 'runtime_draining', stopped
+ finally:
+ await owner.close()
+ await viewer.close()
diff --git a/website/docs/developer-guide/gateway-internals.md b/website/docs/developer-guide/gateway-internals.md
index 8336190414b..aaaaa749d36 100644
--- a/website/docs/developer-guide/gateway-internals.md
+++ b/website/docs/developer-guide/gateway-internals.md
@@ -17,7 +17,7 @@ native frontend already renders the shared form or that ordinary bootstrap is co
A response uses `clarify.respond` with `session_id`, `execution_generation`, `prompt_id`,
and an `answer` string. The connected viewer must be subscribed and have
-`session:control`. The authority checks the running generation and resolves the existing
+`session:respond`. The authority checks the running generation and resolves the existing
native clarify waiter by exact prompt ID, without adding a user prompt to the admission
queue. The first response wins; detach does not cancel it. Native answers and timeouts
retire the same prompt and emit `clarify.settled`; response text is not placed in the
@@ -29,6 +29,21 @@ create an actionable shared form. Ordinary approvals use the separate `approval.
permission path; sudo/secret prompts and durable control restoration after daemon restart
are not implemented by this clarification path.
+## Revision-checked session metadata
+
+The authority-bound WebSocket accepts `session.mutate` for a registered session.
+Parameters are `session_id`, `request_id`, `expected_revision`, `operation`, and
+`payload`. `rename` accepts only `{"title": "New title"}`; `archive` accepts only
+`{"archived": true}` or `{"archived": false}`. The caller needs `session:control`.
+The session handle in `session.resume` supplies the current revision.
+
+The edit and its retry receipt commit together. Repeating the same request returns
+the original result without incrementing the revision again; reusing its ID with
+different contents returns `admission_conflict`. A competing edit with a stale
+revision returns `revision_conflict`. Draining rejects new mutations. These edits
+do not interrupt a running turn. This RPC does not yet migrate legacy direct
+writers, expose arbitrary SQL, or implement reset, delete, or rewind.
+
## Key Files
| File | Purpose |