@narumitw/pi-worktree 0.51.8 → 0.51.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-worktree",
3
- "version": "0.51.8",
3
+ "version": "0.51.10",
4
4
  "description": "Pi extension for safe interactive Git worktree management and workspace switching.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -17,6 +17,7 @@
17
17
  "src",
18
18
  "dist",
19
19
  "README.md",
20
+ "CHANGELOG.md",
20
21
  "LICENSE"
21
22
  ],
22
23
  "pi": {
@@ -35,8 +36,8 @@
35
36
  "@earendil-works/pi-coding-agent": "*"
36
37
  },
37
38
  "devDependencies": {
38
- "@earendil-works/pi-coding-agent": "0.86.0",
39
- "@types/node": "26.6.2",
39
+ "@earendil-works/pi-coding-agent": "1.5.0",
40
+ "@types/node": "26.6.6",
40
41
  "esbuild": "0.28.2",
41
42
  "typescript": "7.0.2"
42
43
  },
@@ -46,6 +47,6 @@
46
47
  "directory": "packages/pi-worktree"
47
48
  },
48
49
  "dependencies": {
49
- "@narumitw/pi-tui-kit": "^0.59.0"
50
+ "@narumitw/pi-tui-kit": "^0.70.0"
50
51
  }
51
52
  }
package/src/git.ts CHANGED
@@ -5,6 +5,11 @@ import type { ExecResult, ExtensionAPI } from "@earendil-works/pi-coding-agent";
5
5
 
6
6
  const GIT_TIMEOUT_MS = 15_000;
7
7
  const GIT_MUTATION_TIMEOUT_MS = 60_000;
8
+ // Metadata is a path, ref, OID, or boolean; record queries need room for large repositories.
9
+ const GIT_METADATA_MAX_BUFFER = 64 * 1024;
10
+ const GIT_RECORDS_MAX_BUFFER = 8 * 1024 * 1024;
11
+ // Mutations can emit progress and hook diagnostics, but their output is not parsed as records.
12
+ const GIT_MUTATION_MAX_BUFFER = 1024 * 1024;
8
13
  const LOCAL_BRANCH_PREFIX = "refs/heads/";
9
14
 
