saffron-ai 0.5.7 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +1 -1
- package/dist-pkg/cli.js +74 -72
- package/package.json +1 -1
- package/skills/saffron/references/syntax.md +14 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,22 @@ All notable changes to Saffron, in one place: the `saffron-ai` runner (npm),
|
|
|
4
4
|
the VS Code extension (`ChathurangaJayasinghe.saffron-vscode`) and the
|
|
5
5
|
JetBrains plugin (`ai.saffron.jetbrains`). Dates are publish dates.
|
|
6
6
|
|
|
7
|
+
## 0.6.0
|
|
8
|
+
|
|
9
|
+
Released 2026-09-18.
|
|
10
|
+
|
|
11
|
+
Runner
|
|
12
|
+
|
|
13
|
+
- **Multi-tab scenarios.** Plain prose, no keyword: "switch back to the
|
|
14
|
+
first tab", "close the preview tab", "open a new tab at the admin page".
|
|
15
|
+
The agent's tab operations during recording are cached as `switchTab`,
|
|
16
|
+
`closeTab` and `newTab` and replay in the same order. New tabs opened by
|
|
17
|
+
clicks are still followed automatically, and an explicit switch back is
|
|
18
|
+
no longer undone by that follow. Replay gives a click a moment for its
|
|
19
|
+
popup to appear and waits for a tab before switching to it.
|
|
20
|
+
- When a pending proposal fails its zero-AI replay and the agent records
|
|
21
|
+
again, the run now says which step failed and why.
|
|
22
|
+
|
|
7
23
|
## 0.5.7
|
|
8
24
|
|
|
9
25
|
Released 2026-09-17.
|
package/README.md
CHANGED
|
@@ -115,7 +115,7 @@ cached replays *and* the agent start authenticated. Generate one with
|
|
|
115
115
|
The full execution-model diagram is on the site: https://saffron-ai.lovable.app/docs
|
|
116
116
|
|
|
117
117
|
- Caches store per-step action lists with semantic targets (`role` + accessible name, `nameRegex` for volatile labels, CSS fallbacks) and `<param>` placeholders, so one cache serves every Examples row of a Scenario Outline.
|
|
118
|
-
- Dynamic-content vocabulary for real-world apps: `captureText` stores displayed values (prices, counters) under a name; `expectDiffers` compares against captured values; `expectMatches`/`expectAttribute` assert patterns instead of literals; `{date±N}` templates keep date-picker locators valid across days; the replayer follows links that open new tabs; native dialogs, file uploads, drag-and-drop, and iframe-scoped elements replay on all three engines; network-aware steps wait on or assert backend API responses (`waitForResponse` / sacred `expectResponse`) with URL/method/status/body matchers, "wait for the order API to return 201" instead of a sleep.
|
|
118
|
+
- Dynamic-content vocabulary for real-world apps: `captureText` stores displayed values (prices, counters) under a name; `expectDiffers` compares against captured values; `expectMatches`/`expectAttribute` assert patterns instead of literals; `{date±N}` templates keep date-picker locators valid across days; the replayer follows links that open new tabs, and multi-tab scenarios are plain prose ("switch back to the first tab", "close the preview tab", "open a new tab at …") recorded as tab actions; native dialogs, file uploads, drag-and-drop, and iframe-scoped elements replay on all three engines; network-aware steps wait on or assert backend API responses (`waitForResponse` / sacred `expectResponse`) with URL/method/status/body matchers, "wait for the order API to return 201" instead of a sleep.
|
|
119
119
|
- The agent and the replayer share one browser over CDP, so healing continues from the exact page state where replay failed.
|
|
120
120
|
- Every recorded action carries a page fingerprint (hash of the normalized aria snapshot) for token-free drift detection.
|
|
121
121
|
|
package/dist-pkg/cli.js
CHANGED
|
@@ -1,45 +1,46 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import D from"node:fs";import E from"node:path";import{spawn as
|
|
3
|
-
`),n=new Map,r=new Map;return{source:t.map((o,i)=>{let a=o.match(
|
|
4
|
-
`),definitions:n,invocations:r}}function
|
|
5
|
-
`)}function
|
|
6
|
-
`}import{Server as Er}from"@modelcontextprotocol/sdk/server/index.js";import{StdioServerTransport as
|
|
7
|
-
`)){let r=n.trim();if(!r||r.startsWith("#"))continue;let s=r.indexOf("=");if(s<=0)continue;let o=r.slice(0,s).trim();if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(o))continue;let i=r.slice(s+1).trim();(i.startsWith('"')&&i.endsWith('"')||i.startsWith("'")&&i.endsWith("'"))&&(i=i.slice(1,-1)),process.env[o]===void 0&&(process.env[o]=i)}}function H(e){_t(e);let t=
|
|
8
|
-
`)}function fe(e){return JSON.parse(
|
|
9
|
-
`)[0]}));return{runId:t.toISOString(),startedAt:t.toISOString(),finishedAt:new Date().toISOString(),baseURL:r,green:s.filter(o=>o.status==="green").length,yellow:s.filter(o=>o.status==="yellow").length,red:s.filter(o=>o.status==="red").length,aiCalls:s.reduce((o,i)=>o+i.aiCalls,0),costUsd:s.reduce((o,i)=>o+i.costUsd,0),divergentSteps:n,scenarios:s}}function zt(e,t){let n=
|
|
10
|
-
`)}function Ie(e,t=50){let n=
|
|
11
|
-
`).filter(Boolean),s=[];for(let o of r.slice(-t))try{s.push(JSON.parse(o))}catch{}return s}var Xe=e=>{let t=e.green+e.yellow+e.red;return t===0?0:Math.round((e.green+e.yellow)/t*100)};function qt(e,t){let n=[...e,t],r=n.slice(-20),s=n.slice(-10),o=e.at(-1),i=o?{passRatePp:Xe(t)-Xe(o),costUsd:t.costUsd-o.costUsd,aiCalls:t.aiCalls-o.aiCalls,red:t.red-o.red}:void 0,a=new Map;for(let
|
|
12
|
-
`),
|
|
13
|
-
`)[0]}`:""}`)}if(e.tags.length&&console.log(`tags: ${e.tags.map(s=>`${s.tag} (${s.scenarios})`).join(", ")}`),console.log(e.proposals.length?`${e.proposals.length} proposal(s) pending review: ${e.proposals.map(s=>s.file).join(", ")}`:"no proposals pending review"),e.lastRun){let s=e.lastRun.totals;console.log(`last run ${e.lastRun.finishedAt}: ${s.green} passed \xB7 ${s.yellow} pending review \xB7 ${s.red} failed \xB7 $${s.costUsd.toFixed(2)}`)}let r=e.vocabulary;console.log(`vocabulary: ${r.steps} steps (${r.recorded} recorded, ${r.unrecorded} unrecorded), ${r.stepSets} step sets, ${r.divergent.length} divergent, ${r.duplicateWordings.length} duplicate wording group(s)`),console.log(`config: ${e.config.file??"(defaults, no saffron.config.json)"}`)}var
|
|
2
|
+
import D from"node:fs";import E from"node:path";import{spawn as Ds}from"node:child_process";import Ms from"node:readline";import{Command as Ns}from"commander";import lr from"node:fs";import dr from"node:path";import{AstBuilder as pr,GherkinClassicTokenMatcher as ur,Parser as fr}from"@cucumber/gherkin";import{IdGenerator as gr,StepKeywordType as Ge}from"@cucumber/messages";function ne(e,t){return e.replace(/<([^<>]+)>/g,(n,r)=>r in t?t[r]:n)}function U(e){if(!e||e.length===0)return{};let t={};if(e.every(n=>n.length===2)){for(let[n,r]of e)t[`table:${n}`]=r;return t}if(e.length>=2){let[n,...r]=e;if(n.some(s=>!s))return{};r.forEach((s,o)=>{n.forEach((i,a)=>{s[a]!==void 0&&(t[`table:${o+1}:${i}`]=s[a])})})}return t}function re(e){return e!==void 0?{docstring:e}:{}}function le(e){let t=new Map;for(let r of e){let s={...U(r.resolvedTable??r.table),...re(r.resolvedDocString??r.docString)};for(let[o,i]of Object.entries(s)){let a=t.get(o);a||t.set(o,a=new Set),a.add(i)}}let n={};for(let[r,s]of t)s.size===1&&(n[r]=[...s][0]);return n}import He from"node:fs";import bt from"node:path";import{AstBuilder as tr,GherkinClassicTokenMatcher as nr,Parser as rr}from"@cucumber/gherkin";import{IdGenerator as sr}from"@cucumber/messages";var N=class extends Error{},or=/^(\s*)StepSet:\s*(.+?)\s*$/,ir=/^(\s*)StepSet\s+([^\s:].*?)\s*$/;function We(e){let t=e.split(`
|
|
3
|
+
`),n=new Map,r=new Map;return{source:t.map((o,i)=>{let a=o.match(or);if(a)return n.set(i+1,a[2]),`${a[1]}Scenario: ${a[2]}`;let c=o.match(ir);return c?(r.set(i+1,c[2]),`${c[1]}* StepSet ${c[2]}`):o}).join(`
|
|
4
|
+
`),definitions:n,invocations:r}}function ar(e){return new rr(new tr(sr.uuid()),new nr).parse(e)}function vt(e,t){for(let n of e)n.scenario?t(n.scenario):"rule"in n&&n.rule&&vt(n.rule.children,t)}function Z(e,t){let n=new Map;for(let r of e){let s=bt.relative(t,r),o=We(He.readFileSync(r,"utf8"));if(o.definitions.size===0)continue;let i=ar(o.source);i.feature&&vt(i.feature.children,a=>{let c=o.definitions.get(a.location.line);if(c===void 0)return;let d=n.get(c);if(d)throw new N(`StepSet "${c}" is defined twice: ${d.file}:${d.line} and ${s}:${a.location.line}. Set names are unique across the project. If line ${a.location.line} was meant to INVOKE the set inside a scenario, remove the colon: "StepSet ${c}".`);if(a.examples.length>0)throw new N(`StepSet "${c}" (${s}:${a.location.line}) has an Examples table \u2014 step sets take their values from the invoking scenario.`);for(let m of a.steps)if(o.invocations.has(m.location.line))throw new N(`StepSet "${c}" (${s}:${a.location.line}) invokes another step set at line ${m.location.line} \u2014 step sets cannot be nested.`);n.set(c,{name:c,file:s,line:a.location.line,steps:[...a.steps]})})}return n}var cr=[".feature",".saffron"];function se(e){return cr.some(t=>e.endsWith(t))}function de(e){if(!He.existsSync(e))return[];let t=[];for(let n of He.readdirSync(e,{recursive:!0})){let r=String(n);r.endsWith(".saffron")&&t.push(bt.join(e,r))}return t.sort()}function wt(e,t,n){return e.map(({step:r,source:s})=>{let o;switch(r.keywordType){case Ge.OUTCOME:o="assertion";break;case Ge.CONTEXT:case Ge.ACTION:o="action";break;default:o=n.value}n.value=o;let i=r.dataTable?r.dataTable.rows.map(c=>c.cells.map(d=>d.value)):void 0,a=r.docString?.content;return{keyword:r.keyword.trim(),kind:o,text:r.text,resolvedText:ne(r.text,t),table:i,resolvedTable:i?.map(c=>c.map(d=>ne(d,t))),docString:a,resolvedDocString:a!==void 0?ne(a,t):void 0,source:s}})}function kt(e,t,n,r,s){let o=[];for(let i of e){let a=r.get(i.location.line);if(a===void 0){o.push({step:i,source:{file:t,line:i.location.line,fromBackground:n}});continue}let c=s?.get(a);if(!c)throw new N(`${t}:${i.location.line} invokes StepSet "${a}", which is not defined anywhere in the project.`);for(let d of c.steps)o.push({step:d,source:{file:c.file,line:d.location.line,fromStepSet:c.name,fromBackground:n}})}return o}function mr(e,t,n,r,s){let o=e.tags.map(a=>a.name),i=[];if(e.examples.length===0)i.push({params:{},label:""});else{let a=0;for(let c of e.examples){let d=c.tableHeader?.cells.map(m=>m.value)??[];for(let m of c.tableBody){a+=1;let p={};m.cells.forEach((l,h)=>{p[d[h]]=l.value}),i.push({params:p,label:` (example ${a})`})}}}return i.map(({params:a,label:c})=>{let d={value:"action"};return{featurePath:t,featureName:n,name:e.name,displayName:`${e.name}${c}`,tags:o,steps:[...wt(r,a,d),...wt(s,a,d)],params:a}})}function ee(e,t,n){let r=lr.readFileSync(e,"utf8"),o=e.endsWith(".saffron")?We(r):{source:r,definitions:new Map,invocations:new Map},a=new fr(new pr(gr.uuid()),new ur).parse(o.source);if(!a.feature)return[];let c=dr.relative(t,e),d=a.feature.name,m=[],p=(l,h)=>{let y=h;for(let u of l)if(u.background)y=[...h,...kt(u.background.steps,c,!0,o.invocations,n)];else if(u.scenario){if(o.definitions.has(u.scenario.location.line))continue;m.push(...mr(u.scenario,c,d,y,kt(u.scenario.steps,c,!1,o.invocations,n)))}else"rule"in u&&u.rule&&p(u.rule.children,y)};return p(a.feature.children,[]),m}import Ct from"node:fs";import Je from"node:path";import Rt from"node:fs";import Tt from"node:path";import Re from"node:fs";import he from"node:path";var $t=".saffron";function Te(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"unnamed"}function V(e,t,n){let r=Te(he.basename(t,".feature"));return he.join(e,$t,"cache",r,`${Te(n)}.json`)}function ye(e,t,n){let r=Te(he.basename(t,".feature"));return he.join(e,$t,"proposals",r,`${Te(n)}.json`)}function z(e){if(!Re.existsSync(e))return;let t=JSON.parse(Re.readFileSync(e,"utf8"));if(t.version!==1)throw new Error(`Unsupported cache version ${t.version} in ${e}`);return t}function pe(e,t){Re.mkdirSync(he.dirname(e),{recursive:!0}),Re.writeFileSync(e,`${JSON.stringify(t,null,2)}
|
|
5
|
+
`)}function Ae(e){let t=[];return{pattern:e.replace(/"((?:[^"\\]|\\.)*)"/g,(r,s)=>(t.push(s),`"\xAB${t.length-1}\xBB"`)),args:t}}function hr(e,t){let n=r=>JSON.stringify(r.map(({pageFingerprint:s,...o})=>o));return n(e)===n(t)}function yr(e){let t=new Set,n=r=>{if(r)for(let s of r.matchAll(/<([^<>]+)>/g))t.add(s[1])};for(let r of e.actions)n(r.value),n(r.url),n(r.pattern),n(r.target?.name),n(r.target?.nameRegex),n(r.target?.label),n(r.target?.text),n(r.target?.placeholder);return n(e.gherkin),t}function G(e){let t=Tt.join(e,".saffron","cache"),n=new Map,r=new Map,s=0;if(Rt.existsSync(t))for(let a of Rt.readdirSync(t,{recursive:!0})){let c=String(a);if(!c.endsWith(".json"))continue;let d=z(Tt.join(t,c));if(d){s++;for(let m of d.steps){let p=m.gherkin,l=JSON.stringify(m.actions.map(({pageFingerprint:u,...g})=>g)),h=r.get(p)??[];h.includes(l)||(h.push(l),r.set(p,h));let y=n.get(p);!y||d.recordedAt>y.recordedAt?n.set(p,{gherkin:m.gherkin,table:m.table,docString:m.docString,actions:m.actions,feature:d.feature,scenario:d.scenario,recordedAt:d.recordedAt,variants:h.length}):y.variants=h.length}}}let o=[...r.entries()].filter(([,a])=>a.length>1).map(([a])=>a),i=new Map;for(let a of n.values()){let{pattern:c,args:d}=Ae(a.gherkin);if(d.length===0)continue;let m=i.get(c);(!m||a.recordedAt>m.recordedAt)&&i.set(c,{...a,args:d})}return{steps:n,patterns:i,divergent:o,scannedCaches:s}}function At(e,t,n){if(!!e.table!=!!t.table)return!1;if(e.table&&t.table){let o=Object.keys(U(e.table)),i=Object.keys(U(t.table));if(o.length>0&&i.length>0){if(JSON.stringify(o)!==JSON.stringify(i))return!1}else if(JSON.stringify(e.table)!==JSON.stringify(t.table))return!1}if(e.docString!==void 0!=(t.docString!==void 0)||e.docString!==void 0&&t.docString!==void 0&&e.docString!==t.docString&&!e.actions.some(o=>[o.value,o.url,o.pattern].some(i=>i?.includes("<docstring>"))))return!1;let r={...n,...U(t.resolvedTable??t.table),...re(t.resolvedDocString??t.docString)},s=yr({gherkin:e.gherkin,keyword:"",kind:"action",actions:e.actions});for(let o of s)if(!(o in r))return!1;return!0}function Et(e,t,n){let r=e.steps.get(t.text);if(r)return At(r,t,n)?r:void 0;let{pattern:s,args:o}=Ae(t.text);if(o.length===0)return;let i=e.patterns.get(s);if(!i)return;let a=new Map;for(let p=0;p<o.length;p++){let l=i.args[p],h=o[p];if(l===h)continue;let y=a.get(l);if(y!==void 0&&y!==h)return;a.set(l,h)}let c=p=>[p?.name,p?.nameRegex,p?.label,p?.text,p?.placeholder];for(let p of a.keys())if(!i.actions.some(h=>[h.value,h.url,h.pattern,...c(h.target)].includes(p)))return;let d=p=>p!==void 0&&a.has(p)?a.get(p):p,m={...i,gherkin:t.text,actions:i.actions.map(({pageFingerprint:p,...l})=>({...l,value:d(l.value),url:d(l.url),pattern:d(l.pattern),target:l.target?{...l.target,name:d(l.target.name),nameRegex:d(l.target.nameRegex),label:d(l.target.label),text:d(l.target.text),placeholder:d(l.target.placeholder)}:void 0}))};return At(m,t,n)?m:void 0}function Pt(e,t){let n=[];return e.forEach((r,s)=>{if(r.actions.length!==0&&!t.steps.has(r.gherkin)){for(let o of t.steps.values())if(!(o.variants>1)&&o.actions.length!==0&&hr(r.actions,o.actions)){n.push({index:s,from:r.gherkin,to:o.gherkin,feature:o.feature,scenario:o.scenario});break}}}),n}function Sr(e){if(!Ct.existsSync(e))return[];let t=[];for(let n of Ct.readdirSync(e,{recursive:!0})){let r=String(n);se(r)&&t.push(Je.join(e,r))}return t.sort()}function q(e,t,n){let r=Je.isAbsolute(t)?t:Je.join(e,t),s=Sr(r),o=n??Z(s.filter(l=>l.endsWith(".saffron")),e),i=new Map,a=new Map;for(let l of s)for(let h of ee(l,e,o)){let y=`${h.featurePath}::${h.name}`;for(let u of h.steps){let g=i.get(u.text);if(g||(g={text:u.text,keywords:new Map,kind:u.kind,scenarios:new Set,files:new Set,fromStepSet:u.source?.fromStepSet},i.set(u.text,g)),g.keywords.set(u.keyword,(g.keywords.get(u.keyword)??0)+1),g.scenarios.add(y),u.source&&g.files.add(u.source.file),u.source?.fromStepSet){let b=a.get(u.source.fromStepSet);b||a.set(u.source.fromStepSet,b=new Set),b.add(y)}}}let c=G(e),d=new Set(c.divergent),m=[...i.values()].map(l=>({text:l.text,keyword:[...l.keywords.entries()].sort((h,y)=>y[1]-h[1])[0][0],kind:l.kind,status:d.has(l.text)?"divergent":c.steps.has(l.text)?"recorded":"unrecorded",usage:l.scenarios.size,files:[...l.files].sort(),fromStepSet:l.fromStepSet})).sort((l,h)=>h.usage-l.usage||l.text.localeCompare(h.text)),p=[...o.values()].map(l=>({name:l.name,file:l.file,line:l.line,steps:l.steps.map(h=>h.text),usage:a.get(l.name)?.size??0})).sort((l,h)=>h.usage-l.usage||l.name.localeCompare(h.name));return{steps:m,stepSets:p,scannedFiles:s.length,scannedCaches:c.scannedCaches}}function Ee(e,t){let n=t.toLowerCase();return{...e,steps:e.steps.filter(r=>r.text.toLowerCase().includes(n)),stepSets:e.stepSets.filter(r=>r.name.toLowerCase().includes(n)||r.steps.some(s=>s.toLowerCase().includes(n)))}}function Ft(e){let t={},n="saffron,feature,gherkin,cucumber";for(let r of e.steps)t[`step: ${r.text}`]={scope:n,prefix:r.text,body:[r.text.replace(/\$/g,"\\$")],description:`${r.status} \xB7 used in ${r.usage} scenario(s)`};for(let r of e.stepSets)t[`StepSet: ${r.name}`]={scope:n,prefix:`StepSet ${r.name}`,body:[`StepSet ${r.name}`],description:`step set (${r.steps.length} steps) \u2014 ${r.file}`};return JSON.stringify(t,null,2)+`
|
|
6
|
+
`}import{Server as Er}from"@modelcontextprotocol/sdk/server/index.js";import{StdioServerTransport as Pr}from"@modelcontextprotocol/sdk/server/stdio.js";import{CallToolRequestSchema as Cr,ListToolsRequestSchema as Fr}from"@modelcontextprotocol/sdk/types.js";import Y from"node:fs";import K from"node:path";import Ot from"node:fs";import br from"node:path";import jt from"node:fs";import xr from"node:path";var It=/\{env:([A-Za-z_][A-Za-z0-9_]*)\}/g;function Se(e){return e.replace(It,(t,n)=>{let r=process.env[n];if(r===void 0)throw new Error(`environment variable ${n} is not set (referenced as {env:${n}}) \u2014 export it or add it to .env`);return r})}function Ye(e){let t=new Set;for(let n of e)if(n)for(let r of n.matchAll(It))t.add(r[1]);return t}function Ke(e){let t=[];for(let n of e.steps){t.push(n.text,n.resolvedText),n.docString&&t.push(n.docString);for(let r of n.table??[])t.push(...r)}return t}function xe(e){let t=new Map;for(let n of Ye(Ke(e))){let r=process.env[n];r!==void 0&&t.set(n,r)}return t}function ue(e,t){let n=e;for(let[r,s]of[...t.entries()].sort((o,i)=>i[1].length-o[1].length))s.length<4||(n=n.split(s).join(`{env:${r}}`));return n}function _t(e){let t=xr.join(e,".env");if(jt.existsSync(t))for(let n of jt.readFileSync(t,"utf8").split(`
|
|
7
|
+
`)){let r=n.trim();if(!r||r.startsWith("#"))continue;let s=r.indexOf("=");if(s<=0)continue;let o=r.slice(0,s).trim();if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(o))continue;let i=r.slice(s+1).trim();(i.startsWith('"')&&i.endsWith('"')||i.startsWith("'")&&i.endsWith("'"))&&(i=i.slice(1,-1)),process.env[o]===void 0&&(process.env[o]=i)}}function H(e){_t(e);let t=br.join(e,"saffron.config.json");return Ot.existsSync(t)?JSON.parse(Ot.readFileSync(t,"utf8")):{}}var vr={features:"features",actionTimeoutMs:5e3,retries:1,maxTurns:100,strict:!1,verifyProposals:!0,snapshotMode:"none",reuseSteps:!0,browser:"chromium",workers:1,assertionPolicy:"strict"};function Dt(e){return{...vr,...Object.fromEntries(Object.entries(e).filter(([,t])=>t!==void 0))}}import Qe from"node:fs";import Pe from"node:path";import{fileURLToPath as wr}from"node:url";function Ce(){let e=Pe.dirname(wr(import.meta.url));for(let t=0;t<6;t++){let n=Pe.join(e,"package.json");if(Qe.existsSync(n))try{if(JSON.parse(Qe.readFileSync(n,"utf8")).name==="saffron-ai")return e}catch{}e=Pe.dirname(e)}}function te(){let e=Ce();if(!e)return"unknown";try{return JSON.parse(Qe.readFileSync(Pe.join(e,"package.json"),"utf8")).version??"unknown"}catch{return"unknown"}}import J from"node:fs";import Fe from"node:path";function Mt(e,t){J.mkdirSync(Fe.dirname(e),{recursive:!0}),J.writeFileSync(e,`${JSON.stringify(t,null,2)}
|
|
8
|
+
`)}function fe(e){return JSON.parse(J.readFileSync(e,"utf8"))}function oe(e){let t=Fe.join(e,".saffron","proposals");if(!J.existsSync(t))return[];let n=[];for(let r of J.readdirSync(t)){let s=Fe.join(t,r);if(J.statSync(s).isDirectory())for(let o of J.readdirSync(s)){if(!o.endsWith(".json"))continue;let i=Fe.join(s,o);n.push({file:i,proposal:fe(i)})}}return n}function Nt(e,t){let n=fe(t),r=V(e,n.cache.feature,n.cache.scenario);return pe(r,n.cache),J.rmSync(t),r}function Ut(e){J.rmSync(e)}import je from"node:fs";import Lt from"node:path";function Bt(e){return Lt.join(e,".saffron","history.jsonl")}function Vt(e,t,n,r){let s=e.map(o=>({feature:o.scenario.featurePath,scenario:o.scenario.displayName,tags:[...o.scenario.tags],status:o.status,source:o.source,durationMs:o.durationMs,verified:o.verified,seededSteps:o.seededSteps,adaptations:o.adaptations.length,driftSteps:o.stepResults.filter(i=>i.drift).length,aiCalls:o.usage.aiCalls,tokensInOut:o.usage.inputTokens+o.usage.outputTokens,cacheRead:o.usage.cacheReadTokens,cacheWrite:o.usage.cacheCreationTokens,costUsd:o.usage.costUsd??0,error:o.error?.split(`
|
|
9
|
+
`)[0]}));return{runId:t.toISOString(),startedAt:t.toISOString(),finishedAt:new Date().toISOString(),baseURL:r,green:s.filter(o=>o.status==="green").length,yellow:s.filter(o=>o.status==="yellow").length,red:s.filter(o=>o.status==="red").length,aiCalls:s.reduce((o,i)=>o+i.aiCalls,0),costUsd:s.reduce((o,i)=>o+i.costUsd,0),divergentSteps:n,scenarios:s}}function zt(e,t){let n=Bt(e);je.mkdirSync(Lt.dirname(n),{recursive:!0}),je.appendFileSync(n,`${JSON.stringify(t)}
|
|
10
|
+
`)}function Ie(e,t=50){let n=Bt(e);if(!je.existsSync(n))return[];let r=je.readFileSync(n,"utf8").trim().split(`
|
|
11
|
+
`).filter(Boolean),s=[];for(let o of r.slice(-t))try{s.push(JSON.parse(o))}catch{}return s}var Xe=e=>{let t=e.green+e.yellow+e.red;return t===0?0:Math.round((e.green+e.yellow)/t*100)};function qt(e,t){let n=[...e,t],r=n.slice(-20),s=n.slice(-10),o=e.at(-1),i=o?{passRatePp:Xe(t)-Xe(o),costUsd:t.costUsd-o.costUsd,aiCalls:t.aiCalls-o.aiCalls,red:t.red-o.red}:void 0,a=new Map;for(let p of s)for(let l of p.scenarios){let h=`${l.feature}::${l.scenario}`,y=a.get(h)??{feature:l.feature,scenario:l.scenario,heals:0,reds:0,runs:0};y.runs++,l.source==="agent-heal"&&l.status==="yellow"&&y.heals++,l.status==="red"&&y.reds++,a.set(h,y)}let c=[...a.values()].filter(p=>p.heals>=2||p.reds>=3).sort((p,l)=>l.heals+l.reds-(p.heals+p.reds)),d=new Map;for(let p of r)for(let l of p.scenarios)l.status==="red"&&l.error&&d.set(l.error,(d.get(l.error)??0)+1);let m=[...d.entries()].sort((p,l)=>l[1]-p[1]).slice(0,3).map(([p,l])=>({message:p,count:l}));return{prev:i,series:{passRate:r.map(Xe),costUsd:r.map(p=>Number(p.costUsd.toFixed(4))),aiCalls:r.map(p=>p.aiCalls)},chronic:c,divergentSteps:t.divergentSteps,topIssues:m}}var kr=/^\s*(?:Scenario(?: Outline| Template)?|Example):\s*(.+?)\s*$/,$r=/^\s*StepSet:/;function Ze(e,t){return K.relative(e,t).split(K.sep).join("/")}function Rr(e,t){let n={green:0,yellow:1,red:2};return!e||n[t]>n[e]?t:e}function Ht(e,t){return`${e} :: ${t}`}function _e(e,t,n={}){let r=t.features??"features",s=K.isAbsolute(r)?r:K.join(e,r),o=K.join(e,".saffron","reports","latest.json"),i=K.join(e,".saffron","reports","latest.html"),a;try{a=JSON.parse(Y.readFileSync(o,"utf8"))}catch{a=void 0}let c=new Map;for(let f of a?.scenarios??[]){let w=f.scenario.replace(/ \(example \d+\)$/,""),v=Ht(f.feature,w);c.set(v,Rr(c.get(v),f.status))}let d=new Map,m;try{d=Z(de(s),e)}catch(f){m=f instanceof Error?f.message:String(f)}let p=Y.existsSync(s)?[...Y.readdirSync(s,{recursive:!0})].map(String).filter(se).map(f=>K.join(s,f)).sort():[],l=[],h=new Map;for(let f of p){let w=Ze(e,f),v="";try{v=Y.readFileSync(f,"utf8")}catch(P){l.push({path:w,name:w,scenarios:[],stepSets:0,error:String(P)});continue}let $=v.split(`
|
|
12
|
+
`),C=new Map,S=0;$.forEach((P,X)=>{let B=P.match(kr);B&&!C.has(B[1])?C.set(B[1],X+1):$r.test(P)&&S++});let k=$.find(P=>/^\s*Feature:/.test(P)),R=k?k.replace(/^\s*Feature:\s*/,"").trim():w,F;try{F=ee(f,e,d)}catch(P){l.push({path:w,name:R,scenarios:[],stepSets:S,error:m??(P instanceof Error?P.message:String(P))});continue}let O=new Map;for(let P of F){let X=O.get(P.name);if(X){X.rows+=1;continue}O.set(P.name,{name:P.name,line:C.get(P.name),tags:[...P.tags],outline:Object.keys(P.params).length>0,rows:1,cached:Y.existsSync(V(e,P.featurePath,P.name)),proposal:Y.existsSync(ye(e,P.featurePath,P.name)),lastStatus:c.get(Ht(P.featurePath,P.name))});for(let B of P.tags)h.set(B,(h.get(B)??0)+1)}l.push({path:w,name:R,scenarios:[...O.values()],stepSets:S})}let y=oe(e).map(({file:f,proposal:w})=>({file:Ze(e,f),feature:w.cache.feature,scenario:w.cache.scenario,mode:w.meta.mode,createdAt:w.meta.createdAt,verified:w.meta.verified,proofError:w.meta.proofError,adaptations:w.meta.adaptations??[],narrative:w.meta.narrative,suggestedFeatureEdit:w.meta.suggestedFeatureEdit,recordedFor:w.meta.recordedFor,aiCalls:w.meta.usage?.aiCalls??0,costUsd:w.meta.usage?.costUsd})).sort((f,w)=>f.file.localeCompare(w.file)),u;try{u=q(e,r,d)}catch{u={steps:[],stepSets:[],scannedFiles:0,scannedCaches:0}}let g=Ar(e),b=Ie(e,n.historyLimit??30).map(f=>({startedAt:f.startedAt,green:f.green,yellow:f.yellow,red:f.red,aiCalls:f.aiCalls,costUsd:f.costUsd})),x=K.join(e,"saffron.config.json");return{tool:"saffron",version:te(),projectRoot:e,packageInstalled:Y.existsSync(K.join(e,"node_modules","saffron-ai")),config:{file:Y.existsSync(x)?"saffron.config.json":void 0,effective:Dt(t)},features:l,tags:[...h.entries()].map(([f,w])=>({tag:f,scenarios:w})).sort((f,w)=>f.tag.localeCompare(w.tag)),proposals:y,lastRun:a?{startedAt:a.startedAt,finishedAt:a.finishedAt,totals:a.totals,reportHtml:Y.existsSync(i)?Ze(e,i):void 0}:void 0,history:b,vocabulary:{steps:u.steps.length,recorded:u.steps.filter(f=>f.status==="recorded").length,unrecorded:u.steps.filter(f=>f.status==="unrecorded").length,stepSets:u.stepSets.length,divergent:u.steps.filter(f=>f.status==="divergent").map(f=>f.text),duplicateWordings:g.slice(0,50)}}}function Gt(e){let t=e.features.reduce((s,o)=>s+o.scenarios.length,0),n=e.features.reduce((s,o)=>s+o.scenarios.filter(i=>i.cached).length,0);console.log(`saffron ${e.version} \xB7 ${e.features.length} feature file(s) \xB7 ${t} scenario(s), ${n} cached`);for(let s of e.features){let o=s.scenarios.map(i=>i.proposal?"\u25D0":i.cached?"\u25CF":"\u25CB").join("");console.log(` ${s.path} ${o}${s.error?` \u2717 ${s.error.split(`
|
|
13
|
+
`)[0]}`:""}`)}if(e.tags.length&&console.log(`tags: ${e.tags.map(s=>`${s.tag} (${s.scenarios})`).join(", ")}`),console.log(e.proposals.length?`${e.proposals.length} proposal(s) pending review: ${e.proposals.map(s=>s.file).join(", ")}`:"no proposals pending review"),e.lastRun){let s=e.lastRun.totals;console.log(`last run ${e.lastRun.finishedAt}: ${s.green} passed \xB7 ${s.yellow} pending review \xB7 ${s.red} failed \xB7 $${s.costUsd.toFixed(2)}`)}let r=e.vocabulary;console.log(`vocabulary: ${r.steps} steps (${r.recorded} recorded, ${r.unrecorded} unrecorded), ${r.stepSets} step sets, ${r.divergent.length} divergent, ${r.duplicateWordings.length} duplicate wording group(s)`),console.log(`config: ${e.config.file??"(defaults, no saffron.config.json)"}`)}var Tr=/^(<[^>]+>|\{env:[^}]+\}|\{date[^}]*\})$/;function Wt(e){return e?e.role?`${e.role}${e.name?` "${e.name}"`:e.nameRegex?` /${e.nameRegex}/`:""}`:e.label?`label "${e.label}"`:e.text?`text "${e.text}"`:e.testId?`testid ${e.testId}`:e.placeholder?`placeholder "${e.placeholder}"`:e.fallbackSelectors?.length?e.fallbackSelectors[0]:"":""}function Ar(e){let t;try{t=G(e)}catch{return[]}let n=new Map;for(let r of t.steps.values()){if(r.actions.length===0)continue;let s=Ae(r.gherkin).pattern,o=[...r.gherkin.matchAll(/"([^"]*)"/g)].map(p=>p[1]),i=p=>p!==void 0&&(o.includes(p)||Tr.test(p))?"\xABarg\xBB":p,a=r.actions.map(p=>{let{pageFingerprint:l,value:h,promptText:y,...u}=p;return{...u,value:i(h),promptText:i(y)}}),c=JSON.stringify(a),d=r.actions.map(p=>`${p.action}${Wt(p.target)?` ${Wt(p.target)}`:p.url?` ${p.url}`:""}`).join(" \xB7 "),m=n.get(c)??{steps:new Map,actions:d};m.steps.has(s)||m.steps.set(s,r.gherkin),n.set(c,m)}return[...n.values()].filter(r=>r.steps.size>1).map(r=>({steps:[...r.steps.values()].sort(),actions:r.actions})).sort((r,s)=>s.steps.length-r.steps.length||r.steps[0].localeCompare(s.steps[0]))}async function Jt(e,t){let n=new Er({name:"saffron",version:te()},{capabilities:{tools:{}}});n.setRequestHandler(Fr,async()=>({tools:[{name:"search_steps",description:"Search the project's step vocabulary. Returns known Gherkin steps with status (recorded = replays at zero tokens, divergent, unrecorded), usage counts, and source files. ALWAYS reuse an existing wording exactly instead of inventing a near-duplicate: exact step text is the replay-cache identity, so a new wording costs a fresh AI recording.",inputSchema:{type:"object",properties:{query:{type:"string",description:"Case-insensitive substring filter over step text; omit to list everything."}}}},{name:"list_step_sets",description:"List the project's StepSet definitions (named reusable step sequences from .saffron files) with their steps, defining file, and usage. Invoke one inside a scenario with a line reading exactly: StepSet <name>",inputSchema:{type:"object",properties:{}}},{name:"project_status",description:"The project overview (same data as `saffron status --json`): every feature file with its scenarios (header line, tags, outline rows, cached / proposal pending / last run status), the tag list, pending proposals with narrative and proof-replay verdict, the last run's totals, run history, vocabulary health (divergent steps, duplicate wordings proven by identical recordings) and the effective config. Call it before editing tests to see what is already recorded and what awaits review.",inputSchema:{type:"object",properties:{}}}]})),n.setRequestHandler(Cr,async r=>jr(r.params.name,r.params.arguments,e,t)),await n.connect(new Pr)}function jr(e,t,n,r){let s=o=>({content:[{type:"text",text:JSON.stringify(o,null,2)}]});if(e==="search_steps"){let o=q(n,r),i=typeof t?.query=="string"?t.query:void 0,a=i?Ee(o,i):o;return s({steps:a.steps,scannedFiles:a.scannedFiles,scannedCaches:a.scannedCaches})}if(e==="list_step_sets"){let o=q(n,r);return s({stepSets:o.stepSets})}return e==="project_status"?s(_e(n,H(n))):{content:[{type:"text",text:`Unknown tool: ${e}`}],isError:!0}}import{query as Ir}from"@anthropic-ai/claude-agent-sdk";function _r(e,t){let n=t.steps.map(s=>`- [${s.status}] ${s.keyword} ${s.text}`).join(`
|
|
14
14
|
`),r=t.stepSets.map(s=>`- StepSet ${s.name}
|
|
15
15
|
${s.steps.map(o=>` ${o}`).join(`
|
|
16
16
|
`)}`).join(`
|
|
17
17
|
`);return["Draft a Saffron feature file from the requirements below.","","EXISTING STEP VOCABULARY \u2014 reuse these wordings EXACTLY (character for character) wherever they express what a step needs to do. Steps marked [recorded] or [divergent] already have replay recordings: reusing them makes the new file replay almost for free. Only invent a new wording when nothing in the vocabulary expresses the intent, and then follow the same style:",n.length>0?n:"(none yet \u2014 this is the project's first file)","",t.stepSets.length>0?`EXISTING STEP SETS \u2014 invoke with a line reading exactly \`StepSet <name>\` instead of repeating their steps:
|
|
18
18
|
${r}`:void 0,"","RULES:","1. Output ONLY the feature file content \u2014 no prose, no code fences, no explanations.","2. Gherkin/.saffron syntax: Feature:, Scenario:, Scenario Outline: + Examples, Given/When/Then/And/But. Prefer concrete scenarios; use an Outline only when the requirements list variations of the same flow.","3. Then steps are assertions \u2014 state exactly what must be true, one condition per step. End every scenario with at least one Then.","4. If the same 3+ step sequence would appear in two or more scenarios, define it once as a StepSet (first step a guard, last step an exit assertion) and invoke it. Definition uses a colon (StepSet: Name), invocation does not (StepSet Name).",'5. Use 2-column data tables for key/value input, header tables for lists of records, and """ doc strings for free text.',"6. Steps are natural language for an AI browser agent \u2014 never CSS selectors or code.","","REQUIREMENTS:",e.trim()].filter(s=>s!==void 0).join(`
|
|
19
|
-
`)}async function Yt(e,t,n){let r={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0},s="",o=
|
|
20
|
-
`,usage:r}}import De from"node:fs";import ie from"node:path";import{createConnection as
|
|
21
|
-
`)
|
|
19
|
+
`)}async function Yt(e,t,n){let r={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0},s="",o=Ir({prompt:_r(e,t),options:{model:n,maxTurns:1,permissionMode:"bypassPermissions",persistSession:!1,settingSources:[],allowedTools:[],systemPrompt:"You are Saffron's feature-file author. You write clean, assertive Gherkin that reuses the project's exact step vocabulary. You output only file content."}});for await(let i of o)if(i.type==="assistant")for(let a of i.message.content)a.type==="text"&&(s+=a.text);else i.type==="result"&&(r.aiCalls=i.num_turns,r.costUsd=i.total_cost_usd,"usage"in i&&i.usage&&(r.inputTokens=i.usage.input_tokens??0,r.outputTokens=i.usage.output_tokens??0,r.cacheReadTokens=i.usage.cache_read_input_tokens??0,r.cacheCreationTokens=i.usage.cache_creation_input_tokens??0));return s=s.replace(/^\s*```(?:gherkin|saffron)?\s*\n/,"").replace(/\n```\s*$/,"").trim(),{content:s+`
|
|
20
|
+
`,usage:r}}import De from"node:fs";import ie from"node:path";import{createConnection as Or,ProposedFeatures as Dr,TextDocuments as Mr,TextDocumentSyncKind as Nr,CompletionItemKind as be,InsertTextFormat as Kt,DiagnosticSeverity as Oe,MarkupKind as Qt}from"vscode-languageserver/node";import{TextDocument as Ur}from"vscode-languageserver-textdocument";var Xt=/^(\s*)(Given|When|Then|And|But|\*)\s+(.+?)\s*$/,Lr=/^\s*StepSet:\s*(.+?)\s*$/,et=/^(\s*)(StepSet)\s+([^\s:].*?)\s*$/,tt={recorded:"\u25CF recorded \u2014 replays at zero tokens",divergent:"\u25CF divergent \u2014 same text, different recordings",unrecorded:"\u25CB written, not recorded yet"};function Br(e,t){let n=i=>new Set(i.toLowerCase().replace(/"[^"]*"/g,'"x"').split(/[^a-z0-9"<>]+/).filter(Boolean)),r=n(e),s=n(t);if(r.size===0||s.size===0)return 0;let o=0;for(let i of r)s.has(i)&&o++;return o/(r.size+s.size-o)}function Vr(e){try{let t=JSON.parse(De.readFileSync(ie.join(e,"saffron.config.json"),"utf8"));return typeof t.features=="string"?t.features:void 0}catch{return}}function tn(e,t,n={}){let r=e,s=t,o=Or(Dr.all,process.stdin,process.stdout),i=new Mr(Ur),a={steps:[],stepSets:[],scannedFiles:0,scannedCaches:0},c=new Map,d,m=()=>{try{let y=ie.isAbsolute(s)?s:ie.join(r,s);c=Z(de(y),r),a=q(r,s,c)}catch(y){o.console.warn(`saffron vocabulary rebuild: ${String(y)}`)}for(let y of i.all())h(y)},p=()=>{clearTimeout(d),d=setTimeout(m,300)};function l(y){return a.steps.find(u=>u.text===y)?.status}function h(y){if(!y.uri.endsWith(".saffron"))return;let u=[],g=y.getText().split(`
|
|
21
|
+
`),b=new Map;for(let x=0;x<g.length;x++){let f=g[x],w={start:{line:x,character:0},end:{line:x,character:f.length}},v=f.match(et);v&&!c.has(v[3])&&u.push({range:w,severity:Oe.Error,source:"saffron",message:`StepSet "${v[3]}" is not defined anywhere in the project.`});let $=f.match(Lr);if($){let k=c.get($[1]),R=ie.relative(r,new URL(y.uri).pathname).replace(/\\/g,"/");(b.has($[1])||k&&k.file!==R)&&u.push({range:w,severity:Oe.Error,source:"saffron",message:`StepSet "${$[1]}" is defined twice \u2014 set names are project-wide. If this line was meant to INVOKE the set, remove the colon: "StepSet ${$[1]}".`}),b.set($[1],x)}let C=f.trim();if(C&&!v&&!$){for(let[k]of c)if(C.toLowerCase()===k.toLowerCase()){u.push({range:w,severity:Oe.Warning,source:"saffron",message:`Did you mean "StepSet ${k}"? Invoking a step set needs the StepSet keyword, like a step needs Given/When/Then.`});break}}let S=f.match(Xt);if(S&&l(S[3])!=="recorded"){for(let k of a.steps)if(!(k.text===S[3]||k.status==="unrecorded")&&Br(k.text,S[3])>=.7){u.push({range:{start:{line:x,character:f.indexOf(S[3])},end:{line:x,character:f.length}},severity:Oe.Information,source:"saffron",message:`Similar to the ${k.status} step "${k.text}" (\xD7${k.usage}). Reusing the exact wording replays at zero tokens; a new wording costs a fresh AI recording.`});break}}}o.sendDiagnostics({uri:y.uri,diagnostics:u})}o.onInitialize(y=>{if(n.rootFromClient){let u=y.workspaceFolders?.[0]?.uri??y.rootUri??void 0;if(u&&u.startsWith("file:")){let g=decodeURIComponent(new URL(u).pathname);De.existsSync(g)&&(r=g,s=Vr(g)??t)}}return m(),{capabilities:{textDocumentSync:Nr.Incremental,completionProvider:{triggerCharacters:[" "]},definitionProvider:!0,hoverProvider:!0}}}),o.onCompletion(y=>{let u=i.get(y.textDocument.uri);if(!u)return[];let g=u.getText({start:{line:y.position.line,character:0},end:y.position});return zr(u.uri,g,y.position.line,a)}),o.onDefinition(y=>{let u=i.get(y.textDocument.uri);if(!u)return;let g=u.getText({start:{line:y.position.line,character:0},end:{line:y.position.line+1,character:0}}).replace(/\n$/,""),b=Zt(g,y.position.character);if(b){let v=en(r,b.name);return v?{uri:`file://${v.file}`,range:{start:{line:v.line,character:0},end:{line:v.line,character:0}}}:void 0}let x=g.match(et);if(!x)return;let f=c.get(x[3]);return f?{uri:`file://${ie.join(r,f.file)}`,range:{start:{line:f.line-1,character:0},end:{line:f.line-1,character:0}}}:void 0}),o.onHover(y=>{let u=i.get(y.textDocument.uri);if(!u)return;{let f=u.getText({start:{line:y.position.line,character:0},end:{line:y.position.line+1,character:0}}).replace(/\n$/,""),w=Zt(f,y.position.character);if(w){let v=en(r,w.name),$=v?`defined in ${ie.relative(r,v.file)} (line ${v.line+1}); the value is resolved at replay and never shown here`:"not found in .env or .env.example; export it in the shell or add it to a git-ignored .env";return{contents:{kind:"markdown",value:`**{env:${w.name}}** ${$}.`}}}}let g=u.getText({start:{line:y.position.line,character:0},end:{line:y.position.line+1,character:0}}).replace(/\n$/,""),b=g.match(et);if(b){let f=a.stepSets.find(v=>v.name===b[3]);if(!f)return;let w=f.steps.map(v=>`- ${tt[l(v)??"unrecorded"][0]} ${v}`).join(`
|
|
22
22
|
`);return{contents:{kind:Qt.Markdown,value:`**StepSet ${f.name}** \u2014 expands to:
|
|
23
23
|
|
|
24
|
-
${
|
|
24
|
+
${w}`}}}let x=g.match(Xt);if(x){let f=a.steps.find(w=>w.text===x[3]);return f?{contents:{kind:Qt.Markdown,value:`${tt[f.status]}
|
|
25
25
|
|
|
26
26
|
Used in ${f.usage} place(s): ${f.files.join(", ")}${f.fromStepSet?`
|
|
27
27
|
|
|
28
|
-
Defined in StepSet **${f.fromStepSet}**`:""}`}}:void 0}}),i.onDidOpen(
|
|
29
|
-
`).findIndex(i=>new RegExp(`^\\s*(?:export\\s+)?${t}\\s*=`).test(i));if(o!==-1)return{file:r,line:o}}}function
|
|
28
|
+
Defined in StepSet **${f.fromStepSet}**`:""}`}}:void 0}}),i.onDidOpen(y=>h(y.document)),i.onDidChangeContent(y=>h(y.document)),i.onDidSave(()=>p()),o.onDidChangeWatchedFiles(()=>p()),i.listen(o),o.listen()}function Zt(e,t){let n=/\{env:([A-Za-z_][A-Za-z0-9_]*)\}/g,r;for(;r=n.exec(e);)if(t>=r.index&&t<=r.index+r[0].length)return{name:r[1],start:r.index,end:r.index+r[0].length}}function en(e,t){for(let n of[".env",".env.example"]){let r=ie.join(e,n);if(!De.existsSync(r))continue;let o=De.readFileSync(r,"utf8").split(`
|
|
29
|
+
`).findIndex(i=>new RegExp(`^\\s*(?:export\\s+)?${t}\\s*=`).test(i));if(o!==-1)return{file:r,line:o}}}function zr(e,t,n,r){let s=e.endsWith(".saffron");if(/^\s*StepSet\s+/.test(t)&&s)return r.stepSets.map(a=>({label:a.name,kind:be.Function,detail:`step set \xB7 ${a.steps.length} step(s) \xB7 \xD7${a.usage}`,documentation:a.steps.join(`
|
|
30
30
|
`)}));if(/^\s*(Given|When|Then|And|But|\*)\s+/.test(t))return r.steps.map((a,c)=>({label:a.text,kind:be.Text,detail:`${tt[a.status]} \xB7 \xD7${a.usage}`,sortText:String(c).padStart(5,"0"),filterText:a.text}));let i=t.replace(/^\s*/,"").trimEnd().toLowerCase().replace(/\s+/g,"");if(i.length>0&&("stepset:".startsWith(i)||i.startsWith("stepset"))&&s){let a={start:{line:n,character:t.length-t.replace(/^\s*/,"").length},end:{line:n,character:t.length}},c=[{label:"StepSet: \u2026",kind:be.Keyword,detail:"define a reusable step set",documentation:`StepSet: Name
|
|
31
31
|
Given \u2026
|
|
32
32
|
When \u2026
|
|
33
33
|
|
|
34
|
-
Invoke it anywhere with: StepSet Name`,filterText:"step set stepset:",sortText:"0",insertTextFormat:Kt.Snippet,textEdit:{range:a,newText:"StepSet: ${1:Name}\n "}}];r.stepSets.length===0&&c.push({label:"StepSet \u2026",kind:be.Keyword,detail:"invoke a step set (none defined yet)",filterText:"step set stepset",sortText:"1",insertTextFormat:Kt.Snippet,textEdit:{range:a,newText:"StepSet ${1:Name}"}});for(let
|
|
35
|
-
`),filterText:`step set ${
|
|
36
|
-
`).filter(Boolean).map(
|
|
37
|
-
`)){let a=
|
|
38
|
-
`)}}}var ge={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0};async function
|
|
39
|
-
`):void 0,featureEdits:
|
|
40
|
-
`)[0]})`:"")+". A row value is probably baked in as a literal instead of a <placeholder>; inspect the proposal, reject it, and re-record with saffron run --rerecord."}}
|
|
41
|
-
`)[0]}
|
|
42
|
-
`)[0]}`}}}return await this.finalizeAgentRun(t,x,y,p,r,$,y?g?.failure:void 0)}finally{await l.close()}}async finalizeAgentRun(t,n,r,s,o,i=0,a){let c=r?"agent-heal":"agent-record",p=n.recordedSteps.map(b=>({gherkin:b.gherkin,kind:b.kind,status:n.success?"passed":"failed"}));if(!n.success)return{scenario:t,status:"red",source:c,stepResults:p,narrative:n.narrative,adaptations:n.adaptations,usage:n.usage,durationMs:Date.now()-o,error:n.narrative};let h=r?xn(s.steps,n.recordedSteps,this.options.assertionPolicy==="adaptable-mid",a?{stepIndex:a.stepIndex,actionIndex:a.actionIndex}:void 0):n.recordedSteps,d=[...n.adaptations],l={...n.usage},m,g;if(this.options.verifyProposals!==!1){let b=await this.proofReplay(t,this.buildCandidateCache(t,h));try{if(b.result.status!=="passed"&&b.result.failure){let w=b.result.failure;if(this.options.agent&&!(r&&w.kind==="assertion"&&(this.options.assertionPolicy!=="adaptable-mid"||Sn(s.steps,w.stepIndex))))try{let S=await this.withDialogsVisible(()=>this.options.agent.run({scenario:t,baseURL:this.options.baseURL,mode:"refine",model:r?this.options.healModel:void 0,cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:b.result.stepResults.filter(k=>k.status==="passed").map(k=>k.gherkin),failedStep:{text:w.stepText,kind:w.kind,error:w.error},takeFingerprint:async()=>ae(we(this.browser)??b.page)}));l=bn(l,S.usage),S.success&&(d.push(...S.adaptations),h=r?xn(h,S.recordedSteps,this.options.assertionPolicy==="adaptable-mid"):as(h,S.recordedSteps),await b.context.close(),b=await this.proofReplay(t,this.buildCandidateCache(t,h)))}catch{}}m=b.result.status==="passed",m||(g=b.result.failure?.error??`proof replay ${b.result.status}`)}finally{await b.context.close().catch(()=>{})}}let u=[...n.featureEdits];for(let b of Pt(h,W(this.options.projectRoot)))d.push(`duplicate wording: "${b.from}" recorded exactly the same actions as the existing step "${b.to}" (${b.feature} \u203A ${b.scenario}). Accept with --with-feature-edit to converge on the canonical wording.`),u.some(w=>w.index===b.index)||u.push({index:b.index,text:b.to});let y=xe(t),$=b=>y.size>0?ue(b,y):b;d=d.map($);let x={meta:{createdAt:new Date().toISOString(),mode:r?"heal":"record",narrative:$(n.narrative),adaptations:d,suggestedFeatureEdit:n.suggestedFeatureEdit?$(n.suggestedFeatureEdit):void 0,featureEdits:u.length>0?u:void 0,verified:m,proofError:g?$(g):void 0,seededSteps:i>0?i:void 0,recordedFor:t.displayName,usage:l},cache:{version:1,feature:t.featurePath,scenario:t.name,recordedAt:new Date().toISOString(),recordedBy:"agent",steps:h}},f=dn(h);f.length>0&&(m=!1,g=[g,...f].filter(Boolean).join(" "));let v=ye(this.options.projectRoot,t.featurePath,t.name);return Mt(v,x),{scenario:t,status:"yellow",source:c,stepResults:p,narrative:n.narrative,adaptations:d,suggestedFeatureEdit:n.suggestedFeatureEdit,featureEdits:u.length>0?u:void 0,verified:m,proofError:g,seededSteps:i>0?i:void 0,proposalFile:v,usage:l,durationMs:Date.now()-o}}};import An from"node:path";import{createRequire as fs}from"node:module";import{query as gs}from"@anthropic-ai/claude-agent-sdk";import{createSdkMcpServer as cs,tool as me}from"@anthropic-ai/claude-agent-sdk";import{z as R}from"zod";var M=e=>({content:[{type:"text",text:e}]});function wn(e){let t={};return e.role&&(t.role=e.role),e.name&&(t.name=e.name),e.nameRegex&&(t.nameRegex=e.nameRegex),e.label&&(t.label=e.label),e.text&&(t.text=e.text),e.selector&&(t.fallbackSelectors=[e.selector]),e.frame&&(t.frame=e.frame),Object.keys(t).length>0?t:void 0}var kn={role:R.string().optional().describe("ARIA role of the element"),name:R.string().optional().describe("Accessible name \u2014 use the SHORTEST stable substring; never include volatile content like prices or dates"),nameRegex:R.string().optional().describe("Regex for the accessible name when it embeds volatile content, e.g. 'Select \\\\{date\\\\+1\\\\} as checkin' cannot work \u2014 instead use nameRegex 'Select .* as checkin'"),label:R.string().optional().describe("Form-label text for getByLabel \u2014 the literal <label> text only, never a free-text description"),text:R.string().optional().describe("getByText locator text"),selector:R.string().optional().describe("CSS fallback selector"),frame:R.string().optional().describe(`CSS selector of the iframe hosting the element, e.g. 'iframe[title="Feedback widget"]' \u2014 REQUIRED whenever the element lives inside an iframe; without it replay looks only at the top-level page`)};function $n(e){return cs({name:"saffron",version:"0.1.0",tools:[me("saffron_step","Announce the Gherkin step you are about to perform. MUST be called before executing browser actions for each step, with the step's zero-based index and exact text.",{index:R.number().int().min(0),text:R.string(),adaptation:R.string().optional().describe("If the written step does not match the real UI and you are deviating from it, describe the deviation in one sentence.")},async t=>(e.currentStepIndex=t.index,e.announced.add(t.index),t.adaptation&&e.adaptations.push(`Step ${t.index+1} ("${t.text}"): ${t.adaptation}`),M(`Recording step ${t.index}: ${t.text}`))),me("saffron_capture",`Record a captureText action for the current step: at replay time, the target element's text is read and stored under saveAs for later expectDiffers comparisons. Use for steps like 'I record the displayed price as "X"'. The target must select exactly the element whose text is the value.`,{saveAs:R.string().describe("Name to store the captured text under"),...kn},async t=>{let n=wn(t);return n?(e.addActions([{action:"captureText",target:n,saveAs:t.saveAs}]),M(`Capture recorded as "${t.saveAs}".`)):M("ERROR: capture needs a target (role/name, nameRegex, label, text, or selector).")}),me("saffron_dialog","Record how a native dialog (alert/confirm/prompt) must be handled at replay. Call this IMMEDIATELY whenever an action triggers a native dialog \u2014 whether you handled it via browser_handle_dialog or the browser reported it as already handled. At replay Saffron arms this handler BEFORE the triggering action, so without this call the replay will auto-dismiss the dialog and diverge from what you saw.",{accept:R.boolean().describe("true = accept/OK the dialog, false = dismiss/cancel"),promptText:R.string().optional().describe("Text typed into a prompt() before accepting")},async t=>(e.addArmedAction({action:"handleDialog",value:t.accept?"accept":"dismiss",...t.promptText?{promptText:t.promptText}:{}}),M(`Dialog handling recorded (${t.accept?"accept":"dismiss"}).`))),me("saffron_wait_api","Record a network wait for the current step: replay will block until a matching API response is observed (responses from the triggering step count). Call AFTER you verified via browser_network_requests that the response actually occurred with the expected status/body. Use a path-only urlPattern regex and never literal volatile IDs (/api/orders/\\d+, not /api/orders/8842).",{urlPattern:R.string().describe("Regex matched against the response URL \u2014 path-only for portability"),method:R.string().optional().describe("HTTP method to match (any if omitted)"),status:R.string().optional().describe("Expected status \u2014 exact ('201') or class ('2xx'); default any 2xx"),bodyPattern:R.string().optional().describe(`Regex the response body must match, e.g. '"status"\\s*:\\s*"READY"' for polling waits`)},async t=>(e.addActions([{action:"waitForResponse",urlPattern:t.urlPattern,...t.method?{method:t.method}:{},...t.status?{status:t.status}:{},...t.bodyPattern?{pattern:t.bodyPattern}:{}}]),M("Network wait recorded."))),me("saffron_assert","Record a verified assertion for the current Then step. Call ONLY after you have confirmed the condition holds via a page snapshot. If the condition does NOT hold, do not call this \u2014 call saffron_done with success=false instead. NEVER record volatile content (prices, dates, counters) as literal expected text \u2014 use kind=matches with a pattern, or kind=differs against captured values.",{kind:R.enum(["visible","notVisible","text","url","value","differs","matches","attribute","response"]),...kn,expected:R.string().optional().describe("Expected text/value for kind=text|value \u2014 stable text only, never volatile values"),url:R.string().optional().describe("URL substring for kind=url (path only, no host)"),pattern:R.string().optional().describe("kind=matches|attribute: regex the target's text (or attribute) must match, e.g. 'NOK [\\\\d,]+' for a price"),attribute:R.string().optional().describe("kind=attribute: attribute name to check, e.g. 'href' for link-target assertions \u2014 use this for 'X should link to Y' instead of clicking through"),capture:R.string().optional().describe("kind=differs: name of the captured value on the LEFT of the comparison (omit when comparing the target element's live text)"),urlPattern:R.string().optional().describe("kind=response: regex matched against the response URL \u2014 path-only for portability, never literal volatile IDs (use /api/orders/\\d+)"),method:R.string().optional().describe("kind=response: HTTP method to match (any if omitted)"),status:R.string().optional().describe("kind=response: expected status \u2014 exact ('201') or class ('2xx'); default any 2xx"),bodyPattern:R.string().optional().describe("kind=response: regex the response body must match (verify via browser_network_requests first)"),compareTo:R.string().optional().describe("kind=differs: name of the captured value to compare against")},async t=>{let n=wn(t),r;switch(t.kind){case"visible":r={action:"expectVisible",target:n};break;case"notVisible":r={action:"expectNotVisible",target:n};break;case"text":r={action:"expectText",target:n,value:t.expected??""};break;case"value":r={action:"expectValue",target:n,value:t.expected??""};break;case"url":r={action:"expectUrl",url:t.url??""};break;case"differs":if(!t.compareTo)return M("ERROR: kind=differs requires compareTo.");if(!t.capture&&!n)return M("ERROR: kind=differs needs either capture (a stored name) or a target element.");r={action:"expectDiffers",target:n,value:t.capture,compareTo:t.compareTo};break;case"matches":if(!t.pattern)return M("ERROR: kind=matches requires pattern.");r={action:"expectMatches",target:n,pattern:t.pattern};break;case"attribute":if(!t.attribute||!t.pattern)return M("ERROR: kind=attribute requires attribute and pattern.");r={action:"expectAttribute",target:n,attribute:t.attribute,pattern:t.pattern};break;case"response":if(!t.urlPattern)return M("ERROR: kind=response requires urlPattern.");r={action:"expectResponse",urlPattern:t.urlPattern,...t.method?{method:t.method}:{},...t.status?{status:t.status}:{},...t.bodyPattern?{pattern:t.bodyPattern}:{}};break}return!["url","differs","response"].includes(t.kind)&&!n?M("ERROR: assertion needs a target (role/name, nameRegex, label, text, or selector)."):(e.addActions([r]),M("Assertion recorded."))}),me("saffron_done","Finalize the run. Call exactly once, after the final step: success=true only if every step's intent was achieved and every assertion verified.",{success:R.boolean(),narrative:R.string().describe("2-4 sentence summary of the run: what happened, and why it failed if it did."),suggestedFeatureEdit:R.string().optional().describe("If you adapted any steps: the corrected Gherkin for those steps, as you would rewrite them in the .feature file (free text, for the human report)."),featureEdits:R.array(R.object({index:R.number().int().min(0).describe("zero-based step index"),text:R.string().describe("replacement step text WITHOUT the Given/When/Then keyword")})).optional().describe("Machine-applicable version of the suggested edits: one entry per adapted step. Provide this whenever you adapted a step.")},async t=>(e.done=!0,e.success=t.success,e.narrative=t.narrative,e.suggestedFeatureEdit=t.suggestedFeatureEdit,e.featureEdits=t.featureEdits??[],M("Run finalized.")))]})}function Rn(e={}){let t=e.deliberateSnapshots?`
|
|
34
|
+
Invoke it anywhere with: StepSet Name`,filterText:"step set stepset:",sortText:"0",insertTextFormat:Kt.Snippet,textEdit:{range:a,newText:"StepSet: ${1:Name}\n "}}];r.stepSets.length===0&&c.push({label:"StepSet \u2026",kind:be.Keyword,detail:"invoke a step set (none defined yet)",filterText:"step set stepset",sortText:"1",insertTextFormat:Kt.Snippet,textEdit:{range:a,newText:"StepSet ${1:Name}"}});for(let d of r.stepSets)c.push({label:`StepSet ${d.name}`,kind:be.Function,detail:`invoke step set \xB7 ${d.steps.length} step(s)`,documentation:d.steps.join(`
|
|
35
|
+
`),filterText:`step set ${d.name}`,sortText:`1${d.name}`,textEdit:{range:a,newText:`StepSet ${d.name}`}});return c}return[]}import yn from"node:fs";import rs from"node:net";import Sn from"node:path";import{chromium as ss,firefox as os,webkit as is}from"playwright";import rn from"node:path";var qr=/\{date([+-]\d+)?(?::([^}]+))?\}/g;function Hr(e,t){let n=String(e.getFullYear()),r=String(e.getMonth()+1).padStart(2,"0"),s=String(e.getDate()).padStart(2,"0");return t.replaceAll("YYYY",n).replaceAll("MM",r).replaceAll("DD",s).replaceAll("M",String(e.getMonth()+1)).replaceAll("D",String(e.getDate()))}function nt(e,t=new Date){return e.replace(qr,(n,r,s)=>{let o=new Date(t);return o.setDate(o.getDate()+(r?parseInt(r,10):0)),Hr(o,s??"YYYY-MM-DD")})}var Wr=/\b(\d{4})-(\d{2})-(\d{2})\b/g,Gr=400;function nn(e,t=new Date){let n=new Date(t.getFullYear(),t.getMonth(),t.getDate());return e.replace(Wr,(r,s,o,i)=>{let a=new Date(Number(s),Number(o)-1,Number(i));if(Number.isNaN(a.getTime()))return r;let c=Math.round((a.getTime()-n.getTime())/864e5);return Math.abs(c)>Gr?r:c===0?"{date}":`{date${c>0?"+":""}${c}}`})}import{createHash as Jr}from"node:crypto";async function ae(e){let t;try{t=await e.locator("body").ariaSnapshot({timeout:3e3})}catch{return"sha256:unavailable"}let n=t.replace(/\d+/g,"#").replace(/\s+/g," ").trim();return`sha256:${Jr("sha256").update(n).digest("hex")}`}var ot=e=>new Promise(t=>setTimeout(t,e)),st=class{entries=[];seq=0;prevStepSeq=0;curStepSeq=0;listener;context;attach(t,n){this.context=t,this.listener=r=>{let s={seq:++this.seq,url:r.url(),method:r.request().method(),status:r.status()};if(this.entries.push(s),n){let o=r.headers()["content-type"]??"";/json|text|xml/i.test(o)&&r.text().then(i=>{s.body=i.length>262144?i.slice(0,262144):i}).catch(()=>{})}},t.on("response",this.listener)}detach(){this.context&&this.listener&&this.context.off("response",this.listener)}markStep(){this.prevStepSeq=this.curStepSeq,this.curStepSeq=this.seq}sincePreviousStep(){return this.entries.filter(t=>t.seq>this.prevStepSeq)}all(){return this.entries}};function Yr(e,t){if(!t)return e>=200&&e<300;let n=/^([1-5])xx$/i.exec(t.trim());if(n){let r=Number(n[1])*100;return e>=r&&e<r+100}return e===Number(t.trim())}function sn(e,t,n,r,s){return!(!t.test(e.url)||n&&e.method.toUpperCase()!==n.toUpperCase()||!Yr(e.status,r)||s&&!s.test(e.body??""))}var Kr=(e,t)=>`response matching ${e.method??"any-method"} /${t}/ status ${e.status??"2xx"}${e.pattern?` body /${e.pattern}/`:""}`;function on(e,t,n,r=!1){let s=a=>Se(nt(ne(a,n))),o=[],i=a=>{if(t.role){let c=t.nameRegex?new RegExp(s(t.nameRegex),"i"):t.name?s(t.name):void 0;o.push(a.getByRole(t.role,{name:c,exact:!1}))}t.label&&o.push(a.getByLabel(s(t.label))),t.placeholder&&o.push(a.getByPlaceholder(s(t.placeholder))),t.text&&o.push(a.getByText(s(t.text))),t.testId&&o.push(a.getByTestId(s(t.testId)));for(let c of t.fallbackSelectors??[])o.push(a.locator(c))};if(t.frame)i(e.frameLocator(s(t.frame)));else if(i(e),r)for(let a of e.frames())a!==e.mainFrame()&&i(a);if(o.length===0)throw new Error("Action target has neither role nor fallback selectors");return o}async function W(e,t,n){let r=Date.now()+t,s;for(;Date.now()<r;){try{if(await e())return}catch(o){s=o}await ot(100)}throw new Error(`Timed out waiting for ${n}${s?` (${String(s)})`:""}`)}async function L(e,t){let n;for(let r of e)try{await t(r.first());return}catch(s){n=s}throw n}var rt=e=>e.replace(/\s+/g," ").trim();async function it(e,t,n,r,s,o=new Map,i){let a=u=>Se(nt(ne(u,n))),c=t.value!==void 0?a(t.value):void 0,d=t.url!==void 0?a(t.url):void 0,m=t.action==="expectVisible"||t.action==="expectText"||t.action==="expectValue"||t.action==="expectMatches"||t.action==="expectAttribute"||t.action==="captureText",p=t.target?on(e,t.target,n,m):[],l={timeout:r},h={timeout:Math.min(1e3,r)},y=u=>async()=>{for(let g of p)try{if(await u(g.first()))return!0}catch{}return!1};switch(t.action){case"goto":{if(!d)throw new Error("goto requires url");let u=/^https?:\/\//.test(d)?d:new URL(d,s).toString();await e.goto(u,l);return}case"click":return L(p,u=>u.click(l));case"fill":return L(p,u=>u.fill(c??"",l));case"press":return p.length>0?L(p,u=>u.press(c??"",l)):e.keyboard.press(c??"");case"selectOption":return L(p,async u=>{await u.selectOption(c??"",l)});case"check":return L(p,u=>u.check(l));case"uncheck":return L(p,u=>u.uncheck(l));case"hover":return L(p,u=>u.hover(l));case"waitFor":return p.length>0?L(p,u=>u.waitFor({state:"visible",timeout:r})):ot(Math.min(Number(c??"0"),r));case"expectVisible":return W(y(u=>u.isVisible()),r,"target to be visible");case"expectNotVisible":return L(p,u=>u.waitFor({state:"hidden",timeout:r}));case"expectText":return W(y(async u=>(await u.textContent(h)??"").includes(c??"")),r,`text "${c}"`);case"expectValue":return W(y(async u=>await u.inputValue(h)===(c??"")),r,`input value "${c}"`);case"expectUrl":{let u=d??c??"";return W(async()=>e.context().pages().some(g=>g.url().includes(u)),r,`URL containing "${u}" in any tab (current: ${e.context().pages().map(g=>g.url()).join(", ")})`)}case"handleDialog":{let u=(c??"accept")!=="dismiss",g=t.promptText?a(t.promptText):void 0;e.once("dialog",b=>{u?b.accept(g):b.dismiss()});return}case"uploadFile":{let u=(c??"").split(`
|
|
36
|
+
`).filter(Boolean).map(g=>rn.isAbsolute(g)?g:rn.resolve(g));if(u.length===0)throw new Error("uploadFile requires a path");if(p.length>0)return L(p,g=>g.setInputFiles(u,l));e.once("filechooser",g=>{g.setFiles(u)});return}case"dragTo":{if(!t.toTarget)throw new Error("dragTo requires toTarget");let u=on(e,t.toTarget,n);return L(p,g=>g.dragTo(u[0].first(),l))}case"captureText":{if(!t.saveAs)throw new Error("captureText requires saveAs");return W(y(async u=>{let g=rt(await u.textContent(h)??"");return g===""?!1:(o.set(t.saveAs,g),!0)}),r,`non-empty text to capture as "${t.saveAs}"`)}case"expectDiffers":{if(!t.compareTo)throw new Error("expectDiffers requires compareTo");let u=o.get(t.compareTo);if(u===void 0)throw new Error(`expectDiffers: no captured value named "${t.compareTo}"`);if(p.length>0)return W(y(async x=>rt(await x.textContent(h)??"")!==u),r,`target text to differ from "${t.compareTo}" (${u})`);let g=c??"",b=o.get(g);if(b===void 0)throw new Error(`expectDiffers: no captured value named "${g}"`);if(b===u)throw new Error(`expectDiffers: "${g}" (${b}) equals "${t.compareTo}" (${u})`);return}case"expectAttribute":{if(!t.attribute)throw new Error("expectAttribute requires attribute");if(!t.pattern)throw new Error("expectAttribute requires pattern");let u=new RegExp(t.pattern,"i");return W(y(async g=>u.test(await g.getAttribute(t.attribute,h)??"")),r,`attribute ${t.attribute} to match /${t.pattern}/i`)}case"expectMatches":{if(!t.pattern)throw new Error("expectMatches requires pattern");let u=new RegExp(t.pattern,"i");return W(y(async g=>u.test(rt(await g.textContent(h)??""))),r,`target text to match /${t.pattern}/i`)}case"switchTab":{let u=Number.parseInt(c??"",10),g=()=>e.context().pages().filter(x=>!x.isClosed());await W(async()=>g().length>u,r,`tab ${c} to exist (${g().length} open: ${g().map(x=>x.url()).join(", ")})`);let b=g()[u];return await b.bringToFront(),b}case"closeTab":{let u=e.context().pages().filter(x=>!x.isClosed()),g=c===void 0?e:u[Number.parseInt(c,10)];if(!g)throw new Error(`tab ${c} does not exist (${u.length} open)`);await g.close();let b=e.context().pages().filter(x=>!x.isClosed());if(b.length===0)throw new Error("closed the last tab; nothing left to drive");return b[b.length-1]}case"newTab":{let u=await e.context().newPage();if(d){let g=/^https?:\/\//.test(d)?d:new URL(d,s).toString();await u.goto(g,l)}return u}case"waitForResponse":case"expectResponse":{if(!t.urlPattern)throw new Error(`${t.action} requires urlPattern`);let u=new RegExp(a(t.urlPattern)),g=t.pattern?new RegExp(a(t.pattern)):void 0;if(!i){await e.waitForResponse(x=>sn({seq:0,url:x.url(),method:x.request().method(),status:x.status()},u,t.method,t.status,void 0),l);return}let b=()=>t.action==="expectResponse"?i.all():i.sincePreviousStep();return W(async()=>b().some(x=>sn(x,u,t.method,t.status,g)),r,Kr(t,t.urlPattern))}default:throw new Error(`Unknown action type: ${t.action}`)}}function an(e,t){return e.actions.some(n=>[n.value,n.url,n.pattern,n.target?.name,n.target?.nameRegex,n.target?.label,n.target?.text,n.target?.placeholder].some(r=>r?.includes(t)))}function Qr(e,t){return t.steps.length!==e.steps.length?!1:t.steps.every((n,r)=>{let s=e.steps[r];if(n.gherkin!==s.text)return!1;let o=n.table,i=s.table;if(!!o!=!!i)return!1;if(o&&i){let a=Object.keys(U(o)),c=Object.keys(U(i));if(a.length>0&&c.length>0){if(JSON.stringify(a)!==JSON.stringify(c))return!1}else if(JSON.stringify(o)!==JSON.stringify(i))return!1}if(n.docString!==void 0!=(s.docString!==void 0))return!1;if(n.docString!==void 0&&s.docString!==void 0&&n.docString!==s.docString){let a=t.steps.some(d=>an(d,"<docstring>")),c=t.steps.some(d=>an(d,n.docString));if(!a||c)return!1}return!0})}function at(e,t){let n=e.steps[t],r=n?.resolvedTable??n?.table;return{...e.params,...le(e.steps),...U(r),...re(n?.resolvedDocString??n?.docString)}}async function ve(e,t,n,r={}){let s=r.actionTimeoutMs??5e3,o=r.retries??1,i=[],a=r.vars??new Map,c=()=>Object.fromEntries(a),d=n.steps.some(g=>g.actions.some(b=>(b.action==="waitForResponse"||b.action==="expectResponse")&&b.pattern!==void 0)),m=new st;m.attach(e.context(),d);let p=e,l=new Set(e.context().pages()),h=[],y=g=>h.push(g);e.context().on("page",y);let u=()=>{let g=e.context().pages();for(let b of g)l.has(b)||(l.add(b),b.isClosed()||(p=b));if(p.isClosed()){let b=g.filter(x=>!x.isClosed());b.length>0&&(p=b[b.length-1])}};if(!Qr(t,n))return m.detach(),{status:"stale",stepResults:i,captured:c()};try{for(let g=0;g<n.steps.length;g++){let b=n.steps[g],x=at(t,g);m.markStep();let f=!1,w=0;for(let v=0;v<b.actions.length;v++){let $=b.actions[v];u(),$.pageFingerprint&&await ae(p)!==$.pageFingerprint&&(f=!0);let C,S=!1;for(let k=0;k<=o&&!S;k++){k>0&&(w++,await ot(250*k),u());try{let R=await it(p,$,x,s,r.baseURL,a,m);for(R&&(p=R,l.add(R)),($.action==="click"||$.action==="press")&&h.length===0&&await e.context().waitForEvent("page",{timeout:150}).catch(()=>{});h.length>0;)await h.shift().waitForLoadState("domcontentloaded",{timeout:s}).catch(()=>{});S=!0}catch(R){C=R}}if(!S){let k=C instanceof Error?C.message:String(C);i.push({gherkin:b.gherkin,fromStepSet:b.fromStepSet,kind:b.kind,status:"failed",drift:f,...w?{retries:w}:{},error:k});for(let R=g+1;R<n.steps.length;R++)i.push({gherkin:n.steps[R].gherkin,kind:n.steps[R].kind,status:"skipped"});return{status:"failed",failure:{stepIndex:g,actionIndex:v,stepText:b.gherkin,kind:b.kind,error:k},stepResults:i,captured:c()}}}i.push({gherkin:b.gherkin,fromStepSet:b.fromStepSet,kind:b.kind,status:"passed",drift:f,...w?{retries:w}:{}})}return{status:"passed",stepResults:i,captured:c()}}finally{e.context().off("page",y),m.detach()}}var cn=new Set(["expectVisible","expectNotVisible","expectText","expectUrl","expectValue","expectDiffers","expectMatches","expectAttribute","expectResponse"]);var Me=class{steps;currentStepIndex=-1;done=!1;success=!1;narrative="";adaptations=[];suggestedFeatureEdit;featureEdits=[];announced=new Set;pendingFingerprint;constructor(t){this.steps=t.steps.map(n=>({gherkin:n.text,keyword:n.keyword,kind:n.kind,table:n.table,docString:n.docString,fromStepSet:n.source?.fromStepSet,actions:[]}))}addArmedAction(t){if(this.done)return;let n=Math.max(this.currentStepIndex,0),r=this.steps[n];if(!r||r.actions.some(i=>i.action===t.action&&i.value===t.value&&i.promptText===t.promptText))return;let o=Math.max(r.actions.length-1,0);r.actions.splice(o,0,t)}addActions(t){if(this.done||t.length===0)return;let n=Math.max(this.currentStepIndex,0),r=this.steps[n];r&&(this.pendingFingerprint&&t[0]&&(t[0].pageFingerprint=this.pendingFingerprint,this.pendingFingerprint=void 0),r.actions.push(...t))}},ce=e=>e.replace(/\\(['"\\])/g,"$1");function Xr(e){let t=e.match(/'((?:[^'\\]|\\.)*)'/);return t?ce(t[1]):void 0}function ct(e){let t={},n=e.match(/(?:frameLocator\('((?:[^'\\]|\\.)*)'\)|locator\('((?:[^'\\]|\\.)*)'\)\.contentFrame\(\))/);n&&(t.frame=ce(n[1]??n[2]),e=e.slice((n.index??0)+n[0].length));let r=e.match(/getByRole\('([^']+)'(?:,\s*\{([^}]*)\})?\)/);if(r){t.role=r[1];let i=(r[2]??"").match(/name:\s*'((?:[^'\\]|\\.)*)'/);i&&(t.name=ce(i[1]))}let s=[[/getByLabel\('((?:[^'\\]|\\.)*)'\)/,"label"],[/getByText\('((?:[^'\\]|\\.)*)'\)/,"text"],[/getByTestId\('((?:[^'\\]|\\.)*)'\)/,"testId"],[/getByPlaceholder\('((?:[^'\\]|\\.)*)'\)/,"placeholder"]];for(let[i,a]of s){let c=e.match(i);c&&(t[a]=ce(c[1]))}let o=e.match(/(?:^|\.)locator\('((?:[^'\\]|\\.)*)'\)/);return o&&(t.fallbackSelectors=[ce(o[1])]),Object.keys(t).length>0?t:void 0}function Zr(e,t){if(t&&e.startsWith(t)){let n=e.slice(t.length);return n.startsWith("/")?n:`/${n}`}return e}var es={click:"click",dblclick:"click",fill:"fill",pressSequentially:"fill",type:"fill",press:"press",check:"check",uncheck:"uncheck",hover:"hover",selectOption:"selectOption"};function ts(e,t){let n=e.match(/page\.goto\('((?:[^'\\]|\\.)*)'\)/);if(n)return{action:"goto",url:Zr(ce(n[1]),t)};let r=e.match(/page\.keyboard\.press\('((?:[^'\\]|\\.)*)'\)/);if(r)return{action:"press",value:ce(r[1])};let s=e.match(/await\s+page\.(.+)\.dragTo\(page\.(.+?)\);?\s*$/);if(s){let l=ct(s[1]),h=ct(s[2]);if(l&&h)return{action:"dragTo",target:l,toTarget:h}}let o=e.match(/await\s+page\.(.+)\.(click|dblclick|fill|pressSequentially|type|press|check|uncheck|hover|selectOption)\((.*)\);?\s*$/);if(!o)return;let[,i,a,c]=o,d=ct(i);if(!d)return;let m={action:es[a],target:d},p=Xr(c);return p!==void 0&&a!=="click"&&a!=="dblclick"&&(m.value=p),m}function ln(e,t){let n=[],r=Array.isArray(e)?e:e?.content;if(Array.isArray(r))for(let o of r){let i=o?.text;typeof i=="string"&&n.push(i)}else typeof e=="string"&&n.push(e);let s=[];for(let o of n)for(let i of o.split(`
|
|
37
|
+
`)){let a=ts(i.trim(),t);a&&s.push(a)}return ns(s)}function ns(e){return e.filter((t,n)=>{let r=e[n+1];return!(t.action==="fill"&&(t.value??"")===""&&r?.action==="fill"&&JSON.stringify(r.target)===JSON.stringify(t.target))})}function dn(e){let t=[];for(let n of e)if(/"[^"]+"|<[^<>]+>/.test(n.gherkin)){for(let s of n.actions)if(s.action==="fill"&&(s.value??"")===""){t.push(`Step "${n.gherkin}" recorded a fill with an empty value although the step passes one; the typed value was lost. Re-record with: saffron run --rerecord`);break}}return t}function lt(e,t){let n=e;for(let[r,s]of Object.entries(t).filter(([,o])=>o.length>=3).sort((o,i)=>i[1].length-o[1].length))n=n.split(s).join(`<${r}>`);return n}function pn(e,t){let n=Object.entries(t).filter(([,i])=>i.length>=3);if(n.length===0)return e;let r=i=>i===void 0?void 0:lt(i,t),s=[...n].sort((i,a)=>a[1].length-i[1].length),o=i=>{if(i!==void 0){for(let[a,c]of s)if(i.includes(c))return`<${a}>`;return i}};return e.map(i=>({...i,actions:i.actions.map(a=>{let c=cn.has(a.action)&&a.action!=="expectValue";return{...a,value:c?o(a.value):r(a.value),url:r(a.url),target:a.target?{...a.target,name:c?o(a.target.name):r(a.target.name),label:r(a.target.label),text:c?o(a.target.text):r(a.target.text),placeholder:r(a.target.placeholder)}:void 0}})}))}function un(e,t){let n=le(t.steps);return e.map((r,s)=>{let o=t.steps[s],i=o?.resolvedTable??o?.table,a=o?.resolvedDocString??o?.docString,c=Object.entries({...n,...U(i),...re(a)}).filter(([,m])=>m.length>=3).sort((m,p)=>p[1].length-m[1].length);if(c.length===0)return r;let d=m=>{if(m===void 0)return;let p=m;for(let[l,h]of c)p=p.split(h).join(`<${l}>`);return p};return{...r,actions:r.actions.map(m=>({...m,value:d(m.value),url:d(m.url),target:m.target?{...m.target,name:d(m.target.name),label:d(m.target.label),text:d(m.target.text),placeholder:d(m.target.placeholder)}:void 0}))}})}function fn(e,t=new Date){let n=r=>r===void 0?void 0:nn(r,t);return e.map(r=>({...r,actions:r.actions.map(s=>({...s,value:n(s.value),url:n(s.url),target:s.target?{...s.target,name:n(s.target.name),nameRegex:n(s.target.nameRegex),label:n(s.target.label),text:n(s.target.text)}:void 0}))}))}function gn(e,t){if(t.size===0)return e;let n=r=>r===void 0?void 0:ue(r,t);return e.map(r=>({...r,actions:r.actions.map(s=>({...s,value:n(s.value),url:n(s.url),pattern:n(s.pattern),target:s.target?{...s.target,name:n(s.target.name),nameRegex:n(s.target.nameRegex),label:n(s.target.label),text:n(s.target.text),placeholder:n(s.target.placeholder)}:void 0}))}))}function mn(e,t){if(e!=="mcp__playwright__browser_tabs")return;let n=t??{},r=typeof n.index=="number"?String(n.index):void 0;switch(n.action){case"select":return r===void 0?void 0:{action:"switchTab",value:r};case"close":return r===void 0?{action:"closeTab"}:{action:"closeTab",value:r};case"new":return typeof n.url=="string"&&n.url?{action:"newTab",url:n.url}:{action:"newTab"};default:return}}function hn(e,t){let n=t??{};if(e==="mcp__playwright__browser_handle_dialog"){let r={action:"handleDialog",value:n.accept?"accept":"dismiss"};return typeof n.promptText=="string"&&n.promptText&&(r.promptText=n.promptText),r}if(e==="mcp__playwright__browser_file_upload"){let r=Array.isArray(n.paths)?n.paths:[];if(r.length===0)return;let s=process.cwd()+"/";return{action:"uploadFile",value:r.map(i=>typeof i=="string"&&i.startsWith(s)?i.slice(s.length):i).join(`
|
|
38
|
+
`)}}}var ge={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0};async function as(){return new Promise((e,t)=>{let n=rs.createServer();n.listen(0,"127.0.0.1",()=>{let{port:r}=n.address();n.close(()=>e(r))}),n.on("error",t)})}function we(e){let t=e.contexts().flatMap(n=>n.pages());return[...t].reverse().find(n=>n.url()!=="about:blank")??t.at(-1)}function wn(e){let t=e.length;for(let n=e.length-1;n>=0&&e[n].kind==="assertion";n--)t=n;return t}function xn(e,t){return e[t]?.kind==="assertion"&&t>=wn(e)}function bn(e,t,n=!1,r){let s=wn(e);return e.map((o,i)=>{let a=t[i],c=a&&a.actions.length>0;return o.kind==="assertion"?n&&i<s&&c?a:o:c?r&&i===r.stepIndex&&r.actionIndex>0?{...a,actions:[...o.actions.slice(0,r.actionIndex),...a.actions]}:a:o})}function cs(e,t){return e.map((n,r)=>{let s=t[r];return s&&s.actions.length>0?s:n})}function vn(e,t){return{aiCalls:e.aiCalls+t.aiCalls,inputTokens:e.inputTokens+t.inputTokens,outputTokens:e.outputTokens+t.outputTokens,cacheReadTokens:e.cacheReadTokens+t.cacheReadTokens,cacheCreationTokens:e.cacheCreationTokens+t.cacheCreationTokens,costUsd:(e.costUsd??0)+(t.costUsd??0),models:e.models||t.models?[...new Set([...e.models??[],...t.models??[]])]:void 0}}var Ne=class{constructor(t){this.options=t}options;browser;cdpEndpoint;stepIndex;get agent(){return this.options.agent}getStepIndex(){if(!this.stepIndex&&(this.stepIndex=G(this.options.projectRoot),this.stepIndex.steps.size>0)){let t=this.stepIndex.divergent.length>0?` \xB7 ${this.stepIndex.divergent.length} step(s) have divergent recordings (Level-2 signal)`:"";console.log(`step index: ${this.stepIndex.steps.size} reusable steps from ${this.stepIndex.scannedCaches} cache(s)${t}`)}return this.stepIndex}async recordWithSeeding(t,n){let r=this.getStepIndex(),s=t.steps.length,o={...t.params,...le(t.steps)},i=t.steps.map(x=>{let f=r.steps.size>0?Et(r,x,o):void 0;return f?{gherkin:x.text,keyword:x.keyword,kind:x.kind,table:x.table,docString:x.docString,fromStepSet:x.source?.fromStepSet,actions:f.actions}:void 0}),a=t.steps.map(x=>({gherkin:x.text,keyword:x.keyword,kind:x.kind,table:x.table,docString:x.docString,fromStepSet:x.source?.fromStepSet,actions:[]})),c=new Map,d=[],m=[],p=[],l=[],h={...ge},y=0,u=0,g=0,b=x=>({success:x,narrative:m.join(" ")||"Composed entirely from seeded recordings.",adaptations:d,suggestedFeatureEdit:l.length>0?l.join(`
|
|
39
|
+
`):void 0,featureEdits:p,recordedSteps:a,announcedSteps:[],usage:h,seededCount:y});for(;g<s;){if(i[g]){let $=g;for(;$<s&&i[$];)$++;let C=i.slice(g,$),S={...t,steps:t.steps.slice(g,$)},k=await ve(n,S,this.buildCandidateCache(t,C),{baseURL:this.options.baseURL,actionTimeoutMs:this.options.actionTimeoutMs,retries:this.options.retries,vars:c}),R=k.status==="passed"?$-g:k.failure?.stepIndex??0;for(let O=0;O<R;O++)a[g+O]=C[O];if(y+=R,k.status==="passed"){g=$;continue}let F=g+R;d.push(`Seeded recording for "${t.steps[F].text}" did not replay in this scenario's context; the agent re-recorded it fresh. Divergence signal.`),i[F]=void 0,g=F;continue}if(!this.options.agent)return b(!1);u++;let x=g;for(;x<s&&!i[x];)x++;u>=3&&(x=s);let f=await this.withDialogsVisible(()=>this.options.agent.run({scenario:t,baseURL:this.options.baseURL,mode:"record",cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:g>0?t.steps.slice(0,g).map($=>$.resolvedText):void 0,recordOnly:x<s?{from:g,to:x-1}:void 0,takeFingerprint:async()=>ae(we(this.browser)??n)}));if(h=vn(h,f.usage),m.push(f.narrative),d.push(...f.adaptations),p.push(...f.featureEdits),f.suggestedFeatureEdit&&l.push(f.suggestedFeatureEdit),!f.success)return b(!1);let w=f.announcedSteps.filter($=>$>=g&&$<s),v=Math.min(s-1,Math.max(w.length>0?Math.max(...w):x-1,g));for(let $=g;$<=v;$++)a[$]=f.recordedSteps[$];g=v+1}return b(!0)}async withDialogsVisible(t){let n=()=>{},r=[],s=[],o=i=>{i.on("dialog",n),r.push(i)};for(let i of this.browser?.contexts()??[]){i.on("page",o),s.push(i);for(let a of i.pages())o(a)}try{return await t()}finally{for(let i of s)i.off("page",o);for(let i of r)try{i.off("dialog",n)}catch{}}}async start(){let t=this.options.browser??"chromium";if(t==="chromium"){let n=await as();this.browser=await ss.launch({headless:!this.options.headed,args:[`--remote-debugging-port=${n}`]}),this.cdpEndpoint=`http://127.0.0.1:${n}`}else this.browser=await(t==="firefox"?os:is).launch({headless:!this.options.headed}),this.cdpEndpoint=void 0}async stop(){await this.browser?.close(),this.browser=void 0}resolveStorageState(){if(!this.options.storageState)return;let t=Sn.isAbsolute(this.options.storageState)?this.options.storageState:Sn.join(this.options.projectRoot,this.options.storageState);if(!yn.existsSync(t))throw new Error(`storageState file not found: ${t}. Generate one with: npx playwright open --save-storage=${this.options.storageState} <url>`);return t}buildCandidateCache(t,n){return{version:1,feature:t.featurePath,scenario:t.name,recordedAt:new Date().toISOString(),recordedBy:"agent",steps:n}}async proofReplay(t,n){let r=await this.browser.newContext({storageState:this.resolveStorageState()}),s=await r.newPage();return{result:await ve(s,t,n,{baseURL:this.options.baseURL,actionTimeoutMs:this.options.actionTimeoutMs,retries:this.options.retries}),context:r,page:s}}async runScenario(t,n={}){let r=await this.runScenarioInner(t,n),s=xe(t);if(s.size>0){let o=i=>i===void 0?i:ue(i,s);r.error=o(r.error),r.narrative=o(r.narrative),r.proofError=o(r.proofError),r.suggestedFeatureEdit=o(r.suggestedFeatureEdit),r.adaptations=r.adaptations.map(i=>ue(i,s));for(let i of r.stepResults)i.error=o(i.error)}return r}async runScenarioInner(t,n={}){if(!this.browser)throw new Error("Orchestrator not started");let r=Date.now(),{projectRoot:s,baseURL:o,actionTimeoutMs:i,retries:a}=this.options,c=V(s,t.featurePath,t.name),d=z(c),m=ye(s,t.featurePath,t.name),p=!d&&yn.existsSync(m)?fe(m):void 0,l=await this.browser.newContext({storageState:this.resolveStorageState()}),h=await l.newPage();try{let y;if(p){let f=await ve(h,t,p.cache,{baseURL:o,actionTimeoutMs:i,retries:a}),w=p.meta.recordedFor??t.name;if(f.status==="passed")return{scenario:t,status:"yellow",source:"agent-record",stepResults:f.stepResults,narrative:`Replayed the pending proposal recorded for "${w}" at zero AI; accept it to go green.`,adaptations:[],verified:!0,proposalFile:m,usage:ge,durationMs:Date.now()-r};if(Object.keys(t.params).length>0&&w!==t.displayName){let $=f.failure;return{scenario:t,status:"red",source:"agent-record",stepResults:f.stepResults,adaptations:[],proposalFile:m,usage:ge,durationMs:Date.now()-r,error:`The pending proposal recorded for "${w}" does not replay for this Examples row`+($?` (step "${$.stepText}": ${$.error.split(`
|
|
40
|
+
`)[0]})`:"")+". A row value is probably baked in as a literal instead of a <placeholder>; inspect the proposal, reject it, and re-record with saffron run --rerecord."}}console.log(` \u21BA pending proposal for "${t.displayName}" did not replay`+(f.failure?` (step "${f.failure.stepText}": ${f.failure.error.split(`
|
|
41
|
+
`)[0]})`:"")+"; recording again"),await h.goto("about:blank")}if(d&&(y=await ve(h,t,d,{baseURL:o,actionTimeoutMs:i,retries:a}),y.status==="passed"))return{scenario:t,status:"green",source:"cache",stepResults:y.stepResults,adaptations:[],usage:ge,durationMs:Date.now()-r};if(!(this.options.agent!==void 0&&this.cdpEndpoint!==void 0&&!n.agentDisabled)){let f=this.options.agent!==void 0&&this.cdpEndpoint===void 0&&!n.agentDisabled?`recording/healing requires Chromium (the agent attaches over CDP). Record with --browser chromium (the default), then replay with --browser ${this.options.browser}.`:"no agent is configured.";return{scenario:t,status:"red",source:"cache",stepResults:y?.stepResults??[],adaptations:[],usage:ge,durationMs:Date.now()-r,error:d?y?.status==="stale"?`Cache is stale (feature file changed) and ${f}`:`Replay failed and ${f} ${y?.failure?.error??""}`.trimEnd():`No cache exists and ${f}`}}let g=d!==void 0&&y!==void 0&&y.status==="failed",b=0,x;try{if(g)x=await this.withDialogsVisible(()=>this.options.agent.run({scenario:t,baseURL:o,mode:"heal",model:this.options.healModel,cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:y.stepResults.filter(f=>f.status==="passed").map(f=>f.gherkin),failedStep:{text:y.failure.stepText,kind:y.failure.kind,error:y.failure.error},takeFingerprint:async()=>{let f=we(this.browser)??h;return ae(f)}}));else if(await h.goto("about:blank"),this.options.reuseSteps!==!1){let f=await this.recordWithSeeding(t,h);b=f.seededCount,x=f}else x=await this.withDialogsVisible(()=>this.options.agent.run({scenario:t,baseURL:o,mode:"record",cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,takeFingerprint:async()=>{let f=we(this.browser)??h;return ae(f)}}))}catch(f){let w=f instanceof Error?f.message:String(f);return{scenario:t,status:"red",source:g?"agent-heal":"agent-record",stepResults:y?.stepResults??[],adaptations:[],usage:ge,durationMs:Date.now()-r,error:`Agent invocation failed: ${w.split(`
|
|
42
|
+
`)[0]}`}}if(x.success&&g&&y.failure.kind==="assertion"&&(this.options.assertionPolicy!=="adaptable-mid"||xn(d.steps,y.failure.stepIndex))){let f=d.steps[y.failure.stepIndex],w=we(this.browser)??h,v=new Map(Object.entries(y.captured)),$=at(t,y.failure.stepIndex);try{for(let C of f.actions)await it(w,C,$,this.options.actionTimeoutMs??5e3,o,v)}catch(C){let S=C instanceof Error?C.message:String(C);return{scenario:t,status:"red",source:"agent-heal",stepResults:y.stepResults,narrative:x.narrative,adaptations:x.adaptations,suggestedFeatureEdit:x.suggestedFeatureEdit,usage:x.usage,durationMs:Date.now()-r,error:`Assertion failed as written: "${f.gherkin}". Saffron never adapts assertions. If this change is intended, update the .feature file or re-record the scenario (saffron run --rerecord --filter @tag). If the recording itself looks wrong (an empty value, a wrong target), re-record it the same way. Verification error: ${S.split(`
|
|
43
|
+
`)[0]}`}}}return await this.finalizeAgentRun(t,x,g,d,r,b,g?y?.failure:void 0)}finally{await l.close()}}async finalizeAgentRun(t,n,r,s,o,i=0,a){let c=r?"agent-heal":"agent-record",d=n.recordedSteps.map(v=>({gherkin:v.gherkin,kind:v.kind,status:n.success?"passed":"failed"}));if(!n.success)return{scenario:t,status:"red",source:c,stepResults:d,narrative:n.narrative,adaptations:n.adaptations,usage:n.usage,durationMs:Date.now()-o,error:n.narrative};let m=r?bn(s.steps,n.recordedSteps,this.options.assertionPolicy==="adaptable-mid",a?{stepIndex:a.stepIndex,actionIndex:a.actionIndex}:void 0):n.recordedSteps,p=[...n.adaptations],l={...n.usage},h,y;if(this.options.verifyProposals!==!1){let v=await this.proofReplay(t,this.buildCandidateCache(t,m));try{if(v.result.status!=="passed"&&v.result.failure){let $=v.result.failure;if(this.options.agent&&!(r&&$.kind==="assertion"&&(this.options.assertionPolicy!=="adaptable-mid"||xn(s.steps,$.stepIndex))))try{let S=await this.withDialogsVisible(()=>this.options.agent.run({scenario:t,baseURL:this.options.baseURL,mode:"refine",model:r?this.options.healModel:void 0,cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:v.result.stepResults.filter(k=>k.status==="passed").map(k=>k.gherkin),failedStep:{text:$.stepText,kind:$.kind,error:$.error},takeFingerprint:async()=>ae(we(this.browser)??v.page)}));l=vn(l,S.usage),S.success&&(p.push(...S.adaptations),m=r?bn(m,S.recordedSteps,this.options.assertionPolicy==="adaptable-mid"):cs(m,S.recordedSteps),await v.context.close(),v=await this.proofReplay(t,this.buildCandidateCache(t,m)))}catch{}}h=v.result.status==="passed",h||(y=v.result.failure?.error??`proof replay ${v.result.status}`)}finally{await v.context.close().catch(()=>{})}}let u=[...n.featureEdits];for(let v of Pt(m,G(this.options.projectRoot)))p.push(`duplicate wording: "${v.from}" recorded exactly the same actions as the existing step "${v.to}" (${v.feature} \u203A ${v.scenario}). Accept with --with-feature-edit to converge on the canonical wording.`),u.some($=>$.index===v.index)||u.push({index:v.index,text:v.to});let g=xe(t),b=v=>g.size>0?ue(v,g):v;p=p.map(b);let x={meta:{createdAt:new Date().toISOString(),mode:r?"heal":"record",narrative:b(n.narrative),adaptations:p,suggestedFeatureEdit:n.suggestedFeatureEdit?b(n.suggestedFeatureEdit):void 0,featureEdits:u.length>0?u:void 0,verified:h,proofError:y?b(y):void 0,seededSteps:i>0?i:void 0,recordedFor:t.displayName,usage:l},cache:{version:1,feature:t.featurePath,scenario:t.name,recordedAt:new Date().toISOString(),recordedBy:"agent",steps:m}},f=dn(m);f.length>0&&(h=!1,y=[y,...f].filter(Boolean).join(" "));let w=ye(this.options.projectRoot,t.featurePath,t.name);return Mt(w,x),{scenario:t,status:"yellow",source:c,stepResults:d,narrative:n.narrative,adaptations:p,suggestedFeatureEdit:n.suggestedFeatureEdit,featureEdits:u.length>0?u:void 0,verified:h,proofError:y,seededSteps:i>0?i:void 0,proposalFile:w,usage:l,durationMs:Date.now()-o}}};import Pn from"node:path";import{createRequire as gs}from"node:module";import{query as ms}from"@anthropic-ai/claude-agent-sdk";import{createSdkMcpServer as ls,tool as me}from"@anthropic-ai/claude-agent-sdk";import{z as T}from"zod";var M=e=>({content:[{type:"text",text:e}]});function kn(e){let t={};return e.role&&(t.role=e.role),e.name&&(t.name=e.name),e.nameRegex&&(t.nameRegex=e.nameRegex),e.label&&(t.label=e.label),e.text&&(t.text=e.text),e.selector&&(t.fallbackSelectors=[e.selector]),e.frame&&(t.frame=e.frame),Object.keys(t).length>0?t:void 0}var $n={role:T.string().optional().describe("ARIA role of the element"),name:T.string().optional().describe("Accessible name \u2014 use the SHORTEST stable substring; never include volatile content like prices or dates"),nameRegex:T.string().optional().describe("Regex for the accessible name when it embeds volatile content, e.g. 'Select \\\\{date\\\\+1\\\\} as checkin' cannot work \u2014 instead use nameRegex 'Select .* as checkin'"),label:T.string().optional().describe("Form-label text for getByLabel \u2014 the literal <label> text only, never a free-text description"),text:T.string().optional().describe("getByText locator text"),selector:T.string().optional().describe("CSS fallback selector"),frame:T.string().optional().describe(`CSS selector of the iframe hosting the element, e.g. 'iframe[title="Feedback widget"]' \u2014 REQUIRED whenever the element lives inside an iframe; without it replay looks only at the top-level page`)};function Rn(e){return ls({name:"saffron",version:"0.1.0",tools:[me("saffron_step","Announce the Gherkin step you are about to perform. MUST be called before executing browser actions for each step, with the step's zero-based index and exact text.",{index:T.number().int().min(0),text:T.string(),adaptation:T.string().optional().describe("If the written step does not match the real UI and you are deviating from it, describe the deviation in one sentence.")},async t=>(e.currentStepIndex=t.index,e.announced.add(t.index),t.adaptation&&e.adaptations.push(`Step ${t.index+1} ("${t.text}"): ${t.adaptation}`),M(`Recording step ${t.index}: ${t.text}`))),me("saffron_capture",`Record a captureText action for the current step: at replay time, the target element's text is read and stored under saveAs for later expectDiffers comparisons. Use for steps like 'I record the displayed price as "X"'. The target must select exactly the element whose text is the value.`,{saveAs:T.string().describe("Name to store the captured text under"),...$n},async t=>{let n=kn(t);return n?(e.addActions([{action:"captureText",target:n,saveAs:t.saveAs}]),M(`Capture recorded as "${t.saveAs}".`)):M("ERROR: capture needs a target (role/name, nameRegex, label, text, or selector).")}),me("saffron_dialog","Record how a native dialog (alert/confirm/prompt) must be handled at replay. Call this IMMEDIATELY whenever an action triggers a native dialog \u2014 whether you handled it via browser_handle_dialog or the browser reported it as already handled. At replay Saffron arms this handler BEFORE the triggering action, so without this call the replay will auto-dismiss the dialog and diverge from what you saw.",{accept:T.boolean().describe("true = accept/OK the dialog, false = dismiss/cancel"),promptText:T.string().optional().describe("Text typed into a prompt() before accepting")},async t=>(e.addArmedAction({action:"handleDialog",value:t.accept?"accept":"dismiss",...t.promptText?{promptText:t.promptText}:{}}),M(`Dialog handling recorded (${t.accept?"accept":"dismiss"}).`))),me("saffron_wait_api","Record a network wait for the current step: replay will block until a matching API response is observed (responses from the triggering step count). Call AFTER you verified via browser_network_requests that the response actually occurred with the expected status/body. Use a path-only urlPattern regex and never literal volatile IDs (/api/orders/\\d+, not /api/orders/8842).",{urlPattern:T.string().describe("Regex matched against the response URL \u2014 path-only for portability"),method:T.string().optional().describe("HTTP method to match (any if omitted)"),status:T.string().optional().describe("Expected status \u2014 exact ('201') or class ('2xx'); default any 2xx"),bodyPattern:T.string().optional().describe(`Regex the response body must match, e.g. '"status"\\s*:\\s*"READY"' for polling waits`)},async t=>(e.addActions([{action:"waitForResponse",urlPattern:t.urlPattern,...t.method?{method:t.method}:{},...t.status?{status:t.status}:{},...t.bodyPattern?{pattern:t.bodyPattern}:{}}]),M("Network wait recorded."))),me("saffron_assert","Record a verified assertion for the current Then step. Call ONLY after you have confirmed the condition holds via a page snapshot. If the condition does NOT hold, do not call this \u2014 call saffron_done with success=false instead. NEVER record volatile content (prices, dates, counters) as literal expected text \u2014 use kind=matches with a pattern, or kind=differs against captured values.",{kind:T.enum(["visible","notVisible","text","url","value","differs","matches","attribute","response"]),...$n,expected:T.string().optional().describe("Expected text/value for kind=text|value \u2014 stable text only, never volatile values"),url:T.string().optional().describe("URL substring for kind=url (path only, no host)"),pattern:T.string().optional().describe("kind=matches|attribute: regex the target's text (or attribute) must match, e.g. 'NOK [\\\\d,]+' for a price"),attribute:T.string().optional().describe("kind=attribute: attribute name to check, e.g. 'href' for link-target assertions \u2014 use this for 'X should link to Y' instead of clicking through"),capture:T.string().optional().describe("kind=differs: name of the captured value on the LEFT of the comparison (omit when comparing the target element's live text)"),urlPattern:T.string().optional().describe("kind=response: regex matched against the response URL \u2014 path-only for portability, never literal volatile IDs (use /api/orders/\\d+)"),method:T.string().optional().describe("kind=response: HTTP method to match (any if omitted)"),status:T.string().optional().describe("kind=response: expected status \u2014 exact ('201') or class ('2xx'); default any 2xx"),bodyPattern:T.string().optional().describe("kind=response: regex the response body must match (verify via browser_network_requests first)"),compareTo:T.string().optional().describe("kind=differs: name of the captured value to compare against")},async t=>{let n=kn(t),r;switch(t.kind){case"visible":r={action:"expectVisible",target:n};break;case"notVisible":r={action:"expectNotVisible",target:n};break;case"text":r={action:"expectText",target:n,value:t.expected??""};break;case"value":r={action:"expectValue",target:n,value:t.expected??""};break;case"url":r={action:"expectUrl",url:t.url??""};break;case"differs":if(!t.compareTo)return M("ERROR: kind=differs requires compareTo.");if(!t.capture&&!n)return M("ERROR: kind=differs needs either capture (a stored name) or a target element.");r={action:"expectDiffers",target:n,value:t.capture,compareTo:t.compareTo};break;case"matches":if(!t.pattern)return M("ERROR: kind=matches requires pattern.");r={action:"expectMatches",target:n,pattern:t.pattern};break;case"attribute":if(!t.attribute||!t.pattern)return M("ERROR: kind=attribute requires attribute and pattern.");r={action:"expectAttribute",target:n,attribute:t.attribute,pattern:t.pattern};break;case"response":if(!t.urlPattern)return M("ERROR: kind=response requires urlPattern.");r={action:"expectResponse",urlPattern:t.urlPattern,...t.method?{method:t.method}:{},...t.status?{status:t.status}:{},...t.bodyPattern?{pattern:t.bodyPattern}:{}};break}return!["url","differs","response"].includes(t.kind)&&!n?M("ERROR: assertion needs a target (role/name, nameRegex, label, text, or selector)."):(e.addActions([r]),M("Assertion recorded."))}),me("saffron_done","Finalize the run. Call exactly once, after the final step: success=true only if every step's intent was achieved and every assertion verified.",{success:T.boolean(),narrative:T.string().describe("2-4 sentence summary of the run: what happened, and why it failed if it did."),suggestedFeatureEdit:T.string().optional().describe("If you adapted any steps: the corrected Gherkin for those steps, as you would rewrite them in the .feature file (free text, for the human report)."),featureEdits:T.array(T.object({index:T.number().int().min(0).describe("zero-based step index"),text:T.string().describe("replacement step text WITHOUT the Given/When/Then keyword")})).optional().describe("Machine-applicable version of the suggested edits: one entry per adapted step. Provide this whenever you adapted a step.")},async t=>(e.done=!0,e.success=t.success,e.narrative=t.narrative,e.suggestedFeatureEdit=t.suggestedFeatureEdit,e.featureEdits=t.featureEdits??[],M("Run finalized.")))]})}function Tn(e={}){let t=e.deliberateSnapshots?`
|
|
43
44
|
8. Tool responses do NOT include a page snapshot. Take snapshots deliberately with browser_snapshot, and only when you actually need to see the page: after navigation, before locating an element you haven't seen yet, and when verifying an assertion. Do NOT snapshot after actions whose outcome is predictable (filling a field you just located, pressing Enter, a click whose result you will verify later anyway). Every snapshot is expensive.
|
|
44
45
|
9. Minimize round-trips: batch tool calls in a single response whenever they don't depend on each other's results \u2014 saffron_step together with that step's browser actions, several fills in one turn, bookkeeping calls (saffron_capture, saffron_assert) alongside the actions they belong to. Only stop and wait when you genuinely need a result before deciding your next call. Do not write narration between tool calls.`:"";return`You are Saffron's test execution agent. You execute Gherkin scenarios in a real browser using the playwright MCP tools, while Saffron records your actions into a deterministic replay cache.
|
|
45
46
|
|
|
@@ -57,14 +58,15 @@ Non-negotiable rules:
|
|
|
57
58
|
5c. Real-world page furniture is supported: elements inside IFRAMES record and replay fine (interact normally) \u2014 but when a saffron_assert targets an element INSIDE an iframe, you MUST pass the iframe's CSS selector as the 'frame' argument (e.g. frame='iframe[title="Feedback widget"]'); replay cannot see into frames otherwise. File uploads: use browser_file_upload with the file path. Drag-and-drop: use browser_drag. All of these are recorded and replay deterministically.
|
|
58
59
|
5d. NATIVE DIALOGS (alert/confirm/prompt): when an action opens one, the page blocks and the snapshot reports a modal state \u2014 resolve it with browser_handle_dialog (accept true/false; promptText for prompt()). That call is recorded automatically as an armed handler replay places BEFORE the triggering action, so do NOT also call saffron_dialog. After handling, verify the post-dialog state as usual. If a step's wording says to confirm/accept, accept; if it says cancel/dismiss, dismiss.
|
|
59
60
|
5e. NETWORK WAITS: when a step says to wait for an API call ("wait for the order API to succeed", "until the job API reports READY"), do it verify-first: perform the trigger, check via browser_network_requests that the matching response actually occurred with the expected status/body, then record it with saffron_wait_api. For Then steps about API contracts ("the request should have returned 201") verify the same way and record with saffron_assert kind=response. urlPattern is a path-only regex \u2014 NEVER a literal volatile ID (/api/orders/\\d+, not /api/orders/8842) and never the host.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
5f. TABS: a click that opens a new tab needs nothing extra, replay follows the new tab by itself. When a step says to switch to another tab, go back to the first tab, or close a tab, do it with browser_tabs (list first if unsure of the index); select, close and new are recorded and replay in the same order. Steps that assert on the other tab's content are ordinary assertions after the switch.
|
|
62
|
+
7. Never ask the user questions; you are unattended.${t}`}function ds(e,t){if(e[t].kind!=="assertion")return"action";let n=e.length;for(let r=e.length-1;r>=0&&e[r].kind==="assertion";r--)n=r;return t>=n?"assertion:final":"assertion"}function An(e){let{scenario:t}=e,n=t.steps.map((s,o)=>{let i=p=>Se(p),a=`${o}. [${ds(t.steps,o)}] ${s.keyword} ${i(s.resolvedText)}`,c=s.resolvedTable??s.table,d=s.resolvedDocString??s.docString,m=a;return c&&(m+=`
|
|
63
|
+
${c.map(p=>` | ${p.map(i).join(" | ")} |`).join(`
|
|
64
|
+
`)}`),d!==void 0&&(m+=`
|
|
63
65
|
"""
|
|
64
|
-
${i(
|
|
65
|
-
`).map(
|
|
66
|
+
${i(d).split(`
|
|
67
|
+
`).map(p=>` ${p}`).join(`
|
|
66
68
|
`)}
|
|
67
|
-
"""`),
|
|
69
|
+
"""`),m}).join(`
|
|
68
70
|
`),r=[`Feature: ${t.featureName} (${t.featurePath})`,`Scenario: ${t.displayName}`,e.baseURL?`Base URL: ${e.baseURL}`:void 0,"","Steps (index. [kind] keyword text):",n,""];if(e.mode==="refine")r.push("MODE: REFINE. You (or a previous agent session) just recorded this scenario and it completed successfully \u2014 but the deterministic zero-AI replay of that recording FAILED at the step below. The application is working; the RECORDING is not replayable.","The browser tab shows the state where the proof replay stopped.","",`Steps that replayed fine:
|
|
69
71
|
${(e.completedSteps??[]).map(s=>` \u2713 ${s}`).join(`
|
|
70
72
|
`)||" (none)"}`,"",`Failed step: [${e.failedStep?.kind}] ${e.failedStep?.text}`,`Replay failure: ${e.failedStep?.error}`,"","Typical causes: a locator that only matched during your session (volatile accessible name \u2014 use nameRegex or a shorter stable name), a literal dynamic value baked into an assertion (use kind=matches/differs/captureText), or an assertion that cannot be checked without navigation (use kind=attribute for link targets).","Re-perform and re-record the failed step and every step after it in a REPLAYABLE form via saffron_step + browser tools / saffron_capture / saffron_assert. Express the SAME conditions the steps state \u2014 do not weaken them.","If a step genuinely cannot be expressed replayably, call saffron_done success=false and explain why.");else if(e.mode==="heal")r.push("MODE: HEAL. A cached deterministic replay of this scenario failed mid-run.","The browser tab already holds the page state reached so far \u2014 do NOT restart from step 0; verify where you are with a snapshot and continue from the failed step.","",`Steps already completed by replay:
|
|
@@ -73,25 +75,25 @@ ${(e.completedSteps??[]).map(s=>` \u2713 ${s}`).join(`
|
|
|
73
75
|
${e.completedSteps.map(i=>` \u2713 ${i}`).join(`
|
|
74
76
|
`)}`:"You are starting from a blank page.","",`Record ONLY steps ${s} through ${o} (inclusive).`,`CRITICAL STOP RULE: the steps AFTER step ${o} already have recordings and will be replayed automatically the moment you stop. After completing step ${o}, immediately call saffron_done \u2014 do NOT perform, verify, or navigate into any later step. Executing a later step yourself would make the automatic replay run it TWICE.`)}else if(e.completedSteps&&e.completedSteps.length>0){let s=e.completedSteps.length;r.push("MODE: RECORD (seeded). No cache exists for this scenario yet, but its first steps matched existing recordings and were ALREADY EXECUTED against the live browser by zero-AI replay:",e.completedSteps.map(o=>` \u2713 ${o}`).join(`
|
|
75
77
|
`),"",`The browser currently shows the state after those ${s} step(s) (verify with a snapshot if unsure). Do NOT redo them and do NOT navigate away from the current state.`,`Start with saffron_step for step index ${s} and record from there to the end.`)}else r.push("MODE: RECORD. No cache exists for this scenario yet.","Execute every step in order, starting at step 0. Navigate to the base URL first if the scenario implies a starting page.");return r.filter(s=>s!==void 0).join(`
|
|
76
|
-
`)}import{query as
|
|
77
|
-
`),a.set(
|
|
78
|
-
`));return o}import mt from"node:fs";import ht from"node:path";var I=e=>e.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',"""),
|
|
79
|
-
<path d="${
|
|
80
|
-
<path d="${
|
|
81
|
-
</svg>`}function
|
|
78
|
+
`)}import{query as ps}from"@anthropic-ai/claude-agent-sdk";var us="usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET";async function dt(e,t=8e3){let n=e?.[us];if(typeof n!="function")return;let r;try{let s=await Promise.race([n.call(e),new Promise(o=>{r=setTimeout(()=>o(void 0),t),r.unref?.()})]);return fs(s)}catch{return}finally{r&&clearTimeout(r)}}function fs(e){if(!e||!e.rate_limits_available||!e.subscription_type)return;let t=e.rate_limits?.five_hour;return{subscriptionType:e.subscription_type,utilization:typeof t?.utilization=="number"?t.utilization:void 0,resetsAt:t?.resets_at??void 0}}async function En(){let e=()=>{},t=new Promise(s=>e=s);async function*n(){await t}let r;try{let s=ps({prompt:n(),options:{maxTurns:1,tools:[],settingSources:[],persistSession:!1}});r=s;let o=(async()=>{try{for await(let a of s);}catch{}})(),i=await dt(s);return e(),r.close?.(),await o,i}catch{e(),r?.close?.();return}}var hs=gs(import.meta.url);function ys(){let e=hs.resolve("@playwright/mcp/package.json");return Pn.join(Pn.dirname(e),"cli.js")}var Ss=new Set(["mcp__playwright__browser_navigate","mcp__playwright__browser_click","mcp__playwright__browser_type","mcp__playwright__browser_fill_form","mcp__playwright__browser_select_option","mcp__playwright__browser_press_key"]),Ue=class{constructor(t={}){this.opts=t}opts;planBefore;async planUsage(){if(!this.planBefore)return;let t=await this.planBefore;if(!t)return;let n=await En();return{subscriptionType:t.subscriptionType,fiveHourBefore:t.utilization,fiveHourAfter:n?.utilization,resetsAt:n?.resetsAt??t.resetsAt}}async run(t){let n=new Me(t.scenario),r={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0},s="",o=ms({prompt:An(t),options:{systemPrompt:Tn({deliberateSnapshots:(this.opts.snapshotMode??"none")==="none",adaptableMidAssertions:t.assertionPolicy==="adaptable-mid"}),model:t.model??this.opts.model,maxTurns:this.opts.maxTurns??100,tools:[],permissionMode:"bypassPermissions",allowDangerouslySkipPermissions:!0,persistSession:!1,settingSources:[],mcpServers:{playwright:{type:"stdio",command:process.execPath,args:[ys(),"--cdp-endpoint",t.cdpEndpoint,"--snapshot-mode",this.opts.snapshotMode??"none","--image-responses","omit"]},saffron:Rn(n)},hooks:{PreToolUse:[{hooks:[async i=>{let a=i;if(Ss.has(a.tool_name)&&t.takeFingerprint&&!n.pendingFingerprint)try{n.pendingFingerprint=await t.takeFingerprint()}catch{}return{continue:!0}}]}],PostToolUse:[{hooks:[async i=>{let a=i,c=hn(a.tool_name,a.tool_input);c&&n.addArmedAction(c);let d=mn(a.tool_name,a.tool_input);return d&&n.addActions([d]),a.tool_name.startsWith("mcp__playwright__")&&(n.addActions(ln(a.tool_response,t.baseURL)),n.pendingFingerprint=void 0),{continue:!0}}]}]}}});this.planBefore||(this.planBefore=dt(o));try{for await(let i of o)i.type==="result"&&(r.aiCalls=i.num_turns,r.costUsd=i.total_cost_usd,"modelUsage"in i&&i.modelUsage&&(r.models=Object.keys(i.modelUsage)),"usage"in i&&i.usage&&(r.inputTokens=i.usage.input_tokens??0,r.outputTokens=i.usage.output_tokens??0,r.cacheReadTokens=i.usage.cache_read_input_tokens??0,r.cacheCreationTokens=i.usage.cache_creation_input_tokens??0),i.subtype==="success"&&(s=i.result))}catch(i){let a=i instanceof Error?i.message:String(i);n.done=!0,n.success=!1,n.narrative=`Agent session error: ${a}`}return n.done||(n.success=!1,n.narrative=n.narrative||`Agent session ended without finalizing the run. Last output: ${s.slice(0,500)}`),{success:n.success,narrative:n.narrative,adaptations:n.adaptations,suggestedFeatureEdit:n.suggestedFeatureEdit,featureEdits:n.featureEdits.map(i=>({index:i.index,text:lt(i.text,t.scenario.params)})),recordedSteps:gn(fn(pn(un(n.steps,t.scenario),t.scenario.params)),xe(t.scenario)),announcedSteps:[...n.announced].sort((i,a)=>i-a),usage:r}}};function Le(e){return`$${e.toFixed(e>=1?2:4)}`}function pt(e,t){let n=e===void 0?"?":`${Math.round(e)}%`,r=t===void 0?"?":`${Math.round(t)}%`;return`${n} \u2192 ${r}`}function Cn(e){return e.plan?` \xB7 5-hour window ${pt(e.plan.fiveHourBefore,e.plan.fiveHourAfter)} (${e.plan.subscriptionType} plan) \xB7 \u2248 ${Le(e.costUsd)} at API rates`:` \xB7 ${Le(e.costUsd)}`}function Fn(e,t){return e?t?`\u2248 ${Le(e)}`:Le(e):""}function jn(e){if(!e)return"";let t=new Date(e);return Number.isNaN(t.getTime())?"":`resets ${t.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})}`}import In from"node:fs";import Be from"node:path";function Ve(e){return JSON.stringify([e.role??null,e.name??null,e.nameRegex??null,e.label??null,e.text??null,e.testId??null,e.placeholder??null])}function _n(e,t){let n=new Map,r=Math.min(e.steps.length,t.steps.length);for(let s=0;s<r;s++){let o=e.steps[s].actions,i=t.steps[s].actions;if(o.length===i.length)for(let a=0;a<o.length;a++){let c=o[a],d=i[a];if(c.action!==d.action||!c.target||!d.target)continue;let m=Ve(c.target);m!==Ve(d.target)&&n.set(m,{from:c.target,to:d.target})}}return[...n.values()]}function xs(e){let t=Be.join(e,".saffron","cache");if(!In.existsSync(t))return[];let n=[];for(let r of In.readdirSync(t,{recursive:!0})){let s=String(r);s.endsWith(".json")&&n.push(Be.join(t,s))}return n}function On(e,t,n,r){if(t.length===0)return[];let s=new Map(t.map(i=>[Ve(i.from),i])),o=[];for(let i of xs(e)){if(Be.resolve(i)===Be.resolve(n))continue;let a=z(i);if(!a)continue;let c=[],d=!1;for(let m of a.steps){let p=0;for(let l of m.actions){if(!l.target)continue;let h=s.get(Ve(l.target));h&&(p++,r||(l.target=structuredClone(h.to),d=!0))}p>0&&c.push({gherkin:m.gherkin,count:p})}c.length>0&&(d&&pe(i,a),o.push({file:i,scenario:a.scenario,feature:a.feature,steps:c}))}return o}function ut(e){return e.role?`${e.role} "${e.name??e.nameRegex??""}"`:e.label?`label "${e.label}"`:e.text?`text "${e.text}"`:e.testId?`testId "${e.testId}"`:(e.fallbackSelectors??[]).join(",")||"(empty target)"}import Dn from"node:fs";import ft from"node:path";function Mn(e,t,n,r,s){let o={applied:[],skipped:[]},i=ee(ft.join(e,t),e,s).find(d=>d.name===n);if(!i){for(let d of r)o.skipped.push({index:d.index,reason:`scenario "${n}" not found in ${t}`});return o}let a=new Map,c=new Set;for(let d of r){let m=i.steps[d.index];if(!m?.source){o.skipped.push({index:d.index,reason:`no step at index ${d.index}`});continue}let{file:p,line:l,fromStepSet:h,fromBackground:y}=m.source;if(y){o.skipped.push({index:d.index,reason:`"${m.text}" is a Background step shared across scenarios; edit it manually`});continue}let u=`${p}:${l}`;if(c.has(u))continue;let g=a.get(p);g||(g=Dn.readFileSync(ft.join(e,p),"utf8").split(`
|
|
79
|
+
`),a.set(p,g));let b=g[l-1],x=b?.match(/^(\s*)(\S+)\s+(.*)$/);if(!x||x[3]!==m.text){o.skipped.push({index:d.index,reason:`${p}:${l} no longer matches the recorded step text`});continue}let f=`${x[1]}${x[2]} ${d.text}`;g[l-1]=f,c.add(u),o.applied.push({file:p,line:l,from:b.trim(),to:f.trim(),fromStepSet:h})}for(let[d,m]of a)o.applied.some(p=>p.file===d)&&Dn.writeFileSync(ft.join(e,d),m.join(`
|
|
80
|
+
`));return o}import mt from"node:fs";import ht from"node:path";var I=e=>e.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',"""),Un="M11 14 L10 4.5 L17.5 9.5 L24 2.5 L30.5 9.5 L38 4.5 L37 14 Z",bs="M24.085 45.646L24.085 45.646Q20.719 45.646 18.407 44.575Q16.095 43.504 14.905 41.651Q13.715 39.798 13.715 37.384L13.715 37.384L19.121 37.384Q19.121 38.336 19.648 39.152Q20.175 39.968 21.263 40.461Q22.351 40.954 24.085 40.954L24.085 40.954Q25.649 40.954 26.703 40.546Q27.757 40.138 28.301 39.407Q28.845 38.676 28.845 37.724L28.845 37.724Q28.845 36.534 27.825 35.837Q26.805 35.140 24.527 34.936L24.527 34.936L22.657 34.766Q18.951 34.460 16.741 32.454Q14.531 30.448 14.531 27.150L14.531 27.150Q14.531 24.770 15.670 23.036Q16.809 21.302 18.849 20.367Q20.889 19.432 23.677 19.432L23.677 19.432Q26.601 19.432 28.692 20.435Q30.783 21.438 31.905 23.257Q33.027 25.076 33.027 27.524L33.027 27.524L27.587 27.524Q27.587 26.606 27.145 25.841Q26.703 25.076 25.836 24.600Q24.969 24.124 23.677 24.124L23.677 24.124Q22.453 24.124 21.620 24.532Q20.787 24.940 20.379 25.637Q19.971 26.334 19.971 27.150L19.971 27.150Q19.971 28.204 20.719 28.986Q21.467 29.768 23.167 29.904L23.167 29.904L25.071 30.074Q27.791 30.312 29.865 31.247Q31.939 32.182 33.112 33.797Q34.285 35.412 34.285 37.724L34.285 37.724Q34.285 40.104 33.044 41.889Q31.803 43.674 29.525 44.660Q27.247 45.646 24.085 45.646Z";function vs(e){return`<svg viewBox="0 0 48 48" width="${e}" height="${e}" aria-hidden="true">
|
|
81
|
+
<path d="${Un}" fill="#F4A300"/>
|
|
82
|
+
<path d="${bs}" fill="#E7E9EC"/>
|
|
83
|
+
</svg>`}function ws(e){return`<svg viewBox="8 1 32 15" height="${e}" aria-hidden="true"><path d="${Un}" fill="#F4A300"/></svg>`}var ks={green:"passed",yellow:"pending review",red:"failed"};function ze(e){return e>=1e3?`${(e/1e3).toFixed(1)}s`:`${e}ms`}function ke(e){return e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e4?`${(e/1e3).toFixed(1)}k`:e.toLocaleString()}function Q(e,t,n,r="",s="",o=""){return`<div class="kpi">
|
|
82
84
|
<div class="kpi-label">${e}</div>
|
|
83
85
|
<div class="kpi-value ${r}">${t}</div>
|
|
84
86
|
<div class="kpi-sub ${s}">${n}</div>${o}
|
|
85
|
-
</div>`}function gt(e,t){if(e.length<2)return"";let n=108,r=22,s=Math.min(...e),i=Math.max(...e)-s||1,a=e.map((c,
|
|
87
|
+
</div>`}function gt(e,t){if(e.length<2)return"";let n=108,r=22,s=Math.min(...e),i=Math.max(...e)-s||1,a=e.map((c,d)=>`${(d/(e.length-1)*n).toFixed(1)},${(r-2-(c-s)/i*(r-4)).toFixed(1)}`).join(" ");return`<svg class="spark" viewBox="0 0 ${n} ${r}" width="${n}" height="${r}" aria-hidden="true"><polyline points="${a}" fill="none" stroke="${t}" stroke-width="1.8" stroke-linejoin="round" stroke-linecap="round"/></svg>`}var Nn=(e,t="",n=0)=>`${e>=0?"+":"\u2212"}${Math.abs(e).toFixed(n)}${t} vs prev`;function $s(e,t){let n=e.totals,r=e.trends,s=n.scenarios===0?0:Math.round((n.green+n.yellow)/n.scenarios*100),o=n.red>0?"red":n.yellow>0?"gold":"green",i=n.inputTokens+n.outputTokens,a=r?.prev?`${Nn(r.prev.passRatePp,"pp")} \xB7 ${n.green+n.yellow} of ${n.scenarios}`:`${n.green+n.yellow} of ${n.scenarios} scenarios`,c=r?.prev?`${Nn(r.prev.costUsd,"",4).replace("+","+$").replace("\u2212","\u2212$")} \xB7 ${ze(t)}`:`${ze(t)} total runtime`;return`<section class="kpis">
|
|
86
88
|
${Q("pass rate",`${s}<span class="unit">%</span>`,a,o,r?.prev?r.prev.passRatePp>=0?"green":"red":"",gt(r?.series.passRate??[],"var(--green)"))}
|
|
87
89
|
${Q("passed",String(n.green),"deterministic replay","green",n.green>0?"green":"")}
|
|
88
90
|
${Q("pending",String(n.yellow),n.yellow>0?"\u25B2 proposals to review":"nothing to review",n.yellow>0?"gold":"",n.yellow>0?"gold":"")}
|
|
89
91
|
${Q("failed",String(n.red),n.red>0?"\u25BC needs attention":"all clear",n.red>0?"red":"",n.red>0?"red":"green")}
|
|
90
92
|
${Q("ai calls",String(n.aiCalls),`${ke(i)} in+out tokens`,n.aiCalls>0?"":"green")}
|
|
91
93
|
${Q("cache traffic",ke(n.cacheReadTokens+n.cacheCreationTokens),n.cacheReadTokens+n.cacheCreationTokens>0?`${ke(n.cacheReadTokens)} read \xB7 ${ke(n.cacheCreationTokens)} written`:"zero (fully cached run)","",n.cacheReadTokens+n.cacheCreationTokens>0?"":"green")}
|
|
92
|
-
${n.plan?Q("plan usage",`${pt(n.plan.fiveHourBefore,n.plan.fiveHourAfter)}`,[`5-hour window \xB7 ${n.plan.subscriptionType} plan`,
|
|
93
|
-
</section>`}function
|
|
94
|
-
`)}</section>`:""}function
|
|
94
|
+
${n.plan?Q("plan usage",`${pt(n.plan.fiveHourBefore,n.plan.fiveHourAfter)}`,[`5-hour window \xB7 ${n.plan.subscriptionType} plan`,jn(n.plan.resetsAt)].filter(Boolean).join(" \xB7 "),"gold","")+Q("api-equivalent",`<span class="unit">\u2248 $</span>${n.costUsd.toFixed(n.costUsd>=1?2:4)}`,`what this run costs on an API key \xB7 ${c}`,"","",gt(r?.series.costUsd??[],"var(--gold)")):Q("ai cost",`<span class="unit">$</span>${n.costUsd.toFixed(n.costUsd>=1?2:4)}`,c,"","",gt(r?.series.costUsd??[],"var(--gold)"))}
|
|
95
|
+
</section>`}function Rs(e){let t=e.trends;if(!t)return"";let n=[];return t.chronic.length>0&&n.push(`<div class="panel accent-gold"><div class="panel-title gold">chronic scenarios: stop paying for heals</div><ul>${t.chronic.map(r=>`<li>${I(r.feature)} \u203A ${I(r.scenario)}: ${r.heals} heal(s), ${r.reds} red(s) in the last ${r.runs} run(s). Re-record it or fix the feature text.</li>`).join("")}</ul></div>`),t.divergentSteps>0&&n.push(`<div class="panel"><div class="panel-title">step divergence (level-2 signal)</div><p>${t.divergentSteps} step text(s) currently have divergent recordings across caches.</p></div>`),t.topIssues.length>0&&n.push(`<div class="panel accent-red"><div class="panel-title red">recurring failure themes (last 20 runs)</div><ul>${t.topIssues.map(r=>`<li>${r.count}\xD7: ${I(r.message)}</li>`).join("")}</ul></div>`),n.length>0?`<section class="trends">${n.join(`
|
|
96
|
+
`)}</section>`:""}function Ts(e){return`<ol class="steps">${e.steps.map(n=>{let r=n.status==="passed"?'<span class="dot green"></span>':n.status==="failed"?'<span class="dot red"></span>':'<span class="dot idle"></span>';return`<li class="step ${n.status}">
|
|
95
97
|
${r}
|
|
96
98
|
<div class="step-body">
|
|
97
99
|
<span class="step-text">${I(n.gherkin)}</span>
|
|
@@ -102,13 +104,13 @@ ${e.completedSteps.map(i=>` \u2713 ${i}`).join(`
|
|
|
102
104
|
${n.error?`<div class="step-error">${I(n.error)}</div>`:""}
|
|
103
105
|
</div>
|
|
104
106
|
</li>`}).join(`
|
|
105
|
-
`)}</ol>`}function
|
|
107
|
+
`)}</ol>`}function As(e){return e.featureEdits&&e.featureEdits.length>0?`<div class="panel">
|
|
106
108
|
<div class="panel-title gold">suggested .feature edit</div>
|
|
107
109
|
<div class="diff">${e.featureEdits.map(n=>{let r=e.steps[n.index]?.gherkin??`step ${n.index}`;return`<div class="diff-line del">\u2212 ${I(r)}</div><div class="diff-line add">+ ${I(n.text)}</div>`}).join("")}</div>
|
|
108
110
|
<div class="panel-hint">apply with <code>saffron accept --with-feature-edit</code></div>
|
|
109
|
-
</div>`:e.suggestedFeatureEdit?`<div class="panel"><div class="panel-title gold">suggested .feature edit</div><pre>${I(e.suggestedFeatureEdit)}</pre></div>`:""}function Es(e,t){let n=e.usage.inputTokens+e.usage.outputTokens;if(e.usage.aiCalls===0)return'<span class="pill green">0 tokens</span>';let r=
|
|
111
|
+
</div>`:e.suggestedFeatureEdit?`<div class="panel"><div class="panel-title gold">suggested .feature edit</div><pre>${I(e.suggestedFeatureEdit)}</pre></div>`:""}function Es(e,t){let n=e.usage.inputTokens+e.usage.outputTokens;if(e.usage.aiCalls===0)return'<span class="pill green">0 tokens</span>';let r=Fn(e.usage.costUsd,t),s=r?` \xB7 ${r}`:"";return`<span class="pill gold">${e.usage.aiCalls} ai \xB7 ${ke(n)} tok${s}</span>`}function Ps(e,t){return`<details class="card" ${e.status!=="green"?"open":""}>
|
|
110
112
|
<summary>
|
|
111
|
-
<span class="pill ${e.status==="green"?"green":e.status==="yellow"?"gold":"red"}">${
|
|
113
|
+
<span class="pill ${e.status==="green"?"green":e.status==="yellow"?"gold":"red"}">${ks[e.status]}</span>
|
|
112
114
|
<span class="card-title">${I(e.scenario)}</span>
|
|
113
115
|
<span class="card-meta">
|
|
114
116
|
<span class="m">${I(e.feature)}</span>
|
|
@@ -120,15 +122,15 @@ ${e.completedSteps.map(i=>` \u2713 ${i}`).join(`
|
|
|
120
122
|
</span>
|
|
121
123
|
</summary>
|
|
122
124
|
<div class="card-body">
|
|
123
|
-
${
|
|
125
|
+
${Ts(e)}
|
|
124
126
|
${e.narrative?`<div class="panel"><div class="panel-title">agent narrative</div><p>${I(e.narrative)}</p></div>`:""}
|
|
125
127
|
${e.adaptations.length>0?`<div class="panel accent-gold"><div class="panel-title gold">adaptations</div><ul>${e.adaptations.map(n=>`<li>${I(n)}</li>`).join("")}</ul></div>`:""}
|
|
126
|
-
${
|
|
128
|
+
${As(e)}
|
|
127
129
|
${e.proposalFile?`<div class="panel"><div class="panel-title">cache proposal pending${e.verified===!0?' <span class="pill green">verified</span>':e.verified===!1?' <span class="pill red">unverified</span>':""}</div><p class="mono">${I(e.proposalFile)}</p>${e.verified===!1&&e.proofError?`<p class="mono" style="color:var(--red)">proof replay failed: ${I(e.proofError.split(`
|
|
128
130
|
`)[0])}</p>`:""}<div class="panel-hint">review with <code>saffron accept</code> / <code>saffron reject</code></div></div>`:""}
|
|
129
131
|
${e.error&&e.status==="red"?`<div class="panel accent-red"><div class="panel-title red">failure</div><p>${I(e.error)}</p></div>`:""}
|
|
130
132
|
</div>
|
|
131
|
-
</details>`}function
|
|
133
|
+
</details>`}function Ln(e){let t=e.totals,n=new Date(e.startedAt),r=new Date(e.finishedAt).getTime()-n.getTime(),s=n.toLocaleString("en-GB",{day:"2-digit",month:"short",year:"numeric",hour:"2-digit",minute:"2-digit"});return`<!doctype html>
|
|
132
134
|
<html lang="en">
|
|
133
135
|
<head>
|
|
134
136
|
<meta charset="utf-8"/>
|
|
@@ -248,7 +250,7 @@ ${e.completedSteps.map(i=>` \u2713 ${i}`).join(`
|
|
|
248
250
|
<div class="wrap">
|
|
249
251
|
<header class="top">
|
|
250
252
|
<div class="brand">
|
|
251
|
-
${
|
|
253
|
+
${vs(38)}
|
|
252
254
|
<div>
|
|
253
255
|
<div class="brand-name">saffron</div>
|
|
254
256
|
<div class="brand-sub">test run report</div>
|
|
@@ -262,43 +264,43 @@ ${e.completedSteps.map(i=>` \u2713 ${i}`).join(`
|
|
|
262
264
|
</div>
|
|
263
265
|
</header>
|
|
264
266
|
|
|
265
|
-
${
|
|
266
|
-
${
|
|
267
|
+
${$s(e,r)}
|
|
268
|
+
${Rs(e)}
|
|
267
269
|
|
|
268
270
|
<div class="section-head">
|
|
269
271
|
<span class="section-title">scenarios \xB7 ${t.scenarios}</span>
|
|
270
272
|
<span class="section-title">saffron v${I(e.version)}</span>
|
|
271
273
|
</div>
|
|
272
|
-
${e.scenarios.map(o=>
|
|
274
|
+
${e.scenarios.map(o=>Ps(o,!!e.totals.plan)).join(`
|
|
273
275
|
`)}
|
|
274
276
|
|
|
275
|
-
<footer>${
|
|
277
|
+
<footer>${ws(11)} generated by saffron v${I(e.version)} \xB7 ${I(new Date(e.finishedAt).toLocaleString("en-GB",{dateStyle:"medium",timeStyle:"medium"}))}</footer>
|
|
276
278
|
</div>
|
|
277
279
|
</body>
|
|
278
280
|
</html>
|
|
279
|
-
`}function
|
|
280
|
-
`),mt.writeFileSync(s,
|
|
281
|
-
`)}function
|
|
281
|
+
`}function Bn(e,t,n){let r={scenarios:e.length,green:0,yellow:0,red:0,aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0},s=new Set;for(let o of e){for(let i of o.usage.models??[])s.add(i);r[o.status]+=1,r.aiCalls+=o.usage.aiCalls,r.inputTokens+=o.usage.inputTokens,r.outputTokens+=o.usage.outputTokens,r.cacheReadTokens+=o.usage.cacheReadTokens,r.cacheCreationTokens+=o.usage.cacheCreationTokens??0,r.costUsd+=o.usage.costUsd??0}return s.size>0&&(r.models=[...s].sort()),{tool:"saffron",version:te(),startedAt:t.toISOString(),finishedAt:new Date().toISOString(),baseURL:n,totals:r,scenarios:e.map(o=>({feature:o.scenario.featurePath,scenario:o.scenario.displayName,tags:[...o.scenario.tags],status:o.status,source:o.source,durationMs:o.durationMs,usage:o.usage,steps:o.stepResults,narrative:o.narrative,adaptations:o.adaptations,suggestedFeatureEdit:o.suggestedFeatureEdit,featureEdits:o.featureEdits,verified:o.verified,proofError:o.proofError,proposalFile:o.proposalFile,error:o.error}))}}function Vn(e,t){let n=ht.join(e,".saffron","reports");mt.mkdirSync(n,{recursive:!0});let r=ht.join(n,"latest.json"),s=ht.join(n,"latest.html");return mt.writeFileSync(r,`${JSON.stringify(t,null,2)}
|
|
282
|
+
`),mt.writeFileSync(s,Ln(t)),{json:r,html:s}}function zn(e){let t=e===void 0?[]:Array.isArray(e)?e:[e],n=new Set;for(let r of t)for(let s of r.split(/[\s,|]+/)){let o=s.trim();o&&n.add(o.startsWith("@")?o:`@${o}`)}return[...n]}function qn(e,t){return t.length===0?e:e.filter(n=>n.tags.some(r=>t.includes(r)))}function Hn(e,t){return[...t??[],e]}import A from"node:fs";import j from"node:path";var $e={claude:".claude/skills",agents:".agents/skills",copilot:".github/skills",cursor:".cursor/skills"},Cs=["claude","agents"],Jn="<!-- saffron:start -->",St="<!-- saffron:end -->";function Fs(){return[Jn,"## Saffron end-to-end tests","","UI tests are plain-Gherkin `.feature` / `.saffron` files under `features/`,","run by Saffron (`npx saffron run`). No step definitions exist: an AI agent","records each scenario once; later runs replay at zero tokens.","","- Load the `saffron` skill before writing or editing scenarios.","- Exact step text is the cache identity: run `npx saffron steps` and reuse"," recorded wordings instead of inventing synonyms.","- `Then` steps are never healed: write them as the verdict, last.","- `npx saffron status` shows what is cached, what awaits review, the tags"," and vocabulary health; editing a cached scenario's text re-records it.","- Never edit `.saffron/cache/` or `.saffron/proposals/` by hand; change the"," scenario text and re-run. Review proposals with `npx saffron accept`"," (`--all`, or chosen files) and `npx saffron reject`.","- Secrets go in `{env:VAR}` tokens, never in files.",St,""].join(`
|
|
283
|
+
`)}function Wn(e,t){if(!A.existsSync(e))return A.writeFileSync(e,t),"created";let n=A.readFileSync(e,"utf8"),r=n.indexOf(Jn),s=n.indexOf(St);if(r!==-1&&s!==-1&&s>r){let i=n.slice(0,r)+t.trimEnd()+n.slice(s+St.length);return i===n?"unchanged":(A.writeFileSync(e,i),"updated")}let o=n.endsWith(`
|
|
282
284
|
`)?`
|
|
283
285
|
`:`
|
|
284
286
|
|
|
285
|
-
`;return
|
|
286
|
-
`),s?"updated":"created")}function
|
|
287
|
-
`),n.scaffolded.push("saffron.config.json"));let o=
|
|
288
|
-
`).some(
|
|
287
|
+
`;return A.writeFileSync(e,n+o+t),"updated"}var yt={command:"npx",args:["saffron","mcp"]},js={claude:{file:".mcp.json",key:"mcpServers",entry:yt},cursor:{file:".cursor/mcp.json",key:"mcpServers",entry:yt},copilot:{file:".vscode/mcp.json",key:"servers",entry:{type:"stdio",...yt}}},Gn={agents:"codex mcp add saffron -- npx saffron mcp (Codex; other hosts: see their MCP settings)"};function Is(e,t){let n=j.join(e,t.file),r={},s=A.existsSync(n);if(s)try{r=JSON.parse(A.readFileSync(n,"utf8"))}catch{throw new Error(`${t.file} is not valid JSON: fix it or remove it, then re-run`)}let o=r[t.key]??{};return JSON.stringify(o.saffron)===JSON.stringify(t.entry)?"unchanged":(o.saffron=t.entry,r[t.key]=o,A.mkdirSync(j.dirname(n),{recursive:!0}),A.writeFileSync(n,JSON.stringify(r,null,2)+`
|
|
288
|
+
`),s?"updated":"created")}function Yn(e,t){A.mkdirSync(t,{recursive:!0});for(let n of A.readdirSync(e,{withFileTypes:!0})){let r=j.join(e,n.name),s=j.join(t,n.name);n.isDirectory()?Yn(r,s):A.copyFileSync(r,s)}}function Kn(e){let t=j.join(e.packageRoot,"skills","saffron");if(!A.existsSync(j.join(t,"SKILL.md")))throw new Error(`bundled skill not found at ${t}`);let n={skillDirs:[],instructionFiles:[],scaffolded:[],mcpFiles:[],mcpHints:[]},r=e.targets??Cs;for(let s of r){let o=j.join(e.projectRoot,$e[s],"saffron");A.existsSync(o)&&A.rmSync(o,{recursive:!0,force:!0}),Yn(t,o),n.skillDirs.push(j.relative(e.projectRoot,o))}if(e.mcp!==!1)for(let s of r){let o=js[s];o?n.mcpFiles.push({file:o.file,outcome:Is(e.projectRoot,o)}):Gn[s]&&n.mcpHints.push(Gn[s])}if(e.instructions!==!1){let s=Fs(),o=j.join(e.projectRoot,"AGENTS.md");n.instructionFiles.push({file:"AGENTS.md",outcome:Wn(o,s)});let i=j.join(e.projectRoot,"CLAUDE.md");A.existsSync(i)&&n.instructionFiles.push({file:"CLAUDE.md",outcome:Wn(i,s)})}if(e.examples){n.examples=_s(e.projectRoot,e.packageRoot);for(let s of n.examples.files)n.scaffolded.push(s)}if(e.scaffold!==!1){let s=j.join(e.projectRoot,"saffron.config.json");A.existsSync(s)||(A.writeFileSync(s,JSON.stringify({baseURL:"http://localhost:3000",features:"features"},null,2)+`
|
|
289
|
+
`),n.scaffolded.push("saffron.config.json"));let o=j.join(e.projectRoot,"features");A.existsSync(o)||(A.mkdirSync(o,{recursive:!0}),n.scaffolded.push("features/"));let i=j.join(e.projectRoot,".gitignore"),a=e.examples?[".saffron/reports/",".env"]:[".saffron/reports/"],c=A.existsSync(i)?A.readFileSync(i,"utf8"):"",d=[];for(let m of a)c.split(`
|
|
290
|
+
`).some(p=>p.trim()===m)||(c+=(c&&!c.endsWith(`
|
|
289
291
|
`)?`
|
|
290
|
-
`:"")+
|
|
291
|
-
`,
|
|
292
|
-
`)}function
|
|
293
|
-
`))}async function
|
|
294
|
-
`)[0]}`),S.error&&console.log(` ${S.error}`)};await c.start();try{if(
|
|
295
|
-
${f.green} passed, ${f.yellow} pending review, ${f.red} failed \xB7 ${f.aiCalls} AI calls \xB7 ${(f.inputTokens+f.outputTokens).toLocaleString()} tokens in+out${
|
|
296
|
-
${x}`);let
|
|
292
|
+
`:"")+m+`
|
|
293
|
+
`,d.push(m));d.length>0&&(A.writeFileSync(i,c),n.scaffolded.push(`.gitignore (+ ${d.join(", ")})`))}return n}function _s(e,t){let n=j.join(t,"templates","saucedemo");if(!A.existsSync(j.join(n,"features")))return{installed:!1,files:[],reason:`bundled examples not found at ${n}`};let r=j.join(e,"features");if(A.existsSync(r)&&A.readdirSync(r).length>0)return{installed:!1,files:[],reason:"features/ is not empty; examples are only installed into an empty features directory"};let s=[];A.mkdirSync(r,{recursive:!0});for(let a of A.readdirSync(j.join(n,"features")))A.copyFileSync(j.join(n,"features",a),j.join(r,a)),s.push(`features/${a}`);let o=j.join(e,"saffron.config.json");A.existsSync(o)||(A.copyFileSync(j.join(n,"saffron.config.json"),o),s.push("saffron.config.json (baseURL https://www.saucedemo.com, retries 1)"));let i=j.join(e,".env.example");return A.existsSync(i)||(A.copyFileSync(j.join(n,".env.example"),i),s.push(".env.example")),{installed:!0,files:s}}import Os from"node:path";function Qn(e){return["saffron lsp is a language server: it speaks LSP over stdio and is meant","to be launched by your editor, not run by hand (nothing happens here).","","JetBrains (IntelliJ, WebStorm, PyCharm, Rider, incl. Community):"," Easiest: install the Saffron plugin from the JetBrains Marketplace"," (https://plugins.jetbrains.com/plugin/34240-saffron): highlighting,"," this language server, run configurations and the Saffron tool window,"," in one click. Manual setup instead:"," 1. Completion, diagnostics, hover, go-to-definition: install the free"," LSP4IJ plugin, then Settings \u2192 Languages & Frameworks \u2192 Language"," Servers \u2192 +: command `npx saffron lsp`, working dir = project root,"," file patterns *.saffron and *.feature."," 2. Syntax highlighting comes from a TextMate bundle, not the LSP:"," Settings \u2192 Editor \u2192 TextMate Bundles \u2192 + and pick this folder:",` ${e?Os.join(e,"textmate","saffron"):"node_modules/saffron-ai/textmate/saffron"}`,"","Neovim / other LSP editors: cmd = { 'npx', 'saffron', 'lsp' },","filetypes = { 'saffron', 'feature' }.","","VS Code: install the extension instead:"," code --install-extension ChathurangaJayasinghe.saffron-vscode"].join(`
|
|
294
|
+
`)}function Us(e,t){let n=t.length>0?t:["features"],r=[];for(let s of n){let o=E.isAbsolute(s)?s:E.join(e,s);if(D.existsSync(o))if(D.statSync(o).isDirectory())for(let i of D.readdirSync(o,{recursive:!0})){let a=String(i);se(a)&&r.push(E.join(o,a))}else se(o)&&r.push(o)}return r.sort()}function Xn(e,t,n){let r=E.isAbsolute(t.features??"features")?t.features:E.join(e,t.features??"features"),s=new Set(de(r));for(let o of n)o.endsWith(".saffron")&&s.add(o);return Z([...s].sort(),e)}function Ls(){let e=process.env.ANTHROPIC_API_KEY||process.env.CLAUDE_CODE_OAUTH_TOKEN||process.env.CLAUDE_CODE_USE_BEDROCK||process.env.CLAUDE_CODE_USE_VERTEX,t=process.env.HOME??"",n=t&&(D.existsSync(E.join(t,".claude",".credentials.json"))||D.existsSync(E.join(t,".claude.json")));e||n||console.error(["note: no Claude credentials detected. Cached scenarios replay fine","without AI, but recording and healing need one of:"," \xB7 ANTHROPIC_API_KEY=sk-ant-... (console.anthropic.com \u2192 API keys)"," \xB7 a Claude Code login on this machine","Replay-only runs: add --no-agent to silence this note.",""].join(`
|
|
295
|
+
`))}async function Bs(e){let t=Ms.createInterface({input:process.stdin,output:process.stdout});try{let n=await new Promise(r=>t.question(e,r));return/^y(es)?$/i.test(n.trim())}finally{t.close()}}function qe(e){console.error(`saffron: ${e.message}`),process.exit(2)}function xt(e){return e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e4?`${(e/1e3).toFixed(1)}k`:e.toLocaleString()}var Vs={green:"\x1B[32m",yellow:"\x1B[33m",red:"\x1B[31m"},zs="\x1B[0m",_=new Ns().name("saffron").description("Gherkin-native AI-fallback test runner: zero-token cached replay, runtime AI healing.").version(te()).option("-p, --project-root <dir>","project root",process.cwd());_.command("run").description("Run feature files (cached replay; AI records/heals on demand)").argument("[paths...]","feature files or directories (default: features/)").option("--base-url <url>","base URL of the app under test").option("--headed","run the browser headed").option("--filter <tags>",'only run scenarios carrying any of these @tags: comma-separated ("@smoke,@WA-TC1") or repeat the flag',Hn).option("--no-agent","disable AI fallback (cache misses/failures go red)").option("--rerecord","discard the committed caches of the selected scenarios and let the agent record them fresh (the honest fix for a wrong recording)").option("--model <model>","model for the agent").option("--storage-state <file>","Playwright storageState JSON for authenticated sessions").option("--strict","treat passed-with-adaptation (yellow) as failure until its proposal is reviewed").option("--no-verify","skip the zero-AI proof replay of agent recordings before filing proposals").option("--no-reuse","disable Level-1 step reuse (seeding new recordings from existing step recordings)").option("--browser <name>",'replay browser: "chromium" (default), "firefox", or "webkit", recording/healing always use Chromium').option("--heal-model <model>","cheaper model for heal sessions").option("--workers <n>","parallel replay workers (agent recording/healing stays sequential)").option("--assertion-policy <policy>",'assertion adaptability: "strict" (default) or "adaptable-mid" (mid-scenario assertions may be adapted; the final assertion block never is)').action(async(e,t)=>{let n=E.resolve(_.opts().projectRoot),r=H(n),s=t.baseUrl??r.baseURL,o=Us(n,e.length>0?e:r.features?[r.features]:[]);o.length===0&&(console.error("No .feature/.saffron files found."),process.exit(2)),t.agent!==!1&&Ls();let i;try{let S=Xn(n,r,o);i=o.flatMap(k=>ee(k,n,S))}catch(S){throw S instanceof N&&qe(S),S}let a=zn(t.filter);if(a.length>0&&(i=qn(i,a),i.length===0&&(console.error(`No scenarios match --filter ${a.join(",")} in ${o.length} feature file(s).`),process.exit(2))),t.rerecord){t.agent===!1&&(console.error("saffron: --rerecord needs the agent; drop --no-agent"),process.exit(2));let S=0;for(let k of i){let R=V(n,k.featurePath,k.name);D.existsSync(R)&&(D.rmSync(R),S++)}console.log(`rerecord: removed ${S} cache file(s) for ${i.length} scenario(s); the agent records them fresh (seeded from the other recordings). Restore with git if needed.`)}{let S=new Map;for(let k of i){let R=Ke(k),F=z(V(n,k.featurePath,k.name));F&&R.push(JSON.stringify(F.steps));for(let O of Ye(R))process.env[O]===void 0&&!S.has(O)&&S.set(O,k.displayName)}if(S.size>0){for(let[k,R]of S)console.error(`missing environment variable ${k} (referenced as {env:${k}} by "${R}")`);console.error("set the variable(s), or add them to a git-ignored .env in the project root"),process.exit(2)}}let c=new Ne({projectRoot:n,baseURL:s,headed:t.headed,actionTimeoutMs:r.actionTimeoutMs,retries:r.retries,storageState:t.storageState??r.storageState,browser:(()=>{let S=t.browser??r.browser??"chromium";return S!=="chromium"&&S!=="firefox"&&S!=="webkit"&&(console.error(`invalid --browser "${S}": use "chromium", "firefox", or "webkit"`),process.exit(2)),S})(),healModel:t.healModel??r.healModel,verifyProposals:t.verify===!1?!1:r.verifyProposals,reuseSteps:t.reuse===!1?!1:r.reuseSteps,assertionPolicy:(()=>{let S=t.assertionPolicy??r.assertionPolicy??"strict";return S!=="strict"&&S!=="adaptable-mid"&&(console.error(`invalid --assertion-policy "${S}": use "strict" or "adaptable-mid"`),process.exit(2)),S})(),agent:t.agent?new Ue({model:t.model??r.model,maxTurns:r.maxTurns,snapshotMode:r.snapshotMode}):void 0}),d=new Date,m=Math.max(1,Number.parseInt(String(t.workers??r.workers??1),10)||1),p=new Array(i.length),l=S=>{let k=S.scenario,R=Vs[S.status],F=S.usage.inputTokens+S.usage.outputTokens,O=S.usage.cacheReadTokens+S.usage.cacheCreationTokens,P=O>0?`, ${xt(O)} cache traffic`:"";console.log(`${R}${S.status.toUpperCase().padEnd(7)}${zs}${k.displayName} (${S.source}, ${S.durationMs}ms, ${S.usage.aiCalls} AI calls, ${F} tokens${P})`);for(let B of S.adaptations)console.log(` \u26A0 ${B}`);S.seededSteps&&console.log(` \u26A1 ${S.seededSteps}/${S.stepResults.length} steps seeded from existing recordings (zero AI)`);let X=S.stepResults.reduce((B,er)=>B+(er.retries??0),0);X&&console.log(` \u21BB ${X} action ${X===1?"retry":"retries"} absorbed before settling (retries: ${r.retries??1} per action)`),S.verified===!0?console.log(" \u2713 proposal verified by zero-AI proof replay"):S.verified===!1&&console.log(` \u2717 proposal UNVERIFIED, proof replay failed: ${S.proofError?.split(`
|
|
296
|
+
`)[0]}`),S.error&&console.log(` ${S.error}`)};await c.start();try{if(m>1&&i.length>1){let S=i.map((R,F)=>({s:R,i:F})),k=[];await Promise.all(Array.from({length:Math.min(m,S.length)},async()=>{for(;;){let R=S.shift();if(!R)return;let F=await c.runScenario(R.s,{agentDisabled:!0});F.status==="red"?k.push(R):(p[R.i]=F,l(F))}}));for(let R of k.sort((F,O)=>F.i-O.i)){let F=await c.runScenario(R.s);p[R.i]=F,l(F)}}else for(let S=0;S<i.length;S++){let k=await c.runScenario(i[S]);p[S]=k,l(k)}}finally{await c.stop()}let h=Bn(p,d,s),y=G(n).divergent.length,u=Vt(p,d,y,s),g=Ie(n);h.trends=qt(g,u),zt(n,u),h.totals.aiCalls>0&&(h.totals.plan=await c.agent?.planUsage?.());let{html:b,json:x}=Vn(n,h),f=h.totals,w=f.cacheReadTokens+f.cacheCreationTokens;console.log(`
|
|
297
|
+
${f.green} passed, ${f.yellow} pending review, ${f.red} failed \xB7 ${f.aiCalls} AI calls \xB7 ${(f.inputTokens+f.outputTokens).toLocaleString()} tokens in+out${w>0?` \xB7 ${xt(f.cacheReadTokens)} cache read \xB7 ${xt(f.cacheCreationTokens)} cache written`:""}${Cn(f)}${f.models?.length?` \xB7 ${f.models.join(", ")}`:""}`);let v=h.trends;if(v?.prev){let S=v.prev.passRatePp,k=v.prev.costUsd;console.log(`vs previous run: pass rate ${S>=0?"+":""}${S}pp \xB7 cost ${k>=0?"+":"-"}$${Math.abs(k).toFixed(4)} \xB7 AI calls ${v.prev.aiCalls>=0?"+":""}${v.prev.aiCalls}`)}for(let S of v?.chronic??[])console.log(`\u26A0 chronic: ${S.feature} \u203A ${S.scenario}: ${S.heals} heal(s), ${S.reds} red(s) in last ${S.runs} run(s); re-record or fix the feature text instead of paying for more heals`);console.log(`report: ${b}
|
|
298
|
+
${x}`);let $=oe(E.resolve(_.opts().projectRoot));$.length>0&&console.log(`${$.length} cache proposal(s) pending. Review with: saffron accept | saffron reject`);let C=t.strict??r.strict??!1;C&&f.yellow>0&&console.log(`strict mode: ${f.yellow} scenario(s) pending review treated as failure. Review proposals to go green.`),process.exit(f.red>0||C&&f.yellow>0?1:0)});_.command("accept").description("Promote pending cache proposals to committed caches").argument("[files...]","proposal file(s) (default with --all: every proposal)").option("--all","accept all pending proposals").option("--with-feature-edit","also apply the proposal's suggested .feature step rewrites").option("--propagate","apply the same locator fixes to every other cache using the same locator").option("--include-unverified","with --all: also accept proposals whose proof replay failed").action((e,t)=>{let n=E.resolve(_.opts().projectRoot),r=H(n),s=e.length>0?e.map(o=>E.resolve(o)):t.all?oe(n).map(o=>o.file):[];if(s.length===0){Zn(n);return}for(let o of s){let i=fe(o);if(i.meta.verified===!1){if(t.all&&!t.includeUnverified){console.log(`skipped UNVERIFIED ${E.basename(o)} (${i.meta.proofError?.split(`
|
|
297
299
|
`)[0]}). Re-record it (saffron run --rerecord), or accept it explicitly by file, or pass --include-unverified.`);continue}console.log(`\u26A0 accepting UNVERIFIED proposal (proof replay failed: ${i.meta.proofError?.split(`
|
|
298
|
-
`)[0]}): expect a heal or red on the next run.`)}let a=z(
|
|
299
|
-
${s.steps.length} step(s) across ${s.scannedFiles} file(s) \xB7 ${s.scannedCaches} cache(s) \xB7 \x1B[32m\u25CF\x1B[0m recorded (replays free) \xB7 \x1B[33m\u25CF\x1B[0m divergent \xB7 \u25CB unrecorded`)});_.command("author").description("Draft a feature file from plain-paragraph requirements, reusing the project's step vocabulary (AI)").argument("<prose-file>","text/markdown file with the requirements").option("-o, --out <file>","output path (default: features/<name>.saffron)").option("--model <model>","model for authoring").action(async(e,t)=>{let n=E.resolve(_.opts().projectRoot),r=H(n),s=D.readFileSync(E.resolve(e),"utf8"),o;try{o=q(n,r.features??"features")}catch(
|
|
300
|
-
`).map(
|
|
301
|
-
`))});_.command("status").description("Project overview for dashboards and IDE panels: feature files and scenarios with cache state, tags, pending proposals, last run, history, vocabulary health, effective config").option("--json","machine-readable output").action(e=>{let t=E.resolve(_.opts().projectRoot),n=H(t),r=_e(t,n);if(e.json){console.log(JSON.stringify(r,null,2));return}Gt(r)});_.command("report").description("Open the latest HTML report").option("--open","open in the default browser",!0).action(()=>{let e=E.resolve(_.opts().projectRoot),t=E.join(e,".saffron","reports","latest.html");D.existsSync(t)||(console.error("No report found. Run `saffron run` first."),process.exit(2)),console.log(t);let n=process.platform==="darwin"?"open":"xdg-open";
|
|
300
|
+
`)[0]}): expect a heal or red on the next run.`)}let a=z(V(n,i.cache.feature,i.cache.scenario)),c=Nt(n,o);console.log(`accepted \u2192 ${c}`);let d=a?_n(a,i.cache):[];if(d.length>0){let l=On(n,d,c,!t.propagate);for(let h of d)console.log(` locator fix: ${ut(h.from)} \u2192 ${ut(h.to)}`);if(l.length===0)console.log(" no other caches use this locator.");else if(t.propagate)for(let h of l)console.log(` propagated \u2192 ${h.feature} \u203A ${h.scenario} (${h.steps.map(y=>`"${y.gherkin}"`).join(", ")})`);else{console.log(` ${l.length} other cache(s) use the same locator: re-run with --propagate to fix them too:`);for(let h of l)console.log(` \xB7 ${h.feature} \u203A ${h.scenario}`)}}let m=i.meta.featureEdits??[];if(!t.withFeatureEdit||m.length===0)continue;let p;try{p=Mn(n,i.cache.feature,i.cache.scenario,m,Xn(n,r,[]))}catch(l){throw l instanceof N&&qe(l),l}for(let l of p.applied){let h=l.fromStepSet?`${l.file}:${l.line} (StepSet "${l.fromStepSet}", fixes every invoking scenario)`:`${l.file}:${l.line}`;console.log(` feature edit ${h}:`),console.log(` - ${l.from}`),console.log(` + ${l.to}`)}for(let l of p.skipped)console.log(` skipped edit [${l.index}]: ${l.reason}`);if(p.applied.length>0){let l=z(c);for(let h of m)l.steps[h.index]&&(l.steps[h.index].gherkin=h.text);pe(c,l)}}});_.command("steps").description("List/search the project's step vocabulary (files + caches): what exists, what's recorded, what to reuse").argument("[search]","case-insensitive substring filter").option("--json","machine-readable vocabulary").option("--snippets","write .vscode/saffron.code-snippets for native VS Code completion").action((e,t)=>{let n=E.resolve(_.opts().projectRoot),r=H(n),s;try{s=q(n,r.features??"features")}catch(i){throw i instanceof N&&qe(i),i}if(e&&(s=Ee(s,e)),t.json){console.log(JSON.stringify(s,null,2));return}if(t.snippets){let i=E.join(n,".vscode","saffron.code-snippets");D.mkdirSync(E.dirname(i),{recursive:!0}),D.writeFileSync(i,Ft(s)),console.log(`wrote ${s.steps.length} step + ${s.stepSets.length} StepSet snippet(s) \u2192 ${i}`),console.log("re-run after recording sessions to keep completions current");return}let o={recorded:"\x1B[32m\u25CF\x1B[0m",divergent:"\x1B[33m\u25CF\x1B[0m",unrecorded:"\u25CB"};if(s.stepSets.length>0){console.log("step sets:");for(let i of s.stepSets)console.log(` ${i.name} (${i.steps.length} steps, ${i.usage} scenario(s)), ${i.file}:${i.line}`);console.log("")}for(let i of s.steps){let a=i.fromStepSet?` [${i.fromStepSet}]`:"";console.log(`${o[i.status]} ${i.keyword} ${i.text} \xD7${i.usage}${a}`)}console.log(`
|
|
301
|
+
${s.steps.length} step(s) across ${s.scannedFiles} file(s) \xB7 ${s.scannedCaches} cache(s) \xB7 \x1B[32m\u25CF\x1B[0m recorded (replays free) \xB7 \x1B[33m\u25CF\x1B[0m divergent \xB7 \u25CB unrecorded`)});_.command("author").description("Draft a feature file from plain-paragraph requirements, reusing the project's step vocabulary (AI)").argument("<prose-file>","text/markdown file with the requirements").option("-o, --out <file>","output path (default: features/<name>.saffron)").option("--model <model>","model for authoring").action(async(e,t)=>{let n=E.resolve(_.opts().projectRoot),r=H(n),s=D.readFileSync(E.resolve(e),"utf8"),o;try{o=q(n,r.features??"features")}catch(d){throw d instanceof N&&qe(d),d}console.log(`authoring with ${o.steps.length} known step(s) and ${o.stepSets.length} step set(s) as vocabulary\u2026`);let i=await Yt(s,o,t.model??r.model),a=t.out?E.resolve(t.out):E.join(n,r.features??"features",`${E.basename(e).replace(/\.[^.]+$/,"")}.saffron`);D.existsSync(a)&&(console.error(`refusing to overwrite ${a}: pass --out for a new path`),process.exit(1)),D.mkdirSync(E.dirname(a),{recursive:!0}),D.writeFileSync(a,i.content);let c=i.content.split(`
|
|
302
|
+
`).map(d=>d.trim().replace(/^(Given|When|Then|And|But|\*)\s+/,"")).filter(d=>o.steps.some(m=>m.text===d)).length;console.log(`wrote ${a}`),console.log(`${c} step line(s) reuse the existing vocabulary (seedable) \xB7 $${(i.usage.costUsd??0).toFixed(4)}`),console.log("review the draft, then record it: saffron run "+E.relative(process.cwd(),a))});_.command("mcp").description("Serve the step vocabulary to AI assistants over stdio MCP (search_steps, list_step_sets)").action(async()=>{let e=E.resolve(_.opts().projectRoot),t=H(e);await Jt(e,t.features??"features")});_.command("lsp").description("Run the Saffron language server over stdio (completion, go-to-definition, hover, diagnostics). For JetBrains (LSP4IJ / Ultimate LSP API), Neovim, and any LSP-capable editor").action(()=>{process.stdin.isTTY&&(console.error(Qn(Ce())),process.exit(2));let e=E.resolve(_.opts().projectRoot),t=H(e),n=process.argv.includes("-p")||process.argv.some(r=>r.startsWith("--project-root"));tn(e,t.features??"features",{rootFromClient:!n})});_.command("reject").description("Discard pending cache proposals (AI will retry next run)").argument("[files...]","proposal file(s)").option("--all","reject all pending proposals").action((e,t)=>{let n=E.resolve(_.opts().projectRoot),r=e.length>0?e.map(s=>E.resolve(s)):t.all?oe(n).map(s=>s.file):[];if(r.length===0){Zn(n);return}for(let s of r)Ut(s),console.log(`rejected ${s}`)});_.command("init").description("Make the project agent-ready: install the bundled Saffron skill into agent directories, add instructions to AGENTS.md, scaffold config").option("--agents <list>",`comma-separated targets: ${Object.keys($e).join(", ")} (default: claude,agents)`).option("--no-instructions","do not touch AGENTS.md / CLAUDE.md").option("--no-scaffold","do not create saffron.config.json, features/, .gitignore entry").option("--no-mcp","do not register `saffron mcp` in project MCP configs (.mcp.json, .cursor/mcp.json, .vscode/mcp.json)").option("--examples","install the Saucedemo example suite (features, config, .env.example) for a guided first run").option("--no-examples","never install the example suite (skips the prompt)").action(async e=>{let t=E.resolve(_.opts().projectRoot),n=Ce();n||(console.error("saffron: cannot locate the installed package root"),process.exit(2));let r=e.agents?String(e.agents).split(",").map(i=>i.trim()).filter(Boolean):void 0;for(let i of r??[])i in $e||(console.error(`saffron: unknown agent target "${i}" (expected one of ${Object.keys($e).join(", ")})`),process.exit(2));let s=e.examples===!0;e.examples===void 0&&process.stdin.isTTY&&process.stdout.isTTY&&(s=await Bs("Install the Saucedemo example tests (login, cart, checkout) into features/ for a guided first run? [y/N] "));let o=Kn({projectRoot:t,packageRoot:n,targets:r,instructions:e.instructions,scaffold:e.scaffold,mcp:e.mcp,examples:s});for(let i of o.skillDirs)console.log(`skill \u2192 ${i}/`);for(let i of o.mcpFiles)console.log(`${i.outcome.padEnd(9)} \u2192 ${i.file} (saffron mcp server)`);for(let i of o.mcpHints)console.log(`mcp hint \u2192 ${i}`);for(let i of o.instructionFiles)console.log(`${i.outcome.padEnd(9)} \u2192 ${i.file} (managed Saffron block)`);for(let i of o.scaffolded)console.log(`created \u2192 ${i}`);o.examples&&!o.examples.installed&&console.log(`examples \u2192 not installed: ${o.examples.reason}`),console.log("\nAgents now load the saffron skill when writing tests. Re-run after `npm update saffron-ai` to refresh it."),o.examples?.installed&&console.log(["","Your guided first run (recording needs Claude credentials; replays never do):"," cp .env.example .env # public demo credentials, resolved at replay"," npx playwright install chromium"," npx saffron run --filter @smoke # the agent records two scenarios (about $3 of AI usage), files proposals"," npx saffron accept --all # review, then promote them to caches"," npx saffron run --filter @smoke # replays at zero tokens","Then try the rest: npx saffron run (login errors outline, checkout with a data table)"].join(`
|
|
303
|
+
`))});_.command("status").description("Project overview for dashboards and IDE panels: feature files and scenarios with cache state, tags, pending proposals, last run, history, vocabulary health, effective config").option("--json","machine-readable output").action(e=>{let t=E.resolve(_.opts().projectRoot),n=H(t),r=_e(t,n);if(e.json){console.log(JSON.stringify(r,null,2));return}Gt(r)});_.command("report").description("Open the latest HTML report").option("--open","open in the default browser",!0).action(()=>{let e=E.resolve(_.opts().projectRoot),t=E.join(e,".saffron","reports","latest.html");D.existsSync(t)||(console.error("No report found. Run `saffron run` first."),process.exit(2)),console.log(t);let n=process.platform==="darwin"?"open":"xdg-open";Ds(n,[t],{detached:!0,stdio:"ignore"}).unref()});function Zn(e){let t=oe(e);if(t.length===0){console.log("No pending proposals.");return}console.log(`Pending proposals:
|
|
302
304
|
`);for(let{file:n,proposal:r}of t){console.log(` ${n}`);let s=r.meta.verified===!0?" \xB7 verified \u2713":r.meta.verified===!1?" \xB7 UNVERIFIED \u2717":"";console.log(` ${r.cache.feature} \u203A ${r.cache.scenario} (${r.meta.mode}, ${r.meta.createdAt}${s})`),console.log(` ${r.meta.narrative}`);for(let o of r.meta.adaptations)console.log(` \u26A0 ${o}`);r.meta.suggestedFeatureEdit&&console.log(` suggested .feature edit:
|
|
303
305
|
${r.meta.suggestedFeatureEdit.split(`
|
|
304
306
|
`).map(o=>` ${o}`).join(`
|
package/package.json
CHANGED
|
@@ -124,6 +124,20 @@ Then the feedback widget should show "Thanks for: Great tool"
|
|
|
124
124
|
Upload paths resolve from the working directory. Links that open new
|
|
125
125
|
tabs are followed automatically.
|
|
126
126
|
|
|
127
|
+
## Tabs (plain prose, no keyword)
|
|
128
|
+
|
|
129
|
+
```gherkin
|
|
130
|
+
When I click the "Open preview" link # opens a new tab: replay follows it
|
|
131
|
+
Then the preview should show "Draft 3" # asserted on the new tab
|
|
132
|
+
When I switch back to the first tab
|
|
133
|
+
And I close the preview tab
|
|
134
|
+
When I open a new tab at the admin page
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Recorded as `switchTab` / `closeTab` / `newTab` with 0-based tab indexes
|
|
138
|
+
in creation order; replay repeats them in order. Say which tab in the
|
|
139
|
+
step ("the first tab", "the preview tab") so the recording is unambiguous.
|
|
140
|
+
|
|
127
141
|
## StepSets (`.saffron` only)
|
|
128
142
|
|
|
129
143
|
```gherkin
|