sela-core 1.0.2 → 1.0.4
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.
- package/dist/cli/commands/showReport.d.ts +3 -0
- package/dist/cli/commands/showReport.d.ts.map +1 -0
- package/dist/cli/commands/showReport.js +80 -0
- package/dist/cli/index.js +4 -1
- package/dist/config/SelaConfig.d.ts +57 -11
- package/dist/config/SelaConfig.d.ts.map +1 -1
- package/dist/config/SelaConfig.js +37 -7
- package/dist/engine/SelaEngine.d.ts +29 -0
- package/dist/engine/SelaEngine.d.ts.map +1 -0
- package/dist/engine/SelaEngine.js +682 -0
- package/dist/engine/singleton.d.ts +2 -2
- package/dist/engine/singleton.d.ts.map +1 -1
- package/dist/engine/singleton.js +2 -2
- package/dist/fixtures/expectProxy.d.ts +2 -2
- package/dist/fixtures/expectProxy.d.ts.map +1 -1
- package/dist/fixtures/expectProxy.js +2 -2
- package/dist/fixtures/index.d.ts.map +1 -1
- package/dist/fixtures/index.js +21 -15
- package/dist/fixtures/moduleExpect.d.ts.map +1 -1
- package/dist/fixtures/moduleExpect.js +13 -6
- package/dist/services/ASTSourceUpdater.d.ts.map +1 -1
- package/dist/services/ASTSourceUpdater.js +10 -1
- package/dist/services/HealReportService.d.ts +110 -0
- package/dist/services/HealReportService.d.ts.map +1 -0
- package/dist/services/HealReportService.js +1195 -0
- package/dist/services/PRAutomationService.d.ts +30 -0
- package/dist/services/PRAutomationService.d.ts.map +1 -0
- package/dist/services/PRAutomationService.js +464 -0
- package/dist/services/SnapshotService.d.ts.map +1 -1
- package/dist/services/SnapshotService.js +7 -8
- package/dist/services/SourceUpdater.d.ts.map +1 -1
- package/dist/services/SourceUpdater.js +13 -2
- package/dist/storage/SnapshotManager.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,682 @@
|
|
|
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: aiFix.confidence,
|
|
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
|
+
// Capture old source line BEFORE the AST mutation — needed for the diff.
|
|
290
|
+
const oldCodeLine = this._safeReadLine(filePath, line);
|
|
291
|
+
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);
|
|
292
|
+
// Capture new source line AFTER mutation, at the line the updater landed on.
|
|
293
|
+
const writtenLine = updateResult.lineUpdated ?? line;
|
|
294
|
+
const newCodeLine = this._safeReadLine(filePath, writtenLine);
|
|
295
|
+
const canonicalSelector = updateResult.healedLocatorString ?? newFullSelector;
|
|
296
|
+
console.log(`[Sela] ✅ Canonical selector (disk == runtime): "${canonicalSelector}"`);
|
|
297
|
+
// Buffer CLI-ready metadata — merged into DNA at commitUpdates() time.
|
|
298
|
+
// Sanitize filePath: strip any residual "at " stack-trace prefix + whitespace,
|
|
299
|
+
// then resolve to an absolute path before converting to project-relative form.
|
|
300
|
+
const match = filePath.match(/at\s+(.*)/i);
|
|
301
|
+
// 2. אם מצאנו התאמה ניקח אותה, אחרת ניקח את המחרוזת המקורית.
|
|
302
|
+
// בנוסף, ננקה רווחים וסוגריים (לפעמים נתיבים ב-Stack trace מוקפים בסוגריים).
|
|
303
|
+
const cleanFilePath = (match ? match[1] : filePath)
|
|
304
|
+
.replace(/^\(|\)$/g, "") // מסיר סוגריים אם קיימים ( )
|
|
305
|
+
.trim();
|
|
306
|
+
// 3. הפיכה לנתיב אבסולוטי ואז ליחסי (כמו שעשית, שזה מעולה)
|
|
307
|
+
const absFilePath = cleanFilePath && path.isAbsolute(cleanFilePath)
|
|
308
|
+
? cleanFilePath
|
|
309
|
+
: cleanFilePath
|
|
310
|
+
? path.resolve(process.cwd(), cleanFilePath)
|
|
311
|
+
: "";
|
|
312
|
+
const relativeSourceFile = absFilePath
|
|
313
|
+
? path.relative(process.cwd(), absFilePath)
|
|
314
|
+
: "";
|
|
315
|
+
this.healMetaBuffer.set(stableId, {
|
|
316
|
+
selector: canonicalSelector,
|
|
317
|
+
sourceFile: relativeSourceFile,
|
|
318
|
+
sourceLine: line,
|
|
319
|
+
originalSelector: elementSelectorOnly,
|
|
320
|
+
healedSelector: canonicalSelector,
|
|
321
|
+
lastHealed: new Date().toISOString(),
|
|
322
|
+
status: "healed",
|
|
323
|
+
definitionSite: updateResult.definitionSite,
|
|
324
|
+
blastRadius: updateResult.blastRadius,
|
|
325
|
+
});
|
|
326
|
+
// ── Report: HEALED event ───────────────────────────────────────
|
|
327
|
+
const healedEvent = {
|
|
328
|
+
kind: "HEALED",
|
|
329
|
+
stableId,
|
|
330
|
+
sourceFile: relativeSourceFile || reportFile,
|
|
331
|
+
sourceLine: line,
|
|
332
|
+
testTitle,
|
|
333
|
+
timestamp: new Date().toISOString(),
|
|
334
|
+
framePath: successfulPath,
|
|
335
|
+
inIframe: successfulPath.length > 0,
|
|
336
|
+
inShadowDom: liveSnapshot?.isInShadowDom ?? false,
|
|
337
|
+
oldSelector: elementSelectorOnly,
|
|
338
|
+
newSelector: canonicalSelector,
|
|
339
|
+
oldCodeLine,
|
|
340
|
+
newCodeLine,
|
|
341
|
+
newLineNumber: writtenLine,
|
|
342
|
+
aiConfidence: aiFix.confidence,
|
|
343
|
+
aiExplanation: aiFix.explanation ?? "",
|
|
344
|
+
aiAlternatives: this._buildAlternatives(aiFix.chainSegments, aiFix.segments),
|
|
345
|
+
reasoningSteps: this._buildReasoningSteps({
|
|
346
|
+
aiFix,
|
|
347
|
+
dnaText: lastKnownState?.text ?? null,
|
|
348
|
+
dnaTag: lastKnownState?.tagName ?? null,
|
|
349
|
+
safetyLevel: safetyDecision.level,
|
|
350
|
+
strategy: updateResult.reason || "ast-update",
|
|
351
|
+
writtenLine,
|
|
352
|
+
blastRadius: updateResult.blastRadius,
|
|
353
|
+
successfulPath,
|
|
354
|
+
}),
|
|
355
|
+
auditor: this._auditorBlockFromDecision(safetyDecision.meta),
|
|
356
|
+
dnaBefore: (0, HealReportService_1.summariseSnapshot)(lastKnownState),
|
|
357
|
+
dnaAfter: (0, HealReportService_1.summariseSnapshot)(liveSnapshot ?? null),
|
|
358
|
+
diffStrategy: updateResult.reason || "ast-update",
|
|
359
|
+
blastRadius: updateResult.blastRadius,
|
|
360
|
+
definitionSite: updateResult.definitionSite,
|
|
361
|
+
contentChange: aiFix.contentChange,
|
|
362
|
+
};
|
|
363
|
+
HealReportService_1.sharedHealReport.record(healedEvent);
|
|
364
|
+
return canonicalSelector;
|
|
365
|
+
}
|
|
366
|
+
// AI did not return FIXED — record as FAILED.
|
|
367
|
+
const failedEvent = {
|
|
368
|
+
kind: "FAILED",
|
|
369
|
+
stableId,
|
|
370
|
+
sourceFile: reportFile,
|
|
371
|
+
sourceLine: line,
|
|
372
|
+
testTitle,
|
|
373
|
+
timestamp: new Date().toISOString(),
|
|
374
|
+
framePath: [],
|
|
375
|
+
inIframe: false,
|
|
376
|
+
inShadowDom: lastKnownStateForReport?.isInShadowDom ?? false,
|
|
377
|
+
oldSelector: elementSelectorOnly,
|
|
378
|
+
reason: "AI could not provide a fix (NOT_FOUND)",
|
|
379
|
+
aiConfidence: aiFix?.confidence,
|
|
380
|
+
aiExplanation: aiFix?.explanation,
|
|
381
|
+
dnaBefore: (0, HealReportService_1.summariseSnapshot)(lastKnownStateForReport),
|
|
382
|
+
};
|
|
383
|
+
HealReportService_1.sharedHealReport.record(failedEvent);
|
|
384
|
+
throw new Error("AI could not provide a fix");
|
|
385
|
+
}
|
|
386
|
+
catch (error) {
|
|
387
|
+
console.error(`[Sela] ❌ Healing Failure:`, error.message);
|
|
388
|
+
// Avoid double-recording when a SafetyGuard PROTECTED event was already pushed.
|
|
389
|
+
const isSafetyBlock = typeof error?.message === "string" && error.message.includes("[SafetyGuard]");
|
|
390
|
+
const isNotFoundAlreadyRecorded = typeof error?.message === "string" && error.message === "AI could not provide a fix";
|
|
391
|
+
if (!isSafetyBlock && !isNotFoundAlreadyRecorded) {
|
|
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: error?.message ?? "Unknown heal failure",
|
|
404
|
+
aiConfidence: aiFixForReport?.confidence,
|
|
405
|
+
aiExplanation: aiFixForReport?.explanation,
|
|
406
|
+
dnaBefore: (0, HealReportService_1.summariseSnapshot)(lastKnownStateForReport),
|
|
407
|
+
};
|
|
408
|
+
HealReportService_1.sharedHealReport.record(failedEvent);
|
|
409
|
+
}
|
|
410
|
+
throw error;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
// ─────────────────────────────────────────────────────────────
|
|
414
|
+
// Report helpers
|
|
415
|
+
// ─────────────────────────────────────────────────────────────
|
|
416
|
+
_toReportFile(filePath) {
|
|
417
|
+
if (!filePath)
|
|
418
|
+
return "";
|
|
419
|
+
const match = filePath.match(/at\s+(.*)/i);
|
|
420
|
+
const cleaned = (match ? match[1] : filePath).replace(/^\(|\)$/g, "").trim();
|
|
421
|
+
const abs = path.isAbsolute(cleaned) ? cleaned : path.resolve(process.cwd(), cleaned);
|
|
422
|
+
return path.relative(process.cwd(), abs) || abs;
|
|
423
|
+
}
|
|
424
|
+
_safeReadLine(filePath, lineNumber) {
|
|
425
|
+
try {
|
|
426
|
+
const cleaned = this._toReportFile(filePath);
|
|
427
|
+
const abs = path.isAbsolute(cleaned) ? cleaned : path.resolve(process.cwd(), cleaned);
|
|
428
|
+
if (!fs.existsSync(abs))
|
|
429
|
+
return "";
|
|
430
|
+
const lines = fs.readFileSync(abs, "utf8").split(/\r?\n/);
|
|
431
|
+
const idx = Math.max(0, Math.min(lines.length - 1, lineNumber - 1));
|
|
432
|
+
return lines[idx] ?? "";
|
|
433
|
+
}
|
|
434
|
+
catch {
|
|
435
|
+
return "";
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
_auditorBlockFromDecision(meta) {
|
|
439
|
+
if (!meta || !meta.auditorVerdict)
|
|
440
|
+
return null;
|
|
441
|
+
return {
|
|
442
|
+
verdict: meta.auditorVerdict,
|
|
443
|
+
confidence: meta.auditorConfidence ?? 0,
|
|
444
|
+
reason: meta.auditScoreBreakdown
|
|
445
|
+
? `Adjusted ${meta.auditScoreBreakdown.rawConfidence}% → ${meta.auditScoreBreakdown.adjustedConfidence}% (penalty ${meta.auditScoreBreakdown.penalty}, bonus +${meta.auditScoreBreakdown.bonus}).`
|
|
446
|
+
: "Auditor verdict recorded by SafetyGuard.",
|
|
447
|
+
inversionType: null,
|
|
448
|
+
rawConfidence: meta.auditScoreBreakdown?.rawConfidence,
|
|
449
|
+
penalty: meta.auditScoreBreakdown?.penalty,
|
|
450
|
+
bonus: meta.auditScoreBreakdown?.bonus,
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
_buildAlternatives(chainSegments, segments) {
|
|
454
|
+
const out = [];
|
|
455
|
+
if (chainSegments && chainSegments.length) {
|
|
456
|
+
for (const seg of chainSegments) {
|
|
457
|
+
switch (seg.type) {
|
|
458
|
+
case "locator":
|
|
459
|
+
out.push(`locator(${JSON.stringify(seg.selector)})`);
|
|
460
|
+
break;
|
|
461
|
+
case "frameLocator":
|
|
462
|
+
out.push(`frameLocator(${JSON.stringify(seg.selector)})`);
|
|
463
|
+
break;
|
|
464
|
+
case "getByRole":
|
|
465
|
+
out.push(`getByRole(${JSON.stringify(seg.role)}${seg.name ? ", { name: " + JSON.stringify(seg.name) + (seg.exact ? ", exact: true" : "") + " }" : ""})`);
|
|
466
|
+
break;
|
|
467
|
+
case "getByLabel":
|
|
468
|
+
out.push(`getByLabel(${JSON.stringify(seg.text)}${seg.exact ? ", { exact: true }" : ""})`);
|
|
469
|
+
break;
|
|
470
|
+
case "getByText":
|
|
471
|
+
out.push(`getByText(${JSON.stringify(seg.text)}${seg.exact ? ", { exact: true }" : ""})`);
|
|
472
|
+
break;
|
|
473
|
+
case "getByPlaceholder":
|
|
474
|
+
out.push(`getByPlaceholder(${JSON.stringify(seg.text)})`);
|
|
475
|
+
break;
|
|
476
|
+
case "getByTestId":
|
|
477
|
+
out.push(`getByTestId(${JSON.stringify(seg.testId)})`);
|
|
478
|
+
break;
|
|
479
|
+
case "getByAltText":
|
|
480
|
+
out.push(`getByAltText(${JSON.stringify(seg.text)})`);
|
|
481
|
+
break;
|
|
482
|
+
case "getByTitle":
|
|
483
|
+
out.push(`getByTitle(${JSON.stringify(seg.text)})`);
|
|
484
|
+
break;
|
|
485
|
+
case "filter":
|
|
486
|
+
out.push(`filter(${JSON.stringify(seg)})`);
|
|
487
|
+
break;
|
|
488
|
+
case "first":
|
|
489
|
+
out.push("first()");
|
|
490
|
+
break;
|
|
491
|
+
case "last":
|
|
492
|
+
out.push("last()");
|
|
493
|
+
break;
|
|
494
|
+
case "nth":
|
|
495
|
+
out.push(`nth(${seg.index})`);
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
if (segments && segments.length) {
|
|
501
|
+
out.push(segments.map((s) => s.selector).join(" >> "));
|
|
502
|
+
}
|
|
503
|
+
return Array.from(new Set(out)).slice(0, 8);
|
|
504
|
+
}
|
|
505
|
+
_buildReasoningSteps(ctx) {
|
|
506
|
+
const steps = [];
|
|
507
|
+
if (ctx.dnaTag || ctx.dnaText) {
|
|
508
|
+
steps.push(`Recovered DNA baseline — previously a <${ctx.dnaTag ?? "?"}>` +
|
|
509
|
+
(ctx.dnaText ? ` with text "${ctx.dnaText}"` : "") + ".");
|
|
510
|
+
}
|
|
511
|
+
if (ctx.successfulPath.length > 0) {
|
|
512
|
+
steps.push(`Resolved DOM context inside ${ctx.successfulPath.length} nested frame(s): ${ctx.successfulPath.join(" >> ")}.`);
|
|
513
|
+
}
|
|
514
|
+
const firstSeg = ctx.aiFix.chainSegments?.[0];
|
|
515
|
+
if (firstSeg) {
|
|
516
|
+
steps.push(`AI preserved the ${firstSeg.type} pattern — semantic locator over raw CSS path.`);
|
|
517
|
+
}
|
|
518
|
+
if (ctx.aiFix.explanation) {
|
|
519
|
+
steps.push(`AI reasoning: ${ctx.aiFix.explanation}`);
|
|
520
|
+
}
|
|
521
|
+
steps.push(`Confidence: ${ctx.aiFix.confidence}%.`);
|
|
522
|
+
if (ctx.aiFix.contentChange) {
|
|
523
|
+
steps.push(`Detected content drift: "${ctx.aiFix.contentChange.oldText}" → "${ctx.aiFix.contentChange.newText}".`);
|
|
524
|
+
}
|
|
525
|
+
steps.push(`SafetyGuard verdict: ${ctx.safetyLevel} — change cleared for write.`);
|
|
526
|
+
steps.push(`Source updated via ${ctx.strategy} at line ${ctx.writtenLine}.`);
|
|
527
|
+
if (ctx.blastRadius != null && ctx.blastRadius > 0) {
|
|
528
|
+
steps.push(`Blast radius: ${ctx.blastRadius} dependent test reference(s) inherited the fix.`);
|
|
529
|
+
}
|
|
530
|
+
return steps;
|
|
531
|
+
}
|
|
532
|
+
preloadTestTitle(stableId, testTitle) {
|
|
533
|
+
this.testTitleBuffer.set(stableId, testTitle);
|
|
534
|
+
}
|
|
535
|
+
async saveSnapshot(stableId, data) {
|
|
536
|
+
this.dnaBuffer.set(stableId, data);
|
|
537
|
+
}
|
|
538
|
+
async commitUpdates() {
|
|
539
|
+
if (this.dnaBuffer.size > 0) {
|
|
540
|
+
for (const [key, dna] of this.dnaBuffer.entries()) {
|
|
541
|
+
const meta = this.healMetaBuffer.get(key);
|
|
542
|
+
const toSave = meta ? { ...dna, ...meta } : dna;
|
|
543
|
+
await this.snapshotService.save(key, toSave);
|
|
544
|
+
}
|
|
545
|
+
this.dnaBuffer.clear();
|
|
546
|
+
this.healMetaBuffer.clear();
|
|
547
|
+
this.testTitleBuffer.clear();
|
|
548
|
+
SourceUpdater_1.SourceUpdater.flushAdvisories();
|
|
549
|
+
}
|
|
550
|
+
// ── Sela Insights — render the transparent healing report ───────
|
|
551
|
+
let reportHtmlPath = null;
|
|
552
|
+
const healedEvents = HealReportService_1.sharedHealReport.getHealedEvents();
|
|
553
|
+
const protectedEvents = HealReportService_1.sharedHealReport.getProtectedEvents();
|
|
554
|
+
if (HealReportService_1.sharedHealReport.hasContent()) {
|
|
555
|
+
try {
|
|
556
|
+
reportHtmlPath = HealReportService_1.sharedHealReport.flushToDisk(process.cwd());
|
|
557
|
+
if (reportHtmlPath) {
|
|
558
|
+
console.log(`\n[Sela] 📄 Insights report written: ${path.relative(process.cwd(), reportHtmlPath)}`);
|
|
559
|
+
console.log(`[Sela] Open with: npx sela show-report`);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
catch (err) {
|
|
563
|
+
console.warn(`[Sela] ⚠️ Failed to write insights report: ${err.message}`);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
// ── PR Automation — batched git-flow at session end ─────────────
|
|
567
|
+
if (this.config.prAutomation.enabled) {
|
|
568
|
+
try {
|
|
569
|
+
const branchInfo = (0, PRAutomationService_1.detectBranch)(process.cwd());
|
|
570
|
+
const decision = (0, PRAutomationService_1.decideEffectiveStrategy)(this.config.prAutomation, branchInfo.branch, healedEvents);
|
|
571
|
+
if (decision.downgradeReason) {
|
|
572
|
+
console.warn(`[Sela PR] ⚠️ Strategy downgraded: '${decision.configured}' → '${decision.effective}' ` +
|
|
573
|
+
`(reason: ${decision.downgradeReason}, ` +
|
|
574
|
+
`minAI: ${decision.minAiConfidence}%, minAuditor: ${decision.minAuditorConfidence}%)`);
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
console.log(`[Sela PR] 🎯 Effective strategy: '${decision.effective}' on branch '${branchInfo.branch ?? "unknown"}'`);
|
|
578
|
+
}
|
|
579
|
+
const ctx = { cwd: process.cwd(), reportHtmlPath };
|
|
580
|
+
await (0, PRAutomationService_1.execute)(this.config.prAutomation, decision, healedEvents, branchInfo, ctx);
|
|
581
|
+
await (0, PRAutomationService_1.handleBugDetected)(this.config.prAutomation.onBugDetected, protectedEvents, branchInfo, ctx);
|
|
582
|
+
}
|
|
583
|
+
catch (err) {
|
|
584
|
+
console.warn(`[Sela PR] ⚠️ PR automation step failed: ${err.message}`);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
// Clear the report buffer last so any downstream consumers (e.g. test
|
|
588
|
+
// re-runs in the same process) start fresh on the next session.
|
|
589
|
+
if (HealReportService_1.sharedHealReport.hasContent()) {
|
|
590
|
+
HealReportService_1.sharedHealReport.clear();
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
// ─────────────────────────────────────────────────────────────
|
|
594
|
+
// fetchLiveText — frame-aware innerText fetch (zero-trust probe)
|
|
595
|
+
//
|
|
596
|
+
// Navigates the frame chain encoded in successfulPath, then calls
|
|
597
|
+
// .first().innerText() on the element-only selector. Uses a short
|
|
598
|
+
// 1.5 s timeout so it never adds meaningful latency. Returns null
|
|
599
|
+
// on ANY failure — the caller must treat null as "no data" and skip
|
|
600
|
+
// live verification rather than blocking the heal.
|
|
601
|
+
// ─────────────────────────────────────────────────────────────
|
|
602
|
+
async fetchLiveText(page, elementOnlySelector, framePath) {
|
|
603
|
+
try {
|
|
604
|
+
let ctx = page;
|
|
605
|
+
for (const frameSel of framePath) {
|
|
606
|
+
ctx = ctx.frameLocator(frameSel);
|
|
607
|
+
}
|
|
608
|
+
const text = await ctx
|
|
609
|
+
.locator(elementOnlySelector)
|
|
610
|
+
.first()
|
|
611
|
+
.innerText({ timeout: 1500 });
|
|
612
|
+
return text.trim().length > 0 ? text.trim() : null;
|
|
613
|
+
}
|
|
614
|
+
catch {
|
|
615
|
+
return null;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
_rebuildSelectorWithFrames(fullSelector, elementSelectorOnly, aiNewSelector) {
|
|
619
|
+
const fullParts = fullSelector.split(" >> ").map((s) => s.trim());
|
|
620
|
+
const elementParts = elementSelectorOnly.split(" >> ").map((s) => s.trim());
|
|
621
|
+
const aiParts = aiNewSelector.split(" >> ").map((s) => s.trim());
|
|
622
|
+
const frameParts = fullParts.slice(0, fullParts.length - elementParts.length);
|
|
623
|
+
if (frameParts.length === 0)
|
|
624
|
+
return aiNewSelector;
|
|
625
|
+
const aiHasFrameSegments = aiParts.some((p) => p.includes("-frame") ||
|
|
626
|
+
p === "iframe" ||
|
|
627
|
+
p === "frame" ||
|
|
628
|
+
p.includes("frame-"));
|
|
629
|
+
if (aiHasFrameSegments) {
|
|
630
|
+
console.log(`[Sela] 🔗 AI returned full frame path — using directly: ${aiNewSelector}`);
|
|
631
|
+
return aiNewSelector;
|
|
632
|
+
}
|
|
633
|
+
return [...frameParts, ...aiParts].join(" >> ");
|
|
634
|
+
}
|
|
635
|
+
getAllFiles(dirPath, arrayOfFiles = []) {
|
|
636
|
+
const files = fs.readdirSync(dirPath);
|
|
637
|
+
const ignoreList = [
|
|
638
|
+
"node_modules",
|
|
639
|
+
".git",
|
|
640
|
+
"fixwright-snapshots",
|
|
641
|
+
"dist",
|
|
642
|
+
"test-results",
|
|
643
|
+
"tests/reset-demo.ts",
|
|
644
|
+
];
|
|
645
|
+
files.forEach((file) => {
|
|
646
|
+
const fullPath = path.join(dirPath, file);
|
|
647
|
+
if (fs.statSync(fullPath).isDirectory()) {
|
|
648
|
+
if (!ignoreList.includes(file))
|
|
649
|
+
this.getAllFiles(fullPath, arrayOfFiles);
|
|
650
|
+
}
|
|
651
|
+
else if (file.endsWith(".ts") || file.endsWith(".js")) {
|
|
652
|
+
arrayOfFiles.push(fullPath);
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
return arrayOfFiles;
|
|
656
|
+
}
|
|
657
|
+
// ─────────────────────────────────────────────────────────────
|
|
658
|
+
// captureSuccessfulElement — capture and buffer a v2 DNA snapshot.
|
|
659
|
+
//
|
|
660
|
+
// Replaces the previous DOMUtils.getElementDNA() path with a single
|
|
661
|
+
// captureElement() call that collects all v2 metrics atomically.
|
|
662
|
+
// ─────────────────────────────────────────────────────────────
|
|
663
|
+
async captureSuccessfulElement(page, selector, stableId) {
|
|
664
|
+
try {
|
|
665
|
+
const snapshot = await this.snapshotService.captureElement(page, selector);
|
|
666
|
+
if (snapshot) {
|
|
667
|
+
const testTitle = this.testTitleBuffer.get(stableId);
|
|
668
|
+
const enriched = {
|
|
669
|
+
...snapshot,
|
|
670
|
+
selector,
|
|
671
|
+
...(testTitle ? { testTitle } : {}),
|
|
672
|
+
};
|
|
673
|
+
this.dnaBuffer.set(stableId, enriched);
|
|
674
|
+
console.log(`[Sela] 🧬 DNA v2 captured for: ${stableId}`);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
catch (error) {
|
|
678
|
+
console.debug(`[Sela] 🧬 DNA capture skipped: ${error.message}`);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
exports.SelaEngine = SelaEngine;
|