gsd-pi 2.33.0 → 2.33.1-dev.29a8268

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.
Files changed (34) hide show
  1. package/README.md +13 -18
  2. package/dist/resources/extensions/gsd/auto-supervisor.ts +10 -5
  3. package/dist/resources/extensions/gsd/auto-worktree.ts +135 -1
  4. package/dist/resources/extensions/gsd/commands.ts +14 -2
  5. package/dist/resources/extensions/gsd/git-service.ts +17 -11
  6. package/dist/resources/extensions/gsd/index.ts +13 -2
  7. package/dist/resources/extensions/gsd/session-lock.ts +80 -16
  8. package/dist/resources/extensions/gsd/tests/auto-dispatch-loop.test.ts +691 -0
  9. package/dist/resources/extensions/gsd/tests/cache-staleness-regression.test.ts +317 -0
  10. package/dist/resources/extensions/gsd/tests/git-service.test.ts +16 -7
  11. package/dist/resources/extensions/gsd/tests/loop-regression.test.ts +39 -1
  12. package/dist/resources/extensions/gsd/tests/roadmap-parse-regression.test.ts +358 -0
  13. package/dist/resources/extensions/gsd/tests/session-lock-regression.test.ts +216 -0
  14. package/dist/resources/extensions/gsd/tests/session-lock.test.ts +119 -0
  15. package/dist/resources/extensions/gsd/tests/worktree-sync-milestones.test.ts +206 -0
  16. package/package.json +1 -1
  17. package/packages/pi-coding-agent/package.json +1 -1
  18. package/pkg/package.json +1 -1
  19. package/src/resources/extensions/gsd/auto-supervisor.ts +10 -5
  20. package/src/resources/extensions/gsd/auto-worktree.ts +135 -1
  21. package/src/resources/extensions/gsd/commands.ts +14 -2
  22. package/src/resources/extensions/gsd/git-service.ts +17 -11
  23. package/src/resources/extensions/gsd/index.ts +13 -2
  24. package/src/resources/extensions/gsd/session-lock.ts +80 -16
  25. package/src/resources/extensions/gsd/tests/auto-dispatch-loop.test.ts +691 -0
  26. package/src/resources/extensions/gsd/tests/cache-staleness-regression.test.ts +317 -0
  27. package/src/resources/extensions/gsd/tests/git-service.test.ts +16 -7
  28. package/src/resources/extensions/gsd/tests/loop-regression.test.ts +39 -1
  29. package/src/resources/extensions/gsd/tests/roadmap-parse-regression.test.ts +358 -0
  30. package/src/resources/extensions/gsd/tests/session-lock-regression.test.ts +216 -0
  31. package/src/resources/extensions/gsd/tests/session-lock.test.ts +119 -0
  32. package/src/resources/extensions/gsd/tests/worktree-sync-milestones.test.ts +206 -0
  33. package/dist/resources/extensions/mcporter/extension-manifest.json +0 -12
  34. package/src/resources/extensions/mcporter/extension-manifest.json +0 -12
