Skip to content
This repository was archived by the owner on Jun 9, 2026. It is now read-only.
Merged
135 changes: 135 additions & 0 deletions .opencode/PAI/CONTEXT_ROUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Context Routing System

> Lazy-loading context routing for PAI-OpenCode v3.0+
> **CRITICAL:** The bootstrap contains a "Skill Discovery Index" - without it the system doesn't know which skills exist!

## Architecture Overview

**Before (WP1):** 233KB static context loaded at session start
**After (WP2):** ~7KB bootstrap-only + on-demand skill loading (or ~12-17KB with User Identity + System Rules)

Comment on lines +9 to +10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Größenangaben sind inkonsistent zur PR-Zielsetzung.

In Line 9 und Line 53-56 wird mit ~7KB bzw. ~12-17KB als Bootstrap-Kontext argumentiert, obwohl in den PR-Zielen explizit ein ~2KB MINIMAL_BOOTSTRAP beschrieben ist. Bitte die Zahlen konsistent auf den aktuellen Stand bringen, damit Migration und Verifikation eindeutig bleiben.

Also applies to: 53-56

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.opencode/PAI/CONTEXT_ROUTING.md around lines 9 - 10, The documented
bootstrap size figures are inconsistent with the PR goal MINIMAL_BOOTSTRAP
(~2KB); update the size annotations where they appear (e.g., the "After (WP2):
~7KB bootstrap-only ..." line and the corresponding descriptions in the section
currently at lines 53-56) to reflect the current ~2KB MINIMAL_BOOTSTRAP (and
adjust any related "~12-17KB" mentions to the correct values or remove if no
longer applicable) so the migration and verification guidance is consistent with
the PR targets.

```text
┌─────────────────────────────────────────────────────────────┐
│ SESSION START │
│ (~7KB load) │
├─────────────────────────────────────────────────────────────┤
│ MINIMAL_BOOTSTRAP.md │
│ ├── Algorithm Core (OBSERVE→LEARN) │
│ ├── System Steering Rules │
│ ├── User Identity (if exists) │
│ └── SKILL DISCOVERY INDEX ⭐️ IMPORTANT! │
│ (List of all skills with triggers) │
└─────────────────────────────────────────────────────────────┘
▼ (on-demand via trigger detection)
┌─────────────────────┼─────────────────────┐
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Skill │ │ Skill │ │ Skill │
│Research │ │ Agents │ │ Council │
└─────────┘ └─────────┘ └─────────┘
│ │ │
▼ ▼ ▼
SKILL.md SKILL.md SKILL.md
```

## Why Skill Discovery Index in Bootstrap?

**Problem:** If the system doesn't know that e.g. "Research" or "Agents" exist, it cannot load these skills!

**Solution:** The bootstrap contains a compact registry of all available skills with:
- Skill name
- Trigger words (when to load)
- Path to SKILL.md

## Loading Strategies

### 1. Bootstrap Loading (Immediate)

Loaded at every session start:

| File | Size | Purpose |
|------|------|---------|
| `MINIMAL_BOOTSTRAP.md` | ~7KB | Algorithm **Essence** + Steering Rules + Skill Discovery |
| System AISTEERINGRULES.md | ~2KB | Behavior rules (if exists) |
| User Identity | ~3-8KB | ABOUTME, TELOS, DAIDENTITY (if exists) |
| **Total** | **~12-17KB** | Minimal Useful |

**Note:** The Algorithm essence covers 95% of use cases. For Extended/Advanced/Deep effort requiring detailed ISC decomposition, the full Algorithm v3.7.0 (383 lines) loads on-demand via `skill_find("Algorithm")` or by reading `.opencode/PAI/Algorithm/v3.7.0.md`.

### 2. Skill Discovery & Loading (On-Demand)

**Step 1: Trigger Detection**
```typescript
// User Input: "Research this topic for me"
// ↓
// Pattern-match against Skill Discovery Index
// ↓
// Trigger "Research" found → Load Research Skill
```

**Step 2: Load skill**
```typescript
// Find a skill by name (known from Discovery Index)
const skill = await skill_find("Research");

// Use the skill (loads its full SKILL.md)
// Note: skill_use expects the skill name, not ID
await skill_use(skill.name);
```

### 3. Lazy Loading Trigger Examples

| User says | Skill loaded | Trigger word |
|-----------|--------------|--------------|
| "Research this topic" | Research | "Research" |
| "Agents discuss this" | Agents | "Agents" |
| "Use Council" | Council | "Council" |
| "Build CLI tool" | CreateCLI | "CLI" |
| "Security scan" | WebAssessment | "Security" |

## Skill Discovery Index in Bootstrap

The bootstrap contains a compact table:

```markdown
| Skill | Trigger | Path |
|-------|---------|------|
| Research | "Research", "investigate" | skills/Research/SKILL.md |
| Agents | "Agents", "spawn agent" | skills/Agents/SKILL.md |
| Council | "Council", "debate" | skills/Council/SKILL.md |
| ... | ... | ... |
```

