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