summaryrefslogtreecommitdiff
path: root/docs/proposals/feature-knowledge-accumulation.md
blob: faef06a9757b0ab76aa4cc997a085421df63ac6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# Feature Proposal: Knowledge Accumulation / Compound Learning System

> **Priority:** High
> **Complexity:** Medium
> **Estimated Effort:** 10-15 days
> **Status:** Proposal
> **Date:** 2026-02-09
> **Dependencies:** Contract Files system (existing)
> **Related:** [Overview Analysis](compound-engineering-analysis.md) · [Plan Deepening](feature-plan-deepening.md) · [Workflow Presets](feature-workflow-presets.md)

---

## Problem Statement

When a makima contract completes, the **knowledge generated during that contract is effectively lost**:

- **Solutions to tricky problems** exist only in task conversation history, which is not searchable or surfaceable
- **Patterns discovered** during one contract cannot inform future contracts
- **Mistakes made** in one contract are likely to be repeated in similar future contracts
- **Best practices** established during execution are not codified anywhere retrievable
- **Contract files** capture deliverables but not the *meta-knowledge* about how those deliverables were produced

This means every new contract starts from zero context, even when the team has solved similar problems before. Engineering effort does not compound.

---

## How Compound Engineering Solves This

The compound engineering plugin implements a `/compound` command that runs **5 parallel sub-agents** immediately after review:

```
┌─────────────────────────────────────────────────────────┐
│                   /compound                              │
│                                                         │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐    │
│  │  Context    │  │  Solution   │  │ Prevention  │    │
│  │  Extractor  │  │ Documenter  │  │ Strategist  │    │
│  └──────┬──────┘  └──────┬──────┘  └──────┬──────┘    │
│         │                │                │            │
│  ┌──────┴──────┐  ┌──────┴──────┐                      │
│  │   Doc       │  │  Category   │                      │
│  │   Linker    │  │  Classifier │                      │
│  └──────┬──────┘  └──────┬──────┘                      │
│         │                │                              │
│         ▼                ▼                              │
│  ┌──────────────────────────────────────┐              │
│  │  docs/solutions/[category]/file.md   │              │
│  │                                      │              │
│  │  ---                                 │              │
│  │  category: build-errors              │              │
│  │  severity: medium                    │              │
│  │  tags: [webpack, esm, cjs]           │              │
│  │  date: 2026-02-09                    │              │
│  │  contract: abc-123                   │              │
│  │  ---                                 │              │
│  │                                      │              │
│  │  # Mixed ESM/CJS Import Resolution  │              │
│  │                                      │              │
│  │  ## Problem                          │              │
│  │  ...                                 │              │
│  │  ## Solution                         │              │
│  │  ...                                 │              │
│  │  ## Prevention                       │              │
│  │  ...                                 │              │
│  └──────────────────────────────────────┘              │
└─────────────────────────────────────────────────────────┘
```

### 9 Auto-Detected Categories

| Category | Description |
|----------|-------------|
| `build-errors` | Compilation, bundling, dependency resolution |
| `test-failures` | Test setup, assertion patterns, mocking |
| `api-patterns` | API design, endpoint structure, versioning |
| `architecture-decisions` | Structural choices, trade-offs, patterns |
| `performance-optimizations` | Speed, memory, caching strategies |
| `security-practices` | Auth, input validation, secrets management |
| `debugging-techniques` | Investigation methods, logging strategies |
| `tooling-configurations` | Tool setup, config patterns, CI/CD |
| `domain-knowledge` | Business logic, domain-specific patterns |

---

## Proposed Makima Implementation

### 1. New "Compound" Phase

Add an optional **compound** phase to the contract lifecycle, positioned after review:

```
Research → Specify → Plan → Execute → Review → Compound
                                                  ▲
                                            (new phase)
```

**Phase behavior:**
- **Auto-triggered** after review phase completes (configurable)
- **Short-lived** — typically completes in 1-3 minutes
- Extracts learnings from the contract's execution and review
- Stores them as searchable, categorized learning documents
- Can be skipped via configuration for trivial contracts

### 2. New Supervisor Command: `makima supervisor compound`

