Skip to content

The agent that grows with you

fix: fence ACP controls and expose the read-only catalog

Tekniumd304b025fca2
diff --git a/acp_adapter/catalog.py b/acp_adapter/catalog.py
new file mode 100644
index 00000000000..bfdc82213e6
--- /dev/null
+++ b/acp_adapter/catalog.py
@@ -0,0 +1,28 @@
+"""ACP historical catalog without storage initialization or an execution owner."""
+from pathlib import Path
+
+from hermes_state import SessionDB
+from acp_adapter.session import (
+ _normalize_cwd_for_compare, _parse_model_config, _session_info, _updated_at_sort_key,
+)
+
+
+def read_catalog_rows(path):
+ path = Path(path)
+ if not path.exists():
+ return {}
+ with SessionDB(db_path=path, read_only=True) as db:
+ return {str(row["id"]): dict(row) for row in db.list_sessions_rich(source="acp", limit=1000)}
+
+
+def catalog_sessions(path, cwd=None):
+ normalized = _normalize_cwd_for_compare(cwd) if cwd else None
+ results = []
+ for sid, row in read_catalog_rows(path).items():
+ count = int(row.get("message_count") or 0)
+ session_cwd = _parse_model_config(row.get("model_config")).get("cwd", ".")
+ if count <= 0 or (normalized and _normalize_cwd_for_compare(session_cwd) != normalized):
+ continue
+ results.append(_session_info(sid, session_cwd, row.get("model") or "", count, row.get("title"),
+ row.get("preview"), row.get("last_active") or row.get("started_at")))
+ return sorted(results, key=lambda row: _updated_at_sort_key(row.get("updated_at")), reverse=True)
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index 4d41bc02a5c..fdf72b4e707 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -108,6 +108,37 @@ class GatewayACPAgent(acp.Agent):
await self._resume(cwd, session_id, mcp_servers)
return ResumeSessionResponse()
+ async def cancel(self, session_id, **kwargs):
+ snapshot = self._snapshots.get(session_id)
+ if snapshot is None:
+ raise GatewayClientError("not_found")
+ client = await self._client()
+ await client.rpc("session.interrupt", session_id=session_id,
+ execution_generation=snapshot["execution_generation"])
+
+ async def fork_session(self, cwd, session_id, mcp_servers=None, **kwargs):
+ raise GatewayClientError("acp_fork_mutation_unavailable")
+
+ async def set_session_model(self, model_id, session_id, **kwargs):
+ raise GatewayClientError("acp_model_mutation_unavailable")
+
+ async def set_session_mode(self, mode_id, session_id, **kwargs):
+ raise GatewayClientError("acp_edit_policy_mutation_unavailable")
+
+ async def set_config_option(self, config_id, session_id, **kwargs):
+ raise GatewayClientError("acp_config_mutation_unavailable")
+
+ async def list_sessions(self, cursor=None, cwd=None, **kwargs):
+ from acp.schema import ListSessionsResponse, SessionInfo
+ from acp_adapter.catalog import catalog_sessions
+ rows = await asyncio.to_thread(catalog_sessions, self._home / "state.db", cwd)
+ if cursor:
+ index = next((i for i, row in enumerate(rows) if row["session_id"] == cursor), None)
+ rows = [] if index is None else rows[index + 1:]
+ page = [SessionInfo(session_id=row["session_id"], cwd=row["cwd"], title=row.get("title"),
+ updated_at=row.get("updated_at")) for row in rows[:50]]
+ return ListSessionsResponse(sessions=page, next_cursor=page[-1].session_id if len(rows) > 50 else None)
+
async def prompt(self, prompt, session_id, **kwargs):
if session_id not in self._snapshots:
raise GatewayClientError("not_found")
diff --git a/acp_adapter/session.py b/acp_adapter/session.py
index a6bd31c6d91..4853dd8e350 100644
--- a/acp_adapter/session.py
+++ b/acp_adapter/session.py
@@ -227,16 +227,11 @@ class SessionManager:
return results
def _catalog_rows(self) -> dict[str, dict[str, Any]]:
- from hermes_state import SessionDB
+ from acp_adapter.catalog import read_catalog_rows
from pathlib import Path
path = Path(self._db_instance.db_path) if self._db_instance is not None else get_hermes_home() / "state.db"
- if not path.exists():
- return {}
- # A separate read-only connection must not initialize schemas, switch
- # journal mode, or start a token writer merely to render the editor picker.
- with SessionDB(db_path=path, read_only=True) as db:
- return {str(row["id"]): dict(row) for row in db.list_sessions_rich(source="acp", limit=1000)}
+ return read_catalog_rows(path)
def update_cwd(self, session_id: str, cwd: str) -> Optional[SessionState]:
"""Update the working directory for a session and its tool overrides."""
diff --git a/tests/acp/test_gateway_controls.py b/tests/acp/test_gateway_controls.py
new file mode 100644
index 00000000000..a2e9c55a06a
--- /dev/null
+++ b/tests/acp/test_gateway_controls.py
@@ -0,0 +1,42 @@
+"""ACP controls retain generation fences and reject unavailable mutations."""
+import pytest
+
+from acp_adapter.server import HermesACPAgent
+from hermes_cli.gateway_client import GatewayClientError
+from hermes_state import SessionDB
+
+
+async def test_cancel_uses_observed_generation_and_mutations_fail_explicitly(tmp_path):
+ agent = HermesACPAgent()
+ calls = []
+
+ class RPC:
+ async def rpc(self, method, **params):
+ calls.append((method, params))
+ raise GatewayClientError("stale_generation")
+
+ agent._gateway = RPC()
+ agent._snapshots["shared"] = {"execution_generation": 4, "revision": 7}
+ with pytest.raises(GatewayClientError, match="stale_generation"):
+ await agent.cancel("shared")
+ assert calls == [("session.interrupt", {"session_id": "shared", "execution_generation": 4})]
+ for method, params in ((agent.fork_session, {"session_id": "shared", "cwd": str(tmp_path)}),
+ (agent.set_session_model, {"session_id": "shared", "model_id": "new-model"}),
+ (agent.set_session_mode, {"session_id": "shared", "mode_id": "dont_ask"})):
+ with pytest.raises(GatewayClientError, match="unavailable"):
+ await method(**params)
+ assert len(calls) == 1, "Unsupported operations must not reach an alternate writer"
+
+
+async def test_gateway_acp_catalog_does_not_boot_a_runtime(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ agent = HermesACPAgent()
+ assert (await agent.list_sessions()).sessions == []
+ assert not (tmp_path / "state.db").exists()
+ with SessionDB(tmp_path / "state.db") as db:
+ db.create_session(session_id="persisted-acp", source="acp", model="fixture", model_config={"cwd": str(tmp_path)})
+ db.append_message("persisted-acp", "user", "picker history")
+ assert [row.session_id for row in (await agent.list_sessions(cwd=str(tmp_path))).sessions] == ["persisted-acp"]
+ assert agent._gateway is None