sela-core 1.0.6 → 1.0.7

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 (71) hide show
  1. package/README.md +1 -1
  2. package/dist/config/ConfigLoader.d.ts +1 -0
  3. package/dist/config/ConfigLoader.d.ts.map +1 -1
  4. package/dist/config/ConfigLoader.js +10 -0
  5. package/dist/config/DryRunGuard.d.ts +14 -0
  6. package/dist/config/DryRunGuard.d.ts.map +1 -0
  7. package/dist/config/DryRunGuard.js +79 -0
  8. package/dist/config/SelaConfig.d.ts +15 -1
  9. package/dist/config/SelaConfig.d.ts.map +1 -1
  10. package/dist/config/SelaConfig.js +27 -1
  11. package/dist/engine/SelaEngine.d.ts +12 -9
  12. package/dist/engine/SelaEngine.d.ts.map +1 -1
  13. package/dist/engine/SelaEngine.js +249 -72
  14. package/dist/errors/SelaError.d.ts +133 -0
  15. package/dist/errors/SelaError.d.ts.map +1 -0
  16. package/dist/errors/SelaError.js +155 -0
  17. package/dist/fixtures/expectProxy.d.ts.map +1 -1
  18. package/dist/fixtures/expectProxy.js +7 -0
  19. package/dist/fixtures/index.d.ts +7 -1
  20. package/dist/fixtures/index.d.ts.map +1 -1
  21. package/dist/fixtures/index.js +15 -0
  22. package/dist/fixtures/proxyTag.d.ts +12 -0
  23. package/dist/fixtures/proxyTag.d.ts.map +1 -0
  24. package/dist/fixtures/proxyTag.js +45 -0
  25. package/dist/index.d.ts +2 -0
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +6 -1
  28. package/dist/reporter/SelaReporter.d.ts +110 -0
  29. package/dist/reporter/SelaReporter.d.ts.map +1 -0
  30. package/dist/reporter/SelaReporter.js +328 -0
  31. package/dist/services/ASTSourceUpdater.d.ts +19 -0
  32. package/dist/services/ASTSourceUpdater.d.ts.map +1 -1
  33. package/dist/services/ASTSourceUpdater.js +173 -29
  34. package/dist/services/HealReportService.d.ts +42 -0
  35. package/dist/services/HealReportService.d.ts.map +1 -1
  36. package/dist/services/HealReportService.js +80 -1
  37. package/dist/services/HealingCacheService.d.ts +135 -0
  38. package/dist/services/HealingCacheService.d.ts.map +1 -0
  39. package/dist/services/HealingCacheService.js +460 -0
  40. package/dist/services/InitializerUpdater.d.ts.map +1 -1
  41. package/dist/services/InitializerUpdater.js +20 -1
  42. package/dist/services/IntentAuditor.d.ts +18 -1
  43. package/dist/services/IntentAuditor.d.ts.map +1 -1
  44. package/dist/services/IntentAuditor.js +19 -6
  45. package/dist/services/InteractiveReview.d.ts +24 -0
  46. package/dist/services/InteractiveReview.d.ts.map +1 -0
  47. package/dist/services/InteractiveReview.js +218 -0
  48. package/dist/services/LLMService.d.ts +17 -1
  49. package/dist/services/LLMService.d.ts.map +1 -1
  50. package/dist/services/LLMService.js +18 -8
  51. package/dist/services/PRAutomationService.d.ts +14 -1
  52. package/dist/services/PRAutomationService.d.ts.map +1 -1
  53. package/dist/services/PRAutomationService.js +260 -69
  54. package/dist/services/PendingPromptLedger.d.ts +44 -0
  55. package/dist/services/PendingPromptLedger.d.ts.map +1 -0
  56. package/dist/services/PendingPromptLedger.js +180 -0
  57. package/dist/services/ReportGenerator.d.ts +70 -0
  58. package/dist/services/ReportGenerator.d.ts.map +1 -0
  59. package/dist/services/ReportGenerator.js +191 -0
  60. package/dist/services/SafetyGuard.d.ts +27 -1
  61. package/dist/services/SafetyGuard.d.ts.map +1 -1
  62. package/dist/services/SafetyGuard.js +58 -12
  63. package/dist/services/SourceUpdater.d.ts.map +1 -1
  64. package/dist/services/SourceUpdater.js +44 -13
  65. package/dist/services/WorkspaceSnapshotService.d.ts +71 -0
  66. package/dist/services/WorkspaceSnapshotService.d.ts.map +1 -0
  67. package/dist/services/WorkspaceSnapshotService.js +202 -0
  68. package/dist/utils/IsolatedDiff.d.ts +45 -0
  69. package/dist/utils/IsolatedDiff.d.ts.map +1 -0
  70. package/dist/utils/IsolatedDiff.js +379 -0
  71. package/package.json +71 -67
