@davideasden/pi-undo 0.2.25 → 0.2.27
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 +5 -1
- package/docs/native-performance.md +65 -0
- package/native/bin/pi-undo-fs-darwin-arm64 +0 -0
- package/package.json +4 -3
- package/src/native-capabilities.ts +42 -0
- package/src/native-directory-scan.ts +174 -0
- package/src/native-restore.ts +12 -0
- package/src/restore-engine.ts +37 -28
- package/src/root-discovery.ts +53 -13
- package/src/snapshot-store.ts +23 -7
package/README.md
CHANGED
|
@@ -161,7 +161,11 @@ recovery_required
|
|
|
161
161
|
|
|
162
162
|
### Performance Notes
|
|
163
163
|
|
|
164
|
-
|
|
164
|
+
普通文件原生恢复路径保留 5 次完整拓扑扫描。macOS/Linux 的新版 helper 使用不跟随符号链接的目录句柄扫描,仍会发现 Git 忽略目录中的嵌套仓库。小工作区根据上次扫描规模选择 TypeScript 完整重扫,避免进程启动开销;没有使用 TTL 缓存或默认跳过 `node_modules`。
|
|
165
|
+
|
|
166
|
+
原生恢复现支持已有父目录中的普通文件删除,以及覆盖、创建、删除混合计划。扩展能力通过 `restore-files-v2` 探测;旧 helper、缺少二进制、目录操作及超出句柄预算的计划继续走 TypeScript 路径。pack 持久化、内容校验、no-clobber 与恢复依据均保留。
|
|
167
|
+
|
|
168
|
+
本机 macOS arm64 的三次 undo 中位数:3,000 包/1 文件从 1,976 ms 降至 924 ms,10,000 包/1 文件从 5,857 ms 降至 2,755 ms;100 文件的子目录撤销从 517 ms 降至 180 ms,混合撤销从 711 ms 降至 186 ms。测量方法和限制见 [原生性能验证记录](docs/native-performance.md)。
|
|
165
169
|
|
|
166
170
|
可通过真实 Pi 0.86.1 SDK 和离线 faux provider 运行大型工作区基准;它覆盖 3,000/10,000 个依赖包、修改 1/100 个文件,每组撤回三次并验证重做:
|
|
167
171
|
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# 原生目录扫描与普通文件恢复性能验证
|
|
2
|
+
|
|
3
|
+
本轮实现目录项缓存与 scoped restore 的可见路径检查,保留恢复前后的拓扑校验。目录扫描仍会逐个打开、核验工作区内的目录;缓存减少 `readdir`,没有把整次检查变成只访问 changedPaths 的增量操作。
|
|
4
|
+
|
|
5
|
+
## 实现与边界
|
|
6
|
+
|
|
7
|
+
- `scan-directories-v2` 在 macOS/Linux 上缓存每个目录的 `dev/ino/mtime/ctime`、Git 标记和直接子目录。每次检查仍核验全部目录的身份与时间戳,匹配时才复用目录项。检查点之间发生变化的目录重新枚举;扫描期间发生变化则显式失败,不接受部分结果。
|
|
8
|
+
- 缓存把 racy 标记固化到记录中,以开始枚举目录前的观察时刻计算两秒窗口。长子树扫描或之后的时间流逝不会让旧的 racy 记录自动变成可信记录。时间戳纳秒部分为零、无效或处于未来时采取保守重扫。
|
|
9
|
+
- 扫描继续进入 ignored 目录,以发现其中的嵌套仓库;不进入名称恰为 `.git` 的元数据目录,不跟随 symlink。目录句柄、父子绑定和工作区身份检查保留。超过 128 层时丢弃原生部分结果,由 TypeScript 从根重扫。
|
|
10
|
+
- v2 cache 绑定工作区路径与身份,结构解析失败或不匹配时不复用。它位于工作区外的私有临时目录,位置检查会解析 `TMPDIR` 的符号链接;无法安全放置请求时使用 TypeScript。cache 创建失败时使用本次请求目录,cache 写入失败不使成功扫描失败。正常进程退出时尽力清理,强制终止仍可能留下系统临时文件。
|
|
11
|
+
- 能力探测一次取得 capability 集合;旧 `scan-directories-v1` helper 继续完整扫描,缺少 helper 时使用 TypeScript。扫描仍受操作剩余时间和 `PI_UNDO_OPERATION_TIMEOUT_MS` 限制,取消或未确认退出保持原有处理契约。
|
|
12
|
+
- `listVisibleLeafPaths` 支持 `includePaths`。带 scope 的 complete restore 只枚举 scope 内的可见叶子;scope 外新增文件保留,scope 内新增可见路径仍会在修改前阻止恢复。未指定 scope 的完整恢复继续枚举整个工作区。嵌套仓库边界、内容、mode、symlink、mutation journal 和 quarantine 检查保留。
|
|
13
|
+
- 本轮没有引入 FSEvents/inotify 变更索引,也没有消除 settled 阶段完整 baseline 的枚举成本。因此,目录很多或 baseline 捕获很慢的工作区仍可能有明显等待,不能据此保证用户的 50GB 项目达到秒级响应。
|
|
14
|
+
|
|
15
|
+
缓存信任应用进程创建的私有文件及操作系统提供的目录元数据,不提供跨整个工作区的原子快照,也不提供抵抗同用户篡改 cache 的完整性保证。
|
|
16
|
+
|
|
17
|
+
## 本轮前后对比
|
|
18
|
+
|
|
19
|
+
环境:本机 macOS arm64、Node 23.11.1、Pi 0.86.1 SDK、离线 faux provider。基线为本轮改动前的 `bbe5c3f8`;基线和最终实现分别在隔离 worktree 中顺序运行相同用例,均使用原生 helper。每组执行三次 undo,中间 redo 并等待后台持久化清理,校验文件内容、历史状态和扫描次数。
|
|
20
|
+
|
|
21
|
+
| 场景 | 本轮改动前 | 本轮改动后 | 耗时降低 |
|
|
22
|
+
| --- | ---: | ---: | ---: |
|
|
23
|
+
| 10,000 个依赖包,修改 1 文件 | 2,792 ms | 2,504 ms | 10.3% |
|
|
24
|
+
|
|
25
|
+
基线三次耗时为 `[2970, 2792, 2763]` ms,最终实现为 `[2497, 2504, 2529]` ms;两组扫描次数都为 `[5, 5, 5]`。上述百分比只比较本轮前后,没有把更早版本的收益算入本轮。强制 TypeScript 回退也通过同一大型工作区用例,扫描次数为 `[6, 6, 6]`;额外一次来自无法复用原生 durable source 时重新捕获安全快照。
|
|
26
|
+
|
|
27
|
+
此前开发中一次 v2 采样为 `[2443, 2423, 2419]` ms;它与最终对比属于不同批次,不合并计算。三次本机采样只能说明这一基准的表现,没有墙钟性能断言,也没有对用户实际 50GB 项目、其他平台或其他磁盘作出速度保证。
|
|
28
|
+
|
|
29
|
+
## 上一轮原生优化的历史数据
|
|
30
|
+
|
|
31
|
+
下表保留上一轮以 `d7d80230` 为起点的结果,便于追踪原生扫描及普通文件恢复的收益;这些数据不是本轮缓存改动的前后对比。单位为毫秒。
|
|
32
|
+
|
|
33
|
+
| 场景 | 上一轮优化前 | 上一轮优化后 | 耗时降低 |
|
|
34
|
+
| --- | ---: | ---: | ---: |
|
|
35
|
+
| 无依赖目录,修改 1 文件 | 146 | 148 | 约持平 |
|
|
36
|
+
| 3,000 包,修改 1 文件 | 1,976 | 924 | 53% |
|
|
37
|
+
| 10,000 包,修改 1 文件 | 5,857 | 2,755 | 53% |
|
|
38
|
+
| 100 文件全部覆盖 | 179 | 182 | 约持平 |
|
|
39
|
+
| 100 文件在根目录新增后撤销 | 177 | 174 | 约持平 |
|
|
40
|
+
| 100 文件在已有子目录新增后撤销 | 517 | 180 | 65% |
|
|
41
|
+
| 50 文件覆盖、50 文件新增后混合撤销 | 711 | 186 | 74% |
|
|
42
|
+
|
|
43
|
+
上一轮分阶段计时中,3,000 包的五次扫描合计从 1,759 ms 降至 716 ms;10,000 包从 5,470 ms 降至 2,398 ms。另一批四组大型工作区测量覆盖 3,000/10,000 包 × 1/100 文件,中位数依次为 878、923、2,582、2,673 ms;这些独立批次不混算。
|
|
44
|
+
|
|
45
|
+
已有 `restore-files-v2` 支持父目录已存在的普通文件覆盖、创建、删除和混合计划,最多保留 128 个非根父目录句柄。目录创建/删除、类型替换、过多父目录和不支持的平台继续使用 TypeScript。durable pack 格式与恢复协议未改变:修改前持久化 pack,逐项校验内容和 mode,失败由现有 recovery 回滚并保留外来冲突文件。历史新增文件测试保留父目录中的 `keep.txt`,不包含目录删除成本。
|
|
46
|
+
|
|
47
|
+
## 复现与验证
|
|
48
|
+
|
|
49
|
+
本轮最终默认 Vitest:623 项通过、4 项按需基准跳过;Rust:12 项通过;原生相关 TypeScript:76 项通过。强制 TypeScript 回退完整测试和大型工作区用例、类型检查、npm 打包内容检查分别运行,不把跳过项算作通过。
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm run build:native
|
|
53
|
+
cp native/pi-undo-fs/target/release/pi-undo-fs native/bin/pi-undo-fs-darwin-arm64
|
|
54
|
+
npm run test:native
|
|
55
|
+
npm test
|
|
56
|
+
PI_UNDO_DISABLE_NATIVE=1 npm test
|
|
57
|
+
npm run typecheck
|
|
58
|
+
npm run pack:dry-run
|
|
59
|
+
PI_UNDO_LARGE_WORKSPACE=1 npx vitest run test/large-workspace.test.ts --testNamePattern='10000 个依赖包,修改 1 个文件'
|
|
60
|
+
PI_UNDO_DISABLE_NATIVE=1 PI_UNDO_LARGE_WORKSPACE=1 npx vitest run test/large-workspace.test.ts --testNamePattern='10000 个依赖包,修改 1 个文件'
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
二进制复制路径适用于本次 macOS arm64 环境;其他平台需要对应构建产物。本轮更新了仓库现有的 macOS arm64 二进制,未在本机验证 Linux、其他架构或 Windows 的构建与运行;发布仍依赖现有六平台构建流程。
|
|
64
|
+
|
|
65
|
+
测试覆盖目录缓存命中、深层 Git 标记新增、目录删除后的失效、racy 记录重扫、缓存身份与重复子项校验、TMPDIR 符号链接、v1/v2 协议、取消和超时,以及 scoped restore 保留范围外文件、拒绝范围内新增可见文件。独立只读审查已完成,审查提出的时间戳窗口、缓存清理和复用测试问题已修正;最终边界修正由主代理复核并重新验证。旧 helper 的混合计划仍可能先准备 pack 再回退,保留额外准备开销。
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@davideasden/pi-undo",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.27",
|
|
4
4
|
"description": "Persistent workspace undo and redo for Pi",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -34,13 +34,14 @@
|
|
|
34
34
|
"extensions",
|
|
35
35
|
"src",
|
|
36
36
|
"native/bin",
|
|
37
|
-
"README.md"
|
|
37
|
+
"README.md",
|
|
38
|
+
"docs/native-performance.md"
|
|
38
39
|
],
|
|
39
40
|
"scripts": {
|
|
40
41
|
"test": "vitest run",
|
|
41
42
|
"test:watch": "vitest",
|
|
42
43
|
"test:integration": "vitest run test/extension.integration.test.ts test/pi-runtime.test.ts test/pi-sdk.integration.test.ts",
|
|
43
|
-
"test:native": "cargo test --manifest-path native/pi-undo-fs/Cargo.toml && vitest run test/native-restore.test.ts test/durable-pack.test.ts test/packed-recovery.test.ts",
|
|
44
|
+
"test:native": "cargo test --manifest-path native/pi-undo-fs/Cargo.toml && vitest run test/native-restore.test.ts test/native-directory-scan.test.ts test/root-discovery.test.ts test/durable-pack.test.ts test/packed-recovery.test.ts",
|
|
44
45
|
"build:native": "cargo build --release --manifest-path native/pi-undo-fs/Cargo.toml",
|
|
45
46
|
"typecheck": "tsc --noEmit",
|
|
46
47
|
"pack:dry-run": "npm pack --dry-run"
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { GitRunError, runSupervisedProcess } from "./git-runner.ts";
|
|
2
|
+
import { checkOperation, OperationError, operationProcessOptions, rethrowOperationFailure } from "./operation-context.ts";
|
|
3
|
+
|
|
4
|
+
/** 在私有目录探测,防止旧 helper 把 --capabilities 当成工作区里的请求文件。 */
|
|
5
|
+
export async function probeNativeCapabilities(
|
|
6
|
+
executable: string,
|
|
7
|
+
isolatedDirectory: string,
|
|
8
|
+
): Promise<ReadonlySet<string>> {
|
|
9
|
+
const budget = operationProcessOptions(5_000);
|
|
10
|
+
try {
|
|
11
|
+
const result = await runSupervisedProcess({
|
|
12
|
+
command: executable,
|
|
13
|
+
args: ["--capabilities"],
|
|
14
|
+
cwd: isolatedDirectory,
|
|
15
|
+
...budget,
|
|
16
|
+
outputLimitBytes: 64 * 1024,
|
|
17
|
+
outputOverflow: "terminate",
|
|
18
|
+
});
|
|
19
|
+
if (!result.stopped) throw new GitRunError("git_termination_failed", "native 能力探测进程未能确认终止");
|
|
20
|
+
if (result.outcome === "cancelled") {
|
|
21
|
+
checkOperation();
|
|
22
|
+
throw new OperationError("operation_cancelled", "native 能力探测已被取消");
|
|
23
|
+
}
|
|
24
|
+
checkOperation();
|
|
25
|
+
if (result.outcome !== "exit" || result.code !== 0) return new Set();
|
|
26
|
+
const value: unknown = JSON.parse(result.stdout.toString("utf8"));
|
|
27
|
+
if (typeof value !== "object" || value === null || !("ok" in value) || value.ok !== true ||
|
|
28
|
+
!("capabilities" in value) || !Array.isArray(value.capabilities)) return new Set();
|
|
29
|
+
return new Set(value.capabilities.filter((entry): entry is string => typeof entry === "string"));
|
|
30
|
+
} catch (error) {
|
|
31
|
+
rethrowOperationFailure(error);
|
|
32
|
+
return new Set();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function probeNativeCapability(
|
|
37
|
+
executable: string,
|
|
38
|
+
capability: string,
|
|
39
|
+
isolatedDirectory: string,
|
|
40
|
+
): Promise<boolean> {
|
|
41
|
+
return (await probeNativeCapabilities(executable, isolatedDirectory)).has(capability);
|
|
42
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { constants, rmSync } from "node:fs";
|
|
2
|
+
import { access, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
|
+
|
|
6
|
+
import { GitRunError, runSupervisedProcess } from "./git-runner.ts";
|
|
7
|
+
import { probeNativeCapabilities } from "./native-capabilities.ts";
|
|
8
|
+
import { nativeExecutable } from "./native-restore.ts";
|
|
9
|
+
import { checkOperation, configuredTimeout, OperationError, operationProcessOptions } from "./operation-context.ts";
|
|
10
|
+
|
|
11
|
+
export interface NativeRepositoryCandidate {
|
|
12
|
+
readonly path: string;
|
|
13
|
+
readonly dev: bigint;
|
|
14
|
+
readonly ino: bigint;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface NativeDirectoryScan {
|
|
18
|
+
readonly directories: number;
|
|
19
|
+
readonly repositories: readonly NativeRepositoryCandidate[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface NativeDirectoryScanPort {
|
|
23
|
+
scan(workspaceRoot: string): Promise<NativeDirectoryScan | undefined>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const nativeScanCacheDirectories = new Set<string>();
|
|
27
|
+
let nativeScanCacheCleanupRegistered = false;
|
|
28
|
+
|
|
29
|
+
function registerNativeScanCacheDirectory(directory: string): void {
|
|
30
|
+
nativeScanCacheDirectories.add(directory);
|
|
31
|
+
if (nativeScanCacheCleanupRegistered) return;
|
|
32
|
+
nativeScanCacheCleanupRegistered = true;
|
|
33
|
+
process.once("exit", () => {
|
|
34
|
+
for (const path of nativeScanCacheDirectories) {
|
|
35
|
+
try { rmSync(path, { recursive: true, force: true }); } catch { /* 退出阶段尽力清理 */ }
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isPathInside(root: string, candidate: string): boolean {
|
|
41
|
+
const child = relative(resolve(root), resolve(candidate));
|
|
42
|
+
return child === "" || (child !== ".." && !child.startsWith(`..${sep}`) && !isAbsolute(child));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 原生 helper 能力之外,目录快照只在逐目录元数据仍可证明时复用。 */
|
|
46
|
+
export class NativeDirectoryScanner implements NativeDirectoryScanPort {
|
|
47
|
+
private readonly executable: string | undefined;
|
|
48
|
+
private capability: Promise<"v2" | "v1" | undefined> | undefined;
|
|
49
|
+
private cacheDirectory: Promise<string> | undefined;
|
|
50
|
+
|
|
51
|
+
constructor(executable = nativeExecutable()) {
|
|
52
|
+
this.executable = process.env.PI_UNDO_DISABLE_NATIVE === "1" || process.platform === "win32"
|
|
53
|
+
? undefined : executable;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async scan(workspaceRoot: string): Promise<NativeDirectoryScan | undefined> {
|
|
57
|
+
checkOperation();
|
|
58
|
+
if (this.executable === undefined ||
|
|
59
|
+
(this.capability !== undefined && await this.capability === undefined)) return undefined;
|
|
60
|
+
// TMPDIR 可能通过符号链接指向工作区;先核验真实路径,再创建任何请求或缓存。
|
|
61
|
+
const temporaryRoot = await realpath(tmpdir()).catch(() => undefined);
|
|
62
|
+
if (temporaryRoot === undefined || isPathInside(workspaceRoot, temporaryRoot)) return undefined;
|
|
63
|
+
const requestDirectory = await mkdtemp(join(temporaryRoot, "pi-undo-native-scan-"));
|
|
64
|
+
try {
|
|
65
|
+
this.capability ??= this.supportsScan(requestDirectory).catch((error) => {
|
|
66
|
+
// 取消或未确认终止不能污染后续操作的能力缓存。
|
|
67
|
+
this.capability = undefined;
|
|
68
|
+
throw error;
|
|
69
|
+
});
|
|
70
|
+
const version = await this.capability;
|
|
71
|
+
if (version === undefined) return undefined;
|
|
72
|
+
const budget = operationProcessOptions(configuredTimeout("PI_UNDO_OPERATION_TIMEOUT_MS", 300_000));
|
|
73
|
+
const requestPath = join(requestDirectory, "request.json");
|
|
74
|
+
const cachePath = version === "v2"
|
|
75
|
+
? await this.directoryCachePath(workspaceRoot, temporaryRoot, requestDirectory) : undefined;
|
|
76
|
+
await writeFile(requestPath, JSON.stringify({
|
|
77
|
+
schemaVersion: version === "v2" ? 2 : 1,
|
|
78
|
+
workspaceRoot,
|
|
79
|
+
...(cachePath === undefined ? {} : { cachePath }),
|
|
80
|
+
}), { mode: 0o600, flag: "wx" });
|
|
81
|
+
const result = await runSupervisedProcess({
|
|
82
|
+
command: this.executable,
|
|
83
|
+
args: ["--scan-directories", requestPath],
|
|
84
|
+
...budget,
|
|
85
|
+
outputLimitBytes: 32 * 1024 * 1024,
|
|
86
|
+
outputOverflow: "terminate",
|
|
87
|
+
});
|
|
88
|
+
if (!result.stopped) throw new GitRunError("git_termination_failed", "native 目录扫描进程未能确认终止");
|
|
89
|
+
if (result.outcome === "cancelled") {
|
|
90
|
+
checkOperation();
|
|
91
|
+
throw new OperationError("operation_cancelled", "native 目录扫描已被取消");
|
|
92
|
+
}
|
|
93
|
+
if (result.outcome === "timeout") throw new OperationError("operation_timeout", "native 目录扫描超时");
|
|
94
|
+
if (result.outcome !== "exit" || result.code !== 0) {
|
|
95
|
+
throw new Error(`native 目录扫描失败:${result.stderr.toString("utf8").trim() || result.outcome}`);
|
|
96
|
+
}
|
|
97
|
+
checkOperation();
|
|
98
|
+
return parseScanResponse(result.stdout.toString("utf8"));
|
|
99
|
+
} finally {
|
|
100
|
+
await rm(requestDirectory, { recursive: true, force: true }).catch(() => {});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private async supportsScan(directory: string): Promise<"v2" | "v1" | undefined> {
|
|
105
|
+
try {
|
|
106
|
+
await access(this.executable!, constants.X_OK);
|
|
107
|
+
} catch {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
const capabilities = await probeNativeCapabilities(this.executable!, directory);
|
|
111
|
+
if (capabilities.has("scan-directories-v2")) return "v2";
|
|
112
|
+
return capabilities.has("scan-directories-v1") ? "v1" : undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private async directoryCachePath(
|
|
116
|
+
workspaceRoot: string,
|
|
117
|
+
temporaryRoot: string,
|
|
118
|
+
requestDirectory: string,
|
|
119
|
+
): Promise<string> {
|
|
120
|
+
try {
|
|
121
|
+
if (this.cacheDirectory === undefined) {
|
|
122
|
+
this.cacheDirectory = mkdtemp(join(temporaryRoot, "pi-undo-native-scan-cache-")).catch((error) => {
|
|
123
|
+
this.cacheDirectory = undefined;
|
|
124
|
+
throw error;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
const directory = await this.cacheDirectory;
|
|
128
|
+
if (isPathInside(workspaceRoot, directory)) {
|
|
129
|
+
// 工作区切换后不再把该目录当成本实例可以删除的私有缓存。
|
|
130
|
+
nativeScanCacheDirectories.delete(directory);
|
|
131
|
+
this.cacheDirectory = undefined;
|
|
132
|
+
return join(requestDirectory, "directories.json");
|
|
133
|
+
}
|
|
134
|
+
registerNativeScanCacheDirectory(directory);
|
|
135
|
+
return join(directory, "directories.json");
|
|
136
|
+
} catch {
|
|
137
|
+
this.cacheDirectory = undefined;
|
|
138
|
+
return join(requestDirectory, "directories.json");
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function parseScanResponse(text: string): NativeDirectoryScan | undefined {
|
|
144
|
+
const value: unknown = JSON.parse(text);
|
|
145
|
+
// 深度上限是执行器的资源边界;此时丢弃部分结果,由 TypeScript 从根完整重扫。
|
|
146
|
+
if (isRecord(value) && value.ok === false && value.code === "depth_limit") return undefined;
|
|
147
|
+
if (!isRecord(value) || value.ok !== true || !Number.isSafeInteger(value.directories) ||
|
|
148
|
+
(value.directories as number) < 1 || !Array.isArray(value.repositories) ||
|
|
149
|
+
value.repositories.length >= (value.directories as number)) {
|
|
150
|
+
throw new Error("native 目录扫描响应无效");
|
|
151
|
+
}
|
|
152
|
+
const seen = new Set<string>();
|
|
153
|
+
const repositories = value.repositories.map((candidate): NativeRepositoryCandidate => {
|
|
154
|
+
if (!isRecord(candidate) || typeof candidate.path !== "string" || candidate.path.includes("\0") ||
|
|
155
|
+
candidate.path.split("/").some((part) => part === "" || part === "." || part === ".." || part === ".git") ||
|
|
156
|
+
seen.has(candidate.path)) {
|
|
157
|
+
throw new Error("native 仓库候选路径无效");
|
|
158
|
+
}
|
|
159
|
+
seen.add(candidate.path);
|
|
160
|
+
return { path: candidate.path, dev: parseIdentity(candidate.dev), ino: parseIdentity(candidate.ino) };
|
|
161
|
+
});
|
|
162
|
+
return { directories: value.directories as number, repositories };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function parseIdentity(value: unknown): bigint {
|
|
166
|
+
if (typeof value !== "string" || !/^(?:0|[1-9][0-9]{0,19})$/.test(value) || BigInt(value) > (1n << 64n) - 1n) {
|
|
167
|
+
throw new Error("native 目录身份字段无效");
|
|
168
|
+
}
|
|
169
|
+
return BigInt(value);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
173
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
174
|
+
}
|
package/src/native-restore.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { dirname, join } from "node:path";
|
|
|
6
6
|
import type { DurablePack } from "./durable-pack.ts";
|
|
7
7
|
import { GitRunError, runSupervisedProcess } from "./git-runner.ts";
|
|
8
8
|
import { OperationError, type OperationProcessOptions, operationProcessOptions, rethrowOperationFailure } from "./operation-context.ts";
|
|
9
|
+
import { probeNativeCapability } from "./native-capabilities.ts";
|
|
9
10
|
import type { MutationJournal } from "./mutation-journal.ts";
|
|
10
11
|
|
|
11
12
|
const NATIVE_TIMEOUT_MS = 120_000;
|
|
@@ -23,6 +24,7 @@ export async function createNativeFileBatch(options: {
|
|
|
23
24
|
readonly journal: MutationJournal;
|
|
24
25
|
/** 测试注入用;默认使用随包分发的平台二进制。 */
|
|
25
26
|
readonly executable?: string;
|
|
27
|
+
readonly requiredCapability?: "restore-files-v2";
|
|
26
28
|
}): Promise<NativeFileBatch | undefined> {
|
|
27
29
|
if (process.env.PI_UNDO_DISABLE_NATIVE === "1") return undefined;
|
|
28
30
|
const executable = options.executable ?? nativeExecutable();
|
|
@@ -32,6 +34,9 @@ export async function createNativeFileBatch(options: {
|
|
|
32
34
|
} catch {
|
|
33
35
|
return undefined;
|
|
34
36
|
}
|
|
37
|
+
if (options.requiredCapability !== undefined && !await probeNativeCapability(
|
|
38
|
+
executable, options.requiredCapability, dirname(options.journal.storagePath),
|
|
39
|
+
)) return undefined;
|
|
35
40
|
const execute = async (pack: DurablePack, requestPath: string, verifyOnly: boolean): Promise<void> => {
|
|
36
41
|
const paths = pack.paths();
|
|
37
42
|
if (paths.length === 0) return;
|
|
@@ -85,6 +90,13 @@ export async function createNativeFileBatch(options: {
|
|
|
85
90
|
};
|
|
86
91
|
}
|
|
87
92
|
|
|
93
|
+
export function nativeRestoreCapability(pack: DurablePack): "restore-files-v2" | undefined {
|
|
94
|
+
const paths = pack.paths();
|
|
95
|
+
const deletes = paths.filter((path) => pack.artifacts(path)?.target === null);
|
|
96
|
+
return deletes.some((path) => path.includes("/")) || (deletes.length > 0 && deletes.length < paths.length)
|
|
97
|
+
? "restore-files-v2" : undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
88
100
|
export function nativeExecutable(): string | undefined {
|
|
89
101
|
const platform = process.platform === "darwin"
|
|
90
102
|
? "darwin"
|
package/src/restore-engine.ts
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
import { assertManifest, assertOperationId, canonicalJson, checksum } from "./encoding.ts";
|
|
19
19
|
import { MutationJournal } from "./mutation-journal.ts";
|
|
20
20
|
import { allCompleted, checkOperation, rethrowOperationFailure, isUnconfirmedExit, operationFailure, withRecoveryBudget } from "./operation-context.ts";
|
|
21
|
-
import { createNativeFileBatch } from "./native-restore.ts";
|
|
21
|
+
import { createNativeFileBatch, nativeRestoreCapability } from "./native-restore.ts";
|
|
22
22
|
import { recoverPackedMutations } from "./packed-recovery.ts";
|
|
23
23
|
import type { ManifestId, RestorePath, SnapshotManifest, SnapshotRoot } from "./model.ts";
|
|
24
24
|
import {
|
|
@@ -106,6 +106,7 @@ interface VisibleSubsetCheck {
|
|
|
106
106
|
*/
|
|
107
107
|
readonly topologyValidated: boolean;
|
|
108
108
|
readonly extraExclusions?: readonly string[];
|
|
109
|
+
readonly scopePaths?: readonly string[];
|
|
109
110
|
readonly ownedPaths?: readonly (ReadonlyMap<string, OwnedPath> | undefined)[];
|
|
110
111
|
}
|
|
111
112
|
|
|
@@ -311,6 +312,7 @@ export class RestoreEngine {
|
|
|
311
312
|
workspaceRoot: this.workspaceRoot,
|
|
312
313
|
planDigest: cached.planDigest,
|
|
313
314
|
journal: cacheJournal,
|
|
315
|
+
requiredCapability: nativeRestoreCapability(cached.pack),
|
|
314
316
|
});
|
|
315
317
|
if (native === undefined || !await native.verifySource(cached.pack)) return false;
|
|
316
318
|
if (fromPersistentIndex) {
|
|
@@ -818,7 +820,8 @@ export class RestoreEngine {
|
|
|
818
820
|
options.mutationJournal,
|
|
819
821
|
{
|
|
820
822
|
topologyValidated: !compatibilityMode,
|
|
821
|
-
|
|
823
|
+
scopePaths: plan.scopePaths,
|
|
824
|
+
ownedPaths: [currentPaths, targetPaths],
|
|
822
825
|
},
|
|
823
826
|
);
|
|
824
827
|
} catch (error) {
|
|
@@ -862,6 +865,7 @@ export class RestoreEngine {
|
|
|
862
865
|
workspaceRoot: this.workspaceRoot,
|
|
863
866
|
planDigest: plan.planDigest,
|
|
864
867
|
journal: options.mutationJournal,
|
|
868
|
+
requiredCapability: nativeRestoreCapability(durablePack),
|
|
865
869
|
})
|
|
866
870
|
: undefined;
|
|
867
871
|
if (durablePack !== undefined && nativeFileBatch === undefined) {
|
|
@@ -940,7 +944,8 @@ export class RestoreEngine {
|
|
|
940
944
|
options.mutationJournal,
|
|
941
945
|
{
|
|
942
946
|
topologyValidated: true,
|
|
943
|
-
|
|
947
|
+
scopePaths: plan.scopePaths,
|
|
948
|
+
ownedPaths: [targetPaths],
|
|
944
949
|
},
|
|
945
950
|
);
|
|
946
951
|
const verification = await this.verifyTarget(
|
|
@@ -978,17 +983,16 @@ export class RestoreEngine {
|
|
|
978
983
|
currentPaths: ReadonlyMap<string, OwnedPath>,
|
|
979
984
|
targetPaths: ReadonlyMap<string, OwnedPath>,
|
|
980
985
|
): boolean {
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
return writeOnly || deleteOnly;
|
|
986
|
+
if (plan.deletePaths.length + plan.writePaths.length === 0) return false;
|
|
987
|
+
if (process.platform === "win32" && plan.deletePaths.length > 0) return false;
|
|
988
|
+
if (!plan.deletePaths.every((path) =>
|
|
989
|
+
currentPaths.get(path)?.entry.kind === "file" && targetPaths.get(path) === undefined)) return false;
|
|
990
|
+
if (!plan.writePaths.every((path) =>
|
|
991
|
+
targetPaths.get(path)?.entry.kind === "file" &&
|
|
992
|
+
(currentPaths.get(path) === undefined || currentPaths.get(path)?.entry.kind === "file"))) return false;
|
|
993
|
+
// 与 helper 的句柄上限一致;目录创建、类型替换及过多父目录继续使用 TypeScript。
|
|
994
|
+
const parents = new Set([...plan.deletePaths, ...plan.writePaths].flatMap(strictPathAncestors));
|
|
995
|
+
return process.platform === "win32" || parents.size <= 128;
|
|
992
996
|
}
|
|
993
997
|
|
|
994
998
|
private async applyNativeFilePlan(
|
|
@@ -1018,7 +1022,8 @@ export class RestoreEngine {
|
|
|
1018
1022
|
? []
|
|
1019
1023
|
: [artifacts.source, ...(artifacts.target === null ? [] : [artifacts.target])];
|
|
1020
1024
|
}),
|
|
1021
|
-
|
|
1025
|
+
scopePaths: plan.scopePaths,
|
|
1026
|
+
ownedPaths: [targetPaths],
|
|
1022
1027
|
},
|
|
1023
1028
|
);
|
|
1024
1029
|
const totalPaths = plan.deletePaths.length + plan.writePaths.length;
|
|
@@ -1198,13 +1203,6 @@ export class RestoreEngine {
|
|
|
1198
1203
|
return result;
|
|
1199
1204
|
}
|
|
1200
1205
|
|
|
1201
|
-
private completeCoverageOwnedPaths(
|
|
1202
|
-
scopePaths: readonly string[] | undefined,
|
|
1203
|
-
ownedPaths: readonly ReadonlyMap<string, OwnedPath>[],
|
|
1204
|
-
): readonly ReadonlyMap<string, OwnedPath>[] | undefined {
|
|
1205
|
-
return scopePaths === undefined ? ownedPaths : undefined;
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
1206
|
private async prefetchCompleteRestoreBlobs(
|
|
1209
1207
|
plan: RestorePlan,
|
|
1210
1208
|
current: SnapshotManifest,
|
|
@@ -1541,7 +1539,8 @@ export class RestoreEngine {
|
|
|
1541
1539
|
options.mutationJournal,
|
|
1542
1540
|
{
|
|
1543
1541
|
topologyValidated: true,
|
|
1544
|
-
|
|
1542
|
+
scopePaths,
|
|
1543
|
+
ownedPaths: [currentPaths],
|
|
1545
1544
|
},
|
|
1546
1545
|
);
|
|
1547
1546
|
const verification = await this.verifyTarget(
|
|
@@ -1586,7 +1585,8 @@ export class RestoreEngine {
|
|
|
1586
1585
|
options.mutationJournal,
|
|
1587
1586
|
{
|
|
1588
1587
|
topologyValidated: true,
|
|
1589
|
-
|
|
1588
|
+
scopePaths,
|
|
1589
|
+
ownedPaths: [currentPaths],
|
|
1590
1590
|
},
|
|
1591
1591
|
);
|
|
1592
1592
|
const verification = await this.verifyTarget(
|
|
@@ -1790,10 +1790,10 @@ export class RestoreEngine {
|
|
|
1790
1790
|
}
|
|
1791
1791
|
const allowedPaths = new Set<string>();
|
|
1792
1792
|
for (const [index, manifest] of allowedManifests.entries()) {
|
|
1793
|
-
for (const path of ignoredWorkspacePaths(manifest)) {
|
|
1793
|
+
for (const path of ignoredWorkspacePaths(manifest, check.scopePaths)) {
|
|
1794
1794
|
allowedPaths.add(path);
|
|
1795
1795
|
}
|
|
1796
|
-
const paths = check.ownedPaths?.[index] ?? await this.readOwnedPaths(manifest);
|
|
1796
|
+
const paths = check.ownedPaths?.[index] ?? await this.readOwnedPaths(manifest, check.scopePaths);
|
|
1797
1797
|
for (const [path, owned] of paths) {
|
|
1798
1798
|
if (owned.entry.kind !== "directory") {
|
|
1799
1799
|
allowedPaths.add(path);
|
|
@@ -1807,6 +1807,7 @@ export class RestoreEngine {
|
|
|
1807
1807
|
}
|
|
1808
1808
|
const livePaths = await this.store.listVisibleLeafPaths(topology, {
|
|
1809
1809
|
excludePaths: exclusions.size === 0 ? undefined : [...exclusions],
|
|
1810
|
+
includePaths: check.scopePaths,
|
|
1810
1811
|
topologyAlreadyValidated: check.topologyValidated,
|
|
1811
1812
|
});
|
|
1812
1813
|
for (const path of livePaths) {
|
|
@@ -2143,9 +2144,17 @@ function workspacePath(root: string, path: string): string {
|
|
|
2143
2144
|
return root === "." ? path : path === "." ? root : `${root}/${path}`;
|
|
2144
2145
|
}
|
|
2145
2146
|
|
|
2146
|
-
function ignoredWorkspacePaths(
|
|
2147
|
+
function ignoredWorkspacePaths(
|
|
2148
|
+
manifest: SnapshotManifest,
|
|
2149
|
+
scopePaths?: readonly string[],
|
|
2150
|
+
): Set<string> {
|
|
2151
|
+
const scope = scopePaths === undefined ? undefined : new Set(scopePaths);
|
|
2152
|
+
const scopeAncestors = new Set(scopePaths?.flatMap(strictPathAncestors));
|
|
2147
2153
|
return new Set(manifest.roots.flatMap((root) =>
|
|
2148
|
-
root.ignoredPresentPaths
|
|
2154
|
+
root.ignoredPresentPaths
|
|
2155
|
+
.map((path) => workspacePath(root.relativeRoot, path))
|
|
2156
|
+
.filter((path) => scope === undefined || scope.has(".") || scope.has(path) ||
|
|
2157
|
+
scopeAncestors.has(path) || strictPathAncestors(path).some((ancestor) => scope.has(ancestor)))
|
|
2149
2158
|
));
|
|
2150
2159
|
}
|
|
2151
2160
|
|
package/src/root-discovery.ts
CHANGED
|
@@ -4,9 +4,11 @@ import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
|
4
4
|
import { checksum, topologyFingerprint } from "./encoding.ts";
|
|
5
5
|
import { GitRunError, GitRunner } from "./git-runner.ts";
|
|
6
6
|
import type { DiscoveryRoot } from "./model.ts";
|
|
7
|
+
import { NativeDirectoryScanner, type NativeDirectoryScanPort, type NativeRepositoryCandidate } from "./native-directory-scan.ts";
|
|
7
8
|
import { allCompleted, checkOperation, OperationError, reportOperationProgress } from "./operation-context.ts";
|
|
8
9
|
|
|
9
10
|
const DIRECTORY_SCAN_CONCURRENCY = 16;
|
|
11
|
+
const NATIVE_SCAN_MIN_DIRECTORIES = 64;
|
|
10
12
|
const GIT_POINTER_MAX_BYTES = 4096;
|
|
11
13
|
|
|
12
14
|
interface RepositoryInfo {
|
|
@@ -57,8 +59,8 @@ export type RootDiscoveryErrorCode = "workspace_not_found" | "discovery_failed";
|
|
|
57
59
|
export class RootDiscoveryError extends Error {
|
|
58
60
|
readonly code: RootDiscoveryErrorCode;
|
|
59
61
|
|
|
60
|
-
constructor(code: RootDiscoveryErrorCode, message: string) {
|
|
61
|
-
super(message);
|
|
62
|
+
constructor(code: RootDiscoveryErrorCode, message: string, options?: ErrorOptions) {
|
|
63
|
+
super(message, options);
|
|
62
64
|
this.name = "RootDiscoveryError";
|
|
63
65
|
this.code = code;
|
|
64
66
|
}
|
|
@@ -70,8 +72,9 @@ export interface RootDiscovery {
|
|
|
70
72
|
|
|
71
73
|
export class RootDiscovery {
|
|
72
74
|
private readonly git: GitRunner;
|
|
75
|
+
private lastDirectoryCount: number | undefined;
|
|
73
76
|
|
|
74
|
-
constructor(git = new GitRunner()) {
|
|
77
|
+
constructor(git = new GitRunner(), private readonly nativeScan: NativeDirectoryScanPort = new NativeDirectoryScanner()) {
|
|
75
78
|
this.git = git;
|
|
76
79
|
}
|
|
77
80
|
|
|
@@ -114,6 +117,24 @@ export class RootDiscovery {
|
|
|
114
117
|
let level: Array<{ readonly path: string; readonly inspect: boolean }> = [{ path: directory, inspect: false }];
|
|
115
118
|
let scanned = 0;
|
|
116
119
|
reportOperationProgress("scan_directories");
|
|
120
|
+
// 上次规模只选择执行器;小工作区仍完整重扫,目录增长后会重新使用 native。
|
|
121
|
+
const native = this.lastDirectoryCount !== undefined && this.lastDirectoryCount < NATIVE_SCAN_MIN_DIRECTORIES
|
|
122
|
+
? undefined : await this.nativeScan.scan(workspaceIdentity);
|
|
123
|
+
if (native !== undefined) {
|
|
124
|
+
this.lastDirectoryCount = native.directories;
|
|
125
|
+
for (let index = 0; index < native.repositories.length; index += DIRECTORY_SCAN_CONCURRENCY) {
|
|
126
|
+
await allCompleted(native.repositories.slice(index, index + DIRECTORY_SCAN_CONCURRENCY).map(async (candidate) => {
|
|
127
|
+
checkOperation();
|
|
128
|
+
const path = join(workspaceIdentity, candidate.path);
|
|
129
|
+
await assertNativeCandidate(path, candidate);
|
|
130
|
+
const inspection = await this.inspectRepository(path, workspaceIdentity);
|
|
131
|
+
await assertNativeCandidate(path, candidate);
|
|
132
|
+
this.recordInspection(workspaceIdentity, inspection, activeRoots);
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
reportOperationProgress(`scan_directories:${native.directories}`);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
117
138
|
while (level.length > 0) {
|
|
118
139
|
checkOperation();
|
|
119
140
|
const next: Array<{ readonly path: string; readonly inspect: true }> = [];
|
|
@@ -128,6 +149,7 @@ export class RootDiscovery {
|
|
|
128
149
|
}
|
|
129
150
|
level = next;
|
|
130
151
|
}
|
|
152
|
+
this.lastDirectoryCount = scanned;
|
|
131
153
|
}
|
|
132
154
|
|
|
133
155
|
private async scanDirectoryNode(
|
|
@@ -139,16 +161,7 @@ export class RootDiscovery {
|
|
|
139
161
|
if (!await isSafeDirectory(candidate.path, workspaceIdentity)) return [];
|
|
140
162
|
if (candidate.inspect) {
|
|
141
163
|
const inspection = await this.inspectRepository(candidate.path, workspaceIdentity);
|
|
142
|
-
|
|
143
|
-
activeRoots.set(
|
|
144
|
-
inspection.repository.absoluteRoot,
|
|
145
|
-
this.activeRoot(workspaceIdentity, inspection.repository),
|
|
146
|
-
);
|
|
147
|
-
} else if (inspection.kind === "broken") {
|
|
148
|
-
activeRoots.set(inspection.absoluteRoot, brokenRoot(workspaceIdentity, inspection.absoluteRoot));
|
|
149
|
-
} else if (inspection.kind === "stale") {
|
|
150
|
-
activeRoots.set(inspection.absoluteRoot, staleWorktreeRoot(workspaceIdentity, inspection.absoluteRoot));
|
|
151
|
-
}
|
|
164
|
+
this.recordInspection(workspaceIdentity, inspection, activeRoots);
|
|
152
165
|
if (!await isSafeDirectory(candidate.path, workspaceIdentity)) return [];
|
|
153
166
|
}
|
|
154
167
|
checkOperation();
|
|
@@ -159,6 +172,20 @@ export class RootDiscovery {
|
|
|
159
172
|
.map((entry) => ({ path: join(candidate.path, entry.name), inspect: true as const }));
|
|
160
173
|
}
|
|
161
174
|
|
|
175
|
+
private recordInspection(
|
|
176
|
+
workspaceIdentity: string,
|
|
177
|
+
inspection: RepositoryInspection,
|
|
178
|
+
activeRoots: Map<string, DiscoveredRoot>,
|
|
179
|
+
): void {
|
|
180
|
+
if (inspection.kind === "active") {
|
|
181
|
+
activeRoots.set(inspection.repository.absoluteRoot, this.activeRoot(workspaceIdentity, inspection.repository));
|
|
182
|
+
} else if (inspection.kind === "broken") {
|
|
183
|
+
activeRoots.set(inspection.absoluteRoot, brokenRoot(workspaceIdentity, inspection.absoluteRoot));
|
|
184
|
+
} else if (inspection.kind === "stale") {
|
|
185
|
+
activeRoots.set(inspection.absoluteRoot, staleWorktreeRoot(workspaceIdentity, inspection.absoluteRoot));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
162
189
|
private async inspectRepository(candidate: string, workspaceIdentity: string): Promise<RepositoryInspection> {
|
|
163
190
|
const marker = await gitMarkerState(candidate);
|
|
164
191
|
if (marker === "absent") {
|
|
@@ -295,6 +322,19 @@ export class RootDiscovery {
|
|
|
295
322
|
}
|
|
296
323
|
}
|
|
297
324
|
|
|
325
|
+
async function assertNativeCandidate(path: string, candidate: NativeRepositoryCandidate): Promise<void> {
|
|
326
|
+
try {
|
|
327
|
+
const metadata = await lstat(path, { bigint: true });
|
|
328
|
+
if (!metadata.isDirectory() || metadata.dev !== candidate.dev || metadata.ino !== candidate.ino ||
|
|
329
|
+
await realpath(path) !== path) {
|
|
330
|
+
throw new RootDiscoveryError("discovery_failed", `native 扫描后仓库目录身份发生变化:${candidate.path}`);
|
|
331
|
+
}
|
|
332
|
+
} catch (error) {
|
|
333
|
+
if (error instanceof RootDiscoveryError) throw error;
|
|
334
|
+
throw new RootDiscoveryError("discovery_failed", `无法复核 native 仓库候选:${candidate.path}`, { cause: error });
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
298
338
|
function syntheticRoot(workspaceIdentity: string): DiscoveredRoot {
|
|
299
339
|
return {
|
|
300
340
|
absoluteRoot: workspaceIdentity,
|
package/src/snapshot-store.ts
CHANGED
|
@@ -155,6 +155,11 @@ export interface CaptureOptions {
|
|
|
155
155
|
readonly topologyAlreadyValidated?: boolean;
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
+
export interface VisibleLeafOptions extends CaptureOptions {
|
|
159
|
+
/** 未传时枚举整个工作区;空数组不枚举,目录路径包含其子树。 */
|
|
160
|
+
readonly includePaths?: readonly string[];
|
|
161
|
+
}
|
|
162
|
+
|
|
158
163
|
export interface SnapshotBlobRequest {
|
|
159
164
|
readonly rootPath: string;
|
|
160
165
|
readonly blobId: string;
|
|
@@ -188,7 +193,7 @@ export interface SnapshotStore {
|
|
|
188
193
|
scope?: readonly string[],
|
|
189
194
|
options?: CaptureOptions,
|
|
190
195
|
): Promise<SnapshotManifest>;
|
|
191
|
-
listVisibleLeafPaths(topology: RootTopology, options?:
|
|
196
|
+
listVisibleLeafPaths(topology: RootTopology, options?: VisibleLeafOptions): Promise<readonly string[]>;
|
|
192
197
|
loadManifest(id: ManifestId): Promise<SnapshotManifest>;
|
|
193
198
|
assertComplete(id: ManifestId, scopePaths?: readonly string[]): Promise<void>;
|
|
194
199
|
listTree(id: ManifestId, root: string, rootScopePaths?: readonly string[]): Promise<readonly RestorePath[]>;
|
|
@@ -552,7 +557,7 @@ export class SnapshotStore {
|
|
|
552
557
|
|
|
553
558
|
async listVisibleLeafPaths(
|
|
554
559
|
topology: RootTopology,
|
|
555
|
-
options:
|
|
560
|
+
options: VisibleLeafOptions = {},
|
|
556
561
|
): Promise<readonly string[]> {
|
|
557
562
|
await this.assertPrivateStore(topology.workspaceIdentity);
|
|
558
563
|
const lockIdentity = `snapshot-store:${await prospectiveCanonicalPath(this.storesRoot)}`;
|
|
@@ -561,7 +566,7 @@ export class SnapshotStore {
|
|
|
561
566
|
|
|
562
567
|
private async listVisibleLeafPathsLocked(
|
|
563
568
|
topology: RootTopology,
|
|
564
|
-
options:
|
|
569
|
+
options: VisibleLeafOptions,
|
|
565
570
|
): Promise<readonly string[]> {
|
|
566
571
|
let transactionDirectory: string | undefined;
|
|
567
572
|
try {
|
|
@@ -581,9 +586,23 @@ export class SnapshotStore {
|
|
|
581
586
|
const transactionsRoot = join(storeDirectory, "transactions");
|
|
582
587
|
await mkdir(transactionsRoot, { recursive: true });
|
|
583
588
|
transactionDirectory = await mkdtemp(join(transactionsRoot, "visible-"));
|
|
589
|
+
const includePaths = options.includePaths === undefined ? undefined : [...new Set(
|
|
590
|
+
options.includePaths.map((path) => {
|
|
591
|
+
const safe = relativeSafePath(topology.workspaceIdentity, path);
|
|
592
|
+
if (safe.split("/").some((part) => part.toLowerCase() === ".git")) {
|
|
593
|
+
throw new SnapshotStoreError("capture_failed", `可见路径 scope 不能包含 Git metadata:${path}`);
|
|
594
|
+
}
|
|
595
|
+
return safe;
|
|
596
|
+
}),
|
|
597
|
+
)].sort(comparePaths);
|
|
584
598
|
const result = new Set<string>();
|
|
585
599
|
for (const root of topology.roots) {
|
|
586
600
|
if (root.state !== "active") continue;
|
|
601
|
+
const exclusions = topology.roots
|
|
602
|
+
.filter((candidate) => isStrictRootAncestor(root.relativeRoot, candidate.relativeRoot))
|
|
603
|
+
.map((candidate) => rootRelativePath(root.relativeRoot, candidate.relativeRoot));
|
|
604
|
+
const inclusions = ownedRootInclusions(rootScopePathspecs(root.relativeRoot, includePaths), exclusions);
|
|
605
|
+
if (inclusions === null) continue;
|
|
587
606
|
const gitDirectory = this.rootGitDirectory(storeDirectory, root);
|
|
588
607
|
await this.ensurePrivateRepository(gitDirectory);
|
|
589
608
|
await this.assertNoAlternates(gitDirectory);
|
|
@@ -592,15 +611,12 @@ export class SnapshotStore {
|
|
|
592
611
|
const environment = privateGitEnvironment(gitDirectory, absoluteRoot, indexPath);
|
|
593
612
|
await this.runGit(["read-tree", "--empty"], { cwd: absoluteRoot, env: environment });
|
|
594
613
|
await this.validateIgnoreQuery(absoluteRoot, environment, root.gitBacked);
|
|
595
|
-
const exclusions = topology.roots
|
|
596
|
-
.filter((candidate) => isStrictRootAncestor(root.relativeRoot, candidate.relativeRoot))
|
|
597
|
-
.map((candidate) => rootRelativePath(root.relativeRoot, candidate.relativeRoot));
|
|
598
614
|
const exactExclusions = ownedArtifactExclusions(topology.roots, root.relativeRoot, artifactExclusions);
|
|
599
615
|
for (const relativePath of await this.queryVisibleLeafPaths(
|
|
600
616
|
absoluteRoot,
|
|
601
617
|
environment,
|
|
602
618
|
root.gitBacked,
|
|
603
|
-
|
|
619
|
+
inclusions,
|
|
604
620
|
exclusions,
|
|
605
621
|
exactExclusions,
|
|
606
622
|
true,
|