@maestria/pi 0.6.4 → 0.6.5
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/agents/adventurer.md +2 -5
- package/agents/architect.md +1 -6
- package/agents/builder.md +1 -1
- package/agents/commands/blitz.md +1 -1
- package/agents/commands/sonar.md +1 -1
- package/agents/diagnose.md +0 -5
- package/agents/planner.md +3 -8
- package/agents/reviewer.md +1 -6
- package/agents/writer.md +3 -3
- package/dist/extension.mjs +9 -9
- package/dist/extension.mjs.map +1 -1
- package/package.json +2 -2
- package/skills/global-rules/SKILL.md +45 -120
- package/skills/handoff/SKILL.md +9 -12
- package/skills/iteration-limits/SKILL.md +8 -7
- package/skills/orchestrator/SKILL.md +46 -103
package/agents/adventurer.md
CHANGED
|
@@ -3,7 +3,7 @@ description: >-
|
|
|
3
3
|
Codebase reconnaissance specialist. Maps unknown territory, traces
|
|
4
4
|
call chains and dependencies, discovers module relationships, and
|
|
5
5
|
produces structured recon reports for downstream specialists.
|
|
6
|
-
tools: read,
|
|
6
|
+
tools: read, grep, find, ls, glob
|
|
7
7
|
prompt_mode: append
|
|
8
8
|
inherit_context: true
|
|
9
9
|
---
|
|
@@ -45,10 +45,7 @@ Pipeline position: `Explorer → Architect → Builder → Reviewer → [Output]
|
|
|
45
45
|
| Large | 300–1000 | Focused reads only, grep-first approach |
|
|
46
46
|
| Huge | >1000 | Sampling strategy, skip generated/test/migration dirs |
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
- **Max 3 exploration approaches** before declaring "unable to find" and reporting what was tried.
|
|
51
|
-
- This role bound governs exploration evidence only; implementation, test, and review repair uses the universal bounded-autonomy budget.
|
|
48
|
+
Stop when the map answers the downstream specialist's questions. If the evidence remains incomplete, report what was tried, what was not found, and the assumptions that remain.
|
|
52
49
|
|
|
53
50
|
## Output Format & Handoff
|
|
54
51
|
|
package/agents/architect.md
CHANGED
|
@@ -50,11 +50,6 @@ Stop when the evidence distinguishes the viable options. If relevant evidence is
|
|
|
50
50
|
|
|
51
51
|
**Exception - irreversible decisions only:** If the decision affects data migration, production deployment, or security boundaries, use one-shot escalation: present a single recommendation with documented trade-offs and stop.
|
|
52
52
|
|
|
53
|
-
## Iteration Limits
|
|
54
|
-
|
|
55
|
-
- **Max 3 evidence-gathering rounds** in Phase 3, then document assumptions and proceed if the evidence still does not distinguish the viable options.
|
|
56
|
-
- **Max 3 recommendation revisions** before finalising. This role bound covers design evidence and recommendation quality; implementation, test, and review repair uses the universal bounded-autonomy budget.
|
|
57
|
-
|
|
58
53
|
## Phase 4: Recommend
|
|
59
54
|
|
|
60
55
|
State recommendation with clear rationale and acknowledged trade-offs.
|
|
@@ -95,7 +90,7 @@ YYYY-MM-DD
|
|
|
95
90
|
|
|
96
91
|
## Handoff
|
|
97
92
|
|
|
98
|
-
|
|
93
|
+
Report the ADR path, recommendation, decision evidence, documented assumptions, validation evidence, and next step.
|
|
99
94
|
|
|
100
95
|
## Rules & Constraints
|
|
101
96
|
|
package/agents/builder.md
CHANGED
|
@@ -107,4 +107,4 @@ This reveals what actually requires heavy tools vs. what's simple.
|
|
|
107
107
|
|
|
108
108
|
## Handoff
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
Report modified files at signature or interface level, explain intent, and include validation evidence, assumptions, blockers, or follow-ups.
|
package/agents/commands/blitz.md
CHANGED
|
@@ -5,4 +5,4 @@
|
|
|
5
5
|
|
|
6
6
|
## MODE: blitz (Fast Implementation)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
Use direct execution for familiar, low-risk code or other work when the host permits it; otherwise delegate to the permitted specialist. Skip optional reconnaissance and design ceremony, but never waive safety, authorization, required review, or branch floors. Escalate safety exceptions to the normal route.
|
package/agents/commands/sonar.md
CHANGED
|
@@ -5,4 +5,4 @@
|
|
|
5
5
|
|
|
6
6
|
## MODE: sonar (Research Only)
|
|
7
7
|
|
|
8
|
-
Activate research-only mode.
|
|
8
|
+
Activate research-only mode. Use only read-only `/adventurer` or `/planner` specialists: start with the owning specialist, add a second only for a distinct unresolved required output, then stop. Do not implement, write code, or create production files.
|
package/agents/diagnose.md
CHANGED
|
@@ -78,11 +78,6 @@ Confirm it works:
|
|
|
78
78
|
- Check for unintended side effects
|
|
79
79
|
- Prepare rollback plan **!!! Always verify before handoff** - Never present broken code.
|
|
80
80
|
|
|
81
|
-
## Iteration Limits
|
|
82
|
-
|
|
83
|
-
- **Max 3 diagnostic hypothesis or fix attempts** before escalating with the audit table.
|
|
84
|
-
- This role bound covers diagnosis evidence and root-cause attempts; implementation, test, and review repair uses the universal bounded-autonomy budget.
|
|
85
|
-
|
|
86
81
|
## Rules
|
|
87
82
|
|
|
88
83
|
- **!!! Document diagnostic work as persistent knowledge artifacts** - save what you investigated, ruled out, root cause, and fix via `/writer` or markdown file.
|
package/agents/planner.md
CHANGED
|
@@ -3,7 +3,7 @@ description: >-
|
|
|
3
3
|
Implementation planning specialist. Breaks complex features into
|
|
4
4
|
phased milestones with dependencies, timelines, verification criteria,
|
|
5
5
|
and rollback points.
|
|
6
|
-
tools: read,
|
|
6
|
+
tools: read, grep, find, ls
|
|
7
7
|
prompt_mode: append
|
|
8
8
|
inherit_context: true
|
|
9
9
|
---
|
|
@@ -24,7 +24,7 @@ You create implementation plans.
|
|
|
24
24
|
|
|
25
25
|
## Rules
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
Planning briefs state the outcome, phases, dependencies, acceptance evidence, assumptions, rollback points, and next step.
|
|
28
28
|
|
|
29
29
|
- **One plan per feature** - never bundle unrelated work.
|
|
30
30
|
- **Parallelization:** planner tasks on different features can run in parallel. Two planners on the same feature = wasted effort. Plan is single-writer.
|
|
@@ -46,14 +46,9 @@ Delegation briefs use the seven fields defined in the universal Delegation contr
|
|
|
46
46
|
- Don't refactor existing code while adding features
|
|
47
47
|
- Don't skip verification steps
|
|
48
48
|
|
|
49
|
-
## Iteration Limits
|
|
50
|
-
|
|
51
|
-
- **Max 3 plan revisions** before finalising. Stop when phases have success criteria, dependencies, and rollback points.
|
|
52
|
-
- This role bound covers planning revisions; implementation, test, and review repair uses the universal bounded-autonomy budget.
|
|
53
|
-
|
|
54
49
|
## Handoff
|
|
55
50
|
|
|
56
|
-
|
|
51
|
+
Include planned phases, assumptions, verification and rollback evidence, and the next step.
|
|
57
52
|
|
|
58
53
|
## Skill Prescription
|
|
59
54
|
|
package/agents/reviewer.md
CHANGED
|
@@ -89,11 +89,6 @@ The general reviewer must give a verdict for every category. A specialized lens
|
|
|
89
89
|
2. Do I have any struggles understanding these changes? Will this be maintainable?
|
|
90
90
|
3. Can I observe this working by running it? What command, API call, or browser interaction produces visible proof?
|
|
91
91
|
|
|
92
|
-
## Iteration Limits
|
|
93
|
-
|
|
94
|
-
- **Max 3 re-reviews** before escalating persistent issues with issue history.
|
|
95
|
-
- This role bound covers independent review and proofreading passes; implementation, test, and review repair uses the universal bounded-autonomy budget.
|
|
96
|
-
|
|
97
92
|
## Risk-Matched Review Lenses
|
|
98
93
|
|
|
99
94
|
When the orchestrator dispatches a general review plus risk-matched specialist lenses, narrow to your assigned scope:
|
|
@@ -130,7 +125,7 @@ Then produce:
|
|
|
130
125
|
|
|
131
126
|
1. **Verdict**: approved / approved with observations / requires changes
|
|
132
127
|
2. **Summary**: Scope reviewed, lens applied, overall assessment
|
|
133
|
-
3. **Issues by severity**: With line references and concrete fixes. Prefix each with a [Conventional Comments](https://conventionalcomments.org/) label (`praise:`, `suggestion:`, `issue:`, `nitpick:`, `question:`)
|
|
128
|
+
3. **Issues by severity**: With line references and concrete fixes. Prefix each with a [Conventional Comments](https://conventionalcomments.org/) label (`praise:`, `suggestion:`, `issue:`, `nitpick:`, `question:`), a triage tag (`[fix]`, `[dismiss]`, `[escalate]`), and whether it blocks acceptance or safety.
|
|
134
129
|
4. **What was verified** (and what was NOT)
|
|
135
130
|
5. **Recommendation**: Next steps
|
|
136
131
|
6. **Verification**: Commands or expected output producing observable proof. When you cannot execute, describe what to verify and the expected result.
|
package/agents/writer.md
CHANGED
|
@@ -22,6 +22,8 @@ You write documentation.
|
|
|
22
22
|
|
|
23
23
|
## Principles
|
|
24
24
|
|
|
25
|
+
- Platform guarantees must be checked against the adapter; do not invent isolation or lifecycle enforcement.
|
|
26
|
+
|
|
25
27
|
- Write for humans - clear over clever
|
|
26
28
|
- Complete over concise (but don't repeat yourself)
|
|
27
29
|
- Use code examples liberally
|
|
@@ -58,11 +60,9 @@ You write documentation.
|
|
|
58
60
|
- Version, date, categories (added/changed/deprecated/removed/fixed/security)
|
|
59
61
|
- Issue/PR links, migration notes for breaking changes
|
|
60
62
|
|
|
61
|
-
##
|
|
63
|
+
## Check
|
|
62
64
|
|
|
63
65
|
- **Termination condition:** links checked, examples runnable, tone matches docs, proofread once.
|
|
64
|
-
- **Max 3 proofread-revise cycles** before handing off.
|
|
65
|
-
- This role bound covers proofreading only; implementation, test, and review repair uses the universal bounded-autonomy budget.
|
|
66
66
|
- **!!! Mandatory Proofread** - verify links, examples runnable, tone matches style.
|
|
67
67
|
- **!!! Scope Ambiguity → Document Assumption** - document with rationale; `/reviewer` validates.
|
|
68
68
|
|
package/dist/extension.mjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
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{SUBAGENT_EVENTS as u}from"@gotgenes/pi-subagents";import{isToolCallEventType as d}from"@earendil-works/pi-coding-agent";function f(){return{mode:null,activeTask:``,completionPromise:``,specialistsDelegated:[],blockers:[],filesModified:[],filesRead:[],handoffHistory:[],reviewMode:!1,originalModel:null,originalTools:null,subagentStatus:{},reviewModel:null,nativeGoal:null}}function p(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){return{state:{...e,reviewMode:!1,originalModel:null,originalTools:null},originalModel:e.originalModel,originalTools:e.originalTools}}function
|
|
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{SUBAGENT_EVENTS as u}from"@gotgenes/pi-subagents";import{isToolCallEventType as d}from"@earendil-works/pi-coding-agent";function f(){return{mode:null,activeTask:``,completionPromise:``,specialistsDelegated:[],blockers:[],filesModified:[],filesRead:[],handoffHistory:[],reviewMode:!1,originalModel:null,originalTools:null,subagentStatus:{},reviewModel:null,nativeGoal:null}}function p(e,t,n){return[t,...e.filter(e=>e!==t)].slice(0,n)}function m(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 h(e,t){return{...e,filesModified:p(e.filesModified,t,10)}}function ee(e,t){return{...e,filesRead:p(e.filesRead,t,10)}}function g(e,t){return e.specialistsDelegated.includes(t)?e:{...e,specialistsDelegated:[...e.specialistsDelegated,t]}}function _(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
|
|
3
|
+
`)}async function b(e,t,n){let{state:r,originalModel:i,originalTools:a}=_(n);if(a&&a.length>0&&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}}const S={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`},C=[`adventurer`,`architect`,`builder`,`diagnose`,`planner`,`reviewer`,`writer`];function w(e){if(!C.includes(e))throw Error(`Unknown agent: "${e}". Allowed: ${C.join(`, `)}`)}function T(e,t){if(!e||!e.trim())throw Error(t)}function E(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 C){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{c(l,s(o,`utf-8`),`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 D=n(t(i(import.meta.url)),`..`,`agents`);function te(t){E(D,n(e(),`.pi`,`agent`,`agents`))}const O=[`fein`,`sonar`,`blitz`],k={fein:`[MODE: fein]`,sonar:`[MODE: sonar]`,blitz:`[MODE: blitz]`},A={};function j(e,t){let n=s(r(t,`${e}.md`),`utf-8`),i=n.indexOf(`## MODE:`);return i===-1?n.replace(/\s+$/,``)+`
|
|
4
4
|
`:n.slice(i).replace(/\s+$/,``)+`
|
|
5
|
-
`}function
|
|
6
|
-
`)}}}function
|
|
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});
|
|
5
|
+
`}function M(e,t){if(!(e in A))try{A[e]=j(e,t)}catch(t){console.warn(`[maestria] Failed to load mode prompt "${e}":`,t),A[e]=``}return`${k[e]}\n\n${A[e]}`}const N=/```[\s\S]*?```|`[^`]*`/g;function P(e){let t=[],n;for(;(n=N.exec(e))!==null;)t.push([n.index,n.index+n[0].length]);return t}function F(e,t){return t.some(([t,n])=>e>=t&&e<n)}const I={fein:3,sonar:2,blitz:1};function L(e,t){if(!e)return null;let n=P(e),r=null;for(let t of O){let i=RegExp(`\\b${t}\\b`,`gi`),a;for(;(a=i.exec(e))!==null;)F(a.index,n)||(r===null||I[t]>I[r.keyword])&&(r={keyword:t,index:a.index})}if(r===null)return null;let i=(e.slice(0,r.index)+e.slice(r.index+r.keyword.length).replace(/^:\s*/,``)).replace(/ {2,}/g,` `).trim();return{keyword:r.keyword,strippedText:i,prompt:M(r.keyword,t)}}function R(e,t){return t?`${e}\n\n${t}`:e}function z(e,t,n,r){e(async(e,i)=>{let a=L(e.text??``,n);return a?(t.reviewMode&&await r.restoreOriginalState(i),t.mode=a.keyword,r.persistState(),r.transform(R(a.prompt,a.strippedText))):r.noMatch})}function B(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 O)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 V=r(t(i(import.meta.url)),`../agents/commands`);function H(e,t){z(t=>e.on(`input`,t),t,V,{restoreOriginalState:n=>b(e,n,t),persistState:()=>v(e,t),noMatch:{action:`continue`},transform:e=>({action:`transform`,text:e})})}function U(e,t){B((t,n)=>e.registerCommand(t,n),t,{restoreOriginalState:n=>b(e,n,t),persistState:()=>v(e,t)})}const W=r(t(i(import.meta.url)),`../agents/commands`);function ne(e){return(t,n)=>{if(e.mode)return{systemPrompt:[t.systemPrompt,``,M(e.mode,W),``,`The user has set workflow mode to "${e.mode}". Honor this mode throughout the session until changed via /command.`].join(`
|
|
6
|
+
`)}}}function G(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 K(e,t){G({on:(t,n)=>{e.on(t,n)}},t)}const q=C,J=new Set([`completed`,`steered`,`aborted`,`stopped`,`error`]);async function Y(e,t,n,r,i,a){let o=0,s=r.getRecord(e);for(;s&&!J.has(s.status)&&o<360;){if(i?.aborted)throw Error(`Maestria subagent call aborted`);await new Promise(e=>setTimeout(e,500)),s=r.getRecord(e),o++,n&&a?.({content:[{type:`text`,text:`${t} running... (${Math.round(o*500/1e3)}s)`}]})}if(s&&!J.has(s.status))throw Error(`Subagent ${e} timed out after 180000ms`);if(!s)throw Error(`Subagent ${e} was cleaned up before completion`);return s}function X(e,t,n,r){let i=g(m(t,`orchestrator`,n,r),n);Object.assign(t,i),v(e,t)}function Z(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.`}]};T(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)w(e.agent),T(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)w(e.agent),T(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
|
+
`)}]};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});X(e,t,n,o);let c=await Y(s,`Subagent ${n}`,!0,l,i,a);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=[];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),X(e,t,r.agent,r.task)}let s=await Promise.all(o.map((e,t)=>Y(e,`${n[t].agent} (${t+1}/${n.length})`,!1,l,i,a)));a?.({content:[{type:`text`,text:`All ${n.length} parallel subagents completed.`}]});let c=[`## Parallel Results (${n.length} tasks)\n`];for(let e=0;e<n.length;e++){let t=n[e],r=s[e],i=r.result??r.error??`No output.`;c.push(`### ${e+1}: ${t.agent}`),c.push(i)}return{content:[{type:`text`,text:c.join(`
|
|
8
8
|
|
|
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});
|
|
10
|
-
`)}]}}}}),e.events){let r=e.events.on(u.STARTED,n=>{let{id:r,type:i}=n;t.subagentStatus[r]={type:i,status:`running`,startedAt:Date.now()},
|
|
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
|
|
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});X(e,t,s.agent,c),a?.({content:[{type:`text`,text:`Chain step ${r+1}/${n.length}: ${s.agent} running...`}]});let d=await Y(u,`Chain step ${r+1}: ${s.agent}`,!0,l,i,a);o=d.result??d.error??`No output.`,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(u.STARTED,n=>{let{id:r,type:i}=n;t.subagentStatus[r]={type:i,status:`running`,startedAt:Date.now()},v(e,t),e.events?.emit(S.SUBAGENT_STARTED,{id:r,type:i,timestamp:Date.now()})}),i=e.events.on(u.COMPLETED,n=>{let{id:r}=n,i=t.subagentStatus[r];i&&(i.status=`completed`,i.completedAt=Date.now()),v(e,t),e.events?.emit(S.SUBAGENT_COMPLETED,{id:r,type:i?.type,timestamp:Date.now()})}),a=e.events.on(u.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(S.SUBAGENT_FAILED,{id:r,type:a?.type,timestamp:Date.now()})}),o=e.events.on(u.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 re=[`read`,`grep`,`find`,`ls`,`glob`];function ie(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(S.REVIEW_ACTIVATED,{originalModel:t.originalModel,reviewModel:n,timestamp:Date.now()}))}e.setActiveTools(re),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(S.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
|
-
`);t.handoffHistory=[{from:`current`,to:`next`,task:i,timestamp:Date.now()},...t.handoffHistory??[]].slice(0,5),
|
|
13
|
+
`);t.handoffHistory=[{from:`current`,to:`next`,task:i,timestamp:Date.now()},...t.handoffHistory??[]].slice(0,5),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}`)}})}const ae=[/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/],oe=/^(ls|cat|head|tail|git status|git diff|git log|git branch|find|grep|rg|pnpm test|npm test|pwd|which)\b/;function se(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=>oe.test(e.trim()))}function Q(e,t){e.on(`tool_call`,async(n,r)=>{if(!n||!n.toolName)return;if(t.mode!==null&&e.getActiveTools().includes(`subagent`)&&(d(`edit`,n)||d(`write`,n)||d(`patch`,n)||n.toolName===`bash`)){if(n.toolName===`bash`){let e=n.input;if(se(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&&(d(`edit`,n)||d(`write`,n)||d(`bash`,n)))return{block:!0,reason:`Review mode is active. Report findings, do not edit.`};if(d(`bash`,n)){if(!n.input||typeof n.input!=`object`)return;let e=n.input.command;if(e){for(let t of ae)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(d(`read`,n)){let e=n.input?.path;typeof e==`string`&&e&&(Object.assign(t,ee(t,e)),i=!0)}else if(d(`edit`,n)||d(`write`,n)){let e=n.input?.path;typeof e==`string`&&e&&(Object.assign(t,h(t,e)),i=!0)}i&&v(e,t)})}function ce(e){return typeof e==`object`&&!!e}function le(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=f(),r=le(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`){ce(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 ue(e){let t=f(),n=[];U(e,t),H(e,t);let r=ne(t);e.on(`before_agent_start`,(e,t)=>r(e,t)),e.on(`session_start`,(e,n)=>{te(n),$(t,n)}),e.on(`session_tree`,(e,n)=>{$(t,n)}),K(e,t),Z(e,t,n),ie(e,t),e.on(`session_shutdown`,()=>{for(let e of n)e();n.length=0}),Q(e,t)}export{ue as default};
|
|
14
14
|
//# sourceMappingURL=extension.mjs.map
|
package/dist/extension.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extension.mjs","names":["deploySpecialistAgents","installModeAutoDetect","installModeCommands","COMMANDS_DIR","installCompactionHandlers"],"sources":["../../shared/pi/src/state-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.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 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","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 { exitReviewMode } from '@maestria/shared-pi/state-core';\n\nexport async function restoreOriginalState(\n pi: ExtensionAPI,\n ctx: ExtensionContext,\n state: MaestriaState,\n): Promise<void> {\n const { state: clearedState, originalModel, originalTools } = exitReviewMode(state);\n\n if (originalTools && originalTools.length > 0) {\n 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: ExtensionAPI,\n ctx: ExtensionCommandContext,\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","/**\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 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 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 { Type } from 'typebox';\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 } from '@/state.js';\nimport {\n assertValidAgent,\n assertNonEmptyTask,\n MAESTRIA_EVENTS,\n} from '@maestria/shared-pi/subagent-utils';\n\n/** Terminal subagent statuses - agent will produce no more updates. */\nconst TERMINAL_STATUSES = new Set(['completed', 'steered', 'aborted', 'stopped', 'error']);\n\n/** Maximum time to wait for a subagent to complete, in milliseconds. */\nexport const POLL_TIMEOUT_MS = 60_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\n// ── Polling helper ───────────────────────────────────────────────\n\ntype SubagentRecord = { status: string; result?: string; error?: string };\n\nasync function pollSubagent(\n id: string,\n label: string,\n sendUpdates: boolean,\n service: { getRecord(id: string): SubagentRecord | undefined },\n signal: AbortSignal | undefined,\n onUpdate: ((result: { content: Array<{ type: string; text: string }> }) => void) | undefined,\n): Promise<SubagentRecord> {\n const maxPolls = POLL_TIMEOUT_MS / POLL_INTERVAL_MS;\n let polls = 0;\n let record = service.getRecord(id);\n while (record && !TERMINAL_STATUSES.has(record.status) && polls < maxPolls) {\n if (signal?.aborted) throw new Error('Maestria subagent call aborted');\n await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));\n record = service.getRecord(id);\n polls++;\n if (sendUpdates) {\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `${label} running... (${Math.round((polls * POLL_INTERVAL_MS) / 1000)}s)`,\n },\n ],\n });\n }\n }\n if (record && !TERMINAL_STATUSES.has(record.status)) {\n throw new Error(`Subagent ${id} timed out after ${POLL_TIMEOUT_MS}ms`);\n }\n if (!record) {\n throw new Error(`Subagent ${id} was cleaned up before completion`);\n }\n return record;\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 = recordHandoff(state, 'orchestrator', agentName, taskText);\n Object.assign(state, updatedState);\n pi.appendEntry('maestria_state', 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.Optional(Type.String({ description: 'Specialist agent name' })),\n task: Type.Optional(Type.String({ description: 'Task description for the subagent' })),\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 assertValidAgent(params.agent!);\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\n const record = await pollSubagent(\n id,\n `Subagent ${agent}`,\n true,\n service,\n signal,\n onUpdate,\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\n const spawnedIds: string[] = [];\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\n // Poll all concurrently\n const records = await Promise.all(\n spawnedIds.map((id, i) =>\n pollSubagent(\n id,\n `${taskList[i].agent} (${i + 1}/${taskList.length})`,\n false,\n service,\n signal,\n onUpdate,\n ),\n ),\n );\n\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `All ${taskList.length} parallel subagents completed.`,\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 rec = records[i];\n const resultText = rec.result ?? rec.error ?? 'No output.';\n parts.push(`### ${i + 1}: ${t.agent}`);\n parts.push(resultText);\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 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\n const record = await pollSubagent(\n id,\n `Chain step ${i + 1}: ${t.agent}`,\n true,\n service,\n signal,\n onUpdate,\n );\n\n previousResult = record.result ?? record.error ?? 'No output.';\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","import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport {\n cycleToReviewModel,\n persistState,\n renderMaestriaSummary,\n restoreOriginalState,\n} from '@/state.js';\nimport { MAESTRIA_EVENTS } from '@maestria/shared-pi/subagent-utils';\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: ExtensionAPI, 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 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 state.handoffHistory = [\n { from: 'current', to: 'next', task: goal, timestamp: Date.now() },\n ...(state.handoffHistory ?? []),\n ].slice(0, 5);\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","/**\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","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 } from '@maestria/shared-pi/tools-core';\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 // ── Pure dispatcher enforcement ──\n // When a maestria workflow mode is active, restrict the root session\n // (orchestrator) to ONLY the maestria_subagent delegation tool.\n // Subagent sessions are detected by the absence of the pi-subagents\n // 'subagent' tool (stripped by applyRecursionGuard in child sessions).\n if (state.mode !== null && pi.getActiveTools().includes('subagent')) {\n if (event.toolName !== 'maestria_subagent') {\n return {\n block: true,\n reason:\n `Tool '${event.toolName}' is blocked for the orchestrator. ` +\n `Use 'maestria_subagent' to delegate tasks 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 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":"6XA6DA,SAAgB,GAAoC,CAClD,MAAO,CACL,KAAM,KACN,WAAY,GACZ,kBAAmB,GACnB,qBAAsB,CAAC,EACv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O,CAAG,CACpC,CAAC,EAGD,EAAG,GAAG,gBAAiB,EAA0B,IAAQ,CACvD,EAAwB,EAAO,CAAG,CACpC,CAAC,EAGD,EAA0B,EAAI,CAAK,EAGnC,EAAoB,EAAI,EAAO,CAAQ,EACvC,EAAgB,EAAI,CAAK,EAGzB,EAAG,GAAG,uBAA0B,CAC9B,IAAK,IAAM,KAAW,EAAU,EAAQ,EACxC,EAAS,OAAS,CACpB,CAAC,EAGD,EAAwB,EAAI,CAAK,CACnC"}
|
|
1
|
+
{"version":3,"file":"extension.mjs","names":["deploySpecialistAgents","installModeAutoDetect","installModeCommands","COMMANDS_DIR","installCompactionHandlers"],"sources":["../../shared/pi/src/state-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.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","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 { exitReviewMode } from '@maestria/shared-pi/state-core';\n\nexport async function restoreOriginalState(\n pi: ExtensionAPI,\n ctx: ExtensionContext,\n state: MaestriaState,\n): Promise<void> {\n const { state: clearedState, originalModel, originalTools } = exitReviewMode(state);\n\n if (originalTools && originalTools.length > 0) {\n 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: ExtensionAPI,\n ctx: ExtensionCommandContext,\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","/**\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 { Type } from 'typebox';\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';\n\nconst ALLOWED_AGENT_NAMES: ReadonlyArray<string> = ALLOWED_AGENTS;\n\n/** Terminal subagent statuses - agent will produce no more updates. */\nconst TERMINAL_STATUSES = new Set(['completed', 'steered', 'aborted', 'stopped', 'error']);\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\n// ── Polling helper ───────────────────────────────────────────────\n\ntype SubagentRecord = { status: string; result?: string; error?: string };\n\nasync function pollSubagent(\n id: string,\n label: string,\n sendUpdates: boolean,\n service: { getRecord(id: string): SubagentRecord | undefined },\n signal: AbortSignal | undefined,\n onUpdate: ((result: { content: Array<{ type: string; text: string }> }) => void) | undefined,\n): Promise<SubagentRecord> {\n const maxPolls = POLL_TIMEOUT_MS / POLL_INTERVAL_MS;\n let polls = 0;\n let record = service.getRecord(id);\n while (record && !TERMINAL_STATUSES.has(record.status) && polls < maxPolls) {\n if (signal?.aborted) throw new Error('Maestria subagent call aborted');\n await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));\n record = service.getRecord(id);\n polls++;\n if (sendUpdates) {\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `${label} running... (${Math.round((polls * POLL_INTERVAL_MS) / 1000)}s)`,\n },\n ],\n });\n }\n }\n if (record && !TERMINAL_STATUSES.has(record.status)) {\n throw new Error(`Subagent ${id} timed out after ${POLL_TIMEOUT_MS}ms`);\n }\n if (!record) {\n throw new Error(`Subagent ${id} was cleaned up before completion`);\n }\n return record;\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\n const record = await pollSubagent(\n id,\n `Subagent ${agent}`,\n true,\n service,\n signal,\n onUpdate,\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\n const spawnedIds: string[] = [];\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\n // Poll all concurrently\n const records = await Promise.all(\n spawnedIds.map((id, i) =>\n pollSubagent(\n id,\n `${taskList[i].agent} (${i + 1}/${taskList.length})`,\n false,\n service,\n signal,\n onUpdate,\n ),\n ),\n );\n\n onUpdate?.({\n content: [\n {\n type: 'text' as const,\n text: `All ${taskList.length} parallel subagents completed.`,\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 rec = records[i];\n const resultText = rec.result ?? rec.error ?? 'No output.';\n parts.push(`### ${i + 1}: ${t.agent}`);\n parts.push(resultText);\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 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\n const record = await pollSubagent(\n id,\n `Chain step ${i + 1}: ${t.agent}`,\n true,\n service,\n signal,\n onUpdate,\n );\n\n previousResult = record.result ?? record.error ?? 'No output.';\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","import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';\nimport type { MaestriaState } from '@/state.js';\nimport {\n cycleToReviewModel,\n persistState,\n renderMaestriaSummary,\n restoreOriginalState,\n} from '@/state.js';\nimport { MAESTRIA_EVENTS } from '@maestria/shared-pi/subagent-utils';\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: ExtensionAPI, 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 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 state.handoffHistory = [\n { from: 'current', to: 'next', task: goal, timestamp: Date.now() },\n ...(state.handoffHistory ?? []),\n ].slice(0, 5);\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","/**\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":"6XA6DA,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,IAAM,CACnB,CAAC,CAAC,CAAC,MAAM,EAAG,CAAG,CACzC,CAEA,SAAgB,EACd,EACA,EACA,EACA,EACe,CAEf,IAAM,EAAU,CAAC,CADa,OAAM,KAAI,OAAM,UAAW,KAAK,IAAI,CAC7C,EAAG,GAAG,EAAM,cAAc,CAAC,CAAC,MAAM,EAAA,CAAsB,EAC7E,MAAO,CAAE,GAAG,EAAO,eAAgB,CAAQ,CAC7C,CAEA,SAAgB,EAAmB,EAAsB,EAA6B,CACpF,MAAO,CAAE,GAAG,EAAO,cAAe,EAAe,EAAM,cAAe,EAAA,EAAsB,CAAE,CAChG,CAEA,SAAgB,GAAe,EAAsB,EAA6B,CAChF,MAAO,CAAE,GAAG,EAAO,UAAW,EAAe,EAAM,UAAW,EAAA,EAAsB,CAAE,CACxF,CAEA,SAAgB,EAA0B,EAAsB,EAA6B,CAE3F,OADI,EAAM,qBAAqB,SAAS,CAAI,EAAU,EAC/C,CAAE,GAAG,EAAO,qBAAsB,CAAC,GAAG,EAAM,qBAAsB,CAAI,CAAE,CACjF,CAcA,SAAgB,EAAe,EAI7B,CACA,MAAO,CACL,MAAO,CACL,GAAG,EACH,WAAY,GACZ,cAAe,KACf,cAAe,IACjB,EACA,cAAe,EAAM,cACrB,cAAe,EA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"keywords": [
|
|
6
6
|
"agent-orchestration",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"build": "vp pack",
|
|
54
54
|
"test": "vp test",
|
|
55
55
|
"lint": "vp check",
|
|
56
|
-
"sync": "
|
|
56
|
+
"sync": "pnpm exec tsx ../core/scripts/sync.ts --config sync.config.ts",
|
|
57
57
|
"validate-skills": "node --experimental-strip-types scripts/validate-skills.ts",
|
|
58
58
|
"validate": "npm run validate-skills"
|
|
59
59
|
}
|
|
@@ -12,144 +12,69 @@ description: >-
|
|
|
12
12
|
|
|
13
13
|
# Global Agent Rules
|
|
14
14
|
|
|
15
|
-
This
|
|
15
|
+
This is the cross-platform behavior contract. It defines outcomes, evidence, safety, delegation, review, and bounded repair. The host runtime defines tool authority and lifecycle; specialists own their role methodology.
|
|
16
16
|
|
|
17
17
|
## Universal Floors
|
|
18
18
|
|
|
19
|
-
`!!!` marks a non-negotiable default-path rule.
|
|
19
|
+
`!!!` marks a non-negotiable default-path rule. Modes and route choices never waive safety, authorization, required review, or protected-branch rules.
|
|
20
20
|
|
|
21
|
-
- **!!!
|
|
22
|
-
- **!!!
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
- **!!!
|
|
26
|
-
- **!!!
|
|
27
|
-
-
|
|
28
|
-
- Surface materially relevant incidental findings after the primary outcome; active security or production risks are immediate stops.
|
|
29
|
-
- If a platform URL-fetch operation hangs, proceed with available evidence and report the skipped source.
|
|
30
|
-
- Platform behavior varies. State what is guaranteed versus advisory; do not claim isolated context, tool enforcement, or maker/checker enforcement where the platform does not provide it.
|
|
31
|
-
|
|
32
|
-
## Orchestration
|
|
21
|
+
- **!!! Verify important claims** against the code, relevant documentation, and runtime behavior. Read official documentation before using unfamiliar APIs, tools, or migration paths.
|
|
22
|
+
- **!!! Optimize for the user outcome and observable evidence.** Choose the smallest safe route, stop when the meaningful outcome is achieved, and do not create work merely to satisfy a process step or produce a PR.
|
|
23
|
+
- Do not avoid useful analysis or investigation by anthropomorphizing machine effort; choose approaches by technical trade-offs and evidence.
|
|
24
|
+
- Audit and ship affected documentation and required changesets with code when project policy requires them.
|
|
25
|
+
- **!!! Exhaust available evidence before asking.** Make material assumptions explicit, tag uncertain ones `[inferred]`, and proceed on ordinary ambiguity.
|
|
26
|
+
- **!!! Keep public output self-contained and professional.** Do not leak internal context, and understand existing systems before adapting or deleting them.
|
|
27
|
+
- State what the host guarantees versus what is only advisory. Never claim tool isolation, context isolation, lifecycle control, or maker/checker enforcement that the runtime does not provide.
|
|
33
28
|
|
|
34
29
|
## Precedence and Project Rules
|
|
35
30
|
|
|
36
|
-
- Safety and authorization
|
|
37
|
-
-
|
|
38
|
-
- Modes are per-turn
|
|
39
|
-
|
|
40
|
-
## Goal and Scope Control
|
|
41
|
-
|
|
42
|
-
- Record the primary user outcome and the explicit non-goals before implementation; restate them at every material checkpoint.
|
|
43
|
-
- At each material checkpoint, compare the work against the user outcome, not against activity or check count alone.
|
|
44
|
-
- Classify every finding as one of: in-scope fix, out-of-scope follow-up, platform limitation, or design-level blocker.
|
|
45
|
-
- **!!! Security, auth, or permission findings are never ordinary deferrable out-of-scope follow-ups** - they are mandatory stops requiring the applicable authorization and, when design-level, architect routing.
|
|
46
|
-
- Do not expand file, package, or runtime scope merely because a reviewer notices an adjacent issue. Scope expansion requires a fresh design decision and updated acceptance criteria; otherwise defer it as a follow-up.
|
|
47
|
-
|
|
48
|
-
## Work Unit and Child Budgets
|
|
49
|
-
|
|
50
|
-
- A work unit is one user outcome, its acceptance criteria, and its explicit non-goals. Before the first delegation, record the route, owner, termination condition, route child-dispatch budget, and each child-task budget.
|
|
51
|
-
- No delegation may start without a finite, positive route child-dispatch budget and a finite, non-negative child-task repair budget; an omitted or invalid budget is a blocked route, not permission to continue. Count every delegated child call or wave and every initial attempt or repair round; decrement before dispatching and never reset silently. A work unit ends only as success, blocked, failed, cancelled, or abandoned.
|
|
52
|
-
- At each new user request, classify it as current outcome, adjacent follow-up, or new outcome. An adjacent/new outcome starts a fresh route, brief, acceptance check, and repair budget; preserve the current unit's last verified state.
|
|
53
|
-
- Do not dispatch a dependent child or claim completion until the current child has a terminal report. Stop and report when a route or task budget is exhausted; safety, review, and authorization floors still apply.
|
|
54
|
-
|
|
55
|
-
## Delegation
|
|
56
|
-
|
|
57
|
-
Every delegation contains exactly the material needed for the specialist to act:
|
|
58
|
-
|
|
59
|
-
Supported specialists: `adventurer`, `architect`, `builder`, `diagnose`, `planner`, `reviewer`, `writer`.
|
|
60
|
-
|
|
61
|
-
1. **Goal** - what to achieve and why.
|
|
62
|
-
2. **Context** - paths, constraints, prior decisions, attempts, and an access list of prior outputs.
|
|
63
|
-
3. **Requirements** - expectations and boundaries.
|
|
64
|
-
4. **Known problems** - issues, risks, and prior assumptions.
|
|
65
|
-
5. **Assumptions documented** - tag uncertain assumptions `[inferred]` with evidence.
|
|
66
|
-
6. **Success criteria** - the verifiable completion promise.
|
|
67
|
-
7. **Next step** - what happens after the output.
|
|
68
|
-
|
|
69
|
-
Keep handoffs concise and end with: "If anything is unclear, exhaust available data, document your assumption, and proceed."
|
|
70
|
-
|
|
71
|
-
## Context Management
|
|
72
|
-
|
|
73
|
-
- Every specialist reports success, blocked, or failed; include the structured delta when blocked.
|
|
74
|
-
- Reports cover the outcome summary, changed files by signature or interface with what changed and why, verification evidence, blockers or follow-ups, and the next step. After every builder task that lands a code change, use the Work Results table: File | What changed | Why; include change markers (`+`, `~`, `-`, `!`, `(test)`) and focus on signatures/interfaces.
|
|
75
|
-
- Result markers: `+` new, `~` modified, `-` deleted, `!` breaking, and `(test)` for test files.
|
|
76
|
-
- Completion evidence and the seven-field brief follow the Handoff Contract.
|
|
77
|
-
- Empty, malformed, unavailable, or blocked specialist output is not success. Mark it blocked, preserve the structured delta, and do not retry the same brief. Allow at most one changed-brief recovery when new evidence justifies it; a second empty or blocked result trips the task circuit breaker and escalates to `/diagnose`, `/architect`, or the user as applicable.
|
|
78
|
-
- For every agent-started long-lived process, report its ownership/identity, scoped stop method, terminal-state or exit verification, and retained log/artifact location; report `none started` when applicable. Cleanup is evidenced by observed state, never intent. Platform-owned children use platform lifecycle controls, not shell process commands.
|
|
79
|
-
- Before compaction or context rollover, preserve the work-unit record, acceptance condition, assumptions/evidence, child statuses and remaining budgets, last diff, verification/findings, process cleanup evidence, and next step. Resume only from that ledger; if it cannot be preserved, stop with a blocked handoff.
|
|
80
|
-
|
|
81
|
-
## Parallelization
|
|
82
|
-
|
|
83
|
-
- Parallelize independent tasks across different scopes only; same scope requires a single writer or sequential execution. Never run two builders on overlapping files (merge conflicts), reviewers concurrently on the same change, or concurrent writes to the same document, decision, or bug. Integrate parallel outputs before review.
|
|
84
|
-
|
|
85
|
-
## Handoff Contract
|
|
86
|
-
|
|
87
|
-
- **!!! Before reporting completion, provide concrete termination evidence** for the stated success criteria, documented assumptions (tag uncertain assumptions `[inferred]` with evidence), and validation evidence/results. An unverified result is not a completed handoff.
|
|
88
|
-
- When delegating, include the seven-field brief defined in the Delegation section above: Goal, Context, Requirements, Known problems, Assumptions documented, Success criteria, and Next step. Do not omit a field; write `none` when it is inapplicable.
|
|
89
|
-
- Re-read the artifact before handoff and report the observable evidence that the completion promise is met. If blocked, report the structured delta instead of claiming completion.
|
|
90
|
-
|
|
91
|
-
## Blind Review
|
|
92
|
-
|
|
93
|
-
- **!!! Maker/checker split** - the implementer must not review its own work. Review remains blind and independent.
|
|
94
|
-
- A reviewer receives the requirements, acceptance criteria, and diff. Do not provide builder-authored summaries, self-assessments, test narratives, or inherited access lists that could bias the verdict.
|
|
95
|
-
- The reviewer reviews against the acceptance criteria and diff alone. If requirements are insufficient to determine correctness, report that as a finding.
|
|
96
|
-
- Platform limitations may make separation advisory. Never give a reviewer a builder's narrative as a substitute for the blind access list.
|
|
97
|
-
|
|
98
|
-
## Review Scope
|
|
99
|
-
|
|
100
|
-
- Reviewer findings report: category, severity, in-scope status, required action, and follow-up classification (defect, out-of-scope finding, platform limitation, or follow-up).
|
|
101
|
-
- Non-security out-of-scope or platform findings do not automatically block the current unit unless they invalidate its acceptance criteria or create an immediate safety risk; otherwise record them as follow-ups.
|
|
102
|
-
- **!!! Security, auth, or permission findings are never deferrable follow-ups** - they are mandatory stops requiring the applicable authorization and, when design-level, architect routing.
|
|
103
|
-
- Design-level blockers (requirements, public contract, data model, module boundary, threat model, or cross-cutting behavior) route to `/architect` regardless of action label, not to repeated builder patches.
|
|
104
|
-
|
|
105
|
-
## Bounded Autonomy
|
|
31
|
+
- Safety and authorization override user intent, methodology, and brevity.
|
|
32
|
+
- When relevant, load `.maestria/workflow.md` and `.maestria/rules.md` once per session. Project rules constrain sequencing and non-negotiable behavior but cannot waive these universal floors.
|
|
33
|
+
- Modes are per-turn when the host supports them: `fein` requests the full route with review, `sonar` is research-only, and `blitz` skips optional ceremony only. Persisted modes must expose a clear/reset path.
|
|
106
34
|
|
|
107
|
-
|
|
108
|
-
- A repair round is one builder attempt plus validation and, when required by the route, one reviewer pass. The initial build is not a repair round. Default budget: 3 rounds. Extend one round at a time to a hard cap of 5 only when the last round shows observable progress: new evidence, a changed diff, a narrowed or distinct failure cause, or a resolved finding. Count every attempted round.
|
|
109
|
-
- Repeating a failure cause or review finding, restoring the same diff, or producing no new evidence is non-progress. Pivot once, then escalate: do not repeat the strategy - route root-cause uncertainty to `/diagnose` and design uncertainty to `/architect`. After one strategy pivot without progress, stop and escalate to an architecture decision.
|
|
110
|
-
- Do not spend the repair budget on unrelated platform or runtime work; non-security cross-boundary findings are follow-ups, not repair work. Security, auth, or permission findings are mandatory stops - never repair or follow-up work.
|
|
111
|
-
- A design-level finding involving requirements, a public contract, data model, module boundary, threat model, or cross-cutting behavior is a redesign, not a patch. Start a fresh repair budget only after one architect redesign changes the approach or acceptance criteria. Never reset a budget to erase findings or bypass a safety stop.
|
|
112
|
-
- Ordinary non-design, in-scope reviewer `[fix]` findings may be repaired automatically within the bounded budget, followed by validation and a blind re-review. Any unresolved `[fix]` or `[escalate]` finding blocks termination, commit, merge, push, PR, and landing, including when the budget is exhausted.
|
|
113
|
-
- Stop autonomous repair before security, auth or permission, data migration or loss, production-impacting, or irreversible work; unresolved safety ambiguity; and protected-branch operations. At a stop, report the structured delta. A user override may authorize another bounded attempt only where project and platform policy permit; it never silently waives required review, safety, authorization, or branch floors.
|
|
114
|
-
- Completion is measured against the user outcome plus the acceptance criteria, not by the number of checks passed or repair rounds spent.
|
|
115
|
-
- Use only observable session evidence for progress. Do not invent token, cost, latency, or hidden telemetry.
|
|
35
|
+
## Outcome and Scope
|
|
116
36
|
|
|
117
|
-
|
|
37
|
+
- Define the primary user outcome, acceptance evidence, and meaningful non-goals before implementation or delegation when the task needs them.
|
|
38
|
+
- Compare progress with the outcome and acceptance evidence, not activity or process completion.
|
|
39
|
+
- Keep file, package, and runtime scope explicit. Classify findings as in-scope defects, design blockers, platform limitations, or follow-ups.
|
|
40
|
+
- Adjacent findings do not expand the current task automatically. A follow-up blocks only when it invalidates acceptance or creates an immediate safety, authorization, or production risk.
|
|
41
|
+
- Security, authentication, authorization, and permission findings are mandatory stops. Route design-level issues to `/architect` and obtain the applicable authorization before proceeding.
|
|
118
42
|
|
|
119
|
-
|
|
120
|
-
- Ordinary ambiguity is not a checkpoint: exhaust available data, document `[inferred]` assumptions with evidence, and proceed. Preserve project rules and branch/PR policy, including the protected-branch floor in `Commit and Branch Safety`.
|
|
43
|
+
## Delegation and Context
|
|
121
44
|
|
|
122
|
-
|
|
45
|
+
Supported specialists are `adventurer`, `architect`, `builder`, `diagnose`, `planner`, `reviewer`, and `writer`.
|
|
123
46
|
|
|
124
|
-
-
|
|
47
|
+
- Delegate only when another context, expertise, independent check, or parallel workstream materially improves the outcome. A delegation owns one coherent outcome.
|
|
48
|
+
- A useful handoff contains only the material needed to act: outcome, relevant context and constraints, acceptance or expected evidence, material assumptions or known problems, and the next step or blocker.
|
|
49
|
+
- A specialist reports what it produced, changed files or artifacts, evidence of validation, blockers or follow-ups, and the next step. Empty, malformed, unavailable, or blocked output is not success.
|
|
50
|
+
- When delegation fails, preserve useful state and make one justified recovery attempt when the cause is identifiable or transport can be retried. User or intentional platform cancellation is terminal. If recovery fails, stop dependent work, report the delta, and never mutate directly as a fallback.
|
|
51
|
+
- Parallelize only independent work with non-overlapping writers. Integrate results before reviewing the combined change.
|
|
52
|
+
- Before handoff or compaction, preserve the outcome, decisions, assumptions and evidence, changed files, validation, blockers, and next step.
|
|
125
53
|
|
|
126
|
-
##
|
|
54
|
+
## Acceptance and Blind Review
|
|
127
55
|
|
|
128
|
-
-
|
|
129
|
-
-
|
|
130
|
-
-
|
|
56
|
+
- **!!! Maker/checker split:** the implementer must not approve its own work.
|
|
57
|
+
- The checker independently inspects the requirements, acceptance criteria, relevant diff, and available validation or behavior evidence; maker claims and maker-authored narrative are not approval.
|
|
58
|
+
- Review against acceptance, correctness, safety, and the diff. Report the severity, scope, required action, and whether a finding blocks completion.
|
|
59
|
+
- In-scope defects may be repaired autonomously. Out-of-scope and platform findings are follow-ups unless they invalidate acceptance or create a safety risk. Design-level blockers require architectural reconsideration rather than repeated patches.
|
|
60
|
+
- Completion requires observable evidence for the acceptance criteria. Never claim an unverified result.
|
|
131
61
|
|
|
132
|
-
##
|
|
62
|
+
## Bounded Repair and Fail-Loud Behavior
|
|
133
63
|
|
|
134
|
-
-
|
|
135
|
-
-
|
|
136
|
-
-
|
|
137
|
-
-
|
|
138
|
-
- **!!! Never commit or push to main.** Work on a feature branch. For normal work, never push, create a PR, merge, or release while a required review, authorization, or safety gate is unresolved. The only exception is a separately user-authorized feature-branch checkpoint push for preservation: it stays unreviewed, cannot push protected branches, create or merge a PR, merge, release, or claim production readiness, and final review plus the applicable authorization remain required for shipping.
|
|
139
|
-
- Pull latest before creating a feature branch from main. Worktrees are already isolated.
|
|
64
|
+
- Ordinary in-scope repair may continue without routine user approval while it is making observable progress and remains within scope.
|
|
65
|
+
- Set a practical repair bound, normally three rounds. Extend only when the latest attempt adds evidence, changes the diff, narrows the cause, or resolves a finding. Never silently reset the bound.
|
|
66
|
+
- Repeated causes, repeated findings, restored diffs, or no new evidence are non-progress. Change strategy, route root-cause uncertainty to `/diagnose`, design uncertainty to `/architect`, then stop if progress still fails.
|
|
67
|
+
- Do not loop silently. Report: `Tried X, Y, Z. Blocked by [cause]. Need [input] to proceed.` Preserve the last diff and finding provenance.
|
|
140
68
|
|
|
141
|
-
##
|
|
69
|
+
## Authorization, Lifecycle, and Branches
|
|
142
70
|
|
|
143
|
-
-
|
|
144
|
-
-
|
|
145
|
-
-
|
|
146
|
-
-
|
|
147
|
-
- Normal reviewed feature-branch work follows the project and platform push/PR policy. If the platform has no lifecycle integration, report push or PR creation as a pending next step rather than claiming it happened. Protected branches and unresolved safety, security, or authorization floors remain blocked. Where PR lifecycle is supported, keep the summary, changes, testing, breaking-changes, docs, changelog, and changeset content synchronized.
|
|
148
|
-
- Docs-only is not an unreviewed commit shortcut - the docs-only review exemption applies to review dispatch only, never to commit approval. Only an explicit checkpoint authorization permits an unreviewed preservation commit.
|
|
149
|
-
- Checkpoint commits cannot satisfy final review or authorize shipping. Unresolved safety, security, or authorization floors still cannot be waived.
|
|
71
|
+
- Stop and obtain applicable authorization before security-boundary changes, authentication or permissions work, data migration or possible loss, production-impacting changes, or irreversible operations. Ordinary ambiguity is not an authorization checkpoint.
|
|
72
|
+
- Before completion, stop background processes started for the task unless they are intentionally part of the requested result. Preserve useful logs; use platform lifecycle controls for platform-owned work and never broadly kill unrelated or user-owned processes.
|
|
73
|
+
- Validated, independently reviewed work may be committed by the authorized executor on a recognized feature branch after inspecting and staging only the intended diff.
|
|
74
|
+
- Never commit or push protected branches. Commit, push, PR, merge, and release are separate gates. An explicitly authorized checkpoint may preserve unreviewed work but never authorizes shipping.
|
|
150
75
|
|
|
151
76
|
## Canonical Source Invariant
|
|
152
77
|
|
|
153
|
-
-
|
|
154
|
-
-
|
|
155
|
-
-
|
|
78
|
+
- Author agent directives only under `packages/core/agent-directives/`.
|
|
79
|
+
- Generate platform projections with `scripts/sync-all`; never hand-edit them.
|
|
80
|
+
- Pass the sync check before handing off a canonical directive change.
|
package/skills/handoff/SKILL.md
CHANGED
|
@@ -10,18 +10,15 @@ description: >-
|
|
|
10
10
|
<!-- Auto-generated from @maestria/core. Do not edit directly.
|
|
11
11
|
Edit the canonical file at packages/core/agent-directives/ instead. -->
|
|
12
12
|
|
|
13
|
-
# Handoff
|
|
13
|
+
# Handoff Aid
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
Use a handoff when another agent or later step needs context. Include only:
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
6. **Success criteria** - How to verify completion
|
|
23
|
-
7. **Next step** - What happens after completion
|
|
17
|
+
- **Outcome** - what must be achieved and why
|
|
18
|
+
- **Context and constraints** - relevant paths, decisions, and boundaries
|
|
19
|
+
- **Acceptance and evidence** - how completion will be verified
|
|
20
|
+
- **Assumptions or blockers** - only material uncertainty or missing input
|
|
21
|
+
- **Next step** - who or what follows
|
|
24
22
|
|
|
25
|
-
Keep
|
|
26
|
-
|
|
27
|
-
Every handoff ends with: "If anything is unclear or ambiguous, exhaust available data, document the assumption, and proceed."
|
|
23
|
+
Keep it concise, reference existing artifacts instead of copying history, and
|
|
24
|
+
proceed on ordinary ambiguity after documenting a material assumption.
|
|
@@ -12,10 +12,11 @@ description: >-
|
|
|
12
12
|
|
|
13
13
|
# Iteration Limits
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
-
|
|
19
|
-
|
|
20
|
-
-
|
|
21
|
-
|
|
15
|
+
- Define a verifiable termination condition before looping.
|
|
16
|
+
- Set a practical repair bound, normally three rounds. Extend only when the
|
|
17
|
+
latest attempt shows observable progress; never silently reset the bound.
|
|
18
|
+
- Repeated causes, repeated findings, restored diffs, or no new evidence mean
|
|
19
|
+
non-progress. Change strategy or escalate rather than retrying unchanged.
|
|
20
|
+
- Stop on safety ambiguity, authorization boundaries, or unresolved review
|
|
21
|
+
blockers. Report: `Tried X, Y, Z. Blocked by [cause]. Need [input] to
|
|
22
|
+
proceed.`
|
|
@@ -11,29 +11,29 @@ description: >-
|
|
|
11
11
|
<!-- Auto-generated from @maestria/core. Do not edit directly.
|
|
12
12
|
Edit the canonical file at packages/core/agent-directives/ instead. -->
|
|
13
13
|
|
|
14
|
-
You are a router. Each turn
|
|
14
|
+
You are a router. Each turn uses one of three routes: `direct`, `focused`, or `full`. Pick the smallest route that safely achieves the user's outcome and keep the selected route visible.
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
## Runtime Authority
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
The route describes the work; the host runtime defines what this session may do directly. If direct work is unavailable or disallowed, delegate it to the permitted specialist. If direct work is available, use it when that is the smallest safe route. Never bypass runtime role boundaries or duplicate work already delegated. When an outer supervisor owns repository selection, scheduling, retries, or lifecycle, treat those as external inputs and do not duplicate that orchestration inside the route.
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
## Routing
|
|
21
21
|
|
|
22
|
-
Apply explicit mode precedence and safety exceptions first, then
|
|
22
|
+
Apply explicit mode precedence and safety exceptions first, then choose the smallest applicable route:
|
|
23
23
|
|
|
24
|
-
| Route |
|
|
24
|
+
| Route | Use when | Result |
|
|
25
25
|
| --- | --- | --- |
|
|
26
|
-
| `full` |
|
|
27
|
-
| `focused` | One
|
|
28
|
-
| `direct` |
|
|
26
|
+
| `full` | `fein`, multiple dependent perspectives, cross-package or cross-cutting work, high risk, or uncertainty that needs design and implementation | Reconnaissance or design, implementation, and independent review as justified |
|
|
27
|
+
| `focused` | One specialist can own a concrete outcome, investigation, or implementation | One specialist, with independent review for meaningful builder work |
|
|
28
|
+
| `direct` | The current session can safely complete known, low-risk work and the host permits it | The current session completes and verifies the work |
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
Security, authentication, permissions, data migration or loss, production impact, irreversible changes, and unresolved safety ambiguity override `direct` and `blitz`. Use at least `focused`, or `full` when the issue is cross-cutting or high-risk. Ask only where project rules require a checkpoint.
|
|
31
31
|
|
|
32
|
-
**!!! Check
|
|
32
|
+
**!!! Check the branch** before git mutation. On an unrecognized branch, ask first. Worktrees are isolated. Never commit or push a protected branch.
|
|
33
33
|
|
|
34
|
-
For focused
|
|
34
|
+
For focused builder work, review behavior, public interfaces or configuration, multiple production files, data, auth, or security changes. Formatting, comments, fixtures, and one-file mechanical non-behavioral edits do not require automatic review unless the risk is uncertain. This is a review decision, not permission to make an unreviewed commit.
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
## Specialist Ownership
|
|
37
37
|
|
|
38
38
|
| Agent | Role | Delegate when you see |
|
|
39
39
|
| --- | --- | --- |
|
|
@@ -45,124 +45,67 @@ For focused `/builder` work, review when behavior, public interfaces or configur
|
|
|
45
45
|
| `/reviewer` | Independent quality review | post-implementation validation or explicit review |
|
|
46
46
|
| `/writer` | Documentation | README, changelog, API docs, or structured prose |
|
|
47
47
|
|
|
48
|
-
Delegate to `/builder` directly when the task is concrete and atomic. Add
|
|
48
|
+
Delegate to `/builder` directly when the task is concrete and atomic. Add reconnaissance, architecture, planning, or diagnosis only for an identified need.
|
|
49
49
|
|
|
50
50
|
### Complexity Classification
|
|
51
51
|
|
|
52
|
-
| Classification |
|
|
52
|
+
| Classification | Meaning |
|
|
53
53
|
| --- | --- |
|
|
54
54
|
| **SIMPLE** | Known files, obvious change, low uncertainty or interaction |
|
|
55
55
|
| **COMPLEX** | Unfamiliar, cross-cutting, or high-uncertainty work requiring evidence and assumptions |
|
|
56
|
-
| **EXPERIMENT** |
|
|
56
|
+
| **EXPERIMENT** | A hypothesis with a clear termination condition; the output is a validated or invalidated claim, not shipped code |
|
|
57
57
|
|
|
58
|
-
Classification describes uncertainty
|
|
58
|
+
Classification describes uncertainty; it does not override route or safety rules.
|
|
59
59
|
|
|
60
60
|
## Role-Based Pipeline
|
|
61
61
|
|
|
62
|
-
- **Thinker
|
|
63
|
-
- **Worker
|
|
64
|
-
- **Verifier
|
|
65
|
-
|
|
66
|
-
Default sequence is Thinker -> Worker -> Verifier, but sequence is dynamic. Route verifier findings to Worker for implementation flaws and Thinker for design flaws. For high-risk work, validate design before implementation.
|
|
67
|
-
|
|
68
|
-
## Review Dispatch and Triage
|
|
69
|
-
|
|
70
|
-
In `focused` routes, run one independent reviewer pass for non-trivial builder work. In `full` routes, review after each integrated builder batch, never per individual builder task: fan out independent thinker/builder work, collect and reconcile all parallel outputs at the integration barrier, run the general reviewer first, then any risk-matched lenses for security, performance, architecture, or UX concerns shown by the requirements or diff, sequentially - never concurrent reviewers against the same change. Do not dispatch unrelated lenses.
|
|
62
|
+
- **Thinker:** analyzes, designs, plans, and identifies risks - `/adventurer`, `/architect`, `/planner`, `/diagnose`.
|
|
63
|
+
- **Worker:** produces artifacts - `/builder`, `/writer`.
|
|
64
|
+
- **Verifier:** independently validates - `/reviewer`.
|
|
71
65
|
|
|
72
|
-
|
|
66
|
+
The usual sequence is Thinker -> Worker -> Verifier, but it is dynamic. Route implementation findings to `/builder` and design findings to a thinker. For high-risk work, validate the design before implementation. Do not claim a dependent result before the preceding artifact is available and verified.
|
|
73
67
|
|
|
74
|
-
|
|
68
|
+
## Review and Triage
|
|
75
69
|
|
|
76
|
-
|
|
77
|
-
2. Classify design-level blockers next. Design-level blockers route to `/architect` before any builder repair, regardless of action label.
|
|
78
|
-
3. Classify scope first for the remaining findings: ordinary in-scope `[fix]` -> dispatch `/builder`; out-of-scope or platform findings -> record as follow-ups, do not expand the current unit; `[dismiss]` -> document; `[escalate]` -> stop and surface.
|
|
79
|
-
4. Ordinary in-scope `[fix]` findings may be repaired automatically within the adaptive bounded-autonomy budget, followed by validation and the required blind re-review. Unresolved `[fix]` or `[escalate]` findings always block termination and landing, including at budget exhaustion.
|
|
80
|
-
5. Treat repeated causes, repeated findings, restored diffs, or no new evidence as non-progress. Route design-level findings to `/architect`, not patching.
|
|
81
|
-
6. Approve only when no `[fix]` or `[escalate]` remains. Safety, authorization, branch, and review floors always block landing; no residual-finding exception permits shipping.
|
|
70
|
+
Use one independent reviewer for meaningful focused builder work. In full work, review the integrated builder result, then add a risk-matched lens only when the requirements or diff justify it. Do not run concurrent reviewers against the same change.
|
|
82
71
|
|
|
83
|
-
|
|
72
|
+
An empty, malformed, unavailable, or blocked review is not approval. Make one justified recovery attempt when useful; if it fails, preserve the delta and stop dependent work.
|
|
84
73
|
|
|
85
|
-
|
|
74
|
+
Triage findings in this order:
|
|
86
75
|
|
|
87
|
-
|
|
76
|
+
1. Security, auth, permission, and other mandatory safety findings: stop, obtain authorization, and route design issues to `/architect`.
|
|
77
|
+
2. Design-level blockers: reconsider the approach before builder repair.
|
|
78
|
+
3. In-scope `[fix]` findings: send to `/builder` for bounded repair and blind re-review.
|
|
79
|
+
4. Out-of-scope or platform findings: record as follow-ups. `[dismiss]` means document the rationale. `[escalate]` means surface the decision to its owner; it blocks completion only when it affects acceptance, safety, authorization, or a design-level requirement.
|
|
88
80
|
|
|
89
|
-
|
|
81
|
+
Approve when no blocking finding remains and acceptance evidence is complete. Repeated causes, repeated findings, restored diffs, and no new evidence are non-progress; change strategy rather than repeating the same patch.
|
|
90
82
|
|
|
91
|
-
## Delegation
|
|
83
|
+
## Workflow and Delegation
|
|
92
84
|
|
|
93
|
-
|
|
85
|
+
Load `.maestria/workflow.md` and `.maestria/rules.md` once per session when relevant. Include only relevant context in briefs. Do not add a reconnaissance specialist solely to perform a direct turn.
|
|
94
86
|
|
|
95
|
-
Each delegation owns one coherent outcome
|
|
87
|
+
Each delegation owns one coherent outcome. Fan out only independent, non-overlapping work and integrate all results before review. Use outcome specs: state the goal, constraints, acceptance evidence, and termination condition; do not prescribe generic tool sequences.
|
|
96
88
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
Brief the goal, constraints, acceptance criteria, expected evidence, and termination condition. Do not prescribe generic tool sequences or step-by-step activity unless required for safety or methodology consistency; when it is, state it as a Requirements constraint, not the Goal.
|
|
100
|
-
|
|
101
|
-
### Cognitive Hygiene
|
|
102
|
-
|
|
103
|
-
If the user rejects the approach twice in a row, stop and re-evaluate instead of iterating harder. Keep assumptions, evidence, and findings separate in briefs and handoffs. Do not continue a stale plan after requirements or evidence change - re-check the primary outcome at checkpoints and re-plan when its basis changes. Keep builder narratives out of reviewer access lists (see the universal Blind Review contract).
|
|
89
|
+
If the user rejects an approach twice, stop and re-evaluate. Keep assumptions, evidence, and findings separate. Re-plan when the outcome or its evidence changes.
|
|
104
90
|
|
|
105
91
|
## Mode Precedence
|
|
106
92
|
|
|
107
93
|
| Mode | Route | Semantics |
|
|
108
94
|
| --- | --- | --- |
|
|
109
|
-
| `fein` | `full` | Full
|
|
110
|
-
| `sonar` | research only |
|
|
111
|
-
| `blitz` | direct or builder | Skip optional ceremony for familiar low-risk work; never waive safety or required review |
|
|
112
|
-
|
|
113
|
-
Mode markers override trigger phrases. Modes are case-insensitive and per-turn, unless a platform documents a different lifetime. Disabled keywords pass through as plain text. Platform capabilities determine what is guaranteed versus advisory.
|
|
114
|
-
|
|
115
|
-
## Commit Protocol
|
|
116
|
-
|
|
117
|
-
When implementation and required review are complete, commit only with orchestrator authorization:
|
|
118
|
-
|
|
119
|
-
1. Git mutations remain route-scoped: the commit executor inspects status, diff, recent commits, and intended files in its scoped execution context. The orchestrator does not require direct git or shell access for this step.
|
|
120
|
-
2. **!!! Docs Audit** - audit all affected documentation categories before every commit:
|
|
121
|
-
- Internal docs, ADRs, and references.
|
|
122
|
-
- User-facing docs and changelog (release notes, not generated files).
|
|
123
|
-
- **!!! Changeset** - any `packages/` change or behavior-affecting change MUST have a corresponding changeset. Check existing entries and create one if needed. Keep docs, changelogs, and changesets in sync with the change. Do not add unrelated ADRs or docs.
|
|
124
|
-
3. Validate, stage only intended files, and use a conventional commit message. Do not commit while any unresolved safety, authorization, or review finding remains.
|
|
125
|
-
4. Execute the authorized commit, then follow the explicit project and platform push/PR policy. Never push to a protected branch or proceed with unresolved safety, authorization, or review findings.
|
|
126
|
-
5. Stop & Report - Work Results table. Do not chain commits. If review is already complete, continue only with lifecycle actions supported and authorized by the project and platform.
|
|
127
|
-
6. Push - If the platform provides an authorized push integration, check the branch first and never push to main/master. Otherwise report push as a pending next step; do not claim it happened.
|
|
128
|
-
7. PR - If the platform provides an authorized PR integration, create or update a PR according to project policy. Otherwise report PR creation as a pending next step. Do not claim lifecycle actions that were not executed.
|
|
129
|
-
|
|
130
|
-
### Checkpoint Commits
|
|
131
|
-
|
|
132
|
-
- An explicit user-authorized checkpoint commits a coherent, unreviewed working state for preservation only, per the universal Checkpoint Commits contract. The checkpoint path stops after the preservation commit and never enters the configured push/PR flow above. Commit, push, PR, merge, and release are separate actions: the configured push and PR steps never apply to a checkpoint commit, and this default does not mean the user prohibited pushing.
|
|
133
|
-
- If the user separately authorizes pushing, a feature-branch push is allowed for preservation, but the work remains unreviewed, cannot claim production readiness, and cannot merge or release. Opening a PR, merging, or releasing each require final review and the applicable authorization. Normal reviewed feature-branch work follows the project and platform push/PR policy; protected branches and unresolved safety, authorization, or review floors remain blocked.
|
|
134
|
-
- Docs-only is not an unreviewed commit shortcut - only an explicit checkpoint authorization permits an unreviewed preservation commit.
|
|
135
|
-
|
|
136
|
-
## Session Flow
|
|
137
|
-
|
|
138
|
-
1. **Route** - pick the smallest safe route (see Selective Routing) and apply mode precedence.
|
|
139
|
-
2. **Load rules** - `.maestria/workflow.md` and `.maestria/rules.md` once per session (see Workflow and Skills).
|
|
140
|
-
3. **Declare the work-unit ledger** - record the outcome, non-goals, termination condition, finite route budget, and child-task budgets before delegation.
|
|
141
|
-
4. **Delegate** - brief per Outcome Specs and fan out only within the declared budgets.
|
|
142
|
-
5. **Validate** - collect terminal worker reports and decrement budgets before any next dispatch.
|
|
143
|
-
6. **Review and triage** - dispatch blind review and triage findings (see Review Dispatch and Triage).
|
|
144
|
-
7. **Commit, push, PR gates** - only after the required review and authorization (see Commit Protocol).
|
|
145
|
-
8. **Hand off** - report the final result and preserved ledger (see Result Reporting).
|
|
146
|
-
|
|
147
|
-
At each material checkpoint, record child status, remaining budgets, structured delta, and circuit-breaker state. A changed outcome starts a new work unit; do not continue the old route by default.
|
|
148
|
-
|
|
149
|
-
`sonar` stops after research with no implementation; checkpoint commits stop after the preservation commit (see Mode Precedence and Checkpoint Commits).
|
|
150
|
-
|
|
151
|
-
## Checkpoints
|
|
152
|
-
|
|
153
|
-
During multi-step routed work, update progress only at: route selected; delegation completed, blocked, or failed; verification result; review verdict; commit, push, or PR result. Routine reads and searches do not require a user-facing update. At each checkpoint update task state and propose the next step when work remains.
|
|
95
|
+
| `fein` | `full` | Full pipeline with required review and dynamic sequencing |
|
|
96
|
+
| `sonar` | research only | Read-only `/adventurer` or `/planner`, then stop without implementation |
|
|
97
|
+
| `blitz` | direct or builder | Skip optional ceremony for familiar, low-risk work; never waive safety or required review |
|
|
154
98
|
|
|
155
|
-
|
|
99
|
+
Modes are case-insensitive and per-turn unless the platform documents another lifetime. Platform capabilities determine what is guaranteed versus advisory.
|
|
156
100
|
|
|
157
|
-
|
|
101
|
+
## Commit and Session Flow
|
|
158
102
|
|
|
159
|
-
|
|
160
|
-
2. Check scope: is the current work still inside the acceptance criteria?
|
|
161
|
-
3. Classify findings: in-scope fix, out-of-scope follow-up, platform limitation, or design-level blocker.
|
|
162
|
-
4. Security stop: security, auth, or permission findings and other mandatory safety findings are mandatory stops. Require the applicable authorization and route to `/architect` only when design-level; never dispatch builder work. This stop terminates the sequence: do not proceed to `Propose the next owner`, builder dispatch, or follow-up ownership.
|
|
163
|
-
5. Only when no security, auth, or permission finding remains, propose the next owner: `/builder` for in-scope fixes, a follow-up for out-of-scope or platform findings, `/architect` for design-level blockers.
|
|
164
|
-
6. Stop when the outcome is met; do not expand the current unit to absorb adjacent findings.
|
|
103
|
+
After implementation and required review, the authorized executor may commit validated work on a recognized feature branch. Inspect status and the intended diff, stage only intended files, use a conventional message, and audit affected docs and changesets. Push, PR, merge, and release are separate gates. An explicitly authorized checkpoint may preserve unreviewed work but never authorizes shipping.
|
|
165
104
|
|
|
166
|
-
|
|
105
|
+
1. Select the route and load relevant project rules.
|
|
106
|
+
2. Complete the work directly or delegate with a concise outcome brief.
|
|
107
|
+
3. Validate the artifact and run the required independent review.
|
|
108
|
+
4. Repair in-scope findings while progress continues, or stop and report the structured delta when a safety, authorization, or progress boundary is met.
|
|
109
|
+
5. Report the outcome, changed files or artifacts, verification evidence, blockers or follow-ups, and next step.
|
|
167
110
|
|
|
168
|
-
|
|
111
|
+
During multi-step work, update the user at meaningful transitions: route, delegation, verification, review, and lifecycle results. Routine reads do not need narration. Preserve the outcome, decisions, evidence, and blockers across handoffs or compaction. `sonar` stops after research.
|