@davideasden/pi-undo 0.2.30 → 0.2.31

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.31",
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",