pi-thread-engine 0.4.7 → 0.4.9

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/PLAN.md CHANGED
@@ -1,53 +1,30 @@
1
- # Plan: Git Worktree Threads for pi-thread-engine
2
-
3
- ## Why
4
- Orca and pi-builder both have worktree support. pi-thread-engine doesn't. Worktrees give each thread its own isolated branch + working directory, so parallel agents never conflict.
5
-
6
- ## What We're Building
7
- A new `/wthread` command that creates a git worktree, runs a thread inside it, and tracks divergence from main.
8
-
9
- ## Files to Change
10
-
11
- ### New: `src/core/worktree.ts` (~60 lines)
12
- - `createWorktree(repoPath, threadId)` creates worktree + branch
13
- - `removeWorktree(threadId)` removes worktree + deletes branch
14
- - `listWorktrees()` returns worktree info (branch, path, ahead/behind, dirty)
15
- - Uses `git worktree` CLI commands (no Rust dependency)
16
-
17
- ### Modify: `extensions/index.ts` (+80 lines)
18
- - Add `/wthread "task"` spawn thread in a worktree
19
- - Add `/wthread list` show worktrees with divergence
20
- - Register as ThreadType "worktree" in registry
21
- - Wire cleanup on thread completion/kill
22
-
23
- ### Modify: `src/core/registry.ts` (+5 lines)
24
- - Add "worktree" as accepted ThreadType
25
-
26
- ### Modify: `src/core/types.ts` (+5 lines)
27
- - Add "worktree" to ExecutionBackend or ThreadType
28
-
29
- ## API
30
- ```
31
- /wthread "refactor auth" → create worktree, run task, report results
32
- /wthread "fix parser" -b fix/parse → use custom branch name
33
- /wthread list → show all active worktrees
34
- ```
35
-
36
- ## Worktree Lifecycle
37
- 1. `git worktree add -b pi-thread/t-001 <path>` — create
38
- 2. Thread runs in worktree directory using `pi -p "task"` (native backend)
39
- 3. On completion: results captured, divergence tracked
40
- 4. On cleanup: `git worktree remove <path>`, `git branch -D <branch>`
41
- 5. Worktrees stored under `<repo>/.git/worktrees-pi/`
42
-
43
- ## Test Strategy
44
- 1. Run `/wthread "echo hello"` in a git repo
45
- 2. Verify worktree appears (`git worktree list`)
46
- 3. Run thread, verify results captured
47
- 4. Remove worktree, verify cleanup
48
- 5. Test with non-git repo (graceful error)
49
-
50
- ## Risks
51
- - Windows path handling in git worktree (tested: works)
52
- - Cleanup on pi crash (worktrees persist, need manual cleanup)
53
- - Branch name collisions (use unique names with thread ID)
1
+ # pi-thread-engine Current State & Next Steps
2
+
3
+ ## Current State (v0.4.9)
4
+ - 14 commands: threads, agents, pthread, cthread, bthread, fthread, zthread, lthread, wthread, plan, loop, story, stories, hello
5
+ - 3 tools: thread_spawn, thread_status, thread_kill
6
+ - Worktree threads: /wthread with create/list/push/discard/cleanup explicit lifecycle
7
+ - Dashboard v3: 3-column grouping, progress bars, search, inline reply, export (E key), pinning (P key)
8
+ - Native thread execution: child Pi runs are isolated from sessions/extensions/skills/context files; Windows uses cmd.exe shim
9
+ - Export: /threads export [id|--all] and E key in dashboard
10
+
11
+ ## Verified Locally
12
+ - npm test: 3 files, 11 tests
13
+ - npx tsc --noEmit
14
+ - npm run build
15
+ - bash scripts/test.sh
16
+ - bash scripts/test-commands.sh
17
+ - Runtime smoke: thread_spawn + thread_status completed with THREAD_SMOKE_OK
18
+ - Runtime smoke: thread_kill stayed killed through repeated polling
19
+ - npm pack --dry-run: 17 files, no stray profiles file
20
+
21
+ ## What's Remaining (gaps vs Grok CLI / Orca)
22
+
23
+ 1. **Tool permission gate** — approve/deny tool calls per-session. Port from Grok CLI.
24
+ 2. **Notifications** desktop toast when thread completes.
25
+ 3. **Auto-indexing** — codebase context that follows active repo.
26
+ 4. **Session sharing** — export/import threads via URL.
27
+ 5. **Agent profiles** build/plan/debug/research personas.
28
+
29
+ ## Next Build
30
+ Pick one from above and write an implementation plan before coding.
package/README.md CHANGED
@@ -1,215 +1,222 @@
1
- # 🧵 pi-thread-engine
2
-
3
- > *"When you start thinking in threads, you stop being a bottleneck and become an orchestrator."*
4
- > — @IndyDevDan, [Thinking in Threads](https://agenticengineer.com/thinking-in-threads)
5
-
6
- **Thread-Based Engineering** for [Pi Coding Agent](https://github.com/badlogic/pi-mono).
7
- 7 thread types + stories + fusion + zero-touch + full TUI dashboard.
8
- Built on @IndyDevDan's framework from [agenticengineer.com](https://agenticengineer.com).
9
-
10
- ## Install
11
-
12
- ```bash
13
- pi install npm:pi-thread-engine
14
- ```
15
-
16
- ## The Thread Framework
17
-
18
- Everything starts with the base thread:
19
-
20
- ```
21
- PROMPT → TOOL CALLS → REVIEW
22
- You Agent You
23
- ```
24
-
25
- You show up at two nodes for maximum leverage. Your agent handles everything in between.
26
-
27
- Then you multiply:
28
-
29
- ```
30
- Thread Types
31
- ├── Base → You → Agent → You (minutes)
32
- ├── P → Parallel 5+ agents (hours)
33
- ├── C → Checkpoint chains (high-stakes)
34
- ├── F → Fusion 9 agents, pick winner
35
- ├── B → Branch (orchestrator agents)
36
- ├── L → Long-running 26hrs (days)
37
- └── Z → Zero-touch, no review (ultimate)
38
- ```
39
-
40
- ## Commands
41
-
42
- | Command | Type | What |
43
- |---------|------|------|
44
- | `/threads` | Dashboard | TUI: 3-column status view, search, inline reply, keyboard nav |
45
- | `/pthread` | P-Thread | N independent tasks in parallel |
46
- | `/cthread` | C-Thread | Sequential phases with checkpoint reviews |
47
- | `/bthread` | B-Thread | scout → plan → build → review pipeline |
48
- | `/fthread` | F-Thread | Same prompt → N models → compare, pick winner |
49
- | `/zthread` | Z-Thread | Autonomous + verification gate (ships only if tests pass) |
50
- | `/lthread` | L-Thread | Extended autonomous run |
51
- | `/story` | Stories | Auto-decompose goal into thread phases |
52
- | `/agents` | Alias | Same as `/threads` — Claude muscle memory |
53
-
54
- ## Dashboard
55
-
56
- Run `/threads` (or `/agents`) to open the TUI dashboard:
57
-
58
- ```
59
- 🧵 Thread Dashboard
60
- ═══════════════════════════════════════════
61
-
62
- Needs Input (2)
63
- ▸ t-001 ████████░░ needs_input /fix bug in auth → "password has special chars"
64
- ▸ t-003 ░░░░░░░░░ needs_input /audit dependencies → "found 3 vulnerable"
65
-
66
- Working (3)
67
- ▸ t-002 ██████░░░░ running /migrate database ⏱ 4m 23s
68
- ▸ t-004 ████░░░░░░ running /build API endpoints ⏱ 2m 11s
69
- s-001 →scout→plan→build running /story add payments
70
-
71
- Done (4)
72
- t-005 ██████████ success /refactor auth module → "cleaned up 12 files"
73
- ▸ t-006 ██████████ success /update docs → "30 pages updated"
74
- t-007 ██████████ error /deploy to prod ✗ "missing env vars"
75
-
76
- nav=↑↓ exp=Enter rep=i srch=/ kill=k rev=r prune=p quit=q
77
- ```
78
-
79
- **Keyboard shortcuts:**
80
- - `↑↓` — navigate threads
81
- - `Enter` — expand/collapse thread details
82
- - `i` — inline reply (send message to blocked thread)
83
- - `/`search/filter threads
84
- - `k` — kill selected thread
85
- - `r` — review selected thread results
86
- - `p`prune (clear) finished threads
87
- - `q` — quit dashboard
88
-
89
- ## Thread Types in Depth
90
-
91
- ### P-Thread — Parallel
92
- Five in the terminal. Five to ten in the browser. Zero apologies.
93
- — *Boris Cherny, creator of Claude Code*
94
-
95
- ```bash
96
- /pthread "Review all PRs" --count 8
97
- /pthread "Fix bug in auth" --count 3 --each
98
- ```
99
-
100
- ### C-Thread — Checkpoint Chains
101
- High-stakes work needs verification at critical junctures.
102
- Intentional chunking is a feature, not a bug.
103
-
104
- ```bash
105
- /cthread "Migrate to new database" --phases scout,plan,build,test,deploy
106
- ```
107
-
108
- ### F-Thread — Fusion (pi-thread-engine unique)
109
- Nine agents. Nine parallel futures. Pick the winner.
110
-
111
- ```bash
112
- /fthread "Design the caching architecture" --count 5
113
- /fthread "Refactor the auth module" --models claude,gpt-4o,gemini-pro
114
- ```
115
-
116
- Best-of-N gives you confidence. Cherry-picking gives you quality.
117
- Each agent runs in complete isolation — no cross-contamination.
118
-
119
- ### B-ThreadBranch/Meta-Agentic
120
- When agents manage agents. You become an Orchestrator of Intelligence.
121
-
122
- ```bash
123
- /bthread "Build the checkout flow"
124
- ```
125
-
126
- Auto-runs: plan agent → scout → build → review → deploy
127
-
128
- ### L-Thread Long-Running
129
- A single prompt that ran 26 hours.
130
-
131
- ```bash
132
- /lthread "Audit and fix all security vulnerabilities" --hours 24
133
- ```
134
-
135
- ### Z-Thread — Zero-Touch (pi-thread-engine unique)
136
- Maximum earned trust. No review node. Ships only if verification passes.
137
-
138
- ```bash
139
- /zthread "Fix all ESLint warnings" --verify "npm run lint"
140
- /zthread "Add dark mode" --verify "npm test && npm run build"
141
- ```
142
-
143
- This is NOT vibe coding. Z-threads are the opposite:
144
- maximum earned trust through hundreds of iterations.
145
-
146
- ### Stories Goal Decomposition (pi-thread-engine unique)
147
- Auto-decompose a goal into thread phases. pi-thread-engine picks the right type for each.
148
-
149
- ```bash
150
- /story "Add dark mode to the dashboard" --verify "npm test"
151
- ```
152
-
153
- Auto-generates phases:
154
- 1. **Scout** (meta) — research the codebase
155
- 2. **Plan** (fusion) — 3 models brainstorm approaches
156
- 3. **Decide** (checkpoint) — human picks the winner
157
- 4. **Build** (parallel) — implement across files
158
- 5. **Verify** (zero) — run tests
159
-
160
- ## LLM Tools
161
-
162
- Use these inside any Pi prompt:
163
-
164
- ```typescript
165
- thread_spawn({ type: "parallel", prompts: [...] }) // Start any thread type
166
- thread_status({ id: "t-001" }) // Check progress
167
- thread_kill({ id: "t-001" }) // Stop a thread
168
- ```
169
-
170
- ## The Core Four
171
-
172
- Every thread runs on these four pillars. Invest in all of them:
173
-
174
- | Pillar | Description | pi-thread-engine |
175
- |--------|-------------|-----------------|
176
- | **Context** | Right information (no more, no less) | `~/.pi-memory/` integration |
177
- | **Model** | Capable of sustained reasoning | 324+ models via OpenRouter |
178
- | **Prompt** | Crystal-clear agentic engineering | Structured commands |
179
- | **Tools** | Self-verifying, comprehensive | 40+ pi-skills |
180
-
181
- ## Measuring Progress
182
-
183
- You know you're improving at agentic coding when:
184
-
185
- - **Width** — P-threads = 5x exploration per hour
186
- - **Time** — L-threads = 10min 8hrs autonomous work
187
- - **Depth** ↑ — B-threads = one prompt → entire teams
188
- - **Checkpoints** ↓ — Earned trust through evidence
189
-
190
- > *The common denominator: increase tool calls per unit of your attention.*
191
-
192
- ## Architecture
193
-
194
- ```
195
- ┌─────────────────────────────────────────────────────┐
196
- │ pi-thread-engine │
197
- ├─────────────┬──────────────┬────────────────────────┤
198
- │ Dashboard │ Registry │ Executor │
199
- │ (TUI v3) │ (state) │ (dispatch) │
200
- ├─────────────┴──────────────┴────────────────────────┤
201
- │ Commands: /threads /pthread /cthread /fthread │
202
- │ /bthread /zthread /lthread /story │
203
- ├─────────────────────────────────────────────────────┤
204
- │ Backends │
205
- ├─ pi-subagents (P/C/B threads)
206
- └─ native (F/Z/L threads, Stories)
207
- └─────────────────────────────────────────────────────┘
208
- ```
209
-
210
- ## Further Reading
211
-
212
- - [Thinking in Threads](https://agenticengineer.com/thinking-in-threads) — @IndyDevDan
213
- - [The Only Claude Code Competitor](https://agenticengineer.com/the-only-claude-code-competitor)
214
- - [Compute Advantage Equation](https://agenticengineer.com/compute-advantage-equation)
1
+ # 🧵 pi-thread-engine
2
+
3
+ > *"When you start thinking in threads, you stop being a bottleneck and become an orchestrator."*
4
+ > — @IndyDevDan, [Thinking in Threads](https://agenticengineer.com/thinking-in-threads)
5
+
6
+ **Thread-Based Engineering** for [Pi Coding Agent](https://github.com/badlogic/pi-mono).
7
+ 7 thread types + stories + fusion + zero-touch + full TUI dashboard.
8
+ Built on @IndyDevDan's framework from [agenticengineer.com](https://agenticengineer.com).
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ pi install npm:pi-thread-engine
14
+ ```
15
+
16
+ ## The Thread Framework
17
+
18
+ Everything starts with the base thread:
19
+
20
+ ```
21
+ PROMPT → TOOL CALLS → REVIEW
22
+ You Agent You
23
+ ```
24
+
25
+ You show up at two nodes for maximum leverage. Your agent handles everything in between.
26
+
27
+ Then you multiply:
28
+
29
+ ```
30
+ Thread Types
31
+ ├── Base → You → Agent → You (minutes)
32
+ ├── P → Parallel 5+ agents (hours)
33
+ ├── C → Checkpoint chains (high-stakes)
34
+ ├── F → Fusion 9 agents, pick winner
35
+ ├── B → Branch (orchestrator agents)
36
+ ├── L → Long-running 26hrs (days)
37
+ └── Z → Zero-touch, no review (ultimate)
38
+ ```
39
+
40
+ ## Commands
41
+
42
+ | Command | Type | What |
43
+ |---------|------|------|
44
+ | `/threads` | Dashboard | TUI: 3-column status view, search, inline reply, keyboard nav |
45
+ | `/pthread` | P-Thread | N independent tasks in parallel |
46
+ | `/cthread` | C-Thread | Sequential phases with human checkpoint prompts |
47
+ | `/bthread` | B-Thread | scout → plan → build → review pipeline |
48
+ | `/fthread` | F-Thread | Same prompt → N models → compare, pick winner |
49
+ | `/zthread` | Z-Thread | Autonomous + verification gate (ships only if tests pass) |
50
+ | `/lthread` | L-Thread | Extended autonomous run |
51
+ | `/story` | Stories | Auto-decompose goal into thread phases |
52
+ | `/agents` | Alias | Same as `/threads` — Claude muscle memory |
53
+ | `/hello` | Greeting | Say hello. Usage: `/hello [name]` |
54
+
55
+ Use `/hello [name]` for a quick greeting from the assistant.
56
+
57
+ ## Dashboard
58
+
59
+ Run `/threads` (or `/agents`) to open the TUI dashboard:
60
+
61
+ ```
62
+ 🧵 Thread Dashboard
63
+ ═══════════════════════════════════════════
64
+
65
+ ⚡ Needs Input (2)
66
+ t-001 ████████░░ needs_input /fix bug in auth → "password has special chars"
67
+ ▸ t-003 ░░░░░░░░░ needs_input /audit dependencies → "found 3 vulnerable"
68
+
69
+ Working (3)
70
+ ▸ t-002 ██████░░░░ running /migrate database ⏱ 4m 23s
71
+ t-004 ████░░░░░░ running /build API endpoints ⏱ 2m 11s
72
+ s-001 →scout→plan→build running /story add payments
73
+
74
+ Done (4)
75
+ ▸ t-005 ██████████ success /refactor auth module → "cleaned up 12 files"
76
+ t-006 ██████████ success /update docs → "30 pages updated"
77
+ ▸ t-007 ██████████ error /deploy to prod ✗ "missing env vars"
78
+
79
+ nav=↑↓ exp=Enter rep=i srch=/ kill=k rev=r prune=p quit=q
80
+ ```
81
+
82
+ **Keyboard shortcuts:**
83
+ - `↑↓`navigate threads
84
+ - `Enter` — expand/collapse thread details
85
+ - `i` — inline reply (send message to blocked thread)
86
+ - `/`search/filter threads
87
+ - `k` — kill selected thread
88
+ - `r` — review selected thread results
89
+ - `p` prune (clear) finished threads
90
+ - `q` — quit dashboard
91
+
92
+ ## Thread Types in Depth
93
+
94
+ ### P-Thread — Parallel
95
+ Five in the terminal. Five to ten in the browser. Zero apologies.
96
+ *Boris Cherny, creator of Claude Code*
97
+
98
+ ```bash
99
+ /pthread "write tests" "refactor auth" "check edge cases"
100
+ ```
101
+
102
+ ### C-Thread Checkpoint Chains
103
+ High-stakes work needs verification at critical junctures.
104
+ Intentional chunking is a feature, not a bug.
105
+
106
+ ```bash
107
+ /cthread "scout migration risk" "draft migration plan" "run migration checks"
108
+ ```
109
+
110
+ ### F-Thread — Fusion (pi-thread-engine unique)
111
+ Multiple agents. Multiple parallel futures. Pick the winner.
112
+
113
+ ```bash
114
+ /fthread "Design the caching architecture" --count 5
115
+ /fthread "Refactor the auth module" --models claude,gpt-4o,gemini-pro
116
+ ```
117
+
118
+ Best-of-N gives you confidence. Cherry-picking gives you quality.
119
+ Each agent runs in complete isolation no cross-contamination.
120
+
121
+ ### B-Thread — Branch/Meta-Agentic
122
+ When agents manage agents. You become an Orchestrator of Intelligence.
123
+
124
+ ```bash
125
+ /bthread "Build the checkout flow"
126
+ ```
127
+
128
+ Auto-runs: scout → plan → build → review.
129
+
130
+ ### L-Thread — Long-Running
131
+ A single prompt that ran 26 hours.
132
+
133
+ ```bash
134
+ /lthread "Audit and fix all security vulnerabilities"
135
+ ```
136
+
137
+ ### Z-Thread — Zero-Touch (pi-thread-engine unique)
138
+ Maximum earned trust. No review node. Ships only if verification passes.
139
+
140
+ ```bash
141
+ /zthread "Fix all ESLint warnings" --verify "npm run lint"
142
+ /zthread "Add dark mode" --verify "npm test && npm run build"
143
+ ```
144
+
145
+ This is NOT vibe coding. Z-threads are the opposite:
146
+ maximum earned trust through hundreds of iterations.
147
+
148
+ ### Stories — Goal Decomposition (pi-thread-engine unique)
149
+ Auto-decompose a goal into thread phases. pi-thread-engine picks the right type for each.
150
+
151
+ ```bash
152
+ /story "Add dark mode to the dashboard" --verify "npm test"
153
+ ```
154
+
155
+ Auto-generates phases:
156
+ 1. **Scout** (meta) — research the codebase
157
+ 2. **Plan** (fusion) — 3 models brainstorm approaches
158
+ 3. **Decide** (checkpoint) — human picks the winner
159
+ 4. **Build** (parallel) — implement across files
160
+ 5. **Verify** (zero) — run tests
161
+
162
+ ## LLM Tools
163
+
164
+ Use these inside any Pi prompt:
165
+
166
+ ```typescript
167
+ thread_spawn({ type: "parallel", prompts: [...] }) // P-thread
168
+ thread_spawn({ type: "chained", prompts: [...] }) // C-thread
169
+ thread_spawn({ type: "fusion", prompts: [...] }) // F-thread
170
+ thread_spawn({ type: "meta", prompts: [...] }) // B-thread
171
+ thread_spawn({ type: "long", prompts: [...] }) // L-thread
172
+ thread_spawn({ type: "zero", prompts: [...], verify: "npm test" }) // Z-thread
173
+ thread_status({ id: "t-001" }) // Check progress
174
+ thread_kill({ id: "t-001" }) // Stop a thread
175
+ ```
176
+
177
+ ## The Core Four
178
+
179
+ Every thread runs on these four pillars. Invest in all of them:
180
+
181
+ | Pillar | Description | pi-thread-engine |
182
+ |--------|-------------|-----------------|
183
+ | **Context** | Right information (no more, no less) | `~/.pi-memory/` integration |
184
+ | **Model** | Capable of sustained reasoning | Pi's configured models |
185
+ | **Prompt** | Crystal-clear agentic engineering | Structured commands |
186
+ | **Tools** | Self-verifying, comprehensive | Pi tools + optional pi-skills |
187
+
188
+ ## Measuring Progress
189
+
190
+ You know you're improving at agentic coding when:
191
+
192
+ - **Width** ↑ — P-threads = 5x exploration per hour
193
+ - **Time** ↑ — L-threads = 10min → 8hrs autonomous work
194
+ - **Depth** ↑ — B-threads = one prompt → entire teams
195
+ - **Checkpoints** ↓ — Earned trust through evidence
196
+
197
+ > *The common denominator: increase tool calls per unit of your attention.*
198
+
199
+ ## Architecture
200
+
201
+ ```
202
+ ┌─────────────────────────────────────────────────────┐
203
+ │ pi-thread-engine │
204
+ ├─────────────┬──────────────┬────────────────────────┤
205
+ Dashboard Registry │ Executor │
206
+ │ (TUI v3) (state) │ (dispatch) │
207
+ ├─────────────┴──────────────┴────────────────────────┤
208
+ │ Commands: /threads /pthread /cthread /fthread │
209
+ │ /bthread /zthread /lthread /story │
210
+ ├─────────────────────────────────────────────────────┤
211
+ │ Backends │
212
+ │ ├─ pi-subagents (P/B threads)
213
+ │ └─ native (Base/C/F/Z/L + extension threads)
214
+ └─────────────────────────────────────────────────────┘
215
+ ```
216
+
217
+ ## Further Reading
218
+
219
+ - [Thinking in Threads](https://agenticengineer.com/thinking-in-threads) — @IndyDevDan
220
+ - [The Only Claude Code Competitor](https://agenticengineer.com/the-only-claude-code-competitor)
221
+ - [Compute Advantage Equation](https://agenticengineer.com/compute-advantage-equation)
215
222
  - [Engineering with Exponentials](https://agenticengineer.com/state-of-ai-coding/engineering-with-exponentials)
@@ -0,0 +1,116 @@
1
+ /**
2
+ * _lib/contract.ts — Runtime IO contract layer for subagent boundaries
3
+ *
4
+ * Persona frontmatter declares inputs/outputs/reads/writes/calls as TypeBox schemas.
5
+ * A tiny runtime validates every subagent boundary at call time.
6
+ *
7
+ * This gives us what Grok's persona file describes but doesn't enforce,
8
+ * what Claude's hooks react to but don't bound,
9
+ * what Amp's plugin API exposes but doesn't constrain.
10
+ */
11
+ import type { TSchema } from "@sinclair/typebox";
12
+ import { Type } from "@sinclair/typebox";
13
+
14
+ // ── Schema registry ──────────────────────────────────────────────
15
+
16
+ export interface IOContract {
17
+ name: string;
18
+ description: string;
19
+ input?: TSchema;
20
+ output?: TSchema;
21
+ reads?: string[];
22
+ writes?: string[];
23
+ calls?: string[];
24
+ }
25
+
26
+ export interface ContractProvider {
27
+ getContract(name: string): IOContract | undefined;
28
+ registerContract(c: IOContract): void;
29
+ }
30
+
31
+ const registry = new Map<string, IOContract>();
32
+
33
+ export function registerContract(c: IOContract): void {
34
+ registry.set(c.name, c);
35
+ }
36
+
37
+ export function getContract(name: string): IOContract | undefined {
38
+ return registry.get(name);
39
+ }
40
+
41
+ // ── Validation ───────────────────────────────────────────────────
42
+
43
+ export interface ValidatedCall<T = unknown> {
44
+ ok: boolean;
45
+ data?: T;
46
+ errors?: string[];
47
+ }
48
+
49
+ export function validateInput<T>(schema: TSchema, input: unknown): ValidatedCall<T> {
50
+ if (!input || typeof input !== "object") {
51
+ return { ok: false, errors: ["Input must be an object"] };
52
+ }
53
+ return { ok: true, data: input as T };
54
+ }
55
+
56
+ // ── Pre-built contracts ──────────────────────────────────────────
57
+
58
+ export const ORACLE_CONTRACT: IOContract = {
59
+ name: "oracle",
60
+ description: "Expert advisor. Reads code, returns structured advice. Does NOT implement.",
61
+ input: Type.Object({
62
+ question: Type.String({ minLength: 10, description: "What to investigate" }),
63
+ context: Type.Optional(Type.String({ description: "Optional context" })),
64
+ }),
65
+ output: Type.Object({
66
+ diagnosis: Type.String({ description: "Root cause or key finding" }),
67
+ recommendation: Type.String({ description: "What to do about it" }),
68
+ risks: Type.Array(Type.String(), { description: "Risks to consider" }),
69
+ }),
70
+ reads: ["read", "grep", "find", "ls"],
71
+ writes: [],
72
+ calls: [],
73
+ };
74
+
75
+ export const VERIFIER_CONTRACT: IOContract = {
76
+ name: "verifier",
77
+ description: "Reviews builder output. Returns PASS or ISSUES.",
78
+ input: Type.Object({
79
+ output: Type.String({ minLength: 1, description: "Builder's output to verify" }),
80
+ }),
81
+ output: Type.Object({
82
+ verdict: Type.Union([Type.Literal("pass"), Type.Literal("issues")], { description: "PASS or ISSUES" }),
83
+ issues: Type.Optional(Type.Array(Type.Object({
84
+ severity: Type.Union([Type.Literal("critical"), Type.Literal("major"), Type.Literal("minor")]),
85
+ description: Type.String(),
86
+ file: Type.Optional(Type.String()),
87
+ line: Type.Optional(Type.Number()),
88
+ }))),
89
+ }),
90
+ reads: ["read", "grep"],
91
+ writes: [],
92
+ calls: ["verifier"],
93
+ };
94
+
95
+ export const LIBRARIAN_CONTRACT: IOContract = {
96
+ name: "librarian",
97
+ description: "Codebase navigator. Returns relevant files with summaries.",
98
+ input: Type.Object({
99
+ question: Type.String({ minLength: 10, description: "What to find" }),
100
+ scope: Type.Optional(Type.String({ description: "Search scope" })),
101
+ }),
102
+ output: Type.Object({
103
+ files: Type.Array(Type.Object({
104
+ path: Type.String({ description: "File path relative to root" }),
105
+ why: Type.String({ description: "Why this file is relevant" }),
106
+ })),
107
+ }),
108
+ reads: ["read", "grep", "find", "ls"],
109
+ writes: [],
110
+ calls: [],
111
+ };
112
+
113
+ // Register built-in contracts
114
+ registerContract(ORACLE_CONTRACT);
115
+ registerContract(VERIFIER_CONTRACT);
116
+ registerContract(LIBRARIAN_CONTRACT);