refactor: split backend into harness (deerflow.*) and app (app.*) (#1131)

* refactor: extract shared utils to break harness→app cross-layer imports

Move _validate_skill_frontmatter to src/skills/validation.py and
CONVERTIBLE_EXTENSIONS + convert_file_to_markdown to src/utils/file_conversion.py.
This eliminates the two reverse dependencies from client.py (harness layer)
into gateway/routers/ (app layer), preparing for the harness/app package split.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: split backend/src into harness (deerflow.*) and app (app.*)

Physically split the monolithic backend/src/ package into two layers:

- **Harness** (`packages/harness/deerflow/`): publishable agent framework
  package with import prefix `deerflow.*`. Contains agents, sandbox, tools,
  models, MCP, skills, config, and all core infrastructure.

- **App** (`app/`): unpublished application code with import prefix `app.*`.
  Contains gateway (FastAPI REST API) and channels (IM integrations).

Key changes:
- Move 13 harness modules to packages/harness/deerflow/ via git mv
- Move gateway + channels to app/ via git mv
- Rename all imports: src.* → deerflow.* (harness) / app.* (app layer)
- Set up uv workspace with deerflow-harness as workspace member
- Update langgraph.json, config.example.yaml, all scripts, Docker files
- Add build-system (hatchling) to harness pyproject.toml
- Add PYTHONPATH=. to gateway startup commands for app.* resolution
- Update ruff.toml with known-first-party for import sorting
- Update all documentation to reflect new directory structure

Boundary rule enforced: harness code never imports from app.
All 429 tests pass. Lint clean.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: add harness→app boundary check test and update docs

Add test_harness_boundary.py that scans all Python files in
packages/harness/deerflow/ and fails if any `from app.*` or
`import app.*` statement is found. This enforces the architectural
rule that the harness layer never depends on the app layer.

Update CLAUDE.md to document the harness/app split architecture,
import conventions, and the boundary enforcement test.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add config versioning with auto-upgrade on startup

When config.example.yaml schema changes, developers' local config.yaml
files can silently become outdated. This adds a config_version field and
auto-upgrade mechanism so breaking changes (like src.* → deerflow.*
renames) are applied automatically before services start.

- Add config_version: 1 to config.example.yaml
- Add startup version check warning in AppConfig.from_file()
- Add scripts/config-upgrade.sh with migration registry for value replacements
- Add `make config-upgrade` target
- Auto-run config-upgrade in serve.sh and start-daemon.sh before starting services
- Add config error hints in service failure messages

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix comments

* fix: update src.* import in test_sandbox_tools_security to deerflow.*

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: handle empty config and search parent dirs for config.example.yaml

Address Copilot review comments on PR #1131:
- Guard against yaml.safe_load() returning None for empty config files
- Search parent directories for config.example.yaml instead of only
  looking next to config.yaml, fixing detection in common setups

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: correct skills root path depth and config_version type coercion

- loader.py: fix get_skills_root_path() to use 5 parent levels (was 3)
  after harness split, file lives at packages/harness/deerflow/skills/
  so parent×3 resolved to backend/packages/harness/ instead of backend/
- app_config.py: coerce config_version to int() before comparison in
  _check_config_version() to prevent TypeError when YAML stores value
  as string (e.g. config_version: "1")
- tests: add regression tests for both fixes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: update test imports from src.* to deerflow.*/app.* after harness refactor

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
DanielWalnut
2026-03-14 22:55:52 +08:00
committed by GitHub
parent 9b49a80dda
commit 76803b826f
198 changed files with 1786 additions and 941 deletions

View File

@@ -16,7 +16,7 @@ from fastapi.testclient import TestClient
def _make_paths(base_dir: Path):
"""Return a Paths instance pointing to base_dir."""
from src.config.paths import Paths
from deerflow.config.paths import Paths
return Paths(base_dir=base_dir)
@@ -72,7 +72,7 @@ class TestPaths:
class TestAgentConfig:
def test_minimal_config(self):
from src.config.agents_config import AgentConfig
from deerflow.config.agents_config import AgentConfig
cfg = AgentConfig(name="my-agent")
assert cfg.name == "my-agent"
@@ -81,7 +81,7 @@ class TestAgentConfig:
assert cfg.tool_groups is None
def test_full_config(self):
from src.config.agents_config import AgentConfig
from deerflow.config.agents_config import AgentConfig
cfg = AgentConfig(
name="code-reviewer",
@@ -94,7 +94,7 @@ class TestAgentConfig:
assert cfg.tool_groups == ["file:read", "bash"]
def test_config_from_dict(self):
from src.config.agents_config import AgentConfig
from deerflow.config.agents_config import AgentConfig
data = {"name": "test-agent", "description": "A test", "model": "gpt-4"}
cfg = AgentConfig(**data)
@@ -113,8 +113,8 @@ class TestLoadAgentConfig:
config_dict = {"name": "code-reviewer", "description": "Code review agent", "model": "deepseek-v3"}
_write_agent(tmp_path, "code-reviewer", config_dict)
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import load_agent_config
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import load_agent_config
cfg = load_agent_config("code-reviewer")
@@ -123,8 +123,8 @@ class TestLoadAgentConfig:
assert cfg.model == "deepseek-v3"
def test_load_missing_agent_raises(self, tmp_path):
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import load_agent_config
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import load_agent_config
with pytest.raises(FileNotFoundError):
load_agent_config("nonexistent-agent")
@@ -133,8 +133,8 @@ class TestLoadAgentConfig:
# Create directory without config.yaml
(tmp_path / "agents" / "broken-agent").mkdir(parents=True)
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import load_agent_config
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import load_agent_config
with pytest.raises(FileNotFoundError):
load_agent_config("broken-agent")
@@ -146,8 +146,8 @@ class TestLoadAgentConfig:
(agent_dir / "config.yaml").write_text("description: My agent\n")
(agent_dir / "SOUL.md").write_text("Hello")
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import load_agent_config
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import load_agent_config
cfg = load_agent_config("inferred-name")
@@ -157,8 +157,8 @@ class TestLoadAgentConfig:
config_dict = {"name": "restricted", "tool_groups": ["file:read", "file:write"]}
_write_agent(tmp_path, "restricted", config_dict)
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import load_agent_config
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import load_agent_config
cfg = load_agent_config("restricted")
@@ -171,8 +171,8 @@ class TestLoadAgentConfig:
(agent_dir / "config.yaml").write_text("name: legacy-agent\nprompt_file: system.md\n")
(agent_dir / "SOUL.md").write_text("Soul content")
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import load_agent_config
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import load_agent_config
cfg = load_agent_config("legacy-agent")
@@ -189,8 +189,8 @@ class TestLoadAgentSoul:
expected_soul = "You are a specialized code review expert."
_write_agent(tmp_path, "code-reviewer", {"name": "code-reviewer"}, soul=expected_soul)
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import AgentConfig, load_agent_soul
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import AgentConfig, load_agent_soul
cfg = AgentConfig(name="code-reviewer")
soul = load_agent_soul(cfg.name)
@@ -203,8 +203,8 @@ class TestLoadAgentSoul:
(agent_dir / "config.yaml").write_text("name: no-soul\n")
# No SOUL.md created
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import AgentConfig, load_agent_soul
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import AgentConfig, load_agent_soul
cfg = AgentConfig(name="no-soul")
soul = load_agent_soul(cfg.name)
@@ -217,8 +217,8 @@ class TestLoadAgentSoul:
(agent_dir / "config.yaml").write_text("name: empty-soul\n")
(agent_dir / "SOUL.md").write_text(" \n ")
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import AgentConfig, load_agent_soul
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import AgentConfig, load_agent_soul
cfg = AgentConfig(name="empty-soul")
soul = load_agent_soul(cfg.name)
@@ -233,8 +233,8 @@ class TestLoadAgentSoul:
class TestListCustomAgents:
def test_empty_when_no_agents_dir(self, tmp_path):
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import list_custom_agents
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import list_custom_agents
agents = list_custom_agents()
@@ -244,8 +244,8 @@ class TestListCustomAgents:
_write_agent(tmp_path, "agent-a", {"name": "agent-a"})
_write_agent(tmp_path, "agent-b", {"name": "agent-b", "description": "B"})
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import list_custom_agents
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import list_custom_agents
agents = list_custom_agents()
@@ -259,8 +259,8 @@ class TestListCustomAgents:
# Invalid dir (no config.yaml)
(tmp_path / "agents" / "invalid-dir").mkdir(parents=True)
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import list_custom_agents
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import list_custom_agents
agents = list_custom_agents()
@@ -274,8 +274,8 @@ class TestListCustomAgents:
(agents_dir / "not-a-dir.txt").write_text("hello")
_write_agent(tmp_path, "real-agent", {"name": "real-agent"})
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import list_custom_agents
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import list_custom_agents
agents = list_custom_agents()
@@ -287,8 +287,8 @@ class TestListCustomAgents:
_write_agent(tmp_path, "a-agent", {"name": "a-agent"})
_write_agent(tmp_path, "m-agent", {"name": "m-agent"})
with patch("src.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from src.config.agents_config import list_custom_agents
with patch("deerflow.config.agents_config.get_paths", return_value=_make_paths(tmp_path)):
from deerflow.config.agents_config import list_custom_agents
agents = list_custom_agents()
@@ -304,35 +304,35 @@ class TestListCustomAgents:
class TestMemoryFilePath:
def test_global_memory_path(self, tmp_path):
"""None agent_name should return global memory file."""
import src.agents.memory.updater as updater_mod
from src.config.memory_config import MemoryConfig
import deerflow.agents.memory.updater as updater_mod
from deerflow.config.memory_config import MemoryConfig
with (
patch("src.agents.memory.updater.get_paths", return_value=_make_paths(tmp_path)),
patch("src.agents.memory.updater.get_memory_config", return_value=MemoryConfig(storage_path="")),
patch("deerflow.agents.memory.updater.get_paths", return_value=_make_paths(tmp_path)),
patch("deerflow.agents.memory.updater.get_memory_config", return_value=MemoryConfig(storage_path="")),
):
path = updater_mod._get_memory_file_path(None)
assert path == tmp_path / "memory.json"
def test_agent_memory_path(self, tmp_path):
"""Providing agent_name should return per-agent memory file."""
import src.agents.memory.updater as updater_mod
from src.config.memory_config import MemoryConfig
import deerflow.agents.memory.updater as updater_mod
from deerflow.config.memory_config import MemoryConfig
with (
patch("src.agents.memory.updater.get_paths", return_value=_make_paths(tmp_path)),
patch("src.agents.memory.updater.get_memory_config", return_value=MemoryConfig(storage_path="")),
patch("deerflow.agents.memory.updater.get_paths", return_value=_make_paths(tmp_path)),
patch("deerflow.agents.memory.updater.get_memory_config", return_value=MemoryConfig(storage_path="")),
):
path = updater_mod._get_memory_file_path("code-reviewer")
assert path == tmp_path / "agents" / "code-reviewer" / "memory.json"
def test_different_paths_for_different_agents(self, tmp_path):
import src.agents.memory.updater as updater_mod
from src.config.memory_config import MemoryConfig
import deerflow.agents.memory.updater as updater_mod
from deerflow.config.memory_config import MemoryConfig
with (
patch("src.agents.memory.updater.get_paths", return_value=_make_paths(tmp_path)),
patch("src.agents.memory.updater.get_memory_config", return_value=MemoryConfig(storage_path="")),
patch("deerflow.agents.memory.updater.get_paths", return_value=_make_paths(tmp_path)),
patch("deerflow.agents.memory.updater.get_memory_config", return_value=MemoryConfig(storage_path="")),
):
path_global = updater_mod._get_memory_file_path(None)
path_a = updater_mod._get_memory_file_path("agent-a")
@@ -352,7 +352,7 @@ def _make_test_app(tmp_path: Path):
"""Create a FastAPI app with the agents router, patching paths to tmp_path."""
from fastapi import FastAPI
from src.gateway.routers.agents import router
from app.gateway.routers.agents import router
app = FastAPI()
app.include_router(router)
@@ -364,7 +364,7 @@ def agent_client(tmp_path):
"""TestClient with agents router, using tmp_path as base_dir."""
paths_instance = _make_paths(tmp_path)
with patch("src.config.agents_config.get_paths", return_value=paths_instance), patch("src.gateway.routers.agents.get_paths", return_value=paths_instance):
with patch("deerflow.config.agents_config.get_paths", return_value=paths_instance), patch("app.gateway.routers.agents.get_paths", return_value=paths_instance):
app = _make_test_app(tmp_path)
with TestClient(app) as client:
client._tmp_path = tmp_path # type: ignore[attr-defined]