```bash
# Run compound learning for the current contract
makima supervisor compound

# Compound with specific focus areas
makima supervisor compound --focus "security,performance"

# Compound with explicit learnings
makima supervisor compound --learning "The retry logic needed exponential backoff, not fixed delay"
```

**Implementation:**

```bash
# Under the hood, this spawns learning sub-agents
makima supervisor spawn-group "compound" \
  --tasks '[
    {
      "name": "context-extractor",
      "plan": "Extract the problem context, constraints, and environment details from the contract execution history..."
    },
    {
      "name": "solution-documenter",
      "plan": "Document the solutions that were applied, including code patterns and configuration changes..."
    },
    {
      "name": "prevention-strategist",
      "plan": "Identify what could prevent this class of problem in the future..."
    },
    {
      "name": "category-classifier",
      "plan": "Classify these learnings into the appropriate category..."
    },
    {
      "name": "doc-linker",
      "plan": "Link these learnings to existing documentation and related learnings..."
    }
  ]'
```

### 3. Learning Document Schema

Each learning is stored as a **contract file** with structured content and metadata:

```yaml
# Learning document metadata (stored in file description/metadata)
learning:
  category: "build-errors"          # One of 9 categories
  severity: "medium"                # low, medium, high, critical
  tags: ["webpack", "esm", "cjs"]   # Free-form tags
  source_contract_id: "abc-123"     # Contract that produced this learning
  source_contract_name: "Fix webpack bundling"
  repository: "github.com/org/repo"
  date: "2026-02-09"
  quality_score: 0.85               # 0-1, set by quality gate
  access_count: 0                   # Incremented on retrieval
  last_accessed: null
  relevance_decay: 0.95             # Per-month decay factor
```

**Document body structure:**

```markdown
# Mixed ESM/CJS Import Resolution

## Problem
When upgrading to webpack 5, mixed ESM and CommonJS imports caused
"Cannot use import statement outside a module" errors in production
but not development.

## Root Cause
The `type: "module"` field in package.json applied ESM resolution
globally, but several dependencies only provided CJS exports.

## Solution
1. Added `resolve.fullySpecified: false` to webpack config
2. Used `@babel/plugin-transform-modules-commonjs` for CJS deps
3. Created explicit `.cjs` extensions for config files

## Code Pattern
```javascript
// webpack.config.cjs (note: .cjs extension)
module.exports = {
  resolve: {
    fullySpecified: false,
    extensions: ['.js', '.mjs', '.cjs', '.json']
  }
};
```

## Prevention
- Add webpack build check to CI before merging
- Document module system choice in project README
- Use `resolve.fullySpecified: false` by default in webpack 5 projects

## Related
- docs/solutions/tooling-configurations/webpack-5-migration.md
- Contract: "Initial Webpack 5 Migration" (2026-01-15)
```

### 4. Storage Architecture

Learnings are stored in two complementary locations:

#### A. Contract Files (Structured, Persistent)

```rust
// Each learning becomes a contract file
File {
    contract_id: Some(source_contract.id),
    contract_phase: Some("compound"),
    name: "Learning: Mixed ESM/CJS Import Resolution",
    description: Some("category=build-errors; tags=webpack,esm,cjs; severity=medium"),
    body: vec![
        BodyElement::Heading { level: 1, text: "Mixed ESM/CJS Import Resolution" },
        BodyElement::Heading { level: 2, text: "Problem" },
        BodyElement::Paragraph { text: "..." },
        // ... structured content
    ],
    repo_file_path: Some("docs/solutions/build-errors/mixed-esm-cjs-resolution.md"),
    repo_sync_status: Some("synced"),
}
```

#### B. Repository Files (Searchable, Portable)

```
docs/solutions/
├── build-errors/
│   ├── mixed-esm-cjs-resolution.md
│   └── docker-multi-stage-cache.md
├── test-failures/
│   ├── async-test-timeout-patterns.md
│   └── mock-service-worker-setup.md
├── api-patterns/
│   └── pagination-cursor-vs-offset.md
├── architecture-decisions/
│   └── event-sourcing-tradeoffs.md
├── performance-optimizations/
│   └── database-connection-pooling.md
├── security-practices/
│   └── jwt-refresh-token-rotation.md
├── debugging-techniques/
│   └── distributed-tracing-setup.md
├── tooling-configurations/
│   └── github-actions-cache-strategy.md
└── domain-knowledge/
    └── payment-processing-idempotency.md
```