**Benefits:**
- ✅ System knows which skills exist
- ✅ Pattern-matching against user input possible
- ✅ Lazy loading works
- ✅ No 233KB static loading needed

## Migration from WP1

### What changed

| Before | After |
|--------|-------|
| 233KB everything loaded | ~7KB bootstrap + Lazy Loading |
| No discovery mechanism | Skill Discovery Index in bootstrap |
| Skills always there | Skills only loaded when needed |

### What stays in bootstrap (Minimal Useful)

1. **Algorithm Core** - How PAI works
2. **System Steering Rules** - Behavior rules
3. **User Identity** - Who the user is (if exists)
4. **Skill Discovery Index** - Which skills exist

### What gets Lazy-Loaded

- Individual skills (only when trigger recognized)
- System docs (MemorySystem, HookSystem, etc.)
- Project-specific contexts

---

*Part of PAI-OpenCode v3.0 Context Modernization (WP2)*
165 changes: 165 additions & 0 deletions .opencode/PAI/MINIMAL_BOOTSTRAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# PAI Bootstrap — Minimal Useful

> **Core context loaded at session start.** ~7KB: Algorithm essence + Steering Rules + User Identity (if exists) + Skill Discovery Index. Skills load on-demand.

---

## The Algorithm (v3.7.0 Essence)

**Goal:** Euphoric Surprise — 9-10 ratings.

**Method:** CURRENT STATE → IDEAL STATE via verifiable criteria (ISC).

**7 Phases:** OBSERVE → THINK → PLAN → BUILD → EXECUTE → VERIFY → LEARN

**Key Rules:**
- ISC before work (8-12 words, binary testable)
- Phases are discrete (never merge)
- All capabilities are skills (actually invoke them)
- Voice curls at every phase (main agent only)
- Direct tools before agents (Grep/Glob/Read <2s)

**Full Algorithm:** This bootstrap contains the Algorithm essence. For complex tasks requiring detailed decomposition, PRD formatting, or extended effort levels:

```typescript
// Load full Algorithm details when needed:
const algorithmSkill = await skill_find("Algorithm");
if (algorithmSkill) {
await skill_use(algorithmSkill.name);
// Full 383-line Algorithm v3.7.0 now available
}
```

Or directly read: `PAI/Algorithm/v3.7.0.md`

---

## AI Steering Rules — System

**Surgical fixes only.** Make precise, targeted corrections. Never delete/gut/rearchitect components as a "fix".

**Never assert without verification.** Don't say "it is X" without checking with tools. Evidence required.

**First principles over bolt-ons.** Understand → Simplify → Reduce → Add (last resort).

**Build ISC from every request.** Decompose into verifiable criteria before executing.

**Ask before destructive actions.** Deletes, force pushes, production deploys — always ask first.

**Read before modifying.** Understand existing code, imports, and patterns first.

**One change when debugging.** Isolate, verify, proceed.

**Minimal scope.** Only change what was asked. No bonus refactoring.

**Plan means stop.** "Create a plan" = present and STOP. No execution without approval.

**Identity.** First person ("I"), user by name (never "the user").

See full rules: `PAI/AISTEERINGRULES.md`

---

## User Identity

Your personal context (loaded when files exist in `.opencode/PAI/USER/`):

| File | Purpose | Loaded |
|------|---------|--------|
| `.opencode/PAI/USER/ABOUTME.md` | Your background, expertise, goals | ✅ If exists |
| `.opencode/PAI/USER/TELOS/TELOS.md` | Life goals, mission, values | ✅ If exists |
| `.opencode/PAI/USER/DAIDENTITY.md` | AI assistant name, personality | ✅ If exists |
| `.opencode/PAI/USER/AISTEERINGRULES.md` | Personal behavior rules | ✅ If exists |

---

## Lazy Loading — On-Demand Skills

**CRITICAL: Skill Discovery Registry**

The system must know which skills exist to load them:

### Available Skills (Discovery Index)

