@mealsea/dsh-prompt-enhance 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/README.md +49 -14
- package/client.js +685 -49
- package/docs/AUDIT-1.0.0.md +76 -0
- package/docs/DETAIL.md +95 -3
- package/docs/RELEASE.md +143 -0
- package/docs/dev/awareness-contract.md +1253 -0
- package/docs/dev/context-facts.md +643 -0
- package/docs/images/enhance-panel.png +0 -0
- package/docs/images/icon-180.png +0 -0
- package/docs/images/settings.png +0 -0
- package/docs/prompt-enhance-plugin-dev.md +3 -3
- package/docs/report-2026-12-06-awareness.md +209 -0
- package/icon.svg +1 -1
- package/index.js +796 -6
- package/locale/en.json +1 -1
- package/package.json +4 -4
- package/test/host.mjs +606 -1
- package/test/smoke.mjs +265 -2
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
# DSH 宿主侧只读上下文与 / 命令描述调研
|
|
2
|
+
|
|
3
|
+
- 调研日期:2026-12-06
|
|
4
|
+
- 调研范围:只读。未修改 DSH 安装、未修改本仓库源码;本文是唯一新增文件。
|
|
5
|
+
- 目标:为 prompt-enhance 增加「工作区/文件感知(只读、可审计)+ / 命令感知 + 参数化限制」确定真实、可用的宿主 API,并给出降级路径。
|
|
6
|
+
- 证据约定:`ASAR` 指本机安装目录 `D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai`(Electron asar,普通 shell 不能直接 `cat`;本次用 `ELECTRON_RUN_AS_NODE=1 "/d/dsh/DeepSeek Harness.exe"` 读取,并在运行中的 Host 上用 Cordis Inspect `Service.listService` 交叉核对签名)。下文证据均为「绝对路径:行号」或「导出的符号名」。
|
|
7
|
+
- 未找到 / 未验证的内容在文中显式标注,不做推测。
|
|
8
|
+
|
|
9
|
+
## 0. 结论速览
|
|
10
|
+
|
|
11
|
+
| 能力 | 结论 | 入口(证据见对应章节) |
|
|
12
|
+
| --- | --- | --- |
|
|
13
|
+
| `sessionId` → 工作区根(live 会话) | **可用** | `ctx.get('sessions').get(id).header.cwd` |
|
|
14
|
+
| `sessionId` → 工作区根(非 live/持久化会话) | **可用(异步)** | `await ctx.get('sessionPersistence').stat(id)` → `snapshot.header.cwd` |
|
|
15
|
+
| 工作区注册表反查 | **可用但只覆盖 live 会话** | `ctx.get('workspaceRegistry').list()` → `sessionIds` / `path` |
|
|
16
|
+
| `workspaceFiles` 的 scope | **可用,Host 可自行构造** | `{ sessionId, workspaceRoot }`(纯对象) |
|
|
17
|
+
| 读文本 / 列目录 / 搜索文件 | **可用** | `workspaceFiles.read/list/search/stat` |
|
|
18
|
+
| 直接 `ctx.get('fs')` 读 | **可用,但无行窗口、无上限、不围栏** | `fs.resolve/stat/readText/readBytes/listDir` |
|
|
19
|
+
| glob | **不可用(无此 API)** | `FileSystem` 没有 glob;用 `workspaceFiles.search` 替代 |
|
|
20
|
+
| `ctx.get('sandboxPolicy')` | **读操作不受它限制** | 它只围栏写操作;默认 `read-only` |
|
|
21
|
+
| `/` 命令描述(Host) | **可用,但必须先拿到 Agent** | `ctx.get('commands').list(agent)` |
|
|
22
|
+
| `/` 命令描述(Client) | **可用,但需要 sessionId 与 `remote.commands` 命名空间** | `ctx.remote.commands.list(sessionId)` |
|
|
23
|
+
| chip 的 `claim.token` → 命令名 | **需解析,不能纯字符串还原** | `InputState.claim.name` 或 `resolveClaim(token)` |
|
|
24
|
+
| Client slot 直接拿 `sessionId` | **未找到** | `conversation.input.left` 无 owner props / 标准 props 无 sessionId |
|
|
25
|
+
| 只读保证 | **可用**:`workspaceFiles` 完全没有写方法;`fs` 的写方法我们不调用 | 见 §5 |
|
|
26
|
+
| 读取上限 / 二进制拒绝 | **有明确上限**,超限是拒绝不是截断 | 见 §5.2 |
|
|
27
|
+
|
|
28
|
+
一句话建议:**Host 侧用 `workspaceFiles` 作为唯一读入口(read/list/search/stat),scope 自己构造;session 根按 `sessions.get()` → `sessionPersistence.stat()` 顺序解析;命令描述用 `commands.list(agents.get(sessionId))`。Client 侧只有在拿到 `sessionId` 后才能请求工作区上下文,否则 Host 必须降级为「最近会话」。**
|
|
29
|
+
|
|
30
|
+
## 1. 证据基线与运行时事实
|
|
31
|
+
|
|
32
|
+
### 1.1 运行中的 Host 服务确实存在
|
|
33
|
+
|
|
34
|
+
在运行中的 Host 上用 Cordis Inspect(`platform=host`, `provider=Service`, `method=listService`)确认以下服务已挂载,且签名可用:
|
|
35
|
+
|
|
36
|
+
- `fs`:抽象 `FileSystem`,方法 `watch/resolve/processPath/processPathFromHostPath/fileUrl/contains/stat/lstat/readText/streamText/readBytes/readByteRange/listDir/writeText/editText/patchText/deleteFile/moveFile`(**没有 glob**)。
|
|
37
|
+
- `workspaceFiles`:`read/readBytes/stat/list/search/changes`;其 scope 类型 `WorkspaceFileScope { sessionId, workspaceRoot }` 只有两个字段。
|
|
38
|
+
- `sessions`:`SessionStore`,`get(id)`、`list()`。
|
|
39
|
+
- `sessionPersistence`:抽象服务,`stat(id, options?)` 返回 `SessionPersistenceSnapshot | undefined`,`snapshot.header` 就是 `SessionHeader`。
|
|
40
|
+
- `workspaceRegistry`:`create/initializeDefault/get/list/delete/insertBefore/archiveSession/unarchiveSession/pinSession/unpinSession/resolveByPath`。
|
|
41
|
+
- `sandboxPolicy`:`defaultMode/workspaceRoot/additionalWritableRoots/resolve/registerWritableRoot/sessionId`。
|
|
42
|
+
- `commands`:`list(agent)`、`find(agent,name)`、`execute(agent,line,attachments,signal)`。
|
|
43
|
+
- `agents`:`get(id)`、`list()` 等。
|
|
44
|
+
- `workingDirectory`:`get(session)`、`ensure(agent)`、`set(agent,path)`、`defaultDirectory`。
|
|
45
|
+
- `fileReferences`:`list(agent, query, signal)` → `FileReferenceCandidate[]`(`@` 文件发现)。
|
|
46
|
+
- `typert`:只有 schema/反射注册表(`register/get/resolve/list/getPackage/listPackages/toJSONSchema`),**没有 scope lookup 的公开调用入口**。
|
|
47
|
+
|
|
48
|
+
说明:以上是运行时事实,源码中的抽象类定义与之一致;具体源码证据在各节给出。
|
|
49
|
+
|
|
50
|
+
### 1.2 本插件是「普通 ESM Host 插件」,不是动态 Cordis 插件
|
|
51
|
+
|
|
52
|
+
`D:/prompt_enhance/index.js:367`:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
export const inject = ['webServer']
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`D:/prompt_enhance/index.js:373` 起是 `export function apply(ctx)`,通过 `ctx.webServer.register` 注册 exact 路由(`index.js:377-383`)。
|
|
59
|
+
|
|
60
|
+
因此 Host 半只需要在 `apply` 的 `ctx` 上用 `ctx.get(...)` 取可选服务;**任何服务都可能因 profile 不同而缺失**,缺失时降级。
|
|
61
|
+
|
|
62
|
+
## 2. 由 sessionId 解析工作区根
|
|
63
|
+
|
|
64
|
+
### 2.1 live 会话:`sessions.get(id).header.cwd`(首选)
|
|
65
|
+
|
|
66
|
+
`SessionHeader` 定义(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-session/src/types.ts:95-136`):
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
export interface SessionHeader {
|
|
70
|
+
readonly version: typeof SESSION_FORMAT_VERSION
|
|
71
|
+
readonly id: SessionId
|
|
72
|
+
readonly createdAt: number
|
|
73
|
+
/** Absolute working directory the Session was created in, when one was given. */
|
|
74
|
+
readonly cwd?: string
|
|
75
|
+
readonly parentSession?: SessionId
|
|
76
|
+
readonly isSeeded: boolean
|
|
77
|
+
readonly origin?: 'subagent'
|
|
78
|
+
readonly delegationDepth?: number
|
|
79
|
+
readonly agentPreset?: string
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
- `cwd` 是**可选**的:没有给定工作目录的 blank 会话可能没有 `cwd`。
|
|
84
|
+
- `SessionStore.get(id)` 只返回 **live** 会话:`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-session/src/index.ts:1455-1457`。
|
|
85
|
+
- `Session.header` 是只读公共字段:`.../dsh-session/src/index.ts:574`。
|
|
86
|
+
- `Context.sessions` 声明:`.../dsh-session/src/index.ts:39-40`。
|
|
87
|
+
|
|
88
|
+
调用序列:
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
const sessions = ctx.get('sessions')
|
|
92
|
+
const header = sessions?.get(sessionId)?.header
|
|
93
|
+
const cwd = header?.cwd // string | undefined
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 2.2 非 live(持久化)会话:`sessionPersistence.stat(id)`
|
|
97
|
+
|
|
98
|
+
`workspaceFiles` 自己就是这么做的:`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-api-workspace-files/src/index.ts:222-233`:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const live = this.sessions.get(sessionId)?.header
|
|
102
|
+
const header = live ?? (await this.persistence.stat(sessionId, { signal }))?.header
|
|
103
|
+
...
|
|
104
|
+
return { sessionId, workspaceRoot: header.cwd ?? this.sandboxPolicy.workspaceRoot }
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
`dsh-api-workspace-files` 的 `static inject = ['fs','sandboxPolicy','sessions','typert']` 里没有 `sessionPersistence`,它通过构造参数拿到 persistence;但对本插件而言,`sessionPersistence` 是**已挂载的公开 Host 服务**(见 §1.1 运行时清单),可以:
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
const live = ctx.get('sessions')?.get(sessionId)?.header
|
|
111
|
+
const persisted = live ?? (await ctx.get('sessionPersistence')?.stat(sessionId, { signal }))?.header
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`stat` 不读事件日志、不取写所有权(Cordis Inspect 描述原文:`Observe one stored session without reading its event log or taking ownership`),并且返回的 `header` 类型就是 §2.1 的 `SessionHeader`。
|
|
115
|
+
|
|
116
|
+
结论:**可用**。这是覆盖「用户打开历史会话但当前没有 live session」场景的正确降级。
|
|
117
|
+
|
|
118
|
+
### 2.3 `workspaceRegistry` 反查(只能作辅助)
|
|
119
|
+
|
|
120
|
+
`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-workspace/src/index.ts:113-116` 声明 `Context.workspaceRegistry`;`186-187` 是 `class WorkspaceRegistry extends Service`,`static inject = ['storageDomain','sessionPersistence']`。
|
|
121
|
+
|
|
122
|
+
- `list(): Workspace[]`:`.../dsh-workspace/src/index.ts:308-316`,按 durable 顺序返回。
|
|
123
|
+
- `get(id)`:`.../dsh-workspace/src/index.ts:298-300`。
|
|
124
|
+
- `create(path, title?)`:`.../dsh-workspace/src/index.ts:254`,要求「全限定、已存在目录」,内部 `realpathNormalize`。
|
|
125
|
+
- `resolveByPath(path)`:运行时签名 `resolveByPath(path): Promise<Workspace | undefined>`(Cordis Inspect)。
|
|
126
|
+
- `Workspace` 接口:`.../dsh-workspace/src/types.ts:69-153`,其中 `path` 是 canonical `fs.realpath`(尾斜杠、`..`、符号链接都会被解析,之后永不改写);`sessionIds` 是 `readonly SessionId[]`,且**按 header cwd 校验**。
|
|
127
|
+
- 成员推导逻辑:`.../dsh-workspace/src/index.ts:860-881` 用 `sessions.list().map(s => s.header)` 建索引,并比较 canonical cwd;`readSessionHeader()`(883-899)只从 `sessions.get(id)` 取。
|
|
128
|
+
|
|
129
|
+
因此:
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
const registry = ctx.get('workspaceRegistry')
|
|
133
|
+
const ws = registry?.list().find((w) => w.sessionIds.includes(sessionId))
|
|
134
|
+
const root = ws?.path
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**限制**:`sessionIds` 只包含 live 会话(header 驱动)。历史会话用 §2.2。**未找到**「按 sessionId 直接查询 Workspace」的方法(`get` 只接受 WorkspaceId),所以必须 `list()` 后过滤,O(n) 且 n 很小。
|
|
138
|
+
|
|
139
|
+
### 2.4 `workspaceFiles` 的 scope:Host 可以自行构造
|
|
140
|
+
|
|
141
|
+
类型(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-api-workspace-files/src/index.ts:58-64`):
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
export interface WorkspaceFileScope {
|
|
145
|
+
readonly sessionId: SessionId
|
|
146
|
+
readonly workspaceRoot: string
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
- 它是**纯普通对象**,没有 brand、没有隐藏字段、没有构造校验。
|
|
151
|
+
- 服务内部把 `sessionId` 注册成 typert lookup(`index.ts:189-236`),那是**远程 wire 的 scope 解析**:客户端只传 `sessionId` 时,Host 用同一段 live/持久化逻辑补出 `workspaceRoot`。**Host 插件无法调用这个 lookup**(`typert` 服务只暴露 schema 注册表,§1.1),所以 Host 必须自己按 §2.1/2.2 算出 `workspaceRoot` 后构造 scope。
|
|
152
|
+
- `workspaceRoot` 的作用是相对路径的 base:`locateFile()` 用 `fs.lstat(path, { cwd: scope.workspaceRoot })`(`.../dsh-api-workspace-files/src/index.ts:445-459`),模块头也明确写「The selected Session header supplies the base for relative paths, with the sandbox policy root as its no-cwd fallback」(`.../dsh-api-workspace-files/src/index.ts:1-21`)。
|
|
153
|
+
|
|
154
|
+
结论:**可用**。构造方式:
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
const scope = { sessionId, workspaceRoot } // 必须与 session 的 cwd 一致,否则相对路径解析到错误目录
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### 2.5 本节最小序列与降级
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
async function resolveContext(ctx, sessionId, signal) {
|
|
164
|
+
const sessions = ctx.get('sessions')
|
|
165
|
+
const live = sessionId ? sessions?.get(sessionId)?.header : undefined
|
|
166
|
+
const header = live ?? (sessionId
|
|
167
|
+
? (await ctx.get('sessionPersistence')?.stat(sessionId, { signal }))?.header
|
|
168
|
+
: undefined)
|
|
169
|
+
|
|
170
|
+
if (!header) return { ok: false, code: 'SESSION_NOT_FOUND' }
|
|
171
|
+
|
|
172
|
+
const root = header.cwd
|
|
173
|
+
?? ctx.get('workspaceRegistry')?.list()
|
|
174
|
+
.find((w) => w.sessionIds.includes(sessionId))?.path
|
|
175
|
+
?? ctx.get('sandboxPolicy')?.workspaceRoot
|
|
176
|
+
if (!root) return { ok: false, code: 'WORKSPACE_NOT_FOUND' }
|
|
177
|
+
return { ok: true, scope: { sessionId, workspaceRoot: root } }
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
降级顺序:`header.cwd` → registry(live)→ `sandboxPolicy.workspaceRoot`(部署默认根,**不是**会话根,必须向用户标注)→ 报错。
|
|
182
|
+
|
|
183
|
+
## 3. `fs` 与 `sandboxPolicy` 是否可用
|
|
184
|
+
|
|
185
|
+
### 3.1 `ctx.get('fs')` 的读方法(确切签名)
|
|
186
|
+
|
|
187
|
+
`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-fs/src/index.ts:53-56` 声明 `Context.fs`;`104` 行是 `export abstract class FileSystem extends Service`。
|
|
188
|
+
|
|
189
|
+
| 用途 | 方法(`.../dsh-fs/src/index.ts` 行号) |
|
|
190
|
+
| --- | --- |
|
|
191
|
+
| 解析路径 | `resolve(path: string, opts?: { cwd?: string; signal?: AbortSignal }): Promise<FsTarget>`(149) |
|
|
192
|
+
| 包含判断 | `contains(parent: FsTarget, child: FsTarget): boolean`(190) |
|
|
193
|
+
| stat(跟随末段链接) | `stat(target, signal?): Promise<FsInfo | undefined>`(198) |
|
|
194
|
+
| lstat(不跟随末段链接) | `lstat(path, opts?: {cwd?}, signal?): Promise<FsPathInfo | undefined>`(214) |
|
|
195
|
+
| 读文本 | `readText(target, signal?): Promise<string>`(222) |
|
|
196
|
+
| 流式读文本 | `streamText(target, signal?): Promise<AsyncIterable<string>>`(233) |
|
|
197
|
+
| 读字节 | `readBytes(target, signal, maxBytes): Promise<Uint8Array>`(245) |
|
|
198
|
+
| 读字节区间 | `readByteRange(target, {offset,length}, signal?)`(260) |
|
|
199
|
+
| 列目录 | `listDir(target, signal?): Promise<FsDirEntry[]>`(269) |
|
|
200
|
+
|
|
201
|
+
`FsTarget` / `FsInfo` / `FsDirEntry`:`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-fs/src/types.ts:60-115`:
|
|
202
|
+
|
|
203
|
+
- `FsTarget { targetKey, displayPath }`
|
|
204
|
+
- `FsInfo { version, type: 'file'|'directory'|'other', size? }`
|
|
205
|
+
- `FsDirEntry { name, type, target, version?, size? }`,`listDir` 只返回**直接子项**,按稳定名字序,不读内容。
|
|
206
|
+
|
|
207
|
+
**没有 glob**:`FileSystem` 的完整方法表不含 glob(§1.1 运行时清单与源码方法表一致)。文件发现请用 §3.4。
|
|
208
|
+
|
|
209
|
+
### 3.2 读语义(来自 `dsh-fs-local`)
|
|
210
|
+
|
|
211
|
+
- `readText` 读**整个文件**,没有行窗口、没有内置大小上限、没有 maxBytes 参数(`.../dsh-fs-local/src/fsio.ts:3` 注释:`line windows belong to dsh-tool-fs`)。
|
|
212
|
+
- `readBytes(..., maxBytes)` 与 `readByteRange` 可以限字节。
|
|
213
|
+
- 读文本前会检查:必须是普通文件、前若干字节不能含 NUL、必须是合法 UTF-8;否则 `FS_NOT_TEXT`(`.../dsh-fs-local/src/fsio.ts:395-403`;`348` 行是非法 UTF-8 的 `FsError('cannot ...: invalid UTF-8 text','FS_NOT_TEXT')`)。
|
|
214
|
+
- 超 `maxBytes` 抛 `FS_TOO_LARGE`(`.../dsh-fs-local/src/fsio.ts:423-437`,消息含 `${info.size} bytes exceeds the ${maxBytes}-byte limit`)。
|
|
215
|
+
|
|
216
|
+
### 3.3 错误形状
|
|
217
|
+
|
|
218
|
+
- `FsError extends HarnessError`,带 `code: FsErrorCode`(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-fs/src/types.ts:218-226`)。
|
|
219
|
+
- 错误码全集(`.../dsh-fs/src/types.ts:190-207`):
|
|
220
|
+
`FS_NOT_FOUND`、`FS_NOT_DIRECTORY`、`FS_NOT_TEXT`、`FS_NOT_REGULAR_FILE`、`FS_TOO_LARGE`、`FS_PERMISSION_DENIED`、`FS_SANDBOX_DENIED`、`FS_IO_ERROR`、`FS_STALE_VERSION`、`FS_NOT_OBSERVED`、`FS_AMBIGUOUS_EDIT`、`FS_EDIT_NOT_FOUND`、`FS_ABORTED`、`FS_PATCH_MISMATCH`、`FS_SAME_TARGET`、`FS_ALREADY_EXISTS`。
|
|
221
|
+
- 例:`cannot read "<displayPath>": binary file`(`FS_NOT_TEXT`)。
|
|
222
|
+
- 越界(写):`cannot write "<path>": file access denied under read-only mode` / `... under workspace-write mode`,code `FS_SANDBOX_DENIED`(`.../dsh-fs-sandbox/src/index.ts:156-178`)。
|
|
223
|
+
|
|
224
|
+
### 3.4 `sandboxPolicy` 只围栏「写」,不围栏「读」
|
|
225
|
+
|
|
226
|
+
`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-fs-sandbox/src/index.ts:1-27` 模块头原文:
|
|
227
|
+
|
|
228
|
+
> Reads pass through untouched: every mode permits reading.
|
|
229
|
+
|
|
230
|
+
- `SandboxedFileSystem extends LocalFileSystem`(`.../dsh-fs-sandbox/src/index.ts`,`static inject = ['sandboxPolicy']`),只有五个写操作会调用 `checkedTarget()`;`danger-full-access` 直接放行,`read-only` 抛 `FS_SANDBOX_DENIED`,`workspace-write` 用 `fs.contains` 检查 `writableRoots`。
|
|
231
|
+
- `SandboxMode = 'read-only' | 'workspace-write' | 'danger-full-access'`(`.../dsh-sandbox/src/index.ts:38`);`SandboxExecutionPolicy { mode, workspaceRoot, additionalWritableRoots?, sessionId? }`(`.../dsh-sandbox/src/index.ts:48`)。
|
|
232
|
+
- `SandboxPolicyService.resolve({session?, mode?})`(`.../dsh-sandbox-policy/src/index.ts:208-220`)返回上面的 policy;`workspaceRoot` 是**公共只读属性**(`src/index.ts:150`),默认 `config.workspaceRoot ?? process.cwd()`(`164-168`);`sessionWorkspaceRoot()`(`222-230`)优先 `session.header.cwd`,缺省回退默认根,相对路径/含 NUL 抛 `SANDBOX_INVALID_ROOT`。
|
|
233
|
+
- 默认 mode 是 `read-only`(config 注释与默认值,`.../dsh-sandbox-policy/src/index.ts:95-112`)。
|
|
234
|
+
|
|
235
|
+
因此**对读路径**:
|
|
236
|
+
|
|
237
|
+
- `ctx.get('sandboxPolicy')` 不是必需依赖;它给的是「写的边界」和「无 cwd 时的默认根」,可以作为 `workspaceRoot` 的兜底。
|
|
238
|
+
- 读操作在任何 mode 下都能读工作区外文件(见 §6.3),**插件必须自己做 containment 才能声称「工作区内」**。
|
|
239
|
+
|
|
240
|
+
### 3.5 glob 的替代
|
|
241
|
+
|
|
242
|
+
- 没有 `FileSystem.glob`。
|
|
243
|
+
- 文件发现用 `workspaceFiles.search(scope, request, signal)`(§4/§5):底层 packaged `rg --files`,支持 `query` 子串、`includeHidden`、`respectGitignore`、`excludedPaths`,返回工作区相对路径;上限 `maxResults=512`、`maxEntries=100000`、超时 30s(`.../dsh-api-workspace-files/src/search.ts:55-124`、`Config` 默认值 `src/index.ts:192-204`)。
|
|
244
|
+
- 若只需要列一层:`workspaceFiles.list` 或 `fs.listDir`。
|
|
245
|
+
- `fileReferences.list(agent, query, signal)` 是给 `@` 补全用的候选(`{path, kind}`),也可用于「文件感知」的候选发现,但需要 Agent。
|
|
246
|
+
|
|
247
|
+
### 3.6 本节结论
|
|
248
|
+
|
|
249
|
+
- **可用**:`fs.resolve/stat/readText/readBytes/listDir/contains`;`sandboxPolicy.resolve/workspaceRoot`。
|
|
250
|
+
- **不可用**:`fs` 的 glob(不存在);行窗口(不存在,需自己切)。
|
|
251
|
+
- **需降级**:`fs.readText` 没有上限和行号,插件必须 `stat` 先看 `size`,再决定 `readText` 还是 `readByteRange`/`workspaceFiles.read`;二进制/超大文件必须拒绝或截断并标注。
|
|
252
|
+
|
|
253
|
+
## 4. `/` 命令描述的权威来源
|
|
254
|
+
|
|
255
|
+
### 4.1 Host:`commands.list(agent)`,字段只有 name/description/input.hint
|
|
256
|
+
|
|
257
|
+
`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-commands/src/index.ts:314-320`:
|
|
258
|
+
|
|
259
|
+
```ts
|
|
260
|
+
@Remote list(agent: Agent): readonly CommandDescriptor[]
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
`CommandRuntime` 是 `TypertRemoteService`,`super(ctx, 'commands')`(`.../dsh-commands/src/index.ts:263,277`);`Context.commands` 声明在 `113-117`。
|
|
264
|
+
|
|
265
|
+
`CommandDescriptor`(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-commands/src/types.ts:57-66`):
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
export interface CommandDescriptor {
|
|
269
|
+
readonly definitionId?: string
|
|
270
|
+
readonly name: string // 小写、无斜杠
|
|
271
|
+
readonly description: string // 必填、非空
|
|
272
|
+
readonly input?: {
|
|
273
|
+
readonly hint: string
|
|
274
|
+
readonly attachments?: boolean
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
即:**返回字段就是 `definitionId?` / `name` / `description` / `input?.hint` / `input?.attachments`;没有 title、没有顶层 hint、没有参数枚举**。`find(agent, name)`(`.../dsh-commands/src/index.ts:328-330`)可按名字取单个。
|
|
280
|
+
|
|
281
|
+
Host 侧调用必须先拿到 `Agent`(`list` 的参数不是 sessionId):
|
|
282
|
+
|
|
283
|
+
```js
|
|
284
|
+
const agent = ctx.get('agents')?.get(sessionId) // Agent | undefined
|
|
285
|
+
const descriptors = agent && ctx.get('commands')
|
|
286
|
+
? ctx.get('commands').list(agent) // readonly CommandDescriptor[]
|
|
287
|
+
: []
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
- `commands.list` 是同步方法(`@Remote` 只影响序列化,Host 直调返回数组)。
|
|
291
|
+
- `agents.get(sessionId)` 只对 live/已加载 Agent 有效;持久化但未加载的会话没有 Agent,此时命令目录不可得(**降级**:不显示描述,或只显示名字)。
|
|
292
|
+
- 生成代码确认 wire 参数名是 `agentId`、lookup 是 `agent`(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-commands/lib/typert.remote-client.js`,`commands/list` 描述符 `scope: { context: 'agent', wire: 'agentId' }`)。
|
|
293
|
+
|
|
294
|
+
结论:**可用**,但依赖 `agents.get(sessionId)` 能返回 Agent。
|
|
295
|
+
|
|
296
|
+
### 4.2 Client:`ctx.remote.commands.list(sessionId)`,返回 `{ok, value}`
|
|
297
|
+
|
|
298
|
+
`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-client-ui-commands/src/client/service.ts:97-111` 是官方用法:
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
const result = await ctx.remote.commands.list(sessionId)
|
|
302
|
+
if (!result.ok) throw new Error(`command.list failed: ${result.error.code}: ${result.error.message}`)
|
|
303
|
+
return result.value
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
- 客户端 `remote` 服务由 `dsh-api-gateway` 注入(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-api-gateway/src/client/index.ts:103,127-132`,`ClientRemote extends TypertClientRemote`),`commands` 命名空间由 `dsh-api-remotes/src/client/index.ts:200` 注册进 assembly。
|
|
307
|
+
- 客户端插件要调用它,需要 `inject: ['remote', 'remote.commands']`(官方 `CommandUiRuntime` 就是 `static inject = ['inputTriggers','sessions','remote','remote.commands']`,`.../dsh-client-ui-commands/src/client/service.ts:76-77`),并且 assembly 必须选中该命名空间;取不到时返回 `undefined`,插件应降级。
|
|
308
|
+
- 客户端拿到的数据形状与 Host 相同的 descriptor 数组(同一 typert schema)。
|
|
309
|
+
- **未找到**客户端 `commandUi` 的公开「读取目录」API:`CommandUiContract` 只有 `register/decorate/dismiss/popupFor`(`.../dsh-client-ui-commands/src/client/contract.ts:135-150`),`directory` 是 `CommandUiRuntime` 私有字段。所以想显示描述必须自己走 `ctx.remote.commands.list`,或让 Host 代查后随响应返回。
|
|
310
|
+
|
|
311
|
+
### 4.3 命令 chip 的 `claim.token` 能否还原命令名
|
|
312
|
+
|
|
313
|
+
- 发送/claimed 阶段,编辑器状态里**同时有 name 和 token**:
|
|
314
|
+
|
|
315
|
+
`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-client-ui-conversation/src/client/contract/input.ts:305-326`:
|
|
316
|
+
|
|
317
|
+
```ts
|
|
318
|
+
export interface InputState {
|
|
319
|
+
readonly phase: 'plain' | 'adjudicating' | 'claimed' | 'submitting' | 'sending'
|
|
320
|
+
readonly claim?: {
|
|
321
|
+
readonly name: string // catalog 名,无斜杠
|
|
322
|
+
readonly token: string // 本地化拼写 + 参数分隔空格,例如 "/goal "
|
|
323
|
+
readonly hint?: string
|
|
324
|
+
readonly attachments?: boolean
|
|
325
|
+
}
|
|
326
|
+
...
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
因此**首选**:直接从 `s.claim.name` 拿命令名,不要解析 token。
|
|
331
|
+
|
|
332
|
+
- 持久化的草稿 chip **只存 token**:`.../dsh-client-ui-conversation/src/client/contract/draft-editor.ts:41-44` 的 `DraftCommand { token: '/goal ' }`(含分隔空格,不含 name),恢复时由输入管线 `resolveClaim(token)` 重建 claim(同文件 36-44 注释)。
|
|
333
|
+
|
|
334
|
+
- token 可能是**本地化词**,不是命令名:`.../dsh-client-ui-commands/src/client/resolution.ts:12-15` 中,内建命令(有 `builtinCommandName`)返回 `t('token.<name>')`;本地化表里 `zh` 的 `token.goal = '目标'` 等(`.../dsh-client-ui-commands/src/client/locales.ts:26-31`)。也就是 `/目标` 能解析到 `goal`,但字符串层面无法可靠反推。
|
|
335
|
+
|
|
336
|
+
- 要还原,走输入管线的解析:`.../dsh-client-ui-input-trigger/src/client/controller.ts:400-416` 的 `resolveClaim(token, signal)` 会轮询各 source 的 `matchClaim(session, token, signal)`,首个非 `undefined` 的 `CommandClaim` 胜出(`.../dsh-client-ui-input-trigger/src/types.ts:201-208`)。`CommandClaim` 定义在 `.../dsh-client-ui-conversation/src/client/contract/input.ts:40-55`,其中 `name` 是目录名、`token` 是拼写。
|
|
337
|
+
- 访问 controller:公共契约 `ctx.inputTriggers` 只暴露 `registerSource` 和 `sessionOf(actx)`(`.../dsh-client-ui-input-trigger/src/client/contract.ts:12-25`),`sessionOf` 需要 `actx`。**未找到**不经过 `actx` 直接解析 token 的公开 API。
|
|
338
|
+
|
|
339
|
+
结论:**可用但有限**——有 `InputState.claim` 时直接用 `name`;只有 token 时必须经 `resolveClaim`(需要 `actx` 或自己注册的 source 回调),不能字符串硬拆。
|
|
340
|
+
|
|
341
|
+
### 4.4 Client 侧如何拿到 `sessionId`(关键缺口)
|
|
342
|
+
|
|
343
|
+
- `conversation.input.left` 是 `{ kind: 'list'; scope: 'session' }`,**没有 owner props**(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-client-ui-conversation/src/client/contract/slots.ts:320`)。
|
|
344
|
+
- session 级 slot 的标准 props 只有 `useConversation` / `useInput` / `inputActions`(`.../dsh-client-ui-conversation/src/client/contract/slots.ts:395-402`),**未找到 sessionId 字段**。
|
|
345
|
+
- `ConversationSnapshot` 只有 `views` / `activeTargets`(`.../dsh-client-ui-conversation/src/client/contract/snapshot.ts:6-9`);`InputState` 也没有 sessionId(§4.3)。
|
|
346
|
+
- 客户端 `ctx.sessions.list` 能枚举会话与 `cwd`(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-api-session-controller/src/client/sessions/service.ts:27-34,61-70`),但**没有「当前会话」指针**。
|
|
347
|
+
- 可行捕获点:`InputTriggerSource` 的每个回调都收到 `ClientSessionContext { readonly sessionId: SessionId }`(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-client-ui-input-trigger/src/types.ts:23-31`),并且 `warm?(session)` 在 session scope 建立时被调用一次(`.../dsh-client-ui-input-trigger/src/types.ts:209-214`)。
|
|
348
|
+
- 因此可注册一个**被动** source(`trigger: '/'`、`name` 唯一、`candidates` 返回 `[]`、只实现 `warm(session)` 记录 sessionId),不实现 `matchSpace/matchEnter` 就不会参与裁决。**代价**:它进入 `/` 的 source 注册表,菜单多一个空分组;需评估 UX。属「可行但需验证」的方案。
|
|
349
|
+
- 另一条:Host 端降级为「最近会话」。`sessions.list()` 只返回 live 且是创建序(`.../dsh-session/src/index.ts:1463-1465`),**不是活跃序**;只能算 best-effort,不能保证就是用户当前会话。
|
|
350
|
+
|
|
351
|
+
结论:**Client 不能从现有 slot props 直接获得 sessionId;必须新增捕获机制(被动 input source)或接受 Host 的 best-effort 降级。**
|
|
352
|
+
|
|
353
|
+
### 4.5 本节最小序列
|
|
354
|
+
|
|
355
|
+
Host:
|
|
356
|
+
|
|
357
|
+
```js
|
|
358
|
+
function commandCatalog(ctx, sessionId) {
|
|
359
|
+
const agent = ctx.get('agents')?.get(sessionId)
|
|
360
|
+
if (!agent) return []
|
|
361
|
+
return ctx.get('commands')?.list(agent) ?? []
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
Client(有 sessionId 时):
|
|
366
|
+
|
|
367
|
+
```js
|
|
368
|
+
const remote = ctx.get('remote')
|
|
369
|
+
const result = await remote.commands.list(sessionId)
|
|
370
|
+
const descriptors = result.ok ? result.value : []
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## 5. 只读保证与读取限制
|
|
374
|
+
|
|
375
|
+
### 5.1 只读面
|
|
376
|
+
|
|
377
|
+
- `workspaceFiles` 模块头明确「exposes no mutations」(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-api-workspace-files/src/index.ts:1-21`),类的公开方法只有 `read/readBytes/stat/list/search/changes`(Cordis Inspect 与源码一致)。**这条路径本身不可写。**
|
|
378
|
+
- `fs` 路径上有写方法(`writeText/editText/patchText/deleteFile/moveFile`),但本插件**只调用** `resolve/stat/lstat/readText/readBytes/readByteRange/listDir/contains`。建议在代码里把允许的方法白名单化,并在测试中断言写方法零调用。
|
|
379
|
+
- 只读审计建议记录:`sessionId`、`workspaceRoot`、工作区相对路径、`absolutePath`(read/stat 返回)、`offset/limit`、返回 `version`/`bytes`、是否 `eof`/`truncated`。`workspaceFiles.read` 返回 `{ absolutePath, version, bytes?, offset, text, lines, eof }`(`.../dsh-api-workspace-files/src/types.ts:52-65,25-38`),足以做审计且不暴露额外信息。
|
|
380
|
+
|
|
381
|
+
### 5.2 读取上限(超限是拒绝,不是截断)
|
|
382
|
+
|
|
383
|
+
`workspaceFiles` 的 Config 默认值(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-api-workspace-files/src/index.ts:74-91,192-204`):
|
|
384
|
+
|
|
385
|
+
| 设置 | 默认 | 行为 |
|
|
386
|
+
| --- | --- | --- |
|
|
387
|
+
| `maxLines` | 5000 | 也是**硬上限**:`limit > maxLines` → `RemoteError 'gateway/bad-request'`(`index.ts:416-423`) |
|
|
388
|
+
| `maxBytes` | 2 MiB | 单页/字节窗上限;超过 → `workspace-file/too-large`(`index.ts:426-440`) |
|
|
389
|
+
| `maxFileBytes` | 32 MiB | 无 range 的 `readBytes` 上限 |
|
|
390
|
+
| `maxEntries` | 2000 | 目录列表上限 |
|
|
391
|
+
| `search.maxResults` | 512 | 搜索结果截断标志 `truncated` |
|
|
392
|
+
| `search.maxEntries` | 100000 | 超过或 rg exit 2 → `incomplete` |
|
|
393
|
+
| `search.graceMs/timeoutMs` | 3000 / 30000 | 超时 → `workspace-file/search-failed` |
|
|
394
|
+
| `search.stderrMaxBytes` | 65536 | — |
|
|
395
|
+
|
|
396
|
+
注意:
|
|
397
|
+
|
|
398
|
+
- 这些是**插件配置默认值**,运行时可能被宿主 profile 覆盖;插件不应假设一定是这个数字,而应把自己的请求限制在更小的值,并处理 `gateway/bad-request`。
|
|
399
|
+
- `workspaceFiles.read` 的 `offset` 是 **1 起始行号**,`limit` 默认 `maxLines`;返回 `lines` 是页内行数、`eof` 表示文件结束(`index.ts:404-423`)。
|
|
400
|
+
- `workspaceFiles.search` 是「文件名子串」搜索,不是 glob,也不是内容搜索(`search.ts:55-124`)。
|
|
401
|
+
- `fs.readText` **没有任何上限**;必须自己 `stat` 后判断 `size`,或者改用 `workspaceFiles.read`。`fs.readBytes(target, signal, maxBytes)` 有上限。
|
|
402
|
+
|
|
403
|
+
建议本插件的参数化限制(可在设置里暴露,默认值取保守值):
|
|
404
|
+
|
|
405
|
+
```js
|
|
406
|
+
const LIMITS = {
|
|
407
|
+
maxFiles: 8,
|
|
408
|
+
maxLinesPerFile: 200, // ≤ 5000
|
|
409
|
+
maxBytesPerFile: 64 * 1024, // ≤ 2 MiB
|
|
410
|
+
maxTotalBytes: 256 * 1024,
|
|
411
|
+
maxDirEntries: 100, // ≤ 2000
|
|
412
|
+
maxSearchResults: 50, // ≤ 512
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
超限一律「拒绝并说明」,不要静默截断;截断只允许在搜索结果(DSH 自带 `truncated` 标志)时附带说明。
|
|
417
|
+
|
|
418
|
+
### 5.3 二进制 / 文本拒绝
|
|
419
|
+
|
|
420
|
+
- `workspaceFiles.read`:按页解码,**fatal UTF-8**,非法字节 → `workspace-file/not-text`;页内含 NUL → `workspace-file/not-text`(`.../dsh-api-workspace-files/src/index.ts:246-260`;`types.ts:206-230`)。
|
|
421
|
+
- `fs.readText`:非普通文件 → `FS_NOT_REGULAR_FILE`;前若干字节含 NUL / 非法 UTF-8 → `FS_NOT_TEXT`(`.../dsh-fs-local/src/fsio.ts:395-403`)。
|
|
422
|
+
- `workspaceFiles.readBytes`:**不解码、不拒二进制**(`.../dsh-api-workspace-files/src/types.ts:87-94` 注释);如果 UI 需要展示二进制字节,应明确标注。
|
|
423
|
+
- 因此「二进制拒绝」在文本读路径上是 DSH 强制的,插件不需要(也不应该)自己写二进制探测;但要处理错误码并给用户可读提示。
|
|
424
|
+
|
|
425
|
+
### 5.4 本节结论
|
|
426
|
+
|
|
427
|
+
- **可用**:`workspaceFiles` 全只读面 + 内建上限;`fs` 只读方法。
|
|
428
|
+
- **需自建**:行窗口(`workspaceFiles.read` 已提供)、总预算、文件数、每文件行数、插件侧参数化限制。
|
|
429
|
+
- **禁止**:调用任何 fs 写方法;不要依赖 `sandboxPolicy` 来限制读。
|
|
430
|
+
|
|
431
|
+
## 6. 安全边界:路径规范化、符号链接、工作区外
|
|
432
|
+
|
|
433
|
+
### 6.1 路径规范化(`fs-local`)
|
|
434
|
+
|
|
435
|
+
- `LocalTarget`(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-fs-local/src/fsio.ts:110-116`):
|
|
436
|
+
- `displayPath` = 绝对拼写(不解析符号链接,用于显示/审计);
|
|
437
|
+
- `targetKey` = `realpath` 身份(I/O 与稳定键)。
|
|
438
|
+
- `expandHomePrefix`(`fsio.ts:150-154`):`~` / `~/`(Windows 还有 `~\`)替换为 `homedir()`;`~user` 保持字面。
|
|
439
|
+
- `localDisplayPath`(`fsio.ts:162-168`):相对路径以 `cwd` 拼接;含 `..` 的 POSIX 保留物理拼写,Windows 用 `path.resolve`。
|
|
440
|
+
- `resolveLocalTarget`(`fsio.ts:178-227`):
|
|
441
|
+
- 空/全空白 → `FsError('file_path must be a non-empty string','FS_NOT_FOUND')`;
|
|
442
|
+
- 先 `realpath(displayPath)`,**解析末段符号链接**;不存在时从最近存在祖先 `realpath` 再拼缺失后缀;
|
|
443
|
+
- `ENOTDIR` → `FS_NOT_FOUND`("a parent path segment is not a directory");
|
|
444
|
+
- `..` 跨越缺失目录 → `FS_NOT_FOUND`。
|
|
445
|
+
- `fs.resolve(path, {cwd})` 就是 `resolveLocalTarget(cwd, expandHomePrefix(path))`(`.../dsh-fs-local/src/index.ts:147-150`),默认 cwd 是 `process.cwd()`(`99` 行)。
|
|
446
|
+
|
|
447
|
+
### 6.2 符号链接
|
|
448
|
+
|
|
449
|
+
- **`workspaceFiles.read` / `stat` 拒绝末段符号链接**:`locateFile()` 先 `lstat` 路径本身,`entry.type !== 'file'` → `workspace-file/not-regular-file`(kind 可能是 `directory`/`symlink`/`other`),再 `resolve+stat` 复核 `type === 'file'`(`.../dsh-api-workspace-files/src/index.ts:475-493`)。
|
|
450
|
+
- **`workspaceFiles.list` 允许末段是「指向目录的符号链接」**:先 `lstat` 看类型,symlink 时再 `stat` 确认是目录;随后 `confine()` 保证解析后的目标仍在工作区内(`index.ts:316-342,462-468`)。
|
|
451
|
+
- **`fs.readText` 会跟随末段符号链接**(`statRegularFile` 用 `stat`,`.../dsh-fs-local/src/fsio.ts:376-386`),所以若走 fs 直读,必须自己 `lstat`/`contains` 复核。
|
|
452
|
+
- 写路径 `workspace-write` 会重新 `resolve`(realpath 最深已存在祖先)以防被换的 symlink 祖先,注释承认仍有 TOCTOU 残余(`.../dsh-fs-sandbox/src/index.ts:15-18,156-178`)。读路径没有这层保护。
|
|
453
|
+
|
|
454
|
+
### 6.3 工作区外读取
|
|
455
|
+
|
|
456
|
+
- **`workspaceFiles.read` / `stat` 不围栏**:模块头原文「File reads follow the composed filesystem's read access, including paths outside the workspace ... not a read-containment restriction」(`.../dsh-api-workspace-files/src/index.ts:1-21`)。也就是说,传绝对路径或 `../` 可以读到工作区外(受 OS 权限限制)。
|
|
457
|
+
- **`workspaceFiles.list` / `search` / `changes` 围栏**:`confine()` 用 `fs.resolve` + `fs.contains(root,target)`,不满足 → `workspace-file/outside-workspace`(`index.ts:462-468`);`search` 的 root 固定为 `scope.workspaceRoot`(`index.ts:358-388`)。
|
|
458
|
+
- **`fs` 本身完全不围栏**:读工作区外是允许的(`fs-sandbox` 只拦写,§3.4)。
|
|
459
|
+
- 因此,若产品承诺「工作区/文件感知且只读、可审计」,**插件必须自己实现工作区围栏**:
|
|
460
|
+
1. `const root = await fs.resolve(workspaceRoot)`;
|
|
461
|
+
2. 对每个待读路径 `const target = await fs.resolve(path, { cwd: workspaceRoot })`;
|
|
462
|
+
3. `if (!fs.contains(root, target)) return { code: 'OUTSIDE_WORKSPACE' }`;
|
|
463
|
+
4. 或者只用 `workspaceFiles.list/search`(已围栏)+ `workspaceFiles.read`(自己做第 3 步)。
|
|
464
|
+
- `workspaceFiles.relativePath` 会拒绝绝对路径、NUL、以 `/` 或 URL scheme 开头的路径(`index.ts:404-413`),但**不拒绝 `..`**;围栏必须靠 `contains`。
|
|
465
|
+
|
|
466
|
+
### 6.4 Windows 特有
|
|
467
|
+
|
|
468
|
+
- `isPathUnder(path, root, caseSensitive = process.platform !== 'win32')`(`D:/dsh/resources/app.asar/dsh/node_modules/@deepseek-ai/dsh-fs-sandbox/src/containment.ts:58-76`):先词法比较(Windows 小写化),否则 `stat` root 与逐级祖先比较 `dev+ino`,支持 8.3 短名/大小写别名;root 不存在返回 `false`。
|
|
469
|
+
- 插件若自己写 `startsWith` 判断,会被 `C:\foo` vs `c:\foo`、`..`、8.3 短名绕过;**应复用 `fs.contains`**(它是 `FsTarget` 身份判断,已处理 realpath 与平台差异)。
|
|
470
|
+
|
|
471
|
+
### 6.5 本节结论
|
|
472
|
+
|
|
473
|
+
- **可用**:`fs.contains` + `fs.resolve` 做围栏;`workspaceFiles.list/search` 自带围栏。
|
|
474
|
+
- **必须自建**:读路径的工作区围栏(因为 read 有意允许工作区外)。
|
|
475
|
+
- **注意**:`workspaceFiles.read` 拒绝末段 symlink,`fs.readText` 跟随末段 symlink;两种路径行为不同,审计输出中应标注实际 `absolutePath`。
|
|
476
|
+
|
|
477
|
+
## 7. 建议的最小可行调用序列(伪代码)
|
|
478
|
+
|
|
479
|
+
### 7.1 Host:解析会话与工作区根
|
|
480
|
+
|
|
481
|
+
```js
|
|
482
|
+
// POST /api/prompt-enhance/context { sessionId?, op, path?, query?, offset?, limit? }
|
|
483
|
+
async function resolveScope(ctx, sessionId, signal) {
|
|
484
|
+
const sessions = ctx.get('sessions')
|
|
485
|
+
const live = sessionId ? sessions?.get(sessionId)?.header : undefined
|
|
486
|
+
const header = live
|
|
487
|
+
?? (sessionId ? (await ctx.get('sessionPersistence')?.stat(sessionId, { signal }))?.header : undefined)
|
|
488
|
+
if (!header) return { ok: false, code: 'SESSION_NOT_FOUND' }
|
|
489
|
+
|
|
490
|
+
const root = header.cwd
|
|
491
|
+
?? ctx.get('workspaceRegistry')?.list().find((w) => w.sessionIds.includes(sessionId))?.path
|
|
492
|
+
?? ctx.get('sandboxPolicy')?.workspaceRoot
|
|
493
|
+
if (!root) return { ok: false, code: 'WORKSPACE_NOT_FOUND' }
|
|
494
|
+
|
|
495
|
+
return { ok: true, scope: { sessionId, workspaceRoot: root }, source: live ? 'live' : 'persisted' }
|
|
496
|
+
}
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
### 7.2 Host:读 / 列 / 搜(只读)
|
|
500
|
+
|
|
501
|
+
```js
|
|
502
|
+
const wf = ctx.get('workspaceFiles')
|
|
503
|
+
if (!wf) return degradeToFs(ctx, scope, request)
|
|
504
|
+
|
|
505
|
+
switch (request.op) {
|
|
506
|
+
case 'read': {
|
|
507
|
+
// 相对路径;服务内部会以 scope.workspaceRoot 为 cwd
|
|
508
|
+
const page = await wf.read(scope, request.path, {
|
|
509
|
+
offset: clamp(request.offset ?? 1, 1, 5000),
|
|
510
|
+
limit: clamp(request.limit ?? 200, 1, 200), // 插件自己的参数化限制
|
|
511
|
+
}, request.signal)
|
|
512
|
+
// page: { absolutePath, version, bytes?, offset, text, lines, eof }
|
|
513
|
+
return { ok: true, page, audit: { sessionId: scope.sessionId, root: scope.workspaceRoot } }
|
|
514
|
+
}
|
|
515
|
+
case 'stat': return { ok: true, stat: await wf.stat(scope, request.path, request.signal) }
|
|
516
|
+
case 'list': return { ok: true, listing: await wf.list(scope, request.path ?? '.', request.signal) }
|
|
517
|
+
case 'search': return { ok: true, result: await wf.search(scope, {
|
|
518
|
+
query: request.query,
|
|
519
|
+
excludedPaths: request.excludedPaths ?? [],
|
|
520
|
+
includeHidden: false,
|
|
521
|
+
respectGitignore: true,
|
|
522
|
+
}, request.signal) }
|
|
523
|
+
}
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
错误映射:`error.code`(`workspace-file/not-found`、`workspace-file/outside-workspace`、`workspace-file/too-large`、`workspace-file/not-text`、`workspace-file/not-regular-file`、`workspace-file/not-directory`、`workspace-file/search-failed`、`gateway/bad-request`)直接透传为 HTTP 4xx/422,并附 `message`。
|
|
527
|
+
|
|
528
|
+
`degradeToFs`:
|
|
529
|
+
|
|
530
|
+
```js
|
|
531
|
+
async function degradeToFs(ctx, scope, request) {
|
|
532
|
+
const fs = ctx.get('fs')
|
|
533
|
+
if (!fs) return { ok: false, code: 'NO_FS' }
|
|
534
|
+
const root = await fs.resolve(scope.workspaceRoot)
|
|
535
|
+
const target = await fs.resolve(request.path, { cwd: scope.workspaceRoot })
|
|
536
|
+
if (!fs.contains(root, target)) return { ok: false, code: 'OUTSIDE_WORKSPACE' }
|
|
537
|
+
const info = await fs.stat(target)
|
|
538
|
+
if (!info) return { ok: false, code: 'NOT_FOUND' }
|
|
539
|
+
if (info.type !== 'file') return { ok: false, code: 'NOT_FILE' }
|
|
540
|
+
if (info.size > LIMITS.maxBytesPerFile) return { ok: false, code: 'TOO_LARGE' }
|
|
541
|
+
const text = await fs.readText(target) // 无行窗口,需插件自己切
|
|
542
|
+
return { ok: true, page: sliceLines(text, request.offset, request.limit) }
|
|
543
|
+
}
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
### 7.3 Host:命令描述
|
|
547
|
+
|
|
548
|
+
```js
|
|
549
|
+
function commandCatalog(ctx, sessionId) {
|
|
550
|
+
const agent = ctx.get('agents')?.get(sessionId)
|
|
551
|
+
if (!agent) return []
|
|
552
|
+
return (ctx.get('commands')?.list(agent) ?? []).map((d) => ({
|
|
553
|
+
name: d.name,
|
|
554
|
+
description: d.description,
|
|
555
|
+
hint: d.input?.hint,
|
|
556
|
+
attachments: d.input?.attachments ?? false,
|
|
557
|
+
}))
|
|
558
|
+
}
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
### 7.4 Client:现有 slot 里能做什么
|
|
562
|
+
|
|
563
|
+
```js
|
|
564
|
+
// 现有:conversation.input.left
|
|
565
|
+
const claim = useInput((s) => s.claim) // { name, token, hint?, attachments? } | undefined
|
|
566
|
+
const draft = useInput((s) => s.draft)
|
|
567
|
+
|
|
568
|
+
// 需要 sessionId 时(二选一):
|
|
569
|
+
// A. 被动 input source 捕获(warm(session) 拿到 ClientSessionContext.sessionId)
|
|
570
|
+
// B. 不传 sessionId,Host 用 best-effort(最近会话)
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
调用 Host:
|
|
574
|
+
|
|
575
|
+
```js
|
|
576
|
+
const res = await fetch('/api/prompt-enhance/context', {
|
|
577
|
+
method: 'POST',
|
|
578
|
+
headers: { 'content-type': 'application/json' },
|
|
579
|
+
body: JSON.stringify({ sessionId, op: 'read', path: 'src/index.ts', offset: 1, limit: 120 }),
|
|
580
|
+
})
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
悬浮位置提示(hover)不需要宿主 API:`conversation.input.overlay` 是 session 级 list slot,适合渲染悬浮层(`.../dsh-client-ui-conversation/src/client/contract/slots.ts:306-310`);位置用编辑器 `selectionStart`/实际 DOM 计算。本仓库现有 slot 是 `conversation.input.left`(`D:/prompt_enhance/client.js` 的 `SLOT` 常量与 `ctx.slots.register`)。
|
|
584
|
+
|
|
585
|
+
## 8. 降级矩阵
|
|
586
|
+
|
|
587
|
+
| 情况 | 降级 |
|
|
588
|
+
| --- | --- |
|
|
589
|
+
| `ctx.get('workspaceFiles')` 缺失 | 用 `ctx.get('fs')` 的 `resolve/stat/readText` + 自建行窗口;列表用 `listDir`;无搜索(可用 `fileReferences` 或直接拒绝) |
|
|
590
|
+
| `ctx.get('sessions')` 缺失 | 无法解析 session 根;返回明确错误(不要猜 process.cwd) |
|
|
591
|
+
| 会话非 live 且 `sessionPersistence` 缺失 | 用 `workspaceRegistry.list()` 的 `sessionIds`(仅 live,通常也拿不到);否则报错 |
|
|
592
|
+
| `header.cwd` 缺失(blank session) | `sandboxPolicy.workspaceRoot` 兜底,并在 UI 标注「部署默认根,非会话根」 |
|
|
593
|
+
| 无 `sessionId` | Host 选 `sessions.list()` 中最后一个(创建序,**不是活跃序**);UI 必须允许用户手动选/填工作区;不得假装准确 |
|
|
594
|
+
| `agents.get(sessionId)` 无 Agent | 命令描述为空,只显示名字(若有) |
|
|
595
|
+
| 无 `remote.commands` 命名空间 | 命令描述为空;不要从 token 猜 |
|
|
596
|
+
| 只有 chip token | 用 `claim.name`(有 claim 时)或 `resolveClaim`(需 actx/source);**禁止**字符串硬拆本地化 token |
|
|
597
|
+
| 读路径无行窗口(fs 降级) | 自己按 `\n` 切页,注意 CRLF 与 `\r\n`,并保持 `offset` 语义 |
|
|
598
|
+
| 文件是二进制/非法 UTF-8 | 返回 `not-text`,UI 显示「二进制文件,未读取」;不要用 `readBytes` 直接塞进 prompt |
|
|
599
|
+
|
|
600
|
+
## 9. 未找到 / 未验证项(明确记录)
|
|
601
|
+
|
|
602
|
+
- **未找到** `workspaceRegistry` 按 `SessionId` 直接查询的方法(只有 `get(WorkspaceId)`、`list()`、`resolveByPath(path)`)。
|
|
603
|
+
- **未找到** `FileSystem` 的 glob 方法(`FileSystem` 完整方法表见 §3.1)。
|
|
604
|
+
- **未找到** Client slot props 中的 `sessionId`(`slots.ts:320,395-402`)。
|
|
605
|
+
- **未找到** `commandUi` 的公开命令目录读取 API(只有 register/decorate/dismiss/popupFor)。
|
|
606
|
+
- **未找到** 不经过 `actx` 就能把 chip token 解析为命令名的公开 API;`resolveClaim` 需要 `InputTriggerController`。
|
|
607
|
+
- **未找到** Host 插件可直接调用的 `workspaceFileScope` typert lookup(`typert` 只有 schema 注册表;lookup 在 `WorkspaceFiles` 构造内部注册给 wire 用)。
|
|
608
|
+
- **未验证** Client 是否能从路由/外壳状态拿到当前 `sessionId`(本次未检查 Web shell 路由;不写入结论)。
|
|
609
|
+
- **未验证** 各 profile 是否一定挂载 `workspaceFiles`/`sessionPersistence`(运行时本机已挂载;其他 profile 需 `ctx.get` 判空)。
|
|
610
|
+
- **未验证** `workspaceFiles` 的 Config 是否在用户设置里可调;本文只记录默认值与硬校验(`limit > maxLines` 拒绝)。
|
|
611
|
+
|
|
612
|
+
## 10. 与 prompt-enhance 现有代码的衔接(事实)
|
|
613
|
+
|
|
614
|
+
- `D:/prompt_enhance/index.js:367`:`export const inject = ['webServer']`;`373` 起 `apply(ctx)`;路由用 `ctx.webServer.register({ kind: 'exact', path, handler })`(`377-383`)。因此新增 `/api/prompt-enhance/context` 不需要新依赖,只需在 handler 里 `ctx.get(...)` 可选服务。
|
|
615
|
+
- `D:/prompt_enhance/client.js:1035`:`const claimToken = useInput((s) => (s.claim && s.claim.token) || '')`。目前只取 token,**建议同时取 `s.claim?.name`**,`analyzeDraft` 的入参从 `claimToken` 扩展为 `{ claimName, claimToken }`,这样不需要解析本地化 token。
|
|
616
|
+
- `D:/prompt_enhance/client.js` 末尾 `__test__` 导出(约 1853 行)包含 `analyzeDraft` 等纯函数,便于为「token/name 映射」加单测。
|
|
617
|
+
- `D:/prompt_enhance/test/host.mjs` 用伪 `ctx`(`ctx.get('agentDefaultModel')`、`ctx.get('llm')`、`webServer.register`、`effect`)驱动 Host 半;新增服务应保持可选,伪 ctx 只需补 `get` 返回 `undefined` 即可跑通「降级」分支。
|
|
618
|
+
|
|
619
|
+
## 11. 建议的验收测试点(基于以上事实)
|
|
620
|
+
|
|
621
|
+
1. `resolveScope`:live header 有 cwd → 直接用;无 live 但有 persistence → 用 persistence;两者都无 → `SESSION_NOT_FOUND`;header 无 cwd → `sandboxPolicy.workspaceRoot` 兜底并标记 source。
|
|
622
|
+
2. 只读断言:fake `fs` 记录调用,断言不出现 `writeText/editText/patchText/deleteFile/moveFile`;`workspaceFiles` 只出现 `read/list/search/stat`。
|
|
623
|
+
3. 上限:`limit=5001` 必须被插件自己 clamp 或拒绝;`workspaceFiles` 返回 `gateway/bad-request` 时转成用户可读错误;`too-large`/`not-text`/`outside-workspace` 分别映射。
|
|
624
|
+
4. 围栏:`read` 传 `../outside.txt` 时,插件必须 `fs.contains` 拒绝;`list/search` 由服务返回 `outside-workspace`,插件透传。
|
|
625
|
+
5. 命令:fake `agents.get` 返回 Agent,fake `commands.list` 返回 descriptor,断言输出只含 `name/description/hint/attachments`;无 Agent 时为空。
|
|
626
|
+
6. 客户端:`claim.name` 优先于 token;token 为 `/目标` 时不得输出 `目标` 当作命令名;`resolveClaim` 缺失时降级不猜。
|
|
627
|
+
7. 兼容:伪 ctx 缺 `workspaceFiles`/`sessionPersistence`/`workspaceRegistry`/`commands`/`agents` 时不抛异常,走降级分支。
|
|
628
|
+
|
|
629
|
+
---
|
|
630
|
+
|
|
631
|
+
### 附:本次使用的只读命令(可复现)
|
|
632
|
+
|
|
633
|
+
```bash
|
|
634
|
+
# 读取 asar 内源码(普通 shell 无法直接读 asar)
|
|
635
|
+
export ELECTRON_RUN_AS_NODE=1
|
|
636
|
+
"/d/dsh/DeepSeek Harness.exe" /c/Users/Administrator/AppData/Local/Temp/ctxfacts/tool.mjs \
|
|
637
|
+
cat dsh-api-workspace-files/src/index.ts 1 240
|
|
638
|
+
|
|
639
|
+
# 运行时服务签名(Host Cordis Inspect)
|
|
640
|
+
# platform=host, provider=Service, method=listService, input={"service":"workspaceFiles"}
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
(Cordis Inspect 通过本会话的 `cordis_inspect_query` 调用;`Service.listService` 的 `input` 只接受 `{"service": "<key>"}`,传 `name` 会被拒绝。)
|