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
25 changes: 6 additions & 19 deletions loopx/chat_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import json
import os
from pathlib import Path
import re
import threading
from typing import Any, Iterator
import uuid
Expand All @@ -37,12 +36,16 @@
AcceptedManagedTurn,
plan_managed_turn_acceptance,
)
from .control_plane.collaboration.source_chat_observation import (
CHAT_SESSION_SCHEMA_VERSION,
CHAT_TURN_SCHEMA_VERSION,
_opaque_id,
_read_json,
)
from .file_lock import exclusive_file_lock, try_exclusive_file_lock, write_json_atomic


CHAT_STORE_SCHEMA_VERSION = "loopx_chat_store_v1"
CHAT_SESSION_SCHEMA_VERSION = "loopx_chat_session_state_v1"
CHAT_TURN_SCHEMA_VERSION = "loopx_chat_turn_state_v1"
CHAT_EVENT_SCHEMA_VERSION = "loopx_chat_event_v1"
CHAT_MESSAGE_SCHEMA_VERSION = "loopx_chat_message_v1"
CHAT_INGRESS_SCHEMA_VERSION = "loopx_chat_ingress_receipt_v1"
Expand All @@ -53,7 +56,6 @@
TERMINAL_EVENT_KINDS = {"turn.completed", "turn.failed", "turn.interrupted"}
REPLAY_ONLY_EVENT_KINDS = {"answer.delta", "assistant.delta", "agent.phase", "turn.activity"}

_OPAQUE_ID = re.compile(r"^[A-Za-z0-9._-]{1,160}$")
_SESSION_RELEASE_UNSET = object()

# Keep both active conversation journals warm within the original total budget.
Expand All @@ -73,28 +75,13 @@ def _instant(value: Any) -> datetime | None:
return None


def _opaque_id(value: Any, *, field: str) -> str:
token = str(value or "").strip()
if not _OPAQUE_ID.fullmatch(token):
raise ValueError(f"{field} must be a compact opaque id")
return token


def _upstream_id(value: Any) -> str:
token = str(value or "").strip()
if not token or len(token) > 1024 or any(ord(character) < 32 for character in token):
raise ValueError("upstream_thread_id must be a bounded opaque string")
return token


def _read_json(path: Path) -> dict[str, Any]:
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
return {}
return payload if isinstance(payload, dict) else {}


def _read_jsonl(
path: Path, *, raise_on_error: bool = False, strict: bool = False,
) -> list[dict[str, Any]]:
Expand Down
31 changes: 22 additions & 9 deletions loopx/control_plane/collaboration/source_chat_observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,35 @@
from __future__ import annotations

from collections.abc import Callable
import json
from pathlib import Path
import re
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from ...chat_store import ChatSessionStore


CHAT_SESSION_SCHEMA_VERSION = "loopx_chat_session_state_v1"
CHAT_TURN_SCHEMA_VERSION = "loopx_chat_turn_state_v1"
_OPAQUE_ID = re.compile(r"^[A-Za-z0-9._-]{1,160}$")


def _opaque_id(value: Any, *, field: str) -> str:
token = str(value or "").strip()
if not _OPAQUE_ID.fullmatch(token):
raise ValueError(f"{field} must be a compact opaque id")
return token


def _read_json(path: Path) -> dict[str, Any]:
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
return {}
return payload if isinstance(payload, dict) else {}