@@ -0,0 +1,135 @@
1
+ import type { SmartChainSegment, SelectorSegment } from "./LLMService";
2
+ export type HealCacheMode = "action" | "assertion";
3
+ export interface CacheKeyInput {
4
+ /** Absolute or project-relative spec path containing the failing locator. */
5
+ filePath: string;
6
+ /** 1-based line of the failing call site — paired with filePath as a fingerprint. */
7
+ line: number;
8
+ /**
9
+ * Stable content hash of the DNA snapshot used to drive the heal. When the
10
+ * DNA rewrites, this changes — every cached entry for the old DNA
11
+ * silently stops matching and ages out via TTL.
12
+ */
13
+ dnaContentHash: string;
14
+ /** The original broken selector. */
15
+ brokenSelector: string;
16
+ /** Heal mode the cached fix was produced under. */
17
+ mode: HealCacheMode;
18
+ }
19
+ export interface HealingCacheEntry {
20
+ /** Full SHA1 of the CacheKeyInput tuple. */
21
+ key: string;
22
+ filePath: string;
23
+ line: number;
24
+ dnaContentHash: string;
25
+ brokenSelector: string;
26
+ mode: HealCacheMode;
27
+ healedSelector: string;
28
+ chainSegments?: SmartChainSegment[];
29
+ segments?: SelectorSegment[];
30
+ confidence?: number;
31
+ contentChange?: {
32
+ oldText: string;
33
+ newText: string;
34
+ };
35
+ explanation?: string;
36
+ alternatives?: string[];
37
+ /**
38
+ * True when SafetyGuard cleared this fix in the originating session
39
+ * (SAFE or REQUIRES_REVIEW). Unsafe entries are never replayed.
40
+ */
41
+ safe: boolean;
42
+ createdAt: string;
43
+ /** ISO timestamp after which the entry is considered expired and ignored. */
44
+ ttlExpiresAt: string;
45
+ lastHitAt?: string;
46
+ hitCount: number;
47
+ }
48
+ interface CacheFile {
49
+ schemaVersion: 1;
50
+ entries: Record<string, HealingCacheEntry>;
51
+ }
52
+ export interface HealingCacheSetInput extends CacheKeyInput {
53
+ healedSelector: string;
54
+ chainSegments?: SmartChainSegment[];
55
+ segments?: SelectorSegment[];
56
+ confidence?: number;
57
+ contentChange?: {
58
+ oldText: string;
59
+ newText: string;
60
+ };
61
+ explanation?: string;
62
+ alternatives?: string[];
63
+ safe: boolean;
64
+ /** Optional override for entry-level TTL (ms). Defaults to env / 7d. */
65
+ ttlMs?: number;
66
+ }
67
+ export declare function computeCacheKey(input: CacheKeyInput): string;
68
+ /**
69
+ * Convenience helper — fingerprint the JSON form of a DNA snapshot so
70
+ * the cache key changes whenever the DNA is rewritten.
71
+ */
72
+ export declare function hashDnaContent(dna: unknown): string;
73
+ export declare class HealingCacheService {
74
+ private readonly cacheDir;
75
+ private readonly filename;
76
+ private readonly lockDir;
77
+ private readonly tmpSuffix;
78
+ private readonly bakSuffix;
79
+ private inFlight;
80
+ constructor(opts?: {
81
+ cacheDir?: string;
82
+ filename?: string;
83
+ });
84
+ /**
85
+ * Read-only lookup. Bumps `hitCount` / `lastHitAt` IN-MEMORY only on
86
+ * the returned entry reference — never persisted on the get() path.
87
+ * This eliminates the read-write race where parallel workers each
88
+ * call get() and serialise full entries-map snapshots back to disk,
89
+ * overwriting peer set() operations.
90
+ */
91
+ get(input: CacheKeyInput): HealingCacheEntry | null;
92
+ /**
93
+ * Persist a new cache entry. Serialises against in-process writers
94
+ * AND across processes via the mkdir lock. The in-process chain
95
+ * isolates each `await` so a rejected upstream Promise never
96
+ * deadlocks downstream callers.
97
+ */
98
+ set(input: HealingCacheSetInput): Promise<void>;
99
+ /** Synchronous variant — used by teardown / CLI paths. */
100
+ setSync(input: HealingCacheSetInput): void;
101
+ /** Wipe every entry. Used by tests and the (forthcoming) `sela cache clear` CLI. */
102
+ clear(): void;
103
+ /** Returns the parsed cache file (or an empty shell on failure). */
104
+ read(): CacheFile;
105
+ /** Absolute path of the on-disk cache file. */
106
+ filePath(): string;
107
+ /**
108
+ * Prunes expired entries. Cheap O(n) scan — intended for engine
109
+ * startup. Returns the number of entries removed.
110
+ */
111
+ pruneExpired(): number;
112
+ private isExpired;
113
+ private lockedWrite;
114
+ private buildEntry;
115
+ /**
116
+ * Reads + parses the cache file. Tolerates every realistic failure
117
+ * (missing, empty, malformed, wrong schema version) by returning a
118
+ * fresh empty shell — the contract is "cache misses are silent".
119
+ */
120
+ private safeRead;
121
+ /**
122
+ * Atomic write: write to `<file>.tmp`, copy current to `<file>.bak`,
123
+ * then rename `<file>.tmp` → `<file>`. The rename is the single
124
+ * commit point — a crash between bak and rename leaves the bak file
125
+ * intact for future recovery.
126
+ */
127
+ private atomicWrite;
128
+ private acquireLockWithBackoff;
129
+ private tryAcquireLock;
130
+ private withFileLockSync;
131
+ private releaseLock;
132
+ }
133
+ export declare const sharedHealingCache: HealingCacheService;
134
+ export {};
135
+ //# sourceMappingURL=HealingCacheService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HealingCacheService.d.ts","sourceRoot":"","sources":["../../src/services/HealingCacheService.ts"],"names":[],"mappings":"AA8DA,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAMvE,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,WAAW,CAAC;AAEnD,MAAM,WAAW,aAAa;IAC5B,6EAA6E;IAC7E,QAAQ,EAAE,MAAM,CAAC;IACjB,qFAAqF;IACrF,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,cAAc,EAAE,MAAM,CAAC;IACvB,oCAAoC;IACpC,cAAc,EAAE,MAAM,CAAC;IACvB,mDAAmD;IACnD,IAAI,EAAE,aAAa,CAAC;CACrB;AAED,MAAM,WAAW,iBAAiB;IAChC,4CAA4C;IAC5C,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,aAAa,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,iBAAiB,EAAE,CAAC;IACpC,QAAQ,CAAC,EAAE,eAAe,EAAE,CAAC;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,EAA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AAC,SAAS,CAAU;IAIpC,OAAO,CAAC,QAAQ,CAAoC;gBAExC,IAAI,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE;IAU3D;;;;;;OAMG;IACH,GAAG,CAAC,KAAK,EAAE,aAAa,GAAG,iBAAiB,GAAG,IAAI;IAgBnD;;;;;OAKG;IACG,GAAG,CAAC,KAAK,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;IAgCrD,0DAA0D;IAC1D,OAAO,CAAC,KAAK,EAAE,oBAAoB,GAAG,IAAI;IAU1C,oFAAoF;IACpF,KAAK,IAAI,IAAI;IAeb,oEAAoE;IACpE,IAAI,IAAI,SAAS;IAIjB,+CAA+C;IAC/C,QAAQ,IAAI,MAAM;IAIlB;;;OAGG;IACH,YAAY,IAAI,MAAM;IAmBtB,OAAO,CAAC,SAAS;YAOH,WAAW;IAkBzB,OAAO,CAAC,UAAU;IA8BlB;;;;OAIG;IACH,OAAO,CAAC,QAAQ;IAqChB;;;;;OAKG;IACH,OAAO,CAAC,WAAW;YA4BL,sBAAsB;IAUpC,OAAO,CAAC,cAAc;IAgBtB,OAAO,CAAC,gBAAgB;IAmBxB,OAAO,CAAC,WAAW;CAOpB;AAqBD,eAAO,MAAM,kBAAkB,qBAA4B,CAAC"}
@@ -0,0 +1,460 @@
1
+ "use strict";
2
+ // src/services/HealingCacheService.ts
3
+ //
4
+ // Local healing cache — short-circuits the LLM round-trip when an
5
+ // identical heal-context has been resolved successfully in a prior
6
+ // session.
7
+ //
8
+ // Cache key (matches DX MVP Phase 6 plan):
9
+ // SHA1(filePath + ":" + line + "::" + dnaContentHash + "::" + brokenSelector + "::" + mode)
10
+ // Including `dnaContentHash` means a DNA rewrite automatically
11
+ // invalidates every entry keyed against that fingerprint — old
12
+ // entries simply stop matching and age out via TTL.
13
+ //
14
+ // Cache value:
15
+ // `{ chainSegments, segments, confidence, contentChange, explanation,
16
+ // alternatives, safe, createdAt, ttlExpiresAt, hitCount, lastHitAt }`.
17
+ // TTL defaults to 7 days; overridable via `SELA_CACHE_TTL_DAYS`.
18
+ //
19
+ // Storage:
20
+ // `.sela-cache.json` in process.cwd() by default (configurable via the
21
+ // constructor for tests). Single-file map of SHA1 keys → entry.
22
+ // Schema-versioned (`schemaVersion: 1`).
23
+ //
24
+ // Concurrency model (three layers, identical to the plan):
25
+ //
26
+ // 1. In-process mutex
27
+ // `inFlight: Promise<void>` chain. Concurrent setters in the SAME
28
+ // Node process queue behind one another via the chained Promise.
29
+ // Any rejection on the chain is swallowed before the next call
30
+ // awaits it, so a single bad write CANNOT deadlock subsequent
31
+ // callers.
32
+ //
33
+ // 2. Cross-process file lock
34
+ // Playwright workers run as separate processes — atomic
35
+ // `mkdir <cacheDir>/.sela-cache.lock` is used as the mutex. Second
36
+ // process gets `EEXIST`, polls with exponential backoff
37
+ // (50ms → 800ms, max 6 retries ≈ 3.15 s ceiling). Lock is held
38
+ // only across the read-modify-write window, never during reads.
39
+ //
40
+ // 3. Atomic write via temp + rename
41
+ // Write to `.sela-cache.json.tmp`, copy the previous file to
42
+ // `.sela-cache.json.bak`, then `fs.renameSync(tmp, target)` —
43
+ // atomic on POSIX and on Windows (Node ≥14) when source and
44
+ // target share a volume (always true here).
45
+ //
46
+ // Read path:
47
+ // memory check → file check (no lock) → on miss the caller (SelaEngine)
48
+ // acquires the LLM via the normal path; cache writes happen post-success.
49
+ //
50
+ // `get()` is strictly read-only on disk: hit-count / lastHitAt are
51
+ // bumped IN-MEMORY only — they never trigger a disk write. This avoids
52
+ // the documented bug where concurrent worker `get()` calls would
53
+ // overwrite valid `set()` operations from peer workers.
54
+ //
55
+ // Failure tolerance: any read failure (missing, malformed JSON, wrong
56
+ // schema, EACCES, …) returns "no entries" instead of throwing. The
57
+ // cache is a latency optimisation — never a hard dependency of the
58
+ // heal pipeline.
59
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
60
+ if (k2 === undefined) k2 = k;
61
+ var desc = Object.getOwnPropertyDescriptor(m, k);
62
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
63
+ desc = { enumerable: true, get: function() { return m[k]; } };
64
+ }
65
+ Object.defineProperty(o, k2, desc);
66
+ }) : (function(o, m, k, k2) {
67
+ if (k2 === undefined) k2 = k;
68
+ o[k2] = m[k];
69
+ }));
70
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
71
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
72
+ }) : function(o, v) {
73
+ o["default"] = v;
74
+ });
75
+ var __importStar = (this && this.__importStar) || (function () {
76
+ var ownKeys = function(o) {
77
+ ownKeys = Object.getOwnPropertyNames || function (o) {
78
+ var ar = [];
79
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
80
+ return ar;
81
+ };
82
+ return ownKeys(o);
83
+ };
84
+ return function (mod) {
85
+ if (mod && mod.__esModule) return mod;
86
+ var result = {};
87
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
88
+ __setModuleDefault(result, mod);
89
+ return result;
90
+ };
91
+ })();
92
+ Object.defineProperty(exports, "__esModule", { value: true });
93
+ exports.sharedHealingCache = exports.HealingCacheService = void 0;
94
+ exports.computeCacheKey = computeCacheKey;
95
+ exports.hashDnaContent = hashDnaContent;
96
+ const fs = __importStar(require("fs"));
97
+ const path = __importStar(require("path"));
98
+ const crypto_1 = require("crypto");
99
+ const DryRunGuard_1 = require("../config/DryRunGuard");
100
+ // ═══════════════════════════════════════════════════════════════════
101
+ // KEY HASH
102
+ // ═══════════════════════════════════════════════════════════════════
103
+ const KEY_SEPARATOR = "::";
104
+ const DEFAULT_TTL_DAYS = 7;
105
+ function defaultTtlMs() {
106
+ const raw = process.env.SELA_CACHE_TTL_DAYS;
107
+ if (!raw)
108
+ return DEFAULT_TTL_DAYS * 24 * 60 * 60 * 1000;
109
+ const n = Number(raw);
110
+ if (!Number.isFinite(n) || n <= 0) {
111
+ return DEFAULT_TTL_DAYS * 24 * 60 * 60 * 1000;
112
+ }
113
+ return n * 24 * 60 * 60 * 1000;
114
+ }
115
+ function computeCacheKey(input) {
116
+ // Normalise filePath separators so a cache entry written from a Windows
117
+ // worker is still found from a POSIX worker hitting the same repo.
118
+ const normPath = path.resolve(input.filePath).split(path.sep).join("/");
119
+ return (0, crypto_1.createHash)("sha1")
120
+ .update([
121
+ normPath + ":" + input.line,
122
+ input.dnaContentHash,
123
+ input.brokenSelector,
124
+ input.mode,
125
+ ].join(KEY_SEPARATOR))
126
+ .digest("hex");
127
+ }
128
+ /**
129
+ * Convenience helper — fingerprint the JSON form of a DNA snapshot so
130
+ * the cache key changes whenever the DNA is rewritten.
131
+ */
132
+ function hashDnaContent(dna) {
133
+ try {
134
+ const json = typeof dna === "string" ? dna : JSON.stringify(dna ?? {});
135
+ return (0, crypto_1.createHash)("sha1").update(json).digest("hex");
136
+ }
137
+ catch {
138
+ return "no-dna";
139
+ }
140
+ }
141
+ // ═══════════════════════════════════════════════════════════════════
142
+ // HealingCacheService
143
+ // ═══════════════════════════════════════════════════════════════════
144
+ class HealingCacheService {
145
+ cacheDir;
146
+ filename;
147
+ lockDir;
148
+ tmpSuffix = ".tmp";
149
+ bakSuffix = ".bak";
150
+ // In-process serialisation. Every new set() chains onto the prior
151
+ // tail Promise. The chain is fault-tolerant — see `set()` below.
152
+ inFlight = Promise.resolve();
153
+ constructor(opts) {
154
+ this.cacheDir = opts?.cacheDir ?? process.cwd();
155
+ this.filename = opts?.filename ?? ".sela-cache.json";
156
+ this.lockDir = path.join(this.cacheDir, ".sela-cache.lock");
157
+ }
158
+ // ─────────────────────────────────────────────────────────────
159
+ // PUBLIC API
160
+ // ─────────────────────────────────────────────────────────────
161
+ /**
162
+ * Read-only lookup. Bumps `hitCount` / `lastHitAt` IN-MEMORY only on
163
+ * the returned entry reference — never persisted on the get() path.
164
+ * This eliminates the read-write race where parallel workers each
165
+ * call get() and serialise full entries-map snapshots back to disk,
166
+ * overwriting peer set() operations.
167
+ */
168
+ get(input) {
169
+ const key = computeCacheKey(input);
170
+ const file = this.safeRead();
171
+ const entry = file.entries[key];
172
+ if (!entry)
173
+ return null;
174
+ if (!entry.safe)
175
+ return null;
176
+ if (this.isExpired(entry))
177
+ return null;
178
+ // In-memory only — DO NOT write back. Hit-count drift across workers
179
+ // is acceptable; corrupting peer writes is not.
180
+ entry.hitCount = (entry.hitCount ?? 0) + 1;
181
+ entry.lastHitAt = new Date().toISOString();
182
+ return entry;
183
+ }
184
+ /**
185
+ * Persist a new cache entry. Serialises against in-process writers
186
+ * AND across processes via the mkdir lock. The in-process chain
187
+ * isolates each `await` so a rejected upstream Promise never
188
+ * deadlocks downstream callers.
189
+ */
190
+ async set(input) {
191
+ if (DryRunGuard_1.DryRunGuard.active()) {
192
+ // Don't pollute the cache during a SELA_DRY_RUN session.
193
+ return;
194
+ }
195
+ // Chain onto the previous tail, but swallow rejections via a
196
+ // `.catch(noop)` adapter. Without this, an earlier rejected set()
197
+ // would propagate through every subsequent `await prev` and either
198
+ // throw at the wrong caller or — worse — leak through the finally
199
+ // block without releasing the queue. The wrapper guarantees the
200
+ // queue is FIFO and rejection-safe.
201
+ const prev = this.inFlight.catch(() => {
202
+ /* swallow — upstream failure handled by its own caller */
203
+ });
204
+ let release;
205
+ const myTurn = new Promise((res) => {
206
+ release = res;
207
+ });
208
+ this.inFlight = myTurn;
209
+ try {
210
+ await prev;
211
+ await this.lockedWrite(input);
212
+ }
213
+ finally {
214
+ // Always release — even on throw — so the next queued caller
215
+ // unblocks. Errors still propagate up to *this* caller normally.
216
+ release();
217
+ }
218
+ }
219
+ /** Synchronous variant — used by teardown / CLI paths. */
220
+ setSync(input) {
221
+ if (DryRunGuard_1.DryRunGuard.active())
222
+ return;
223
+ this.withFileLockSync(() => {
224
+ const file = this.safeRead();
225
+ const key = computeCacheKey(input);
226
+ file.entries[key] = this.buildEntry(key, input, file.entries[key]);
227
+ this.atomicWrite(file);
228
+ });
229
+ }
230
+ /** Wipe every entry. Used by tests and the (forthcoming) `sela cache clear` CLI. */
231
+ clear() {
232
+ this.withFileLockSync(() => {
233
+ const target = path.join(this.cacheDir, this.filename);
234
+ const bak = target + this.bakSuffix;
235
+ const tmp = target + this.tmpSuffix;
236
+ for (const f of [target, bak, tmp]) {
237
+ try {
238
+ if (fs.existsSync(f))
239
+ fs.unlinkSync(f);
240
+ }
241
+ catch {
242
+ // best-effort
243
+ }
244
+ }
245
+ });
246
+ }
247
+ /** Returns the parsed cache file (or an empty shell on failure). */
248
+ read() {
249
+ return this.safeRead();
250
+ }
251
+ /** Absolute path of the on-disk cache file. */
252
+ filePath() {
253
+ return path.join(this.cacheDir, this.filename);
254
+ }
255
+ /**
256
+ * Prunes expired entries. Cheap O(n) scan — intended for engine
257
+ * startup. Returns the number of entries removed.
258
+ */
259
+ pruneExpired() {
260
+ let removed = 0;
261
+ this.withFileLockSync(() => {
262
+ const file = this.safeRead();
263
+ for (const [k, e] of Object.entries(file.entries)) {
264
+ if (this.isExpired(e)) {
265
+ delete file.entries[k];
266
+ removed++;
267
+ }
268
+ }
269
+ if (removed > 0)
270
+ this.atomicWrite(file);
271
+ });
272
+ return removed;
273
+ }
274
+ // ─────────────────────────────────────────────────────────────
275
+ // INTERNAL — read / write / lock
276
+ // ─────────────────────────────────────────────────────────────
277
+ isExpired(entry) {
278
+ if (!entry.ttlExpiresAt)
279
+ return false;
280
+ const t = Date.parse(entry.ttlExpiresAt);
281
+ if (!Number.isFinite(t))
282
+ return false;
283
+ return t <= Date.now();
284
+ }
285
+ async lockedWrite(input) {
286
+ const acquired = await this.acquireLockWithBackoff();
287
+ if (!acquired) {
288
+ console.warn(`[HealingCache] ⚠️ Could not acquire lock at ${this.lockDir} — write skipped`);
289
+ return;
290
+ }
291
+ try {
292
+ const file = this.safeRead();
293
+ const key = computeCacheKey(input);
294
+ file.entries[key] = this.buildEntry(key, input, file.entries[key]);
295
+ this.atomicWrite(file);
296
+ }
297
+ finally {
298
+ this.releaseLock();
299
+ }
300
+ }
301
+ buildEntry(key, input, prev) {
302
+ const now = new Date().toISOString();
303
+ const ttlMs = input.ttlMs ?? defaultTtlMs();
304
+ const ttlExpiresAt = new Date(Date.now() + ttlMs).toISOString();
305
+ return {
306
+ key,
307
+ filePath: input.filePath,
308
+ line: input.line,
309
+ dnaContentHash: input.dnaContentHash,
310
+ brokenSelector: input.brokenSelector,
311
+ mode: input.mode,
312
+ healedSelector: input.healedSelector,
313
+ chainSegments: input.chainSegments,
314
+ segments: input.segments,
315
+ confidence: input.confidence,
316
+ contentChange: input.contentChange,
317
+ explanation: input.explanation,
318
+ alternatives: input.alternatives,
319
+ safe: input.safe,
320
+ createdAt: prev?.createdAt ?? now,
321
+ ttlExpiresAt,
322
+ lastHitAt: prev?.lastHitAt,
323
+ hitCount: prev?.hitCount ?? 0,
324
+ };
325
+ }
326
+ /**
327
+ * Reads + parses the cache file. Tolerates every realistic failure
328
+ * (missing, empty, malformed, wrong schema version) by returning a
329
+ * fresh empty shell — the contract is "cache misses are silent".
330
+ */
331
+ safeRead() {
332
+ const target = path.join(this.cacheDir, this.filename);
333
+ let raw;
334
+ try {
335
+ raw = fs.readFileSync(target, "utf8");
336
+ }
337
+ catch {
338
+ return { schemaVersion: 1, entries: {} };
339
+ }
340
+ if (!raw || raw.trim().length === 0) {
341
+ return { schemaVersion: 1, entries: {} };
342
+ }
343
+ let parsed;
344
+ try {
345
+ parsed = JSON.parse(raw);
346
+ }
347
+ catch (err) {
348
+ console.warn(`[HealingCache] ⚠️ Corrupt cache file at ${target} — starting fresh (${err.message})`);
349
+ return { schemaVersion: 1, entries: {} };
350
+ }
351
+ if (!parsed ||
352
+ typeof parsed !== "object" ||
353
+ Array.isArray(parsed) ||
354
+ parsed.schemaVersion !== 1 ||
355
+ typeof parsed.entries !== "object" ||
356
+ parsed.entries === null ||
357
+ Array.isArray(parsed.entries)) {
358
+ console.warn(`[HealingCache] ⚠️ Unknown cache schema at ${target} — starting fresh`);
359
+ return { schemaVersion: 1, entries: {} };
360
+ }
361
+ return parsed;
362
+ }
363
+ /**
364
+ * Atomic write: write to `<file>.tmp`, copy current to `<file>.bak`,
365
+ * then rename `<file>.tmp` → `<file>`. The rename is the single
366
+ * commit point — a crash between bak and rename leaves the bak file
367
+ * intact for future recovery.
368
+ */
369
+ atomicWrite(file) {
370
+ if (!fs.existsSync(this.cacheDir)) {
371
+ fs.mkdirSync(this.cacheDir, { recursive: true });
372
+ }
373
+ const target = path.join(this.cacheDir, this.filename);
374
+ const tmp = target + this.tmpSuffix;
375
+ const bak = target + this.bakSuffix;
376
+ const json = JSON.stringify(file, null, 2);
377
+ fs.writeFileSync(tmp, json, "utf8");
378
+ if (fs.existsSync(target)) {
379
+ try {
380
+ fs.copyFileSync(target, bak);
381
+ }
382
+ catch (err) {
383
+ console.warn(`[HealingCache] ⚠️ Failed to write backup ${bak}: ${err.message}`);
384
+ }
385
+ }
386
+ fs.renameSync(tmp, target);
387
+ }
388
+ // ─────────────────────────────────────────────────────────────
389
+ // FILE LOCK — atomic mkdir as a cross-process mutex
390
+ // ─────────────────────────────────────────────────────────────
391
+ async acquireLockWithBackoff() {
392
+ const delaysMs = [50, 100, 200, 400, 800, 800];
393
+ for (let attempt = 0; attempt <= delaysMs.length; attempt++) {
394
+ if (this.tryAcquireLock())
395
+ return true;
396
+ if (attempt === delaysMs.length)
397
+ break;
398
+ await sleep(delaysMs[attempt]);
399
+ }
400
+ return false;
401
+ }
402
+ tryAcquireLock() {
403
+ if (!fs.existsSync(this.cacheDir)) {
404
+ fs.mkdirSync(this.cacheDir, { recursive: true });
405
+ }
406
+ try {
407
+ fs.mkdirSync(this.lockDir);
408
+ return true;
409
+ }
410
+ catch (err) {
411
+ if (err.code === "EEXIST")
412
+ return false;
413
+ console.warn(`[HealingCache] ⚠️ Lock acquire failed: ${err.message}`);
414
+ return false;
415
+ }
416
+ }
417
+ withFileLockSync(fn) {
418
+ const delaysMs = [10, 25, 50, 100];
419
+ for (let attempt = 0; attempt <= delaysMs.length; attempt++) {
420
+ if (this.tryAcquireLock()) {
421
+ try {
422
+ fn();
423
+ }
424
+ finally {
425
+ this.releaseLock();
426
+ }
427
+ return;
428
+ }
429
+ if (attempt === delaysMs.length)
430
+ break;
431
+ sleepBusy(delaysMs[attempt]);
432
+ }
433
+ console.warn(`[HealingCache] ⚠️ Lock acquire failed (sync) — operation skipped`);
434
+ }
435
+ releaseLock() {
436
+ try {
437
+ fs.rmdirSync(this.lockDir);
438
+ }
439
+ catch {
440
+ // best-effort
441
+ }
442
+ }
443
+ }
444
+ exports.HealingCacheService = HealingCacheService;
445
+ // ═══════════════════════════════════════════════════════════════════
446
+ // Utilities
447
+ // ═══════════════════════════════════════════════════════════════════
448
+ function sleep(ms) {
449
+ return new Promise((res) => setTimeout(res, ms));
450
+ }
451
+ function sleepBusy(ms) {
452
+ const end = Date.now() + ms;
453
+ while (Date.now() < end) {
454
+ // intentional spin
455
+ }
456
+ }
457
+ // ═══════════════════════════════════════════════════════════════════
458
+ // Module-level singleton — engines reuse the same cache file by default
459
+ // ═══════════════════════════════════════════════════════════════════
460
+ exports.sharedHealingCache = new HealingCacheService();
@@ -1 +1 @@
1
- {"version":3,"file":"InitializerUpdater.d.ts","sourceRoot":"","sources":["../../src/services/InitializerUpdater.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAiB,MAAM,UAAU,CAAC;AAY3D,MAAM,WAAW,iBAAiB;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAED,qBAAa,kBAAkB;IAC7B;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,CACV,SAAS,EAAE,IAAI,EACf,UAAU,EAAE,UAAU,EACtB,QAAQ,EAAE,MAAM,GACf,iBAAiB,GAAG,IAAI;CAqB5B"}
1
+ {"version":3,"file":"InitializerUpdater.d.ts","sourceRoot":"","sources":["../../src/services/InitializerUpdater.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAiB,MAAM,UAAU,CAAC;AAc3D,MAAM,WAAW,iBAAiB;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAED,qBAAa,kBAAkB;IAC7B;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,CACV,SAAS,EAAE,IAAI,EACf,UAAU,EAAE,UAAU,EACtB,QAAQ,EAAE,MAAM,GACf,iBAAiB,GAAG,IAAI;CAsC5B"}
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InitializerUpdater = void 0;
4
4
  const ts_morph_1 = require("ts-morph");
5
5
  const TemplateDiffService_js_1 = require("./TemplateDiffService.js");
6
+ const DryRunGuard_1 = require("../config/DryRunGuard");
7
+ const WorkspaceSnapshotService_1 = require("./WorkspaceSnapshotService");
6
8
  class InitializerUpdater {
7
9
  /**
8
10
  * Mutate a StringLiteral or NoSubstitutionTemplateLiteral node to
@@ -22,7 +24,24 @@ class InitializerUpdater {
22
24
  else {
23
25
  return null;
24
26
  }
25
- sourceFile.saveSync();
27
+ // Centralised write gate — mirrors ASTSourceUpdater._persistChanges.
28
+ // InitializerUpdater is the canonical write-head for definition-site
29
+ // mutations (SemanticDispatch → tracer-resolved identifier / property
30
+ // access / cross-file paths all converge here), so the Clean-Room
31
+ // snapshot ledger and DryRunGuard MUST be enforced here too.
32
+ // Without this, an upstream-traced fix (e.g. global StringLiteral at
33
+ // top of file) writes to disk but `WorkspaceSnapshotService` never
34
+ // sees it — `getMutatedSnapshots()` returns empty and PR automation
35
+ // skips the heal entirely.
36
+ const fp = sourceFile.getFilePath();
37
+ if (DryRunGuard_1.DryRunGuard.active()) {
38
+ DryRunGuard_1.DryRunGuard.logSkippedWrite("saveSync", fp);
39
+ }
40
+ else {
41
+ WorkspaceSnapshotService_1.sharedWorkspaceSnapshot.preWrite(fp);
42
+ sourceFile.saveSync();
43
+ WorkspaceSnapshotService_1.sharedWorkspaceSnapshot.postWrite(fp);
44
+ }
26
45
  return {
27
46
  lineUpdated: valueNode.getStartLineNumber() - 1,
28
47
  healedLocatorString: newValue,
@@ -1,3 +1,4 @@
1
+ import Anthropic from "@anthropic-ai/sdk";
1
2
  import { AncestorNode, AnchorInfo } from "../types";
2
3
  export interface AuditRequest {
3
4
  dnaText: string;
@@ -27,10 +28,26 @@ export interface AuditVerdict {
27
28
  bonus: number;
28
29
  };
29
30
  }
31
+ /**
32
+ * Constructor options for IntentAuditor.
33
+ *
34
+ * - `anthropic`: pre-built Anthropic client to use. Pass `null` explicitly
35
+ * to force the auditor into its disabled state (useful in tests that
36
+ * want to assert the SUSPICIOUS fallback path without touching env vars).
37
+ * Pass `undefined` (default) to fall back to env-based construction.
38
+ * - `apiKey`: override the ANTHROPIC_API_KEY env var (DI-friendly).
39
+ * - `model`: override the Haiku model id (lets tests pin the SDK call).
40
+ */
41
+ export interface IntentAuditorOptions {
42
+ anthropic?: Anthropic | null;
43
+ apiKey?: string;
44
+ model?: string;
45
+ }
30
46
  export declare class IntentAuditor {
31
47
  private anthropic;
32
48
  private readonly disabled;
33
- constructor();
49
+ private readonly model;
50
+ constructor(opts?: IntentAuditorOptions);
34
51
  auditIntent(req: AuditRequest): Promise<AuditVerdict>;
35
52
  computeAncestryPenalty(dnaPath: AncestorNode[], livePath: AncestorNode[]): number;
36
53
  computeAnchorBonus(dnaAnchors: AnchorInfo | null | undefined, liveAnchors: AnchorInfo | null | undefined): number;
@@ -1 +1 @@
1
- {"version":3,"file":"IntentAuditor.d.ts","sourceRoot":"","sources":["../../src/services/IntentAuditor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAOpD,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAA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