Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(todo): resolve protected member access in CodeCheck

Nephamaster1e3d4d5f7fd1
diff --git a/jiuwenbox/tests/unit/test_file_snapshot.py b/jiuwenbox/tests/unit/test_file_snapshot.py
index a2afb89fc1..693f414002 100644
--- a/jiuwenbox/tests/unit/test_file_snapshot.py
+++ b/jiuwenbox/tests/unit/test_file_snapshot.py
@@ -73,11 +73,13 @@ def test_snapshot_read_always_closes_descriptor(tmp_path, monkeypatch, failure):
monkeypatch.setattr(os, "fdopen", fail_fdopen)
if failure == "read":
monkeypatch.setattr(sandbox_daemon, "MAX_FILE_BYTES", 1)
+ response, content = request(path)
if failure:
- with pytest.raises(OSError):
- sandbox_daemon._snapshot_read(str(path))
+ assert not response["ok"]
+ assert response["error"] == "io_error"
else:
- assert sandbox_daemon._snapshot_read(str(path))[1] == b"old"
+ assert response["ok"]
+ assert content == b"old"
assert len(descriptors) == 1
with pytest.raises(OSError):
os.fstat(descriptors[0])
diff --git a/jiuwenswarm/agents/harness/common/rails/concurrent_safe_rails.py b/jiuwenswarm/agents/harness/common/rails/concurrent_safe_rails.py
index b80497f988..9223772352 100644
--- a/jiuwenswarm/agents/harness/common/rails/concurrent_safe_rails.py
+++ b/jiuwenswarm/agents/harness/common/rails/concurrent_safe_rails.py
@@ -284,7 +284,7 @@ class ConcurrentSafeTaskPlanningRail(TaskPlanningRail):
async def apply():
try:
- todos = await tool._load_todos(session_id)
+ todos = await tool.read_todos(session_id)
except Exception:
logger.debug("ConcurrentSafeTaskPlanningRail: no todos for sync")
return
@@ -320,7 +320,7 @@ class ConcurrentSafeTaskPlanningRail(TaskPlanningRail):
if not changed:
return
- await tool._save_todos(session_id, todos)
+ await tool.write_todos(session_id, todos)
logger.info(
"ConcurrentSafeTaskPlanningRail: synced %d todos from TaskPlan",
len(todos),
diff --git a/jiuwenswarm/agents/harness/common/rails/task_execution_rail.py b/jiuwenswarm/agents/harness/common/rails/task_execution_rail.py
index 7cb0c2076f..7e9a54fb6f 100644
--- a/jiuwenswarm/agents/harness/common/rails/task_execution_rail.py
+++ b/jiuwenswarm/agents/harness/common/rails/task_execution_rail.py
@@ -1901,13 +1901,13 @@ class TaskExecutionRail(DeepAgentRail):
tool = TodoModifyTool(self._deep_agent.deep_config.sys_operation, str(todo_path.parent.parent))
async def advance():
- items = await tool._load_todos(session_id)
+ items = await tool.read_todos(session_id)
for item in items:
if item.id == raw_id:
if item.status.value not in self._BINDING_PENDING:
return {"changed": False}
item.status = TodoStatus.IN_PROGRESS
- await tool._save_todos(session_id, items)
+ await tool.write_todos(session_id, items)
return {"changed": True}
if item.status not in (TodoStatus.COMPLETED, TodoStatus.CANCELLED) and not is_persistent_todo(item):
return {"changed": False}
diff --git a/jiuwenswarm/agents/harness/common/tools/todo_compat.py b/jiuwenswarm/agents/harness/common/tools/todo_compat.py
index 0eb36187f7..63fc2d911a 100644
--- a/jiuwenswarm/agents/harness/common/tools/todo_compat.py
+++ b/jiuwenswarm/agents/harness/common/tools/todo_compat.py
@@ -230,7 +230,7 @@ async def persist_backlog_create(tool: Any, session: Any, result: Any) -> Any:
if session_id is None:
return result
# todo_create already replaced the file with this call's tasks.
- todos = await tool._load_todos(session_id)
+ todos = await tool.read_todos(session_id)
changed = False
for todo in todos:
if is_persistent_todo(todo):
@@ -240,7 +240,7 @@ async def persist_backlog_create(tool: Any, session: Any, result: Any) -> Any:
changed = True
if not changed:
return result
- await tool._save_todos(session_id, todos)
+ await tool.write_todos(session_id, todos)
message = result.get("message")
if not isinstance(message, str):
return result
@@ -270,7 +270,7 @@ def _is_missing_todo_file(exc: Exception) -> bool:
async def _load_todos_or_empty(tool: Any, session_id: str) -> list[TodoItem]:
"""Read the current file. A missing file means there is nothing to keep."""
try:
- loaded = await tool._load_todos(session_id)
+ loaded = await tool.read_todos(session_id)
except Exception as exc:
if not _is_missing_todo_file(exc):
logger.warning("todo_create could not read existing reminders: %s", exc)
@@ -303,12 +303,12 @@ async def restore_persistent_reminders(
preserved = [todo for todo in before if _is_active_reminder(todo)]
if not preserved:
return
- current = await tool._load_todos(session_id)
+ current = await tool.read_todos(session_id)
seen = {todo.id for todo in current if todo.id}
extras = [todo for todo in preserved if todo.id and todo.id not in seen]
if not extras:
return
- await tool._save_todos(session_id, [*current, *extras])
+ await tool.write_todos(session_id, [*current, *extras])
async def merge_persistent_list_items(tool: Any, session: Any, result: Any) -> Any:
@@ -326,7 +326,7 @@ async def merge_persistent_list_items(tool: Any, session: Any, result: Any) -> A
if isinstance(item, dict) and isinstance(item.get("id"), str):
seen.add(item["id"])
extras: list[dict[str, Any]] = []
- todos = await tool._load_todos(session_id)
+ todos = await tool.read_todos(session_id)
for todo in todos:
entry = _persistent_list_entry(todo)
if entry is None or entry["id"] in seen:
diff --git a/jiuwenswarm/server/runtime/agent_adapter/plan_pause_helpers.py b/jiuwenswarm/server/runtime/agent_adapter/plan_pause_helpers.py
index 0392e5403e..9c2a15220b 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/plan_pause_helpers.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/plan_pause_helpers.py
@@ -342,12 +342,12 @@ async def pause_pending_todos_on_tool(modify_tool: Any, session_id: str) -> bool
from openjiuwen.harness.tools.todo_store import transact
async def apply():
- todos = await modify_tool._load_todos(session_id)
+ todos = await modify_tool.read_todos(session_id)
if not todos:
return False
if not pause_todo_items(todos):
return False
- await modify_tool._save_todos(session_id, todos)
+ await modify_tool.write_todos(session_id, todos)
return True
return await transact(modify_tool, session_id, apply)
@@ -361,7 +361,7 @@ async def snapshot_and_isolate_unfinished_todos(modify_tool: Any, session_id: st
from openjiuwen.harness.tools.todo_store import transact
async def apply():
- todos = await modify_tool._load_todos(session_id)
+ todos = await modify_tool.read_todos(session_id)
if not todos:
return None
@@ -401,7 +401,7 @@ async def delete_todos_via_modify_tool(modify_tool: Any, ids: list[str], *, sess
async def apply():
if not ids:
return
- current = await modify_tool._load_todos(session_id)
+ current = await modify_tool.read_todos(session_id)
await modify_tool._delete_todos(session_id, ids, current) # pylint: disable=protected-access
return await transact(modify_tool, session_id, apply)
@@ -413,7 +413,7 @@ async def cancel_todos_via_modify_tool(modify_tool: Any, ids: list[str], *, sess
async def apply():
if not ids:
return
- current = await modify_tool._load_todos(session_id)
+ current = await modify_tool.read_todos(session_id)
await modify_tool._cancel_todos(session_id, ids, current) # pylint: disable=protected-access
return await transact(modify_tool, session_id, apply)
@@ -423,7 +423,7 @@ async def cancel_pending_todos_on_tool(modify_tool: Any, session_id: str) -> boo
from openjiuwen.harness.tools.todo_store import transact
async def apply():
- todos = await modify_tool._load_todos(session_id)
+ todos = await modify_tool.read_todos(session_id)
if not todos:
return False
diff --git a/tests/unit_tests/agentserver/rails/test_concurrent_safe_todo_lifecycle.py b/tests/unit_tests/agentserver/rails/test_concurrent_safe_todo_lifecycle.py
index 9e0f68371d..309fa94c0e 100644
--- a/tests/unit_tests/agentserver/rails/test_concurrent_safe_todo_lifecycle.py
+++ b/tests/unit_tests/agentserver/rails/test_concurrent_safe_todo_lifecycle.py
@@ -36,9 +36,9 @@ async def isolate_todo_context(tmp_path, monkeypatch):
def business_tool(tool):
"""Retain isolated plan-algorithm mocks; satisfy the snapshot boundary."""
- tool._load_todos = tool.load_todos
+ tool.read_todos = tool.load_todos
if hasattr(tool, "save_todos"):
- tool._save_todos = tool.save_todos
+ tool.write_todos = tool.save_todos
items = tool.load_todos.return_value
raw = json.dumps([t.to_dict() for t in items])
tool.get_file_path = lambda sid: f"/test/{id(tool)}/{sid}/todo.json"
diff --git a/tests/unit_tests/agentserver/rails/test_persistent_todo.py b/tests/unit_tests/agentserver/rails/test_persistent_todo.py
index df2ca3f662..5dbff5e41b 100644
--- a/tests/unit_tests/agentserver/rails/test_persistent_todo.py
+++ b/tests/unit_tests/agentserver/rails/test_persistent_todo.py
@@ -115,10 +115,10 @@ class _TodoFile:
def __init__(self, todos: list[TodoItem]) -> None:
self.todos = todos
- async def _load_todos(self, _session_id: str) -> list[TodoItem]:
+ async def read_todos(self, _session_id: str) -> list[TodoItem]:
return list(self.todos)
- async def _save_todos(self, _session_id: str, todos: list[TodoItem]) -> None:
+ async def write_todos(self, _session_id: str, todos: list[TodoItem]) -> None:
self.todos = list(todos)
diff --git a/tests/unit_tests/common/test_todo_integration.py b/tests/unit_tests/common/test_todo_integration.py
index 27319d2d9f..e57fbe479b 100644
--- a/tests/unit_tests/common/test_todo_integration.py
+++ b/tests/unit_tests/common/test_todo_integration.py
@@ -10,7 +10,7 @@ from openjiuwen.core.session.agent import Session
from openjiuwen.core.sys_operation import LocalWorkConfig, OperationMode
from openjiuwen.core.sys_operation.local._rw_lock_manager import ReadWriteLockManager
from openjiuwen.core.sys_operation.local.fs_operation import FsOperation
-from openjiuwen.harness.tools.todo import TodoCreateTool, TodoModifyTool
+from openjiuwen.harness.tools.todo import TodoCreateTool, TodoModifyTool, TodoTool
from openjiuwen.harness.tools.todo_store import (
_BINDING,
bind_todo_batch,
@@ -28,12 +28,17 @@ from jiuwenswarm.agents.harness.common.tools.deepresearch.todo_progress import (
persist_deepresearch_task_update,
)
from jiuwenswarm.agents.harness.common.tools.todo_compat import (
+ CompatibleTodoModifyTool,
_install_backlog_create_patch,
)
-from jiuwenswarm.agents.harness.common.tools.todo_resume import remember_turn_user_query
+from jiuwenswarm.agents.harness.common.tools.todo_resume import (
+ mark_persistent_todo,
+ remember_turn_user_query,
+)
from jiuwenswarm.common.todo_snapshot import TODO_SNAPSHOT_MODE
from jiuwenswarm.server.runtime.agent_adapter.plan_pause_helpers import (
pause_pending_todos_on_tool,
+ snapshot_and_isolate_unfinished_todos,
)
@@ -191,6 +196,34 @@ async def test_reminder_create_and_mark_are_one_commit(env, monkeypatch):
assert result.todo_snapshot == saved
+async def test_pause_isolation_preserves_reminder_without_legacy_cache(env, monkeypatch):
+ creator, original, session, path = env
+ await creator.invoke(
+ {"tasks": [{"id": "reminder", "content": "remind"}, {"id": "work", "content": "work"}]},
+ session=session,
+ )
+
+ async def mark(items):
+ mark_persistent_todo(items[0])
+
+ await original.update_todos("s", mark)
+ before = json.loads(path.read_text())
+ modifier = CompatibleTodoModifyTool(SimpleNamespace(fs=lambda: original.fs), original.workspace)
+ modifier.snapshot_guard = original.snapshot_guard
+
+ async def legacy_cache(*args, **kwargs):
+ raise AssertionError("Pause isolation must not use the legacy cache")
+
+ monkeypatch.setattr(TodoTool, "load_todos", legacy_cache)
+ monkeypatch.setattr(TodoTool, "save_todos", legacy_cache)
+ snapshot = await snapshot_and_isolate_unfinished_todos(modifier, "s")
+ saved = json.loads(path.read_text())
+ assert saved["revision"] == before["revision"] + 1
+ assert [(item["id"], item["status"]) for item in saved["todos"]] == [("reminder", "pending")]
+ assert {item["id"] for item in snapshot["unfinished"]} == {"reminder", "work"}
+
+
@pytest.mark.asyncio
async def test_pause_and_concurrent_completion_preserve_new_state(env):
c, m, session, path = env