10
15
  export interface WorktreeRecord {
@@ -41,6 +46,7 @@ interface GitExecOptions {
41
46
  cwd?: string;
42
47
  signal?: AbortSignal;
43
48
  timeout?: number;
49
+ maxBuffer?: number;
44
50
  }
45
51
 
46
52
  export class GitWorktreeError extends Error {
@@ -190,7 +196,7 @@ export async function listWorktrees(
190
196
  cwd: string,
191
197
  signal?: AbortSignal,
192
198
  ): Promise<WorktreeRecord[]> {
193
- const result = await runGit(pi, ["worktree", "list", "--porcelain", "-z"], cwd, signal);
199
+ const result = await runGit(pi, ["worktree", "list", "--porcelain", "-z"], cwd, GIT_RECORDS_MAX_BUFFER, signal);
194
200
  return parseWorktreePorcelain(result.stdout);
195
201
  }
196
202
 
@@ -199,7 +205,7 @@ export async function currentWorktreePath(
199
205
  cwd: string,
200
206
  signal?: AbortSignal,
201
207
  ): Promise<string> {
202
- const result = await runGit(pi, ["rev-parse", "--show-toplevel"], cwd, signal);
208
+ const result = await runGit(pi, ["rev-parse", "--show-toplevel"], cwd, GIT_METADATA_MAX_BUFFER, signal);
203
209
  const path = removeLineEnding(result.stdout);
204
210
  if (!path) throw new GitWorktreeError("Git did not return the current worktree path.");
205
211
  return pathIdentity(path);
@@ -210,7 +216,13 @@ export async function symbolicBranch(
210
216
  cwd: string,
211
217
  signal?: AbortSignal,
212
218
  ): Promise<string | undefined> {
213
- const result = await runGitAllowFailure(pi, ["symbolic-ref", "--quiet", "--short", "HEAD"], cwd, signal);
219
+ const result = await runGitAllowFailure(
220
+ pi,
221
+ ["symbolic-ref", "--quiet", "--short", "HEAD"],
222
+ cwd,
223
+ GIT_METADATA_MAX_BUFFER,
224
+ signal,
225
+ );
214
226
  if (result.killed) throw killedError(["symbolic-ref", "--quiet", "--short", "HEAD"]);
215
227
  if (result.code !== 0) return undefined;
216
228
  return result.stdout.trim() || undefined;
@@ -222,7 +234,7 @@ export async function validateBranch(
222
234
  branch: string,
223
235
  signal?: AbortSignal,
224
236
  ): Promise<string> {
225
- const result = await runGit(pi, ["check-ref-format", "--branch", branch], cwd, signal);
237
+ const result = await runGit(pi, ["check-ref-format", "--branch", branch], cwd, GIT_METADATA_MAX_BUFFER, signal);
226
238
  const normalized = result.stdout.trim();
227
239
  if (!normalized) throw new GitWorktreeError("Git returned an empty branch name.");
228
240
  return normalized;
@@ -238,6 +250,7 @@ export async function localBranchExists(
238
250
  pi,
239
251
  ["show-ref", "--verify", "--quiet", `${LOCAL_BRANCH_PREFIX}${branch}`],
240
252
  cwd,
253
+ GIT_METADATA_MAX_BUFFER,
241
254
  signal,
242
255
  );
243
256
  if (result.killed) throw killedError(["show-ref", "--verify", "--quiet"]);
@@ -252,7 +265,13 @@ export async function resolveCommit(
252
265
  startPoint: string,
253
266
  signal?: AbortSignal,
254
267
  ): Promise<string> {
255
- const result = await runGit(pi, ["rev-parse", "--verify", "--end-of-options", `${startPoint}^{commit}`], cwd, signal);
268
+ const result = await runGit(
269
+ pi,
270
+ ["rev-parse", "--verify", "--end-of-options", `${startPoint}^{commit}`],
271
+ cwd,
272
+ GIT_METADATA_MAX_BUFFER,
273
+ signal,
274
+ );
256
275
  const oid = result.stdout.trim();
257
276
  if (!/^[0-9a-fA-F]{40,64}$/u.test(oid)) {
258
277
  throw new GitWorktreeError(`Git returned an invalid commit object for ${startPoint}.`);
@@ -266,7 +285,7 @@ export async function addWorktree(
266
285
  input: AddArguments,
267
286
  signal?: AbortSignal,
268
287
  ): Promise<void> {
269
- await runGit(pi, buildAddArguments(input), cwd, signal, GIT_MUTATION_TIMEOUT_MS);
288
+ await runGit(pi, buildAddArguments(input), cwd, GIT_MUTATION_MAX_BUFFER, signal, GIT_MUTATION_TIMEOUT_MS);
270
289
  }
271
290
 
272
291
  export async function removeWorktree(
@@ -275,7 +294,7 @@ export async function removeWorktree(
275
294
  path: string,
276
295
  signal?: AbortSignal,
277
296
  ): Promise<void> {
278
- await runGit(pi, ["worktree", "remove", path], cwd, signal, GIT_MUTATION_TIMEOUT_MS);
297
+ await runGit(pi, ["worktree", "remove", path], cwd, GIT_MUTATION_MAX_BUFFER, signal, GIT_MUTATION_TIMEOUT_MS);
279
298
  }
280
299
 
281
300
  export async function worktreeInventory(
@@ -290,10 +309,16 @@ export async function worktreeInventory(
290
309
  "--ignored=matching",
291
310
  "--ignore-submodules=none",
292
311
  ];
293
- const status = await runGit(pi, statusArgs, path, signal);
294
- const indexFlags = await runGit(pi, ["ls-files", "-v", "-z"], path, signal);
312
+ const status = await runGit(pi, statusArgs, path, GIT_RECORDS_MAX_BUFFER, signal);
313
+ const indexFlags = await runGit(pi, ["ls-files", "-v", "-z"], path, GIT_RECORDS_MAX_BUFFER, signal);
295
314
  const indexInventory = await indexFlagInventory(pi, indexFlags.stdout, path, signal);
296
- const submoduleStatus = await runGit(pi, ["submodule", "status", "--recursive"], path, signal);
315
+ const submoduleStatus = await runGit(
316
+ pi,
317
+ ["submodule", "status", "--recursive"],
318
+ path,
319
+ GIT_RECORDS_MAX_BUFFER,
320
+ signal,
321
+ );
297
322
  const initializedSubmodules = nonEmptyLines(submoduleStatus.stdout)
298
323
  .filter((line) => !line.startsWith("-"))
299
324
  .map((line) => `initialized submodule: ${line.slice(1).trimStart()}`);
@@ -307,6 +332,7 @@ export async function worktreeInventory(
307
332
  "git status --porcelain=v1 --untracked-files=all --ignored=matching --ignore-submodules=none",
308
333
  ],
309
334
  path,
335
+ GIT_RECORDS_MAX_BUFFER,
310
336
  signal,
311
337
  );
312
338
  return [
@@ -322,7 +348,13 @@ export async function worktreeAdministrativeDirectory(
322
348
  cwd: string,
323
349
  signal?: AbortSignal,
324
350
  ): Promise<string> {
325
- const result = await runGit(pi, ["rev-parse", "--path-format=absolute", "--git-dir"], cwd, signal);
351
+ const result = await runGit(
352
+ pi,
353
+ ["rev-parse", "--path-format=absolute", "--git-dir"],
354
+ cwd,
355
+ GIT_METADATA_MAX_BUFFER,
356
+ signal,
357
+ );
326
358
  const value = removeLineEnding(result.stdout);
327
359
  if (!value) throw new GitWorktreeError("Git did not return its worktree administrative path.");
328
360
  return resolve(cwd, value);
@@ -341,6 +373,7 @@ export async function administrativeHistoryOids(
341
373
  pi,
342
374
  [gitDirArgument, "for-each-ref", "--format=%(objectname)", "refs/worktree", "refs/bisect"],
343
375
  cwd,
376
+ GIT_RECORDS_MAX_BUFFER,
344
377
  signal,
345
378
  );
346
379
  values.push(...splitAdministrativeOids(refs.stdout, "per-worktree refs"));
@@ -362,7 +395,13 @@ export async function administrativePruneCandidates(
362
395
  cwd: string,
363
396
  signal?: AbortSignal,
364
397
  ): Promise<AdministrativePruneCandidate[]> {
365
- const commonResult = await runGit(pi, ["rev-parse", "--path-format=absolute", "--git-common-dir"], cwd, signal);
398
+ const commonResult = await runGit(
399
+ pi,
400
+ ["rev-parse", "--path-format=absolute", "--git-common-dir"],
401
+ cwd,
402
+ GIT_METADATA_MAX_BUFFER,
403
+ signal,
404
+ );
366
405
  const commonValue = removeLineEnding(commonResult.stdout);
367
406
  if (!commonValue) throw new GitWorktreeError("Git did not return its common directory.");
368
407
  const commonDirectory = resolve(cwd, commonValue);
@@ -443,7 +482,7 @@ async function administrativeIndexIsDirty(
443
482
  "--ignore-submodules=none",
444
483
  "--",
445
484
  ];
446
- const result = await runGitAllowFailure(pi, args, cwd, signal);
485
+ const result = await runGitAllowFailure(pi, args, cwd, GIT_METADATA_MAX_BUFFER, signal);
447
486
  if (result.killed) throw killedError(args);
448
487
  if (result.code === 0) return false;
449
488
  if (result.code === 1) return true;
@@ -459,7 +498,13 @@ export async function durableRefExists(
459
498
  if (!ref.startsWith("refs/") || ref.includes("\0")) {
460
499
  throw new GitWorktreeError("Git worktree administrative HEAD contains an invalid ref.");
461
500
  }
462
- const result = await runGitAllowFailure(pi, ["show-ref", "--verify", "--quiet", ref], cwd, signal);
501
+ const result = await runGitAllowFailure(
502
+ pi,
503
+ ["show-ref", "--verify", "--quiet", ref],
504
+ cwd,
505
+ GIT_METADATA_MAX_BUFFER,
506
+ signal,
507
+ );
463
508
  if (result.killed) throw killedError(["show-ref", "--verify", "--quiet"]);
464
509
  if (result.code === 0) return true;
465
510
  if (result.code === 1) return false;
@@ -479,13 +524,14 @@ export async function durableRefsContaining(
479
524
  pi,
480
525
  ["for-each-ref", "--format=%(refname)", `--contains=${head}`, "refs/heads", "refs/tags", "refs/remotes"],
481
526
  cwd,
527
+ GIT_RECORDS_MAX_BUFFER,
482
528
  signal,
483
529
  );
484
530
  return nonEmptyLines(result.stdout);
485
531
  }
486
532
 
487
533
  export async function prunePreview(pi: Pick<ExtensionAPI, "exec">, cwd: string, signal?: AbortSignal): Promise<string> {
488
- const result = await runGit(pi, ["worktree", "prune", "--dry-run", "--verbose"], cwd, signal);
534
+ const result = await runGit(pi, ["worktree", "prune", "--dry-run", "--verbose"], cwd, GIT_RECORDS_MAX_BUFFER, signal);
489
535
  return combineOutput(result);
490
536
  }
491
537
 
@@ -494,7 +540,14 @@ export async function pruneWorktrees(
494
540
  cwd: string,
495
541
  signal?: AbortSignal,
496
542
  ): Promise<string> {
497
- const result = await runGit(pi, ["worktree", "prune", "--verbose"], cwd, signal, GIT_MUTATION_TIMEOUT_MS);
543
+ const result = await runGit(
544
+ pi,
545
+ ["worktree", "prune", "--verbose"],
546
+ cwd,
547
+ GIT_MUTATION_MAX_BUFFER,
548
+ signal,
549
+ GIT_MUTATION_TIMEOUT_MS,
550
+ );
498
551
  return combineOutput(result);
499
552
  }
500
553
 
@@ -526,10 +579,11 @@ async function runGit(
526
579
  pi: Pick<ExtensionAPI, "exec">,
527
580
  args: string[],
528
581
  cwd: string,
582
+ maxBuffer: number,
529
583
  signal?: AbortSignal,
530
584
  timeout = GIT_TIMEOUT_MS,
531
585
  ): Promise<ExecResult> {
532
- const result = await runGitAllowFailure(pi, args, cwd, signal, timeout);
586
+ const result = await runGitAllowFailure(pi, args, cwd, maxBuffer, signal, timeout);
533
587
  if (result.killed) throw killedError(args);
534
588
  if (result.code !== 0) throw gitFailure(args, result);
535
589
  return result;
@@ -539,11 +593,13 @@ async function runGitAllowFailure(
539
593
  pi: Pick<ExtensionAPI, "exec">,
540
594
  args: string[],
541
595
  cwd: string,
596
+ maxBuffer: number,
542
597
  signal?: AbortSignal,
543
598
  timeout = GIT_TIMEOUT_MS,
544
599
  ): Promise<ExecResult> {
600
+ let result: ExecResult;
545
601
  try {
546
- return await pi.exec("git", args, { cwd, signal, timeout });
602
+ result = await pi.exec("git", args, { cwd, signal, timeout, maxBuffer });
547
603
  } catch (error) {
548
604
  const message = formatError(error);
549
605
  if (/\bENOENT\b|not found/i.test(message)) {
@@ -551,6 +607,16 @@ async function runGitAllowFailure(
551
607
  }
552
608
  throw new GitWorktreeError(`Could not start git ${args.slice(0, 2).join(" ")}: ${message}`, args);
553
609
  }
610
+ // Even injected executors must not let incomplete data reach parsing or nonzero-exit fallbacks.
611
+ if (result.truncated) throw outputLimitError(args, maxBuffer);
612
+ return result;
613
+ }
614
+
615
+ function outputLimitError(args: string[], maxBuffer: number): GitWorktreeError {
616
+ return new GitWorktreeError(
617
+ `git ${args.slice(0, 2).join(" ")} output exceeded the ${maxBuffer}-byte limit. Partial output was not used; inspect Git state before retrying.`,
618
+ args,
619
+ );
554
620
  }
555
621
 
556
622
  function gitFailure(args: string[], result: ExecResult): GitWorktreeError {
@@ -624,13 +690,14 @@ async function sparseManagedSkipWorktreePaths(
624
690
  ): Promise<ReadonlySet<string>> {
625
691
  if (paths.length === 0) return new Set();
626
692
  const configArgs = ["config", "--bool", "--get", "core.sparseCheckout"];
627
- const config = await runGitAllowFailure(pi, configArgs, cwd, signal);
693
+ const config = await runGitAllowFailure(pi, configArgs, cwd, GIT_METADATA_MAX_BUFFER, signal);
628
694
  if (config.killed) throw killedError(configArgs);
629
695
  if (config.code !== 0 || config.stdout.trim() !== "true") return new Set();
630
696
 
631
697
  const candidates = new Set(paths);
632
698
  const checkArgs = ["sparse-checkout", "check-rules", "-z"];
633
699
  const checked = await runGitWithInputAllowFailure(checkArgs, cwd, `${[...candidates].join("\0")}\0`, signal);
700
+ if (checked.truncated) throw outputLimitError(checkArgs, GIT_RECORDS_MAX_BUFFER);
634
701
  if (checked.killed) throw killedError(checkArgs);
635
702
  // Older Git versions lack check-rules; retain every flag rather than guessing.
636
703
  if (checked.code !== 0) return new Set();
@@ -650,7 +717,7 @@ function runGitWithInputAllowFailure(
650
717
  timeout = GIT_TIMEOUT_MS,
651
718
  ): Promise<ExecResult> {
652
719
  if (signal?.aborted) {
653
- return Promise.resolve({ stdout: "", stderr: "", code: 1, killed: true });
720
+ return Promise.resolve({ stdout: "", stderr: "", code: 1, killed: true, truncated: false });
654
721
  }
655
722
  return new Promise((resolveResult, reject) => {
656
723
  // ExtensionAPI.exec has no stdin channel, so this read-only check uses an argv-only child.
@@ -658,6 +725,8 @@ function runGitWithInputAllowFailure(
658
725
  let stdout = "";
659
726
  let stderr = "";
660
727
  let killed = false;
728
+ let truncated = false;
729
+ let outputBytes = 0;
661
730
  let settled = false;
662
731
  const finish = (result: ExecResult) => {
663
732
  if (settled) return;
@@ -684,20 +753,36 @@ function runGitWithInputAllowFailure(
684
753
  child.kill();
685
754
  };
686
755
  const timeoutHandle = setTimeout(stop, timeout);
756
+ const acceptChunk = (chunk: string): boolean => {
757
+ if (truncated) return false;
758
+ outputBytes += Buffer.byteLength(chunk);
759
+ if (outputBytes <= GIT_RECORDS_MAX_BUFFER) return true;
760
+ truncated = true;
761
+ child.stdout.destroy();
762
+ child.stderr.destroy();
763
+ stop();
764
+ return false;
765
+ };
687
766
  child.stdout.setEncoding("utf8");
688
767
  child.stderr.setEncoding("utf8");
689
768
  child.stdout.on("data", (chunk: string) => {
690
- stdout += chunk;
769
+ if (acceptChunk(chunk)) stdout += chunk;
691
770
  });
692
771
  child.stderr.on("data", (chunk: string) => {
693
- stderr += chunk;
772
+ if (acceptChunk(chunk)) stderr += chunk;
694
773
  });
695
774
  child.stdin.on("error", (error) => {
696
775
  if (!isNodeError(error) || error.code !== "EPIPE") fail(error);
697
776
  });
698
777
  child.once("error", fail);
699
778
  child.once("close", (code, closeSignal) => {
700
- finish({ stdout, stderr, code: code ?? 1, killed: killed || closeSignal !== null });
779
+ finish({
780
+ stdout,
781
+ stderr,
782
+ code: truncated && code === 0 ? 1 : (code ?? 1),
783
+ killed: killed || closeSignal !== null,
784
+ truncated,
785
+ });
701
786
  });
702
787
  signal?.addEventListener("abort", stop, { once: true });
703
788
  if (signal?.aborted) stop();
package/src/status.ts CHANGED
@@ -3,6 +3,9 @@ import type { ExecResult, ExtensionAPI } from "@earendil-works/pi-coding-agent";
3
3
  import { pathsEqual, stripTerminalControls, type WorktreeRecord } from "./git.js";
4
4
 
5
5
  const GIT_STATUS_TIMEOUT_MS = 15_000;
6
+ // Status can contain every repository path; the last-commit query returns only a date and subject.
7
+ const GIT_STATUS_MAX_BUFFER = 8 * 1024 * 1024;
8
+ const GIT_LAST_COMMIT_MAX_BUFFER = 64 * 1024;
6
9
  const DEFAULT_STATUS_CONCURRENCY = 4;
7
10
  const OID_PATTERN = /^[0-9a-fA-F]{40,64}$/u;
8
11
  const STATUS_XY_PATTERN = /^[.MADRCUT]{2}$/u;
@@ -203,6 +206,7 @@ export async function loadWorktreeStatusCards(
203
206
  pi,
204
207
  ["status", "--porcelain=v2", "--branch", "-z", "--untracked-files=all", "--ignore-submodules=none"],
205
208
  record.path,
209
+ GIT_STATUS_MAX_BUFFER,
206
210
  signal,
207
211
  );
208
212
  const snapshot = parseWorktreeStatusPorcelain(status.stdout);
@@ -213,6 +217,7 @@ export async function loadWorktreeStatusCards(
213
217
  pi,
214
218
  ["show", "-s", "--format=%aI%x00%s", "--end-of-options", snapshot.headOid],
215
219
  record.path,
220
+ GIT_LAST_COMMIT_MAX_BUFFER,
216
221
  signal,
217
222
  )
218
223
  ).stdout,
@@ -314,10 +319,14 @@ async function runGit(
314
319
  pi: Pick<ExtensionAPI, "exec">,
315
320
  args: string[],
316
321
  cwd: string,
322
+ maxBuffer: number,
317
323
  signal?: AbortSignal,
318
324
  ): Promise<ExecResult> {
319
325
  throwIfAborted(signal);
320
- const result = await pi.exec("git", args, { cwd, signal, timeout: GIT_STATUS_TIMEOUT_MS });
326
+ const result = await pi.exec("git", args, { cwd, signal, timeout: GIT_STATUS_TIMEOUT_MS, maxBuffer });
327
+ if (result.truncated) {
328
+ throw new Error(`git ${args.slice(0, 2).join(" ")} output exceeded the ${maxBuffer}-byte limit.`);
329
+ }
321
330
  if (result.killed) {
322
331
  throwIfAborted(signal);
323
332
  throw new Error(`git ${args.slice(0, 2).join(" ")} timed out.`);