Skip to content

The agent that grows with you

fix(worker): managed workers receive main's transcript row state

teknium1d86eac03207d
diff --git a/agent/runtime_session_compression.py b/agent/runtime_session_compression.py
index 897c5cb46aa..f8490b19944 100644
--- a/agent/runtime_session_compression.py
+++ b/agent/runtime_session_compression.py
@@ -8,8 +8,8 @@ class RuntimeSessionCompressionMixin:
result = self._apply('compression.append', dict(messages=messages,
compression_lock_holder=compression_lock_holder, turn_lease_holder=turn_lease_holder,
turn_lease_ttl_seconds=turn_lease_ttl_seconds))
- for message, annotation in zip(messages, result['annotations'], strict=True):
- message.update(annotation)
+ from agent.runtime_session_store import apply_row_annotations
+ apply_row_annotations(messages, result['annotations'])
return result['count']
def get_session(self, session_id):
diff --git a/agent/runtime_session_store.py b/agent/runtime_session_store.py
index 18f3ba89c37..1392ff1056f 100644
--- a/agent/runtime_session_store.py
+++ b/agent/runtime_session_store.py
@@ -26,6 +26,16 @@ def is_worker_process():
return _worker_process
+
+def apply_row_annotations(messages, annotations):
+ """Mirror the owner's in-place row stamps onto the worker's dicts (None = the owner popped it)."""
+ for message, annotation in zip(messages, annotations, strict=True):
+ for key, value in annotation.items():
+ if value is None:
+ message.pop(key, None)
+ else:
+ message[key] = value
+
class WorkerPersistenceError(RuntimeError):
pass
@@ -216,8 +226,7 @@ class RuntimeSessionStore(RuntimeSessionCompressionMixin, RuntimeSessionLifecycl
return self._append_compression_messages(session_id, messages, compression_lock_holder,
turn_lease_holder, turn_lease_ttl_seconds)
result = self._apply('transcript.append', {'messages': messages, 'turn_lease_holder': turn_lease_holder})
- for message, annotation in zip(messages, result['annotations'], strict=True):
- message.update(annotation)
+ apply_row_annotations(messages, result['annotations'])
return result['count']
def try_acquire_session_turn_lease(self, session_id, holder, *, ttl_seconds=300.0, patience_s=None):
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index d6c62ce8001..48960a91fb2 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -6,6 +6,7 @@ SessionDB transaction owner, including its inode guard and SQLite retry policy.
import json
import uuid
+from agent.message_metadata import CANONICAL_ROW, DB_ROW_SNAPSHOT
from gateway.session_admission import admission_fingerprint
@@ -510,8 +511,15 @@ _MESSAGE_FIELDS = frozenset({
'finish_reason', 'reasoning', 'reasoning_content', 'reasoning_details',
'codex_reasoning_items', 'codex_message_items', 'platform_message_id', 'message_id',
'observed', 'effect_disposition', '_compressed_summary', 'timestamp', 'api_content',
- 'display_kind', 'display_metadata', '_row_id', '_canonical_content',
+ 'display_kind', 'display_metadata', '_row_id', '_canonical_content', DB_ROW_SNAPSHOT, CANONICAL_ROW,
})
+# Row state the owner's transcript repair stamps on each message (main mutates the caller's dict
+# in place; a worker gets it back as an annotation). None = absent, so a stale adoption is cleared.
+_ROW_ANNOTATION_KEYS = ('_row_id', 'timestamp', DB_ROW_SNAPSHOT, CANONICAL_ROW)
+
+
+def row_annotations(messages):
+ return [{key: msg.get(key) for key in _ROW_ANNOTATION_KEYS} for msg in messages]
def _worker_append(db, conn, session_id, payload):
@@ -533,8 +541,7 @@ def _worker_append(db, conn, session_id, payload):
if holder is not None:
_text(holder)
count = db._append_messages_in_transaction(conn, session_id, messages, turn_lease_holder=holder)
- return {'count': count, 'annotations': [
- {key: msg[key] for key in ('_row_id', '_canonical_content') if key in msg} for msg in messages]}
+ return {'count': count, 'annotations': row_annotations(messages)}
def _worker_turn(db, conn, session_id, payload, operation):
diff --git a/hermes_state_worker_compression.py b/hermes_state_worker_compression.py
index 718be3c5a3f..5811b9635ad 100644
--- a/hermes_state_worker_compression.py
+++ b/hermes_state_worker_compression.py
@@ -478,8 +478,8 @@ def worker_compression_append(db, conn, sid, payload):
if not 0.1 <= _number(payload['turn_lease_ttl_seconds']) <= 3600:
raise RuntimeStoreError('invalid_params')
count = db._append_messages_in_transaction(conn, sid, **payload)
- return {'count': count, 'annotations': [
- {key: msg[key] for key in ('_row_id', '_canonical_content') if key in msg} for msg in payload['messages']]}
+ from hermes_state_runtime import row_annotations
+ return {'count': count, 'annotations': row_annotations(payload['messages'])}
def worker_turn_cleanup(db, conn, sid, payload):
diff --git a/tests/hermes_cli/test_session_maintenance_http.py b/tests/hermes_cli/test_session_maintenance_http.py
index ed01217d0d9..6605e087aa3 100644
--- a/tests/hermes_cli/test_session_maintenance_http.py
+++ b/tests/hermes_cli/test_session_maintenance_http.py
@@ -45,6 +45,6 @@ async def test_exclusive_offline_maintenance_preserves_bulk_behavior(tmp_path, m
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url='http://localhost') as client:
result = await client.post('/api/sessions/bulk-delete', json={'ids': ['parent', 'absent']})
assert result.status_code == 200, result.text
- assert result.json() == {'ok': True, 'deleted': 1}
+ assert result.json() == {'ok': True, 'deleted': 1, 'skipped_active': []}
assert db.get_session('parent') is None
assert db.get_session('child')['parent_session_id'] is None
diff --git a/tests/hermes_state/test_runtime_worker_operations.py b/tests/hermes_state/test_runtime_worker_operations.py
index 6fbde4aeb20..8806deb7608 100644
--- a/tests/hermes_state/test_runtime_worker_operations.py
+++ b/tests/hermes_state/test_runtime_worker_operations.py
@@ -3,6 +3,7 @@ import pytest
from hermes_state import SessionDB
from hermes_state_runtime import RuntimeStoreError, begin_runtime_epoch, register_worker_execution
+from agent.message_metadata import CANONICAL_ROW, DB_ROW_SNAPSHOT
def test_structured_worker_receipts_are_atomic_and_scoped(tmp_path):
@@ -51,7 +52,9 @@ def test_structured_worker_receipts_are_atomic_and_scoped(tmp_path):
repaired = apply(6, 'transcript.append', {'messages': [{'role': 'assistant',
'content': 'loser', '_row_id': winner['annotations'][0]['_row_id']}]})
assert repaired['count'] == 0
- assert repaired['annotations'][0]['_canonical_content'] == assistant['content']
+ adopted = repaired['annotations'][0]
+ assert adopted[CANONICAL_ROW]['content'] == assistant['content']
+ assert adopted[DB_ROW_SNAPSHOT] and adopted['_row_id'] == winner['annotations'][0]['_row_id']
terminal = apply(7, 'execution.finish', {})
assert terminal['status'] == 'terminal'
assert apply(7, 'execution.finish', {}) == terminal
diff --git a/tests/tools/test_bot_delivery_install_launcher.py b/tests/tools/test_bot_delivery_install_launcher.py
index ad3516d4ea9..33cd716d69e 100644
--- a/tests/tools/test_bot_delivery_install_launcher.py
+++ b/tests/tools/test_bot_delivery_install_launcher.py
@@ -7,6 +7,12 @@ import pytest
from tools import bot_relay
+def _delivery_argv(profile, query_file):
+ # Bot-chat delivery runs over the gateway here; the DM path (tools/bot_mode_dm.py) still spawns
+ # ``_hermes_cli() -p <profile> ...``, so the launcher it picks is the invariant under test.
+ return [bot_relay._hermes_cli(), "-p", profile, "--query-file", query_file]
+
+
@pytest.fixture
def launchers(tmp_path, monkeypatch):
root = tmp_path / "source install"
@@ -28,23 +34,21 @@ def launchers(tmp_path, monkeypatch):
def test_delivery_prefers_install_launcher_over_old_generation(launchers):
published, sibling = launchers
published.touch()
- argv = bot_relay.local_delivery_command("researcher", "message with spaces.txt")
+ argv = _delivery_argv("researcher", "message with spaces.txt")
assert argv[0] == str(published)
assert str(sibling) not in argv
- assert argv[1:] == ["-p", "researcher", *bot_relay.BOT_CHAT_TURN_ARGS,
- "--query-file", "message with spaces.txt"]
def test_unpublished_install_retains_interpreter_sibling(launchers):
_, sibling = launchers
- assert bot_relay.local_delivery_command("default", "body.txt")[0] == str(sibling)
+ assert _delivery_argv("default", "body.txt")[0] == str(sibling)
@pytest.mark.platforms("windows")
def test_windows_delivery_does_not_select_batch_shims(launchers):
published, sibling = launchers
published.with_suffix(".cmd").touch()
- assert bot_relay.local_delivery_command("default", "body.txt")[0] == str(sibling)
+ assert _delivery_argv("default", "body.txt")[0] == str(sibling)
def test_path_then_bare_fallback_remain_available(launchers, monkeypatch):
@@ -106,7 +110,7 @@ def test_real_delivery_launcher_imports_new_generation(tmp_path, monkeypatch):
assert _launchers.mint_launcher("hermes", old_package.parent, old_bin, real_python, None)
monkeypatch.setattr(bot_relay, "__file__", str(root / "tools" / "bot_relay.py"))
monkeypatch.setattr(sys, "executable", str(old_bin / "python.exe"))
- argv = bot_relay.local_delivery_command("researcher", str(tmp_path / "message&extra.txt"))
+ argv = _delivery_argv("researcher", str(tmp_path / "message&extra.txt"))
for generation in ("first", "new-plugin"):
selected = record.parent / "environments" / generation / "venv"
packages = site_packages(selected)