Skip to content

The agent that grows with you

fix(gateway): keep inherited picks on branches created before picks were recorded

unsupportedpastels8ad07e6365e3
diff --git a/hermes_state_mutation_branch.py b/hermes_state_mutation_branch.py
index ba6b6460207..5667d38010d 100644
--- a/hermes_state_mutation_branch.py
+++ b/hermes_state_mutation_branch.py
@@ -92,9 +92,9 @@ def branch_in_transaction(db, conn, session_id, payload, prepared=None):
receipt = dict(profile_id=saved['profile_id'], principal_id=saved['principal_id'],
request_id=request_id, session_id=child, route=route, entry=entry.to_dict(), policy=policy)
# The copied policy holds the parent's picks; the child's refresh must keep them as picks, not defaults.
+ # Always written: a receipt without it predates the field, and picks_on reads that one's lineage.
from hermes_state_mutation_prepared import picks_on
- if picks := picks_on(conn, session_id):
- receipt['picks'] = sorted(picks)
+ receipt['picks'] = sorted(picks_on(conn, session_id))
conn.execute("INSERT INTO gateway_routing(scope,session_key,entry_json,updated_at) VALUES('',?,?,?)",
(route, _json(entry.to_dict()), now.timestamp()))
conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?)', (POLICY_PREFIX + child, _json(receipt)))
diff --git a/hermes_state_mutation_prepared.py b/hermes_state_mutation_prepared.py
index 0c929b58f6f..3ea0f15554a 100644
--- a/hermes_state_mutation_prepared.py
+++ b/hermes_state_mutation_prepared.py
@@ -89,17 +89,30 @@ POLICY_HANDLERS = {'model': model_in_transaction, 'reasoning': reasoning_in_tran
'workspace': workspace_in_transaction, 'editor_mcp': editor_mcp_in_transaction}
-def picks_on(conn, session_id):
- """The policy edits (``reasoning``, ``workspace``) a ``session.mutate`` ever committed for *session_id*:
- their receipts (kept for exact retries, also past deletion) are the durable record of the user's pick.
- A branch copied its parent's policy, picks included, under its own id: its local receipt's ``picks``."""
+_RECEIPTS = "FROM state_meta WHERE key GLOB 'gateway.mutation.v1.*' AND json_valid(value) "
+
+
+def picks_on(conn, session_id, before=None):
+ """The policy edits (``reasoning``, ``workspace``) a ``session.mutate`` committed for *session_id* (below
+ revision *before*): their receipts (kept for exact retries, also past deletion) are the durable record of
+ the user's pick. A branch copied its parent's policy, picks included, under its own id: its local
+ receipt's ``picks``; a branch stored before that field has its parent's as they stood at its branch."""
+ bound = '' if before is None else " AND json_extract(value, '$.result.revision')<?"
picked = {row[0] for row in conn.execute(
- "SELECT DISTINCT json_extract(value, '$.result.operation') FROM state_meta "
- "WHERE key GLOB 'gateway.mutation.v1.*' AND json_valid(value) "
+ "SELECT DISTINCT json_extract(value, '$.result.operation') " + _RECEIPTS +
"AND json_extract(value, '$.result.operation') IN ('reasoning', 'workspace') "
- "AND json_extract(value, '$.result.session_id')=?", (session_id,))}
+ "AND json_extract(value, '$.result.session_id')=?" + bound, (session_id, *(() if before is None else (before,))))}
saved = conn.execute('SELECT value FROM state_meta WHERE key=?', (POLICY_PREFIX + session_id,)).fetchone()
- return picked | set(json.loads(saved[0]).get('picks', ())) if saved else picked
+ if saved is None:
+ return picked
+ if 'picks' in (receipt := json.loads(saved[0])):
+ return picked | set(receipt['picks'])
+ # The parent's branch receipt names this session and the parent's revision the branch committed at.
+ branched = conn.execute(
+ "SELECT json_extract(value, '$.result.session_id'), json_extract(value, '$.result.revision') " + _RECEIPTS +
+ "AND json_extract(value, '$.result.operation')='branch' "
+ "AND json_extract(value, '$.result.branched_session_id')=?", (session_id,)).fetchone()
+ return picked | picks_on(conn, *branched) if branched else picked
def policy_picks(db, session_id):
diff --git a/tests/gateway/test_session_resume_config_refresh.py b/tests/gateway/test_session_resume_config_refresh.py
index 27158d2d817..e53cc9ac3bf 100644
--- a/tests/gateway/test_session_resume_config_refresh.py
+++ b/tests/gateway/test_session_resume_config_refresh.py
@@ -427,6 +427,38 @@ async def test_a_branch_keeps_the_reasoning_and_workspace_its_parent_picked(tmp_
store.close_all_db_handles()
+async def test_a_branch_stored_before_receipts_carried_picks_keeps_its_lineages_picks(tmp_path, monkeypatch):
+ """A branch stored before its receipt recorded ``picks`` takes them from its lineage: the parent's pick
+ receipts committed before the branch, and the parent's own inherited picks, walking up. A pick the parent
+ committed after the branch is not the branch's."""
+ from hermes_state_local import POLICY_PREFIX
+ from hermes_state_mutation_prepared import policy_picks
+ home = _home(monkeypatch)
+ store, authority = await _owner_authority(home)
+ owner, later = _connect(authority), _connect(authority)
+ try:
+ sid = (await _call(owner, 'session.create', request_id='p', source='cli', cwd=str(tmp_path)))['session_id']
+ await _mutate(owner, store, sid, 'pick', 'reasoning', {'effort': 'medium'})
+ child = (await _mutate(owner, store, sid, 'fork', 'branch', {}))['branched_session_id']
+ grandchild = (await _mutate(owner, store, child, 'fork-again', 'branch', {}))['branched_session_id']
+ (tmp_path / 'project').mkdir()
+ await _mutate(owner, store, sid, 'move', 'workspace', {'cwd': str(tmp_path / 'project')})
+ for branch in (child, grandchild): # the receipt shape before this field existed
+ store._db._execute_write(lambda conn, key=POLICY_PREFIX + branch: conn.execute(
+ "UPDATE state_meta SET value=json_remove(value, '$.picks') WHERE key=?", (key,)))
+ (home / 'config.yaml').write_text(AFTER)
+ for branch in (child, grandchild):
+ assert policy_picks(store._db, branch) == {'reasoning'}, branch
+ await _call(later, 'session.resume', session_id=branch)
+ policy = _policy(authority, branch)
+ assert (policy.max_turns, policy.reasoning_config.get('effort')) == (7, 'medium'), branch
+ finally:
+ for connection in (owner, later):
+ await connection.close()
+ store.close_all_db_handles()
+
+
class _Sandbox:
cleaned = False