Skip to content

The agent that grows with you

fix: give watch events stable owner-bound automation identities

Teknium135730d1bc3d
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index 389b0770224..7f53fb7d861 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -1026,6 +1026,8 @@ class GatewayNotificationsMixin:
task_idx = ((evt.get("results") or [{}])[0] or {}).get("task_index", "")
return (evt_type, producer_id, f"task_failure:{task_idx}")
return (evt_type, producer_id, "")
+ if evt_type in {"watch_match", "watch_disabled"} and evt.get("event_id"):
+ return (evt_type, str(evt["session_id"]), str(evt["event_id"]))
if evt_type == "completion":
producer_id = str(evt.get("session_id") or "")
started_at = evt.get("started_at")
diff --git a/tests/gateway/fixtures/automation_peer.py b/tests/gateway/fixtures/automation_peer.py
index 1fca1a56110..b11edabbef7 100644
--- a/tests/gateway/fixtures/automation_peer.py
+++ b/tests/gateway/fixtures/automation_peer.py
@@ -36,7 +36,8 @@ class Model(BaseHTTPRequestHandler):
command = shlex.join([sys.executable, '-c', code])
message = {'role': 'assistant', 'content': None, 'tool_calls': [{'id': 'terminal-spawn',
'type': 'function', 'function': {'name': 'terminal', 'arguments': json.dumps({
- 'command': command, 'background': True, 'notify_on_complete': True, 'check_interval': 1})}}]}
+ 'command': command, 'background': True, 'notify_on_complete': not getattr(self.server, 'watch', False),
+ **({'watch_patterns': ['REAL_TERMINAL_COMPLETION']} if getattr(self.server, 'watch', False) else {})})}}]}
finish = 'tool_calls' if message.get('tool_calls') else 'stop'
payload = json.dumps({'id': 'local', 'choices': [{'index': 0, 'message': message, 'finish_reason': finish}],
'usage': {'prompt_tokens': 10, 'completion_tokens': 5, 'total_tokens': 15}}).encode()
diff --git a/tests/gateway/fixtures/local_automation_peer.py b/tests/gateway/fixtures/local_automation_peer.py
index c07ff5e1f96..d50b7340ef4 100644
--- a/tests/gateway/fixtures/local_automation_peer.py
+++ b/tests/gateway/fixtures/local_automation_peer.py
@@ -14,12 +14,14 @@ from local_recovery_probe import daemon, websocket, rpc
def probe(base, source):
restart = source == 'restart'
- source = 'gui' if restart else source
+ watch = source == 'watch'
+ source = 'gui' if restart or watch else source
root = Path(__file__).resolve().parents[3]
home, user = base / 'state', base / 'user'
home.mkdir(); user.mkdir()
model = ThreadingHTTPServer(('127.0.0.1', 0), Model)
model.requests = []
+ model.watch = watch
model.blocked, model.release = threading.Event(), threading.Event()
model.gate = home / 'terminal-gate'
threading.Thread(target=model.serve_forever, daemon=True).start()
@@ -70,6 +72,8 @@ def probe(base, source):
automatic = [r for r in ledger if r['principal_id'].startswith('automation:')]
assert automatic, {'missing_automation': ledger}
assert len(automatic) == 1 and automatic[0]['status'] == 'queued', automatic
+ if watch:
+ assert json.loads(automatic[0]['request_id'])[0] == 'watch_match', automatic
assert next(r for r in ledger if r['request_id'] == 'held')['status'] == 'started'
assert automatic[0]['seq'] > next(r for r in ledger if r['request_id'] == 'follower')['seq']
model.release.set()
diff --git a/tests/gateway/test_local_automation.py b/tests/gateway/test_local_automation.py
index cb7c2403f63..a5a8bc487e4 100644
--- a/tests/gateway/test_local_automation.py
+++ b/tests/gateway/test_local_automation.py
@@ -18,7 +18,7 @@ def _probe(tmp_path, source):
print(result.stdout)
[email protected]('source', ['cli', 'tui', 'gui'])
[email protected]('source', ['cli', 'tui', 'gui', 'watch'])
def test_local_completion_joins_fifo_without_an_observer(tmp_path, source):
_probe(tmp_path, source)
diff --git a/tests/gateway/test_watch_automation_identity.py b/tests/gateway/test_watch_automation_identity.py
new file mode 100644
index 00000000000..126a02d19d6
--- /dev/null
+++ b/tests/gateway/test_watch_automation_identity.py
@@ -0,0 +1,24 @@
+"""Distinct pattern hits must not collapse into one completion identity."""
+import json
+
+
+def test_watch_event_identity_survives_retry_but_not_another_hit():
+ from gateway.run import GatewayRunner
+ from tools.process_registry import ProcessRegistry, ProcessSession
+ registry = ProcessRegistry()
+ session = ProcessSession(id='proc-watch', command='owned', task_id='task',
+ session_key='agent:main:local:dm:owned', parent_session_id='parent')
+ session.watch_patterns = ['READY']
+ registry._check_watch_patterns(session, 'READY\n')
+ first = registry.completion_queue.get_nowait()
+ session._watch_cooldown_until = 0
+ registry._check_watch_patterns(session, 'READY\n')
+ second = registry.completion_queue.get_nowait()
+ registry._emit_watch_disabled(session, 0, 'test cap')
+ disabled = registry.completion_queue.get_nowait()
+ identity = GatewayRunner._completion_delivery_identity(first)
+ assert identity is not None
+ assert identity == GatewayRunner._completion_delivery_identity(json.loads(json.dumps(first)))
+ assert identity != GatewayRunner._completion_delivery_identity(second)
+ assert GatewayRunner._completion_delivery_identity(disabled) not in (None, identity)
+ assert all(e['parent_session_id'] == session.parent_session_id for e in (first, second, disabled))
diff --git a/tools/process_registry.py b/tools/process_registry.py
index f35027d8f2d..a58aa6ce099 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -605,6 +605,9 @@ class ProcessRegistry(ProcessCheckpointMixin):
def _watch_event_base(session: ProcessSession) -> dict:
"""Session identity + watcher routing fields shared by every watch event."""
return {
+ "event_id": uuid.uuid4().hex,
+ "started_at": session.started_at,
+ "parent_session_id": session.parent_session_id,
"session_id": session.id,
"session_key": session.session_key,
"task_id": session.task_id,