gsd-pi 2.28.0-dev.b23c118 → 2.28.0-dev.e19bf89

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 (25) hide show
  1. package/dist/resources/extensions/gsd/auto-post-unit.ts +3 -8
  2. package/dist/resources/extensions/gsd/auto-start.ts +9 -24
  3. package/dist/resources/extensions/gsd/auto.ts +2 -45
  4. package/dist/resources/extensions/gsd/commands.ts +0 -19
  5. package/dist/resources/extensions/gsd/doctor-types.ts +0 -13
  6. package/dist/resources/extensions/gsd/doctor.ts +6 -2
  7. package/dist/resources/extensions/gsd/export.ts +2 -28
  8. package/dist/resources/extensions/gsd/gsd-db.ts +0 -19
  9. package/package.json +3 -3
  10. package/src/resources/extensions/gsd/auto-post-unit.ts +3 -8
  11. package/src/resources/extensions/gsd/auto-start.ts +9 -24
  12. package/src/resources/extensions/gsd/auto.ts +2 -45
  13. package/src/resources/extensions/gsd/commands.ts +0 -19
  14. package/src/resources/extensions/gsd/doctor-types.ts +0 -13
  15. package/src/resources/extensions/gsd/doctor.ts +6 -2
  16. package/src/resources/extensions/gsd/export.ts +2 -28
  17. package/src/resources/extensions/gsd/gsd-db.ts +0 -19
  18. package/dist/resources/extensions/gsd/commands-logs.ts +0 -537
  19. package/dist/resources/extensions/gsd/session-lock.ts +0 -284
  20. package/dist/resources/extensions/gsd/tests/commands-logs.test.ts +0 -241
  21. package/dist/resources/extensions/gsd/tests/session-lock.test.ts +0 -315
  22. package/src/resources/extensions/gsd/commands-logs.ts +0 -537
  23. package/src/resources/extensions/gsd/session-lock.ts +0 -284
  24. package/src/resources/extensions/gsd/tests/commands-logs.test.ts +0 -241
  25. package/src/resources/extensions/gsd/tests/session-lock.test.ts +0 -315
