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(tui): update /diff command docs with usage guide, data source details, and effect boundaries

haowenzhong1e6d5e4faa2d2
diff --git a/docs/en/SlashCommands.md b/docs/en/SlashCommands.md
index 130b7a923d..211d091b56 100644
--- a/docs/en/SlashCommands.md
+++ b/docs/en/SlashCommands.md
@@ -55,7 +55,7 @@ Identified by Gateway and forwarded to AgentServer and other backend capabilitie
| `/skills` | Skills management (list, install, uninstall, marketplace, ClawHub, SkillNet) (see below) |
| `/model` | Model view, add, switch (see below) |
| `/mcp` | MCP server management (see below) |
-| `/diff` | View session changes by turn (see below) |
+| `/diff` | Interactive change review: per-turn diffs + uncommitted working tree changes (see below) |
| `/compact` | Compress current context (see below) |
| `/init` | Project initialization (see below) |
| `/branch` | Create a branch session from current conversation point (see below) |
@@ -168,7 +168,11 @@ Behavior:
### `/diff` (Interactive Change Review)
- Usage: `/diff` (no subcommands).
-- Data source: TUI requests Agent diff service via `command.diff`, returns `turns` (change sets per turn) and `gitDiff` (uncommitted working tree changes) for current `session_id`.
+- Applicable modes: All modes.
+- Data source: TUI sends a `command.diff` request to the AgentServer (60s timeout). The handler resolves the current `session_id` and `project_dir` from request metadata, then fetches two data sets **in parallel** via worker threads:
+ - `turns` — per-turn change sets derived from `.agent_history` file operation logs;
+ - `gitDiff` — uncommitted working tree changes from `git diff HEAD`.
+- Response payload: `{ type: "list", turns: [...], gitDiff?: {...} }`. On error: `{ ok: false, error: "..." }`.
- Display mode: Opens a **full-screen interactive Diff viewer**:
- **List view**: Shows all changed files (working tree `working` and per-turn `Turn N`) with relative paths, source label, and added/removed line counts;
- **Detail view**: Press `Enter` on a selected file to view its full hunk-by-hunk diff with scrolling support.
@@ -184,9 +188,45 @@ Behavior:
- `Home` / `g` — Go to file top;
- `End` / `Shift+g` — Go to file bottom;
- `←` / `Esc` — Return to list view.
-- Scope: Covers both the working tree (`git diff HEAD`) and per-turn change traces. Not a replacement for `git diff` full version control perspective.
- Fallback: When the TUI does not provide the `enterDiffViewer` capability, falls back to inline display (file names, source, and line stats only).
+#### Turn-based diff data source
+
+Per-turn diffs are computed from `.agent_history/file_ops_jiuwenswarm*.json` logs, not from git. The service reads and merges file operation logs from multiple locations:
+
+1. Agent workspace (`~/.jiuwenswarm/agent/jiuwenswarm_workspace/.agent_history/`)
+2. User workspace `.agent_history/`
+3. Project directory `.agent_history/` (session-specific and global files)
+
+Entries are deduplicated by path normalization and timestamp proximity (±1 second). Turn boundaries are defined by user messages in session history: a turn spans from one user message timestamp to the next. Only turns with file changes are returned; empty turns are filtered out. Turn indices are preserved (aligned with the actual user message count in history) to allow `/rewind` to map correctly.
+
+#### Git diff data source
+
+Working tree changes are obtained via `git diff HEAD` (tracked files only). The git repo root is resolved from `project_dir` (which may be a subdirectory). Staged renames (including brace shorthand form `a/{b => c}/d.txt`) are handled to align numstat keys with hunk keys.
+
+#### Effect boundaries and limits
+
+| Boundary | Value | Behavior |
+|---|---|---|
+| Max files in detail | 50 | Only the first 50 tracked files get hunks; stats still cover all changed files |
+| Max lines per file | 400 | Hunks are truncated beyond 400 lines per file; `isTruncated` flag is set |
+| Max diff size per file | 1 MB | Files whose diff exceeds 1 MB are skipped in hunk parsing; `isLargeFile` flag is set, stats still counted |
+| Max files for details | 500 | If more than 500 files changed, only aggregate stats are returned (no per-file hunks) |
+| Git command timeout | 10s | Git commands that exceed 10 seconds return `None` |
+| Git root resolution timeout | 5s | `git rev-parse --show-toplevel` that exceeds 5 seconds returns `None` |
+
+#### What is NOT covered
+
+- **Untracked files**: `git diff HEAD` only covers tracked file modifications. Untracked files are excluded from the git diff section. (Untracked files edited by the agent may still appear in per-turn diffs via file_ops logs.)
+- **Manual/bash edits in turn diffs**: Per-turn diffs are derived from the agent's `.agent_history` file operation logs. Files edited manually or via bash commands are not tracked in turn diffs.
+- **Committed changes**: Only uncommitted working tree changes are shown. Committed history is not covered.
+- **Transient git states**: During merge, rebase, cherry-pick, or revert, git diff returns `None` to avoid showing misleading incoming changes.
+- **Binary files**: Binary file changes are counted in stats but no hunks are shown (`isBinary` flag set).
+- **Not a git repo**: If `project_dir` is not in a git repository, `gitDiff` is `None`; only per-turn diffs are returned.
+- **No project_dir**: If `project_dir` cannot be resolved, `gitDiff` is `None`; per-turn diffs may still work using session metadata.
+
+> `/diff` is not a replacement for `git diff` from a full version control perspective. It combines agent-tracked per-turn changes with a snapshot of uncommitted tracked-file changes for quick review within a coding session.
+
### `/compact` (Context Compression)
- Usage: `/compact` (no parameters).
diff --git "a/docs/zh/Slash\345\221\275\344\273\244\350\241\250.md" "b/docs/zh/Slash\345\221\275\344\273\244\350\241\250.md"
index 69dbd4b6eb..7d676ad9e6 100644
--- "a/docs/zh/Slash\345\221\275\344\273\244\350\241\250.md"
+++ "b/docs/zh/Slash\345\221\275\344\273\244\350\241\250.md"
@@ -55,7 +55,7 @@
| `/skills` | 技能管理(列表、安装、卸载、市场源、ClawHub、SkillNet) |
| `/model` | 模型查看、新增、切换(见下文) |
| `/mcp` | MCP 服务管理(见下文) |
-| `/diff` | 查看当前会话按轮次改动(见下文) |
+| `/diff` | 交互式改动回顾:按轮次 diff + 未提交工作树改动(见下文) |
| `/compact` | 压缩当前上下文(见下文) |
| `/init` | 项目初始化(见下文) |
| `/branch` | 从当前对话点创建分支会话(见下文) |
@@ -170,7 +170,11 @@
### `/diff`(交互式改动回顾)
- 用法:`/diff`(无子命令)。
-- 数据来源:TUI 通过 `command.diff` 请求 Agent 侧 diff 服务,按当前 `session_id` 返回 `turns`(每轮改动集合)及 `gitDiff`(未提交的工作树改动)。
+- 适用模式:全部模式。
+- 数据来源:TUI 通过 `command.diff` 请求 AgentServer diff 服务(60s 超时),处理器从请求元数据解析当前 `session_id` 与 `project_dir`,然后**并行**通过 worker 线程获取两组数据:
+ - `turns` — 基于 `.agent_history` 文件操作日志计算的每轮改动集合;
+ - `gitDiff` — 基于 `git diff HEAD` 获取的未提交工作树改动。
+- 响应格式:`{ type: "list", turns: [...], gitDiff?: {...} }`;出错时返回 `{ ok: false, error: "..." }`。
- 展示方式:打开 **交互式 Diff 查看器**(全屏覆盖模式):
- **列表视图**:展示所有变更文件(含工作树 `working` 和按轮次 `Turn N`),显示相对路径、来源、增删行数;
- **详情视图**:选中文件后 `Enter` 进入,展示完整的 hunk diff,支持上下滚动。
@@ -186,9 +190,45 @@
- `Home` / `g` — 跳至文件开头;
- `End` / `Shift+g` — 跳至文件末尾;
- `←` / `Esc` — 返回列表视图。
-- 作用范围:同时覆盖工作树(`git diff HEAD`)和会话按轮次改动轨迹,不替代 `git diff` 的完整版本控制视角。
- 回退行为:当 TUI 不提供 `enterDiffViewer` 能力时,回退为内联展示(仅显示文件名、来源和增删行数)。
+#### 按轮次 diff 数据来源
+
+按轮次 diff 基于 `.agent_history/file_ops_jiuwenswarm*.json` 日志计算,而非 git。服务从多个位置读取并合并文件操作日志:
+
+1. Agent 工作区(`~/.jiuwenswarm/agent/jiuwenswarm_workspace/.agent_history/`)
+2. 用户工作区 `.agent_history/`
+3. 项目目录 `.agent_history/`(含 session 专属文件和全局文件)
+
+条目通过路径规范化和时间戳邻近度(±1 秒)去重。轮次边界由 session history 中的用户消息定义:一个轮次从一条用户消息时间戳到下一条用户消息为止。仅返回有文件变更的轮次,空轮次被过滤。轮次编号保留原始序号(与 history 中实际用户消息计数对齐),以便 `/rewind` 正确映射。
+
+#### Git diff 数据来源
+
+工作树改动通过 `git diff HEAD`(仅已跟踪文件)获取。Git 仓库根目录从 `project_dir`(可以是子目录)解析。暂存区重命名(含 brace 简写形式 `a/{b => c}/d.txt`)会进行归一化处理,确保 numstat key 与 hunk key 对齐。
+
+#### 效果边界与限制
+
+| 边界 | 值 | 行为 |
+|---|---|---|
+| 详情最大文件数 | 50 | 仅前 50 个已跟踪文件有 hunks;统计仍覆盖所有变更文件 |
+| 单文件最大行数 | 400 | 超过 400 行的 hunk 被截断,设置 `isTruncated` 标志 |
+| 单文件最大 diff 大小 | 1 MB | diff 超过 1 MB 的文件跳过 hunk 解析,设置 `isLargeFile` 标志,统计仍计入 |
+| 详情最大文件数阈值 | 500 | 变更文件超过 500 时,仅返回汇总统计(无逐文件 hunk) |
+| Git 命令超时 | 10s | 超过 10 秒的 git 命令返回 `None` |
+| Git 根目录解析超时 | 5s | `git rev-parse --show-toplevel` 超过 5 秒返回 `None` |
+
+#### 不覆盖的范围
+
+- **未跟踪文件**:`git diff HEAD` 仅覆盖已跟踪文件的修改。未跟踪文件不在 git diff 部分中显示。(由 agent 编辑过的未跟踪文件可能通过 file_ops 日志出现在按轮次 diff 中。)
+- **手动/bash 编辑**:按轮次 diff 来源于 agent 的 `.agent_history` 文件操作日志。手动或通过 bash 命令编辑的文件不在按轮次 diff 中追踪。
+- **已提交的改动**:仅显示未提交的工作树改动,已提交的历史不覆盖。
+- **瞬态 git 状态**:在 merge、rebase、cherry-pick、revert 期间,git diff 返回 `None`,避免显示误导性的 incoming 改动。
+- **二进制文件**:二进制文件变更计入统计但不展示 hunks(设置 `isBinary` 标志)。
+- **非 git 仓库**:`project_dir` 不在 git 仓库中时,`gitDiff` 为 `None`,仅返回按轮次 diff。
+- **无 project_dir**:无法解析 `project_dir` 时,`gitDiff` 为 `None`,按轮次 diff 仍可通过 session metadata 工作。
+
+> `/diff` 并非 `git diff` 的完整版本控制替代方案。它将 agent 追踪的按轮次变更与未提交已跟踪文件的快照结合,用于在编码会话内快速回顾改动。
+
### `/compact`(上下文压缩)
- 用法:`/compact`(无参数)。
diff --git "a/docs/zh/TUI\344\275\277\347\224\250\346\214\207\345\215\227.md" "b/docs/zh/TUI\344\275\277\347\224\250\346\214\207\345\215\227.md"
index 77b8d385d3..b8ff677454 100644
--- "a/docs/zh/TUI\344\275\277\347\224\250\346\214\207\345\215\227.md"
+++ "b/docs/zh/TUI\344\275\277\347\224\250\346\214\207\345\215\227.md"
@@ -81,7 +81,7 @@
| `/compact` | - | 压缩上下文,保留摘要 | `/compact` | 全部 |
| `/config` | `/settings`, `/setting` | 查看/设置后端配置 | `/config`、`/config get`、`/config set key value` | 全部 |
| `/context` | - | 查看上下文窗口占用与 Token 用量明细 | `/context` | 全部 |
-| `/diff` | - | 交互式查看工作树与按轮次的文件改动 | `/diff` | 全部 |
+| `/diff` | - | 交互式回顾按轮次 diff + 未提交工作树改动 | `/diff` | 全部 |
| `/evolve` | - | 触发技能演进 | `/evolve myskill 修正错误处理` | `agent.plan` / `team`(见下) |
| `/evolve_list` | - | 列出某技能的演进条目 | `/evolve_list myskill --sort score` | `agent.plan` / `team` |
| `/evolve_rebuild` | - | 从归档与演进记录重建 SKILL.md | `/evolve_rebuild myskill 强化错误处理` | `agent.plan` / `team` |
@@ -220,7 +220,9 @@
#### `/diff`(交互式改动回顾)
-- **`/diff`**:调用 `command.diff`,获取工作树(uncommitted changes)及本会话内有文件变更的轮次,然后打开 **交互式 Diff 查看器**(全屏覆盖模式)。
+- **`/diff`**:调用 `command.diff`(60s 超时),处理器从请求元数据解析 `session_id` 与 `project_dir`,并行获取两组数据后打开 **交互式 Diff 查看器**(全屏覆盖模式):
+ - **按轮次改动**:基于 `.agent_history` 文件操作日志计算,涵盖 agent 工作区、用户工作区和项目目录三处日志,去重合并后按用户消息边界划分轮次;
+ - **工作树改动**:基于 `git diff HEAD` 获取未提交的已跟踪文件改动。
**快捷键**:
@@ -234,7 +236,14 @@
| `Home` / `g` | 列表 → 跳至顶部;详情 → 跳至文件开头 |
| `End` / `Shift+g` | 列表 → 跳至底部;详情 → 跳至文件末尾 |
- 注意:未提交的工作树改动通过 `git diff HEAD` 获取;同一文件在工作树和某轮次中均出现时会重复列出,来源标注为 `working` 或 `Turn N`。
+ **效果边界**:
+ - 同一文件在工作树和某轮次中均出现时会重复列出,来源标注为 `working` 或 `Turn N`;
+ - 按轮次 diff 仅追踪 agent 的文件操作日志,不覆盖手动或 bash 编辑的文件;
+ - git diff 部分仅覆盖已跟踪文件的未提交改动,不包含未跟踪文件和已提交历史;
+ - 在 merge/rebase/cherry-pick/revert 等瞬态 git 状态下,git diff 返回 `None`;
+ - 单文件 hunk 超过 400 行会被截断;单文件 diff 超过 1 MB 跳过 hunk 解析;变更文件超过 500 时仅返回统计;
+ - 非 git 仓库或无法解析 `project_dir` 时,仅返回按轮次 diff;
+ - 详见 [Slash命令表](Slash命令表.md#diff交互式改动回顾)。
- **`/compact`**:调用 `command.compact`,返回 `busy` | `compressed` | `noop`;成功时展示 token 节省比例(`compact.ts`)。