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

feat(aipc): 补充code模式和git管理的接口文档

kdgmpw10845b440bd5
diff --git a/docs/en/Project-Session-Management.md b/docs/en/Project-Session-Management.md
index 7a1151912e..8f1bef86f8 100644
--- a/docs/en/Project-Session-Management.md
+++ b/docs/en/Project-Session-Management.md
@@ -1,42 +1,46 @@
# Project & Session Management (Web)
-> **Implementation**: `jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py` (handler registration), `jiuwenswarm/server/runtime/session/project_store.py` (project storage), `jiuwenswarm/server/runtime/session/session_metadata.py` (session metadata), `jiuwenswarm/server/runtime/session/session_rename.py` (rename shared logic)
+> **Implementation**: `jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py` (handler registration), `jiuwenswarm/gateway/channel_manager/web/git_ws_handler.py` (Git Diff WebSocket), `jiuwenswarm/server/runtime/session/project_store.py` (project storage), `jiuwenswarm/server/runtime/session/session_metadata.py` (session metadata), `jiuwenswarm/server/runtime/session/project_git.py` (Git operations), `jiuwenswarm/server/runtime/session/git_diff_status.py` (Diff aggregation), `jiuwenswarm/server/runtime/session/git_diff_watcher.py` (Diff monitoring)
> **中文版**: [../zh/项目与会话管理.md](../zh/项目与会话管理.md)
---
## 1. Overview
-The Web channel provides project-scoped session management. A session is bound to a specific project via `project_id`, or uses the default empty project. Projects and sessions are linked via `project_id` matching.
+The Web channel provides project-scoped session management. Sessions bind to projects through `project_id`, and can also fall back to mode-specific default projects.
+
+The current implementation adds a `work_mode` dimension to isolate regular work collaboration from code projects. `code` projects support Git status, branch operations, Diff snapshots, live Diff monitoring, and discarding the last turn's file changes. `work` projects keep the regular project/session behavior.
### Core Concepts
| Concept | Description |
|---------|-------------|
-| **Default project** | Not persisted; dynamically injected by the API, `project_id="default"`. Rename/remove/pin are forbidden. Sessions with empty `project_id` or no matching visible project are assigned to the default project |
-| **Soft delete** | `project.remove` marks `hidden:true` without deleting the record; recoverable. While hidden, its non-pinned sessions temporarily belong to the default project |
-| **Pinned sessions** | Detached from project groups; fetched independently via `project.pinned_sessions`, sorted by `pin_order` ascending |
-| **Pinned projects** | Visually distinguished from non-pinned projects; pinned projects appear first (sorted by `pin_order` ascending) |
-| **project_id / project_dir immutability** | Once a session's `project_id` and `project_dir` are bound, they cannot be changed; no cross-project migration API is provided |
+| **Default projects** | Not persisted; dynamically injected by APIs. `default` is the work default project, `default_code` is the code default project. Rename/remove/pin are forbidden |
+| **`work_mode`** | Isolation dimension for projects, sessions, and scheduled tasks. Values: `work` / `code`. Web defaults to `work`; TUI defaults to `code` |
+| **Code project Git capabilities** | Real `code` projects expose Git status, init, branch, Diff, history, monitoring, and discard APIs. Default projects and `work` projects do not expose Git operations |
+| **Soft delete** | `project.remove` marks `hidden:true` without deleting the record. While hidden, its non-pinned sessions temporarily fall back to the mode-specific default project |
+| **Pinned sessions** | Detached from project groups and fetched via `project.pinned_sessions`, sorted by `pin_order` ascending |
+| **Pinned projects** | Pinned projects appear first, sorted by `pin_order` ascending |
+| **Immutability** | Once `project_id`, `project_dir`, and `work_mode` are bound to a session, they cannot be changed; no cross-project/cross-mode migration API is provided |
### Communication Convention
- Transport: WebSocket JSON messages
- Request structure: `{ "id": "<req_id>", "method": "<method_name>", "params": { ... } }`
-- Response envelope (unified across all interfaces):
+- Response envelope:
| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `ok` | boolean | yes | Success flag |
| `payload` | object | on success | Response data |
-| `error` | string | on failure | Error description |
+| `error` | string | on failure | Error message |
| `code` | string | on failure | Error code |
-### session_id Generation Convention
+### session_id Generation
-- `session_id` is **generated by the frontend**, format: `sess_<hex milliseconds timestamp>_<6-char hex random>`
-- The `sess_` prefix is a Web convention: only sessions starting with `sess_` support `history.get` for history recovery
-- The backend does not validate this format, but the frontend must follow it
+- `session_id` is generated by the frontend: `sess_<hex millisecond timestamp>_<6-char hex random>`
+- The `sess_` prefix is a Web convention: only these sessions support `history.get` recovery
+- The backend does not validate the format, but the frontend must follow it
---
@@ -48,36 +52,54 @@ Returned by `project.list`.
| Field | Type | Description |
|-------|------|-------------|
-| `project_id` | string | Unique project ID; default project is `"default"` |
+| `project_id` | string | Unique project ID; defaults are `default` / `default_code` |
| `name` | string | Display name |
-| `project_dir` | string | Working directory absolute path; default project is `""` |
+| `project_dir` | string | Absolute working directory; default projects use `""` |
| `pinned` | boolean | Whether pinned |
-| `pin_order` | integer | Pin order, lower comes first; 0 when not pinned |
-| `is_default` | boolean | Whether default project |
-| `hidden` | boolean | Whether soft-deleted; default project is always false |
-| `session_count` | integer | Number of **non-pinned** sessions in this project |
-| `last_message_at` | number \| null | Latest message timestamp (UTC seconds) in this project; null if no sessions |
-| `last_user_message_at` | number \| null | Latest user message timestamp (UTC seconds); falls back to `created_at` when no user messages |
-| `created_at` | number | Creation timestamp (UTC seconds); default project is 0 |
+| `pin_order` | integer | Pin order; lower comes first; `0` when not pinned |
+| `is_default` | boolean | Whether this is a default project |
+| `hidden` | boolean | Whether soft-deleted; defaults are always false |
+| `session_count` | integer | Number of non-pinned sessions in this project |
+| `last_message_at` | number \| null | Latest message timestamp (UTC seconds) |
+| `last_user_message_at` | number \| null | Latest user message timestamp; falls back to `created_at` |
+| `created_at` | number | Creation timestamp; defaults use `0` |
+| `updated_at` | number | Update timestamp; defaults use `0` |
+| `work_mode` | string | `work` or `code` |
+| `git` | object | Git snapshot. Written by Git probe/operations for code projects; disabled/empty for work/default projects |
+
+### GitStatus
+
+Used by `ProjectInfo.git` and Git status-style responses.
+
+| Field | Type | Description |
+|-------|------|-------------|
+| `enabled` | boolean | Whether Git capability is enabled in the persisted snapshot |
+| `status` | string | Probe state, such as `ready`, `not_git`, `git_missing`, `transient`, `error`, or `disabled` |
+| `repo_root` | string \| null | Git repository root |
+| `branch` | string \| null | Current branch; may be a short HEAD in detached mode |
+| `head` | string \| null | Short HEAD hash |
+| `is_dirty` | boolean | Whether the worktree has changes |
+| `error` | object \| null | Structured Git error with `code`, `message`, `hint`, etc. |
### SessionInfo
-Returned by `project.get_sessions` / `project.pinned_sessions`. Projected from `metadata.json`, excluding internal fields like `delivery_context` / `channel_metadata`.
+Returned by `project.get_sessions` / `project.pinned_sessions`.
| Field | Type | Description |
|-------|------|-------------|
| `session_id` | string | Unique session ID |
| `title` | string | Session title |
-| `created_at` | number | Creation timestamp (UTC seconds) |
-| `last_message_at` | number | Last message timestamp (UTC seconds) |
+| `created_at` | number | Creation timestamp |
+| `last_message_at` | number | Last message timestamp |
| `message_count` | integer | Message count |
| `mode` | string | Session mode |
| `pinned` | boolean | Whether pinned |
-| `pin_order` | integer | Pin order; 0 when not pinned |
-| `project_id` | string | Bound project ID; empty string for default project |
-| `project_dir` | string | Bound project path; empty string for default project |
-| `last_user_message_at` | number \| null | Last user message timestamp (UTC seconds); falls back to `created_at` when no user messages |
-| `model` | string | Current model identifier; empty string when unset |
+| `pin_order` | integer | Pin order; `0` when not pinned |
+| `project_id` | string | Bound project ID; empty string means default |
+| `project_dir` | string | Bound project path; empty string means default |
+| `work_mode` | string | `work` or `code` |
+| `last_user_message_at` | number \| null | Last user message timestamp; falls back to `created_at` |
+| `model` | string | Current model identifier; empty when unset |
### Mode Values
@@ -86,7 +108,7 @@ Returned by `project.get_sessions` / `project.pinned_sessions`. Projected from `
| `agent.plan` | Agent planning mode |
| `agent.fast` | Agent fast mode |
| `code.plan` | Code planning mode |
-| `code.normal` | Code normal mode (default) |
+| `code.normal` | Code normal mode |
| `code.team` | Code team mode |
| `team` | Team mode |
@@ -96,19 +118,19 @@ Returned by `project.get_sessions` / `project.pinned_sessions`. Projected from `
| Scenario | Rule |
|----------|------|
-| Pinned sessions (`project.pinned_sessions`) | Sorted by `pin_order` ascending, detached from project groups |
-| Project list (`project.list`) | Pinned projects first (by `pin_order` ascending), then by `last_user_message_at` descending, default project last |
-| Sessions in project (`project.get_sessions`) | Non-pinned only, sorted by `last_user_message_at` descending |
+| Pinned sessions (`project.pinned_sessions`) | Sorted by `pin_order` ascending and detached from project groups |
+| Project list (`project.list`) | Pinned projects first, then by `last_user_message_at` descending, with default projects at the end |
+| Sessions in a project (`project.get_sessions`) | Non-pinned only, sorted by `last_user_message_at` descending |
-> **Statistics**: `session_count` and `project.get_sessions` always count only **non-pinned** sessions in the current project. Pinned sessions are returned independently by `project.pinned_sessions` and are not counted in any project's `session_count`.
+> `session_count` and `project.get_sessions` count only non-pinned sessions. Pinned sessions are returned independently by `project.pinned_sessions`.
---
## 4. Session Interfaces
-### session.list — List sessions
+### session.list - List sessions
-Flat paginated session list, unchanged behavior.
+Flat paginated session list.
**Request params:**
@@ -121,310 +143,441 @@ Flat paginated session list, unchanged behavior.
| Field | Type | Description |
|-------|------|-------------|
-| `sessions` | object[] | Session metadata list (full fields) |
+| `sessions` | object[] | Full session metadata list, including `work_mode` |
| `total` | integer | Total session count |
-| `limit` | integer | Actual limit used |
-| `offset` | integer | Actual offset used |
+| `limit` | integer | Actual limit |
+| `offset` | integer | Actual offset |
+| `has_more` | boolean | Whether more sessions exist |
---
-### session.create — Create session
+### session.create - Create session
-Adds optional `project_dir` and `project_id` params; behavior is fully backward-compatible when omitted.
+Adds optional `project_dir`, `project_id`, and `work_mode`. When omitted, behavior remains backward compatible. Web defaults to `work`; TUI defaults to `code`.
**Request params:**
| Param | Type | Required | Description |
|-------|------|----------|-------------|
-| `session_id` | string | yes | Session ID, generated by frontend (`sess_` prefix) |
+| `session_id` | string | yes | Session ID generated by the frontend |
| `title` | string | no | Session title |
-| `mode` | string | no | Mode, default `unknown` (overwritten with the real mode on first `chat.send`) |
-| `project_dir` | string | no | Working directory absolute path. Cannot be provided alone — a valid `project_id` (non-empty, not `"default"`) must accompany it, otherwise `BAD_REQUEST`. When `project_id` is given without `project_dir`, it is auto-filled from the project's bound path |
-| `project_id` | string | no | Target project ID, immutable after first bind. When non-empty and not `"default"`, must correspond to an existing visible project, otherwise `NOT_FOUND`. If `project_dir` is also provided, it must match the project's bound path (consistency check), otherwise `BAD_REQUEST`. Omitted or `"default"` (with empty `project_dir`) means the default empty project |
-
-> Validation — `project_id` / `project_dir` binding rules:
-> - Both empty (or `project_id="default"` with empty `project_dir`) → default project (backward compatible);
-> - `project_id` only (no `project_dir`) → `project_dir` auto-filled from the project record;
-> - `project_id` + `project_dir` both provided → consistency check (must match the project's bound path, trailing-separator/case differences tolerated);
-> - `project_dir` only without a valid `project_id` → rejected (`BAD_REQUEST`).
->
-> Additionally, `project_dir` non-empty must be an absolute path (`BAD_REQUEST` otherwise). Both fields are written to session metadata on creation and are immutable thereafter.
+| `mode` | string | no | Mode; default `unknown`, overwritten by the first `chat.send` |
+| `project_dir` | string | no | Absolute working directory. Cannot be supplied alone; must accompany a valid real `project_id`, or be auto-filled from that project |
+| `project_id` | string | no | Target project ID. Empty/default project IDs mean the mode-specific default project. Real IDs must point to visible projects |
+| `work_mode` | string | no | `work` / `code`. Must match the target project if `project_id` is real |
**Response payload:**
| Field | Type | Description |
|-------|------|-------------|
| `session_id` | string | Created session ID |
+| `project_id` | string | Final project binding |
+| `project_dir` | string | Final project directory |
+| `work_mode` | string | Final work mode |
-**Error codes:** `BAD_REQUEST` (`project_dir` non-empty but not absolute; `project_dir` provided without a valid `project_id`; `project_dir` does not match the project's bound path), `ALREADY_EXISTS` (session already exists), `NOT_FOUND` (`project_id` does not match an existing visible project)
+**Error codes:** `BAD_REQUEST`, `ALREADY_EXISTS`, `NOT_FOUND`
---
-### session.delete — Delete session
+### session.delete - Delete session
+
+Deletes the session directory and runtime state.
+
+**Request params:** `session_id` (string, required)
+
+**Response payload:** `{ "session_id": "<deleted session id>" }`
-Deletes the session directory and runtime state, unchanged behavior.
+**Error codes:** `NOT_FOUND`
+
+---
+
+### session.rename - Rename session
+
+Supports query / set / clear semantics.
**Request params:**
| Param | Type | Required | Description |
|-------|------|----------|-------------|
| `session_id` | string | yes | Target session ID |
+| `title` | string | no | Omitted/null queries; empty clears; non-empty sets, truncated to 200 chars |
-**Response payload:**
+**Response payload:** `session_id`, `title`, `previous_title`.
-| Field | Type | Description |
-|-------|------|-------------|
-| `session_id` | string | Deleted session ID |
+**Error codes:** `BAD_REQUEST`
+
+---
+
+### session.pin - Pin/unpin session
+
+Pinned sessions are detached from project groups into the global pinned area. Pin order is compacted after each operation.
+
+**Request params:** `session_id` (string, required), `pinned` (boolean, required)
+
+**Response payload:** `pinned`, `pin_order`.
-**Error codes:** `NOT_FOUND` (session not found)
+**Error codes:** `NOT_FOUND`
---
-### session.rename — Rename session
+## 5. Project Interfaces
+
+### project.list - List projects
-Supports three semantics: query / set / clear.
+Returns sorted projects with statistics and injected default projects.
**Request params:**
| Param | Type | Required | Description |
|-------|------|----------|-------------|
-| `session_id` | string | yes | Target session ID |
-| `title` | string | no | Omitted (or `null`) → query current title; empty/whitespace → clear title; non-empty → set (truncated to 200 chars) |
+| `filter` | string | no | `all` / `pinned` / `unpinned`, default `all` |
+| `include_hidden` | boolean | no | Whether to include soft-deleted projects |
+| `work_mode` | string | no | Filter by `work` / `code`; omitted returns all modes |
-**Response payload:**
+**Response payload:** `projects` (`ProjectInfo[]`).
-| Field | Type | Description |
-|-------|------|-------------|
-| `session_id` | string | Target session ID |
-| `title` | string | Title after operation |
-| `previous_title` | string | Title before operation (same as `title` for query semantics) |
+---
+
+### project.get_sessions - List sessions in a project
-> If session metadata does not exist, it is automatically created before writing; no error.
+Returns non-pinned sessions for a project. `default` returns unbound/fallback work sessions; `default_code` returns unbound/fallback code sessions.
-**Error codes:** `BAD_REQUEST` (`session_id` is empty)
+**Request params:** `project_id` (string, required), optional `limit`, `offset`.
+
+**Response payload:** `sessions` (`SessionInfo[]`), `total`.
+
+**Error codes:** `NOT_FOUND`
---
-### session.pin — Pin/unpin session
+### project.create - Create project
-Pinning detaches the session from its project group into the global pinned area; unpinning returns it to the original project. All pinned sessions are renumbered compactly to 1..N after the operation.
+Creates a project with an optional directory. If a non-empty `project_dir` matches a hidden project in the same `work_mode`, that project is restored. Code projects automatically probe or initialize Git.
**Request params:**
| Param | Type | Required | Description |
|-------|------|----------|-------------|
-| `session_id` | string | yes | Session ID |
-| `pinned` | boolean | yes | `true`=pin, `false`=unpin |
+| `name` | string | yes | Display name; unique within the same `work_mode`, including hidden projects |
+| `project_dir` | string | no | Absolute directory; empty path projects are allowed |
+| `work_mode` | string | no | `work` / `code`; inferred from channel when omitted |
**Response payload:**
| Field | Type | Description |
|-------|------|-------------|
-| `pinned` | boolean | Pinned state after operation |
-| `pin_order` | integer | `pin_order` after operation |
+| `project_id` | string | New or restored project ID |
+| `project_dir` | string | Project path |
+| `restored` | boolean | Whether a hidden project was restored |
+| `project` | ProjectInfo | Full project object |
+| `work_mode` | string | Project work mode |
+| `git` | object | Git snapshot for code projects; disabled/empty for work projects |
-> **Idempotent**: operating on a session already in the target state is treated as success.
-> A newly pinned session has `pin_order` defaulting to 0, placing it at the top after renumbering.
-
-**Error codes:** `NOT_FOUND` (session not found)
+**Error codes:** `BAD_REQUEST`, `CONFLICT`
---
-## 5. Project Interfaces
+### project.rename - Rename project
-### project.list — List projects
+Renames the display name only. Name conflicts are checked within the same `work_mode`, including hidden projects.
-Returns project list (with statistics), sorted, including the default project.
+**Request params:** `project_id` (string, required), `name` (string, required)
-**Request params:**
+**Response payload:** empty object.
-| Param | Type | Required | Description |
-|-------|------|----------|-------------|
-| `filter` | string | no | `"all"` (default) / `"pinned"` / `"unpinned"` |
-| `include_hidden` | boolean | no | Whether to include soft-deleted projects, default `false`. Only effective for `"all"` / `"unpinned"` |
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `FORBIDDEN`, `CONFLICT`
-**filter semantics:**
+---
-| Value | Description |
-|-------|-------------|
-| `"all"` | Pinned projects first (by `pin_order` ascending), then by `last_user_message_at` descending, default project last |
-| `"pinned"` | Pinned projects only, by `pin_order` ascending |
-| `"unpinned"` | Non-pinned projects only, by `last_user_message_at` descending, default project last |
+### project.pin - Pin/unpin project
-**Response payload:**
+Toggles project pin state and compacts pin order.
-| Field | Type | Description |
-|-------|------|-------------|
-| `projects` | ProjectInfo[] | Project list, sorted |
+**Request params:** `project_id` (string, required), `pinned` (boolean, required)
-**Error codes:** none
+**Response payload:** `pinned`, `pin_order`.
+
+**Error codes:** `NOT_FOUND`, `FORBIDDEN`
---
-### project.get_sessions — Get sessions in project
+### project.remove - Soft-delete project
-Returns **non-pinned** sessions for the given `project_id`, sorted. Sessions are matched by `project_id` only. Passing `"default"` returns non-pinned sessions not belonging to any visible project (including sessions with empty `project_id` or matching hidden projects).
+Marks a project as hidden and unpins it. Its non-pinned sessions temporarily fall back to the mode-specific default project.
-**Request params:**
+**Request params:** `project_id` (string, required)
-| Param | Type | Required | Description |
-|-------|------|----------|-------------|
-| `project_id` | string | yes | Project ID; pass `"default"` for default project sessions |
-| `limit` | integer | no | Return limit, default unlimited |
-| `offset` | integer | no | Offset, default 0 |
+**Response payload:** `affected_sessions`.
-**Response payload:**
+**Error codes:** `NOT_FOUND`, `FORBIDDEN`
-| Field | Type | Description |
-|-------|------|-------------|
-| `sessions` | SessionInfo[] | Sorted non-pinned session list (by `last_user_message_at` descending) |
-| `total` | integer | Total non-pinned session count in this project (before truncation) |
+---
+
+### project.restore - Restore project
+
+Restores a hidden project. Sessions automatically reattach by `project_id`.
+
+**Request params:** `project_id` (string, required)
+
+**Response payload:** `affected_sessions`.
-**Error codes:** `NOT_FOUND` (project not found or soft-deleted/hidden)
+**Error codes:** `NOT_FOUND`, `CONFLICT`, `FORBIDDEN`
---
-### project.create — Create project
+### project.pinned_sessions - List pinned sessions
-Creates a project with an optional working directory. If the non-empty `project_dir` matches a hidden project, it is automatically restored. Supports creating empty-path projects (multiple allowed, distinguished by `project_id` + `name`).
+Returns all pinned sessions sorted by `pin_order`.
-**Request params:**
+**Request params:** none
-| Param | Type | Required | Description |
-|-------|------|----------|-------------|
-| `name` | string | yes | Project display name (non-empty, globally unique including hidden projects); used to update the name when restoring |
-| `project_dir` | string | no | Working directory absolute path; omitted or empty creates an empty-path project |
+**Response payload:** `sessions` (`SessionInfo[]`).
-**Response payload:**
+---
+
+## 6. Git & Diff Interfaces (Code Projects)
+
+These methods use the regular `/ws` request envelope. They are only available for real `work_mode="code"` projects.
+
+### GitStatusPayload
| Field | Type | Description |
|-------|------|-------------|
-| `project_id` | string | Project ID (`proj_<8-char hex>` for new, original ID for restored) |
-| `project_dir` | string | Project path (empty string for empty-path projects) |
-| `restored` | boolean | `true`=restored a hidden project, `false`=newly created |
+| `project_id` | string | Project ID |
+| `project_name` | string | Project display name |
+| `project_dir` | string | Project working directory |
+| `work_mode` | string | Always `code` |
+| `repo` | object | `is_git`, `repo_root`, `branch`, `head`, `detached`, `transient`, `upstream` |
+| `working_tree` | object | `is_dirty`, `staged`, `unstaged`, `untracked`, `conflicted` |
+| `branches` | object | `current`, `locals`, `remotes` |
+| `generated_at` | number | Generation timestamp |
+
+> `project.git.status` reads the live worktree without persisting or waking watchers. `project.git.probe` re-detects Git state, persists `ProjectInfo.git`, and wakes `/ws/git` watchers.
+
+### DiffSummary / DiffFileEntry / Hunk
+
+| Structure | Key fields | Description |
+|-----------|------------|-------------|
+| `DiffSummary` | `stats`, `files` | Summary stats plus a file map; `files` is `{}` when files are not requested |
+| `DiffFileEntry` | `file_path`, `status`, `change_type`, `lines_added`, `lines_removed`, `is_binary`, `is_new_file`, `is_deleted_file`, `is_untracked`, `is_large_file`, `is_truncated`, `hunks` | Per-file change entry. `hunks` is omitted when not requested |
+| `Hunk` | `old_start`, `old_lines`, `new_start`, `new_lines`, `lines` | A continuous diff block for locating changed lines and context |
-> **Auto-restore**: when a non-empty `project_dir` matches a hidden project, sets `hidden:false` and updates the display name; its sessions automatically re-associate via `project_id`. Returns `CONFLICT` when `project_dir` duplicates an existing visible project, or `name` duplicates any existing project (including hidden).
+### project.git.status - Query Git status
-**Error codes:** `BAD_REQUEST` (`name` empty, `project_dir` non-empty but not absolute), `CONFLICT` (`project_dir` duplicates an existing visible project, or `name` duplicates an existing project)
+Live Git status for status bars and branch pickers.
+
+**Request params:** `project_id` (string, required)
+
+**Response payload:** `GitStatusPayload`.
+
+**Error codes:** `BAD_REQUEST`, `PROJECT_NOT_FOUND`, `FORBIDDEN`, `GIT_NOT_FOUND`, `NOT_GIT_REPOSITORY`
---
-### project.rename — Rename project
+### project.git.probe - Re-probe Git and persist snapshot
-Changes only the display name; does not alter the working directory path. Returns `CONFLICT` if `name` duplicates any existing project (including hidden).
+Re-detects Git state and writes the project `git` snapshot. Use after external `git init`, Git installation, or `.git` removal.
-**Request params:**
+**Request params:** `project_id` (string, required)
-| Param | Type | Required | Description |
-|-------|------|----------|-------------|
-| `project_id` | string | yes | Project ID |
-| `name` | string | yes | New name (non-empty, globally unique including hidden projects) |
+**Response payload:** `GitStatusPayload`.
+
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `FORBIDDEN`, `GIT_NOT_FOUND`, `NOT_GIT_REPOSITORY`
+
+---
-**Response payload:** empty (`{}`)
+### project.git.init - Initialize Git repository
-**Error codes:** `BAD_REQUEST` (`name` empty), `NOT_FOUND` (project not found), `FORBIDDEN` (default project cannot be renamed), `CONFLICT` (`name` duplicates an existing project)
+Runs `git init`, refreshes the project snapshot, and wakes Diff watchers.
+
+**Request params:** `project_id` (string, required), optional `initial_branch` (default `main`)
+
+**Response payload:** `GitStatusPayload`.
+
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `FORBIDDEN`, `GIT_NOT_FOUND`, `GIT_INIT_FAILED`
---
-### project.pin — Pin/unpin project
+### project.git.switch_branch - Switch branch
-Toggles project pin state. All pinned projects are renumbered compactly to 1..N after the operation.
+Switches to an existing local branch. `require_clean=true` rejects dirty worktrees.
-**Request params:**
+**Request params:** `project_id`, `branch`, optional `require_clean`.
-| Param | Type | Required | Description |
-|-------|------|----------|-------------|
-| `project_id` | string | yes | Project ID |
-| `pinned` | boolean | yes | `true`=pin, `false`=unpin |
+**Response payload:** `switched`, `previous_branch`, `current_branch`, `status` (`GitStatusPayload`).
+
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `FORBIDDEN`, `GIT_TRANSIENT_STATE`, `BRANCH_NOT_FOUND`, `WORKTREE_DIRTY`, `CHECKOUT_FAILED`
+
+---
+
+### project.git.create_branch - Create branch
+
+Creates a local branch and optionally checks it out.
+
+**Request params:** `project_id`, `branch`, optional `checkout` (default `true`), optional `start_point`.
+
+**Response payload:** `created`, `checked_out`, `branch`, `status` (`GitStatusPayload`).
+
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `FORBIDDEN`, `GIT_TRANSIENT_STATE`, `BRANCH_EXISTS`, `START_POINT_NOT_FOUND`, `CHECKOUT_FAILED`
+
+---
+
+### project.git.diff_status - Query current and last-turn Diff
+
+Fetches current worktree Diff and, when `session_id` is provided, the last agent turn Diff. For live updates, use `/ws/git` subscriptions.
+
+**Request params:** `project_id`, optional `session_id`, optional `include_files`, optional `include_hunks`.
**Response payload:**
| Field | Type | Description |
|-------|------|-------------|
-| `pinned` | boolean | Pinned state after operation |
-| `pin_order` | integer | `pin_order` after operation |
+| `project_id` | string | Project ID |
+| `session_id` | string \| null | Requested session |
+| `work_mode` | string | Always `code` |
+| `repo` | object | `is_git`, `repo_root`, `branch`, `head`, `transient` |
+| `current` | DiffSummary \| null | Current worktree Diff; may be null when no Git, Git missing, or transient |
+| `last_turn` | DiffSummary \| null | Last turn Diff; null when no session or no history |
+| `generated_at` | number | Generation timestamp |
+
+**Error codes:** `BAD_REQUEST`, `PROJECT_NOT_FOUND`, `FORBIDDEN`, `GIT_NOT_FOUND`
+
+---
+
+### project.git.turn_diff_list - List historical turn Diffs
+
+Returns paginated historical Diff summaries for a session. The session must belong to the requested project.
+
+**Request params:** `project_id`, `session_id`, optional `limit` (default 50), optional `cursor` (default 0).
-> **Idempotent**: operating on a project already in the target state is treated as success.
-> A newly pinned project has `pin_order` defaulting to 0, placing it at the top after renumbering.
+**Response payload:** `project_id`, `session_id`, `repo_root`, `branch`, `base_head`, `turns`, `cursor`, `next_cursor`, `has_more`, `limit`, `total`.
-**Error codes:** `NOT_FOUND` (project not found or hidden), `FORBIDDEN` (default project cannot be pinned)
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `SESSION_NOT_FOUND`, `SESSION_NOT_BOUND`, `PROJECT_SESSION_MISMATCH`, `GIT_TRANSIENT_STATE`
---
-### project.remove — Remove project (soft delete)
+### project.git.turn_diff - Query one historical turn Diff
-Marks the project as `hidden:true` and automatically unpins it. Its non-pinned sessions temporarily move to the default project; pinned sessions are unaffected.
+Returns detail for one historical turn. `change_set_id` takes precedence over `turn_index`.
-**Request params:**
+**Request params:** `project_id`, `session_id`, optional `change_set_id`, optional `turn_index`, optional `include_files`, optional `include_hunks`.
-| Param | Type | Required | Description |
-|-------|------|----------|-------------|
-| `project_id` | string | yes | Project ID |
+**Response payload:** one turn detail with `project_id`, `session_id`, repo context, `change_set_id`, `turn_index`, message IDs, `stats`, and `files`.
-**Response payload:**
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `SESSION_NOT_FOUND`, `SESSION_NOT_BOUND`, `PROJECT_SESSION_MISMATCH`, `CHANGE_SET_NOT_FOUND`, `TURN_DIFF_NOT_FOUND`, `DIFF_HISTORY_EXPIRED`, `GIT_TRANSIENT_STATE`
-| Field | Type | Description |
-|-------|------|-------------|
-| `affected_sessions` | integer | Number of non-pinned sessions affected (temporarily moved to default project) |
+---
+
+## 7. /ws/git Live Monitoring & Discard
+
+`/ws/git` uses the same request/response envelope as `/ws`, and also pushes live events.
-> **Idempotent**: removing an already-hidden project returns `affected_sessions: 0`.
+| Event | Trigger | Payload summary |
+|-------|---------|-----------------|
+| `project.git.diff_changed` | Summary fingerprint changed | Project/session/repo/current/last_turn/revision |
+| `project.git.diff_files_changed` | Subscribed file list changed | Project/session/source/revision/files; last-turn metadata when source is `last_turn` |
+| `project.git.diff_detail_changed` | Subscribed hunks changed | Project/session/source/revision/files; last-turn metadata when source is `last_turn` |
+| `project.git.error` | Watcher recomputation failed | Project/session/error code/message/detail |
-**Error codes:** `NOT_FOUND` (project not found), `FORBIDDEN` (default project cannot be removed)
+### project.git.diff_watch - Watch Diff summary
+
+Creates a summary watcher. The initial response contains summary stats only; file maps are empty. Later changes are pushed via `project.git.diff_changed`.
+
+**Request params:** `project_id`, optional `session_id`, optional `scope` (`summary` only), optional `include_last_turn` (default `true`).
+
+**Response payload:** `watch_id`, `scope`, `snapshot` (`project_id`, `session_id`, `repo`, `current`, `last_turn`, `revision`).
+
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `FORBIDDEN`, Git domain errors
---
-### project.restore — Restore project
+### project.git.diff_files_watch - Watch file list
-Restores a soft-deleted (`hidden:true`) project to visible. Its sessions automatically re-associate because `project_id` still matches. Returns `CONFLICT` if the restored `name` would duplicate an existing project (including hidden).
+Enables or switches file-list watching on an existing `watch_id`. Initial response returns files without hunks; later changes are pushed via `project.git.diff_files_changed`.
-**Request params:**
+**Request params:** `project_id`, `watch_id`, `source` (`current` / `last_turn`), optional `session_id`.
-| Param | Type | Required | Description |
-|-------|------|----------|-------------|
-| `project_id` | string | yes | Project ID |
+**Response payload:** `watch_id`, `files_scope`, `revision`, `files` (map of path to `DiffFileEntry` without `hunks`).
-**Response payload:**
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `FORBIDDEN`, Git domain errors
-| Field | Type | Description |
-|-------|------|-------------|
-| `affected_sessions` | integer | Number of non-pinned sessions re-associated to this project |
+---
+
+### project.git.diff_detail_watch - Watch file details
+
+Replaces the detail subscription for an existing watcher. Only subscribed file hunk changes are pushed.
-**Error codes:** `NOT_FOUND` (project not found), `CONFLICT` (project is not hidden — already visible, or restored `name` duplicates an existing project), `FORBIDDEN` (default project)
+**Request params:** `project_id`, `watch_id`, `source` (`current` / `last_turn`), `files` (`string[]`), optional `session_id`.
+
+**Response payload:** `watch_id`, `detail_scope`, `revision`, `files` (map of requested path to `DiffFileEntry`; missing files are `null`; includes `hunks`).
+
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `FORBIDDEN`, Git domain errors
---
-### project.pinned_sessions — Get all pinned sessions
+### project.git.diff_unwatch - Cancel watch
-Returns all pinned sessions, sorted by `pin_order` ascending. Pinned sessions are detached from project groups and fetched via this interface.
+Releases watcher resources. `all` removes the whole watcher; `files` / `detail` only cancels the child subscription. Missing `watch_id` is idempotently successful.
-**Request params:** none
+**Request params:** `watch_id` (required), optional `scope` (`all` / `files` / `detail`, default `all`).
+
+**Response payload:** `watch_id`, `cancelled`, `scope`.
+
+**Error codes:** `BAD_REQUEST`
+
+---
+
+### project.git.discard_turn_changes - Discard last turn file changes
+
+Restores the current session's last agent turn file changes to their pre-turn state, or deletes files created in that turn. On full success, session-specific file_ops for that turn are truncated and Diff watchers are woken. On partial failure, the response uses `ok=false` and keeps file_ops so the operation can be retried.
+
+**Request params:** `project_id` (code project ID), `session_id` (must belong to the project).
**Response payload:**
| Field | Type | Description |
|-------|------|-------------|
-| `sessions` | SessionInfo[] | All pinned sessions, by `pin_order` ascending. `project_dir` still points to the original project |
-
-> After a project is soft-deleted, its pinned sessions remain in the global pinned list, unaffected by the project's hidden state.
-
-**Error codes:** none
+| `session_id` | string | Session ID |
+| `turn_index` | integer | Discarded last turn index |
+| `change_set_id` | string \| null | Discarded change set ID; null on partial failure |
+| `restored_files` | string[] | Files restored to previous contents |
+| `deleted_files` | string[] | Newly created files deleted by discard |
+| `errors` | object[] | Per-file restore errors |
+| `file_ops_truncated` | boolean | Whether session-specific file_ops were truncated |
+| `global_file_ops_truncated` | boolean | Always `false`; global file_ops are not truncated to avoid cross-session damage |
+| `partial` | boolean | Whether the restore partially failed; when true, top-level `ok=false` and `code=PARTIAL_RESTORE_FAILED` |
+
+**Error codes:** `BAD_REQUEST`, `NOT_FOUND`, `FORBIDDEN`, `SESSION_NOT_BOUND`, `PROJECT_SESSION_MISMATCH`, `SESSION_BUSY`, `NO_TURN_TO_DISCARD`, `PARTIAL_RESTORE_FAILED`
---
-## 6. Interface Overview
+## 8. Interface Overview
-| Method | Category | Description |
-|--------|----------|-------------|
-| `session.list` | Session | Flat pagination, unchanged |
-| `session.create` | Session | Adds optional `project_dir` / `project_id` params, backward-compatible default |
-| `session.delete` | Session | Unchanged |
-| `session.rename` | Session | Web local handler, query/set/clear |
+| Method | Area | Description |
+|--------|------|-------------|
+| `session.list` | Session | Flat paginated session list |
+| `session.create` | Session | Create session with optional `project_dir` / `project_id` / `work_mode` |
+| `session.delete` | Session | Delete session |
+| `session.rename` | Session | Query/set/clear title |
| `session.pin` | Session | Pin/unpin session |
-| `project.list` | Project | Project list, supports filter by pin state |
-| `project.get_sessions` | Project | Returns non-pinned sessions by project_id |
-| `project.create` | Project | Create project (supports empty path); auto-restore on hidden path match; globally unique name |
-| `project.rename` | Project | Rename |
+| `project.list` | Project | List projects with pin and work_mode filters |
+| `project.get_sessions` | Project | List non-pinned sessions by project |
+| `project.create` | Project | Create/restore project; code projects auto probe/init Git |
+| `project.rename` | Project | Rename project |
| `project.pin` | Project | Pin/unpin project |
-| `project.remove` | Project | Soft delete (hide) |
-| `project.restore` | Project | Restore soft-deleted |
-| `project.pinned_sessions` | Project | Get all pinned sessions, sorted by pin_order |
+| `project.remove` | Project | Soft delete project |
+| `project.restore` | Project | Restore soft-deleted project |
+| `project.pinned_sessions` | Project | List pinned sessions |
+| `project.git.status` | Git | Live Git status, not persisted |
+| `project.git.probe` | Git | Re-probe Git and persist `ProjectInfo.git` |
+| `project.git.init` | Git | Initialize Git repository |
+| `project.git.switch_branch` | Git | Switch branch |
+| `project.git.create_branch` | Git | Create branch, optionally checkout |
+| `project.git.diff_status` | Diff | Query current and last-turn Diff snapshot |
+| `project.git.turn_diff_list` | Diff | List historical turn Diff summaries |
+| `project.git.turn_diff` | Diff | Query one historical turn Diff detail |
+| `project.git.diff_watch` | `/ws/git` | Watch Diff summary |
+| `project.git.diff_files_watch` | `/ws/git` | Watch current/last_turn file list |
+| `project.git.diff_detail_watch` | `/ws/git` | Watch selected file hunks |
+| `project.git.diff_unwatch` | `/ws/git` | Cancel watches |
+| `project.git.discard_turn_changes` | `/ws/git` | Discard the current session's last turn code changes |
diff --git "a/docs/zh/\351\241\271\347\233\256\344\270\216\344\274\232\350\257\235\347\256\241\347\220\206.md" "b/docs/zh/\351\241\271\347\233\256\344\270\216\344\274\232\350\257\235\347\256\241\347\220\206.md"
index a10a3b40b0..08067e9084 100644
--- "a/docs/zh/\351\241\271\347\233\256\344\270\216\344\274\232\350\257\235\347\256\241\347\220\206.md"
+++ "b/docs/zh/\351\241\271\347\233\256\344\270\216\344\274\232\350\257\235\347\256\241\347\220\206.md"
@@ -1,6 +1,6 @@
# 项目与会话管理(Web 端)
-> **实现**:`jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py`(handler 注册)、`jiuwenswarm/server/runtime/session/project_store.py`(项目存储)、`jiuwenswarm/server/runtime/session/session_metadata.py`(会话元数据)、`jiuwenswarm/server/runtime/session/session_rename.py`(重命名共享逻辑)
+> **实现**:`jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py`(handler 注册)、`jiuwenswarm/gateway/channel_manager/web/git_ws_handler.py`(Git Diff WebSocket)、`jiuwenswarm/server/runtime/session/project_store.py`(项目存储)、`jiuwenswarm/server/runtime/session/session_metadata.py`(会话元数据)、`jiuwenswarm/server/runtime/session/project_git.py`(Git 操作)、`jiuwenswarm/server/runtime/session/git_diff_status.py`(Diff 聚合)、`jiuwenswarm/server/runtime/session/git_diff_watcher.py`(Diff 监控)
> **English**: [../en/Project-Session-Management.md](../en/Project-Session-Management.md)
---
@@ -9,15 +9,20 @@
Web 端提供项目(Project)维度的会话管理能力。会话通过 `project_id` 绑定到指定项目,也可使用默认空项目。项目与项目之间通过 `project_id` 建立归属关系。
+当前实现额外引入 `work_mode` 维度,将普通办公协作与代码工程隔离管理。`code` 模式项目支持 Git 状态、分支、Diff 历史、实时监控与撤销本轮文件修改;`work` 模式保持普通项目/会话能力。
+
### 核心概念
| 概念 | 说明 |
|------|------|
| **默认项目** | 不入库,由接口动态注入,`project_id="default"`。禁止 rename / remove / pin。会话 `project_id` 为空或不匹配任何可见项目时归入默认项目 |
+| **工作模式 `work_mode`** | 项目/会话/定时任务的隔离维度,取值 `work` 或 `code`。Web 默认 `work`,TUI 默认 `code` |
+| **代码默认项目** | `project_id="default_code"`,不入库,用于承接未绑定真实 code 项目的会话。与 `default` 分别对应 `code` / `work` 两个默认桶 |
+| **Code 项目 Git 能力** | 真实 `code` 项目开放 Git 状态、初始化、分支、Diff、历史变更和撤销接口。默认项目和 `work` 项目不开放 Git 操作 |
| **软删除** | `project.remove` 标记 `hidden:true`,不删记录,可恢复。隐藏期间其下非置顶会话临时归入默认项目 |
| **置顶会话** | 从项目分组中剥离,通过 `project.pinned_sessions` 独立获取,按 `pin_order` 升序排列 |
| **置顶项目** | 与非置顶项目视觉区分,置顶项目在前(按 `pin_order` 升序) |
-| **project_id / project_dir 不可变** | 会话首次绑定 `project_id` 和 `project_dir` 后不可修改,不提供跨项目迁移接口 |
+| **project_id / project_dir / work_mode 不可变** | 会话首次绑定 `project_id`、`project_dir` 和 `work_mode` 后不可修改,不提供跨项目/跨模式迁移接口 |
### 通信约定
@@ -59,6 +64,23 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
| `last_message_at` | number \| null | 该项目下最新消息时间戳(UTC 秒);无会话为 null |
| `last_user_message_at` | number \| null | 该项目下最新用户消息时间戳(UTC 秒);无用户消息时回退到 `created_at` |
| `created_at` | number | 创建时间戳(UTC 秒);默认项目为 0 |
+| `updated_at` | number | 更新时间戳(UTC 秒);默认项目为 0 |
+| `work_mode` | string | 工作模式,`work` 或 `code` |
+| `git` | object | Git 状态快照。`code` 项目由 Git 探测/操作写入;`work` 与默认项目通常为 disabled/空状态 |
+
+### GitStatus
+
+`ProjectInfo.git` 与 Git RPC 响应中的 `repo` 用于展示代码项目的仓库状态。
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `enabled` | boolean | 项目是否启用 Git 能力(持久化快照字段) |
+| `status` | string | 探测状态,如 `ready` / `not_git` / `git_missing` / `transient` / `error` / `disabled` |
+| `repo_root` | string \| null | Git 仓库根目录 |
+| `branch` | string \| null | 当前分支;detached 时可能为 HEAD 短哈希 |
+| `head` | string \| null | HEAD 短哈希 |
+| `is_dirty` | boolean | 工作区是否有变更 |
+| `error` | object \| null | 结构化 Git 错误,包含 `code`、`message`、`hint` 等 |
### SessionInfo
@@ -76,6 +98,7 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
| `pin_order` | integer | 置顶顺序;未置顶为 0 |
| `project_id` | string | 绑定项目 ID;空串为默认项目 |
| `project_dir` | string | 绑定项目路径;空串为默认项目 |
+| `work_mode` | string | 会话工作模式,`work` 或 `code` |
| `last_user_message_at` | number \| null | 最后一次用户消息时间戳(UTC 秒);无用户消息时回退到 `created_at` |
| `model` | string | 当前使用的模型标识;未设置时为空串 |
@@ -121,16 +144,17 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
| 字段 | 类型 | 说明 |
|------|------|------|
-| `sessions` | object[] | 会话元数据列表(完整字段) |
+| `sessions` | object[] | 会话元数据列表(完整字段,包含 `work_mode`) |
| `total` | integer | 会话总数 |
| `limit` | integer | 实际使用的 limit |
| `offset` | integer | 实际使用的 offset |
+| `has_more` | boolean | 是否还有更多会话 |
---
### session.create — 创建会话
-追加可选参数 `project_dir` 和 `project_id`,不传时行为与现状完全一致。
+追加可选参数 `project_dir`、`project_id` 和 `work_mode`,不传时按通道默认值兼容旧行为。Web 通道默认 `work`,TUI 通道默认 `code`。
**请求参数:**
@@ -140,21 +164,25 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
| `title` | string | 否 | 会话标题 |
| `mode` | string | 否 | 模式,默认 `unknown`(首次 `chat.send` 时由请求参数覆盖为真实模式) |
| `project_dir` | string | 否 | 工作目录绝对路径。不可单独传入——必须同时带有效 `project_id`(非空且非 `"default"`),否则 `BAD_REQUEST`。仅传 `project_id` 未传 `project_dir` 时,由项目记录自动补齐 |
-| `project_id` | string | 否 | 所属项目 ID,绑定后不可变(首次锁定)。非空且非 `"default"` 时必须对应一个存在且可见的项目,否则 `NOT_FOUND`;若同时传了 `project_dir`,必须与项目绑定路径一致(一致性校验),否则 `BAD_REQUEST`。不传或 `"default"`(且 `project_dir` 为空)表示默认空项目 |
+| `project_id` | string | 否 | 所属项目 ID,绑定后不可变(首次锁定)。非空且非 `"default"` / `"default_code"` 时必须对应一个存在且可见的项目,否则 `NOT_FOUND`;若同时传了 `project_dir`,必须与项目绑定路径一致(一致性校验),否则 `BAD_REQUEST`。不传或默认项目 ID(且 `project_dir` 为空)表示默认项目 |
+| `work_mode` | string | 否 | `work` / `code`。未传按通道推断;显式非法值返回 `BAD_REQUEST`;显式值与项目记录不一致时返回 `BAD_REQUEST` |
> 校验 —— `project_id` / `project_dir` 绑定规则:
-> - 两者皆空(或 `project_id="default"` 且 `project_dir` 为空)→ 默认项目(兼容旧版);
+> - 两者皆空(或 `project_id="default"` / `"default_code"` 且 `project_dir` 为空)→ 对应模式的默认项目(兼容旧版);
> - 仅传 `project_id`(无 `project_dir`)→ 按项目记录自动补齐 `project_dir`;
> - 同时传 `project_id` + `project_dir` → 一致性校验(须与项目绑定路径一致,容忍尾部分隔符/大小写差异);
> - 仅传 `project_dir` 而无有效 `project_id` → 拒绝(`BAD_REQUEST`)。
>
-> 此外,`project_dir` 非空时必须为绝对路径,否则 `BAD_REQUEST`。创建时将 `project_dir` 和 `project_id` 写入 session metadata,首次绑定后均不可变。
+> 此外,`project_dir` 非空时必须为绝对路径,否则 `BAD_REQUEST`。创建时将 `project_dir`、`project_id` 和 `work_mode` 写入 session metadata,首次绑定后均不可变。
**响应 payload:**
| 字段 | 类型 | 说明 |
|------|------|------|
| `session_id` | string | 创建的会话 ID |
+| `project_id` | string | 最终绑定的项目 ID |
+| `project_dir` | string | 最终绑定的项目目录 |
+| `work_mode` | string | 最终工作模式 |
**错误码:** `BAD_REQUEST`(`project_dir` 非空且非绝对路径;`project_dir` 未搭配有效 `project_id`;`project_dir` 与项目绑定路径不一致)、`ALREADY_EXISTS`(会话已存在)、`NOT_FOUND`(`project_id` 对应的项目不存在或已隐藏)
@@ -242,12 +270,13 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
|------|------|------|------|
| `filter` | string | 否 | `"all"`(默认) / `"pinned"` / `"unpinned"` |
| `include_hidden` | boolean | 否 | 是否包含已软删除项目,默认 `false`。仅 `"all"` / `"unpinned"` 时生效 |
+| `work_mode` | string | 否 | 按 `work` / `code` 过滤项目。不传返回全部模式 |
**filter 参数语义:**
| 值 | 说明 |
|------|------|
-| `"all"` | 置顶项目在前(按 `pin_order` 升序),其余按 `last_user_message_at` 倒序,末位为默认项目 |
+| `"all"` | 置顶项目在前(按 `pin_order` 升序),其余按 `last_user_message_at` 倒序,末位为默认项目。默认项目按 `work_mode` 拆分为 `default` 与 `default_code` |
| `"pinned"` | 仅置顶项目,按 `pin_order` 升序 |
| `"unpinned"` | 仅非置顶项目,按 `last_user_message_at` 倒序,末位为默认项目 |
@@ -255,7 +284,7 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
| 字段 | 类型 | 说明 |
|------|------|------|
-| `projects` | ProjectInfo[] | 项目列表,已排序 |
+| `projects` | ProjectInfo[] | 项目列表,已排序,包含 `work_mode` 与 `git` 字段 |
**错误码:** 无
@@ -269,7 +298,7 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
-| `project_id` | string | 是 | 项目 ID,传 `"default"` 获取默认项目下会话 |
+| `project_id` | string | 是 | 项目 ID,传 `"default"` 获取 work 默认项目会话,传 `"default_code"` 获取 code 默认项目会话 |
| `limit` | integer | 否 | 返回上限,默认不限 |
| `offset` | integer | 否 | 偏移量,默认 0 |
@@ -286,7 +315,7 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
### project.create — 创建项目
-创建项目,指定工作目录(可选)。若传入的非空 `project_dir` 命中已隐藏项目,自动恢复该项目。支持创建空路径项目(允许多个,靠 `project_id` + `name` 区分)。
+创建项目,指定工作目录(可选)。若传入的非空 `project_dir` 命中同 `work_mode` 的已隐藏项目,自动恢复该项目。支持创建空路径项目(允许多个,靠 `project_id` + `name` + `work_mode` 区分)。
**请求参数:**
@@ -294,6 +323,7 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
|------|------|------|------|
| `name` | string | 是 | 项目展示名(非空,全局唯一含隐藏项目);命中已隐藏项目时用于更新展示名 |
| `project_dir` | string | 否 | 工作目录绝对路径;不传或空串创建空路径项目 |
+| `work_mode` | string | 否 | `work` / `code`;未传按通道推断。code 项目创建后自动探测或初始化 Git |
**响应 payload:**
@@ -302,8 +332,13 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
| `project_id` | string | 项目 ID(新建为 `proj_<8位hex>`,恢复为原 ID) |
| `project_dir` | string | 项目路径(空路径项目为空串) |
| `restored` | boolean | `true`=恢复了已隐藏项目,`false`=新建 |
+| `project` | ProjectInfo | 完整项目对象 |
+| `work_mode` | string | 项目工作模式 |
+| `git` | object | Git 快照。code 项目可能为 `ready` / `not_git` / `git_missing` 等;work 项目为 disabled/空状态 |
-> **自动恢复**: 非空 `project_dir` 命中已隐藏项目时,置 `hidden:false` 并更新展示名,其下会话因 `project_id` 仍匹配自动重新归属。`project_dir` 与已有可见项目重复,或 `name` 与已有项目(含隐藏)重复时返回 `CONFLICT`。
+> **自动恢复**: 非空 `project_dir` 命中同 `work_mode` 的已隐藏项目时,置 `hidden:false` 并更新展示名,其下会话因 `project_id` 仍匹配自动重新归属。`project_dir` 与同模式已有可见项目重复,或 `name` 与同模式已有项目(含隐藏)重复时返回 `CONFLICT`。
+>
+> **Code 项目 Git 探测**: 新建 code 项目时,空目录会自动 `git init`;已是 Git 仓库写入 `ready`;非空非 Git 目录写入 `not_git`;系统无 Git 写入 `git_missing`。恢复隐藏项目时只刷新探测快照,不自动重建 `.git`。
**错误码:** `BAD_REQUEST`(`name` 为空、`project_dir` 非空且非绝对路径)、`CONFLICT`(`project_dir` 与已有可见项目重复、`name` 与已有项目重复)
@@ -311,7 +346,7 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
### project.rename — 重命名项目
-仅修改展示名,不改动工作目录路径。`name` 与已有项目(含隐藏)重复时返回 `CONFLICT`。
+仅修改展示名,不改动工作目录路径。`name` 与同 `work_mode` 已有项目(含隐藏)重复时返回 `CONFLICT`。
**请求参数:**
@@ -375,7 +410,7 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
### project.restore — 恢复项目
-将已软删除的项目恢复为可见。其下会话因 `project_id` 仍匹配,自动重新归属。恢复后 `name` 与已有项目(含隐藏)重复时返回 `CONFLICT`。
+将已软删除的项目恢复为可见。其下会话因 `project_id` 仍匹配,自动重新归属。恢复后 `name` 与同 `work_mode` 已有项目(含隐藏)重复时返回 `CONFLICT`。
**请求参数:**
@@ -411,20 +446,390 @@ Web 端提供项目(Project)维度的会话管理能力。会话通过 `proj
---
-## 6. 接口总览
+## 6. Git 与 Diff 接口(Code 项目)
+
+以下接口仍走普通 `/ws` WebSocket 请求信封,用于 code 项目的 Git 状态、分支和 Diff 快照查询。仅真实 `work_mode="code"` 项目可用;默认项目、`work` 项目、隐藏项目或空路径项目会被拒绝。
+
+### GitStatusPayload
+
+`project.git.status` / `probe` / `init` 以及分支操作中的 `status` 使用此结构。
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `project_id` | string | 项目 ID |
+| `project_name` | string | 项目名称 |
+| `project_dir` | string | 项目工作目录 |
+| `work_mode` | string | 恒为 `code` |
+| `repo` | object | 仓库信息:`is_git`、`repo_root`、`branch`、`head`、`detached`、`transient`、`upstream` |
+| `working_tree` | object | 工作区统计:`is_dirty`、`staged`、`unstaged`、`untracked`、`conflicted` |
+| `branches` | object | 分支列表:`current`、`locals`、`remotes` |
+| `generated_at` | number | 生成时间戳(UTC 秒) |
+
+> `project.git.status` 是实时读取工作区状态,不写入项目存储;`project.git.probe` 会重新探测并刷新 `ProjectInfo.git` 持久化快照,同时唤醒 `/ws/git` 监控重算。
+
+### DiffSummary / DiffFileEntry / Hunk
+
+Diff 接口中的 `current` 表示当前工作区相对 Git 基线的变更,`last_turn` 表示指定会话上一轮 agent 产生的变更。
+
+| 结构 | 关键字段 | 说明 |
+|------|----------|------|
+| `DiffSummary` | `stats`、`files` | 汇总统计和文件映射;未请求文件时 `files` 为空对象 |
+| `DiffFileEntry` | `file_path`、`status`、`change_type`、`lines_added`、`lines_removed`、`is_binary`、`is_new_file`、`is_deleted_file`、`is_untracked`、`is_large_file`、`is_truncated`、`hunks` | 单文件变更。`include_hunks=false` 时不返回 `hunks` |
+| `Hunk` | `old_start`、`old_lines`、`new_start`、`new_lines`、`lines` | 文件 diff 片段。hunk 即一个连续变更块,用于定位旧文件/新文件中的起止行和具体增删上下文 |
+
+> 非 Git 或 Git 不可用时,`diff_status` 可退化返回 `repo.is_git=false`、`current=null`;若有 file_ops 历史,`last_turn` 仍可返回。merge/rebase/cherry-pick 等中间状态下 `repo.transient=true`,当前 Diff 置空,历史详情接口会返回 `GIT_TRANSIENT_STATE`。
+
+### project.git.status — 查询 Git 状态
+
+实时读取项目目录的 Git 状态,用于状态栏、分支选择器和写操作前校验。不刷新 `ProjectInfo.git`,不触发 watcher。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+
+**响应 payload:** `GitStatusPayload`。
+
+**错误码:** `BAD_REQUEST`、`PROJECT_NOT_FOUND`、`FORBIDDEN`、`GIT_NOT_FOUND`、`NOT_GIT_REPOSITORY`
+
+---
+
+### project.git.probe — 重新探测并写回 Git 快照
+
+重新探测项目目录是否为 Git 仓库、当前分支、HEAD、工作区脏状态等,并写回项目记录的 `git` 字段。适用于用户手动 `git init`、安装 Git 后刷新、删除 `.git` 后重新识别等场景。成功后唤醒 `/ws/git` 监控。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+
+**响应 payload:** `GitStatusPayload`。
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`FORBIDDEN`、`GIT_NOT_FOUND`、`NOT_GIT_REPOSITORY`
+
+---
+
+### project.git.init — 初始化 Git 仓库
+
+对 code 项目目录执行 `git init`,用于非 Git 目录经用户确认后初始化,或创建项目时 Git 初始化失败后的重试。成功后刷新项目 Git 快照并唤醒 `/ws/git` 监控。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `initial_branch` | string | 否 | 初始分支名,默认 `main` |
+
+**响应 payload:** `GitStatusPayload`。
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`FORBIDDEN`、`GIT_NOT_FOUND`、`GIT_INIT_FAILED`
+
+---
+
+### project.git.switch_branch — 切换分支
+
+切换到已有本地分支。`require_clean=true` 时若工作区存在未提交变更则拒绝切换。成功后刷新状态并触发 Diff watcher 重算。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `branch` | string | 是 | 目标本地分支名 |
+| `require_clean` | boolean | 否 | 是否要求工作区干净,默认 `false` |
+
+**响应 payload:**
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `switched` | boolean | 恒为 `true` |
+| `previous_branch` | string \| null | 切换前分支 |
+| `current_branch` | string \| null | 切换后分支 |
+| `status` | GitStatusPayload | 切换后的完整 Git 状态 |
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`FORBIDDEN`、`GIT_TRANSIENT_STATE`、`BRANCH_NOT_FOUND`、`WORKTREE_DIRTY`、`CHECKOUT_FAILED`
+
+---
+
+### project.git.create_branch — 创建分支
+
+创建本地分支,默认同时切换到新分支。可通过 `start_point` 指定起点;不传时从当前 HEAD 创建。成功后刷新状态并触发 Diff watcher 重算。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `branch` | string | 是 | 新分支名 |
+| `checkout` | boolean | 否 | 创建后是否切换到新分支,默认 `true` |
+| `start_point` | string | 否 | 分支起点,如 commit、tag、远端分支等 |
+
+**响应 payload:**
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `created` | boolean | 恒为 `true` |
+| `checked_out` | boolean | 是否已切换到新分支 |
+| `branch` | string | 新分支名 |
+| `status` | GitStatusPayload | 创建后的完整 Git 状态 |
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`FORBIDDEN`、`GIT_TRANSIENT_STATE`、`BRANCH_EXISTS`、`START_POINT_NOT_FOUND`、`CHECKOUT_FAILED`
+
+---
+
+### project.git.diff_status — 查询当前与上一轮 Diff 快照
+
+一次性拉取当前工作区 Diff 和指定会话上一轮 Diff,用于首次加载、手动刷新和断线重连。实时变化应使用 `/ws/git` 订阅,不依赖此接口轮询。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `session_id` | string | 否 | 会话 ID;传入后返回 `last_turn` |
+| `include_files` | boolean | 否 | 是否返回文件列表,默认 `false` |
+| `include_hunks` | boolean | 否 | 是否返回 hunk 内容,默认 `false`;为 `true` 时隐含 `include_files=true` |
+
+**响应 payload:**
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `project_id` | string | 项目 ID |
+| `session_id` | string \| null | 请求中的会话 ID |
+| `work_mode` | string | 恒为 `code` |
+| `repo` | object | Diff 仓库上下文:`is_git`、`repo_root`、`branch`、`head`、`transient` |
+| `current` | DiffSummary \| null | 当前工作区 Diff;非 Git、Git 缺失或 transient 时可能为 null |
+| `last_turn` | DiffSummary \| null | 上一轮会话 Diff;未传 `session_id` 或无记录时为 null |
+| `generated_at` | number | 生成时间戳(UTC 秒) |
+
+**错误码:** `BAD_REQUEST`、`PROJECT_NOT_FOUND`、`FORBIDDEN`、`GIT_NOT_FOUND`
+
+---
+
+### project.git.turn_diff_list — 查询历史轮次 Diff 摘要
+
+按会话分页返回历史轮次 Diff 摘要,用于“本轮/历史变更”列表。会校验 `session_id` 必须绑定到请求的 `project_id`。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `session_id` | string | 是 | 会话 ID |
+| `limit` | integer | 否 | 返回条数,默认 50;`0` 表示不截断 |
+| `cursor` | integer | 否 | 起始偏移,默认 0 |
+
+**响应 payload:**
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `project_id` | string | 项目 ID |
+| `session_id` | string | 会话 ID |
+| `repo_root` | string \| null | 仓库根目录 |
+| `branch` | string \| null | 当前或历史分支上下文 |
+| `base_head` | string \| null | 基线 HEAD |
+| `turns` | object[] | 轮次摘要列表,包含 `change_set_id`、`turn_index`、消息 ID、统计和文件摘要 |
+| `cursor` | integer | 当前页起始偏移 |
+| `next_cursor` | integer | 下一页偏移 |
+| `has_more` | boolean | 是否还有更多 |
+| `limit` | integer | 实际使用的 limit |
+| `total` | integer | 总轮次数 |
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`SESSION_NOT_FOUND`、`SESSION_NOT_BOUND`、`PROJECT_SESSION_MISMATCH`、`GIT_TRANSIENT_STATE`
+
+---
+
+### project.git.turn_diff — 查询指定轮次 Diff 详情
+
+返回某个历史轮次的完整 Diff。优先使用 `change_set_id` 查询;未传时用 `turn_index` 查询。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `session_id` | string | 是 | 会话 ID |
+| `change_set_id` | string | 否 | 变更集 ID,优先级高于 `turn_index` |
+| `turn_index` | integer | 否 | 轮次序号;未传 `change_set_id` 时必填 |
+| `include_files` | boolean | 否 | 是否返回文件列表,默认 `true` |
+| `include_hunks` | boolean | 否 | 是否返回 hunk 内容,默认 `true`;为 `true` 时隐含 `include_files=true` |
+
+**响应 payload:** 单个轮次 Diff 详情,包含 `project_id`、`session_id`、`repo_root`、`branch`、`base_head`、`change_set_id`、`turn_index`、消息 ID、`stats` 与 `files`。
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`SESSION_NOT_FOUND`、`SESSION_NOT_BOUND`、`PROJECT_SESSION_MISMATCH`、`CHANGE_SET_NOT_FOUND`、`TURN_DIFF_NOT_FOUND`、`DIFF_HISTORY_EXPIRED`、`GIT_TRANSIENT_STATE`
+
+---
+
+## 7. /ws/git 实时监控与撤销接口
+
+`/ws/git` 是 Git Diff 专用 WebSocket。请求/响应信封与普通 `/ws` 一致,但除了响应请求外,还会主动推送事件:
+
+| 事件 | 触发时机 | payload 简述 |
+|------|----------|--------------|
+| `project.git.diff_changed` | summary 指纹变化 | `project_id`、`session_id`、`repo`、`current`、`last_turn`、`revision` |
+| `project.git.diff_files_changed` | 已订阅来源的文件列表变化 | `project_id`、`session_id`、`source`、`revision`、`files`;`last_turn` 来源额外包含轮次元信息 |
+| `project.git.diff_detail_changed` | 已订阅文件的 hunk 变化 | `project_id`、`session_id`、`source`、`revision`、`files`;`last_turn` 来源额外包含轮次元信息 |
+| `project.git.error` | 监控重算失败 | `project_id`、`session_id`、`code`、`message`、`detail` |
+
+### project.git.diff_watch — 订阅 Diff summary
+
+创建 summary 级 watcher。首次响应只返回统计快照,文件列表固定为空;后续变化通过 `project.git.diff_changed` 推送。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `session_id` | string | 否 | 会话 ID,用于计算 `last_turn` |
+| `scope` | string | 否 | 当前仅支持 `summary`,默认 `summary` |
+| `include_last_turn` | boolean | 否 | 是否监控上一轮 Diff,默认 `true` |
+
+**响应 payload:**
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `watch_id` | string | 本次监控 ID,供后续 files/detail/unwatch 使用 |
+| `scope` | string | 恒为 `summary` |
+| `snapshot` | object | 首次快照,包含 `project_id`、`session_id`、`repo`、`current`、`last_turn`、`revision` |
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`FORBIDDEN`、Git 领域错误码
+
+---
+
+### project.git.diff_files_watch — 订阅文件列表
+
+在已有 `watch_id` 上订阅或切换某个来源的文件列表。首次响应返回当前文件列表(不含 hunk),后续列表变化通过 `project.git.diff_files_changed` 推送。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `watch_id` | string | 是 | `diff_watch` 返回的监控 ID |
+| `source` | string | 是 | `current` 或 `last_turn` |
+| `session_id` | string | 否 | 覆盖 watcher 上的 session_id;通常不传 |
+
+**响应 payload:**
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `watch_id` | string | 监控 ID |
+| `files_scope` | object | `{ "source": "current|last_turn" }` |
+| `revision` | string | 快照版本 |
+| `files` | object | 文件路径到 `DiffFileEntry` 的映射,不含 `hunks` |
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`FORBIDDEN`、Git 领域错误码
+
+---
+
+### project.git.diff_detail_watch — 订阅文件详情
+
+在已有 `watch_id` 上订阅具体文件的 hunk。每次调用会替换该 watcher 的 detail 订阅范围。只有订阅文件的内容变化时才推送详情事件。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `watch_id` | string | 是 | `diff_watch` 返回的监控 ID |
+| `source` | string | 是 | `current` 或 `last_turn` |
+| `files` | string[] | 是 | 要订阅详情的文件路径列表 |
+| `session_id` | string | 否 | 覆盖 watcher 上的 session_id;通常不传 |
+
+**响应 payload:**
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `watch_id` | string | 监控 ID |
+| `detail_scope` | object | `{ "source": "...", "files": [...] }` |
+| `revision` | string | 快照版本 |
+| `files` | object | 请求路径到 `DiffFileEntry` 的映射;找不到的文件值为 `null`;包含 `hunks` |
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`FORBIDDEN`、Git 领域错误码
+
+---
+
+### project.git.diff_unwatch — 取消监控
+
+释放 watcher 资源。`scope="all"` 移除整个 watcher;`files`/`detail` 仅取消对应子订阅并保留 summary。`watch_id` 不存在时幂等成功。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `watch_id` | string | 是 | 监控 ID |
+| `scope` | string | 否 | `all` / `files` / `detail`,默认 `all` |
+
+**响应 payload:**
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `watch_id` | string | 监控 ID |
+| `cancelled` | boolean | 恒为 `true` |
+| `scope` | string | 实际取消范围 |
+
+**错误码:** `BAD_REQUEST`
+
+---
+
+### project.git.discard_turn_changes — 撤销本轮代码修改
+
+撤销当前会话最后一轮 agent 通过文件工具产生的代码修改,将受影响文件恢复到该轮开始前的状态,或删除本轮新建文件。成功后清理本轮 session-specific file_ops 日志并唤醒 Diff watcher;若部分文件恢复失败,返回 `ok=false` 且保留 file_ops 以便重试。
+
+**请求参数:**
+
+| 参数 | 类型 | 必填 | 说明 |
+|------|------|------|------|
+| `project_id` | string | 是 | code 项目 ID |
+| `session_id` | string | 是 | 会话 ID,必须绑定到该项目 |
+
+**响应 payload:**
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `session_id` | string | 会话 ID |
+| `turn_index` | integer | 被撤销的最后一轮序号 |
+| `change_set_id` | string \| null | 被标记为已撤销的变更集 ID;部分失败时为 null |
+| `restored_files` | string[] | 已恢复到旧内容的文件 |
+| `deleted_files` | string[] | 已删除的新建文件 |
+| `errors` | object[] | 单文件恢复失败信息;为空表示完全成功 |
+| `file_ops_truncated` | boolean | 是否已清理本轮 session-specific file_ops |
+| `global_file_ops_truncated` | boolean | 恒为 `false`,全局 file_ops 不按路径清理以避免误伤其他会话 |
+| `partial` | boolean | 是否部分失败;为 `true` 时顶层 `ok=false`、`code=PARTIAL_RESTORE_FAILED` |
+
+**错误码:** `BAD_REQUEST`、`NOT_FOUND`、`FORBIDDEN`、`SESSION_NOT_BOUND`、`PROJECT_SESSION_MISMATCH`、`SESSION_BUSY`、`NO_TURN_TO_DISCARD`、`PARTIAL_RESTORE_FAILED`
+
+---
+
+## 8. 接口总览
| 方法 | 归属 | 说明 |
|------|------|------|
| `session.list` | 会话 | 扁平分页,不变 |
-| `session.create` | 会话 | 追加可选参数 `project_dir` / `project_id`,默认值兼容现有行为 |
+| `session.create` | 会话 | 追加可选参数 `project_dir` / `project_id` / `work_mode`,默认值兼容现有行为 |
| `session.delete` | 会话 | 不变 |
| `session.rename` | 会话 | Web 本地 handler,查询/设置/清除 |
| `session.pin` | 会话 | 会话置顶/取消 |
-| `project.list` | 项目 | 项目列表,支持 filter 按置顶状态过滤 |
+| `project.list` | 项目 | 项目列表,支持 filter 与 work_mode 过滤 |
| `project.get_sessions` | 项目 | 按 project_id 返回非置顶会话列表 |
-| `project.create` | 项目 | 创建项目(支持空路径);命中已隐藏路径自动恢复;名称全局唯一 |
+| `project.create` | 项目 | 创建项目(支持空路径与 work_mode);命中已隐藏路径自动恢复;code 项目自动探测/初始化 Git |
| `project.rename` | 项目 | 重命名 |
| `project.pin` | 项目 | 项目置顶/取消 |
| `project.remove` | 项目 | 软删除(隐藏) |
| `project.restore` | 项目 | 恢复软删除 |
| `project.pinned_sessions` | 项目 | 获取全部置顶会话,按 pin_order 排序 |
+| `project.git.status` | Git | 实时查询 Git 状态,不写回项目快照 |
+| `project.git.probe` | Git | 重新探测 Git 并写回 `ProjectInfo.git` |
+| `project.git.init` | Git | 初始化 Git 仓库并刷新快照 |
+| `project.git.switch_branch` | Git | 切换已有本地分支 |
+| `project.git.create_branch` | Git | 创建分支,可选自动切换 |
+| `project.git.diff_status` | Diff | 一次性查询当前工作区与上一轮 Diff |
+| `project.git.turn_diff_list` | Diff | 查询历史轮次 Diff 摘要列表 |
+| `project.git.turn_diff` | Diff | 查询指定历史轮次 Diff 详情 |
+| `project.git.diff_watch` | `/ws/git` | 订阅 Diff summary 实时变化 |
+| `project.git.diff_files_watch` | `/ws/git` | 订阅 current/last_turn 文件列表变化 |
+| `project.git.diff_detail_watch` | `/ws/git` | 订阅指定文件 hunk 变化 |
+| `project.git.diff_unwatch` | `/ws/git` | 取消 summary/files/detail 监控 |
+| `project.git.discard_turn_changes` | `/ws/git` | 撤销当前会话最后一轮代码修改 |