mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-11-16 09:21:50 +00:00
fix(codex): unify workspace thread ownership
This commit is contained in:
parent
25f9319c64
commit
0b2b328ea1
3 changed files with 80 additions and 26 deletions
|
|
@ -394,7 +394,7 @@ class CodexAgentWrapper(BaseAgentWrapper):
|
|||
**common,
|
||||
)
|
||||
if fork_session or not thread_id:
|
||||
self._record_thread_ownership(alias, thread.id)
|
||||
await self._record_thread_ownership(alias, thread.id)
|
||||
tool_contexts[thread.id] = requested_tool_context
|
||||
return thread
|
||||
|
||||
|
|
@ -426,32 +426,51 @@ class CodexAgentWrapper(BaseAgentWrapper):
|
|||
return None
|
||||
return workspace.mem_session_path / "codex" / "owned_threads.json"
|
||||
|
||||
def _owned_thread_ids(self, alias: str) -> set[str]:
|
||||
"""Load the persisted ids into the owning workspace's agent state."""
|
||||
state = self._agent_state(alias)
|
||||
if state is not None and state.owned_thread_ids is not None:
|
||||
return state.owned_thread_ids
|
||||
ids: set[str] = set()
|
||||
path = self._thread_ownership_path(alias)
|
||||
if path is not None and path.is_file():
|
||||
try:
|
||||
raw = json.loads(path.read_text(encoding="utf-8"))
|
||||
ids = {str(thread_id) for thread_id in raw.get("thread_ids", [])}
|
||||
except (OSError, ValueError, AttributeError) as exc:
|
||||
self.logger.warning(f"Ignoring unreadable Codex thread ownership file {path}: {exc}")
|
||||
if state is not None:
|
||||
state.owned_thread_ids = ids
|
||||
return ids
|
||||
def _codex_thread_state(self, alias: str):
|
||||
workspaces = _context_workspaces(self.app_context)
|
||||
if not workspaces:
|
||||
return None
|
||||
return workspaces[self._normalize_thread_alias(alias)].codex_thread_state
|
||||
|
||||
def _record_thread_ownership(self, alias: str, thread_id: str) -> None:
|
||||
async def _owned_thread_ids(self, alias: str) -> set[str]:
|
||||
"""Load ownership through the single authoritative state of each workspace."""
|
||||
state = self._codex_thread_state(alias)
|
||||
if state is None:
|
||||
return set()
|
||||
async with state.lock:
|
||||
if state.owned_thread_ids is not None:
|
||||
return state.owned_thread_ids
|
||||
ids: set[str] = set()
|
||||
path = self._thread_ownership_path(alias)
|
||||
if path is not None and path.is_file():
|
||||
try:
|
||||
raw = json.loads(path.read_text(encoding="utf-8"))
|
||||
ids = {str(thread_id) for thread_id in raw.get("thread_ids", [])}
|
||||
except (OSError, ValueError, AttributeError) as exc:
|
||||
self.logger.warning(f"Ignoring unreadable Codex thread ownership file {path}: {exc}")
|
||||
state.owned_thread_ids = ids
|
||||
return ids
|
||||
|
||||
async def _record_thread_ownership(self, alias: str, thread_id: str) -> None:
|
||||
alias = self._normalize_thread_alias(alias)
|
||||
state = self._codex_thread_state(alias)
|
||||
path = self._thread_ownership_path(alias)
|
||||
ids = self._owned_thread_ids(alias)
|
||||
if path is None or thread_id in ids:
|
||||
if state is None or path is None:
|
||||
return
|
||||
ids.add(thread_id)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(json.dumps({"thread_ids": sorted(ids)}, indent=1), encoding="utf-8")
|
||||
ids = await self._owned_thread_ids(alias)
|
||||
async with state.lock:
|
||||
if thread_id in ids:
|
||||
return
|
||||
updated = {*ids, thread_id}
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
fd, raw_path = tempfile.mkstemp(prefix=".owned_threads-", suffix=".tmp", dir=path.parent)
|
||||
try:
|
||||
with os.fdopen(fd, "w", encoding="utf-8") as stream:
|
||||
json.dump({"thread_ids": sorted(updated)}, stream, indent=1)
|
||||
os.replace(raw_path, path)
|
||||
finally:
|
||||
Path(raw_path).unlink(missing_ok=True)
|
||||
ids.add(thread_id)
|
||||
|
||||
async def _check_thread_ownership(self, thread_id: str, alias: str) -> None:
|
||||
"""Reject resume/fork/compact of a thread owned by another workspace.
|
||||
|
|
@ -464,7 +483,7 @@ class CodexAgentWrapper(BaseAgentWrapper):
|
|||
return
|
||||
alias = self._normalize_thread_alias(alias)
|
||||
for owner_alias in workspaces:
|
||||
if owner_alias != alias and thread_id in self._owned_thread_ids(owner_alias):
|
||||
if owner_alias != alias and thread_id in await self._owned_thread_ids(owner_alias):
|
||||
raise ValueError(
|
||||
f"Codex thread {thread_id} belongs to workspace {owner_alias!r}; "
|
||||
f"it cannot be used from workspace {alias!r}",
|
||||
|
|
|
|||
|
|
@ -14,7 +14,14 @@ class WorkspaceAgentState:
|
|||
"""Session execution state owned by one agent wrapper within one workspace."""
|
||||
|
||||
thread_tool_contexts: dict[str, str] = field(default_factory=dict)
|
||||
|
||||
|
||||
@dataclass
|
||||
class WorkspaceCodexThreadState:
|
||||
"""Authoritative Codex thread ownership shared by every wrapper in a workspace."""
|
||||
|
||||
owned_thread_ids: set[str] | None = None
|
||||
lock: asyncio.Lock = field(default_factory=asyncio.Lock)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -33,6 +40,7 @@ class WorkspaceContext:
|
|||
tool_contexts: dict[str, Any] = field(default_factory=dict)
|
||||
search_call_budgets: dict[str, Any] = field(default_factory=dict)
|
||||
agent_states: dict[str, WorkspaceAgentState] = field(default_factory=dict)
|
||||
codex_thread_state: WorkspaceCodexThreadState = field(default_factory=WorkspaceCodexThreadState)
|
||||
locks: dict[str, asyncio.Lock] = field(default_factory=dict)
|
||||
|
||||
def agent_state(self, wrapper_name: str) -> WorkspaceAgentState:
|
||||
|
|
|
|||
|
|
@ -162,8 +162,8 @@ async def test_codex_thread_tool_contexts_namespaced_per_workspace(tmp_path):
|
|||
assert set(alpha_state.thread_tool_contexts) == {"thread-1"}
|
||||
assert set(beta_state.thread_tool_contexts) == {"thread-2"}
|
||||
assert not wrapper._thread_tool_contexts
|
||||
assert alpha_state.owned_thread_ids == {"thread-1"}
|
||||
assert beta_state.owned_thread_ids == {"thread-2"}
|
||||
assert context.workspaces["alpha"].codex_thread_state.owned_thread_ids == {"thread-1"}
|
||||
assert context.workspaces["beta"].codex_thread_state.owned_thread_ids == {"thread-2"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
@ -203,6 +203,33 @@ async def test_codex_thread_ownership_persists_across_restart(tmp_path):
|
|||
await restarted._open_thread(codex, {"session_id": "thread-elsewhere", "workspace": "beta"})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_codex_thread_ownership_is_shared_and_atomic_across_wrappers(tmp_path):
|
||||
context = ApplicationContext(workspace_dir={"alpha": str(tmp_path / "alpha"), "beta": str(tmp_path / "beta")})
|
||||
wrapper_a = CodexAgentWrapper(name="codex-a", app_context=context)
|
||||
wrapper_b = CodexAgentWrapper(name="codex-b", app_context=context)
|
||||
|
||||
assert await wrapper_b._owned_thread_ids("alpha") == set()
|
||||
await wrapper_a._record_thread_ownership("alpha", "alpha-thread")
|
||||
with pytest.raises(ValueError, match="belongs to workspace 'alpha'"):
|
||||
await wrapper_b._check_thread_ownership("alpha-thread", "beta")
|
||||
|
||||
await wrapper_b._record_thread_ownership("alpha", "second-alpha-thread")
|
||||
ownership = tmp_path / "alpha" / "mem_session" / "codex" / "owned_threads.json"
|
||||
assert set(json.loads(ownership.read_text(encoding="utf-8"))["thread_ids"]) == {
|
||||
"alpha-thread",
|
||||
"second-alpha-thread",
|
||||
}
|
||||
assert not list(ownership.parent.glob("*.tmp"))
|
||||
|
||||
restarted_context = ApplicationContext(
|
||||
workspace_dir={"alpha": str(tmp_path / "alpha"), "beta": str(tmp_path / "beta")},
|
||||
)
|
||||
restarted = CodexAgentWrapper(name="codex-restarted", app_context=restarted_context)
|
||||
with pytest.raises(ValueError, match="belongs to workspace 'alpha'"):
|
||||
await restarted._check_thread_ownership("alpha-thread", "beta")
|
||||
|
||||
|
||||
# ----- Background/cron binding -----------------------------------------------
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue