@maestria/pi 0.7.1 → 0.7.3
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/extension.mjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import{homedir as e}from"node:os";import{dirname as t,join as n,resolve as r}from"node:path";import{fileURLToPath as i}from"node:url";import{existsSync as a,mkdirSync as o,readFileSync as s,writeFileSync as c}from"node:fs";import{Type as l}from"typebox";import{Data as u,Effect as d}from"effect";import{SUBAGENT_EVENTS as f}from"@gotgenes/pi-subagents";import{isToolCallEventType as p}from"@earendil-works/pi-coding-agent";function m(){return{mode:null,activeTask:``,completionPromise:``,specialistsDelegated:[],blockers:[],filesModified:[],filesRead:[],handoffHistory:[],reviewMode:!1,originalModel:null,originalTools:null,subagentStatus:{},reviewModel:null,nativeGoal:null}}function h(e,t,n){return[t,...e.filter(e=>e!==t)].slice(0,n)}function g(e,t,n,r){let i=[{from:t,to:n,task:r,timestamp:Date.now()},...e.handoffHistory].slice(0,5);return{...e,handoffHistory:i}}function ee(e,t){return{...e,filesModified:h(e.filesModified,t,10)}}function te(e,t){return{...e,filesRead:h(e.filesRead,t,10)}}function _(e,t){return e.specialistsDelegated.includes(t)?e:{...e,specialistsDelegated:[...e.specialistsDelegated,t]}}function ne(e){return{state:{...e,reviewMode:!1,originalModel:null,originalTools:null},originalModel:e.originalModel,originalTools:e.originalTools}}function v(e,t){e.appendEntry(`maestria_state`,{...t})}function y(e){let t=[];if(e.mode&&t.push(`**Mode:** ${e.mode.toUpperCase()}`),e.reviewModel&&t.push(`**Review Model:** ${e.reviewModel}`),e.activeTask&&t.push(`**Goal:** ${e.activeTask}`),e.nativeGoal&&t.push(`**Native Goal:** ${e.nativeGoal.objective} (${e.nativeGoal.status})`),e.completionPromise&&t.push(`**Completion Promise:** ${e.completionPromise}`),e.specialistsDelegated.length>0&&t.push(`**Specialists Delegated:** ${e.specialistsDelegated.join(`, `)}`),e.blockers.length>0){t.push(`**Blockers:**`);for(let n of e.blockers)t.push(`- ${n}`)}let n=[];if(e.filesModified.length>0&&n.push(`**Modified:** ${e.filesModified.join(`, `)}`),e.filesRead.length>0&&n.push(`**Read:** ${e.filesRead.join(`, `)}`),n.length>0&&t.push(`**Files:** ${n.join(`; `)}`),e.handoffHistory.length>0){t.push(`**Recent Handoffs:**`);for(let n of e.handoffHistory)t.push(`- ${n.from} → ${n.to}: ${n.task}`)}return t.join(`
|
|
2
2
|
|
|
3
|
-
`)}async function b(e,t,n){let{state:r,originalModel:i,originalTools:a}=ne(n);if(a&&a.length>0&&await e.setActiveTools(a),i)try{let n=t.modelRegistry.getAll().find(e=>e.id===i);n&&await e.setModel(n)}catch{}Object.assign(n,r)}async function
|
|
4
|
-
`:
|
|
5
|
-
`}function
|
|
6
|
-
`)}}}function
|
|
3
|
+
`)}async function b(e,t,n){let{state:r,originalModel:i,originalTools:a}=ne(n);if(a&&a.length>0&&await e.setActiveTools(a),i)try{let n=t.modelRegistry.getAll().find(e=>e.id===i);n&&await e.setModel(n)}catch{}Object.assign(n,r)}async function x(e,t,n){let r=n.reviewModel;if(!r)return null;try{let n=t.modelRegistry.getAll().find(e=>e.id===r);return n?(await e.setModel(n),r):(t.ui.notify(`Review model "${r}" not found in registry, staying on current.`),null)}catch{return t.ui.notify(`Could not switch to review model "${r}", staying on current.`),null}}async function S(e,t,n){await b(e,t,n)}const C={REVIEW_ACTIVATED:`maestria:review:activated`,REVIEW_DEACTIVATED:`maestria:review:deactivated`,SUBAGENT_STARTED:`maestria:subagent:started`,SUBAGENT_COMPLETED:`maestria:subagent:completed`,SUBAGENT_FAILED:`maestria:subagent:failed`},w=[`adventurer`,`architect`,`builder`,`diagnose`,`planner`,`reviewer`,`writer`];function T(e){if(!w.includes(e))throw Error(`Unknown agent: "${e}". Allowed: ${w.join(`, `)}`)}function E(e,t){if(!e||!e.trim())throw Error(t)}function re(e,t){if(!a(e))return console.warn(`[maestria] Agents source directory not found:`,e),0;try{o(t,{recursive:!0})}catch{return console.warn(`[maestria] Could not create agents directory:`,t),0}let r=0;for(let i of w){let o=n(e,`${i}.md`),l=n(t,`${i}.md`);if(!a(o)){console.warn(`[maestria] Agent source not found: ${i}.md`);continue}if(!a(l))try{let e=s(o,`utf-8`);c(l,e,`utf-8`),r++}catch(e){console.warn(`[maestria] Failed to deploy agent ${i}:`,e)}}return r>0&&console.log(`[maestria] Deployed ${r} specialist agents to ${t}`),r}const ie=i(import.meta.url),ae=t(ie),oe=n(ae,`..`,`agents`);function se(t){re(oe,n(e(),`.pi`,`agent`,`agents`))}const D=[`fein`,`sonar`,`blitz`],ce={fein:`[MODE: fein]`,sonar:`[MODE: sonar]`,blitz:`[MODE: blitz]`},O={fein:3,sonar:2,blitz:1},k=/```[\s\S]*?```|`[^`]*`/g;function le(e){let t=[],n;for(k.lastIndex=0;(n=k.exec(e))!==null;)t.push([n.index,n.index+n[0].length]);return t}function ue(e,t){return t.some(([t,n])=>e>=t&&e<n)}function A(e){let t=e.indexOf(`## MODE:`);return t===-1?e.replace(/\s+$/,``)+`
|
|
4
|
+
`:e.slice(t).replace(/\s+$/,``)+`
|
|
5
|
+
`}function j(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}function M(e){return RegExp(`\\b${j(e)}\\b`,`gi`)}function N(e,t){if(!e)return null;let n=le(e),r=t?new Set(Array.from(t).map(e=>e.toLowerCase())):void 0,i=null;for(let t of D){if(r?.has(t))continue;let a=M(t),o;for(;(o=a.exec(e))!==null;)ue(o.index,n)||(i===null||O[t]>O[i.mode])&&(i={mode:t,keyword:o[0],index:o.index})}return i}function P(e,t){return(e.slice(0,t.index)+e.slice(t.index+t.keyword.length).replace(/^:\s*/,``)).replace(/ {2,}/g,` `).trim()}const F=D,I=ce,L={};function R(e,t){return A(s(r(t,`${e}.md`),`utf-8`))}function z(e,t){if(!(e in L))try{L[e]=R(e,t)}catch(t){console.warn(`[maestria] Failed to load mode prompt "${e}":`,t),L[e]=``}return`${I[e]}\n\n${L[e]}`}function B(e,t,n){if(!e)return null;let r=N(e,n);if(r===null)return null;let i=P(e,r);return{keyword:r.mode,strippedText:i,prompt:z(r.mode,t)}}function V(e,t){return t?`${e}\n\n${t}`:e}function H(e,t,n,r){e(async(e,i)=>{let a=B(e.text??``,n);return a?(t.reviewMode&&await r.restoreOriginalState(i),t.mode=a.keyword,r.persistState(),r.transform(V(a.prompt,a.strippedText))):r.noMatch})}function U(e,t,n){e(`mode-clear`,{description:`Clear workflow mode and return to neutral routing`,handler:async(e,r)=>{t.reviewMode&&await n.restoreOriginalState(r),t.mode=null,n.persistState(),r.ui.notify(`Workflow mode cleared. Neutral routing is active.`)}});for(let r of F)e(r,{description:`Set workflow mode to ${r}`,handler:async(e,i)=>{t.reviewMode&&await n.restoreOriginalState(i),t.mode=r,n.persistState(),i.ui.notify(`Mode set to ${r}. Describe what you'd like to work on.`)}})}const de=t(i(import.meta.url)),fe=r(de,`../agents/commands`);function pe(e,t){H(t=>e.on(`input`,t),t,fe,{restoreOriginalState:n=>S(e,n,t),persistState:()=>v(e,t),noMatch:{action:`continue`},transform:e=>({action:`transform`,text:e})})}function me(e,t){U((t,n)=>e.registerCommand(t,n),t,{restoreOriginalState:n=>S(e,n,t),persistState:()=>v(e,t)})}const he=t(i(import.meta.url)),ge=r(he,`../agents/commands`);function _e(e){return(t,n)=>{if(e.mode)return{systemPrompt:[t.systemPrompt,``,z(e.mode,ge),``,`The user has set workflow mode to "${e.mode}". Honor this mode throughout the session until changed via /command.`].join(`
|
|
6
|
+
`)}}}function ve(e,t){e.on(`session_before_compact`,e=>{let n=e.preparation;return{compaction:{summary:y(t),details:{...t},firstKeptEntryId:n?.firstKeptEntryId,tokensBefore:n?.tokensBefore}}}),e.on(`session_before_tree`,e=>{if(e.preparation?.userWantsSummary)return{summary:{summary:y(t)}}})}function ye(e,t){ve({on:(t,n)=>{e.on(t,n)}},t)}const W=new Set([`completed`,`steered`,`aborted`,`stopped`,`error`]);var G=class extends u.TaggedError(`SubagentPollError`){};function be(e){let t=e.intervalMs??500,n=e.timeoutMs??18e4,r=Math.ceil(n/t);return d.gen(function*(){let i=0,a=yield*d.sync(()=>e.service.getRecord(e.id));for(;a&&!W.has(a.status)&&i<r;)yield*d.sleep(t),a=yield*d.sync(()=>e.service.getRecord(e.id)),i++,e.sendUpdates&&(yield*d.sync(()=>{e.onUpdate?.({content:[{type:`text`,text:`${e.label} running... (${Math.round(i*t/1e3)}s)`}]})}));return a&&!W.has(a.status)?yield*d.fail(new G({id:e.id,reason:`timeout`,message:`Subagent ${e.id} timed out after ${n}ms`})):a||(yield*d.fail(new G({id:e.id,reason:`missing`,message:`Subagent ${e.id} was cleaned up before completion`})))})}function xe(e,t){return d.callback(n=>{let r=()=>{n(d.fail(new G({id:e,reason:`aborted`,message:`Maestria subagent call aborted`})))};if(t.aborted){r();return}return t.addEventListener(`abort`,r,{once:!0}),d.sync(()=>t.removeEventListener(`abort`,r))})}function K(e){let t=be(e);return e.signal?d.raceFirst(t,xe(e.id,e.signal)):t}const q=w,J=18e4;function Y(e,t){if(typeof e.abort==`function`)for(let n of t)try{e.abort(n)}catch{}}function X(e){return d.tapError(K(e),()=>d.sync(()=>Y(e.service,[e.id])))}function Z(e,t,n,r){let i=_(g(t,`orchestrator`,n,r),n);Object.assign(t,i),v(e,t)}function Se(e,t,n){if(e.registerTool({name:`maestria_subagent`,label:`Maestria Subagent`,description:`Dispatch a task to a @maestria specialist subagent`,promptSnippet:`Delegate tasks to @maestria specialist subagents (adventurer, architect, builder, planner, diagnose, reviewer, writer)`,promptGuidelines:[`Use maestria_subagent when a task MUST be delegated to a specialist subagent rather than handled directly. Each specialist has focused capabilities: adventurer (recon), architect (design), builder (impl), planner (planning), diagnose (bugs), reviewer (QA), writer (docs).`],prepareArguments(e){return e},parameters:l.Object({agent:l.String({description:`Specialist agent name (required): adventurer, architect, builder, diagnose, planner, reviewer, writer`}),task:l.String({description:`Task description for the subagent (required)`}),tasks:l.Optional(l.Array(l.Object({agent:l.String(),task:l.String()}),{description:`Array of task objects for parallel or chain dispatch`})),mode:l.Optional(l.Union([l.Literal(`parallel`),l.Literal(`chain`),l.Literal(`single`)]))}),async execute(n,r,i,a,o){if(t.reviewMode)return{content:[{type:`text`,text:`Subagent dispatch is not available during review mode. Use /restore-model to exit review mode first.`}]};let s=r.mode??`single`;if(s===`single`){if(!r.agent||!q.includes(r.agent))return{content:[{type:`text`,text:`Invalid maestria_subagent call: 'agent' is required and must be one of ${q.join(`, `)}. Re-dispatch with a valid agent name; the orchestrator may continue read-only exploration while the brief is corrected.`}]};E(r.task,`Task description is required`)}else if(s===`parallel`){if(!r.tasks||r.tasks.length<2)throw Error(`For parallel mode, tasks array is required with at least 2 items`);if(r.tasks.length>8)throw Error(`For parallel mode, tasks array may have at most 8 items (got ${r.tasks.length})`);for(let e of r.tasks)T(e.agent),E(e.task,`Task description is required for all tasks`)}else if(s===`chain`){if(!r.tasks||r.tasks.length<2)throw Error(`For chain mode, tasks array is required with at least 2 items`);for(let e of r.tasks)T(e.agent),E(e.task,`Task description is required for all tasks`)}let{getSubagentsService:c}=await import(`@gotgenes/pi-subagents`),l=c();if(!l||typeof l.spawn!=`function`)return{content:[{type:`text`,text:[`## Subagent Dispatch Unavailable`,``,"The `@gotgenes/pi-subagents` extension is required for subagent dispatch but has not been loaded.",``,`Install it as a Pi extension:`,``,"```",`pi install npm:@gotgenes/pi-subagents`,"```",``,`Then restart your Pi session.`].join(`
|
|
7
7
|
`)}]};try{if(s===`single`){let n=r.agent,o=r.task,s=l.spawn(n,o,{description:o.slice(0,80),foreground:!0,inheritContext:!0});Z(e,t,n,o);let c=await d.runPromise(X({id:s,label:`Subagent ${n}`,sendUpdates:!0,service:l,signal:i,onUpdate:a,intervalMs:500,timeoutMs:J}));return{content:[{type:`text`,text:c.result??c.error??`No output.`}],details:{subagentId:s}}}if(s===`parallel`){let n=r.tasks;a?.({content:[{type:`text`,text:`Spawning ${n.length} parallel subagents...`}]});let o=[];try{for(let r of n){let n=l.spawn(r.agent,r.task,{description:r.task.slice(0,80),foreground:!0,inheritContext:!0});o.push(n),Z(e,t,r.agent,r.task)}}catch(e){throw Y(l,o),e}let s=await d.runPromise(d.all(o.map((e,t)=>d.match(K({id:e,label:`${n[t].agent} (${t+1}/${n.length})`,sendUpdates:!1,service:l,signal:i,onUpdate:a,intervalMs:500,timeoutMs:J}),{onSuccess:e=>({record:e}),onFailure:e=>(Y(l,o),{error:e})})),{concurrency:`unbounded`}));a?.({content:[{type:`text`,text:`All ${n.length} parallel subagents settled.`}]});let c=[`## Parallel Results (${n.length} tasks)\n`];for(let e=0;e<n.length;e++){let t=n[e],r=s[e],i=`### ${e+1}: ${t.agent}`;if(`error`in r){let e=r.error instanceof Error?r.error.message:String(r.error);c.push(i),c.push(`⚠️ ${e}`)}else{let e=r.record.result??r.record.error??`No output.`;c.push(i),c.push(e)}}return{content:[{type:`text`,text:c.join(`
|
|
8
8
|
|
|
9
9
|
`)}],details:{subagentIds:o}}}if(s===`chain`){let n=r.tasks,o=``;for(let r=0;r<n.length;r++){let s=n[r],c=s.task;r>0&&c.includes(`{previous}`)&&(c=c.replace(/\{previous\}/g,()=>o));let u=l.spawn(s.agent,c,{description:c.slice(0,80),foreground:!0,inheritContext:!0});Z(e,t,s.agent,c),a?.({content:[{type:`text`,text:`Chain step ${r+1}/${n.length}: ${s.agent} running...`}]});try{let e=await d.runPromise(X({id:u,label:`Chain step ${r+1}: ${s.agent}`,sendUpdates:!0,service:l,signal:i,onUpdate:a,intervalMs:500,timeoutMs:J}));o=e.result??e.error??`No output.`}catch(e){o=`[error] ${e instanceof Error?e.message:String(e)}`;break}r<n.length-1&&a?.({content:[{type:`text`,text:`Chain step ${r+1}/${n.length}: ${s.agent} completed. Moving to next step.`}]})}return{content:[{type:`text`,text:o}],details:{subagentId:`chain-completed`}}}throw Error(`Unknown dispatch mode`)}catch(e){console.warn(`[maestria] Subagent dispatch failed:`,e);let t=r.agent??r.tasks?.[0]?.agent??`unknown`,n=r.task??r.tasks?.map(e=>e.task).join(`; `)??`unknown`;return{content:[{type:`text`,text:[`## Subagent Handoff Required`,``,`**From:** orchestrator`,`**To:** ${t}`,`**Task:** ${n}`,``,`Subagent dispatch failed. Please delegate this work manually.`].join(`
|
|
10
|
-
`)}]}}}}),e.events){let r=e.events.on(f.STARTED,n=>{let{id:r,type:i}=n;t.subagentStatus[r]={type:i,status:`running`,startedAt:Date.now()},v(e,t),e.events?.emit(
|
|
11
|
-
`),{deliverAs:`steer`})}}),e.registerCommand(`restore-model`,{description:`Restore the original model and tools that were active before review mode was entered.`,handler:async(n,r)=>{if(!t.reviewMode){r.ui.notify(`Not in review mode. Nothing to restore.`);return}let i=t.originalModel;await b(e,r,t),v(e,t),r.ui.notify(`Restored original model and tools.`),e.events?.emit(
|
|
10
|
+
`)}]}}}}),e.events){let r=e.events.on(f.STARTED,n=>{let{id:r,type:i}=n;t.subagentStatus[r]={type:i,status:`running`,startedAt:Date.now()},v(e,t),e.events?.emit(C.SUBAGENT_STARTED,{id:r,type:i,timestamp:Date.now()})}),i=e.events.on(f.COMPLETED,n=>{let{id:r}=n,i=t.subagentStatus[r];i&&(i.status=`completed`,i.completedAt=Date.now()),v(e,t),e.events?.emit(C.SUBAGENT_COMPLETED,{id:r,type:i?.type,timestamp:Date.now()})}),a=e.events.on(f.FAILED,n=>{let{id:r,status:i}=n,a=t.subagentStatus[r];a&&(a.status=i??`error`,a.completedAt=Date.now()),v(e,t),e.events?.emit(C.SUBAGENT_FAILED,{id:r,type:a?.type,timestamp:Date.now()})}),o=e.events.on(f.STEERED,n=>{let{id:r}=n;t.subagentStatus[r]||(t.subagentStatus[r]={type:`unknown`,status:`running`,startedAt:Date.now()}),v(e,t)});n&&n.push(r,i,a,o)}}const Ce=[`read`,`grep`,`find`,`ls`,`glob`];function we(e,t){e.registerCommand(`maestria-status`,{description:`Show current maestria session state including handoff history`,handler:async(e,n)=>{let r=y(t);if(!r){n.ui.notify(`No active maestria state to report.`);return}n.ui.setEditorText(r)}}),e.registerCommand(`review`,{description:`Enter review mode. Blocks destructive tools, sets read-only toolset.`,handler:async(n,r)=>{if(!n.trim()){r.ui.notify(`Usage: /review <target> - describe what to review`);return}let i=r.model?.id??null,a=e.getActiveTools(),o={...t,reviewMode:!0,originalModel:i,originalTools:a};if(Object.assign(t,o),v(e,t),t.reviewModel){let n=await x(e,r,t);n&&(r.ui.notify(`Review mode: switched to ${n}`),e.events?.emit(C.REVIEW_ACTIVATED,{originalModel:t.originalModel,reviewModel:n,timestamp:Date.now()}))}e.setActiveTools(Ce),e.sendUserMessage([`[REVIEW: ${n}]`,``,`Review: ${n}. Use the reviewer prompt template.`,`Read only, no edits, report findings.`].join(`
|
|
11
|
+
`),{deliverAs:`steer`})}}),e.registerCommand(`restore-model`,{description:`Restore the original model and tools that were active before review mode was entered.`,handler:async(n,r)=>{if(!t.reviewMode){r.ui.notify(`Not in review mode. Nothing to restore.`);return}let i=t.originalModel;await b(e,r,t),v(e,t),r.ui.notify(`Restored original model and tools.`),e.events?.emit(C.REVIEW_DEACTIVATED,{originalModel:i,timestamp:Date.now()})}}),e.registerCommand(`handoff`,{description:`Generate a structured handoff prompt for a new task context`,handler:async(n,r)=>{if(!n.trim()){r.ui.notify(`Usage: /handoff <goal> - describe the task context for handoff`);return}let i=n.trim(),a=[`**Goal:** `+i,``,`**Context:**`,`- Mode: `+(t.mode??`none`),`- Active task: `+(t.activeTask||`none`),`- Specialists delegated: `+((t.specialistsDelegated?.length??0)>0?t.specialistsDelegated.join(`, `):`none`),`- Recent handoffs: `+(t.handoffHistory?.length??0)+` entries`,`- Files modified: `+((t.filesModified?.length??0)>0?t.filesModified.join(`, `):`none`),``,`**Requirements:**`,`(fill in specific requirements)`,``,`**Known problems:**`,(t.blockers?.length??0)>0?t.blockers.map(e=>`- `+e).join(`
|
|
12
12
|
`):`(no known problems documented)`,``,`**Assumptions documented:**`,`(document assumptions made, tagged [inferred] where uncertain)`,``,`**Success criteria:**`,`(fill in how to verify completion)`,``,`**Next step:**`,`(fill in what happens after this task)`,``,`---`,`Complete the fields above before sending.`].join(`
|
|
13
|
-
`);Object.assign(t,g(t,`current`,`next`,i)),v(e,t),e.sendUserMessage(a,{deliverAs:`steer`})}}),e.registerCommand(`review-model`,{description:`Set which model to use when entering review mode`,handler:async(n,r)=>{if(!n.trim()){r.ui.notify(`Usage: /review-model <model-id>`);return}let i=n.trim(),a=r.modelRegistry.getAll();if(!a.find(e=>e.id===i)){r.ui.notify(`Unknown model: "${i}". Available: ${a.map(e=>e.id).join(`, `)}`);return}t.reviewModel=i,v(e,t),r.ui.notify(`Review model set to: ${i}`)}})}function Q(e,t){
|
|
13
|
+
`);Object.assign(t,g(t,`current`,`next`,i)),v(e,t),e.sendUserMessage(a,{deliverAs:`steer`})}}),e.registerCommand(`review-model`,{description:`Set which model to use when entering review mode`,handler:async(n,r)=>{if(!n.trim()){r.ui.notify(`Usage: /review-model <model-id>`);return}let i=n.trim(),a=r.modelRegistry.getAll();if(!a.find(e=>e.id===i)){r.ui.notify(`Unknown model: "${i}". Available: ${a.map(e=>e.id).join(`, `)}`);return}t.reviewModel=i,v(e,t),r.ui.notify(`Review model set to: ${i}`)}})}function Q(e,t){we(e,t)}const Te=[/rm\s+-rf\s+\//,/dd\s+if=/,/>\s*\/dev\/sd/,/chmod\s+-R\s+777\s+\//,/mkfs\.\w+/,/:(){ :\|:& };:/,/>\s*\/etc\/(passwd|shadow|sudoers)/,/\beval\b/,/wget\s+-O\s*-\s*\|\s*(bash|sh)/,/curl\s+.*\|\s*(bash|sh)/,/crontab\s+-r/],Ee=/^(ls|cat|head|tail|git status|git diff|git log|git branch|find|grep|rg|pnpm test|npm test|pwd|which)\b/;function De(e){let t=e.trim();if(t.includes(`$(`)||t.includes("`"))return!1;let n=t.replace(/\d?>&[12]/g,``);return!n.includes(`>`)&&n.split(/[\n;&|]+/).every(e=>Ee.test(e.trim()))}function Oe(e,t){e.on(`tool_call`,async(n,r)=>{if(!n||!n.toolName)return;if(t.mode!==null&&e.getActiveTools().includes(`subagent`)&&(p(`edit`,n)||p(`write`,n)||p(`patch`,n)||n.toolName===`bash`)){if(n.toolName===`bash`){let e=n.input;if(De(typeof e?.command==`string`?e.command:``))return}return{block:!0,reason:`Tool '${n.toolName}' is blocked for the orchestrator. Use 'maestria_subagent' to delegate mutations to specialists.`}}if(t.reviewMode&&(p(`edit`,n)||p(`write`,n)||p(`bash`,n)))return{block:!0,reason:`Review mode is active. Report findings, do not edit.`};if(p(`bash`,n)){if(!n.input||typeof n.input!=`object`)return;let e=n.input.command;if(e){for(let t of Te)if(t.test(e))return r.hasUI&&await r.ui.confirm(`Dangerous Pattern Detected`,`This command matches a dangerous pattern:\n${e}\nProceed?`)?void 0:{block:!0,reason:`Command matches dangerous pattern: ${t}`}}}let i=!1;if(p(`read`,n)){let e=n.input?.path;typeof e==`string`&&e&&(Object.assign(t,te(t,e)),i=!0)}else if(p(`edit`,n)||p(`write`,n)){let e=n.input?.path;typeof e==`string`&&e&&(Object.assign(t,ee(t,e)),i=!0)}i&&v(e,t)})}function ke(e){return typeof e==`object`&&!!e}function Ae(e){let t=e?.sessionManager;if(typeof t?.getBranch!=`function`)return null;let n=t.getBranch();return Array.isArray(n)?n:null}function $(e,t){let n=m(),r=Ae(t);if(!r){let t=e;for(let e of Object.keys(t))delete t[e];Object.assign(e,n);return}for(let e=r.length-1;e>=0;e--){let t=r[e];if(t.type===`custom`&&t.customType===`maestria_state`){ke(t.data)&&Object.assign(n,t.data);break}}let i=e;for(let e of Object.keys(i))delete i[e];Object.assign(e,n)}function je(e){let t=m(),n=[];me(e,t),pe(e,t);let r=_e(t);e.on(`before_agent_start`,(e,t)=>r(e,t)),e.on(`session_start`,(e,n)=>{se(n),$(t,n)}),e.on(`session_tree`,(e,n)=>{$(t,n)}),ye(e,t),Se(e,t,n),Q(e,t),e.on(`session_shutdown`,()=>{for(let e of n)e();n.length=0}),Oe(e,t)}export{je as default};
|
|
14
14
|
//# sourceMappingURL=extension.mjs.map
|
package/dist/extension.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extension.mjs","names":["restoreOriginalState","restoreCore","deploySpecialistAgents","__dirname","installModeAutoDetect","installModeCommands","__dirname","COMMANDS_DIR","installCompactionHandlers","installCommands","restoreOriginalState"],"sources":["../../shared/pi/src/state-core.ts","../../shared/pi/src/review-core.ts","../src/state/review.ts","../../shared/pi/src/subagent-utils.ts","../../shared/pi/src/agent-deployment.ts","../src/agents.ts","../../shared/pi/src/modes-core.ts","../src/modes.ts","../src/rules.ts","../../shared/pi/src/compaction-core.ts","../src/compaction.ts","../src/subagent-polling.ts","../src/subagent.ts","../../shared/pi/src/commands-core.ts","../src/commands.ts","../../shared/pi/src/tools-core.ts","../src/tools.ts","../src/extension.ts"],"sourcesContent":["/**\n * Shared state management for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Provides shared state-management types, transforms, persistence, and rendering\n * consumed directly by @maestria/omp and @maestria/pi.\n *\n * @module\n */\n\n// ── Types ──\n\nexport type ModeKeyword = 'fein' | 'sonar' | 'blitz';\n\nexport const HANDOFF_HISTORY_CAP = 5;\nexport const FILE_HISTORY_CAP = 10;\n\nexport interface HandoffEntry {\n from: string;\n to: string;\n task: string;\n timestamp: number;\n}\n\nexport interface SubagentStatusInfo {\n type: string;\n status: string;\n startedAt: number;\n completedAt?: number;\n}\n\n/**\n * Mirror of the host platform's native goal (e.g. OMP goal mode).\n *\n * Platform-agnostic by design: only the objective text and status are\n * carried so shared state stays free of platform-specific types.\n */\nexport interface NativeGoalMirror {\n objective: string;\n status: string;\n}\n\nexport interface MaestriaState {\n mode: ModeKeyword | null;\n activeTask: string;\n completionPromise: string;\n specialistsDelegated: string[];\n blockers: string[];\n filesModified: string[];\n filesRead: string[];\n handoffHistory: HandoffEntry[];\n reviewMode: boolean;\n originalModel: string | null;\n originalTools: string[] | null;\n subagentStatus: Record<string, SubagentStatusInfo>;\n reviewModel: string | null;\n nativeGoal: NativeGoalMirror | null;\n}\n\n// ── Transforms ──\n\nexport function createInitialState(): MaestriaState {\n return {\n mode: null,\n activeTask: '',\n completionPromise: '',\n specialistsDelegated: [],\n blockers: [],\n filesModified: [],\n filesRead: [],\n handoffHistory: [],\n reviewMode: false,\n originalModel: null,\n originalTools: null,\n subagentStatus: {},\n reviewModel: null,\n nativeGoal: null,\n };\n}\n\nfunction prependDeduped(files: string[], path: string, cap: number): string[] {\n const filtered = files.filter((f) => f !== path);\n return [path, ...filtered].slice(0, cap);\n}\n\nexport function recordHandoff(\n state: MaestriaState,\n from: string,\n to: string,\n task: string,\n): MaestriaState {\n const entry: HandoffEntry = { from, to, task, timestamp: Date.now() };\n const history = [entry, ...state.handoffHistory].slice(0, HANDOFF_HISTORY_CAP);\n return { ...state, handoffHistory: history };\n}\n\nexport function recordFileModified(state: MaestriaState, path: string): MaestriaState {\n return { ...state, filesModified: prependDeduped(state.filesModified, path, FILE_HISTORY_CAP) };\n}\n\nexport function recordFileRead(state: MaestriaState, path: string): MaestriaState {\n return { ...state, filesRead: prependDeduped(state.filesRead, path, FILE_HISTORY_CAP) };\n}\n\nexport function recordSpecialistDelegated(state: MaestriaState, name: string): MaestriaState {\n if (state.specialistsDelegated.includes(name)) return state;\n return { ...state, specialistsDelegated: [...state.specialistsDelegated, name] };\n}\n\nexport function recordSubagentStatus(\n state: MaestriaState,\n id: string,\n info: SubagentStatusInfo,\n): MaestriaState {\n return { ...state, subagentStatus: { ...state.subagentStatus, [id]: info } };\n}\n\nexport function setReviewMode(state: MaestriaState, active: boolean): MaestriaState {\n return { ...state, reviewMode: active };\n}\n\nexport function exitReviewMode(state: MaestriaState): {\n state: MaestriaState;\n originalModel: string | null;\n originalTools: string[] | null;\n} {\n return {\n state: {\n ...state,\n reviewMode: false,\n originalModel: null,\n originalTools: null,\n },\n originalModel: state.originalModel,\n originalTools: state.originalTools,\n };\n}\n\n// ── Persistence ──\n\nexport function persistState(\n pi: { appendEntry: (type: string, data: unknown) => void },\n state: MaestriaState,\n): void {\n pi.appendEntry('maestria_state', { ...state });\n}\n\n// ── Render ──\n\nexport function renderMaestriaSummary(state: MaestriaState): string {\n const parts: string[] = [];\n\n if (state.mode) {\n parts.push(`**Mode:** ${state.mode.toUpperCase()}`);\n }\n\n if (state.reviewModel) {\n parts.push(`**Review Model:** ${state.reviewModel}`);\n }\n\n if (state.activeTask) {\n parts.push(`**Goal:** ${state.activeTask}`);\n }\n\n if (state.nativeGoal) {\n parts.push(`**Native Goal:** ${state.nativeGoal.objective} (${state.nativeGoal.status})`);\n }\n\n if (state.completionPromise) {\n parts.push(`**Completion Promise:** ${state.completionPromise}`);\n }\n\n if (state.specialistsDelegated.length > 0) {\n parts.push(`**Specialists Delegated:** ${state.specialistsDelegated.join(', ')}`);\n }\n\n if (state.blockers.length > 0) {\n parts.push('**Blockers:**');\n for (const blocker of state.blockers) {\n parts.push(`- ${blocker}`);\n }\n }\n\n const fileSubs: string[] = [];\n if (state.filesModified.length > 0) {\n fileSubs.push(`**Modified:** ${state.filesModified.join(', ')}`);\n }\n if (state.filesRead.length > 0) {\n fileSubs.push(`**Read:** ${state.filesRead.join(', ')}`);\n }\n if (fileSubs.length > 0) {\n parts.push(`**Files:** ${fileSubs.join('; ')}`);\n }\n\n if (state.handoffHistory.length > 0) {\n parts.push('**Recent Handoffs:**');\n for (const entry of state.handoffHistory) {\n parts.push(`- ${entry.from} → ${entry.to}: ${entry.task}`);\n }\n }\n\n return parts.join('\\n\\n');\n}\n","/**\n * Shared review-mode orchestration for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Consumed by both @maestria/omp and @maestria/pi to eliminate duplication\n * of review-mode model/tool restoration and switching.\n *\n * @module\n */\n\nimport type { MaestriaState } from './state-core.js';\nimport { exitReviewMode } from './state-core.js';\n\n// ── Duck-typed platform interfaces ──\n\ninterface ReviewPi {\n setActiveTools(tools: string[]): void | Promise<void>;\n setModel(model: unknown): void | Promise<void>;\n}\n\ninterface ReviewCtx {\n modelRegistry: { getAll(): Array<{ id: string }> };\n ui: { notify(msg: string): void };\n}\n\nexport async function restoreOriginalState(\n pi: ReviewPi,\n ctx: ReviewCtx,\n state: MaestriaState,\n): Promise<void> {\n const { state: clearedState, originalModel, originalTools } = exitReviewMode(state);\n\n if (originalTools && originalTools.length > 0) {\n await pi.setActiveTools(originalTools);\n }\n\n if (originalModel) {\n try {\n const models = ctx.modelRegistry.getAll();\n const model = models.find((m: { id: string }) => m.id === originalModel);\n if (model) {\n await pi.setModel(model);\n }\n } catch {\n // Best-effort: model restoration is non-critical\n }\n }\n\n Object.assign(state, clearedState);\n}\n\nexport async function cycleToReviewModel(\n pi: ReviewPi,\n ctx: ReviewCtx,\n state: MaestriaState,\n): Promise<string | null> {\n const reviewModel = state.reviewModel;\n if (!reviewModel) {\n return null;\n }\n try {\n const models = ctx.modelRegistry.getAll();\n const model = models.find((m) => m.id === reviewModel);\n if (model) {\n await pi.setModel(model);\n return reviewModel;\n } else {\n ctx.ui.notify(`Review model \"${reviewModel}\" not found in registry, staying on current.`);\n return null;\n }\n } catch {\n ctx.ui.notify(`Could not switch to review model \"${reviewModel}\", staying on current.`);\n return null;\n }\n}\n","import type {\n ExtensionAPI,\n ExtensionCommandContext,\n ExtensionContext,\n} from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@maestria/shared-pi/state-core';\nimport {\n cycleToReviewModel as cycleCore,\n restoreOriginalState as restoreCore,\n} from '@maestria/shared-pi/review-core';\n\nexport async function restoreOriginalState(\n pi: ExtensionAPI,\n ctx: ExtensionContext,\n state: MaestriaState,\n): Promise<void> {\n await restoreCore(pi as never, ctx as never, state);\n}\n\nexport async function cycleToReviewModel(\n pi: ExtensionAPI,\n ctx: ExtensionCommandContext,\n state: MaestriaState,\n): Promise<string | null> {\n return cycleCore(pi as never, ctx as never, state);\n}\n","/**\n * Shared subagent validation utilities for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Imported by both @maestria/omp and @maestria/pi to eliminate duplication.\n *\n * @module\n */\n\n/** Maestria cross-extension event names. */\nexport const MAESTRIA_EVENTS = {\n REVIEW_ACTIVATED: 'maestria:review:activated',\n REVIEW_DEACTIVATED: 'maestria:review:deactivated',\n SUBAGENT_STARTED: 'maestria:subagent:started',\n SUBAGENT_COMPLETED: 'maestria:subagent:completed',\n SUBAGENT_FAILED: 'maestria:subagent:failed',\n} as const;\n\n/** The set of specialist agent types maestria supports. */\nexport const ALLOWED_AGENTS = [\n 'adventurer',\n 'architect',\n 'builder',\n 'diagnose',\n 'planner',\n 'reviewer',\n 'writer',\n] as const;\n\n/** A valid specialist agent name. */\nexport type AllowedAgent = (typeof ALLOWED_AGENTS)[number];\n\n/** The 7-field handoff contract used in delegation. */\nexport const HANDOFF_FIELDS = [\n 'Goal',\n 'Context',\n 'Requirements',\n 'Known problems',\n 'Assumptions documented',\n 'Success criteria',\n 'Next step',\n] as const;\n\n/** Result of validating a handoff document against the contract fields. */\nexport interface HandoffValidation {\n valid: boolean;\n errors: string[];\n}\n\n/**\n * Asserts that `agent` is a known maestria specialist.\n * @throws {Error} if the agent name is not in ALLOWED_AGENTS.\n */\nexport function assertValidAgent(agent: string): asserts agent is AllowedAgent {\n if (!ALLOWED_AGENTS.includes(agent as AllowedAgent)) {\n throw new Error(`Unknown agent: \"${agent}\". Allowed: ${ALLOWED_AGENTS.join(', ')}`);\n }\n}\n\n/**\n * Asserts that `task` is a non-empty, non-whitespace string.\n * @throws {Error} with the given label if task is falsy or all-whitespace.\n */\nexport function assertNonEmptyTask(\n task: string | undefined,\n label: string,\n): asserts task is string {\n if (!task || !task.trim()) {\n throw new Error(label);\n }\n}\n\n/**\n * Validates that a handoff document contains all required fields\n * with non-empty content. Each field is expected in markdown bold format:\n * `**Field:** content`.\n */\nexport function validateHandoff(handoff: string): HandoffValidation {\n const errors: string[] = [];\n for (const field of HANDOFF_FIELDS) {\n // Match field header and capture content up to the next field or end of string.\n // This avoids false positives when an empty field is followed by another field's `**` header.\n const pattern = `\\\\*\\\\*${field}:\\\\*\\\\*([\\\\s\\\\S]*?)(?=\\\n\\\\*\\\\*|$)`;\n const match = handoff.match(new RegExp(pattern, 'i'));\n if (!match || !match[1] || !match[1].trim()) {\n errors.push(`Missing or empty field: \"${field}\"`);\n }\n }\n return { valid: errors.length === 0, errors };\n}\n","/**\n * Shared agent deployment logic for Maestria platform packages.\n *\n * Both @maestria/omp and @maestria/pi deploy specialist agent .md files\n * to their respective platform agent directories. This module eliminates\n * the duplication between the two packages.\n *\n * @module\n */\n\nimport { readFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { join } from 'node:path';\nimport { ALLOWED_AGENTS } from './subagent-utils.js';\n\n/**\n * Deploy bundled specialist agent .md files to the given destination directory.\n *\n * Only creates files that don't already exist - never overwrites user-customized agents.\n *\n * @param agentsSrc - Path to the source directory containing specialist .md files\n * @param agentsDest - Absolute path to the platform's agents destination directory\n * (e.g. `join(homedir(), '.omp', 'agent', 'agents')`)\n * @returns The number of agents newly deployed\n */\nexport function deploySpecialistAgents(agentsSrc: string, agentsDest: string): number {\n if (!existsSync(agentsSrc)) {\n console.warn('[maestria] Agents source directory not found:', agentsSrc);\n return 0;\n }\n\n try {\n mkdirSync(agentsDest, { recursive: true });\n } catch {\n console.warn('[maestria] Could not create agents directory:', agentsDest);\n return 0;\n }\n\n let deployed = 0;\n for (const name of ALLOWED_AGENTS) {\n const srcFile = join(agentsSrc, `${name}.md`);\n const destFile = join(agentsDest, `${name}.md`);\n\n if (!existsSync(srcFile)) {\n console.warn(`[maestria] Agent source not found: ${name}.md`);\n continue;\n }\n\n if (existsSync(destFile)) continue;\n\n try {\n const content = readFileSync(srcFile, 'utf-8');\n writeFileSync(destFile, content, 'utf-8');\n deployed++;\n } catch (err) {\n console.warn(`[maestria] Failed to deploy agent ${name}:`, err);\n }\n }\n\n if (deployed > 0) {\n console.log(`[maestria] Deployed ${deployed} specialist agents to ${agentsDest}`);\n }\n\n return deployed;\n}\n","import { homedir } from 'node:os';\nimport { join, dirname } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { deploySpecialistAgents as deployAgents } from '@maestria/shared-pi/agent-deployment';\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\nconst AGENTS_SRC = join(__dirname, '..', 'agents');\n\n/**\n * Deploy bundled specialist agent .md files to the pi-subagents agents directory.\n *\n * pi-subagents discovers agent types from ~/.pi/agent/agents/*.md on every\n * registry.reload() call (which fires automatically on each tool invocation).\n * This function ensures the files are in place before the first subagent dispatch.\n *\n * Only creates files that don't already exist - never overwrites user-customized agents.\n */\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport function deploySpecialistAgents(_ctx?: unknown): void {\n deployAgents(AGENTS_SRC, join(homedir(), '.pi', 'agent', 'agents'));\n}\n","/**\n * Shared mode constants and utilities for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Imported by both @maestria/omp and @maestria/pi to eliminate duplication\n * in mode prompt loading, keyword detection, and text transformation.\n *\n * @module\n */\n\nimport { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport type { MaestriaState } from './state-core.js';\n\n// ── Constants ──\n\nexport const MODE_KEYWORDS = ['fein', 'sonar', 'blitz'] as const;\nexport const MODE_CLEAR_COMMAND = 'mode-clear';\nexport type ModeKeyword = (typeof MODE_KEYWORDS)[number];\n\nexport const MODE_MARKERS: Record<ModeKeyword, string> = {\n fein: '[MODE: fein]',\n sonar: '[MODE: sonar]',\n blitz: '[MODE: blitz]',\n};\n\n// ── Prompt loading ──\n\n/** Lazily cached mode prompts - shared across platforms. */\nconst _promptCache: Partial<Record<ModeKeyword, string>> = {};\n\n/**\n * Load and cache a mode prompt from a commands directory.\n * The commandsDir should point to a directory containing `fein.md`,\n * `sonar.md`, and `blitz.md` files.\n */\nexport function loadModePrompt(name: string, commandsDir: string): string {\n const content = readFileSync(resolve(commandsDir, `${name}.md`), 'utf-8');\n const modeIdx = content.indexOf('## MODE:');\n if (modeIdx !== -1) {\n return content.slice(modeIdx).replace(/\\s+$/, '') + '\\n';\n }\n return content.replace(/\\s+$/, '') + '\\n';\n}\n\n/**\n * Get the full mode prompt (marker + body) for a keyword, loading from\n * the given commands directory on first access.\n */\nexport function getModePrompt(keyword: ModeKeyword, commandsDir: string): string {\n if (!(keyword in _promptCache)) {\n try {\n _promptCache[keyword] = loadModePrompt(keyword, commandsDir);\n } catch (e) {\n console.warn(`[maestria] Failed to load mode prompt \"${keyword}\":`, e);\n _promptCache[keyword] = '';\n }\n }\n return `${MODE_MARKERS[keyword]}\\n\\n${_promptCache[keyword]}`;\n}\n\n// ── Keyword detection ──\n\n/** Result of detecting a mode keyword in text. */\nexport interface ModeDetectResult {\n /** The detected keyword. */\n keyword: ModeKeyword;\n /** The text with the keyword stripped and trimmed. */\n strippedText: string;\n /** The full mode prompt (marker + body). */\n prompt: string;\n}\n\n/** Regex matching fenced code blocks (```) and inline backtick spans (`). */\nconst CODE_BLOCK_RE = /```[\\s\\S]*?```|`[^`]*`/g;\n\n/**\n * Find ranges of fenced code blocks and inline code spans in text.\n * Returns [start, end) positions. Keywords inside these ranges are\n * ignored during detection (per ADR-OC-003).\n */\nfunction findAllCodeBlockRanges(text: string): Array<[number, number]> {\n const ranges: Array<[number, number]> = [];\n let match: RegExpExecArray | null;\n while ((match = CODE_BLOCK_RE.exec(text)) !== null) {\n ranges.push([match.index, match.index + match[0].length]);\n }\n return ranges;\n}\n\nfunction isInRanges(index: number, ranges: Array<[number, number]>): boolean {\n return ranges.some(([start, end]) => index >= start && index < end);\n}\n\n/**\n * Priority mapping for mode keyword restrictiveness.\n * Higher number = more restrictive = wins when multiple keywords are present.\n * fein (3): full pipeline with mandatory gates\n * sonar (2): research only, no code\n * blitz (1): fast implementation, skip optional ceremony; required review remains\n */\nconst MODE_PRIORITY: Record<ModeKeyword, number> = {\n fein: 3,\n sonar: 2,\n blitz: 1,\n};\n\n/**\n * Detect a mode keyword (fein/sonar/blitz) in text as a whole word,\n * case-insensitive. Detection rules (per ADR-OC-003):\n * - Word-boundary regex matching (\\bfein\\b, \\bsonar\\b, \\bblitz\\b)\n * - Most restrictive match wins (fein > sonar > blitz)\n * - Case-insensitive\n * - Matches inside fenced code blocks (```) and inline backticks (`) are ignored\n */\nexport function detectModeInText(text: string, commandsDir: string): ModeDetectResult | null {\n if (!text) return null;\n\n const codeRanges = findAllCodeBlockRanges(text);\n let best: { keyword: ModeKeyword; index: number } | null = null;\n\n for (const keyword of MODE_KEYWORDS) {\n const regex = new RegExp(`\\\\b${keyword}\\\\b`, 'gi');\n let match: RegExpExecArray | null;\n while ((match = regex.exec(text)) !== null) {\n if (isInRanges(match.index, codeRanges)) continue;\n // Most-restrictive wins: prefer higher-priority mode over position\n if (best === null || MODE_PRIORITY[keyword] > MODE_PRIORITY[best.keyword]) {\n best = { keyword, index: match.index };\n }\n }\n }\n\n if (best === null) return null;\n\n // Strip the matched keyword, cleaning up a trailing colon and collapsing\n // double spaces (mirrors opencode's stripKeyword behavior).\n const before = text.slice(0, best.index);\n const after = text.slice(best.index + best.keyword.length).replace(/^:\\s*/, '');\n const strippedText = (before + after).replace(/ {2,}/g, ' ').trim();\n\n return {\n keyword: best.keyword,\n strippedText,\n prompt: getModePrompt(best.keyword, commandsDir),\n };\n}\n\n/**\n * Build the final text to send to the LLM: prompt + stripped text.\n * If strippedText is empty, returns just the prompt.\n */\nexport function buildModeText(prompt: string, strippedText: string): string {\n return strippedText ? `${prompt}\\n\\n${strippedText}` : prompt;\n}\n\n// ── Platform handler factories ──\n\n/**\n * Install an input event handler that detects mode keywords (fein/sonar/blitz)\n * in user input, strips them, and injects the mode prompt.\n *\n * @param onInput - Platform's `pi.on('input', handler)` method\n * @param state - Shared maestria state\n * @param commandsDir - Path to directory containing fein.md/sonar.md/blitz.md\n * @param opts - Platform-specific callbacks and result builders\n */\nexport function installModeAutoDetect(\n onInput: (handler: (event: unknown, ctx: unknown) => unknown) => void,\n state: MaestriaState,\n commandsDir: string,\n opts: {\n /** Exit review mode - calls platform's restoreOriginalState */\n restoreOriginalState: (ctx: unknown) => Promise<void>;\n /** Persist state after mode change */\n persistState: () => void;\n /** Return value when no keyword is detected (e.g. Pi: { action: 'continue' }) */\n noMatch: unknown;\n /** Build return value from transformed text (e.g. Pi: { action: 'transform', text }) */\n transform: (text: string) => unknown;\n },\n): void {\n onInput(async (event: unknown, ctx: unknown) => {\n const text = ((event as Record<string, unknown>).text as string) ?? '';\n const result = detectModeInText(text, commandsDir);\n if (!result) return opts.noMatch;\n\n if (state.reviewMode) {\n await opts.restoreOriginalState(ctx);\n }\n\n state.mode = result.keyword;\n opts.persistState();\n\n return opts.transform(buildModeText(result.prompt, result.strippedText));\n });\n}\n\n/**\n * Install slash commands for fein/sonar/blitz that set the workflow mode\n * and show a notification. Task description injection is handled by the\n * auto-detect handler instead.\n *\n * @param registerCommand - Platform's `pi.registerCommand(name, opts)` method\n * @param state - Shared maestria state\n * @param opts - Platform-specific callbacks\n */\nexport function installModeCommands(\n registerCommand: (\n name: string,\n options: { description: string; handler: (...args: unknown[]) => unknown },\n ) => void,\n state: MaestriaState,\n opts: {\n /** Exit review mode before switching modes */\n restoreOriginalState: (ctx: unknown) => Promise<void>;\n /** Persist state after mode change */\n persistState: () => void;\n },\n): void {\n registerCommand(MODE_CLEAR_COMMAND, {\n description: 'Clear workflow mode and return to neutral routing',\n handler: async (_args: unknown, ctx: unknown) => {\n if (state.reviewMode) {\n await opts.restoreOriginalState(ctx);\n }\n state.mode = null;\n opts.persistState();\n ((ctx as Record<string, unknown>).ui as { notify: (msg: string) => void }).notify(\n 'Workflow mode cleared. Neutral routing is active.',\n );\n },\n });\n\n for (const keyword of MODE_KEYWORDS) {\n registerCommand(keyword, {\n description: `Set workflow mode to ${keyword}`,\n handler: async (_args: unknown, ctx: unknown) => {\n if (state.reviewMode) {\n await opts.restoreOriginalState(ctx);\n }\n\n state.mode = keyword;\n opts.persistState();\n\n ((ctx as Record<string, unknown>).ui as { notify: (msg: string) => void }).notify(\n `Mode set to ${keyword}. Describe what you'd like to work on.`,\n );\n },\n });\n }\n}\n","import { dirname, resolve } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport type { ExtensionAPI, ExtensionContext } from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { persistState, restoreOriginalState } from '@/state.js';\nimport {\n installModeAutoDetect as installAutoDetect,\n installModeCommands as installCommands,\n} from '@maestria/shared-pi/modes-core';\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\nconst COMMANDS_DIR = resolve(__dirname, '../agents/commands');\n\nexport function installModeAutoDetect(pi: ExtensionAPI, state: MaestriaState): void {\n installAutoDetect((handler) => pi.on('input', handler as never), state, COMMANDS_DIR, {\n restoreOriginalState: (ctx) => restoreOriginalState(pi, ctx as ExtensionContext, state),\n persistState: () => persistState(pi, state),\n noMatch: { action: 'continue' as const },\n transform: (text) => ({ action: 'transform' as const, text }),\n });\n}\n\nexport function installModeCommands(pi: ExtensionAPI, state: MaestriaState): void {\n installCommands((name, opts) => pi.registerCommand(name, opts as never), state, {\n restoreOriginalState: (ctx) => restoreOriginalState(pi, ctx as ExtensionContext, state),\n persistState: () => persistState(pi, state),\n });\n}\n","import { dirname, resolve } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport type {\n BeforeAgentStartEvent,\n BeforeAgentStartEventResult,\n ExtensionContext,\n} from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { getModePrompt } from '@maestria/shared-pi/modes-core';\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\nconst COMMANDS_DIR = resolve(__dirname, '../agents/commands');\n\n/**\n * Creates a before_agent_start handler that injects workflow mode prompts.\n *\n * This is the only dynamic prompt injection needed from the extension.\n * Static behavioral content (orchestrator prompt + global rules) is\n * auto-injected by Pi's skill system via SKILL.md files registered in\n * the pi.skills manifest field - the standard Pi extension pattern.\n *\n * When no mode is active, the handler returns void (no modification),\n * letting Pi's built-in prompt assembly (skills + context files + tools)\n * stand as-is.\n */\nexport function createModePromptHandler(state: MaestriaState) {\n return (\n event: BeforeAgentStartEvent,\n _ctx: ExtensionContext,\n ): BeforeAgentStartEventResult | void => {\n if (!state.mode) return;\n\n const parts: string[] = [\n event.systemPrompt,\n '',\n getModePrompt(state.mode, COMMANDS_DIR),\n '',\n `The user has set workflow mode to \"${state.mode}\". ` +\n 'Honor this mode throughout the session until changed via /command.',\n ];\n\n return { systemPrompt: parts.join('\\n') };\n };\n}\n","/**\n * Shared compaction handlers for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Imported by both @maestria/omp and @maestria/pi to eliminate duplication.\n *\n * @module\n */\n\nimport { renderMaestriaSummary } from './state-core.js';\nimport type { MaestriaState } from './state-core.js';\n\n/**\n * Install handlers for session compaction and tree events to persist\n * and restore maestria state across session compaction boundaries.\n *\n * Uses duck-typed `pi` parameter - both Pi and OMP ExtensionAPI types\n * satisfy the `{ on(event: string, handler): void }` shape needed here.\n */\nexport function installCompactionHandlers(\n pi: {\n on: (event: string, handler: (...args: unknown[]) => unknown) => void;\n },\n state: MaestriaState,\n): void {\n pi.on('session_before_compact', (event: unknown) => {\n const prep = (event as Record<string, unknown>).preparation as\n | Record<string, unknown>\n | undefined;\n return {\n compaction: {\n summary: renderMaestriaSummary(state),\n details: { ...state },\n firstKeptEntryId: prep?.firstKeptEntryId,\n tokensBefore: prep?.tokensBefore,\n },\n };\n });\n\n pi.on('session_before_tree', (event: unknown) => {\n const prep = (event as Record<string, unknown>).preparation as\n | Record<string, unknown>\n | undefined;\n if (prep?.userWantsSummary) {\n return {\n summary: {\n summary: renderMaestriaSummary(state),\n },\n };\n }\n return undefined;\n });\n}\n","/**\n * Pi platform compaction handlers.\n *\n * Thin wrapper around the shared implementation in\n * @maestria/shared-pi/compaction-core.\n *\n * @module\n */\n\nimport type { ExtensionAPI } from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { installCompactionHandlers as installHandlers } from '@maestria/shared-pi/compaction-core';\n\n/**\n * Install session compaction and tree event handlers for Pi.\n * Delegates to the shared implementation which is duck-type compatible\n * with Pi's ExtensionAPI.\n */\nexport function installCompactionHandlers(pi: ExtensionAPI, state: MaestriaState): void {\n // Bridge: ExtensionAPI.on has overloaded event types incompatible with\n // the duck-typed { on: (event: string, handler) => void } in the shared\n // module. The as-never cast is safe at runtime - both SDKs share the same\n // event shapes.\n installHandlers(\n {\n on: (event, handler) => {\n pi.on(event as never, handler as never);\n },\n },\n state,\n );\n}\n","import { Data, Effect } from 'effect';\n\n/** Terminal subagent statuses - agent will produce no more updates. */\nconst TERMINAL_STATUSES = new Set(['completed', 'steered', 'aborted', 'stopped', 'error']);\n\nexport type SubagentRecord = { status: string; result?: string; error?: string };\n\nexport type SubagentPollFailureReason = 'aborted' | 'timeout' | 'missing';\n\n/**\n * Typed failures from the polling boundary.\n *\n * Keeping these failures in the Effect error channel lets parallel polling\n * interrupt sibling fibers while preserving enough context for the existing\n * tool-level handoff fallback.\n */\nexport class SubagentPollError extends Data.TaggedError('SubagentPollError')<{\n readonly id: string;\n readonly reason: SubagentPollFailureReason;\n readonly message: string;\n}> {}\n\nexport interface SubagentPollingService {\n getRecord(id: string): SubagentRecord | undefined;\n abort?: (id: string) => boolean;\n}\n\nexport interface PollSubagentOptions {\n readonly id: string;\n readonly label: string;\n readonly sendUpdates: boolean;\n readonly service: SubagentPollingService;\n readonly signal?: AbortSignal;\n readonly onUpdate?: (result: { content: Array<{ type: string; text: string }> }) => void;\n readonly intervalMs?: number;\n readonly timeoutMs?: number;\n}\n\nfunction pollLoop(options: PollSubagentOptions): Effect.Effect<SubagentRecord, SubagentPollError> {\n const intervalMs = options.intervalMs ?? 500;\n const timeoutMs = options.timeoutMs ?? 180_000;\n const maxPolls = Math.ceil(timeoutMs / intervalMs);\n\n return Effect.gen(function* () {\n let polls = 0;\n let record = yield* Effect.sync(() => options.service.getRecord(options.id));\n\n while (record && !TERMINAL_STATUSES.has(record.status) && polls < maxPolls) {\n yield* Effect.sleep(intervalMs);\n record = yield* Effect.sync(() => options.service.getRecord(options.id));\n polls++;\n\n if (options.sendUpdates) {\n yield* Effect.sync(() => {\n options.onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `${options.label} running... (${Math.round((polls * intervalMs) / 1000)}s)`,\n },\n ],\n });\n });\n }\n }\n\n if (record && !TERMINAL_STATUSES.has(record.status)) {\n return yield* Effect.fail(\n new SubagentPollError({\n id: options.id,\n reason: 'timeout',\n message: `Subagent ${options.id} timed out after ${timeoutMs}ms`,\n }),\n );\n }\n\n if (!record) {\n return yield* Effect.fail(\n new SubagentPollError({\n id: options.id,\n reason: 'missing',\n message: `Subagent ${options.id} was cleaned up before completion`,\n }),\n );\n }\n\n return record;\n });\n}\n\nfunction abortEffect(id: string, signal: AbortSignal): Effect.Effect<never, SubagentPollError> {\n return Effect.callback<never, SubagentPollError>((resume) => {\n const onAbort = () => {\n resume(\n Effect.fail(\n new SubagentPollError({\n id,\n reason: 'aborted',\n message: 'Maestria subagent call aborted',\n }),\n ),\n );\n };\n\n if (signal.aborted) {\n onAbort();\n return;\n }\n\n signal.addEventListener('abort', onAbort, { once: true });\n return Effect.sync(() => signal.removeEventListener('abort', onAbort));\n });\n}\n\n/**\n * Poll one subagent as a cancellable Effect.\n *\n * The host Pi API is Promise-based, so callers cross the boundary with\n * `Effect.runPromise`. Keeping the polling workflow as an Effect gives the\n * parallel dispatcher structured cancellation: a failed or aborted poll\n * interrupts its sibling poll fibers instead of leaving timers running.\n */\nexport function pollSubagentEffect(\n options: PollSubagentOptions,\n): Effect.Effect<SubagentRecord, SubagentPollError> {\n const poll = pollLoop(options);\n return options.signal ? Effect.raceFirst(poll, abortEffect(options.id, options.signal)) : poll;\n}\n","import { Type } from 'typebox';\nimport { Effect } from 'effect';\nimport type { ExtensionAPI, ExtensionContext } from '@earendil-works/pi-coding-agent';\nimport { SUBAGENT_EVENTS } from '@gotgenes/pi-subagents';\nimport type { MaestriaState } from '@/state.js';\nimport { persistState, recordHandoff, recordSpecialistDelegated } from '@/state.js';\nimport {\n ALLOWED_AGENTS,\n assertValidAgent,\n assertNonEmptyTask,\n MAESTRIA_EVENTS,\n} from '@maestria/shared-pi/subagent-utils';\nimport { pollSubagentEffect, type SubagentPollingService } from '@/subagent-polling.js';\n\nconst ALLOWED_AGENT_NAMES: ReadonlyArray<string> = ALLOWED_AGENTS;\n\n/** Maximum time to wait for a subagent to complete, in milliseconds. */\nexport const POLL_TIMEOUT_MS = 180_000;\n\n/** Interval between subagent status checks, in milliseconds. */\nexport const POLL_INTERVAL_MS = 500;\n\n/** Maximum number of tasks allowed in parallel dispatch. */\nexport const MAX_PARALLEL_TASKS = 8;\n\nfunction abortSubagents(service: SubagentPollingService, ids: readonly string[]): void {\n if (typeof service.abort !== 'function') return;\n\n for (const id of ids) {\n try {\n service.abort(id);\n } catch {\n // Best-effort cleanup: the original polling failure remains authoritative.\n }\n }\n}\n\nfunction pollSubagentOrAbortEffect(options: Parameters<typeof pollSubagentEffect>[0]) {\n return Effect.tapError(pollSubagentEffect(options), () =>\n Effect.sync(() => abortSubagents(options.service, [options.id])),\n );\n}\n\n// ── Handoff recording helper ────────────────────────────────────\n\nfunction recordAndPersist(\n pi: ExtensionAPI,\n state: MaestriaState,\n agentName: string,\n taskText: string,\n): void {\n const updatedState = recordSpecialistDelegated(\n recordHandoff(state, 'orchestrator', agentName, taskText),\n agentName,\n );\n Object.assign(state, updatedState);\n persistState(pi, state);\n}\n\nexport function installSubagentTool(\n pi: ExtensionAPI,\n state: MaestriaState,\n cleanups?: Array<() => void>,\n): void {\n pi.registerTool({\n name: 'maestria_subagent',\n label: 'Maestria Subagent',\n description: 'Dispatch a task to a @maestria specialist subagent',\n promptSnippet:\n 'Delegate tasks to @maestria specialist subagents (adventurer, architect, builder, planner, diagnose, reviewer, writer)',\n promptGuidelines: [\n 'Use maestria_subagent when a task MUST be delegated to a specialist subagent rather than handled directly. Each specialist has focused capabilities: adventurer (recon), architect (design), builder (impl), planner (planning), diagnose (bugs), reviewer (QA), writer (docs).',\n ],\n prepareArguments(args: unknown) {\n return args;\n },\n parameters: Type.Object({\n agent: Type.String({\n description:\n 'Specialist agent name (required): adventurer, architect, builder, diagnose, planner, reviewer, writer',\n }),\n task: Type.String({ description: 'Task description for the subagent (required)' }),\n tasks: Type.Optional(\n Type.Array(\n Type.Object({\n agent: Type.String(),\n task: Type.String(),\n }),\n { description: 'Array of task objects for parallel or chain dispatch' },\n ),\n ),\n mode: Type.Optional(\n Type.Union([Type.Literal('parallel'), Type.Literal('chain'), Type.Literal('single')]),\n ),\n }),\n async execute(\n _toolCallId: string,\n params: {\n agent?: string;\n task?: string;\n tasks?: Array<{ agent: string; task: string }>;\n mode?: 'parallel' | 'chain' | 'single';\n },\n signal: AbortSignal | undefined,\n onUpdate: ((result: { content: Array<{ type: string; text: string }> }) => void) | undefined,\n _ctx: ExtensionContext,\n ) {\n // Block subagent dispatch when in review mode\n if (state.reviewMode) {\n return {\n content: [\n {\n type: 'text' as const,\n text: 'Subagent dispatch is not available during review mode. Use /restore-model to exit review mode first.',\n },\n ],\n };\n }\n\n // Determine dispatch mode (default to 'single' for backward compat)\n const mode = params.mode ?? 'single';\n\n // Validate parameters based on mode\n if (mode === 'single') {\n if (!params.agent || !ALLOWED_AGENT_NAMES.includes(params.agent)) {\n return {\n content: [\n {\n type: 'text' as const,\n text:\n `Invalid maestria_subagent call: 'agent' is required and must be one of ` +\n `${ALLOWED_AGENT_NAMES.join(', ')}. ` +\n `Re-dispatch with a valid agent name; the orchestrator may continue read-only exploration while the brief is corrected.`,\n },\n ],\n };\n }\n assertNonEmptyTask(params.task, 'Task description is required');\n } else if (mode === 'parallel') {\n if (!params.tasks || params.tasks.length < 2) {\n throw new Error(`For parallel mode, tasks array is required with at least 2 items`);\n }\n if (params.tasks.length > MAX_PARALLEL_TASKS) {\n throw new Error(\n `For parallel mode, tasks array may have at most ${MAX_PARALLEL_TASKS} items (got ${params.tasks.length})`,\n );\n }\n for (const t of params.tasks) {\n assertValidAgent(t.agent);\n assertNonEmptyTask(t.task, 'Task description is required for all tasks');\n }\n } else if (mode === 'chain') {\n if (!params.tasks || params.tasks.length < 2) {\n throw new Error('For chain mode, tasks array is required with at least 2 items');\n }\n for (const t of params.tasks) {\n assertValidAgent(t.agent);\n assertNonEmptyTask(t.task, 'Task description is required for all tasks');\n }\n }\n\n // Attempt to dispatch via @gotgenes/pi-subagents; handle missing service\n const { getSubagentsService } = await import('@gotgenes/pi-subagents');\n const service = getSubagentsService();\n if (!service || typeof service.spawn !== 'function') {\n return {\n content: [\n {\n type: 'text' as const,\n text: [\n '## Subagent Dispatch Unavailable',\n '',\n 'The `@gotgenes/pi-subagents` extension is required for subagent dispatch but has not been loaded.',\n '',\n 'Install it as a Pi extension:',\n '',\n '```',\n 'pi install npm:@gotgenes/pi-subagents',\n '```',\n '',\n 'Then restart your Pi session.',\n ].join('\\n'),\n },\n ],\n };\n }\n\n try {\n // --- SINGLE MODE ---\n if (mode === 'single') {\n const agent = params.agent!;\n const task = params.task!;\n\n // Spawn in foreground - returns subagent ID synchronously\n const id = service.spawn(agent, task, {\n description: task.slice(0, 80),\n foreground: true,\n inheritContext: true,\n });\n\n // Record handoff in state and persist (only after spawn succeeds)\n recordAndPersist(pi, state, agent, task);\n\n // Poll for completion (abort on poll failure so nothing is orphaned)\n const record = await Effect.runPromise(\n pollSubagentOrAbortEffect({\n id,\n label: `Subagent ${agent}`,\n sendUpdates: true,\n service,\n signal,\n onUpdate,\n intervalMs: POLL_INTERVAL_MS,\n timeoutMs: POLL_TIMEOUT_MS,\n }),\n );\n\n const resultText = record.result ?? record.error ?? 'No output.';\n\n return {\n content: [{ type: 'text' as const, text: resultText }],\n details: { subagentId: id },\n };\n }\n\n // --- PARALLEL MODE ---\n if (mode === 'parallel') {\n const taskList = params.tasks!;\n\n onUpdate?.({\n content: [\n { type: 'text' as const, text: `Spawning ${taskList.length} parallel subagents...` },\n ],\n });\n\n // Spawn all tasks. If a later spawn throws, abort the subagents already\n // spawned so they are not orphaned; the outer catch returns the handoff\n // fallback.\n const spawnedIds: string[] = [];\n try {\n for (const t of taskList) {\n const id = service.spawn(t.agent, t.task, {\n description: t.task.slice(0, 80),\n foreground: true,\n inheritContext: true,\n });\n spawnedIds.push(id);\n\n // Record each handoff\n recordAndPersist(pi, state, t.agent, t.task);\n }\n } catch (err) {\n abortSubagents(service, spawnedIds);\n throw err;\n }\n\n // Poll all concurrently, preserving completed results when one poll fails.\n // A failed poll aborts every sibling so no subagent is orphaned.\n const outcomes = await Effect.runPromise(\n Effect.all(\n spawnedIds.map((id, i) =>\n Effect.match(\n pollSubagentEffect({\n id,\n label: `${taskList[i].agent} (${i + 1}/${taskList.length})`,\n sendUpdates: false,\n service,\n signal,\n onUpdate,\n intervalMs: POLL_INTERVAL_MS,\n timeoutMs: POLL_TIMEOUT_MS,\n }),\n {\n onSuccess: (record) => ({ record }),\n onFailure: (error) => {\n abortSubagents(service, spawnedIds);\n return { error };\n },\n },\n ),\n ),\n { concurrency: 'unbounded' },\n ),\n );\n\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `All ${taskList.length} parallel subagents settled.`,\n },\n ],\n });\n\n // Aggregate results\n const parts = [`## Parallel Results (${taskList.length} tasks)\\n`];\n for (let i = 0; i < taskList.length; i++) {\n const t = taskList[i];\n const outcome = outcomes[i];\n const header = `### ${i + 1}: ${t.agent}`;\n if ('error' in outcome) {\n const message =\n outcome.error instanceof Error ? outcome.error.message : String(outcome.error);\n parts.push(header);\n parts.push(`⚠️ ${message}`);\n } else {\n const resultText = outcome.record.result ?? outcome.record.error ?? 'No output.';\n parts.push(header);\n parts.push(resultText);\n }\n }\n\n return {\n content: [{ type: 'text' as const, text: parts.join('\\n\\n') }],\n details: { subagentIds: spawnedIds },\n };\n }\n\n // --- CHAIN MODE ---\n if (mode === 'chain') {\n const taskList = params.tasks!;\n let previousResult = '';\n\n for (let i = 0; i < taskList.length; i++) {\n const t = taskList[i];\n let taskText = t.task;\n\n // Substitute {previous} placeholder with previous result.\n // A replacer function (not a string) is required: string replacements interpret\n // $&, $', $`, and $1..$99 in the replacement text, so a previous result\n // containing those sequences would silently corrupt the next task brief.\n if (i > 0 && taskText.includes('{previous}')) {\n taskText = taskText.replace(/\\{previous\\}/g, () => previousResult);\n }\n\n const id = service.spawn(t.agent, taskText, {\n description: taskText.slice(0, 80),\n foreground: true,\n inheritContext: true,\n });\n\n // Record handoff\n recordAndPersist(pi, state, t.agent, taskText);\n\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `Chain step ${i + 1}/${taskList.length}: ${t.agent} running...`,\n },\n ],\n });\n\n // Poll for completion (abort on poll failure so nothing is orphaned)\n try {\n const record = await Effect.runPromise(\n pollSubagentOrAbortEffect({\n id,\n label: `Chain step ${i + 1}: ${t.agent}`,\n sendUpdates: true,\n service,\n signal,\n onUpdate,\n intervalMs: POLL_INTERVAL_MS,\n timeoutMs: POLL_TIMEOUT_MS,\n }),\n );\n previousResult = record.result ?? record.error ?? 'No output.';\n } catch (error) {\n previousResult = `[error] ${error instanceof Error ? error.message : String(error)}`;\n break;\n }\n\n if (i < taskList.length - 1) {\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `Chain step ${i + 1}/${taskList.length}: ${t.agent} completed. Moving to next step.`,\n },\n ],\n });\n }\n }\n\n return {\n content: [{ type: 'text' as const, text: previousResult }],\n details: { subagentId: 'chain-completed' },\n };\n }\n\n // Should not reach here - all modes are handled above\n throw new Error('Unknown dispatch mode');\n } catch (err) {\n console.warn('[maestria] Subagent dispatch failed:', err);\n // Return handoff payload as structured text when dispatch fails\n const agentName = params.agent ?? params.tasks?.[0]?.agent ?? 'unknown';\n const taskDesc = params.task ?? params.tasks?.map((t) => t.task).join('; ') ?? 'unknown';\n const handoffInfo = [\n `## Subagent Handoff Required`,\n ``,\n `**From:** orchestrator`,\n `**To:** ${agentName}`,\n `**Task:** ${taskDesc}`,\n ``,\n `Subagent dispatch failed. Please delegate this work manually.`,\n ].join('\\n');\n\n return {\n content: [{ type: 'text' as const, text: handoffInfo }],\n };\n }\n },\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any); // TypeBox inferred types don't match ToolDefinition exactly\n\n // Subscribe to subagent lifecycle events for accurate state tracking.\n // These subscriptions are set up once at extension init, not on every tool call.\n // pi.events is the shared EventBus - distinct from pi.on() lifecycle hooks.\n if (pi.events) {\n const unsubStarted = pi.events.on(SUBAGENT_EVENTS.STARTED, (data: unknown) => {\n const { id, type } = data as { id: string; type: string };\n state.subagentStatus[id] = { type, status: 'running', startedAt: Date.now() };\n persistState(pi, state);\n pi.events?.emit(MAESTRIA_EVENTS.SUBAGENT_STARTED, {\n id,\n type,\n timestamp: Date.now(),\n });\n });\n\n const unsubCompleted = pi.events.on(SUBAGENT_EVENTS.COMPLETED, (data: unknown) => {\n const { id } = data as { id: string };\n const existing = state.subagentStatus[id];\n if (existing) {\n existing.status = 'completed';\n existing.completedAt = Date.now();\n }\n persistState(pi, state);\n pi.events?.emit(MAESTRIA_EVENTS.SUBAGENT_COMPLETED, {\n id,\n type: existing?.type,\n timestamp: Date.now(),\n });\n });\n\n const unsubFailed = pi.events.on(SUBAGENT_EVENTS.FAILED, (data: unknown) => {\n const { id, status } = data as { id: string; status: string };\n const existing = state.subagentStatus[id];\n if (existing) {\n existing.status = status ?? 'error';\n existing.completedAt = Date.now();\n }\n persistState(pi, state);\n pi.events?.emit(MAESTRIA_EVENTS.SUBAGENT_FAILED, {\n id,\n type: existing?.type,\n timestamp: Date.now(),\n });\n });\n\n const unsubSteered = pi.events.on(SUBAGENT_EVENTS.STEERED, (data: unknown) => {\n // Steering is informational - no status transition, but ensure\n // the agent is tracked as running if it wasn't already observed.\n const { id } = data as { id: string };\n if (!state.subagentStatus[id]) {\n state.subagentStatus[id] = { type: 'unknown', status: 'running', startedAt: Date.now() };\n }\n persistState(pi, state);\n });\n\n if (cleanups) {\n cleanups.push(unsubStarted, unsubCompleted, unsubFailed, unsubSteered);\n }\n }\n}\n","/**\n * Shared slash-command installation for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Consumed by both @maestria/omp and @maestria/pi to eliminate duplication\n * of the maestria-status/review/restore-model/handoff/review-model commands.\n *\n * @module\n */\n\nimport type { MaestriaState } from './state-core.js';\nimport { persistState, recordHandoff, renderMaestriaSummary } from './state-core.js';\nimport { cycleToReviewModel, restoreOriginalState } from './review-core.js';\nimport { MAESTRIA_EVENTS } from './subagent-utils.js';\n\n// ── Duck-typed platform interfaces ──\n\ninterface CommandsCtx {\n ui: {\n notify(msg: string): void;\n setEditorText(text: string): void;\n };\n model?: { id: string };\n modelRegistry: { getAll(): Array<{ id: string }> };\n}\n\ninterface CommandsPi {\n registerCommand(\n name: string,\n opts: {\n description: string;\n handler: (args: string, ctx: CommandsCtx) => Promise<void> | void;\n },\n ): void;\n getActiveTools(): string[];\n setActiveTools(tools: string[]): void | Promise<void>;\n setModel(model: unknown): void | Promise<void>;\n sendUserMessage(text: string, opts: { deliverAs: string }): void;\n appendEntry(type: string, data: unknown): void;\n events?: { emit(event: string, data: unknown): void };\n}\n\n/**\n * Read-only tools that let a reviewer inspect code without making changes.\n *\n * - `read`, `grep`, `find`, `ls`, `glob` - all non-destructive.\n * - Excluded: `bash`, `edit`, `write` - these can modify the filesystem.\n *\n * `glob` is included for file pattern matching even though it's not a\n * built-in Pi tool - extensions may register it, and including it is a no-op\n * if absent.\n */\nconst READ_ONLY_TOOLS = ['read', 'grep', 'find', 'ls', 'glob'];\n\nexport function installCommands(pi: CommandsPi, state: MaestriaState): void {\n pi.registerCommand('maestria-status', {\n description: 'Show current maestria session state including handoff history',\n handler: async (_args: string, ctx) => {\n const summary = renderMaestriaSummary(state);\n if (!summary) {\n ctx.ui.notify('No active maestria state to report.');\n return;\n }\n ctx.ui.setEditorText(summary);\n },\n });\n\n pi.registerCommand('review', {\n description: 'Enter review mode. Blocks destructive tools, sets read-only toolset.',\n handler: async (args: string, ctx) => {\n if (!args.trim()) {\n ctx.ui.notify('Usage: /review <target> - describe what to review');\n return;\n }\n\n // 1. Save current model and tools for later restoration\n const currentModelId = ctx.model?.id ?? null;\n const currentTools = pi.getActiveTools();\n\n // 2. Update state: mark review mode, store originals\n const updatedState: MaestriaState = {\n ...state,\n reviewMode: true,\n originalModel: currentModelId,\n originalTools: currentTools,\n };\n Object.assign(state, updatedState);\n persistState(pi, state);\n\n // 3. Switch to review model if configured\n if (state.reviewModel) {\n const switched = await cycleToReviewModel(pi, ctx, state);\n if (switched) {\n ctx.ui.notify(`Review mode: switched to ${switched}`);\n pi.events?.emit(MAESTRIA_EVENTS.REVIEW_ACTIVATED, {\n originalModel: state.originalModel,\n reviewModel: switched,\n timestamp: Date.now(),\n });\n }\n }\n\n // 4. Restrict to read-only tools\n void pi.setActiveTools(READ_ONLY_TOOLS);\n\n pi.sendUserMessage(\n [\n `[REVIEW: ${args}]`,\n '',\n `Review: ${args}. Use the reviewer prompt template.`,\n 'Read only, no edits, report findings.',\n ].join('\\n'),\n { deliverAs: 'steer' },\n );\n },\n });\n\n pi.registerCommand('restore-model', {\n description:\n 'Restore the original model and tools that were active before review mode was entered.',\n handler: async (_args: string, ctx) => {\n if (!state.reviewMode) {\n ctx.ui.notify('Not in review mode. Nothing to restore.');\n return;\n }\n const prevOriginalModel = state.originalModel;\n await restoreOriginalState(pi, ctx, state);\n persistState(pi, state);\n ctx.ui.notify('Restored original model and tools.');\n pi.events?.emit(MAESTRIA_EVENTS.REVIEW_DEACTIVATED, {\n originalModel: prevOriginalModel,\n timestamp: Date.now(),\n });\n },\n });\n\n pi.registerCommand('handoff', {\n description: 'Generate a structured handoff prompt for a new task context',\n handler: async (args: string, ctx) => {\n if (!args.trim()) {\n ctx.ui.notify('Usage: /handoff <goal> - describe the task context for handoff');\n return;\n }\n\n // Build a structured handoff document with 7 fields\n const goal = args.trim();\n const handoffPrompt = [\n '**Goal:** ' + goal,\n '',\n '**Context:**',\n '- Mode: ' + (state.mode ?? 'none'),\n '- Active task: ' + (state.activeTask || 'none'),\n '- Specialists delegated: ' +\n ((state.specialistsDelegated?.length ?? 0) > 0\n ? state.specialistsDelegated.join(', ')\n : 'none'),\n '- Recent handoffs: ' + (state.handoffHistory?.length ?? 0) + ' entries',\n '- Files modified: ' +\n ((state.filesModified?.length ?? 0) > 0 ? state.filesModified.join(', ') : 'none'),\n '',\n '**Requirements:**',\n '(fill in specific requirements)',\n '',\n '**Known problems:**',\n (state.blockers?.length ?? 0) > 0\n ? state.blockers.map((b: string) => '- ' + b).join('\\n')\n : '(no known problems documented)',\n '',\n '**Assumptions documented:**',\n '(document assumptions made, tagged [inferred] where uncertain)',\n '',\n '**Success criteria:**',\n '(fill in how to verify completion)',\n '',\n '**Next step:**',\n '(fill in what happens after this task)',\n '',\n '---',\n 'Complete the fields above before sending.',\n ].join('\\n');\n\n // Record in state\n Object.assign(state, recordHandoff(state, 'current', 'next', goal));\n\n // Persist state\n persistState(pi, state);\n\n // Send as user message with steer delivery\n pi.sendUserMessage(handoffPrompt, { deliverAs: 'steer' });\n },\n });\n\n pi.registerCommand('review-model', {\n description: 'Set which model to use when entering review mode',\n handler: async (args: string, ctx) => {\n if (!args.trim()) {\n ctx.ui.notify('Usage: /review-model <model-id>');\n return;\n }\n const modelId = args.trim();\n const models = ctx.modelRegistry.getAll();\n const model = models.find((m) => m.id === modelId);\n if (!model) {\n ctx.ui.notify(\n `Unknown model: \"${modelId}\". Available: ${models.map((m) => m.id).join(', ')}`,\n );\n return;\n }\n state.reviewModel = modelId;\n persistState(pi, state);\n ctx.ui.notify(`Review model set to: ${modelId}`);\n },\n });\n}\n","import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { installCommands as installCommandsCore } from '@maestria/shared-pi/commands-core';\n\nexport function installCommands(pi: ExtensionAPI, state: MaestriaState): void {\n installCommandsCore(pi as never, state);\n}\n","/**\n * Shared tool interceptor utilities for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Imported by both @maestria/omp and @maestria/pi to eliminate duplication.\n *\n * @module\n */\n\n/**\n * Dangerous bash command patterns that should always be blocked,\n * regardless of mode or specialist role.\n */\nexport const DANGEROUS_PATTERNS = [\n /rm\\s+-rf\\s+\\//,\n /dd\\s+if=/,\n />\\s*\\/dev\\/sd/,\n /chmod\\s+-R\\s+777\\s+\\//,\n /mkfs\\.\\w+/,\n /:(){ :\\|:& };:/,\n />\\s*\\/etc\\/(passwd|shadow|sudoers)/,\n /\\beval\\b/,\n /wget\\s+-O\\s*-\\s*\\|\\s*(bash|sh)/,\n /curl\\s+.*\\|\\s*(bash|sh)/,\n /crontab\\s+-r/,\n];\n\n/**\n * Read-only bash command prefixes allowed for the orchestrator's recon and\n * verification. Anything not matching - or chaining into a mutation - is\n * blocked; mutations belong to specialists.\n */\nconst READ_ONLY_BASH_PREFIX =\n /^(ls|cat|head|tail|git status|git diff|git log|git branch|find|grep|rg|pnpm test|npm test|pwd|which)\\b/;\n\n/**\n * True when a bash command performs no mutation.\n *\n * A naive prefix check is bypassable - `git status && git checkout .` or\n * `ls; rm -rf dist` both pass a prefix-only match - so every segment of a\n * chained command (`;`, `&&`, `||`, `|`, or newline) must itself be\n * read-only, and command substitution (`$(...)`, backticks) and output\n * redirection (`>` / `>>`) are rejected because they can hide a mutation\n * behind a read-only prefix. `2>&1`-style fd redirects are allowed (they\n * don't write).\n */\nexport function isReadOnlyBashCommand(rawCommand: string): boolean {\n const command = rawCommand.trim();\n if (command.includes('$(') || command.includes('`')) return false;\n // Strip `2>&1`-style fd redirects first so the `&` inside them is not\n // mistaken for a command separator and the `>` is not counted as output\n // redirection.\n const withoutFdRedirects = command.replace(/\\d?>&[12]/g, '');\n if (withoutFdRedirects.includes('>')) return false;\n return withoutFdRedirects\n .split(/[\\n;&|]+/)\n .every((segment) => READ_ONLY_BASH_PREFIX.test(segment.trim()));\n}\n","import {\n isToolCallEventType,\n type ExtensionAPI,\n type ToolCallEvent,\n type ExtensionContext,\n} from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { DANGEROUS_PATTERNS, isReadOnlyBashCommand } from '@maestria/shared-pi/tools-core';\nimport { persistState, recordFileModified, recordFileRead } from '@/state.js';\n\nexport function installToolInterceptors(pi: ExtensionAPI, state: MaestriaState): void {\n pi.on('tool_call', async (event: ToolCallEvent, ctx: ExtensionContext) => {\n if (!event || !event.toolName) return;\n\n // ── Orchestrator routing enforcement ──\n // When a maestria workflow mode is active, restrict the root session\n // (orchestrator) to read-only recon + delegation. The orchestrator may\n // read, search, and inspect to route and verify, but mutations (edit,\n // write, mutation-capable bash) belong to specialists. Subagent sessions\n // are detected by the absence of the pi-subagents 'subagent' tool\n // (stripped by applyRecursionGuard in child sessions).\n if (state.mode !== null && pi.getActiveTools().includes('subagent')) {\n const isMutation =\n isToolCallEventType('edit', event) ||\n isToolCallEventType('write', event) ||\n isToolCallEventType('patch', event) ||\n event.toolName === 'bash';\n if (isMutation) {\n // Read-only bash (ls, git status, git diff, tests) is allowed so the\n // orchestrator can verify state; mutation-capable commands still\n // belong to specialists.\n if (event.toolName === 'bash') {\n const input = event.input as { command?: unknown } | undefined;\n const command = typeof input?.command === 'string' ? input.command : '';\n if (isReadOnlyBashCommand(command)) {\n return undefined;\n }\n }\n return {\n block: true,\n reason:\n `Tool '${event.toolName}' is blocked for the orchestrator. ` +\n `Use 'maestria_subagent' to delegate mutations to specialists.`,\n };\n }\n }\n\n // Block destructive tools in review mode\n if (state.reviewMode) {\n if (\n isToolCallEventType('edit', event) ||\n isToolCallEventType('write', event) ||\n isToolCallEventType('bash', event)\n ) {\n return {\n block: true,\n reason: 'Review mode is active. Report findings, do not edit.',\n };\n }\n }\n\n // Block dangerous bash patterns regardless of mode\n if (isToolCallEventType('bash', event)) {\n if (!event.input || typeof event.input !== 'object') return undefined;\n const command = event.input.command;\n if (command) {\n for (const pattern of DANGEROUS_PATTERNS) {\n if (pattern.test(command)) {\n if (ctx.hasUI) {\n const confirmed = await ctx.ui.confirm(\n 'Dangerous Pattern Detected',\n `This command matches a dangerous pattern:\\n${command}\\nProceed?`,\n );\n if (confirmed) return undefined;\n }\n return {\n block: true,\n reason: `Command matches dangerous pattern: ${pattern}`,\n };\n }\n }\n }\n }\n\n // Record file access for session state (ADR-PI-002: tool_call maintains file tracking).\n // Only record when the call is allowed to proceed.\n let tracked = false;\n if (isToolCallEventType('read', event)) {\n const path = event.input?.path;\n if (typeof path === 'string' && path) {\n Object.assign(state, recordFileRead(state, path));\n tracked = true;\n }\n } else if (isToolCallEventType('edit', event) || isToolCallEventType('write', event)) {\n const path = event.input?.path;\n if (typeof path === 'string' && path) {\n Object.assign(state, recordFileModified(state, path));\n tracked = true;\n }\n }\n if (tracked) {\n persistState(pi, state);\n }\n\n return undefined; // allow\n });\n}\n","import type {\n ExtensionAPI,\n ExtensionContext,\n SessionStartEvent,\n SessionTreeEvent,\n} from '@earendil-works/pi-coding-agent';\nimport { createInitialState } from '@/state.js';\nimport type { MaestriaState } from '@/state.js';\nimport { deploySpecialistAgents } from '@/agents.js';\nimport { installModeCommands, installModeAutoDetect } from '@/modes.js';\nimport { createModePromptHandler } from '@/rules.js';\nimport { installCompactionHandlers } from '@/compaction.js';\nimport { installSubagentTool } from '@/subagent.js';\nimport { installCommands } from '@/commands.js';\nimport { installToolInterceptors } from '@/tools.js';\n\ninterface PersistedStateEntry {\n type: string;\n customType?: string;\n data?: unknown;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null;\n}\n\nfunction currentSessionEntries(ctx: ExtensionContext): PersistedStateEntry[] | null {\n // getBranch() is the public current-session view and avoids restoring state\n // from a sibling branch in the same session tree. Never fall back to\n // getEntries(), which spans the entire session tree.\n const sessionManager = ctx?.sessionManager;\n if (typeof sessionManager?.getBranch !== 'function') return null;\n\n const branch = sessionManager.getBranch();\n return Array.isArray(branch) ? (branch as PersistedStateEntry[]) : null;\n}\n\nfunction restoreStateFromSession(state: MaestriaState, ctx: ExtensionContext): void {\n const next = createInitialState();\n const entries = currentSessionEntries(ctx);\n\n if (!entries) {\n const mutableState = state as unknown as Record<string, unknown>;\n for (const key of Object.keys(mutableState)) delete mutableState[key];\n Object.assign(state, next);\n return;\n }\n\n for (let i = entries.length - 1; i >= 0; i--) {\n const entry = entries[i];\n if (entry.type === 'custom' && entry.customType === 'maestria_state') {\n if (isRecord(entry.data)) Object.assign(next, entry.data);\n break;\n }\n }\n\n const mutableState = state as unknown as Record<string, unknown>;\n for (const key of Object.keys(mutableState)) delete mutableState[key];\n Object.assign(state, next);\n}\n\nexport default function (pi: ExtensionAPI): void {\n const state = createInitialState();\n const cleanups: Array<() => void> = [];\n\n // Install mode commands: /fein, /sonar, /blitz\n installModeCommands(pi, state);\n installModeAutoDetect(pi, state);\n\n // Inject mode prompt when a workflow mode is active\n const handleModePrompt = createModePromptHandler(state);\n\n pi.on('before_agent_start', (event, ctx) => {\n return handleModePrompt(event, ctx);\n });\n\n // Deploy specialist agent files for pi-subagents discovery\n pi.on('session_start', (_event: SessionStartEvent, ctx) => {\n deploySpecialistAgents(ctx);\n\n // Restore persisted state on session start (reload/resume/fork)\n restoreStateFromSession(state, ctx);\n });\n\n // Rehydrate state when navigating the session tree to a different branch\n pi.on('session_tree', (_event: SessionTreeEvent, ctx) => {\n restoreStateFromSession(state, ctx);\n });\n\n // Install compaction preservation handlers\n installCompactionHandlers(pi, state);\n\n // Install orchestration hooks: subagent tool and commands\n installSubagentTool(pi, state, cleanups);\n installCommands(pi, state);\n\n // Cleanup subscriptions on shutdown\n pi.on('session_shutdown', () => {\n for (const cleanup of cleanups) cleanup();\n cleanups.length = 0;\n });\n\n // Install tool call interceptors for review mode and dangerous patterns\n installToolInterceptors(pi, state);\n}\n"],"mappings":"uaA6DA,SAAgB,GAAoC,CAClD,MAAO,CACL,KAAM,KACN,WAAY,GACZ,kBAAmB,GACnB,qBAAsB,CAAC,EACvB,SAAU,CAAC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D,EAAG,GAAG,iBAAkB,EAA2B,IAAQ,CACzD,GAAuB,CAAG,EAG1B,EAAwB,EAAO,CAAG,CACpC,CAAC,EAGD,EAAG,GAAG,gBAAiB,EAA0B,IAAQ,CACvD,EAAwB,EAAO,CAAG,CACpC,CAAC,EAGD,GAA0B,EAAI,CAAK,EAGnC,GAAoB,EAAI,EAAO,CAAQ,EACvC,EAAgB,EAAI,CAAK,EAGzB,EAAG,GAAG,uBAA0B,CAC9B,IAAK,IAAM,KAAW,EAAU,EAAQ,EACxC,EAAS,OAAS,CACpB,CAAC,EAGD,GAAwB,EAAI,CAAK,CACnC"}
|
|
1
|
+
{"version":3,"file":"extension.mjs","names":["restoreOriginalState","restoreCore","deploySpecialistAgents","__dirname","MODE_KEYWORDS","MODE_MARKERS","SHARED_KEYWORDS","SHARED_MARKERS","sharedDetectMode","sharedStripKeyword","installModeAutoDetect","installModeCommands","__dirname","COMMANDS_DIR","installCompactionHandlers","installCommands","restoreOriginalState"],"sources":["../../shared/pi/src/state-core.ts","../../shared/pi/src/review-core.ts","../src/state/review.ts","../../shared/pi/src/subagent-utils.ts","../../shared/pi/src/agent-deployment.ts","../src/agents.ts","../../shared/mode/src/index.ts","../../shared/pi/src/modes-core.ts","../src/modes.ts","../src/rules.ts","../../shared/pi/src/compaction-core.ts","../src/compaction.ts","../src/subagent-polling.ts","../src/subagent.ts","../../shared/pi/src/commands-core.ts","../src/commands.ts","../../shared/pi/src/tools-core.ts","../src/tools.ts","../src/extension.ts"],"sourcesContent":["/**\n * Shared state management for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Provides shared state-management types, transforms, persistence, and rendering\n * consumed directly by @maestria/omp and @maestria/pi.\n *\n * @module\n */\n\n// ── Types ──\n\nexport type ModeKeyword = 'fein' | 'sonar' | 'blitz';\n\nexport const HANDOFF_HISTORY_CAP = 5;\nexport const FILE_HISTORY_CAP = 10;\n\nexport interface HandoffEntry {\n from: string;\n to: string;\n task: string;\n timestamp: number;\n}\n\nexport interface SubagentStatusInfo {\n type: string;\n status: string;\n startedAt: number;\n completedAt?: number;\n}\n\n/**\n * Mirror of the host platform's native goal (e.g. OMP goal mode).\n *\n * Platform-agnostic by design: only the objective text and status are\n * carried so shared state stays free of platform-specific types.\n */\nexport interface NativeGoalMirror {\n objective: string;\n status: string;\n}\n\nexport interface MaestriaState {\n mode: ModeKeyword | null;\n activeTask: string;\n completionPromise: string;\n specialistsDelegated: string[];\n blockers: string[];\n filesModified: string[];\n filesRead: string[];\n handoffHistory: HandoffEntry[];\n reviewMode: boolean;\n originalModel: string | null;\n originalTools: string[] | null;\n subagentStatus: Record<string, SubagentStatusInfo>;\n reviewModel: string | null;\n nativeGoal: NativeGoalMirror | null;\n}\n\n// ── Transforms ──\n\nexport function createInitialState(): MaestriaState {\n return {\n mode: null,\n activeTask: '',\n completionPromise: '',\n specialistsDelegated: [],\n blockers: [],\n filesModified: [],\n filesRead: [],\n handoffHistory: [],\n reviewMode: false,\n originalModel: null,\n originalTools: null,\n subagentStatus: {},\n reviewModel: null,\n nativeGoal: null,\n };\n}\n\nfunction prependDeduped(files: string[], path: string, cap: number): string[] {\n const filtered = files.filter((f) => f !== path);\n return [path, ...filtered].slice(0, cap);\n}\n\nexport function recordHandoff(\n state: MaestriaState,\n from: string,\n to: string,\n task: string,\n): MaestriaState {\n const entry: HandoffEntry = { from, to, task, timestamp: Date.now() };\n const history = [entry, ...state.handoffHistory].slice(0, HANDOFF_HISTORY_CAP);\n return { ...state, handoffHistory: history };\n}\n\nexport function recordFileModified(state: MaestriaState, path: string): MaestriaState {\n return { ...state, filesModified: prependDeduped(state.filesModified, path, FILE_HISTORY_CAP) };\n}\n\nexport function recordFileRead(state: MaestriaState, path: string): MaestriaState {\n return { ...state, filesRead: prependDeduped(state.filesRead, path, FILE_HISTORY_CAP) };\n}\n\nexport function recordSpecialistDelegated(state: MaestriaState, name: string): MaestriaState {\n if (state.specialistsDelegated.includes(name)) return state;\n return { ...state, specialistsDelegated: [...state.specialistsDelegated, name] };\n}\n\nexport function recordSubagentStatus(\n state: MaestriaState,\n id: string,\n info: SubagentStatusInfo,\n): MaestriaState {\n return { ...state, subagentStatus: { ...state.subagentStatus, [id]: info } };\n}\n\nexport function setReviewMode(state: MaestriaState, active: boolean): MaestriaState {\n return { ...state, reviewMode: active };\n}\n\nexport function exitReviewMode(state: MaestriaState): {\n state: MaestriaState;\n originalModel: string | null;\n originalTools: string[] | null;\n} {\n return {\n state: {\n ...state,\n reviewMode: false,\n originalModel: null,\n originalTools: null,\n },\n originalModel: state.originalModel,\n originalTools: state.originalTools,\n };\n}\n\n// ── Persistence ──\n\nexport function persistState(\n pi: { appendEntry: (type: string, data: unknown) => void },\n state: MaestriaState,\n): void {\n pi.appendEntry('maestria_state', { ...state });\n}\n\n// ── Render ──\n\nexport function renderMaestriaSummary(state: MaestriaState): string {\n const parts: string[] = [];\n\n if (state.mode) {\n parts.push(`**Mode:** ${state.mode.toUpperCase()}`);\n }\n\n if (state.reviewModel) {\n parts.push(`**Review Model:** ${state.reviewModel}`);\n }\n\n if (state.activeTask) {\n parts.push(`**Goal:** ${state.activeTask}`);\n }\n\n if (state.nativeGoal) {\n parts.push(`**Native Goal:** ${state.nativeGoal.objective} (${state.nativeGoal.status})`);\n }\n\n if (state.completionPromise) {\n parts.push(`**Completion Promise:** ${state.completionPromise}`);\n }\n\n if (state.specialistsDelegated.length > 0) {\n parts.push(`**Specialists Delegated:** ${state.specialistsDelegated.join(', ')}`);\n }\n\n if (state.blockers.length > 0) {\n parts.push('**Blockers:**');\n for (const blocker of state.blockers) {\n parts.push(`- ${blocker}`);\n }\n }\n\n const fileSubs: string[] = [];\n if (state.filesModified.length > 0) {\n fileSubs.push(`**Modified:** ${state.filesModified.join(', ')}`);\n }\n if (state.filesRead.length > 0) {\n fileSubs.push(`**Read:** ${state.filesRead.join(', ')}`);\n }\n if (fileSubs.length > 0) {\n parts.push(`**Files:** ${fileSubs.join('; ')}`);\n }\n\n if (state.handoffHistory.length > 0) {\n parts.push('**Recent Handoffs:**');\n for (const entry of state.handoffHistory) {\n parts.push(`- ${entry.from} → ${entry.to}: ${entry.task}`);\n }\n }\n\n return parts.join('\\n\\n');\n}\n","/**\n * Shared review-mode orchestration for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Consumed by both @maestria/omp and @maestria/pi to eliminate duplication\n * of review-mode model/tool restoration and switching.\n *\n * @module\n */\n\nimport type { MaestriaState } from './state-core.js';\nimport { exitReviewMode } from './state-core.js';\n\n// ── Duck-typed platform interfaces ──\n\ninterface ReviewPi {\n setActiveTools(tools: string[]): void | Promise<void>;\n setModel(model: unknown): void | Promise<void>;\n}\n\ninterface ReviewCtx {\n modelRegistry: { getAll(): Array<{ id: string }> };\n ui: { notify(msg: string): void };\n}\n\nexport async function restoreOriginalState(\n pi: ReviewPi,\n ctx: ReviewCtx,\n state: MaestriaState,\n): Promise<void> {\n const { state: clearedState, originalModel, originalTools } = exitReviewMode(state);\n\n if (originalTools && originalTools.length > 0) {\n await pi.setActiveTools(originalTools);\n }\n\n if (originalModel) {\n try {\n const models = ctx.modelRegistry.getAll();\n const model = models.find((m: { id: string }) => m.id === originalModel);\n if (model) {\n await pi.setModel(model);\n }\n } catch {\n // Best-effort: model restoration is non-critical\n }\n }\n\n Object.assign(state, clearedState);\n}\n\nexport async function cycleToReviewModel(\n pi: ReviewPi,\n ctx: ReviewCtx,\n state: MaestriaState,\n): Promise<string | null> {\n const reviewModel = state.reviewModel;\n if (!reviewModel) {\n return null;\n }\n try {\n const models = ctx.modelRegistry.getAll();\n const model = models.find((m) => m.id === reviewModel);\n if (model) {\n await pi.setModel(model);\n return reviewModel;\n } else {\n ctx.ui.notify(`Review model \"${reviewModel}\" not found in registry, staying on current.`);\n return null;\n }\n } catch {\n ctx.ui.notify(`Could not switch to review model \"${reviewModel}\", staying on current.`);\n return null;\n }\n}\n","import type {\n ExtensionAPI,\n ExtensionCommandContext,\n ExtensionContext,\n} from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@maestria/shared-pi/state-core';\nimport {\n cycleToReviewModel as cycleCore,\n restoreOriginalState as restoreCore,\n} from '@maestria/shared-pi/review-core';\n\nexport async function restoreOriginalState(\n pi: ExtensionAPI,\n ctx: ExtensionContext,\n state: MaestriaState,\n): Promise<void> {\n await restoreCore(pi as never, ctx as never, state);\n}\n\nexport async function cycleToReviewModel(\n pi: ExtensionAPI,\n ctx: ExtensionCommandContext,\n state: MaestriaState,\n): Promise<string | null> {\n return cycleCore(pi as never, ctx as never, state);\n}\n","/**\n * Shared subagent validation utilities for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Imported by both @maestria/omp and @maestria/pi to eliminate duplication.\n *\n * @module\n */\n\n/** Maestria cross-extension event names. */\nexport const MAESTRIA_EVENTS = {\n REVIEW_ACTIVATED: 'maestria:review:activated',\n REVIEW_DEACTIVATED: 'maestria:review:deactivated',\n SUBAGENT_STARTED: 'maestria:subagent:started',\n SUBAGENT_COMPLETED: 'maestria:subagent:completed',\n SUBAGENT_FAILED: 'maestria:subagent:failed',\n} as const;\n\n/** The set of specialist agent types maestria supports. */\nexport const ALLOWED_AGENTS = [\n 'adventurer',\n 'architect',\n 'builder',\n 'diagnose',\n 'planner',\n 'reviewer',\n 'writer',\n] as const;\n\n/** A valid specialist agent name. */\nexport type AllowedAgent = (typeof ALLOWED_AGENTS)[number];\n\n/** The 7-field handoff contract used in delegation. */\nexport const HANDOFF_FIELDS = [\n 'Goal',\n 'Context',\n 'Requirements',\n 'Known problems',\n 'Assumptions documented',\n 'Success criteria',\n 'Next step',\n] as const;\n\n/** Result of validating a handoff document against the contract fields. */\nexport interface HandoffValidation {\n valid: boolean;\n errors: string[];\n}\n\n/**\n * Asserts that `agent` is a known maestria specialist.\n * @throws {Error} if the agent name is not in ALLOWED_AGENTS.\n */\nexport function assertValidAgent(agent: string): asserts agent is AllowedAgent {\n if (!ALLOWED_AGENTS.includes(agent as AllowedAgent)) {\n throw new Error(`Unknown agent: \"${agent}\". Allowed: ${ALLOWED_AGENTS.join(', ')}`);\n }\n}\n\n/**\n * Asserts that `task` is a non-empty, non-whitespace string.\n * @throws {Error} with the given label if task is falsy or all-whitespace.\n */\nexport function assertNonEmptyTask(\n task: string | undefined,\n label: string,\n): asserts task is string {\n if (!task || !task.trim()) {\n throw new Error(label);\n }\n}\n\n/**\n * Validates that a handoff document contains all required fields\n * with non-empty content. Each field is expected in markdown bold format:\n * `**Field:** content`.\n */\nexport function validateHandoff(handoff: string): HandoffValidation {\n const errors: string[] = [];\n for (const field of HANDOFF_FIELDS) {\n // Match field header and capture content up to the next field or end of string.\n // This avoids false positives when an empty field is followed by another field's `**` header.\n const pattern = `\\\\*\\\\*${field}:\\\\*\\\\*([\\\\s\\\\S]*?)(?=\\\n\\\\*\\\\*|$)`;\n const match = handoff.match(new RegExp(pattern, 'i'));\n if (!match || !match[1] || !match[1].trim()) {\n errors.push(`Missing or empty field: \"${field}\"`);\n }\n }\n return { valid: errors.length === 0, errors };\n}\n","/**\n * Shared agent deployment logic for Maestria platform packages.\n *\n * Both @maestria/omp and @maestria/pi deploy specialist agent .md files\n * to their respective platform agent directories. This module eliminates\n * the duplication between the two packages.\n *\n * @module\n */\n\nimport { readFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { join } from 'node:path';\nimport { ALLOWED_AGENTS } from './subagent-utils.js';\n\n/**\n * Deploy bundled specialist agent .md files to the given destination directory.\n *\n * Only creates files that don't already exist - never overwrites user-customized agents.\n *\n * @param agentsSrc - Path to the source directory containing specialist .md files\n * @param agentsDest - Absolute path to the platform's agents destination directory\n * (e.g. `join(homedir(), '.omp', 'agent', 'agents')`)\n * @returns The number of agents newly deployed\n */\nexport function deploySpecialistAgents(agentsSrc: string, agentsDest: string): number {\n if (!existsSync(agentsSrc)) {\n console.warn('[maestria] Agents source directory not found:', agentsSrc);\n return 0;\n }\n\n try {\n mkdirSync(agentsDest, { recursive: true });\n } catch {\n console.warn('[maestria] Could not create agents directory:', agentsDest);\n return 0;\n }\n\n let deployed = 0;\n for (const name of ALLOWED_AGENTS) {\n const srcFile = join(agentsSrc, `${name}.md`);\n const destFile = join(agentsDest, `${name}.md`);\n\n if (!existsSync(srcFile)) {\n console.warn(`[maestria] Agent source not found: ${name}.md`);\n continue;\n }\n\n if (existsSync(destFile)) continue;\n\n try {\n const content = readFileSync(srcFile, 'utf-8');\n writeFileSync(destFile, content, 'utf-8');\n deployed++;\n } catch (err) {\n console.warn(`[maestria] Failed to deploy agent ${name}:`, err);\n }\n }\n\n if (deployed > 0) {\n console.log(`[maestria] Deployed ${deployed} specialist agents to ${agentsDest}`);\n }\n\n return deployed;\n}\n","import { homedir } from 'node:os';\nimport { join, dirname } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { deploySpecialistAgents as deployAgents } from '@maestria/shared-pi/agent-deployment';\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\nconst AGENTS_SRC = join(__dirname, '..', 'agents');\n\n/**\n * Deploy bundled specialist agent .md files to the pi-subagents agents directory.\n *\n * pi-subagents discovers agent types from ~/.pi/agent/agents/*.md on every\n * registry.reload() call (which fires automatically on each tool invocation).\n * This function ensures the files are in place before the first subagent dispatch.\n *\n * Only creates files that don't already exist - never overwrites user-customized agents.\n */\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport function deploySpecialistAgents(_ctx?: unknown): void {\n deployAgents(AGENTS_SRC, join(homedir(), '.pi', 'agent', 'agents'));\n}\n","/**\n * Private runtime-neutral shared mode mechanics.\n *\n * Pure TypeScript - no host SDKs, no filesystem APIs.\n * Consolidates the duplicated pure logic from:\n * - `packages/opencode/src/modes/index.ts`\n * - `packages/shared/pi/src/modes-core.ts`\n *\n * Consumers (OpenCode, shared-pi) delegate pure detection,\n * stripping, and mode-section extraction to this module while\n * keeping host-specific concerns (lazy file loading, session\n * state, command registration) locally.\n *\n * @module @maestria/shared-mode\n */\n\n// ── Constants ──\n\nexport const MODE_KEYWORDS = ['fein', 'sonar', 'blitz'] as const;\nexport type ModeKeyword = (typeof MODE_KEYWORDS)[number];\n\n/**\n * Backward-compatible alias used by OpenCode (`VALID_KEYWORDS`).\n */\nexport const VALID_KEYWORDS: readonly ModeKeyword[] = MODE_KEYWORDS;\n\nexport const MODE_MARKERS: Record<ModeKeyword, string> = {\n fein: '[MODE: fein]',\n sonar: '[MODE: sonar]',\n blitz: '[MODE: blitz]',\n};\n\n/**\n * Priority mapping for mode keyword restrictiveness.\n * Higher number = more restrictive = wins when multiple keywords are present.\n * fein (3): full pipeline with mandatory gates\n * sonar (2): research only, no code\n * blitz (1): fast implementation, skip optional ceremony; required review remains\n */\nexport const MODE_PRIORITY: Record<ModeKeyword, number> = {\n fein: 3,\n sonar: 2,\n blitz: 1,\n};\n\n// ── Types ──\n\n/**\n * Result of pure mode detection (no prompt/marker).\n * Host wrappers add prompt/marker and stripped text as needed.\n */\nexport interface ModeDetectPure {\n mode: ModeKeyword;\n keyword: string;\n index: number;\n}\n\n// ── Pure helpers ──\n\n/**\n * Regex matching fenced code blocks (```) and inline backtick spans (`).\n * Unclosed fenced blocks (``` without closing ```) are intentionally not\n * excluded - the regex requires matching fences. This is the accepted\n * false-positive behavior documented in ADR-OC-003.\n */\nexport const CODE_BLOCK_RE: RegExp = /```[\\s\\S]*?```|`[^`]*`/g;\n\nexport function findCodeBlockRanges(text: string): Array<[number, number]> {\n const ranges: Array<[number, number]> = [];\n let match: RegExpExecArray | null;\n // Reset lastIndex in case callers reuse the exported regex\n CODE_BLOCK_RE.lastIndex = 0;\n while ((match = CODE_BLOCK_RE.exec(text)) !== null) {\n ranges.push([match.index, match.index + match[0].length]);\n }\n return ranges;\n}\n\nexport function isInRanges(index: number, ranges: Array<[number, number]>): boolean {\n return ranges.some(([start, end]) => index >= start && index < end);\n}\n\n/**\n * Extract the `## MODE:` section from a command file's content.\n * Mirrors the logic in both original modules: find `## MODE:`, slice\n * from there, trim trailing whitespace, append a single newline; if\n * no marker is found, normalize trailing whitespace the same way.\n */\nexport function extractModeSection(content: string): string {\n const modeIdx = content.indexOf('## MODE:');\n if (modeIdx !== -1) {\n return content.slice(modeIdx).replace(/\\s+$/, '') + '\\n';\n }\n return content.replace(/\\s+$/, '') + '\\n';\n}\n\nfunction escapeRegExp(value: string): string {\n return value.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\nfunction buildKeywordRegex(keyword: string): RegExp {\n return new RegExp(`\\\\b${escapeRegExp(keyword)}\\\\b`, 'gi');\n}\n\nfunction isModeKeyword(value: string): value is ModeKeyword {\n return (MODE_KEYWORDS as readonly string[]).includes(value);\n}\n\nexport function getModeMarker(mode: string): string {\n if (isModeKeyword(mode)) return MODE_MARKERS[mode];\n return '';\n}\n\n// ── Detection ──\n\n/**\n * Detect a workflow mode keyword in the given text.\n *\n * Rules (per ADR-OC-003):\n * - Word-boundary regex matching (`\\bfein\\b`, etc.), case-insensitive\n * - Most restrictive match wins (fein > sonar > blitz) regardless of position\n * - Disabled keywords (case-insensitive) are ignored\n * - Matches inside fenced code blocks (```) and inline backticks (`) are ignored\n * - Unclosed fences are not excluded (accepted false-positive)\n *\n * Returns the pure detection result (mode, matched keyword text, index) or null.\n */\nexport function detectMode(text: string, disabled?: Set<string>): ModeDetectPure | null {\n if (!text) return null;\n const codeRanges = findCodeBlockRanges(text);\n const normalizedDisabled = disabled\n ? new Set(Array.from(disabled).map((k) => k.toLowerCase()))\n : undefined;\n let best: ModeDetectPure | null = null;\n\n for (const keyword of MODE_KEYWORDS) {\n if (normalizedDisabled?.has(keyword)) continue;\n const regex = buildKeywordRegex(keyword);\n let match: RegExpExecArray | null;\n while ((match = regex.exec(text)) !== null) {\n if (isInRanges(match.index, codeRanges)) continue;\n if (best === null || MODE_PRIORITY[keyword] > MODE_PRIORITY[best.mode]) {\n best = { mode: keyword, keyword: match[0], index: match.index };\n }\n }\n }\n\n return best;\n}\n\n/**\n * Remove the matched keyword from the text, cleaning up a trailing colon\n * and collapsing double spaces.\n *\n * Mirrors both originals: strip a leading colon and surrounding whitespace\n * after the keyword, then collapse double spaces and trim.\n */\nexport function stripKeyword(text: string, result: { index: number; keyword: string }): string {\n const before = text.slice(0, result.index);\n const after = text.slice(result.index + result.keyword.length);\n const cleaned = after.replace(/^:\\s*/, '');\n return (before + cleaned).replace(/ {2,}/g, ' ').trim();\n}\n","/**\n * Shared mode constants and utilities for Maestria Pi-family packages.\n *\n * Delegates pure mechanics to `@maestria/shared-mode` while preserving\n * host-specific concerns: lazy prompt loading from `commandsDir`,\n * session-state side effects, and platform handler factories.\n *\n * @module\n */\n\nimport { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport {\n MODE_KEYWORDS as SHARED_KEYWORDS,\n MODE_MARKERS as SHARED_MARKERS,\n extractModeSection,\n detectMode as sharedDetectMode,\n stripKeyword as sharedStripKeyword,\n} from '@maestria/shared-mode';\nimport type { ModeKeyword as SharedModeKeyword } from '@maestria/shared-mode';\nimport type { MaestriaState } from './state-core.js';\n\n// ── Constants (re-exported from shared-mode to preserve public API) ──\n\nexport const MODE_KEYWORDS = SHARED_KEYWORDS;\nexport const MODE_CLEAR_COMMAND = 'mode-clear';\nexport type ModeKeyword = SharedModeKeyword;\n\nexport const MODE_MARKERS: Record<ModeKeyword, string> = SHARED_MARKERS;\n\n// ── Prompt loading ──\n\n/** Lazily cached mode prompts - shared across Pi-family. */\nconst _promptCache: Partial<Record<ModeKeyword, string>> = {};\n\n/**\n * Load and cache a mode prompt from a commands directory.\n * Delegates `## MODE:` extraction to the neutral shared-mode helper.\n */\nexport function loadModePrompt(name: string, commandsDir: string): string {\n const content = readFileSync(resolve(commandsDir, `${name}.md`), 'utf-8');\n return extractModeSection(content);\n}\n\n/**\n * Get the full mode prompt (marker + body) for a keyword, loading from\n * the given commands directory on first access.\n */\nexport function getModePrompt(keyword: ModeKeyword, commandsDir: string): string {\n if (!(keyword in _promptCache)) {\n try {\n _promptCache[keyword] = loadModePrompt(keyword, commandsDir);\n } catch (e) {\n console.warn(`[maestria] Failed to load mode prompt \"${keyword}\":`, e);\n _promptCache[keyword] = '';\n }\n }\n return `${MODE_MARKERS[keyword]}\\n\\n${_promptCache[keyword]}`;\n}\n\n// ── Keyword detection ──\n\n/** Result of detecting a mode keyword in text. */\nexport interface ModeDetectResult {\n /** The detected keyword. */\n keyword: ModeKeyword;\n /** The text with the keyword stripped and trimmed. */\n strippedText: string;\n /** The full mode prompt (marker + body). */\n prompt: string;\n}\n\n/**\n * Detect a mode keyword (fein/sonar/blitz) in text as a whole word,\n * case-insensitive. Delegates pure detection (including code-block\n * exclusion, word-boundary, priority, and case-insensitivity) to\n * `@maestria/shared-mode`. The accepted unclosed-fence behavior\n * (ADR-OC-003) is preserved via the shared helper.\n *\n * Lazy prompt loading remains host-specific (reads `commandsDir`).\n * Optional `disabled` set mirrors OpenCode's disabled-keyword support.\n */\nexport function detectModeInText(\n text: string,\n commandsDir: string,\n disabled?: Set<string>,\n): ModeDetectResult | null {\n if (!text) return null;\n\n const pure = sharedDetectMode(text, disabled);\n if (pure === null) return null;\n\n const strippedText = sharedStripKeyword(text, pure);\n\n return {\n keyword: pure.mode,\n strippedText,\n prompt: getModePrompt(pure.mode, commandsDir),\n };\n}\n\n/**\n * Build the final text to send to the LLM: prompt + stripped text.\n * If strippedText is empty, returns just the prompt.\n */\nexport function buildModeText(prompt: string, strippedText: string): string {\n return strippedText ? `${prompt}\\n\\n${strippedText}` : prompt;\n}\n\n// ── Platform handler factories ──\n\n/**\n * Install an input event handler that detects mode keywords (fein/sonar/blitz)\n * in user input, strips them, and injects the mode prompt.\n */\nexport function installModeAutoDetect(\n onInput: (handler: (event: unknown, ctx: unknown) => unknown) => void,\n state: MaestriaState,\n commandsDir: string,\n opts: {\n /** Exit review mode - calls platform's restoreOriginalState */\n restoreOriginalState: (ctx: unknown) => Promise<void>;\n /** Persist state after mode change */\n persistState: () => void;\n /** Return value when no keyword is detected (e.g. Pi: { action: 'continue' }) */\n noMatch: unknown;\n /** Build return value from transformed text (e.g. Pi: { action: 'transform', text }) */\n transform: (text: string) => unknown;\n },\n): void {\n onInput(async (event: unknown, ctx: unknown) => {\n const text = ((event as Record<string, unknown>).text as string) ?? '';\n const result = detectModeInText(text, commandsDir);\n if (!result) return opts.noMatch;\n\n if (state.reviewMode) {\n await opts.restoreOriginalState(ctx);\n }\n\n state.mode = result.keyword;\n opts.persistState();\n\n return opts.transform(buildModeText(result.prompt, result.strippedText));\n });\n}\n\n/**\n * Install slash commands for fein/sonar/blitz that set the workflow mode\n * and show a notification. Task description injection is handled by the\n * auto-detect handler instead.\n */\nexport function installModeCommands(\n registerCommand: (\n name: string,\n options: { description: string; handler: (...args: unknown[]) => unknown },\n ) => void,\n state: MaestriaState,\n opts: {\n /** Exit review mode before switching modes */\n restoreOriginalState: (ctx: unknown) => Promise<void>;\n /** Persist state after mode change */\n persistState: () => void;\n },\n): void {\n registerCommand(MODE_CLEAR_COMMAND, {\n description: 'Clear workflow mode and return to neutral routing',\n handler: async (_args: unknown, ctx: unknown) => {\n if (state.reviewMode) {\n await opts.restoreOriginalState(ctx);\n }\n state.mode = null;\n opts.persistState();\n ((ctx as Record<string, unknown>).ui as { notify: (msg: string) => void }).notify(\n 'Workflow mode cleared. Neutral routing is active.',\n );\n },\n });\n\n for (const keyword of MODE_KEYWORDS) {\n registerCommand(keyword, {\n description: `Set workflow mode to ${keyword}`,\n handler: async (_args: unknown, ctx: unknown) => {\n if (state.reviewMode) {\n await opts.restoreOriginalState(ctx);\n }\n\n state.mode = keyword;\n opts.persistState();\n\n ((ctx as Record<string, unknown>).ui as { notify: (msg: string) => void }).notify(\n `Mode set to ${keyword}. Describe what you'd like to work on.`,\n );\n },\n });\n }\n}\n","import { dirname, resolve } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport type { ExtensionAPI, ExtensionContext } from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { persistState, restoreOriginalState } from '@/state.js';\nimport {\n installModeAutoDetect as installAutoDetect,\n installModeCommands as installCommands,\n} from '@maestria/shared-pi/modes-core';\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\nconst COMMANDS_DIR = resolve(__dirname, '../agents/commands');\n\nexport function installModeAutoDetect(pi: ExtensionAPI, state: MaestriaState): void {\n installAutoDetect((handler) => pi.on('input', handler as never), state, COMMANDS_DIR, {\n restoreOriginalState: (ctx) => restoreOriginalState(pi, ctx as ExtensionContext, state),\n persistState: () => persistState(pi, state),\n noMatch: { action: 'continue' as const },\n transform: (text) => ({ action: 'transform' as const, text }),\n });\n}\n\nexport function installModeCommands(pi: ExtensionAPI, state: MaestriaState): void {\n installCommands((name, opts) => pi.registerCommand(name, opts as never), state, {\n restoreOriginalState: (ctx) => restoreOriginalState(pi, ctx as ExtensionContext, state),\n persistState: () => persistState(pi, state),\n });\n}\n","import { dirname, resolve } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport type {\n BeforeAgentStartEvent,\n BeforeAgentStartEventResult,\n ExtensionContext,\n} from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { getModePrompt } from '@maestria/shared-pi/modes-core';\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\nconst COMMANDS_DIR = resolve(__dirname, '../agents/commands');\n\n/**\n * Creates a before_agent_start handler that injects workflow mode prompts.\n *\n * This is the only dynamic prompt injection needed from the extension.\n * Static behavioral content (orchestrator prompt + global rules) is\n * auto-injected by Pi's skill system via SKILL.md files registered in\n * the pi.skills manifest field - the standard Pi extension pattern.\n *\n * When no mode is active, the handler returns void (no modification),\n * letting Pi's built-in prompt assembly (skills + context files + tools)\n * stand as-is.\n */\nexport function createModePromptHandler(state: MaestriaState) {\n return (\n event: BeforeAgentStartEvent,\n _ctx: ExtensionContext,\n ): BeforeAgentStartEventResult | void => {\n if (!state.mode) return;\n\n const parts: string[] = [\n event.systemPrompt,\n '',\n getModePrompt(state.mode, COMMANDS_DIR),\n '',\n `The user has set workflow mode to \"${state.mode}\". ` +\n 'Honor this mode throughout the session until changed via /command.',\n ];\n\n return { systemPrompt: parts.join('\\n') };\n };\n}\n","/**\n * Shared compaction handlers for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Imported by both @maestria/omp and @maestria/pi to eliminate duplication.\n *\n * @module\n */\n\nimport { renderMaestriaSummary } from './state-core.js';\nimport type { MaestriaState } from './state-core.js';\n\n/**\n * Install handlers for session compaction and tree events to persist\n * and restore maestria state across session compaction boundaries.\n *\n * Uses duck-typed `pi` parameter - both Pi and OMP ExtensionAPI types\n * satisfy the `{ on(event: string, handler): void }` shape needed here.\n */\nexport function installCompactionHandlers(\n pi: {\n on: (event: string, handler: (...args: unknown[]) => unknown) => void;\n },\n state: MaestriaState,\n): void {\n pi.on('session_before_compact', (event: unknown) => {\n const prep = (event as Record<string, unknown>).preparation as\n | Record<string, unknown>\n | undefined;\n return {\n compaction: {\n summary: renderMaestriaSummary(state),\n details: { ...state },\n firstKeptEntryId: prep?.firstKeptEntryId,\n tokensBefore: prep?.tokensBefore,\n },\n };\n });\n\n pi.on('session_before_tree', (event: unknown) => {\n const prep = (event as Record<string, unknown>).preparation as\n | Record<string, unknown>\n | undefined;\n if (prep?.userWantsSummary) {\n return {\n summary: {\n summary: renderMaestriaSummary(state),\n },\n };\n }\n return undefined;\n });\n}\n","/**\n * Pi platform compaction handlers.\n *\n * Thin wrapper around the shared implementation in\n * @maestria/shared-pi/compaction-core.\n *\n * @module\n */\n\nimport type { ExtensionAPI } from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { installCompactionHandlers as installHandlers } from '@maestria/shared-pi/compaction-core';\n\n/**\n * Install session compaction and tree event handlers for Pi.\n * Delegates to the shared implementation which is duck-type compatible\n * with Pi's ExtensionAPI.\n */\nexport function installCompactionHandlers(pi: ExtensionAPI, state: MaestriaState): void {\n // Bridge: ExtensionAPI.on has overloaded event types incompatible with\n // the duck-typed { on: (event: string, handler) => void } in the shared\n // module. The as-never cast is safe at runtime - both SDKs share the same\n // event shapes.\n installHandlers(\n {\n on: (event, handler) => {\n pi.on(event as never, handler as never);\n },\n },\n state,\n );\n}\n","import { Data, Effect } from 'effect';\n\n/** Terminal subagent statuses - agent will produce no more updates. */\nconst TERMINAL_STATUSES = new Set(['completed', 'steered', 'aborted', 'stopped', 'error']);\n\nexport type SubagentRecord = { status: string; result?: string; error?: string };\n\nexport type SubagentPollFailureReason = 'aborted' | 'timeout' | 'missing';\n\n/**\n * Typed failures from the polling boundary.\n *\n * Keeping these failures in the Effect error channel lets parallel polling\n * interrupt sibling fibers while preserving enough context for the existing\n * tool-level handoff fallback.\n */\nexport class SubagentPollError extends Data.TaggedError('SubagentPollError')<{\n readonly id: string;\n readonly reason: SubagentPollFailureReason;\n readonly message: string;\n}> {}\n\nexport interface SubagentPollingService {\n getRecord(id: string): SubagentRecord | undefined;\n abort?: (id: string) => boolean;\n}\n\nexport interface PollSubagentOptions {\n readonly id: string;\n readonly label: string;\n readonly sendUpdates: boolean;\n readonly service: SubagentPollingService;\n readonly signal?: AbortSignal;\n readonly onUpdate?: (result: { content: Array<{ type: string; text: string }> }) => void;\n readonly intervalMs?: number;\n readonly timeoutMs?: number;\n}\n\nfunction pollLoop(options: PollSubagentOptions): Effect.Effect<SubagentRecord, SubagentPollError> {\n const intervalMs = options.intervalMs ?? 500;\n const timeoutMs = options.timeoutMs ?? 180_000;\n const maxPolls = Math.ceil(timeoutMs / intervalMs);\n\n return Effect.gen(function* () {\n let polls = 0;\n let record = yield* Effect.sync(() => options.service.getRecord(options.id));\n\n while (record && !TERMINAL_STATUSES.has(record.status) && polls < maxPolls) {\n yield* Effect.sleep(intervalMs);\n record = yield* Effect.sync(() => options.service.getRecord(options.id));\n polls++;\n\n if (options.sendUpdates) {\n yield* Effect.sync(() => {\n options.onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `${options.label} running... (${Math.round((polls * intervalMs) / 1000)}s)`,\n },\n ],\n });\n });\n }\n }\n\n if (record && !TERMINAL_STATUSES.has(record.status)) {\n return yield* Effect.fail(\n new SubagentPollError({\n id: options.id,\n reason: 'timeout',\n message: `Subagent ${options.id} timed out after ${timeoutMs}ms`,\n }),\n );\n }\n\n if (!record) {\n return yield* Effect.fail(\n new SubagentPollError({\n id: options.id,\n reason: 'missing',\n message: `Subagent ${options.id} was cleaned up before completion`,\n }),\n );\n }\n\n return record;\n });\n}\n\nfunction abortEffect(id: string, signal: AbortSignal): Effect.Effect<never, SubagentPollError> {\n return Effect.callback<never, SubagentPollError>((resume) => {\n const onAbort = () => {\n resume(\n Effect.fail(\n new SubagentPollError({\n id,\n reason: 'aborted',\n message: 'Maestria subagent call aborted',\n }),\n ),\n );\n };\n\n if (signal.aborted) {\n onAbort();\n return;\n }\n\n signal.addEventListener('abort', onAbort, { once: true });\n return Effect.sync(() => signal.removeEventListener('abort', onAbort));\n });\n}\n\n/**\n * Poll one subagent as a cancellable Effect.\n *\n * The host Pi API is Promise-based, so callers cross the boundary with\n * `Effect.runPromise`. Keeping the polling workflow as an Effect gives the\n * parallel dispatcher structured cancellation: a failed or aborted poll\n * interrupts its sibling poll fibers instead of leaving timers running.\n */\nexport function pollSubagentEffect(\n options: PollSubagentOptions,\n): Effect.Effect<SubagentRecord, SubagentPollError> {\n const poll = pollLoop(options);\n return options.signal ? Effect.raceFirst(poll, abortEffect(options.id, options.signal)) : poll;\n}\n","import { Type } from 'typebox';\nimport { Effect } from 'effect';\nimport type { ExtensionAPI, ExtensionContext } from '@earendil-works/pi-coding-agent';\nimport { SUBAGENT_EVENTS } from '@gotgenes/pi-subagents';\nimport type { MaestriaState } from '@/state.js';\nimport { persistState, recordHandoff, recordSpecialistDelegated } from '@/state.js';\nimport {\n ALLOWED_AGENTS,\n assertValidAgent,\n assertNonEmptyTask,\n MAESTRIA_EVENTS,\n} from '@maestria/shared-pi/subagent-utils';\nimport { pollSubagentEffect, type SubagentPollingService } from '@/subagent-polling.js';\n\nconst ALLOWED_AGENT_NAMES: ReadonlyArray<string> = ALLOWED_AGENTS;\n\n/** Maximum time to wait for a subagent to complete, in milliseconds. */\nexport const POLL_TIMEOUT_MS = 180_000;\n\n/** Interval between subagent status checks, in milliseconds. */\nexport const POLL_INTERVAL_MS = 500;\n\n/** Maximum number of tasks allowed in parallel dispatch. */\nexport const MAX_PARALLEL_TASKS = 8;\n\nfunction abortSubagents(service: SubagentPollingService, ids: readonly string[]): void {\n if (typeof service.abort !== 'function') return;\n\n for (const id of ids) {\n try {\n service.abort(id);\n } catch {\n // Best-effort cleanup: the original polling failure remains authoritative.\n }\n }\n}\n\nfunction pollSubagentOrAbortEffect(options: Parameters<typeof pollSubagentEffect>[0]) {\n return Effect.tapError(pollSubagentEffect(options), () =>\n Effect.sync(() => abortSubagents(options.service, [options.id])),\n );\n}\n\n// ── Handoff recording helper ────────────────────────────────────\n\nfunction recordAndPersist(\n pi: ExtensionAPI,\n state: MaestriaState,\n agentName: string,\n taskText: string,\n): void {\n const updatedState = recordSpecialistDelegated(\n recordHandoff(state, 'orchestrator', agentName, taskText),\n agentName,\n );\n Object.assign(state, updatedState);\n persistState(pi, state);\n}\n\nexport function installSubagentTool(\n pi: ExtensionAPI,\n state: MaestriaState,\n cleanups?: Array<() => void>,\n): void {\n pi.registerTool({\n name: 'maestria_subagent',\n label: 'Maestria Subagent',\n description: 'Dispatch a task to a @maestria specialist subagent',\n promptSnippet:\n 'Delegate tasks to @maestria specialist subagents (adventurer, architect, builder, planner, diagnose, reviewer, writer)',\n promptGuidelines: [\n 'Use maestria_subagent when a task MUST be delegated to a specialist subagent rather than handled directly. Each specialist has focused capabilities: adventurer (recon), architect (design), builder (impl), planner (planning), diagnose (bugs), reviewer (QA), writer (docs).',\n ],\n prepareArguments(args: unknown) {\n return args;\n },\n parameters: Type.Object({\n agent: Type.String({\n description:\n 'Specialist agent name (required): adventurer, architect, builder, diagnose, planner, reviewer, writer',\n }),\n task: Type.String({ description: 'Task description for the subagent (required)' }),\n tasks: Type.Optional(\n Type.Array(\n Type.Object({\n agent: Type.String(),\n task: Type.String(),\n }),\n { description: 'Array of task objects for parallel or chain dispatch' },\n ),\n ),\n mode: Type.Optional(\n Type.Union([Type.Literal('parallel'), Type.Literal('chain'), Type.Literal('single')]),\n ),\n }),\n async execute(\n _toolCallId: string,\n params: {\n agent?: string;\n task?: string;\n tasks?: Array<{ agent: string; task: string }>;\n mode?: 'parallel' | 'chain' | 'single';\n },\n signal: AbortSignal | undefined,\n onUpdate: ((result: { content: Array<{ type: string; text: string }> }) => void) | undefined,\n _ctx: ExtensionContext,\n ) {\n // Block subagent dispatch when in review mode\n if (state.reviewMode) {\n return {\n content: [\n {\n type: 'text' as const,\n text: 'Subagent dispatch is not available during review mode. Use /restore-model to exit review mode first.',\n },\n ],\n };\n }\n\n // Determine dispatch mode (default to 'single' for backward compat)\n const mode = params.mode ?? 'single';\n\n // Validate parameters based on mode\n if (mode === 'single') {\n if (!params.agent || !ALLOWED_AGENT_NAMES.includes(params.agent)) {\n return {\n content: [\n {\n type: 'text' as const,\n text:\n `Invalid maestria_subagent call: 'agent' is required and must be one of ` +\n `${ALLOWED_AGENT_NAMES.join(', ')}. ` +\n `Re-dispatch with a valid agent name; the orchestrator may continue read-only exploration while the brief is corrected.`,\n },\n ],\n };\n }\n assertNonEmptyTask(params.task, 'Task description is required');\n } else if (mode === 'parallel') {\n if (!params.tasks || params.tasks.length < 2) {\n throw new Error(`For parallel mode, tasks array is required with at least 2 items`);\n }\n if (params.tasks.length > MAX_PARALLEL_TASKS) {\n throw new Error(\n `For parallel mode, tasks array may have at most ${MAX_PARALLEL_TASKS} items (got ${params.tasks.length})`,\n );\n }\n for (const t of params.tasks) {\n assertValidAgent(t.agent);\n assertNonEmptyTask(t.task, 'Task description is required for all tasks');\n }\n } else if (mode === 'chain') {\n if (!params.tasks || params.tasks.length < 2) {\n throw new Error('For chain mode, tasks array is required with at least 2 items');\n }\n for (const t of params.tasks) {\n assertValidAgent(t.agent);\n assertNonEmptyTask(t.task, 'Task description is required for all tasks');\n }\n }\n\n // Attempt to dispatch via @gotgenes/pi-subagents; handle missing service\n const { getSubagentsService } = await import('@gotgenes/pi-subagents');\n const service = getSubagentsService();\n if (!service || typeof service.spawn !== 'function') {\n return {\n content: [\n {\n type: 'text' as const,\n text: [\n '## Subagent Dispatch Unavailable',\n '',\n 'The `@gotgenes/pi-subagents` extension is required for subagent dispatch but has not been loaded.',\n '',\n 'Install it as a Pi extension:',\n '',\n '```',\n 'pi install npm:@gotgenes/pi-subagents',\n '```',\n '',\n 'Then restart your Pi session.',\n ].join('\\n'),\n },\n ],\n };\n }\n\n try {\n // --- SINGLE MODE ---\n if (mode === 'single') {\n const agent = params.agent!;\n const task = params.task!;\n\n // Spawn in foreground - returns subagent ID synchronously\n const id = service.spawn(agent, task, {\n description: task.slice(0, 80),\n foreground: true,\n inheritContext: true,\n });\n\n // Record handoff in state and persist (only after spawn succeeds)\n recordAndPersist(pi, state, agent, task);\n\n // Poll for completion (abort on poll failure so nothing is orphaned)\n const record = await Effect.runPromise(\n pollSubagentOrAbortEffect({\n id,\n label: `Subagent ${agent}`,\n sendUpdates: true,\n service,\n signal,\n onUpdate,\n intervalMs: POLL_INTERVAL_MS,\n timeoutMs: POLL_TIMEOUT_MS,\n }),\n );\n\n const resultText = record.result ?? record.error ?? 'No output.';\n\n return {\n content: [{ type: 'text' as const, text: resultText }],\n details: { subagentId: id },\n };\n }\n\n // --- PARALLEL MODE ---\n if (mode === 'parallel') {\n const taskList = params.tasks!;\n\n onUpdate?.({\n content: [\n { type: 'text' as const, text: `Spawning ${taskList.length} parallel subagents...` },\n ],\n });\n\n // Spawn all tasks. If a later spawn throws, abort the subagents already\n // spawned so they are not orphaned; the outer catch returns the handoff\n // fallback.\n const spawnedIds: string[] = [];\n try {\n for (const t of taskList) {\n const id = service.spawn(t.agent, t.task, {\n description: t.task.slice(0, 80),\n foreground: true,\n inheritContext: true,\n });\n spawnedIds.push(id);\n\n // Record each handoff\n recordAndPersist(pi, state, t.agent, t.task);\n }\n } catch (err) {\n abortSubagents(service, spawnedIds);\n throw err;\n }\n\n // Poll all concurrently, preserving completed results when one poll fails.\n // A failed poll aborts every sibling so no subagent is orphaned.\n const outcomes = await Effect.runPromise(\n Effect.all(\n spawnedIds.map((id, i) =>\n Effect.match(\n pollSubagentEffect({\n id,\n label: `${taskList[i].agent} (${i + 1}/${taskList.length})`,\n sendUpdates: false,\n service,\n signal,\n onUpdate,\n intervalMs: POLL_INTERVAL_MS,\n timeoutMs: POLL_TIMEOUT_MS,\n }),\n {\n onSuccess: (record) => ({ record }),\n onFailure: (error) => {\n abortSubagents(service, spawnedIds);\n return { error };\n },\n },\n ),\n ),\n { concurrency: 'unbounded' },\n ),\n );\n\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `All ${taskList.length} parallel subagents settled.`,\n },\n ],\n });\n\n // Aggregate results\n const parts = [`## Parallel Results (${taskList.length} tasks)\\n`];\n for (let i = 0; i < taskList.length; i++) {\n const t = taskList[i];\n const outcome = outcomes[i];\n const header = `### ${i + 1}: ${t.agent}`;\n if ('error' in outcome) {\n const message =\n outcome.error instanceof Error ? outcome.error.message : String(outcome.error);\n parts.push(header);\n parts.push(`⚠️ ${message}`);\n } else {\n const resultText = outcome.record.result ?? outcome.record.error ?? 'No output.';\n parts.push(header);\n parts.push(resultText);\n }\n }\n\n return {\n content: [{ type: 'text' as const, text: parts.join('\\n\\n') }],\n details: { subagentIds: spawnedIds },\n };\n }\n\n // --- CHAIN MODE ---\n if (mode === 'chain') {\n const taskList = params.tasks!;\n let previousResult = '';\n\n for (let i = 0; i < taskList.length; i++) {\n const t = taskList[i];\n let taskText = t.task;\n\n // Substitute {previous} placeholder with previous result.\n // A replacer function (not a string) is required: string replacements interpret\n // $&, $', $`, and $1..$99 in the replacement text, so a previous result\n // containing those sequences would silently corrupt the next task brief.\n if (i > 0 && taskText.includes('{previous}')) {\n taskText = taskText.replace(/\\{previous\\}/g, () => previousResult);\n }\n\n const id = service.spawn(t.agent, taskText, {\n description: taskText.slice(0, 80),\n foreground: true,\n inheritContext: true,\n });\n\n // Record handoff\n recordAndPersist(pi, state, t.agent, taskText);\n\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `Chain step ${i + 1}/${taskList.length}: ${t.agent} running...`,\n },\n ],\n });\n\n // Poll for completion (abort on poll failure so nothing is orphaned)\n try {\n const record = await Effect.runPromise(\n pollSubagentOrAbortEffect({\n id,\n label: `Chain step ${i + 1}: ${t.agent}`,\n sendUpdates: true,\n service,\n signal,\n onUpdate,\n intervalMs: POLL_INTERVAL_MS,\n timeoutMs: POLL_TIMEOUT_MS,\n }),\n );\n previousResult = record.result ?? record.error ?? 'No output.';\n } catch (error) {\n previousResult = `[error] ${error instanceof Error ? error.message : String(error)}`;\n break;\n }\n\n if (i < taskList.length - 1) {\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `Chain step ${i + 1}/${taskList.length}: ${t.agent} completed. Moving to next step.`,\n },\n ],\n });\n }\n }\n\n return {\n content: [{ type: 'text' as const, text: previousResult }],\n details: { subagentId: 'chain-completed' },\n };\n }\n\n // Should not reach here - all modes are handled above\n throw new Error('Unknown dispatch mode');\n } catch (err) {\n console.warn('[maestria] Subagent dispatch failed:', err);\n // Return handoff payload as structured text when dispatch fails\n const agentName = params.agent ?? params.tasks?.[0]?.agent ?? 'unknown';\n const taskDesc = params.task ?? params.tasks?.map((t) => t.task).join('; ') ?? 'unknown';\n const handoffInfo = [\n `## Subagent Handoff Required`,\n ``,\n `**From:** orchestrator`,\n `**To:** ${agentName}`,\n `**Task:** ${taskDesc}`,\n ``,\n `Subagent dispatch failed. Please delegate this work manually.`,\n ].join('\\n');\n\n return {\n content: [{ type: 'text' as const, text: handoffInfo }],\n };\n }\n },\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any); // TypeBox inferred types don't match ToolDefinition exactly\n\n // Subscribe to subagent lifecycle events for accurate state tracking.\n // These subscriptions are set up once at extension init, not on every tool call.\n // pi.events is the shared EventBus - distinct from pi.on() lifecycle hooks.\n if (pi.events) {\n const unsubStarted = pi.events.on(SUBAGENT_EVENTS.STARTED, (data: unknown) => {\n const { id, type } = data as { id: string; type: string };\n state.subagentStatus[id] = { type, status: 'running', startedAt: Date.now() };\n persistState(pi, state);\n pi.events?.emit(MAESTRIA_EVENTS.SUBAGENT_STARTED, {\n id,\n type,\n timestamp: Date.now(),\n });\n });\n\n const unsubCompleted = pi.events.on(SUBAGENT_EVENTS.COMPLETED, (data: unknown) => {\n const { id } = data as { id: string };\n const existing = state.subagentStatus[id];\n if (existing) {\n existing.status = 'completed';\n existing.completedAt = Date.now();\n }\n persistState(pi, state);\n pi.events?.emit(MAESTRIA_EVENTS.SUBAGENT_COMPLETED, {\n id,\n type: existing?.type,\n timestamp: Date.now(),\n });\n });\n\n const unsubFailed = pi.events.on(SUBAGENT_EVENTS.FAILED, (data: unknown) => {\n const { id, status } = data as { id: string; status: string };\n const existing = state.subagentStatus[id];\n if (existing) {\n existing.status = status ?? 'error';\n existing.completedAt = Date.now();\n }\n persistState(pi, state);\n pi.events?.emit(MAESTRIA_EVENTS.SUBAGENT_FAILED, {\n id,\n type: existing?.type,\n timestamp: Date.now(),\n });\n });\n\n const unsubSteered = pi.events.on(SUBAGENT_EVENTS.STEERED, (data: unknown) => {\n // Steering is informational - no status transition, but ensure\n // the agent is tracked as running if it wasn't already observed.\n const { id } = data as { id: string };\n if (!state.subagentStatus[id]) {\n state.subagentStatus[id] = { type: 'unknown', status: 'running', startedAt: Date.now() };\n }\n persistState(pi, state);\n });\n\n if (cleanups) {\n cleanups.push(unsubStarted, unsubCompleted, unsubFailed, unsubSteered);\n }\n }\n}\n","/**\n * Shared slash-command installation for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Consumed by both @maestria/omp and @maestria/pi to eliminate duplication\n * of the maestria-status/review/restore-model/handoff/review-model commands.\n *\n * @module\n */\n\nimport type { MaestriaState } from './state-core.js';\nimport { persistState, recordHandoff, renderMaestriaSummary } from './state-core.js';\nimport { cycleToReviewModel, restoreOriginalState } from './review-core.js';\nimport { MAESTRIA_EVENTS } from './subagent-utils.js';\n\n// ── Duck-typed platform interfaces ──\n\ninterface CommandsCtx {\n ui: {\n notify(msg: string): void;\n setEditorText(text: string): void;\n };\n model?: { id: string };\n modelRegistry: { getAll(): Array<{ id: string }> };\n}\n\ninterface CommandsPi {\n registerCommand(\n name: string,\n opts: {\n description: string;\n handler: (args: string, ctx: CommandsCtx) => Promise<void> | void;\n },\n ): void;\n getActiveTools(): string[];\n setActiveTools(tools: string[]): void | Promise<void>;\n setModel(model: unknown): void | Promise<void>;\n sendUserMessage(text: string, opts: { deliverAs: string }): void;\n appendEntry(type: string, data: unknown): void;\n events?: { emit(event: string, data: unknown): void };\n}\n\n/**\n * Read-only tools that let a reviewer inspect code without making changes.\n *\n * - `read`, `grep`, `find`, `ls`, `glob` - all non-destructive.\n * - Excluded: `bash`, `edit`, `write` - these can modify the filesystem.\n *\n * `glob` is included for file pattern matching even though it's not a\n * built-in Pi tool - extensions may register it, and including it is a no-op\n * if absent.\n */\nconst READ_ONLY_TOOLS = ['read', 'grep', 'find', 'ls', 'glob'];\n\nexport function installCommands(pi: CommandsPi, state: MaestriaState): void {\n pi.registerCommand('maestria-status', {\n description: 'Show current maestria session state including handoff history',\n handler: async (_args: string, ctx) => {\n const summary = renderMaestriaSummary(state);\n if (!summary) {\n ctx.ui.notify('No active maestria state to report.');\n return;\n }\n ctx.ui.setEditorText(summary);\n },\n });\n\n pi.registerCommand('review', {\n description: 'Enter review mode. Blocks destructive tools, sets read-only toolset.',\n handler: async (args: string, ctx) => {\n if (!args.trim()) {\n ctx.ui.notify('Usage: /review <target> - describe what to review');\n return;\n }\n\n // 1. Save current model and tools for later restoration\n const currentModelId = ctx.model?.id ?? null;\n const currentTools = pi.getActiveTools();\n\n // 2. Update state: mark review mode, store originals\n const updatedState: MaestriaState = {\n ...state,\n reviewMode: true,\n originalModel: currentModelId,\n originalTools: currentTools,\n };\n Object.assign(state, updatedState);\n persistState(pi, state);\n\n // 3. Switch to review model if configured\n if (state.reviewModel) {\n const switched = await cycleToReviewModel(pi, ctx, state);\n if (switched) {\n ctx.ui.notify(`Review mode: switched to ${switched}`);\n pi.events?.emit(MAESTRIA_EVENTS.REVIEW_ACTIVATED, {\n originalModel: state.originalModel,\n reviewModel: switched,\n timestamp: Date.now(),\n });\n }\n }\n\n // 4. Restrict to read-only tools\n void pi.setActiveTools(READ_ONLY_TOOLS);\n\n pi.sendUserMessage(\n [\n `[REVIEW: ${args}]`,\n '',\n `Review: ${args}. Use the reviewer prompt template.`,\n 'Read only, no edits, report findings.',\n ].join('\\n'),\n { deliverAs: 'steer' },\n );\n },\n });\n\n pi.registerCommand('restore-model', {\n description:\n 'Restore the original model and tools that were active before review mode was entered.',\n handler: async (_args: string, ctx) => {\n if (!state.reviewMode) {\n ctx.ui.notify('Not in review mode. Nothing to restore.');\n return;\n }\n const prevOriginalModel = state.originalModel;\n await restoreOriginalState(pi, ctx, state);\n persistState(pi, state);\n ctx.ui.notify('Restored original model and tools.');\n pi.events?.emit(MAESTRIA_EVENTS.REVIEW_DEACTIVATED, {\n originalModel: prevOriginalModel,\n timestamp: Date.now(),\n });\n },\n });\n\n pi.registerCommand('handoff', {\n description: 'Generate a structured handoff prompt for a new task context',\n handler: async (args: string, ctx) => {\n if (!args.trim()) {\n ctx.ui.notify('Usage: /handoff <goal> - describe the task context for handoff');\n return;\n }\n\n // Build a structured handoff document with 7 fields\n const goal = args.trim();\n const handoffPrompt = [\n '**Goal:** ' + goal,\n '',\n '**Context:**',\n '- Mode: ' + (state.mode ?? 'none'),\n '- Active task: ' + (state.activeTask || 'none'),\n '- Specialists delegated: ' +\n ((state.specialistsDelegated?.length ?? 0) > 0\n ? state.specialistsDelegated.join(', ')\n : 'none'),\n '- Recent handoffs: ' + (state.handoffHistory?.length ?? 0) + ' entries',\n '- Files modified: ' +\n ((state.filesModified?.length ?? 0) > 0 ? state.filesModified.join(', ') : 'none'),\n '',\n '**Requirements:**',\n '(fill in specific requirements)',\n '',\n '**Known problems:**',\n (state.blockers?.length ?? 0) > 0\n ? state.blockers.map((b: string) => '- ' + b).join('\\n')\n : '(no known problems documented)',\n '',\n '**Assumptions documented:**',\n '(document assumptions made, tagged [inferred] where uncertain)',\n '',\n '**Success criteria:**',\n '(fill in how to verify completion)',\n '',\n '**Next step:**',\n '(fill in what happens after this task)',\n '',\n '---',\n 'Complete the fields above before sending.',\n ].join('\\n');\n\n // Record in state\n Object.assign(state, recordHandoff(state, 'current', 'next', goal));\n\n // Persist state\n persistState(pi, state);\n\n // Send as user message with steer delivery\n pi.sendUserMessage(handoffPrompt, { deliverAs: 'steer' });\n },\n });\n\n pi.registerCommand('review-model', {\n description: 'Set which model to use when entering review mode',\n handler: async (args: string, ctx) => {\n if (!args.trim()) {\n ctx.ui.notify('Usage: /review-model <model-id>');\n return;\n }\n const modelId = args.trim();\n const models = ctx.modelRegistry.getAll();\n const model = models.find((m) => m.id === modelId);\n if (!model) {\n ctx.ui.notify(\n `Unknown model: \"${modelId}\". Available: ${models.map((m) => m.id).join(', ')}`,\n );\n return;\n }\n state.reviewModel = modelId;\n persistState(pi, state);\n ctx.ui.notify(`Review model set to: ${modelId}`);\n },\n });\n}\n","import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { installCommands as installCommandsCore } from '@maestria/shared-pi/commands-core';\n\nexport function installCommands(pi: ExtensionAPI, state: MaestriaState): void {\n installCommandsCore(pi as never, state);\n}\n","/**\n * Shared tool interceptor utilities for Maestria platform packages.\n *\n * Pure TypeScript - no platform-specific dependencies.\n * Imported by both @maestria/omp and @maestria/pi to eliminate duplication.\n *\n * @module\n */\n\n/**\n * Dangerous bash command patterns that should always be blocked,\n * regardless of mode or specialist role.\n */\nexport const DANGEROUS_PATTERNS = [\n /rm\\s+-rf\\s+\\//,\n /dd\\s+if=/,\n />\\s*\\/dev\\/sd/,\n /chmod\\s+-R\\s+777\\s+\\//,\n /mkfs\\.\\w+/,\n /:(){ :\\|:& };:/,\n />\\s*\\/etc\\/(passwd|shadow|sudoers)/,\n /\\beval\\b/,\n /wget\\s+-O\\s*-\\s*\\|\\s*(bash|sh)/,\n /curl\\s+.*\\|\\s*(bash|sh)/,\n /crontab\\s+-r/,\n];\n\n/**\n * Read-only bash command prefixes allowed for the orchestrator's recon and\n * verification. Anything not matching - or chaining into a mutation - is\n * blocked; mutations belong to specialists.\n */\nconst READ_ONLY_BASH_PREFIX =\n /^(ls|cat|head|tail|git status|git diff|git log|git branch|find|grep|rg|pnpm test|npm test|pwd|which)\\b/;\n\n/**\n * True when a bash command performs no mutation.\n *\n * A naive prefix check is bypassable - `git status && git checkout .` or\n * `ls; rm -rf dist` both pass a prefix-only match - so every segment of a\n * chained command (`;`, `&&`, `||`, `|`, or newline) must itself be\n * read-only, and command substitution (`$(...)`, backticks) and output\n * redirection (`>` / `>>`) are rejected because they can hide a mutation\n * behind a read-only prefix. `2>&1`-style fd redirects are allowed (they\n * don't write).\n */\nexport function isReadOnlyBashCommand(rawCommand: string): boolean {\n const command = rawCommand.trim();\n if (command.includes('$(') || command.includes('`')) return false;\n // Strip `2>&1`-style fd redirects first so the `&` inside them is not\n // mistaken for a command separator and the `>` is not counted as output\n // redirection.\n const withoutFdRedirects = command.replace(/\\d?>&[12]/g, '');\n if (withoutFdRedirects.includes('>')) return false;\n return withoutFdRedirects\n .split(/[\\n;&|]+/)\n .every((segment) => READ_ONLY_BASH_PREFIX.test(segment.trim()));\n}\n","import {\n isToolCallEventType,\n type ExtensionAPI,\n type ToolCallEvent,\n type ExtensionContext,\n} from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport { DANGEROUS_PATTERNS, isReadOnlyBashCommand } from '@maestria/shared-pi/tools-core';\nimport { persistState, recordFileModified, recordFileRead } from '@/state.js';\n\nexport function installToolInterceptors(pi: ExtensionAPI, state: MaestriaState): void {\n pi.on('tool_call', async (event: ToolCallEvent, ctx: ExtensionContext) => {\n if (!event || !event.toolName) return;\n\n // ── Orchestrator routing enforcement ──\n // When a maestria workflow mode is active, restrict the root session\n // (orchestrator) to read-only recon + delegation. The orchestrator may\n // read, search, and inspect to route and verify, but mutations (edit,\n // write, mutation-capable bash) belong to specialists. Subagent sessions\n // are detected by the absence of the pi-subagents 'subagent' tool\n // (stripped by applyRecursionGuard in child sessions).\n if (state.mode !== null && pi.getActiveTools().includes('subagent')) {\n const isMutation =\n isToolCallEventType('edit', event) ||\n isToolCallEventType('write', event) ||\n isToolCallEventType('patch', event) ||\n event.toolName === 'bash';\n if (isMutation) {\n // Read-only bash (ls, git status, git diff, tests) is allowed so the\n // orchestrator can verify state; mutation-capable commands still\n // belong to specialists.\n if (event.toolName === 'bash') {\n const input = event.input as { command?: unknown } | undefined;\n const command = typeof input?.command === 'string' ? input.command : '';\n if (isReadOnlyBashCommand(command)) {\n return undefined;\n }\n }\n return {\n block: true,\n reason:\n `Tool '${event.toolName}' is blocked for the orchestrator. ` +\n `Use 'maestria_subagent' to delegate mutations to specialists.`,\n };\n }\n }\n\n // Block destructive tools in review mode\n if (state.reviewMode) {\n if (\n isToolCallEventType('edit', event) ||\n isToolCallEventType('write', event) ||\n isToolCallEventType('bash', event)\n ) {\n return {\n block: true,\n reason: 'Review mode is active. Report findings, do not edit.',\n };\n }\n }\n\n // Block dangerous bash patterns regardless of mode\n if (isToolCallEventType('bash', event)) {\n if (!event.input || typeof event.input !== 'object') return undefined;\n const command = event.input.command;\n if (command) {\n for (const pattern of DANGEROUS_PATTERNS) {\n if (pattern.test(command)) {\n if (ctx.hasUI) {\n const confirmed = await ctx.ui.confirm(\n 'Dangerous Pattern Detected',\n `This command matches a dangerous pattern:\\n${command}\\nProceed?`,\n );\n if (confirmed) return undefined;\n }\n return {\n block: true,\n reason: `Command matches dangerous pattern: ${pattern}`,\n };\n }\n }\n }\n }\n\n // Record file access for session state (ADR-PI-002: tool_call maintains file tracking).\n // Only record when the call is allowed to proceed.\n let tracked = false;\n if (isToolCallEventType('read', event)) {\n const path = event.input?.path;\n if (typeof path === 'string' && path) {\n Object.assign(state, recordFileRead(state, path));\n tracked = true;\n }\n } else if (isToolCallEventType('edit', event) || isToolCallEventType('write', event)) {\n const path = event.input?.path;\n if (typeof path === 'string' && path) {\n Object.assign(state, recordFileModified(state, path));\n tracked = true;\n }\n }\n if (tracked) {\n persistState(pi, state);\n }\n\n return undefined; // allow\n });\n}\n","import type {\n ExtensionAPI,\n ExtensionContext,\n SessionStartEvent,\n SessionTreeEvent,\n} from '@earendil-works/pi-coding-agent';\nimport { createInitialState } from '@/state.js';\nimport type { MaestriaState } from '@/state.js';\nimport { deploySpecialistAgents } from '@/agents.js';\nimport { installModeCommands, installModeAutoDetect } from '@/modes.js';\nimport { createModePromptHandler } from '@/rules.js';\nimport { installCompactionHandlers } from '@/compaction.js';\nimport { installSubagentTool } from '@/subagent.js';\nimport { installCommands } from '@/commands.js';\nimport { installToolInterceptors } from '@/tools.js';\n\ninterface PersistedStateEntry {\n type: string;\n customType?: string;\n data?: unknown;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null;\n}\n\nfunction currentSessionEntries(ctx: ExtensionContext): PersistedStateEntry[] | null {\n // getBranch() is the public current-session view and avoids restoring state\n // from a sibling branch in the same session tree. Never fall back to\n // getEntries(), which spans the entire session tree.\n const sessionManager = ctx?.sessionManager;\n if (typeof sessionManager?.getBranch !== 'function') return null;\n\n const branch = sessionManager.getBranch();\n return Array.isArray(branch) ? (branch as PersistedStateEntry[]) : null;\n}\n\nfunction restoreStateFromSession(state: MaestriaState, ctx: ExtensionContext): void {\n const next = createInitialState();\n const entries = currentSessionEntries(ctx);\n\n if (!entries) {\n const mutableState = state as unknown as Record<string, unknown>;\n for (const key of Object.keys(mutableState)) delete mutableState[key];\n Object.assign(state, next);\n return;\n }\n\n for (let i = entries.length - 1; i >= 0; i--) {\n const entry = entries[i];\n if (entry.type === 'custom' && entry.customType === 'maestria_state') {\n if (isRecord(entry.data)) Object.assign(next, entry.data);\n break;\n }\n }\n\n const mutableState = state as unknown as Record<string, unknown>;\n for (const key of Object.keys(mutableState)) delete mutableState[key];\n Object.assign(state, next);\n}\n\nexport default function (pi: ExtensionAPI): void {\n const state = createInitialState();\n const cleanups: Array<() => void> = [];\n\n // Install mode commands: /fein, /sonar, /blitz\n installModeCommands(pi, state);\n installModeAutoDetect(pi, state);\n\n // Inject mode prompt when a workflow mode is active\n const handleModePrompt = createModePromptHandler(state);\n\n pi.on('before_agent_start', (event, ctx) => {\n return handleModePrompt(event, ctx);\n });\n\n // Deploy specialist agent files for pi-subagents discovery\n pi.on('session_start', (_event: SessionStartEvent, ctx) => {\n deploySpecialistAgents(ctx);\n\n // Restore persisted state on session start (reload/resume/fork)\n restoreStateFromSession(state, ctx);\n });\n\n // Rehydrate state when navigating the session tree to a different branch\n pi.on('session_tree', (_event: SessionTreeEvent, ctx) => {\n restoreStateFromSession(state, ctx);\n });\n\n // Install compaction preservation handlers\n installCompactionHandlers(pi, state);\n\n // Install orchestration hooks: subagent tool and commands\n installSubagentTool(pi, state, cleanups);\n installCommands(pi, state);\n\n // Cleanup subscriptions on shutdown\n pi.on('session_shutdown', () => {\n for (const cleanup of cleanups) cleanup();\n cleanups.length = 0;\n });\n\n // Install tool call interceptors for review mode and dangerous patterns\n installToolInterceptors(pi, state);\n}\n"],"mappings":"uaA6DA,SAAgB,GAAoC,CAClD,MAAO,CACL,KAAM,KACN,WAAY,GACZ,kBAAmB,GACnB,qBAAsB,CAAC,EACvB,SAAU,CAAC,EACX,cAAe,CAAC,EAChB,UAAW,CAAC,EACZ,eAAgB,CAAC,EACjB,WAAY,GACZ,cAAe,KACf,cAAe,KACf,eAAgB,CAAC,EACjB,YAAa,KACb,WAAY,IACd,CACF,CAEA,SAAS,EAAe,EAAiB,EAAc,EAAuB,CAE5E,MAAO,CAAC,EAAM,GADG,EAAM,OAAQ,GAAM,IA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,IAAI,EACxD,KACF,CACF,CAEA,IAAM,EAAe,EACrB,IAAK,IAAM,KAAO,OAAO,KAAK,CAAY,EAAG,OAAO,EAAa,GACjE,OAAO,OAAO,EAAO,CAAI,CAC3B,CAEA,SAAA,GAAyB,EAAwB,CAC/C,IAAM,EAAQ,EAAmB,EAC3B,EAA8B,CAAC,EAGrC,GAAoB,EAAI,CAAK,EAC7B,GAAsB,EAAI,CAAK,EAG/B,IAAM,EAAmB,GAAwB,CAAK,EAEtD,EAAG,GAAG,sBAAuB,EAAO,IAC3B,EAAiB,EAAO,CAAG,CACnC,EAGD,EAAG,GAAG,iBAAkB,EAA2B,IAAQ,CACzD,GAAuB,CAAG,EAG1B,EAAwB,EAAO,CAAG,CACpC,CAAC,EAGD,EAAG,GAAG,gBAAiB,EAA0B,IAAQ,CACvD,EAAwB,EAAO,CAAG,CACpC,CAAC,EAGD,GAA0B,EAAI,CAAK,EAGnC,GAAoB,EAAI,EAAO,CAAQ,EACvC,EAAgB,EAAI,CAAK,EAGzB,EAAG,GAAG,uBAA0B,CAC9B,IAAK,IAAM,KAAW,EAAU,EAAQ,EACxC,EAAS,OAAS,CACpB,CAAC,EAGD,GAAwB,EAAI,CAAK,CACnC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maestria/pi",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Maestria extension for the Pi coding agent",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent-orchestration",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"@types/node": "^26.2.0",
|
|
34
34
|
"typescript": "^7.0.2",
|
|
35
35
|
"vitest": "4.1.11",
|
|
36
|
+
"@maestria/core": "0.8.2",
|
|
36
37
|
"@maestria/shared-pi": "0.4.0"
|
|
37
38
|
},
|
|
38
39
|
"peerDependencies": {
|
|
@@ -12,7 +12,7 @@ description: >-
|
|
|
12
12
|
|
|
13
13
|
# Global Agent Rules
|
|
14
14
|
|
|
15
|
-
Cross-platform behavior contract
|
|
15
|
+
Cross-platform behavior contract for outcomes, evidence, safety, delegation, review, and bounded repair. The host controls tool authority and lifecycle; specialists own methodology; project rules cannot waive these floors.
|
|
16
16
|
|
|
17
17
|
## Universal Floors
|
|
18
18
|
|
|
@@ -23,7 +23,11 @@ Cross-platform behavior contract: outcomes, evidence, safety, delegation, review
|
|
|
23
23
|
- **!!! Prefer reuse over reinvention.** Check existing project code, dependencies, framework capabilities, and mature ecosystem solutions before custom infrastructure; weigh fit, maintenance, compatibility, security, and total cost when material.
|
|
24
24
|
- **!!! Exhaust available evidence before asking.** Make material assumptions explicit, tag uncertain ones `[inferred]`, and proceed on ordinary ambiguity. Ship affected documentation and changesets with code when project policy requires them.
|
|
25
25
|
- **!!! Keep output self-contained and professional.** Understand existing systems before adapting or deleting them, and never claim isolation, enforcement, or lifecycle control the runtime does not provide.
|
|
26
|
-
- **!!! Human-facing output.** In agent responses, status updates,
|
|
26
|
+
- **!!! Human-facing output.** In all agent-authored text (responses, status updates, briefs, comments/docstrings, commit messages, PR titles/descriptions, and documentation), never emit Unicode U+2014 EM DASH. Prefer commas, colons, parentheses, or ASCII hyphen-minus (`-`). Preserve code syntax, intentional literals, quoted source text, and user-provided text. Scan authored output before handoff or delivery.
|
|
27
|
+
|
|
28
|
+
### Prefer self-explanatory code over comments
|
|
29
|
+
|
|
30
|
+
Default to code that explains itself: prefer clear naming, small functions, appropriate abstractions, and simple control flow; rewrite code that needs comments to explain mechanics. Do not add comments that merely restate what the code does. Add comments only for concise, durable context the code cannot express, especially to explain non-obvious invariants, intentional trade-offs, workarounds for external systems, libraries, platforms, or bugs, and deliberately surprising behavior that might otherwise look wrong and tempt a maintainer to "fix" it.
|
|
27
31
|
|
|
28
32
|
## Modes
|
|
29
33
|
|
|
@@ -31,9 +35,7 @@ Per-turn keywords when the host supports them: `fein` requests the full route wi
|
|
|
31
35
|
|
|
32
36
|
## Outcome and Scope
|
|
33
37
|
|
|
34
|
-
Define the primary user outcome, acceptance evidence, and
|
|
35
|
-
|
|
36
|
-
Changes altering security, authentication, or permission boundaries are mandatory stops; ordinary in-scope security defects may be repaired autonomously.
|
|
38
|
+
Define the primary user outcome, acceptance evidence, and non-goals before substantial work or delegation; measure progress against them, not activity. Keep file, package, and runtime scope explicit. Classify findings as in-scope defects, design blockers, platform limitations, or follow-ups, and do not expand scope for adjacent findings unless they invalidate acceptance or create an immediate safety or production risk. Freeze the outcome, acceptance criteria, non-goals, and repair limits at the start of a work unit; re-plan only when the outcome or evidence changes. Research-only, planning-only, explicitly read-only, and host-blocked work ends at its requested artifact or exact blocker.
|
|
37
39
|
|
|
38
40
|
## Delegation and Context
|
|
39
41
|
|
|
@@ -49,12 +51,10 @@ Default to one independent review and, only when blockers exist, one repair/re-r
|
|
|
49
51
|
|
|
50
52
|
## Authorization, Lifecycle, and Branches
|
|
51
53
|
|
|
52
|
-
Safety and authorization override user intent, methodology, and brevity. Stop and obtain applicable authorization before changes that alter
|
|
54
|
+
Safety and authorization override user intent, methodology, and brevity. Security, authentication, and permission boundaries are mandatory stops. Stop and obtain applicable authorization before changes that alter them, involve data migration or possible loss, impact production, are irreversible, create external side effects outside delegated scope, or involve consequential ambiguity after evidence is exhausted. Ordinary in-scope security defects may be repaired autonomously.
|
|
53
55
|
|
|
54
56
|
The orchestrator owns continuation for implementation and delivery work until the outcome reaches its terminal artifact; incomplete todos, pending handoffs, or specialist messages saying "continue if needed" are not a user checkpoint. Routine delivery is autonomous. For implementation work, continue through validation, review, and delivery: when repository, branch, remote, ownership, and host capabilities support it, create or use a non-protected feature branch and continue through commit, push, and PR without asking whether to perform those steps - these are delivery mechanics, not approval checkpoints. Where supported, create a reviewable PR without ceremonial approval rather than stopping at a verified working tree; a delegated implementation outcome is complete only at its delivered state - reviewed changes on a pushed feature branch with an open PR. Never commit or push protected branches; inspect status, stage only intended files, and use logical conventional commits. Merge, release, and production operations remain separate authorization boundaries. Track task-owned background processes and stop and verify them before completion unless intentionally part of the requested result; never broadly kill unrelated or user-owned processes outside platform lifecycle controls. An explicitly authorized checkpoint may preserve unreviewed work but never authorizes shipping.
|
|
55
57
|
|
|
56
|
-
Freeze the outcome, acceptance criteria, non-goals, and repair limits at the start of a work unit; re-plan only when the outcome or its evidence changes. Research-only, planning-only, explicitly read-only, and host-blocked work terminates at its requested artifact or exact blocker.
|
|
57
|
-
|
|
58
58
|
## Canonical Source Invariant
|
|
59
59
|
|
|
60
60
|
Author agent directives only under `packages/core/agent-directives/`. Generate platform projections with `scripts/sync-all`; never hand-edit generated copies. Pass the sync check before handing off any canonical directive change.
|