@@ -223,11 +223,22 @@ export default function (pi: ExtensionAPI) {
223
223
  // chance to persist state and pause instead of crashing (see issue #739).
224
224
  if (!process.listeners("uncaughtException").some(l => l.name === "_gsdEpipeGuard")) {
225
225
  const _gsdEpipeGuard = (err: Error): void => {
226
- if ((err as NodeJS.ErrnoException).code === "EPIPE") {
226
+ const code = (err as NodeJS.ErrnoException).code;
227
+ if (code === "EPIPE") {
227
228
  // Pipe closed — nothing we can write; just exit cleanly
228
229
  process.exit(0);
229
230
  }
230
- // Re-throw anything that isn't EPIPE so real crashes still surface
231
+ // ECOMPROMISED: proper-lockfile's update timer detected mtime drift (system
232
+ // sleep, heavy event loop stall, or filesystem precision mismatch on Node.js
233
+ // v25+). The onCompromised callback already set _lockCompromised = true, but
234
+ // due to a subtle interaction between the synchronous fs adapter and the
235
+ // setTimeout boundary, the error can still propagate here as an uncaught
236
+ // exception. Exit cleanly so the process.once("exit") handler removes the
237
+ // lock directory — allowing the next session to acquire cleanly (#1322).
238
+ if (code === "ECOMPROMISED") {
239
+ process.exit(1);
240
+ }
241
+ // Re-throw anything that isn't EPIPE or ECOMPROMISED so real crashes still surface
231
242
  throw err;
232
243
  };
233
244
  process.on("uncaughtException", _gsdEpipeGuard);
@@ -17,7 +17,7 @@
17
17
  */
18
18
 
19
19
  import { createRequire } from "node:module";
20
- import { existsSync, readFileSync, mkdirSync, unlinkSync, rmSync, statSync } from "node:fs";
20
+ import { existsSync, readFileSync, readdirSync, mkdirSync, unlinkSync, rmSync, statSync } from "node:fs";
21
21
  import { join, dirname } from "node:path";
22
22
  import { gsdRoot } from "./paths.js";
23
23
  import { atomicWriteSync } from "./atomic-write.js";
@@ -54,12 +54,81 @@ let _lockPid: number = 0;
54
54
  /** Set to true when proper-lockfile fires onCompromised (mtime drift, sleep, etc.). */
55
55
  let _lockCompromised: boolean = false;
56
56
 
57
+ /** Whether we've already registered a process.on('exit') handler. */
58
+ let _exitHandlerRegistered: boolean = false;
59
+
57
60
  const LOCK_FILE = "auto.lock";
58
61
 
59
62
  function lockPath(basePath: string): string {
60
63
  return join(gsdRoot(basePath), LOCK_FILE);
61
64
  }
62
65
 
66
+ // ─── Stray Lock Cleanup ─────────────────────────────────────────────────────
67
+
68
+ /**
69
+ * Remove numbered lock file variants (e.g. "auto 2.lock", "auto 3.lock")
70
+ * that accumulate from macOS file conflict resolution (iCloud/Dropbox/OneDrive)
71
+ * or other filesystem-level copy-on-conflict behavior (#1315).
72
+ *
73
+ * Also removes stray proper-lockfile directories beyond the canonical `.gsd.lock/`.
74
+ */
75
+ export function cleanupStrayLockFiles(basePath: string): void {
76
+ const gsdDir = gsdRoot(basePath);
77
+
78
+ // Clean numbered auto lock files inside .gsd/
79
+ try {
80
+ if (existsSync(gsdDir)) {
81
+ for (const entry of readdirSync(gsdDir)) {
82
+ // Match "auto <N>.lock" or "auto (<N>).lock" variants but NOT the canonical "auto.lock"
83
+ if (entry !== LOCK_FILE && /^auto\s.+\.lock$/i.test(entry)) {
84
+ try { unlinkSync(join(gsdDir, entry)); } catch { /* best-effort */ }
85
+ }
86
+ }
87
+ }
88
+ } catch { /* non-fatal: directory read failure */ }
89
+
90
+ // Clean stray proper-lockfile directories (e.g. ".gsd 2.lock/")
91
+ // The canonical one is ".gsd.lock/" — anything else is stray.
92
+ try {
93
+ const parentDir = dirname(gsdDir);
94
+ const gsdDirName = gsdDir.split("/").pop() || ".gsd";
95
+ if (existsSync(parentDir)) {
96
+ for (const entry of readdirSync(parentDir)) {
97
+ // Match ".gsd <N>.lock" or ".gsd (<N>).lock" directories but NOT ".gsd.lock"
98
+ if (entry !== `${gsdDirName}.lock` && entry.startsWith(gsdDirName) && entry.endsWith(".lock")) {
99
+ const fullPath = join(parentDir, entry);
100
+ try {
101
+ const stat = statSync(fullPath);
102
+ if (stat.isDirectory()) {
103
+ rmSync(fullPath, { recursive: true, force: true });
104
+ }
105
+ } catch { /* best-effort */ }
106
+ }
107
+ }
108
+ }
109
+ } catch { /* non-fatal */ }
110
+ }
111
+
112
+ /**
113
+ * Register a single process exit handler that cleans up lock state.
114
+ * Uses module-level references so it always operates on current state.
115
+ * Only registers once — subsequent calls are no-ops.
116
+ */
117
+ function ensureExitHandler(gsdDir: string): void {
118
+ if (_exitHandlerRegistered) return;
119
+ _exitHandlerRegistered = true;
120
+
121
+ process.once("exit", () => {
122
+ try {
123
+ if (_releaseFunction) { _releaseFunction(); _releaseFunction = null; }
124
+ } catch { /* best-effort */ }
125
+ try {
126
+ const lockDir = join(gsdDir + ".lock");
127
+ if (existsSync(lockDir)) rmSync(lockDir, { recursive: true, force: true });
128
+ } catch { /* best-effort */ }
129
+ });
130
+ }
131
+
63
132
  // ─── Public API ─────────────────────────────────────────────────────────────
64
133
 
65
134
  /**
@@ -77,6 +146,9 @@ export function acquireSessionLock(basePath: string): SessionLockResult {
77
146
  // Ensure the directory exists
78
147
  mkdirSync(dirname(lp), { recursive: true });
79
148
 
149
+ // Clean up numbered lock file variants from cloud sync conflicts (#1315)
150
+ cleanupStrayLockFiles(basePath);
151
+
80
152
  // Write our lock data first (the content is informational; the OS lock is the real guard)
81
153
  const lockData: SessionLockData = {
82
154
  pid: process.pid,
@@ -124,15 +196,7 @@ export function acquireSessionLock(basePath: string): SessionLockResult {
124
196
 
125
197
  // Safety net: clean up lock dir on process exit if _releaseFunction
126
198
  // wasn't called (e.g., normal exit after clean completion) (#1245).
127
- const lockDirForCleanup = join(gsdDir + ".lock");
128
- process.once("exit", () => {
129
- try {
130
- if (_releaseFunction) { _releaseFunction(); _releaseFunction = null; }
131
- } catch { /* best-effort */ }
132
- try {
133
- if (existsSync(lockDirForCleanup)) rmSync(lockDirForCleanup, { recursive: true, force: true });
134
- } catch { /* best-effort */ }
135
- });
199
+ ensureExitHandler(gsdDir);
136
200
 
