aiwcli 0.10.1 → 0.10.3

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 (110) hide show
  1. package/dist/commands/clean.js +1 -0
  2. package/dist/commands/clear.d.ts +19 -2
  3. package/dist/commands/clear.js +351 -160
  4. package/dist/commands/init/index.d.ts +1 -17
  5. package/dist/commands/init/index.js +19 -104
  6. package/dist/lib/gitignore-manager.d.ts +9 -0
  7. package/dist/lib/gitignore-manager.js +121 -0
  8. package/dist/lib/template-installer.d.ts +7 -12
  9. package/dist/lib/template-installer.js +69 -193
  10. package/dist/lib/template-settings-reconstructor.d.ts +35 -0
  11. package/dist/lib/template-settings-reconstructor.js +130 -0
  12. package/dist/templates/_shared/hooks/__pycache__/archive_plan.cpython-313.pyc +0 -0
  13. package/dist/templates/_shared/hooks/__pycache__/session_end.cpython-313.pyc +0 -0
  14. package/dist/templates/_shared/hooks/archive_plan.py +10 -2
  15. package/dist/templates/_shared/hooks/session_end.py +37 -29
  16. package/dist/templates/_shared/lib/base/__pycache__/hook_utils.cpython-313.pyc +0 -0
  17. package/dist/templates/_shared/lib/base/__pycache__/inference.cpython-313.pyc +0 -0
  18. package/dist/templates/_shared/lib/base/__pycache__/logger.cpython-313.pyc +0 -0
  19. package/dist/templates/_shared/lib/base/__pycache__/stop_words.cpython-313.pyc +0 -0
  20. package/dist/templates/_shared/lib/base/__pycache__/utils.cpython-313.pyc +0 -0
  21. package/dist/templates/_shared/lib/base/hook_utils.py +8 -10
  22. package/dist/templates/_shared/lib/base/inference.py +51 -62
  23. package/dist/templates/_shared/lib/base/logger.py +35 -21
  24. package/dist/templates/_shared/lib/base/stop_words.py +8 -0
  25. package/dist/templates/_shared/lib/base/utils.py +29 -8
  26. package/dist/templates/_shared/lib/context/__pycache__/plan_manager.cpython-313.pyc +0 -0
  27. package/dist/templates/_shared/lib/context/plan_manager.py +101 -2
  28. package/dist/templates/_shared/lib-ts/base/atomic-write.ts +138 -0
  29. package/dist/templates/_shared/lib-ts/base/constants.ts +299 -0
  30. package/dist/templates/_shared/lib-ts/base/git-state.ts +58 -0
  31. package/dist/templates/_shared/lib-ts/base/hook-utils.ts +360 -0
  32. package/dist/templates/_shared/lib-ts/base/inference.ts +245 -0
  33. package/dist/templates/_shared/lib-ts/base/logger.ts +234 -0
  34. package/dist/templates/_shared/lib-ts/base/state-io.ts +114 -0
  35. package/dist/templates/_shared/lib-ts/base/stop-words.ts +184 -0
  36. package/dist/templates/_shared/lib-ts/base/subprocess-utils.ts +23 -0
  37. package/dist/templates/_shared/lib-ts/base/utils.ts +184 -0
  38. package/dist/templates/_shared/lib-ts/context/context-formatter.ts +432 -0
  39. package/dist/templates/_shared/lib-ts/context/context-selector.ts +497 -0
  40. package/dist/templates/_shared/lib-ts/context/context-store.ts +679 -0
  41. package/dist/templates/_shared/lib-ts/context/plan-manager.ts +292 -0
  42. package/dist/templates/_shared/lib-ts/context/task-tracker.ts +181 -0
  43. package/dist/templates/_shared/lib-ts/handoff/document-generator.ts +215 -0
  44. package/dist/templates/_shared/lib-ts/package.json +21 -0
  45. package/dist/templates/_shared/lib-ts/templates/formatters.ts +102 -0
  46. package/dist/templates/_shared/lib-ts/templates/plan-context.ts +65 -0
  47. package/dist/templates/_shared/lib-ts/tsconfig.json +13 -0
  48. package/dist/templates/_shared/lib-ts/types.ts +151 -0
  49. package/dist/templates/_shared/scripts/__pycache__/status_line.cpython-313.pyc +0 -0
  50. package/dist/templates/_shared/scripts/save_handoff.ts +359 -0
  51. package/dist/templates/_shared/scripts/status_line.py +17 -2
  52. package/dist/templates/cc-native/_cc-native/agents/ARCH-EVOLUTION.md +63 -0
  53. package/dist/templates/cc-native/_cc-native/agents/ARCH-PATTERNS.md +62 -0
  54. package/dist/templates/cc-native/_cc-native/agents/ARCH-STRUCTURE.md +63 -0
  55. package/dist/templates/cc-native/_cc-native/agents/{ASSUMPTION-CHAIN-TRACER.md → ASSUMPTION-TRACER.md} +6 -10
  56. package/dist/templates/cc-native/_cc-native/agents/CLARITY-AUDITOR.md +6 -10
  57. package/dist/templates/cc-native/_cc-native/agents/CLAUDE.md +74 -1
  58. package/dist/templates/cc-native/_cc-native/agents/COMPLETENESS-FEASIBILITY.md +67 -0
  59. package/dist/templates/cc-native/_cc-native/agents/COMPLETENESS-GAPS.md +71 -0
  60. package/dist/templates/cc-native/_cc-native/agents/COMPLETENESS-ORDERING.md +63 -0
  61. package/dist/templates/cc-native/_cc-native/agents/CONSTRAINT-VALIDATOR.md +73 -0
  62. package/dist/templates/cc-native/_cc-native/agents/DESIGN-ADR-VALIDATOR.md +62 -0
  63. package/dist/templates/cc-native/_cc-native/agents/DESIGN-SCALE-MATCHER.md +65 -0
  64. package/dist/templates/cc-native/_cc-native/agents/DEVILS-ADVOCATE.md +6 -9
  65. package/dist/templates/cc-native/_cc-native/agents/DOCUMENTATION-PHILOSOPHY.md +87 -0
  66. package/dist/templates/cc-native/_cc-native/agents/HANDOFF-READINESS.md +5 -9
  67. package/dist/templates/cc-native/_cc-native/agents/{HIDDEN-COMPLEXITY-DETECTOR.md → HIDDEN-COMPLEXITY.md} +6 -10
  68. package/dist/templates/cc-native/_cc-native/agents/INCREMENTAL-DELIVERY.md +67 -0
  69. package/dist/templates/cc-native/_cc-native/agents/PLAN-ORCHESTRATOR.md +91 -18
  70. package/dist/templates/cc-native/_cc-native/agents/RISK-DEPENDENCY.md +63 -0
  71. package/dist/templates/cc-native/_cc-native/agents/RISK-FMEA.md +67 -0
  72. package/dist/templates/cc-native/_cc-native/agents/RISK-PREMORTEM.md +72 -0
  73. package/dist/templates/cc-native/_cc-native/agents/RISK-REVERSIBILITY.md +75 -0
  74. package/dist/templates/cc-native/_cc-native/agents/SCOPE-BOUNDARY.md +78 -0
  75. package/dist/templates/cc-native/_cc-native/agents/SIMPLICITY-GUARDIAN.md +5 -9
  76. package/dist/templates/cc-native/_cc-native/agents/SKEPTIC.md +16 -12
  77. package/dist/templates/cc-native/_cc-native/agents/TESTDRIVEN-BEHAVIOR-AUDITOR.md +62 -0
  78. package/dist/templates/cc-native/_cc-native/agents/TESTDRIVEN-CHARACTERIZATION.md +72 -0
  79. package/dist/templates/cc-native/_cc-native/agents/TESTDRIVEN-FIRST-VALIDATOR.md +62 -0
  80. package/dist/templates/cc-native/_cc-native/agents/TESTDRIVEN-PYRAMID-ANALYZER.md +62 -0
  81. package/dist/templates/cc-native/_cc-native/agents/TRADEOFF-COSTS.md +68 -0
  82. package/dist/templates/cc-native/_cc-native/agents/TRADEOFF-STAKEHOLDERS.md +66 -0
  83. package/dist/templates/cc-native/_cc-native/agents/VERIFY-COVERAGE.md +75 -0
  84. package/dist/templates/cc-native/_cc-native/agents/VERIFY-STRENGTH.md +70 -0
  85. package/dist/templates/cc-native/_cc-native/hooks/__pycache__/cc-native-plan-review.cpython-313.pyc +0 -0
  86. package/dist/templates/cc-native/_cc-native/hooks/cc-native-plan-review.py +125 -40
  87. package/dist/templates/cc-native/_cc-native/lib/__pycache__/utils.cpython-313.pyc +0 -0
  88. package/dist/templates/cc-native/_cc-native/lib/utils.py +57 -13
  89. package/dist/templates/cc-native/_cc-native/plan-review.config.json +11 -7
  90. package/oclif.manifest.json +17 -2
  91. package/package.json +1 -1
  92. package/dist/lib/template-merger.d.ts +0 -47
  93. package/dist/lib/template-merger.js +0 -162
  94. package/dist/templates/cc-native/_cc-native/agents/ACCESSIBILITY-TESTER.md +0 -79
  95. package/dist/templates/cc-native/_cc-native/agents/ARCHITECT-REVIEWER.md +0 -48
  96. package/dist/templates/cc-native/_cc-native/agents/CODE-REVIEWER.md +0 -70
  97. package/dist/templates/cc-native/_cc-native/agents/COMPLETENESS-CHECKER.md +0 -59
  98. package/dist/templates/cc-native/_cc-native/agents/CONTEXT-EXTRACTOR.md +0 -92
  99. package/dist/templates/cc-native/_cc-native/agents/DOCUMENTATION-REVIEWER.md +0 -51
  100. package/dist/templates/cc-native/_cc-native/agents/FEASIBILITY-ANALYST.md +0 -57
  101. package/dist/templates/cc-native/_cc-native/agents/FRESH-PERSPECTIVE.md +0 -54
  102. package/dist/templates/cc-native/_cc-native/agents/INCENTIVE-MAPPER.md +0 -61
  103. package/dist/templates/cc-native/_cc-native/agents/PENETRATION-TESTER.md +0 -79
  104. package/dist/templates/cc-native/_cc-native/agents/PERFORMANCE-ENGINEER.md +0 -75
  105. package/dist/templates/cc-native/_cc-native/agents/PRECEDENT-FINDER.md +0 -70
  106. package/dist/templates/cc-native/_cc-native/agents/REVERSIBILITY-ANALYST.md +0 -61
  107. package/dist/templates/cc-native/_cc-native/agents/RISK-ASSESSOR.md +0 -58
  108. package/dist/templates/cc-native/_cc-native/agents/SECOND-ORDER-ANALYST.md +0 -61
  109. package/dist/templates/cc-native/_cc-native/agents/STAKEHOLDER-ADVOCATE.md +0 -55
  110. package/dist/templates/cc-native/_cc-native/agents/TRADE-OFF-ILLUMINATOR.md +0 -204
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Cross-platform atomic file writes with security.
3
+ * Crash-safe writes by writing to temp file then renaming.
4
+ * NOT for concurrent access — assumes single-session-per-context.
5
+ * See SPEC.md §4
6
+ */
7
+
8
+ import * as fs from "node:fs";
9
+ import * as path from "node:path";
10
+ import * as os from "node:os";
11
+ import * as crypto from "node:crypto";
12
+
13
+ /**
14
+ * Write file atomically with retry logic.
15
+ * Creates temp file, writes, fsyncs, renames.
16
+ * Returns [success, error].
17
+ * See SPEC.md §4.2
18
+ */
19
+ export function atomicWrite(
20
+ filePath: string,
21
+ content: string,
22
+ maxAttempts = 2,
23
+ backoffMs: number[] = [500, 1000],
24
+ ): [boolean, string | null] {
25
+ // Ensure parent directory exists
26
+ const dir = path.dirname(filePath);
27
+ fs.mkdirSync(dir, { recursive: true });
28
+
29
+ const stem = path.basename(filePath, path.extname(filePath));
30
+
31
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
32
+ const tmpName = `.${stem}_${crypto.randomBytes(4).toString("hex")}.tmp`;
33
+ const tmpPath = path.join(dir, tmpName);
34
+
35
+ try {
36
+ // Write to temp file
37
+ const fd = fs.openSync(tmpPath, "w");
38
+ try {
39
+ fs.writeSync(fd, content, undefined, "utf-8");
40
+ fs.fsyncSync(fd);
41
+ } finally {
42
+ fs.closeSync(fd);
43
+ }
44
+
45
+ // Set restrictive permissions (best-effort)
46
+ try {
47
+ fs.chmodSync(tmpPath, 0o600);
48
+ } catch {
49
+ // May fail on some filesystems
50
+ }
51
+
52
+ // Atomic rename (cross-platform on modern Node/Bun)
53
+ fs.renameSync(tmpPath, filePath);
54
+
55
+ return [true, null];
56
+ } catch (e: any) {
57
+ // Clean up temp file
58
+ try {
59
+ fs.unlinkSync(tmpPath);
60
+ } catch {
61
+ // Best-effort cleanup
62
+ }
63
+
64
+ if (attempt < maxAttempts - 1) {
65
+ const waitMs = backoffMs[Math.min(attempt, backoffMs.length - 1)] ?? backoffMs[backoffMs.length - 1] ?? 500;
66
+ sleepSync(waitMs);
67
+ } else {
68
+ const errType = e?.constructor?.name ?? "Error";
69
+ const errMsg = String(e).split("\n")[0]?.slice(0, 200) ?? "";
70
+ return [false, `${errType}: ${errMsg}`];
71
+ }
72
+ }
73
+ }
74
+
75
+ return [false, "Max retry attempts exceeded"];
76
+ }
77
+
78
+ /**
79
+ * Append to file with retry logic.
80
+ * For JSONL files where each line is independent.
81
+ * See SPEC.md §4.3
82
+ */
83
+ export function atomicAppend(
84
+ filePath: string,
85
+ content: string,
86
+ maxAttempts = 2,
87
+ backoffMs: number[] = [500, 1000],
88
+ ): [boolean, string | null] {
89
+ // Ensure parent directory exists
90
+ const dir = path.dirname(filePath);
91
+ fs.mkdirSync(dir, { recursive: true });
92
+
93
+ const isNewFile = !fs.existsSync(filePath);
94
+
95
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
96
+ try {
97
+ const fd = fs.openSync(filePath, "a");
98
+ try {
99
+ fs.writeSync(fd, content, undefined, "utf-8");
100
+ fs.fsyncSync(fd);
101
+ } finally {
102
+ fs.closeSync(fd);
103
+ }
104
+
105
+ // Set permissions on newly created files (best-effort)
106
+ if (isNewFile) {
107
+ try {
108
+ fs.chmodSync(filePath, 0o600);
109
+ } catch {
110
+ // May fail on some filesystems
111
+ }
112
+ }
113
+
114
+ return [true, null];
115
+ } catch (e: any) {
116
+ if (attempt < maxAttempts - 1) {
117
+ const waitMs = backoffMs[Math.min(attempt, backoffMs.length - 1)] ?? backoffMs[backoffMs.length - 1] ?? 500;
118
+ sleepSync(waitMs);
119
+ } else {
120
+ const errType = e?.constructor?.name ?? "Error";
121
+ const errMsg = String(e).split("\n")[0]?.slice(0, 200) ?? "";
122
+ return [false, `${errType}: ${errMsg}`];
123
+ }
124
+ }
125
+ }
126
+
127
+ return [false, "Max retry attempts exceeded"];
128
+ }
129
+
130
+ /**
131
+ * Synchronous sleep for retry backoff.
132
+ * Uses Atomics.wait() for CPU-friendly blocking instead of busy-wait.
133
+ */
134
+ function sleepSync(ms: number): void {
135
+ const sab = new SharedArrayBuffer(4);
136
+ const i32 = new Int32Array(sab);
137
+ Atomics.wait(i32, 0, 0, ms);
138
+ }
@@ -0,0 +1,299 @@
1
+ /**
2
+ * Constants and path utilities for shared context management.
3
+ * See SPEC.md §2
4
+ */
5
+
6
+ import * as path from "node:path";
7
+ import * as fs from "node:fs";
8
+ import { logWarn } from "./logger.js";
9
+
10
+ // Directory names (relative to project root)
11
+ const OUTPUT_DIR = "_output";
12
+ const CONTEXTS_DIR = "contexts";
13
+ const ARCHIVE_DIR = "_archive";
14
+ const INDEX_FILENAME = "index.json";
15
+
16
+ // Context ID validation
17
+ export const MAX_CONTEXT_ID_LENGTH = 64;
18
+ export const VALID_CONTEXT_ID_PATTERN =
19
+ /^[a-z0-9][a-z0-9_-]*[a-z0-9]$|^[a-z0-9]$/;
20
+
21
+ // File size limits
22
+ export const MAX_EVENT_SIZE = 64 * 1024;
23
+ export const MAX_INDEX_SIZE = 1024 * 1024;
24
+
25
+ // Performance constants
26
+ export const MAX_RETRY_ATTEMPTS = 2;
27
+ export const RETRY_BACKOFF_MS = [500, 1000];
28
+
29
+ // Windows reserved filenames
30
+ const WINDOWS_RESERVED = new Set([
31
+ "CON", "PRN", "AUX", "NUL",
32
+ "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
33
+ "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
34
+ ]);
35
+
36
+ /**
37
+ * Sanitize a string into a valid context ID.
38
+ * See SPEC.md §2.3
39
+ */
40
+ export function sanitizeContextId(contextId: string): string {
41
+ if (!contextId) return "context";
42
+
43
+ let result = contextId.toLowerCase();
44
+ result = result.replace(/[^a-z0-9_-]/g, "-");
45
+ result = result.replace(/[-_]+/g, "-");
46
+ result = result.replace(/^[-_]+|[-_]+$/g, "");
47
+
48
+ if (result.length > MAX_CONTEXT_ID_LENGTH) {
49
+ result = result.slice(0, MAX_CONTEXT_ID_LENGTH).replace(/[-_]+$/, "");
50
+ }
51
+
52
+ return result || "context";
53
+ }
54
+
55
+ /**
56
+ * Validate and normalize context ID.
57
+ * Throws only for security violations (path traversal).
58
+ * See SPEC.md §2.3
59
+ */
60
+ export function validateContextId(contextId: string): string {
61
+ if (!contextId) return "context";
62
+
63
+ // SECURITY: Check for path traversal BEFORE any normalization
64
+ if (
65
+ contextId.includes("..") ||
66
+ contextId.includes("/") ||
67
+ contextId.includes("\\")
68
+ ) {
69
+ throw new Error(
70
+ `Invalid context ID '${contextId}': path traversal not allowed`,
71
+ );
72
+ }
73
+
74
+ // Check for URL-encoded variants
75
+ const lower = contextId.toLowerCase();
76
+ if (
77
+ lower.includes("%2e") ||
78
+ lower.includes("%2f") ||
79
+ lower.includes("%5c")
80
+ ) {
81
+ throw new Error(
82
+ `Invalid context ID '${contextId}': encoded path traversal not allowed`,
83
+ );
84
+ }
85
+
86
+ return sanitizeContextId(contextId);
87
+ }
88
+
89
+ /**
90
+ * Get project root from environment or cwd.
91
+ * Priority: CLAUDE_PROJECT_DIR > payload cwd > process.cwd()
92
+ * See SPEC.md §2.2
93
+ */
94
+ export function getProjectRoot(payloadCwd?: string): string {
95
+ const envDir = process.env.CLAUDE_PROJECT_DIR;
96
+ if (envDir) {
97
+ if (!path.isAbsolute(envDir)) {
98
+ logWarn("utils", `CLAUDE_PROJECT_DIR is not absolute: '${envDir}', ignoring`);
99
+ } else if (envDir.includes("..")) {
100
+ logWarn("utils", `CLAUDE_PROJECT_DIR contains '..': '${envDir}', ignoring`);
101
+ } else {
102
+ return envDir;
103
+ }
104
+ }
105
+ if (payloadCwd) return payloadCwd;
106
+ return process.cwd();
107
+ }
108
+
109
+ // §2.4 — Path functions
110
+
111
+ export function getOutputDir(projectRoot?: string): string {
112
+ return path.join(projectRoot ?? getProjectRoot(), OUTPUT_DIR);
113
+ }
114
+
115
+ export function getContextsDir(projectRoot?: string): string {
116
+ return path.join(getOutputDir(projectRoot), CONTEXTS_DIR);
117
+ }
118
+
119
+ export function getContextDir(
120
+ contextId: string,
121
+ projectRoot?: string,
122
+ ): string {
123
+ const validatedId = validateContextId(contextId);
124
+ const contextsDir = getContextsDir(projectRoot);
125
+ const resultPath = path.join(contextsDir, validatedId);
126
+
127
+ // SECURITY: Verify resolved path stays within contexts directory
128
+ const resolved = path.resolve(resultPath);
129
+ const contextsResolved = path.resolve(contextsDir);
130
+ if (
131
+ !resolved.toLowerCase().startsWith(contextsResolved.toLowerCase())
132
+ ) {
133
+ throw new Error(
134
+ `Invalid context ID '${contextId}': path escapes contexts directory`,
135
+ );
136
+ }
137
+
138
+ return resultPath;
139
+ }
140
+
141
+ export function getContextPlansDir(
142
+ contextId: string,
143
+ projectRoot?: string,
144
+ ): string {
145
+ return path.join(getContextDir(contextId, projectRoot), "plans");
146
+ }
147
+
148
+ export function getContextHandoffsDir(
149
+ contextId: string,
150
+ projectRoot?: string,
151
+ ): string {
152
+ return path.join(getContextDir(contextId, projectRoot), "handoffs");
153
+ }
154
+
155
+ export function getContextReviewsDir(
156
+ contextId: string,
157
+ projectRoot?: string,
158
+ ): string {
159
+ return path.join(getContextDir(contextId, projectRoot), "reviews");
160
+ }
161
+
162
+ export function getIndexPath(projectRoot?: string): string {
163
+ return path.join(getOutputDir(projectRoot), INDEX_FILENAME);
164
+ }
165
+
166
+ export function getContextFilePath(
167
+ contextId: string,
168
+ projectRoot?: string,
169
+ ): string {
170
+ return path.join(getContextDir(contextId, projectRoot), "context.json");
171
+ }
172
+
173
+ export function getEventsFilePath(
174
+ contextId: string,
175
+ projectRoot?: string,
176
+ ): string {
177
+ return path.join(getContextDir(contextId, projectRoot), "events.jsonl");
178
+ }
179
+
180
+ export function getAutoStatePath(
181
+ contextId: string,
182
+ projectRoot?: string,
183
+ ): string {
184
+ return path.join(
185
+ getContextDir(contextId, projectRoot),
186
+ "auto-state.json",
187
+ );
188
+ }
189
+
190
+ export function getArchiveDir(projectRoot?: string): string {
191
+ return path.join(getContextsDir(projectRoot), ARCHIVE_DIR);
192
+ }
193
+
194
+ export function getArchiveContextDir(
195
+ contextId: string,
196
+ projectRoot?: string,
197
+ ): string {
198
+ const validatedId = validateContextId(contextId);
199
+ return path.join(getArchiveDir(projectRoot), validatedId);
200
+ }
201
+
202
+ export function getArchiveIndexPath(projectRoot?: string): string {
203
+ return path.join(getArchiveDir(projectRoot), INDEX_FILENAME);
204
+ }
205
+
206
+ /**
207
+ * Get path for a new handoff folder with datetime naming.
208
+ * Handles collisions by appending -N suffix.
209
+ * See SPEC.md §2.4
210
+ */
211
+ export function getHandoffFolderPath(
212
+ contextId: string,
213
+ projectRoot?: string,
214
+ ): string {
215
+ const handoffsDir = getContextHandoffsDir(contextId, projectRoot);
216
+ const now = new Date();
217
+ const timestamp = [
218
+ now.getFullYear().toString(),
219
+ String(now.getMonth() + 1).padStart(2, "0"),
220
+ String(now.getDate()).padStart(2, "0"),
221
+ "-",
222
+ String(now.getHours()).padStart(2, "0"),
223
+ String(now.getMinutes()).padStart(2, "0"),
224
+ ].join("");
225
+ // Format: YYYY-MM-DD-HHMM
226
+ const ts = `${timestamp.slice(0, 4)}-${timestamp.slice(4, 6)}-${timestamp.slice(6, 8)}${timestamp.slice(8)}`;
227
+
228
+ let folder = path.join(handoffsDir, ts);
229
+ let counter = 1;
230
+ while (fs.existsSync(folder)) {
231
+ folder = path.join(handoffsDir, `${ts}-${counter}`);
232
+ counter++;
233
+ }
234
+ return folder;
235
+ }
236
+
237
+ /**
238
+ * Get path for a new review folder.
239
+ * See SPEC.md §2.4
240
+ */
241
+ export function getReviewFolderPath(
242
+ contextId: string,
243
+ iteration: number,
244
+ projectRoot?: string,
245
+ ): string {
246
+ const reviewsDir = path.join(
247
+ getContextReviewsDir(contextId, projectRoot),
248
+ "cc-native",
249
+ );
250
+ const now = new Date();
251
+ const ts = [
252
+ now.getFullYear().toString(),
253
+ "-",
254
+ String(now.getMonth() + 1).padStart(2, "0"),
255
+ "-",
256
+ String(now.getDate()).padStart(2, "0"),
257
+ "-",
258
+ String(now.getHours()).padStart(2, "0"),
259
+ String(now.getMinutes()).padStart(2, "0"),
260
+ ].join("");
261
+ return path.join(reviewsDir, `${ts}-iteration-${iteration}`);
262
+ }
263
+
264
+ // §2.5 — Filename sanitization
265
+
266
+ export function sanitizeFilename(
267
+ s: string,
268
+ maxLen = 32,
269
+ allowLeadingDot = false,
270
+ ): string {
271
+ let result = s.replace(/[^A-Za-z0-9._-]+/g, "_");
272
+ result = result.replace(/^[._-]+|[._-]+$/g, "").slice(0, maxLen) || "unknown";
273
+
274
+ if (!allowLeadingDot) {
275
+ result = result.replace(/^\.+/, "");
276
+ }
277
+
278
+ const baseName = (result.split(".")[0] ?? result).toUpperCase();
279
+ if (WINDOWS_RESERVED.has(baseName)) {
280
+ result = `_${result}`;
281
+ }
282
+
283
+ return result || "unknown";
284
+ }
285
+
286
+ export function sanitizeTitle(s: string, maxLen = 50): string {
287
+ let result = s.toLowerCase().trim();
288
+ result = result.replace(/ /g, "-");
289
+ result = result.replace(/[^a-z0-9._-]+/g, "_");
290
+ result = result.replace(/[-_]+/g, "-");
291
+ result = result.replace(/^[._-]+|[._-]+$/g, "").slice(0, maxLen) || "unknown";
292
+
293
+ const baseName = (result.split(".")[0] ?? result).toUpperCase();
294
+ if (WINDOWS_RESERVED.has(baseName)) {
295
+ result = `_${result}`;
296
+ }
297
+
298
+ return result || "unknown";
299
+ }
@@ -0,0 +1,58 @@
1
+ import { execFileSync } from "node:child_process";
2
+
3
+ /**
4
+ * Capture current git state for session snapshots.
5
+ * All fields are optional — failures are silently ignored.
6
+ */
7
+ export function getGitState(projectRoot: string): Record<string, any> {
8
+ const gitState: Record<string, any> = {};
9
+ const isWin = process.platform === "win32";
10
+ const opts = {
11
+ cwd: projectRoot,
12
+ timeout: 5000,
13
+ encoding: "utf-8" as const,
14
+ stdio: ["pipe", "pipe", "pipe"] as ["pipe", "pipe", "pipe"],
15
+ shell: isWin,
16
+ };
17
+
18
+ try {
19
+ const branch = execFileSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], opts);
20
+ if (branch) gitState.branch = branch.trim();
21
+ } catch { /* non-fatal */ }
22
+
23
+ try {
24
+ const status = execFileSync("git", ["status", "--short"], opts);
25
+ if (status) {
26
+ const files = status.trim().split("\n")
27
+ .filter(Boolean)
28
+ .slice(0, 10)
29
+ .map(line => line.trimStart().split(/\s+/).slice(1).join(" "));
30
+ if (files.length > 0) gitState.uncommitted_files = files;
31
+ }
32
+ } catch { /* non-fatal */ }
33
+
34
+ try {
35
+ const log = execFileSync("git", ["log", "-1", "--oneline"], opts);
36
+ if (log) gitState.last_commit_short = log.trim();
37
+ } catch { /* non-fatal */ }
38
+
39
+ return gitState;
40
+ }
41
+
42
+ /**
43
+ * Get short git status string for display (e.g., in handoff documents).
44
+ */
45
+ export function getGitStatusShort(projectRoot?: string): string {
46
+ try {
47
+ const result = execFileSync("git", ["status", "--short"], {
48
+ encoding: "utf-8",
49
+ timeout: 5000,
50
+ stdio: ["pipe", "pipe", "pipe"],
51
+ ...(projectRoot ? { cwd: projectRoot } : {}),
52
+ shell: process.platform === "win32",
53
+ });
54
+ return result.trim() || "(no changes)";
55
+ } catch {
56
+ return "(git status unavailable)";
57
+ }
58
+ }