@davideasden/pi-undo 0.2.30 → 0.2.32

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.
@@ -12,7 +12,7 @@ metadata inspect 默认每批最多处理 2,048 条路径,并受 1 MiB 路径
12
12
 
13
13
  在本机 macOS arm64、Node 23.11.1、10,000 个依赖包且修改 1 个文件的 SDK 基准中,默认 2,048 批次三次耗时为 `[2437, 2440, 2426]` ms,中位数 `2437 ms`;设置 4,096 批次后三次为 `[2425, 2438, 2432]` ms,中位数 `2432 ms`。差异约 `0.2%`,低于该基准的运行波动,因此没有把默认批次提高到 4,096。两组扫描次数都为 `[5, 5, 5]`,峰值 RSS 分别为约 `260 MiB` 和 `279 MiB`;RSS 未显示出提高批次上限的收益。
14
14
 
15
- ## 实现与边界
15
+ GitRunner 会对单次 Git stdout 应用 256 MiB 硬上限;超过上限时终止整个 Git 进程组并抛出 `git_output_limit`,避免在 `Buffer.concat(...).toString()` 阶段触发 Node.js `ERR_STRING_TOO_LONG` 或无界内存增长。正常的 blob batch 读取仍按 16 MiB 分批,不受该上限影响。
16
16
 
17
17
  - `scan-directories-v2` 在 macOS/Linux 上缓存每个目录的 `dev/ino/mtime/ctime`、Git 标记和直接子目录。每次检查仍核验全部目录的身份与时间戳,匹配时才复用目录项。检查点之间发生变化的目录重新枚举;扫描期间发生变化则显式失败,不接受部分结果。
18
18
  - 缓存把 racy 标记固化到记录中,以开始枚举目录前的观察时刻计算两秒窗口。长子树扫描或之后的时间流逝不会让旧的 racy 记录自动变成可信记录。时间戳纳秒部分为零、无效或处于未来时采取保守重扫。
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@davideasden/pi-undo",
3
- "version": "0.2.30",
3
+ "version": "0.2.32",
4
4
  "description": "Persistent workspace undo and redo for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/git-runner.ts CHANGED
@@ -3,6 +3,8 @@ import { spawn, type ChildProcess } from "node:child_process";
3
3
  import { type OperationContext, configuredTimeout, currentOperationContext, isOperationTimeoutReason, markProcessExitUnconfirmed, reportProcessDiagnostic } from "./operation-context.ts";
4
4
 
5
5
  export const DEFAULT_STDERR_LIMIT = 64 * 1024;
6
+ /** 防止 Git 单次 stdout 触发 Node 字符串/Buffer 上限;正常 blob 读取按批次拆分。 */
7
+ export const DEFAULT_STDOUT_LIMIT = 256 * 1024 * 1024;
6
8
  /** Git 单次调用的默认预算;调用链上的 deadline 可进一步收紧它。 */
7
9
  export const DEFAULT_GIT_TIMEOUT_MS = 120_000;
8
10
  const TERMINATION_GRACE_MS = 50;
@@ -28,6 +30,7 @@ export interface GitRunOptions {
28
30
  readonly signal?: AbortSignal;
29
31
  readonly timeoutMs?: number;
30
32
  readonly stderrLimit?: number;
33
+ readonly stdoutLimit?: number;
31
34
  }
32
35
 
