mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-11-15 09:11:24 +00:00
fix(codex): claim ownership for resumed threads
This commit is contained in:
parent
1599b7236b
commit
48af5032a8
7 changed files with 143 additions and 24 deletions
|
|
@ -90,7 +90,8 @@ workspace_dir:
|
|||
The first entry is the default workspace; every alias gets the same subdirectory layout (`metadata_dir`,
|
||||
`session_dir`, and the others above) materialized under its own root at startup. Aliases must be non-empty, and
|
||||
resolved directories must be distinct and must not contain one another. A single string keeps the existing
|
||||
behavior and is normalized internally to a `default` alias.
|
||||
behavior and is normalized internally to a `default` alias. Multi-workspace configurations also require a non-empty
|
||||
`mem_session_dir` so agent thread ownership remains enforceable after restart.
|
||||
|
||||
Job calls accept an optional `workspace` field naming the alias to bind, for example
|
||||
`reme read_note path=daily/note.md workspace=scratch`; omitting it binds the default workspace, and an unknown
|
||||
|
|
|
|||
|
|
@ -93,7 +93,8 @@ workspace_dir:
|
|||
|
||||
第一个 entry 是默认 workspace;每个别名在启动时都会在自己的根目录下创建相同的子目录布局
|
||||
(`metadata_dir`、`session_dir` 等上述目录)。别名必须非空,且解析后的目录必须互不相同、不能互相包含。
|
||||
单个字符串保持原有行为,内部归一化为 `default` 别名。
|
||||
单个字符串保持原有行为,内部归一化为 `default` 别名。多 workspace 配置还要求 `mem_session_dir` 非空,
|
||||
以确保重启后仍能强制执行 agent thread 的归属隔离。
|
||||
|
||||
Job 调用接受可选的 `workspace` 字段指定绑定的别名,例如
|
||||
`reme read_note path=daily/note.md workspace=scratch`;缺省绑定默认 workspace,未知别名会直接报错。
|
||||
|
|
|
|||
|
|
@ -399,8 +399,7 @@ class CodexAgentWrapper(BaseAgentWrapper):
|
|||
thread_source=thread_source,
|
||||
**common,
|
||||
)
|
||||
if fork_session or not thread_id:
|
||||
await self._record_thread_ownership(alias, thread.id)
|
||||
await self._record_thread_ownership(alias, thread.id)
|
||||
tool_contexts[thread.id] = requested_tool_context
|
||||
return thread
|
||||
|
||||
|
|
@ -458,25 +457,40 @@ class CodexAgentWrapper(BaseAgentWrapper):
|
|||
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)
|
||||
if state is None or path is None:
|
||||
workspaces = _context_workspaces(self.app_context)
|
||||
if not workspaces:
|
||||
return
|
||||
ids = await self._owned_thread_ids(alias)
|
||||
async with state.lock:
|
||||
if thread_id in ids:
|
||||
alias = self._normalize_thread_alias(alias)
|
||||
async with self.app_context.codex_thread_ownership_lock:
|
||||
for owner_alias in workspaces:
|
||||
if thread_id not in await self._owned_thread_ids(owner_alias):
|
||||
continue
|
||||
if owner_alias == alias:
|
||||
return
|
||||
raise ValueError(
|
||||
f"Codex thread {thread_id} belongs to workspace {owner_alias!r}; "
|
||||
f"it cannot be used from workspace {alias!r}",
|
||||
)
|
||||
|
||||
state = self._codex_thread_state(alias)
|
||||
ids = await self._owned_thread_ids(alias)
|
||||
path = self._thread_ownership_path(alias)
|
||||
if path is None:
|
||||
ids.add(thread_id)
|
||||
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 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.
|
||||
|
|
@ -642,6 +656,7 @@ class CodexAgentWrapper(BaseAgentWrapper):
|
|||
thread = await codex.thread_resume(session_id)
|
||||
try:
|
||||
await thread.compact()
|
||||
await self._record_thread_ownership(str(workspace or ""), thread.id)
|
||||
finally:
|
||||
if self._release_threads:
|
||||
await self._release_thread(codex, thread.id)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
"""Application context: shared state container for components, jobs, and service."""
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Iterator, MutableMapping
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
|
@ -66,6 +67,7 @@ class ApplicationContext:
|
|||
self._components = _LegacyComponentView(self)
|
||||
self.jobs: dict[str, "BaseJob"] = {}
|
||||
self.thread_pool: ThreadPoolExecutor | None = None
|
||||
self.codex_thread_ownership_lock = asyncio.Lock()
|
||||
self.metadata: dict[str, Any] = {}
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -115,6 +115,8 @@ class ApplicationConfig(BaseModel):
|
|||
"""Normalize every workspace_dir form to an ordered mapping; the first entry is the default."""
|
||||
self.workspaces = dict(self.workspaces) or {DEFAULT_WORKSPACE_ALIAS: self.workspace_dir}
|
||||
if len(self.workspaces) > 1:
|
||||
if not self.mem_session_dir.strip():
|
||||
raise ValueError("mem_session_dir must be non-empty in multi-workspace configurations")
|
||||
for alias, workspace in self.workspaces.items():
|
||||
root = Path(workspace).resolve(strict=False)
|
||||
for field_name in (
|
||||
|
|
|
|||
|
|
@ -225,6 +225,17 @@ def test_multi_workspace_rejects_derived_directory_escape(tmp_path):
|
|||
)
|
||||
|
||||
|
||||
def test_multi_workspace_requires_session_ownership_storage(tmp_path):
|
||||
with pytest.raises(ValidationError, match="mem_session_dir must be non-empty"):
|
||||
ApplicationConfig(
|
||||
workspace_dir={"alpha": str(tmp_path / "alpha"), "beta": str(tmp_path / "beta")},
|
||||
mem_session_dir="",
|
||||
)
|
||||
|
||||
config = ApplicationConfig(workspace_dir=str(tmp_path / "single"), mem_session_dir="")
|
||||
assert config.mem_session_dir == ""
|
||||
|
||||
|
||||
def test_single_workspace_keeps_absolute_derived_directory(tmp_path):
|
||||
external = tmp_path / "external-metadata"
|
||||
config = ApplicationConfig(workspace_dir=str(tmp_path / "single"), metadata_dir=str(external))
|
||||
|
|
|
|||
|
|
@ -230,6 +230,89 @@ async def test_codex_thread_ownership_is_shared_and_atomic_across_wrappers(tmp_p
|
|||
await restarted._check_thread_ownership("alpha-thread", "beta")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_codex_resume_and_compact_claim_external_threads(tmp_path, monkeypatch):
|
||||
context = ApplicationContext(workspace_dir={"alpha": str(tmp_path / "alpha"), "beta": str(tmp_path / "beta")})
|
||||
wrapper_a = CodexAgentWrapper(name="codex-a", app_context=context, release_threads=False)
|
||||
wrapper_b = CodexAgentWrapper(name="codex-b", app_context=context, release_threads=False)
|
||||
|
||||
class FakeCodex:
|
||||
"""Resume an existing external thread."""
|
||||
|
||||
async def thread_resume(self, thread_id, **_kwargs):
|
||||
return SimpleNamespace(id=thread_id)
|
||||
|
||||
await wrapper_a._open_thread(FakeCodex(), {"session_id": "external-thread", "workspace": "alpha"})
|
||||
assert context.workspaces["alpha"].codex_thread_state.owned_thread_ids == {"external-thread"}
|
||||
with pytest.raises(ValueError, match="belongs to workspace 'alpha'"):
|
||||
await wrapper_b._open_thread(FakeCodex(), {"session_id": "external-thread", "workspace": "beta"})
|
||||
|
||||
class CompactThread:
|
||||
"""Minimal native compaction double."""
|
||||
|
||||
id = "compact-thread"
|
||||
|
||||
async def compact(self):
|
||||
return None
|
||||
|
||||
compact_codex = SimpleNamespace(thread_resume=lambda _thread_id: None)
|
||||
|
||||
async def resume_compact(_thread_id):
|
||||
return CompactThread()
|
||||
|
||||
compact_codex.thread_resume = resume_compact
|
||||
|
||||
async def get_compact_codex():
|
||||
return compact_codex
|
||||
|
||||
monkeypatch.setattr(wrapper_a, "_get_codex", get_compact_codex)
|
||||
await wrapper_a.compact_session("compact-thread", workspace="alpha")
|
||||
assert "compact-thread" in context.workspaces["alpha"].codex_thread_state.owned_thread_ids
|
||||
|
||||
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._open_thread(FakeCodex(), {"session_id": "external-thread", "workspace": "beta"})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_codex_concurrent_first_resume_has_one_owner(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)
|
||||
|
||||
class FakeCodex:
|
||||
"""Yield between resume and ownership registration to force interleaving."""
|
||||
|
||||
async def thread_resume(self, thread_id, **_kwargs):
|
||||
await asyncio.sleep(0)
|
||||
return SimpleNamespace(id=thread_id)
|
||||
|
||||
results = await asyncio.gather(
|
||||
wrapper_a._open_thread(FakeCodex(), {"session_id": "shared-external", "workspace": "alpha"}),
|
||||
wrapper_b._open_thread(FakeCodex(), {"session_id": "shared-external", "workspace": "beta"}),
|
||||
return_exceptions=True,
|
||||
)
|
||||
assert len([result for result in results if not isinstance(result, Exception)]) == 1
|
||||
assert len([result for result in results if isinstance(result, ValueError)]) == 1
|
||||
|
||||
owners = [
|
||||
alias
|
||||
for alias, workspace in context.workspaces.items()
|
||||
if "shared-external" in (workspace.codex_thread_state.owned_thread_ids or set())
|
||||
]
|
||||
assert len(owners) == 1
|
||||
other = "beta" if owners[0] == "alpha" else "alpha"
|
||||
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"):
|
||||
await restarted._open_thread(FakeCodex(), {"session_id": "shared-external", "workspace": other})
|
||||
|
||||
|
||||
# ----- Background/cron binding -----------------------------------------------
|
||||
|
||||
|
||||
|
|
@ -437,15 +520,19 @@ async def test_codex_thread_cwd_follows_bound_workspace(tmp_path):
|
|||
class FakeCodex:
|
||||
"""Capture thread_start parameters without an app-server."""
|
||||
|
||||
next_id = 1
|
||||
|
||||
async def thread_start(self, **kwargs):
|
||||
observed.update(kwargs)
|
||||
return SimpleNamespace(id="thread-1")
|
||||
self.next_id += 1
|
||||
return SimpleNamespace(id=f"thread-{self.next_id - 1}")
|
||||
|
||||
await wrapper._open_thread(FakeCodex(), {"workspace": "beta"})
|
||||
codex = FakeCodex()
|
||||
await wrapper._open_thread(codex, {"workspace": "beta"})
|
||||
assert observed["cwd"] == str(tmp_path / "beta")
|
||||
|
||||
observed.clear()
|
||||
await wrapper._open_thread(FakeCodex(), {})
|
||||
await wrapper._open_thread(codex, {})
|
||||
assert observed["cwd"] != str(tmp_path / "beta")
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue