agileflow 2.89.3 → 2.90.0

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 (37) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +3 -3
  3. package/lib/placeholder-registry.js +617 -0
  4. package/lib/smart-json-file.js +205 -1
  5. package/lib/table-formatter.js +504 -0
  6. package/lib/transient-status.js +374 -0
  7. package/lib/ui-manager.js +612 -0
  8. package/lib/validate-args.js +213 -0
  9. package/lib/validate-names.js +143 -0
  10. package/lib/validate-paths.js +434 -0
  11. package/lib/validate.js +37 -737
  12. package/package.json +4 -1
  13. package/scripts/check-update.js +16 -3
  14. package/scripts/lib/sessionRegistry.js +682 -0
  15. package/scripts/session-manager.js +77 -10
  16. package/scripts/tui/App.js +176 -0
  17. package/scripts/tui/index.js +75 -0
  18. package/scripts/tui/lib/crashRecovery.js +302 -0
  19. package/scripts/tui/lib/eventStream.js +316 -0
  20. package/scripts/tui/lib/keyboard.js +252 -0
  21. package/scripts/tui/lib/loopControl.js +371 -0
  22. package/scripts/tui/panels/OutputPanel.js +278 -0
  23. package/scripts/tui/panels/SessionPanel.js +178 -0
  24. package/scripts/tui/panels/TracePanel.js +333 -0
  25. package/src/core/commands/tui.md +91 -0
  26. package/tools/cli/commands/config.js +7 -30
  27. package/tools/cli/commands/doctor.js +18 -38
  28. package/tools/cli/commands/list.js +47 -35
  29. package/tools/cli/commands/status.js +13 -37
  30. package/tools/cli/commands/uninstall.js +9 -38
  31. package/tools/cli/installers/core/installer.js +13 -0
  32. package/tools/cli/lib/command-context.js +374 -0
  33. package/tools/cli/lib/config-manager.js +394 -0
  34. package/tools/cli/lib/ide-registry.js +186 -0
  35. package/tools/cli/lib/npm-utils.js +16 -3
  36. package/tools/cli/lib/self-update.js +148 -0
  37. package/tools/cli/lib/validation-middleware.js +491 -0
@@ -42,6 +42,85 @@ function debugLog(operation, details) {
42
42
  }
43
43
  }
44
44
 
45
+ // Security: Secure file permission mode for sensitive config files
46
+ // 0o600 = owner read/write only (no group or world access)
47
+ const SECURE_FILE_MODE = 0o600;
48
+
49
+ /**
50
+ * Check if file permissions are too permissive (security risk)
51
+ * @param {number} mode - File mode (from fs.statSync)
52
+ * @returns {{ok: boolean, warning?: string}} Check result
53
+ */
54
+ function checkFilePermissions(mode) {
55
+ // Skip permission checks on Windows (different permission model)
56
+ if (process.platform === 'win32') {
57
+ return { ok: true };
58
+ }
59
+
60
+ // Extract permission bits (last 9 bits)
61
+ const permissions = mode & 0o777;
62
+
63
+ // Check for group/world readable/writable (security risk)
64
+ const groupRead = permissions & 0o040;
65
+ const groupWrite = permissions & 0o020;
66
+ const worldRead = permissions & 0o004;
67
+ const worldWrite = permissions & 0o002;
68
+
69
+ if (worldWrite) {
70
+ return {
71
+ ok: false,
72
+ warning: 'File is world-writable (mode: ' + permissions.toString(8) + '). Security risk - others can modify.',
73
+ };
74
+ }
75
+
76
+ if (worldRead) {
77
+ return {
78
+ ok: false,
79
+ warning: 'File is world-readable (mode: ' + permissions.toString(8) + '). May expose sensitive config.',
80
+ };
81
+ }
82
+
83
+ if (groupWrite) {
84
+ return {
85
+ ok: false,
86
+ warning: 'File is group-writable (mode: ' + permissions.toString(8) + '). Consider restricting to 0600.',
87
+ };
88
+ }
89
+
90
+ if (groupRead) {
91
+ return {
92
+ ok: false,
93
+ warning: 'File is group-readable (mode: ' + permissions.toString(8) + '). Consider restricting to 0600.',
94
+ };
95
+ }
96
+
97
+ return { ok: true };
98
+ }
99
+
100
+ /**
101
+ * Set secure permissions on a file (0o600 - owner only)
102
+ * @param {string} filePath - Path to the file
103
+ * @returns {{ok: boolean, error?: Error}}
104
+ */
105
+ function setSecurePermissions(filePath) {
106
+ // Skip on Windows
107
+ if (process.platform === 'win32') {
108
+ return { ok: true };
109
+ }
110
+
111
+ try {
112
+ fs.chmodSync(filePath, SECURE_FILE_MODE);
113
+ debugLog('setSecurePermissions', { filePath, mode: SECURE_FILE_MODE.toString(8) });
114
+ return { ok: true };
115
+ } catch (err) {
116
+ const error = createTypedError(`Failed to set secure permissions on ${filePath}: ${err.message}`, 'EPERM', {
117
+ cause: err,
118
+ context: { filePath, mode: SECURE_FILE_MODE },
119
+ });
120
+ return { ok: false, error };
121
+ }
122
+ }
123
+
45
124
  /**
46
125
  * Generate a unique temporary file path
47
126
  * @param {string} filePath - Original file path
@@ -78,6 +157,8 @@ class SmartJsonFile {
78
157
  * @param {number} [options.spaces=2] - JSON indentation spaces
79
158
  * @param {Function} [options.schema] - Optional validation function (throws on invalid)
80
159
  * @param {*} [options.defaultValue] - Default value if file doesn't exist
160
+ * @param {boolean} [options.secureMode=false] - Enforce 0o600 permissions on write
161
+ * @param {boolean} [options.warnInsecure=false] - Warn if file has insecure permissions on read
81
162
  */
