werkmeister 0.76.1 → 0.77.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { createRequire as __sandboxCreateRequire } from "node:module";
2
2
  const require = __sandboxCreateRequire(import.meta.url);
3
- var $=Object.defineProperty;var r=(t,e)=>$(t,"name",{value:e,configurable:!0});import*as a from"node:fs";import*as d from"node:path";import*as N from"node:path";var E=".werkmeister";function l(t){return N.join(t,E)}r(l,"dotDir");var _="mcp__ccd_turn__wrap_up";import*as g from"node:fs";import*as p from"node:path";function w(t){let e=p.resolve(t);for(;;){if(g.existsSync(p.join(l(e),"workspace.yml")))return e;let n=p.dirname(e);if(n===e)return null;e=n}}r(w,"findRecordRoot");var v=new Set(["working","needs-you","done","idle"]),U=new Set(["AskUserQuestion","ExitPlanMode"]),b=new Set(["permission_prompt","elicitation_dialog"]),R=r(t=>d.join(l(t),"agent-state.json"),"stateFile");function j(t){try{let e=JSON.parse(a.readFileSync(R(t),"utf8"));return e&&typeof e=="object"&&!Array.isArray(e)?e:null}catch{return null}}r(j,"readRaw");function q(t){if(!v.has(t?.state)||typeof t?.at!="string")return null;let e={state:t.state,at:t.at};return typeof t.question=="string"&&(e.question=t.question),t.via==="wrap_up"&&(e.via="wrap_up"),e}r(q,"stateOf");function I(t){let e={};for(let[n,s]of Object.entries(t?.sessions??{}))(s?.status==="busy"||s?.status==="idle")&&typeof s.at=="string"&&(e[n]={status:s.status,at:s.at,...s.headless===!0&&{headless:!0}});return e}r(I,"sessionsOf");var W=new Set(["UserPromptSubmit","PreToolUse","PostToolUse"]);function C(t){return t==="SessionEnd"?null:t==="SessionStart"||t==="Stop"?"idle":W.has(t)?"busy":void 0}r(C,"nextSessionStatus");function F(t){return/\?[\s*_`'")\]]*$/.test(t)}r(F,"asksQuestion");function J(t){let e=t.trim().split(/\n\s*\n/);return e[e.length-1].trim().slice(0,200)}r(J,"questionOf");function Y(t){let e=a.openSync(t,"r"),n;try{let o=a.fstatSync(e).size,i=Math.min(o,65536),f=Buffer.alloc(i);a.readSync(e,f,0,i,o-i),n=f.toString("utf8")}finally{a.closeSync(e)}let s=n.split(`
4
- `);for(let o=s.length-1;o>=0;o--){let i;try{i=JSON.parse(s[o])}catch{continue}if(i.type!=="assistant")continue;let f=i.message?.content;if(typeof f=="string")return f;if(!Array.isArray(f))continue;let u=f.filter(c=>c?.type==="text"&&typeof c.text=="string");if(u.length)return u.map(c=>c.text).join(`
5
- `)}return null}r(Y,"lastAssistantText");function B(t,e){switch(t.hook_event_name){case"UserPromptSubmit":return{state:"working"};case"PostToolUse":return t.tool_name===_&&e?.via==="wrap_up"?null:{state:"working"};case"PreToolUse":{let n=String(t.tool_name??""),s=t.tool_input?.ask;if(n===_&&typeof s=="string"&&s.trim())return{state:"needs-you",question:s.trim(),via:"wrap_up"};if(!U.has(n))return{state:"working"};let o=t.tool_input?.questions?.[0]?.question;return{state:"needs-you",question:typeof o=="string"?o:"Approve the plan?"}}case"Notification":{let n=t.notification_type,s=String(t.message??"");return!(typeof n=="string"?b.has(n):!/waiting for your input/i.test(s))||e?.state==="needs-you"&&e.question?null:{state:"needs-you",question:s||void 0}}case"Stop":{let n=null;return typeof t.last_assistant_message=="string"?n=t.last_assistant_message:typeof t.transcript_path=="string"&&(n=Y(t.transcript_path)),n&&F(n)?{state:"needs-you",question:J(n)}:e?.via==="wrap_up"?null:{state:"done"}}case"SessionEnd":return e?.state==="working"?{state:"idle"}:null;default:return null}}r(B,"nextState");var G="Workspace paused by the person from its card. End your turn now; it continues when they press Resume.";var L=r(t=>d.join(l(t),"paused.json"),"pausedPath"),M=r(t=>d.join(l(t),"resume-nudge.json"),"nudgePath"),K=r(t=>d.join(l(t),"waiting.json"),"waitingPath");function Q(t,e){let n=`${t}.${process.pid}.tmp`;a.writeFileSync(n,`${JSON.stringify(e)}
6
- `),a.renameSync(n,t)}r(Q,"writeAtomic");function z(t){try{let e=JSON.parse(a.readFileSync(K(t),"utf8"));return Number.isInteger(e?.position)?{position:e.position,reason:String(e.reason??"")}:null}catch{return null}}r(z,"readWaiting");var T=r((t,e)=>d.join(l(t),`wake-nudge-${e}.json`),"wakeNudgePath"),H=/^[\w-]+$/;function V(t){try{let e=JSON.parse(a.readFileSync(t,"utf8"))?.message;return typeof e=="string"?e:null}catch{return null}}r(V,"readWakeNudge");var A=r((t,e)=>typeof e=="string"&&H.test(e)&&a.existsSync(T(t,e)),"pendingWake");function X(t,e){let n=t.hook_event_name;if(n!=="UserPromptSubmit"&&n!=="PostToolUse"&&n!=="Stop"||t.agent_id||!A(e,t.session_id))return null;let s=T(e,t.session_id),o=`${s}.${process.pid}-${Date.now()}.claimed`;try{a.renameSync(s,o)}catch{return null}let i=V(o);return a.rmSync(o,{force:!0}),i===null?null:n==="Stop"?{decision:"block",reason:i}:{hookSpecificOutput:{hookEventName:n,additionalContext:i}}}r(X,"deliverWake");function Z(t,e){let n=a.existsSync(L(e)),s=n?null:z(e);if(!n&&!s)return null;let o=s?`Waiting for capacity (position ${s.position}). End your turn now; it starts when another feature stops.`:G;switch(t.hook_event_name){case"PreToolUse":return{continue:!1,stopReason:o,hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:o}};case"UserPromptSubmit":return{decision:"block",reason:s?"This workspace is waiting for capacity; it starts on its own.":"This workspace is paused. Press \u25B6 Resume on its card first."};default:return null}}r(Z,"pauseGate");function tt(t,e=new Date){let n=w(String(t.cwd||process.cwd()));if(!n)return null;let s=j(n),o=q(s),i=B(t,o);i?.state==="working"&&o?.state==="needs-you"&&A(n,t.session_id)&&(i=null);let f=i!==null&&!(o&&o.state===i.state&&o.question===i.question&&o.via===i.via),u=I(s),c=t.session_id,x=process.env.CLAUDE_CODE_ENTRYPOINT?.startsWith("sdk")??!1,k=x&&u[c]&&!u[c].headless,O=x&&!k,S=typeof c=="string"&&c?C(t.hook_event_name):void 0,h=!1;if(S===null&&c in u&&!k?(delete u[c],h=!0):S&&(u[c]?.status!==S||!!u[c]?.headless!==O)&&(u[c]={status:S,at:e.toISOString(),...O&&{headless:!0}},h=!0),!f&&!h)return null;let m={...o};f&&i&&(m={state:i.state,at:e.toISOString()},i.question&&(m.question=i.question),i.via&&(m.via=i.via)),Object.keys(u).length&&(m.sessions=u);let P=R(n);return Q(P,m),P}r(tt,"record");function D(){try{let t=process.stdin.isTTY?"":a.readFileSync(0,"utf8"),e=t.trim()?JSON.parse(t):{};if(typeof e!="object"||e===null||Array.isArray(e))return;let n=w(String(e.cwd||process.cwd())),s=n?Z(e,n):null;if(s){console.log(JSON.stringify(s));return}n&&e.hook_event_name==="UserPromptSubmit"&&a.rmSync(M(n),{force:!0});let o=n?X(e,n):null;if(o&&console.log(JSON.stringify(o)),o&&e.hook_event_name==="Stop")return;tt(e)}catch{}}r(D,"main");D();
3
+ var $=Object.defineProperty;var i=(t,e)=>$(t,"name",{value:e,configurable:!0});import*as a from"node:fs";import*as d from"node:path";import*as N from"node:path";var E=".werkmeister";function f(t){return N.join(t,E)}i(f,"dotDir");var _="mcp__ccd_turn__wrap_up";import*as g from"node:fs";import*as p from"node:path";function w(t){let e=p.resolve(t);for(;;){if(g.existsSync(p.join(f(e),"workspace.yml")))return e;let n=p.dirname(e);if(n===e)return null;e=n}}i(w,"findRecordRoot");var D=new Set(["working","needs-you","done","idle"]),b=new Set(["AskUserQuestion","ExitPlanMode"]),U=new Set(["permission_prompt","elicitation_dialog"]),R=i(t=>d.join(f(t),"agent-state.json"),"stateFile");function j(t){try{let e=JSON.parse(a.readFileSync(R(t),"utf8"));return e&&typeof e=="object"&&!Array.isArray(e)?e:null}catch{return null}}i(j,"readRaw");function q(t){if(!D.has(t?.state)||typeof t?.at!="string")return null;let e={state:t.state,at:t.at};return typeof t.question=="string"&&(e.question=t.question),t.via==="wrap_up"&&(e.via="wrap_up"),e}i(q,"stateOf");function I(t){let e={};for(let[n,s]of Object.entries(t?.sessions??{}))(s?.status==="busy"||s?.status==="idle")&&typeof s.at=="string"&&(e[n]={status:s.status,at:s.at,...s.headless===!0&&{headless:!0}});return e}i(I,"sessionsOf");var W=new Set(["UserPromptSubmit","PreToolUse","PostToolUse"]);function C(t){return t==="SessionEnd"?null:t==="SessionStart"||t==="Stop"?"idle":W.has(t)?"busy":void 0}i(C,"nextSessionStatus");function F(t){return/\?[\s*_`'")\]]*$/.test(t)}i(F,"asksQuestion");function J(t){let e=t.trim().split(/\n\s*\n/);return e[e.length-1].trim().slice(0,200)}i(J,"questionOf");function K(t){let e=a.openSync(t,"r"),n;try{let o=a.fstatSync(e).size,r=Math.min(o,65536),l=Buffer.alloc(r);a.readSync(e,l,0,r,o-r),n=l.toString("utf8")}finally{a.closeSync(e)}let s=n.split(`
4
+ `);for(let o=s.length-1;o>=0;o--){let r;try{r=JSON.parse(s[o])}catch{continue}if(r.type!=="assistant")continue;let l=r.message?.content;if(typeof l=="string")return l;if(!Array.isArray(l))continue;let u=l.filter(c=>c?.type==="text"&&typeof c.text=="string");if(u.length)return u.map(c=>c.text).join(`
5
+ `)}return null}i(K,"lastAssistantText");function M(t,e){switch(t.hook_event_name){case"UserPromptSubmit":return{state:"working"};case"PostToolUse":return t.tool_name===_&&e?.via==="wrap_up"?null:{state:"working"};case"PreToolUse":{let n=String(t.tool_name??""),s=t.tool_input?.ask;if(n===_&&typeof s=="string"&&s.trim())return{state:"needs-you",question:s.trim(),via:"wrap_up"};if(!b.has(n))return{state:"working"};let o=t.tool_input?.questions?.[0]?.question;return{state:"needs-you",question:typeof o=="string"?o:"Approve the plan?"}}case"Notification":{let n=t.notification_type,s=String(t.message??"");return!(typeof n=="string"?U.has(n):!/waiting for your input/i.test(s))||e?.state==="needs-you"&&e.question?null:{state:"needs-you",question:s||void 0}}case"PermissionRequest":{let n=t.tool_input?.description;return{state:"needs-you",question:typeof n=="string"&&n.trim()?n.trim():`Approve ${String(t.tool_name??"the tool call")}?`}}case"Stop":{let n=null;return typeof t.last_assistant_message=="string"?n=t.last_assistant_message:typeof t.transcript_path=="string"&&(n=K(t.transcript_path)),n&&F(n)?{state:"needs-you",question:J(n)}:e?.via==="wrap_up"?null:{state:"done"}}case"SessionEnd":return e?.state==="working"?{state:"idle"}:null;default:return null}}i(M,"nextState");var Y="Workspace paused by the person from its card. End your turn now; it continues when they press Resume.";var B=i(t=>d.join(f(t),"paused.json"),"pausedPath"),G=i(t=>d.join(f(t),"resume-nudge.json"),"nudgePath"),H=i(t=>d.join(f(t),"waiting.json"),"waitingPath");function L(t,e){let n=`${t}.${process.pid}.tmp`;a.writeFileSync(n,`${JSON.stringify(e)}
6
+ `),a.renameSync(n,t)}i(L,"writeAtomic");function Q(t){try{let e=JSON.parse(a.readFileSync(H(t),"utf8"));return Number.isInteger(e?.position)?{position:e.position,reason:String(e.reason??"")}:null}catch{return null}}i(Q,"readWaiting");var T=i((t,e)=>d.join(f(t),`wake-nudge-${e}.json`),"wakeNudgePath"),z=/^[\w-]+$/;function V(t){try{let e=JSON.parse(a.readFileSync(t,"utf8"))?.message;return typeof e=="string"?e:null}catch{return null}}i(V,"readWakeNudge");var A=i((t,e)=>typeof e=="string"&&z.test(e)&&a.existsSync(T(t,e)),"pendingWake");function X(t,e){let n=t.hook_event_name;if(n!=="UserPromptSubmit"&&n!=="PostToolUse"&&n!=="Stop"||t.agent_id||!A(e,t.session_id))return null;let s=T(e,t.session_id),o=`${s}.${process.pid}-${Date.now()}.claimed`;try{a.renameSync(s,o)}catch{return null}let r=V(o);return a.rmSync(o,{force:!0}),r===null?null:n==="Stop"?{decision:"block",reason:r}:{hookSpecificOutput:{hookEventName:n,additionalContext:r}}}i(X,"deliverWake");function Z(t,e){let n=a.existsSync(B(e)),s=n?null:Q(e);if(!n&&!s)return null;let o=s?`Waiting for capacity (position ${s.position}). End your turn now; it starts when another feature stops.`:Y;switch(t.hook_event_name){case"PreToolUse":{let r={hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:o}};return process.env.WERKMEISTER_HOOK_HARNESS==="codex"?r:{continue:!1,stopReason:o,...r}}case"UserPromptSubmit":return{decision:"block",reason:s?"This workspace is waiting for capacity; it starts on its own.":"This workspace is paused. Press \u25B6 Resume on its card first."};default:return null}}i(Z,"pauseGate");function tt(t,e=new Date){let n=w(String(t.cwd||process.cwd()));if(!n)return null;let s=j(n),o=q(s),r=M(t,o);r?.state==="working"&&o?.state==="needs-you"&&A(n,t.session_id)&&(r=null);let l=r!==null&&!(o&&o.state===r.state&&o.question===r.question&&o.via===r.via),u=I(s),c=t.session_id,x=process.env.CLAUDE_CODE_ENTRYPOINT?.startsWith("sdk")??!1,k=x&&u[c]&&!u[c].headless,O=x&&!k,S=typeof c=="string"&&c?C(t.hook_event_name):void 0,h=!1;if(S===null&&c in u&&!k?(delete u[c],h=!0):S&&(u[c]?.status!==S||!!u[c]?.headless!==O)&&(u[c]={status:S,at:e.toISOString(),...O&&{headless:!0}},h=!0),!l&&!h)return null;let m={...o};l&&r&&(m={state:r.state,at:e.toISOString()},r.question&&(m.question=r.question),r.via&&(m.via=r.via)),Object.keys(u).length&&(m.sessions=u);let P=R(n);return L(P,m),P}i(tt,"record");function v(){try{let t=process.stdin.isTTY?"":a.readFileSync(0,"utf8"),e=t.trim()?JSON.parse(t):{};if(typeof e!="object"||e===null||Array.isArray(e))return;let n=w(String(e.cwd||process.cwd())),s=n?Z(e,n):null;if(s){console.log(JSON.stringify(s));return}n&&e.hook_event_name==="UserPromptSubmit"&&a.rmSync(G(n),{force:!0});let o=n?X(e,n):null;if(o&&console.log(JSON.stringify(o)),o&&e.hook_event_name==="Stop")return;tt(e)}catch{}}i(v,"main");v();
7
7
  //# sourceMappingURL=agent_state.js.map
package/dist/lib/gate.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createRequire as __sandboxCreateRequire } from "node:module";
2
2
  const require = __sandboxCreateRequire(import.meta.url);
3
- var O=Object.defineProperty;var a=(t,e)=>O(t,"name",{value:e,configurable:!0});import*as u from"node:fs";import*as i from"node:path";import*as $ from"node:path";var k=".werkmeister";function w(t){return $.join(t,k)}a(w,"dotDir");var A="Precedence: these standards' rules win over conflicting task framing \u2014 if a rule and the task clash, follow the rule and say so (invariant: apply it and write a `decision` note; rule: surface the conflict in one line, then apply). This gate fires once; your retry will proceed.",R="Standards gate: invoke `w9r-implement` before coding \u2014 it loads the universal base (ponytail + comments + engineering) and the workspace notes. Then retry this edit.";function F(t){let e="",n=0,r=t.length;for(;n<r;){let c=t[n];if(n+=1,c==="*")e+=".*";else if(c==="?")e+=".";else if(c==="["){let s=n;for(s<r&&t[s]==="!"&&(s+=1),s<r&&t[s]==="]"&&(s+=1);s<r&&t[s]!=="]";)s+=1;if(s>=r)e+="\\[";else{let o=t.slice(n,s).replace(/\\/g,"\\\\");n=s+1,o.startsWith("!")?o="^"+o.slice(1):o.startsWith("^")&&(o="\\"+o),e+=`[${o}]`}}else e+=c.replace(/[.+^${}()|[\]\\]/g,"\\$&")}return`^(?:${e})$`}a(F,"_translate");function W(t,e){return new RegExp(F(e),"s").test(t)}a(W,"fnmatch");function J(t,e){let n=T(t);for(let r of e){let c=T(r),s=i.relative(c,n);if(!(s===""||s===".."||s.startsWith(".."+i.sep)||i.isAbsolute(s)))return i.join(c,s.split(i.sep)[0])}return null}a(J,"workspaceUnder");function T(t){try{return u.realpathSync(t)}catch{return i.resolve(t)}}a(T,"_realpath");function C(t,e){if(e){let r=J(t,e);return r!==null&&x(w(r))?r:null}let n=i.resolve(t);for(;;){if(x(w(n)))return n;let r=i.dirname(n);if(r===n)return null;n=r}}a(C,"findWorkspaceRoot");function x(t){try{return u.statSync(t).isDirectory()}catch{return!1}}a(x,"_isDir");function N(t){try{return u.statSync(t).isFile()}catch{return!1}}a(N,"_isFile");function d(t){return typeof t=="object"&&t!==null&&!Array.isArray(t)}a(d,"_isDict");function G(t){let e=i.join(w(t),"standards.json");if(!N(e))return null;let n;try{n=JSON.parse(u.readFileSync(e,"utf8"))}catch{return null}return d(n)?n:null}a(G,"loadManifest");function b(t,e){return i.join(w(t),".gate",`${e}.json`)}a(b,"_statePath");function L(t,e){let n=b(t,e);if(!N(n))return{};let r;try{r=JSON.parse(u.readFileSync(n,"utf8"))}catch{return null}return d(r)?r:null}a(L,"_loadState");function U(t,e,n){try{let r=b(t,e);return u.mkdirSync(i.dirname(r),{recursive:!0}),u.writeFileSync(r,JSON.stringify(n)),!0}catch{return!1}}a(U,"_saveState");function E(t){return[...t].sort()}a(E,"_sorted");function X(t){let e=/^---\r?\n([\s\S]*?)\r?\n---/.exec(t);if(!e)return;let n=/^status:\s*["']?([^\s"'#]+)/m.exec(e[1]);return n?n[1].toLowerCase():null}a(X,"_noteStatus");function Y(t,e){let n=String(e.note??"");if(!/^[a-z]+$/.test(n))return!0;let r=e.status?String(e.status).toLowerCase():null,c=i.join(w(t),"notes"),s;try{s=u.readdirSync(c)}catch(l){return l.code!=="ENOENT"}let o=new RegExp(`^\\d{3}-${n}-.+\\.md$`);for(let l of s){if(!o.test(l))continue;let p;try{p=u.readFileSync(i.join(c,l),"utf8")}catch{return!0}let h=X(p);if(h===void 0)return!0;if(!(h==="superseded"||h==="graduated")&&(r===null||h===r))return!0}return!1}a(Y,"_phaseGateSatisfied");function z(t){let e=String(t.note);if(t.status){let n=String(t.status);return`Phase gate: no \`${e}\` note in \`.werkmeister/notes/\` has \`status: ${n}\`, so source edits wait for an approved design. Invoke \`w9r-research\`, present the design to the user, and when they approve write \`NNN-${e}-<slug>.md\` with \`status: ${n}\` recording what was approved. Then retry this edit. Edits under \`.werkmeister/\` are not gated.`}return`Phase gate: no \`${e}\` note in \`.werkmeister/notes/\` names the root cause yet. Invoke \`w9r-research\`, reproduce the problem, and write \`NNN-${e}-<slug>.md\` stating the root cause. Then retry this edit. Edits under \`.werkmeister/\` are not gated.`}a(z,"_phaseGateText");function H(t,e,n){if(!e||e.includes("/")||e.includes(".."))return null;let r=C(t);if(r===null)return null;let c=G(r);if(c===null)return null;let s=L(r,e);if(s===null)return null;let o=n?i.relative(r,n):"",l=c.phase_gate;if(d(l)&&o&&o!==".."&&!o.startsWith(".."+i.sep)&&!i.isAbsolute(o)&&!o.startsWith(k+i.sep)&&!Y(r,l))return z(l);let p=new Set(Array.isArray(s.delivered)?s.delivered:[]),h=!s.phase_entry_delivered,f=null,g=d(c.domains)?c.domains:{};for(let m of E(Object.keys(g))){let y=g[m];if(p.has(m)||!d(y))continue;if((Array.isArray(y.applies_to)?y.applies_to:[]).some(P=>W(o,String(P)))){f=m;break}}if(!h&&f===null)return null;let S=[];if(h&&S.push(R),f!==null){let m=g[f],y=String(d(m)&&m.summary||"").trim(),_=`Standards gate: you are editing a path governed by the \`${f}\` standard, which has not been loaded this session. Invoke the \`w9r-${f}-standard\` skill, then retry this edit.`;y&&(_+=` Summary: ${y}.`),S.push(_)}S.push(A);let D={phase_entry_delivered:!0,delivered:E(f?new Set([...p,f]):p)};return U(r,e,D)?S.join(`
4
- `):null}a(H,"decide");function j(){let t;try{let o=process.stdin.isTTY?"":u.readFileSync(0,"utf8"),l=o.trim()?JSON.parse(o):{};t=d(l)?l:{}}catch{t={}}let e=String(t.session_id||""),n=t.tool_input,r=String(d(n)&&n.file_path||""),c=String(t.cwd||process.cwd()),s;try{s=H(c,e,r)}catch{s=null}s&&console.log(JSON.stringify({hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:s}}))}a(j,"main");j();
3
+ var j=Object.defineProperty;var a=(t,e)=>j(t,"name",{value:e,configurable:!0});import*as u from"node:fs";import*as i from"node:path";import*as T from"node:path";var k=".werkmeister";function w(t){return T.join(t,k)}a(w,"dotDir");var F="Precedence: these standards' rules win over conflicting task framing \u2014 if a rule and the task clash, follow the rule and say so (invariant: apply it and write a `decision` note; rule: surface the conflict in one line, then apply). This gate fires once; your retry will proceed.",O="Standards gate: invoke `w9r-implement` before coding \u2014 it loads the universal base (ponytail + comments + engineering) and the workspace notes. Then retry this edit.";function R(t){let e="",n=0,r=t.length;for(;n<r;){let o=t[n];if(n+=1,o==="*")e+=".*";else if(o==="?")e+=".";else if(o==="["){let s=n;for(s<r&&t[s]==="!"&&(s+=1),s<r&&t[s]==="]"&&(s+=1);s<r&&t[s]!=="]";)s+=1;if(s>=r)e+="\\[";else{let c=t.slice(n,s).replace(/\\/g,"\\\\");n=s+1,c.startsWith("!")?c="^"+c.slice(1):c.startsWith("^")&&(c="\\"+c),e+=`[${c}]`}}else e+=o.replace(/[.+^${}()|[\]\\]/g,"\\$&")}return`^(?:${e})$`}a(R,"_translate");function W(t,e){return new RegExp(R(e),"s").test(t)}a(W,"fnmatch");function C(t,e){let n=$(t);for(let r of e){let o=$(r),s=i.relative(o,n);if(!(s===""||s===".."||s.startsWith(".."+i.sep)||i.isAbsolute(s)))return i.join(o,s.split(i.sep)[0])}return null}a(C,"workspaceUnder");function $(t){try{return u.realpathSync(t)}catch{return i.resolve(t)}}a($,"_realpath");function J(t,e){if(e){let r=C(t,e);return r!==null&&x(w(r))?r:null}let n=i.resolve(t);for(;;){if(x(w(n)))return n;let r=i.dirname(n);if(r===n)return null;n=r}}a(J,"findWorkspaceRoot");function x(t){try{return u.statSync(t).isDirectory()}catch{return!1}}a(x,"_isDir");function N(t){try{return u.statSync(t).isFile()}catch{return!1}}a(N,"_isFile");function p(t){return typeof t=="object"&&t!==null&&!Array.isArray(t)}a(p,"_isDict");function G(t){let e=i.join(w(t),"standards.json");if(!N(e))return null;let n;try{n=JSON.parse(u.readFileSync(e,"utf8"))}catch{return null}return p(n)?n:null}a(G,"loadManifest");function P(t,e){return i.join(w(t),".gate",`${e}.json`)}a(P,"_statePath");function H(t,e){let n=P(t,e);if(!N(n))return{};let r;try{r=JSON.parse(u.readFileSync(n,"utf8"))}catch{return null}return p(r)?r:null}a(H,"_loadState");function U(t,e,n){try{let r=P(t,e);return u.mkdirSync(i.dirname(r),{recursive:!0}),u.writeFileSync(r,JSON.stringify(n)),!0}catch{return!1}}a(U,"_saveState");function E(t){return[...t].sort()}a(E,"_sorted");function L(t){let e=/^---\r?\n([\s\S]*?)\r?\n---/.exec(t);if(!e)return;let n=/^status:\s*["']?([^\s"'#]+)/m.exec(e[1]);return n?n[1].toLowerCase():null}a(L,"_noteStatus");function M(t,e){let n=String(e.note??"");if(!/^[a-z]+$/.test(n))return!0;let r=e.status?String(e.status).toLowerCase():null,o=i.join(w(t),"notes"),s;try{s=u.readdirSync(o)}catch(l){return l.code!=="ENOENT"}let c=new RegExp(`^\\d{3}-${n}-.+\\.md$`);for(let l of s){if(!c.test(l))continue;let f;try{f=u.readFileSync(i.join(o,l),"utf8")}catch{return!0}let d=L(f);if(d===void 0)return!0;if(!(d==="superseded"||d==="graduated")&&(r===null||d===r))return!0}return!1}a(M,"_phaseGateSatisfied");function X(t){let e=String(t.note);if(t.status){let n=String(t.status);return`Phase gate: no \`${e}\` note in \`.werkmeister/notes/\` has \`status: ${n}\`, so source edits wait for an approved design. Invoke \`w9r-research\`, present the design to the user, and when they approve write \`NNN-${e}-<slug>.md\` with \`status: ${n}\` recording what was approved. Then retry this edit. Edits under \`.werkmeister/\` are not gated.`}return`Phase gate: no \`${e}\` note in \`.werkmeister/notes/\` names the root cause yet. Invoke \`w9r-research\`, reproduce the problem, and write \`NNN-${e}-<slug>.md\` stating the root cause. Then retry this edit. Edits under \`.werkmeister/\` are not gated.`}a(X,"_phaseGateText");function Y(t,e,n){if(!e||e.includes("/")||e.includes(".."))return null;let r=J(t);if(r===null)return null;let o=G(r);if(o===null)return null;let s=H(r,e);if(s===null)return null;let c=n?i.relative(r,n):"",l=o.phase_gate;if(p(l)&&c&&c!==".."&&!c.startsWith(".."+i.sep)&&!i.isAbsolute(c)&&!c.startsWith(k+i.sep)&&!M(r,l))return X(l);let f=new Set(Array.isArray(s.delivered)?s.delivered:[]),d=!s.phase_entry_delivered,h=null,S=p(o.domains)?o.domains:{};for(let m of E(Object.keys(S))){let y=S[m];if(f.has(m)||!p(y))continue;if((Array.isArray(y.applies_to)?y.applies_to:[]).some(D=>W(c,String(D)))){h=m;break}}if(!d&&h===null)return null;let g=[];if(d&&g.push(O),h!==null){let m=S[h],y=String(p(m)&&m.summary||"").trim(),_=`Standards gate: you are editing a path governed by the \`${h}\` standard, which has not been loaded this session. Invoke the \`w9r-${h}-standard\` skill, then retry this edit.`;y&&(_+=` Summary: ${y}.`),g.push(_)}g.push(F);let A={phase_entry_delivered:!0,delivered:E(h?new Set([...f,h]):f)};return U(r,e,A)?g.join(`
4
+ `):null}a(Y,"decide");var z=/^\*\*\* (?:Add File|Update File|Delete File|Move to): (.+)$/gm;function I(t){return[...new Set([...t.matchAll(z)].map(e=>e[1].trim()))]}a(I,"patchPaths");function b(){let t;try{let f=process.stdin.isTTY?"":u.readFileSync(0,"utf8"),d=f.trim()?JSON.parse(f):{};t=p(d)?d:{}}catch{t={}}let e=String(t.session_id||""),n=t.tool_input,r=p(n)?n:{},o=String(t.cwd||process.cwd()),s=t.tool_name==="apply_patch"?r.command:void 0,c=typeof s=="string"?I(s).map(f=>i.resolve(o,f)):[String(r.file_path||"")],l=null;try{for(let f of c)if(l=Y(o,e,f),l)break}catch{l=null}l&&console.log(JSON.stringify({hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:l}}))}a(b,"main");b();
5
5
  //# sourceMappingURL=gate.js.map