### 5. Auto-Surface Relevant Learnings

When a new contract is created, automatically search for relevant learnings:

```bash
# Supervisor plan template automatically includes:
# "Search existing learnings relevant to this task"

makima supervisor search-learnings --query "webpack bundling errors"
makima supervisor search-learnings --category "build-errors" --tags "webpack"
makima supervisor search-learnings --repository "github.com/org/repo"
```

**Search algorithm:**

```
Relevance Score =
    keyword_match_score * 0.4
  + category_match_score * 0.2
  + tag_overlap_score * 0.2
  + recency_score * 0.1        # Decays over time
  + quality_score * 0.1        # Higher quality = more relevant
```

**Integration with plan phase:**

```
┌──────────────┐       ┌───────────────────┐
│ New Contract │──────▶│ Plan Phase        │
│ Created      │       │                   │
└──────────────┘       │ 1. Create plan    │
                       │ 2. Search for     │◀── Learnings DB
                       │    relevant       │
                       │    learnings      │
                       │ 3. Inject context │
                       │    into plan      │
                       └───────────────────┘
```

### 6. Quality Control

#### Relevance Decay

Learnings lose relevance over time unless accessed:

```
effective_relevance = quality_score * (decay_factor ^ months_since_creation)
                    + access_bonus * recent_access_count
```

- Default decay factor: 0.95/month (learning at 60% relevance after 1 year)
- Access bonus: +0.05 per access (caps at +0.25)
- Learnings below 0.3 effective relevance are archived

#### Deduplication

When a new learning is created, check for existing similar learnings:

```
similarity = cosine_similarity(new_learning_embedding, existing_learning_embedding)
if similarity > 0.85:
    merge_or_update(existing_learning, new_learning)
elif similarity > 0.70:
    link_as_related(new_learning, existing_learning)
```

#### Quality Gate

Before storing a learning, validate:

| Check | Threshold | Action if Failed |
|-------|-----------|------------------|
| Has problem statement | Required | Reject |
| Has solution | Required | Reject |
| Has prevention strategy | Recommended | Warn, store with quality penalty |
| Code examples present | Recommended | Warn, store with quality penalty |
| Category valid | Required | Auto-classify |
| Not duplicate | >0.85 similarity | Merge with existing |
| Minimum length | >200 characters | Reject |

---

## Integration with Existing Makima Features

### Contract Phases

The compound phase integrates into the existing phase system:

```rust
// New phase variant
enum ContractPhase {
    Research,
    Specify,
    Plan,
    Execute,
    Review,
    Compound,  // NEW
}
```

- Contracts with `contract_type: "specification"` get the full 6-phase cycle
- Contracts with `contract_type: "simple"` can opt-in via config
- Phase guard still applies: user must approve transition to compound

### Contract Files

Learnings are first-class contract files, leveraging existing:
- Versioning system
- Structured body format (`BodyElement` types)
- Repository file sync (`repo_file_path`, `repo_sync_status`)
- Phase association (`contract_phase: "compound"`)

### Directive System

For directive-based workflows, learnings can be captured per-step:

```rust
DirectiveStep {
    name: "compound-step-3",
    description: "Capture learnings from database migration step",
    depends_on: [step_3_id, review_step_id],
    task_plan: "Extract and document learnings from the completed migration...",
}
```

### Supervisor CLI

New commands integrate with existing CLI infrastructure:

```bash
# In supervisor context
makima supervisor compound                    # Run compound phase
makima supervisor search-learnings "query"    # Search knowledge base
makima supervisor list-learnings              # List all learnings
makima supervisor learning-stats              # Knowledge base statistics
```

---

## Implementation Plan

### Phase 1: Core Infrastructure (4-5 days)

| Task | Effort | Description |
|------|--------|-------------|
| Add `compound` phase to contract lifecycle | 1 day | New phase enum, transition rules |
| Learning document schema | 1 day | Metadata structure, validation |
| `supervisor compound` command | 1-2 days | Spawn learning sub-agents |
| Repository file sync for learnings | 1 day | Write to `docs/solutions/` |

### Phase 2: Search & Retrieval (3-5 days)

