Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions loopx/capabilities/native_chat/codex_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import subprocess
import threading
import time
from typing import Any, Callable, Mapping, TYPE_CHECKING
from typing import Any, Callable, Iterator, Mapping, TYPE_CHECKING
from urllib.parse import urlsplit

from ...chat_agent import (
Expand Down Expand Up @@ -865,6 +865,29 @@ def inventory():
except (RuntimeError, ValueError, OSError):
return False

@staticmethod
@contextmanager
def _persisted_thread_observer(
*, codex_bin: str, codex_home: Path, work_dir: Path | None = None,
) -> Iterator[CodexChatAgentSession]:
"""Initialize a stdio client for persisted item reads."""
from ...chat_agent import _reader

process = subprocess.Popen([codex_bin, "app-server", "--listen", "stdio://"],
cwd=str(codex_home), env={**os.environ, "CODEX_HOME": str(codex_home)},
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
text=True, encoding="utf-8", bufsize=1)
native = CodexChatAgentSession(process=process, messages=queue.Queue(), thread_id="",
work_dir=work_dir or codex_home, response_timeout_sec=5)
try:
threading.Thread(target=_reader, args=(process.stdout, native.messages), daemon=True).start()
native._request("initialize", {"clientInfo": {"name": "loopx_chat", "version": "0.1.0"},
"capabilities": {"experimentalApi": True}})
native._notify("initialized", {})
yield native
finally:
native.close()

@staticmethod
def failed_request_observation(*, codex_bin: str, codex_home: Path,
thread_ids: list[str], turn_id: str,
Expand All @@ -882,23 +905,13 @@ def failed_request_observation(*, codex_bin: str, codex_home: Path,
uses the same native dispatcher as authentication/control and never
calls thread/start, resume, inject or turn/start.
"""
from ...chat_agent import _reader

if completed_result_review and not read_only_review:
raise ValueError("a completed result observation permits only read-only review")
if allow_untyped_result_review and not (completed_result_review and read_only_review):
raise ValueError("untyped result evidence permits only a completed read-only review")
process = subprocess.Popen([codex_bin, "app-server", "--listen", "stdio://"],
cwd=str(codex_home), env={**os.environ, "CODEX_HOME": str(codex_home)},
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
text=True, encoding="utf-8", bufsize=1)
native = CodexChatAgentSession(process=process, messages=queue.Queue(), thread_id="",
work_dir=work_dir or codex_home, response_timeout_sec=5)
try:
threading.Thread(target=_reader, args=(process.stdout, native.messages), daemon=True).start()
native._request("initialize", {"clientInfo": {"name": "loopx_chat", "version": "0.1.0"},
"capabilities": {"experimentalApi": True}})
native._notify("initialized", {})
with CodexAppServerAdapter._persisted_thread_observer(
codex_bin=codex_bin, codex_home=codex_home, work_dir=work_dir,
) as native:
deadline = time.monotonic() + 5
for thread_id in dict.fromkeys(thread_ids):
native.thread_id = thread_id
Expand Down Expand Up @@ -944,8 +957,6 @@ def failed_request_observation(*, codex_bin: str, codex_home: Path,
if time.monotonic() >= deadline:
break
return None
finally:
native.close()

@property
def current_model_id(self) -> str | None:
Expand Down Expand Up @@ -1144,6 +1155,13 @@ def steering_delivery_readback(self, turn_id: str) -> list[dict[str, Any]]:
return list(self.session._control_items("thread/items/list", {"turnId": turn_id},
time.monotonic() + min(self.session.response_timeout_sec, 5.0)))

@staticmethod
def persisted_steering_delivery_readback(*, codex_bin: str, codex_home: Path,
thread_id: str, turn_id: str) -> list[dict[str, Any]]:
with CodexAppServerAdapter._persisted_thread_observer(codex_bin=codex_bin, codex_home=codex_home) as native:
native.thread_id = thread_id
return CodexAppServerAdapter(native).steering_delivery_readback(turn_id)

def start_turn_with_attachments(
self,
message: str,
Expand Down
39 changes: 31 additions & 8 deletions loopx/capabilities/native_chat/queued_admission.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ...chat_attachments import require_text_document_route
from ...attached_session import enqueue_attached_agent_turn
from ...chat_store import CHAT_SESSION_MODE_ATTACHED
from ...chat_store import CHAT_SESSION_MODE_ATTACHED, CHAT_SESSION_MODE_MANAGED
from ...control_plane.collaboration import conversation_scope
from . import project_context as project_context_policy

Expand Down Expand Up @@ -290,20 +290,43 @@ def reconcile_steering_delivery(controller, session, receipt) -> dict[str, Any]
from .codex_adapter import CodexAppServerAdapter
from ...control_plane.effect_runtime import effect_runtime_result

adapter = controller.adapters.get(session["session_id"])
if not isinstance(adapter, CodexAppServerAdapter) or not adapter.healthcheck():
return None
capture = receipt.get("native_steering")
if not isinstance(capture, dict) or not isinstance(capture.get("local_turn_id"), str):
return None
turn = controller.store.load_turn(session["session_id"], capture["local_turn_id"])
facts = {"receipt": receipt, "session_thread_id": session.get("upstream_thread_id"),
"adapter_thread_id": adapter.upstream_thread_id, "turn": turn or {}}
session_id = session["session_id"]
turn = controller.store.load_turn(session_id, capture["local_turn_id"])
adapter = controller.adapters.get(session_id)
live = isinstance(adapter, CodexAppServerAdapter) and adapter.healthcheck()
session_thread_id = session.get("upstream_thread_id")
facts = {"receipt": receipt, "session_thread_id": session_thread_id,
"adapter_thread_id": adapter.upstream_thread_id if live else session_thread_id, "turn": turn or {}}
try:
decision = effect_runtime_result("chat.queue.steering_readback", facts)
if decision["action"] != "read":
return None
facts["items"] = adapter.steering_delivery_readback(decision["turn_id"])
if live:
facts["items"] = adapter.steering_delivery_readback(decision["turn_id"])
else:
# The original managed host store remains authoritative after restart.
if (session.get("session_mode") != CHAT_SESSION_MODE_MANAGED or session.get("agent_id") != "codex"
or session.get("project_context") is not None or not session.get("codex_home")):
return None
from .codex_context import checked_session_home

home = checked_session_home(controller.codex_home, session)
if str(home) != session["codex_home"]:
return None
facts["items"] = CodexAppServerAdapter.persisted_steering_delivery_readback(
codex_bin=controller.codex_bin, codex_home=home,
thread_id=session_thread_id, turn_id=decision["turn_id"],
)
fresh = controller.store.load_session(session_id)
if (fresh is None or fresh.get("status") == "closed"
or any(fresh.get(key) != session.get(key) for key in (
"session_mode", "agent_id", "codex_home", "upstream_thread_id",
"channel_id", "goal_id", "steward_context", "project_context", "manager_authorization_scope_id",
))):
return None
facts["inventory_complete"] = True
if effect_runtime_result("chat.queue.steering_readback", facts)["action"] != "confirmed":
return None
Expand Down
97 changes: 97 additions & 0 deletions tests/test_lark_private_conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7072,6 +7072,103 @@ def request(method, path, body=None):
fixture.close()


def test_private_steward_app_recovers_queued_steering_after_restart(tmp_path, monkeypatch):
from pathlib import Path

from loopx.capabilities.native_chat.codex_adapter import CodexAppServerAdapter
from loopx.capabilities.native_chat.queued_admission import steer_queued_web_turn
from loopx.chat_runtime import ChatRuntimeController
from loopx.chat_store import ChatSessionStore
from test_native_steward_private import steward

fixture = steward.__wrapped__(tmp_path)
store, runtime, provider, transport, _, capture, _ = next(fixture)
observer = None
try:
runtime.codex_home = tmp_path / "original-codex-home"
runtime.codex_home.mkdir()
inventory = tmp_path / "thread-items.json"
inventory.write_text("[]")
fake = Path(runtime.codex_bin)
source = fake.read_text()
original_inventory = ' elif method in {"thread/items/list", "thread/backgroundTerminals/list"}:'
assert original_inventory in source
fake.write_text(source.replace(original_inventory, f''' elif method == "thread/items/list":
with open({str(inventory)!r}) as file:
result = {{"data": json.load(file)}}
elif method == "thread/backgroundTerminals/list":'''))

event = provider.event("steward-app", "cold-original", "wait for steer")
assert transport.admit("steward-app", event)["status"] == "durably_accepted"
original, = transport.core.pending()
_wait_private_upstream(store, original)
sid, tid = original["session_id"], original["turn_id"]
message = "Follow the correction on this Turn."
pending, _ = runtime.enqueue_turn(session_id=sid, client_turn_id="cold-queued", message=message,
work_dir=None, objective="", origin="web")
qid = pending["turn_id"]
attempt = dict(session_id=sid, queued_turn_id=qid, expected_turn_id=tid,
client_ingress_id="cold-ack")
original_steer = CodexAppServerAdapter.steer_turn

def lose_response(adapter, *args):
original_steer(adapter, *args)
raise RuntimeError("response lost after accepting the correction")

monkeypatch.setattr(CodexAppServerAdapter, "steer_turn", lose_response)
with pytest.raises(RuntimeError, match="live_steering_delivery_unresolved"):
steer_queued_web_turn(runtime, **attempt)
completed = runtime.wait_for_turn(session_id=sid, turn_id=tid, timeout_sec=10)
assert completed["status"] == "completed"
held = store.load_turn(sid, qid)
assert held["_queue_steering"]["status"] == "pending"
runtime.adapters.pop(sid).close_session()
observer = ChatRuntimeController(
store=ChatSessionStore(store.root.parent, startup_maintenance=False),
codex_bin=runtime.codex_bin, project_contexts=runtime.project_contexts,
registry_path=runtime.registry_path,
)
observer.codex_home = runtime.codex_home
observer.manager_scope_resolver = runtime.manager_scope_resolver
calls = [json.loads(line) for line in capture.read_text().splitlines()]
sent, = [call["params"] for call in calls if call["method"] == "turn/steer"]
for wrong in ([], [{"turnId": completed["upstream_turn_id"], "item": {
"type": "userMessage", "clientId": sent["clientUserMessageId"],
"content": [{"type": "text", "text": "Different text"}],
}}]):
inventory.write_text(json.dumps(wrong))
with pytest.raises(RuntimeError, match="live_steering_delivery_unresolved"):
steer_queued_web_turn(observer, **attempt)
assert store.load_turn(sid, qid) == held
assert store.claim_next_queued_turn(sid) is None
inventory.write_text(json.dumps([{"turnId": completed["upstream_turn_id"], "item": {
"type": "userMessage", "clientId": sent["clientUserMessageId"], "content": sent["input"],
}}]))
observer.codex_home = tmp_path / "different-codex-home"
with pytest.raises(RuntimeError, match="live_steering_delivery_unresolved"):
steer_queued_web_turn(observer, **attempt)
assert store.load_turn(sid, qid) == held
observer.codex_home = runtime.codex_home
delivered = steer_queued_web_turn(observer, **attempt)
assert delivered["status"] == "delivered" and delivered["turn_id"] == tid
assert delivered["queued_turn_id"] == qid
assert steer_queued_web_turn(observer, **attempt) == delivered
assert not observer.adapters and not store.claim_next_queued_turn(sid)
assert store.load_turn(sid, qid)["status"] == "completed"
after = [json.loads(line) for line in capture.read_text().splitlines()]
observed = [call["params"] for call in after if call["method"] == "thread/items/list"]
assert len(observed) == 3 and all(call["threadId"] == sent["threadId"]
and call["turnId"] == sent["expectedTurnId"] for call in observed)
assert [call for call in after if call["method"] in {"thread/start", "thread/resume", "thread/fork",
"turn/start", "turn/steer"}] == [call for call in calls if call["method"] in {
"thread/start", "thread/resume", "thread/fork", "turn/start", "turn/steer"}]
assert sum(row["kind"] == "turn.steered" for row in store.events_after(sid, tid, None)) == 1
finally:
if observer is not None:
observer.close()
fixture.close()


def test_private_steward_native_mention_uses_original_manager_turn(tmp_path, monkeypatch):
from test_native_steward_private import steward

Expand Down
Loading