137
201
  // Write the informational lock data
138
202
  atomicWriteSync(lp, JSON.stringify(lockData, null, 2));
@@ -158,18 +222,15 @@ export function acquireSessionLock(basePath: string): SessionLockResult {
158
222
  update: 10_000,
159
223
  onCompromised: () => {
160
224
  _lockCompromised = true;
225
+ _releaseFunction = null;
161
226
  },
162
227
  });
163
228
  _releaseFunction = release;
164
229
  _lockedPath = basePath;
165
230
  _lockPid = process.pid;
166
231
 
167
- // Safety net for retry path too
168
- const retryLockDir = join(gsdDir + ".lock");
169
- process.once("exit", () => {
170
- try { if (_releaseFunction) { _releaseFunction(); _releaseFunction = null; } } catch {}
171
- try { if (existsSync(retryLockDir)) rmSync(retryLockDir, { recursive: true, force: true }); } catch {}
172
- });
232
+ // Safety net — uses centralized handler to avoid double-registration
233
+ ensureExitHandler(gsdDir);
173
234
 
174
235
  atomicWriteSync(lp, JSON.stringify(lockData, null, 2));
175
236
  return { acquired: true };
@@ -310,6 +371,9 @@ export function releaseSessionLock(basePath: string): void {
310
371
  // Non-fatal
311
372
  }
312
373
 
374
+ // Clean up numbered lock file variants from cloud sync conflicts (#1315)
375
+ cleanupStrayLockFiles(basePath);
376
+
313
377
  _lockedPath = null;
314
378
  _lockPid = 0;
315
379
  _lockCompromised = false;