sela-core 1.0.3 → 1.0.5

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.
@@ -0,0 +1,726 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.SelaEngine = void 0;
37
+ const fs = __importStar(require("fs"));
38
+ const path = __importStar(require("path"));
39
+ const LLMService_1 = require("../services/LLMService");
40
+ const SnapshotService_1 = require("../services/SnapshotService");
41
+ const DOMUtils_1 = require("../utils/DOMUtils");
42
+ const SourceUpdater_1 = require("../services/SourceUpdater");
43
+ const SafetyGuard_1 = require("../services/SafetyGuard");
44
+ const ConfigLoader_1 = require("../config/ConfigLoader");
45
+ const HealReportService_1 = require("../services/HealReportService");
46
+ const PRAutomationService_1 = require("../services/PRAutomationService");
47
+ class SelaEngine {
48
+ llmService;
49
+ snapshotService;
50
+ safetyGuard;
51
+ config;
52
+ dnaBuffer = new Map();
53
+ healMetaBuffer = new Map();
54
+ testTitleBuffer = new Map();
55
+ constructor() {
56
+ this.config = ConfigLoader_1.ConfigLoader.getInstance();
57
+ this.llmService = new LLMService_1.LLMService();
58
+ this.snapshotService = new SnapshotService_1.SnapshotService();
59
+ this.safetyGuard = new SafetyGuard_1.SafetyGuard(this.config.thresholds);
60
+ }
61
+ // ─────────────────────────────────────────────────────────────
62
+ // healArgument — repairs a wrong action argument (e.g. selectOption)
63
+ // ─────────────────────────────────────────────────────────────
64
+ async healArgument(page, selector, action, oldArgument, stableId, filePath, line) {
65
+ console.log(`[Sela] 🔧 HEALING ARGUMENT for action '${action}': "${oldArgument}"`);
66
+ try {
67
+ const { dom } = await DOMUtils_1.DOMUtils.getNeighborhoodDom(page, selector);
68
+ const neighborhoodDom = dom;
69
+ const targetIntent = action === "selectOption"
70
+ ? `ARGUMENT_HEALING: The selectOption argument "${oldArgument}" does not exist.
71
+ Look at the <select> element in the DOM and find all <option> elements inside it.
72
+ Return in 'new_selector' ONLY the TEXT CONTENT of the closest matching <option>.
73
+ For example: if the options are "Male" and "Female" and the old argument was "Man", return "Male".
74
+ CRITICAL: Do NOT return a CSS selector like "#my-select". Return plain text only, exactly as it appears in the option.`
75
+ : `Fix the broken argument "${oldArgument}" for action "${action}". Return the correct value.`;
76
+ const aiFix = await this.llmService.getFix({
77
+ targetIntent,
78
+ failedSelector: `${selector}::${action}("${oldArgument}")`,
79
+ previousState: {},
80
+ currentDom: neighborhoodDom,
81
+ });
82
+ if (aiFix?.status === "FIXED" && aiFix.new_selector) {
83
+ const newArgument = aiFix.new_selector;
84
+ console.log(`[Sela] 🔧 New argument: "${newArgument}"`);
85
+ SourceUpdater_1.SourceUpdater.updateArgument({ filePath, line }, action, oldArgument, newArgument);
86
+ return newArgument;
87
+ }
88
+ }
89
+ catch (error) {
90
+ console.error(`[Sela] ❌ Argument healing error:`, error.message);
91
+ }
92
+ return null;
93
+ }
94
+ // ─────────────────────────────────────────────────────────────
95
+ // heal — repairs a broken selector
96
+ // ─────────────────────────────────────────────────────────────
97
+ async heal(page, fullSelector, elementSelectorOnly, stableId, filePath, line, healMode = "action") {
98
+ console.log(`\n[Sela] 🛠️ HEALING START for: ${stableId}`);
99
+ // Resolve file path for report purposes (project-relative when possible).
100
+ const reportFile = this._toReportFile(filePath);
101
+ const testTitle = this.testTitleBuffer.get(stableId);
102
+ // Snapshot for report event context (loaded once below, used at all exit paths).
103
+ let lastKnownStateForReport = null;
104
+ let aiFixForReport = null;
105
+ try {
106
+ // load() now always returns ElementSnapshotV2 (v1 snapshots are
107
+ // migrated transparently; v2 fields default to safe values).
108
+ const lastKnownState = await this.snapshotService.load(stableId);
109
+ lastKnownStateForReport = lastKnownState;
110
+ // ── Extract v2 DNA context ────────────────────────────────
111
+ const dnaAncestry = lastKnownState?.ancestry ?? [];
112
+ const dnaAnchors = lastKnownState?.anchors;
113
+ console.log(`[Sela] 🔍 Step 2: Extracting neighborhood DOM...`);
114
+ const { dom: neighborhoodDom, successfulPath } = await DOMUtils_1.DOMUtils.getNeighborhoodDom(page, fullSelector);
115
+ if (!neighborhoodDom || neighborhoodDom.length === 0) {
116
+ throw new Error("Failed to extract any DOM context for AI");
117
+ }
118
+ console.log(`[Sela] 🔍 Step 2: DOM Extracted (${neighborhoodDom.length} chars) ✅`);
119
+ const contextHint = successfulPath.length > 0
120
+ ? `\n\n[CONTEXT HINT]: The provided DOM is extracted from INSIDE this frame path: "${successfulPath.join(" >> ")}". ` +
121
+ `If you find the element, your suggested selector should be relative to THIS context.`
122
+ : "";
123
+ const oldText = lastKnownState?.text ?? null;
124
+ const dnaRole = lastKnownState?.role ?? undefined;
125
+ const intentDescription = lastKnownState
126
+ ? `The element that was a ${lastKnownState.tagName} with text "${lastKnownState.text}"`
127
+ : `Element identified by ${stableId}`;
128
+ const contentChangeInstructions = `
129
+
130
+ [CONTENT CHANGE DETECTION]:
131
+ If the element's VISIBLE TEXT has changed compared to its previous value${oldText ? ` ("${oldText}")` : ""}, you MUST include a "contentChange" field.
132
+ NOTE: This change will NOT be applied automatically to the source code to prevent masking potential bugs.
133
+ It will only be presented as a suggested fix in the terminal.
134
+ {
135
+ "contentChange": {
136
+ "oldText": "<the previous visible text>",
137
+ "newText": "<the new visible text as it appears in the current DOM>"
138
+ }
139
+ }
140
+ This is required so that any expect(...).toHaveText("${oldText ?? "..."}") assertions in the test file can be automatically updated to match the new text.
141
+ If the text has NOT changed, omit the "contentChange" field entirely.`;
142
+ // ── Semantic anchor context hint for the LLM ─────────────────────
143
+ // When DNA v2 anchor data exists, tell the AI which label and row
144
+ // key the element was associated with so it can use semantic landmarks
145
+ // to disambiguate identical controls (e.g. multiple "Edit" buttons).
146
+ const anchorContextHint = (() => {
147
+ const anchors = lastKnownState?.anchors;
148
+ if (!anchors)
149
+ return "";
150
+ const lines = [];
151
+ if (anchors.closestLabel) {
152
+ lines.push(`- Label: "${anchors.closestLabel}" — the UI label or heading associated with this element.`);
153
+ }
154
+ if (anchors.rowAnchor) {
155
+ lines.push(`- Row key: "${anchors.rowAnchor}" — unique cell text identifying the table row this element belonged to.`);
156
+ }
157
+ if (lines.length === 0)
158
+ return "";
159
+ return ("\n\n[ELEMENT SEMANTIC CONTEXT]:\n" +
160
+ "The DNA snapshot recorded these semantic landmarks for the element you must find:\n" +
161
+ lines.join("\n") +
162
+ "\n" +
163
+ "Prioritize selectors that preserve these relationships (e.g. filter by row key, then target by label).");
164
+ })();
165
+ console.log(`[Sela] 🔍 Step 3: Requesting AI fix from LLM...`);
166
+ const aiFix = await this.llmService.getFix({
167
+ targetIntent: intentDescription,
168
+ failedSelector: fullSelector,
169
+ previousState: lastKnownState || {},
170
+ currentDom: neighborhoodDom +
171
+ contextHint +
172
+ anchorContextHint +
173
+ contentChangeInstructions,
174
+ });
175
+ aiFixForReport = aiFix;
176
+ if (aiFix && aiFix.status === "FIXED") {
177
+ console.log(`[Sela] 🔍 Step 3: AI responded ✅`);
178
+ // ── Build new selector ────────────────────────────────────
179
+ const aiSegments = aiFix.segments || [];
180
+ const newSegmentsFromAI = aiSegments.filter((aiSeg) => !successfulPath.includes(aiSeg.selector));
181
+ const newFullSelector = [
182
+ ...successfulPath,
183
+ ...newSegmentsFromAI.map((s) => s.selector),
184
+ ].join(" >> ");
185
+ const rawElementSelector = newSegmentsFromAI.map((s) => s.selector).join(" >> ") ||
186
+ newFullSelector;
187
+ const framePrefix = successfulPath.join(" >> ");
188
+ const elementOnlySelector = framePrefix && rawElementSelector.startsWith(framePrefix + " >> ")
189
+ ? rawElementSelector.slice(framePrefix.length + 4)
190
+ : rawElementSelector;
191
+ // ── Step 3.5: Fetch live text (lightweight zero-trust probe) ─
192
+ const liveText = await this.fetchLiveText(page, elementOnlySelector, successfulPath);
193
+ const dnaBaselineText = oldText ?? undefined;
194
+ if (liveText !== null) {
195
+ console.log(`[Sela] 🔍 Step 3.5: Live text fetched: "${liveText}"` +
196
+ (dnaBaselineText ? ` (DNA baseline: "${dnaBaselineText}")` : ""));
197
+ }
198
+ else {
199
+ console.log(`[Sela] 🔍 Step 3.5: Live text fetch skipped (element not yet rendered or non-text)`);
200
+ }
201
+ // ── Step 3.6: Atomic v2 capture of the candidate element ──
202
+ // Single page.evaluate() call — collects visibility, ancestry,
203
+ // and anchors of the healed element for SafetyGuard use.
204
+ // Returns null on any failure; never blocks the heal.
205
+ const liveSnapshot = await this.snapshotService.captureElement(page, elementOnlySelector, successfulPath);
206
+ const liveVisibility = liveSnapshot?.visibility;
207
+ const liveAncestry = liveSnapshot?.ancestry;
208
+ const liveAnchors = liveSnapshot?.anchors;
209
+ if (liveSnapshot) {
210
+ const ghostFlag = liveVisibility?.isGhost
211
+ ? ` 👻 GHOST(${liveVisibility.ghostReason})`
212
+ : "";
213
+ const occFlag = liveVisibility?.isOccluded ? " 🫥 OCCLUDED" : "";
214
+ console.log(`[Sela] 🔍 Step 3.6: Live v2 capture complete` +
215
+ `${ghostFlag}${occFlag} — ancestry depth: ${liveAncestry?.length ?? 0}`);
216
+ }
217
+ else {
218
+ console.log(`[Sela] 🔍 Step 3.6: Live v2 capture skipped`);
219
+ }
220
+ // ── Safety: delegate all go/no-go decisions to SafetyGuard ─
221
+ const branch = ConfigLoader_1.ConfigLoader.getBranch() ?? undefined;
222
+ const safetyDecision = await this.safetyGuard.evaluate({ mode: healMode, branch }, {
223
+ confidence: aiFix.confidence,
224
+ contentChange: aiFix.contentChange,
225
+ explanation: aiFix.explanation,
226
+ liveText: liveText ?? undefined,
227
+ dnaBaselineText,
228
+ dnaRole,
229
+ // v2 structural context
230
+ liveVisibility,
231
+ liveAncestry,
232
+ liveAnchors,
233
+ dnaAncestry,
234
+ dnaAnchors,
235
+ });
236
+ console.log(`[SafetyGuard] ${safetyDecision.canProceed ? "✅" : "❌"} [${safetyDecision.level}] ${safetyDecision.reason}`);
237
+ if (!safetyDecision.canProceed) {
238
+ if (safetyDecision.level === "BLOCKED" &&
239
+ safetyDecision.meta?.auditorVerdict === "SUSPICIOUS") {
240
+ const strictness = this.config.thresholds.auditorStrictness;
241
+ const conf = safetyDecision.meta.auditorConfidence ?? "?";
242
+ const suggestion = strictness === "strict" ? "Balanced" : "Loose";
243
+ console.warn(`[Sela] ⚠️ Auditor status: SUSPICIOUS (Confidence: ${conf}%)`);
244
+ console.warn(`[Sela] 🚫 Policy: '${strictness}' (Branch: ${branch ?? "unknown"}). Auto-fix blocked.`);
245
+ console.warn(`[Sela] 💡 To allow this, change 'auditorStrictness' to '${suggestion}' in sela.config.ts.`);
246
+ }
247
+ // ── Report: PROTECTED (or BLOCKED non-INCONSISTENT) ─────────
248
+ // The auto-fix was blocked. If the auditor verdict is INCONSISTENT
249
+ // this is a "Security Win" — a masked regression was caught.
250
+ const protectedEvent = {
251
+ kind: "PROTECTED",
252
+ stableId,
253
+ sourceFile: reportFile,
254
+ sourceLine: line,
255
+ testTitle,
256
+ timestamp: new Date().toISOString(),
257
+ framePath: successfulPath,
258
+ inIframe: successfulPath.length > 0,
259
+ inShadowDom: liveSnapshot?.isInShadowDom ?? false,
260
+ oldSelector: elementSelectorOnly,
261
+ candidateNewSelector: newFullSelector,
262
+ reason: safetyDecision.reason,
263
+ safetyLevel: safetyDecision.level,
264
+ auditor: this._auditorBlockFromDecision(safetyDecision.meta),
265
+ aiConfidence: typeof aiFix.confidence === "number" ? aiFix.confidence : 0,
266
+ aiExplanation: aiFix.explanation ?? "",
267
+ dnaBefore: (0, HealReportService_1.summariseSnapshot)(lastKnownState),
268
+ dnaAfter: (0, HealReportService_1.summariseSnapshot)(liveSnapshot ?? null),
269
+ contentChange: aiFix.contentChange,
270
+ };
271
+ HealReportService_1.sharedHealReport.record(protectedEvent);
272
+ throw new Error(`[SafetyGuard] ${safetyDecision.level}: ${safetyDecision.reason}`);
273
+ }
274
+ // ── Heal-completion score summary ─────────────────────────────────
275
+ // Emits a single consolidated line showing AI confidence plus the
276
+ // auditor's structural adjustments (ancestry drift / anchor bonus).
277
+ const breakdown = safetyDecision.meta?.auditScoreBreakdown;
278
+ if (breakdown && (breakdown.penalty !== 0 || breakdown.bonus !== 0)) {
279
+ const parts = [];
280
+ if (breakdown.penalty !== 0)
281
+ parts.push(`${breakdown.penalty} ancestry drift`);
282
+ if (breakdown.bonus !== 0)
283
+ parts.push(`+${breakdown.bonus} anchor match`);
284
+ console.log(`[Sela] 📊 Score — AI: ${aiFix.confidence}% | ` +
285
+ `Auditor: ${breakdown.adjustedConfidence}% ` +
286
+ `(raw ${breakdown.rawConfidence}%, ${parts.join(", ")})`);
287
+ }
288
+ console.log(`[Sela] 🚀 Rebuilt Full Runtime Selector (Clean): ${newFullSelector}`);
289
+ // Snapshot the ENTIRE source file BEFORE the AST mutation.
290
+ // We don't know which line will actually be mutated until update()
291
+ // returns (JumpToDef may land on the const-decl line, not the failure
292
+ // line) — so we keep the pre-image around and slice into it after.
293
+ const preMutationLines = this._safeReadAllLines(filePath);
294
+ const updateResult = SourceUpdater_1.SourceUpdater.update({ filePath, line }, elementSelectorOnly, newFullSelector, neighborhoodDom, undefined, fullSelector, aiFix.segments, aiFix.contentChange, aiFix.chainSegments, aiFix.originalChainHint, this.config.updateStrategy, this.config.autoCommit);
295
+ // Resolve the line that was actually mutated. Order of precedence:
296
+ // 1. updateResult.lineUpdated (the strategy's literal write location)
297
+ // 2. updateResult.definitionSite.line (semantic JumpToDef target,
298
+ // used when the mutation crossed into a const/variable declaration)
299
+ // 3. line (the original failure line — last-resort fallback)
300
+ // For cross-file definitionSite.file, we still read the new line from
301
+ // the SAME file that was mutated.
302
+ const defSite = updateResult.definitionSite;
303
+ const sameFileDef = defSite && defSite.file
304
+ ? path.resolve(defSite.file) ===
305
+ (SourceUpdater_1.SourceUpdater.resolveFilePath(filePath) ?? "")
306
+ : true;
307
+ const writtenLine = updateResult.lineUpdated ??
308
+ (sameFileDef ? defSite?.line : undefined) ??
309
+ line;
310
+ const mutatedFilePath = !sameFileDef && defSite ? defSite.file : filePath;
311
+ // Old line: PRE-mutation content at the line that ended up being mutated.
312
+ // If the mutation was cross-file, read the pre-image of THAT file by
313
+ // recovering it from disk minus our own change (we don't track that —
314
+ // fall back to current pre-image if same file, else empty string).
315
+ const oldCodeLine = sameFileDef && preMutationLines.length > 0
316
+ ? (preMutationLines[writtenLine - 1] ?? "")
317
+ : this._safeReadLine(mutatedFilePath, writtenLine);
318
+ // New line: POST-mutation content at the same line in the mutated file.
319
+ const newCodeLine = this._safeReadLine(mutatedFilePath, writtenLine);
320
+ const canonicalSelector = updateResult.healedLocatorString ?? newFullSelector;
321
+ console.log(`[Sela] ✅ Canonical selector (disk == runtime): "${canonicalSelector}"`);
322
+ // Buffer CLI-ready metadata — merged into DNA at commitUpdates() time.
323
+ // Sanitize filePath: strip any residual "at " stack-trace prefix + whitespace,
324
+ // then resolve to an absolute path before converting to project-relative form.
325
+ const match = filePath.match(/at\s+(.*)/i);
326
+ // 2. אם מצאנו התאמה ניקח אותה, אחרת ניקח את המחרוזת המקורית.
327
+ // בנוסף, ננקה רווחים וסוגריים (לפעמים נתיבים ב-Stack trace מוקפים בסוגריים).
328
+ const cleanFilePath = (match ? match[1] : filePath)
329
+ .replace(/^\(|\)$/g, "") // מסיר סוגריים אם קיימים ( )
330
+ .trim();
331
+ // 3. הפיכה לנתיב אבסולוטי ואז ליחסי (כמו שעשית, שזה מעולה)
332
+ const absFilePath = cleanFilePath && path.isAbsolute(cleanFilePath)
333
+ ? cleanFilePath
334
+ : cleanFilePath
335
+ ? path.resolve(process.cwd(), cleanFilePath)
336
+ : "";
337
+ const relativeSourceFile = absFilePath
338
+ ? path.relative(process.cwd(), absFilePath)
339
+ : "";
340
+ this.healMetaBuffer.set(stableId, {
341
+ selector: canonicalSelector,
342
+ sourceFile: relativeSourceFile,
343
+ sourceLine: line,
344
+ originalSelector: elementSelectorOnly,
345
+ healedSelector: canonicalSelector,
346
+ lastHealed: new Date().toISOString(),
347
+ status: "healed",
348
+ definitionSite: updateResult.definitionSite,
349
+ blastRadius: updateResult.blastRadius,
350
+ });
351
+ // ── Report: HEALED event ───────────────────────────────────────
352
+ const healedEvent = {
353
+ kind: "HEALED",
354
+ stableId,
355
+ sourceFile: relativeSourceFile || reportFile,
356
+ sourceLine: line,
357
+ testTitle,
358
+ timestamp: new Date().toISOString(),
359
+ framePath: successfulPath,
360
+ inIframe: successfulPath.length > 0,
361
+ inShadowDom: liveSnapshot?.isInShadowDom ?? false,
362
+ oldSelector: elementSelectorOnly,
363
+ newSelector: canonicalSelector,
364
+ oldCodeLine,
365
+ newCodeLine,
366
+ newLineNumber: writtenLine,
367
+ aiConfidence: typeof aiFix.confidence === "number" ? aiFix.confidence : 0,
368
+ aiExplanation: aiFix.explanation ?? "",
369
+ aiAlternatives: this._buildAlternatives(aiFix.chainSegments, aiFix.segments),
370
+ reasoningSteps: this._buildReasoningSteps({
371
+ aiFix,
372
+ dnaText: lastKnownState?.text ?? null,
373
+ dnaTag: lastKnownState?.tagName ?? null,
374
+ safetyLevel: safetyDecision.level,
375
+ strategy: updateResult.reason || "ast-update",
376
+ writtenLine,
377
+ blastRadius: updateResult.blastRadius,
378
+ successfulPath,
379
+ }),
380
+ auditor: this._auditorBlockFromDecision(safetyDecision.meta),
381
+ dnaBefore: (0, HealReportService_1.summariseSnapshot)(lastKnownState),
382
+ dnaAfter: (0, HealReportService_1.summariseSnapshot)(liveSnapshot ?? null),
383
+ diffStrategy: updateResult.reason || "ast-update",
384
+ blastRadius: updateResult.blastRadius,
385
+ definitionSite: updateResult.definitionSite,
386
+ contentChange: aiFix.contentChange,
387
+ };
388
+ HealReportService_1.sharedHealReport.record(healedEvent);
389
+ return canonicalSelector;
390
+ }
391
+ // AI did not return FIXED — record as FAILED.
392
+ const failedEvent = {
393
+ kind: "FAILED",
394
+ stableId,
395
+ sourceFile: reportFile,
396
+ sourceLine: line,
397
+ testTitle,
398
+ timestamp: new Date().toISOString(),
399
+ framePath: [],
400
+ inIframe: false,
401
+ inShadowDom: lastKnownStateForReport?.isInShadowDom ?? false,
402
+ oldSelector: elementSelectorOnly,
403
+ reason: "AI could not provide a fix (NOT_FOUND)",
404
+ aiConfidence: aiFix?.confidence,
405
+ aiExplanation: aiFix?.explanation,
406
+ dnaBefore: (0, HealReportService_1.summariseSnapshot)(lastKnownStateForReport),
407
+ };
408
+ HealReportService_1.sharedHealReport.record(failedEvent);
409
+ throw new Error("AI could not provide a fix");
410
+ }
411
+ catch (error) {
412
+ console.error(`[Sela] ❌ Healing Failure:`, error.message);
413
+ // Avoid double-recording when a SafetyGuard PROTECTED event was already pushed.
414
+ const isSafetyBlock = typeof error?.message === "string" && error.message.includes("[SafetyGuard]");
415
+ const isNotFoundAlreadyRecorded = typeof error?.message === "string" && error.message === "AI could not provide a fix";
416
+ if (!isSafetyBlock && !isNotFoundAlreadyRecorded) {
417
+ const failedEvent = {
418
+ kind: "FAILED",
419
+ stableId,
420
+ sourceFile: reportFile,
421
+ sourceLine: line,
422
+ testTitle,
423
+ timestamp: new Date().toISOString(),
424
+ framePath: [],
425
+ inIframe: false,
426
+ inShadowDom: lastKnownStateForReport?.isInShadowDom ?? false,
427
+ oldSelector: elementSelectorOnly,
428
+ reason: error?.message ?? "Unknown heal failure",
429
+ aiConfidence: aiFixForReport?.confidence,
430
+ aiExplanation: aiFixForReport?.explanation,
431
+ dnaBefore: (0, HealReportService_1.summariseSnapshot)(lastKnownStateForReport),
432
+ };
433
+ HealReportService_1.sharedHealReport.record(failedEvent);
434
+ }
435
+ throw error;
436
+ }
437
+ }
438
+ // ─────────────────────────────────────────────────────────────
439
+ // Report helpers
440
+ // ─────────────────────────────────────────────────────────────
441
+ _toReportFile(filePath) {
442
+ if (!filePath)
443
+ return "";
444
+ const match = filePath.match(/at\s+(.*)/i);
445
+ const cleaned = (match ? match[1] : filePath).replace(/^\(|\)$/g, "").trim();
446
+ const abs = path.isAbsolute(cleaned) ? cleaned : path.resolve(process.cwd(), cleaned);
447
+ return path.relative(process.cwd(), abs) || abs;
448
+ }
449
+ _safeReadLine(filePath, lineNumber) {
450
+ try {
451
+ const cleaned = this._toReportFile(filePath);
452
+ const abs = path.isAbsolute(cleaned) ? cleaned : path.resolve(process.cwd(), cleaned);
453
+ if (!fs.existsSync(abs))
454
+ return "";
455
+ const lines = fs.readFileSync(abs, "utf8").split(/\r?\n/);
456
+ const idx = Math.max(0, Math.min(lines.length - 1, lineNumber - 1));
457
+ return lines[idx] ?? "";
458
+ }
459
+ catch {
460
+ return "";
461
+ }
462
+ }
463
+ /**
464
+ * Snapshot the entire file as a string[] of lines. Returns an empty array
465
+ * if the file is missing or unreadable. Used to preserve the PRE-mutation
466
+ * source so the diff in HealedEvent reflects the line that was actually
467
+ * mutated (which may differ from the failure line when JumpToDef lands on
468
+ * a const declaration above).
469
+ */
470
+ _safeReadAllLines(filePath) {
471
+ try {
472
+ const cleaned = this._toReportFile(filePath);
473
+ const abs = path.isAbsolute(cleaned) ? cleaned : path.resolve(process.cwd(), cleaned);
474
+ if (!fs.existsSync(abs))
475
+ return [];
476
+ return fs.readFileSync(abs, "utf8").split(/\r?\n/);
477
+ }
478
+ catch {
479
+ return [];
480
+ }
481
+ }
482
+ _auditorBlockFromDecision(meta) {
483
+ if (!meta || !meta.auditorVerdict)
484
+ return null;
485
+ return {
486
+ verdict: meta.auditorVerdict,
487
+ confidence: meta.auditorConfidence ?? 0,
488
+ reason: meta.auditScoreBreakdown
489
+ ? `Adjusted ${meta.auditScoreBreakdown.rawConfidence}% → ${meta.auditScoreBreakdown.adjustedConfidence}% (penalty ${meta.auditScoreBreakdown.penalty}, bonus +${meta.auditScoreBreakdown.bonus}).`
490
+ : "Auditor verdict recorded by SafetyGuard.",
491
+ inversionType: null,
492
+ rawConfidence: meta.auditScoreBreakdown?.rawConfidence,
493
+ penalty: meta.auditScoreBreakdown?.penalty,
494
+ bonus: meta.auditScoreBreakdown?.bonus,
495
+ };
496
+ }
497
+ _buildAlternatives(chainSegments, segments) {
498
+ const out = [];
499
+ if (chainSegments && chainSegments.length) {
500
+ for (const seg of chainSegments) {
501
+ switch (seg.type) {
502
+ case "locator":
503
+ out.push(`locator(${JSON.stringify(seg.selector)})`);
504
+ break;
505
+ case "frameLocator":
506
+ out.push(`frameLocator(${JSON.stringify(seg.selector)})`);
507
+ break;
508
+ case "getByRole":
509
+ out.push(`getByRole(${JSON.stringify(seg.role)}${seg.name ? ", { name: " + JSON.stringify(seg.name) + (seg.exact ? ", exact: true" : "") + " }" : ""})`);
510
+ break;
511
+ case "getByLabel":
512
+ out.push(`getByLabel(${JSON.stringify(seg.text)}${seg.exact ? ", { exact: true }" : ""})`);
513
+ break;
514
+ case "getByText":
515
+ out.push(`getByText(${JSON.stringify(seg.text)}${seg.exact ? ", { exact: true }" : ""})`);
516
+ break;
517
+ case "getByPlaceholder":
518
+ out.push(`getByPlaceholder(${JSON.stringify(seg.text)})`);
519
+ break;
520
+ case "getByTestId":
521
+ out.push(`getByTestId(${JSON.stringify(seg.testId)})`);
522
+ break;
523
+ case "getByAltText":
524
+ out.push(`getByAltText(${JSON.stringify(seg.text)})`);
525
+ break;
526
+ case "getByTitle":
527
+ out.push(`getByTitle(${JSON.stringify(seg.text)})`);
528
+ break;
529
+ case "filter":
530
+ out.push(`filter(${JSON.stringify(seg)})`);
531
+ break;
532
+ case "first":
533
+ out.push("first()");
534
+ break;
535
+ case "last":
536
+ out.push("last()");
537
+ break;
538
+ case "nth":
539
+ out.push(`nth(${seg.index})`);
540
+ break;
541
+ }
542
+ }
543
+ }
544
+ if (segments && segments.length) {
545
+ out.push(segments.map((s) => s.selector).join(" >> "));
546
+ }
547
+ return Array.from(new Set(out)).slice(0, 8);
548
+ }
549
+ _buildReasoningSteps(ctx) {
550
+ const steps = [];
551
+ if (ctx.dnaTag || ctx.dnaText) {
552
+ steps.push(`Recovered DNA baseline — previously a <${ctx.dnaTag ?? "?"}>` +
553
+ (ctx.dnaText ? ` with text "${ctx.dnaText}"` : "") + ".");
554
+ }
555
+ if (ctx.successfulPath.length > 0) {
556
+ steps.push(`Resolved DOM context inside ${ctx.successfulPath.length} nested frame(s): ${ctx.successfulPath.join(" >> ")}.`);
557
+ }
558
+ const firstSeg = ctx.aiFix.chainSegments?.[0];
559
+ if (firstSeg) {
560
+ steps.push(`AI preserved the ${firstSeg.type} pattern — semantic locator over raw CSS path.`);
561
+ }
562
+ if (ctx.aiFix.explanation) {
563
+ steps.push(`AI reasoning: ${ctx.aiFix.explanation}`);
564
+ }
565
+ steps.push(`Confidence: ${ctx.aiFix.confidence}%.`);
566
+ if (ctx.aiFix.contentChange) {
567
+ steps.push(`Detected content drift: "${ctx.aiFix.contentChange.oldText}" → "${ctx.aiFix.contentChange.newText}".`);
568
+ }
569
+ steps.push(`SafetyGuard verdict: ${ctx.safetyLevel} — change cleared for write.`);
570
+ steps.push(`Source updated via ${ctx.strategy} at line ${ctx.writtenLine}.`);
571
+ if (ctx.blastRadius != null && ctx.blastRadius > 0) {
572
+ steps.push(`Blast radius: ${ctx.blastRadius} dependent test reference(s) inherited the fix.`);
573
+ }
574
+ return steps;
575
+ }
576
+ preloadTestTitle(stableId, testTitle) {
577
+ this.testTitleBuffer.set(stableId, testTitle);
578
+ }
579
+ async saveSnapshot(stableId, data) {
580
+ this.dnaBuffer.set(stableId, data);
581
+ }
582
+ async commitUpdates() {
583
+ if (this.dnaBuffer.size > 0) {
584
+ for (const [key, dna] of this.dnaBuffer.entries()) {
585
+ const meta = this.healMetaBuffer.get(key);
586
+ const toSave = meta ? { ...dna, ...meta } : dna;
587
+ await this.snapshotService.save(key, toSave);
588
+ }
589
+ this.dnaBuffer.clear();
590
+ this.healMetaBuffer.clear();
591
+ this.testTitleBuffer.clear();
592
+ SourceUpdater_1.SourceUpdater.flushAdvisories();
593
+ }
594
+ // ── Sela Insights — render the transparent healing report ───────
595
+ let reportHtmlPath = null;
596
+ const healedEvents = HealReportService_1.sharedHealReport.getHealedEvents();
597
+ const protectedEvents = HealReportService_1.sharedHealReport.getProtectedEvents();
598
+ if (HealReportService_1.sharedHealReport.hasContent()) {
599
+ try {
600
+ reportHtmlPath = HealReportService_1.sharedHealReport.flushToDisk(process.cwd());
601
+ if (reportHtmlPath) {
602
+ console.log(`\n[Sela] 📄 Insights report written: ${path.relative(process.cwd(), reportHtmlPath)}`);
603
+ console.log(`[Sela] Open with: npx sela show-report`);
604
+ }
605
+ }
606
+ catch (err) {
607
+ console.warn(`[Sela] ⚠️ Failed to write insights report: ${err.message}`);
608
+ }
609
+ }
610
+ // ── PR Automation — batched git-flow at session end ─────────────
611
+ if (this.config.prAutomation.enabled) {
612
+ try {
613
+ const branchInfo = (0, PRAutomationService_1.detectBranch)(process.cwd());
614
+ const decision = (0, PRAutomationService_1.decideEffectiveStrategy)(this.config.prAutomation, branchInfo.branch, healedEvents);
615
+ if (decision.downgradeReason) {
616
+ console.warn(`[Sela PR] ⚠️ Strategy downgraded: '${decision.configured}' → '${decision.effective}' ` +
617
+ `(reason: ${decision.downgradeReason}, ` +
618
+ `minAI: ${decision.minAiConfidence}%, minAuditor: ${decision.minAuditorConfidence}%)`);
619
+ }
620
+ else {
621
+ console.log(`[Sela PR] 🎯 Effective strategy: '${decision.effective}' on branch '${branchInfo.branch ?? "unknown"}'`);
622
+ }
623
+ const ctx = { cwd: process.cwd(), reportHtmlPath };
624
+ await (0, PRAutomationService_1.execute)(this.config.prAutomation, decision, healedEvents, branchInfo, ctx);
625
+ await (0, PRAutomationService_1.handleBugDetected)(this.config.prAutomation.onBugDetected, protectedEvents, branchInfo, ctx);
626
+ }
627
+ catch (err) {
628
+ console.warn(`[Sela PR] ⚠️ PR automation step failed: ${err.message}`);
629
+ }
630
+ }
631
+ // Clear the report buffer last so any downstream consumers (e.g. test
632
+ // re-runs in the same process) start fresh on the next session.
633
+ if (HealReportService_1.sharedHealReport.hasContent()) {
634
+ HealReportService_1.sharedHealReport.clear();
635
+ }
636
+ }
637
+ // ─────────────────────────────────────────────────────────────
638
+ // fetchLiveText — frame-aware innerText fetch (zero-trust probe)
639
+ //
640
+ // Navigates the frame chain encoded in successfulPath, then calls
641
+ // .first().innerText() on the element-only selector. Uses a short
642
+ // 1.5 s timeout so it never adds meaningful latency. Returns null
643
+ // on ANY failure — the caller must treat null as "no data" and skip
644
+ // live verification rather than blocking the heal.
645
+ // ─────────────────────────────────────────────────────────────
646
+ async fetchLiveText(page, elementOnlySelector, framePath) {
647
+ try {
648
+ let ctx = page;
649
+ for (const frameSel of framePath) {
650
+ ctx = ctx.frameLocator(frameSel);
651
+ }
652
+ const text = await ctx
653
+ .locator(elementOnlySelector)
654
+ .first()
655
+ .innerText({ timeout: 1500 });
656
+ return text.trim().length > 0 ? text.trim() : null;
657
+ }
658
+ catch {
659
+ return null;
660
+ }
661
+ }
662
+ _rebuildSelectorWithFrames(fullSelector, elementSelectorOnly, aiNewSelector) {
663
+ const fullParts = fullSelector.split(" >> ").map((s) => s.trim());
664
+ const elementParts = elementSelectorOnly.split(" >> ").map((s) => s.trim());
665
+ const aiParts = aiNewSelector.split(" >> ").map((s) => s.trim());
666
+ const frameParts = fullParts.slice(0, fullParts.length - elementParts.length);
667
+ if (frameParts.length === 0)
668
+ return aiNewSelector;
669
+ const aiHasFrameSegments = aiParts.some((p) => p.includes("-frame") ||
670
+ p === "iframe" ||
671
+ p === "frame" ||
672
+ p.includes("frame-"));
673
+ if (aiHasFrameSegments) {
674
+ console.log(`[Sela] 🔗 AI returned full frame path — using directly: ${aiNewSelector}`);
675
+ return aiNewSelector;
676
+ }
677
+ return [...frameParts, ...aiParts].join(" >> ");
678
+ }
679
+ getAllFiles(dirPath, arrayOfFiles = []) {
680
+ const files = fs.readdirSync(dirPath);
681
+ const ignoreList = [
682
+ "node_modules",
683
+ ".git",
684
+ "fixwright-snapshots",
685
+ "dist",
686
+ "test-results",
687
+ "tests/reset-demo.ts",
688
+ ];
689
+ files.forEach((file) => {
690
+ const fullPath = path.join(dirPath, file);
691
+ if (fs.statSync(fullPath).isDirectory()) {
692
+ if (!ignoreList.includes(file))
693
+ this.getAllFiles(fullPath, arrayOfFiles);
694
+ }
695
+ else if (file.endsWith(".ts") || file.endsWith(".js")) {
696
+ arrayOfFiles.push(fullPath);
697
+ }
698
+ });
699
+ return arrayOfFiles;
700
+ }
701
+ // ─────────────────────────────────────────────────────────────
702
+ // captureSuccessfulElement — capture and buffer a v2 DNA snapshot.
703
+ //
704
+ // Replaces the previous DOMUtils.getElementDNA() path with a single
705
+ // captureElement() call that collects all v2 metrics atomically.
706
+ // ─────────────────────────────────────────────────────────────
707
+ async captureSuccessfulElement(page, selector, stableId) {
708
+ try {
709
+ const snapshot = await this.snapshotService.captureElement(page, selector);
710
+ if (snapshot) {
711
+ const testTitle = this.testTitleBuffer.get(stableId);
712
+ const enriched = {
713
+ ...snapshot,
714
+ selector,
715
+ ...(testTitle ? { testTitle } : {}),
716
+ };
717
+ this.dnaBuffer.set(stableId, enriched);
718
+ console.log(`[Sela] 🧬 DNA v2 captured for: ${stableId}`);
719
+ }
720
+ }
721
+ catch (error) {
722
+ console.debug(`[Sela] 🧬 DNA capture skipped: ${error.message}`);
723
+ }
724
+ }
725
+ }
726
+ exports.SelaEngine = SelaEngine;