-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow-state.js
More file actions
660 lines (572 loc) · 18.4 KB
/
workflow-state.js
File metadata and controls
660 lines (572 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import fs from "fs/promises";
import { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const STATE_DIR = ".state";
const STATE_FILENAME = "workflow-state.json";
const LEGACY_STATE_FILENAME = ".workflow-state.json";
const PROJECT_SUMMARY_FILENAME = "project-summary.json";
const USER_ID_FILENAME = "user-id";
let cachedUserId = null;
let cachedUserStateDir = null;
function generateUserId() {
const timestamp = Date.now().toString(16);
const randomSuffix = Math.random().toString(16).slice(2, 10);
return `user-${timestamp}-${randomSuffix}`;
}
function resolveUserId(stateDir) {
const envUserId = (process.env.DEV_WORKFLOW_USER_ID || "").trim();
if (envUserId) {
cachedUserId = envUserId;
cachedUserStateDir = stateDir;
return envUserId;
}
if (cachedUserId && cachedUserStateDir === stateDir) {
return cachedUserId;
}
const userIdFile = path.join(stateDir, USER_ID_FILENAME);
try {
if (existsSync(userIdFile)) {
const stored = readFileSync(userIdFile, "utf-8").trim();
if (stored) {
cachedUserId = stored;
cachedUserStateDir = stateDir;
return stored;
}
}
} catch {
// Ignore read errors; we'll generate a new ID below.
}
const usersDir = path.join(stateDir, "users");
try {
const entries = readdirSync(usersDir, { withFileTypes: true });
const candidateDirs = entries.filter((entry) => entry.isDirectory()).map((entry) => entry.name);
if (candidateDirs.length === 1) {
const legacyId = candidateDirs[0];
try {
mkdirSync(stateDir, { recursive: true });
writeFileSync(userIdFile, legacyId, "utf-8");
} catch {
// Persisting the legacy ID is best-effort.
}
cachedUserId = legacyId;
cachedUserStateDir = stateDir;
return legacyId;
}
} catch (error) {
if (error?.code !== "ENOENT") {
// Unexpected error accessing users directory – fall through to generate new ID.
}
}
const legacyDefaultPath = path.join(usersDir, "default", STATE_FILENAME);
if (existsSync(legacyDefaultPath)) {
try {
mkdirSync(stateDir, { recursive: true });
writeFileSync(userIdFile, "default", "utf-8");
} catch {
// Best-effort persistence; continue with in-memory fallback.
}
cachedUserId = "default";
cachedUserStateDir = stateDir;
return "default";
}
const generatedId = generateUserId();
try {
mkdirSync(stateDir, { recursive: true });
writeFileSync(userIdFile, generatedId, "utf-8");
} catch {
// Ignore persistence errors; continue with in-memory ID.
}
cachedUserId = generatedId;
cachedUserStateDir = stateDir;
return generatedId;
}
function hasProjectMarkers(candidate) {
if (!candidate) {
return false;
}
const resolved = path.resolve(candidate);
return (
existsSync(path.join(resolved, "package.json")) || existsSync(path.join(resolved, ".git"))
);
}
function normalizeAndStoreCandidate(list, candidate) {
if (!candidate) {
return;
}
const resolved = path.resolve(candidate);
if (!list.includes(resolved)) {
list.push(resolved);
}
}
function addAncestorCandidates(list, start) {
if (!start) {
return;
}
let current = path.resolve(start);
while (!isRootPath(current)) {
const parent = path.dirname(current);
if (parent === current) {
break;
}
if (!isRootPath(parent)) {
normalizeAndStoreCandidate(list, parent);
}
current = parent;
}
}
function selectProjectRoot(candidates, moduleRoot) {
const cwdResolved = path.resolve(process.cwd());
const prioritized = [];
for (const candidate of candidates) {
if (!candidate) {
continue;
}
const resolved = path.resolve(candidate);
if (prioritized.some((entry) => entry.resolved === resolved)) {
continue;
}
prioritized.push({
resolved,
hasMarkers: hasProjectMarkers(resolved),
isRoot: isRootPath(resolved),
isCwdAncestor: cwdResolved === resolved || cwdResolved.startsWith(`${resolved}${path.sep}`),
});
}
const ancestorWithMarkers = prioritized.find((entry) => entry.isCwdAncestor && entry.hasMarkers);
if (ancestorWithMarkers) {
return ancestorWithMarkers.resolved;
}
const ancestorWithoutMarkers = prioritized
.filter((entry) => entry.isCwdAncestor && !entry.hasMarkers && !entry.isRoot)
.sort((a, b) => b.resolved.length - a.resolved.length);
if (ancestorWithoutMarkers.length > 0) {
return ancestorWithoutMarkers[0].resolved;
}
if (moduleRoot) {
const moduleResolved = path.resolve(moduleRoot);
const moduleEntry = prioritized.find((entry) => entry.resolved === moduleResolved && !entry.isRoot);
if (moduleEntry) {
return moduleEntry.resolved;
}
}
const withMarkers = prioritized.find((entry) => entry.hasMarkers);
if (withMarkers) {
return withMarkers.resolved;
}
const nonRoot = prioritized.find((entry) => !entry.isRoot);
if (nonRoot) {
return nonRoot.resolved;
}
return prioritized.length > 0 ? prioritized[0].resolved : null;
}
function getUserScopedDir(baseDir) {
const userId = resolveUserId(baseDir);
return path.join(baseDir, "users", userId);
}
function getCurrentUserId() {
const envUserId = (process.env.DEV_WORKFLOW_USER_ID || "").trim();
if (envUserId) {
return envUserId;
}
if (cachedUserId) {
return cachedUserId;
}
if (cachedUserStateDir) {
return resolveUserId(cachedUserStateDir);
}
return "default";
}
async function migrateLegacyStateFile(targetPath) {
const stateDir = path.dirname(targetPath);
if (path.basename(stateDir) !== STATE_DIR) {
return;
}
const projectRoot = path.dirname(stateDir);
const legacyPath = path.join(projectRoot, LEGACY_STATE_FILENAME);
if (legacyPath === targetPath) {
return;
}
try {
await fs.access(legacyPath);
} catch {
return;
}
await fs.mkdir(stateDir, { recursive: true });
try {
await fs.access(targetPath);
await fs.unlink(legacyPath);
return;
} catch {
// New state file doesn't exist yet; fall through to migrate.
}
await fs.rename(legacyPath, targetPath);
}
function isRootPath(candidate) {
if (!candidate) {
return true;
}
const resolved = path.resolve(candidate);
return path.parse(resolved).root === resolved;
}
function findProjectRoot(startDir) {
if (!startDir) {
return null;
}
let current = path.resolve(startDir);
const { root } = path.parse(current);
while (true) {
const hasPackageJson = existsSync(path.join(current, "package.json"));
const hasGitDir = existsSync(path.join(current, ".git"));
if (hasPackageJson || hasGitDir) {
return current;
}
if (current === root) {
return null;
}
const parent = path.dirname(current);
if (parent === current) {
return null;
}
current = parent;
}
}
function getCompatibilityPaths(primaryPath) {
const resolvedPrimary = path.resolve(primaryPath);
const candidates = new Set();
const moduleDir = __dirname;
const directCandidate = path.resolve(moduleDir, STATE_DIR, STATE_FILENAME);
if (directCandidate !== resolvedPrimary) {
candidates.add(directCandidate);
}
const possibleDistDirs = [
path.join(moduleDir, "dist"),
path.join(path.dirname(moduleDir), "dist"),
];
for (const distDir of possibleDistDirs) {
const candidate = path.resolve(distDir, STATE_DIR, STATE_FILENAME);
if (candidate !== resolvedPrimary) {
candidates.add(candidate);
}
}
return Array.from(candidates);
}
async function pathExists(filePath) {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}
async function ensureCompatibilityLinks(primaryPath) {
const resolvedPrimary = path.resolve(primaryPath);
if (!(await pathExists(resolvedPrimary))) {
return;
}
const compatibilityPaths = getCompatibilityPaths(resolvedPrimary);
for (const compatPath of compatibilityPaths) {
try {
await fs.mkdir(path.dirname(compatPath), { recursive: true });
} catch {
// Ignore directory creation errors; we'll surface them on actual writes.
}
try {
const compatStat = await fs.lstat(compatPath);
if (compatStat.isSymbolicLink()) {
const target = await fs.readlink(compatPath);
if (path.resolve(path.dirname(compatPath), target) === resolvedPrimary) {
continue;
}
await fs.unlink(compatPath);
} else if (compatStat.isFile()) {
const [primaryStat, existingStat] = await Promise.all([
fs.stat(resolvedPrimary),
fs.stat(compatPath),
]);
if (primaryStat.dev === existingStat.dev && primaryStat.ino === existingStat.ino) {
continue;
}
await fs.unlink(compatPath);
} else {
await fs.unlink(compatPath);
}
} catch (error) {
if (error.code !== "ENOENT") {
// Unexpected error accessing legacy path; skip mirroring to avoid breaking primary file.
continue;
}
}
try {
await fs.link(resolvedPrimary, compatPath);
continue;
} catch (error) {
if (!["EEXIST", "EXDEV", "EPERM"].includes(error.code)) {
continue;
}
if (error.code === "EEXIST") {
continue;
}
}
try {
await fs.symlink(resolvedPrimary, compatPath);
continue;
} catch (error) {
if (error.code === "EEXIST") {
continue;
}
if (!["EPERM", "ENOTSUP", "EEXIST"].includes(error.code)) {
// If symlink fails for another reason, skip to avoid interfering with primary file.
continue;
}
}
try {
await fs.copyFile(resolvedPrimary, compatPath);
} catch {
// Final fallback failed; ignore to avoid disrupting primary save.
}
}
}
export function resolveStateFile() {
const customPath = process.env.DEV_WORKFLOW_STATE_FILE;
if (customPath) {
return path.resolve(customPath);
}
const moduleRoot = path.resolve(__dirname, "..");
const cwdResolved = path.resolve(process.cwd());
const initCwd = process.env.INIT_CWD ? path.resolve(process.env.INIT_CWD) : null;
// 1. Prefer the project rooted at the current working directory
let projectRoot = findProjectRoot(cwdResolved);
// 2. Fall back to the project rooted at INIT_CWD if different
if (!projectRoot && initCwd) {
projectRoot = findProjectRoot(initCwd);
}
// 3. Fall back to the project containing this module
if (!projectRoot) {
projectRoot = findProjectRoot(moduleRoot);
}
// 4. Final fallback: use a non-root working directory or the module root
if (!projectRoot) {
projectRoot = !isRootPath(cwdResolved) ? cwdResolved : moduleRoot;
}
const stateDir = path.join(projectRoot, STATE_DIR);
const userDir = getUserScopedDir(stateDir);
return path.join(userDir, STATE_FILENAME);
}
export class WorkflowState {
constructor(stateFilePath = resolveStateFile()) {
this.stateFile = stateFilePath;
this.state = {
currentPhase: "idle",
taskDescription: "",
bugFixed: false,
testsCreated: false,
testsPassed: false,
testsSkipped: false,
testsSkippedReason: "",
documentationCreated: false,
readyToCommit: false,
readyCheckCompleted: false,
released: false,
releaseSkipped: false,
releaseSkippedReason: "",
releaseCommand: "",
releaseNotes: "",
commitAndPushCompleted: false,
lastCommitMessage: "",
lastPushBranch: "",
featureFlowCreated: false,
featureFlowDescription: "",
history: [],
taskType: "",
};
}
async load() {
await migrateLegacyStateFile(this.stateFile);
try {
const data = await fs.readFile(this.stateFile, "utf-8");
const parsed = JSON.parse(data);
this.state = {
...this.state,
...parsed,
};
// Ensure compatibility mirrors exist whenever the primary file is present
// Run this as non-blocking to speed up startup
ensureCompatibilityLinks(this.stateFile).catch(() => { });
if (typeof this.state.testsCreated !== "boolean") {
this.state.testsCreated = false;
}
if (typeof this.state.testsSkipped !== "boolean") {
this.state.testsSkipped = false;
}
if (typeof this.state.testsSkippedReason !== "string") {
this.state.testsSkippedReason = "";
}
if (typeof this.state.readyCheckCompleted !== "boolean") {
this.state.readyCheckCompleted = false;
}
if (typeof this.state.released !== "boolean") {
this.state.released = false;
}
if (typeof this.state.releaseSkipped !== "boolean") {
this.state.releaseSkipped = false;
}
if (typeof this.state.releaseSkippedReason !== "string") {
this.state.releaseSkippedReason = "";
}
if (typeof this.state.commitAndPushCompleted !== "boolean") {
this.state.commitAndPushCompleted = false;
}
if (typeof this.state.releaseCommand !== "string") {
this.state.releaseCommand = "";
}
if (typeof this.state.releaseNotes !== "string") {
this.state.releaseNotes = "";
}
if (typeof this.state.lastCommitMessage !== "string") {
this.state.lastCommitMessage = "";
}
if (typeof this.state.lastPushBranch !== "string") {
this.state.lastPushBranch = "";
}
if (typeof this.state.featureFlowCreated !== "boolean") {
this.state.featureFlowCreated = false;
}
if (typeof this.state.featureFlowDescription !== "string") {
this.state.featureFlowDescription = "";
}
} catch (error) {
if (error?.code !== "ENOENT") {
throw error;
}
// Missing file is fine; bootstrap fresh artifacts for downstream consumers.
await this.ensurePrimaryFile();
}
}
async ensurePrimaryFile() {
let created = false;
try {
await fs.access(this.stateFile);
} catch {
await fs.mkdir(path.dirname(this.stateFile), { recursive: true });
await fs.writeFile(this.stateFile, JSON.stringify(this.state, null, 2));
created = true;
}
// Always ensure a fresh project summary so dependent tooling can read it immediately.
await this.updateProjectSummary();
ensureCompatibilityLinks(this.stateFile).catch(() => { });
return created;
}
async save() {
await fs.writeFile(this.stateFile, JSON.stringify(this.state, null, 2));
await ensureCompatibilityLinks(this.stateFile);
await this.updateProjectSummary();
await this.syncToDatabase();
}
async updateProjectSummary() {
const summaryPath = path.join(path.dirname(this.stateFile), PROJECT_SUMMARY_FILENAME);
const summary = this.generateProjectSummaryData();
await fs.mkdir(path.dirname(summaryPath), { recursive: true });
await fs.writeFile(summaryPath, JSON.stringify(summary, null, 2));
}
async syncToDatabase() {
try {
const { insertHistoryEntry, updateSummaryForUser, saveState } = await import("./db/index.js");
const userId = getCurrentUserId();
// Use the project directory (parent of .state) as the project identifier
// this.stateFile is .../.state/users/<user>/workflow-state.json
// so we want resolution logic. For now, let's use the absolute path to the project root.
// We can derive it from stateFile path logic in resolveStateFile or just use the directory we are in.
// A safe bet that is unique per project on this machine is the project root found by resolveStateFile.
// Let's perform a simple resolution:
const stateDir = path.dirname(path.dirname(path.dirname(this.stateFile)));
// stateFile = <root>/.state/users/<user>/workflow-state.json
// dirname = <root>/.state/users/<user>
// dirname = <root>/.state/users
// dirname = <root>/.state
// dirname = <root>
// Wait, let's look at the path structure from resolveStateFile:
// path.join(userDir, STATE_FILENAME); where userDir is path.join(stateDir, "users", userId);
// and stateDir is path.join(projectRoot, STATE_DIR);
// So: projectRoot/.state/users/userId/workflow-state.json
// 1. dirname -> .../userId
// 2. dirname -> .../users
// 3. dirname -> .../.state
// 4. dirname -> .../projectRoot
const projectRoot = path.dirname(path.dirname(path.dirname(path.dirname(this.stateFile))));
for (const entry of this.state.history || []) {
await insertHistoryEntry(userId, projectRoot, entry);
}
await updateSummaryForUser(userId, projectRoot);
await saveState(userId, projectRoot, this.state);
} catch (error) {
// Database sync failed; log it but don't crash
console.error("Database sync failed:", error.message);
}
}
generateProjectSummaryData() {
const history = this.state.history || [];
if (!history || history.length === 0) {
return {
totalTasks: 0,
taskTypes: {},
lastActive: null,
recentTasks: [],
updatedAt: new Date().toISOString(),
};
}
const taskTypes = {};
const recentTasks = history.slice(-20);
for (const entry of recentTasks) {
const type = entry.taskType || "other";
taskTypes[type] = (taskTypes[type] || 0) + 1;
}
const totalTasks = history.length;
const lastTask = history[history.length - 1];
const lastActive = lastTask ? lastTask.timestamp : null;
return {
totalTasks,
taskTypes,
lastActive,
recentTasks: history.slice(-5).reverse().map((e) => ({
description: e.taskDescription,
type: e.taskType,
timestamp: e.timestamp,
})),
updatedAt: new Date().toISOString(),
};
}
reset() {
this.state = {
currentPhase: "idle",
taskDescription: "",
bugFixed: false,
testsCreated: false,
testsPassed: false,
testsSkipped: false,
testsSkippedReason: "",
documentationCreated: false,
readyToCommit: false,
readyCheckCompleted: false,
released: false,
releaseSkipped: false,
releaseSkippedReason: "",
releaseCommand: "",
releaseNotes: "",
commitAndPushCompleted: false,
lastCommitMessage: "",
lastPushBranch: "",
featureFlowCreated: false,
featureFlowDescription: "",
history: this.state.history,
};
}
addToHistory(entry) {
this.state.history.push({
...entry,
timestamp: new Date().toISOString(),
});
}
}