From e87f022dd42ab08f5929162a2bd742495e2e01a6 Mon Sep 17 00:00:00 2001
From: dengm
Date: Sat, 16 May 2026 18:22:28 +0800
Subject: [PATCH 01/90] feat: add manual MCP server reconnect with secondary
menu
Replace automatic retry with user-initiated reconnect:
- Failed servers show error details and a [Reconnect] option
- Reconnect reads latest config from disk (no restart needed)
- Single attempt per reconnect, no backoff/retry
---
src/mcp/mcp-client.ts | 34 ++++-
src/mcp/mcp-manager.ts | 266 +++++++++++++++++++++++---------------
src/session.ts | 4 +
src/tests/session.test.ts | 55 ++++++++
src/ui/App.tsx | 9 +-
src/ui/McpStatusList.tsx | 129 ++++++++++++------
6 files changed, 346 insertions(+), 151 deletions(-)
diff --git a/src/mcp/mcp-client.ts b/src/mcp/mcp-client.ts
index 9636732..3651c88 100644
--- a/src/mcp/mcp-client.ts
+++ b/src/mcp/mcp-client.ts
@@ -106,19 +106,24 @@ export class McpClient {
>();
private stderrBuffer = "";
private notificationHandler: McpNotificationHandler | null = null;
+ private disconnectHandler: ((reason: string) => void) | null = null;
+ private intentionallyDisconnected = false;
constructor(
private readonly serverName: string,
private readonly command: string,
private readonly args: string[] = [],
private readonly env?: Record,
- onNotification?: McpNotificationHandler
+ onNotification?: McpNotificationHandler,
+ onDisconnect?: (reason: string) => void
) {
this.notificationHandler = onNotification ?? null;
+ this.disconnectHandler = onDisconnect ?? null;
}
async connect(timeoutMs: number): Promise {
return new Promise((resolve, reject) => {
+ this.intentionallyDisconnected = false;
const childEnv = {
...process.env,
...this.env,
@@ -144,17 +149,35 @@ export class McpClient {
});
}
+ let resolved = false;
+ const safeReject = (err: Error) => {
+ if (!resolved) {
+ resolved = true;
+ reject(err);
+ }
+ };
+
this.process.on("error", (err) => {
- reject(this.withStderr(`Failed to start MCP server "${this.serverName}" (${this.command}): ${err.message}`));
+ safeReject(
+ this.withStderr(`Failed to start MCP server "${this.serverName}" (${this.command}): ${err.message}`)
+ );
});
this.process.on("close", (code) => {
- const error = this.withStderr(`MCP server "${this.serverName}" exited with code ${code}`);
+ const reason = `MCP server "${this.serverName}" exited with code ${code}`;
+ const error = this.withStderr(reason);
for (const [, pending] of this.pendingRequests) {
clearTimeout(pending.timer);
pending.reject(error);
}
this.pendingRequests.clear();
+ this.reader?.close();
+ this.reader = null;
+ this.process = null;
+ if (!this.intentionallyDisconnected && this.disconnectHandler) {
+ this.disconnectHandler(reason);
+ }
+ safeReject(error);
});
if (this.process.stderr) {
@@ -263,6 +286,7 @@ export class McpClient {
}
disconnect(): void {
+ this.intentionallyDisconnected = true;
if (this.reader) {
this.reader.close();
this.reader = null;
@@ -273,6 +297,10 @@ export class McpClient {
}
}
+ isConnected(): boolean {
+ return this.process !== null && this.process.exitCode === null;
+ }
+
private sendRequest(method: string, params: Record, timeoutMs = 30_000): Promise {
return new Promise((resolve, reject) => {
const id = this.nextId++;
diff --git a/src/mcp/mcp-manager.ts b/src/mcp/mcp-manager.ts
index 5a9f553..217e3fc 100644
--- a/src/mcp/mcp-manager.ts
+++ b/src/mcp/mcp-manager.ts
@@ -1,7 +1,9 @@
import { McpClient, type McpToolDefinition, type McpPromptDefinition, type McpResourceDefinition } from "./mcp-client";
import type { McpServerConfig } from "../settings";
-const MCP_STARTUP_TIMEOUT_MS = 30_000;
+const MCP_STARTUP_TIMEOUT_MS = process.env.DEEPCODE_MCP_TIMEOUT
+ ? parseInt(process.env.DEEPCODE_MCP_TIMEOUT, 10)
+ : 30_000;
const MCP_CALL_TOOL_TIMEOUT_MS = 60_000;
type McpToolEntry = {
@@ -14,7 +16,7 @@ type McpToolEntry = {
export type McpServerStatus = {
name: string;
- status: "starting" | "ready" | "failed";
+ status: "starting" | "ready" | "failed" | "reconnecting";
connected: boolean;
error?: string;
toolCount: number;
@@ -46,12 +48,10 @@ export class McpManager {
private serverStatuses: McpServerStatus[] = [];
private onToolsListChanged: (() => void) | null = null;
private onStatusChanged: (() => void) | null = null;
+ private serverConfigs: Record = {};
prepare(servers?: Record): void {
if (!servers || Object.keys(servers).length === 0) return;
- // Clear the disposed flag — a re-prepare means we are live again.
- // (disconnect() sets disposed=true to stop a stale initialize() loop,
- // but prepare+initialize must be able to start a new one.)
this.disposed = false;
for (const name of Object.keys(servers)) {
@@ -81,116 +81,175 @@ export class McpManager {
if (!servers || Object.keys(servers).length === 0) return;
- const entries = Object.entries(servers);
+ this.serverConfigs = servers;
this.prepare(servers);
- for (const [name, config] of entries) {
+ for (const [name, config] of Object.entries(servers)) {
if (this.disposed) break;
- let client: McpClient | null = null;
- try {
- client = new McpClient(name, config.command, config.args ?? [], config.env, (method) => {
+ await this.connectServer(name, config);
+ }
+ }
+
+ async reconnect(name: string, config?: McpServerConfig): Promise {
+ if (this.disposed) return;
+ const effectiveConfig = config ?? this.serverConfigs[name];
+ if (!effectiveConfig) return;
+ if (config) {
+ this.serverConfigs[name] = config;
+ }
+
+ this.setStatus({
+ name,
+ status: "reconnecting",
+ connected: false,
+ error: "Reconnecting...",
+ toolCount: 0,
+ tools: [],
+ promptCount: 0,
+ prompts: [],
+ resourceCount: 0,
+ resources: [],
+ });
+
+ await this.connectServer(name, effectiveConfig);
+ }
+
+ private async connectServer(name: string, config: McpServerConfig): Promise {
+ if (this.disposed) return;
+
+ // Clean up stale entries from previous connection attempts
+ this.clients = this.clients.filter((c) => c.isConnected());
+ this.tools = this.tools.filter((t) => t.serverName !== name);
+ this.prompts = this.prompts.filter((p) => p.serverName !== name);
+ this.resources = this.resources.filter((r) => r.serverName !== name);
+
+ let client: McpClient | null = null;
+ try {
+ client = new McpClient(
+ name,
+ config.command,
+ config.args ?? [],
+ config.env,
+ (method) => {
if (method === "notifications/tools/list_changed") {
- this.refreshServerTools(name, client!).catch(() => {
- // swallow refresh errors
- });
+ this.refreshServerTools(name, client!).catch(() => {});
+ }
+ },
+ (reason) => {
+ if (!this.disposed && this.serverConfigs[name]) {
+ this.onServerCrash(name, reason);
}
- });
- await client.connect(MCP_STARTUP_TIMEOUT_MS);
- if (this.disposed) {
- client.disconnect();
- break;
- }
- this.clients.push(client);
-
- // Discover tools
- const serverTools = await client.listTools(MCP_STARTUP_TIMEOUT_MS);
- if (this.disposed) break;
- const toolNamespacedNames: string[] = [];
- for (const tool of serverTools) {
- const namespacedName = `mcp__${name}__${tool.name}`;
- this.tools.push({
- serverName: name,
- originalName: tool.name,
- namespacedName,
- definition: tool,
- client,
- });
- toolNamespacedNames.push(namespacedName);
- }
-
- // Discover prompts
- let serverPrompts: McpPromptDefinition[] = [];
- try {
- serverPrompts = await client.listPrompts(MCP_STARTUP_TIMEOUT_MS);
- } catch {
- // Server may not support prompts — safe to ignore
- }
- if (this.disposed) break;
- const promptNamespacedNames: string[] = [];
- for (const prompt of serverPrompts) {
- const namespacedName = `mcp__${name}__${prompt.name}`;
- this.prompts.push({
- serverName: name,
- namespacedName,
- definition: prompt,
- client,
- });
- promptNamespacedNames.push(namespacedName);
}
+ );
+ await client.connect(MCP_STARTUP_TIMEOUT_MS);
+ if (this.disposed) {
+ client.disconnect();
+ return;
+ }
+ this.clients.push(client);
- // Discover resources
- let serverResources: McpResourceDefinition[] = [];
- try {
- serverResources = await client.listResources(MCP_STARTUP_TIMEOUT_MS);
- } catch {
- // Server may not support resources — safe to ignore
- }
- if (this.disposed) break;
- const resourceNamespacedNames: string[] = [];
- for (const resource of serverResources) {
- const namespacedName = `mcp__${name}__${resource.name}`;
- this.resources.push({
- serverName: name,
- namespacedName,
- definition: resource,
- client,
- });
- resourceNamespacedNames.push(namespacedName);
- }
+ const serverTools = await client.listTools(MCP_STARTUP_TIMEOUT_MS);
+ if (this.disposed) return;
+ const toolNamespacedNames: string[] = [];
+ for (const tool of serverTools) {
+ const namespacedName = `mcp__${name}__${tool.name}`;
+ this.tools.push({
+ serverName: name,
+ originalName: tool.name,
+ namespacedName,
+ definition: tool,
+ client,
+ });
+ toolNamespacedNames.push(namespacedName);
+ }
- this.setStatus({
- name,
- status: "ready",
- connected: true,
- toolCount: serverTools.length,
- tools: toolNamespacedNames,
- promptCount: serverPrompts.length,
- prompts: promptNamespacedNames,
- resourceCount: serverResources.length,
- resources: resourceNamespacedNames,
+ let serverPrompts: McpPromptDefinition[] = [];
+ try {
+ serverPrompts = await client.listPrompts(MCP_STARTUP_TIMEOUT_MS);
+ } catch {
+ // server may not support prompts
+ }
+ if (this.disposed) return;
+ const promptNamespacedNames: string[] = [];
+ for (const prompt of serverPrompts) {
+ const namespacedName = `mcp__${name}__${prompt.name}`;
+ this.prompts.push({
+ serverName: name,
+ namespacedName,
+ definition: prompt,
+ client,
});
- } catch (err) {
- if (this.disposed) break;
- client?.disconnect();
- const message = err instanceof Error ? err.message : String(err);
- // 不在控制台输出错误日志,避免暴露敏感信息
- // process.stderr.write(`[deepcode] MCP server "${name}" failed to initialize: ${message}\n`);
- this.setStatus({
- name,
- status: "failed",
- connected: false,
- error: message,
- toolCount: 0,
- tools: [],
- promptCount: 0,
- prompts: [],
- resourceCount: 0,
- resources: [],
+ promptNamespacedNames.push(namespacedName);
+ }
+
+ let serverResources: McpResourceDefinition[] = [];
+ try {
+ serverResources = await client.listResources(MCP_STARTUP_TIMEOUT_MS);
+ } catch {
+ // server may not support resources
+ }
+ if (this.disposed) return;
+ const resourceNamespacedNames: string[] = [];
+ for (const resource of serverResources) {
+ const namespacedName = `mcp__${name}__${resource.name}`;
+ this.resources.push({
+ serverName: name,
+ namespacedName,
+ definition: resource,
+ client,
});
+ resourceNamespacedNames.push(namespacedName);
}
+
+ this.setStatus({
+ name,
+ status: "ready",
+ connected: true,
+ toolCount: serverTools.length,
+ tools: toolNamespacedNames,
+ promptCount: serverPrompts.length,
+ prompts: promptNamespacedNames,
+ resourceCount: serverResources.length,
+ resources: resourceNamespacedNames,
+ });
+ } catch (err) {
+ client?.disconnect();
+ const message = err instanceof Error ? err.message : String(err);
+ this.setStatus({
+ name,
+ status: "failed",
+ connected: false,
+ error: message,
+ toolCount: 0,
+ tools: [],
+ promptCount: 0,
+ prompts: [],
+ resourceCount: 0,
+ resources: [],
+ });
}
}
+ private onServerCrash(name: string, reason: string): void {
+ if (this.disposed) return;
+ this.clients = this.clients.filter((c) => c.isConnected());
+ this.tools = this.tools.filter((t) => t.serverName !== name);
+ this.prompts = this.prompts.filter((p) => p.serverName !== name);
+ this.resources = this.resources.filter((r) => r.serverName !== name);
+ this.setStatus({
+ name,
+ status: "failed",
+ connected: false,
+ error: reason,
+ toolCount: 0,
+ tools: [],
+ promptCount: 0,
+ prompts: [],
+ resourceCount: 0,
+ resources: [],
+ });
+ }
+
getStatus(): McpServerStatus[] {
const result = [...this.serverStatuses];
const knownNames = new Set(result.map((s) => s.name));
@@ -345,12 +404,12 @@ export class McpManager {
this.resources = [];
this.serverStatuses = [];
this.configuredServerNames = [];
+ this.serverConfigs = {};
this.initialized = false;
}
private async refreshServerTools(serverName: string, client: McpClient): Promise {
const serverTools = await client.listTools(MCP_STARTUP_TIMEOUT_MS);
- // Remove old tool entries for this server
this.tools = this.tools.filter((t) => t.serverName !== serverName);
const toolNamespacedNames: string[] = [];
for (const tool of serverTools) {
@@ -364,13 +423,11 @@ export class McpManager {
});
toolNamespacedNames.push(namespacedName);
}
- // Update status
const existing = this.serverStatuses.find((s) => s.name === serverName);
if (existing) {
existing.toolCount = serverTools.length;
existing.tools = toolNamespacedNames;
}
- // Notify listener
this.onToolsListChanged?.();
}
@@ -390,7 +447,6 @@ export class McpManager {
} else {
this.serverStatuses[index] = status;
}
- // 触发状态变更回调
this.onStatusChanged?.();
}
}
diff --git a/src/session.ts b/src/session.ts
index 095cd3a..9e97f86 100644
--- a/src/session.ts
+++ b/src/session.ts
@@ -255,6 +255,10 @@ export class SessionManager {
return this.mcpManager.getStatus();
}
+ async reconnectMcpServer(name: string, config?: McpServerConfig): Promise {
+ await this.mcpManager.reconnect(name, config);
+ }
+
dispose(): void {
this.mcpManager.disconnect();
}
diff --git a/src/tests/session.test.ts b/src/tests/session.test.ts
index 50d016c..8ecb85e 100644
--- a/src/tests/session.test.ts
+++ b/src/tests/session.test.ts
@@ -1540,6 +1540,61 @@ test("SessionManager treats OpenAI APIUserAbortError as interrupted", async () =
assert.equal(session?.failReason, "interrupted");
});
+test("SessionManager marks MCP server as failed on single failed attempt (no auto-retry)", async () => {
+ const workspace = createTempDir("deepcode-mcp-fail-noworkspace-");
+ const serverPath = path.join(workspace, "mcp-server-fail.cjs");
+ fs.writeFileSync(serverPath, "process.exit(7);", "utf8");
+
+ const manager = createSessionManager(workspace, "machine-id-mcp-fail-no");
+ await manager.initMcpServers({ broken: { command: process.execPath, args: [serverPath] } });
+
+ const status = manager.getMcpStatus();
+ assert.equal(status.length, 1);
+ assert.equal(status[0]?.status, "failed");
+ assert.match(status[0]?.error ?? "", /exited with code 7/);
+
+ manager.dispose();
+});
+
+test("SessionManager reconnect succeeds on previously failed server", async () => {
+ const workspace = createTempDir("deepcode-mcp-reconn-ok-workspace-");
+ const serverPath = path.join(workspace, "mcp-server-ok.cjs");
+ fs.writeFileSync(
+ serverPath,
+ `
+const readline = require("readline");
+const rl = readline.createInterface({ input: process.stdin, crlfDelay: Infinity });
+function send(message) {
+ process.stdout.write(JSON.stringify(message) + "\\n");
+}
+rl.on("line", (line) => {
+ const request = JSON.parse(line);
+ if (!("id" in request)) return;
+ if (request.method === "initialize") {
+ send({ jsonrpc: "2.0", id: request.id, result: { protocolVersion: "2024-11-05", capabilities: {} } });
+ return;
+ }
+ if (request.method === "tools/list") {
+ send({ jsonrpc: "2.0", id: request.id, result: { tools: [{ name: "ping", inputSchema: { type: "object", properties: {} } }] } });
+ return;
+ }
+ send({ jsonrpc: "2.0", id: request.id, result: { content: [] } });
+});
+`,
+ "utf8"
+ );
+
+ const manager = createSessionManager(workspace, "machine-id-mcp-reconn-ok");
+ await manager.initMcpServers({ fixable: { command: process.execPath, args: [serverPath] } });
+
+ const status = manager.getMcpStatus();
+ assert.equal(status.length, 1);
+ assert.equal(status[0]?.status, "ready");
+ assert.equal(status[0]?.toolCount, 1);
+
+ manager.dispose();
+});
+
function createSessionManager(projectRoot: string, machineId: string): SessionManager {
return new SessionManager({
projectRoot,
diff --git a/src/ui/App.tsx b/src/ui/App.tsx
index bafb412..1f12198 100644
--- a/src/ui/App.tsx
+++ b/src/ui/App.tsx
@@ -455,7 +455,14 @@ export function App({ projectRoot, version = "", onRestart }: AppProps): React.R
onCancel={() => setView("chat")}
/>
) : view === "mcp-status" ? (
- setView("chat")} />
+ setView("chat")}
+ onReconnect={(name) => {
+ const latest = resolveCurrentSettings(projectRoot);
+ void sessionManager.reconnectMcpServer(name, latest.mcpServers?.[name]);
+ }}
+ />
) : shouldShowQuestionPrompt && pendingQuestion && !busy ? (
void;
+ onReconnect: (name: string) => void;
};
-export function McpStatusList({ statuses, onCancel }: Props): React.ReactElement {
+export function McpStatusList({ statuses, onCancel, onReconnect }: Props): React.ReactElement {
const { columns, rows } = useWindowSize();
// 视图模式:server-list(服务器列表) 或 server-detail(服务器详情)
@@ -20,10 +21,10 @@ export function McpStatusList({ statuses, onCancel }: Props): React.ReactElement
setViewMode("server-list");
}, []);
- // 进入服务器详情
+ // 进入服务器详情(允许 ready、failed、reconnecting 状态)
const enterDetail = useCallback(() => {
const server = statuses[selectedServerIndex];
- if (server && server.status === "ready") {
+ if (server && (server.status === "ready" || server.status === "failed" || server.status === "reconnecting")) {
setViewMode("server-detail");
}
}, [statuses, selectedServerIndex]);
@@ -59,6 +60,7 @@ export function McpStatusList({ statuses, onCancel }: Props): React.ReactElement
server={statuses[selectedServerIndex]}
onBack={goBack}
onCancel={onCancel}
+ onReconnect={onReconnect}
rows={rows}
columns={columns}
/>
@@ -173,6 +175,7 @@ function ServerListView({
const readyCount = statuses.filter((s) => s.status === "ready").length;
const startingCount = statuses.filter((s) => s.status === "starting").length;
+ const reconnectingCount = statuses.filter((s) => s.status === "reconnecting").length;
const failedCount = statuses.filter((s) => s.status === "failed").length;
return (
@@ -198,6 +201,11 @@ function ServerListView({
{startingCount} starting,
+ {reconnectingCount > 0 && (
+
+ {reconnectingCount} reconnecting,
+
+ )}
{failedCount} failed
@@ -257,15 +265,23 @@ function ServerRow({
selected: boolean;
labelColumnWidth: number;
}): React.ReactElement {
- const icon = status.status === "ready" ? "✓" : status.status === "failed" ? "✗" : "●";
- const color = status.status === "ready" ? "green" : status.status === "failed" ? "red" : "yellow";
+ const icon =
+ status.status === "ready" ? "✓" : status.status === "failed" ? "✗" : status.status === "reconnecting" ? "↻" : "●";
+ const color =
+ status.status === "ready"
+ ? "green"
+ : status.status === "failed"
+ ? "red"
+ : status.status === "reconnecting"
+ ? "#ff9900"
+ : "yellow";
// 加载动画:循环显示 (空) → . → .. → ... → (空) → ...
const [dots, setDots] = React.useState(0);
React.useEffect(() => {
- if (status.status !== "starting") return;
+ if (status.status !== "starting" && status.status !== "reconnecting") return;
const interval = setInterval(() => {
- setDots((d) => (d + 1) % 4); // 0 → 1 → 2 → 3 → 0 ...
+ setDots((d) => (d + 1) % 4);
}, 500);
return () => clearInterval(interval);
}, [status.status]);
@@ -275,7 +291,9 @@ function ServerRow({
? `Ready (${status.toolCount} tools, ${status.promptCount} prompts, ${status.resourceCount} resources)`
: status.status === "failed"
? `Failed`
- : "Starting" + (dots > 0 ? ".".repeat(dots) : " "); // 动态显示 (空) / . / .. / ...
+ : status.status === "reconnecting"
+ ? `Reconnecting${dots > 0 ? ".".repeat(dots) : " "}`
+ : "Starting" + (dots > 0 ? ".".repeat(dots) : " ");
return (
@@ -293,8 +311,10 @@ function ServerRow({
- {/* Error message for failed servers */}
- {status.status === "failed" && status.error ? : null}
+ {/* Error message for failed or reconnecting servers */}
+ {(status.status === "failed" || status.status === "reconnecting") && status.error ? (
+
+ ) : null}
);
}
@@ -304,59 +324,54 @@ function ServerDetailView({
server,
onBack,
onCancel,
+ onReconnect,
rows,
columns,
}: {
server: McpServerStatus;
onBack: () => void;
onCancel: () => void;
+ onReconnect: (name: string) => void;
rows: number;
columns: number;
}): React.ReactElement {
- const [activeIndex, setActiveIndex] = useState(0);
+ const [activeIndex, setActiveIndex] = React.useState(0);
+ const hasReconnect = server.status === "failed";
+ const canScroll = server.status === "ready";
- // 合并所有 items(tools, prompts, resources)
+ // 合并所有 items(tools, prompts, resources)+ Reconnect 选项
const allItems = useMemo(() => {
const items: { type: string; name: string }[] = [];
+ if (hasReconnect) {
+ items.push({ type: "action", name: "Reconnect" });
+ }
server.tools.forEach((tool) => items.push({ type: "tool", name: tool }));
server.prompts.forEach((prompt) => items.push({ type: "prompt", name: prompt }));
server.resources.forEach((resource) => items.push({ type: "resource", name: resource }));
return items;
- }, [server]);
+ }, [server, hasReconnect]);
const totalItems = allItems.length;
const maxVisible = useMemo(() => {
- const reservedLines = 10; // header + title + stats + footer + borders
+ const reservedLines = 12; // header + title + stats + error + footer + borders
const availableLines = Math.max(0, Math.min(rows, 30) - reservedLines);
return Math.max(1, availableLines);
}, [rows]);
- // 使用 ref 跟踪 visibleStart,避免循环依赖
const visibleStartRef = React.useRef(0);
- // 计算可见窗口起始位置:当 activeIndex 超出可见区域时才滚动(类似终端光标行为)
const visibleStart = useMemo(() => {
if (totalItems === 0) return 0;
-
const currentStart = visibleStartRef.current;
let newStart = currentStart;
-
- // 如果 activeIndex 在当前可见窗口之前,滚动到 activeIndex
if (activeIndex < currentStart) {
newStart = activeIndex;
- }
- // 如果 activeIndex 在当前可见窗口之后,滚动到 activeIndex
- else if (activeIndex >= currentStart + maxVisible) {
+ } else if (activeIndex >= currentStart + maxVisible) {
newStart = activeIndex - maxVisible + 1;
}
-
- // 限制在合法范围内
newStart = Math.max(0, Math.min(newStart, Math.max(0, totalItems - maxVisible)));
-
- // 更新 ref
visibleStartRef.current = newStart;
-
return newStart;
}, [activeIndex, maxVisible, totalItems]);
@@ -371,11 +386,16 @@ function ServerDetailView({
onBack();
return;
}
- // Space 或 Enter 键返回一级菜单
- if (input === " " || key.return) {
+ if (key.return || input === " ") {
+ if (activeIndex === 0 && hasReconnect) {
+ onReconnect(server.name);
+ onBack();
+ return;
+ }
onBack();
return;
}
+ if (!canScroll && !hasReconnect) return;
if (key.upArrow) {
setActiveIndex((prev) => Math.max(0, prev - 1));
return;
@@ -384,25 +404,33 @@ function ServerDetailView({
setActiveIndex((prev) => Math.min(totalItems - 1, prev + 1));
return;
}
- if (key.pageUp) {
+ if (key.pageUp && canScroll) {
setActiveIndex((prev) => Math.max(0, prev - maxVisible));
return;
}
- if (key.pageDown) {
+ if (key.pageDown && canScroll) {
setActiveIndex((prev) => Math.min(totalItems - 1, prev + maxVisible));
return;
}
- if (key.home) {
+ if (key.home && canScroll) {
setActiveIndex(0);
return;
}
- if (key.end) {
+ if (key.end && canScroll) {
setActiveIndex(totalItems - 1);
}
});
- const icon = "✓";
- const color = "green";
+ const statusIcon =
+ server.status === "ready" ? "✓" : server.status === "failed" ? "✗" : server.status === "reconnecting" ? "↻" : "●";
+ const statusColor =
+ server.status === "ready"
+ ? "green"
+ : server.status === "failed"
+ ? "red"
+ : server.status === "reconnecting"
+ ? "#ff9900"
+ : "yellow";
return (
{/* Header row */}
- {icon}
+ {statusIcon}
{server.name}
- — Details
+ — {server.status === "ready" ? "Details" : "Status"}
{/* Server info */}
- {server.toolCount} tools, {server.promptCount} prompts, {server.resourceCount} resources
+ {server.status === "ready"
+ ? `${server.toolCount} tools, ${server.promptCount} prompts, ${server.resourceCount} resources`
+ : `Status: ${server.status}`}
+ {/* Error for failed/reconnecting */}
+ {server.error && (server.status === "failed" || server.status === "reconnecting") ? (
+
+
+
+ ) : null}
{/* Items list */}
{/* Footer */}
- ↑/↓ scroll · Space/Enter back · Esc back · Ctrl+C close
+
+ {hasReconnect
+ ? "Enter to reconnect · Esc back · Ctrl+C close"
+ : canScroll
+ ? "↑/↓ scroll · Space/Enter back · Esc back · Ctrl+C close"
+ : "Space/Enter back · Esc back · Ctrl+C close"}
+
@@ -481,13 +523,16 @@ function ServerDetailView({
}
function ItemRow({ item, selected }: { item: { type: string; name: string }; selected: boolean }): React.ReactElement {
- const icon = item.type === "tool" ? "🔧" : item.type === "prompt" ? "📝" : "📦";
+ const isAction = item.type === "action";
+ const icon = isAction ? "↻" : item.type === "tool" ? "🔧" : item.type === "prompt" ? "📝" : "📦";
+ const color = isAction && selected ? "#ff9900" : selected ? "#229ac3" : undefined;
return (
+ {selected ? "> " : " "}
{icon}
-
- {item.name}
+
+ {isAction ? `[${item.name}]` : item.name}
);
From ac223020cdbca70c145f93608066f21b942f54e2 Mon Sep 17 00:00:00 2001
From: Ji Zhang
Date: Mon, 18 May 2026 09:33:40 +0800
Subject: [PATCH 02/90] feat: add update plan tool
---
docs/SKILL.md | 259 ++++++++++++++++++++++++++++++
docs/SKILL_new.md | 264 +++++++++++++++++++++++++++++++
src/prompt.ts | 24 +++
src/tests/prompt.test.ts | 13 ++
src/tests/tool-handlers.test.ts | 26 +++
src/tools/executor.ts | 2 +
src/tools/update-plan-handler.ts | 23 +++
src/ui/MessageView.tsx | 31 ++++
templates/tools/update-plan.md | 38 +++++
9 files changed, 680 insertions(+)
create mode 100644 docs/SKILL.md
create mode 100644 docs/SKILL_new.md
create mode 100644 src/tools/update-plan-handler.ts
create mode 100644 templates/tools/update-plan.md
diff --git a/docs/SKILL.md b/docs/SKILL.md
new file mode 100644
index 0000000..f6d7149
--- /dev/null
+++ b/docs/SKILL.md
@@ -0,0 +1,259 @@
+---
+name: plan-and-execute
+description: Automatically plan and execute tasks from issue documents. Reads issue requirements, creates a task list at the end of the document, and systematically executes each task while updating progress. Use when working with issue documents, task planning, or when you need to break down and execute complex multi-step requirements.
+---
+
+# Plan and Execute
+
+This Skill helps you automatically plan and execute tasks based on issue documents. It reads your requirements, creates a structured task list directly in the document, and systematically works through each task while keeping the document updated with progress.
+
+## Quick Start
+
+When you need to work through an issue document:
+
+1. The Skill will first ask you for the issue document path
+2. It reads the document to understand requirements
+3. Creates a task list at the end of the document
+4. Executes tasks one by one, updating status in real-time
+
+## Instructions
+
+### Step 1: Get the issue document path
+
+Ask the user for the path to their issue document:
+
+```
+What is the path to your issue document?
+```
+
+The document can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions.
+
+### Step 2: Read and analyze the issue document
+
+Use the Read tool to load the document content and analyze:
+
+- What are the main requirements?
+- What tasks need to be completed?
+- Are there dependencies between tasks?
+- What is the complexity level?
+
+### Step 3: Create the task list
+
+Create a structured task list at the END of the issue document using this format:
+
+```markdown
+## Task List
+
+- [ ] Task 1 description
+- [ ] Task 2 description
+- [ ] Task 3 description
+
+### Task Status Legend
+- [ ] Pending
+- [>] In Progress
+- [x] Completed
+```
+
+Use the Edit tool to append this section to the document. Break down complex requirements into specific, actionable tasks.
+
+### Step 4: Execute tasks systematically
+
+For each task in the list:
+
+1. **Mark as in progress**: Update the task in the document from `[ ]` to `[>]`
+2. **Execute the task**: Use appropriate tools to complete the work
+3. **Mark as completed**: Update the task from `[>]` to `[x]` when finished
+4. **Move to next task**: Only ONE task should be in progress at a time
+
+Important rules:
+- Always update the document BEFORE starting work on a task
+- Always update the document IMMEDIATELY after completing a task
+- Never work on multiple tasks simultaneously
+- If you encounter errors, keep the task as `[>]` and create new tasks to resolve blockers
+
+### Step 5: Handle task breakdown
+
+If during execution you discover a task is more complex than expected:
+
+1. Keep the current task as `[>]`
+2. Add new sub-tasks below it with indentation:
+ ```markdown
+ - [>] Main task
+ - [ ] Sub-task 1
+ - [ ] Sub-task 2
+ ```
+3. Complete sub-tasks first, then mark the main task as complete
+
+### Step 6: Final verification
+
+After all tasks are completed (`[x]`):
+
+1. Review the issue requirements to ensure everything is addressed
+2. Run any final checks (tests, builds, linting)
+3. Add a completion summary at the end of the document
+
+## Task State Symbols
+
+- `[ ]` - **Pending**: Not started yet
+- `[>]` - **In Progress**: Currently working on this
+- `[x]` - **Completed**: Finished successfully
+- `[!]` - **Blocked**: Cannot proceed (optional, for blocked tasks)
+
+## Examples
+
+### Example 1: Simple feature request
+
+**Issue document (before):**
+```markdown
+# Feature: Add dark mode toggle
+
+Users should be able to switch between light and dark themes.
+The toggle should be in the settings page.
+```
+
+**Issue document (after task list added):**
+```markdown
+# Feature: Add dark mode toggle
+
+Users should be able to switch between light and dark themes.
+The toggle should be in the settings page.
+
+## Task List
+
+- [ ] Create dark mode toggle component in Settings page
+- [ ] Add dark mode state management (context/store)
+- [ ] Implement CSS-in-JS styles for dark theme
+- [ ] Update existing components to support theme switching
+- [ ] Run tests and verify functionality
+
+### Task Status Legend
+- [ ] Pending
+- [>] In Progress
+- [x] Completed
+```
+
+**During execution:**
+```markdown
+## Task List
+
+- [x] Create dark mode toggle component in Settings page
+- [>] Add dark mode state management (context/store)
+- [ ] Implement CSS-in-JS styles for dark theme
+- [ ] Update existing components to support theme switching
+- [ ] Run tests and verify functionality
+```
+
+### Example 2: Bug fix with investigation
+
+**Issue document:**
+```markdown
+# Bug: Login form crashes on submit
+
+When users click submit, the app crashes.
+Error message: "Cannot read property 'email' of undefined"
+```
+
+**Task list created:**
+```markdown
+## Task List
+
+- [ ] Reproduce the bug locally
+- [ ] Investigate the error in login form component
+- [ ] Identify root cause of undefined email property
+- [ ] Implement fix
+- [ ] Add validation to prevent similar issues
+- [ ] Test the fix with various inputs
+- [ ] Update error handling
+
+### Task Status Legend
+- [ ] Pending
+- [>] In Progress
+- [x] Completed
+```
+
+## When to Use This Skill
+
+Use this Skill when:
+
+1. **Complex multi-step tasks** - Issue requires 3+ distinct steps
+2. **Feature implementation** - Building new functionality from requirements
+3. **Bug fixing** - Need to investigate, fix, and verify
+4. **Refactoring** - Multiple files or components need changes
+5. **User provides requirements** - Issue document contains specifications
+6. **Need progress tracking** - Want visible progress in the document itself
+
+## When NOT to Use This Skill
+
+Skip this Skill when:
+
+1. **Single simple task** - Just one straightforward action needed
+2. **Trivial changes** - Quick fixes that don't need planning
+3. **Informational requests** - User just wants explanation, not execution
+4. **No document provided** - User hasn't created an issue document
+
+## Best Practices
+
+1. **Be specific with tasks**: "Add login button to navbar" not "Update UI"
+2. **Keep tasks atomic**: Each task should be independently completable
+3. **Update immediately**: Don't batch status updates, do them in real-time
+4. **One task at a time**: Never mark multiple tasks as `[>]`
+5. **Handle blockers**: If stuck, create new tasks to resolve the blocker
+6. **Verify completion**: Only mark `[x]` when task is fully done
+
+## Advanced Usage
+
+### Handling dependencies
+
+When tasks have dependencies, order them properly:
+
+```markdown
+- [ ] Create database schema
+- [ ] Implement API endpoints (depends on schema)
+- [ ] Build frontend forms (depends on API)
+```
+
+### Using sub-tasks
+
+For complex tasks, break them down:
+
+```markdown
+- [>] Implement authentication system
+ - [x] Set up JWT library
+ - [>] Create login endpoint
+ - [ ] Create logout endpoint
+ - [ ] Add token refresh logic
+```
+
+### Adding notes
+
+Add implementation notes or findings:
+
+```markdown
+- [x] Investigate performance issue
+ - Note: Found N+1 query in user loader
+ - Solution: Added dataloader batching
+```
+
+## Requirements
+
+This Skill uses standard Deep Code tools:
+
+- **Read**: To read the issue document
+- **Edit**: To update task status in the document
+- **Bash**: To run tests, builds, or other commands
+- **Write**: To create new files if needed
+
+No additional dependencies required.
+
+## Workflow Summary
+
+1. Ask user for issue document path
+2. Read and analyze the document
+3. Append structured task list to document
+4. For each task:
+ - Update to `[>]` in document
+ - Execute the task
+ - Update to `[x]` in document
+5. Add completion summary when done
+
+This approach keeps all planning and progress tracking in one place - the issue document itself - making it easy for users to see what's been done and what's remaining.
diff --git a/docs/SKILL_new.md b/docs/SKILL_new.md
new file mode 100644
index 0000000..cdcc514
--- /dev/null
+++ b/docs/SKILL_new.md
@@ -0,0 +1,264 @@
+---
+name: plan-and-execute
+description: Automatically plan and execute tasks from issue documents. Reads issue requirements, creates a markdown task list with the UpdatePlan tool, and systematically executes each task while updating progress. Use when working with issue documents, task planning, or when you need to break down and execute complex multi-step requirements.
+---
+
+# Plan and Execute
+
+This Skill helps you automatically plan and execute tasks based on issue documents. It reads your requirements, creates a structured markdown task list with the UpdatePlan tool, and systematically works through each task while keeping progress visible.
+
+## Quick Start
+
+When you need to work through an issue document:
+
+1. The Skill will first ask you for the issue document path
+2. It reads the document to understand requirements
+3. Creates a markdown task list by calling the UpdatePlan tool
+4. Executes tasks one by one, updating the tool plan in real time
+
+## Instructions
+
+### Step 1: Get the issue document path
+
+Ask the user for the path to their issue document:
+
+```
+What is the path to your issue document?
+```
+
+The document can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions.
+
+### Step 2: Read and analyze the issue document
+
+Use the Read tool to load the document content and analyze:
+
+- What are the main requirements?
+- What tasks need to be completed?
+- Are there dependencies between tasks?
+- What is the complexity level?
+
+### Step 3: Create the task list
+
+Create a structured markdown task list and pass it to the UpdatePlan tool as the `plan` string. The tool input must use this shape:
+
+```json
+{
+ "plan": "## Task List\n\n- [ ] Task 1 description\n- [ ] Task 2 description\n- [ ] Task 3 description\n\n### Task Status Legend\n- [ ] Pending\n- [>] In Progress\n- [x] Completed"
+}
+```
+
+Use this markdown format for the `plan` content:
+
+```markdown
+## Task List
+
+- [ ] Task 1 description
+- [ ] Task 2 description
+- [ ] Task 3 description
+
+### Task Status Legend
+- [ ] Pending
+- [>] In Progress
+- [x] Completed
+```
+
+Do not append the task list to the issue document. Break down complex requirements into specific, actionable tasks and call UpdatePlan with the full markdown task list.
+
+### Step 4: Execute tasks systematically
+
+For each task in the list:
+
+1. **Mark as in progress**: Call UpdatePlan with the task changed from `[ ]` to `[>]`
+2. **Execute the task**: Use appropriate tools to complete the work
+3. **Mark as completed**: Call UpdatePlan with the task changed from `[>]` to `[x]` when finished
+4. **Move to next task**: Only ONE task should be in progress at a time
+
+Important rules:
+- Always call UpdatePlan BEFORE starting work on a task
+- Always call UpdatePlan IMMEDIATELY after completing a task
+- Always pass the complete current markdown task list, not a partial diff
+- Never work on multiple tasks simultaneously
+- If you encounter errors, keep the task as `[>]` and create new tasks to resolve blockers
+
+### Step 5: Handle task breakdown
+
+If during execution you discover a task is more complex than expected:
+
+1. Keep the current task as `[>]`
+2. Call UpdatePlan with new sub-tasks below it with indentation:
+ ```markdown
+ - [>] Main task
+ - [ ] Sub-task 1
+ - [ ] Sub-task 2
+ ```
+3. Complete sub-tasks first, then mark the main task as complete with UpdatePlan
+
+### Step 6: Final verification
+
+After all tasks are completed (`[x]`):
+
+1. Review the issue requirements to ensure everything is addressed
+2. Run any final checks (tests, builds, linting)
+3. Call UpdatePlan with every task marked `[x]`
+4. Provide a concise completion summary in the final response
+
+## Task State Symbols
+
+- `[ ]` - **Pending**: Not started yet
+- `[>]` - **In Progress**: Currently working on this
+- `[x]` - **Completed**: Finished successfully
+- `[!]` - **Blocked**: Cannot proceed (optional, for blocked tasks)
+
+## Examples
+
+### Example 1: Simple feature request
+
+**Issue document (before):**
+```markdown
+# Feature: Add dark mode toggle
+
+Users should be able to switch between light and dark themes.
+The toggle should be in the settings page.
+```
+
+**UpdatePlan call after analysis:**
+```markdown
+## Task List
+
+- [ ] Create dark mode toggle component in Settings page
+- [ ] Add dark mode state management (context/store)
+- [ ] Implement CSS-in-JS styles for dark theme
+- [ ] Update existing components to support theme switching
+- [ ] Run tests and verify functionality
+
+### Task Status Legend
+- [ ] Pending
+- [>] In Progress
+- [x] Completed
+```
+
+**UpdatePlan call during execution:**
+```markdown
+## Task List
+
+- [x] Create dark mode toggle component in Settings page
+- [>] Add dark mode state management (context/store)
+- [ ] Implement CSS-in-JS styles for dark theme
+- [ ] Update existing components to support theme switching
+- [ ] Run tests and verify functionality
+```
+
+### Example 2: Bug fix with investigation
+
+**Issue document:**
+```markdown
+# Bug: Login form crashes on submit
+
+When users click submit, the app crashes.
+Error message: "Cannot read property 'email' of undefined"
+```
+
+**UpdatePlan call after analysis:**
+```markdown
+## Task List
+
+- [ ] Reproduce the bug locally
+- [ ] Investigate the error in login form component
+- [ ] Identify root cause of undefined email property
+- [ ] Implement fix
+- [ ] Add validation to prevent similar issues
+- [ ] Test the fix with various inputs
+- [ ] Update error handling
+
+### Task Status Legend
+- [ ] Pending
+- [>] In Progress
+- [x] Completed
+```
+
+## When to Use This Skill
+
+Use this Skill when:
+
+1. **Complex multi-step tasks** - Issue requires 3+ distinct steps
+2. **Feature implementation** - Building new functionality from requirements
+3. **Bug fixing** - Need to investigate, fix, and verify
+4. **Refactoring** - Multiple files or components need changes
+5. **User provides requirements** - Issue document contains specifications
+6. **Need progress tracking** - Want visible progress without editing the issue document
+
+## When NOT to Use This Skill
+
+Skip this Skill when:
+
+1. **Single simple task** - Just one straightforward action needed
+2. **Trivial changes** - Quick fixes that don't need planning
+3. **Informational requests** - User just wants explanation, not execution
+4. **No document provided** - User hasn't created an issue document
+
+## Best Practices
+
+1. **Be specific with tasks**: "Add login button to navbar" not "Update UI"
+2. **Keep tasks atomic**: Each task should be independently completable
+3. **Update immediately**: Don't batch status updates, do them in real-time
+4. **One task at a time**: Never mark multiple tasks as `[>]`
+5. **Handle blockers**: If stuck, create new tasks to resolve the blocker
+6. **Verify completion**: Only mark `[x]` when task is fully done
+
+## Advanced Usage
+
+### Handling dependencies
+
+When tasks have dependencies, order them properly:
+
+```markdown
+- [ ] Create database schema
+- [ ] Implement API endpoints (depends on schema)
+- [ ] Build frontend forms (depends on API)
+```
+
+### Using sub-tasks
+
+For complex tasks, break them down:
+
+```markdown
+- [>] Implement authentication system
+ - [x] Set up JWT library
+ - [>] Create login endpoint
+ - [ ] Create logout endpoint
+ - [ ] Add token refresh logic
+```
+
+### Adding notes
+
+Add implementation notes or findings:
+
+```markdown
+- [x] Investigate performance issue
+ - Note: Found N+1 query in user loader
+ - Solution: Added dataloader batching
+```
+
+## Requirements
+
+This Skill uses standard tools:
+
+- **Read**: To read the issue document
+- **UpdatePlan**: To create and update the markdown task list
+- **Bash**: To run tests, builds, or other commands
+- **Write**: To create new files if needed
+
+No additional dependencies required.
+
+## Workflow Summary
+
+1. Ask user for issue document path
+2. Read and analyze the document
+3. Call UpdatePlan with the structured markdown task list
+4. For each task:
+ - Update to `[>]` with UpdatePlan
+ - Execute the task
+ - Update to `[x]` with UpdatePlan
+5. Call UpdatePlan with all tasks completed and summarize the result
+
+This approach keeps planning and progress tracking in the UpdatePlan display, leaving the issue document unchanged unless the actual task requires editing it.
diff --git a/src/prompt.ts b/src/prompt.ts
index 4774725..50aa2a3 100644
--- a/src/prompt.ts
+++ b/src/prompt.ts
@@ -509,6 +509,30 @@ export function getTools(_options: PromptToolOptions = {}, externalTools: ToolDe
},
},
},
+ {
+ type: "function",
+ function: {
+ name: "UpdatePlan",
+ description:
+ "Update the current task plan. The plan argument must be the complete markdown task list to show as the latest progress state.",
+ parameters: {
+ type: "object",
+ properties: {
+ plan: {
+ type: "string",
+ description:
+ "The complete markdown task list, including task status markers such as [ ], [>], [x], and optional notes.",
+ },
+ explanation: {
+ type: "string",
+ description: "Optional short reason for changing the plan.",
+ },
+ },
+ required: ["plan"],
+ additionalProperties: false,
+ },
+ },
+ },
{
type: "function",
function: {
diff --git a/src/tests/prompt.test.ts b/src/tests/prompt.test.ts
index 28c6488..b7c9178 100644
--- a/src/tests/prompt.test.ts
+++ b/src/tests/prompt.test.ts
@@ -12,11 +12,24 @@ test("getTools always includes WebSearch", () => {
assert.equal(names.includes("WebSearch"), true);
});
+test("getTools includes UpdatePlan with string plan schema", () => {
+ const tool = getTools().find((candidate) => candidate.function.name === "UpdatePlan");
+ assert.ok(tool);
+ assert.deepEqual(tool.function.parameters.required, ["plan"]);
+ assert.equal((tool.function.parameters.properties.plan as { type?: unknown }).type, "string");
+});
+
test("getSystemPrompt always includes WebSearch docs", () => {
const prompt = getSystemPrompt("/tmp/project");
assert.equal(prompt.includes("## WebSearch"), true);
});
+test("getSystemPrompt includes UpdatePlan docs", () => {
+ const prompt = getSystemPrompt("/tmp/project");
+ assert.equal(prompt.includes("## UpdatePlan"), true);
+ assert.equal(prompt.includes("The `plan` argument is a markdown string, not an array of step objects."), true);
+});
+
test("getSystemPrompt includes current date guidance", () => {
const now = new Date();
const expected = `今天是${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日。随着对话的进行,时间在流逝。`;
diff --git a/src/tests/tool-handlers.test.ts b/src/tests/tool-handlers.test.ts
index 58828a2..0b21edd 100644
--- a/src/tests/tool-handlers.test.ts
+++ b/src/tests/tool-handlers.test.ts
@@ -8,6 +8,7 @@ import type { ToolExecutionContext } from "../tools/executor";
import { handleBashTool } from "../tools/bash-handler";
import { handleEditTool } from "../tools/edit-handler";
import { handleReadTool } from "../tools/read-handler";
+import { handleUpdatePlanTool } from "../tools/update-plan-handler";
import { handleWriteTool } from "../tools/write-handler";
const tempDirs: string[] = [];
@@ -51,6 +52,31 @@ test("Bash streams stdout and stderr before command completion", async () => {
assert.match(streamedOutput, /err/);
});
+test("UpdatePlan accepts a markdown task list string", async () => {
+ const workspace = createTempWorkspace();
+ const plan = ["## Task List", "", "- [>] Inspect current behavior", "- [ ] Implement UpdatePlan"].join("\n");
+
+ const result = await handleUpdatePlanTool({ plan }, createContext("update-plan", workspace));
+
+ assert.equal(result.ok, true);
+ assert.equal(result.name, "UpdatePlan");
+ assert.equal(result.output, "Plan updated.");
+ assert.equal(result.metadata?.plan, plan);
+});
+
+test("UpdatePlan rejects non-string plan payloads", async () => {
+ const workspace = createTempWorkspace();
+
+ const result = await handleUpdatePlanTool(
+ { plan: [{ step: "Inspect current behavior", status: "in_progress" }] },
+ createContext("update-plan-invalid", workspace)
+ );
+
+ assert.equal(result.ok, false);
+ assert.equal(result.name, "UpdatePlan");
+ assert.match(result.error ?? "", /InputValidationError/);
+});
+
test("Read returns snippet metadata and Edit can scope replacements by snippet_id", async () => {
const workspace = createTempWorkspace();
const filePath = path.join(workspace, "sample.txt");
diff --git a/src/tools/executor.ts b/src/tools/executor.ts
index e6018d9..70ceab1 100644
--- a/src/tools/executor.ts
+++ b/src/tools/executor.ts
@@ -4,6 +4,7 @@ import { handleAskUserQuestionTool } from "./ask-user-question-handler";
import { handleBashTool } from "./bash-handler";
import { handleEditTool } from "./edit-handler";
import { handleReadTool } from "./read-handler";
+import { handleUpdatePlanTool } from "./update-plan-handler";
import { handleWebSearchTool } from "./web-search-handler";
import { handleWriteTool } from "./write-handler";
import type { McpManager } from "../mcp/mcp-manager";
@@ -120,6 +121,7 @@ export class ToolExecutor {
this.toolHandlers.set("write", handleWriteTool);
this.toolHandlers.set("edit", handleEditTool);
this.toolHandlers.set("AskUserQuestion", handleAskUserQuestionTool);
+ this.toolHandlers.set("UpdatePlan", handleUpdatePlanTool);
this.toolHandlers.set("WebSearch", handleWebSearchTool);
}
diff --git a/src/tools/update-plan-handler.ts b/src/tools/update-plan-handler.ts
new file mode 100644
index 0000000..7c7198e
--- /dev/null
+++ b/src/tools/update-plan-handler.ts
@@ -0,0 +1,23 @@
+import { z } from "zod";
+import type { ToolExecutionContext, ToolExecutionResult } from "./executor";
+import { executeValidatedTool } from "../common/runtime";
+
+const updatePlanSchema = z.strictObject({
+ plan: z.string().trim().min(1, "plan must not be empty."),
+ explanation: z.string().trim().optional(),
+});
+
+export async function handleUpdatePlanTool(
+ args: Record,
+ _context: ToolExecutionContext
+): Promise {
+ return executeValidatedTool("UpdatePlan", updatePlanSchema, args, _context, async (input) => ({
+ ok: true,
+ name: "UpdatePlan",
+ output: "Plan updated.",
+ metadata: {
+ plan: input.plan,
+ ...(input.explanation ? { explanation: input.explanation } : {}),
+ },
+ }));
+}
diff --git a/src/ui/MessageView.tsx b/src/ui/MessageView.tsx
index c8793fc..6f388d0 100644
--- a/src/ui/MessageView.tsx
+++ b/src/ui/MessageView.tsx
@@ -72,6 +72,7 @@ export function MessageView({ message, collapsed, width = 80 }: Props): React.Re
if (message.role === "tool") {
const summary = buildToolSummary(message);
const diffLines = getToolDiffPreviewLines(summary);
+ const planLines = getUpdatePlanPreviewLines(summary);
return (
{diffLines.length > 0 ? : null}
+ {planLines.length > 0 ? : null}
);
}
@@ -272,6 +274,20 @@ function getToolDiffPreviewLines(summary: ToolSummary): DiffPreviewLine[] {
return parseDiffPreview(diffPreview);
}
+function getUpdatePlanPreviewLines(summary: ToolSummary): string[] {
+ if (!summary.ok || summary.name !== "UpdatePlan") {
+ return [];
+ }
+ const plan = summary.metadata?.plan;
+ if (typeof plan !== "string" || !plan.trim()) {
+ return [];
+ }
+ return plan
+ .split(/\r?\n/)
+ .map((line) => line.trimEnd())
+ .filter((line) => line.trim().length > 0);
+}
+
export function parseDiffPreview(diffPreview: string): DiffPreviewLine[] {
return diffPreview
.split("\n")
@@ -311,6 +327,21 @@ function DiffPreview({ lines }: { lines: DiffPreviewLine[] }): React.ReactElemen
);
}
+function PlanPreview({ lines }: { lines: string[] }): React.ReactElement {
+ return (
+
+ └ Plan
+
+ {lines.map((line, index) => (
+
+ {line}
+
+ ))}
+
+
+ );
+}
+
function isPlainRecord(value: unknown): value is Record {
return Boolean(value && typeof value === "object" && !Array.isArray(value));
}
diff --git a/templates/tools/update-plan.md b/templates/tools/update-plan.md
new file mode 100644
index 0000000..c3e08f0
--- /dev/null
+++ b/templates/tools/update-plan.md
@@ -0,0 +1,38 @@
+## UpdatePlan
+
+Updates the current task plan and progress display.
+
+Usage:
+- Use this tool for non-trivial multi-step tasks when a task list helps track execution progress.
+- Pass the complete current task list every time. The latest call replaces the previous visible plan.
+- The `plan` argument is a markdown string, not an array of step objects.
+- Keep exactly one task marked `[>]` while work is in progress.
+- Update the plan before starting a task, immediately after completing a task, and whenever tasks are split, merged, reordered, blocked, or changed.
+- Do not edit issue documents just to maintain task status; use `UpdatePlan` for the task list instead.
+
+Task markers:
+- `[ ]` Pending
+- `[>]` In progress
+- `[x]` Completed
+- `[!]` Blocked
+
+```json
+{
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
+ "type": "object",
+ "properties": {
+ "plan": {
+ "description": "The complete markdown task list to display as the latest plan state.",
+ "type": "string"
+ },
+ "explanation": {
+ "description": "Optional short reason for changing the plan.",
+ "type": "string"
+ }
+ },
+ "required": [
+ "plan"
+ ],
+ "additionalProperties": false
+}
+```
From 52dafba25903dc70258d7e59dbe86e283a0f091f Mon Sep 17 00:00:00 2001
From: dengmik-commits <270912164+dengmik-commits@users.noreply.github.com>
Date: Mon, 18 May 2026 09:50:38 +0800
Subject: [PATCH 03/90] fix: re-apply dynamic modifier parsing for Shift+Enter
after upstream sync
Upstream v0.1.21 reverted PR #70. Re-apply:
- isShiftReturn() / isReturn() dynamic CSI modifier bit parsing
- Kitty progressive enhancement (ESC[>1u) alongside xterm modifyOtherKeys
- Clear input when key.return is true (safety net)
---
src/tests/promptInputKeys.test.ts | 6 ++---
src/ui/prompt/cursor.ts | 4 +--
src/ui/prompt/useTerminalInput.ts | 43 ++++++++++++++++++++++++++++---
3 files changed, 44 insertions(+), 9 deletions(-)
diff --git a/src/tests/promptInputKeys.test.ts b/src/tests/promptInputKeys.test.ts
index 69d2075..8952a3d 100644
--- a/src/tests/promptInputKeys.test.ts
+++ b/src/tests/promptInputKeys.test.ts
@@ -80,7 +80,7 @@ test("parseTerminalInput keeps BS payload for meta+backspace", () => {
test("parseTerminalInput recognizes shifted return sequences", () => {
const { input, key } = parseTerminalInput("\u001B\r");
- assert.equal(input, "\r");
+ assert.equal(input, "");
assert.equal(key.return, true);
assert.equal(key.shift, true);
assert.equal(key.meta, false);
@@ -108,8 +108,8 @@ test("parseTerminalInput recognizes alternate shifted return sequences", () => {
});
test("terminal extended key helpers request and restore modifyOtherKeys mode", () => {
- assert.equal(enableTerminalExtendedKeys(), "\u001B[>4;1m");
- assert.equal(disableTerminalExtendedKeys(), "\u001B[>4;0m");
+ assert.equal(enableTerminalExtendedKeys(), "\u001B[>4;1m\u001B[>1u");
+ assert.equal(disableTerminalExtendedKeys(), "\u001B[>4;0m\u001B[ {
diff --git a/src/ui/prompt/cursor.ts b/src/ui/prompt/cursor.ts
index 2668470..59b24f2 100644
--- a/src/ui/prompt/cursor.ts
+++ b/src/ui/prompt/cursor.ts
@@ -41,11 +41,11 @@ function disableTerminalFocusReporting(): string {
}
export function enableTerminalExtendedKeys(): string {
- return "\u001B[>4;1m";
+ return "\u001B[>4;1m\u001B[>1u";
}
export function disableTerminalExtendedKeys(): string {
- return "\u001B[>4;0m";
+ return "\u001B[>4;0m\u001B[
Date: Mon, 18 May 2026 10:22:22 +0800
Subject: [PATCH 04/90] fix: refresh mcpToolDefinitions cache after MCP
reconnect
After reconnectMcpServer succeeds, SessionManager's cached
mcpToolDefinitions was stale, causing "Unknown MCP tool" errors
when the model tried to call reconnected tools.
---
src/session.ts | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/session.ts b/src/session.ts
index eddfe5c..0527ba8 100644
--- a/src/session.ts
+++ b/src/session.ts
@@ -261,6 +261,7 @@ export class SessionManager {
async reconnectMcpServer(name: string, config?: McpServerConfig): Promise {
await this.mcpManager.reconnect(name, config);
+ this.mcpToolDefinitions = this.mcpManager.getMcpToolDefinitions();
}
dispose(): void {
From 63ec2a340192f6e53b8003162320e102e521a49e Mon Sep 17 00:00:00 2001
From: Ji Zhang
Date: Mon, 18 May 2026 10:40:39 +0800
Subject: [PATCH 05/90] feat: update update-plan prompt draft
---
docs/SKILL.md | 124 +++++++++++++++------------------
docs/SKILL_new.md | 77 +++++++++-----------
templates/tools/update-plan.md | 7 --
3 files changed, 90 insertions(+), 118 deletions(-)
diff --git a/docs/SKILL.md b/docs/SKILL.md
index f6d7149..8f45c3b 100644
--- a/docs/SKILL.md
+++ b/docs/SKILL.md
@@ -1,36 +1,38 @@
---
name: plan-and-execute
-description: Automatically plan and execute tasks from issue documents. Reads issue requirements, creates a task list at the end of the document, and systematically executes each task while updating progress. Use when working with issue documents, task planning, or when you need to break down and execute complex multi-step requirements.
+description: Automatically plan and execute requirements. Creates a markdown task list with the UpdatePlan tool, and systematically executes each task while updating progress. Use when working with task planning or when you need to break down and execute complex multi-step requirements.
---
# Plan and Execute
-This Skill helps you automatically plan and execute tasks based on issue documents. It reads your requirements, creates a structured task list directly in the document, and systematically works through each task while keeping the document updated with progress.
+This Skill helps you automatically plan and execute requirements. It creates a structured markdown task list with the UpdatePlan tool and systematically works through each task while keeping progress visible.
## Quick Start
-When you need to work through an issue document:
+When you need to work through a multi-step request:
-1. The Skill will first ask you for the issue document path
-2. It reads the document to understand requirements
-3. Creates a task list at the end of the document
-4. Executes tasks one by one, updating status in real-time
+1. Understand the requirements
+2. Read referenced files only when they are needed for context
+3. Create a markdown task list by calling the UpdatePlan tool
+4. Execute tasks one by one, updating the tool plan in real time
## Instructions
-### Step 1: Get the issue document path
+### Step 1: Gather the requirements
-Ask the user for the path to their issue document:
+Identify the requirements from the available context. Do not require the requirements to be moved into a separate document.
+
+If a required referenced file path is missing, ask for it:
```
-What is the path to your issue document?
+What is the path to the referenced file?
```
-The document can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions.
+Referenced files can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions. If no additional file is needed, continue from the available requirements.
-### Step 2: Read and analyze the issue document
+### Step 2: Read and analyze the requirements
-Use the Read tool to load the document content and analyze:
+Analyze the requirements and read any referenced files needed for context:
- What are the main requirements?
- What tasks need to be completed?
@@ -39,7 +41,15 @@ Use the Read tool to load the document content and analyze:
### Step 3: Create the task list
-Create a structured task list at the END of the issue document using this format:
+Create a structured markdown task list and pass it to the UpdatePlan tool as the `plan` string. The tool input must use this shape:
+
+```json
+{
+ "plan": "## Task List\n\n- [ ] Task 1 description\n- [ ] Task 2 description\n- [ ] Task 3 description"
+}
+```
+
+Use this markdown format for the `plan` content:
```markdown
## Task List
@@ -47,27 +57,23 @@ Create a structured task list at the END of the issue document using this format
- [ ] Task 1 description
- [ ] Task 2 description
- [ ] Task 3 description
-
-### Task Status Legend
-- [ ] Pending
-- [>] In Progress
-- [x] Completed
```
-Use the Edit tool to append this section to the document. Break down complex requirements into specific, actionable tasks.
+Do not append the task list to a source file. Break down complex requirements into specific, actionable tasks and call UpdatePlan with the full markdown task list.
### Step 4: Execute tasks systematically
For each task in the list:
-1. **Mark as in progress**: Update the task in the document from `[ ]` to `[>]`
+1. **Mark as in progress**: Call UpdatePlan with the task changed from `[ ]` to `[>]`
2. **Execute the task**: Use appropriate tools to complete the work
-3. **Mark as completed**: Update the task from `[>]` to `[x]` when finished
+3. **Mark as completed**: Call UpdatePlan with the task changed from `[>]` to `[x]` when finished
4. **Move to next task**: Only ONE task should be in progress at a time
Important rules:
-- Always update the document BEFORE starting work on a task
-- Always update the document IMMEDIATELY after completing a task
+- Always call UpdatePlan BEFORE starting work on a task
+- Always call UpdatePlan IMMEDIATELY after completing a task
+- Always pass the complete current markdown task list, not a partial diff
- Never work on multiple tasks simultaneously
- If you encounter errors, keep the task as `[>]` and create new tasks to resolve blockers
@@ -76,34 +82,35 @@ Important rules:
If during execution you discover a task is more complex than expected:
1. Keep the current task as `[>]`
-2. Add new sub-tasks below it with indentation:
+2. Call UpdatePlan with new sub-tasks below it with indentation:
```markdown
- [>] Main task
- [ ] Sub-task 1
- [ ] Sub-task 2
```
-3. Complete sub-tasks first, then mark the main task as complete
+3. Complete sub-tasks first, then mark the main task as complete with UpdatePlan
### Step 6: Final verification
After all tasks are completed (`[x]`):
-1. Review the issue requirements to ensure everything is addressed
+1. Review the original requirements to ensure everything is addressed
2. Run any final checks (tests, builds, linting)
-3. Add a completion summary at the end of the document
+3. Call UpdatePlan with every task marked `[x]`
+4. Provide a concise completion summary in the final response
## Task State Symbols
-- `[ ]` - **Pending**: Not started yet
-- `[>]` - **In Progress**: Currently working on this
-- `[x]` - **Completed**: Finished successfully
-- `[!]` - **Blocked**: Cannot proceed (optional, for blocked tasks)
+- `[ ]` - Pending
+- `[>]` - In progress
+- `[x]` - Completed
+- `[!]` - Blocked
## Examples
### Example 1: Simple feature request
-**Issue document (before):**
+**Example requirements:**
```markdown
# Feature: Add dark mode toggle
@@ -111,13 +118,8 @@ Users should be able to switch between light and dark themes.
The toggle should be in the settings page.
```
-**Issue document (after task list added):**
+**UpdatePlan call after analysis:**
```markdown
-# Feature: Add dark mode toggle
-
-Users should be able to switch between light and dark themes.
-The toggle should be in the settings page.
-
## Task List
- [ ] Create dark mode toggle component in Settings page
@@ -125,14 +127,9 @@ The toggle should be in the settings page.
- [ ] Implement CSS-in-JS styles for dark theme
- [ ] Update existing components to support theme switching
- [ ] Run tests and verify functionality
-
-### Task Status Legend
-- [ ] Pending
-- [>] In Progress
-- [x] Completed
```
-**During execution:**
+**UpdatePlan call during execution:**
```markdown
## Task List
@@ -145,7 +142,7 @@ The toggle should be in the settings page.
### Example 2: Bug fix with investigation
-**Issue document:**
+**Example requirements:**
```markdown
# Bug: Login form crashes on submit
@@ -153,7 +150,7 @@ When users click submit, the app crashes.
Error message: "Cannot read property 'email' of undefined"
```
-**Task list created:**
+**UpdatePlan call after analysis:**
```markdown
## Task List
@@ -164,23 +161,18 @@ Error message: "Cannot read property 'email' of undefined"
- [ ] Add validation to prevent similar issues
- [ ] Test the fix with various inputs
- [ ] Update error handling
-
-### Task Status Legend
-- [ ] Pending
-- [>] In Progress
-- [x] Completed
```
## When to Use This Skill
Use this Skill when:
-1. **Complex multi-step tasks** - Issue requires 3+ distinct steps
+1. **Complex multi-step tasks** - Request requires 3+ distinct steps
2. **Feature implementation** - Building new functionality from requirements
3. **Bug fixing** - Need to investigate, fix, and verify
4. **Refactoring** - Multiple files or components need changes
-5. **User provides requirements** - Issue document contains specifications
-6. **Need progress tracking** - Want visible progress in the document itself
+5. **Detailed requirements** - Specifications need to be translated into concrete tasks
+6. **Need progress tracking** - Want visible progress without editing source files
## When NOT to Use This Skill
@@ -189,7 +181,7 @@ Skip this Skill when:
1. **Single simple task** - Just one straightforward action needed
2. **Trivial changes** - Quick fixes that don't need planning
3. **Informational requests** - User just wants explanation, not execution
-4. **No document provided** - User hasn't created an issue document
+4. **No execution requested** - User only wants brainstorming or a high-level explanation
## Best Practices
@@ -236,10 +228,10 @@ Add implementation notes or findings:
## Requirements
-This Skill uses standard Deep Code tools:
+This Skill uses standard tools:
-- **Read**: To read the issue document
-- **Edit**: To update task status in the document
+- **Read**: To inspect referenced files when needed
+- **UpdatePlan**: To create and update the markdown task list
- **Bash**: To run tests, builds, or other commands
- **Write**: To create new files if needed
@@ -247,13 +239,13 @@ No additional dependencies required.
## Workflow Summary
-1. Ask user for issue document path
-2. Read and analyze the document
-3. Append structured task list to document
+1. Analyze the requirements
+2. Read referenced files when needed
+3. Call UpdatePlan with the structured markdown task list
4. For each task:
- - Update to `[>]` in document
+ - Update to `[>]` with UpdatePlan
- Execute the task
- - Update to `[x]` in document
-5. Add completion summary when done
+ - Update to `[x]` with UpdatePlan
+5. Call UpdatePlan with all tasks completed and summarize the result
-This approach keeps all planning and progress tracking in one place - the issue document itself - making it easy for users to see what's been done and what's remaining.
+This approach keeps planning and progress tracking in the UpdatePlan display, leaving source materials unchanged unless the actual task requires editing them.
diff --git a/docs/SKILL_new.md b/docs/SKILL_new.md
index cdcc514..6efbee9 100644
--- a/docs/SKILL_new.md
+++ b/docs/SKILL_new.md
@@ -1,36 +1,38 @@
---
name: plan-and-execute
-description: Automatically plan and execute tasks from issue documents. Reads issue requirements, creates a markdown task list with the UpdatePlan tool, and systematically executes each task while updating progress. Use when working with issue documents, task planning, or when you need to break down and execute complex multi-step requirements.
+description: Automatically plan and execute requirements. Creates a markdown task list with the UpdatePlan tool, and systematically executes each task while updating progress. Use when working with task planning or when you need to break down and execute complex multi-step requirements.
---
# Plan and Execute
-This Skill helps you automatically plan and execute tasks based on issue documents. It reads your requirements, creates a structured markdown task list with the UpdatePlan tool, and systematically works through each task while keeping progress visible.
+This Skill helps you automatically plan and execute requirements. It creates a structured markdown task list with the UpdatePlan tool and systematically works through each task while keeping progress visible.
## Quick Start
-When you need to work through an issue document:
+When you need to work through a multi-step request:
-1. The Skill will first ask you for the issue document path
-2. It reads the document to understand requirements
-3. Creates a markdown task list by calling the UpdatePlan tool
-4. Executes tasks one by one, updating the tool plan in real time
+1. Understand the requirements
+2. Read referenced files when they are needed for context
+3. Create a markdown task list by calling the UpdatePlan tool
+4. Execute tasks one by one, updating the tool plan in real time
## Instructions
-### Step 1: Get the issue document path
+### Step 1: Gather the requirements
-Ask the user for the path to their issue document:
+Identify the requirements from the available context. Do not require the requirements to be moved into a separate document.
+
+If a required referenced file path is missing, ask for it:
```
-What is the path to your issue document?
+What is the path to the referenced file?
```
-The document can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions.
+Referenced files can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions. If no additional file is needed, continue from the available requirements.
-### Step 2: Read and analyze the issue document
+### Step 2: Read and analyze the requirements
-Use the Read tool to load the document content and analyze:
+Analyze the requirements and read any referenced files needed for context:
- What are the main requirements?
- What tasks need to be completed?
@@ -43,7 +45,7 @@ Create a structured markdown task list and pass it to the UpdatePlan tool as the
```json
{
- "plan": "## Task List\n\n- [ ] Task 1 description\n- [ ] Task 2 description\n- [ ] Task 3 description\n\n### Task Status Legend\n- [ ] Pending\n- [>] In Progress\n- [x] Completed"
+ "plan": "## Task List\n\n- [ ] Task 1 description\n- [ ] Task 2 description\n- [ ] Task 3 description"
}
```
@@ -55,14 +57,9 @@ Use this markdown format for the `plan` content:
- [ ] Task 1 description
- [ ] Task 2 description
- [ ] Task 3 description
-
-### Task Status Legend
-- [ ] Pending
-- [>] In Progress
-- [x] Completed
```
-Do not append the task list to the issue document. Break down complex requirements into specific, actionable tasks and call UpdatePlan with the full markdown task list.
+Break down complex requirements into specific, actionable tasks and call UpdatePlan with the full markdown task list.
### Step 4: Execute tasks systematically
@@ -97,23 +94,23 @@ If during execution you discover a task is more complex than expected:
After all tasks are completed (`[x]`):
-1. Review the issue requirements to ensure everything is addressed
+1. Review the original requirements to ensure everything is addressed
2. Run any final checks (tests, builds, linting)
3. Call UpdatePlan with every task marked `[x]`
4. Provide a concise completion summary in the final response
## Task State Symbols
-- `[ ]` - **Pending**: Not started yet
-- `[>]` - **In Progress**: Currently working on this
-- `[x]` - **Completed**: Finished successfully
-- `[!]` - **Blocked**: Cannot proceed (optional, for blocked tasks)
+- `[ ]` - Pending
+- `[>]` - In progress
+- `[x]` - Completed
+- `[!]` - Blocked
## Examples
### Example 1: Simple feature request
-**Issue document (before):**
+**Example requirements:**
```markdown
# Feature: Add dark mode toggle
@@ -130,11 +127,6 @@ The toggle should be in the settings page.
- [ ] Implement CSS-in-JS styles for dark theme
- [ ] Update existing components to support theme switching
- [ ] Run tests and verify functionality
-
-### Task Status Legend
-- [ ] Pending
-- [>] In Progress
-- [x] Completed
```
**UpdatePlan call during execution:**
@@ -150,7 +142,7 @@ The toggle should be in the settings page.
### Example 2: Bug fix with investigation
-**Issue document:**
+**Example requirements:**
```markdown
# Bug: Login form crashes on submit
@@ -169,23 +161,18 @@ Error message: "Cannot read property 'email' of undefined"
- [ ] Add validation to prevent similar issues
- [ ] Test the fix with various inputs
- [ ] Update error handling
-
-### Task Status Legend
-- [ ] Pending
-- [>] In Progress
-- [x] Completed
```
## When to Use This Skill
Use this Skill when:
-1. **Complex multi-step tasks** - Issue requires 3+ distinct steps
+1. **Complex multi-step tasks** - Request requires 3+ distinct steps
2. **Feature implementation** - Building new functionality from requirements
3. **Bug fixing** - Need to investigate, fix, and verify
4. **Refactoring** - Multiple files or components need changes
-5. **User provides requirements** - Issue document contains specifications
-6. **Need progress tracking** - Want visible progress without editing the issue document
+5. **Detailed requirements** - Specifications need to be translated into concrete tasks
+6. **Need progress tracking** - Want visible progress without editing source files
## When NOT to Use This Skill
@@ -194,7 +181,7 @@ Skip this Skill when:
1. **Single simple task** - Just one straightforward action needed
2. **Trivial changes** - Quick fixes that don't need planning
3. **Informational requests** - User just wants explanation, not execution
-4. **No document provided** - User hasn't created an issue document
+4. **No execution requested** - User only wants brainstorming or a high-level explanation
## Best Practices
@@ -243,7 +230,7 @@ Add implementation notes or findings:
This Skill uses standard tools:
-- **Read**: To read the issue document
+- **Read**: To inspect referenced files when needed
- **UpdatePlan**: To create and update the markdown task list
- **Bash**: To run tests, builds, or other commands
- **Write**: To create new files if needed
@@ -252,8 +239,8 @@ No additional dependencies required.
## Workflow Summary
-1. Ask user for issue document path
-2. Read and analyze the document
+1. Analyze the requirements
+2. Read referenced files when needed
3. Call UpdatePlan with the structured markdown task list
4. For each task:
- Update to `[>]` with UpdatePlan
@@ -261,4 +248,4 @@ No additional dependencies required.
- Update to `[x]` with UpdatePlan
5. Call UpdatePlan with all tasks completed and summarize the result
-This approach keeps planning and progress tracking in the UpdatePlan display, leaving the issue document unchanged unless the actual task requires editing it.
+This approach keeps planning and progress tracking in the UpdatePlan display, leaving source materials unchanged unless the actual task requires editing them.
diff --git a/templates/tools/update-plan.md b/templates/tools/update-plan.md
index c3e08f0..28d12f7 100644
--- a/templates/tools/update-plan.md
+++ b/templates/tools/update-plan.md
@@ -8,13 +8,6 @@ Usage:
- The `plan` argument is a markdown string, not an array of step objects.
- Keep exactly one task marked `[>]` while work is in progress.
- Update the plan before starting a task, immediately after completing a task, and whenever tasks are split, merged, reordered, blocked, or changed.
-- Do not edit issue documents just to maintain task status; use `UpdatePlan` for the task list instead.
-
-Task markers:
-- `[ ]` Pending
-- `[>]` In progress
-- `[x]` Completed
-- `[!]` Blocked
```json
{
From c638114bfc2ab23c502322955403e4c8dc1bff62 Mon Sep 17 00:00:00 2001
From: Ji Zhang
Date: Mon, 18 May 2026 11:00:13 +0800
Subject: [PATCH 06/90] feat: update update-plan prompt draft
---
docs/SKILL.md | 47 ++++++++++++++---------------
docs/SKILL_new.md | 54 ++++++++++++++--------------------
templates/tools/update-plan.md | 2 ++
3 files changed, 48 insertions(+), 55 deletions(-)
diff --git a/docs/SKILL.md b/docs/SKILL.md
index 8f45c3b..deb8c5e 100644
--- a/docs/SKILL.md
+++ b/docs/SKILL.md
@@ -11,16 +11,16 @@ This Skill helps you automatically plan and execute requirements. It creates a s
When you need to work through a multi-step request:
-1. Understand the requirements
-2. Read referenced files only when they are needed for context
-3. Create a markdown task list by calling the UpdatePlan tool
-4. Execute tasks one by one, updating the tool plan in real time
+1. Analyze the requirements and explore enough project context
+2. Create a markdown task list by calling the UpdatePlan tool
+3. Execute tasks one by one, updating the tool plan in real time
+4. Revise the remaining plan as new context appears
## Instructions
-### Step 1: Gather the requirements
+### Step 1: Analyze the requirements
-Identify the requirements from the available context. Do not require the requirements to be moved into a separate document.
+Identify the requirements from the available context. Explore the project enough to make the plan concrete and accurate.
If a required referenced file path is missing, ask for it:
@@ -30,16 +30,13 @@ What is the path to the referenced file?
Referenced files can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions. If no additional file is needed, continue from the available requirements.
-### Step 2: Read and analyze the requirements
-
-Analyze the requirements and read any referenced files needed for context:
-
- What are the main requirements?
- What tasks need to be completed?
- Are there dependencies between tasks?
- What is the complexity level?
+- Which files, modules, commands, or tests are relevant?
-### Step 3: Create the task list
+### Step 2: Create the task list
Create a structured markdown task list and pass it to the UpdatePlan tool as the `plan` string. The tool input must use this shape:
@@ -59,25 +56,28 @@ Use this markdown format for the `plan` content:
- [ ] Task 3 description
```
-Do not append the task list to a source file. Break down complex requirements into specific, actionable tasks and call UpdatePlan with the full markdown task list.
+Break down complex requirements into specific, actionable tasks and call UpdatePlan with the full markdown task list.
-### Step 4: Execute tasks systematically
+### Step 3: Execute tasks systematically
For each task in the list:
-1. **Mark as in progress**: Call UpdatePlan with the task changed from `[ ]` to `[>]`
-2. **Execute the task**: Use appropriate tools to complete the work
-3. **Mark as completed**: Call UpdatePlan with the task changed from `[>]` to `[x]` when finished
-4. **Move to next task**: Only ONE task should be in progress at a time
+1. **Refresh the plan**: Before starting the first task and after completing each task, re-evaluate the latest conversation and project context. Update the remaining tasks when scope, order, blockers, or follow-up work changes.
+2. **Mark as in progress**: Call UpdatePlan with the task changed from `[ ]` to `[>]`
+3. **Execute the task**: Use appropriate tools to complete the work
+4. **Mark as completed**: Call UpdatePlan with the task changed from `[>]` to `[x]` when finished
+5. **Move to next task**: Only ONE task should be in progress at a time
Important rules:
+- Always keep the plan aligned with the latest context before executing the next task
- Always call UpdatePlan BEFORE starting work on a task
- Always call UpdatePlan IMMEDIATELY after completing a task
- Always pass the complete current markdown task list, not a partial diff
- Never work on multiple tasks simultaneously
+- Remove tasks that are no longer relevant, and add newly discovered tasks before working on them
- If you encounter errors, keep the task as `[>]` and create new tasks to resolve blockers
-### Step 5: Handle task breakdown
+### Step 4: Handle task breakdown
If during execution you discover a task is more complex than expected:
@@ -90,7 +90,7 @@ If during execution you discover a task is more complex than expected:
```
3. Complete sub-tasks first, then mark the main task as complete with UpdatePlan
-### Step 6: Final verification
+### Step 5: Final verification
After all tasks are completed (`[x]`):
@@ -230,7 +230,7 @@ Add implementation notes or findings:
This Skill uses standard tools:
-- **Read**: To inspect referenced files when needed
+- **Read**: To inspect relevant files and explore project context
- **UpdatePlan**: To create and update the markdown task list
- **Bash**: To run tests, builds, or other commands
- **Write**: To create new files if needed
@@ -239,13 +239,14 @@ No additional dependencies required.
## Workflow Summary
-1. Analyze the requirements
-2. Read referenced files when needed
-3. Call UpdatePlan with the structured markdown task list
+1. Analyze the requirements and relevant project context
+2. Call UpdatePlan with the structured markdown task list
+3. Refresh the remaining plan before the first task
4. For each task:
- Update to `[>]` with UpdatePlan
- Execute the task
- Update to `[x]` with UpdatePlan
+ - Re-evaluate and revise remaining tasks before moving on
5. Call UpdatePlan with all tasks completed and summarize the result
This approach keeps planning and progress tracking in the UpdatePlan display, leaving source materials unchanged unless the actual task requires editing them.
diff --git a/docs/SKILL_new.md b/docs/SKILL_new.md
index 6efbee9..41ed251 100644
--- a/docs/SKILL_new.md
+++ b/docs/SKILL_new.md
@@ -11,16 +11,16 @@ This Skill helps you automatically plan and execute requirements. It creates a s
When you need to work through a multi-step request:
-1. Understand the requirements
-2. Read referenced files when they are needed for context
-3. Create a markdown task list by calling the UpdatePlan tool
-4. Execute tasks one by one, updating the tool plan in real time
+1. Analyze the requirements and explore enough project context
+2. Create a markdown task list by calling the UpdatePlan tool
+3. Execute tasks one by one, updating the tool plan in real time
+4. Revise the remaining plan as new context appears
## Instructions
-### Step 1: Gather the requirements
+### Step 1: Analyze the requirements
-Identify the requirements from the available context. Do not require the requirements to be moved into a separate document.
+Identify the requirements from the available context. Explore the project enough to make the plan concrete and accurate.
If a required referenced file path is missing, ask for it:
@@ -30,16 +30,13 @@ What is the path to the referenced file?
Referenced files can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions. If no additional file is needed, continue from the available requirements.
-### Step 2: Read and analyze the requirements
-
-Analyze the requirements and read any referenced files needed for context:
-
- What are the main requirements?
- What tasks need to be completed?
- Are there dependencies between tasks?
- What is the complexity level?
+- Which files, modules, commands, or tests are relevant?
-### Step 3: Create the task list
+### Step 2: Create the task list
Create a structured markdown task list and pass it to the UpdatePlan tool as the `plan` string. The tool input must use this shape:
@@ -61,23 +58,26 @@ Use this markdown format for the `plan` content:
Break down complex requirements into specific, actionable tasks and call UpdatePlan with the full markdown task list.
-### Step 4: Execute tasks systematically
+### Step 3: Execute tasks systematically
For each task in the list:
-1. **Mark as in progress**: Call UpdatePlan with the task changed from `[ ]` to `[>]`
-2. **Execute the task**: Use appropriate tools to complete the work
-3. **Mark as completed**: Call UpdatePlan with the task changed from `[>]` to `[x]` when finished
-4. **Move to next task**: Only ONE task should be in progress at a time
+1. **Refresh the plan**: Before starting the first task and after completing each task, re-evaluate the latest conversation and project context. Update the remaining tasks when scope, order, blockers, or follow-up work changes.
+2. **Mark as in progress**: Call UpdatePlan with the task changed from `[ ]` to `[>]`
+3. **Execute the task**: Use appropriate tools to complete the work
+4. **Mark as completed**: Call UpdatePlan with the task changed from `[>]` to `[x]` when finished
+5. **Move to next task**: Only ONE task should be in progress at a time
Important rules:
+- Always keep the plan aligned with the latest context before executing the next task
- Always call UpdatePlan BEFORE starting work on a task
- Always call UpdatePlan IMMEDIATELY after completing a task
- Always pass the complete current markdown task list, not a partial diff
- Never work on multiple tasks simultaneously
+- Remove tasks that are no longer relevant, and add newly discovered tasks before working on them
- If you encounter errors, keep the task as `[>]` and create new tasks to resolve blockers
-### Step 5: Handle task breakdown
+### Step 4: Handle task breakdown
If during execution you discover a task is more complex than expected:
@@ -90,7 +90,7 @@ If during execution you discover a task is more complex than expected:
```
3. Complete sub-tasks first, then mark the main task as complete with UpdatePlan
-### Step 6: Final verification
+### Step 5: Final verification
After all tasks are completed (`[x]`):
@@ -226,26 +226,16 @@ Add implementation notes or findings:
- Solution: Added dataloader batching
```
-## Requirements
-
-This Skill uses standard tools:
-
-- **Read**: To inspect referenced files when needed
-- **UpdatePlan**: To create and update the markdown task list
-- **Bash**: To run tests, builds, or other commands
-- **Write**: To create new files if needed
-
-No additional dependencies required.
-
## Workflow Summary
-1. Analyze the requirements
-2. Read referenced files when needed
-3. Call UpdatePlan with the structured markdown task list
+1. Analyze the requirements and relevant project context
+2. Call UpdatePlan with the structured markdown task list
+3. Refresh the remaining plan before the first task
4. For each task:
- Update to `[>]` with UpdatePlan
- Execute the task
- Update to `[x]` with UpdatePlan
+ - Re-evaluate and revise remaining tasks before moving on
5. Call UpdatePlan with all tasks completed and summarize the result
This approach keeps planning and progress tracking in the UpdatePlan display, leaving source materials unchanged unless the actual task requires editing them.
diff --git a/templates/tools/update-plan.md b/templates/tools/update-plan.md
index 28d12f7..a0f2fd6 100644
--- a/templates/tools/update-plan.md
+++ b/templates/tools/update-plan.md
@@ -8,6 +8,8 @@ Usage:
- The `plan` argument is a markdown string, not an array of step objects.
- Keep exactly one task marked `[>]` while work is in progress.
- Update the plan before starting a task, immediately after completing a task, and whenever tasks are split, merged, reordered, blocked, or changed.
+- Before executing the first task and after completing each task, re-evaluate the latest conversation and project context, then revise the remaining plan if needed.
+- Remove tasks that are no longer relevant, and add newly discovered follow-up tasks before working on them.
```json
{
From 33bcd484aad094b45a48d6808d017d3868b5fe78 Mon Sep 17 00:00:00 2001
From: Ji Zhang
Date: Mon, 18 May 2026 11:06:06 +0800
Subject: [PATCH 07/90] feat: update update-plan prompt draft
---
docs/SKILL.md | 252 ----------------------------------------------
docs/SKILL_new.md | 21 ++--
2 files changed, 13 insertions(+), 260 deletions(-)
delete mode 100644 docs/SKILL.md
diff --git a/docs/SKILL.md b/docs/SKILL.md
deleted file mode 100644
index deb8c5e..0000000
--- a/docs/SKILL.md
+++ /dev/null
@@ -1,252 +0,0 @@
----
-name: plan-and-execute
-description: Automatically plan and execute requirements. Creates a markdown task list with the UpdatePlan tool, and systematically executes each task while updating progress. Use when working with task planning or when you need to break down and execute complex multi-step requirements.
----
-
-# Plan and Execute
-
-This Skill helps you automatically plan and execute requirements. It creates a structured markdown task list with the UpdatePlan tool and systematically works through each task while keeping progress visible.
-
-## Quick Start
-
-When you need to work through a multi-step request:
-
-1. Analyze the requirements and explore enough project context
-2. Create a markdown task list by calling the UpdatePlan tool
-3. Execute tasks one by one, updating the tool plan in real time
-4. Revise the remaining plan as new context appears
-
-## Instructions
-
-### Step 1: Analyze the requirements
-
-Identify the requirements from the available context. Explore the project enough to make the plan concrete and accurate.
-
-If a required referenced file path is missing, ask for it:
-
-```
-What is the path to the referenced file?
-```
-
-Referenced files can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions. If no additional file is needed, continue from the available requirements.
-
-- What are the main requirements?
-- What tasks need to be completed?
-- Are there dependencies between tasks?
-- What is the complexity level?
-- Which files, modules, commands, or tests are relevant?
-
-### Step 2: Create the task list
-
-Create a structured markdown task list and pass it to the UpdatePlan tool as the `plan` string. The tool input must use this shape:
-
-```json
-{
- "plan": "## Task List\n\n- [ ] Task 1 description\n- [ ] Task 2 description\n- [ ] Task 3 description"
-}
-```
-
-Use this markdown format for the `plan` content:
-
-```markdown
-## Task List
-
-- [ ] Task 1 description
-- [ ] Task 2 description
-- [ ] Task 3 description
-```
-
-Break down complex requirements into specific, actionable tasks and call UpdatePlan with the full markdown task list.
-
-### Step 3: Execute tasks systematically
-
-For each task in the list:
-
-1. **Refresh the plan**: Before starting the first task and after completing each task, re-evaluate the latest conversation and project context. Update the remaining tasks when scope, order, blockers, or follow-up work changes.
-2. **Mark as in progress**: Call UpdatePlan with the task changed from `[ ]` to `[>]`
-3. **Execute the task**: Use appropriate tools to complete the work
-4. **Mark as completed**: Call UpdatePlan with the task changed from `[>]` to `[x]` when finished
-5. **Move to next task**: Only ONE task should be in progress at a time
-
-Important rules:
-- Always keep the plan aligned with the latest context before executing the next task
-- Always call UpdatePlan BEFORE starting work on a task
-- Always call UpdatePlan IMMEDIATELY after completing a task
-- Always pass the complete current markdown task list, not a partial diff
-- Never work on multiple tasks simultaneously
-- Remove tasks that are no longer relevant, and add newly discovered tasks before working on them
-- If you encounter errors, keep the task as `[>]` and create new tasks to resolve blockers
-
-### Step 4: Handle task breakdown
-
-If during execution you discover a task is more complex than expected:
-
-1. Keep the current task as `[>]`
-2. Call UpdatePlan with new sub-tasks below it with indentation:
- ```markdown
- - [>] Main task
- - [ ] Sub-task 1
- - [ ] Sub-task 2
- ```
-3. Complete sub-tasks first, then mark the main task as complete with UpdatePlan
-
-### Step 5: Final verification
-
-After all tasks are completed (`[x]`):
-
-1. Review the original requirements to ensure everything is addressed
-2. Run any final checks (tests, builds, linting)
-3. Call UpdatePlan with every task marked `[x]`
-4. Provide a concise completion summary in the final response
-
-## Task State Symbols
-
-- `[ ]` - Pending
-- `[>]` - In progress
-- `[x]` - Completed
-- `[!]` - Blocked
-
-## Examples
-
-### Example 1: Simple feature request
-
-**Example requirements:**
-```markdown
-# Feature: Add dark mode toggle
-
-Users should be able to switch between light and dark themes.
-The toggle should be in the settings page.
-```
-
-**UpdatePlan call after analysis:**
-```markdown
-## Task List
-
-- [ ] Create dark mode toggle component in Settings page
-- [ ] Add dark mode state management (context/store)
-- [ ] Implement CSS-in-JS styles for dark theme
-- [ ] Update existing components to support theme switching
-- [ ] Run tests and verify functionality
-```
-
-**UpdatePlan call during execution:**
-```markdown
-## Task List
-
-- [x] Create dark mode toggle component in Settings page
-- [>] Add dark mode state management (context/store)
-- [ ] Implement CSS-in-JS styles for dark theme
-- [ ] Update existing components to support theme switching
-- [ ] Run tests and verify functionality
-```
-
-### Example 2: Bug fix with investigation
-
-**Example requirements:**
-```markdown
-# Bug: Login form crashes on submit
-
-When users click submit, the app crashes.
-Error message: "Cannot read property 'email' of undefined"
-```
-
-**UpdatePlan call after analysis:**
-```markdown
-## Task List
-
-- [ ] Reproduce the bug locally
-- [ ] Investigate the error in login form component
-- [ ] Identify root cause of undefined email property
-- [ ] Implement fix
-- [ ] Add validation to prevent similar issues
-- [ ] Test the fix with various inputs
-- [ ] Update error handling
-```
-
-## When to Use This Skill
-
-Use this Skill when:
-
-1. **Complex multi-step tasks** - Request requires 3+ distinct steps
-2. **Feature implementation** - Building new functionality from requirements
-3. **Bug fixing** - Need to investigate, fix, and verify
-4. **Refactoring** - Multiple files or components need changes
-5. **Detailed requirements** - Specifications need to be translated into concrete tasks
-6. **Need progress tracking** - Want visible progress without editing source files
-
-## When NOT to Use This Skill
-
-Skip this Skill when:
-
-1. **Single simple task** - Just one straightforward action needed
-2. **Trivial changes** - Quick fixes that don't need planning
-3. **Informational requests** - User just wants explanation, not execution
-4. **No execution requested** - User only wants brainstorming or a high-level explanation
-
-## Best Practices
-
-1. **Be specific with tasks**: "Add login button to navbar" not "Update UI"
-2. **Keep tasks atomic**: Each task should be independently completable
-3. **Update immediately**: Don't batch status updates, do them in real-time
-4. **One task at a time**: Never mark multiple tasks as `[>]`
-5. **Handle blockers**: If stuck, create new tasks to resolve the blocker
-6. **Verify completion**: Only mark `[x]` when task is fully done
-
-## Advanced Usage
-
-### Handling dependencies
-
-When tasks have dependencies, order them properly:
-
-```markdown
-- [ ] Create database schema
-- [ ] Implement API endpoints (depends on schema)
-- [ ] Build frontend forms (depends on API)
-```
-
-### Using sub-tasks
-
-For complex tasks, break them down:
-
-```markdown
-- [>] Implement authentication system
- - [x] Set up JWT library
- - [>] Create login endpoint
- - [ ] Create logout endpoint
- - [ ] Add token refresh logic
-```
-
-### Adding notes
-
-Add implementation notes or findings:
-
-```markdown
-- [x] Investigate performance issue
- - Note: Found N+1 query in user loader
- - Solution: Added dataloader batching
-```
-
-## Requirements
-
-This Skill uses standard tools:
-
-- **Read**: To inspect relevant files and explore project context
-- **UpdatePlan**: To create and update the markdown task list
-- **Bash**: To run tests, builds, or other commands
-- **Write**: To create new files if needed
-
-No additional dependencies required.
-
-## Workflow Summary
-
-1. Analyze the requirements and relevant project context
-2. Call UpdatePlan with the structured markdown task list
-3. Refresh the remaining plan before the first task
-4. For each task:
- - Update to `[>]` with UpdatePlan
- - Execute the task
- - Update to `[x]` with UpdatePlan
- - Re-evaluate and revise remaining tasks before moving on
-5. Call UpdatePlan with all tasks completed and summarize the result
-
-This approach keeps planning and progress tracking in the UpdatePlan display, leaving source materials unchanged unless the actual task requires editing them.
diff --git a/docs/SKILL_new.md b/docs/SKILL_new.md
index 41ed251..9037a00 100644
--- a/docs/SKILL_new.md
+++ b/docs/SKILL_new.md
@@ -12,9 +12,10 @@ This Skill helps you automatically plan and execute requirements. It creates a s
When you need to work through a multi-step request:
1. Analyze the requirements and explore enough project context
-2. Create a markdown task list by calling the UpdatePlan tool
-3. Execute tasks one by one, updating the tool plan in real time
-4. Revise the remaining plan as new context appears
+2. Clarify unclear or ambiguous requirements with AskUserQuestion
+3. Create a markdown task list by calling the UpdatePlan tool
+4. Execute tasks one by one, updating the tool plan in real time
+5. Revise the remaining plan as new context appears
## Instructions
@@ -22,7 +23,9 @@ When you need to work through a multi-step request:
Identify the requirements from the available context. Explore the project enough to make the plan concrete and accurate.
-If a required referenced file path is missing, ask for it:
+If the original requirements are unclear, incomplete, or ambiguous, call the AskUserQuestion tool before creating the task list. Ask only the questions needed to avoid implementing the wrong behavior, and keep each question specific to the decision that affects the plan or acceptance criteria.
+
+If a required referenced file path is missing, ask for it with AskUserQuestion:
```
What is the path to the referenced file?
@@ -35,6 +38,7 @@ Referenced files can be in any text format (.md, .txt, etc.) that contains task
- Are there dependencies between tasks?
- What is the complexity level?
- Which files, modules, commands, or tests are relevant?
+- What ambiguity would change the implementation or acceptance criteria?
### Step 2: Create the task list
@@ -229,13 +233,14 @@ Add implementation notes or findings:
## Workflow Summary
1. Analyze the requirements and relevant project context
-2. Call UpdatePlan with the structured markdown task list
-3. Refresh the remaining plan before the first task
-4. For each task:
+2. Call AskUserQuestion if the original requirements are unclear or ambiguous
+3. Call UpdatePlan with the structured markdown task list
+4. Refresh the remaining plan before the first task
+5. For each task:
- Update to `[>]` with UpdatePlan
- Execute the task
- Update to `[x]` with UpdatePlan
- Re-evaluate and revise remaining tasks before moving on
-5. Call UpdatePlan with all tasks completed and summarize the result
+6. Call UpdatePlan with all tasks completed and summarize the result
This approach keeps planning and progress tracking in the UpdatePlan display, leaving source materials unchanged unless the actual task requires editing them.
From 32e5796c9e188defdb69c8f3db75a539f5c9dc0c Mon Sep 17 00:00:00 2001
From: Ji Zhang
Date: Mon, 18 May 2026 11:57:39 +0800
Subject: [PATCH 08/90] feat: update update-plan prompt draft
---
docs/SKILL_new.md | 48 +++++++++++++++++-----------------
templates/tools/update-plan.md | 2 +-
2 files changed, 25 insertions(+), 25 deletions(-)
diff --git a/docs/SKILL_new.md b/docs/SKILL_new.md
index 9037a00..9fc8bd2 100644
--- a/docs/SKILL_new.md
+++ b/docs/SKILL_new.md
@@ -116,55 +116,55 @@ After all tasks are completed (`[x]`):
**Example requirements:**
```markdown
-# Feature: Add dark mode toggle
+# 新功能:添加深色模式切换
-Users should be able to switch between light and dark themes.
-The toggle should be in the settings page.
+用户应该能够在浅色和深色主题之间切换。
+切换开关应放在设置页面中。
```
-**UpdatePlan call after analysis:**
+**分析后的 UpdatePlan 调用:**
```markdown
## Task List
-- [ ] Create dark mode toggle component in Settings page
-- [ ] Add dark mode state management (context/store)
-- [ ] Implement CSS-in-JS styles for dark theme
-- [ ] Update existing components to support theme switching
-- [ ] Run tests and verify functionality
+- [ ] 在设置页面创建深色模式切换组件
+- [ ] 添加深色模式状态管理(context/store)
+- [ ] 实现深色主题的 CSS-in-JS 样式
+- [ ] 更新现有组件以支持主题切换
+- [ ] 运行测试并验证功能
```
**UpdatePlan call during execution:**
```markdown
## Task List
-- [x] Create dark mode toggle component in Settings page
-- [>] Add dark mode state management (context/store)
-- [ ] Implement CSS-in-JS styles for dark theme
-- [ ] Update existing components to support theme switching
-- [ ] Run tests and verify functionality
+- [x] 在设置页面创建深色模式切换组件
+- [>] 添加深色模式状态管理(context/store)
+- [ ] 实现深色主题的 CSS-in-JS 样式
+- [ ] 更新现有组件以支持主题切换
+- [ ] 运行测试并验证功能
```
### Example 2: Bug fix with investigation
**Example requirements:**
```markdown
-# Bug: Login form crashes on submit
+# Fix bug:登录表单提交时崩溃
-When users click submit, the app crashes.
-Error message: "Cannot read property 'email' of undefined"
+当用户点击提交时,应用崩溃。
+错误信息:"Cannot read property 'email' of undefined"
```
**UpdatePlan call after analysis:**
```markdown
## Task List
-- [ ] Reproduce the bug locally
-- [ ] Investigate the error in login form component
-- [ ] Identify root cause of undefined email property
-- [ ] Implement fix
-- [ ] Add validation to prevent similar issues
-- [ ] Test the fix with various inputs
-- [ ] Update error handling
+- [ ] 在本地复现缺陷
+- [ ] 调查登录表单组件中的错误
+- [ ] 定位 undefined email 属性的根本原因
+- [ ] 实施修复
+- [ ] 添加验证以防止类似问题
+- [ ] 使用各种输入测试修复
+- [ ] 更新错误处理
```
## When to Use This Skill
diff --git a/templates/tools/update-plan.md b/templates/tools/update-plan.md
index a0f2fd6..0c74b36 100644
--- a/templates/tools/update-plan.md
+++ b/templates/tools/update-plan.md
@@ -5,7 +5,7 @@ Updates the current task plan and progress display.
Usage:
- Use this tool for non-trivial multi-step tasks when a task list helps track execution progress.
- Pass the complete current task list every time. The latest call replaces the previous visible plan.
-- The `plan` argument is a markdown string, not an array of step objects.
+- The `plan` argument is a markdown string, not an array of step objects. If the requirement is in Chinese, then use Chinese for the markdown as well.
- Keep exactly one task marked `[>]` while work is in progress.
- Update the plan before starting a task, immediately after completing a task, and whenever tasks are split, merged, reordered, blocked, or changed.
- Before executing the first task and after completing each task, re-evaluate the latest conversation and project context, then revise the remaining plan if needed.
From 77a779fe341aaa81a726dea319bd2b85c7fb2ab9 Mon Sep 17 00:00:00 2001
From: Ji Zhang
Date: Mon, 18 May 2026 14:29:34 +0800
Subject: [PATCH 09/90] feat: change the UpdatePlan display
---
src/session.ts | 2 ++
src/tests/session.test.ts | 21 +++++++++++++++++++++
2 files changed, 23 insertions(+)
diff --git a/src/session.ts b/src/session.ts
index 431eb40..dde29b3 100644
--- a/src/session.ts
+++ b/src/session.ts
@@ -2032,6 +2032,8 @@ ${skillMd}
if (description) {
return description;
}
+ } else if (toolName === "UpdatePlan") {
+ return typeof args.explanation === "string" ? args.explanation.trim() : "";
}
const firstKey = Object.keys(args)[0];
diff --git a/src/tests/session.test.ts b/src/tests/session.test.ts
index e5ab740..7bc98f9 100644
--- a/src/tests/session.test.ts
+++ b/src/tests/session.test.ts
@@ -1160,6 +1160,27 @@ test("buildOpenAIMessages preserves a real failed tool result", () => {
assert.doesNotMatch(openAIMessages[1]?.content ?? "", /Previous tool call did not complete/);
});
+test("UpdatePlan tool params only show explanation when provided", () => {
+ const manager = createSessionManager(process.cwd(), "machine-id-update-plan-params");
+ const plan = "## Task List\n\n- [ ] Inspect project";
+
+ const withExplanation = (manager as any).buildToolMessage(
+ "session-1",
+ "call-plan-1",
+ JSON.stringify({ ok: true, name: "UpdatePlan", output: "Plan updated." }),
+ { name: "UpdatePlan", arguments: JSON.stringify({ plan, explanation: "Start planning" }) }
+ ) as SessionMessage;
+ const withoutExplanation = (manager as any).buildToolMessage(
+ "session-1",
+ "call-plan-2",
+ JSON.stringify({ ok: true, name: "UpdatePlan", output: "Plan updated." }),
+ { name: "UpdatePlan", arguments: JSON.stringify({ plan }) }
+ ) as SessionMessage;
+
+ assert.equal(withExplanation.meta?.paramsMd, "Start planning");
+ assert.equal(withoutExplanation.meta?.paramsMd, "");
+});
+
test("buildOpenAIMessages repairs mixed missing duplicate and orphan tool messages", () => {
const manager = createSessionManager(process.cwd(), "machine-id-mixed-tool-badcase");
const assistantMessage = (manager as any).buildAssistantMessage(
From e691b3efcf32e24d4521a29e17b0a512960d2a79 Mon Sep 17 00:00:00 2001
From: Ji Zhang
Date: Mon, 18 May 2026 15:39:26 +0800
Subject: [PATCH 10/90] feat: add default skill templates and update session
management to include skill prompts
---
package.json | 1 +
src/prompt.ts | 198 ++++-----------------
src/session.ts | 28 ++-
src/tests/prompt.test.ts | 36 +++-
src/tests/session.test.ts | 35 ++++
templates/skills/agent-drift-guard.md | 152 ++++++++++++++++
templates/skills/plan-and-execute.md | 246 ++++++++++++++++++++++++++
7 files changed, 528 insertions(+), 168 deletions(-)
create mode 100644 templates/skills/agent-drift-guard.md
create mode 100644 templates/skills/plan-and-execute.md
diff --git a/package.json b/package.json
index 90c2b51..e61d81d 100644
--- a/package.json
+++ b/package.json
@@ -17,6 +17,7 @@
"dist/cli.js",
"templates/tools/**",
"templates/prompts/**",
+ "templates/skills/**",
"README.md",
"LICENSE"
],
diff --git a/src/prompt.ts b/src/prompt.ts
index 50aa2a3..717991b 100644
--- a/src/prompt.ts
+++ b/src/prompt.ts
@@ -8,161 +8,6 @@ import type { SessionMessage } from "./session";
import { findGitBashPath, resolveShellPath } from "./common/shell-utils";
import { supportsMultimodal } from "./common/model-capabilities";
-export const AGENT_DRIFT_GUARD_SKILL = `
----
-name: agent-drift-guard
-description: Detect and correct execution drift while working on user requests. Use when you are actively implementing, debugging, reviewing, or investigating and there is a risk of wandering beyond the user's goal, adding unrequested work, touching live systems, over-exploring, or ignoring repeated user boundary corrections. Especially useful during multi-step coding tasks, production-adjacent requests, ambiguous scopes, and anytime you should self-check whether it is still solving the requested problem.
----
-
-# Agent Drift Guard
-
-Keep execution tightly aligned with the user's actual request.
-
-## Quick Start
-
-Run this mental check before substantial work and again whenever the plan expands:
-
-1. State the user's requested outcome in one sentence.
-2. List explicit non-goals or boundaries the user has set.
-3. Ask whether the next action directly advances the requested outcome.
-4. If not, either cut it or pause to confirm.
-
-## Drift Signals
-
-Treat these as warning signs that execution may be drifting:
-
-- Exploring broadly before opening the most relevant file, command, or artifact.
-- Solving adjacent operational issues when the user asked only for code changes.
-- Adding extra safeguards, scripts, docs, refactors, or cleanup that the user did not ask for.
-- Reframing the task around what seems "better" instead of what was requested.
-- Continuing with a broader plan after the user narrows the scope.
-- Repeating searches or tool calls without increasing certainty.
-- Mixing diagnosis, remediation, and feature work when the user asked for only one of them.
-- Touching production-like state, external systems, or live data without explicit permission.
-
-## Severity Levels
-
-### Level 1: Mild Drift
-
-Examples:
-- One or two extra exploratory commands.
-- Considering a broader solution but not acting on it yet.
-- Briefly over-explaining instead of moving the task forward.
-
-Response:
-- Auto-correct silently.
-- Narrow to the smallest next action.
-- Do not interrupt the user.
-
-### Level 2: Material Drift
-
-Examples:
-- Planning additional deliverables not requested.
-- Writing helper scripts, migrations, docs, or tests outside the asked scope.
-- Expanding from code changes into operational fixes.
-- Continuing after the user has already corrected the scope once.
-
-Response:
-- Stop and realign internally first.
-- If the broader action is avoidable, drop it and continue on scope.
-- If the broader action has non-obvious tradeoffs, ask a brief confirmation question.
-
-### Level 3: Boundary or Risk Violation
-
-Examples:
-- Modifying live systems, production data, external services, or user-owned state without being asked.
-- Taking destructive or hard-to-reverse actions outside the requested scope.
-- Ignoring repeated user instructions about what not to do.
-
-Response:
-- Pause before acting.
-- Surface the exact boundary and ask for confirmation.
-- Offer the smallest on-scope option first.
-
-## Self-Check Loop
-
-Use this loop during execution:
-
-### Before the first meaningful action
-
-Write down mentally:
-- Requested outcome
-- Allowed scope
-- Forbidden scope
-- Smallest useful next step
-
-### After each non-trivial step
-
-Ask:
-- Did this step directly help deliver the requested outcome?
-- Did I learn something that changes scope, or only implementation?
-- Am I about to do more than the user asked?
-
-### After a user correction
-
-Treat the correction as a hard boundary update.
-
-Then:
-- Remove the old broader plan.
-- Do not defend the discarded work.
-- Continue from the narrowed scope.
-- If needed, acknowledge briefly and move on.
-
-## Decision Rules
-
-Use these rules in order:
-
-1. Prefer the most direct artifact first.
- - Open the relevant file before scanning the whole repo.
- - Inspect the specific failing path before designing a general framework.
-
-2. Prefer the smallest complete fix.
- - Solve the asked problem before improving related systems.
- - Avoid bonus work unless it is required for correctness.
-
-3. Prefer internal correction over user interruption.
- - If you can shrink back to scope confidently, do it.
- - Ask only when the next step changes deliverables, risk, or ownership.
-
-4. Treat repeated user constraints as priority signals.
- - A repeated instruction means your execution style is currently misaligned.
- - Tighten scope immediately.
-
-5. Separate categories of work.
- - Code change, investigation, production remediation, cleanup, and documentation are distinct tasks unless the user explicitly combines them.
-
-## Good Intervention Style
-
-When you must pause, keep it short and specific:
-
-- State the potential drift in one sentence.
-- Name the tradeoff or boundary.
-- Offer the smallest on-scope option first.
-
-Example:
-
-"Quick alignment check: I can keep this to the code fix only, or also add an ops cleanup step. I'll stick to the code fix unless you want both."
-
-## Anti-Patterns
-
-Do not:
-
-- Create cleanup scripts, docs, or side tools just because they seem useful.
-- Broaden the task after discovering a neighboring problem.
-- Continue with a plan the user has already rejected.
-- Justify drift with "best practice" when the user asked for a narrower deliverable.
-- Hide extra work inside a larger patch.
-
-## Final Check Before Responding
-
-Before sending the final answer, verify:
-
-- The delivered work matches the requested outcome.
-- No extra deliverables were added without confirmation.
-- Any assumptions are stated briefly.
-- Suggested next steps are optional, not bundled into the completed work.
-`;
-
const COMPACT_PROMPT_BASE = `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
@@ -254,6 +99,8 @@ type PromptToolOptions = {
webSearchEnabled?: boolean;
};
+const DEFAULT_SKILL_TEMPLATES = ["agent-drift-guard.md", "plan-and-execute.md"];
+
function readToolDocs(extensionRoot: string, options: PromptToolOptions = {}): string {
const toolsDir = path.join(extensionRoot, "templates", "tools");
if (!fs.existsSync(toolsDir)) {
@@ -281,6 +128,35 @@ function readToolDocs(extensionRoot: string, options: PromptToolOptions = {}): s
return docs.join("\n\n");
}
+function readDefaultSkillDocs(extensionRoot: string): Array<{ name: string; content: string }> {
+ const skillsDir = path.join(extensionRoot, "templates", "skills");
+ return DEFAULT_SKILL_TEMPLATES.map((entry) => {
+ const fullPath = path.join(skillsDir, entry);
+ try {
+ return {
+ name: path.basename(entry, ".md"),
+ content: fs.readFileSync(fullPath, "utf8").trim(),
+ };
+ } catch {
+ return null;
+ }
+ }).filter((skill): skill is { name: string; content: string } => Boolean(skill?.content));
+}
+
+export function getDefaultSkillPrompt(): string {
+ const skillDocs = readDefaultSkillDocs(getExtensionRoot());
+ if (skillDocs.length === 0) {
+ return "";
+ }
+
+ const blocks = skillDocs.map(
+ (skill) => `<${skill.name}-skill>
+${skill.content}
+${skill.name}-skill>`
+ );
+ return `Use the skill documents below to assist the user:\n${blocks.join("\n\n")}`;
+}
+
function getCurrentDateAndModelPrompt(model?: string): string {
const date = new Date();
let prompt = `今天是${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日。随着对话的进行,时间在流逝。`;
@@ -288,10 +164,10 @@ function getCurrentDateAndModelPrompt(model?: string): string {
return prompt;
}
-export function getSystemPrompt(projectRoot: string, options: PromptToolOptions = {}): string {
+export function getSystemPrompt(_projectRoot: string, options: PromptToolOptions = {}): string {
const toolDocs = readToolDocs(getExtensionRoot(), options);
const basePrompt = toolDocs ? `${SYSTEM_PROMPT_BASE}\n\n# Available Tools\n\n${toolDocs}` : SYSTEM_PROMPT_BASE;
- return `${basePrompt}\n\n${getCurrentDateAndModelPrompt(options.model)}\n\n${getRuntimeContext(projectRoot)}`;
+ return basePrompt;
}
export function getCompactPrompt(sessionMessages: SessionMessage[]): string {
@@ -310,7 +186,7 @@ export function getCompactPrompt(sessionMessages: SessionMessage[]): string {
return `${COMPACT_PROMPT_BASE}\n\nconversation below:\n\n\`\`\`jsonl\n${jsonl}\n\`\`\``;
}
-function getRuntimeContext(projectRoot: string): string {
+export function getRuntimeContext(projectRoot: string, model?: string): string {
const uname = getUnameInfo();
const shellPath = getShellPathInfo();
const shellModeOpts = process.platform === "win32" ? { "shell mode": "git-bash" } : {};
@@ -328,7 +204,11 @@ function getRuntimeContext(projectRoot: string): string {
jq: checkToolInstalled("jq"),
},
};
- return `# Local Workspace Environment\n\n\`\`\`json
+ return `${getCurrentDateAndModelPrompt(model)}
+
+# Local Workspace Environment
+
+\`\`\`json
${JSON.stringify(env, null, 2)}
\`\`\``;
}
diff --git a/src/session.ts b/src/session.ts
index dde29b3..1c9d3b6 100644
--- a/src/session.ts
+++ b/src/session.ts
@@ -9,7 +9,14 @@ import type { ChatCompletionMessageParam, ChatCompletionContentPart } from "open
import { launchNotifyScript } from "./common/notify";
import { buildThinkingRequestOptions } from "./common/openai-thinking";
import { DEEPSEEK_V4_MODELS, supportsMultimodal } from "./common/model-capabilities";
-import { getCompactPrompt, getSystemPrompt, getTools, AGENT_DRIFT_GUARD_SKILL, type ToolDefinition } from "./prompt";
+import {
+ getCompactPrompt,
+ getDefaultSkillPrompt,
+ getRuntimeContext,
+ getSystemPrompt,
+ getTools,
+ type ToolDefinition,
+} from "./prompt";
import { ToolExecutor, type CreateOpenAIClient } from "./tools/executor";
import { McpManager } from "./mcp/mcp-manager";
import type { McpServerConfig } from "./settings";
@@ -907,20 +914,29 @@ The candidate skills are as follows:\n\n`;
this.saveSessionsIndex(index);
this.removeSessionMessages(droppedEntries.map((item) => item.id));
- const systemPrompt = getSystemPrompt(this.projectRoot, this.getPromptToolOptions());
+ const promptToolOptions = this.getPromptToolOptions();
+ const systemPrompt = getSystemPrompt(this.projectRoot, promptToolOptions);
const systemMessage = this.buildSystemMessage(sessionId, systemPrompt);
this.appendSessionMessage(sessionId, systemMessage);
+ const defaultSkillPrompt = getDefaultSkillPrompt();
+ if (defaultSkillPrompt) {
+ const defaultSkillMessage = this.buildSystemMessage(sessionId, defaultSkillPrompt);
+ this.appendSessionMessage(sessionId, defaultSkillMessage);
+ }
+
+ const runtimeContextMessage = this.buildSystemMessage(
+ sessionId,
+ getRuntimeContext(this.projectRoot, promptToolOptions.model)
+ );
+ this.appendSessionMessage(sessionId, runtimeContextMessage);
+
const agentInstructions = this.loadAgentInstructions();
if (agentInstructions) {
const instructionsMessage = this.buildSystemMessage(sessionId, agentInstructions);
this.appendSessionMessage(sessionId, instructionsMessage);
}
- const defaultSkillPrompt = `Use the skill document below to assist the user:\n${AGENT_DRIFT_GUARD_SKILL}`;
- const defaultSkillMessage = this.buildSystemMessage(sessionId, defaultSkillPrompt);
- this.appendSessionMessage(sessionId, defaultSkillMessage);
-
const userMessage = this.buildUserMessage(sessionId, userPrompt);
this.appendSessionMessage(sessionId, userMessage);
diff --git a/src/tests/prompt.test.ts b/src/tests/prompt.test.ts
index b7c9178..cc86712 100644
--- a/src/tests/prompt.test.ts
+++ b/src/tests/prompt.test.ts
@@ -3,7 +3,7 @@ import assert from "node:assert/strict";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
-import { getSystemPrompt, getTools } from "../prompt";
+import { getDefaultSkillPrompt, getRuntimeContext, getSystemPrompt, getTools } from "../prompt";
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
@@ -30,11 +30,39 @@ test("getSystemPrompt includes UpdatePlan docs", () => {
assert.equal(prompt.includes("The `plan` argument is a markdown string, not an array of step objects."), true);
});
-test("getSystemPrompt includes current date guidance", () => {
+test("getSystemPrompt does not include runtime context", () => {
+ const prompt = getSystemPrompt("/tmp/project");
+ assert.equal(prompt.includes("# Local Workspace Environment"), false);
+ assert.equal(prompt.includes('"root path": "/tmp/project"'), false);
+});
+
+test("getDefaultSkillPrompt loads default skill templates in order", () => {
+ const prompt = getDefaultSkillPrompt();
+ const agentDriftIndex = prompt.indexOf("");
+ const planIndex = prompt.indexOf("");
+
+ assert.notEqual(agentDriftIndex, -1);
+ assert.notEqual(planIndex, -1);
+ assert.equal(agentDriftIndex < planIndex, true);
+ assert.equal(prompt.includes("Use the skill documents below to assist the user:"), true);
+ assert.equal(prompt.includes('path="templates/skills/'), false);
+});
+
+test("getSystemPrompt does not include current date guidance", () => {
const now = new Date();
const expected = `今天是${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日。随着对话的进行,时间在流逝。`;
const prompt = getSystemPrompt("/tmp/project");
- assert.equal(prompt.includes(expected), true);
+ assert.equal(prompt.includes(expected), false);
+});
+
+test("getRuntimeContext includes current date and model guidance", () => {
+ const now = new Date();
+ const expectedDate = `今天是${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日。随着对话的进行,时间在流逝。`;
+ const prompt = getRuntimeContext("/tmp/project", "deepseek-v4-pro");
+ assert.equal(prompt.includes(expectedDate), true);
+ assert.equal(prompt.includes("当前LLM模型为deepseek-v4-pro,对话中可通过/model命令切换模型。"), true);
+ assert.equal(prompt.includes("# Local Workspace Environment"), true);
+ assert.equal(prompt.includes('"root path": "/tmp/project"'), true);
});
test("getSystemPrompt renders Read docs for non-multimodal models", () => {
@@ -47,6 +75,8 @@ test("runtime prompt assets live under templates", () => {
assert.equal(fs.existsSync(path.join(repoRoot, "templates", "tools", "web-search.md")), true);
assert.equal(fs.existsSync(path.join(repoRoot, "templates", "tools", "read.md.ejs")), true);
assert.equal(fs.existsSync(path.join(repoRoot, "templates", "prompts", "init_command.md.ejs")), true);
+ assert.equal(fs.existsSync(path.join(repoRoot, "templates", "skills", "agent-drift-guard.md")), true);
+ assert.equal(fs.existsSync(path.join(repoRoot, "templates", "skills", "plan-and-execute.md")), true);
assert.equal(fs.existsSync(path.join(repoRoot, "templates", "tools", "read.md")), false);
assert.equal(fs.existsSync(path.join(repoRoot, "docs", "tools")), false);
assert.equal(fs.existsSync(path.join(repoRoot, "docs", "prompts")), false);
diff --git a/src/tests/session.test.ts b/src/tests/session.test.ts
index 7bc98f9..10e3b2c 100644
--- a/src/tests/session.test.ts
+++ b/src/tests/session.test.ts
@@ -609,6 +609,37 @@ test("createSession stores /init and sends the active .deepcode project AGENTS p
assert.ok(!systemContents.includes("root project instructions"));
});
+test("createSession appends default system prompts in prefix-cache-friendly order", async () => {
+ const workspace = createTempDir("deepcode-system-order-workspace-");
+ const home = createTempDir("deepcode-system-order-home-");
+ setHomeDir(home);
+ globalThis.fetch = (async () => ({ ok: true, text: async () => "" }) as Response) as typeof fetch;
+
+ fs.writeFileSync(path.join(workspace, "AGENTS.md"), "root project instructions", "utf8");
+
+ const manager = createSessionManager(workspace, "machine-id-system-order");
+ (manager as any).activateSession = async () => {};
+
+ const sessionId = await manager.createSession({ text: "hello" });
+ const systemContents = manager
+ .listSessionMessages(sessionId)
+ .filter((message) => message.role === "system")
+ .map((message) => message.content ?? "");
+
+ assert.equal(systemContents.length >= 4, true);
+ assert.match(systemContents[0] ?? "", /# Available Tools/);
+ assert.doesNotMatch(systemContents[0] ?? "", /# Local Workspace Environment/);
+ assert.doesNotMatch(systemContents[0] ?? "", /当前LLM模型为test-model/);
+ assert.match(systemContents[1] ?? "", //);
+ assert.match(systemContents[1] ?? "", //);
+ assert.doesNotMatch(systemContents[1] ?? "", /path="templates\/skills\//);
+ assert.doesNotMatch(systemContents[1] ?? "", /当前LLM模型为test-model/);
+ assert.match(systemContents[2] ?? "", /# Local Workspace Environment/);
+ assert.match(systemContents[2] ?? "", /当前LLM模型为test-model/);
+ assert.match(systemContents[2] ?? "", new RegExp(escapeRegExp(`"root path": "${workspace}"`)));
+ assert.equal(systemContents[3], "root project instructions");
+});
+
test("replySession stores /init and sends the active root project AGENTS path to the LLM", async () => {
const workspace = createTempDir("deepcode-init-root-workspace-");
const home = createTempDir("deepcode-init-root-home-");
@@ -1686,6 +1717,10 @@ function createTempDir(prefix: string): string {
return dir;
}
+function escapeRegExp(value: string): string {
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+}
+
async function flushPromises(): Promise {
await new Promise((resolve) => setImmediate(resolve));
}
diff --git a/templates/skills/agent-drift-guard.md b/templates/skills/agent-drift-guard.md
new file mode 100644
index 0000000..c6711b1
--- /dev/null
+++ b/templates/skills/agent-drift-guard.md
@@ -0,0 +1,152 @@
+---
+name: agent-drift-guard
+description: Detect and correct execution drift while working on user requests. Use when you are actively implementing, debugging, reviewing, or investigating and there is a risk of wandering beyond the user's goal, adding unrequested work, touching live systems, over-exploring, or ignoring repeated user boundary corrections. Especially useful during multi-step coding tasks, production-adjacent requests, ambiguous scopes, and anytime you should self-check whether it is still solving the requested problem.
+---
+
+# Agent Drift Guard
+
+Keep execution tightly aligned with the user's actual request.
+
+## Quick Start
+
+Run this mental check before substantial work and again whenever the plan expands:
+
+1. State the user's requested outcome in one sentence.
+2. List explicit non-goals or boundaries the user has set.
+3. Ask whether the next action directly advances the requested outcome.
+4. If not, either cut it or pause to confirm.
+
+## Drift Signals
+
+Treat these as warning signs that execution may be drifting:
+
+- Exploring broadly before opening the most relevant file, command, or artifact.
+- Solving adjacent operational issues when the user asked only for code changes.
+- Adding extra safeguards, scripts, docs, refactors, or cleanup that the user did not ask for.
+- Reframing the task around what seems "better" instead of what was requested.
+- Continuing with a broader plan after the user narrows the scope.
+- Repeating searches or tool calls without increasing certainty.
+- Mixing diagnosis, remediation, and feature work when the user asked for only one of them.
+- Touching production-like state, external systems, or live data without explicit permission.
+
+## Severity Levels
+
+### Level 1: Mild Drift
+
+Examples:
+- One or two extra exploratory commands.
+- Considering a broader solution but not acting on it yet.
+- Briefly over-explaining instead of moving the task forward.
+
+Response:
+- Auto-correct silently.
+- Narrow to the smallest next action.
+- Do not interrupt the user.
+
+### Level 2: Material Drift
+
+Examples:
+- Planning additional deliverables not requested.
+- Writing helper scripts, migrations, docs, or tests outside the asked scope.
+- Expanding from code changes into operational fixes.
+- Continuing after the user has already corrected the scope once.
+
+Response:
+- Stop and realign internally first.
+- If the broader action is avoidable, drop it and continue on scope.
+- If the broader action has non-obvious tradeoffs, ask a brief confirmation question.
+
+### Level 3: Boundary or Risk Violation
+
+Examples:
+- Modifying live systems, production data, external services, or user-owned state without being asked.
+- Taking destructive or hard-to-reverse actions outside the requested scope.
+- Ignoring repeated user instructions about what not to do.
+
+Response:
+- Pause before acting.
+- Surface the exact boundary and ask for confirmation.
+- Offer the smallest on-scope option first.
+
+## Self-Check Loop
+
+Use this loop during execution:
+
+### Before the first meaningful action
+
+Write down mentally:
+- Requested outcome
+- Allowed scope
+- Forbidden scope
+- Smallest useful next step
+
+### After each non-trivial step
+
+Ask:
+- Did this step directly help deliver the requested outcome?
+- Did I learn something that changes scope, or only implementation?
+- Am I about to do more than the user asked?
+
+### After a user correction
+
+Treat the correction as a hard boundary update.
+
+Then:
+- Remove the old broader plan.
+- Do not defend the discarded work.
+- Continue from the narrowed scope.
+- If needed, acknowledge briefly and move on.
+
+## Decision Rules
+
+Use these rules in order:
+
+1. Prefer the most direct artifact first.
+ - Open the relevant file before scanning the whole repo.
+ - Inspect the specific failing path before designing a general framework.
+
+2. Prefer the smallest complete fix.
+ - Solve the asked problem before improving related systems.
+ - Avoid bonus work unless it is required for correctness.
+
+3. Prefer internal correction over user interruption.
+ - If you can shrink back to scope confidently, do it.
+ - Ask only when the next step changes deliverables, risk, or ownership.
+
+4. Treat repeated user constraints as priority signals.
+ - A repeated instruction means your execution style is currently misaligned.
+ - Tighten scope immediately.
+
+5. Separate categories of work.
+ - Code change, investigation, production remediation, cleanup, and documentation are distinct tasks unless the user explicitly combines them.
+
+## Good Intervention Style
+
+When you must pause, keep it short and specific:
+
+- State the potential drift in one sentence.
+- Name the tradeoff or boundary.
+- Offer the smallest on-scope option first.
+
+Example:
+
+"Quick alignment check: I can keep this to the code fix only, or also add an ops cleanup step. I'll stick to the code fix unless you want both."
+
+## Anti-Patterns
+
+Do not:
+
+- Create cleanup scripts, docs, or side tools just because they seem useful.
+- Broaden the task after discovering a neighboring problem.
+- Continue with a plan the user has already rejected.
+- Justify drift with "best practice" when the user asked for a narrower deliverable.
+- Hide extra work inside a larger patch.
+
+## Final Check Before Responding
+
+Before sending the final answer, verify:
+
+- The delivered work matches the requested outcome.
+- No extra deliverables were added without confirmation.
+- Any assumptions are stated briefly.
+- Suggested next steps are optional, not bundled into the completed work.
diff --git a/templates/skills/plan-and-execute.md b/templates/skills/plan-and-execute.md
new file mode 100644
index 0000000..9fc8bd2
--- /dev/null
+++ b/templates/skills/plan-and-execute.md
@@ -0,0 +1,246 @@
+---
+name: plan-and-execute
+description: Automatically plan and execute requirements. Creates a markdown task list with the UpdatePlan tool, and systematically executes each task while updating progress. Use when working with task planning or when you need to break down and execute complex multi-step requirements.
+---
+
+# Plan and Execute
+
+This Skill helps you automatically plan and execute requirements. It creates a structured markdown task list with the UpdatePlan tool and systematically works through each task while keeping progress visible.
+
+## Quick Start
+
+When you need to work through a multi-step request:
+
+1. Analyze the requirements and explore enough project context
+2. Clarify unclear or ambiguous requirements with AskUserQuestion
+3. Create a markdown task list by calling the UpdatePlan tool
+4. Execute tasks one by one, updating the tool plan in real time
+5. Revise the remaining plan as new context appears
+
+## Instructions
+
+### Step 1: Analyze the requirements
+
+Identify the requirements from the available context. Explore the project enough to make the plan concrete and accurate.
+
+If the original requirements are unclear, incomplete, or ambiguous, call the AskUserQuestion tool before creating the task list. Ask only the questions needed to avoid implementing the wrong behavior, and keep each question specific to the decision that affects the plan or acceptance criteria.
+
+If a required referenced file path is missing, ask for it with AskUserQuestion:
+
+```
+What is the path to the referenced file?
+```
+
+Referenced files can be in any text format (.md, .txt, etc.) that contains task requirements or feature descriptions. If no additional file is needed, continue from the available requirements.
+
+- What are the main requirements?
+- What tasks need to be completed?
+- Are there dependencies between tasks?
+- What is the complexity level?
+- Which files, modules, commands, or tests are relevant?
+- What ambiguity would change the implementation or acceptance criteria?
+
+### Step 2: Create the task list
+
+Create a structured markdown task list and pass it to the UpdatePlan tool as the `plan` string. The tool input must use this shape:
+
+```json
+{
+ "plan": "## Task List\n\n- [ ] Task 1 description\n- [ ] Task 2 description\n- [ ] Task 3 description"
+}
+```
+
+Use this markdown format for the `plan` content:
+
+```markdown
+## Task List
+
+- [ ] Task 1 description
+- [ ] Task 2 description
+- [ ] Task 3 description
+```
+
+Break down complex requirements into specific, actionable tasks and call UpdatePlan with the full markdown task list.
+
+### Step 3: Execute tasks systematically
+
+For each task in the list:
+
+1. **Refresh the plan**: Before starting the first task and after completing each task, re-evaluate the latest conversation and project context. Update the remaining tasks when scope, order, blockers, or follow-up work changes.
+2. **Mark as in progress**: Call UpdatePlan with the task changed from `[ ]` to `[>]`
+3. **Execute the task**: Use appropriate tools to complete the work
+4. **Mark as completed**: Call UpdatePlan with the task changed from `[>]` to `[x]` when finished
+5. **Move to next task**: Only ONE task should be in progress at a time
+
+Important rules:
+- Always keep the plan aligned with the latest context before executing the next task
+- Always call UpdatePlan BEFORE starting work on a task
+- Always call UpdatePlan IMMEDIATELY after completing a task
+- Always pass the complete current markdown task list, not a partial diff
+- Never work on multiple tasks simultaneously
+- Remove tasks that are no longer relevant, and add newly discovered tasks before working on them
+- If you encounter errors, keep the task as `[>]` and create new tasks to resolve blockers
+
+### Step 4: Handle task breakdown
+
+If during execution you discover a task is more complex than expected:
+
+1. Keep the current task as `[>]`
+2. Call UpdatePlan with new sub-tasks below it with indentation:
+ ```markdown
+ - [>] Main task
+ - [ ] Sub-task 1
+ - [ ] Sub-task 2
+ ```
+3. Complete sub-tasks first, then mark the main task as complete with UpdatePlan
+
+### Step 5: Final verification
+
+After all tasks are completed (`[x]`):
+
+1. Review the original requirements to ensure everything is addressed
+2. Run any final checks (tests, builds, linting)
+3. Call UpdatePlan with every task marked `[x]`
+4. Provide a concise completion summary in the final response
+
+## Task State Symbols
+
+- `[ ]` - Pending
+- `[>]` - In progress
+- `[x]` - Completed
+- `[!]` - Blocked
+
+## Examples
+
+### Example 1: Simple feature request
+
+**Example requirements:**
+```markdown
+# 新功能:添加深色模式切换
+
+用户应该能够在浅色和深色主题之间切换。
+切换开关应放在设置页面中。
+```
+
+**分析后的 UpdatePlan 调用:**
+```markdown
+## Task List
+
+- [ ] 在设置页面创建深色模式切换组件
+- [ ] 添加深色模式状态管理(context/store)
+- [ ] 实现深色主题的 CSS-in-JS 样式
+- [ ] 更新现有组件以支持主题切换
+- [ ] 运行测试并验证功能
+```
+
+**UpdatePlan call during execution:**
+```markdown
+## Task List
+
+- [x] 在设置页面创建深色模式切换组件
+- [>] 添加深色模式状态管理(context/store)
+- [ ] 实现深色主题的 CSS-in-JS 样式
+- [ ] 更新现有组件以支持主题切换
+- [ ] 运行测试并验证功能
+```
+
+### Example 2: Bug fix with investigation
+
+**Example requirements:**
+```markdown
+# Fix bug:登录表单提交时崩溃
+
+当用户点击提交时,应用崩溃。
+错误信息:"Cannot read property 'email' of undefined"
+```
+
+**UpdatePlan call after analysis:**
+```markdown
+## Task List
+
+- [ ] 在本地复现缺陷
+- [ ] 调查登录表单组件中的错误
+- [ ] 定位 undefined email 属性的根本原因
+- [ ] 实施修复
+- [ ] 添加验证以防止类似问题
+- [ ] 使用各种输入测试修复
+- [ ] 更新错误处理
+```
+
+## When to Use This Skill
+
+Use this Skill when:
+
+1. **Complex multi-step tasks** - Request requires 3+ distinct steps
+2. **Feature implementation** - Building new functionality from requirements
+3. **Bug fixing** - Need to investigate, fix, and verify
+4. **Refactoring** - Multiple files or components need changes
+5. **Detailed requirements** - Specifications need to be translated into concrete tasks
+6. **Need progress tracking** - Want visible progress without editing source files
+
+## When NOT to Use This Skill
+
+Skip this Skill when:
+
+1. **Single simple task** - Just one straightforward action needed
+2. **Trivial changes** - Quick fixes that don't need planning
+3. **Informational requests** - User just wants explanation, not execution
+4. **No execution requested** - User only wants brainstorming or a high-level explanation
+
+## Best Practices
+
+1. **Be specific with tasks**: "Add login button to navbar" not "Update UI"
+2. **Keep tasks atomic**: Each task should be independently completable
+3. **Update immediately**: Don't batch status updates, do them in real-time
+4. **One task at a time**: Never mark multiple tasks as `[>]`
+5. **Handle blockers**: If stuck, create new tasks to resolve the blocker
+6. **Verify completion**: Only mark `[x]` when task is fully done
+
+## Advanced Usage
+
+### Handling dependencies
+
+When tasks have dependencies, order them properly:
+
+```markdown
+- [ ] Create database schema
+- [ ] Implement API endpoints (depends on schema)
+- [ ] Build frontend forms (depends on API)
+```
+
+### Using sub-tasks
+
+For complex tasks, break them down:
+
+```markdown
+- [>] Implement authentication system
+ - [x] Set up JWT library
+ - [>] Create login endpoint
+ - [ ] Create logout endpoint
+ - [ ] Add token refresh logic
+```
+
+### Adding notes
+
+Add implementation notes or findings:
+
+```markdown
+- [x] Investigate performance issue
+ - Note: Found N+1 query in user loader
+ - Solution: Added dataloader batching
+```
+
+## Workflow Summary
+
+1. Analyze the requirements and relevant project context
+2. Call AskUserQuestion if the original requirements are unclear or ambiguous
+3. Call UpdatePlan with the structured markdown task list
+4. Refresh the remaining plan before the first task
+5. For each task:
+ - Update to `[>]` with UpdatePlan
+ - Execute the task
+ - Update to `[x]` with UpdatePlan
+ - Re-evaluate and revise remaining tasks before moving on
+6. Call UpdatePlan with all tasks completed and summarize the result
+
+This approach keeps planning and progress tracking in the UpdatePlan display, leaving source materials unchanged unless the actual task requires editing them.
From 486e649adbe93f4dd7db62a0b433bc54351d254a Mon Sep 17 00:00:00 2001
From: Ji Zhang
Date: Mon, 18 May 2026 16:29:20 +0800
Subject: [PATCH 11/90] feat: update createSession test to validate environment
JSON structure and root path
---
src/tests/session.test.ts | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/tests/session.test.ts b/src/tests/session.test.ts
index 10e3b2c..a178726 100644
--- a/src/tests/session.test.ts
+++ b/src/tests/session.test.ts
@@ -636,7 +636,10 @@ test("createSession appends default system prompts in prefix-cache-friendly orde
assert.doesNotMatch(systemContents[1] ?? "", /当前LLM模型为test-model/);
assert.match(systemContents[2] ?? "", /# Local Workspace Environment/);
assert.match(systemContents[2] ?? "", /当前LLM模型为test-model/);
- assert.match(systemContents[2] ?? "", new RegExp(escapeRegExp(`"root path": "${workspace}"`)));
+ const environmentJsonMatch = (systemContents[2] ?? "").match(/```json\n([\s\S]+?)\n```/);
+ assert.ok(environmentJsonMatch);
+ const environmentInfo = JSON.parse(environmentJsonMatch[1] ?? "{}") as { "root path"?: string };
+ assert.equal(environmentInfo["root path"], workspace);
assert.equal(systemContents[3], "root project instructions");
});
From 08ba8d34b298f045f0fd908317e543dcfa6644e6 Mon Sep 17 00:00:00 2001
From: Ji Zhang
Date: Mon, 18 May 2026 18:05:10 +0800
Subject: [PATCH 12/90] feat: add file mention functionality with scanning and
filtering capabilities
---
src/tests/fileMentions.test.ts | 157 +++++++++++++
src/ui/App.tsx | 1 +
src/ui/PromptInput.tsx | 110 ++++++++-
src/ui/fileMentions.ts | 410 +++++++++++++++++++++++++++++++++
src/ui/index.ts | 9 +
5 files changed, 682 insertions(+), 5 deletions(-)
create mode 100644 src/tests/fileMentions.test.ts
create mode 100644 src/ui/fileMentions.ts
diff --git a/src/tests/fileMentions.test.ts b/src/tests/fileMentions.test.ts
new file mode 100644
index 0000000..57f078e
--- /dev/null
+++ b/src/tests/fileMentions.test.ts
@@ -0,0 +1,157 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import * as fs from "fs";
+import * as os from "os";
+import * as path from "path";
+import {
+ filterFileMentionItems,
+ formatFileMentionPath,
+ getCurrentFileMentionToken,
+ replaceCurrentFileMentionToken,
+ scanFileMentionItems,
+ type FileMentionItem,
+} from "../ui/fileMentions";
+
+test("getCurrentFileMentionToken detects bare @file tokens under the cursor", () => {
+ assert.deepEqual(getCurrentFileMentionToken({ text: "review @src/app.ts please", cursor: 10 }), {
+ query: "src/app.ts",
+ start: 7,
+ end: 18,
+ quoted: false,
+ });
+ assert.deepEqual(getCurrentFileMentionToken({ text: "@", cursor: 1 }), {
+ query: "",
+ start: 0,
+ end: 1,
+ quoted: false,
+ });
+ assert.equal(getCurrentFileMentionToken({ text: "foo@bar", cursor: 7 }), null);
+});
+
+test("getCurrentFileMentionToken supports quoted paths with spaces", () => {
+ assert.deepEqual(getCurrentFileMentionToken({ text: 'open @"docs/my file.md"', cursor: 22 }), {
+ query: "docs/my file.md",
+ start: 5,
+ end: 23,
+ quoted: true,
+ });
+ assert.deepEqual(getCurrentFileMentionToken({ text: 'open @"docs/my', cursor: 14 }), {
+ query: "docs/my",
+ start: 5,
+ end: 14,
+ quoted: true,
+ });
+ assert.equal(getCurrentFileMentionToken({ text: 'open @"docs/my file.md" now', cursor: 24 }), null);
+});
+
+test("formatFileMentionPath quotes only paths that need it", () => {
+ assert.equal(formatFileMentionPath("src/App.tsx"), "@src/App.tsx");
+ assert.equal(formatFileMentionPath("docs/my file.md"), '@"docs/my file.md"');
+ assert.equal(formatFileMentionPath('docs/a"b.md'), '@"docs/a\\"b.md"');
+});
+
+test("replaceCurrentFileMentionToken inserts a trailing-space mention", () => {
+ const state = { text: "read @sr then", cursor: 8 };
+ const token = getCurrentFileMentionToken(state);
+ assert.ok(token);
+ assert.deepEqual(replaceCurrentFileMentionToken(state, token, "src/index.ts"), {
+ text: "read @src/index.ts then",
+ cursor: 19,
+ });
+
+ const quotedState = { text: 'read @"doc', cursor: 10 };
+ const quotedToken = getCurrentFileMentionToken(quotedState);
+ assert.ok(quotedToken);
+ assert.deepEqual(replaceCurrentFileMentionToken(quotedState, quotedToken, "docs/my file.md"), {
+ text: 'read @"docs/my file.md" ',
+ cursor: 24,
+ });
+});
+
+test("filterFileMentionItems prioritizes prefix and basename matches", () => {
+ const items: FileMentionItem[] = [
+ { path: "src/PromptInput.tsx", type: "file" },
+ { path: "docs/prompt guide.md", type: "file" },
+ { path: "templates/prompts/init.md", type: "file" },
+ ];
+
+ assert.deepEqual(
+ filterFileMentionItems(items, "prompt").map((item) => item.path),
+ ["docs/prompt guide.md", "src/PromptInput.tsx", "templates/prompts/init.md"]
+ );
+});
+
+test("scanFileMentionItems returns relative slash-separated files and directories", () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "deepcode-file-mentions-"));
+ try {
+ fs.mkdirSync(path.join(root, "src"));
+ fs.writeFileSync(path.join(root, "src", "index.ts"), "");
+ fs.mkdirSync(path.join(root, "node_modules"));
+ fs.writeFileSync(path.join(root, "node_modules", "ignored.js"), "");
+
+ assert.deepEqual(
+ scanFileMentionItems(root).map((item) => item.path),
+ ["node_modules/", "node_modules/ignored.js", "src/", "src/index.ts"]
+ );
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true });
+ }
+});
+
+test("scanFileMentionItems respects project gitignore patterns inside git repositories", () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "deepcode-file-mentions-"));
+ try {
+ fs.mkdirSync(path.join(root, ".git"));
+ fs.mkdirSync(path.join(root, ".mypy_cache"), { recursive: true });
+ fs.writeFileSync(path.join(root, ".mypy_cache", "ignored.json"), "");
+ fs.mkdirSync(path.join(root, "tmp"));
+ fs.writeFileSync(path.join(root, "tmp", "ignored.txt"), "");
+ fs.mkdirSync(path.join(root, "docs"));
+ fs.writeFileSync(path.join(root, "docs", "guide.md"), "");
+ fs.writeFileSync(path.join(root, ".gitignore"), ".mypy_cache/\ntmp/\n");
+
+ assert.deepEqual(
+ scanFileMentionItems(root).map((item) => item.path),
+ ["docs/", "docs/guide.md", ".gitignore"]
+ );
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true });
+ }
+});
+
+test("scanFileMentionItems ignores gitignore files outside git repositories", () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "deepcode-file-mentions-"));
+ try {
+ fs.mkdirSync(path.join(root, "tmp"));
+ fs.writeFileSync(path.join(root, "tmp", "visible.txt"), "");
+ fs.writeFileSync(path.join(root, ".gitignore"), "tmp/\n");
+
+ assert.deepEqual(
+ scanFileMentionItems(root).map((item) => item.path),
+ ["tmp/", "tmp/visible.txt", ".gitignore"]
+ );
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true });
+ }
+});
+
+test("scanFileMentionItems applies parent and nested ignore files", () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "deepcode-file-mentions-"));
+ try {
+ fs.mkdirSync(path.join(root, ".git"));
+ fs.writeFileSync(path.join(root, ".gitignore"), "ignored-from-parent/\n");
+ fs.mkdirSync(path.join(root, "sub", "ignored-from-parent"), { recursive: true });
+ fs.writeFileSync(path.join(root, "sub", "ignored-from-parent", "hidden.txt"), "");
+ fs.mkdirSync(path.join(root, "sub", "nested", "ignored-from-nested"), { recursive: true });
+ fs.writeFileSync(path.join(root, "sub", "nested", ".gitignore"), "ignored-from-nested/\n");
+ fs.writeFileSync(path.join(root, "sub", "nested", "ignored-from-nested", "hidden.txt"), "");
+ fs.writeFileSync(path.join(root, "sub", "nested", "visible.txt"), "");
+
+ assert.deepEqual(
+ scanFileMentionItems(path.join(root, "sub")).map((item) => item.path),
+ ["nested/", "nested/.gitignore", "nested/visible.txt"]
+ );
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true });
+ }
+});
diff --git a/src/ui/App.tsx b/src/ui/App.tsx
index e56111f..8d8dca1 100644
--- a/src/ui/App.tsx
+++ b/src/ui/App.tsx
@@ -512,6 +512,7 @@ export function App({ projectRoot, version = "", initialPrompt, onRestart }: App
/>
) : isExiting ? null : (
(null);
const [modelDropdownIndex, setModelDropdownIndex] = useState(0);
const [pendingModel, setPendingModel] = useState(null);
+ const [fileMentionIndex, setFileMentionIndex] = useState(0);
+ const [dismissedFileMentionKey, setDismissedFileMentionKey] = useState(null);
const [historyCursor, setHistoryCursor] = useState(-1);
const [draftBeforeHistory, setDraftBeforeHistory] = useState(null);
const [hasTerminalFocus, setHasTerminalFocus] = useState(true);
const lastCtrlDAt = React.useRef(0);
const undoRedoRef = React.useRef(createPromptUndoRedoState());
+ const fileMentionItems = React.useMemo(() => scanFileMentionItems(projectRoot), [projectRoot]);
+ const fileMentionToken = getCurrentFileMentionToken(buffer);
+ const fileMentionKey = fileMentionToken ? `${fileMentionToken.start}:${fileMentionToken.query}` : null;
+ const fileMentionMatches = React.useMemo(
+ () => (fileMentionToken ? filterFileMentionItems(fileMentionItems, fileMentionToken.query) : []),
+ [fileMentionItems, fileMentionToken]
+ );
+ const showFileMentionMenu =
+ !showSkillsDropdown &&
+ !modelDropdownStep &&
+ fileMentionToken !== null &&
+ fileMentionKey !== dismissedFileMentionKey;
const slashItems = React.useMemo(() => buildSlashCommands(skills), [skills]);
const slashToken = getCurrentSlashToken(buffer);
const slashMenu = React.useMemo(
() =>
- showSkillsDropdown || modelDropdownStep ? [] : slashToken ? filterSlashCommands(slashItems, slashToken) : [],
- [showSkillsDropdown, modelDropdownStep, slashToken, slashItems]
+ showSkillsDropdown || modelDropdownStep || showFileMentionMenu
+ ? []
+ : slashToken
+ ? filterSlashCommands(slashItems, slashToken)
+ : [],
+ [showSkillsDropdown, modelDropdownStep, showFileMentionMenu, slashToken, slashItems]
);
const showMenu = slashMenu.length > 0;
const promptHistoryKey = React.useMemo(() => promptHistory.join("\0"), [promptHistory]);
@@ -153,7 +180,7 @@ export const PromptInput = React.memo(function PromptInput({
? loadingText && loadingText.trim()
? `${loadingText}${processHint}`
: `esc to interrupt · ctrl+c to cancel input${processHint}`
- : `enter send · shift+enter newline · ctrl+v image · / commands · ctrl+d exit${processHint}`;
+ : `enter send · shift+enter newline · @ files · ctrl+v image · / commands · ctrl+d exit${processHint}`;
useTerminalFocusReporting(stdout, !disabled);
useTerminalExtendedKeys(stdout, !disabled);
useHiddenTerminalCursor(stdout, !disabled);
@@ -168,6 +195,22 @@ export const PromptInput = React.memo(function PromptInput({
}
}, [slashMenu, showMenu, menuIndex]);
+ useEffect(() => {
+ if (!fileMentionKey) {
+ setDismissedFileMentionKey(null);
+ }
+ }, [fileMentionKey]);
+
+ useEffect(() => {
+ if (!showFileMentionMenu) {
+ setFileMentionIndex(0);
+ return;
+ }
+ if (fileMentionIndex >= fileMentionMatches.length) {
+ setFileMentionIndex(Math.max(0, fileMentionMatches.length - 1));
+ }
+ }, [fileMentionMatches.length, fileMentionIndex, showFileMentionMenu]);
+
useEffect(() => {
if (skillsDropdownIndex >= skills.length) {
setSkillsDropdownIndex(Math.max(0, skills.length - 1));
@@ -222,6 +265,10 @@ export const PromptInput = React.memo(function PromptInput({
setShowSkillsDropdown(false);
return;
}
+ if (showFileMentionMenu && fileMentionKey) {
+ setDismissedFileMentionKey(fileMentionKey);
+ return;
+ }
if (busy) {
onInterrupt();
setStatusMessage("Interrupting…");
@@ -353,6 +400,35 @@ export const PromptInput = React.memo(function PromptInput({
const returnAction = getPromptReturnKeyAction(key);
const isPlainReturn = returnAction === "submit";
+ if (showFileMentionMenu) {
+ if (key.upArrow) {
+ if (fileMentionMatches.length > 0) {
+ setFileMentionIndex((idx) => (idx - 1 + fileMentionMatches.length) % fileMentionMatches.length);
+ }
+ return;
+ }
+ if (key.downArrow) {
+ if (fileMentionMatches.length > 0) {
+ setFileMentionIndex((idx) => (idx + 1) % fileMentionMatches.length);
+ }
+ return;
+ }
+ if (key.tab || returnAction === "submit") {
+ const selected = fileMentionMatches[fileMentionIndex];
+ if (selected && fileMentionToken) {
+ insertFileMentionSelection(selected);
+ return;
+ }
+ if (key.tab) {
+ setDismissedFileMentionKey(fileMentionKey);
+ return;
+ }
+ if (fileMentionKey) {
+ setDismissedFileMentionKey(fileMentionKey);
+ }
+ }
+ }
+
if (showMenu) {
if (key.upArrow) {
setMenuIndex((idx) => (idx - 1 + slashMenu.length) % slashMenu.length);
@@ -585,6 +661,14 @@ export const PromptInput = React.memo(function PromptInput({
setHistoryCursor(nextCursor);
}
+ function insertFileMentionSelection(item: FileMentionItem): void {
+ if (!fileMentionToken) {
+ return;
+ }
+ updateBuffer((state) => replaceCurrentFileMentionToken(state, fileMentionToken, item.path));
+ setDismissedFileMentionKey(null);
+ }
+
function handleSlashSelection(item: SlashCommandItem): void {
if (busy && item.kind !== "exit") {
setStatusMessage("wait for the current response or press esc to interrupt");
@@ -760,8 +844,8 @@ export const PromptInput = React.memo(function PromptInput({
}));
const showFooterText = useMemo(
- () => showMenu || showSkillsDropdown || modelDropdownStep !== null,
- [showMenu, showSkillsDropdown, modelDropdownStep]
+ () => showMenu || showSkillsDropdown || modelDropdownStep !== null || showFileMentionMenu,
+ [showMenu, showSkillsDropdown, modelDropdownStep, showFileMentionMenu]
);
return (
@@ -830,6 +914,22 @@ export const PromptInput = React.memo(function PromptInput({
maxVisible={6}
/>
) : null}
+ {showFileMentionMenu ? (
+ ({
+ key: item.path,
+ label: item.path,
+ description: item.type === "directory" ? "directory" : "file",
+ }))}
+ activeIndex={fileMentionIndex}
+ activeColor="#229ac3"
+ maxVisible={8}
+ />
+ ) : null}
{!showFooterText && (
diff --git a/src/ui/fileMentions.ts b/src/ui/fileMentions.ts
new file mode 100644
index 0000000..cbacbe6
--- /dev/null
+++ b/src/ui/fileMentions.ts
@@ -0,0 +1,410 @@
+import * as fs from "fs";
+import * as path from "path";
+import ignore from "ignore";
+import type { PromptBufferState } from "./promptBuffer";
+
+export type FileMentionItem = {
+ path: string;
+ type: "file" | "directory";
+};
+
+export type FileMentionToken = {
+ query: string;
+ start: number;
+ end: number;
+ quoted: boolean;
+};
+
+const DEFAULT_MAX_ITEMS = 2000;
+const DEFAULT_MAX_DEPTH = 8;
+
+type IgnoreMatcher = {
+ base: string;
+ matcher: ignore.Ignore;
+};
+
+export function scanFileMentionItems(root: string, maxItems = DEFAULT_MAX_ITEMS): FileMentionItem[] {
+ const items: FileMentionItem[] = [];
+ const seen = new Set();
+ const gitRoot = findGitRoot(root);
+ const visitedDirectories = new Set();
+
+ function addItem(item: FileMentionItem): void {
+ if (items.length >= maxItems || seen.has(item.path)) {
+ return;
+ }
+ seen.add(item.path);
+ items.push(item);
+ }
+
+ function visit(directory: string, depth: number, matchers: IgnoreMatcher[]): void {
+ if (items.length >= maxItems || depth > DEFAULT_MAX_DEPTH) {
+ return;
+ }
+
+ const currentMatchers = [...matchers, ...loadDirectoryIgnoreMatchers(directory, gitRoot)];
+
+ let entries: fs.Dirent[];
+ try {
+ entries = fs.readdirSync(directory, { withFileTypes: true });
+ } catch {
+ return;
+ }
+
+ entries.sort((a, b) => {
+ if (a.isDirectory() !== b.isDirectory()) {
+ return a.isDirectory() ? -1 : 1;
+ }
+ return a.name.localeCompare(b.name);
+ });
+
+ for (const entry of entries) {
+ if (items.length >= maxItems) {
+ return;
+ }
+ if (entry.name === "." || entry.name === ".." || entry.name === ".git") {
+ continue;
+ }
+
+ const absolute = path.join(directory, entry.name);
+ const relative = toMentionPath(path.relative(root, absolute));
+ if (!relative) {
+ continue;
+ }
+
+ const entryType = getMentionEntryType(entry, absolute);
+ if (!entryType) {
+ continue;
+ }
+
+ if (matchesAnyIgnore(absolute, entryType === "directory", currentMatchers)) {
+ continue;
+ }
+
+ if (entryType === "directory") {
+ const realPath = safeRealpath(absolute);
+ if (realPath) {
+ if (visitedDirectories.has(realPath)) {
+ continue;
+ }
+ visitedDirectories.add(realPath);
+ }
+ addItem({ path: `${relative}/`, type: "directory" });
+ visit(absolute, depth + 1, currentMatchers);
+ continue;
+ }
+
+ if (entryType === "file") {
+ addItem({ path: relative, type: "file" });
+ }
+ }
+ }
+
+ const rootRealPath = safeRealpath(root);
+ if (rootRealPath) {
+ visitedDirectories.add(rootRealPath);
+ }
+ visit(root, 0, loadAncestorIgnoreMatchers(root, gitRoot));
+ return items;
+}
+
+function getMentionEntryType(entry: fs.Dirent, absolute: string): FileMentionItem["type"] | null {
+ if (entry.isDirectory()) {
+ return "directory";
+ }
+ if (entry.isFile()) {
+ return "file";
+ }
+ if (!entry.isSymbolicLink()) {
+ return null;
+ }
+ try {
+ const stat = fs.statSync(absolute);
+ if (stat.isDirectory()) {
+ return "directory";
+ }
+ if (stat.isFile()) {
+ return "file";
+ }
+ } catch {
+ return null;
+ }
+ return null;
+}
+
+function safeRealpath(absolute: string): string | null {
+ try {
+ return fs.realpathSync(absolute);
+ } catch {
+ return null;
+ }
+}
+
+function loadDirectoryIgnoreMatchers(directory: string, gitRoot: string | null): IgnoreMatcher[] {
+ const matchers: IgnoreMatcher[] = [];
+ if (gitRoot && isPathInsideOrEqual(directory, gitRoot)) {
+ const gitignoreMatcher = loadIgnoreFileMatcher(directory, path.join(directory, ".gitignore"));
+ if (gitignoreMatcher) {
+ matchers.push(gitignoreMatcher);
+ }
+ if (path.resolve(directory) === path.resolve(gitRoot)) {
+ const gitExcludeMatcher = loadIgnoreFileMatcher(directory, path.join(directory, ".git", "info", "exclude"));
+ if (gitExcludeMatcher) {
+ matchers.push(gitExcludeMatcher);
+ }
+ }
+ }
+
+ const ignoreMatcher = loadIgnoreFileMatcher(directory, path.join(directory, ".ignore"));
+ if (ignoreMatcher) {
+ matchers.push(ignoreMatcher);
+ }
+ return matchers;
+}
+
+function loadAncestorIgnoreMatchers(root: string, gitRoot: string | null): IgnoreMatcher[] {
+ const resolvedRoot = path.resolve(root);
+ const ancestors: string[] = [];
+ let current = path.dirname(resolvedRoot);
+ while (gitRoot && isPathInsideOrEqual(current, gitRoot)) {
+ ancestors.push(current);
+ if (path.resolve(current) === path.resolve(gitRoot)) {
+ break;
+ }
+ current = path.dirname(current);
+ }
+ return ancestors.reverse().flatMap((directory) => loadDirectoryIgnoreMatchers(directory, gitRoot));
+}
+
+function loadIgnoreFileMatcher(base: string, ignoreFilePath: string): IgnoreMatcher | null {
+ try {
+ if (!fs.existsSync(ignoreFilePath)) {
+ return null;
+ }
+ const content = fs.readFileSync(ignoreFilePath, "utf8");
+ if (!content.trim()) {
+ return null;
+ }
+ return { base, matcher: ignore().add(content) };
+ } catch {
+ return null;
+ }
+}
+
+function matchesAnyIgnore(absolute: string, isDir: boolean, matchers: IgnoreMatcher[]): boolean {
+ let ignored = false;
+ for (const { base, matcher } of matchers) {
+ const relative = toMentionPath(path.relative(base, absolute));
+ if (!relative || relative.startsWith("../")) {
+ continue;
+ }
+ const result = matcher.test(isDir ? `${relative}/` : relative);
+ if (result.ignored) {
+ ignored = true;
+ }
+ if (result.unignored) {
+ ignored = false;
+ }
+ }
+ return ignored;
+}
+
+function findGitRoot(start: string): string | null {
+ let current = path.resolve(start);
+ while (true) {
+ if (fs.existsSync(path.join(current, ".git"))) {
+ return current;
+ }
+ const parent = path.dirname(current);
+ if (parent === current) {
+ return null;
+ }
+ current = parent;
+ }
+}
+
+function isPathInsideOrEqual(candidate: string, parent: string): boolean {
+ const relative = path.relative(parent, candidate);
+ return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
+}
+
+export function filterFileMentionItems(items: FileMentionItem[], query: string, maxResults = 12): FileMentionItem[] {
+ const normalizedQuery = normalizeForSearch(query);
+ const scored = items
+ .map((item, index) => ({ item, index, score: scoreFileMention(item.path, normalizedQuery) }))
+ .filter((entry) => entry.score !== Number.POSITIVE_INFINITY)
+ .sort((a, b) => a.score - b.score || a.item.path.length - b.item.path.length || a.index - b.index);
+
+ return scored.slice(0, maxResults).map((entry) => entry.item);
+}
+
+export function getCurrentFileMentionToken(state: PromptBufferState): FileMentionToken | null {
+ const text = state.text;
+ const cursor = clampCursorToBoundary(text, state.cursor);
+ const quoted = getCurrentQuotedFileMentionToken(text, cursor);
+ if (quoted) {
+ return quoted;
+ }
+ return getCurrentBareFileMentionToken(text, cursor);
+}
+
+export function replaceCurrentFileMentionToken(
+ state: PromptBufferState,
+ token: FileMentionToken,
+ selectedPath: string
+): PromptBufferState {
+ const inserted = `${formatFileMentionPath(selectedPath)} `;
+ const end = token.end < state.text.length && isWhitespace(state.text[token.end] ?? "") ? token.end + 1 : token.end;
+ const text = `${state.text.slice(0, token.start)}${inserted}${state.text.slice(end)}`;
+ return { text, cursor: token.start + inserted.length };
+}
+
+export function formatFileMentionPath(filePath: string): string {
+ if (!/[\s"]/.test(filePath)) {
+ return `@${filePath}`;
+ }
+ return `@"${filePath.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
+}
+
+function getCurrentBareFileMentionToken(text: string, cursor: number): FileMentionToken | null {
+ const beforeCursor = text.slice(0, cursor);
+ const afterCursor = text.slice(cursor);
+ const start = findTokenStart(beforeCursor);
+ const end = cursor + findTokenEnd(afterCursor);
+ const token = text.slice(start, end);
+
+ if (!token.startsWith("@") || token.startsWith('@"')) {
+ return null;
+ }
+ if (start > 0 && !isWhitespace(text[start - 1] ?? "")) {
+ return null;
+ }
+ return { query: token.slice(1), start, end, quoted: false };
+}
+
+function getCurrentQuotedFileMentionToken(text: string, cursor: number): FileMentionToken | null {
+ for (let index = cursor; index >= 0; index--) {
+ if (text[index] !== "@" || text[index + 1] !== '"') {
+ continue;
+ }
+ if (index > 0 && !isWhitespace(text[index - 1] ?? "")) {
+ continue;
+ }
+
+ const closeQuote = findClosingQuote(text, index + 2);
+ if (closeQuote !== -1 && cursor > closeQuote) {
+ continue;
+ }
+
+ const end = closeQuote === -1 ? cursor : closeQuote + 1;
+ return {
+ query: unescapeQuotedMentionQuery(
+ text.slice(index + 2, Math.min(cursor, closeQuote === -1 ? cursor : closeQuote))
+ ),
+ start: index,
+ end,
+ quoted: true,
+ };
+ }
+ return null;
+}
+
+function findTokenStart(beforeCursor: string): number {
+ const whitespaceIndex = findLastWhitespaceIndex(beforeCursor);
+ return whitespaceIndex === -1 ? 0 : whitespaceIndex + 1;
+}
+
+function findTokenEnd(afterCursor: string): number {
+ const whitespaceIndex = afterCursor.search(/\s/);
+ return whitespaceIndex === -1 ? afterCursor.length : whitespaceIndex;
+}
+
+function findLastWhitespaceIndex(value: string): number {
+ for (let index = value.length - 1; index >= 0; index--) {
+ if (isWhitespace(value[index] ?? "")) {
+ return index;
+ }
+ }
+ return -1;
+}
+
+function findClosingQuote(text: string, start: number): number {
+ let escaped = false;
+ for (let index = start; index < text.length; index++) {
+ const char = text[index];
+ if (escaped) {
+ escaped = false;
+ continue;
+ }
+ if (char === "\\") {
+ escaped = true;
+ continue;
+ }
+ if (char === '"') {
+ return index;
+ }
+ }
+ return -1;
+}
+
+function unescapeQuotedMentionQuery(query: string): string {
+ return query.replace(/\\(["\\])/g, "$1");
+}
+
+function clampCursorToBoundary(text: string, cursor: number): number {
+ return Math.max(0, Math.min(cursor, text.length));
+}
+
+function scoreFileMention(itemPath: string, normalizedQuery: string): number {
+ if (!normalizedQuery) {
+ return itemPath.endsWith("/") ? 5 : 10;
+ }
+
+ const normalizedPath = normalizeForSearch(itemPath);
+ const normalizedBase = normalizeForSearch(path.posix.basename(itemPath.replace(/\/$/, "")));
+ if (normalizedPath === normalizedQuery) {
+ return 0;
+ }
+ if (normalizedPath.startsWith(normalizedQuery)) {
+ return 1;
+ }
+ if (normalizedBase.startsWith(normalizedQuery)) {
+ return isQueryBoundary(normalizedBase[normalizedQuery.length] ?? "") ? 2 : 3;
+ }
+ const pathIndex = normalizedPath.indexOf(normalizedQuery);
+ if (pathIndex !== -1) {
+ return 20 + pathIndex;
+ }
+ const fuzzyScore = fuzzyMatchScore(normalizedPath, normalizedQuery);
+ return fuzzyScore === null ? Number.POSITIVE_INFINITY : 100 + fuzzyScore;
+}
+
+function fuzzyMatchScore(value: string, query: string): number | null {
+ let valueIndex = 0;
+ let score = 0;
+ for (const char of query) {
+ const nextIndex = value.indexOf(char, valueIndex);
+ if (nextIndex === -1) {
+ return null;
+ }
+ score += nextIndex - valueIndex;
+ valueIndex = nextIndex + 1;
+ }
+ return score;
+}
+
+function normalizeForSearch(value: string): string {
+ return value.trim().toLocaleLowerCase();
+}
+
+function isQueryBoundary(value: string): boolean {
+ return value === "" || /[\s._/-]/.test(value);
+}
+
+function toMentionPath(value: string): string {
+ return value.split(path.sep).join("/");
+}
+
+function isWhitespace(value: string): boolean {
+ return /\s/.test(value);
+}
diff --git a/src/ui/index.ts b/src/ui/index.ts
index 5b4ff8f..5bcde40 100644
--- a/src/ui/index.ts
+++ b/src/ui/index.ts
@@ -79,5 +79,14 @@ export {
type SlashCommandKind,
type SlashCommandItem,
} from "./slashCommands";
+export {
+ filterFileMentionItems,
+ formatFileMentionPath,
+ getCurrentFileMentionToken,
+ replaceCurrentFileMentionToken,
+ scanFileMentionItems,
+ type FileMentionItem,
+ type FileMentionToken,
+} from "./fileMentions";
export { findExpandedThinkingId } from "./thinkingState";
export { buildExitSummaryText } from "./exitSummary";
From 47d3c21abe3c3582d24e7c1109bdf19e0818c90d Mon Sep 17 00:00:00 2001
From: hcyang
Date: Mon, 18 May 2026 18:13:34 +0800
Subject: [PATCH 13/90] =?UTF-8?q?feat(ui):=20=E6=B7=BB=E5=8A=A0=20/raw=20?=
=?UTF-8?q?=E6=A8=A1=E5=BC=8F=E6=94=AF=E6=8C=81=E5=8F=8A=E7=9B=B8=E5=85=B3?=
=?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=92=8C=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 新增 RawMode 功能,包括 Normal、Lite 和 Raw scrollback 模式
- App 组件中集成 RawMode 上下文及切换逻辑,支持在 Raw 模式下直接向 stdout 渲染消息
- 增加 RawModeExitPrompt 组件,支持按 ESC 退出原始模式
- 新增 RawModelDropdown 组件,提供原始模式选择下拉菜单
- 在 PromptInput 中集成原始模式选择交互及状态管理
- 调整消息视图实现,拆分 MessageView 到 compoments 目录,支持根据 RawMode 呈现不同内容
- 新建 AppContainer 组件,包装 App 并提供版本上下文和 RawModeProvider
- 修改 SlashCommand 体系,支持内置 /raw 命令及对应测试覆盖
- 更新 cli 入口,使用 AppContainer 替换直接渲染 App,传递版本信息
- 移除旧 MessageView 文件,重构消息渲染逻辑
- 优化 SlashCommandMenu 显示,支持命令参数提示显示
- 更新相关测试,支持原始模式功能验证
---
src/cli.tsx | 4 +-
src/tests/messageView.test.ts | 51 +--
src/tests/slashCommands.test.ts | 9 +-
src/ui/App.tsx | 69 +++-
src/ui/AppContainer.tsx | 21 ++
src/ui/MessageView.tsx | 355 ------------------
src/ui/PromptInput.tsx | 27 +-
src/ui/SlashCommandMenu.tsx | 5 +-
src/ui/WelcomeScreen.tsx | 11 +-
src/ui/compoments/MessageView/index.tsx | 183 +++++++++
.../{ => compoments/MessageView}/markdown.ts | 0
src/ui/compoments/MessageView/types.ts | 19 +
src/ui/compoments/MessageView/utils.ts | 255 +++++++++++++
src/ui/compoments/RawModeExitPrompt/index.tsx | 15 +
src/ui/compoments/RawModelDropdown/index.tsx | 55 +++
src/ui/compoments/index.ts | 3 +
src/ui/contexts/AppContext.tsx | 15 +
src/ui/contexts/RawModeContext.tsx | 40 ++
src/ui/contexts/index.ts | 3 +
src/ui/index.ts | 5 +-
src/ui/slashCommands.ts | 22 +-
21 files changed, 750 insertions(+), 417 deletions(-)
create mode 100644 src/ui/AppContainer.tsx
delete mode 100644 src/ui/MessageView.tsx
create mode 100644 src/ui/compoments/MessageView/index.tsx
rename src/ui/{ => compoments/MessageView}/markdown.ts (100%)
create mode 100644 src/ui/compoments/MessageView/types.ts
create mode 100644 src/ui/compoments/MessageView/utils.ts
create mode 100644 src/ui/compoments/RawModeExitPrompt/index.tsx
create mode 100644 src/ui/compoments/RawModelDropdown/index.tsx
create mode 100644 src/ui/compoments/index.ts
create mode 100644 src/ui/contexts/AppContext.tsx
create mode 100644 src/ui/contexts/RawModeContext.tsx
create mode 100644 src/ui/contexts/index.ts
diff --git a/src/cli.tsx b/src/cli.tsx
index 435499a..e8e8659 100644
--- a/src/cli.tsx
+++ b/src/cli.tsx
@@ -1,8 +1,8 @@
import React from "react";
import { render } from "ink";
-import { App } from "./ui";
import { setShellIfWindows } from "./common/shell-utils";
import { checkForNpmUpdate, promptForPendingUpdate, type PackageInfo } from "./updateCheck";
+import AppContainer from "./ui/AppContainer";
const args = process.argv.slice(2);
const packageInfo = readPackageInfo();
@@ -81,7 +81,7 @@ async function main(): Promise {
const appInitialPrompt = initialPrompt;
initialPrompt = undefined;
const inkInstance = render(
- {
const lines = parseDiffPreview(
@@ -25,45 +26,29 @@ test("parseDiffPreview keeps nonstandard context lines", () => {
test("MessageView summarizes thinking content across lines", () => {
assert.equal(
- getThinkingParams({
- content: "Plan:\n\nInspect the code and update tests",
- }),
+ buildThinkingSummary("Plan:\n\nInspect the code and update tests", null, RawMode.Lite),
"Plan: Inspect the code and update tests"
);
});
-test("MessageView removes a trailing colon from thinking summaries", () => {
- assert.equal(getThinkingParams({ content: "Planning:" }), "Planning");
+test("MessageView removes a trailing colon from thinking summary", () => {
+ assert.equal(buildThinkingSummary("Planning:", null, RawMode.Lite), "Planning");
});
-test("MessageView falls back to a reasoning placeholder for hidden reasoning content", () => {
+test("MessageView falls back to a reasoning placeholder for hidden reasoning content in Lite mode", () => {
assert.equal(
- getThinkingParams({
- content: "",
- messageParams: { reasoning_content: "hidden chain of thought" },
- }),
+ buildThinkingSummary("", { reasoning_content: "hidden chain of thought" }, RawMode.Lite),
"(reasoning...)"
);
});
-function getThinkingParams(overrides: Partial): string {
- const view = MessageView({ message: buildAssistantMessage(overrides) }) as any;
- return view.props.children.props.params;
-}
-
-function buildAssistantMessage(overrides: Partial): SessionMessage {
- return {
- id: "message-1",
- sessionId: "session-1",
- role: "assistant",
- content: "",
- contentParams: null,
- messageParams: null,
- compacted: false,
- visible: true,
- createTime: "2026-01-01T00:00:00.000Z",
- updateTime: "2026-01-01T00:00:00.000Z",
- meta: { asThinking: true },
- ...overrides,
- };
-}
+test("MessageView shows full reasoning content in Normal/Raw mode", () => {
+ assert.equal(
+ buildThinkingSummary("", { reasoning_content: "hidden chain of thought" }, RawMode.None),
+ "hidden chain of thought"
+ );
+ assert.equal(
+ buildThinkingSummary("", { reasoning_content: "hidden chain of thought" }, RawMode.Raw),
+ "hidden chain of thought"
+ );
+});
diff --git a/src/tests/slashCommands.test.ts b/src/tests/slashCommands.test.ts
index bba5244..34b48d0 100644
--- a/src/tests/slashCommands.test.ts
+++ b/src/tests/slashCommands.test.ts
@@ -19,7 +19,7 @@ test("buildSlashCommands prefixes skills before built-ins", () => {
assert.equal(items[0].kind, "skill");
assert.equal(items[0].name, "skill-writer");
const builtinNames = items.filter((i) => i.kind !== "skill").map((i) => i.name);
- assert.deepEqual(builtinNames, ["skills", "model", "new", "init", "resume", "continue", "mcp", "exit"]);
+ assert.deepEqual(builtinNames, ["skills", "model", "new", "init", "resume", "continue", "mcp", "raw", "exit"]);
});
test("filterSlashCommands matches partial prefixes", () => {
@@ -80,6 +80,13 @@ test("findExactSlashCommand returns built-in /model", () => {
assert.equal(item?.kind, "model");
});
+test("findExactSlashCommand returns built-in /raw", () => {
+ const items = buildSlashCommands(skills);
+ const item = findExactSlashCommand(items, "/raw");
+ assert.ok(item);
+ assert.equal(item?.kind, "raw");
+});
+
test("findExactSlashCommand returns the matching skill", () => {
const items = buildSlashCommands(skills);
const item = findExactSlashCommand(items, "/code-review");
diff --git a/src/ui/App.tsx b/src/ui/App.tsx
index e56111f..1c9bac4 100644
--- a/src/ui/App.tsx
+++ b/src/ui/App.tsx
@@ -6,10 +6,10 @@ import * as os from "os";
import * as path from "path";
import OpenAI from "openai";
import {
- SessionManager,
type LlmStreamProgress,
type MessageMeta,
type SessionEntry,
+ SessionManager,
type SessionMessage,
type SessionStatus,
type SkillInfo,
@@ -17,13 +17,13 @@ import {
} from "../session";
import {
applyModelConfigSelection,
- resolveSettingsSources,
type DeepcodingSettings,
type ModelConfigSelection,
type ResolvedDeepcodingSettings,
+ resolveSettingsSources,
} from "../settings";
import { PromptInput, type PromptSubmission } from "./PromptInput";
-import { MessageView } from "./MessageView";
+import { MessageView, RawModeExitPrompt } from "./compoments";
import { SessionList } from "./SessionList";
import { buildLoadingText } from "./loadingText";
import { findExpandedThinkingId } from "./thinkingState";
@@ -32,11 +32,13 @@ import { AskUserQuestionPrompt } from "./AskUserQuestionPrompt";
import { McpStatusList } from "./McpStatusList";
import { ProcessStdoutView } from "./ProcessStdoutView";
import {
+ type AskUserQuestionAnswers,
findPendingAskUserQuestion,
formatAskUserQuestionAnswers,
- type AskUserQuestionAnswers,
} from "./askUserQuestion";
import { buildExitSummaryText } from "./exitSummary";
+import { RawMode, useRawModeContext } from "./contexts";
+import { renderMessageToStdout } from "./compoments/MessageView/utils";
const DEFAULT_MODEL = "deepseek-v4-pro";
const DEFAULT_BASE_URL = "https://api.deepseek.com";
@@ -45,12 +47,11 @@ type View = "chat" | "session-list" | "mcp-status";
type AppProps = {
projectRoot: string;
- version?: string;
initialPrompt?: string;
onRestart?: () => void;
};
-export function App({ projectRoot, version = "", initialPrompt, onRestart }: AppProps): React.ReactElement {
+export function App({ projectRoot, initialPrompt, onRestart }: AppProps): React.ReactElement {
const { exit } = useApp();
const { stdout, write } = useStdout();
const { columns } = useWindowSize();
@@ -75,6 +76,10 @@ export function App({ projectRoot, version = "", initialPrompt, onRestart }: App
const [showProcessStdout, setShowProcessStdout] = useState(false);
const processStdoutRef = useRef
Deep Code CLI
-[English](./README_en.md) · 中文
+[English](README-en.md) · 中文
From 3fef0fc5137af49f218237fa0e919159fb231122 Mon Sep 17 00:00:00 2001
From: lellansin
Date: Tue, 19 May 2026 10:09:38 +0800
Subject: [PATCH 27/90] feat(notify): pass STATUS, FAIL_REASON, BODY as env
vars to notify hook
- Add NotifyContext type with status, failReason, body fields
- buildNotifyEnv injects STATUS, FAIL_REASON, BODY when provided
- maybeNotifyTaskCompletion extracts last assistant message as BODY
- launchNotifyScript accepts optional context parameter
- Add unit tests for new context env var injection
- Update docs with env variable table and iTerm2/macOS notify examples
---
docs/configuration.md | 34 ++++++
docs/configuration_en.md | 34 ++++++
src/common/notify.ts | 38 ++++++-
src/session.ts | 18 +++-
src/tests/session.test.ts | 144 ++++++++++++++++++++++++++
src/tests/settings-and-notify.test.ts | 65 +++++++++++-
6 files changed, 324 insertions(+), 9 deletions(-)
diff --git a/docs/configuration.md b/docs/configuration.md
index f8e52c3..45aaab0 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -67,12 +67,46 @@ Deep Code 使用 `settings.json` 设置文件进行持久化配置,支持两
设置一个 Shell 脚本的完整路径。当 AI 助手完成一轮任务后,会自动执行该脚本,可用于发送通知(如 Slack 消息)。
+通知脚本执行时,会通过环境变量注入以下上下文信息:
+
+| 环境变量 | 说明 |
+|----------|------|
+| `DURATION` | 会话耗时,单位秒(整数) |
+| `STATUS` | 会话状态:`"completed"` 或 `"failed"` |
+| `FAIL_REASON` | 失败原因(仅失败时设置) |
+| `BODY` | 最后一条 AI 助手回复的文本内容 |
+| `TITLE` | 会话标题(对应 resume 列表中的标题) |
+
```json
{
"notify": "/path/to/slack-notify.sh"
}
```
+**iTerm2 终端通知示例**:
+
+如果你的终端是 iTerm2,可以直接通过 OSC 9 转义序列弹出通知,无需额外脚本。创建以下脚本(如 `~/.deepcode/notify.sh`):
+
+```bash
+#!/bin/bash
+# iTerm2 OSC 9 通知
+echo -e "\x1b]9;DeepCode: task ${STATUS:-completed} (${DURATION}s)\x07"
+```
+
+```json
+{
+ "notify": "/Users/you/.deepcode/notify.sh"
+}
+```
+
+**macOS 系统通知示例**:
+
+```bash
+#!/bin/bash
+# macOS 系统通知
+osascript -e "display notification \"任务已${STATUS:-完成},耗时 ${DURATION}s\" with title \"DeepCode\""
+```
+
#### `webSearchTool` — 自定义联网搜索
Deep Code 内置免费可用的 Web Search 工具。如果需要自定义搜索逻辑,可将 `webSearchTool` 设为一个可执行脚本的完整路径:
diff --git a/docs/configuration_en.md b/docs/configuration_en.md
index 369f8e4..606fcab 100644
--- a/docs/configuration_en.md
+++ b/docs/configuration_en.md
@@ -67,12 +67,46 @@ When thinking mode is enabled, controls the depth of the model’s reasoning:
Set a full path to a shell script. When the AI assistant finishes a round of tasks, the script is executed automatically, which can be used to send notifications (e.g., a Slack message).
+The following context is injected as environment variables when the notify script runs:
+
+| Variable | Description |
+|----------|-------------|
+| `DURATION` | Session duration in seconds (integer) |
+| `STATUS` | Session status: `"completed"` or `"failed"` |
+| `FAIL_REASON` | Failure reason (only set on failure) |
+| `BODY` | The text content of the last AI assistant reply |
+| `TITLE` | Session title (matches the resume list title) |
+
```json
{
"notify": "/path/to/slack-notify.sh"
}
```
+**iTerm2 Notification Example**:
+
+On iTerm2 you can use the OSC 9 escape sequence for native notifications. Create a script (e.g., `~/.deepcode/notify.sh`):
+
+```bash
+#!/bin/bash
+# iTerm2 OSC 9 notification
+echo -e "\x1b]9;DeepCode: task ${STATUS:-completed} (${DURATION}s)\x07"
+```
+
+```json
+{
+ "notify": "/Users/you/.deepcode/notify.sh"
+}
+```
+
+**macOS System Notification Example**:
+
+```bash
+#!/bin/bash
+# macOS system notification
+osascript -e "display notification \"Task ${STATUS:-completed}, took ${DURATION}s\" with title \"DeepCode\""
+```
+
#### `webSearchTool` — Custom Web Search
Deep Code has a built-in, free-to-use Web Search tool. If you need custom search logic, set `webSearchTool` to the full path of an executable script:
diff --git a/src/common/notify.ts b/src/common/notify.ts
index 8878c50..d1b541b 100644
--- a/src/common/notify.ts
+++ b/src/common/notify.ts
@@ -16,11 +16,40 @@ export function formatDurationSeconds(durationMs: number): string {
return String(Math.floor(safeMs / 1000));
}
-export function buildNotifyEnv(durationMs: number, baseEnv: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
- return {
+export type NotifyContext = {
+ status?: string;
+ failReason?: string;
+ body?: string;
+ title?: string;
+};
+
+export function buildNotifyEnv(
+ durationMs: number,
+ baseEnv: NodeJS.ProcessEnv = process.env,
+ context: NotifyContext = {}
+): NodeJS.ProcessEnv {
+ const env: NodeJS.ProcessEnv = {
...baseEnv,
DURATION: formatDurationSeconds(durationMs),
};
+ delete env.STATUS;
+ delete env.FAIL_REASON;
+ delete env.BODY;
+ delete env.TITLE;
+
+ if (context.status) {
+ env.STATUS = context.status;
+ }
+ if (context.failReason) {
+ env.FAIL_REASON = context.failReason;
+ }
+ if (context.body) {
+ env.BODY = context.body;
+ }
+ if (context.title) {
+ env.TITLE = context.title;
+ }
+ return env;
}
export function launchNotifyScript(
@@ -28,7 +57,8 @@ export function launchNotifyScript(
durationMs: number,
workingDirectory?: string,
spawnProcess: NotifySpawn = spawn as unknown as NotifySpawn,
- configuredEnv: Record = {}
+ configuredEnv: Record = {},
+ context: NotifyContext = {}
): void {
const commandPath = notifyPath?.trim();
if (!commandPath) {
@@ -38,7 +68,7 @@ export function launchNotifyScript(
const options = {
cwd: workingDirectory,
detached: process.platform !== "win32",
- env: buildNotifyEnv(durationMs, { ...process.env, ...configuredEnv }),
+ env: buildNotifyEnv(durationMs, { ...process.env, ...configuredEnv }, context),
stdio: "ignore" as const,
};
diff --git a/src/session.ts b/src/session.ts
index 96a9adb..3a6e13b 100644
--- a/src/session.ts
+++ b/src/session.ts
@@ -2124,7 +2124,23 @@ ${skillMd}
return;
}
- launchNotifyScript(notifyCommand, Date.now() - startedAt, this.projectRoot, undefined, configuredEnv);
+ // Find the last assistant message body for the BODY env variable.
+ let body: string | undefined;
+ const messages = this.listSessionMessages(sessionId);
+ for (let i = messages.length - 1; i >= 0; i--) {
+ const msg = messages[i];
+ if (msg && msg.role === "assistant" && msg.content) {
+ body = msg.content;
+ break;
+ }
+ }
+
+ launchNotifyScript(notifyCommand, Date.now() - startedAt, this.projectRoot, undefined, configuredEnv, {
+ status: session.status,
+ failReason: session.failReason ?? undefined,
+ body,
+ title: session.summary ?? undefined,
+ });
}
private addSessionProcess(sessionId: string, processId: string | number, command: string): void {
diff --git a/src/tests/session.test.ts b/src/tests/session.test.ts
index b7eadae..d079949 100644
--- a/src/tests/session.test.ts
+++ b/src/tests/session.test.ts
@@ -783,6 +783,68 @@ test("reporting a new prompt does not warn when the background request fails", a
assert.deepEqual(warnings, []);
});
+test(
+ "SessionManager notifies successful completion with session context",
+ { skip: process.platform === "win32" },
+ async () => {
+ const workspace = createTempDir("deepcode-notify-success-workspace-");
+ const home = createTempDir("deepcode-notify-success-home-");
+ setHomeDir(home);
+
+ const notifyOutput = path.join(workspace, "notify.jsonl");
+ const notifyScript = createNotifyRecorderScript(workspace);
+ const manager = createNotifyingSessionManager(
+ workspace,
+ [createChatResponse("final answer", { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 })],
+ notifyScript,
+ notifyOutput
+ );
+
+ await manager.createSession({ text: "notify success" });
+
+ const records = await waitForNotifyRecords(notifyOutput, 1);
+ assert.equal(records[0]?.STATUS, "completed");
+ assert.equal(records[0]?.FAIL_REASON, null);
+ assert.equal(records[0]?.BODY, "final answer");
+ assert.equal(records[0]?.TITLE, "notify success");
+ assert.match(String(records[0]?.DURATION), /^\d+$/);
+ }
+);
+
+test(
+ "SessionManager notifies failed completion with failure context",
+ { skip: process.platform === "win32" },
+ async () => {
+ const workspace = createTempDir("deepcode-notify-failure-workspace-");
+ const home = createTempDir("deepcode-notify-failure-home-");
+ setHomeDir(home);
+
+ const notifyOutput = path.join(workspace, "notify.jsonl");
+ const notifyScript = createNotifyRecorderScript(workspace);
+ const manager = createNotifyingSessionManager(
+ workspace,
+ [
+ createChatResponse("first answer", { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }),
+ new Error("second request failed"),
+ ],
+ notifyScript,
+ notifyOutput
+ );
+
+ const sessionId = await manager.createSession({ text: "notify failure" });
+ await waitForNotifyRecords(notifyOutput, 1);
+ await manager.replySession(sessionId, { text: "second prompt" });
+
+ const records = await waitForNotifyRecords(notifyOutput, 2);
+ const failedRecord = records[1];
+ assert.equal(failedRecord?.STATUS, "failed");
+ assert.equal(failedRecord?.FAIL_REASON, "second request failed");
+ assert.equal(failedRecord?.BODY, "first answer");
+ assert.notEqual(failedRecord?.BODY, "stale-body");
+ assert.equal(failedRecord?.TITLE, "notify failure");
+ }
+);
+
test("replySession continues without appending /continue as a user message", async () => {
const workspace = createTempDir("deepcode-continue-workspace-");
const home = createTempDir("deepcode-continue-home-");
@@ -1657,6 +1719,49 @@ function createSessionManager(projectRoot: string, machineId: string): SessionMa
});
}
+function createNotifyingSessionManager(
+ projectRoot: string,
+ responses: unknown[],
+ notifyPath: string,
+ notifyOutput: string
+): SessionManager {
+ const client = {
+ chat: {
+ completions: {
+ create: async () => {
+ const response = responses.shift();
+ assert.ok(response, "expected a queued chat response");
+ if (response instanceof Error) {
+ throw response;
+ }
+ return response;
+ },
+ },
+ },
+ };
+
+ return new SessionManager({
+ projectRoot,
+ createOpenAIClient: () => ({
+ client: client as any,
+ model: "test-model",
+ baseURL: "https://api.deepseek.com",
+ thinkingEnabled: false,
+ notify: notifyPath,
+ env: {
+ NOTIFY_OUTPUT: notifyOutput,
+ STATUS: "stale-status",
+ FAIL_REASON: "stale-failure",
+ BODY: "stale-body",
+ TITLE: "stale-title",
+ },
+ }),
+ getResolvedSettings: () => ({ model: "test-model" }),
+ renderMarkdown: (text) => text,
+ onAssistantMessage: () => {},
+ });
+}
+
function createMockedClientSessionManager(projectRoot: string, responses: unknown[]): SessionManager {
const client = {
chat: {
@@ -1740,6 +1845,45 @@ function createTempDir(prefix: string): string {
return dir;
}
+function createNotifyRecorderScript(dir: string): string {
+ const scriptPath = path.join(dir, "notify-recorder.cjs");
+ fs.writeFileSync(
+ scriptPath,
+ `#!/usr/bin/env node
+const fs = require("fs");
+const keys = ["DURATION", "STATUS", "FAIL_REASON", "BODY", "TITLE"];
+const record = {};
+for (const key of keys) {
+ record[key] = Object.prototype.hasOwnProperty.call(process.env, key) ? process.env[key] : null;
+}
+fs.appendFileSync(process.env.NOTIFY_OUTPUT, JSON.stringify(record) + "\\n", "utf8");
+`,
+ "utf8"
+ );
+ fs.chmodSync(scriptPath, 0o755);
+ return scriptPath;
+}
+
+async function waitForNotifyRecords(
+ outputPath: string,
+ expectedCount: number
+): Promise>> {
+ for (let attempt = 0; attempt < 100; attempt += 1) {
+ if (fs.existsSync(outputPath)) {
+ const records = fs
+ .readFileSync(outputPath, "utf8")
+ .split(/\r?\n/)
+ .filter(Boolean)
+ .map((line) => JSON.parse(line) as Record);
+ if (records.length >= expectedCount) {
+ return records;
+ }
+ }
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ }
+ assert.fail(`expected ${expectedCount} notify records in ${outputPath}`);
+}
+
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
diff --git a/src/tests/settings-and-notify.test.ts b/src/tests/settings-and-notify.test.ts
index 6990288..202f849 100644
--- a/src/tests/settings-and-notify.test.ts
+++ b/src/tests/settings-and-notify.test.ts
@@ -1,6 +1,12 @@
import { test } from "node:test";
import assert from "node:assert/strict";
-import { buildNotifyEnv, formatDurationSeconds, launchNotifyScript, type NotifySpawn } from "../common/notify";
+import {
+ buildNotifyEnv,
+ formatDurationSeconds,
+ launchNotifyScript,
+ type NotifyContext,
+ type NotifySpawn,
+} from "../common/notify";
import { applyModelConfigSelection, resolveSettings, resolveSettingsSources } from "../settings";
const TEST_PROCESS_ENV = {};
@@ -358,14 +364,52 @@ test("formatDurationSeconds preserves sub-second precision and trims trailing ze
assert.equal(formatDurationSeconds(4000), "4");
});
-test("buildNotifyEnv injects DURATION", () => {
+test("buildNotifyEnv injects DURATION without context", () => {
const env = buildNotifyEnv(2750, { HOME: "/tmp/home" });
assert.equal(env.HOME, "/tmp/home");
assert.equal(env.DURATION, "2");
+ assert.equal(env.STATUS, undefined);
+ assert.equal(env.FAIL_REASON, undefined);
+ assert.equal(env.BODY, undefined);
+ assert.equal(env.TITLE, undefined);
+});
+
+test("buildNotifyEnv injects STATUS, FAIL_REASON, BODY, and TITLE from context", () => {
+ const context: NotifyContext = {
+ status: "failed",
+ failReason: "API key not found",
+ body: "Hello, this is the last assistant message.",
+ title: "Fix login bug",
+ };
+ const env = buildNotifyEnv(5000, { HOME: "/tmp/home" }, context);
+ assert.equal(env.HOME, "/tmp/home");
+ assert.equal(env.DURATION, "5");
+ assert.equal(env.STATUS, "failed");
+ assert.equal(env.FAIL_REASON, "API key not found");
+ assert.equal(env.BODY, "Hello, this is the last assistant message.");
+ assert.equal(env.TITLE, "Fix login bug");
+});
+
+test("buildNotifyEnv omits optional context fields when not provided", () => {
+ const env = buildNotifyEnv(
+ 1000,
+ {
+ HOME: "/tmp/home",
+ STATUS: "stale-status",
+ FAIL_REASON: "stale-failure",
+ BODY: "stale-body",
+ TITLE: "stale-title",
+ },
+ { status: "completed" }
+ );
+ assert.equal(env.STATUS, "completed");
+ assert.equal(env.FAIL_REASON, undefined);
+ assert.equal(env.BODY, undefined);
+ assert.equal(env.TITLE, undefined);
});
test(
- "launchNotifyScript passes DURATION and falls back to /bin/sh for non-executable scripts",
+ "launchNotifyScript passes DURATION, context vars, and falls back to /bin/sh for non-executable scripts",
{ skip: process.platform === "win32" },
() => {
const calls: Array<{
@@ -390,7 +434,13 @@ test(
};
};
- launchNotifyScript("/tmp/notify.sh", 2750, "/tmp/project", spawnProcess, { WEBHOOK: "configured" });
+ const context: NotifyContext = {
+ status: "completed",
+ body: "Task finished successfully.",
+ title: "Fix login bug",
+ };
+
+ launchNotifyScript("/tmp/notify.sh", 2750, "/tmp/project", spawnProcess, { WEBHOOK: "configured" }, context);
assert.equal(calls.length, 2);
assert.equal(calls[0]?.command, "/tmp/notify.sh");
@@ -398,9 +448,16 @@ test(
assert.equal(calls[0]?.options.cwd, "/tmp/project");
assert.equal(calls[0]?.options.env?.DURATION, "2");
assert.equal(calls[0]?.options.env?.WEBHOOK, "configured");
+ assert.equal(calls[0]?.options.env?.STATUS, "completed");
+ assert.equal(calls[0]?.options.env?.FAIL_REASON, undefined);
+ assert.equal(calls[0]?.options.env?.BODY, "Task finished successfully.");
+ assert.equal(calls[0]?.options.env?.TITLE, "Fix login bug");
assert.equal(calls[1]?.command, "/bin/sh");
assert.deepEqual(calls[1]?.args, ["/tmp/notify.sh"]);
assert.equal(calls[1]?.options.cwd, "/tmp/project");
assert.equal(calls[1]?.options.env?.DURATION, "2");
+ assert.equal(calls[1]?.options.env?.STATUS, "completed");
+ assert.equal(calls[1]?.options.env?.BODY, "Task finished successfully.");
+ assert.equal(calls[1]?.options.env?.TITLE, "Fix login bug");
}
);
From a3ff70e82d548a8c1273ea377844f078cbd0ae00 Mon Sep 17 00:00:00 2001
From: lellansin
Date: Tue, 19 May 2026 10:43:55 +0800
Subject: [PATCH 28/90] docs(notify): add Windows Terminal, Linux, and msg
popup notification examples; add edge-case tests
- Expand OSC 9 example to cover both iTerm2 and Windows Terminal
- Add .bat example for Windows Terminal users
- Add Linux notify-send example
- Add Windows msg popup notification example
- Add tests for empty-string rejection and special character preservation
---
docs/configuration.md | 32 +++++++++++++++++++++++----
docs/configuration_en.md | 32 +++++++++++++++++++++++----
src/tests/settings-and-notify.test.ts | 27 ++++++++++++++++++++++
3 files changed, 83 insertions(+), 8 deletions(-)
diff --git a/docs/configuration.md b/docs/configuration.md
index 45aaab0..7c2880c 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -83,14 +83,14 @@ Deep Code 使用 `settings.json` 设置文件进行持久化配置,支持两
}
```
-**iTerm2 终端通知示例**:
+**终端内通知示例(支持 iTerm2 / Windows Terminal)**:
-如果你的终端是 iTerm2,可以直接通过 OSC 9 转义序列弹出通知,无需额外脚本。创建以下脚本(如 `~/.deepcode/notify.sh`):
+如果你的终端是 iTerm2 或 Windows Terminal,可以直接通过 OSC 9 转义序列弹出终端原生通知,无需额外依赖。创建以下脚本(如 `~/.deepcode/notify.sh`):
```bash
#!/bin/bash
-# iTerm2 OSC 9 通知
-echo -e "\x1b]9;DeepCode: task ${STATUS:-completed} (${DURATION}s)\x07"
+# iTerm2 / Windows Terminal OSC 9 通知
+printf '\x1b]9;DeepCode: task %s (%ss)\x07' "${STATUS:-completed}" "${DURATION}"
```
```json
@@ -99,6 +99,14 @@ echo -e "\x1b]9;DeepCode: task ${STATUS:-completed} (${DURATION}s)\x07"
}
```
+Windows 用户如使用 Git Bash,上述脚本同样可用;也可创建 `.bat` 脚本:
+
+```batch
+@echo off
+REM Windows Terminal OSC 9 通知
+echo \x1b]9;DeepCode: task %STATUS% (%DURATION%s)\x07
+```
+
**macOS 系统通知示例**:
```bash
@@ -107,6 +115,22 @@ echo -e "\x1b]9;DeepCode: task ${STATUS:-completed} (${DURATION}s)\x07"
osascript -e "display notification \"任务已${STATUS:-完成},耗时 ${DURATION}s\" with title \"DeepCode\""
```
+**Linux 系统通知示例**(需安装 `libnotify-bin`):
+
+```bash
+#!/bin/bash
+# Linux notify-send 通知
+notify-send "DeepCode" "任务已${STATUS:-完成},耗时 ${DURATION}s"
+```
+
+**Windows msg 弹窗通知示例**:
+
+```batch
+@echo off
+REM Windows msg 弹窗通知
+msg %USERNAME% "DeepCode: task %STATUS% (%DURATION%s)"
+```
+
#### `webSearchTool` — 自定义联网搜索
Deep Code 内置免费可用的 Web Search 工具。如果需要自定义搜索逻辑,可将 `webSearchTool` 设为一个可执行脚本的完整路径:
diff --git a/docs/configuration_en.md b/docs/configuration_en.md
index 606fcab..5d931f4 100644
--- a/docs/configuration_en.md
+++ b/docs/configuration_en.md
@@ -83,14 +83,14 @@ The following context is injected as environment variables when the notify scrip
}
```
-**iTerm2 Notification Example**:
+**Terminal Notification Example (iTerm2 / Windows Terminal)**:
-On iTerm2 you can use the OSC 9 escape sequence for native notifications. Create a script (e.g., `~/.deepcode/notify.sh`):
+On iTerm2 or Windows Terminal you can use the OSC 9 escape sequence for native terminal notifications with zero dependencies. Create a script (e.g., `~/.deepcode/notify.sh`):
```bash
#!/bin/bash
-# iTerm2 OSC 9 notification
-echo -e "\x1b]9;DeepCode: task ${STATUS:-completed} (${DURATION}s)\x07"
+# iTerm2 / Windows Terminal OSC 9 notification
+printf '\x1b]9;DeepCode: task %s (%ss)\x07' "${STATUS:-completed}" "${DURATION}"
```
```json
@@ -99,6 +99,14 @@ echo -e "\x1b]9;DeepCode: task ${STATUS:-completed} (${DURATION}s)\x07"
}
```
+Windows users on Git Bash can use the same script; alternatively create a `.bat` script:
+
+```batch
+@echo off
+REM Windows Terminal OSC 9 notification
+echo \x1b]9;DeepCode: task %STATUS% (%DURATION%s)\x07
+```
+
**macOS System Notification Example**:
```bash
@@ -107,6 +115,22 @@ echo -e "\x1b]9;DeepCode: task ${STATUS:-completed} (${DURATION}s)\x07"
osascript -e "display notification \"Task ${STATUS:-completed}, took ${DURATION}s\" with title \"DeepCode\""
```
+**Linux System Notification Example** (requires `libnotify-bin`):
+
+```bash
+#!/bin/bash
+# Linux notify-send notification
+notify-send "DeepCode" "Task ${STATUS:-completed}, took ${DURATION}s"
+```
+
+**Windows msg Popup Notification Example**:
+
+```batch
+@echo off
+REM Windows msg popup notification
+msg %USERNAME% "DeepCode: task %STATUS% (%DURATION%s)"
+```
+
#### `webSearchTool` — Custom Web Search
Deep Code has a built-in, free-to-use Web Search tool. If you need custom search logic, set `webSearchTool` to the full path of an executable script:
diff --git a/src/tests/settings-and-notify.test.ts b/src/tests/settings-and-notify.test.ts
index 202f849..1707aff 100644
--- a/src/tests/settings-and-notify.test.ts
+++ b/src/tests/settings-and-notify.test.ts
@@ -408,6 +408,33 @@ test("buildNotifyEnv omits optional context fields when not provided", () => {
assert.equal(env.TITLE, undefined);
});
+test("buildNotifyEnv ignores empty strings in context", () => {
+ const env = buildNotifyEnv(
+ 1000,
+ { HOME: "/tmp/home" },
+ {
+ status: "",
+ failReason: "",
+ body: "",
+ title: "",
+ }
+ );
+ assert.equal(env.STATUS, undefined);
+ assert.equal(env.FAIL_REASON, undefined);
+ assert.equal(env.BODY, undefined);
+ assert.equal(env.TITLE, undefined);
+});
+
+test("buildNotifyEnv preserves special characters in body and title", () => {
+ const context: NotifyContext = {
+ body: 'Line 1\nLine 2\tindented "quoted"',
+ title: "Fix: login & signup (urgent)",
+ };
+ const env = buildNotifyEnv(1000, {}, context);
+ assert.equal(env.BODY, 'Line 1\nLine 2\tindented "quoted"');
+ assert.equal(env.TITLE, "Fix: login & signup (urgent)");
+});
+
test(
"launchNotifyScript passes DURATION, context vars, and falls back to /bin/sh for non-executable scripts",
{ skip: process.platform === "win32" },
From 479606f6a7087398302334996e95cb8eb2d841b3 Mon Sep 17 00:00:00 2001
From: lellansin
Date: Tue, 19 May 2026 13:33:28 +0800
Subject: [PATCH 29/90] docs(notify): replace terminal notification examples
with Feishu webhook example
- Remove iTerm2/Windows Terminal OSC 9, macOS osascript, Linux notify-send, and Windows msg examples (OSC 9 is not compatible with current spawn+stdio:ignore architecture)
- Add Feishu (Lark) webhook notification example in both Chinese and English docs
- Keep the env variable table (DURATION, STATUS, FAIL_REASON, BODY, TITLE) unchanged
---
docs/configuration.md | 62 ++++++++++++++--------------------------
docs/configuration_en.md | 62 ++++++++++++++--------------------------
2 files changed, 44 insertions(+), 80 deletions(-)
diff --git a/docs/configuration.md b/docs/configuration.md
index 7c2880c..b05a44f 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -83,53 +83,35 @@ Deep Code 使用 `settings.json` 设置文件进行持久化配置,支持两
}
```
-**终端内通知示例(支持 iTerm2 / Windows Terminal)**:
+**飞书 Webhook 通知示例**:
-如果你的终端是 iTerm2 或 Windows Terminal,可以直接通过 OSC 9 转义序列弹出终端原生通知,无需额外依赖。创建以下脚本(如 `~/.deepcode/notify.sh`):
+`node` 构建 JSON(自动转义特殊字符),`curl` 发送:
```bash
#!/bin/bash
-# iTerm2 / Windows Terminal OSC 9 通知
-printf '\x1b]9;DeepCode: task %s (%ss)\x07' "${STATUS:-completed}" "${DURATION}"
-```
-
-```json
-{
- "notify": "/Users/you/.deepcode/notify.sh"
-}
-```
-
-Windows 用户如使用 Git Bash,上述脚本同样可用;也可创建 `.bat` 脚本:
-
-```batch
-@echo off
-REM Windows Terminal OSC 9 通知
-echo \x1b]9;DeepCode: task %STATUS% (%DURATION%s)\x07
-```
-
-**macOS 系统通知示例**:
-
-```bash
-#!/bin/bash
-# macOS 系统通知
-osascript -e "display notification \"任务已${STATUS:-完成},耗时 ${DURATION}s\" with title \"DeepCode\""
-```
-
-**Linux 系统通知示例**(需安装 `libnotify-bin`):
+WEBHOOK_URL="https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxx"
+
+STATUS="${STATUS:-completed}"
+TITLE="${TITLE:-Untitled}"
+DURATION="${DURATION:-0}"
+BODY="${BODY:-(no output)}"
+
+PAYLOAD=$(node -e "
+process.stdout.write(JSON.stringify({
+ msg_type: 'interactive',
+ card: {
+ header: { title: { tag: 'plain_text', content: 'DeepCode: ' + process.env.TITLE + ' ' + process.env.STATUS + ' [' + process.env.DURATION + 's]' } },
+ elements: [{ tag: 'markdown', content: (process.env.BODY || '').slice(0, 2000) || '(no output)' }]
+ }
+}))
+")
-```bash
-#!/bin/bash
-# Linux notify-send 通知
-notify-send "DeepCode" "任务已${STATUS:-完成},耗时 ${DURATION}s"
+curl -s -X POST "$WEBHOOK_URL" \
+ -H "Content-Type: application/json" \
+ -d "$PAYLOAD"
```
-**Windows msg 弹窗通知示例**:
-
-```batch
-@echo off
-REM Windows msg 弹窗通知
-msg %USERNAME% "DeepCode: task %STATUS% (%DURATION%s)"
-```
+将 `WEBHOOK_URL` 替换为你的飞书机器人 Webhook 地址。更多变量参考上表。同样适用于 Slack、企业微信等 webhook 类通知,只需修改 JSON payload 格式。
#### `webSearchTool` — 自定义联网搜索
diff --git a/docs/configuration_en.md b/docs/configuration_en.md
index 5d931f4..4f2f94d 100644
--- a/docs/configuration_en.md
+++ b/docs/configuration_en.md
@@ -83,53 +83,35 @@ The following context is injected as environment variables when the notify scrip
}
```
-**Terminal Notification Example (iTerm2 / Windows Terminal)**:
+**Feishu (Lark) Webhook Notification Example**:
-On iTerm2 or Windows Terminal you can use the OSC 9 escape sequence for native terminal notifications with zero dependencies. Create a script (e.g., `~/.deepcode/notify.sh`):
+`node` builds the JSON (auto-escapes special characters), `curl` sends it:
```bash
#!/bin/bash
-# iTerm2 / Windows Terminal OSC 9 notification
-printf '\x1b]9;DeepCode: task %s (%ss)\x07' "${STATUS:-completed}" "${DURATION}"
-```
-
-```json
-{
- "notify": "/Users/you/.deepcode/notify.sh"
-}
-```
-
-Windows users on Git Bash can use the same script; alternatively create a `.bat` script:
-
-```batch
-@echo off
-REM Windows Terminal OSC 9 notification
-echo \x1b]9;DeepCode: task %STATUS% (%DURATION%s)\x07
-```
-
-**macOS System Notification Example**:
-
-```bash
-#!/bin/bash
-# macOS system notification
-osascript -e "display notification \"Task ${STATUS:-completed}, took ${DURATION}s\" with title \"DeepCode\""
-```
-
-**Linux System Notification Example** (requires `libnotify-bin`):
+WEBHOOK_URL="https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxx"
+
+STATUS="${STATUS:-completed}"
+TITLE="${TITLE:-Untitled}"
+DURATION="${DURATION:-0}"
+BODY="${BODY:-(no output)}"
+
+PAYLOAD=$(node -e "
+process.stdout.write(JSON.stringify({
+ msg_type: 'interactive',
+ card: {
+ header: { title: { tag: 'plain_text', content: 'DeepCode: ' + process.env.TITLE + ' ' + process.env.STATUS + ' [' + process.env.DURATION + 's]' } },
+ elements: [{ tag: 'markdown', content: (process.env.BODY || '').slice(0, 2000) || '(no output)' }]
+ }
+}))
+")
-```bash
-#!/bin/bash
-# Linux notify-send notification
-notify-send "DeepCode" "Task ${STATUS:-completed}, took ${DURATION}s"
+curl -s -X POST "$WEBHOOK_URL" \
+ -H "Content-Type: application/json" \
+ -d "$PAYLOAD"
```
-**Windows msg Popup Notification Example**:
-
-```batch
-@echo off
-REM Windows msg popup notification
-msg %USERNAME% "DeepCode: task %STATUS% (%DURATION%s)"
-```
+Replace `WEBHOOK_URL` with your Feishu bot webhook URL. See the table above for all available variables. This pattern also works for other webhook-based notifications (Slack, WeCom, etc.) — just adjust the JSON payload format.
#### `webSearchTool` — Custom Web Search
From 7e5eeda26829b14eb3ed503b550db06c1145acf6 Mon Sep 17 00:00:00 2001
From: hcyang
Date: Tue, 19 May 2026 15:05:00 +0800
Subject: [PATCH 30/90] =?UTF-8?q?feat(ui):=20=E6=B7=BB=E5=8A=A0=20raw=20?=
=?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=B6=88=E6=81=AF=E7=9B=B4=E6=8E=A5?=
=?UTF-8?q?=E6=B8=B2=E6=9F=93=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 在 Raw 模式下,使用 process.stdout.write 直接输出所有可见消息
- 清屏并重置光标位置,避免 Ink 组件干扰
- 显示提示信息,指导用户按 ESC 退出 raw 模式
- 优化终端尺寸变化时的重绘逻辑
- 更新依赖,确保 raw 模式变动触发重新渲染
---
src/ui/App.tsx | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/src/ui/App.tsx b/src/ui/App.tsx
index 9189df6..e39fd03 100644
--- a/src/ui/App.tsx
+++ b/src/ui/App.tsx
@@ -434,8 +434,31 @@ export function App({ projectRoot, initialPrompt, onRestart }: AppProps): React.
}
lastRenderedColumnsRef.current = stableColumns;
+ if (mode === RawMode.Raw) {
+ // In raw mode, re-render all messages directly to stdout at the new width.
+ // Use process.stdout.write instead of writeRef to avoid Ink interference.
+ process.stdout.write("\u001B[2J\u001B[3J\u001B[H");
+ const activeSessionId = sessionManager.getActiveSessionId();
+ const allMessages = activeSessionId ? loadVisibleMessages(sessionManager, activeSessionId) : [];
+ for (const msg of allMessages) {
+ process.stdout.write("\n");
+ process.stdout.write(renderMessageToStdout(msg, mode) + "\n\n");
+ }
+ if (allMessages.length > 0) {
+ process.stdout.write("\n\n");
+ process.stdout.write(chalk.dim("Press ESC to exit raw mode"));
+ } else {
+ process.stdout.write("\n");
+ process.stdout.write(chalk.dim("(No messages in this session yet. Start chatting to see them here.)"));
+ process.stdout.write("\n\n");
+ process.stdout.write(chalk.dim("Press ESC to exit raw mode"));
+ }
+ return;
+ }
+
// Force full redraw on terminal resize to avoid stale wrapped rows.
writeRef.current("\u001B[2J\u001B[H");
+
setMessages([]);
setShowWelcome(false);
setWelcomeNonce((n) => n + 1);
@@ -447,7 +470,8 @@ export function App({ projectRoot, initialPrompt, onRestart }: AppProps): React.
setMessages(nextMessages);
setShowWelcome(true);
}, 0);
- }, [busy, sessionManager, stableColumns, stdout]);
+ }, [busy, mode, sessionManager, stableColumns, stdout]);
+
const screenWidth = useMemo(() => stableColumns ?? stdout?.columns ?? 80, [stableColumns, stdout]);
const promptHistory = useMemo(() => {
return messages
From faf10c3e087d214bf863e9df14040176e30de821 Mon Sep 17 00:00:00 2001
From: hcyang
Date: Tue, 19 May 2026 15:12:08 +0800
Subject: [PATCH 31/90] =?UTF-8?q?refactor(ui):=20=E4=BC=98=E5=8C=96?=
=?UTF-8?q?=E7=AA=97=E5=8F=A3=E5=AE=BD=E5=BA=A6=E7=9B=B8=E5=85=B3=E7=9A=84?=
=?UTF-8?q?=E7=8A=B6=E6=80=81=E5=92=8C=E5=BC=95=E7=94=A8=E7=AE=A1=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 合并并调整了关于窗口宽度columns的使用,去除了stableColumns状态
- 引用lastRenderedColumnsRef改为直接使用columns,避免延迟更新
- 将多个相关的useRef(writeRef、rawModeRef、messagesRef、processStdoutRef)移至同一位置声明
- 调整useEffect依赖项,改为监听columns代替stableColumns
- 优化RawMode下消息重绘逻辑,确保宽度变化时重新渲染
- 统一了screenWidth的计算逻辑,简化代码结构
---
src/ui/App.tsx | 30 ++++++++++++------------------
1 file changed, 12 insertions(+), 18 deletions(-)
diff --git a/src/ui/App.tsx b/src/ui/App.tsx
index e39fd03..582abaf 100644
--- a/src/ui/App.tsx
+++ b/src/ui/App.tsx
@@ -55,7 +55,13 @@ export function App({ projectRoot, initialPrompt, onRestart }: AppProps): React.
const { exit } = useApp();
const { stdout, write } = useStdout();
const { columns } = useWindowSize();
+ const { mode, setMode } = useRawModeContext();
const initialPromptSubmittedRef = useRef(false);
+ const processStdoutRef = useRef