33
36
  export interface GitRunResult {
@@ -40,7 +43,7 @@ export interface GitRunResult {
40
43
  readonly aborted: boolean;
41
44
  }
42
45
 
43
- export type GitRunErrorCode = "git_failed" | "git_spawn_failed" | "git_termination_failed";
46
+ export type GitRunErrorCode = "git_failed" | "git_spawn_failed" | "git_termination_failed" | "git_output_limit";
44
47
 
45
48
  export class GitRunError extends Error {
46
49
  readonly code: GitRunErrorCode;
@@ -84,9 +87,13 @@ export class GitRunner {
84
87
 
85
88
  private async runCommand(args: readonly string[], options: GitRunOptions, command: string): Promise<GitRunResult> {
86
89
  const stderrLimit = options.stderrLimit ?? DEFAULT_STDERR_LIMIT;
90
+ const stdoutLimit = options.stdoutLimit ?? DEFAULT_STDOUT_LIMIT;
87
91
  if (!Number.isInteger(stderrLimit) || stderrLimit < 0) {
88
92
  throw new RangeError("stderrLimit 必须是非负整数");
89
93
  }
94
+ if (!Number.isInteger(stdoutLimit) || stdoutLimit < 0) {
95
+ throw new RangeError("stdoutLimit 必须是非负整数");
96
+ }
90
97
  if (options.timeoutMs !== undefined && (!Number.isFinite(options.timeoutMs) || options.timeoutMs < 0)) {
91
98
  throw new RangeError("timeoutMs 必须是非负有限数字");
92
99
  }
@@ -121,6 +128,8 @@ export class GitRunner {
121
128
  const stdout: Buffer[] = [];
122
129
  const stderr: Buffer[] = [];
123
130
  let stderrBytes = 0;
131
+ let stdoutBytes = 0;
132
+ let outputLimitExceeded = false;
124
133
  let killed = false;
125
134
  let timedOut = false;
126
135
  let aborted = false;
@@ -132,7 +141,17 @@ export class GitRunner {
132
141
  let terminationFailed = false;
133
142
 
134
143
  child.stdout?.on("data", (chunk: Buffer | string) => {
135
- stdout.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
144
+ const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
145
+ const remaining = stdoutLimit - stdoutBytes;
146
+ if (bytes.length > remaining) {
147
+ if (remaining > 0) stdout.push(bytes.subarray(0, remaining));
148
+ stdoutBytes = stdoutLimit;
149
+ outputLimitExceeded = true;
150
+ terminate("output_limit");
151
+ return;
152
+ }
153
+ stdout.push(bytes);
154
+ stdoutBytes += bytes.length;
136
155
  });
137
156
  child.stderr?.on("data", (chunk: Buffer | string) => {
138
157
  if (stderrBytes >= stderrLimit) {
@@ -154,7 +173,7 @@ export class GitRunner {
154
173
  finish();
155
174
  };
156
175
 
157
- const terminate = (reason: "timeout" | "abort"): void => {
176
+ const terminate = (reason: "timeout" | "abort" | "output_limit"): void => {
158
177
  if (settled || killed) {
159
178
  return;
160
179
  }
@@ -211,6 +230,10 @@ export class GitRunner {
211
230
  reject(new GitRunError("git_termination_failed", "Git 进程组未能完全终止", result));
212
231
  return;
213
232
  }
233
+ if (outputLimitExceeded) {
234
+ reject(new GitRunError("git_output_limit", `Git stdout 超过 ${stdoutLimit} 字节上限`, result));
235
+ return;
236
+ }
214
237
  if (!killed && exit.code !== 0) {
215
238
  reject(new GitRunError(
216
239
  "git_failed",
@@ -100,6 +100,8 @@ interface VisibleLeaf {
100
100
  readonly kind: "file" | "symlink";
101
101
  readonly mode: number;
102
102
  readonly fingerprint: string;
103
+ /** 不含 ctime,用于把 macOS 元数据写入与内容或路径变化区分开。 */
104
+ readonly stableFingerprint?: string;
103
105
  readonly changedAtNs: bigint;
104
106
  readonly cacheable: boolean;
105
107
  }
@@ -136,6 +138,7 @@ interface PersistedLeafCacheEntry {
136
138
  readonly kind: "file" | "symlink";
137
139
  readonly mode: number;
138
140
  readonly fingerprint: string;
141
+ readonly stableFingerprint?: string;
139
142
  readonly cacheable: boolean;
140
143
  readonly objectId: string;
141
144
  readonly changedAtNs: string;
@@ -515,6 +518,7 @@ export class SnapshotStore {
515
518
  const cache = this.visibleLeafCache.get(gitDirectory);
516
519
  if (cache === undefined) return false;
517
520
  const entriesByPath = new Map(entries.map((entry) => [entry.relativePath, entry]));
521
+ const expectedObjectIds = new Map(entries.map((entry) => [entry.relativePath, entry.objectId]));
518
522
  for (const leaf of leaves) {
519
523
  const entry = entriesByPath.get(leaf.relativePath);
520
524
  const cached = cache.get(leaf.relativePath);
@@ -548,7 +552,13 @@ export class SnapshotStore {
548
552
  ignoredPresentPaths,
549
553
  })) return false;
550
554
  // metadata 初检与最终复核之间若有变化,放弃 baseline,回退完整 capture。
551
- await this.assertVisibleLeavesUnchanged(absoluteRoot, leaves, transactionDirectory);
555
+ await this.assertVisibleLeavesUnchanged(
556
+ absoluteRoot,
557
+ leaves,
558
+ transactionDirectory,
559
+ expectedObjectIds,
560
+ environment,
561
+ );
552
562
  }
553
563
  return true;
554
564
  } catch (error) {
@@ -1219,7 +1229,8 @@ export class SnapshotStore {
1219
1229
  ...batch.map((leaf) => leaf.relativePath),
1220
1230
  ], { cwd, env: environment });
1221
1231
  const hashes = parseObjectIdLines(output, batch.length);
1222
- await this.assertVisibleLeavesUnchanged(cwd, batch);
1232
+ const batchObjectIds = new Map(batch.map((leaf, index) => [leaf.relativePath, hashes[index]!]));
1233
+ await this.assertVisibleLeavesUnchanged(cwd, batch, undefined, batchObjectIds, environment);
1223
1234
  return batch.map((leaf, index) => [leaf.relativePath, hashes[index]!] as const);
1224
1235
  });
1225
1236
  for (const batch of hashedBatches) {
@@ -1240,7 +1251,7 @@ export class SnapshotStore {
1240
1251
  throw new SnapshotStoreError("capture_failed", `文件对象 materialize 失败:${leaf.relativePath}`);
1241
1252
  }
1242
1253
  await assertNoSymlinkEscape(cwd, leaf.relativePath);
1243
- await this.assertVisibleLeafUnchanged(cwd, leaf);
1254
+ await this.assertVisibleLeafUnchanged(cwd, leaf, objectId, environment);
1244
1255
  objectIds.set(leaf.relativePath, objectId);
1245
1256
  }
1246
1257
  for (const indexInput of indexInfoBatches(leaves, objectIds)) {
@@ -1250,7 +1261,7 @@ export class SnapshotStore {
1250
1261
  stdin: indexInput,
1251
1262
  });
1252
1263
  }
1253
- await this.assertVisibleLeavesUnchanged(cwd, leaves, requestDirectory);
1264
+ await this.assertVisibleLeavesUnchanged(cwd, leaves, requestDirectory, objectIds, environment);
1254
1265
  return { leaves, objectIds, verifiedAtNs: BigInt(Date.now()) * 1_000_000n };
1255
1266
  }
1256
1267
 
@@ -1303,6 +1314,7 @@ export class SnapshotStore {
1303
1314
  kind: entry.kind,
1304
1315
  mode: entry.mode,
1305
1316
  fingerprint: entry.fingerprint,
1317
+ stableFingerprint: entry.stableFingerprint,
1306
1318
  cacheable: entry.cacheable,
1307
1319
  changedAtNs: BigInt(entry.changedAtNs),
1308
1320
  objectId: entry.objectId,
@@ -1326,6 +1338,7 @@ export class SnapshotStore {
1326
1338
  kind: leaf.kind,
1327
1339
  mode: leaf.mode,
1328
1340
  fingerprint: leaf.fingerprint,
1341
+ stableFingerprint: leaf.stableFingerprint,
1329
1342
  cacheable: leaf.cacheable,
1330
1343
  objectId: leaf.objectId,
1331
1344
  changedAtNs: leaf.changedAtNs.toString(),
@@ -1351,6 +1364,8 @@ export class SnapshotStore {
1351
1364
  cwd: string,
1352
1365
  leaves: readonly VisibleLeaf[],
1353
1366
  requestDirectory?: string,
1367
+ expectedObjectIds?: ReadonlyMap<string, string>,
1368
+ environment?: Readonly<Record<string, string | undefined>>,
1354
1369
  ): Promise<void> {
1355
1370
  if (leaves.length === 0) return;
1356
1371
  const paths = leaves.map((leaf) => leaf.relativePath);
@@ -1359,32 +1374,105 @@ export class SnapshotStore {
1359
1374
  await assertNoSymlinkParents(cwd, paths);
1360
1375
  const inspected = await this.inspectNativeMetadataBatches(cwd, paths, requestDirectory);
1361
1376
  if (inspected !== undefined) {
1377
+ const contentChecks: VisibleLeaf[] = [];
1362
1378
  for (let index = 0; index < leaves.length; index += 1) {
1363
1379
  const leaf = leaves[index]!;
1364
1380
  const metadata = nativeVisibleLeafMetadata(inspected[index]!);
1365
- if (metadata === null || visibleLeafFingerprint(metadata) !== leaf.fingerprint) {
1381
+ if (metadata === null || visibleLeafStableFingerprint(metadata) !== leaf.stableFingerprint) {
1366
1382
  throw new SnapshotStoreError("capture_failed", `捕获期间工作区叶子已变化:${leaf.relativePath}`);
1367
1383
  }
1384
+ if (visibleLeafFingerprint(metadata) !== leaf.fingerprint) contentChecks.push(leaf);
1368
1385
  }
1386
+ await mapConcurrentOrdered(contentChecks, FILE_SYSTEM_INSPECTION_CONCURRENCY, (leaf) =>
1387
+ this.assertVisibleLeafContentUnchanged(
1388
+ cwd,
1389
+ leaf,
1390
+ expectedObjectIds?.get(leaf.relativePath),
1391
+ environment,
1392
+ ));
1369
1393
  await assertNoSymlinkParents(cwd, paths);
1370
1394
  return;
1371
1395
  }
1372
1396
  await mapConcurrentOrdered(leaves, FILE_SYSTEM_INSPECTION_CONCURRENCY, (leaf) =>
1373
- this.assertVisibleLeafUnchanged(cwd, leaf));
1397
+ this.assertVisibleLeafUnchanged(
1398
+ cwd,
1399
+ leaf,
1400
+ expectedObjectIds?.get(leaf.relativePath),
1401
+ environment,
1402
+ ));
1374
1403
  await assertNoSymlinkParents(cwd, paths);
1375
1404
  return;
1376
1405
  }
1377
1406
  await assertNoSymlinkParents(cwd, paths);
1378
1407
  await mapConcurrentOrdered(leaves, FILE_SYSTEM_INSPECTION_CONCURRENCY, (leaf) =>
1379
- this.assertVisibleLeafUnchanged(cwd, leaf));
1408
+ this.assertVisibleLeafUnchanged(
1409
+ cwd,
1410
+ leaf,
1411
+ expectedObjectIds?.get(leaf.relativePath),
1412
+ environment,
1413
+ ));
1380
1414
  }
1381
1415
 
1382
- private async assertVisibleLeafUnchanged(cwd: string, leaf: VisibleLeaf): Promise<void> {
1416
+ private async assertVisibleLeafUnchanged(
1417
+ cwd: string,
1418
+ leaf: VisibleLeaf,
1419
+ expectedObjectId?: string,
1420
+ environment?: Readonly<Record<string, string | undefined>>,
1421
+ ): Promise<void> {
1383
1422
  const metadata = await lstat(join(cwd, ...leaf.relativePath.split("/")), { bigint: true }).catch((error) => {
1384
1423
  if (hasErrorCode(error, "ENOENT")) return null;
1385
1424
  throw error;
1386
1425
  });
1387
- if (metadata === null || visibleLeafFingerprint(visibleLeafMetadataFromStats(metadata)) !== leaf.fingerprint) {
1426
+ if (metadata === null) {
1427
+ throw new SnapshotStoreError("capture_failed", `捕获期间工作区叶子已变化:${leaf.relativePath}`);
1428
+ }
1429
+ const visibleMetadata = visibleLeafMetadataFromStats(metadata);
1430
+ if (visibleLeafFingerprint(visibleMetadata) === leaf.fingerprint) return;
1431
+ if (visibleLeafStableFingerprint(visibleMetadata) !== leaf.stableFingerprint) {
1432
+ throw new SnapshotStoreError("capture_failed", `捕获期间工作区叶子已变化:${leaf.relativePath}`);
1433
+ }
1434
+ // 首次 hash 前还没有可信对象 ID;稳定元数据不变时先继续,hash 后再做内容证明。
1435
+ if (expectedObjectId === undefined || environment === undefined) return;
1436
+ await this.assertVisibleLeafContentUnchanged(cwd, leaf, expectedObjectId, environment);
1437
+ }
1438
+
1439
+ /** ctime 单独变化时重新核验内容,并确认内容核验期间稳定元数据没有漂移。 */
1440
+ private async assertVisibleLeafContentUnchanged(
1441
+ cwd: string,
1442
+ leaf: VisibleLeaf,
1443
+ expectedObjectId: string | undefined,
1444
+ environment: Readonly<Record<string, string | undefined>> | undefined,
1445
+ ): Promise<void> {
1446
+ if (expectedObjectId === undefined || environment === undefined) {
1447
+ throw new SnapshotStoreError("capture_failed", `捕获期间工作区叶子已变化:${leaf.relativePath}`);
1448
+ }
1449
+ let objectId: string;
1450
+ if (leaf.kind === "symlink") {
1451
+ await assertNoSymlinkEscape(cwd, leaf.relativePath);
1452
+ const linkText = await readlink(join(cwd, ...leaf.relativePath.split("/")), { encoding: "buffer" });
1453
+ decodeUtf8(linkText, "symlink target 不是可无损表示的 UTF-8");
1454
+ objectId = (await this.runGit(["hash-object", "--stdin"], {
1455
+ cwd,
1456
+ env: environment,
1457
+ stdin: linkText,
1458
+ })).trim();
1459
+ } else {
1460
+ objectId = (await this.runGit(["hash-object", "--no-filters", "--", leaf.relativePath], {
1461
+ cwd,
1462
+ env: environment,
1463
+ })).trim();
1464
+ }
1465
+ if (!isObjectId(objectId) || objectId !== expectedObjectId) {
1466
+ throw new SnapshotStoreError("capture_failed", `捕获期间工作区叶子已变化:${leaf.relativePath}`);
1467
+ }
1468
+ const finalMetadata = await lstat(join(cwd, ...leaf.relativePath.split("/")), { bigint: true }).catch((error) => {
1469
+ if (hasErrorCode(error, "ENOENT")) return null;
1470
+ throw error;
1471
+ });
1472
+ if (
1473
+ finalMetadata === null ||
1474
+ visibleLeafStableFingerprint(visibleLeafMetadataFromStats(finalMetadata)) !== leaf.stableFingerprint
1475
+ ) {
1388
1476
  throw new SnapshotStoreError("capture_failed", `捕获期间工作区叶子已变化:${leaf.relativePath}`);
1389
1477
  }
1390
1478
  }
@@ -1473,6 +1561,7 @@ export class SnapshotStore {
1473
1561
  ? 0o120000
1474
1562
  : (metadata.mode & 0o111n) === 0n ? 0o100644 : 0o100755,
1475
1563
  fingerprint: visibleLeafFingerprint(metadata),
1564
+ stableFingerprint: visibleLeafStableFingerprint(metadata),
1476
1565
  changedAtNs: metadata.mtimeNs > metadata.ctimeNs ? metadata.mtimeNs : metadata.ctimeNs,
1477
1566
  cacheable,
1478
1567
  });
@@ -2126,6 +2215,7 @@ function samePersistedLeafCache(
2126
2215
  existing.kind !== entry.kind ||
2127
2216
  existing.mode !== entry.mode ||
2128
2217
  existing.fingerprint !== entry.fingerprint ||
2218
+ existing.stableFingerprint !== entry.stableFingerprint ||
2129
2219
  existing.cacheable !== entry.cacheable ||
2130
2220
  existing.objectId !== entry.objectId ||
2131
2221
  existing.changedAtNs !== entry.changedAtNs ||
@@ -2257,6 +2347,7 @@ function isPersistedLeafCacheFile(value: unknown): value is PersistedLeafCacheFi
2257
2347
  typeof candidate.mode !== "number" ||
2258
2348
  typeof candidate.fingerprint !== "string" ||
2259
2349
  typeof candidate.cacheable !== "boolean" ||
2350
+ (typeof candidate.stableFingerprint !== "undefined" && typeof candidate.stableFingerprint !== "string") ||
2260
2351
  typeof candidate.objectId !== "string" ||
2261
2352
  typeof candidate.changedAtNs !== "string" ||
2262
2353
  typeof candidate.verifiedAtNs !== "string" ||
@@ -2270,6 +2361,17 @@ function isPersistedLeafCacheFile(value: unknown): value is PersistedLeafCacheFi
2270
2361
  return true;
2271
2362
  }
2272
2363
 
2364
+ function visibleLeafStableFingerprint(metadata: VisibleLeafMetadata): string {
2365
+ return checksum(canonicalJson({
2366
+ kind: metadata.kind,
2367
+ dev: metadata.dev.toString(),
2368
+ ino: metadata.ino.toString(),
2369
+ mode: metadata.mode.toString(),
2370
+ size: metadata.size.toString(),
2371
+ mtimeNs: metadata.mtimeNs.toString(),
2372
+ }));
2373
+ }
2374
+
2273
2375
  function visibleLeafFingerprint(metadata: VisibleLeafMetadata): string {
2274
2376
  return checksum(canonicalJson({
2275
2377
  kind: metadata.kind,