Skip to content

The agent that grows with you

fix(gateway): a Desktop chat saved before the local mark moved still resumes and runs as local work

alt-glitche500371ae1ca
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index a9266322a3c..78449444bb4 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -1014,7 +1014,9 @@ def local_desktop_turn(runner, source, scope):
policy = None
if getattr(source, 'platform', None) == Platform.LOCAL:
policy = getattr(runner._adapter_for_source(source), 'policies', {}).get(source.chat_id)
- if policy is None or policy.source != 'gui' or json.loads(policy.config_json).get(LOCAL_DESKTOP_CHAT) is not True:
+ # The request_json mark: a chat an earlier build created, which stamped it into the creation request.
+ if policy is None or policy.source != 'gui' or not any(
+ json.loads(raw).get(LOCAL_DESKTOP_CHAT) is True for raw in (policy.config_json, policy.request_json)):
return scope
return _serving_local_desktop_within(scope)
diff --git a/hermes_state_local.py b/hermes_state_local.py
index 572ba6ddba7..ca810f57191 100644
--- a/hermes_state_local.py
+++ b/hermes_state_local.py
@@ -87,8 +87,11 @@ def commit_local_session(db, *, epoch, receipt):
def client_request(request_json, derived=None):
"""A frozen create request as the client sent it: an owner-resolved ``-m`` route (``model_requested``)
compares by the name the client typed, not by the model and the *derived* fields (a receipt from
- before ``route_derived``: the endpoint) its ``${VAR}`` refs expanded to at the time."""
+ before ``route_derived``: the endpoint) its ``${VAR}`` refs expanded to at the time. ``desktop_local``
+ (``gateway.session_policy.LOCAL_DESKTOP_CHAT``), which an earlier owner stamped into the request, was
+ never sent."""
request = json.loads(request_json)
+ request.pop('desktop_local', None)
if derived is None:
request.pop('route_derived', None)
if 'model_requested' in request:
diff --git a/tests/gateway/test_local_desktop_identity.py b/tests/gateway/test_local_desktop_identity.py
index 6840f2531f4..1b88df0d152 100644
--- a/tests/gateway/test_local_desktop_identity.py
+++ b/tests/gateway/test_local_desktop_identity.py
@@ -320,3 +320,49 @@ async def test_a_desktop_boot_that_could_not_record_the_install_tries_again_on_t
finally:
await conn.connection.close()
free_tier_bootstrap.reset_for_tests()
+
+
+async def test_a_desktop_chat_saved_before_the_mark_moved_still_resumes_and_runs_as_the_local_desktops(
+ tmp_path, monkeypatch):
+ """An earlier build stamped ``desktop_local`` into the frozen create request. The Desktop's exact retry
+ after a reconnect never sends it, so it still gets that chat, and the chat's turns still run as
+ work for the local Desktop."""
+ import json
+ from contextlib import nullcontext
+ from gateway.session_local import publish_local_policy
+ from gateway.session_local_recovery import local_source
+ from gateway.session_policy import local_desktop_turn
+ from hermes_cli.anon_auth import on_local_desktop_backend
+ from hermes_state_local import POLICY_PREFIX
+
+ conn = await _connect(tmp_path, monkeypatch,
+ config="model:\n provider: openrouter\n default: anthropic/claude-sonnet-4\n")
+ try:
+ await _request(conn, 1, "client.capabilities", {"desktop_local": True})
+ stored = (await _request(conn, 2, "session.create", {"source": "gui", "request_id": "a"}))[
+ "result"]["stored_session_id"]
+ db = conn.authority.db
+
+ def saved_the_old_way(c):
+ key = POLICY_PREFIX + stored
+ receipt = json.loads(c.execute("SELECT value FROM state_meta WHERE key=?", (key,)).fetchone()[0])
+ policy = receipt["policy"]
+ config = json.loads(policy["config_json"])
+ config.pop("desktop_local")
+ policy["config_json"] = json.dumps(config)
+ policy["request_json"] = json.dumps({**json.loads(policy["request_json"]), "desktop_local": True},
+ sort_keys=True)
+ c.execute("UPDATE state_meta SET value=? WHERE key=?", (json.dumps(receipt), key))
+ db._execute_write(saved_the_old_way)
+ publish_local_policy(conn.authority, stored, evict=False) # the chat as an owner restores it
+ conn.transport.desktop_local = False
+
+ retry = await _request(conn, 3, "session.create", {"source": "gui", "request_id": "a"})
+ assert "result" in retry, retry
+ assert retry["result"]["stored_session_id"] == stored
+ source = local_source(conn.authority, stored, conn.connection.actor.subject)
+ with local_desktop_turn(conn.runner, source, nullcontext()):
+ assert on_local_desktop_backend() is True
+ finally:
+ await conn.connection.close()