dsh-vscode-mode 0.35.0 → 0.36.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/README.md +16 -5
- package/lib/client.js +63 -63
- package/lib/client.js.map +1 -1
- package/lib/index.js +1388 -54
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/capture.ts +3 -0
- package/src/client/reviewBatch.ts +8 -0
- package/src/client/state/diffSummary.ts +6 -1
- package/src/client/state/reviewFeed.ts +35 -4
- package/src/client/ui/EditorView.ts +7 -0
- package/src/client/ui/McpSettings.ts +21 -0
- package/src/client/ui/OfficialReview.ts +20 -2
- package/src/fileOpenSettings.ts +29 -0
- package/src/index.ts +74 -6
- package/src/mcpManaged.ts +12 -3
- package/src/mcpManagerBridge.ts +5 -1
- package/src/mcpRuntime.ts +33 -4
- package/src/pending/capture.ts +32 -4
- package/src/pending/decide.ts +12 -3
- package/src/pending/externalAlign.ts +297 -0
- package/src/pending/externalCapture.ts +581 -0
- package/src/pending/kernel.ts +19 -1
- package/src/pending/persist.ts +114 -12
- package/src/pending/provenance.ts +239 -0
- package/src/pending/store.ts +30 -4
- package/src/pending/vcs.ts +122 -0
- package/src/pendingCompat.ts +13 -6
- package/src/pendingRpc.ts +19 -2
- package/src/rpc.ts +7 -2
- package/src/shared/pendingReview.ts +19 -1
- package/src/shared/review.ts +3 -1
- package/src/shared/rpc.ts +4 -0
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 外部改动实时采集:非 DSH 文件工具(`ops_bash` 里 `node -e`、prettier、生成器、python/sed 等)
|
|
3
|
+
* 改过的文件,用被监听目录的写事件补进待审投影。
|
|
4
|
+
*
|
|
5
|
+
* 设计取舍:
|
|
6
|
+
* - 只监听工作区根(递归),事件先入队,300ms 静默或 2000ms 封顶后统一读盘;单轮最多处理
|
|
7
|
+
* MAX_BATCH 个文件、MAX_FLUSH_BYTES 字节,其余留到下一轮。批量按**入队顺序**处理,不按 mtime:
|
|
8
|
+
* 同一毫秒内的多次写入 mtime 相同,用它排序反而可能造出倒序 diff。
|
|
9
|
+
* - 判定顺序固定:已知内容未变 → 跳过;插件自写窗口 → 跳过;编辑器保存窗口 → 只推进基线;
|
|
10
|
+
* 版本库说干净 → 跳过或标成版本库同步;其余按窗口判定归属后落库。绝不按时间戳猜重复。
|
|
11
|
+
* - 递归监听不可用(Linux)时降级为逐子目录非递归监听;仍不可用就只靠补扫,并把原因写进缺口。
|
|
12
|
+
* - 读盘一律走待审采集的严格 UTF-8 往返:二进制、GBK 等无法字节往返的内容不落记录(记缺口),
|
|
13
|
+
* 绝不因为"分析"而改写任何字节。
|
|
14
|
+
* 作者 ddj 2026-11-24
|
|
15
|
+
*/
|
|
16
|
+
import { readdirSync, watch } from 'node:fs'
|
|
17
|
+
import type { FSWatcher } from 'node:fs'
|
|
18
|
+
import { join, resolve } from 'node:path'
|
|
19
|
+
import type { Ctx } from '../store.js'
|
|
20
|
+
import { log } from '../log.js'
|
|
21
|
+
import type { PendingWrite } from './capture.js'
|
|
22
|
+
import { noteCaptureGap, readText } from './capture.js'
|
|
23
|
+
import type { StoredEntry } from './persist.js'
|
|
24
|
+
import { loadEntries } from './persist.js'
|
|
25
|
+
import { FILE_EXCLUDES, EXCLUDES } from '../search/ripgrep.js'
|
|
26
|
+
import { pathKeyOf, windows } from './provenance.js'
|
|
27
|
+
import type { VcsStatus } from './vcs.js'
|
|
28
|
+
import { vcsBaselineOf, vcsStatus } from './vcs.js'
|
|
29
|
+
import { vcsScopeOf } from './externalAlign.js'
|
|
30
|
+
import { invalidateIndex } from '../treeIndex.js'
|
|
31
|
+
|
|
32
|
+
// --region 预算与忽略清单
|
|
33
|
+
|
|
34
|
+
/** 事件静默合并窗口。 */
|
|
35
|
+
export const DEBOUNCE_MS = 300
|
|
36
|
+
/** 保存风暴的封顶:从首个事件算起最多等这么久就开跑。 */
|
|
37
|
+
export const MAX_WAIT_MS = 2000
|
|
38
|
+
/** 单轮最多处理的文件数。 */
|
|
39
|
+
export const MAX_BATCH = 64
|
|
40
|
+
/** 单轮读取总字节上限。 */
|
|
41
|
+
export const MAX_FLUSH_BYTES = 8 << 20
|
|
42
|
+
/** 单文件读取上限(与待审采集一致)。 */
|
|
43
|
+
export const MAX_FILE_BYTES = 1024 * 1024
|
|
44
|
+
/** 版本库状态命令的最小间隔:补扫与 watch 合并取用,避免频繁起进程。 */
|
|
45
|
+
export const VCS_STATUS_MIN_INTERVAL_MS = 5000
|
|
46
|
+
/** 递归监听不可用时,最多逐目录监听多少个一级子目录。 */
|
|
47
|
+
export const WATCH_DIR_MAX = 64
|
|
48
|
+
/** 内存基线表上限(路径比较键数)。 */
|
|
49
|
+
export const SEEN_MAX = 5000
|
|
50
|
+
/** 定时补扫间隔。 */
|
|
51
|
+
export const ALIGN_INTERVAL_MS = 10 * 60_000
|
|
52
|
+
/** 版本库写命令之后推迟多久补扫:等命令输出落盘、索引失效完成。 */
|
|
53
|
+
export const ALIGN_AFTER_VCS_MS = 1500
|
|
54
|
+
|
|
55
|
+
/** 会改动工作副本的版本库 RPC:只有这些方法之后才值得补扫。 */
|
|
56
|
+
export const VCS_WRITE_METHODS = new Set([
|
|
57
|
+
'svn.revert', 'svn.resolve', 'svn.add', 'svn.cleanup', 'svn.update', 'svn.ignore', 'svn.commit',
|
|
58
|
+
])
|
|
59
|
+
|
|
60
|
+
/** 会改动工作副本的版本库工具名。 */
|
|
61
|
+
export const VCS_WRITE_TOOLS = new Set([
|
|
62
|
+
'svn_add', 'svn_delete', 'svn_revert', 'svn_commit', 'svn_update', 'svn_resolve', 'svn_cleanup',
|
|
63
|
+
'svn_propset', 'svn_propdel', 'svn_changelist', 'remote_ssh_write',
|
|
64
|
+
])
|
|
65
|
+
|
|
66
|
+
/** 额外忽略的目录名(在搜索排除项之外,按原样大小写敏感匹配,口径与 EXCLUDES 一致)。 */
|
|
67
|
+
export const EXTRA_IGNORE_SEGMENTS = ['.svn', '.hg', '.p4config', 'Library', 'Temp', 'obj', 'Logs']
|
|
68
|
+
|
|
69
|
+
/** 忽略集合:目录段 + 文件名模式。 */
|
|
70
|
+
const IGNORE_SEGMENTS = new Set<string>([...EXCLUDES, ...EXTRA_IGNORE_SEGMENTS])
|
|
71
|
+
|
|
72
|
+
/** 忽略文件名(glob 只有这几条,直接判后缀/全名,不引第三方匹配器)。 */
|
|
73
|
+
const IGNORE_FILES = new Set(FILE_EXCLUDES.map((pattern) => pattern.replace(/^\*\*\//, '')))
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @public
|
|
77
|
+
* @author ddj 2026-11-24
|
|
78
|
+
* @description: 判断绝对路径是否不进记录:工作区外、忽略目录段、插件自身 sidecar、构建产物、`.map`、
|
|
79
|
+
* 以及计划页打包临时目录 `plans/<slug>/runtime/`。
|
|
80
|
+
* @param cwd 工作区根
|
|
81
|
+
* @param absolute 绝对路径
|
|
82
|
+
* @returns 忽略返回 true
|
|
83
|
+
*/
|
|
84
|
+
export function isIgnoredPath(cwd: string, absolute: string): boolean {
|
|
85
|
+
const root = cwd.replace(/\\/g, '/').replace(/\/+$/, '')
|
|
86
|
+
const unified = absolute.replace(/\\/g, '/')
|
|
87
|
+
if (root && !(unified === root || unified.toLowerCase().startsWith(root.toLowerCase() + '/'))) return true
|
|
88
|
+
const relative = root && unified.length > root.length ? unified.slice(root.length + 1) : unified
|
|
89
|
+
const segments = relative.split('/').filter(Boolean)
|
|
90
|
+
if (!segments.length) return true
|
|
91
|
+
// 目录段命中即忽略;最后一段是文件名,单独按文件名规则判。
|
|
92
|
+
for (const segment of segments.slice(0, -1)) {
|
|
93
|
+
if (IGNORE_SEGMENTS.has(segment)) return true
|
|
94
|
+
}
|
|
95
|
+
const name = segments[segments.length - 1]!
|
|
96
|
+
if (IGNORE_SEGMENTS.has(name)) return true
|
|
97
|
+
if (IGNORE_FILES.has(name)) return true
|
|
98
|
+
if (name.endsWith('.map')) return true
|
|
99
|
+
// 计划页被打包后只剩 plan.packed.html,但打包期间的 runtime/ 是构建临时产物
|
|
100
|
+
for (let index = 0; index + 2 < segments.length; index++) {
|
|
101
|
+
if (segments[index] === 'plans' && segments[index + 2] === 'runtime') return true
|
|
102
|
+
}
|
|
103
|
+
return false
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// --endregion
|
|
107
|
+
|
|
108
|
+
// --region 类型
|
|
109
|
+
|
|
110
|
+
/** 外部采集的两个开关。 */
|
|
111
|
+
export interface ExternalSettings {
|
|
112
|
+
watch: boolean
|
|
113
|
+
scan: boolean
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** 采集依赖:Host 能力 + 可注入的测试接缝。 */
|
|
117
|
+
export interface ExternalCaptureDeps {
|
|
118
|
+
ctx: Ctx
|
|
119
|
+
settings(): ExternalSettings
|
|
120
|
+
/** 落库:与 installPending 复用同一个 sink。 */
|
|
121
|
+
record(write: PendingWrite): void
|
|
122
|
+
/** 覆盖读盘(测试注入);缺省走 ctx.fs 的严格 UTF-8 往返。 */
|
|
123
|
+
read?(cwd: string, absolute: string): Promise<string | null>
|
|
124
|
+
/** 覆盖条目读取(测试注入);缺省读待审库。 */
|
|
125
|
+
entries?(cwd: string): Map<string, StoredEntry>
|
|
126
|
+
/** 覆盖版本库状态(测试注入);缺省跑一条 status 命令。 */
|
|
127
|
+
status?(cwd: string, sessionId: string): Promise<VcsStatus | null>
|
|
128
|
+
/** 覆盖补扫执行(测试注入);缺省调用 alignInWorkspace。 */
|
|
129
|
+
align?(cwd: string, sessionId: string): Promise<void>
|
|
130
|
+
now?(): number
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 采集器对外能力。 */
|
|
134
|
+
export interface ExternalCapture {
|
|
135
|
+
/** 确保工作区已开始监听(幂等;同时触发一次后台补扫)。 */
|
|
136
|
+
ensure(cwd: string, sessionId: string): void
|
|
137
|
+
/** 申请一次延迟补扫(版本库命令之后、手动按钮都用它;同一工作区会合并)。 */
|
|
138
|
+
requestAlign(cwd: string, delayMs?: number): void
|
|
139
|
+
/** 工作区不再需要监听(会话销毁)。 */
|
|
140
|
+
dismiss(cwd: string): void
|
|
141
|
+
/** 收到一个写事件(watcher 与测试共用)。 */
|
|
142
|
+
enqueue(cwd: string, absolute: string): void
|
|
143
|
+
/** 立即处理脏队列(测试与卸载共用)。 */
|
|
144
|
+
flush(cwd?: string): Promise<void>
|
|
145
|
+
/** 停监听、清定时器。 */
|
|
146
|
+
dispose(): void
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** 单工作区运行时状态。 */
|
|
150
|
+
interface Workspace {
|
|
151
|
+
cwd: string
|
|
152
|
+
sessionId: string
|
|
153
|
+
watchers: FSWatcher[]
|
|
154
|
+
dirty: Map<string, { path: string; seq: number }>
|
|
155
|
+
seen: Map<string, string | null>
|
|
156
|
+
timer: ReturnType<typeof setTimeout> | null
|
|
157
|
+
firstAt: number
|
|
158
|
+
flushing: boolean
|
|
159
|
+
again: boolean
|
|
160
|
+
degraded: string
|
|
161
|
+
statusAt: number
|
|
162
|
+
status?: VcsStatus | null
|
|
163
|
+
alignedAt: number
|
|
164
|
+
alignTimer: ReturnType<typeof setTimeout> | null
|
|
165
|
+
/** 延后补扫的定时器(版本库命令后/手动按钮,同一工作区合并一次)。 */
|
|
166
|
+
requestTimer: ReturnType<typeof setTimeout> | null
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// --endregion
|
|
170
|
+
|
|
171
|
+
// --region 采集器
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @public
|
|
175
|
+
* @author ddj 2026-11-24
|
|
176
|
+
* @description: 装配外部改动采集:watch 进队、合并读盘、按判定链落库;可整体卸载。
|
|
177
|
+
* @param deps Host 能力与测试接缝
|
|
178
|
+
* @returns 采集器
|
|
179
|
+
*/
|
|
180
|
+
export function installExternalCapture(deps: ExternalCaptureDeps): ExternalCapture {
|
|
181
|
+
const spaces = new Map<string, Workspace>()
|
|
182
|
+
const now = deps.now ?? Date.now
|
|
183
|
+
let seq = 0
|
|
184
|
+
let stopped = false
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @private
|
|
188
|
+
* @description: 取(或建)工作区运行时状态。
|
|
189
|
+
* @param cwd 工作区
|
|
190
|
+
* @param sessionId 会话
|
|
191
|
+
* @returns 状态
|
|
192
|
+
*/
|
|
193
|
+
function spaceOf(cwd: string, sessionId: string): Workspace {
|
|
194
|
+
const key = pathKeyOf(cwd)
|
|
195
|
+
const existing = spaces.get(key)
|
|
196
|
+
if (existing) return existing
|
|
197
|
+
const created: Workspace = {
|
|
198
|
+
cwd: String(cwd), sessionId, watchers: [], dirty: new Map(), seen: new Map(),
|
|
199
|
+
timer: null, firstAt: 0, flushing: false, again: false, degraded: '',
|
|
200
|
+
statusAt: 0, status: undefined, alignedAt: 0, alignTimer: null, requestTimer: null,
|
|
201
|
+
}
|
|
202
|
+
spaces.set(key, created)
|
|
203
|
+
return created
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @private
|
|
208
|
+
* @description: 事件分发:只收文件名,忽略目录与忽略清单。
|
|
209
|
+
* @param space 工作区状态
|
|
210
|
+
* @param base 被监听目录
|
|
211
|
+
* @param name 事件文件名
|
|
212
|
+
* @returns 无
|
|
213
|
+
*/
|
|
214
|
+
function onEvent(space: Workspace, base: string, name: string | Buffer | null): void {
|
|
215
|
+
if (stopped || !name) return
|
|
216
|
+
enqueue(space.cwd, resolve(base, String(name)))
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @private
|
|
221
|
+
* @description: 关闭一个工作区的全部监听器。
|
|
222
|
+
* @param space 工作区状态
|
|
223
|
+
* @returns 无
|
|
224
|
+
*/
|
|
225
|
+
function closeWatchers(space: Workspace): void {
|
|
226
|
+
for (const watcher of space.watchers) {
|
|
227
|
+
try { watcher.close() } catch { /* 关闭失败不影响卸载 */ }
|
|
228
|
+
}
|
|
229
|
+
space.watchers = []
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @private
|
|
234
|
+
* @description: 开始监听;递归可用就用递归,否则逐一级子目录监听,都不可用时只留补扫。
|
|
235
|
+
* @param space 工作区状态
|
|
236
|
+
* @returns 无
|
|
237
|
+
*/
|
|
238
|
+
function startWatch(space: Workspace): void {
|
|
239
|
+
if (space.watchers.length || stopped) return
|
|
240
|
+
try {
|
|
241
|
+
const watcher = watch(space.cwd, { recursive: true, persistent: false }, (_event, name) => onEvent(space, space.cwd, name))
|
|
242
|
+
// EventEmitter 的 'error' 无监听者时抛出未捕获异常,必须挂处理器
|
|
243
|
+
watcher.on('error', (error) => { log.warn('外部改动监听中断(改为只靠补扫):' + String(error)); closeWatchers(space) })
|
|
244
|
+
space.watchers.push(watcher)
|
|
245
|
+
return
|
|
246
|
+
} catch (error) {
|
|
247
|
+
log.debug('递归监听不可用,降级逐目录监听:' + String(error))
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
const entries = readdirSync(space.cwd, { withFileTypes: true })
|
|
251
|
+
.filter((entry) => entry.isDirectory() && !IGNORE_SEGMENTS.has(entry.name))
|
|
252
|
+
.slice(0, WATCH_DIR_MAX)
|
|
253
|
+
for (const entry of entries) {
|
|
254
|
+
const dir = join(space.cwd, entry.name)
|
|
255
|
+
const watcher = watch(dir, { persistent: false }, (_event, name) => onEvent(space, dir, name))
|
|
256
|
+
watcher.on('error', (error) => log.debug('目录监听中断:' + dir + ' ' + String(error)))
|
|
257
|
+
space.watchers.push(watcher)
|
|
258
|
+
}
|
|
259
|
+
if (!space.watchers.length) space.degraded = '目录监听不可用'
|
|
260
|
+
} catch (error) {
|
|
261
|
+
space.degraded = '目录监听不可用:' + String(error)
|
|
262
|
+
log.warn('外部改动监听不可用(改为只靠补扫):' + String(error))
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @private
|
|
268
|
+
* @description: 重新安排本批处理时刻:静默 300ms,但不超过首个事件后 2000ms。
|
|
269
|
+
* @param space 工作区状态
|
|
270
|
+
* @returns 无
|
|
271
|
+
*/
|
|
272
|
+
function schedule(space: Workspace): void {
|
|
273
|
+
const at = now()
|
|
274
|
+
if (!space.firstAt) space.firstAt = at
|
|
275
|
+
if (space.timer) clearTimeout(space.timer)
|
|
276
|
+
const wait = Math.max(0, Math.min(DEBOUNCE_MS, space.firstAt + MAX_WAIT_MS - at))
|
|
277
|
+
space.timer = setTimeout(() => {
|
|
278
|
+
space.timer = null
|
|
279
|
+
void flushSpace(space)
|
|
280
|
+
}, wait)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* @private
|
|
285
|
+
* @description: 取条目集合:真实路径读待审库,测试可注入。
|
|
286
|
+
* @param cwd 工作区
|
|
287
|
+
* @returns 路径比较键 → 条目
|
|
288
|
+
*/
|
|
289
|
+
function entriesOf(cwd: string): Map<string, StoredEntry> {
|
|
290
|
+
if (deps.entries) return deps.entries(cwd)
|
|
291
|
+
const map = new Map<string, StoredEntry>()
|
|
292
|
+
for (const stored of loadEntries(cwd)) map.set(pathKeyOf(stored.entry.id), stored)
|
|
293
|
+
return map
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* @private
|
|
298
|
+
* @description: 读磁盘内容;缺失返回 null,其它失败抛出(由调用点记缺口)。
|
|
299
|
+
* @param cwd 工作区
|
|
300
|
+
* @param absolute 绝对路径
|
|
301
|
+
* @returns 内容或 null
|
|
302
|
+
*/
|
|
303
|
+
async function readOf(cwd: string, absolute: string): Promise<string | null> {
|
|
304
|
+
if (deps.read) return deps.read(cwd, absolute)
|
|
305
|
+
const fs = deps.ctx.get('fs')
|
|
306
|
+
if (!fs) throw new Error('缺少 fs 服务')
|
|
307
|
+
const target = await fs.resolve(absolute, { cwd })
|
|
308
|
+
return readText(fs, target)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* @private
|
|
313
|
+
* @description: 取版本库状态(带最小间隔缓存);无 shell 或非 git/svn 时返回 null。
|
|
314
|
+
* @param space 工作区状态
|
|
315
|
+
* @returns 状态或 null
|
|
316
|
+
*/
|
|
317
|
+
async function statusOf(space: Workspace): Promise<VcsStatus | null> {
|
|
318
|
+
const at = now()
|
|
319
|
+
if (space.status !== undefined && at - space.statusAt < VCS_STATUS_MIN_INTERVAL_MS) return space.status
|
|
320
|
+
space.statusAt = at
|
|
321
|
+
if (deps.status) {
|
|
322
|
+
space.status = await deps.status(space.cwd, space.sessionId)
|
|
323
|
+
return space.status
|
|
324
|
+
}
|
|
325
|
+
const scope = vcsScopeOf(deps.ctx, space.cwd, space.sessionId)
|
|
326
|
+
space.status = scope ? await vcsStatus(scope.shell, space.cwd) : null
|
|
327
|
+
return space.status
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* @private
|
|
332
|
+
* @description: 处理一个路径:判定链见文件头注释。
|
|
333
|
+
* @param space 工作区状态
|
|
334
|
+
* @param absolute 绝对路径
|
|
335
|
+
* @param entries 本批共享的条目表
|
|
336
|
+
* @returns 是否新增或更新了条目
|
|
337
|
+
*/
|
|
338
|
+
async function handleOne(space: Workspace, absolute: string, entries: Map<string, StoredEntry>): Promise<boolean> {
|
|
339
|
+
const key = pathKeyOf(absolute)
|
|
340
|
+
const at = now()
|
|
341
|
+
const content = await readOf(space.cwd, absolute)
|
|
342
|
+
const stored = entries.get(key)
|
|
343
|
+
const sameContent = stored !== undefined && stored.entry.newText === (content ?? '')
|
|
344
|
+
const sameMissing = stored !== undefined && stored.flags.missing === (content === null)
|
|
345
|
+
if (stored && sameContent && sameMissing) {
|
|
346
|
+
space.seen.set(key, content)
|
|
347
|
+
return false
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const window = windows.windowOf(key, at)
|
|
351
|
+
if (window === 'self') {
|
|
352
|
+
// 插件自己刚写回(撤销/镜像写穿):内容随后会出现在条目里,这里不建新记录
|
|
353
|
+
space.seen.set(key, content)
|
|
354
|
+
return false
|
|
355
|
+
}
|
|
356
|
+
if (window === 'editor') {
|
|
357
|
+
space.seen.set(key, content)
|
|
358
|
+
// 编辑器保存:只推进基线;已有条目按外部改动吸收(基线不动、标分歧),不新开行
|
|
359
|
+
if (!stored) return false
|
|
360
|
+
deps.record(writeOf(space, absolute, stored.entry.newText, content, at, 'watch', 'editor', 'known'))
|
|
361
|
+
return true
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const status = await statusOf(space)
|
|
365
|
+
if (status && !status.entries.has(key)) {
|
|
366
|
+
// 版本库说这个路径相对基线干净:内容变化只能来自版本库同步
|
|
367
|
+
space.seen.set(key, content)
|
|
368
|
+
if (!stored) return false
|
|
369
|
+
deps.record(writeOf(space, absolute, stored.entry.newText, content, at, 'vcs', 'vcs', 'known'))
|
|
370
|
+
return true
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const before = await beforeOf(space, key, absolute, stored, status, content)
|
|
374
|
+
if (before.text === null && content === null) {
|
|
375
|
+
// 观察不到任何内容(既没文件也没基线):没有可审的差异
|
|
376
|
+
space.seen.set(key, null)
|
|
377
|
+
return false
|
|
378
|
+
}
|
|
379
|
+
deps.record(writeOf(space, absolute, before.text, content, at, 'watch',
|
|
380
|
+
windows.inAgentWindow(at) ? 'agent' : 'external', before.kind))
|
|
381
|
+
space.seen.set(key, content)
|
|
382
|
+
return true
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @private
|
|
387
|
+
* @description: 求修改前内容:已知内容 → 首次观察 → 版本库新增/基线 → 未知。
|
|
388
|
+
* @param space 工作区状态
|
|
389
|
+
* @param key 路径比较键
|
|
390
|
+
* @param absolute 绝对路径
|
|
391
|
+
* @param stored 既有条目
|
|
392
|
+
* @param status 版本库状态
|
|
393
|
+
* @param content 当前内容
|
|
394
|
+
* @returns 修改前内容与可信度
|
|
395
|
+
*/
|
|
396
|
+
async function beforeOf(
|
|
397
|
+
space: Workspace, key: string, absolute: string, stored: StoredEntry | undefined,
|
|
398
|
+
status: VcsStatus | null, content: string | null,
|
|
399
|
+
): Promise<{ text: string | null; kind: 'known' | 'absent' | 'vcs' | 'unknown' }> {
|
|
400
|
+
if (stored) return { text: stored.flags.missing ? null : stored.entry.newText, kind: 'known' }
|
|
401
|
+
if (space.seen.has(key)) {
|
|
402
|
+
const seen = space.seen.get(key) ?? null
|
|
403
|
+
return { text: seen, kind: seen === null ? 'absent' : 'known' }
|
|
404
|
+
}
|
|
405
|
+
const entry = status?.entries.get(key)
|
|
406
|
+
if (entry?.status === 'added') return { text: null, kind: 'absent' }
|
|
407
|
+
if (entry && content !== null) {
|
|
408
|
+
const scope = vcsScopeOf(deps.ctx, space.cwd, space.sessionId)
|
|
409
|
+
if (scope && status) {
|
|
410
|
+
const base = await vcsBaselineOf(scope.shell, status, absolute)
|
|
411
|
+
if (base !== null) return { text: base, kind: 'vcs' }
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return { text: null, kind: 'unknown' }
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* @private
|
|
419
|
+
* @description: 组装一条待审写入(watch 通道)。
|
|
420
|
+
* @param space 工作区状态
|
|
421
|
+
* @param absolute 绝对路径
|
|
422
|
+
* @param before 修改前内容
|
|
423
|
+
* @param after 当前内容
|
|
424
|
+
* @param at 时间
|
|
425
|
+
* @param source 采集通道
|
|
426
|
+
* @param origin 归属
|
|
427
|
+
* @param baseKind 基线可信度
|
|
428
|
+
* @returns 待审写入
|
|
429
|
+
*/
|
|
430
|
+
function writeOf(
|
|
431
|
+
space: Workspace, absolute: string, before: string | null, after: string | null, at: number,
|
|
432
|
+
source: 'watch' | 'vcs', origin: 'agent' | 'editor' | 'vcs' | 'external',
|
|
433
|
+
baseKind: 'known' | 'absent' | 'vcs' | 'unknown',
|
|
434
|
+
): PendingWrite {
|
|
435
|
+
return {
|
|
436
|
+
cwd: space.cwd, sessionId: space.sessionId, path: absolute, identity: absolute,
|
|
437
|
+
before, after, now: at, tool: source, origin, baseKind,
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* @private
|
|
443
|
+
* @description: 处理本工作区的一个批次:取一批 → 读盘判定 → 落库 → 失效目录树。
|
|
444
|
+
* @param space 工作区状态
|
|
445
|
+
* @returns 无
|
|
446
|
+
*/
|
|
447
|
+
async function flushSpace(space: Workspace): Promise<void> {
|
|
448
|
+
if (stopped) return
|
|
449
|
+
if (space.flushing) { space.again = true; return }
|
|
450
|
+
space.flushing = true
|
|
451
|
+
space.firstAt = 0
|
|
452
|
+
let bytes = 0
|
|
453
|
+
let recorded = 0
|
|
454
|
+
try {
|
|
455
|
+
const entries = entriesOf(space.cwd)
|
|
456
|
+
const batch = [...space.dirty.entries()].slice(0, MAX_BATCH)
|
|
457
|
+
for (const [key] of batch) space.dirty.delete(key)
|
|
458
|
+
const ordered = batch.sort((left, right) => left[1].seq - right[1].seq)
|
|
459
|
+
const touched: string[] = []
|
|
460
|
+
for (const [, item] of ordered) {
|
|
461
|
+
if (stopped) break
|
|
462
|
+
try {
|
|
463
|
+
const size = item.path.length
|
|
464
|
+
if (bytes + size > MAX_FLUSH_BYTES) { space.dirty.set(pathKeyOf(item.path), item); continue }
|
|
465
|
+
bytes += size
|
|
466
|
+
if (await handleOne(space, item.path, entries)) { recorded += 1; touched.push(item.path) }
|
|
467
|
+
} catch (error) {
|
|
468
|
+
noteCaptureGap(space.cwd, space.sessionId, item.path, error)
|
|
469
|
+
log.debug('外部改动跳过:' + item.path + ': ' + String(error))
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
for (const path of touched) invalidateIndex(deps.ctx, space.cwd, path)
|
|
473
|
+
if (recorded) log.debug(`外部改动记录 ${recorded} 条(${space.cwd})`)
|
|
474
|
+
} catch (error) {
|
|
475
|
+
log.warn('外部改动处理失败:' + String(error))
|
|
476
|
+
} finally {
|
|
477
|
+
space.flushing = false
|
|
478
|
+
if (space.seen.size > SEEN_MAX) {
|
|
479
|
+
for (const key of [...space.seen.keys()].slice(0, space.seen.size - SEEN_MAX)) space.seen.delete(key)
|
|
480
|
+
}
|
|
481
|
+
if (space.dirty.size) schedule(space)
|
|
482
|
+
else if (space.again) { space.again = false; schedule(space) }
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* @private
|
|
488
|
+
* @description: 收一个写事件进队并重新排期;忽略清单命中直接丢。
|
|
489
|
+
* @param cwd 工作区
|
|
490
|
+
* @param absolute 绝对路径
|
|
491
|
+
* @returns 无
|
|
492
|
+
*/
|
|
493
|
+
function enqueue(cwd: string, absolute: string): void {
|
|
494
|
+
if (stopped || !absolute || !cwd || !deps.settings().watch) return
|
|
495
|
+
if (isIgnoredPath(cwd, absolute)) return
|
|
496
|
+
const space = spaces.get(pathKeyOf(cwd)) ?? spaceOf(cwd, '')
|
|
497
|
+
seq += 1
|
|
498
|
+
space.dirty.set(pathKeyOf(absolute), { path: absolute, seq })
|
|
499
|
+
schedule(space)
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
return {
|
|
503
|
+
ensure(cwd: string, sessionId: string): void {
|
|
504
|
+
if (stopped || !cwd) return
|
|
505
|
+
const space = spaceOf(cwd, sessionId)
|
|
506
|
+
if (!deps.settings().watch) return
|
|
507
|
+
startWatch(space)
|
|
508
|
+
// 启动对齐:后台跑一次补扫,把 watch 启动之前漏掉的改动补录进来
|
|
509
|
+
if (!deps.settings().scan || space.alignedAt) return
|
|
510
|
+
space.alignedAt = now()
|
|
511
|
+
const timer = setTimeout(() => { void alignNow(space) }, 0)
|
|
512
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
513
|
+
if (!space.alignTimer) space.alignTimer = setInterval(() => { void alignNow(space) }, ALIGN_INTERVAL_MS)
|
|
514
|
+
if (typeof space.alignTimer.unref === 'function') space.alignTimer.unref()
|
|
515
|
+
},
|
|
516
|
+
dismiss(cwd: string): void {
|
|
517
|
+
const key = pathKeyOf(cwd)
|
|
518
|
+
const space = spaces.get(key)
|
|
519
|
+
if (!space) return
|
|
520
|
+
closeWatchers(space)
|
|
521
|
+
if (space.timer) clearTimeout(space.timer)
|
|
522
|
+
if (space.requestTimer) clearTimeout(space.requestTimer)
|
|
523
|
+
if (space.alignTimer) clearInterval(space.alignTimer)
|
|
524
|
+
spaces.delete(key)
|
|
525
|
+
},
|
|
526
|
+
requestAlign(cwd: string, delayMs = ALIGN_AFTER_VCS_MS): void {
|
|
527
|
+
// 版本库写命令和手动按钮共用:同工作区在延迟窗内只跑一次补扫
|
|
528
|
+
if (stopped || !cwd || !deps.settings().scan) return
|
|
529
|
+
const space = spaces.get(pathKeyOf(cwd))
|
|
530
|
+
if (!space) return
|
|
531
|
+
if (space.requestTimer) clearTimeout(space.requestTimer)
|
|
532
|
+
const timer = setTimeout(() => {
|
|
533
|
+
space.requestTimer = null
|
|
534
|
+
void alignNow(space)
|
|
535
|
+
}, Math.max(0, delayMs))
|
|
536
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
537
|
+
space.requestTimer = timer
|
|
538
|
+
},
|
|
539
|
+
enqueue(cwd: string, absolute: string): void {
|
|
540
|
+
enqueue(cwd, absolute)
|
|
541
|
+
},
|
|
542
|
+
async flush(cwd?: string): Promise<void> {
|
|
543
|
+
for (const [key, space] of spaces) {
|
|
544
|
+
if (cwd && key !== pathKeyOf(cwd)) continue
|
|
545
|
+
if (space.timer) { clearTimeout(space.timer); space.timer = null }
|
|
546
|
+
await flushSpace(space)
|
|
547
|
+
}
|
|
548
|
+
},
|
|
549
|
+
dispose(): void {
|
|
550
|
+
stopped = true
|
|
551
|
+
for (const space of spaces.values()) {
|
|
552
|
+
closeWatchers(space)
|
|
553
|
+
if (space.timer) clearTimeout(space.timer)
|
|
554
|
+
if (space.alignTimer) clearInterval(space.alignTimer)
|
|
555
|
+
space.dirty.clear()
|
|
556
|
+
space.seen.clear()
|
|
557
|
+
}
|
|
558
|
+
spaces.clear()
|
|
559
|
+
},
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* @private
|
|
564
|
+
* @description: 后台补扫一次(复用 externalAlign 的候选与预算)。
|
|
565
|
+
* @param space 工作区状态
|
|
566
|
+
* @returns 无
|
|
567
|
+
*/
|
|
568
|
+
async function alignNow(space: Workspace): Promise<void> {
|
|
569
|
+
if (stopped || !deps.settings().scan) return
|
|
570
|
+
try {
|
|
571
|
+
if (deps.align) { await deps.align(space.cwd, space.sessionId); return }
|
|
572
|
+
const { alignInWorkspace } = await import('./externalAlign.js')
|
|
573
|
+
const outcome = await alignInWorkspace(deps.ctx, space.cwd, space.sessionId, deps.record)
|
|
574
|
+
if (outcome.gaps.length) log.debug('补扫缺口:' + outcome.gaps.join(';'))
|
|
575
|
+
} catch (error) {
|
|
576
|
+
log.debug('后台补扫失败:' + String(error))
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// --endregion
|
package/src/pending/kernel.ts
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
* 无 IO、无时钟、不改入参;调用方传入 now 与内容,便于测试与复用。
|
|
4
4
|
* @author ddj 2026年10月29号
|
|
5
5
|
*/
|
|
6
|
-
import type { PendingEarlier, PendingLineage } from '../shared/pendingReview.js'
|
|
6
|
+
import type { PendingBaseKind, PendingEarlier, PendingLineage, PendingOrigin, PendingSource } from '../shared/pendingReview.js'
|
|
7
7
|
import { logicalLines } from '../shared/lineEndings.js'
|
|
8
|
+
import { strongerSource } from './provenance.js'
|
|
8
9
|
|
|
9
10
|
/** 内存态条目:比展示投影多保存两侧全文。 */
|
|
10
11
|
export interface PendingEntry {
|
|
@@ -18,6 +19,12 @@ export interface PendingEntry {
|
|
|
18
19
|
sessionId: string
|
|
19
20
|
sessionIds: string[]
|
|
20
21
|
lineage?: PendingLineage
|
|
22
|
+
/** 采集通道;缺省按 `tool` 解释。 */
|
|
23
|
+
source?: PendingSource
|
|
24
|
+
/** 归属;缺省按 `agent` 解释。 */
|
|
25
|
+
origin?: PendingOrigin
|
|
26
|
+
/** 修改前内容的可信度;缺省按 `known` 解释。 */
|
|
27
|
+
baseKind?: PendingBaseKind
|
|
21
28
|
}
|
|
22
29
|
|
|
23
30
|
/** 新建条目的入参。 */
|
|
@@ -30,6 +37,9 @@ export interface NewPending {
|
|
|
30
37
|
newText: string
|
|
31
38
|
updatedAt: number
|
|
32
39
|
lineage?: PendingLineage
|
|
40
|
+
source?: PendingSource
|
|
41
|
+
origin?: PendingOrigin
|
|
42
|
+
baseKind?: PendingBaseKind
|
|
33
43
|
}
|
|
34
44
|
|
|
35
45
|
/** 决定后对条目的局部推进;未出现的字段表示不改。 */
|
|
@@ -76,6 +86,11 @@ export function mergeEntries(earlier: PendingEntry, later: PendingEntry): Pendin
|
|
|
76
86
|
...later,
|
|
77
87
|
updatedAt: later.updatedAt,
|
|
78
88
|
sessionIds: unionSessions(earlier, later),
|
|
89
|
+
// 采集通道取更强的一侧(工具链路胜过 watch,watch 胜过补扫),归属取最近一次改动。
|
|
90
|
+
source: strongerSource(earlier.source, later.source),
|
|
91
|
+
origin: later.origin ?? earlier.origin,
|
|
92
|
+
// 基线一旦落成"已知内容",就不再退回未知;未知只来自首次观察失败的那条记录。
|
|
93
|
+
baseKind: later.baseKind ?? earlier.baseKind,
|
|
79
94
|
}
|
|
80
95
|
if (later.lineage === undefined && earlier.lineage !== undefined) merged.lineage = earlier.lineage
|
|
81
96
|
if (earlier.earlierVersion === 'none' && later.earlierVersion === 'none') {
|
|
@@ -107,6 +122,9 @@ export function entryFromNew(input: NewPending): PendingEntry {
|
|
|
107
122
|
sessionIds: [input.sessionId],
|
|
108
123
|
}
|
|
109
124
|
if (input.lineage !== undefined) entry.lineage = input.lineage
|
|
125
|
+
if (input.source !== undefined) entry.source = input.source
|
|
126
|
+
if (input.origin !== undefined) entry.origin = input.origin
|
|
127
|
+
if (input.baseKind !== undefined) entry.baseKind = input.baseKind
|
|
110
128
|
return entry
|
|
111
129
|
}
|
|
112
130
|
|