82
163
  constructor(filePath, options = {}) {
83
164
  if (!filePath || typeof filePath !== 'string') {
@@ -99,10 +180,12 @@ class SmartJsonFile {
99
180
  this.spaces = options.spaces ?? 2;
100
181
  this.schema = options.schema ?? null;
101
182
  this.defaultValue = options.defaultValue;
183
+ this.secureMode = options.secureMode ?? false;
184
+ this.warnInsecure = options.warnInsecure ?? false;
102
185
 
103
186
  debugLog('constructor', {
104
187
  filePath,
105
- options: { retries: this.retries, backoff: this.backoff },
188
+ options: { retries: this.retries, backoff: this.backoff, secureMode: this.secureMode },
106
189
  });
107
190
  }
108
191
 
@@ -133,6 +216,21 @@ class SmartJsonFile {
133
216
  // Read file
134
217
  const content = fs.readFileSync(this.filePath, 'utf8');
135
218
 
219
+ // Security: Check file permissions if warnInsecure is enabled
220
+ if (this.warnInsecure) {
221
+ try {
222
+ const stats = fs.statSync(this.filePath);
223
+ const permCheck = checkFilePermissions(stats.mode);
224
+ if (!permCheck.ok) {
225
+ debugLog('read', { security: 'insecure permissions', warning: permCheck.warning });
226
+ // Log warning to stderr (non-blocking)
227
+ console.error(`[Security Warning] ${this.filePath}: ${permCheck.warning}`);
228
+ }
229
+ } catch (statErr) {
230
+ debugLog('read', { security: 'could not check permissions', error: statErr.message });
231
+ }
232
+ }
233
+
136
234
  // Parse JSON
137
235
  let data;
138
236
  try {
@@ -242,6 +340,16 @@ class SmartJsonFile {
242
340
 
243
341
  // Atomic rename
244
342
  fs.renameSync(tempPath, this.filePath);
343
+
344
+ // Security: Set secure permissions if secureMode is enabled
345
+ if (this.secureMode) {
346
+ const permResult = setSecurePermissions(this.filePath);
347
+ if (!permResult.ok) {
348
+ debugLog('write', { status: 'warning', security: 'failed to set secure permissions' });
349
+ // Don't fail the write, just warn
350
+ }
351
+ }
352
+
245
353
  debugLog('write', { status: 'success' });
246
354
 
247
355
  return { ok: true };
@@ -424,6 +532,14 @@ class SmartJsonFile {
424
532
  fs.writeFileSync(tempPath, content, 'utf8');
425
533
  fs.renameSync(tempPath, this.filePath);
426
534
 
535
+ // Security: Set secure permissions if secureMode is enabled
536
+ if (this.secureMode) {
537
+ const permResult = setSecurePermissions(this.filePath);
538
+ if (!permResult.ok) {
539
+ debugLog('writeSync', { status: 'warning', security: 'failed to set secure permissions' });
540
+ }
541
+ }
542
+
427
543
  return { ok: true };
428
544
  } catch (err) {
429
545
  // Clean up temp file
@@ -446,4 +562,92 @@ class SmartJsonFile {
446
562
  }
447
563
  }
448
564
 
565
+ // Default max age for temp files (24 hours in milliseconds)
566
+ const DEFAULT_TEMP_MAX_AGE_MS = 24 * 60 * 60 * 1000;
567
+
568
+ /**
569
+ * Clean up orphaned temp files in a directory
570
+ *
571
+ * Removes files matching the temp file pattern that are older than maxAge.
572
+ * Pattern: .{basename}.{timestamp}.{random}.{ext}.tmp
573
+ *
574
+ * @param {string} directory - Directory to clean
575
+ * @param {Object} [options={}] - Cleanup options
576
+ * @param {number} [options.maxAgeMs=86400000] - Max age in ms (default: 24 hours)
577
+ * @param {boolean} [options.dryRun=false] - Don't delete, just report
578
+ * @returns {{ok: boolean, cleaned: string[], errors: string[]}}
579
+ */
580
+ function cleanupTempFiles(directory, options = {}) {
581
+ const { maxAgeMs = DEFAULT_TEMP_MAX_AGE_MS, dryRun = false } = options;
582
+
583
+ const cleaned = [];
584
+ const errors = [];
585
+
586
+ try {
587
+ if (!fs.existsSync(directory)) {
588
+ return { ok: true, cleaned, errors };
589
+ }
590
+
591
+ const now = Date.now();
592
+ const entries = fs.readdirSync(directory);
593
+
594
+ // Pattern: .{basename}.{timestamp}.{random}.{ext}.tmp
595
+ const tempFilePattern = /^\.[^.]+\.\d+\.[a-z0-9]+\.[^.]+\.tmp$/;
596
+
597
+ for (const entry of entries) {
598
+ // Check if it matches temp file pattern
599
+ if (!tempFilePattern.test(entry)) continue;
600
+
601
+ const filePath = path.join(directory, entry);
602
+
603
+ try {
604
+ const stat = fs.statSync(filePath);
605
+
606
+ // Skip if not a file
607
+ if (!stat.isFile()) continue;
608
+
609
+ // Check age
610
+ const age = now - stat.mtimeMs;
611
+ if (age < maxAgeMs) continue;
612
+
613
+ // Delete the temp file
614
+ if (!dryRun) {
615
+ fs.unlinkSync(filePath);
616
+ }
617
+
618
+ cleaned.push(filePath);
619
+ debugLog('cleanupTempFiles', { action: dryRun ? 'would delete' : 'deleted', filePath, ageHours: Math.round(age / 3600000) });
620
+ } catch (err) {
621
+ errors.push(`${filePath}: ${err.message}`);
622
+ debugLog('cleanupTempFiles', { action: 'error', filePath, error: err.message });
623
+ }
624
+ }
625
+
626
+ return { ok: errors.length === 0, cleaned, errors };
627
+ } catch (err) {
628
+ errors.push(`Directory read error: ${err.message}`);
629
+ return { ok: false, cleaned, errors };
630
+ }
631
+ }
632
+
633
+ /**
634
+ * Clean up temp files in the directory of a specific JSON file
635
+ *
636
+ * @param {string} filePath - Path to the JSON file
637
+ * @param {Object} [options={}] - Cleanup options
638
+ * @returns {{ok: boolean, cleaned: string[], errors: string[]}}
639
+ */
640
+ function cleanupTempFilesFor(filePath, options = {}) {
641
+ const directory = path.dirname(filePath);
642
+ return cleanupTempFiles(directory, options);
643
+ }
644
+
449
645
  module.exports = SmartJsonFile;
646
+
647
+ // Export helper functions for external use
648
+ module.exports.SECURE_FILE_MODE = SECURE_FILE_MODE;
649
+ module.exports.checkFilePermissions = checkFilePermissions;
650
+ module.exports.setSecurePermissions = setSecurePermissions;
651
+ module.exports.cleanupTempFiles = cleanupTempFiles;
652
+ module.exports.cleanupTempFilesFor = cleanupTempFilesFor;
653
+ module.exports.DEFAULT_TEMP_MAX_AGE_MS = DEFAULT_TEMP_MAX_AGE_MS;