| Task | Effort | Description |
|------|--------|-------------|
| `search-learnings` command | 1-2 days | Keyword + category search |
| Auto-surface in plan phase | 1-2 days | Inject relevant learnings into plans |
| Learning index | 1 day | Category/tag index for fast lookup |

### Phase 3: Quality & Maintenance (3-5 days)

| Task | Effort | Description |
|------|--------|-------------|
| Quality gate validation | 1 day | Pre-storage checks |
| Relevance decay system | 1 day | Scheduled decay + access tracking |
| Deduplication check | 1-2 days | Similarity detection and merging |
| Documentation & defaults | 1 day | User guide, default categories |

---

## Configuration Examples

### Enable Compound Phase (Contract-Level)

```yaml
# Contract configuration
compound:
  enabled: true
  auto_trigger: true        # Auto-run after review completes
  categories:               # Override default categories
    - build-errors
    - test-failures
    - api-patterns
    - architecture-decisions
    - performance-optimizations
    - security-practices
    - debugging-techniques
    - tooling-configurations
    - domain-knowledge
  quality_gate:
    min_length: 200
    require_problem: true
    require_solution: true
    require_prevention: false
  storage:
    contract_files: true     # Store as contract files
    repo_files: true         # Also write to docs/solutions/
    repo_path: "docs/solutions"
```

### Repository-Level Configuration (`.makima/compound.yaml`)

```yaml
# .makima/compound.yaml
version: 1
compound:
  # Default settings for all contracts in this repo
  auto_trigger: true

  # Custom categories for this project
  categories:
    - build-errors
    - test-failures
    - api-patterns
    - payment-processing     # Custom domain category
    - compliance-requirements # Custom domain category

  # Search settings
  search:
    max_results: 10
    min_relevance: 0.3
    include_archived: false

  # Decay settings
  decay:
    factor: 0.95             # Per month
    archive_threshold: 0.3
    access_bonus: 0.05
    max_access_bonus: 0.25
```

### Searching Learnings

```bash
# Full-text search
makima supervisor search-learnings "webpack ESM import error"

# Category filter
makima supervisor search-learnings --category build-errors

# Tag filter
makima supervisor search-learnings --tags webpack,esm

# Repository filter
makima supervisor search-learnings --repo github.com/org/repo

# Combined
makima supervisor search-learnings "import error" \
  --category build-errors \
  --tags webpack \
  --min-relevance 0.5 \
  --limit 5
```

---

## Open Questions

1. **Cross-repository knowledge**: Should learnings be scoped to a single repository or shared across all repositories for an owner?
2. **Learning ownership**: Who owns a learning — the contract creator, the repository, or the organization?
3. **Privacy**: Are learnings visible to all users, or scoped by access control?
4. **Embedding model**: For similarity-based deduplication and search, which embedding model should be used? Trade-off between quality and cost.
5. **Storage limits**: Should there be a cap on the number of learnings per repository/owner?
6. **Manual curation**: Should users be able to manually create, edit, or delete learnings outside the compound phase?
7. **Export/import**: Should learnings be exportable/importable across makima instances?

---

## Alternatives Considered

| Alternative | Pros | Cons | Decision |
|-------------|------|------|----------|
| Store learnings only in contract files | Simple, uses existing infrastructure | Not easily searchable across contracts | Rejected — search is critical |
| Store learnings only in repo files | Portable, version-controlled, greppable | Lost if repo deleted; no cross-repo search | Partial — use as secondary storage |
| Use external knowledge base (e.g., vector DB) | Best search quality | Added infrastructure dependency | Deferred — consider for v2 |
| Manual-only knowledge capture | No noise | Knowledge rarely captured | Rejected — must be automatic |
| Full contract history indexing | Most complete | Massive storage, noise, privacy concerns | Rejected — too much signal-to-noise |

---

## Priority & Complexity Assessment

- **Priority: HIGH** — This is the defining feature of compound engineering. Without knowledge accumulation, every contract starts from scratch. This is the feature that creates compounding returns.
- **Complexity: MEDIUM** — Core capture and storage is straightforward using existing contract files and repo sync. Search quality and relevance decay require iterative refinement.
- **Risk: MEDIUM** — Primary risk is low adoption (users skip compound phase) mitigated by auto-trigger. Secondary risk is knowledge base noise mitigated by quality gates.