def _source_chat_root(coordination_root: Path, route: dict[str, Any]) -> Path:
locator = route.get("source_chat_runtime_root", str(Path(coordination_root).resolve()))
if not isinstance(locator, str) or not locator or not Path(locator).is_absolute():
Expand All @@ -32,8 +54,6 @@ def original_turn_reader(store: ChatSessionStore, session_id: str) -> Callable[[
Keep only trusted store paths, not Turn contents or authorization. The
caller discards this locator after its read, including between pages.
"""
from ...chat_store import _read_json

turns_dir = store._session_dir(session_id) / "turns"
paths: dict[str, Path] = {}
for path in turns_dir.glob("*.json") if turns_dir.is_dir() else []:
Expand All @@ -54,13 +74,6 @@ def read(client_turn_id: str) -> dict[str, Any] | None:


def observe_source_chat(coordination_root: Path, route: dict[str, Any]) -> tuple[dict[str, Any], dict[str, Any]]:
from ...chat_store import (
CHAT_SESSION_SCHEMA_VERSION,
CHAT_TURN_SCHEMA_VERSION,
_opaque_id,
_read_json,
)

source_root = _source_chat_root(coordination_root, route)
session_id = _opaque_id(route.get("session_id"), field="session_id")
client_turn_id = _opaque_id(route.get("client_turn_id"), field="client_turn_id")
Expand Down
3 changes: 3 additions & 0 deletions packages/loopx-core/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@


def _workspace_source(path: Path) -> bool:
# Core owns typed action receipts, including operation handoffs read by its Inbox.
if path.as_posix() == "loopx/chat_action_store.py":
return False
return (
path.as_posix() in WORKSPACE_MODULES
or path.is_relative_to("loopx/chat")
Expand Down
33 changes: 19 additions & 14 deletions tests/control_plane/test_installed_core_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,37 @@ def command(*args):
assert (workspace / "host-invocations").read_text().splitlines() == ["called", "called"]


def test_installed_empty_operation_inbox_keeps_the_workspace_provider_unloaded(core_python, tmp_path):
"""No action store means no operation handoff; a present store needs its provider."""
def test_installed_operation_inbox_reads_core_store_only_when_present(core_python, tmp_path):
"""Keep the optional path lazy; a persisted store remains readable without Workspace."""
script = '''
import importlib.util, json, sys
import importlib.util, sys
from pathlib import Path
assert importlib.util.find_spec("loopx.chat_action_store") is None
assert importlib.util.find_spec("loopx.chat_store") is None
from loopx.control_plane.collaboration.operation_handoff import pending_operation_handoffs
root = Path(sys.argv[1])
empty = pending_operation_handoffs(root, "synthetic-goal", "synthetic-agent", cursor_scope="synthetic-scope")
assert empty == {"items": [], "pending_count": 0, "overflow": None, "next_cursor": None}
expected = {"items": [], "pending_count": 0, "overflow": None, "next_cursor": None}

def read():
return pending_operation_handoffs(root, "synthetic-goal", "synthetic-agent", cursor_scope="synthetic-scope")

assert read() == expected
assert "loopx.chat_action_store" not in sys.modules
store = root / "chat/actions/actions.json"
store.parent.mkdir(parents=True)
from loopx.chat_action_store import ChatActionStore
store = ChatActionStore(root / "chat" / "actions").path
before = store.read_bytes()
assert read() == expected and store.read_bytes() == before
store.write_text("{}")
try:
pending_operation_handoffs(root, "synthetic-goal", "synthetic-agent", cursor_scope="synthetic-scope")
except ModuleNotFoundError as error:
assert error.name == "loopx.chat_action_store"
read()
except ValueError as error:
assert "unsupported schema" in str(error)
else:
raise AssertionError("a present operation store cannot silently disappear without its provider")
print(json.dumps({"empty": empty, "present_store_requires_provider": True}))
raise AssertionError("a malformed operation store cannot silently disappear")
assert store.read_text() == "{}"
'''
result = subprocess.run([str(core_python), "-I", "-c", script, str(tmp_path / "runtime")],
env=_environment(core_python), cwd=tmp_path, capture_output=True, text=True, timeout=45)
assert result.returncode == 0, (result.stdout, result.stderr)
assert json.loads(result.stdout)["present_store_requires_provider"] is True


def test_installed_context_recovers_partial_delivery_revisions_and_compaction(core_python, tmp_path):
Expand Down
2 changes: 1 addition & 1 deletion tests/control_plane/test_peer_source_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import pytest

from canonical_authority_fixture import isolate_sqlite_runtime, promoted_create_fixture
from tests.control_plane.canonical_authority_fixture import isolate_sqlite_runtime, promoted_create_fixture
from loopx.capabilities.manager_context import POLICY_SCHEMA, _hash, _root, _write, register_ingress
from loopx.chat.coordination import apply_context_handoff
from loopx.chat_store import ChatSessionStore
Expand Down
5 changes: 3 additions & 2 deletions tests/test_core_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def test_core_staging_excludes_workspace_and_reuses_kernel_bytes(tmp_path):
staged = _prepare_module().prepare_core_source(tmp_path / "source")
for name in ("chat", "web", "canary", "cli.py", "chat.py"):
assert not (staged / "loopx" / name).exists()
assert not list((staged / "loopx").glob("chat_*.py"))
for name in ("entrypoint.py", "control_plane/turn_driver/executor.py",
# Chat UI/store modules are excluded; the typed action authority is Core-owned.
assert {path.name for path in (staged / "loopx").glob("chat_*.py")} == {"chat_action_store.py"}
for name in ("entrypoint.py", "chat_action_store.py", "control_plane/turn_driver/executor.py",
"control_plane/coordination/sqlite_authority_store.ts",
"extensions/lark/extension.toml"):
assert (staged / "loopx" / name).read_bytes() == (REPOSITORY / "loopx" / name).read_bytes()
Expand Down
61 changes: 61 additions & 0 deletions tests/test_core_workspace_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import pytest

from tests.control_plane.codex_checkpoint_fixture import reference_metadata_oracle, read_cli_policy_payload
from tests.control_plane.canonical_authority_fixture import isolate_sqlite_runtime
from tests.control_plane.test_peer_source_store import source_request
from tests.test_core_distribution import AGENT, GOAL, _assert_committed, _environment, _run
from tests.test_core_distribution import task as task

Expand Down Expand Up @@ -54,6 +56,8 @@ def test_workspace_wheel_owns_the_complete_bundle_without_core_files(wheels):
assert all(sha256(workspace_zip.read(prefix + name)).hexdigest() == digest
for name, digest in manifest["files"].items())
assert "loopx/chat_server.py" in workspace_zip.namelist()
assert "loopx/chat_store.py" not in core_zip.namelist()
assert "loopx/chat_action_store.py" in core_zip.namelist()
assert "loopx/dashboard_launcher.py" in workspace_zip.namelist()
assert {
"loopx/capabilities/native_chat/steward_turn.py",
Expand All @@ -74,6 +78,11 @@ def test_core_requests_and_returns_peer_work_without_workspace(core_python, task
workspace, runtime, registry, _ = task
route = ["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json"]
source = "source-fixture"
# Core must still read the typed store left by a previously installed Workspace.
from loopx.chat_action_store import ChatActionStore

actions = ChatActionStore(runtime / "chat" / "actions").path
original_actions = actions.read_bytes()
_run(core_python, [*route, "configure-goal", "--goal-id", GOAL, "--registered-agent", AGENT,
"--registered-agent", source, "--execute"], workspace)
brief = workspace / "brief.json"
Expand Down Expand Up @@ -119,6 +128,58 @@ def inbox(action, agent, *args):
else:
row = inbox("read", agent)["peer_returns"]["items"][0]
assert row["request_id"] == request_id and row["text"] == "The checked total is 35."
assert actions.read_bytes() == original_actions


@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_core_forwards_source_backed_peer_context_without_workspace(core_python, tmp_path, monkeypatch, provider):
isolate_sqlite_runtime(tmp_path, monkeypatch)
runtime, registry, store, session, turn, receipt = source_request(tmp_path, monkeypatch, provider=provider)
route = ["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json"]
brief = tmp_path / "forwarding-brief.json"
brief.write_text(json.dumps({
"schema_version": "collaboration_brief_v0",
"purpose": "Check the exact source",
"context": "Preserve the original correction.",
"constraints": ["No work launch"],
"inputs": [],
"acceptance": ["Return evidence and unresolved gaps"],
"return_requirement": "Return to the requester.",
}), encoding="utf-8")
source_session = store._session_dir(session["session_id"])

def source_records():
return {path.relative_to(source_session): path.read_bytes()
for path in source_session.rglob("*.json")}

def inbox(action, agent, *args, expected=0):
out = _run(core_python, [*route, "manager-inbox", action, "--goal-id", "goal-a",
"--agent-id", agent, *args], tmp_path, expected=expected)
return json.loads(out.stdout)

def forward(agent, *, expected=0):
return inbox("request", "agent-a", "--peer-agent-id", agent,
"--parent-request-id", receipt["request_id"], "--operation-id", f"review-{agent}",
"--brief-file", str(brief), expected=expected)

before = source_records()
assert not _python_json(core_python, tmp_path, """
import importlib.util, json
print(json.dumps(importlib.util.find_spec("loopx.chat_store") is not None))
""")
assert inbox("read", "agent-a")["items"][0]["request_id"] == receipt["request_id"]
# The third agent is registered to the same Goal but outside the original audience grant.
denied = forward("third", expected=1)
assert "not authorized" in str(denied)
assert inbox("read", "third")["items"] == []
child = forward("reviewer")
received = inbox("read", "reviewer")["items"]
assert len(received) == 1
assert received[0]["request_id"] == child["request_id"]
assert received[0]["parent_request_id"] == receipt["request_id"]
assert received[0]["inherited_context"]["message"] == turn["message"]
assert source_records() == before
assert not (runtime / "chat").exists()


def _python_json(python, cwd, source):
Expand Down
Loading