saffron-ai 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist-pkg/cli.js CHANGED
@@ -1,34 +1,34 @@
1
1
  #!/usr/bin/env node
2
- import I from"node:fs";import R from"node:path";import{spawn as qr}from"node:child_process";import{fileURLToPath as Wr}from"node:url";import{Command as Hr}from"commander";import jn from"node:fs";import _n from"node:path";import{AstBuilder as Dn,GherkinClassicTokenMatcher as On,Parser as Mn}from"@cucumber/gherkin";import{IdGenerator as Ln,StepKeywordType as Ie}from"@cucumber/messages";function V(t,e){return t.replace(/<([^<>]+)>/g,(n,r)=>r in e?e[r]:n)}function L(t){if(!t||t.length===0)return{};let e={};if(t.every(n=>n.length===2)){for(let[n,r]of t)e[`table:${n}`]=r;return e}if(t.length>=2){let[n,...r]=t;if(n.some(s=>!s))return{};r.forEach((s,o)=>{n.forEach((i,a)=>{s[a]!==void 0&&(e[`table:${o+1}:${i}`]=s[a])})})}return e}function q(t){return t!==void 0?{docstring:t}:{}}function K(t){let e=new Map;for(let r of t){let s={...L(r.resolvedTable??r.table),...q(r.resolvedDocString??r.docString)};for(let[o,i]of Object.entries(s)){let a=e.get(o);a||e.set(o,a=new Set),a.add(i)}}let n={};for(let[r,s]of e)s.size===1&&(n[r]=[...s][0]);return n}import Pe from"node:fs";import tt from"node:path";import{AstBuilder as Rn,GherkinClassicTokenMatcher as Tn,Parser as En}from"@cucumber/gherkin";import{IdGenerator as An}from"@cucumber/messages";var M=class extends Error{},Cn=/^(\s*)StepSet:\s*(.+?)\s*$/,Pn=/^(\s*)StepSet\s+([^\s:].*?)\s*$/;function Fe(t){let e=t.split(`
3
- `),n=new Map,r=new Map;return{source:e.map((o,i)=>{let a=o.match(Cn);if(a)return n.set(i+1,a[2]),`${a[1]}Scenario: ${a[2]}`;let d=o.match(Pn);return d?(r.set(i+1,d[2]),`${d[1]}* StepSet ${d[2]}`):o}).join(`
4
- `),definitions:n,invocations:r}}function Fn(t){return new En(new Rn(An.uuid()),new Tn).parse(t)}function nt(t,e){for(let n of t)n.scenario?e(n.scenario):"rule"in n&&n.rule&&nt(n.rule.children,e)}function X(t,e){let n=new Map;for(let r of t){let s=tt.relative(e,r),o=Fe(Pe.readFileSync(r,"utf8"));if(o.definitions.size===0)continue;let i=Fn(o.source);i.feature&&nt(i.feature.children,a=>{let d=o.definitions.get(a.location.line);if(d===void 0)return;let p=n.get(d);if(p)throw new M(`StepSet "${d}" is defined twice: ${p.file}:${p.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 ${d}".`);if(a.examples.length>0)throw new M(`StepSet "${d}" (${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 M(`StepSet "${d}" (${s}:${a.location.line}) invokes another step set at line ${m.location.line} \u2014 step sets cannot be nested.`);n.set(d,{name:d,file:s,line:a.location.line,steps:[...a.steps]})})}return n}var In=[".feature",".saffron"];function se(t){return In.some(e=>t.endsWith(e))}function ue(t){if(!Pe.existsSync(t))return[];let e=[];for(let n of Pe.readdirSync(t,{recursive:!0})){let r=String(n);r.endsWith(".saffron")&&e.push(tt.join(t,r))}return e.sort()}function rt(t,e,n){return t.map(({step:r,source:s})=>{let o;switch(r.keywordType){case Ie.OUTCOME:o="assertion";break;case Ie.CONTEXT:case Ie.ACTION:o="action";break;default:o=n.value}n.value=o;let i=r.dataTable?r.dataTable.rows.map(d=>d.cells.map(p=>p.value)):void 0,a=r.docString?.content;return{keyword:r.keyword.trim(),kind:o,text:r.text,resolvedText:V(r.text,e),table:i,resolvedTable:i?.map(d=>d.map(p=>V(p,e))),docString:a,resolvedDocString:a!==void 0?V(a,e):void 0,source:s}})}function st(t,e,n,r,s){let o=[];for(let i of t){let a=r.get(i.location.line);if(a===void 0){o.push({step:i,source:{file:e,line:i.location.line,fromBackground:n}});continue}let d=s?.get(a);if(!d)throw new M(`${e}:${i.location.line} invokes StepSet "${a}", which is not defined anywhere in the project.`);for(let p of d.steps)o.push({step:p,source:{file:d.file,line:p.location.line,fromStepSet:d.name,fromBackground:n}})}return o}function Un(t,e,n,r,s){let o=t.tags.map(a=>a.name),i=[];if(t.examples.length===0)i.push({params:{},label:""});else{let a=0;for(let d of t.examples){let p=d.tableHeader?.cells.map(m=>m.value)??[];for(let m of d.tableBody){a+=1;let l={};m.cells.forEach((c,g)=>{l[p[g]]=c.value}),i.push({params:l,label:` \u2014 example ${a}`})}}}return i.map(({params:a,label:d})=>{let p={value:"action"};return{featurePath:e,featureName:n,name:t.name,displayName:`${t.name}${d}`,tags:o,steps:[...rt(r,a,p),...rt(s,a,p)],params:a}})}function Z(t,e,n){let r=jn.readFileSync(t,"utf8"),o=t.endsWith(".saffron")?Fe(r):{source:r,definitions:new Map,invocations:new Map},a=new Mn(new Dn(Ln.uuid()),new On).parse(o.source);if(!a.feature)return[];let d=_n.relative(e,t),p=a.feature.name,m=[],l=(c,g)=>{let h=g;for(let u of c)if(u.background)h=[...g,...st(u.background.steps,d,!0,o.invocations,n)];else if(u.scenario){if(o.definitions.has(u.scenario.location.line))continue;m.push(...Un(u.scenario,d,p,h,st(u.scenario.steps,d,!1,o.invocations,n)))}else"rule"in u&&u.rule&&l(u.rule.children,h)};return l(a.feature.children,[]),m}import ft from"node:fs";import je from"node:path";import at from"node:fs";import ct from"node:path";import fe from"node:fs";import oe from"node:path";var ot=".saffron";function ge(t){return t.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"unnamed"}function W(t,e,n){let r=ge(oe.basename(e,".feature"));return oe.join(t,ot,"cache",r,`${ge(n)}.json`)}function it(t,e,n){let r=ge(oe.basename(e,".feature"));return oe.join(t,ot,"proposals",r,`${ge(n)}.json`)}function N(t){if(!fe.existsSync(t))return;let e=JSON.parse(fe.readFileSync(t,"utf8"));if(e.version!==1)throw new Error(`Unsupported cache version ${e.version} in ${t}`);return e}function ee(t,e){fe.mkdirSync(oe.dirname(t),{recursive:!0}),fe.writeFileSync(t,`${JSON.stringify(e,null,2)}
5
- `)}function dt(t){let e=[];return{pattern:t.replace(/"((?:[^"\\]|\\.)*)"/g,(r,s)=>(e.push(s),`"\xAB${e.length-1}\xBB"`)),args:e}}function Nn(t,e){let n=r=>JSON.stringify(r.map(({pageFingerprint:s,...o})=>o));return n(t)===n(e)}function zn(t){let e=new Set,n=r=>{if(r)for(let s of r.matchAll(/<([^<>]+)>/g))e.add(s[1])};for(let r of t.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(t.gherkin),e}function H(t){let e=ct.join(t,".saffron","cache"),n=new Map,r=new Map,s=0;if(at.existsSync(e))for(let a of at.readdirSync(e,{recursive:!0})){let d=String(a);if(!d.endsWith(".json"))continue;let p=N(ct.join(e,d));if(p){s++;for(let m of p.steps){let l=m.gherkin,c=JSON.stringify(m.actions.map(({pageFingerprint:u,...f})=>f)),g=r.get(l)??[];g.includes(c)||(g.push(c),r.set(l,g));let h=n.get(l);!h||p.recordedAt>h.recordedAt?n.set(l,{gherkin:m.gherkin,table:m.table,docString:m.docString,actions:m.actions,feature:p.feature,scenario:p.scenario,recordedAt:p.recordedAt,variants:g.length}):h.variants=g.length}}}let o=[...r.entries()].filter(([,a])=>a.length>1).map(([a])=>a),i=new Map;for(let a of n.values()){let{pattern:d,args:p}=dt(a.gherkin);if(p.length===0)continue;let m=i.get(d);(!m||a.recordedAt>m.recordedAt)&&i.set(d,{...a,args:p})}return{steps:n,patterns:i,divergent:o,scannedCaches:s}}function lt(t,e,n){if(!!t.table!=!!e.table)return!1;if(t.table&&e.table){let o=Object.keys(L(t.table)),i=Object.keys(L(e.table));if(o.length>0&&i.length>0){if(JSON.stringify(o)!==JSON.stringify(i))return!1}else if(JSON.stringify(t.table)!==JSON.stringify(e.table))return!1}if(t.docString!==void 0!=(e.docString!==void 0)||t.docString!==void 0&&e.docString!==void 0&&t.docString!==e.docString&&!t.actions.some(o=>[o.value,o.url,o.pattern].some(i=>i?.includes("<docstring>"))))return!1;let r={...n,...L(e.resolvedTable??e.table),...q(e.resolvedDocString??e.docString)},s=zn({gherkin:t.gherkin,keyword:"",kind:"action",actions:t.actions});for(let o of s)if(!(o in r))return!1;return!0}function pt(t,e,n){let r=t.steps.get(e.text);if(r)return lt(r,e,n)?r:void 0;let{pattern:s,args:o}=dt(e.text);if(o.length===0)return;let i=t.patterns.get(s);if(!i)return;let a=new Map;for(let l=0;l<o.length;l++){let c=i.args[l],g=o[l];if(c===g)continue;let h=a.get(c);if(h!==void 0&&h!==g)return;a.set(c,g)}let d=l=>[l?.name,l?.nameRegex,l?.label,l?.text,l?.placeholder];for(let l of a.keys())if(!i.actions.some(g=>[g.value,g.url,g.pattern,...d(g.target)].includes(l)))return;let p=l=>l!==void 0&&a.has(l)?a.get(l):l,m={...i,gherkin:e.text,actions:i.actions.map(({pageFingerprint:l,...c})=>({...c,value:p(c.value),url:p(c.url),pattern:p(c.pattern),target:c.target?{...c.target,name:p(c.target.name),nameRegex:p(c.target.nameRegex),label:p(c.target.label),text:p(c.target.text),placeholder:p(c.target.placeholder)}:void 0}))};return lt(m,e,n)?m:void 0}function ut(t,e){let n=[];return t.forEach((r,s)=>{if(r.actions.length!==0&&!e.steps.has(r.gherkin)){for(let o of e.steps.values())if(!(o.variants>1)&&o.actions.length!==0&&Nn(r.actions,o.actions)){n.push({index:s,from:r.gherkin,to:o.gherkin,feature:o.feature,scenario:o.scenario});break}}}),n}function Bn(t){if(!ft.existsSync(t))return[];let e=[];for(let n of ft.readdirSync(t,{recursive:!0})){let r=String(n);se(r)&&e.push(je.join(t,r))}return e.sort()}function G(t,e,n){let r=je.isAbsolute(e)?e:je.join(t,e),s=Bn(r),o=n??X(s.filter(c=>c.endsWith(".saffron")),t),i=new Map,a=new Map;for(let c of s)for(let g of Z(c,t,o)){let h=`${g.featurePath}::${g.name}`;for(let u of g.steps){let f=i.get(u.text);if(f||(f={text:u.text,keywords:new Map,kind:u.kind,scenarios:new Set,files:new Set,fromStepSet:u.source?.fromStepSet},i.set(u.text,f)),f.keywords.set(u.keyword,(f.keywords.get(u.keyword)??0)+1),f.scenarios.add(h),u.source&&f.files.add(u.source.file),u.source?.fromStepSet){let y=a.get(u.source.fromStepSet);y||a.set(u.source.fromStepSet,y=new Set),y.add(h)}}}let d=H(t),p=new Set(d.divergent),m=[...i.values()].map(c=>({text:c.text,keyword:[...c.keywords.entries()].sort((g,h)=>h[1]-g[1])[0][0],kind:c.kind,status:p.has(c.text)?"divergent":d.steps.has(c.text)?"recorded":"unrecorded",usage:c.scenarios.size,files:[...c.files].sort(),fromStepSet:c.fromStepSet})).sort((c,g)=>g.usage-c.usage||c.text.localeCompare(g.text)),l=[...o.values()].map(c=>({name:c.name,file:c.file,line:c.line,steps:c.steps.map(g=>g.text),usage:a.get(c.name)?.size??0})).sort((c,g)=>g.usage-c.usage||c.name.localeCompare(g.name));return{steps:m,stepSets:l,scannedFiles:s.length,scannedCaches:d.scannedCaches}}function me(t,e){let n=e.toLowerCase();return{...t,steps:t.steps.filter(r=>r.text.toLowerCase().includes(n)),stepSets:t.stepSets.filter(r=>r.name.toLowerCase().includes(n)||r.steps.some(s=>s.toLowerCase().includes(n)))}}function gt(t){let e={},n="saffron,feature,gherkin,cucumber";for(let r of t.steps)e[`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 t.stepSets)e[`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(e,null,2)+`
6
- `}import{Server as Vn}from"@modelcontextprotocol/sdk/server/index.js";import{StdioServerTransport as qn}from"@modelcontextprotocol/sdk/server/stdio.js";import{CallToolRequestSchema as Wn,ListToolsRequestSchema as Hn}from"@modelcontextprotocol/sdk/types.js";async function mt(t,e){let n=new Vn({name:"saffron",version:"0.1.0"},{capabilities:{tools:{}}});n.setRequestHandler(Hn,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 \u2014 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:{}}}]})),n.setRequestHandler(Wn,async r=>{let s=G(t,e);if(r.params.name==="search_steps"){let o=r.params.arguments?.query,i=o?me(s,o):s;return{content:[{type:"text",text:JSON.stringify({steps:i.steps,scannedFiles:i.scannedFiles,scannedCaches:i.scannedCaches},null,2)}]}}return r.params.name==="list_step_sets"?{content:[{type:"text",text:JSON.stringify({stepSets:s.stepSets},null,2)}]}:{content:[{type:"text",text:`Unknown tool: ${r.params.name}`}],isError:!0}}),await n.connect(new qn)}import{query as Gn}from"@anthropic-ai/claude-agent-sdk";function Yn(t,e){let n=e.steps.map(s=>`- [${s.status}] ${s.keyword} ${s.text}`).join(`
2
+ import I from"node:fs";import $ from"node:path";import{spawn as Qr}from"node:child_process";import{Command as Kr}from"commander";import Mn from"node:fs";import Ln from"node:path";import{AstBuilder as Un,GherkinClassicTokenMatcher as Nn,Parser as Bn}from"@cucumber/gherkin";import{IdGenerator as zn,StepKeywordType as De}from"@cucumber/messages";function V(t,e){return t.replace(/<([^<>]+)>/g,(n,r)=>r in e?e[r]:n)}function L(t){if(!t||t.length===0)return{};let e={};if(t.every(n=>n.length===2)){for(let[n,r]of t)e[`table:${n}`]=r;return e}if(t.length>=2){let[n,...r]=t;if(n.some(s=>!s))return{};r.forEach((s,o)=>{n.forEach((i,a)=>{s[a]!==void 0&&(e[`table:${o+1}:${i}`]=s[a])})})}return e}function q(t){return t!==void 0?{docstring:t}:{}}function K(t){let e=new Map;for(let r of t){let s={...L(r.resolvedTable??r.table),...q(r.resolvedDocString??r.docString)};for(let[o,i]of Object.entries(s)){let a=e.get(o);a||e.set(o,a=new Set),a.add(i)}}let n={};for(let[r,s]of e)s.size===1&&(n[r]=[...s][0]);return n}import je from"node:fs";import ot from"node:path";import{AstBuilder as Cn,GherkinClassicTokenMatcher as Pn,Parser as Fn}from"@cucumber/gherkin";import{IdGenerator as In}from"@cucumber/messages";var M=class extends Error{},jn=/^(\s*)StepSet:\s*(.+?)\s*$/,_n=/^(\s*)StepSet\s+([^\s:].*?)\s*$/;function _e(t){let e=t.split(`
3
+ `),n=new Map,r=new Map;return{source:e.map((o,i)=>{let a=o.match(jn);if(a)return n.set(i+1,a[2]),`${a[1]}Scenario: ${a[2]}`;let d=o.match(_n);return d?(r.set(i+1,d[2]),`${d[1]}* StepSet ${d[2]}`):o}).join(`
4
+ `),definitions:n,invocations:r}}function Dn(t){return new Fn(new Cn(In.uuid()),new Pn).parse(t)}function it(t,e){for(let n of t)n.scenario?e(n.scenario):"rule"in n&&n.rule&&it(n.rule.children,e)}function X(t,e){let n=new Map;for(let r of t){let s=ot.relative(e,r),o=_e(je.readFileSync(r,"utf8"));if(o.definitions.size===0)continue;let i=Dn(o.source);i.feature&&it(i.feature.children,a=>{let d=o.definitions.get(a.location.line);if(d===void 0)return;let p=n.get(d);if(p)throw new M(`StepSet "${d}" is defined twice: ${p.file}:${p.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 ${d}".`);if(a.examples.length>0)throw new M(`StepSet "${d}" (${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 M(`StepSet "${d}" (${s}:${a.location.line}) invokes another step set at line ${m.location.line} \u2014 step sets cannot be nested.`);n.set(d,{name:d,file:s,line:a.location.line,steps:[...a.steps]})})}return n}var On=[".feature",".saffron"];function se(t){return On.some(e=>t.endsWith(e))}function ue(t){if(!je.existsSync(t))return[];let e=[];for(let n of je.readdirSync(t,{recursive:!0})){let r=String(n);r.endsWith(".saffron")&&e.push(ot.join(t,r))}return e.sort()}function at(t,e,n){return t.map(({step:r,source:s})=>{let o;switch(r.keywordType){case De.OUTCOME:o="assertion";break;case De.CONTEXT:case De.ACTION:o="action";break;default:o=n.value}n.value=o;let i=r.dataTable?r.dataTable.rows.map(d=>d.cells.map(p=>p.value)):void 0,a=r.docString?.content;return{keyword:r.keyword.trim(),kind:o,text:r.text,resolvedText:V(r.text,e),table:i,resolvedTable:i?.map(d=>d.map(p=>V(p,e))),docString:a,resolvedDocString:a!==void 0?V(a,e):void 0,source:s}})}function ct(t,e,n,r,s){let o=[];for(let i of t){let a=r.get(i.location.line);if(a===void 0){o.push({step:i,source:{file:e,line:i.location.line,fromBackground:n}});continue}let d=s?.get(a);if(!d)throw new M(`${e}:${i.location.line} invokes StepSet "${a}", which is not defined anywhere in the project.`);for(let p of d.steps)o.push({step:p,source:{file:d.file,line:p.location.line,fromStepSet:d.name,fromBackground:n}})}return o}function Vn(t,e,n,r,s){let o=t.tags.map(a=>a.name),i=[];if(t.examples.length===0)i.push({params:{},label:""});else{let a=0;for(let d of t.examples){let p=d.tableHeader?.cells.map(m=>m.value)??[];for(let m of d.tableBody){a+=1;let l={};m.cells.forEach((c,g)=>{l[p[g]]=c.value}),i.push({params:l,label:` \u2014 example ${a}`})}}}return i.map(({params:a,label:d})=>{let p={value:"action"};return{featurePath:e,featureName:n,name:t.name,displayName:`${t.name}${d}`,tags:o,steps:[...at(r,a,p),...at(s,a,p)],params:a}})}function Z(t,e,n){let r=Mn.readFileSync(t,"utf8"),o=t.endsWith(".saffron")?_e(r):{source:r,definitions:new Map,invocations:new Map},a=new Bn(new Un(zn.uuid()),new Nn).parse(o.source);if(!a.feature)return[];let d=Ln.relative(e,t),p=a.feature.name,m=[],l=(c,g)=>{let h=g;for(let u of c)if(u.background)h=[...g,...ct(u.background.steps,d,!0,o.invocations,n)];else if(u.scenario){if(o.definitions.has(u.scenario.location.line))continue;m.push(...Vn(u.scenario,d,p,h,ct(u.scenario.steps,d,!1,o.invocations,n)))}else"rule"in u&&u.rule&&l(u.rule.children,h)};return l(a.feature.children,[]),m}import yt from"node:fs";import Oe from"node:path";import pt from"node:fs";import ut from"node:path";import fe from"node:fs";import oe from"node:path";var lt=".saffron";function ge(t){return t.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"unnamed"}function W(t,e,n){let r=ge(oe.basename(e,".feature"));return oe.join(t,lt,"cache",r,`${ge(n)}.json`)}function dt(t,e,n){let r=ge(oe.basename(e,".feature"));return oe.join(t,lt,"proposals",r,`${ge(n)}.json`)}function N(t){if(!fe.existsSync(t))return;let e=JSON.parse(fe.readFileSync(t,"utf8"));if(e.version!==1)throw new Error(`Unsupported cache version ${e.version} in ${t}`);return e}function ee(t,e){fe.mkdirSync(oe.dirname(t),{recursive:!0}),fe.writeFileSync(t,`${JSON.stringify(e,null,2)}
5
+ `)}function gt(t){let e=[];return{pattern:t.replace(/"((?:[^"\\]|\\.)*)"/g,(r,s)=>(e.push(s),`"\xAB${e.length-1}\xBB"`)),args:e}}function qn(t,e){let n=r=>JSON.stringify(r.map(({pageFingerprint:s,...o})=>o));return n(t)===n(e)}function Wn(t){let e=new Set,n=r=>{if(r)for(let s of r.matchAll(/<([^<>]+)>/g))e.add(s[1])};for(let r of t.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(t.gherkin),e}function H(t){let e=ut.join(t,".saffron","cache"),n=new Map,r=new Map,s=0;if(pt.existsSync(e))for(let a of pt.readdirSync(e,{recursive:!0})){let d=String(a);if(!d.endsWith(".json"))continue;let p=N(ut.join(e,d));if(p){s++;for(let m of p.steps){let l=m.gherkin,c=JSON.stringify(m.actions.map(({pageFingerprint:u,...f})=>f)),g=r.get(l)??[];g.includes(c)||(g.push(c),r.set(l,g));let h=n.get(l);!h||p.recordedAt>h.recordedAt?n.set(l,{gherkin:m.gherkin,table:m.table,docString:m.docString,actions:m.actions,feature:p.feature,scenario:p.scenario,recordedAt:p.recordedAt,variants:g.length}):h.variants=g.length}}}let o=[...r.entries()].filter(([,a])=>a.length>1).map(([a])=>a),i=new Map;for(let a of n.values()){let{pattern:d,args:p}=gt(a.gherkin);if(p.length===0)continue;let m=i.get(d);(!m||a.recordedAt>m.recordedAt)&&i.set(d,{...a,args:p})}return{steps:n,patterns:i,divergent:o,scannedCaches:s}}function ft(t,e,n){if(!!t.table!=!!e.table)return!1;if(t.table&&e.table){let o=Object.keys(L(t.table)),i=Object.keys(L(e.table));if(o.length>0&&i.length>0){if(JSON.stringify(o)!==JSON.stringify(i))return!1}else if(JSON.stringify(t.table)!==JSON.stringify(e.table))return!1}if(t.docString!==void 0!=(e.docString!==void 0)||t.docString!==void 0&&e.docString!==void 0&&t.docString!==e.docString&&!t.actions.some(o=>[o.value,o.url,o.pattern].some(i=>i?.includes("<docstring>"))))return!1;let r={...n,...L(e.resolvedTable??e.table),...q(e.resolvedDocString??e.docString)},s=Wn({gherkin:t.gherkin,keyword:"",kind:"action",actions:t.actions});for(let o of s)if(!(o in r))return!1;return!0}function mt(t,e,n){let r=t.steps.get(e.text);if(r)return ft(r,e,n)?r:void 0;let{pattern:s,args:o}=gt(e.text);if(o.length===0)return;let i=t.patterns.get(s);if(!i)return;let a=new Map;for(let l=0;l<o.length;l++){let c=i.args[l],g=o[l];if(c===g)continue;let h=a.get(c);if(h!==void 0&&h!==g)return;a.set(c,g)}let d=l=>[l?.name,l?.nameRegex,l?.label,l?.text,l?.placeholder];for(let l of a.keys())if(!i.actions.some(g=>[g.value,g.url,g.pattern,...d(g.target)].includes(l)))return;let p=l=>l!==void 0&&a.has(l)?a.get(l):l,m={...i,gherkin:e.text,actions:i.actions.map(({pageFingerprint:l,...c})=>({...c,value:p(c.value),url:p(c.url),pattern:p(c.pattern),target:c.target?{...c.target,name:p(c.target.name),nameRegex:p(c.target.nameRegex),label:p(c.target.label),text:p(c.target.text),placeholder:p(c.target.placeholder)}:void 0}))};return ft(m,e,n)?m:void 0}function ht(t,e){let n=[];return t.forEach((r,s)=>{if(r.actions.length!==0&&!e.steps.has(r.gherkin)){for(let o of e.steps.values())if(!(o.variants>1)&&o.actions.length!==0&&qn(r.actions,o.actions)){n.push({index:s,from:r.gherkin,to:o.gherkin,feature:o.feature,scenario:o.scenario});break}}}),n}function Hn(t){if(!yt.existsSync(t))return[];let e=[];for(let n of yt.readdirSync(t,{recursive:!0})){let r=String(n);se(r)&&e.push(Oe.join(t,r))}return e.sort()}function G(t,e,n){let r=Oe.isAbsolute(e)?e:Oe.join(t,e),s=Hn(r),o=n??X(s.filter(c=>c.endsWith(".saffron")),t),i=new Map,a=new Map;for(let c of s)for(let g of Z(c,t,o)){let h=`${g.featurePath}::${g.name}`;for(let u of g.steps){let f=i.get(u.text);if(f||(f={text:u.text,keywords:new Map,kind:u.kind,scenarios:new Set,files:new Set,fromStepSet:u.source?.fromStepSet},i.set(u.text,f)),f.keywords.set(u.keyword,(f.keywords.get(u.keyword)??0)+1),f.scenarios.add(h),u.source&&f.files.add(u.source.file),u.source?.fromStepSet){let y=a.get(u.source.fromStepSet);y||a.set(u.source.fromStepSet,y=new Set),y.add(h)}}}let d=H(t),p=new Set(d.divergent),m=[...i.values()].map(c=>({text:c.text,keyword:[...c.keywords.entries()].sort((g,h)=>h[1]-g[1])[0][0],kind:c.kind,status:p.has(c.text)?"divergent":d.steps.has(c.text)?"recorded":"unrecorded",usage:c.scenarios.size,files:[...c.files].sort(),fromStepSet:c.fromStepSet})).sort((c,g)=>g.usage-c.usage||c.text.localeCompare(g.text)),l=[...o.values()].map(c=>({name:c.name,file:c.file,line:c.line,steps:c.steps.map(g=>g.text),usage:a.get(c.name)?.size??0})).sort((c,g)=>g.usage-c.usage||c.name.localeCompare(g.name));return{steps:m,stepSets:l,scannedFiles:s.length,scannedCaches:d.scannedCaches}}function me(t,e){let n=e.toLowerCase();return{...t,steps:t.steps.filter(r=>r.text.toLowerCase().includes(n)),stepSets:t.stepSets.filter(r=>r.name.toLowerCase().includes(n)||r.steps.some(s=>s.toLowerCase().includes(n)))}}function St(t){let e={},n="saffron,feature,gherkin,cucumber";for(let r of t.steps)e[`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 t.stepSets)e[`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(e,null,2)+`
6
+ `}import{Server as Gn}from"@modelcontextprotocol/sdk/server/index.js";import{StdioServerTransport as Jn}from"@modelcontextprotocol/sdk/server/stdio.js";import{CallToolRequestSchema as Yn,ListToolsRequestSchema as Qn}from"@modelcontextprotocol/sdk/types.js";async function xt(t,e){let n=new Gn({name:"saffron",version:"0.1.0"},{capabilities:{tools:{}}});n.setRequestHandler(Qn,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 \u2014 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:{}}}]})),n.setRequestHandler(Yn,async r=>{let s=G(t,e);if(r.params.name==="search_steps"){let o=r.params.arguments?.query,i=o?me(s,o):s;return{content:[{type:"text",text:JSON.stringify({steps:i.steps,scannedFiles:i.scannedFiles,scannedCaches:i.scannedCaches},null,2)}]}}return r.params.name==="list_step_sets"?{content:[{type:"text",text:JSON.stringify({stepSets:s.stepSets},null,2)}]}:{content:[{type:"text",text:`Unknown tool: ${r.params.name}`}],isError:!0}}),await n.connect(new Jn)}import{query as Kn}from"@anthropic-ai/claude-agent-sdk";function Xn(t,e){let n=e.steps.map(s=>`- [${s.status}] ${s.keyword} ${s.text}`).join(`
7
7
  `),r=e.stepSets.map(s=>`- StepSet ${s.name}
8
8
  ${s.steps.map(o=>` ${o}`).join(`
9
9
  `)}`).join(`
10
10
  `);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)","",e.stepSets.length>0?`EXISTING STEP SETS \u2014 invoke with a line reading exactly \`StepSet <name>\` instead of repeating their steps:
11
11
  ${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:",t.trim()].filter(s=>s!==void 0).join(`
12
- `)}async function ht(t,e,n){let r={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0},s="",o=Gn({prompt:Yn(t,e),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+`
13
- `,usage:r}}import he from"node:path";import{createConnection as Jn,ProposedFeatures as Qn,TextDocuments as Kn,TextDocumentSyncKind as Xn,CompletionItemKind as _e,DiagnosticSeverity as ye,MarkupKind as yt}from"vscode-languageserver/node";import{TextDocument as Zn}from"vscode-languageserver-textdocument";var St=/^(\s*)(Given|When|Then|And|But|\*)\s+(.+?)\s*$/,er=/^\s*StepSet:\s*(.+?)\s*$/,De=/^(\s*)(StepSet)\s+([^\s:].*?)\s*$/,Oe={recorded:"\u25CF recorded \u2014 replays at zero tokens",divergent:"\u25CF divergent \u2014 same text, different recordings",unrecorded:"\u25CB written, not recorded yet"};function tr(t,e){let n=i=>new Set(i.toLowerCase().replace(/"[^"]*"/g,'"x"').split(/[^a-z0-9"<>]+/).filter(Boolean)),r=n(t),s=n(e);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 xt(t,e){let n=Jn(Qn.all,process.stdin,process.stdout),r=new Kn(Zn),s={steps:[],stepSets:[],scannedFiles:0,scannedCaches:0},o=new Map,i,a=()=>{try{let l=he.isAbsolute(e)?e:he.join(t,e);o=X(ue(l),t),s=G(t,e,o)}catch(l){n.console.warn(`saffron vocabulary rebuild: ${String(l)}`)}for(let l of r.all())m(l)},d=()=>{clearTimeout(i),i=setTimeout(a,300)};function p(l){return s.steps.find(c=>c.text===l)?.status}function m(l){if(!l.uri.endsWith(".saffron"))return;let c=[],g=l.getText().split(`
14
- `),h=new Map;for(let u=0;u<g.length;u++){let f=g[u],y={start:{line:u,character:0},end:{line:u,character:f.length}},x=f.match(De);x&&!o.has(x[3])&&c.push({range:y,severity:ye.Error,source:"saffron",message:`StepSet "${x[3]}" is not defined anywhere in the project.`});let b=f.match(er);if(b){let v=o.get(b[1]),j=he.relative(t,new URL(l.uri).pathname).replace(/\\/g,"/");(h.has(b[1])||v&&v.file!==j)&&c.push({range:y,severity:ye.Error,source:"saffron",message:`StepSet "${b[1]}" is defined twice \u2014 set names are project-wide. If this line was meant to INVOKE the set, remove the colon: "StepSet ${b[1]}".`}),h.set(b[1],u)}let w=f.trim();if(w&&!x&&!b){for(let[v]of o)if(w.toLowerCase()===v.toLowerCase()){c.push({range:y,severity:ye.Warning,source:"saffron",message:`Did you mean "StepSet ${v}"? Invoking a step set needs the StepSet keyword, like a step needs Given/When/Then.`});break}}let k=f.match(St);if(k&&p(k[3])!=="recorded"){for(let v of s.steps)if(!(v.text===k[3]||v.status==="unrecorded")&&tr(v.text,k[3])>=.7){c.push({range:{start:{line:u,character:f.indexOf(k[3])},end:{line:u,character:f.length}},severity:ye.Information,source:"saffron",message:`Similar to the ${v.status} step "${v.text}" (\xD7${v.usage}). Reusing the exact wording replays at zero tokens; a new wording costs a fresh AI recording.`});break}}}n.sendDiagnostics({uri:l.uri,diagnostics:c})}n.onInitialize(()=>(a(),{capabilities:{textDocumentSync:Xn.Incremental,completionProvider:{triggerCharacters:[" "]},definitionProvider:!0,hoverProvider:!0}})),n.onCompletion(l=>{let c=r.get(l.textDocument.uri);if(!c)return[];let g=c.getText({start:{line:l.position.line,character:0},end:l.position});if(/^\s*StepSet\s+/.test(g)&&c.uri.endsWith(".saffron"))return s.stepSets.map(f=>({label:f.name,kind:_e.Function,detail:`step set \xB7 ${f.steps.length} step(s) \xB7 \xD7${f.usage}`,documentation:f.steps.join(`
15
- `)}));if(/^\s*(Given|When|Then|And|But|\*)\s+/.test(g))return s.steps.map((f,y)=>({label:f.text,kind:_e.Text,detail:`${Oe[f.status]} \xB7 \xD7${f.usage}`,sortText:String(y).padStart(5,"0"),filterText:f.text}));let u=g.replace(/^\s*/,"").trimEnd().toLowerCase().replace(/\s+/g,"");if(u.length>0&&("stepset".startsWith(u)||u.startsWith("stepset"))&&c.uri.endsWith(".saffron")){let f={start:{line:l.position.line,character:g.length-g.replace(/^\s*/,"").length},end:l.position};return s.stepSets.map(y=>({label:`StepSet ${y.name}`,kind:_e.Function,detail:`invoke step set \xB7 ${y.steps.length} step(s)`,documentation:y.steps.join(`
16
- `),filterText:`step set ${y.name}`,textEdit:{range:f,newText:`StepSet ${y.name}`}}))}return[]}),n.onDefinition(l=>{let c=r.get(l.textDocument.uri);if(!c)return;let h=c.getText({start:{line:l.position.line,character:0},end:{line:l.position.line+1,character:0}}).replace(/\n$/,"").match(De);if(!h)return;let u=o.get(h[3]);return u?{uri:`file://${he.join(t,u.file)}`,range:{start:{line:u.line-1,character:0},end:{line:u.line-1,character:0}}}:void 0}),n.onHover(l=>{let c=r.get(l.textDocument.uri);if(!c)return;let g=c.getText({start:{line:l.position.line,character:0},end:{line:l.position.line+1,character:0}}).replace(/\n$/,""),h=g.match(De);if(h){let f=s.stepSets.find(x=>x.name===h[3]);if(!f)return;let y=f.steps.map(x=>`- ${Oe[p(x)??"unrecorded"][0]} ${x}`).join(`
17
- `);return{contents:{kind:yt.Markdown,value:`**StepSet ${f.name}** \u2014 expands to:
12
+ `)}async function bt(t,e,n){let r={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0},s="",o=Kn({prompt:Xn(t,e),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+`
13
+ `,usage:r}}import he from"node:path";import{createConnection as Zn,ProposedFeatures as er,TextDocuments as tr,TextDocumentSyncKind as nr,CompletionItemKind as Me,DiagnosticSeverity as ye,MarkupKind as vt}from"vscode-languageserver/node";import{TextDocument as rr}from"vscode-languageserver-textdocument";var wt=/^(\s*)(Given|When|Then|And|But|\*)\s+(.+?)\s*$/,sr=/^\s*StepSet:\s*(.+?)\s*$/,Le=/^(\s*)(StepSet)\s+([^\s:].*?)\s*$/,Ue={recorded:"\u25CF recorded \u2014 replays at zero tokens",divergent:"\u25CF divergent \u2014 same text, different recordings",unrecorded:"\u25CB written, not recorded yet"};function or(t,e){let n=i=>new Set(i.toLowerCase().replace(/"[^"]*"/g,'"x"').split(/[^a-z0-9"<>]+/).filter(Boolean)),r=n(t),s=n(e);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 kt(t,e){let n=Zn(er.all,process.stdin,process.stdout),r=new tr(rr),s={steps:[],stepSets:[],scannedFiles:0,scannedCaches:0},o=new Map,i,a=()=>{try{let l=he.isAbsolute(e)?e:he.join(t,e);o=X(ue(l),t),s=G(t,e,o)}catch(l){n.console.warn(`saffron vocabulary rebuild: ${String(l)}`)}for(let l of r.all())m(l)},d=()=>{clearTimeout(i),i=setTimeout(a,300)};function p(l){return s.steps.find(c=>c.text===l)?.status}function m(l){if(!l.uri.endsWith(".saffron"))return;let c=[],g=l.getText().split(`
14
+ `),h=new Map;for(let u=0;u<g.length;u++){let f=g[u],y={start:{line:u,character:0},end:{line:u,character:f.length}},x=f.match(Le);x&&!o.has(x[3])&&c.push({range:y,severity:ye.Error,source:"saffron",message:`StepSet "${x[3]}" is not defined anywhere in the project.`});let b=f.match(sr);if(b){let v=o.get(b[1]),j=he.relative(t,new URL(l.uri).pathname).replace(/\\/g,"/");(h.has(b[1])||v&&v.file!==j)&&c.push({range:y,severity:ye.Error,source:"saffron",message:`StepSet "${b[1]}" is defined twice \u2014 set names are project-wide. If this line was meant to INVOKE the set, remove the colon: "StepSet ${b[1]}".`}),h.set(b[1],u)}let w=f.trim();if(w&&!x&&!b){for(let[v]of o)if(w.toLowerCase()===v.toLowerCase()){c.push({range:y,severity:ye.Warning,source:"saffron",message:`Did you mean "StepSet ${v}"? Invoking a step set needs the StepSet keyword, like a step needs Given/When/Then.`});break}}let k=f.match(wt);if(k&&p(k[3])!=="recorded"){for(let v of s.steps)if(!(v.text===k[3]||v.status==="unrecorded")&&or(v.text,k[3])>=.7){c.push({range:{start:{line:u,character:f.indexOf(k[3])},end:{line:u,character:f.length}},severity:ye.Information,source:"saffron",message:`Similar to the ${v.status} step "${v.text}" (\xD7${v.usage}). Reusing the exact wording replays at zero tokens; a new wording costs a fresh AI recording.`});break}}}n.sendDiagnostics({uri:l.uri,diagnostics:c})}n.onInitialize(()=>(a(),{capabilities:{textDocumentSync:nr.Incremental,completionProvider:{triggerCharacters:[" "]},definitionProvider:!0,hoverProvider:!0}})),n.onCompletion(l=>{let c=r.get(l.textDocument.uri);if(!c)return[];let g=c.getText({start:{line:l.position.line,character:0},end:l.position});if(/^\s*StepSet\s+/.test(g)&&c.uri.endsWith(".saffron"))return s.stepSets.map(f=>({label:f.name,kind:Me.Function,detail:`step set \xB7 ${f.steps.length} step(s) \xB7 \xD7${f.usage}`,documentation:f.steps.join(`
15
+ `)}));if(/^\s*(Given|When|Then|And|But|\*)\s+/.test(g))return s.steps.map((f,y)=>({label:f.text,kind:Me.Text,detail:`${Ue[f.status]} \xB7 \xD7${f.usage}`,sortText:String(y).padStart(5,"0"),filterText:f.text}));let u=g.replace(/^\s*/,"").trimEnd().toLowerCase().replace(/\s+/g,"");if(u.length>0&&("stepset".startsWith(u)||u.startsWith("stepset"))&&c.uri.endsWith(".saffron")){let f={start:{line:l.position.line,character:g.length-g.replace(/^\s*/,"").length},end:l.position};return s.stepSets.map(y=>({label:`StepSet ${y.name}`,kind:Me.Function,detail:`invoke step set \xB7 ${y.steps.length} step(s)`,documentation:y.steps.join(`
16
+ `),filterText:`step set ${y.name}`,textEdit:{range:f,newText:`StepSet ${y.name}`}}))}return[]}),n.onDefinition(l=>{let c=r.get(l.textDocument.uri);if(!c)return;let h=c.getText({start:{line:l.position.line,character:0},end:{line:l.position.line+1,character:0}}).replace(/\n$/,"").match(Le);if(!h)return;let u=o.get(h[3]);return u?{uri:`file://${he.join(t,u.file)}`,range:{start:{line:u.line-1,character:0},end:{line:u.line-1,character:0}}}:void 0}),n.onHover(l=>{let c=r.get(l.textDocument.uri);if(!c)return;let g=c.getText({start:{line:l.position.line,character:0},end:{line:l.position.line+1,character:0}}).replace(/\n$/,""),h=g.match(Le);if(h){let f=s.stepSets.find(x=>x.name===h[3]);if(!f)return;let y=f.steps.map(x=>`- ${Ue[p(x)??"unrecorded"][0]} ${x}`).join(`
17
+ `);return{contents:{kind:vt.Markdown,value:`**StepSet ${f.name}** \u2014 expands to:
18
18
 
19
- ${y}`}}}let u=g.match(St);if(u){let f=s.steps.find(y=>y.text===u[3]);return f?{contents:{kind:yt.Markdown,value:`${Oe[f.status]}
19
+ ${y}`}}}let u=g.match(wt);if(u){let f=s.steps.find(y=>y.text===u[3]);return f?{contents:{kind:vt.Markdown,value:`${Ue[f.status]}
20
20
 
21
21
  Used in ${f.usage} place(s): ${f.files.join(", ")}${f.fromStepSet?`
22
22
 
23
- Defined in StepSet **${f.fromStepSet}**`:""}`}}:void 0}}),r.onDidOpen(l=>m(l.document)),r.onDidChangeContent(l=>m(l.document)),r.onDidSave(()=>d()),n.onDidChangeWatchedFiles(()=>d()),r.listen(n),n.listen()}import bt from"node:fs";import nr from"node:path";var vt=/\{env:([A-Za-z_][A-Za-z0-9_]*)\}/g;function ie(t){return t.replace(vt,(e,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 Me(t){let e=new Set;for(let n of t)if(n)for(let r of n.matchAll(vt))e.add(r[1]);return e}function Le(t){let e=[];for(let n of t.steps){e.push(n.text,n.resolvedText),n.docString&&e.push(n.docString);for(let r of n.table??[])e.push(...r)}return e}function ae(t){let e=new Map;for(let n of Me(Le(t))){let r=process.env[n];r!==void 0&&e.set(n,r)}return e}function te(t,e){let n=t;for(let[r,s]of[...e.entries()].sort((o,i)=>i[1].length-o[1].length))s.length<4||(n=n.split(s).join(`{env:${r}}`));return n}function wt(t){let e=nr.join(t,".env");if(bt.existsSync(e))for(let n of bt.readFileSync(e,"utf8").split(`
24
- `)){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)}}import pr from"node:fs";import ur from"node:net";import Ft from"node:path";import{chromium as fr,firefox as gr,webkit as mr}from"playwright";import z from"node:fs";import Se from"node:path";function kt(t,e){z.mkdirSync(Se.dirname(t),{recursive:!0}),z.writeFileSync(t,`${JSON.stringify(e,null,2)}
25
- `)}function xe(t){return JSON.parse(z.readFileSync(t,"utf8"))}function ce(t){let e=Se.join(t,".saffron","proposals");if(!z.existsSync(e))return[];let n=[];for(let r of z.readdirSync(e)){let s=Se.join(e,r);if(z.statSync(s).isDirectory())for(let o of z.readdirSync(s)){if(!o.endsWith(".json"))continue;let i=Se.join(s,o);n.push({file:i,proposal:xe(i)})}}return n}function $t(t,e){let n=xe(e),r=W(t,n.cache.feature,n.cache.scenario);return ee(r,n.cache),z.rmSync(e),r}function Rt(t){z.rmSync(t)}import Et from"node:path";var rr=/\{date([+-]\d+)?(?::([^}]+))?\}/g;function sr(t,e){let n=String(t.getFullYear()),r=String(t.getMonth()+1).padStart(2,"0"),s=String(t.getDate()).padStart(2,"0");return e.replaceAll("YYYY",n).replaceAll("MM",r).replaceAll("DD",s).replaceAll("M",String(t.getMonth()+1)).replaceAll("D",String(t.getDate()))}function Ue(t,e=new Date){return t.replace(rr,(n,r,s)=>{let o=new Date(e);return o.setDate(o.getDate()+(r?parseInt(r,10):0)),sr(o,s??"YYYY-MM-DD")})}var or=/\b(\d{4})-(\d{2})-(\d{2})\b/g,ir=400;function Tt(t,e=new Date){let n=new Date(e.getFullYear(),e.getMonth(),e.getDate());return t.replace(or,(r,s,o,i)=>{let a=new Date(Number(s),Number(o)-1,Number(i));if(Number.isNaN(a.getTime()))return r;let d=Math.round((a.getTime()-n.getTime())/864e5);return Math.abs(d)>ir?r:d===0?"{date}":`{date${d>0?"+":""}${d}}`})}import{createHash as ar}from"node:crypto";async function Y(t){let e;try{e=await t.locator("body").ariaSnapshot({timeout:3e3})}catch{return"sha256:unavailable"}let n=e.replace(/\d+/g,"#").replace(/\s+/g," ").trim();return`sha256:${ar("sha256").update(n).digest("hex")}`}var Be=t=>new Promise(e=>setTimeout(e,t)),ze=class{entries=[];seq=0;prevStepSeq=0;curStepSeq=0;listener;context;attach(e,n){this.context=e,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(()=>{})}},e.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(e=>e.seq>this.prevStepSeq)}all(){return this.entries}};function cr(t,e){if(!e)return t>=200&&t<300;let n=/^([1-5])xx$/i.exec(e.trim());if(n){let r=Number(n[1])*100;return t>=r&&t<r+100}return t===Number(e.trim())}function At(t,e,n,r,s){return!(!e.test(t.url)||n&&t.method.toUpperCase()!==n.toUpperCase()||!cr(t.status,r)||s&&!s.test(t.body??""))}var lr=(t,e)=>`response matching ${t.method??"any-method"} /${e}/ status ${t.status??"2xx"}${t.pattern?` body /${t.pattern}/`:""}`;function Ct(t,e,n,r=!1){let s=a=>ie(Ue(V(a,n))),o=[],i=a=>{if(e.role){let d=e.nameRegex?new RegExp(s(e.nameRegex),"i"):e.name?s(e.name):void 0;o.push(a.getByRole(e.role,{name:d,exact:!1}))}e.label&&o.push(a.getByLabel(s(e.label))),e.placeholder&&o.push(a.getByPlaceholder(s(e.placeholder))),e.text&&o.push(a.getByText(s(e.text))),e.testId&&o.push(a.getByTestId(s(e.testId)));for(let d of e.fallbackSelectors??[])o.push(a.locator(d))};if(e.frame)i(t.frameLocator(s(e.frame)));else if(i(t),r)for(let a of t.frames())a!==t.mainFrame()&&i(a);if(o.length===0)throw new Error("Action target has neither role nor fallback selectors");return o}async function B(t,e,n){let r=Date.now()+e,s;for(;Date.now()<r;){try{if(await t())return}catch(o){s=o}await Be(100)}throw new Error(`Timed out waiting for ${n}${s?` (${String(s)})`:""}`)}async function U(t,e){let n;for(let r of t)try{await e(r.first());return}catch(s){n=s}throw n}var Ne=t=>t.replace(/\s+/g," ").trim();async function Ve(t,e,n,r,s,o=new Map,i){let a=u=>ie(Ue(V(u,n))),d=e.value!==void 0?a(e.value):void 0,p=e.url!==void 0?a(e.url):void 0,m=e.action==="expectVisible"||e.action==="expectText"||e.action==="expectValue"||e.action==="expectMatches"||e.action==="expectAttribute"||e.action==="captureText",l=e.target?Ct(t,e.target,n,m):[],c={timeout:r},g={timeout:Math.min(1e3,r)},h=u=>async()=>{for(let f of l)try{if(await u(f.first()))return!0}catch{}return!1};switch(e.action){case"goto":{if(!p)throw new Error("goto requires url");let u=/^https?:\/\//.test(p)?p:new URL(p,s).toString();await t.goto(u,c);return}case"click":return U(l,u=>u.click(c));case"fill":return U(l,u=>u.fill(d??"",c));case"press":return l.length>0?U(l,u=>u.press(d??"",c)):t.keyboard.press(d??"");case"selectOption":return U(l,async u=>{await u.selectOption(d??"",c)});case"check":return U(l,u=>u.check(c));case"uncheck":return U(l,u=>u.uncheck(c));case"hover":return U(l,u=>u.hover(c));case"waitFor":return l.length>0?U(l,u=>u.waitFor({state:"visible",timeout:r})):Be(Math.min(Number(d??"0"),r));case"expectVisible":return B(h(u=>u.isVisible()),r,"target to be visible");case"expectNotVisible":return U(l,u=>u.waitFor({state:"hidden",timeout:r}));case"expectText":return B(h(async u=>(await u.textContent(g)??"").includes(d??"")),r,`text "${d}"`);case"expectValue":return B(h(async u=>await u.inputValue(g)===(d??"")),r,`input value "${d}"`);case"expectUrl":{let u=p??d??"";return B(async()=>t.context().pages().some(f=>f.url().includes(u)),r,`URL containing "${u}" in any tab (current: ${t.context().pages().map(f=>f.url()).join(", ")})`)}case"handleDialog":{let u=(d??"accept")!=="dismiss",f=e.promptText?a(e.promptText):void 0;t.once("dialog",y=>{u?y.accept(f):y.dismiss()});return}case"uploadFile":{let u=(d??"").split(`
26
- `).filter(Boolean).map(f=>Et.isAbsolute(f)?f:Et.resolve(f));if(u.length===0)throw new Error("uploadFile requires a path");if(l.length>0)return U(l,f=>f.setInputFiles(u,c));t.once("filechooser",f=>{f.setFiles(u)});return}case"dragTo":{if(!e.toTarget)throw new Error("dragTo requires toTarget");let u=Ct(t,e.toTarget,n);return U(l,f=>f.dragTo(u[0].first(),c))}case"captureText":{if(!e.saveAs)throw new Error("captureText requires saveAs");return B(h(async u=>{let f=Ne(await u.textContent(g)??"");return f===""?!1:(o.set(e.saveAs,f),!0)}),r,`non-empty text to capture as "${e.saveAs}"`)}case"expectDiffers":{if(!e.compareTo)throw new Error("expectDiffers requires compareTo");let u=o.get(e.compareTo);if(u===void 0)throw new Error(`expectDiffers: no captured value named "${e.compareTo}"`);if(l.length>0)return B(h(async x=>Ne(await x.textContent(g)??"")!==u),r,`target text to differ from "${e.compareTo}" (${u})`);let f=d??"",y=o.get(f);if(y===void 0)throw new Error(`expectDiffers: no captured value named "${f}"`);if(y===u)throw new Error(`expectDiffers: "${f}" (${y}) equals "${e.compareTo}" (${u})`);return}case"expectAttribute":{if(!e.attribute)throw new Error("expectAttribute requires attribute");if(!e.pattern)throw new Error("expectAttribute requires pattern");let u=new RegExp(e.pattern,"i");return B(h(async f=>u.test(await f.getAttribute(e.attribute,g)??"")),r,`attribute ${e.attribute} to match /${e.pattern}/i`)}case"expectMatches":{if(!e.pattern)throw new Error("expectMatches requires pattern");let u=new RegExp(e.pattern,"i");return B(h(async f=>u.test(Ne(await f.textContent(g)??""))),r,`target text to match /${e.pattern}/i`)}case"waitForResponse":case"expectResponse":{if(!e.urlPattern)throw new Error(`${e.action} requires urlPattern`);let u=new RegExp(a(e.urlPattern)),f=e.pattern?new RegExp(a(e.pattern)):void 0;if(!i){await t.waitForResponse(x=>At({seq:0,url:x.url(),method:x.request().method(),status:x.status()},u,e.method,e.status,void 0),c);return}let y=()=>e.action==="expectResponse"?i.all():i.sincePreviousStep();return B(async()=>y().some(x=>At(x,u,e.method,e.status,f)),r,lr(e,e.urlPattern))}default:throw new Error(`Unknown action type: ${e.action}`)}}function Pt(t,e){return t.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(e)))}function dr(t,e){return e.steps.length!==t.steps.length?!1:e.steps.every((n,r)=>{let s=t.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(L(o)),d=Object.keys(L(i));if(a.length>0&&d.length>0){if(JSON.stringify(a)!==JSON.stringify(d))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=e.steps.some(p=>Pt(p,"<docstring>")),d=e.steps.some(p=>Pt(p,n.docString));if(!a||d)return!1}return!0})}function qe(t,e){let n=t.steps[e],r=n?.resolvedTable??n?.table;return{...t.params,...K(t.steps),...L(r),...q(n?.resolvedDocString??n?.docString)}}async function be(t,e,n,r={}){let s=r.actionTimeoutMs??5e3,o=r.retries??1,i=[],a=r.vars??new Map,d=()=>Object.fromEntries(a),p=n.steps.some(g=>g.actions.some(h=>(h.action==="waitForResponse"||h.action==="expectResponse")&&h.pattern!==void 0)),m=new ze;m.attach(t.context(),p);let l=t,c=()=>{let g=t.context().pages(),h=g[g.length-1];h&&h!==l&&!h.isClosed()&&(l=h)};if(!dr(e,n))return m.detach(),{status:"stale",stepResults:i,captured:d()};try{for(let g=0;g<n.steps.length;g++){let h=n.steps[g],u=qe(e,g);m.markStep();let f=!1;for(let y=0;y<h.actions.length;y++){let x=h.actions[y];c(),x.pageFingerprint&&await Y(l)!==x.pageFingerprint&&(f=!0);let b,w=!1;for(let k=0;k<=o&&!w;k++){k>0&&(await Be(250*k),c());try{await Ve(l,x,u,s,r.baseURL,a,m),w=!0}catch(v){b=v}}if(!w){let k=b instanceof Error?b.message:String(b);i.push({gherkin:h.gherkin,fromStepSet:h.fromStepSet,kind:h.kind,status:"failed",drift:f,error:k});for(let v=g+1;v<n.steps.length;v++)i.push({gherkin:n.steps[v].gherkin,kind:n.steps[v].kind,status:"skipped"});return{status:"failed",failure:{stepIndex:g,actionIndex:y,stepText:h.gherkin,kind:h.kind,error:k},stepResults:i,captured:d()}}}i.push({gherkin:h.gherkin,fromStepSet:h.fromStepSet,kind:h.kind,status:"passed",drift:f})}return{status:"passed",stepResults:i,captured:d()}}finally{m.detach()}}var ve={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0};async function hr(){return new Promise((t,e)=>{let n=ur.createServer();n.listen(0,"127.0.0.1",()=>{let{port:r}=n.address();n.close(()=>t(r))}),n.on("error",e)})}function le(t){let e=t.contexts().flatMap(n=>n.pages());return[...e].reverse().find(n=>n.url()!=="about:blank")??e.at(-1)}function Dt(t){let e=t.length;for(let n=t.length-1;n>=0&&t[n].kind==="assertion";n--)e=n;return e}function It(t,e){return t[e]?.kind==="assertion"&&e>=Dt(t)}function jt(t,e,n=!1,r){let s=Dt(t);return t.map((o,i)=>{let a=e[i],d=a&&a.actions.length>0;return o.kind==="assertion"?n&&i<s&&d?a:o:d?r&&i===r.stepIndex&&r.actionIndex>0?{...a,actions:[...o.actions.slice(0,r.actionIndex),...a.actions]}:a:o})}function yr(t,e){return t.map((n,r)=>{let s=e[r];return s&&s.actions.length>0?s:n})}function _t(t,e){return{aiCalls:t.aiCalls+e.aiCalls,inputTokens:t.inputTokens+e.inputTokens,outputTokens:t.outputTokens+e.outputTokens,cacheReadTokens:t.cacheReadTokens+e.cacheReadTokens,cacheCreationTokens:t.cacheCreationTokens+e.cacheCreationTokens,costUsd:(t.costUsd??0)+(e.costUsd??0)}}var we=class{constructor(e){this.options=e}options;browser;cdpEndpoint;stepIndex;getStepIndex(){if(!this.stepIndex&&(this.stepIndex=H(this.options.projectRoot),this.stepIndex.steps.size>0)){let e=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)${e}`)}return this.stepIndex}async recordWithSeeding(e,n){let r=this.getStepIndex(),s=e.steps.length,o={...e.params,...K(e.steps)},i=e.steps.map(x=>{let b=r.steps.size>0?pt(r,x,o):void 0;return b?{gherkin:x.text,keyword:x.keyword,kind:x.kind,table:x.table,docString:x.docString,fromStepSet:x.source?.fromStepSet,actions:b.actions}:void 0}),a=e.steps.map(x=>({gherkin:x.text,keyword:x.keyword,kind:x.kind,table:x.table,docString:x.docString,fromStepSet:x.source?.fromStepSet,actions:[]})),d=new Map,p=[],m=[],l=[],c=[],g={...ve},h=0,u=0,f=0,y=x=>({success:x,narrative:m.join(" ")||"Composed entirely from seeded recordings.",adaptations:p,suggestedFeatureEdit:c.length>0?c.join(`
27
- `):void 0,featureEdits:l,recordedSteps:a,announcedSteps:[],usage:g,seededCount:h});for(;f<s;){if(i[f]){let v=f;for(;v<s&&i[v];)v++;let j=i.slice(f,v),S={...e,steps:e.steps.slice(f,v)},T=await be(n,S,this.buildCandidateCache(e,j),{baseURL:this.options.baseURL,actionTimeoutMs:this.options.actionTimeoutMs,retries:this.options.retries,vars:d}),C=T.status==="passed"?v-f:T.failure?.stepIndex??0;for(let _=0;_<C;_++)a[f+_]=j[_];if(h+=C,T.status==="passed"){f=v;continue}let F=f+C;p.push(`Seeded recording for "${e.steps[F].text}" did not replay in this scenario's context; the agent re-recorded it fresh. Divergence signal.`),i[F]=void 0,f=F;continue}if(!this.options.agent)return y(!1);u++;let x=f;for(;x<s&&!i[x];)x++;u>=3&&(x=s);let b=await this.withDialogsVisible(()=>this.options.agent.run({scenario:e,baseURL:this.options.baseURL,mode:"record",cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:f>0?e.steps.slice(0,f).map(v=>v.resolvedText):void 0,recordOnly:x<s?{from:f,to:x-1}:void 0,takeFingerprint:async()=>Y(le(this.browser)??n)}));if(g=_t(g,b.usage),m.push(b.narrative),p.push(...b.adaptations),l.push(...b.featureEdits),b.suggestedFeatureEdit&&c.push(b.suggestedFeatureEdit),!b.success)return y(!1);let w=b.announcedSteps.filter(v=>v>=f&&v<s),k=Math.min(s-1,Math.max(w.length>0?Math.max(...w):x-1,f));for(let v=f;v<=k;v++)a[v]=b.recordedSteps[v];f=k+1}return y(!0)}async withDialogsVisible(e){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 e()}finally{for(let i of s)i.off("page",o);for(let i of r)try{i.off("dialog",n)}catch{}}}async start(){let e=this.options.browser??"chromium";if(e==="chromium"){let n=await hr();this.browser=await fr.launch({headless:!this.options.headed,args:[`--remote-debugging-port=${n}`]}),this.cdpEndpoint=`http://127.0.0.1:${n}`}else this.browser=await(e==="firefox"?gr:mr).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 e=Ft.isAbsolute(this.options.storageState)?this.options.storageState:Ft.join(this.options.projectRoot,this.options.storageState);if(!pr.existsSync(e))throw new Error(`storageState file not found: ${e}. Generate one with: npx playwright open --save-storage=${this.options.storageState} <url>`);return e}buildCandidateCache(e,n){return{version:1,feature:e.featurePath,scenario:e.name,recordedAt:new Date().toISOString(),recordedBy:"agent",steps:n}}async proofReplay(e,n){let r=await this.browser.newContext({storageState:this.resolveStorageState()}),s=await r.newPage();return{result:await be(s,e,n,{baseURL:this.options.baseURL,actionTimeoutMs:this.options.actionTimeoutMs,retries:this.options.retries}),context:r,page:s}}async runScenario(e,n={}){let r=await this.runScenarioInner(e,n),s=ae(e);if(s.size>0){let o=i=>i===void 0?i:te(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=>te(i,s));for(let i of r.stepResults)i.error=o(i.error)}return r}async runScenarioInner(e,n={}){if(!this.browser)throw new Error("Orchestrator not started");let r=Date.now(),{projectRoot:s,baseURL:o,actionTimeoutMs:i,retries:a}=this.options,d=W(s,e.featurePath,e.name),p=N(d),m=await this.browser.newContext({storageState:this.resolveStorageState()}),l=await m.newPage();try{let c;if(p&&(c=await be(l,e,p,{baseURL:o,actionTimeoutMs:i,retries:a}),c.status==="passed"))return{scenario:e,status:"green",source:"cache",stepResults:c.stepResults,adaptations:[],usage:ve,durationMs:Date.now()-r};if(!(this.options.agent!==void 0&&this.cdpEndpoint!==void 0&&!n.agentDisabled)){let y=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:e,status:"red",source:"cache",stepResults:c?.stepResults??[],adaptations:[],usage:ve,durationMs:Date.now()-r,error:p?c?.status==="stale"?`Cache is stale (feature file changed) and ${y}`:`Replay failed and ${y} ${c?.failure?.error??""}`.trimEnd():`No cache exists and ${y}`}}let h=p!==void 0&&c!==void 0&&c.status==="failed",u=0,f;try{if(h)f=await this.withDialogsVisible(()=>this.options.agent.run({scenario:e,baseURL:o,mode:"heal",model:this.options.healModel,cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:c.stepResults.filter(y=>y.status==="passed").map(y=>y.gherkin),failedStep:{text:c.failure.stepText,kind:c.failure.kind,error:c.failure.error},takeFingerprint:async()=>{let y=le(this.browser)??l;return Y(y)}}));else if(await l.goto("about:blank"),this.options.reuseSteps!==!1){let y=await this.recordWithSeeding(e,l);u=y.seededCount,f=y}else f=await this.withDialogsVisible(()=>this.options.agent.run({scenario:e,baseURL:o,mode:"record",cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,takeFingerprint:async()=>{let y=le(this.browser)??l;return Y(y)}}))}catch(y){let x=y instanceof Error?y.message:String(y);return{scenario:e,status:"red",source:h?"agent-heal":"agent-record",stepResults:c?.stepResults??[],adaptations:[],usage:ve,durationMs:Date.now()-r,error:`Agent invocation failed: ${x.split(`
28
- `)[0]}`}}if(f.success&&h&&c.failure.kind==="assertion"&&(this.options.assertionPolicy!=="adaptable-mid"||It(p.steps,c.failure.stepIndex))){let y=p.steps[c.failure.stepIndex],x=le(this.browser)??l,b=new Map(Object.entries(c.captured)),w=qe(e,c.failure.stepIndex);try{for(let k of y.actions)await Ve(x,k,w,this.options.actionTimeoutMs??5e3,o,b)}catch(k){let v=k instanceof Error?k.message:String(k);return{scenario:e,status:"red",source:"agent-heal",stepResults:c.stepResults,narrative:f.narrative,adaptations:f.adaptations,suggestedFeatureEdit:f.suggestedFeatureEdit,usage:f.usage,durationMs:Date.now()-r,error:`Assertion failed as written: "${y.gherkin}". Saffron never adapts assertions \u2014 if this change is intended, update the .feature file or re-record the scenario. Verification error: ${v.split(`
29
- `)[0]}`}}}return await this.finalizeAgentRun(e,f,h,p,r,u,h?c?.failure:void 0)}finally{await m.close()}}async finalizeAgentRun(e,n,r,s,o,i=0,a){let d=r?"agent-heal":"agent-record",p=n.recordedSteps.map(w=>({gherkin:w.gherkin,kind:w.kind,status:n.success?"passed":"failed"}));if(!n.success)return{scenario:e,status:"red",source:d,stepResults:p,narrative:n.narrative,adaptations:n.adaptations,usage:n.usage,durationMs:Date.now()-o,error:n.narrative};let m=r?jt(s.steps,n.recordedSteps,this.options.assertionPolicy==="adaptable-mid",a?{stepIndex:a.stepIndex,actionIndex:a.actionIndex}:void 0):n.recordedSteps,l=[...n.adaptations],c={...n.usage},g,h;if(this.options.verifyProposals!==!1){let w=await this.proofReplay(e,this.buildCandidateCache(e,m));try{if(w.result.status!=="passed"&&w.result.failure){let k=w.result.failure;if(this.options.agent&&!(r&&k.kind==="assertion"&&(this.options.assertionPolicy!=="adaptable-mid"||It(s.steps,k.stepIndex))))try{let j=await this.withDialogsVisible(()=>this.options.agent.run({scenario:e,baseURL:this.options.baseURL,mode:"refine",model:r?this.options.healModel:void 0,cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:w.result.stepResults.filter(S=>S.status==="passed").map(S=>S.gherkin),failedStep:{text:k.stepText,kind:k.kind,error:k.error},takeFingerprint:async()=>Y(le(this.browser)??w.page)}));c=_t(c,j.usage),j.success&&(l.push(...j.adaptations),m=r?jt(m,j.recordedSteps,this.options.assertionPolicy==="adaptable-mid"):yr(m,j.recordedSteps),await w.context.close(),w=await this.proofReplay(e,this.buildCandidateCache(e,m)))}catch{}}g=w.result.status==="passed",g||(h=w.result.failure?.error??`proof replay ${w.result.status}`)}finally{await w.context.close().catch(()=>{})}}let u=[...n.featureEdits];for(let w of ut(m,H(this.options.projectRoot)))l.push(`duplicate wording: "${w.from}" recorded exactly the same actions as the existing step "${w.to}" (${w.feature} \u203A ${w.scenario}). Accept with --with-feature-edit to converge on the canonical wording.`),u.some(k=>k.index===w.index)||u.push({index:w.index,text:w.to});let f=ae(e),y=w=>f.size>0?te(w,f):w;l=l.map(y);let x={meta:{createdAt:new Date().toISOString(),mode:r?"heal":"record",narrative:y(n.narrative),adaptations:l,suggestedFeatureEdit:n.suggestedFeatureEdit?y(n.suggestedFeatureEdit):void 0,featureEdits:u.length>0?u:void 0,verified:g,proofError:h?y(h):void 0,seededSteps:i>0?i:void 0,usage:c},cache:{version:1,feature:e.featurePath,scenario:e.name,recordedAt:new Date().toISOString(),recordedBy:"agent",steps:m}},b=it(this.options.projectRoot,e.featurePath,e.name);return kt(b,x),{scenario:e,status:"yellow",source:d,stepResults:p,narrative:n.narrative,adaptations:l,suggestedFeatureEdit:n.suggestedFeatureEdit,featureEdits:u.length>0?u:void 0,verified:g,proofError:h,seededSteps:i>0?i:void 0,proposalFile:b,usage:c,durationMs:Date.now()-o}}};import Gt from"node:path";import{createRequire as $r}from"node:module";import{query as Rr}from"@anthropic-ai/claude-agent-sdk";var ke=class{steps;currentStepIndex=-1;done=!1;success=!1;narrative="";adaptations=[];suggestedFeatureEdit;featureEdits=[];announced=new Set;pendingFingerprint;constructor(e){this.steps=e.steps.map(n=>({gherkin:n.text,keyword:n.keyword,kind:n.kind,table:n.table,docString:n.docString,fromStepSet:n.source?.fromStepSet,actions:[]}))}addArmedAction(e){if(this.done)return;let n=Math.max(this.currentStepIndex,0),r=this.steps[n];if(!r||r.actions.some(i=>i.action===e.action&&i.value===e.value&&i.promptText===e.promptText))return;let o=Math.max(r.actions.length-1,0);r.actions.splice(o,0,e)}addActions(e){if(this.done||e.length===0)return;let n=Math.max(this.currentStepIndex,0),r=this.steps[n];r&&(this.pendingFingerprint&&e[0]&&(e[0].pageFingerprint=this.pendingFingerprint,this.pendingFingerprint=void 0),r.actions.push(...e))}},J=t=>t.replace(/\\(['"\\])/g,"$1");function Sr(t){let e=t.match(/'((?:[^'\\]|\\.)*)'/);return e?J(e[1]):void 0}function We(t){let e={},n=t.match(/(?:frameLocator\('((?:[^'\\]|\\.)*)'\)|locator\('((?:[^'\\]|\\.)*)'\)\.contentFrame\(\))/);n&&(e.frame=J(n[1]??n[2]),t=t.slice((n.index??0)+n[0].length));let r=t.match(/getByRole\('([^']+)'(?:,\s*\{([^}]*)\})?\)/);if(r){e.role=r[1];let i=(r[2]??"").match(/name:\s*'((?:[^'\\]|\\.)*)'/);i&&(e.name=J(i[1]))}let s=[[/getByLabel\('((?:[^'\\]|\\.)*)'\)/,"label"],[/getByText\('((?:[^'\\]|\\.)*)'\)/,"text"],[/getByTestId\('((?:[^'\\]|\\.)*)'\)/,"testId"],[/getByPlaceholder\('((?:[^'\\]|\\.)*)'\)/,"placeholder"]];for(let[i,a]of s){let d=t.match(i);d&&(e[a]=J(d[1]))}let o=t.match(/(?:^|\.)locator\('((?:[^'\\]|\\.)*)'\)/);return o&&(e.fallbackSelectors=[J(o[1])]),Object.keys(e).length>0?e:void 0}function xr(t,e){if(e&&t.startsWith(e)){let n=t.slice(e.length);return n.startsWith("/")?n:`/${n}`}return t}var br={click:"click",dblclick:"click",fill:"fill",press:"press",check:"check",uncheck:"uncheck",hover:"hover",selectOption:"selectOption"};function vr(t,e){let n=t.match(/page\.goto\('((?:[^'\\]|\\.)*)'\)/);if(n)return{action:"goto",url:xr(J(n[1]),e)};let r=t.match(/page\.keyboard\.press\('((?:[^'\\]|\\.)*)'\)/);if(r)return{action:"press",value:J(r[1])};let s=t.match(/await\s+page\.(.+)\.dragTo\(page\.(.+?)\);?\s*$/);if(s){let c=We(s[1]),g=We(s[2]);if(c&&g)return{action:"dragTo",target:c,toTarget:g}}let o=t.match(/await\s+page\.(.+)\.(click|dblclick|fill|press|check|uncheck|hover|selectOption)\((.*)\);?\s*$/);if(!o)return;let[,i,a,d]=o,p=We(i);if(!p)return;let m={action:br[a],target:p},l=Sr(d);return l!==void 0&&a!=="click"&&a!=="dblclick"&&(m.value=l),m}function Ot(t,e){let n=[],r=Array.isArray(t)?t:t?.content;if(Array.isArray(r))for(let o of r){let i=o?.text;typeof i=="string"&&n.push(i)}else typeof t=="string"&&n.push(t);let s=[];for(let o of n)for(let i of o.split(`
30
- `)){let a=vr(i.trim(),e);a&&s.push(a)}return s}function He(t,e){let n=t;for(let[r,s]of Object.entries(e).filter(([,o])=>o.length>=3).sort((o,i)=>i[1].length-o[1].length))n=n.split(s).join(`<${r}>`);return n}function Mt(t,e){if(Object.entries(e).filter(([,s])=>s.length>=3).length===0)return t;let r=s=>s===void 0?void 0:He(s,e);return t.map(s=>({...s,actions:s.actions.map(o=>({...o,value:r(o.value),url:r(o.url),target:o.target?{...o.target,name:r(o.target.name),label:r(o.target.label),text:r(o.target.text),placeholder:r(o.target.placeholder)}:void 0}))}))}function Lt(t,e){let n=K(e.steps);return t.map((r,s)=>{let o=e.steps[s],i=o?.resolvedTable??o?.table,a=o?.resolvedDocString??o?.docString,d=Object.entries({...n,...L(i),...q(a)}).filter(([,m])=>m.length>=3).sort((m,l)=>l[1].length-m[1].length);if(d.length===0)return r;let p=m=>{if(m===void 0)return;let l=m;for(let[c,g]of d)l=l.split(g).join(`<${c}>`);return l};return{...r,actions:r.actions.map(m=>({...m,value:p(m.value),url:p(m.url),target:m.target?{...m.target,name:p(m.target.name),label:p(m.target.label),text:p(m.target.text),placeholder:p(m.target.placeholder)}:void 0}))}})}function Ut(t,e=new Date){let n=r=>r===void 0?void 0:Tt(r,e);return t.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 Nt(t,e){if(e.size===0)return t;let n=r=>r===void 0?void 0:te(r,e);return t.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 zt(t,e){let n=e??{};if(t==="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(t==="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(`
31
- `)}}}import{createSdkMcpServer as wr,tool as ne}from"@anthropic-ai/claude-agent-sdk";import{z as $}from"zod";var D=t=>({content:[{type:"text",text:t}]});function Bt(t){let e={};return t.role&&(e.role=t.role),t.name&&(e.name=t.name),t.nameRegex&&(e.nameRegex=t.nameRegex),t.label&&(e.label=t.label),t.text&&(e.text=t.text),t.selector&&(e.fallbackSelectors=[t.selector]),t.frame&&(e.frame=t.frame),Object.keys(e).length>0?e:void 0}var Vt={role:$.string().optional().describe("ARIA role of the element"),name:$.string().optional().describe("Accessible name \u2014 use the SHORTEST stable substring; never include volatile content like prices or dates"),nameRegex:$.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:$.string().optional().describe("Form-label text for getByLabel \u2014 the literal <label> text only, never a free-text description"),text:$.string().optional().describe("getByText locator text"),selector:$.string().optional().describe("CSS fallback selector"),frame:$.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 qt(t){return wr({name:"saffron",version:"0.1.0",tools:[ne("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:$.number().int().min(0),text:$.string(),adaptation:$.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 e=>(t.currentStepIndex=e.index,t.announced.add(e.index),e.adaptation&&t.adaptations.push(`Step ${e.index+1} ("${e.text}"): ${e.adaptation}`),D(`Recording step ${e.index}: ${e.text}`))),ne("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:$.string().describe("Name to store the captured text under"),...Vt},async e=>{let n=Bt(e);return n?(t.addActions([{action:"captureText",target:n,saveAs:e.saveAs}]),D(`Capture recorded as "${e.saveAs}".`)):D("ERROR: capture needs a target (role/name, nameRegex, label, text, or selector).")}),ne("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:$.boolean().describe("true = accept/OK the dialog, false = dismiss/cancel"),promptText:$.string().optional().describe("Text typed into a prompt() before accepting")},async e=>(t.addArmedAction({action:"handleDialog",value:e.accept?"accept":"dismiss",...e.promptText?{promptText:e.promptText}:{}}),D(`Dialog handling recorded (${e.accept?"accept":"dismiss"}).`))),ne("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:$.string().describe("Regex matched against the response URL \u2014 path-only for portability"),method:$.string().optional().describe("HTTP method to match (any if omitted)"),status:$.string().optional().describe("Expected status \u2014 exact ('201') or class ('2xx'); default any 2xx"),bodyPattern:$.string().optional().describe(`Regex the response body must match, e.g. '"status"\\s*:\\s*"READY"' for polling waits`)},async e=>(t.addActions([{action:"waitForResponse",urlPattern:e.urlPattern,...e.method?{method:e.method}:{},...e.status?{status:e.status}:{},...e.bodyPattern?{pattern:e.bodyPattern}:{}}]),D("Network wait recorded."))),ne("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:$.enum(["visible","notVisible","text","url","value","differs","matches","attribute","response"]),...Vt,expected:$.string().optional().describe("Expected text/value for kind=text|value \u2014 stable text only, never volatile values"),url:$.string().optional().describe("URL substring for kind=url (path only, no host)"),pattern:$.string().optional().describe("kind=matches|attribute: regex the target's text (or attribute) must match, e.g. 'NOK [\\\\d,]+' for a price"),attribute:$.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:$.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:$.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:$.string().optional().describe("kind=response: HTTP method to match (any if omitted)"),status:$.string().optional().describe("kind=response: expected status \u2014 exact ('201') or class ('2xx'); default any 2xx"),bodyPattern:$.string().optional().describe("kind=response: regex the response body must match (verify via browser_network_requests first)"),compareTo:$.string().optional().describe("kind=differs: name of the captured value to compare against")},async e=>{let n=Bt(e),r;switch(e.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:e.expected??""};break;case"value":r={action:"expectValue",target:n,value:e.expected??""};break;case"url":r={action:"expectUrl",url:e.url??""};break;case"differs":if(!e.compareTo)return D("ERROR: kind=differs requires compareTo.");if(!e.capture&&!n)return D("ERROR: kind=differs needs either capture (a stored name) or a target element.");r={action:"expectDiffers",target:n,value:e.capture,compareTo:e.compareTo};break;case"matches":if(!e.pattern)return D("ERROR: kind=matches requires pattern.");r={action:"expectMatches",target:n,pattern:e.pattern};break;case"attribute":if(!e.attribute||!e.pattern)return D("ERROR: kind=attribute requires attribute and pattern.");r={action:"expectAttribute",target:n,attribute:e.attribute,pattern:e.pattern};break;case"response":if(!e.urlPattern)return D("ERROR: kind=response requires urlPattern.");r={action:"expectResponse",urlPattern:e.urlPattern,...e.method?{method:e.method}:{},...e.status?{status:e.status}:{},...e.bodyPattern?{pattern:e.bodyPattern}:{}};break}return!["url","differs","response"].includes(e.kind)&&!n?D("ERROR: assertion needs a target (role/name, nameRegex, label, text, or selector)."):(t.addActions([r]),D("Assertion recorded."))}),ne("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:$.boolean(),narrative:$.string().describe("2-4 sentence summary of the run: what happened, and why it failed if it did."),suggestedFeatureEdit:$.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:$.array($.object({index:$.number().int().min(0).describe("zero-based step index"),text:$.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 e=>(t.done=!0,t.success=e.success,t.narrative=e.narrative,t.suggestedFeatureEdit=e.suggestedFeatureEdit,t.featureEdits=e.featureEdits??[],D("Run finalized.")))]})}function Wt(t={}){let e=t.deliberateSnapshots?`
23
+ Defined in StepSet **${f.fromStepSet}**`:""}`}}:void 0}}),r.onDidOpen(l=>m(l.document)),r.onDidChangeContent(l=>m(l.document)),r.onDidSave(()=>d()),n.onDidChangeWatchedFiles(()=>d()),r.listen(n),n.listen()}import Rt from"node:fs";import ir from"node:path";var $t=/\{env:([A-Za-z_][A-Za-z0-9_]*)\}/g;function ie(t){return t.replace($t,(e,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 Ne(t){let e=new Set;for(let n of t)if(n)for(let r of n.matchAll($t))e.add(r[1]);return e}function Be(t){let e=[];for(let n of t.steps){e.push(n.text,n.resolvedText),n.docString&&e.push(n.docString);for(let r of n.table??[])e.push(...r)}return e}function ae(t){let e=new Map;for(let n of Ne(Be(t))){let r=process.env[n];r!==void 0&&e.set(n,r)}return e}function te(t,e){let n=t;for(let[r,s]of[...e.entries()].sort((o,i)=>i[1].length-o[1].length))s.length<4||(n=n.split(s).join(`{env:${r}}`));return n}function Tt(t){let e=ir.join(t,".env");if(Rt.existsSync(e))for(let n of Rt.readFileSync(e,"utf8").split(`
24
+ `)){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)}}import mr from"node:fs";import hr from"node:net";import Dt from"node:path";import{chromium as yr,firefox as Sr,webkit as xr}from"playwright";import B from"node:fs";import Se from"node:path";function Et(t,e){B.mkdirSync(Se.dirname(t),{recursive:!0}),B.writeFileSync(t,`${JSON.stringify(e,null,2)}
25
+ `)}function xe(t){return JSON.parse(B.readFileSync(t,"utf8"))}function ce(t){let e=Se.join(t,".saffron","proposals");if(!B.existsSync(e))return[];let n=[];for(let r of B.readdirSync(e)){let s=Se.join(e,r);if(B.statSync(s).isDirectory())for(let o of B.readdirSync(s)){if(!o.endsWith(".json"))continue;let i=Se.join(s,o);n.push({file:i,proposal:xe(i)})}}return n}function At(t,e){let n=xe(e),r=W(t,n.cache.feature,n.cache.scenario);return ee(r,n.cache),B.rmSync(e),r}function Ct(t){B.rmSync(t)}import Ft from"node:path";var ar=/\{date([+-]\d+)?(?::([^}]+))?\}/g;function cr(t,e){let n=String(t.getFullYear()),r=String(t.getMonth()+1).padStart(2,"0"),s=String(t.getDate()).padStart(2,"0");return e.replaceAll("YYYY",n).replaceAll("MM",r).replaceAll("DD",s).replaceAll("M",String(t.getMonth()+1)).replaceAll("D",String(t.getDate()))}function ze(t,e=new Date){return t.replace(ar,(n,r,s)=>{let o=new Date(e);return o.setDate(o.getDate()+(r?parseInt(r,10):0)),cr(o,s??"YYYY-MM-DD")})}var lr=/\b(\d{4})-(\d{2})-(\d{2})\b/g,dr=400;function Pt(t,e=new Date){let n=new Date(e.getFullYear(),e.getMonth(),e.getDate());return t.replace(lr,(r,s,o,i)=>{let a=new Date(Number(s),Number(o)-1,Number(i));if(Number.isNaN(a.getTime()))return r;let d=Math.round((a.getTime()-n.getTime())/864e5);return Math.abs(d)>dr?r:d===0?"{date}":`{date${d>0?"+":""}${d}}`})}import{createHash as pr}from"node:crypto";async function J(t){let e;try{e=await t.locator("body").ariaSnapshot({timeout:3e3})}catch{return"sha256:unavailable"}let n=e.replace(/\d+/g,"#").replace(/\s+/g," ").trim();return`sha256:${pr("sha256").update(n).digest("hex")}`}var We=t=>new Promise(e=>setTimeout(e,t)),qe=class{entries=[];seq=0;prevStepSeq=0;curStepSeq=0;listener;context;attach(e,n){this.context=e,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(()=>{})}},e.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(e=>e.seq>this.prevStepSeq)}all(){return this.entries}};function ur(t,e){if(!e)return t>=200&&t<300;let n=/^([1-5])xx$/i.exec(e.trim());if(n){let r=Number(n[1])*100;return t>=r&&t<r+100}return t===Number(e.trim())}function It(t,e,n,r,s){return!(!e.test(t.url)||n&&t.method.toUpperCase()!==n.toUpperCase()||!ur(t.status,r)||s&&!s.test(t.body??""))}var fr=(t,e)=>`response matching ${t.method??"any-method"} /${e}/ status ${t.status??"2xx"}${t.pattern?` body /${t.pattern}/`:""}`;function jt(t,e,n,r=!1){let s=a=>ie(ze(V(a,n))),o=[],i=a=>{if(e.role){let d=e.nameRegex?new RegExp(s(e.nameRegex),"i"):e.name?s(e.name):void 0;o.push(a.getByRole(e.role,{name:d,exact:!1}))}e.label&&o.push(a.getByLabel(s(e.label))),e.placeholder&&o.push(a.getByPlaceholder(s(e.placeholder))),e.text&&o.push(a.getByText(s(e.text))),e.testId&&o.push(a.getByTestId(s(e.testId)));for(let d of e.fallbackSelectors??[])o.push(a.locator(d))};if(e.frame)i(t.frameLocator(s(e.frame)));else if(i(t),r)for(let a of t.frames())a!==t.mainFrame()&&i(a);if(o.length===0)throw new Error("Action target has neither role nor fallback selectors");return o}async function z(t,e,n){let r=Date.now()+e,s;for(;Date.now()<r;){try{if(await t())return}catch(o){s=o}await We(100)}throw new Error(`Timed out waiting for ${n}${s?` (${String(s)})`:""}`)}async function U(t,e){let n;for(let r of t)try{await e(r.first());return}catch(s){n=s}throw n}var Ve=t=>t.replace(/\s+/g," ").trim();async function He(t,e,n,r,s,o=new Map,i){let a=u=>ie(ze(V(u,n))),d=e.value!==void 0?a(e.value):void 0,p=e.url!==void 0?a(e.url):void 0,m=e.action==="expectVisible"||e.action==="expectText"||e.action==="expectValue"||e.action==="expectMatches"||e.action==="expectAttribute"||e.action==="captureText",l=e.target?jt(t,e.target,n,m):[],c={timeout:r},g={timeout:Math.min(1e3,r)},h=u=>async()=>{for(let f of l)try{if(await u(f.first()))return!0}catch{}return!1};switch(e.action){case"goto":{if(!p)throw new Error("goto requires url");let u=/^https?:\/\//.test(p)?p:new URL(p,s).toString();await t.goto(u,c);return}case"click":return U(l,u=>u.click(c));case"fill":return U(l,u=>u.fill(d??"",c));case"press":return l.length>0?U(l,u=>u.press(d??"",c)):t.keyboard.press(d??"");case"selectOption":return U(l,async u=>{await u.selectOption(d??"",c)});case"check":return U(l,u=>u.check(c));case"uncheck":return U(l,u=>u.uncheck(c));case"hover":return U(l,u=>u.hover(c));case"waitFor":return l.length>0?U(l,u=>u.waitFor({state:"visible",timeout:r})):We(Math.min(Number(d??"0"),r));case"expectVisible":return z(h(u=>u.isVisible()),r,"target to be visible");case"expectNotVisible":return U(l,u=>u.waitFor({state:"hidden",timeout:r}));case"expectText":return z(h(async u=>(await u.textContent(g)??"").includes(d??"")),r,`text "${d}"`);case"expectValue":return z(h(async u=>await u.inputValue(g)===(d??"")),r,`input value "${d}"`);case"expectUrl":{let u=p??d??"";return z(async()=>t.context().pages().some(f=>f.url().includes(u)),r,`URL containing "${u}" in any tab (current: ${t.context().pages().map(f=>f.url()).join(", ")})`)}case"handleDialog":{let u=(d??"accept")!=="dismiss",f=e.promptText?a(e.promptText):void 0;t.once("dialog",y=>{u?y.accept(f):y.dismiss()});return}case"uploadFile":{let u=(d??"").split(`
26
+ `).filter(Boolean).map(f=>Ft.isAbsolute(f)?f:Ft.resolve(f));if(u.length===0)throw new Error("uploadFile requires a path");if(l.length>0)return U(l,f=>f.setInputFiles(u,c));t.once("filechooser",f=>{f.setFiles(u)});return}case"dragTo":{if(!e.toTarget)throw new Error("dragTo requires toTarget");let u=jt(t,e.toTarget,n);return U(l,f=>f.dragTo(u[0].first(),c))}case"captureText":{if(!e.saveAs)throw new Error("captureText requires saveAs");return z(h(async u=>{let f=Ve(await u.textContent(g)??"");return f===""?!1:(o.set(e.saveAs,f),!0)}),r,`non-empty text to capture as "${e.saveAs}"`)}case"expectDiffers":{if(!e.compareTo)throw new Error("expectDiffers requires compareTo");let u=o.get(e.compareTo);if(u===void 0)throw new Error(`expectDiffers: no captured value named "${e.compareTo}"`);if(l.length>0)return z(h(async x=>Ve(await x.textContent(g)??"")!==u),r,`target text to differ from "${e.compareTo}" (${u})`);let f=d??"",y=o.get(f);if(y===void 0)throw new Error(`expectDiffers: no captured value named "${f}"`);if(y===u)throw new Error(`expectDiffers: "${f}" (${y}) equals "${e.compareTo}" (${u})`);return}case"expectAttribute":{if(!e.attribute)throw new Error("expectAttribute requires attribute");if(!e.pattern)throw new Error("expectAttribute requires pattern");let u=new RegExp(e.pattern,"i");return z(h(async f=>u.test(await f.getAttribute(e.attribute,g)??"")),r,`attribute ${e.attribute} to match /${e.pattern}/i`)}case"expectMatches":{if(!e.pattern)throw new Error("expectMatches requires pattern");let u=new RegExp(e.pattern,"i");return z(h(async f=>u.test(Ve(await f.textContent(g)??""))),r,`target text to match /${e.pattern}/i`)}case"waitForResponse":case"expectResponse":{if(!e.urlPattern)throw new Error(`${e.action} requires urlPattern`);let u=new RegExp(a(e.urlPattern)),f=e.pattern?new RegExp(a(e.pattern)):void 0;if(!i){await t.waitForResponse(x=>It({seq:0,url:x.url(),method:x.request().method(),status:x.status()},u,e.method,e.status,void 0),c);return}let y=()=>e.action==="expectResponse"?i.all():i.sincePreviousStep();return z(async()=>y().some(x=>It(x,u,e.method,e.status,f)),r,fr(e,e.urlPattern))}default:throw new Error(`Unknown action type: ${e.action}`)}}function _t(t,e){return t.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(e)))}function gr(t,e){return e.steps.length!==t.steps.length?!1:e.steps.every((n,r)=>{let s=t.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(L(o)),d=Object.keys(L(i));if(a.length>0&&d.length>0){if(JSON.stringify(a)!==JSON.stringify(d))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=e.steps.some(p=>_t(p,"<docstring>")),d=e.steps.some(p=>_t(p,n.docString));if(!a||d)return!1}return!0})}function Ge(t,e){let n=t.steps[e],r=n?.resolvedTable??n?.table;return{...t.params,...K(t.steps),...L(r),...q(n?.resolvedDocString??n?.docString)}}async function be(t,e,n,r={}){let s=r.actionTimeoutMs??5e3,o=r.retries??1,i=[],a=r.vars??new Map,d=()=>Object.fromEntries(a),p=n.steps.some(g=>g.actions.some(h=>(h.action==="waitForResponse"||h.action==="expectResponse")&&h.pattern!==void 0)),m=new qe;m.attach(t.context(),p);let l=t,c=()=>{let g=t.context().pages(),h=g[g.length-1];h&&h!==l&&!h.isClosed()&&(l=h)};if(!gr(e,n))return m.detach(),{status:"stale",stepResults:i,captured:d()};try{for(let g=0;g<n.steps.length;g++){let h=n.steps[g],u=Ge(e,g);m.markStep();let f=!1;for(let y=0;y<h.actions.length;y++){let x=h.actions[y];c(),x.pageFingerprint&&await J(l)!==x.pageFingerprint&&(f=!0);let b,w=!1;for(let k=0;k<=o&&!w;k++){k>0&&(await We(250*k),c());try{await He(l,x,u,s,r.baseURL,a,m),w=!0}catch(v){b=v}}if(!w){let k=b instanceof Error?b.message:String(b);i.push({gherkin:h.gherkin,fromStepSet:h.fromStepSet,kind:h.kind,status:"failed",drift:f,error:k});for(let v=g+1;v<n.steps.length;v++)i.push({gherkin:n.steps[v].gherkin,kind:n.steps[v].kind,status:"skipped"});return{status:"failed",failure:{stepIndex:g,actionIndex:y,stepText:h.gherkin,kind:h.kind,error:k},stepResults:i,captured:d()}}}i.push({gherkin:h.gherkin,fromStepSet:h.fromStepSet,kind:h.kind,status:"passed",drift:f})}return{status:"passed",stepResults:i,captured:d()}}finally{m.detach()}}var ve={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0};async function br(){return new Promise((t,e)=>{let n=hr.createServer();n.listen(0,"127.0.0.1",()=>{let{port:r}=n.address();n.close(()=>t(r))}),n.on("error",e)})}function le(t){let e=t.contexts().flatMap(n=>n.pages());return[...e].reverse().find(n=>n.url()!=="about:blank")??e.at(-1)}function Ut(t){let e=t.length;for(let n=t.length-1;n>=0&&t[n].kind==="assertion";n--)e=n;return e}function Ot(t,e){return t[e]?.kind==="assertion"&&e>=Ut(t)}function Mt(t,e,n=!1,r){let s=Ut(t);return t.map((o,i)=>{let a=e[i],d=a&&a.actions.length>0;return o.kind==="assertion"?n&&i<s&&d?a:o:d?r&&i===r.stepIndex&&r.actionIndex>0?{...a,actions:[...o.actions.slice(0,r.actionIndex),...a.actions]}:a:o})}function vr(t,e){return t.map((n,r)=>{let s=e[r];return s&&s.actions.length>0?s:n})}function Lt(t,e){return{aiCalls:t.aiCalls+e.aiCalls,inputTokens:t.inputTokens+e.inputTokens,outputTokens:t.outputTokens+e.outputTokens,cacheReadTokens:t.cacheReadTokens+e.cacheReadTokens,cacheCreationTokens:t.cacheCreationTokens+e.cacheCreationTokens,costUsd:(t.costUsd??0)+(e.costUsd??0)}}var we=class{constructor(e){this.options=e}options;browser;cdpEndpoint;stepIndex;getStepIndex(){if(!this.stepIndex&&(this.stepIndex=H(this.options.projectRoot),this.stepIndex.steps.size>0)){let e=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)${e}`)}return this.stepIndex}async recordWithSeeding(e,n){let r=this.getStepIndex(),s=e.steps.length,o={...e.params,...K(e.steps)},i=e.steps.map(x=>{let b=r.steps.size>0?mt(r,x,o):void 0;return b?{gherkin:x.text,keyword:x.keyword,kind:x.kind,table:x.table,docString:x.docString,fromStepSet:x.source?.fromStepSet,actions:b.actions}:void 0}),a=e.steps.map(x=>({gherkin:x.text,keyword:x.keyword,kind:x.kind,table:x.table,docString:x.docString,fromStepSet:x.source?.fromStepSet,actions:[]})),d=new Map,p=[],m=[],l=[],c=[],g={...ve},h=0,u=0,f=0,y=x=>({success:x,narrative:m.join(" ")||"Composed entirely from seeded recordings.",adaptations:p,suggestedFeatureEdit:c.length>0?c.join(`
27
+ `):void 0,featureEdits:l,recordedSteps:a,announcedSteps:[],usage:g,seededCount:h});for(;f<s;){if(i[f]){let v=f;for(;v<s&&i[v];)v++;let j=i.slice(f,v),S={...e,steps:e.steps.slice(f,v)},T=await be(n,S,this.buildCandidateCache(e,j),{baseURL:this.options.baseURL,actionTimeoutMs:this.options.actionTimeoutMs,retries:this.options.retries,vars:d}),C=T.status==="passed"?v-f:T.failure?.stepIndex??0;for(let _=0;_<C;_++)a[f+_]=j[_];if(h+=C,T.status==="passed"){f=v;continue}let F=f+C;p.push(`Seeded recording for "${e.steps[F].text}" did not replay in this scenario's context; the agent re-recorded it fresh. Divergence signal.`),i[F]=void 0,f=F;continue}if(!this.options.agent)return y(!1);u++;let x=f;for(;x<s&&!i[x];)x++;u>=3&&(x=s);let b=await this.withDialogsVisible(()=>this.options.agent.run({scenario:e,baseURL:this.options.baseURL,mode:"record",cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:f>0?e.steps.slice(0,f).map(v=>v.resolvedText):void 0,recordOnly:x<s?{from:f,to:x-1}:void 0,takeFingerprint:async()=>J(le(this.browser)??n)}));if(g=Lt(g,b.usage),m.push(b.narrative),p.push(...b.adaptations),l.push(...b.featureEdits),b.suggestedFeatureEdit&&c.push(b.suggestedFeatureEdit),!b.success)return y(!1);let w=b.announcedSteps.filter(v=>v>=f&&v<s),k=Math.min(s-1,Math.max(w.length>0?Math.max(...w):x-1,f));for(let v=f;v<=k;v++)a[v]=b.recordedSteps[v];f=k+1}return y(!0)}async withDialogsVisible(e){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 e()}finally{for(let i of s)i.off("page",o);for(let i of r)try{i.off("dialog",n)}catch{}}}async start(){let e=this.options.browser??"chromium";if(e==="chromium"){let n=await br();this.browser=await yr.launch({headless:!this.options.headed,args:[`--remote-debugging-port=${n}`]}),this.cdpEndpoint=`http://127.0.0.1:${n}`}else this.browser=await(e==="firefox"?Sr:xr).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 e=Dt.isAbsolute(this.options.storageState)?this.options.storageState:Dt.join(this.options.projectRoot,this.options.storageState);if(!mr.existsSync(e))throw new Error(`storageState file not found: ${e}. Generate one with: npx playwright open --save-storage=${this.options.storageState} <url>`);return e}buildCandidateCache(e,n){return{version:1,feature:e.featurePath,scenario:e.name,recordedAt:new Date().toISOString(),recordedBy:"agent",steps:n}}async proofReplay(e,n){let r=await this.browser.newContext({storageState:this.resolveStorageState()}),s=await r.newPage();return{result:await be(s,e,n,{baseURL:this.options.baseURL,actionTimeoutMs:this.options.actionTimeoutMs,retries:this.options.retries}),context:r,page:s}}async runScenario(e,n={}){let r=await this.runScenarioInner(e,n),s=ae(e);if(s.size>0){let o=i=>i===void 0?i:te(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=>te(i,s));for(let i of r.stepResults)i.error=o(i.error)}return r}async runScenarioInner(e,n={}){if(!this.browser)throw new Error("Orchestrator not started");let r=Date.now(),{projectRoot:s,baseURL:o,actionTimeoutMs:i,retries:a}=this.options,d=W(s,e.featurePath,e.name),p=N(d),m=await this.browser.newContext({storageState:this.resolveStorageState()}),l=await m.newPage();try{let c;if(p&&(c=await be(l,e,p,{baseURL:o,actionTimeoutMs:i,retries:a}),c.status==="passed"))return{scenario:e,status:"green",source:"cache",stepResults:c.stepResults,adaptations:[],usage:ve,durationMs:Date.now()-r};if(!(this.options.agent!==void 0&&this.cdpEndpoint!==void 0&&!n.agentDisabled)){let y=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:e,status:"red",source:"cache",stepResults:c?.stepResults??[],adaptations:[],usage:ve,durationMs:Date.now()-r,error:p?c?.status==="stale"?`Cache is stale (feature file changed) and ${y}`:`Replay failed and ${y} ${c?.failure?.error??""}`.trimEnd():`No cache exists and ${y}`}}let h=p!==void 0&&c!==void 0&&c.status==="failed",u=0,f;try{if(h)f=await this.withDialogsVisible(()=>this.options.agent.run({scenario:e,baseURL:o,mode:"heal",model:this.options.healModel,cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:c.stepResults.filter(y=>y.status==="passed").map(y=>y.gherkin),failedStep:{text:c.failure.stepText,kind:c.failure.kind,error:c.failure.error},takeFingerprint:async()=>{let y=le(this.browser)??l;return J(y)}}));else if(await l.goto("about:blank"),this.options.reuseSteps!==!1){let y=await this.recordWithSeeding(e,l);u=y.seededCount,f=y}else f=await this.withDialogsVisible(()=>this.options.agent.run({scenario:e,baseURL:o,mode:"record",cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,takeFingerprint:async()=>{let y=le(this.browser)??l;return J(y)}}))}catch(y){let x=y instanceof Error?y.message:String(y);return{scenario:e,status:"red",source:h?"agent-heal":"agent-record",stepResults:c?.stepResults??[],adaptations:[],usage:ve,durationMs:Date.now()-r,error:`Agent invocation failed: ${x.split(`
28
+ `)[0]}`}}if(f.success&&h&&c.failure.kind==="assertion"&&(this.options.assertionPolicy!=="adaptable-mid"||Ot(p.steps,c.failure.stepIndex))){let y=p.steps[c.failure.stepIndex],x=le(this.browser)??l,b=new Map(Object.entries(c.captured)),w=Ge(e,c.failure.stepIndex);try{for(let k of y.actions)await He(x,k,w,this.options.actionTimeoutMs??5e3,o,b)}catch(k){let v=k instanceof Error?k.message:String(k);return{scenario:e,status:"red",source:"agent-heal",stepResults:c.stepResults,narrative:f.narrative,adaptations:f.adaptations,suggestedFeatureEdit:f.suggestedFeatureEdit,usage:f.usage,durationMs:Date.now()-r,error:`Assertion failed as written: "${y.gherkin}". Saffron never adapts assertions \u2014 if this change is intended, update the .feature file or re-record the scenario. Verification error: ${v.split(`
29
+ `)[0]}`}}}return await this.finalizeAgentRun(e,f,h,p,r,u,h?c?.failure:void 0)}finally{await m.close()}}async finalizeAgentRun(e,n,r,s,o,i=0,a){let d=r?"agent-heal":"agent-record",p=n.recordedSteps.map(w=>({gherkin:w.gherkin,kind:w.kind,status:n.success?"passed":"failed"}));if(!n.success)return{scenario:e,status:"red",source:d,stepResults:p,narrative:n.narrative,adaptations:n.adaptations,usage:n.usage,durationMs:Date.now()-o,error:n.narrative};let m=r?Mt(s.steps,n.recordedSteps,this.options.assertionPolicy==="adaptable-mid",a?{stepIndex:a.stepIndex,actionIndex:a.actionIndex}:void 0):n.recordedSteps,l=[...n.adaptations],c={...n.usage},g,h;if(this.options.verifyProposals!==!1){let w=await this.proofReplay(e,this.buildCandidateCache(e,m));try{if(w.result.status!=="passed"&&w.result.failure){let k=w.result.failure;if(this.options.agent&&!(r&&k.kind==="assertion"&&(this.options.assertionPolicy!=="adaptable-mid"||Ot(s.steps,k.stepIndex))))try{let j=await this.withDialogsVisible(()=>this.options.agent.run({scenario:e,baseURL:this.options.baseURL,mode:"refine",model:r?this.options.healModel:void 0,cdpEndpoint:this.cdpEndpoint,assertionPolicy:this.options.assertionPolicy,completedSteps:w.result.stepResults.filter(S=>S.status==="passed").map(S=>S.gherkin),failedStep:{text:k.stepText,kind:k.kind,error:k.error},takeFingerprint:async()=>J(le(this.browser)??w.page)}));c=Lt(c,j.usage),j.success&&(l.push(...j.adaptations),m=r?Mt(m,j.recordedSteps,this.options.assertionPolicy==="adaptable-mid"):vr(m,j.recordedSteps),await w.context.close(),w=await this.proofReplay(e,this.buildCandidateCache(e,m)))}catch{}}g=w.result.status==="passed",g||(h=w.result.failure?.error??`proof replay ${w.result.status}`)}finally{await w.context.close().catch(()=>{})}}let u=[...n.featureEdits];for(let w of ht(m,H(this.options.projectRoot)))l.push(`duplicate wording: "${w.from}" recorded exactly the same actions as the existing step "${w.to}" (${w.feature} \u203A ${w.scenario}). Accept with --with-feature-edit to converge on the canonical wording.`),u.some(k=>k.index===w.index)||u.push({index:w.index,text:w.to});let f=ae(e),y=w=>f.size>0?te(w,f):w;l=l.map(y);let x={meta:{createdAt:new Date().toISOString(),mode:r?"heal":"record",narrative:y(n.narrative),adaptations:l,suggestedFeatureEdit:n.suggestedFeatureEdit?y(n.suggestedFeatureEdit):void 0,featureEdits:u.length>0?u:void 0,verified:g,proofError:h?y(h):void 0,seededSteps:i>0?i:void 0,usage:c},cache:{version:1,feature:e.featurePath,scenario:e.name,recordedAt:new Date().toISOString(),recordedBy:"agent",steps:m}},b=dt(this.options.projectRoot,e.featurePath,e.name);return Et(b,x),{scenario:e,status:"yellow",source:d,stepResults:p,narrative:n.narrative,adaptations:l,suggestedFeatureEdit:n.suggestedFeatureEdit,featureEdits:u.length>0?u:void 0,verified:g,proofError:h,seededSteps:i>0?i:void 0,proposalFile:b,usage:c,durationMs:Date.now()-o}}};import Kt from"node:path";import{createRequire as Ar}from"node:module";import{query as Cr}from"@anthropic-ai/claude-agent-sdk";var ke=class{steps;currentStepIndex=-1;done=!1;success=!1;narrative="";adaptations=[];suggestedFeatureEdit;featureEdits=[];announced=new Set;pendingFingerprint;constructor(e){this.steps=e.steps.map(n=>({gherkin:n.text,keyword:n.keyword,kind:n.kind,table:n.table,docString:n.docString,fromStepSet:n.source?.fromStepSet,actions:[]}))}addArmedAction(e){if(this.done)return;let n=Math.max(this.currentStepIndex,0),r=this.steps[n];if(!r||r.actions.some(i=>i.action===e.action&&i.value===e.value&&i.promptText===e.promptText))return;let o=Math.max(r.actions.length-1,0);r.actions.splice(o,0,e)}addActions(e){if(this.done||e.length===0)return;let n=Math.max(this.currentStepIndex,0),r=this.steps[n];r&&(this.pendingFingerprint&&e[0]&&(e[0].pageFingerprint=this.pendingFingerprint,this.pendingFingerprint=void 0),r.actions.push(...e))}},Y=t=>t.replace(/\\(['"\\])/g,"$1");function wr(t){let e=t.match(/'((?:[^'\\]|\\.)*)'/);return e?Y(e[1]):void 0}function Je(t){let e={},n=t.match(/(?:frameLocator\('((?:[^'\\]|\\.)*)'\)|locator\('((?:[^'\\]|\\.)*)'\)\.contentFrame\(\))/);n&&(e.frame=Y(n[1]??n[2]),t=t.slice((n.index??0)+n[0].length));let r=t.match(/getByRole\('([^']+)'(?:,\s*\{([^}]*)\})?\)/);if(r){e.role=r[1];let i=(r[2]??"").match(/name:\s*'((?:[^'\\]|\\.)*)'/);i&&(e.name=Y(i[1]))}let s=[[/getByLabel\('((?:[^'\\]|\\.)*)'\)/,"label"],[/getByText\('((?:[^'\\]|\\.)*)'\)/,"text"],[/getByTestId\('((?:[^'\\]|\\.)*)'\)/,"testId"],[/getByPlaceholder\('((?:[^'\\]|\\.)*)'\)/,"placeholder"]];for(let[i,a]of s){let d=t.match(i);d&&(e[a]=Y(d[1]))}let o=t.match(/(?:^|\.)locator\('((?:[^'\\]|\\.)*)'\)/);return o&&(e.fallbackSelectors=[Y(o[1])]),Object.keys(e).length>0?e:void 0}function kr(t,e){if(e&&t.startsWith(e)){let n=t.slice(e.length);return n.startsWith("/")?n:`/${n}`}return t}var Rr={click:"click",dblclick:"click",fill:"fill",press:"press",check:"check",uncheck:"uncheck",hover:"hover",selectOption:"selectOption"};function $r(t,e){let n=t.match(/page\.goto\('((?:[^'\\]|\\.)*)'\)/);if(n)return{action:"goto",url:kr(Y(n[1]),e)};let r=t.match(/page\.keyboard\.press\('((?:[^'\\]|\\.)*)'\)/);if(r)return{action:"press",value:Y(r[1])};let s=t.match(/await\s+page\.(.+)\.dragTo\(page\.(.+?)\);?\s*$/);if(s){let c=Je(s[1]),g=Je(s[2]);if(c&&g)return{action:"dragTo",target:c,toTarget:g}}let o=t.match(/await\s+page\.(.+)\.(click|dblclick|fill|press|check|uncheck|hover|selectOption)\((.*)\);?\s*$/);if(!o)return;let[,i,a,d]=o,p=Je(i);if(!p)return;let m={action:Rr[a],target:p},l=wr(d);return l!==void 0&&a!=="click"&&a!=="dblclick"&&(m.value=l),m}function Nt(t,e){let n=[],r=Array.isArray(t)?t:t?.content;if(Array.isArray(r))for(let o of r){let i=o?.text;typeof i=="string"&&n.push(i)}else typeof t=="string"&&n.push(t);let s=[];for(let o of n)for(let i of o.split(`
30
+ `)){let a=$r(i.trim(),e);a&&s.push(a)}return s}function Ye(t,e){let n=t;for(let[r,s]of Object.entries(e).filter(([,o])=>o.length>=3).sort((o,i)=>i[1].length-o[1].length))n=n.split(s).join(`<${r}>`);return n}function Bt(t,e){if(Object.entries(e).filter(([,s])=>s.length>=3).length===0)return t;let r=s=>s===void 0?void 0:Ye(s,e);return t.map(s=>({...s,actions:s.actions.map(o=>({...o,value:r(o.value),url:r(o.url),target:o.target?{...o.target,name:r(o.target.name),label:r(o.target.label),text:r(o.target.text),placeholder:r(o.target.placeholder)}:void 0}))}))}function zt(t,e){let n=K(e.steps);return t.map((r,s)=>{let o=e.steps[s],i=o?.resolvedTable??o?.table,a=o?.resolvedDocString??o?.docString,d=Object.entries({...n,...L(i),...q(a)}).filter(([,m])=>m.length>=3).sort((m,l)=>l[1].length-m[1].length);if(d.length===0)return r;let p=m=>{if(m===void 0)return;let l=m;for(let[c,g]of d)l=l.split(g).join(`<${c}>`);return l};return{...r,actions:r.actions.map(m=>({...m,value:p(m.value),url:p(m.url),target:m.target?{...m.target,name:p(m.target.name),label:p(m.target.label),text:p(m.target.text),placeholder:p(m.target.placeholder)}:void 0}))}})}function Vt(t,e=new Date){let n=r=>r===void 0?void 0:Pt(r,e);return t.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 qt(t,e){if(e.size===0)return t;let n=r=>r===void 0?void 0:te(r,e);return t.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 Wt(t,e){let n=e??{};if(t==="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(t==="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(`
31
+ `)}}}import{createSdkMcpServer as Tr,tool as ne}from"@anthropic-ai/claude-agent-sdk";import{z as R}from"zod";var D=t=>({content:[{type:"text",text:t}]});function Ht(t){let e={};return t.role&&(e.role=t.role),t.name&&(e.name=t.name),t.nameRegex&&(e.nameRegex=t.nameRegex),t.label&&(e.label=t.label),t.text&&(e.text=t.text),t.selector&&(e.fallbackSelectors=[t.selector]),t.frame&&(e.frame=t.frame),Object.keys(e).length>0?e:void 0}var Gt={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 Jt(t){return Tr({name:"saffron",version:"0.1.0",tools:[ne("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 e=>(t.currentStepIndex=e.index,t.announced.add(e.index),e.adaptation&&t.adaptations.push(`Step ${e.index+1} ("${e.text}"): ${e.adaptation}`),D(`Recording step ${e.index}: ${e.text}`))),ne("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"),...Gt},async e=>{let n=Ht(e);return n?(t.addActions([{action:"captureText",target:n,saveAs:e.saveAs}]),D(`Capture recorded as "${e.saveAs}".`)):D("ERROR: capture needs a target (role/name, nameRegex, label, text, or selector).")}),ne("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 e=>(t.addArmedAction({action:"handleDialog",value:e.accept?"accept":"dismiss",...e.promptText?{promptText:e.promptText}:{}}),D(`Dialog handling recorded (${e.accept?"accept":"dismiss"}).`))),ne("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 e=>(t.addActions([{action:"waitForResponse",urlPattern:e.urlPattern,...e.method?{method:e.method}:{},...e.status?{status:e.status}:{},...e.bodyPattern?{pattern:e.bodyPattern}:{}}]),D("Network wait recorded."))),ne("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"]),...Gt,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 e=>{let n=Ht(e),r;switch(e.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:e.expected??""};break;case"value":r={action:"expectValue",target:n,value:e.expected??""};break;case"url":r={action:"expectUrl",url:e.url??""};break;case"differs":if(!e.compareTo)return D("ERROR: kind=differs requires compareTo.");if(!e.capture&&!n)return D("ERROR: kind=differs needs either capture (a stored name) or a target element.");r={action:"expectDiffers",target:n,value:e.capture,compareTo:e.compareTo};break;case"matches":if(!e.pattern)return D("ERROR: kind=matches requires pattern.");r={action:"expectMatches",target:n,pattern:e.pattern};break;case"attribute":if(!e.attribute||!e.pattern)return D("ERROR: kind=attribute requires attribute and pattern.");r={action:"expectAttribute",target:n,attribute:e.attribute,pattern:e.pattern};break;case"response":if(!e.urlPattern)return D("ERROR: kind=response requires urlPattern.");r={action:"expectResponse",urlPattern:e.urlPattern,...e.method?{method:e.method}:{},...e.status?{status:e.status}:{},...e.bodyPattern?{pattern:e.bodyPattern}:{}};break}return!["url","differs","response"].includes(e.kind)&&!n?D("ERROR: assertion needs a target (role/name, nameRegex, label, text, or selector)."):(t.addActions([r]),D("Assertion recorded."))}),ne("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 e=>(t.done=!0,t.success=e.success,t.narrative=e.narrative,t.suggestedFeatureEdit=e.suggestedFeatureEdit,t.featureEdits=e.featureEdits??[],D("Run finalized.")))]})}function Yt(t={}){let e=t.deliberateSnapshots?`
32
32
  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.
33
33
  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.
34
34
 
@@ -44,7 +44,7 @@ Non-negotiable rules:
44
44
  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.
45
45
  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.
46
46
  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.
47
- 7. Never ask the user questions; you are unattended.${e}`}function kr(t,e){if(t[e].kind!=="assertion")return"action";let n=t.length;for(let r=t.length-1;r>=0&&t[r].kind==="assertion";r--)n=r;return e>=n?"assertion:final":"assertion"}function Ht(t){let{scenario:e}=t,n=e.steps.map((s,o)=>{let i=l=>ie(l),a=`${o}. [${kr(e.steps,o)}] ${s.keyword} ${i(s.resolvedText)}`,d=s.resolvedTable??s.table,p=s.resolvedDocString??s.docString,m=a;return d&&(m+=`
47
+ 7. Never ask the user questions; you are unattended.${e}`}function Er(t,e){if(t[e].kind!=="assertion")return"action";let n=t.length;for(let r=t.length-1;r>=0&&t[r].kind==="assertion";r--)n=r;return e>=n?"assertion:final":"assertion"}function Qt(t){let{scenario:e}=t,n=e.steps.map((s,o)=>{let i=l=>ie(l),a=`${o}. [${Er(e.steps,o)}] ${s.keyword} ${i(s.resolvedText)}`,d=s.resolvedTable??s.table,p=s.resolvedDocString??s.docString,m=a;return d&&(m+=`
48
48
  ${d.map(l=>` | ${l.map(i).join(" | ")} |`).join(`
49
49
  `)}`),p!==void 0&&(m+=`
50
50
  """
@@ -60,25 +60,25 @@ ${(t.completedSteps??[]).map(s=>` \u2713 ${s}`).join(`
60
60
  ${t.completedSteps.map(i=>` \u2713 ${i}`).join(`
61
61
  `)}`:"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(t.completedSteps&&t.completedSteps.length>0){let s=t.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:",t.completedSteps.map(o=>` \u2713 ${o}`).join(`
62
62
  `),"",`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(`
63
- `)}var Tr=$r(import.meta.url);function Er(){let t=Tr.resolve("@playwright/mcp/package.json");return Gt.join(Gt.dirname(t),"cli.js")}var Ar=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"]),$e=class{constructor(e={}){this.opts=e}opts;async run(e){let n=new ke(e.scenario),r={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0},s="",o=Rr({prompt:Ht(e),options:{systemPrompt:Wt({deliberateSnapshots:(this.opts.snapshotMode??"none")==="none",adaptableMidAssertions:e.assertionPolicy==="adaptable-mid"}),model:e.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:[Er(),"--cdp-endpoint",e.cdpEndpoint,"--snapshot-mode",this.opts.snapshotMode??"none","--image-responses","omit"]},saffron:qt(n)},hooks:{PreToolUse:[{hooks:[async i=>{let a=i;if(Ar.has(a.tool_name)&&e.takeFingerprint&&!n.pendingFingerprint)try{n.pendingFingerprint=await e.takeFingerprint()}catch{}return{continue:!0}}]}],PostToolUse:[{hooks:[async i=>{let a=i,d=zt(a.tool_name,a.tool_input);return d&&n.addArmedAction(d),a.tool_name.startsWith("mcp__playwright__")&&(n.addActions(Ot(a.tool_response,e.baseURL)),n.pendingFingerprint=void 0),{continue:!0}}]}]}}});try{for await(let i of o)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),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:He(i.text,e.scenario.params)})),recordedSteps:Nt(Ut(Mt(Lt(n.steps,e.scenario),e.scenario.params)),ae(e.scenario)),announcedSteps:[...n.announced].sort((i,a)=>i-a),usage:r}}};import Yt from"node:fs";import Re from"node:path";function Te(t){return JSON.stringify([t.role??null,t.name??null,t.nameRegex??null,t.label??null,t.text??null,t.testId??null,t.placeholder??null])}function Jt(t,e){let n=new Map,r=Math.min(t.steps.length,e.steps.length);for(let s=0;s<r;s++){let o=t.steps[s].actions,i=e.steps[s].actions;if(o.length===i.length)for(let a=0;a<o.length;a++){let d=o[a],p=i[a];if(d.action!==p.action||!d.target||!p.target)continue;let m=Te(d.target);m!==Te(p.target)&&n.set(m,{from:d.target,to:p.target})}}return[...n.values()]}function Cr(t){let e=Re.join(t,".saffron","cache");if(!Yt.existsSync(e))return[];let n=[];for(let r of Yt.readdirSync(e,{recursive:!0})){let s=String(r);s.endsWith(".json")&&n.push(Re.join(e,s))}return n}function Qt(t,e,n,r){if(e.length===0)return[];let s=new Map(e.map(i=>[Te(i.from),i])),o=[];for(let i of Cr(t)){if(Re.resolve(i)===Re.resolve(n))continue;let a=N(i);if(!a)continue;let d=[],p=!1;for(let m of a.steps){let l=0;for(let c of m.actions){if(!c.target)continue;let g=s.get(Te(c.target));g&&(l++,r||(c.target=structuredClone(g.to),p=!0))}l>0&&d.push({gherkin:m.gherkin,count:l})}d.length>0&&(p&&ee(i,a),o.push({file:i,scenario:a.scenario,feature:a.feature,steps:d}))}return o}function Ge(t){return t.role?`${t.role} "${t.name??t.nameRegex??""}"`:t.label?`label "${t.label}"`:t.text?`text "${t.text}"`:t.testId?`testId "${t.testId}"`:(t.fallbackSelectors??[]).join(",")||"(empty target)"}import Kt from"node:fs";import Ye from"node:path";function Xt(t,e,n,r,s){let o={applied:[],skipped:[]},i=Z(Ye.join(t,e),t,s).find(p=>p.name===n);if(!i){for(let p of r)o.skipped.push({index:p.index,reason:`scenario "${n}" not found in ${e}`});return o}let a=new Map,d=new Set;for(let p of r){let m=i.steps[p.index];if(!m?.source){o.skipped.push({index:p.index,reason:`no step at index ${p.index}`});continue}let{file:l,line:c,fromStepSet:g,fromBackground:h}=m.source;if(h){o.skipped.push({index:p.index,reason:`"${m.text}" is a Background step shared across scenarios \u2014 edit it manually`});continue}let u=`${l}:${c}`;if(d.has(u))continue;let f=a.get(l);f||(f=Kt.readFileSync(Ye.join(t,l),"utf8").split(`
64
- `),a.set(l,f));let y=f[c-1],x=y?.match(/^(\s*)(\S+)\s+(.*)$/);if(!x||x[3]!==m.text){o.skipped.push({index:p.index,reason:`${l}:${c} no longer matches the recorded step text`});continue}let b=`${x[1]}${x[2]} ${p.text}`;f[c-1]=b,d.add(u),o.applied.push({file:l,line:c,from:y.trim(),to:b.trim(),fromStepSet:g})}for(let[p,m]of a)o.applied.some(l=>l.file===p)&&Kt.writeFileSync(Ye.join(t,p),m.join(`
65
- `));return o}import Je from"node:fs";import Qe from"node:path";var A=t=>t.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll('"',"&quot;"),tn="M11 14 L10 4.5 L17.5 9.5 L24 2.5 L30.5 9.5 L38 4.5 L37 14 Z",Pr="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 Fr(t){return`<svg viewBox="0 0 48 48" width="${t}" height="${t}" aria-hidden="true">
66
- <path d="${tn}" fill="#F4A300"/>
67
- <path d="${Pr}" fill="#E7E9EC"/>
68
- </svg>`}function Ir(t){return`<svg viewBox="8 1 32 15" height="${t}" aria-hidden="true"><path d="${tn}" fill="#F4A300"/></svg>`}var jr={green:"passed",yellow:"adapted",red:"failed"};function Ee(t){return t>=1e3?`${(t/1e3).toFixed(1)}s`:`${t}ms`}function de(t){return t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e4?`${(t/1e3).toFixed(1)}k`:t.toLocaleString()}function Q(t,e,n,r="",s="",o=""){return`<div class="kpi">
63
+ `)}var Pr=Ar(import.meta.url);function Fr(){let t=Pr.resolve("@playwright/mcp/package.json");return Kt.join(Kt.dirname(t),"cli.js")}var Ir=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"]),Re=class{constructor(e={}){this.opts=e}opts;async run(e){let n=new ke(e.scenario),r={aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0},s="",o=Cr({prompt:Qt(e),options:{systemPrompt:Yt({deliberateSnapshots:(this.opts.snapshotMode??"none")==="none",adaptableMidAssertions:e.assertionPolicy==="adaptable-mid"}),model:e.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:[Fr(),"--cdp-endpoint",e.cdpEndpoint,"--snapshot-mode",this.opts.snapshotMode??"none","--image-responses","omit"]},saffron:Jt(n)},hooks:{PreToolUse:[{hooks:[async i=>{let a=i;if(Ir.has(a.tool_name)&&e.takeFingerprint&&!n.pendingFingerprint)try{n.pendingFingerprint=await e.takeFingerprint()}catch{}return{continue:!0}}]}],PostToolUse:[{hooks:[async i=>{let a=i,d=Wt(a.tool_name,a.tool_input);return d&&n.addArmedAction(d),a.tool_name.startsWith("mcp__playwright__")&&(n.addActions(Nt(a.tool_response,e.baseURL)),n.pendingFingerprint=void 0),{continue:!0}}]}]}}});try{for await(let i of o)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),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:Ye(i.text,e.scenario.params)})),recordedSteps:qt(Vt(Bt(zt(n.steps,e.scenario),e.scenario.params)),ae(e.scenario)),announcedSteps:[...n.announced].sort((i,a)=>i-a),usage:r}}};import Xt from"node:fs";import $e from"node:path";function Te(t){return JSON.stringify([t.role??null,t.name??null,t.nameRegex??null,t.label??null,t.text??null,t.testId??null,t.placeholder??null])}function Zt(t,e){let n=new Map,r=Math.min(t.steps.length,e.steps.length);for(let s=0;s<r;s++){let o=t.steps[s].actions,i=e.steps[s].actions;if(o.length===i.length)for(let a=0;a<o.length;a++){let d=o[a],p=i[a];if(d.action!==p.action||!d.target||!p.target)continue;let m=Te(d.target);m!==Te(p.target)&&n.set(m,{from:d.target,to:p.target})}}return[...n.values()]}function jr(t){let e=$e.join(t,".saffron","cache");if(!Xt.existsSync(e))return[];let n=[];for(let r of Xt.readdirSync(e,{recursive:!0})){let s=String(r);s.endsWith(".json")&&n.push($e.join(e,s))}return n}function en(t,e,n,r){if(e.length===0)return[];let s=new Map(e.map(i=>[Te(i.from),i])),o=[];for(let i of jr(t)){if($e.resolve(i)===$e.resolve(n))continue;let a=N(i);if(!a)continue;let d=[],p=!1;for(let m of a.steps){let l=0;for(let c of m.actions){if(!c.target)continue;let g=s.get(Te(c.target));g&&(l++,r||(c.target=structuredClone(g.to),p=!0))}l>0&&d.push({gherkin:m.gherkin,count:l})}d.length>0&&(p&&ee(i,a),o.push({file:i,scenario:a.scenario,feature:a.feature,steps:d}))}return o}function Qe(t){return t.role?`${t.role} "${t.name??t.nameRegex??""}"`:t.label?`label "${t.label}"`:t.text?`text "${t.text}"`:t.testId?`testId "${t.testId}"`:(t.fallbackSelectors??[]).join(",")||"(empty target)"}import tn from"node:fs";import Ke from"node:path";function nn(t,e,n,r,s){let o={applied:[],skipped:[]},i=Z(Ke.join(t,e),t,s).find(p=>p.name===n);if(!i){for(let p of r)o.skipped.push({index:p.index,reason:`scenario "${n}" not found in ${e}`});return o}let a=new Map,d=new Set;for(let p of r){let m=i.steps[p.index];if(!m?.source){o.skipped.push({index:p.index,reason:`no step at index ${p.index}`});continue}let{file:l,line:c,fromStepSet:g,fromBackground:h}=m.source;if(h){o.skipped.push({index:p.index,reason:`"${m.text}" is a Background step shared across scenarios \u2014 edit it manually`});continue}let u=`${l}:${c}`;if(d.has(u))continue;let f=a.get(l);f||(f=tn.readFileSync(Ke.join(t,l),"utf8").split(`
64
+ `),a.set(l,f));let y=f[c-1],x=y?.match(/^(\s*)(\S+)\s+(.*)$/);if(!x||x[3]!==m.text){o.skipped.push({index:p.index,reason:`${l}:${c} no longer matches the recorded step text`});continue}let b=`${x[1]}${x[2]} ${p.text}`;f[c-1]=b,d.add(u),o.applied.push({file:l,line:c,from:y.trim(),to:b.trim(),fromStepSet:g})}for(let[p,m]of a)o.applied.some(l=>l.file===p)&&tn.writeFileSync(Ke.join(t,p),m.join(`
65
+ `));return o}import Ze from"node:fs";import Xe from"node:fs";import Ee from"node:path";import{fileURLToPath as _r}from"node:url";function Ae(){let t=Ee.dirname(_r(import.meta.url));for(let e=0;e<6;e++){let n=Ee.join(t,"package.json");if(Xe.existsSync(n))try{if(JSON.parse(Xe.readFileSync(n,"utf8")).name==="saffron-ai")return t}catch{}t=Ee.dirname(t)}}function Ce(){let t=Ae();if(!t)return"unknown";try{return JSON.parse(Xe.readFileSync(Ee.join(t,"package.json"),"utf8")).version??"unknown"}catch{return"unknown"}}import et from"node:path";var A=t=>t.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll('"',"&quot;"),on="M11 14 L10 4.5 L17.5 9.5 L24 2.5 L30.5 9.5 L38 4.5 L37 14 Z",Dr="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 Or(t){return`<svg viewBox="0 0 48 48" width="${t}" height="${t}" aria-hidden="true">
66
+ <path d="${on}" fill="#F4A300"/>
67
+ <path d="${Dr}" fill="#E7E9EC"/>
68
+ </svg>`}function Mr(t){return`<svg viewBox="8 1 32 15" height="${t}" aria-hidden="true"><path d="${on}" fill="#F4A300"/></svg>`}var Lr={green:"passed",yellow:"adapted",red:"failed"};function Pe(t){return t>=1e3?`${(t/1e3).toFixed(1)}s`:`${t}ms`}function de(t){return t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e4?`${(t/1e3).toFixed(1)}k`:t.toLocaleString()}function Q(t,e,n,r="",s="",o=""){return`<div class="kpi">
69
69
  <div class="kpi-label">${t}</div>
70
70
  <div class="kpi-value ${r}">${e}</div>
71
71
  <div class="kpi-sub ${s}">${n}</div>${o}
72
- </div>`}function Zt(t,e){if(t.length<2)return"";let n=108,r=22,s=Math.min(...t),i=Math.max(...t)-s||1,a=t.map((d,p)=>`${(p/(t.length-1)*n).toFixed(1)},${(r-2-(d-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="${e}" stroke-width="1.8" stroke-linejoin="round" stroke-linecap="round"/></svg>`}var en=(t,e="",n=0)=>`${t>=0?"+":"\u2212"}${Math.abs(t).toFixed(n)}${e} vs prev`;function _r(t,e){let n=t.totals,r=t.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?`${en(r.prev.passRatePp,"pp")} \xB7 ${n.green+n.yellow} of ${n.scenarios}`:`${n.green+n.yellow} of ${n.scenarios} scenarios`,d=r?.prev?`${en(r.prev.costUsd,"",4).replace("+","+$").replace("\u2212","\u2212$")} \xB7 ${Ee(e)}`:`${Ee(e)} total runtime`;return`<section class="kpis">
73
- ${Q("pass rate",`${s}<span class="unit">%</span>`,a,o,r?.prev?r.prev.passRatePp>=0?"green":"red":"",Zt(r?.series.passRate??[],"var(--green)"))}
72
+ </div>`}function rn(t,e){if(t.length<2)return"";let n=108,r=22,s=Math.min(...t),i=Math.max(...t)-s||1,a=t.map((d,p)=>`${(p/(t.length-1)*n).toFixed(1)},${(r-2-(d-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="${e}" stroke-width="1.8" stroke-linejoin="round" stroke-linecap="round"/></svg>`}var sn=(t,e="",n=0)=>`${t>=0?"+":"\u2212"}${Math.abs(t).toFixed(n)}${e} vs prev`;function Ur(t,e){let n=t.totals,r=t.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?`${sn(r.prev.passRatePp,"pp")} \xB7 ${n.green+n.yellow} of ${n.scenarios}`:`${n.green+n.yellow} of ${n.scenarios} scenarios`,d=r?.prev?`${sn(r.prev.costUsd,"",4).replace("+","+$").replace("\u2212","\u2212$")} \xB7 ${Pe(e)}`:`${Pe(e)} total runtime`;return`<section class="kpis">
73
+ ${Q("pass rate",`${s}<span class="unit">%</span>`,a,o,r?.prev?r.prev.passRatePp>=0?"green":"red":"",rn(r?.series.passRate??[],"var(--green)"))}
74
74
  ${Q("passed",String(n.green),"deterministic replay","green",n.green>0?"green":"")}
75
75
  ${Q("adapted",String(n.yellow),n.yellow>0?"\u25B2 proposals pending review":"no adaptations",n.yellow>0?"gold":"",n.yellow>0?"gold":"")}
76
76
  ${Q("failed",String(n.red),n.red>0?"\u25BC needs attention":"all clear",n.red>0?"red":"",n.red>0?"red":"green")}
77
77
  ${Q("ai calls",String(n.aiCalls),`${de(i)} in+out tokens`,n.aiCalls>0?"":"green")}
78
78
  ${Q("cache traffic",de(n.cacheReadTokens+n.cacheCreationTokens),n.cacheReadTokens+n.cacheCreationTokens>0?`${de(n.cacheReadTokens)} read \xB7 ${de(n.cacheCreationTokens)} written`:"zero \u2014 fully cached run","",n.cacheReadTokens+n.cacheCreationTokens>0?"":"green")}
79
- ${Q("ai cost",`<span class="unit">$</span>${n.costUsd.toFixed(n.costUsd>=1?2:4)}`,d,"","",Zt(r?.series.costUsd??[],"var(--gold)"))}
80
- </section>`}function Dr(t){let e=t.trends;if(!e)return"";let n=[];return e.chronic.length>0&&n.push(`<div class="panel accent-gold"><div class="panel-title gold">chronic scenarios \u2014 stop paying for heals</div><ul>${e.chronic.map(r=>`<li>${A(r.feature)} \u203A ${A(r.scenario)} \u2014 ${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>`),e.divergentSteps>0&&n.push(`<div class="panel"><div class="panel-title">step divergence (level-2 signal)</div><p>${e.divergentSteps} step text(s) currently have divergent recordings across caches.</p></div>`),e.topIssues.length>0&&n.push(`<div class="panel accent-red"><div class="panel-title red">recurring failure themes (last 20 runs)</div><ul>${e.topIssues.map(r=>`<li>${r.count}\xD7 \u2014 ${A(r.message)}</li>`).join("")}</ul></div>`),n.length>0?`<section class="trends">${n.join(`
81
- `)}</section>`:""}function Or(t){return`<ol class="steps">${t.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}">
79
+ ${Q("ai cost",`<span class="unit">$</span>${n.costUsd.toFixed(n.costUsd>=1?2:4)}`,d,"","",rn(r?.series.costUsd??[],"var(--gold)"))}
80
+ </section>`}function Nr(t){let e=t.trends;if(!e)return"";let n=[];return e.chronic.length>0&&n.push(`<div class="panel accent-gold"><div class="panel-title gold">chronic scenarios \u2014 stop paying for heals</div><ul>${e.chronic.map(r=>`<li>${A(r.feature)} \u203A ${A(r.scenario)} \u2014 ${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>`),e.divergentSteps>0&&n.push(`<div class="panel"><div class="panel-title">step divergence (level-2 signal)</div><p>${e.divergentSteps} step text(s) currently have divergent recordings across caches.</p></div>`),e.topIssues.length>0&&n.push(`<div class="panel accent-red"><div class="panel-title red">recurring failure themes (last 20 runs)</div><ul>${e.topIssues.map(r=>`<li>${r.count}\xD7 \u2014 ${A(r.message)}</li>`).join("")}</ul></div>`),n.length>0?`<section class="trends">${n.join(`
81
+ `)}</section>`:""}function Br(t){return`<ol class="steps">${t.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}">
82
82
  ${r}
83
83
  <div class="step-body">
84
84
  <span class="step-text">${A(n.gherkin)}</span>
@@ -88,33 +88,33 @@ ${t.completedSteps.map(i=>` \u2713 ${i}`).join(`
88
88
  ${n.error?`<div class="step-error">${A(n.error)}</div>`:""}
89
89
  </div>
90
90
  </li>`}).join(`
91
- `)}</ol>`}function Mr(t){return t.featureEdits&&t.featureEdits.length>0?`<div class="panel">
91
+ `)}</ol>`}function zr(t){return t.featureEdits&&t.featureEdits.length>0?`<div class="panel">
92
92
  <div class="panel-title gold">suggested .feature edit</div>
93
93
  <div class="diff">${t.featureEdits.map(n=>{let r=t.steps[n.index]?.gherkin??`step ${n.index}`;return`<div class="diff-line del">\u2212 ${A(r)}</div><div class="diff-line add">+ ${A(n.text)}</div>`}).join("")}</div>
94
94
  <div class="panel-hint">apply with <code>saffron accept --with-feature-edit</code></div>
95
- </div>`:t.suggestedFeatureEdit?`<div class="panel"><div class="panel-title gold">suggested .feature edit</div><pre>${A(t.suggestedFeatureEdit)}</pre></div>`:""}function Lr(t){let e=t.usage.inputTokens+t.usage.outputTokens;if(t.usage.aiCalls===0)return'<span class="pill green">0 tokens</span>';let n=t.usage.costUsd?` \xB7 $${t.usage.costUsd.toFixed(2)}`:"";return`<span class="pill gold">${t.usage.aiCalls} ai \xB7 ${de(e)} tok${n}</span>`}function Ur(t){return`<details class="card" ${t.status!=="green"?"open":""}>
95
+ </div>`:t.suggestedFeatureEdit?`<div class="panel"><div class="panel-title gold">suggested .feature edit</div><pre>${A(t.suggestedFeatureEdit)}</pre></div>`:""}function Vr(t){let e=t.usage.inputTokens+t.usage.outputTokens;if(t.usage.aiCalls===0)return'<span class="pill green">0 tokens</span>';let n=t.usage.costUsd?` \xB7 $${t.usage.costUsd.toFixed(2)}`:"";return`<span class="pill gold">${t.usage.aiCalls} ai \xB7 ${de(e)} tok${n}</span>`}function qr(t){return`<details class="card" ${t.status!=="green"?"open":""}>
96
96
  <summary>
97
- <span class="pill ${t.status==="green"?"green":t.status==="yellow"?"gold":"red"}">${jr[t.status]}</span>
97
+ <span class="pill ${t.status==="green"?"green":t.status==="yellow"?"gold":"red"}">${Lr[t.status]}</span>
98
98
  <span class="card-title">${A(t.scenario)}</span>
99
99
  <span class="card-meta">
100
100
  <span class="m">${A(t.feature)}</span>
101
101
  <span class="m-sep">\xB7</span>
102
102
  <span class="m">${A(t.source)}</span>
103
103
  <span class="m-sep">\xB7</span>
104
- <span class="m">${Ee(t.durationMs)}</span>
105
- ${Lr(t)}
104
+ <span class="m">${Pe(t.durationMs)}</span>
105
+ ${Vr(t)}
106
106
  </span>
107
107
  </summary>
108
108
  <div class="card-body">
109
- ${Or(t)}
109
+ ${Br(t)}
110
110
  ${t.narrative?`<div class="panel"><div class="panel-title">agent narrative</div><p>${A(t.narrative)}</p></div>`:""}
111
111
  ${t.adaptations.length>0?`<div class="panel accent-gold"><div class="panel-title gold">adaptations</div><ul>${t.adaptations.map(e=>`<li>${A(e)}</li>`).join("")}</ul></div>`:""}
112
- ${Mr(t)}
112
+ ${zr(t)}
113
113
  ${t.proposalFile?`<div class="panel"><div class="panel-title">cache proposal pending${t.verified===!0?' <span class="pill green">verified</span>':t.verified===!1?' <span class="pill red">unverified</span>':""}</div><p class="mono">${A(t.proposalFile)}</p>${t.verified===!1&&t.proofError?`<p class="mono" style="color:var(--red)">proof replay failed: ${A(t.proofError.split(`
114
114
  `)[0])}</p>`:""}<div class="panel-hint">review with <code>saffron accept</code> / <code>saffron reject</code></div></div>`:""}
115
115
  ${t.error&&t.status==="red"?`<div class="panel accent-red"><div class="panel-title red">failure</div><p>${A(t.error)}</p></div>`:""}
116
116
  </div>
117
- </details>`}function nn(t){let e=t.totals,n=new Date(t.startedAt),r=new Date(t.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>
117
+ </details>`}function an(t){let e=t.totals,n=new Date(t.startedAt),r=new Date(t.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>
118
118
  <html lang="en">
119
119
  <head>
120
120
  <meta charset="utf-8"/>
@@ -234,7 +234,7 @@ ${t.completedSteps.map(i=>` \u2713 ${i}`).join(`
234
234
  <div class="wrap">
235
235
  <header class="top">
236
236
  <div class="brand">
237
- ${Fr(38)}
237
+ ${Or(38)}
238
238
  <div>
239
239
  <div class="brand-name">saffron</div>
240
240
  <div class="brand-sub">test run report</div>
@@ -243,48 +243,49 @@ ${t.completedSteps.map(i=>` \u2713 ${i}`).join(`
243
243
  <div class="run-meta">
244
244
  <span>${A(s)}</span>
245
245
  <span>\xB7</span>
246
- <span>${Ee(r)}</span>
246
+ <span>${Pe(r)}</span>
247
247
  ${t.baseURL?`<span class="chip base">${A(t.baseURL)}</span>`:""}
248
248
  </div>
249
249
  </header>
250
250
 
251
- ${_r(t,r)}
252
- ${Dr(t)}
251
+ ${Ur(t,r)}
252
+ ${Nr(t)}
253
253
 
254
254
  <div class="section-head">
255
255
  <span class="section-title">scenarios \xB7 ${e.scenarios}</span>
256
256
  <span class="section-title">saffron v${A(t.version)}</span>
257
257
  </div>
258
- ${t.scenarios.map(Ur).join(`
258
+ ${t.scenarios.map(qr).join(`
259
259
  `)}
260
260
 
261
- <footer>${Ir(11)} generated by saffron v${A(t.version)} \xB7 ${A(new Date(t.finishedAt).toLocaleString("en-GB",{dateStyle:"medium",timeStyle:"medium"}))}</footer>
261
+ <footer>${Mr(11)} generated by saffron v${A(t.version)} \xB7 ${A(new Date(t.finishedAt).toLocaleString("en-GB",{dateStyle:"medium",timeStyle:"medium"}))}</footer>
262
262
  </div>
263
263
  </body>
264
264
  </html>
265
- `}function rn(t,e,n){let r={scenarios:t.length,green:0,yellow:0,red:0,aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0};for(let s of t)r[s.status]+=1,r.aiCalls+=s.usage.aiCalls,r.inputTokens+=s.usage.inputTokens,r.outputTokens+=s.usage.outputTokens,r.cacheReadTokens+=s.usage.cacheReadTokens,r.cacheCreationTokens+=s.usage.cacheCreationTokens??0,r.costUsd+=s.usage.costUsd??0;return{tool:"saffron",version:"0.1.0",startedAt:e.toISOString(),finishedAt:new Date().toISOString(),baseURL:n,totals:r,scenarios:t.map(s=>({feature:s.scenario.featurePath,scenario:s.scenario.displayName,tags:[...s.scenario.tags],status:s.status,source:s.source,durationMs:s.durationMs,usage:s.usage,steps:s.stepResults,narrative:s.narrative,adaptations:s.adaptations,suggestedFeatureEdit:s.suggestedFeatureEdit,featureEdits:s.featureEdits,verified:s.verified,proofError:s.proofError,proposalFile:s.proposalFile,error:s.error}))}}function sn(t,e){let n=Qe.join(t,".saffron","reports");Je.mkdirSync(n,{recursive:!0});let r=Qe.join(n,"latest.json"),s=Qe.join(n,"latest.html");return Je.writeFileSync(r,`${JSON.stringify(e,null,2)}
266
- `),Je.writeFileSync(s,nn(e)),{json:r,html:s}}function on(t){let e=t===void 0?[]:Array.isArray(t)?t:[t],n=new Set;for(let r of e)for(let s of r.split(/[\s,|]+/)){let o=s.trim();o&&n.add(o.startsWith("@")?o:`@${o}`)}return[...n]}function an(t,e){return e.length===0?t:t.filter(n=>n.tags.some(r=>e.includes(r)))}function cn(t,e){return[...e??[],t]}import Ae from"node:fs";import ln from"node:path";function dn(t){return ln.join(t,".saffron","history.jsonl")}function pn(t,e,n,r){let s=t.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(`
267
- `)[0]}));return{runId:e.toISOString(),startedAt:e.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 un(t,e){let n=dn(t);Ae.mkdirSync(ln.dirname(n),{recursive:!0}),Ae.appendFileSync(n,`${JSON.stringify(e)}
268
- `)}function fn(t,e=50){let n=dn(t);if(!Ae.existsSync(n))return[];let r=Ae.readFileSync(n,"utf8").trim().split(`
269
- `).filter(Boolean),s=[];for(let o of r.slice(-e))try{s.push(JSON.parse(o))}catch{}return s}var Ke=t=>{let e=t.green+t.yellow+t.red;return e===0?0:Math.round((t.green+t.yellow)/e*100)};function gn(t,e){let n=[...t,e],r=n.slice(-20),s=n.slice(-10),o=t.at(-1),i=o?{passRatePp:Ke(e)-Ke(o),costUsd:e.costUsd-o.costUsd,aiCalls:e.aiCalls-o.aiCalls,red:e.red-o.red}:void 0,a=new Map;for(let l of s)for(let c of l.scenarios){let g=`${c.feature}::${c.scenario}`,h=a.get(g)??{feature:c.feature,scenario:c.scenario,heals:0,reds:0,runs:0};h.runs++,c.source==="agent-heal"&&c.status==="yellow"&&h.heals++,c.status==="red"&&h.reds++,a.set(g,h)}let d=[...a.values()].filter(l=>l.heals>=2||l.reds>=3).sort((l,c)=>c.heals+c.reds-(l.heals+l.reds)),p=new Map;for(let l of r)for(let c of l.scenarios)c.status==="red"&&c.error&&p.set(c.error,(p.get(c.error)??0)+1);let m=[...p.entries()].sort((l,c)=>c[1]-l[1]).slice(0,3).map(([l,c])=>({message:l,count:c}));return{prev:i,series:{passRate:r.map(Ke),costUsd:r.map(l=>Number(l.costUsd.toFixed(4))),aiCalls:r.map(l=>l.aiCalls)},chronic:d,divergentSteps:e.divergentSteps,topIssues:m}}import E from"node:fs";import O from"node:path";var pe={claude:".claude/skills",agents:".agents/skills",copilot:".github/skills",cursor:".cursor/skills"},Nr=["claude","agents"],yn="<!-- saffron:start -->",Ze="<!-- saffron:end -->";function zr(){return[yn,"## 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 \u2014 write them as the verdict, last.","- Never edit `.saffron/cache/` or `.saffron/proposals/` by hand; change the"," scenario text and re-run. Review proposals with `npx saffron accept`.","- Secrets go in `{env:VAR}` tokens, never in files.",Ze,""].join(`
270
- `)}function mn(t,e){if(!E.existsSync(t))return E.writeFileSync(t,e),"created";let n=E.readFileSync(t,"utf8"),r=n.indexOf(yn),s=n.indexOf(Ze);if(r!==-1&&s!==-1&&s>r){let i=n.slice(0,r)+e.trimEnd()+n.slice(s+Ze.length);return i===n?"unchanged":(E.writeFileSync(t,i),"updated")}let o=n.endsWith(`
265
+ `}function cn(t,e,n){let r={scenarios:t.length,green:0,yellow:0,red:0,aiCalls:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheCreationTokens:0,costUsd:0};for(let s of t)r[s.status]+=1,r.aiCalls+=s.usage.aiCalls,r.inputTokens+=s.usage.inputTokens,r.outputTokens+=s.usage.outputTokens,r.cacheReadTokens+=s.usage.cacheReadTokens,r.cacheCreationTokens+=s.usage.cacheCreationTokens??0,r.costUsd+=s.usage.costUsd??0;return{tool:"saffron",version:Ce(),startedAt:e.toISOString(),finishedAt:new Date().toISOString(),baseURL:n,totals:r,scenarios:t.map(s=>({feature:s.scenario.featurePath,scenario:s.scenario.displayName,tags:[...s.scenario.tags],status:s.status,source:s.source,durationMs:s.durationMs,usage:s.usage,steps:s.stepResults,narrative:s.narrative,adaptations:s.adaptations,suggestedFeatureEdit:s.suggestedFeatureEdit,featureEdits:s.featureEdits,verified:s.verified,proofError:s.proofError,proposalFile:s.proposalFile,error:s.error}))}}function ln(t,e){let n=et.join(t,".saffron","reports");Ze.mkdirSync(n,{recursive:!0});let r=et.join(n,"latest.json"),s=et.join(n,"latest.html");return Ze.writeFileSync(r,`${JSON.stringify(e,null,2)}
266
+ `),Ze.writeFileSync(s,an(e)),{json:r,html:s}}function dn(t){let e=t===void 0?[]:Array.isArray(t)?t:[t],n=new Set;for(let r of e)for(let s of r.split(/[\s,|]+/)){let o=s.trim();o&&n.add(o.startsWith("@")?o:`@${o}`)}return[...n]}function pn(t,e){return e.length===0?t:t.filter(n=>n.tags.some(r=>e.includes(r)))}function un(t,e){return[...e??[],t]}import Fe from"node:fs";import fn from"node:path";function gn(t){return fn.join(t,".saffron","history.jsonl")}function mn(t,e,n,r){let s=t.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(`
267
+ `)[0]}));return{runId:e.toISOString(),startedAt:e.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 hn(t,e){let n=gn(t);Fe.mkdirSync(fn.dirname(n),{recursive:!0}),Fe.appendFileSync(n,`${JSON.stringify(e)}
268
+ `)}function yn(t,e=50){let n=gn(t);if(!Fe.existsSync(n))return[];let r=Fe.readFileSync(n,"utf8").trim().split(`
269
+ `).filter(Boolean),s=[];for(let o of r.slice(-e))try{s.push(JSON.parse(o))}catch{}return s}var tt=t=>{let e=t.green+t.yellow+t.red;return e===0?0:Math.round((t.green+t.yellow)/e*100)};function Sn(t,e){let n=[...t,e],r=n.slice(-20),s=n.slice(-10),o=t.at(-1),i=o?{passRatePp:tt(e)-tt(o),costUsd:e.costUsd-o.costUsd,aiCalls:e.aiCalls-o.aiCalls,red:e.red-o.red}:void 0,a=new Map;for(let l of s)for(let c of l.scenarios){let g=`${c.feature}::${c.scenario}`,h=a.get(g)??{feature:c.feature,scenario:c.scenario,heals:0,reds:0,runs:0};h.runs++,c.source==="agent-heal"&&c.status==="yellow"&&h.heals++,c.status==="red"&&h.reds++,a.set(g,h)}let d=[...a.values()].filter(l=>l.heals>=2||l.reds>=3).sort((l,c)=>c.heals+c.reds-(l.heals+l.reds)),p=new Map;for(let l of r)for(let c of l.scenarios)c.status==="red"&&c.error&&p.set(c.error,(p.get(c.error)??0)+1);let m=[...p.entries()].sort((l,c)=>c[1]-l[1]).slice(0,3).map(([l,c])=>({message:l,count:c}));return{prev:i,series:{passRate:r.map(tt),costUsd:r.map(l=>Number(l.costUsd.toFixed(4))),aiCalls:r.map(l=>l.aiCalls)},chronic:d,divergentSteps:e.divergentSteps,topIssues:m}}import E from"node:fs";import O from"node:path";var pe={claude:".claude/skills",agents:".agents/skills",copilot:".github/skills",cursor:".cursor/skills"},Wr=["claude","agents"],vn="<!-- saffron:start -->",rt="<!-- saffron:end -->";function Hr(){return[vn,"## 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.","- Never edit `.saffron/cache/` or `.saffron/proposals/` by hand; change the"," scenario text and re-run. Review proposals with `npx saffron accept`.","- Secrets go in `{env:VAR}` tokens, never in files.",rt,""].join(`
270
+ `)}function xn(t,e){if(!E.existsSync(t))return E.writeFileSync(t,e),"created";let n=E.readFileSync(t,"utf8"),r=n.indexOf(vn),s=n.indexOf(rt);if(r!==-1&&s!==-1&&s>r){let i=n.slice(0,r)+e.trimEnd()+n.slice(s+rt.length);return i===n?"unchanged":(E.writeFileSync(t,i),"updated")}let o=n.endsWith(`
271
271
  `)?`
272
272
  `:`
273
273
 
274
- `;return E.writeFileSync(t,n+o+e),"updated"}var Xe={command:"npx",args:["saffron","mcp"]},Br={claude:{file:".mcp.json",key:"mcpServers",entry:Xe},cursor:{file:".cursor/mcp.json",key:"mcpServers",entry:Xe},copilot:{file:".vscode/mcp.json",key:"servers",entry:{type:"stdio",...Xe}}},hn={agents:"codex mcp add saffron -- npx saffron mcp (Codex; other hosts: see their MCP settings)"};function Vr(t,e){let n=O.join(t,e.file),r={},s=E.existsSync(n);if(s)try{r=JSON.parse(E.readFileSync(n,"utf8"))}catch{throw new Error(`${e.file} is not valid JSON \u2014 fix it or remove it, then re-run`)}let o=r[e.key]??{};return JSON.stringify(o.saffron)===JSON.stringify(e.entry)?"unchanged":(o.saffron=e.entry,r[e.key]=o,E.mkdirSync(O.dirname(n),{recursive:!0}),E.writeFileSync(n,JSON.stringify(r,null,2)+`
275
- `),s?"updated":"created")}function Sn(t,e){E.mkdirSync(e,{recursive:!0});for(let n of E.readdirSync(t,{withFileTypes:!0})){let r=O.join(t,n.name),s=O.join(e,n.name);n.isDirectory()?Sn(r,s):E.copyFileSync(r,s)}}function xn(t){let e=O.join(t.packageRoot,"skills","saffron");if(!E.existsSync(O.join(e,"SKILL.md")))throw new Error(`bundled skill not found at ${e}`);let n={skillDirs:[],instructionFiles:[],scaffolded:[],mcpFiles:[],mcpHints:[]},r=t.targets??Nr;for(let s of r){let o=O.join(t.projectRoot,pe[s],"saffron");E.existsSync(o)&&E.rmSync(o,{recursive:!0,force:!0}),Sn(e,o),n.skillDirs.push(O.relative(t.projectRoot,o))}if(t.mcp!==!1)for(let s of r){let o=Br[s];o?n.mcpFiles.push({file:o.file,outcome:Vr(t.projectRoot,o)}):hn[s]&&n.mcpHints.push(hn[s])}if(t.instructions!==!1){let s=zr(),o=O.join(t.projectRoot,"AGENTS.md");n.instructionFiles.push({file:"AGENTS.md",outcome:mn(o,s)});let i=O.join(t.projectRoot,"CLAUDE.md");E.existsSync(i)&&n.instructionFiles.push({file:"CLAUDE.md",outcome:mn(i,s)})}if(t.scaffold!==!1){let s=O.join(t.projectRoot,"saffron.config.json");E.existsSync(s)||(E.writeFileSync(s,JSON.stringify({baseURL:"http://localhost:3000",features:"features"},null,2)+`
274
+ `;return E.writeFileSync(t,n+o+e),"updated"}var nt={command:"npx",args:["saffron","mcp"]},Gr={claude:{file:".mcp.json",key:"mcpServers",entry:nt},cursor:{file:".cursor/mcp.json",key:"mcpServers",entry:nt},copilot:{file:".vscode/mcp.json",key:"servers",entry:{type:"stdio",...nt}}},bn={agents:"codex mcp add saffron -- npx saffron mcp (Codex; other hosts: see their MCP settings)"};function Jr(t,e){let n=O.join(t,e.file),r={},s=E.existsSync(n);if(s)try{r=JSON.parse(E.readFileSync(n,"utf8"))}catch{throw new Error(`${e.file} is not valid JSON: fix it or remove it, then re-run`)}let o=r[e.key]??{};return JSON.stringify(o.saffron)===JSON.stringify(e.entry)?"unchanged":(o.saffron=e.entry,r[e.key]=o,E.mkdirSync(O.dirname(n),{recursive:!0}),E.writeFileSync(n,JSON.stringify(r,null,2)+`
275
+ `),s?"updated":"created")}function wn(t,e){E.mkdirSync(e,{recursive:!0});for(let n of E.readdirSync(t,{withFileTypes:!0})){let r=O.join(t,n.name),s=O.join(e,n.name);n.isDirectory()?wn(r,s):E.copyFileSync(r,s)}}function kn(t){let e=O.join(t.packageRoot,"skills","saffron");if(!E.existsSync(O.join(e,"SKILL.md")))throw new Error(`bundled skill not found at ${e}`);let n={skillDirs:[],instructionFiles:[],scaffolded:[],mcpFiles:[],mcpHints:[]},r=t.targets??Wr;for(let s of r){let o=O.join(t.projectRoot,pe[s],"saffron");E.existsSync(o)&&E.rmSync(o,{recursive:!0,force:!0}),wn(e,o),n.skillDirs.push(O.relative(t.projectRoot,o))}if(t.mcp!==!1)for(let s of r){let o=Gr[s];o?n.mcpFiles.push({file:o.file,outcome:Jr(t.projectRoot,o)}):bn[s]&&n.mcpHints.push(bn[s])}if(t.instructions!==!1){let s=Hr(),o=O.join(t.projectRoot,"AGENTS.md");n.instructionFiles.push({file:"AGENTS.md",outcome:xn(o,s)});let i=O.join(t.projectRoot,"CLAUDE.md");E.existsSync(i)&&n.instructionFiles.push({file:"CLAUDE.md",outcome:xn(i,s)})}if(t.scaffold!==!1){let s=O.join(t.projectRoot,"saffron.config.json");E.existsSync(s)||(E.writeFileSync(s,JSON.stringify({baseURL:"http://localhost:3000",features:"features"},null,2)+`
276
276
  `),n.scaffolded.push("saffron.config.json"));let o=O.join(t.projectRoot,"features");E.existsSync(o)||(E.mkdirSync(o,{recursive:!0}),n.scaffolded.push("features/"));let i=O.join(t.projectRoot,".gitignore"),a=".saffron/reports/",d=E.existsSync(i)?E.readFileSync(i,"utf8"):"";d.split(`
277
277
  `).some(p=>p.trim()===a)||(E.writeFileSync(i,d+(d&&!d.endsWith(`
278
278
  `)?`
279
279
  `:"")+a+`
280
- `),n.scaffolded.push(".gitignore (+ .saffron/reports/)"))}return n}function re(t){wt(t);let e=R.join(t,"saffron.config.json");return I.existsSync(e)?JSON.parse(I.readFileSync(e,"utf8")):{}}function Gr(t,e){let n=e.length>0?e:["features"],r=[];for(let s of n){let o=R.isAbsolute(s)?s:R.join(t,s);if(I.existsSync(o))if(I.statSync(o).isDirectory())for(let i of I.readdirSync(o,{recursive:!0})){let a=String(i);se(a)&&r.push(R.join(o,a))}else se(o)&&r.push(o)}return r.sort()}function bn(t,e,n){let r=R.isAbsolute(e.features??"features")?e.features:R.join(t,e.features??"features"),s=new Set(ue(r));for(let o of n)o.endsWith(".saffron")&&s.add(o);return X([...s].sort(),t)}function Yr(){let t=process.env.ANTHROPIC_API_KEY||process.env.CLAUDE_CODE_OAUTH_TOKEN||process.env.CLAUDE_CODE_USE_BEDROCK||process.env.CLAUDE_CODE_USE_VERTEX,e=process.env.HOME??"",n=e&&(I.existsSync(R.join(e,".claude",".credentials.json"))||I.existsSync(R.join(e,".claude.json")));t||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(`
281
- `))}function Ce(t){console.error(`saffron: ${t.message}`),process.exit(2)}function et(t){return t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e4?`${(t/1e3).toFixed(1)}k`:t.toLocaleString()}var Jr={green:"\x1B[32m",yellow:"\x1B[33m",red:"\x1B[31m"},Qr="\x1B[0m";function vn(){let t=R.dirname(Wr(import.meta.url));for(let e=0;e<5;e++){let n=R.join(t,"package.json");if(I.existsSync(n))try{if(JSON.parse(I.readFileSync(n,"utf8")).name==="saffron-ai")return t}catch{}t=R.dirname(t)}}function Kr(){let t=vn();if(!t)return"unknown";try{return JSON.parse(I.readFileSync(R.join(t,"package.json"),"utf8")).version??"unknown"}catch{return"unknown"}}var P=new Hr().name("saffron").description("Gherkin-native AI-fallback test runner: zero-token cached replay, runtime AI healing.").version(Kr()).option("-p, --project-root <dir>","project root",process.cwd());P.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 \u2014 comma-separated ("@smoke,@WA-TC1") or repeat the flag',cn).option("--no-agent","disable AI fallback (cache misses/failures go red)").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" \u2014 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(t,e)=>{let n=R.resolve(P.opts().projectRoot),r=re(n),s=e.baseUrl??r.baseURL,o=Gr(n,t.length>0?t:r.features?[r.features]:[]);o.length===0&&(console.error("No .feature/.saffron files found."),process.exit(2)),e.agent!==!1&&Yr();let i;try{let S=bn(n,r,o);i=o.flatMap(T=>Z(T,n,S))}catch(S){throw S instanceof M&&Ce(S),S}let a=on(e.filter);a.length>0&&(i=an(i,a),i.length===0&&(console.error(`No scenarios match --filter ${a.join(",")} in ${o.length} feature file(s).`),process.exit(2)));{let S=new Map;for(let T of i){let C=Le(T),F=N(W(n,T.featurePath,T.name));F&&C.push(JSON.stringify(F.steps));for(let _ of Me(C))process.env[_]===void 0&&!S.has(_)&&S.set(_,T.displayName)}if(S.size>0){for(let[T,C]of S)console.error(`missing environment variable ${T} (referenced as {env:${T}} by "${C}")`);console.error("set the variable(s), or add them to a git-ignored .env in the project root"),process.exit(2)}}let d=new we({projectRoot:n,baseURL:s,headed:e.headed,actionTimeoutMs:r.actionTimeoutMs,retries:r.retries,storageState:e.storageState??r.storageState,browser:(()=>{let S=e.browser??r.browser??"chromium";return S!=="chromium"&&S!=="firefox"&&S!=="webkit"&&(console.error(`invalid --browser "${S}" \u2014 use "chromium", "firefox", or "webkit"`),process.exit(2)),S})(),healModel:e.healModel??r.healModel,verifyProposals:e.verify===!1?!1:r.verifyProposals,reuseSteps:e.reuse===!1?!1:r.reuseSteps,assertionPolicy:(()=>{let S=e.assertionPolicy??r.assertionPolicy??"strict";return S!=="strict"&&S!=="adaptable-mid"&&(console.error(`invalid --assertion-policy "${S}" \u2014 use "strict" or "adaptable-mid"`),process.exit(2)),S})(),agent:e.agent?new $e({model:e.model??r.model,maxTurns:r.maxTurns,snapshotMode:r.snapshotMode}):void 0}),p=new Date,m=Math.max(1,Number.parseInt(String(e.workers??r.workers??1),10)||1),l=new Array(i.length),c=S=>{let T=S.scenario,C=Jr[S.status],F=S.usage.inputTokens+S.usage.outputTokens,_=S.usage.cacheReadTokens+S.usage.cacheCreationTokens,kn=_>0?`, ${et(_)} cache traffic`:"";console.log(`${C}${S.status.toUpperCase().padEnd(7)}${Qr}${T.displayName} (${S.source}, ${S.durationMs}ms, ${S.usage.aiCalls} AI calls, ${F} tokens${kn})`);for(let $n of S.adaptations)console.log(` \u26A0 ${$n}`);S.seededSteps&&console.log(` \u26A1 ${S.seededSteps}/${S.stepResults.length} steps seeded from existing recordings (zero AI)`),S.verified===!0?console.log(" \u2713 proposal verified by zero-AI proof replay"):S.verified===!1&&console.log(` \u2717 proposal UNVERIFIED \u2014 proof replay failed: ${S.proofError?.split(`
282
- `)[0]}`),S.error&&console.log(` ${S.error}`)};await d.start();try{if(m>1&&i.length>1){let S=i.map((C,F)=>({s:C,i:F})),T=[];await Promise.all(Array.from({length:Math.min(m,S.length)},async()=>{for(;;){let C=S.shift();if(!C)return;let F=await d.runScenario(C.s,{agentDisabled:!0});F.status==="red"?T.push(C):(l[C.i]=F,c(F))}}));for(let C of T.sort((F,_)=>F.i-_.i)){let F=await d.runScenario(C.s);l[C.i]=F,c(F)}}else for(let S=0;S<i.length;S++){let T=await d.runScenario(i[S]);l[S]=T,c(T)}}finally{await d.stop()}let g=rn(l,p,s),h=H(n).divergent.length,u=pn(l,p,h,s),f=fn(n);g.trends=gn(f,u),un(n,u);let{html:y,json:x}=sn(n,g),b=g.totals,w=b.cacheReadTokens+b.cacheCreationTokens;console.log(`
283
- ${b.green} passed, ${b.yellow} adapted, ${b.red} failed \xB7 ${b.aiCalls} AI calls \xB7 ${(b.inputTokens+b.outputTokens).toLocaleString()} tokens in+out${w>0?` \xB7 ${et(b.cacheReadTokens)} cache read \xB7 ${et(b.cacheCreationTokens)} cache written`:""} \xB7 $${b.costUsd.toFixed(4)}`);let k=g.trends;if(k?.prev){let S=k.prev.passRatePp,T=k.prev.costUsd;console.log(`vs previous run: pass rate ${S>=0?"+":""}${S}pp \xB7 cost ${T>=0?"+":"-"}$${Math.abs(T).toFixed(4)} \xB7 AI calls ${k.prev.aiCalls>=0?"+":""}${k.prev.aiCalls}`)}for(let S of k?.chronic??[])console.log(`\u26A0 chronic: ${S.feature} \u203A ${S.scenario} \u2014 ${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: ${y}
284
- ${x}`);let v=ce(R.resolve(P.opts().projectRoot));v.length>0&&console.log(`${v.length} cache proposal(s) pending \u2014 review with: saffron accept | saffron reject`);let j=e.strict??r.strict??!1;j&&b.yellow>0&&console.log(`strict mode: ${b.yellow} adapted scenario(s) treated as failure \u2014 review proposals to go green.`),process.exit(b.red>0||j&&b.yellow>0?1:0)});P.command("accept").description("Promote pending cache proposals to committed caches").argument("[file]","proposal file (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").action((t,e)=>{let n=R.resolve(P.opts().projectRoot),r=re(n),s=t?[R.resolve(t)]:e.all?ce(n).map(o=>o.file):[];if(s.length===0){wn(n);return}for(let o of s){let i=xe(o);i.meta.verified===!1&&console.log(`\u26A0 accepting UNVERIFIED proposal (proof replay failed: ${i.meta.proofError?.split(`
285
- `)[0]}) \u2014 expect a heal or red on the next run.`);let a=N(W(n,i.cache.feature,i.cache.scenario)),d=$t(n,o);console.log(`accepted \u2192 ${d}`);let p=a?Jt(a,i.cache):[];if(p.length>0){let c=Qt(n,p,d,!e.propagate);for(let g of p)console.log(` locator fix: ${Ge(g.from)} \u2192 ${Ge(g.to)}`);if(c.length===0)console.log(" no other caches use this locator.");else if(e.propagate)for(let g of c)console.log(` propagated \u2192 ${g.feature} \u203A ${g.scenario} (${g.steps.map(h=>`"${h.gherkin}"`).join(", ")})`);else{console.log(` ${c.length} other cache(s) use the same locator \u2014 re-run with --propagate to fix them too:`);for(let g of c)console.log(` \xB7 ${g.feature} \u203A ${g.scenario}`)}}let m=i.meta.featureEdits??[];if(!e.withFeatureEdit||m.length===0)continue;let l;try{l=Xt(n,i.cache.feature,i.cache.scenario,m,bn(n,r,[]))}catch(c){throw c instanceof M&&Ce(c),c}for(let c of l.applied){let g=c.fromStepSet?`${c.file}:${c.line} (StepSet "${c.fromStepSet}" \u2014 fixes every invoking scenario)`:`${c.file}:${c.line}`;console.log(` feature edit ${g}:`),console.log(` - ${c.from}`),console.log(` + ${c.to}`)}for(let c of l.skipped)console.log(` skipped edit [${c.index}]: ${c.reason}`);if(l.applied.length>0){let c=N(d);for(let g of m)c.steps[g.index]&&(c.steps[g.index].gherkin=g.text);ee(d,c)}}});P.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((t,e)=>{let n=R.resolve(P.opts().projectRoot),r=re(n),s;try{s=G(n,r.features??"features")}catch(i){throw i instanceof M&&Ce(i),i}if(t&&(s=me(s,t)),e.json){console.log(JSON.stringify(s,null,2));return}if(e.snippets){let i=R.join(n,".vscode","saffron.code-snippets");I.mkdirSync(R.dirname(i),{recursive:!0}),I.writeFileSync(i,gt(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)) \u2014 ${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(`
286
- ${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`)});P.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(t,e)=>{let n=R.resolve(P.opts().projectRoot),r=re(n),s=I.readFileSync(R.resolve(t),"utf8"),o;try{o=G(n,r.features??"features")}catch(p){throw p instanceof M&&Ce(p),p}console.log(`authoring with ${o.steps.length} known step(s) and ${o.stepSets.length} step set(s) as vocabulary\u2026`);let i=await ht(s,o,e.model??r.model),a=e.out?R.resolve(e.out):R.join(n,r.features??"features",`${R.basename(t).replace(/\.[^.]+$/,"")}.saffron`);I.existsSync(a)&&(console.error(`refusing to overwrite ${a} \u2014 pass --out for a new path`),process.exit(1)),I.mkdirSync(R.dirname(a),{recursive:!0}),I.writeFileSync(a,i.content);let d=i.content.split(`
287
- `).map(p=>p.trim().replace(/^(Given|When|Then|And|But|\*)\s+/,"")).filter(p=>o.steps.some(m=>m.text===p)).length;console.log(`wrote ${a}`),console.log(`${d} 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 "+R.relative(process.cwd(),a))});P.command("mcp").description("Serve the step vocabulary to AI assistants over stdio MCP (search_steps, list_step_sets)").action(async()=>{let t=R.resolve(P.opts().projectRoot),e=re(t);await mt(t,e.features??"features")});P.command("lsp").description("Run the Saffron language server over stdio (completion, go-to-definition, hover, diagnostics) \u2014 for JetBrains (LSP4IJ / Ultimate LSP API), Neovim, and any LSP-capable editor").action(()=>{let t=R.resolve(P.opts().projectRoot),e=re(t);xt(t,e.features??"features")});P.command("reject").description("Discard pending cache proposals (AI will retry next run)").argument("[file]","proposal file").option("--all","reject all pending proposals").action((t,e)=>{let n=R.resolve(P.opts().projectRoot),r=t?[R.resolve(t)]:e.all?ce(n).map(s=>s.file):[];if(r.length===0){wn(n);return}for(let s of r)Rt(s),console.log(`rejected ${s}`)});P.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(pe).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)").action(t=>{let e=R.resolve(P.opts().projectRoot),n=vn();n||(console.error("saffron: cannot locate the installed package root"),process.exit(2));let r=t.agents?String(t.agents).split(",").map(o=>o.trim()).filter(Boolean):void 0;for(let o of r??[])o in pe||(console.error(`saffron: unknown agent target "${o}" (expected one of ${Object.keys(pe).join(", ")})`),process.exit(2));let s=xn({projectRoot:e,packageRoot:n,targets:r,instructions:t.instructions,scaffold:t.scaffold,mcp:t.mcp});for(let o of s.skillDirs)console.log(`skill \u2192 ${o}/`);for(let o of s.mcpFiles)console.log(`${o.outcome.padEnd(9)} \u2192 ${o.file} (saffron mcp server)`);for(let o of s.mcpHints)console.log(`mcp hint \u2192 ${o}`);for(let o of s.instructionFiles)console.log(`${o.outcome.padEnd(9)} \u2192 ${o.file} (managed Saffron block)`);for(let o of s.scaffolded)console.log(`created \u2192 ${o}`);console.log("\nAgents now load the saffron skill when writing tests. Re-run after `npm update saffron-ai` to refresh it.")});P.command("report").description("Open the latest HTML report").option("--open","open in the default browser",!0).action(()=>{let t=R.resolve(P.opts().projectRoot),e=R.join(t,".saffron","reports","latest.html");I.existsSync(e)||(console.error("No report found. Run `saffron run` first."),process.exit(2)),console.log(e);let n=process.platform==="darwin"?"open":"xdg-open";qr(n,[e],{detached:!0,stdio:"ignore"}).unref()});function wn(t){let e=ce(t);if(e.length===0){console.log("No pending proposals.");return}console.log(`Pending proposals:
280
+ `),n.scaffolded.push(".gitignore (+ .saffron/reports/)"))}return n}import Yr from"node:path";function Rn(t){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):"," 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:",` ${t?Yr.join(t,"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(`
281
+ `)}function re(t){Tt(t);let e=$.join(t,"saffron.config.json");return I.existsSync(e)?JSON.parse(I.readFileSync(e,"utf8")):{}}function Xr(t,e){let n=e.length>0?e:["features"],r=[];for(let s of n){let o=$.isAbsolute(s)?s:$.join(t,s);if(I.existsSync(o))if(I.statSync(o).isDirectory())for(let i of I.readdirSync(o,{recursive:!0})){let a=String(i);se(a)&&r.push($.join(o,a))}else se(o)&&r.push(o)}return r.sort()}function $n(t,e,n){let r=$.isAbsolute(e.features??"features")?e.features:$.join(t,e.features??"features"),s=new Set(ue(r));for(let o of n)o.endsWith(".saffron")&&s.add(o);return X([...s].sort(),t)}function Zr(){let t=process.env.ANTHROPIC_API_KEY||process.env.CLAUDE_CODE_OAUTH_TOKEN||process.env.CLAUDE_CODE_USE_BEDROCK||process.env.CLAUDE_CODE_USE_VERTEX,e=process.env.HOME??"",n=e&&(I.existsSync($.join(e,".claude",".credentials.json"))||I.existsSync($.join(e,".claude.json")));t||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(`
282
+ `))}function Ie(t){console.error(`saffron: ${t.message}`),process.exit(2)}function st(t){return t>=1e6?`${(t/1e6).toFixed(1)}M`:t>=1e4?`${(t/1e3).toFixed(1)}k`:t.toLocaleString()}var es={green:"\x1B[32m",yellow:"\x1B[33m",red:"\x1B[31m"},ts="\x1B[0m",P=new Kr().name("saffron").description("Gherkin-native AI-fallback test runner: zero-token cached replay, runtime AI healing.").version(Ce()).option("-p, --project-root <dir>","project root",process.cwd());P.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 \u2014 comma-separated ("@smoke,@WA-TC1") or repeat the flag',un).option("--no-agent","disable AI fallback (cache misses/failures go red)").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" \u2014 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(t,e)=>{let n=$.resolve(P.opts().projectRoot),r=re(n),s=e.baseUrl??r.baseURL,o=Xr(n,t.length>0?t:r.features?[r.features]:[]);o.length===0&&(console.error("No .feature/.saffron files found."),process.exit(2)),e.agent!==!1&&Zr();let i;try{let S=$n(n,r,o);i=o.flatMap(T=>Z(T,n,S))}catch(S){throw S instanceof M&&Ie(S),S}let a=dn(e.filter);a.length>0&&(i=pn(i,a),i.length===0&&(console.error(`No scenarios match --filter ${a.join(",")} in ${o.length} feature file(s).`),process.exit(2)));{let S=new Map;for(let T of i){let C=Be(T),F=N(W(n,T.featurePath,T.name));F&&C.push(JSON.stringify(F.steps));for(let _ of Ne(C))process.env[_]===void 0&&!S.has(_)&&S.set(_,T.displayName)}if(S.size>0){for(let[T,C]of S)console.error(`missing environment variable ${T} (referenced as {env:${T}} by "${C}")`);console.error("set the variable(s), or add them to a git-ignored .env in the project root"),process.exit(2)}}let d=new we({projectRoot:n,baseURL:s,headed:e.headed,actionTimeoutMs:r.actionTimeoutMs,retries:r.retries,storageState:e.storageState??r.storageState,browser:(()=>{let S=e.browser??r.browser??"chromium";return S!=="chromium"&&S!=="firefox"&&S!=="webkit"&&(console.error(`invalid --browser "${S}" \u2014 use "chromium", "firefox", or "webkit"`),process.exit(2)),S})(),healModel:e.healModel??r.healModel,verifyProposals:e.verify===!1?!1:r.verifyProposals,reuseSteps:e.reuse===!1?!1:r.reuseSteps,assertionPolicy:(()=>{let S=e.assertionPolicy??r.assertionPolicy??"strict";return S!=="strict"&&S!=="adaptable-mid"&&(console.error(`invalid --assertion-policy "${S}" \u2014 use "strict" or "adaptable-mid"`),process.exit(2)),S})(),agent:e.agent?new Re({model:e.model??r.model,maxTurns:r.maxTurns,snapshotMode:r.snapshotMode}):void 0}),p=new Date,m=Math.max(1,Number.parseInt(String(e.workers??r.workers??1),10)||1),l=new Array(i.length),c=S=>{let T=S.scenario,C=es[S.status],F=S.usage.inputTokens+S.usage.outputTokens,_=S.usage.cacheReadTokens+S.usage.cacheCreationTokens,En=_>0?`, ${st(_)} cache traffic`:"";console.log(`${C}${S.status.toUpperCase().padEnd(7)}${ts}${T.displayName} (${S.source}, ${S.durationMs}ms, ${S.usage.aiCalls} AI calls, ${F} tokens${En})`);for(let An of S.adaptations)console.log(` \u26A0 ${An}`);S.seededSteps&&console.log(` \u26A1 ${S.seededSteps}/${S.stepResults.length} steps seeded from existing recordings (zero AI)`),S.verified===!0?console.log(" \u2713 proposal verified by zero-AI proof replay"):S.verified===!1&&console.log(` \u2717 proposal UNVERIFIED \u2014 proof replay failed: ${S.proofError?.split(`
283
+ `)[0]}`),S.error&&console.log(` ${S.error}`)};await d.start();try{if(m>1&&i.length>1){let S=i.map((C,F)=>({s:C,i:F})),T=[];await Promise.all(Array.from({length:Math.min(m,S.length)},async()=>{for(;;){let C=S.shift();if(!C)return;let F=await d.runScenario(C.s,{agentDisabled:!0});F.status==="red"?T.push(C):(l[C.i]=F,c(F))}}));for(let C of T.sort((F,_)=>F.i-_.i)){let F=await d.runScenario(C.s);l[C.i]=F,c(F)}}else for(let S=0;S<i.length;S++){let T=await d.runScenario(i[S]);l[S]=T,c(T)}}finally{await d.stop()}let g=cn(l,p,s),h=H(n).divergent.length,u=mn(l,p,h,s),f=yn(n);g.trends=Sn(f,u),hn(n,u);let{html:y,json:x}=ln(n,g),b=g.totals,w=b.cacheReadTokens+b.cacheCreationTokens;console.log(`
284
+ ${b.green} passed, ${b.yellow} adapted, ${b.red} failed \xB7 ${b.aiCalls} AI calls \xB7 ${(b.inputTokens+b.outputTokens).toLocaleString()} tokens in+out${w>0?` \xB7 ${st(b.cacheReadTokens)} cache read \xB7 ${st(b.cacheCreationTokens)} cache written`:""} \xB7 $${b.costUsd.toFixed(4)}`);let k=g.trends;if(k?.prev){let S=k.prev.passRatePp,T=k.prev.costUsd;console.log(`vs previous run: pass rate ${S>=0?"+":""}${S}pp \xB7 cost ${T>=0?"+":"-"}$${Math.abs(T).toFixed(4)} \xB7 AI calls ${k.prev.aiCalls>=0?"+":""}${k.prev.aiCalls}`)}for(let S of k?.chronic??[])console.log(`\u26A0 chronic: ${S.feature} \u203A ${S.scenario} \u2014 ${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: ${y}
285
+ ${x}`);let v=ce($.resolve(P.opts().projectRoot));v.length>0&&console.log(`${v.length} cache proposal(s) pending \u2014 review with: saffron accept | saffron reject`);let j=e.strict??r.strict??!1;j&&b.yellow>0&&console.log(`strict mode: ${b.yellow} adapted scenario(s) treated as failure \u2014 review proposals to go green.`),process.exit(b.red>0||j&&b.yellow>0?1:0)});P.command("accept").description("Promote pending cache proposals to committed caches").argument("[file]","proposal file (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").action((t,e)=>{let n=$.resolve(P.opts().projectRoot),r=re(n),s=t?[$.resolve(t)]:e.all?ce(n).map(o=>o.file):[];if(s.length===0){Tn(n);return}for(let o of s){let i=xe(o);i.meta.verified===!1&&console.log(`\u26A0 accepting UNVERIFIED proposal (proof replay failed: ${i.meta.proofError?.split(`
286
+ `)[0]}) \u2014 expect a heal or red on the next run.`);let a=N(W(n,i.cache.feature,i.cache.scenario)),d=At(n,o);console.log(`accepted \u2192 ${d}`);let p=a?Zt(a,i.cache):[];if(p.length>0){let c=en(n,p,d,!e.propagate);for(let g of p)console.log(` locator fix: ${Qe(g.from)} \u2192 ${Qe(g.to)}`);if(c.length===0)console.log(" no other caches use this locator.");else if(e.propagate)for(let g of c)console.log(` propagated \u2192 ${g.feature} \u203A ${g.scenario} (${g.steps.map(h=>`"${h.gherkin}"`).join(", ")})`);else{console.log(` ${c.length} other cache(s) use the same locator \u2014 re-run with --propagate to fix them too:`);for(let g of c)console.log(` \xB7 ${g.feature} \u203A ${g.scenario}`)}}let m=i.meta.featureEdits??[];if(!e.withFeatureEdit||m.length===0)continue;let l;try{l=nn(n,i.cache.feature,i.cache.scenario,m,$n(n,r,[]))}catch(c){throw c instanceof M&&Ie(c),c}for(let c of l.applied){let g=c.fromStepSet?`${c.file}:${c.line} (StepSet "${c.fromStepSet}" \u2014 fixes every invoking scenario)`:`${c.file}:${c.line}`;console.log(` feature edit ${g}:`),console.log(` - ${c.from}`),console.log(` + ${c.to}`)}for(let c of l.skipped)console.log(` skipped edit [${c.index}]: ${c.reason}`);if(l.applied.length>0){let c=N(d);for(let g of m)c.steps[g.index]&&(c.steps[g.index].gherkin=g.text);ee(d,c)}}});P.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((t,e)=>{let n=$.resolve(P.opts().projectRoot),r=re(n),s;try{s=G(n,r.features??"features")}catch(i){throw i instanceof M&&Ie(i),i}if(t&&(s=me(s,t)),e.json){console.log(JSON.stringify(s,null,2));return}if(e.snippets){let i=$.join(n,".vscode","saffron.code-snippets");I.mkdirSync($.dirname(i),{recursive:!0}),I.writeFileSync(i,St(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)) \u2014 ${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(`
287
+ ${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`)});P.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(t,e)=>{let n=$.resolve(P.opts().projectRoot),r=re(n),s=I.readFileSync($.resolve(t),"utf8"),o;try{o=G(n,r.features??"features")}catch(p){throw p instanceof M&&Ie(p),p}console.log(`authoring with ${o.steps.length} known step(s) and ${o.stepSets.length} step set(s) as vocabulary\u2026`);let i=await bt(s,o,e.model??r.model),a=e.out?$.resolve(e.out):$.join(n,r.features??"features",`${$.basename(t).replace(/\.[^.]+$/,"")}.saffron`);I.existsSync(a)&&(console.error(`refusing to overwrite ${a} \u2014 pass --out for a new path`),process.exit(1)),I.mkdirSync($.dirname(a),{recursive:!0}),I.writeFileSync(a,i.content);let d=i.content.split(`
288
+ `).map(p=>p.trim().replace(/^(Given|When|Then|And|But|\*)\s+/,"")).filter(p=>o.steps.some(m=>m.text===p)).length;console.log(`wrote ${a}`),console.log(`${d} 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 "+$.relative(process.cwd(),a))});P.command("mcp").description("Serve the step vocabulary to AI assistants over stdio MCP (search_steps, list_step_sets)").action(async()=>{let t=$.resolve(P.opts().projectRoot),e=re(t);await xt(t,e.features??"features")});P.command("lsp").description("Run the Saffron language server over stdio (completion, go-to-definition, hover, diagnostics) \u2014 for JetBrains (LSP4IJ / Ultimate LSP API), Neovim, and any LSP-capable editor").action(()=>{process.stdin.isTTY&&(console.error(Rn(Ae())),process.exit(2));let t=$.resolve(P.opts().projectRoot),e=re(t);kt(t,e.features??"features")});P.command("reject").description("Discard pending cache proposals (AI will retry next run)").argument("[file]","proposal file").option("--all","reject all pending proposals").action((t,e)=>{let n=$.resolve(P.opts().projectRoot),r=t?[$.resolve(t)]:e.all?ce(n).map(s=>s.file):[];if(r.length===0){Tn(n);return}for(let s of r)Ct(s),console.log(`rejected ${s}`)});P.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(pe).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)").action(t=>{let e=$.resolve(P.opts().projectRoot),n=Ae();n||(console.error("saffron: cannot locate the installed package root"),process.exit(2));let r=t.agents?String(t.agents).split(",").map(o=>o.trim()).filter(Boolean):void 0;for(let o of r??[])o in pe||(console.error(`saffron: unknown agent target "${o}" (expected one of ${Object.keys(pe).join(", ")})`),process.exit(2));let s=kn({projectRoot:e,packageRoot:n,targets:r,instructions:t.instructions,scaffold:t.scaffold,mcp:t.mcp});for(let o of s.skillDirs)console.log(`skill \u2192 ${o}/`);for(let o of s.mcpFiles)console.log(`${o.outcome.padEnd(9)} \u2192 ${o.file} (saffron mcp server)`);for(let o of s.mcpHints)console.log(`mcp hint \u2192 ${o}`);for(let o of s.instructionFiles)console.log(`${o.outcome.padEnd(9)} \u2192 ${o.file} (managed Saffron block)`);for(let o of s.scaffolded)console.log(`created \u2192 ${o}`);console.log("\nAgents now load the saffron skill when writing tests. Re-run after `npm update saffron-ai` to refresh it.")});P.command("report").description("Open the latest HTML report").option("--open","open in the default browser",!0).action(()=>{let t=$.resolve(P.opts().projectRoot),e=$.join(t,".saffron","reports","latest.html");I.existsSync(e)||(console.error("No report found. Run `saffron run` first."),process.exit(2)),console.log(e);let n=process.platform==="darwin"?"open":"xdg-open";Qr(n,[e],{detached:!0,stdio:"ignore"}).unref()});function Tn(t){let e=ce(t);if(e.length===0){console.log("No pending proposals.");return}console.log(`Pending proposals:
288
289
  `);for(let{file:n,proposal:r}of e){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:
289
290
  ${r.meta.suggestedFeatureEdit.split(`
290
291
  `).map(o=>` ${o}`).join(`