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(harness): doc for harness

lamsaan_yauaf6d700d782b
diff --git a/docs/assets/images/harness_arch.png b/docs/assets/images/harness_arch.png
new file mode 100644
index 0000000000..c47c16185d
Binary files /dev/null and b/docs/assets/images/harness_arch.png differ
diff --git a/docs/assets/images/hook_system_use_cases.png b/docs/assets/images/hook_system_use_cases.png
new file mode 100644
index 0000000000..3cad2aef31
Binary files /dev/null and b/docs/assets/images/hook_system_use_cases.png differ
diff --git a/docs/en/Harness.md b/docs/en/Harness.md
new file mode 100644
index 0000000000..d2ea42929f
--- /dev/null
+++ b/docs/en/Harness.md
@@ -0,0 +1,624 @@
+# openJiuwen Harness
+
+## Introduction
+
+`Agent = Model + Harness`. openJiuwen Harness is the general-purpose agent runtime framework in openJiuwen for Harness Engineering. It turns a large language model from a "conversational reasoning unit" into a task execution system that can plan, call tools, manage context, obey permissions, delegate subtasks, and evolve over time.
+
+The core goal of Harness Engineering is to build the engineered execution environment around the model. This environment manages task loops, tools, context, state, permissions, memory, verification, stop conditions, and feedback loops, keeping agents controllable, reliable, and extensible in long-running tasks and complex environments. openJiuwen Harness targets general agent scenarios; code editing, research, browser operation, multimodal tasks, system operations, team collaboration, and business automation can all be built on the same Harness capabilities.
+
+## 1. Conceptual Positioning
+
+### 1.1 What Is Harness?
+
+Harness is the engineering control layer of an agent. The model generates reasoning and action intent, while the Harness places that intent into a controlled execution system. In other words, openJiuwen Harness does not replace the model; it supplies the execution, state, constraints, and feedback mechanisms outside the model:
+
+- Turn user goals into task loops that can keep making progress.
+- Govern tool calls through unified registration, filtering, permission checks, and auditing.
+- Organize context into static prompts, dynamic attachments, session state, and long-term memory.
+- Split the execution process into extensible lifecycle events.
+- Delegate complex tasks to sub-agents while isolating sub-agent context.
+- Unify completion conditions, timeouts, round limits, token budgets, and custom predicates into one stop strategy.
+- Embed safety, human confirmation, plan mode, and verification gates into the runtime.
+
+### 1.2 Responsibilities of DeepAgent
+
+`DeepAgent` is the runtime body of Harness. Internally, it reuses a ReAct reasoning loop; externally, it adds task management, state management, Rail lifecycle hooks, sub-agent delegation, and infrastructure integration.
+
+DeepAgent has two runtime modes:
+
+- Single-round ReAct mode: suitable for short tasks and direct tool calls.
+- Dual-layer Task Loop mode: suitable for long-running tasks, multi-round progress, follow-up, steering, abort, task planning, and stop-condition control.
+
+### 1.3 Design Principles
+
+- Generality: Harness targets many kinds of agent execution scenarios.
+- Governability: model actions must pass through tool, permission, state, and stop-condition governance.
+- Extensibility: features extend through Rails, Tools, SubAgents, and Stop Evaluators.
+- Recoverability: task state, plans, pending follow-ups, and plan-mode state are persisted into the session.
+- Composability: Workspace, SysOperation, Permission, PromptAttachment, ContextEngine, and other infrastructure pieces are decoupled and composable.
+- Evolvability: trajectory collection, skill use, skill creation, and skill evolution form a feedback loop for experience reuse.
+
+## 2. Overall Architecture
+
+![openJiuwen Harness Architecture](../assets/images/harness_arch.png)
+
+The architecture has three layers:
+
+- DeepAgent Core Engine: task loop, stop conditions, ReAct reasoning, sub-agent delegation, and Rail lifecycle.
+- Extension: four extension surfaces: Subagent, Tool, Rails, and Stop-Condition.
+- Infrastructure: system operation abstraction, session management, and permission control.
+
+## 3. Core Features
+
+### 3.1 Agent Loop: From ReAct to a Dual-Layer Task Loop
+
+The Agent Loop is the execution backbone of Harness. The inner ReAct loop is responsible for Think -> Act -> Observe. The outer Task Loop turns open-ended goals into multi-round task execution, state updates, and completion checks.
+
+The outer loop is composed of `TaskLoopController`, `LoopCoordinator`, `TaskLoopEventHandler`, `TaskLoopEventExecutor`, and `LoopQueues`. It submits each round as a `CoreTask`, waits for execution to complete, and evaluates whether another round is needed. Runtime interaction also lives at this layer:
+
+- `follow_up`: queue a user follow-up for the next round.
+- `steer`: inject runtime guidance into the current task.
+- `abort`: cancel the outer task loop and try to interrupt the inner ReAct loop.
+
+Task planning and task completion are two key capabilities of the Agent Loop:
+
+- `TaskPlanningRail` connects todo tools, `TaskPlan`, task dependencies, progress reminders, and multi-model selection to the loop.
+- `TaskCompletionRail` unifies max rounds, timeout, completion promise, repeated confirmation, and custom stop conditions.
+
+Stop conditions use OR semantics: if any condition is met, the loop stops. Built-in evaluators include `MaxRoundsEvaluator`, `TimeoutEvaluator`, `TokenBudgetEvaluator`, `CompletionPromiseEvaluator`, and `CustomPredicateEvaluator`.
+
+### 3.2 Rail Lifecycle: Lifecycle Extension Mechanism
+
+Rails are the lifecycle extension surface of Harness. A Rail does not replace the main flow; it injects capabilities at key points in outer tasks, inner model calls, and tool calls.
+
+| Lifecycle event | Location | Typical use |
+| --- | --- | --- |
+| `before_invoke` / `after_invoke` | Outer DeepAgent | Initialize resources, load skills, clean caches, sync external memory |
+| `before_task_iteration` / `after_task_iteration` | Outer Task Loop | Rewrite task instructions, sync todo, detect completion promises, trigger evolution |
+| `before_model_call` / `after_model_call` | Inner ReAct | Assemble prompts, inject safety rules, compress context, collect trajectories |
+| `before_tool_call` / `after_tool_call` | Inner ReAct | Permission checks, plan-mode restrictions, LSP diagnostics, progress reminders |
+| `on_model_exception` / `on_tool_exception` | Inner ReAct | Repair context and govern exceptions |
+
+The following figure shows four representative Harness lifecycle-hook use cases: dynamic context engineering, self-reflection, LSP diagnostics, and prompt injection.
+
+![Hook System Use Cases](../assets/images/hook_system_use_cases.png)
+
+Common Rails can be grouped by capability:
+
+- Execution control: `TaskPlanningRail`, `TaskCompletionRail`, `AgentModeRail`.
+- Tools and system operations: `SysOperationRail`, `ProgressiveToolRail`, `LspRail`, `McpRail`.
+- Safety and permissions: `SafetyPromptRail`, `PermissionInterruptRail`, `VerificationRail`.
+- Context and memory: `ContextAssembleRail`, `ContextProcessorRail`, `MemoryRail`, `CodingMemoryRail`, `ExternalMemoryRail`.
+- Skills and evolution: `SkillUseRail`, `EvolutionRail`, `SkillCreateRail`, `SkillEvolutionRail`.
+- Sub-agents: `SubagentRail`, `VerificationContractRail`.
+
+### 3.3 Tool & Execution: Tools, System Operations, and Permission Governance
+
+Tool & Execution is the execution layer that turns model intent into real-world action. Tools describe metadata through `ToolCard`, execute actions through `Tool` instances, and are exposed to the model through the ability manager.
+
+Major tool families include:
+
+- File system: `read_file`, `write_file`, `edit_file`, `glob`, `list_files`, `grep`.
+- Shell and code execution: `bash`, `powershell`, `code`.
+- Web and external resources: web search/fetch, MCP resource list/read.
+- Multimodal: OCR, VQA, audio transcription, audio QA, audio metadata, video understanding.
+- Code intelligence: LSP definition/reference/symbol/call hierarchy.
+- Workflow helpers: todo, skill, subagent, plan mode, worktree, cron, `ask_user`.
+
+For the full built-in tool index, see [3.12 Built-in Capabilities Index](#312-built-in-capabilities-index).
+
+`SysOperationRail` registers file, shell, and optional code tools. All system operations execute through a shared `SysOperation`, so local execution, sandboxing, working-directory restriction, and permission control are handled consistently.
+
+When the tool set is large, `ProgressiveToolRail` provides progressive tool disclosure: the model sees only meta tools and basic tools by default, then uses `tool_search` to search for tools and `load_tools` to load the tools needed for the current session. This reduces context noise and lowers the chance of misusing high-risk tools.
+
+Permission governance is composed of `PermissionEngine` and `PermissionInterruptRail`. `PermissionEngine` evaluates allow / ask / deny policies, while `PermissionInterruptRail` intercepts every tool call before execution and handles direct allow, direct deny, HITL confirmation, "allow once", and "always allow" flows. Shell commands go through AST analysis, and external path checks are combined with the current decision using the stricter result.
+
+Workspace is the persistence boundary of the execution layer. The default schema includes `AGENT.md`, `SOUL.md`, `HEARTBEAT.md`, `IDENTITY.md`, `memory/`, `coding_memory/`, `todo/`, `messages/`, `skills/`, `agents/`, and `context/session_memory.md`. It also supports custom directories, localized default content, team workspace links, and worktree links.
+
+### 3.4 Agent Mode: Runtime Modes and Plan Mode Constraints
+
+Different modes are an important runtime policy layer in Harness. `AgentMode` currently supports two modes: `normal` and `plan`. `normal` is the default execution mode and allows regular tool calls and task execution. `plan` is a read-only planning mode for understanding, exploration, and solution design before real execution begins. Mode state is stored in session-scoped `DeepAgentState.plan_mode`, including `mode`, `pre_plan_mode`, and `plan_slug`.
+
+`AgentModeRail` registers `switch_mode`, `enter_plan_mode`, and `exit_plan_mode`. During the model/tool lifecycle, it injects mode instructions, filters model-visible tools, and intercepts tool calls that do not match the current mode.
+
+| Capability | Mechanism |
+| --- | --- |
+| Mode switching | `switch_mode` switches the session runtime mode between `normal` and `plan` |
+| Plan file initialization | `enter_plan_mode` creates or reuses `.plans/<slug>.md` under the workspace |
+| Plan exit | `exit_plan_mode` reads the full plan and restores the mode that was active before entering plan mode |
+| Tool visibility | `plan` mode hides todo/session tools; `normal` mode hides `enter_plan_mode` / `exit_plan_mode` |
+| Write restrictions | `plan` mode blocks shell/git write operations to avoid modifying the real workspace during planning |
+| File-write restrictions | `write_file` / `edit_file` can only write the plan file in `plan` mode |
+| Sub-agent collaboration | After entering `plan` mode, `task_tool` can be registered dynamically to support planning-time sub-agent exploration |
+
+### 3.5 Context & Memory: Context Engineering and Memory System
+
+Context & Memory ensures the model sees the right information at the right time. Harness uses an extended `SystemPromptBuilder` to manage static prompt sections, such as `identity`, `safety`, `skills`, `tools`, `todo`, `task_tool`, `memory`, `workspace`, `context`, `completion_signal`, and `verification_contract`.
+
+Dynamic context is managed by `PromptAttachmentManager`. It stores auto-attached content by session and section for the current model call, then injects it into the final context window as separate user messages. Good candidates for PromptAttachment include long context files, heartbeat, memory recall, external memory prefetch, dynamic plan-mode state, and LSP diagnostics.
+
+Context engineering is mainly handled by two Rails:
+
+- `ContextAssembleRail`: injects workspace structure, context files, tool lists, and file-based prompt sections.
+- `ContextProcessorRail`: configures ContextEngine processors, compresses long conversations, offloads large tool results, repairs incomplete tool_call / ToolMessage context, and manages session memory.
+
+The memory system covers three scenarios:
+
+| Rail | Scenario | Capabilities |
+| --- | --- | --- |
+| `MemoryRail` | General long-term memory | Initialize memory manager, register memory tools, inject memory-use prompts |
+| `CodingMemoryRail` | Project/task memory | Automatic recall, vector + BM25 hybrid retrieval, top-5 memory or index injection |
+| `ExternalMemoryRail` | External memory systems | Provider initialization, prefetch, sync_turn, provider tool registration, and circuit breaking |
+
+### 3.6 Subagent & Verification: Sub-Agent Delegation and Verification Gates
+
+Subagent & Verification splits complex work across more specialized agents and introduces independent verification after high-risk implementation work.
+
+Synchronous sub-agents are delegated through `task_tool`:
+
+- The parent agent waits for the sub-agent to finish after calling the tool.
+- The sub-agent uses an isolated session.
+- `browser_agent` and `verification_agent` use deterministic sub-session IDs, so context can be reused after failures.
+
+Asynchronous sub-agents are delegated through session tools:
+
+- `sessions_spawn`: submit a background subtask.
+- `sessions_list`: inspect background tasks.
+- `sessions_cancel`: cancel a background task.
+- `SessionToolkit`: maintain background task state.
+
+Publicly exported built-in sub-agents include `browser_agent`, `code_agent`, `research_agent`, `verification_agent`, and `mobile_gui_agent`; `general-purpose` can be injected automatically with `add_general_purpose_agent=True`. For the full list, see [3.12 Built-in Capabilities Index](#312-built-in-capabilities-index).
+
+Verification gates have two layers: the parent agent and the verification agent. `VerificationContractRail` injects the verification contract into the parent agent and requires a verification agent after non-trivial implementation. `VerificationRail` restricts the verification agent's tool set and write permissions, ensuring that the verification agent only reads, searches, runs verification commands, and reports results without modifying project files.
+
+### 3.7 Skill & Evolution: Skill Reuse and Experience Evolution
+
+Skill & Evolution lets Harness do more than execute the current task: it can also convert execution experience into reusable capability.
+
+`SkillUseRail` manages skill usage:
+
+- Supports multiple `skills_dir` directories.
+- Supports both `all` and `auto_list` exposure modes.
+- Registers `skill_tool` and `list_skill`.
+- Supports enabled/disabled filtering.
+- Refreshes incrementally based on `SKILL.md` mtime.
+- Can inject evolution experience into skill descriptions.
+
+`EvolutionRail` manages experience collection and evolution triggers:
+
+- Collects model-call trajectories.
+- Collects tool-call trajectories.
+- Supports triggers after invoke, model call, tool call, and task iteration.
+- Supports background asynchronous evolution, concurrency limits, and host-visible events.
+
+Concrete evolution capabilities include `SkillCreateRail`, `TeamSkillCreateRail`, `SkillEvolutionRail`, `TeamSkillEvolutionRail`, and `TrajectoryRail`. They can propose new skills, evolve existing skills, or distill team skills from tool-call patterns, collaboration patterns, and historical trajectories.
+
+### 3.8 Coding Intelligence: LSP Code Intelligence
+
+LSP stands for Language Server Protocol. It is a standard protocol between editors or developer tools and language servers. Through a unified interface, it provides code intelligence capabilities such as go-to-definition, reference lookup, symbol indexing, implementation lookup, call hierarchy, and diagnostics. Harness uses LSP to expose editor-grade code understanding to the Agent, so coding agents do not have to rely only on text search and can understand code structure through a language server.
+
+#### LSP Tool and Rail
+
+`LspRail` is the lifecycle entry point for LSP capabilities. It initializes the LSP subsystem and registers a single `lsp` tool into the Agent's ability manager. The `lsp` tool is the unified tool entry point for model calls to LSP capabilities: it receives an operation type, file path, line/character position, or query condition, then returns structured results.
+
+`LspRail` creates the tool based on the Agent's workspace, sys_operation, language, and agent_id, binding LSP capabilities to the current workspace and runtime. When the Agent finishes or the Rail is unloaded, `LspRail` removes the tool and shuts down the LSP subsystem.
+
+#### Code Intelligence Operations
+
+The `lsp` tool supports eight code intelligence operations:
+
+| Operation | Purpose |
+| --- | --- |
+| `goToDefinition` | Find the definition location of a symbol |
+| `findReferences` | Find references to a symbol |
+| `documentSymbol` | List functions, classes, variables, and other symbols in one file |
+| `workspaceSymbol` | Search symbols across the entire workspace |
+| `goToImplementation` | Find implementations of an interface or abstract method |
+| `prepareCallHierarchy` | Prepare the call hierarchy item at the current position |
+| `incomingCalls` | Find methods that call the current function |
+| `outgoingCalls` | Find methods called by the current function |
+
+#### Code Diagnostics
+
+LSP supports not only code navigation but also code diagnostics. After the Agent modifies files, `LspRail` triggers the language server to reanalyze the code. Before the next model call, Harness injects diagnostic information into the context. These diagnostics help the Agent discover syntax errors, type errors, unresolved references, call signature mismatches, and similar issues, forming an `edit -> diagnose -> fix` coding loop.
+
+LSP execution is workspace-aware: paths are resolved relative to the workspace, gitignored results are filtered, and large files are not sent to the language server. If a language server is not configured for the current language, the `lsp` tool returns an explicit error. This makes the capability suitable for semantic navigation, impact analysis, call-chain understanding, and post-edit diagnosis and repair.
+
+### 3.9 Multimodal Understanding: Images, Audio, and Video
+
+Harness wraps image, audio, and video understanding as tools, so the Agent can bring non-text materials into task context.
+
+Image tools are driven by `VisionModelConfig`:
+
+- `image_ocr`: extract text from images while preserving structure, line breaks, numbers, and symbols as much as possible.
+- `visual_question_answering`: answer visual questions based on an image and optional OCR results.
+- Local image paths and HTTP(S) image URLs are supported; sandbox-only paths are rejected to avoid accessing paths that are not visible to the tool.
+
+Audio tools are driven by `AudioModelConfig`:
+
+- `audio_transcription`: transcribe speech in audio into text.
+- `audio_question_answering`: answer questions based on audio content.
+- `audio_metadata`: identify audio duration and, when ACR is configured, song metadata.
+- Local audio paths and HTTP(S) audio URLs are supported, with `max_audio_bytes` limiting download size.
+
+Video tools are driven by the vision model:
+
+- `video_understanding`: accept a local video or HTTP(S) video URL and answer questions about the video content.
+- Local videos are encoded as data URLs; calls can limit `max_tokens`, `temperature`, and `timeout_seconds`.
+
+Multimodal model configuration can be passed explicitly or constructed from environment variables. `VisionModelConfig.from_env()` reads variables such as `VISION_API_KEY`, `VISION_BASE_URL`, and `VISION_MODEL`; `AudioModelConfig.from_env()` reads variables such as `AUDIO_API_KEY`, `AUDIO_BASE_URL`, `AUDIO_TRANSCRIPTION_MODEL`, and `AUDIO_QUESTION_ANSWERING_MODEL`. When `enable_read_image_multimodal=True`, reading an image through `read_file` can include native multimodal input. When it is disabled, `read_file` returns only image metadata and suggests using vision tools.
+
+### 3.10 Web & External Resources: Web and External Resource Access
+
+Harness can connect Web and external resource systems into the Agent task loop, allowing agents to perform research search, webpage fetching, and cross-system resource reading.
+
+Web tools cover two main scenarios:
+
+- `free_search` / `paid_search`: search and research tasks, using free or paid search providers depending on deployment.
+- `fetch_webpage`: fetch webpage content for material organization, citation checking, and later context injection.
+
+MCP is integrated through `McpRail`. DeepAgent can register MCP servers through `mcps`; after initialization, `McpRail` registers `list_mcp_resources` and `read_mcp_resource` for the Agent, allowing the model to discover and read registered MCP resources. This is useful for connecting document repositories, data sources, tool runtimes, or external system resources to Harness without placing all content into the prompt ahead of time.
+
+### 3.11 Isolated Execution & Automation: Isolated Execution and Long-Term Automation
+
+Harness also provides tools for isolated execution and long-term automation, allowing agents to operate closer to real engineering environments under explicit constraints.
+
+`WorktreeRail` registers `enter_worktree` and `exit_worktree`, and manages a per-agent `WorktreeManager`. The Agent can enter an isolated git worktree before running file operations and shell commands, avoiding pollution of the main workspace. On exit, it can choose to keep or remove the worktree. This capability is suitable for code modification, experimental refactoring, parallel verification, and tasks that need an isolated rollback boundary.
+
+The cron tool family integrates with a host-provided scheduled-task backend. It can expose a unified `cron` tool, and it can also remain compatible with split tool forms such as list/get/create/update/delete/toggle/preview. This capability is suitable for long-term automation, periodic tasks, scheduled reminders, and agent workflows that need to maintain plans across sessions.
+
+### 3.12 Built-in Capabilities Index
+
+This section provides a quick index of Harness built-in capabilities. Whether a capability is actually visible in the model-callable tool list depends on `create_deep_agent` parameters, explicitly registered rails, host-provided backends, permission configuration, and the current runtime environment.
+
+#### Built-in Tool
+
+| Capability Family | Representative Tools | Main Purpose | Enablement / Source |
+| --- | --- | --- | --- |
+| Files and search | `read_file`, `write_file`, `edit_file`, `glob`, `grep`, `list_files` | Read, write, edit, and locate workspace context by glob or text search | Registered by `SysOperationRail`; constrained by workspace, sys_operation, and permission policy |
+| Shell and code execution | `bash`, `powershell`, `code` | Run shell commands, scripts, and code snippets | Registered by `SysOperationRail`; `powershell` is mainly for Windows, and `code` can be enabled by rail configuration |
+| Todo / planning | `todo_create`, `todo_list`, `todo_get`, `todo_modify` | Manage session-level task lists for long-task decomposition, tracking, and state updates | Mounted by related rails or host configuration, persisted under the workspace todo area |
+| Web | `free_search`, `paid_search`, `fetch_webpage` | Web search, webpage fetching, and material organization | Available after Web tools are registered; search tools depend on deployment and provider configuration |
+| Multimodal | `image_ocr`, `visual_question_answering`, `audio_transcription`, `audio_question_answering`, `audio_metadata`, `video_understanding` | Image OCR / VQA, audio transcription / QA / metadata, and video QA | Depends on `VisionModelConfig` / `AudioModelConfig`; supports local or HTTP(S) media |
+| LSP code intelligence | `lsp` | Definitions, references, symbols, implementations, call hierarchy, and code diagnostics | Registered by `LspRail`; depends on language servers and the current workspace |
+| MCP external resources | `list_mcp_resources`, `read_mcp_resource` | List and read resources from registered MCP servers | Registered by `McpRail`; DeepAgent connects MCP servers through `mcps` |
+| Sub-agent delegation | `task_tool`, `sessions_spawn`, `sessions_list`, `sessions_cancel` | Delegate synchronous subtasks or create, inspect, and cancel background sub-agent tasks | Registered by `SubagentRail`; sync/async form depends on `enable_async_subagent` |
+| Skill / tool discovery | `skill_tool`, `list_skill`, `search_tools`, `load_tools` | Invoke skills, list skills, and progressively search/load tools | Registered by `SkillUseRail` and `ProgressiveToolRail`; depends on skill directories and progressive-tool config |
+| Memory / Coding Memory | `memory_*`, `coding_memory_*` | Read and write long-term memory, coding experience, and project knowledge | Registered by `MemoryRail`, `CodingMemoryRail`, and related memory rails |
+| Agent mode and user interaction | `switch_mode`, `enter_plan_mode`, `exit_plan_mode`, `ask_user` | Switch normal/plan modes and request user input at key decision points | Registered by `AgentModeRail`, `AskUserRail`, or host interaction rails |
+| Worktree / Cron | `enter_worktree`, `exit_worktree`, `cron`, `cron_list_jobs`, `cron_get_job`, `cron_create_job`, `cron_update_job`, `cron_delete_job`, `cron_toggle_job`, `cron_preview_job` | Enter isolated git worktrees and manage scheduled/periodic automation | Registered by `WorktreeRail` or a host cron backend; cron supports both unified and split tool forms |
+| Mobile GUI / browser runtime | Mobile coordinate/navigation tools, browser runtime tools | Mobile GUI actions, browser task execution, runtime health checks, and cancellation control | Registered by mobile GUI / browser runtime tool packages or host runtimes |
+
+#### Built-in Rail
+
+| Rail | Main Responsibility | Enablement / Source |
+| --- | --- | --- |
+| `SecurityRail` | Base safety guardrails and prompt/tool security checks | Auto-added by `create_deep_agent` unless a compatible user rail is already provided |
+| `TaskPlanningRail` | Task planning, decomposition, and model-selection assistance | Auto-added when `enable_task_planning=True`; can also be registered explicitly |
+| `SkillUseRail` | Discover, load, and invoke workspace / team skills | Auto-added when `skills` is configured or `enable_skill_discovery=True`; can also be registered explicitly |
+| `SubagentRail` | Register synchronous `task_tool` or async session tools and inject sub-agent guidance | Auto-added when `subagents` are configured; async mode is controlled by `enable_async_subagent` |
+| `SysOperationRail` | Register file, shell, and code-execution system-operation tools | Usually registered explicitly, bound to workspace and sys_operation |
+| `ProgressiveToolRail` | Progressive tool disclosure, tool search, and on-demand loading | Registered when progressive-tool capability is configured |
+| `LspRail` | Initialize the LSP subsystem, register `lsp`, and inject code diagnostics | Explicitly registered; depends on language servers and workspace code |
+| `McpRail` | Register MCP resource list/read tools | Explicitly registered and used with `mcps` to connect MCP servers |
+| `MemoryRail`, `CodingMemoryRail`, `ExternalMemoryRail` | Manage long-term memory, coding memory, and external memory reads/writes | Registered explicitly as needed or assembled by the host |
+| `AgentModeRail` | Manage normal/plan modes, register mode-switching tools, and enforce Plan Mode tool filtering and write restrictions | Enable plan mode or register explicitly; see the Agent Mode section in 3.4 |
+| `AskUserRail`, `ConfirmInterruptRail` | Request user input, confirmation, or interrupt recovery during execution | Registered for HITL scenarios or host interaction capabilities |
+| `PermissionInterruptRail`, `SafetyPromptRail` | Intercept tool calls for permission checks, user confirmation, and safety prompt injection | Enabled through `permissions` or safety rails |
+| `TaskCompletionRail` | Determine task completion and support outer Task Loop stop conditions | Registered for Task Loop or custom completion evaluation scenarios |
+| `SessionRail` | Manage sub-session context and lifecycle capabilities | Registered for sub-agent / session scenarios |
+| `VerificationRail`, `VerificationContractRail` | Restrict verification-agent permissions and inject verification contracts into the parent agent | Registered for implementation, regression verification, and high-risk tasks |
+| `SkillCreateRail`, `TeamSkillCreateRail`, `SkillEvolutionRail`, `TeamSkillEvolutionRail` | Create personal/team skills and evolve reusable experience | Registered for skill self-evolution or team skill distillation |
+| `TrajectoryRail`, `ContextEvolutionRail`, `EvolutionRail`, `EvolutionInterruptRail` | Collect trajectories, trigger context/skill evolution, and handle evolution interrupts | Registered in experience-evolution pipelines |
+| `HeartbeatRail` | Maintain runtime heartbeat and long-task status | Registered when long tasks or host heartbeat awareness are needed |
+
+#### Built-in Subagent
+
+| Subagent | Main Purpose | Enablement / Source |
+| --- | --- | --- |
+| `browser_agent` | Browser use, webpage interaction, and tasks requiring browser runtime | `build_browser_agent_config` / `create_browser_agent` |
+| `code_agent` | Code reading, local implementation, and engineering changes | `build_code_agent_config` / `create_code_agent` |
+| `research_agent` | Search research, material organization, fact checking, and report synthesis | `build_research_agent_config` / `create_research_agent` |
+| `verification_agent` | Read changes, run verification commands, inspect regression risk, and report conclusions | `build_verification_agent_config` / `create_verification_agent` |
+| `mobile_gui_agent` | Mobile GUI operation, coordinate taps, scrolling, text input, and navigation | `build_mobile_gui_agent_config` / `create_mobile_gui_agent` |
+| `general-purpose` | General fallback sub-agent that can inherit the parent prompt, tools, mcps, and skills | Injected automatically when `add_general_purpose_agent=True` |
+| `plan_agent`, `explore_agent` | Planning/exploration templates present in source | Internal templates / not the main exports from `subagents/__init__.py`; use concrete assembly code as the source of truth |
+
+## 4. Runtime Flow
+
+### 4.1 Runtime Sequence Diagram
+
+```mermaid
+sequenceDiagram
+ autonumber
+ participant Caller as Caller
+ participant DA as DeepAgent.invoke/stream
+ participant Rail as Rail Lifecycle
+ participant Controller as TaskLoopController
+ participant Handler as TaskLoopEventHandler
+ participant Executor as TaskLoopEventExecutor
+ participant RA as ReActAgent
+ participant MT as Model/Tools
+ participant Coord as LoopCoordinator
+ participant State as Session/State
+
+ Caller->>DA: invoke/stream(inputs, session)
+ DA->>DA: _ensure_initialized()
+ DA->>State: load DeepAgentState
+ DA->>Rail: before_invoke
+ alt Single-round mode
+ DA->>RA: invoke/stream effective inputs
+ RA->>Rail: before_model_call / before_tool_call
+ RA->>MT: model reasoning and tool execution
+ MT-->>RA: observations and final answer
+ RA->>Rail: after_tool_call / after_model_call
+ RA-->>DA: result or stream chunks
+ else Task Loop mode
+ DA->>Controller: submit round task
+ Controller->>Handler: dispatch INPUT event
+ Handler->>Executor: execute task iteration
+ Executor->>Rail: before_task_iteration
+ Executor->>RA: invoke internal ReAct
+ RA->>Rail: before_model_call / before_tool_call
+ RA->>MT: model reasoning and tool execution
+ MT-->>RA: observations and final answer
+ RA->>Rail: after_tool_call / after_model_call
+ RA-->>Executor: iteration result
+ Executor->>Rail: after_task_iteration
+ Executor->>Coord: evaluate stop conditions
+ alt continue
+ Coord-->>Controller: continue with follow_up / steer / async results
+ else stop or abort
+ Coord-->>DA: finish current loop
+ end
+ end
+ DA->>State: save session state
+ DA->>Rail: after_invoke
+ DA-->>Caller: output
+```
+
+### 4.2 Runtime Main Flow
+
+The sequence diagram in 4.1 describes the runtime process after the agent has already been created. The main flow proceeds as follows:
+
+1. The caller starts a run through `invoke/stream(inputs, session)`.
+2. `DeepAgent.invoke` or `DeepAgent.stream` first executes `_ensure_initialized()` to ensure that workspace, MCP, Rails, and task-loop runtime objects are initialized.
+3. DeepAgent loads `DeepAgentState` from the session and triggers the `before_invoke` lifecycle event.
+4. In single-round mode, DeepAgent calls the internal `ReActAgent` directly. The inner ReAct loop triggers model/tool-level Rail hooks around model reasoning and tool execution, then returns a result or stream chunks to DeepAgent.
+5. In Task Loop mode, DeepAgent submits the current round to `TaskLoopController`; the event is dispatched by `TaskLoopEventHandler`, and `TaskLoopEventExecutor` executes the task iteration.
+6. Each Task Loop iteration triggers `before_task_iteration`, calls the internal `ReActAgent` to complete Think / Act / Observe, and then triggers `after_task_iteration`.
+7. `LoopCoordinator` decides whether to continue, stop, or abort based on stop conditions, abort signals, follow-up, steering, or asynchronous sub-agent results.
+8. When the run finishes, DeepAgent saves session state, triggers `after_invoke`, and returns output to the caller.
+
+### 4.3 Single-Round Mode and Task Loop Mode
+
+Single-round mode focuses on low overhead and immediate response. It is suitable for one-shot Q&A, short tool tasks, and automation that does not need planning or multi-round progress. Although there is no outer Task Loop, inner hooks such as `before_model_call`, `after_model_call`, `before_tool_call`, and `after_tool_call` still take effect.
+
+Task Loop mode focuses on long-running progress and controlled convergence. It is suitable for task planning, asynchronous sub-agents, runtime intervention, explicit completion conditions, and multi-round verification. Compared with single-round mode, Task Loop maintains additional round state, stop conditions, and runtime interaction queues, allowing the agent to keep moving toward the goal across iterations.
+
+### 4.4 Runtime Interaction and State
+
+Harness supports three kinds of runtime interaction:
+
+- `follow_up(query)`: append a follow-up question to the current task.
+- `steer(message)`: inject guidance into a running agent.
+- `abort()`: cancel the current task loop.
+
+These interactions enter `TaskLoopEventHandler` through the controller event queue, then write to `LoopQueues` or trigger `LoopCoordinator.request_abort()`. The Task Loop consumes these signals at iteration boundaries, so it can append requirements, adjust direction, or end the task without breaking the consistency of the current tool call.
+
+DeepAgent runtime state is saved into the session. Core state includes:
+
+- Current iteration count.
+- TaskPlan.
+- Stop-condition state.
+- Pending follow-ups.
+- Plan-mode state.
+
+State persistence allows long-running tasks to resume and lets external runtimes inspect task progress. `invoke` and `stream` save and clear the current run state after completion. If asynchronous session-spawn subtasks are still pending, the controller is retained to wait for later results.
+
+## 5. Usage Guide
+
+### 5.1 Suitable Scenarios
+
+Harness is suitable for:
+
+- Long-running task execution.
+- Multi-tool automation.
+- Multi-round research and information organization.
+- Browser tasks.
+- Multimodal understanding.
+- Code semantic navigation, reference analysis, call-chain analysis, and post-edit diagnostics.
+- File and system operations.
+- Web/MCP external knowledge and resource access.
+- Code modification and verification in isolated worktrees.
+- Scheduled and periodic task automation.
+- High-risk tasks that require permission confirmation.
+- Tasks that require planning, verification, and rollback.
+- Agents that need memory, skill reuse, and self-evolution.
+
+### 5.2 Complete DeepAgent Example
+
+In the example below, `AgentModeRail()` enables mode capabilities. If `default_mode` is not explicitly passed, the Agent starts from `AgentMode.NORMAL` / `normal`.
+
+```python
+import asyncio
+
+from openjiuwen.core.single_agent import AgentCard, create_agent_session
+from openjiuwen.harness import Workspace, create_deep_agent
+from openjiuwen.harness.rails import (
+ AgentModeRail,
+ ContextAssembleRail,
+ ContextProcessorRail,
+ SysOperationRail,
+ TaskCompletionRail,
+ VerificationContractRail,
+)
+from openjiuwen.harness.schema.config import SubAgentConfig
+
+
+async def main():
+ # The business side creates the model from its existing model configuration,
+ # such as OpenAI, Huawei Cloud, or a local model adapter.
+ model = ...
+
+ workspace = Workspace(root_path="./workspace", language="en")
+
+ research_agent = SubAgentConfig(
+ agent_card=AgentCard(
+ name="research_agent",
+ description="Research, fact-checking, and report-material specialist",
+ ),
+ system_prompt=(
+ "You are research_agent. Retrieve sources, cross-check facts, "
+ "and summarize evidence, citations, and uncertainties for the main Agent."
+ ),
+ model=model,
+ workspace=workspace,
+ )
+
+ agent = create_deep_agent(
+ model=model,
+ card=AgentCard(
+ name="deep_agent",
+ description="A general task execution agent that can plan, use tools, and delegate to sub-agents",
+ ),
+ workspace=workspace,
+ system_prompt=(
+ "You are a rigorous DeepAgent. First understand the goal, then plan the steps. "
+ "When needed, read and modify workspace files, call sub-agents, and reuse skills. "
+ "Finally, provide clear results, key evidence, and remaining risks."
+ ),
+ enable_task_loop=True,
+ enable_task_planning=True,
+ max_iterations=20,
+ subagents=[research_agent],
+ skills=["./skills"],
+ enable_skill_discovery=True,
+ permissions={
+ "enabled": True,
+ "permission_mode": "normal",
+ "tools": {
+ "read_file": "allow",
+ "write_file": "ask",
+ "edit_file": "ask",
+ "bash": "ask",
+ "code": "ask",
+ },
+ "defaults": {"*": "ask"},
+ "external_directory": {"*": "ask"},
+ },
+ rails=[
+ SysOperationRail(with_code_tool=True),
+ ContextAssembleRail(),
+ ContextProcessorRail(preset=True),
+ TaskCompletionRail(
+ max_rounds=8,
+ timeout_seconds=1200,
+ completion_promise="Complete the user goal and report completed items, unfinished items, and verification results.",
+ ),
+ AgentModeRail(),
+ VerificationContractRail(),
+ ],
+ )
+
+ session = create_agent_session(session_id="deep-agent-demo", card=agent.card)
+
+ run_task = asyncio.create_task(
+ agent.invoke(
+ {
+ "query": (
+ "Read the materials under ./workspace/input, prepare a technical proposal summary, "
+ "delegate key fact checks to research_agent when needed, and write the result to "
+ "./workspace/output/summary.md."
+ )
+ },
+ session=session,
+ )
+ )
+
+ # While the task is still running, you can append requirements, adjust direction,
+ # or terminate the task.
+ await asyncio.sleep(2)
+ await agent.follow_up("Add a risk-assessment section and mark assumptions that need human confirmation.", session=session)
+ await agent.steer("Prioritize factual accuracy; do not guess missing information for completeness.", session=session)
+ # await agent.abort(session=session)
+
+ result = await run_task
+ print(result.get("output", result))
+
+
+asyncio.run(main())
+```
+
+This example covers workspace, permission governance, task planning, context engineering, stop conditions, verification contracts, sub-agents, and skill discovery. `TaskPlanningRail`, `SubagentRail`, `SkillUseRail`, and the default safety Rail are automatically assembled by `create_deep_agent` based on configuration, so the example does not register them manually.
+
+## 6. Extension Development
+
+### 6.1 Custom Rail
+
+```python
+from openjiuwen.harness.rails import DeepAgentRail
+
+
+class AuditRail(DeepAgentRail):
+ priority = 70
+
+ async def before_tool_call(self, ctx):
+ tool_name = ctx.inputs.tool_name
+ tool_args = ctx.inputs.tool_args
+ # Record audit information.
+
+ async def after_task_iteration(self, ctx):
+ # Run after each outer task iteration.
+ ...
+```
+
+### 6.2 Custom Tool
+
+A custom tool needs a ToolCard and Tool execution logic. Tools can be connected in three ways:
+
+- Pass them as parameters to `create_deep_agent(tools=[...])`.
+- Register them into the ability manager during Rail initialization.
+- Declare a package or entry_point through `harness_config.yaml`.
+
+System-operation tools should connect to the shared `SysOperation` to avoid bypassing the execution boundary of Harness.
+
+### 6.3 Custom Sub-Agent
+
+Use `SubAgentConfig` to describe a sub-agent:
+
+```python
+SubAgentConfig(
+ agent_card=AgentCard(name="review_agent", description="Review specialist"),
+ system_prompt="You are responsible for independent review and feedback.",
+ tools=[...],
+ rails=[...],
+ enable_task_loop=False,
+)
+```
+
+### 6.4 Custom Stop Condition
+
+Implement `StopConditionEvaluator` for a custom stop condition, then pass it to `TaskCompletionRail(evaluators=[...])`. The outer loop evaluates all stop conditions with OR semantics.
+
+## 7. Code Directory
+
+The core Harness implementation is located at:
+
+```text
+openjiuwen/harness/
+ cli/ CLI entrypoints and command support
+ deep_agent.py DeepAgent runtime implementation
+ factory.py DeepAgent creation and assembly entrypoint
+ harness_config/ Declarative YAML configuration assembly
+ lsp/ LSP subsystem and language server management
+ prompts/ Prompt sections, tool descriptions, and dynamic attachments
+ rails/ Lifecycle extension mechanisms
+ resources/ Harness resource definitions
+ schema/ Config, state, task plan, stop-condition schemas
+ security/ Permission engine and security policies
+ subagents/ Built-in sub-agents
+ task_loop/ Outer task loop
+ tools/ Tool implementations
+ workspace/ Workspace schema and directory management
+```
diff --git a/docs/zh/Harness.md b/docs/zh/Harness.md
new file mode 100644
index 0000000000..2262fda523
--- /dev/null
+++ b/docs/zh/Harness.md
@@ -0,0 +1,623 @@
+# openJiuwen Harness
+
+## 简介
+
+`Agent = Model + Harness`,openJiuwen Harness 是 openJiuwen 面向 Harness Engineering(驾驭工程)的通用 Agent 运行框架。它负责把大模型从“可对话的推理单元”驾驭为“可规划、可调用工具、可管理上下文、可受权限约束、可委派子任务、可持续演进的任务执行系统”。
+
+Harness Engineering 的核心目标是构建模型之外的工程化执行环境。这个环境管理任务循环、工具、上下文、状态、权限、记忆、验证、停止条件和反馈回路,使 Agent 在长程任务和复杂环境中保持可控、可靠和可扩展。openJiuwen Harness 面向通用 Agent 场景;代码编辑、研究检索、浏览器操作、多模态任务、系统运维、团队协作和业务自动化都可以基于同一套 Harness 能力构建。
+
+
+## 一、概念定位
+
+### 1.1 Harness 是什么
+
+Harness 是 Agent 的工程控制层。模型负责生成推理和行动意图,Harness 负责把这些意图放入受控执行系统中。因此,openJiuwen Harness 关注的不是替代模型,而是补齐模型外部的执行、状态、约束和反馈机制:
+
+- 将用户目标转换为可持续推进的任务循环。
+- 将工具调用纳入统一注册、筛选、权限和审计体系。
+- 将上下文组织为静态 prompt、动态附件、会话状态和长期记忆。
+- 将执行过程切分为可扩展的生命周期事件。
+- 将复杂任务委派给子代理,并隔离子代理上下文。
+- 将完成条件、超时、轮数、token 预算和自定义谓词纳入统一停止策略。
+- 将安全、人机确认、计划模式和验证门控嵌入运行过程。
+
+### 1.2 DeepAgent 的职责
+
+`DeepAgent` 是 Harness 的运行时主体。它在内部复用 ReAct 推理循环,并在外层提供任务管理、状态管理、Rail 生命周期、子代理委派和基础设施接入。
+
+DeepAgent 具备两种运行形态:
+
+- 单轮 ReAct 模式:适合短任务和直接工具调用。
+- 双层 Task Loop 模式:适合长程任务、多轮推进、follow-up、steer、abort、任务计划和停止条件控制。
+
+### 1.3 Harness 的设计原则
+
+- 通用性:Harness 面向多类型 Agent 执行场景。
+- 可驾驭性:模型行动必须经过工具、权限、状态和停止条件治理。
+- 可扩展性:功能以 Rails、Tools、SubAgents、Stop Evaluators 扩展。
+- 可恢复性:任务状态、计划、pending follow-ups 和 plan mode 状态持久化到 session。
+- 可组合性:Workspace、SysOperation、Permission、PromptAttachment、ContextEngine 等基础设施解耦组合。
+- 可演进性:轨迹采集、Skill 使用、Skill 创建和 Skill 演进形成经验反馈闭环。
+
+## 二、总体架构
+
+![openJiuwen Harness Architecture](../assets/images/harness_arch.png)
+
+架构由三层组成:
+
+- DeepAgent Core Engine:任务循环、停止条件、ReAct 推理、子代理委派和 Rail 生命周期。
+- Extension:Subagent、Tool、Rails、Stop-Condition 四类扩展面。
+- Infrastructure:系统操作抽象、会话管理和权限控制。
+
+## 三、核心特性
+
+### 3.1 Agent Loop:从 ReAct 到双层任务循环
+
+Agent Loop 是 Harness 的执行主干。内层 ReAct 循环负责 Think -> Act -> Observe,外层 Task Loop 负责把开放目标推进为多轮任务执行、状态更新和完成判断。
+
+外层循环由 `TaskLoopController`、`LoopCoordinator`、`TaskLoopEventHandler`、`TaskLoopEventExecutor` 和 `LoopQueues` 组成。它把每轮任务提交为 `CoreTask`,等待执行完成后评估是否进入下一轮。运行中交互也挂在这一层:
+
+- `follow_up`:把用户补充问题排入下一轮。
+- `steer`:向当前任务注入运行时引导。
+- `abort`:取消外层 task loop,并尝试中断内层 ReAct。
+
+任务规划和任务完成是 Agent Loop 的两个关键能力:
+
+- `TaskPlanningRail` 将 todo 工具、`TaskPlan`、任务依赖、进度提醒和多模型选择接入循环。
+- `TaskCompletionRail` 统一承载最大轮数、超时、完成承诺、多次确认和自定义停止条件。
+
+停止条件采用 OR 语义,任意条件满足即可停止。内置 evaluator 包括 `MaxRoundsEvaluator`、`TimeoutEvaluator`、`TokenBudgetEvaluator`、`CompletionPromiseEvaluator` 和 `CustomPredicateEvaluator`。
+
+### 3.2 Rail Lifecycle:生命周期扩展机制
+
+Rail 是 Harness 的生命周期扩展面。它不替换主流程,而是在外层任务、内层模型调用和工具调用的关键节点注入能力。
+
+| 生命周期事件 | 位置 | 典型用途 |
+| --- | --- | --- |
+| `before_invoke` / `after_invoke` | 外层 DeepAgent | 初始化资源、加载 skills、清理缓存、同步外部记忆 |
+| `before_task_iteration` / `after_task_iteration` | 外层 Task Loop | 改写任务指令、同步 todo、检测完成承诺、触发演进 |
+| `before_model_call` / `after_model_call` | 内层 ReAct | 组装 prompt、注入安全规则、压缩上下文、采集轨迹 |
+| `before_tool_call` / `after_tool_call` | 内层 ReAct | 权限判断、plan mode 限制、LSP 诊断、进度提醒 |
+| `on_model_exception` / `on_tool_exception` | 内层 ReAct | 修复上下文、治理异常 |
+
+下图展示了 Harness lifecycle hooks 的四类典型应用:动态上下文工程、自我反思、LSP 诊断和 Prompt Injection。
+
+![Hook System Use Cases](../assets/images/hook_system_use_cases.png)
+
+常用 Rail 可以按能力族理解:
+
+- 执行控制:`TaskPlanningRail`、`TaskCompletionRail`、`AgentModeRail`。
+- 工具与系统:`SysOperationRail`、`ProgressiveToolRail`、`LspRail`、`McpRail`。
+- 安全与权限:`SafetyPromptRail`、`PermissionInterruptRail`、`VerificationRail`。
+- 上下文与记忆:`ContextAssembleRail`、`ContextProcessorRail`、`MemoryRail`、`CodingMemoryRail`、`ExternalMemoryRail`。
+- 技能与演进:`SkillUseRail`、`EvolutionRail`、`SkillCreateRail`、`SkillEvolutionRail`。
+- 子代理:`SubagentRail`、`VerificationContractRail`。
+
+### 3.3 Tool & Execution:工具、系统操作与权限治理
+
+Tool & Execution 是 Harness 把模型意图落到真实环境的执行层。工具通过 ToolCard 描述元数据,通过 Tool 实例执行动作,并由 ability manager 统一暴露给模型。
+
+主要工具族包括:
+
+- 文件系统:`read_file`、`write_file`、`edit_file`、`glob`、`list_files`、`grep`。
+- Shell 和代码执行:`bash`、`powershell`、`code`。
+- Web 和外部资源:web search/fetch、MCP resource list/read。
+- 多模态:OCR、VQA、音频转写、音频问答、音频元数据、视频理解。
+- 代码智能:LSP definition/reference/symbol/call hierarchy。
+- 工作流辅助:todo、skill、subagent、plan mode、worktree、cron、`ask_user`。
+
+更完整的内置工具索引见 [3.12 内置能力索引](#312-内置能力索引)。
+
+`SysOperationRail` 注册文件、Shell 和可选 code tool。所有系统操作通过共享 `SysOperation` 执行,从而统一接入本地执行、沙箱、工作目录限制和权限控制。
+
+当工具集很大时,`ProgressiveToolRail` 负责渐进式工具暴露:模型默认只看到元工具和基础工具,再通过 `tool_search` 搜索工具,通过 `load_tools` 加载当前 session 需要的工具,减少上下文噪声和误用高风险工具的概率。
+
+权限治理由 `PermissionEngine` 和 `PermissionInterruptRail` 组成。`PermissionEngine` 负责 allow / ask / deny 策略评估,`PermissionInterruptRail` 负责在工具调用前拦截所有工具,执行直接放行、直接拒绝、HITL 确认、“允许一次”和“总是允许”等流程。Shell 命令会经过 AST 分析,外部路径检查会与当前决策取更严格结果。
+
+Workspace 是执行层的落盘边界。默认 schema 包括 `AGENT.md`、`SOUL.md`、`HEARTBEAT.md`、`IDENTITY.md`、`memory/`、`coding_memory/`、`todo/`、`messages/`、`skills/`、`agents/` 和 `context/session_memory.md`,并支持自定义目录、语言化默认内容、团队 workspace 链接和 worktree 链接。
+
+### 3.4 Agent Mode:运行模式与 Plan Mode 约束
+
+不同 mode 是 Harness 在工具执行层上的重要运行时策略。`AgentMode` 当前支持 `normal` 和 `plan` 两种模式:`normal` 是默认执行模式,允许常规工具调用和任务执行;`plan` 是只读规划模式,用于先理解、探索和设计方案,再进入实际执行。mode 状态存储在 session 级 `DeepAgentState.plan_mode` 中,包含 `mode`、`pre_plan_mode` 和 `plan_slug`。
+
+`AgentModeRail` 负责注册 `switch_mode`、`enter_plan_mode` 和 `exit_plan_mode`,并在 model/tool 生命周期中注入模式提示、过滤模型可见工具、拦截不符合当前 mode 的工具调用。
+
+| 能力 | 机制 |
+| --- | --- |
+| 模式切换 | `switch_mode` 在 `normal` 和 `plan` 之间切换 session 运行模式 |
+| Plan 文件初始化 | `enter_plan_mode` 在 workspace 下创建或复用 `.plans/<slug>.md` |
+| Plan 结束 | `exit_plan_mode` 读取计划全文并恢复进入 plan 前的模式 |
+| 工具可见性 | `plan` 模式隐藏 todo/session tools;`normal` 模式隐藏 `enter_plan_mode` / `exit_plan_mode` |
+| 写操作约束 | `plan` 模式禁止 shell/git 写操作,避免规划阶段修改真实工作区 |
+| 文件写入约束 | `write_file` / `edit_file` 在 `plan` 模式下只能写 plan 文件 |
+| 子代理协同 | 进入 `plan` 模式后可动态注册 `task_tool`,支持规划期委派子代理探索问题 |
+
+### 3.5 Context & Memory:上下文工程与记忆系统
+
+Context & Memory 负责让模型在合适的时间看到合适的信息。Harness 使用扩展版 `SystemPromptBuilder` 管理静态 prompt section,例如 `identity`、`safety`、`skills`、`tools`、`todo`、`task_tool`、`memory`、`workspace`、`context`、`completion_signal` 和 `verification_contract`。
+
+动态上下文由 `PromptAttachmentManager` 管理。它按 session 和 section 存储本次模型调用自动附加的内容,并以独立 user message 注入最终上下文窗口。适合放入 PromptAttachment 的内容包括长上下文文件、heartbeat、memory recall、external memory prefetch、plan mode 动态状态和 LSP diagnostics。
+
+上下文工程主要由两个 Rail 承担:
+
+- `ContextAssembleRail`:注入 workspace 结构、context 文件、工具列表和文件型 prompt section。
+- `ContextProcessorRail`:配置 ContextEngine processors,压缩长对话,offload 大工具结果,修复不完整 tool_call / ToolMessage 上下文,并管理 session memory。
+
+记忆系统覆盖三类场景:
+
+| Rail | 场景 | 能力 |
+| --- | --- | --- |
+| `MemoryRail` | 通用长期记忆 | 初始化 memory manager,注册 memory tools,注入记忆使用 prompt |
+| `CodingMemoryRail` | 项目/任务记忆 | 自动召回、向量 + BM25 混合检索、注入 top5 记忆或索引 |
+| `ExternalMemoryRail` | 外部记忆系统 | provider 初始化、prefetch、sync_turn、provider 工具注册和熔断 |
+
+### 3.6 Subagent & Verification:子代理委派与验证门控
+
+Subagent & Verification 负责把复杂任务拆给更专门的 Agent,并在高风险实现后引入独立验证。
+
+同步子代理通过 `task_tool` 委派:
+
+- 主代理调用工具后等待子代理完成。
+- 子代理使用隔离 session。
+- `browser_agent` 和 `verification_agent` 使用确定性子 session id,支持失败后复用上下文。
+
+异步子代理通过 session tools 委派:
+
+- `sessions_spawn` 提交后台子任务。
+- `sessions_list` 查看后台任务。
+- `sessions_cancel` 取消后台任务。
+- `SessionToolkit` 维护后台任务状态。
+
+公开导出的内置子代理包括 `browser_agent`、`code_agent`、`research_agent`、`verification_agent` 和 `mobile_gui_agent`;`general-purpose` 可由 `add_general_purpose_agent=True` 自动注入。完整清单见 [3.12 内置能力索引](#312-内置能力索引)。
+
+验证门控分为父代理和验证代理两层:`VerificationContractRail` 注入父代理验证契约,要求非平凡实现后启动验证代理;`VerificationRail` 限制验证代理的工具集合和写权限,确保验证代理只读取、检索、运行验证命令和报告结果,不修改项目文件。
+
+### 3.7 Skill & Evolution:技能复用和经验演进
+
+Skill & Evolution 让 Harness 不只执行当前任务,还能把执行经验沉淀为可复用能力。
+
+`SkillUseRail` 管理 Skill 使用:
+
+- 支持多个 `skills_dir`。
+- 支持 `all` 和 `auto_list` 两种暴露模式。
+- 注册 `skill_tool` 和 `list_skill`。
+- 支持 enabled/disabled 过滤。
+- 按 `SKILL.md` mtime 增量刷新。
+- 可把演进经验注入 skill 描述。
+
+`EvolutionRail` 管理经验采集和演进触发:
+
+- 采集模型调用轨迹。
+- 采集工具调用轨迹。
+- 支持 after_invoke、after_model_call、after_tool_call、after_task_iteration 触发。
+- 支持后台异步演进、并发限制和 host-visible event。
+
+具体演进能力包括 `SkillCreateRail`、`TeamSkillCreateRail`、`SkillEvolutionRail`、`TeamSkillEvolutionRail` 和 `TrajectoryRail`。它们可以基于工具调用模式、团队协作模式和历史轨迹提出新 skill、演进已有 skill 或沉淀团队技能。
+
+### 3.8 Coding Intelligence:LSP 代码智能
+
+LSP 即 Language Server Protocol,是编辑器或开发工具与语言服务器之间的标准协议。它用统一接口提供定义跳转、引用查找、符号索引、实现定位、调用层级和诊断等代码智能能力。Harness 通过 LSP 把“编辑器级代码理解能力”暴露给 Agent,使 Agent 在 coding 场景中不只依赖文本搜索,还能基于语言服务器理解代码结构。
+
+#### LSP Tool 与 Rail
+
+`LspRail` 是 LSP 能力的生命周期入口,负责初始化 LSP subsystem,并把单一 `lsp` tool 注册到 Agent 的 ability manager。`lsp` tool 是模型调用 LSP 能力的统一工具入口,负责接收操作类型、文件路径、行列位置或查询条件,并返回结构化结果。
+
+`LspRail` 会根据 Agent 的 workspace、sys_operation、language 和 agent_id 创建工具,使 LSP 能力与当前工作区和 Agent 运行时绑定。Agent 结束或 Rail 卸载时,`LspRail` 会移除工具并关闭 LSP subsystem。
+
+#### 代码智能操作
+
+`lsp` tool 支持 8 类代码智能操作:
+
+| 操作 | 用途 |
+| --- | --- |
+| `goToDefinition` | 查找符号定义位置 |
+| `findReferences` | 查找符号引用 |
+| `documentSymbol` | 获取单文件函数、类、变量等符号 |
+| `workspaceSymbol` | 在整个 workspace 中搜索符号 |
+| `goToImplementation` | 查找接口或抽象方法的实现 |
+| `prepareCallHierarchy` | 获取当前位置的调用层级条目 |
+| `incomingCalls` | 查找调用当前函数的方法 |
+| `outgoingCalls` | 查找当前函数调用的方法 |
+
+#### 代码诊断
+
+LSP 不只支持代码导航,也支持代码诊断。Agent 修改文件后,`LspRail` 会触发语言服务器重新分析;下一轮模型调用前,Harness 会把诊断信息注入上下文。诊断内容可以帮助 Agent 发现语法错误、类型错误、未解析引用、调用签名不匹配等问题,形成“修改 -> 诊断 -> 修复”的 coding 闭环。
+
+### 3.9 Multimodal Understanding:多模态理解
+
+Harness 将图像、音频和视频理解封装为工具,使 Agent 可以把非文本资料纳入任务上下文。
+
+图像工具由 `VisionModelConfig` 驱动:
+
+- `image_ocr`:提取图片中的文字,尽量保留结构、换行、数字和符号。
+- `visual_question_answering`:结合图片和可选 OCR 结果回答视觉问题。
+- 支持本地图片路径和 HTTP(S) 图片 URL;sandbox-only 路径会被拒绝,避免工具访问不可见路径。
+
+音频工具由 `AudioModelConfig` 驱动:
+
+- `audio_transcription`:将音频语音转写为文本。
+- `audio_question_answering`:基于音频内容回答问题。
+- `audio_metadata`:识别音频时长,并在配置 ACR 时识别歌曲元数据。
+- 支持本地音频路径和 HTTP(S) 音频 URL,并通过 `max_audio_bytes` 限制下载大小。
+
+视频工具由视觉模型驱动:
+
+- `video_understanding`:接收本地视频或 HTTP(S) 视频 URL,并围绕用户 query 进行视频内容问答。
+- 本地视频会编码为 data URL;调用时可限制 `max_tokens`、`temperature` 和 `timeout_seconds`。
+
+多模态模型配置可以显式传入,也可以通过环境变量构造:`VisionModelConfig.from_env()` 会读取 `VISION_API_KEY`、`VISION_BASE_URL`、`VISION_MODEL` 等变量;`AudioModelConfig.from_env()` 会读取 `AUDIO_API_KEY`、`AUDIO_BASE_URL`、`AUDIO_TRANSCRIPTION_MODEL`、`AUDIO_QUESTION_ANSWERING_MODEL` 等变量。`enable_read_image_multimodal=True` 时,`read_file` 读取图片可以附带原生多模态输入;关闭后只返回图片元数据并提示使用视觉工具。
+
+### 3.10 Web & External Resources:网页与外部资源接入
+
+Harness 支持把 Web 和外部资源系统接入 Agent 的任务循环,使 Agent 能执行研究检索、网页获取和跨系统资料读取。
+
+Web 工具覆盖两类场景:
+
+- `free_search` / `paid_search`:面向搜索和研究任务,可按部署环境使用免费或付费搜索 provider。
+- `fetch_webpage`:获取网页内容,支持资料整理、引用核对和后续上下文注入。
+
+MCP 通过 `McpRail` 接入。DeepAgent 可以通过 `mcps` 注册 MCP server;初始化后,`McpRail` 向 Agent 注册 `list_mcp_resources` 和 `read_mcp_resource`,让模型能发现并读取已经注册的 MCP resources。该能力适合把文档库、数据源、工具运行时或外部系统资源接入 Harness,而不需要把所有内容预先塞进 prompt。
+
+### 3.11 Isolated Execution & Automation:隔离执行与长期自动化
+
+Harness 还提供隔离执行和长期自动化相关工具,使 Agent 能在更接近真实工程环境的约束下运行。
+
+`WorktreeRail` 注册 `enter_worktree` 和 `exit_worktree`,并管理 per-agent 的 `WorktreeManager`。Agent 可以进入隔离 git worktree 后执行文件操作和 shell 命令,避免污染主工作区;退出时可以选择保留或移除 worktree。该能力适合代码修改、实验性重构、并行验证和需要隔离回滚边界的任务。
+
+Cron tool 族用于对接宿主提供的定时任务 backend。它可以暴露统一 `cron` 工具,也可以兼容拆分后的 list/get/create/update/delete/toggle/preview 等工具形态。该能力适合长期自动化、周期任务、定时提醒和需要跨 session 维持计划的 agent 工作流。
+
+### 3.12 内置能力索引
+
+本节用表格快速索引 Harness 的预置能力。表格中的能力是否实际出现在模型可调用工具列表中,取决于 `create_deep_agent` 参数、显式注册的 rails、宿主提供的 backend、权限配置和当前运行环境。
+
+#### 内置 Tool
+
+| 能力族 | 代表工具 | 主要用途 | 启用方式 / 来源 |
+| --- | --- | --- | --- |
+| 文件与搜索 | `read_file`、`write_file`、`edit_file`、`glob`、`grep`、`list_files` | 读取、写入、编辑文件,按 glob 或文本搜索定位上下文 | `SysOperationRail` 注册;受 workspace、sys_operation 和权限策略约束 |
+| Shell 与代码执行 | `bash`、`powershell`、`code` | 执行 shell 命令、脚本和代码片段 | `SysOperationRail` 注册;`powershell` 主要用于 Windows 环境,`code` 可按 rail 配置启用 |
+| Todo / 计划 | `todo_create`、`todo_list`、`todo_get`、`todo_modify` | 管理 session 级任务清单,支撑长任务拆解、追踪和状态更新 | 工具实例由相关 rail 或宿主配置挂载,落盘到 workspace 的 todo 区域 |
+| Web | `free_search`、`paid_search`、`fetch_webpage` | 网络检索、网页抓取和资料整理 | Web tool 注册后可用;搜索工具受部署环境和 provider 配置影响 |
+| 多模态 | `image_ocr`、`visual_question_answering`、`audio_transcription`、`audio_question_answering`、`audio_metadata`、`video_understanding` | 图片 OCR / VQA、音频转写 / 问答 / 元数据识别、视频内容问答 | 依赖 `VisionModelConfig` / `AudioModelConfig`,支持本地或 HTTP(S) 媒体 |
+| LSP 代码智能 | `lsp` | 定义跳转、引用查找、符号检索、实现定位、调用层级和代码诊断 | `LspRail` 注册;依赖语言服务器和当前 workspace |
+| MCP 外部资源 | `list_mcp_resources`、`read_mcp_resource` | 枚举和读取已注册 MCP server 的资源 | `McpRail` 注册;DeepAgent 通过 `mcps` 接入 MCP server |
+| 子代理委派 | `task_tool`、`sessions_spawn`、`sessions_list`、`sessions_cancel` | 同步委派子任务,或异步创建、查看、取消后台子代理任务 | `SubagentRail` 注册;同步 / 异步形态由 `enable_async_subagent` 决定 |
+| Skill / 工具发现 | `skill_tool`、`list_skill`、`search_tools`、`load_tools` | 调用技能、查看技能,渐进式搜索和加载工具 | `SkillUseRail`、`ProgressiveToolRail` 注册;受 skill 目录和渐进工具配置影响 |
+| Memory / Coding Memory | `memory_*`、`coding_memory_*` | 读写长期记忆、代码经验和项目知识 | `MemoryRail`、`CodingMemoryRail` 等 memory rails 注册 |
+| Agent 模式与用户交互 | `switch_mode`、`enter_plan_mode`、`exit_plan_mode`、`ask_user` | 切换普通 / 计划模式,在关键分支向用户发起确认或补充输入 | `AgentModeRail`、`AskUserRail` 或宿主交互 rail 注册 |
+| Worktree / Cron | `enter_worktree`、`exit_worktree`、`cron`、`cron_list_jobs`、`cron_get_job`、`cron_create_job`、`cron_update_job`、`cron_delete_job`、`cron_toggle_job`、`cron_preview_job` | 进入隔离 git worktree,管理定时任务和周期自动化 | `WorktreeRail` 或宿主 cron backend 注册;cron 兼容统一工具和拆分工具两种形态 |
+| 移动 GUI / 浏览器运行时 | 移动坐标 / 导航工具、浏览器运行时工具 | 移动端 GUI 操作、浏览器任务运行、运行时健康检查和取消控制 | 由移动 GUI / browser runtime 相关工具包或宿主 runtime 注册 |
+
+#### 内置 Rail
+
+| Rail | 主要职责 | 启用方式 / 来源 |
+| --- | --- | --- |
+| `SecurityRail` | 基础安全防护和 prompt/tool 安全检查 | `create_deep_agent` 默认自动补齐,除非用户提供同类 rail |
+| `TaskPlanningRail` | 任务规划、拆解和模型选择辅助 | `enable_task_planning=True` 时自动补齐,也可显式注册 |
+| `SkillUseRail` | 发现、装载和调用 workspace / team skills | 配置 `skills` 或 `enable_skill_discovery=True` 时自动补齐,也可显式注册 |
+| `SubagentRail` | 注册同步 `task_tool` 或异步 session tools,注入子代理提示 | 配置 `subagents` 时自动补齐;异步模式由 `enable_async_subagent` 控制 |
+| `SysOperationRail` | 注册文件、Shell、代码执行等系统操作工具 | 通常作为显式 rail 注册,绑定 workspace 和 sys_operation |
+| `ProgressiveToolRail` | 渐进式工具暴露、工具搜索和按需加载 | 配置 progressive tool 能力时注册 |
+| `LspRail` | 初始化 LSP subsystem,注册 `lsp` tool,注入代码诊断 | 显式注册;依赖语言服务器和工作区代码 |
+| `McpRail` | 注册 MCP resource list/read 工具 | 显式注册,并配合 `mcps` 接入 MCP server |
+| `MemoryRail`、`CodingMemoryRail`、`ExternalMemoryRail` | 管理长期记忆、代码记忆和外部记忆读写 | 按记忆能力需要显式注册或由宿主装配 |
+| `AgentModeRail` | 管理 normal / plan 等 agent mode,注册模式切换工具,并执行 Plan Mode 工具过滤和写操作约束 | 启用 plan mode 或显式注册;详细机制见 3.4 的 Agent Mode 段落 |
+| `AskUserRail`、`ConfirmInterruptRail` | 在任务执行中发起用户输入、确认或中断恢复 | HITL 场景显式注册或由宿主交互能力装配 |
+| `PermissionInterruptRail`、`SafetyPromptRail` | 工具调用权限拦截、用户确认和安全提示注入 | 配置 `permissions` 或安全 rail 时启用 |
+| `TaskCompletionRail` | 判断任务是否完成,配合外层 Task Loop 停止条件 | Task Loop 或自定义完成判定场景注册 |
+| `SessionRail` | 管理子 session 相关上下文和生命周期能力 | 子代理 / session 场景注册 |
+| `VerificationRail`、`VerificationContractRail` | 约束验证代理权限,向父代理注入验证契约 | 代码实现、回归验证和高风险任务场景注册 |
+| `SkillCreateRail`、`TeamSkillCreateRail`、`SkillEvolutionRail`、`TeamSkillEvolutionRail` | 创建个人 / 团队技能,沉淀和演进技能经验 | 技能自演进或团队技能沉淀场景注册 |
+| `TrajectoryRail`、`ContextEvolutionRail`、`EvolutionRail`、`EvolutionInterruptRail` | 采集轨迹、触发上下文/技能演进,并处理演进中断 | 经验演进链路注册 |
+| `HeartbeatRail` | 运行心跳和持续任务状态维护 | 长任务或宿主需要心跳感知时注册 |
+
+#### 内置 Subagent
+
+| Subagent | 主要用途 | 启用方式 / 来源 |
+| --- | --- | --- |
+| `browser_agent` | 浏览器使用、网页交互和需要浏览器 runtime 的子任务 | `build_browser_agent_config` / `create_browser_agent` |
+| `code_agent` | 代码阅读、修改、局部实现和工程化处理 | `build_code_agent_config` / `create_code_agent` |
+| `research_agent` | 搜索研究、资料整理、信息核验和报告汇总 | `build_research_agent_config` / `create_research_agent` |
+| `verification_agent` | 读取变更、运行验证命令、检查回归风险并输出验证结论 | `build_verification_agent_config` / `create_verification_agent` |
+| `mobile_gui_agent` | 移动端 GUI 操作、坐标点击、滚动、输入和导航 | `build_mobile_gui_agent_config` / `create_mobile_gui_agent` |
+| `general-purpose` | 通用任务兜底子代理,可继承主代理的 prompt、tools、mcps 和 skills | `add_general_purpose_agent=True` 时由 factory 自动注入 |
+| `plan_agent`、`explore_agent` | 源码中存在的计划 / 探索类子代理模板 | 内部模板 / 非当前 `subagents/__init__.py` 主导出入口,使用时应以具体装配代码为准 |
+
+## 四、运行流程
+
+### 4.1 运行时时序图
+
+```mermaid
+sequenceDiagram
+ autonumber
+ participant Caller as 调用方
+ participant DA as DeepAgent.invoke/stream
+ participant Rail as Rail Lifecycle
+ participant Controller as TaskLoopController
+ participant Handler as TaskLoopEventHandler
+ participant Executor as TaskLoopEventExecutor
+ participant RA as ReActAgent
+ participant MT as Model/Tools
+ participant Coord as LoopCoordinator
+ participant State as Session/State
+
+ Caller->>DA: invoke/stream(inputs, session)
+ DA->>DA: _ensure_initialized()
+ DA->>State: load DeepAgentState
+ DA->>Rail: before_invoke
+ alt Single-round mode
+ DA->>RA: invoke/stream effective inputs
+ RA->>Rail: before_model_call / before_tool_call
+ RA->>MT: model reasoning and tool execution
+ MT-->>RA: observations and final answer
+ RA->>Rail: after_tool_call / after_model_call
+ RA-->>DA: result or stream chunks
+ else Task Loop mode
+ DA->>Controller: submit round task
+ Controller->>Handler: dispatch INPUT event
+ Handler->>Executor: execute task iteration
+ Executor->>Rail: before_task_iteration
+ Executor->>RA: invoke internal ReAct
+ RA->>Rail: before_model_call / before_tool_call
+ RA->>MT: model reasoning and tool execution
+ MT-->>RA: observations and final answer
+ RA->>Rail: after_tool_call / after_model_call
+ RA-->>Executor: iteration result
+ Executor->>Rail: after_task_iteration
+ Executor->>Coord: evaluate stop conditions
+ alt continue
+ Coord-->>Controller: continue with follow_up / steer / async results
+ else stop or abort
+ Coord-->>DA: finish current loop
+ end
+ end
+ DA->>State: save session state
+ DA->>Rail: after_invoke
+ DA-->>Caller: output
+```
+
+### 4.2 运行时主流程说明
+
+4.1 的时序图只描述 Agent 已经创建后的运行过程。主流程按以下顺序推进:
+
+1. 调用方通过 `invoke/stream(inputs, session)` 发起一次运行。
+2. `DeepAgent.invoke` 或 `DeepAgent.stream` 先执行 `_ensure_initialized()`,确保 workspace、MCP、Rails 和 task loop 运行时对象已经初始化。
+3. DeepAgent 从 session 中加载 `DeepAgentState`,并触发 `before_invoke` 生命周期事件。
+4. 如果是单轮模式,DeepAgent 直接调用内部 `ReActAgent`。内部 ReAct 在模型推理和工具执行前后触发 model/tool 级 Rail 钩子,然后把结果或流式 chunk 返回给 DeepAgent。
+5. 如果是 Task Loop 模式,DeepAgent 将当前轮次提交给 `TaskLoopController`;事件经 `TaskLoopEventHandler` 分发后,由 `TaskLoopEventExecutor` 执行本轮任务迭代。
+6. Task Loop 每轮迭代会先触发 `before_task_iteration`,再调用内部 `ReActAgent` 完成 Think / Act / Observe,随后触发 `after_task_iteration`。
+7. `LoopCoordinator` 根据停止条件、abort 信号、follow-up、steer 或异步子代理结果决定继续下一轮、停止或中止。
+8. 运行结束时,DeepAgent 保存 session state,触发 `after_invoke`,并向调用方返回 output。
+
+
+### 4.3 单轮模式与 Task Loop 模式
+
+单轮模式强调低开销和即时响应。它适合单次问答、短工具任务,以及不需要计划和多轮推进的自动化。虽然没有外层 Task Loop,Rails 的 `before_model_call`、`after_model_call`、`before_tool_call`、`after_tool_call` 等内层钩子仍然生效。
+
+Task Loop 模式强调长程推进和可控收敛。它适合任务计划、异步子代理、运行中干预、明确完成条件和需要多轮验证的执行场景。相比单轮模式,Task Loop 额外维护轮次状态、停止条件和运行中交互队列,使 Agent 能在多个迭代中持续逼近目标。
+
+### 4.4 运行中交互与状态
+
+Harness 支持三类运行中交互:
+
+- `follow_up(query)`:向当前任务追加后续问题。
+- `steer(message)`:向运行中的 Agent 注入指导信息。
+- `abort()`:取消当前任务循环。
+
+这些交互会通过 controller 的事件队列进入 `TaskLoopEventHandler`,再写入 `LoopQueues` 或触发 `LoopCoordinator.request_abort()`。Task Loop 在每轮迭代边界消费这些信号,因此可以在不中断当前工具调用一致性的前提下追加需求、调整方向或结束任务。
+
+DeepAgent 的运行状态保存在 session 中,核心状态包括:
+
+- 当前迭代次数。
+- TaskPlan。
+- stop condition state。
+- pending follow-ups。
+- plan mode state。
+
+状态保存使长程任务可以恢复,也使外部运行时可以读取当前任务进展。`invoke` 和 `stream` 在完成后会保存并清理本次运行状态;如果仍有异步 session-spawn 子任务未完成,controller 会保留以等待后续结果。
+
+## 五、使用指导
+
+### 5.1 适用场景
+
+Harness 适用于:
+
+- 长程任务执行。
+- 多工具自动化。
+- 多轮研究和资料整理。
+- 浏览器任务。
+- 多模态理解。
+- 代码语义导航、引用分析、调用链分析和修改后诊断。
+- 文件和系统操作。
+- Web/MCP 外部资料接入。
+- 隔离 worktree 中的代码修改和验证。
+- 定时任务和周期任务自动化。
+- 需要权限确认的高风险任务。
+- 需要计划、验证和回滚的任务。
+- 需要记忆、Skill 复用和自演进的 Agent。
+
+### 5.2 完整 DeepAgent 示例
+
+下面示例中的 `AgentModeRail()` 用于启用 mode 能力;未显式传入 `default_mode` 时,Agent 默认从 `AgentMode.NORMAL` / `normal` 模式开始。
+
+```python
+import asyncio
+
+from openjiuwen.core.single_agent import AgentCard, create_agent_session
+from openjiuwen.harness import Workspace, create_deep_agent
+from openjiuwen.harness.rails import (
+ AgentModeRail,
+ ContextAssembleRail,
+ ContextProcessorRail,
+ SysOperationRail,
+ TaskCompletionRail,
+ VerificationContractRail,
+)
+from openjiuwen.harness.schema.config import SubAgentConfig
+
+
+async def main():
+ # model 由业务侧按现有模型配置创建,例如 OpenAI、华为云或本地模型适配器。
+ model = ...
+
+ workspace = Workspace(root_path="./workspace", language="cn")
+
+ research_agent = SubAgentConfig(
+ agent_card=AgentCard(
+ name="research_agent",
+ description="资料检索、信息核对和报告素材整理专家",
+ ),
+ system_prompt=(
+ "你是 research_agent,负责检索资料、交叉核对事实,"
+ "并把证据、出处和不确定性整理给主 Agent。"
+ ),
+ model=model,
+ workspace=workspace,
+ )
+
+ agent = create_deep_agent(
+ model=model,
+ card=AgentCard(
+ name="deep_agent",
+ description="可计划、可调用工具、可委派子代理的通用任务执行智能体",
+ ),
+ workspace=workspace,
+ system_prompt=(
+ "你是一个严谨的 DeepAgent。你需要先理解目标,再规划步骤,"
+ "必要时读取和修改工作区文件、调用子代理、复用 skill,"
+ "最后给出清晰的执行结果、关键证据和剩余风险。"
+ ),
+ enable_task_loop=True,
+ enable_task_planning=True,
+ max_iterations=20,
+ subagents=[research_agent],
+ skills=["./skills"],
+ enable_skill_discovery=True,
+ permissions={
+ "enabled": True,
+ "permission_mode": "normal",
+ "tools": {
+ "read_file": "allow",
+ "write_file": "ask",
+ "edit_file": "ask",
+ "bash": "ask",
+ "code": "ask",
+ },
+ "defaults": {"*": "ask"},
+ "external_directory": {"*": "ask"},
+ },
+ rails=[
+ SysOperationRail(with_code_tool=True),
+ ContextAssembleRail(),
+ ContextProcessorRail(preset=True),
+ TaskCompletionRail(
+ max_rounds=8,
+ timeout_seconds=1200,
+ completion_promise="完成用户目标,并说明已完成项、未完成项和验证结果。",
+ ),
+ AgentModeRail(),
+ VerificationContractRail(),
+ ],
+ )
+
+ session = create_agent_session(session_id="deep-agent-demo", card=agent.card)
+
+ run_task = asyncio.create_task(
+ agent.invoke(
+ {
+ "query": (
+ "阅读 ./workspace/input 目录中的资料,整理一份技术方案摘要,"
+ "必要时委派 research_agent 核对关键事实,并把结果写入 "
+ "./workspace/output/summary.md。"
+ )
+ },
+ session=session,
+ )
+ )
+
+ # 任务仍在运行时,可以追加需求、调整方向或终止任务。
+ await asyncio.sleep(2)
+ await agent.follow_up("补充一节风险评估,并标出需要人工确认的假设。", session=session)
+ await agent.steer("优先保证事实准确性,不要为了完整性臆测缺失资料。", session=session)
+ # await agent.abort(session=session)
+
+ result = await run_task
+ print(result.get("output", result))
+
+
+asyncio.run(main())
+```
+
+这个示例同时覆盖了 workspace、权限治理、任务规划、上下文工程、停止条件、验证契约、子代理和 skill 发现。`TaskPlanningRail`、`SubagentRail`、`SkillUseRail` 和默认安全 Rail 会由 `create_deep_agent` 根据配置自动装配,因此示例中不再重复手动注册。
+
+## 六、扩展开发
+
+### 6.1 自定义 Rail
+
+```python
+from openjiuwen.harness.rails import DeepAgentRail
+
+class AuditRail(DeepAgentRail):
+ priority = 70
+
+ async def before_tool_call(self, ctx):
+ tool_name = ctx.inputs.tool_name
+ tool_args = ctx.inputs.tool_args
+ # 记录审计信息
+
+ async def after_task_iteration(self, ctx):
+ # 每个外层任务轮次结束后执行
+ ...
+```
+
+### 6.2 自定义 Tool
+
+自定义工具需要提供 ToolCard 和 Tool 执行逻辑。工具可以通过三种方式接入:
+
+- 作为参数传给 `create_deep_agent(tools=[...])`。
+- 在 Rail 初始化时注册到 ability manager。
+- 通过 `harness_config.yaml` 声明 package 或 entry_point。
+
+系统操作类工具应接入共享 `SysOperation`,避免绕过 Harness 的执行边界。
+
+### 6.3 自定义子代理
+
+子代理使用 `SubAgentConfig` 描述:
+
+```python
+SubAgentConfig(
+ agent_card=AgentCard(name="review_agent", description="审查专家"),
+ system_prompt="你负责独立审查和反馈。",
+ tools=[...],
+ rails=[...],
+ enable_task_loop=False,
+)
+```
+
+### 6.4 自定义停止条件
+
+自定义停止条件实现 `StopConditionEvaluator`,并传入 `TaskCompletionRail(evaluators=[...])`。外层循环按 OR 语义统一评估。
+
+## 七、代码目录
+
+Harness 的核心实现位于:
+
+```text
+openjiuwen/harness/
+ cli/ CLI 相关入口和命令支持
+ deep_agent.py DeepAgent 运行时主体
+ factory.py DeepAgent 创建和装配入口
+ harness_config/ 声明式 YAML 配置装配
+ lsp/ LSP subsystem 与语言服务器管理
+ prompts/ Prompt section、工具描述和动态附件
+ rails/ 生命周期扩展机制
+ resources/ Harness 资源定义
+ schema/ 配置、状态、任务计划、停止条件等 schema
+ security/ 权限引擎与安全策略
+ subagents/ 内置子代理
+ task_loop/ 外层任务循环
+ tools/ 工具实现
+ workspace/ Workspace schema 与目录管理
+```
+
+