| Skill | Trigger (when to load) | Path |
|-------|------------------------|------|
| **Research** | "Research", "investigate", "find information" | `skills/Research/SKILL.md` |
| **Agents** | "Agents", "spawn agent", "subagent" | `skills/Agents/SKILL.md` |
| **Council** | "Council", "debate", "discuss", "perspectives" | `skills/Council/SKILL.md` |
| **CreateSkill** | "Create skill", "new skill", "build skill" | `skills/CreateSkill/SKILL.md` |
| **CreateCLI** | "Build CLI", "create CLI", "command line tool" | `skills/CreateCLI/SKILL.md` |
| **Documents** | "Process document", "PDF", "Word", "Excel" | `skills/Documents/SKILL.md` |
| **KnowledgeExtraction** | "Extract course", "transcribe", "wisdom" | `skills/KnowledgeExtraction/SKILL.md` |
| **FirstPrinciples** | "First principles", "decompose", "root cause" | `skills/FirstPrinciples/SKILL.md` |
| **BeCreative** | "Be creative", "deep thinking", "extended reasoning" | `skills/BeCreative/SKILL.md` |
| **RedTeam** | "Red team", "attack", "critique", "stress test" | `skills/RedTeam/SKILL.md` |
| **WebAssessment** | "Security scan", "pentest", "vulnerability" | `skills/WebAssessment/SKILL.md` |
| **Fabric** | "Fabric pattern", "extract wisdom", "summarize" | `skills/Fabric/SKILL.md` |
| **Blog** | "Blog post", "article", "write content" | `skills/Blog/SKILL.md` |
| **ContactEnrichment** | "Enrich contact", "verify email", "OSINT" | `skills/ContactEnrichment/SKILL.md` |
| **OSINT** | "OSINT", "due diligence", "investigate person" | `skills/OSINT/SKILL.md` |
| **Recon** | "Recon", "reconnaissance", "bug bounty" | `skills/Recon/SKILL.md` |
| **Apify** | "Scrape Twitter", "Instagram", "LinkedIn", "Google Maps" | `skills/Apify/SKILL.md` |
| **BrightData** | "Bright Data", "scrape URL", "web scraping" | `skills/BrightData/SKILL.md` |
| **AnnualReports** | "Annual report", "security report", "threat report" | `skills/AnnualReports/SKILL.md` |
| **SECUpdates** | "Security news", "breaches", "security updates" | `skills/SECUpdates/SKILL.md` |
| **PrivateInvestigator** | "Find person", "locate", "skip trace" | `skills/PrivateInvestigator/SKILL.md` |
| **WarriorPatterns** | "Warrior patterns", "business analysis", "positioning" | `skills/WarriorPatterns/SKILL.md` |
| **WarriorsWay** | "Warriors Way", "Core 4", "4Ps", "breakthrough" | `skills/WarriorsWay/SKILL.md` |
| **Telos** | "TELOS", "life goals", "projects", "books" | `skills/Telos/SKILL.md` |
| **Aphorisms** | "Aphorism", "quote", "saying" | `skills/Aphorisms/SKILL.md` |
| **Algorithm** | "Algorithm details", "full algorithm", "PRD format", "ISC decomposition", "Extended effort", "Advanced effort" | `PAI/Algorithm/v3.7.0.md` |

### Agent Types (via Task Tool)

| Agent | Usage | Invocation |
|-------|-------|------------|
| **Algorithm** | ISC-specialized work | `Task: subagent_type=Algorithm` |
| **Engineer** | Build, implement, code | `Task: subagent_type=Engineer` |
| **Architect** | Design, structure, system thinking | `Task: subagent_type=Architect` |
| **Pentester** | Security testing, vuln scan | `Task: subagent_type=Pentester` |
| **Designer** | UI/UX design, Figma | `Task: subagent_type=Designer` |
| **QATester** | Testing, verification | `Task: subagent_type=QATester` |
| **BrowserAgent** | Browser automation, screenshots | `Task: subagent_type=BrowserAgent` |
| **UIReviewer** | UI review, accessibility | `Task: subagent_type=UIReviewer` |
| **Artist** | Visual content, images | `Task: subagent_type=Artist` |
| **Writer** | Technical writing, content | `Task: subagent_type=Writer` |
| **DeepResearcher** | Multi-model research | `Task: subagent_type=DeepResearcher` |
| **CodexResearcher** | Code archaeology, technical research | `Task: subagent_type=CodexResearcher` |
| **ClaudeResearcher** | Anthropic ecosystem research | `Task: subagent_type=ClaudeResearcher` |
| **GeminiResearcher** | Multi-perspective research | `Task: subagent_type=GeminiResearcher` |
| **PerplexityResearcher** | Real-time web research | `Task: subagent_type=PerplexityResearcher` |
| **GrokResearcher** | Contrarian, fact-based research | `Task: subagent_type=GrokResearcher` |

### Skill Discovery Pattern

```typescript
// 1. Analyze user input for skill triggers
const userInput = "Research this topic for me";

// 2. Identify matching skill from Discovery Index
// Trigger "Research" → Skill: Research

// 3. Load the skill
const skill = await skill_find("Research");
if (skill) {
// Note: skill_use expects the skill name
await skill_use(skill.name);
// Skill is now available
}
```

**Important:** Without this registry, the system doesn't know that "Research" or "Agents" exist!

---

## Context Routing

- **Immediate:** This bootstrap (~7KB)
- **On-demand:** Skills via `skill_find`/`skill_use`
- **User context:** Auto-loaded if files exist in `.opencode/PAI/USER/`
- **System docs:** Lazy load from `PAI/` when referenced

---

*This is the minimal useful bootstrap. Everything else loads when needed.*
Loading
Loading