@@ -1,315 +0,0 @@
1
- import test from "node:test";
2
- import assert from "node:assert/strict";
3
- import { mkdirSync, mkdtempSync, writeFileSync, existsSync, readFileSync, rmSync } from "node:fs";
4
- import { join } from "node:path";
5
- import { tmpdir } from "node:os";
6
-
7
- import {
8
- acquireSessionLock,
9
- releaseSessionLock,
10
- updateSessionLock,
11
- validateSessionLock,
12
- readSessionLockData,
13
- isSessionLockHeld,
14
- isSessionLockProcessAlive,
15
- } from "../session-lock.ts";
16
-
17
- // ─── acquireSessionLock ──────────────────────────────────────────────────
18
-
19
- test("acquireSessionLock succeeds on empty directory", () => {
20
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
21
- mkdirSync(join(dir, ".gsd"), { recursive: true });
22
-
23
- const result = acquireSessionLock(dir);
24
- assert.equal(result.acquired, true, "should acquire lock on empty dir");
25
-
26
- // Verify lock file was created with correct data
27
- const lockPath = join(dir, ".gsd", "auto.lock");
28
- assert.ok(existsSync(lockPath), "auto.lock should exist after acquire");
29
-
30
- const data = JSON.parse(readFileSync(lockPath, "utf-8"));
31
- assert.equal(data.pid, process.pid, "lock should contain current PID");
32
- assert.equal(data.unitType, "starting", "initial unit type should be 'starting'");
33
-
34
- releaseSessionLock(dir);
35
- rmSync(dir, { recursive: true, force: true });
36
- });
37
-
38
- test("acquireSessionLock rejects when another live process holds lock", () => {
39
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
40
- mkdirSync(join(dir, ".gsd"), { recursive: true });
41
-
42
- // Simulate another process holding the lock by writing a lock with parent PID
43
- const fakeLockData = {
44
- pid: process.ppid,
45
- startedAt: new Date().toISOString(),
46
- unitType: "execute-task",
47
- unitId: "M001/S01/T01",
48
- unitStartedAt: new Date().toISOString(),
49
- completedUnits: 2,
50
- };
51
- writeFileSync(join(dir, ".gsd", "auto.lock"), JSON.stringify(fakeLockData, null, 2));
52
-
53
- // First acquire to set up proper-lockfile state
54
- const result1 = acquireSessionLock(dir);
55
-
56
- // If proper-lockfile is available, it should manage the OS lock.
57
- // If not (fallback mode), the PID check should detect the live process.
58
- // Either way, we can't fully simulate another process holding an OS lock
59
- // from within the same process, so we test the fallback path.
60
- if (result1.acquired) {
61
- // We got the lock (proper-lockfile saw no OS lock from another process)
62
- // This is expected since we're in the same process
63
- releaseSessionLock(dir);
64
- }
65
-
66
- rmSync(dir, { recursive: true, force: true });
67
- });
68
-
69
- test("acquireSessionLock takes over stale lock from dead process", () => {
70
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
71
- mkdirSync(join(dir, ".gsd"), { recursive: true });
72
-
73
- // Write a lock from a dead process
74
- const staleLockData = {
75
- pid: 9999999,
76
- startedAt: "2026-03-01T00:00:00Z",
77
- unitType: "execute-task",
78
- unitId: "M001/S01/T01",
79
- unitStartedAt: "2026-03-01T00:00:00Z",
80
- completedUnits: 0,
81
- };
82
- writeFileSync(join(dir, ".gsd", "auto.lock"), JSON.stringify(staleLockData, null, 2));
83
-
84
- const result = acquireSessionLock(dir);
85
- assert.equal(result.acquired, true, "should take over lock from dead process");
86
-
87
- // Verify our PID is now in the lock
88
- const data = readSessionLockData(dir);
89
- assert.ok(data, "lock data should exist after acquire");
90
- assert.equal(data!.pid, process.pid, "lock should contain our PID now");
91
-
92
- releaseSessionLock(dir);
93
- rmSync(dir, { recursive: true, force: true });
94
- });
95
-
96
- // ─── releaseSessionLock ─────────────────────────────────────────────────
97
-
98
- test("releaseSessionLock removes the lock file", () => {
99
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
100
- mkdirSync(join(dir, ".gsd"), { recursive: true });
101
-
102
- const result = acquireSessionLock(dir);
103
- assert.equal(result.acquired, true);
104
-
105
- releaseSessionLock(dir);
106
-
107
- const lockPath = join(dir, ".gsd", "auto.lock");
108
- assert.ok(!existsSync(lockPath), "auto.lock should be removed after release");
109
-
110
- rmSync(dir, { recursive: true, force: true });
111
- });
112
-
113
- test("releaseSessionLock is safe when no lock exists", () => {
114
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
115
- mkdirSync(join(dir, ".gsd"), { recursive: true });
116
-
117
- // Should not throw
118
- releaseSessionLock(dir);
119
-
120
- rmSync(dir, { recursive: true, force: true });
121
- });
122
-
123
- // ─── updateSessionLock ──────────────────────────────────────────────────
124
-
125
- test("updateSessionLock updates the lock data without re-acquiring", () => {
126
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
127
- mkdirSync(join(dir, ".gsd"), { recursive: true });
128
-
129
- const result = acquireSessionLock(dir);
130
- assert.equal(result.acquired, true);
131
-
132
- updateSessionLock(dir, "execute-task", "M001/S01/T02", 3, "/tmp/session.jsonl");
133
-
134
- const data = readSessionLockData(dir);
135
- assert.ok(data, "lock data should exist after update");
136
- assert.equal(data!.pid, process.pid, "PID should still be ours");
137
- assert.equal(data!.unitType, "execute-task", "unit type should be updated");
138
- assert.equal(data!.unitId, "M001/S01/T02", "unit ID should be updated");
139
- assert.equal(data!.completedUnits, 3, "completed count should be updated");
140
- assert.equal(data!.sessionFile, "/tmp/session.jsonl", "session file should be recorded");
141
-
142
- releaseSessionLock(dir);
143
- rmSync(dir, { recursive: true, force: true });
144
- });
145
-
146
- // ─── validateSessionLock ────────────────────────────────────────────────
147
-
148
- test("validateSessionLock returns true when we hold the lock", () => {
149
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
150
- mkdirSync(join(dir, ".gsd"), { recursive: true });
151
-
152
- const result = acquireSessionLock(dir);
153
- assert.equal(result.acquired, true);
154
-
155
- assert.equal(validateSessionLock(dir), true, "should validate when we hold the lock");
156
-
157
- releaseSessionLock(dir);
158
- rmSync(dir, { recursive: true, force: true });
159
- });
160
-
161
- test("validateSessionLock returns false after release", () => {
162
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
163
- mkdirSync(join(dir, ".gsd"), { recursive: true });
164
-
165
- const result = acquireSessionLock(dir);
166
- assert.equal(result.acquired, true);
167
- assert.equal(validateSessionLock(dir), true, "should be valid while held");
168
-
169
- // Release the lock — both OS lock and lock file are removed
170
- releaseSessionLock(dir);
171
-
172
- // After release, _lockedPath is cleared and lock file is gone
173
- assert.equal(isSessionLockHeld(dir), false, "should not be held after release");
174
-
175
- rmSync(dir, { recursive: true, force: true });
176
- });
177
-
178
- test("validateSessionLock returns false when another PID owns the lock", () => {
179
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
180
- mkdirSync(join(dir, ".gsd"), { recursive: true });
181
-
182
- // Write lock data with a different PID (parent process)
183
- const foreignLockData = {
184
- pid: process.ppid,
185
- startedAt: new Date().toISOString(),
186
- unitType: "execute-task",
187
- unitId: "M001/S01/T01",
188
- unitStartedAt: new Date().toISOString(),
189
- completedUnits: 0,
190
- };
191
- writeFileSync(join(dir, ".gsd", "auto.lock"), JSON.stringify(foreignLockData, null, 2));
192
-
193
- // Without holding the OS lock, validate should check PID
194
- assert.equal(validateSessionLock(dir), false, "should fail when another PID owns lock");
195
-
196
- rmSync(dir, { recursive: true, force: true });
197
- });
198
-
199
- // ─── isSessionLockHeld ──────────────────────────────────────────────────
200
-
201
- test("isSessionLockHeld returns true after acquire", () => {
202
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
203
- mkdirSync(join(dir, ".gsd"), { recursive: true });
204
-
205
- acquireSessionLock(dir);
206
- assert.equal(isSessionLockHeld(dir), true);
207
-
208
- releaseSessionLock(dir);
209
- assert.equal(isSessionLockHeld(dir), false, "should return false after release");
210
-
211
- rmSync(dir, { recursive: true, force: true });
212
- });
213
-
214
- // ─── isSessionLockProcessAlive ──────────────────────────────────────────
215
-
216
- test("isSessionLockProcessAlive returns false for dead PID", () => {
217
- const data = {
218
- pid: 9999999,
219
- startedAt: new Date().toISOString(),
220
- unitType: "starting",
221
- unitId: "bootstrap",
222
- unitStartedAt: new Date().toISOString(),
223
- completedUnits: 0,
224
- };
225
- assert.equal(isSessionLockProcessAlive(data), false);
226
- });
227
-
228
- test("isSessionLockProcessAlive returns false for own PID (recycled)", () => {
229
- const data = {
230
- pid: process.pid,
231
- startedAt: new Date().toISOString(),
232
- unitType: "starting",
233
- unitId: "bootstrap",
234
- unitStartedAt: new Date().toISOString(),
235
- completedUnits: 0,
236
- };
237
- // Own PID returns false because it means the lock is from a recycled PID
238
- assert.equal(isSessionLockProcessAlive(data), false);
239
- });
240
-
241
- // ─── readSessionLockData ────────────────────────────────────────────────
242
-
243
- test("readSessionLockData returns null when no lock exists", () => {
244
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
245
- mkdirSync(join(dir, ".gsd"), { recursive: true });
246
-
247
- const data = readSessionLockData(dir);
248
- assert.equal(data, null);
249
-
250
- rmSync(dir, { recursive: true, force: true });
251
- });
252
-
253
- test("readSessionLockData reads existing lock data", () => {
254
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
255
- mkdirSync(join(dir, ".gsd"), { recursive: true });
256
-
257
- const lockData = {
258
- pid: 12345,
259
- startedAt: "2026-03-18T00:00:00Z",
260
- unitType: "execute-task",
261
- unitId: "M001/S01/T01",
262
- unitStartedAt: "2026-03-18T00:01:00Z",
263
- completedUnits: 2,
264
- sessionFile: "/tmp/session.jsonl",
265
- };
266
- writeFileSync(join(dir, ".gsd", "auto.lock"), JSON.stringify(lockData, null, 2));
267
-
268
- const data = readSessionLockData(dir);
269
- assert.ok(data, "should read lock data");
270
- assert.equal(data!.pid, 12345);
271
- assert.equal(data!.unitType, "execute-task");
272
- assert.equal(data!.unitId, "M001/S01/T01");
273
- assert.equal(data!.completedUnits, 2);
274
- assert.equal(data!.sessionFile, "/tmp/session.jsonl");
275
-
276
- rmSync(dir, { recursive: true, force: true });
277
- });
278
-
279
- // ─── Acquire → Release → Re-Acquire lifecycle ──────────────────────────
280
-
281
- test("session lock supports acquire → release → re-acquire cycle", () => {
282
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
283
- mkdirSync(join(dir, ".gsd"), { recursive: true });
284
-
285
- // First acquire
286
- const r1 = acquireSessionLock(dir);
287
- assert.equal(r1.acquired, true, "first acquire should succeed");
288
- assert.equal(isSessionLockHeld(dir), true);
289
-
290
- // Release
291
- releaseSessionLock(dir);
292
- assert.equal(isSessionLockHeld(dir), false);
293
-
294
- // Re-acquire
295
- const r2 = acquireSessionLock(dir);
296
- assert.equal(r2.acquired, true, "re-acquire after release should succeed");
297
- assert.equal(isSessionLockHeld(dir), true);
298
-
299
- releaseSessionLock(dir);
300
- rmSync(dir, { recursive: true, force: true });
301
- });
302
-
303
- // ─── Lock creates .gsd/ directory if needed ─────────────────────────────
304
-
305
- test("acquireSessionLock creates .gsd/ if it does not exist", () => {
306
- const dir = mkdtempSync(join(tmpdir(), "gsd-session-lock-"));
307
- // Do NOT create .gsd/ — let the lock function do it
308
-
309
- const result = acquireSessionLock(dir);
310
- assert.equal(result.acquired, true, "should succeed even without .gsd/");
311
- assert.ok(existsSync(join(dir, ".gsd")), ".gsd/ should be created");
312
-
313
- releaseSessionLock(dir);
314
- rmSync(dir, { recursive: true, force: true });
315
- });