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

docs(coding_memory): update migration process details and improve clarity in documentation

NickFuryXXX149fc35fb560
diff --git a/docs/en/CodingMemory.md b/docs/en/CodingMemory.md
index 4ff9fd3d44..9f79a7ad20 100644
--- a/docs/en/CodingMemory.md
+++ b/docs/en/CodingMemory.md
@@ -104,6 +104,11 @@ merged into the new project bucket automatically:
fingerprint causes a later initialization to merge only the new content.
- The target records `.coding-memory-migration-v1.json`. A failed attempt does not
block Code mode or mark that source complete, so the next initialization retries it.
+- Run the first migration after stopping writes from the legacy backend, and avoid
+ concurrent `MEMORY.md` writes from other sessions. The Rail index lock is
+ process-local and cannot coordinate with a cross-process upgrade migration. Memory
+ content remains available to full-text indexing if such a race drops an index link,
+ but the link may need to be rebuilt.
Only an Agent workspace that can be identified unambiguously as the default space
considers the legacy `service_default/agent_default` and flat legacy workspace
diff --git "a/docs/zh/\347\274\226\347\240\201\350\256\260\345\277\206.md" "b/docs/zh/\347\274\226\347\240\201\350\256\260\345\277\206.md"
index c5093ce140..952907dc78 100644
--- "a/docs/zh/\347\274\226\347\240\201\350\256\260\345\277\206.md"
+++ "b/docs/zh/\347\274\226\347\240\201\350\256\260\345\277\206.md"
@@ -100,6 +100,9 @@ modes:
- 迁移具有幂等性;旧版回滚后新增的记忆会在下次使用时继续增量合并。
- 迁移报告写入目标目录的 `.coding-memory-migration-v1.json`。单次迁移失败不会
阻断 Code 模式,也不会写入完成标记,后续初始化会自动重试。
+- 首次迁移应在停止旧后端写入后进行,并避免其他会话同时写 `MEMORY.md`。当前
+ Rail 的索引锁只在进程内生效,无法与升级迁移建立跨进程互斥;即使发生竞争,
+ 记忆正文仍会保留并进入全文索引,但索引链接可能需要重建。
只有当前 Agent 工作区能够明确识别为默认空间时,系统才会兼容迁移旧版
`service_default/agent_default` 和旧扁平工作区中的编码记忆。非 default 的
diff --git a/jiuwenswarm/common/coding_memory_paths.py b/jiuwenswarm/common/coding_memory_paths.py
index 4b6db67809..64040fcd56 100644
--- a/jiuwenswarm/common/coding_memory_paths.py
+++ b/jiuwenswarm/common/coding_memory_paths.py
@@ -132,17 +132,15 @@ def _is_default_agent_workspace(agent_workspace: Path) -> bool:
):
return False
tenant_root = agent_workspace.parents[1]
- if tenant_root.name == "workspace_default":
+ tenant_name = tenant_root.name.casefold()
+ if tenant_name == "workspace_default":
return True
if (
- tenant_root.name == "agent_default"
- and tenant_root.parent.name == "service_default"
+ tenant_name == "agent_default"
+ and tenant_root.parent.name.casefold() == "service_default"
):
return True
- return not (
- tenant_root.name.startswith("workspace_")
- or tenant_root.name.startswith("agent_")
- )
+ return tenant_name in {".jiuwenswarm", ".jiuwenclaw"}
except IndexError:
return False
@@ -575,7 +573,7 @@ def prepare_project_coding_memory_dir(
"source": source_key,
"fingerprint": fingerprint,
"state": state,
- "complete": truncated == 0,
+ "complete": True,
"completed_at": datetime.now(timezone.utc).isoformat(),
"copied": copied,
"duplicates": duplicates,
@@ -598,13 +596,13 @@ def prepare_project_coding_memory_dir(
report_path,
json.dumps(
report,
- ensure_ascii=False,
+ ensure_ascii=True,
indent=2,
sort_keys=True,
)
+ "\n",
)
- except (OSError, portalocker.exceptions.LockException) as exc:
+ except Exception as exc:
warnings.append(f"failed to prepare {target}: {exc}")
failed = any(item.startswith("failed to ") for item in warnings)
diff --git a/tests/unit_tests/common/test_coding_memory_paths.py b/tests/unit_tests/common/test_coding_memory_paths.py
index 15a55e577b..285d6874d3 100644
--- a/tests/unit_tests/common/test_coding_memory_paths.py
+++ b/tests/unit_tests/common/test_coding_memory_paths.py
@@ -241,6 +241,31 @@ def test_non_default_unbound_workspace_does_not_import_default_tenant(
assert not (Path(result.target_dir) / "default-only.md").exists()
+def test_unknown_unbound_workspace_layout_does_not_import_default_tenant(
+ tmp_path: Path,
+) -> None:
+ tenant_root = tmp_path / "tenant-customer"
+ agent_workspace = tenant_root / "agent" / "workspace"
+ legacy = (
+ tenant_root
+ / "service_default"
+ / "agent_default"
+ / "agent"
+ / "jiuwenclaw_workspace"
+ / "coding_memory"
+ )
+ legacy.mkdir(parents=True)
+ (legacy / "default-only.md").write_text("default tenant", encoding="utf-8")
+
+ result = prepare_project_coding_memory_dir(
+ agent_workspace_dir=agent_workspace,
+ project_dir=None,
+ )
+
+ assert result.sources_found == 0
+ assert not (Path(result.target_dir) / "default-only.md").exists()
+
+
def test_default_legacy_named_agent_workspace_is_recognized(tmp_path: Path) -> None:
data_root = tmp_path / ".jiuwenswarm"
agent_workspace = (
@@ -296,20 +321,12 @@ def test_index_limit_does_not_drop_imported_memory_files(tmp_path: Path) -> None
assert (target / "legacy-0.md").is_file()
assert (target / "legacy-1.md").is_file()
- retained_lines = (target / "MEMORY.md").read_text(encoding="utf-8").splitlines()
- (target / "MEMORY.md").write_text(
- "\n".join(retained_lines[1:]),
- encoding="utf-8",
- )
- retried = prepare_project_coding_memory_dir(
+ repeated = prepare_project_coding_memory_dir(
agent_workspace_dir=agent_workspace,
project_dir=project,
)
- retried_index = (target / "MEMORY.md").read_text(encoding="utf-8")
- assert retried.sources_migrated == 1
- assert retried.index_truncated == 0
- assert "(legacy-0.md)" in retried_index
- assert "(legacy-1.md)" in retried_index
+ assert repeated.sources_migrated == 0
+ assert repeated.index_truncated == 0
def test_completed_migration_fast_path_does_not_hash_file_contents(
@@ -409,6 +426,64 @@ def test_failed_copy_is_fail_open_and_retried(
assert (Path(retried.target_dir) / "legacy.md").is_file()
+def test_report_serialization_handles_surrogate_source_path(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ agent_workspace = _agent_workspace(tmp_path / "data")
+ project = tmp_path / "demo"
+ legacy = project / "coding_memory"
+ legacy.mkdir(parents=True)
+ (legacy / "legacy.md").write_text("legacy", encoding="utf-8")
+ original_canonical_path = coding_memory_paths._canonical_path
+
+ def _canonical_path_with_surrogate(path: Path) -> str:
+ value = original_canonical_path(path)
+ return value + "\udcff" if path == legacy else value
+
+ monkeypatch.setattr(
+ coding_memory_paths,
+ "_canonical_path",
+ _canonical_path_with_surrogate,
+ )
+ result = prepare_project_coding_memory_dir(
+ agent_workspace_dir=agent_workspace,
+ project_dir=project,
+ )
+
+ assert result.failed is False
+ report = (
+ Path(result.target_dir) / ".coding-memory-migration-v1.json"
+ ).read_text(encoding="utf-8")
+ assert "\\udcff" in report
+
+
+def test_unexpected_report_write_error_is_fail_open(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ agent_workspace = _agent_workspace(tmp_path / "data")
+ project = tmp_path / "demo"
+ legacy = project / "coding_memory"
+ legacy.mkdir(parents=True)
+ (legacy / "legacy.md").write_text("legacy", encoding="utf-8")
+ original_atomic_write = coding_memory_paths._atomic_write_text
+
+ def _fail_report(path: Path, content: str) -> None:
+ if path.name == ".coding-memory-migration-v1.json":
+ raise UnicodeEncodeError("utf-8", "\udcff", 0, 1, "surrogate")
+ original_atomic_write(path, content)
+
+ monkeypatch.setattr(coding_memory_paths, "_atomic_write_text", _fail_report)
+ result = prepare_project_coding_memory_dir(
+ agent_workspace_dir=agent_workspace,
+ project_dir=project,
+ )
+
+ assert result.failed is True
+ assert (legacy / "legacy.md").is_file()
+
+
def test_lock_failure_is_fail_open_and_leaves_source_untouched(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,