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(workspace): preserve user config and data during workspace migration

z2tng3fef2c913ef4
diff --git a/jiuwenclaw/config.py b/jiuwenclaw/config.py
index 76215211cb..e1b902eb25 100644
--- a/jiuwenclaw/config.py
+++ b/jiuwenclaw/config.py
@@ -319,18 +319,58 @@ def update_memory_forbidden_in_config(updates: dict[str, Any]) -> None:
_dump_yaml_round_trip(_CONFIG_YAML_PATH, data)
+def _deep_merge(
+ template: dict[str, Any],
+ user: dict[str, Any],
+ depth: int = 0,
+) -> dict[str, Any]:
+ """Recursively merge template with user config, cleaning deprecated fields.
+
+ Rules:
+ - Add: fields only in template (new config options)
+ - Keep: user values for fields that exist in template (preserve user settings)
+ - Remove: fields only in user (deprecated config, cleanup)
+ - Max recursion depth: 4 (covers deep nested config like context_engine_config)
+
+ Args:
+ template: Template config dict with default values
+ user: User config dict
+ depth: Current recursion depth
+
+ Returns:
+ Merged dict synced with template structure, preserving user values.
+ """
+ if depth >= 4:
+ return user
+
+ result: dict[str, Any] = {}
+
+ for key, template_value in template.items():
+ if key not in user:
+ result[key] = template_value
+ elif isinstance(template_value, dict) and isinstance(user.get(key), dict):
+ result[key] = _deep_merge(template_value, user[key], depth + 1)
+ else:
+ result[key] = user[key]
+
+ return result
+
+
def migrate_config_from_template(
template_path: Path,
user_config_path: Path,
) -> bool:
- """Simplified config migration: merge template and user config.
+ """Sync user config with template structure, preserving user values.
+
+ Three-way merge:
+ - Add: new fields from template (new config options)
+ - Keep: user values for fields that exist in template
+ - Remove: deprecated fields not in template (cleanup)
- Merge rules (for first two levels):
- 1. First level: keep user value if exists, otherwise use template value
- 2. Second level: keep user value if exists, otherwise add from template
- 3. Exceptions (overwrite directly):
- - react.context_engine_config
- - permissions.tools
+ This preserves user settings like:
+ - models.*.model_config_obj.temperature
+ - react.context_engine_config.enabled
+ - react.context_engine_config.message_summary_offloader_config.*
Args:
template_path: Path to template config.yaml
@@ -348,38 +388,22 @@ def migrate_config_from_template(
template_data = _load_yaml_round_trip(template_path)
user_data = _load_yaml_round_trip(user_config_path)
- if template_data is None:
+ if not isinstance(template_data, dict):
return False
if user_data is None:
user_data = {}
- modified = False
-
- # First level traversal
- for key, value in template_data.items():
- if key not in user_data:
- # User doesn't have this key, use template value
- user_data[key] = value
- modified = True
- elif isinstance(value, dict) and isinstance(user_data[key], dict):
- # Traverse second level of template
- for sub_key, sub_value in value.items():
- # Check for overwrite exceptions
- is_overwrite = (
- key == "react" and sub_key == "context_engine_config"
- ) or (key == "permissions" and sub_key == "tools")
-
- if is_overwrite:
- # Overwrite directly from template
- user_data[key][sub_key] = sub_value
- modified = True
- elif sub_key not in user_data[key]:
- # User doesn't have this sub-key, add it
- user_data[key][sub_key] = sub_value
- modified = True
-
- if modified:
- _dump_yaml_round_trip(user_config_path, user_data)
-
- return modified
+ # Deep merge: template provides defaults, user values preserved
+ merged_data = _deep_merge(template_data, user_data)
+
+ # Guard against empty merged_data overwriting valid user config
+ if merged_data is None or not merged_data:
+ return False
+
+ # Only write if there are actual changes
+ if merged_data != user_data:
+ _dump_yaml_round_trip(user_config_path, merged_data)
+ return True
+
+ return False
diff --git a/jiuwenclaw/utils.py b/jiuwenclaw/utils.py
index cb1d5531c2..eacff82e72 100644
--- a/jiuwenclaw/utils.py
+++ b/jiuwenclaw/utils.py
@@ -27,6 +27,7 @@ Runtime layout:
import json
import os
+import re
import sys
import datetime
import shutil
@@ -455,37 +456,46 @@ def _migrate_legacy_workspace(
# 4. Migrate memory
new_memory = new_workspace / "memory"
+ new_memory.mkdir(parents=True, exist_ok=True)
+
if old_memory.exists():
- new_memory.mkdir(parents=True, exist_ok=True)
-
# 4.1 Migrate USER.md to workspace root (not in memory/)
old_user = old_memory / "USER.md"
new_user = new_workspace / "USER.md"
if old_user.exists() and not new_user.exists():
shutil.copy2(old_user, new_user)
logger.info("Migrated USER.md from memory/ to workspace root")
-
- # 4.2 Create daily_memory directory and migrate date-based .md files
+
+ # 4.2 Create daily_memory directory
daily_memory = new_memory / "daily_memory"
daily_memory.mkdir(parents=True, exist_ok=True)
-
+
+ # 4.3 Merge memory files (skip if already exists)
+ # Date pattern: YYYY-MM-DD.md (e.g., 2026-04-14.md)
+ date_pattern = re.compile(r"^\d{4}-\d{2}-\d{2}\.md$")
+
for item in old_memory.iterdir():
if item.name == "USER.md":
continue # Already handled above
if item.name == "MEMORY.md":
- # Copy MEMORY.md to new location
- shutil.copy2(item, new_memory / "MEMORY.md")
- elif item.is_file() and item.suffix == ".md":
- # Date-based memory files (e.g., 2025-01-01.md) go to daily_memory/
- shutil.copy2(item, daily_memory / item.name)
- logger.info(f"Migrated daily memory: {item.name}")
+ dest = new_memory / "MEMORY.md"
+ if not dest.exists():
+ shutil.copy2(item, dest)
+ logger.info("Migrated MEMORY.md")
+ elif item.is_file():
+ # Date-based memory files (YYYY-MM-DD.md) -> daily_memory/
+ # Other files -> new_memory/ root
+ dest = daily_memory / item.name if date_pattern.match(item.name) else new_memory / item.name
+ if not dest.exists():
+ shutil.copy2(item, dest)
+ logger.info(f"Migrated memory file: {item.name}")
elif item.is_dir():
- # Copy other directories as-is
+ # Other directories (e.g., specific memory categories)
dest = new_memory / item.name
- if dest.exists():
- shutil.rmtree(dest)
- shutil.copytree(item, dest)
-
+ if not dest.exists():
+ shutil.copytree(item, dest)
+ logger.info(f"Migrated memory directory: {item.name}")
+
logger.info(f"Migrated memory: {old_memory} -> {new_memory}")
# 5. Migrate cron_jobs.json from old_home to gateway
@@ -541,7 +551,10 @@ def _migrate_legacy_workspace(
logger.info(f"Migration completed: {new_workspace}")
-def prepare_workspace(overwrite: bool = True, preferred_language: Optional[str] = None):
+def prepare_workspace(
+ overwrite: bool = True,
+ preferred_language: Optional[str] = None,
+) -> None:
package_root = _find_package_root()
if not package_root:
raise RuntimeError("package root not found")
@@ -554,10 +567,14 @@ def prepare_workspace(overwrite: bool = True, preferred_language: Optional[str]
old_home = workspace_dir / "agent" / "home"
old_skills = workspace_dir / "agent" / "skills"
old_memory = workspace_dir / "agent" / "memory"
- new_workspace = workspace_dir / "agent" / "jiuwenclaw_workspace"
- # If old workspace exists and new workspace does not exist, migrate (for start command)
- if old_workspace.exists() and not new_workspace.exists() and not overwrite:
+ # Check for legacy directory migration (for start command, overwrite=False)
+ # Migration triggers when ANY legacy directory exists, not just old_workspace
+ legacy_dirs_exist = (
+ old_workspace.exists() or old_skills.exists() or old_memory.exists()
+ )
+
+ if legacy_dirs_exist and not overwrite:
_migrate_legacy_workspace(workspace_dir, preferred_language)
# If overwrite (init command), clean up old legacy directories first
elif overwrite:
@@ -698,6 +715,18 @@ def prepare_workspace(overwrite: bool = True, preferred_language: Optional[str]
if not dest_skill_state.exists():
shutil.copy2(skills_state_src, agent_skills / "skills_state.json")
+ # Explicitly copy template skills (was excluded by ignore_patterns above)
+ # Only copy skills that don't exist in user workspace (preserve user modifications)
+ template_skills = template_agent_workspace / "skills"
+ if template_skills.exists():
+ agent_skills.mkdir(parents=True, exist_ok=True)
+ for skill_dir in template_skills.iterdir():
+ if skill_dir.is_dir():
+ dest_skill = agent_skills / skill_dir.name
+ if not dest_skill.exists():
+ shutil.copytree(skill_dir, dest_skill)
+ logger.info(f"Copied template skill: {skill_dir.name}")
+
# sessions is runtime-only (template may not include it)
agent_sessions.mkdir(parents=True, exist_ok=True)