summaryrefslogtreecommitdiff
path: root/makima-architecture.md
blob: 69ab06f145d275764e5b0993ead7b8d3bb8ac5d4 (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
# Makima Architecture Analysis

## Executive Summary

Makima is a distributed task orchestration system for managing AI coding agents (primarily Claude Code instances). It follows a client-server architecture with daemons running on local machines that execute tasks, while a central server coordinates work through contracts. The system already implements several patterns similar to ralph, including completion gates, autonomous loop mode, and circuit breakers.

---

## 1. Current Architecture Overview

### 1.1 High-Level Architecture

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                            MAKIMA SERVER (Rust)                              │
│  ┌─────────────┐  ┌──────────────┐  ┌─────────────┐  ┌──────────────────┐   │
│  │   REST API  │  │  WebSocket   │  │  PostgreSQL │  │   LLM Tools      │   │
│  │   Handlers  │  │    Hub       │  │     DB      │  │ (Chat, Analysis) │   │
│  └─────────────┘  └──────────────┘  └─────────────┘  └──────────────────┘   │
└─────────────────────────────────────────────────────────────────────────────┘
                                    │
                    ┌───────────────┼───────────────┐
                    ▼               ▼               ▼
            ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
            │   DAEMON 1   │ │   DAEMON 2   │ │   DAEMON N   │
            │  (Worker)    │ │  (Worker)    │ │  (Worker)    │
            ├──────────────┤ ├──────────────┤ ├──────────────┤
            │ Task Manager │ │ Task Manager │ │ Task Manager │
            │   Worktree   │ │   Worktree   │ │   Worktree   │
            │   Manager    │ │   Manager    │ │   Manager    │
            │   Process    │ │   Process    │ │   Process    │
            │   Manager    │ │   Manager    │ │   Manager    │
            └──────────────┘ └──────────────┘ └──────────────┘
                    │               │               │
                    ▼               ▼               ▼
            ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
            │ Claude Code  │ │ Claude Code  │ │ Claude Code  │
            │  Instances   │ │  Instances   │ │  Instances   │
            └──────────────┘ └──────────────┘ └──────────────┘
```

### 1.2 Core Components

#### Server-Side (`makima/src/server/`)

| Component | Location | Responsibility |
|-----------|----------|----------------|
| **REST API** | `handlers/*.rs` | HTTP endpoints for contracts, tasks, files, mesh operations |
| **WebSocket Hub** | `handlers/mesh_daemon.rs`, `mesh_ws.rs` | Real-time communication with daemons |
| **Database** | `../db/` | PostgreSQL via sqlx for persistent state |
| **Authentication** | `auth.rs` | API key and JWT authentication |
| **LLM Integration** | `../llm/` | Claude/Groq clients, tool execution |

#### Daemon-Side (`makima/src/daemon/`)

| Component | Location | Responsibility |
|-----------|----------|----------------|
| **Task Manager** | `task/manager.rs` | Task lifecycle, concurrency control |
| **Task State** | `task/state.rs` | State machine for task progression |
| **Completion Gate** | `task/completion_gate.rs` | Autonomous loop termination logic |
| **Process Manager** | `process/` | Spawns and manages Claude Code subprocesses |
| **Worktree Manager** | `worktree/` | Git worktree isolation for tasks |
| **WebSocket Client** | `ws/` | Bidirectional communication with server |
| **Local DB** | `db/local.rs` | SQLite for crash recovery |
| **TUI** | `tui/` | Interactive terminal interface |

---

## 2. Key Components and Responsibilities

### 2.1 Contract System

Contracts are the top-level organizational unit representing a body of work:

```rust
// From db/models.rs
pub struct Contract {
    pub id: Uuid,
    pub name: String,
    pub contract_type: String,    // "simple" or "specification"
    pub phase: String,            // research, specify, plan, execute, review
    pub status: String,           // active, completed, archived
    pub supervisor_task_id: Option<Uuid>,
    pub autonomous_loop: bool,    // Enable auto-restart on incomplete
    pub phase_guard: bool,        // Require user approval for phase transitions
}
```

**Contract Types:**
- `simple`: Plan → Execute workflow
- `specification`: Research → Specify → Plan → Execute → Review

### 2.2 Task Orchestration

Tasks represent individual units of work executed by Claude Code:

```rust
// Simplified from db/models.rs
pub struct Task {
    pub id: Uuid,
    pub contract_id: Option<Uuid>,
    pub parent_task_id: Option<Uuid>,
    pub is_supervisor: bool,
    pub status: String,  // pending, running, paused, blocked, done, failed
    pub plan: String,
    pub daemon_id: Option<Uuid>,
    pub continue_from_task_id: Option<Uuid>,  // For task continuation chains
    pub conversation_state: Option<serde_json::Value>,  // For resumption
}
```

**Task Hierarchy:**
1. **Supervisor Tasks**: Long-running orchestrators that spawn worker tasks
2. **Worker Tasks**: Execute specific implementation work
3. **Subtask Chains**: Tasks can continue from other tasks' worktrees

### 2.3 Task State Machine

```
                    ┌────────────────┐
                    │  Initializing  │
                    └───────┬────────┘
                            │
                    ┌───────▼────────┐
                    │    Starting    │
                    └───────┬────────┘
                            │
            ┌───────────────▼───────────────┐
            │           Running             │
            └───────┬───────┬───────┬───────┘
                    │       │       │
        ┌───────────▼─┐ ┌───▼───┐ ┌─▼────────┐
        │   Paused    │ │Blocked│ │Completed │
        └─────────────┘ └───────┘ └──────────┘
                    │       │
                    ▼       ▼
            ┌──────────────────────┐
            │  Failed/Interrupted  │
            └──────────────────────┘
```

### 2.4 Worktree Isolation

Each task gets its own git worktree, providing:
- Complete isolation from other tasks
- Ability to merge changes via git
- Checkpointing via git commits

```rust
// From daemon/worktree/mod.rs
pub struct WorktreeInfo {
    pub path: PathBuf,
    pub branch: String,
    pub task_id: Uuid,
}
```

---

## 3. Existing Context Management Mechanisms

### 3.1 Completion Gate (Ralph-Inspired)

The `CompletionGate` system allows tasks to signal completion status:

```rust
// From daemon/task/completion_gate.rs
pub struct CompletionGate {
    pub ready: bool,
    pub reason: Option<String>,
    pub progress: Option<String>,
    pub blockers: Option<Vec<String>>,
}
```

**Format in Claude output:**
```xml
<COMPLETION_GATE>
ready: true
reason: "All tests pass"
progress: "Implemented feature X"
</COMPLETION_GATE>
```

### 3.2 Circuit Breaker

Prevents infinite loops in autonomous mode:

```rust
pub struct CircuitBreaker {
    pub runs_without_changes: u32,      // Trips after 3 runs with no changes
    pub same_error_count: u32,          // Trips after 5 identical errors
    pub iteration_count: u32,           // Trips after 10 iterations
    pub is_open: bool,
    pub open_reason: Option<String>,
}
```

### 3.3 Autonomous Loop Mode

When `autonomous_loop: true` on a contract:
1. Task runs to completion
2. Output is parsed for `COMPLETION_GATE`
3. If `ready: false`, task is restarted with `--continue`
4. Circuit breaker monitors for stuck states

### 3.4 Supervisor State Persistence

```rust
pub struct SupervisorState {
    pub conversation_history: serde_json::Value,
    pub pending_task_ids: Vec<Uuid>,
    pub phase: String,
    pub last_activity: DateTime<Utc>,
}
```

### 3.5 Conversation Snapshots

```rust
pub struct ConversationSnapshot {
    pub task_id: Uuid,
    pub checkpoint_id: Option<Uuid>,
    pub snapshot_type: String,  // 'auto', 'manual', 'checkpoint'
    pub conversation_state: serde_json::Value,
}
```

### 3.6 Phase Guidance System

```rust
// From llm/phase_guidance.rs
pub struct PhaseDeliverables {
    pub phase: String,
    pub recommended_files: Vec<RecommendedFile>,
    pub requires_repository: bool,
    pub requires_tasks: bool,
    pub guidance: String,
}
```

---

## 4. Extension Points for Ralph-Inspired Features

### 4.1 Task Manager Hook Points

Location: `daemon/task/manager.rs`

| Hook Point | Current State | Extension Opportunity |
|------------|---------------|----------------------|
| `spawn_task()` | Creates worktree, spawns process | Add pre-flight checks, memory injection |
| `handle_output()` | Streams to server | Enhanced context extraction |
| `on_completion()` | Cleanup, status update | Post-task analysis, learning |
| `restart_with_continue()` | Autonomous loop restart | Context summarization |

### 4.2 Process Manager Hook Points

Location: `daemon/process/claude.rs`

| Hook Point | Current State | Extension Opportunity |
|------------|---------------|----------------------|
| `build_command()` | Constructs CLI args | Dynamic prompt injection |
| `inject_system_prompt()` | Static prompts | Context-aware prompting |
| `parse_output()` | JSON message parsing | Structured output extraction |

### 4.3 Server API Extension Points

Location: `server/handlers/`

| Endpoint Category | Files | Extension Opportunity |
|-------------------|-------|----------------------|
| Contract Daemon API | `contract_daemon.rs` | Enhanced progress tracking |
| Supervisor API | `mesh_supervisor.rs` | Smarter task scheduling |
| History API | `history.rs` | Learning from past sessions |

### 4.4 Configuration Extension Points

Location: `daemon/config.rs`

```rust
pub struct ProcessConfig {
    pub claude_command: String,
    pub claude_args: Vec<String>,
    pub env_vars: HashMap<String, String>,
    // Extension: Add ralph-style configs
    // pub context_window_size: usize,
    // pub memory_extraction_enabled: bool,
    // pub learning_mode: LearningMode,
}
```

---

## 5. Current Limitations That Ralph Patterns Could Address

### 5.1 Context Window Management

**Current State:**
- No explicit context window tracking
- Conversation history stored but not summarized
- `--continue` flag relies on Claude's session state

**Ralph Pattern Opportunities:**
- Token counting per message
- Automatic context summarization when approaching limits
- Smart context pruning strategies

### 5.2 Memory and Learning

**Current State:**
- Output stored in `task_events` table
- Checkpoints stored with diffs
- No cross-task learning

**Ralph Pattern Opportunities:**
- Extract patterns from successful completions
- Build knowledge base from task outputs
- Learn from failures to improve future prompts

### 5.3 Progress Tracking

**Current State:**
- `progress_summary` field on tasks
- `COMPLETION_GATE` for completion signaling
- Phase checklist for deliverables

**Ralph Pattern Opportunities:**
- Structured progress metrics extraction
- Confidence scoring
- Automatic milestone detection

### 5.4 Error Recovery

**Current State:**
- Circuit breaker stops on repeated errors
- Daemon failover with retry count
- Manual intervention required for stuck tasks

**Ralph Pattern Opportunities:**
- Intelligent error classification
- Automatic recovery strategies
- Error pattern learning

### 5.5 Task Planning

**Current State:**
- Human-written plans
- LLM-generated task breakdowns (via `task_output.rs`)
- Static orchestrator prompts

**Ralph Pattern Opportunities:**
- Dynamic plan refinement
- Dependency inference
- Resource estimation

---

## 6. Data Flow Diagrams

### 6.1 Task Execution Flow

```
┌─────────┐    SpawnTask     ┌──────────┐
│ Server  │ ───────────────► │  Daemon  │
└─────────┘                  └────┬─────┘
                                  │
                        ┌─────────▼─────────┐
                        │   Task Manager    │
                        │   - Create worktree│
                        │   - Setup env vars │
                        └─────────┬─────────┘
                                  │
                        ┌─────────▼─────────┐
                        │  Process Manager  │
                        │   - Spawn Claude  │
                        │   - Inject prompt │
                        └─────────┬─────────┘
                                  │
                        ┌─────────▼─────────┐
                        │   Claude Code     │
                        │   - Execute task  │
                        │   - Stream output │
                        └─────────┬─────────┘
                                  │
              ┌───────────────────┼───────────────────┐
              ▼                   ▼                   ▼
       ┌────────────┐    ┌──────────────┐    ┌─────────────┐
       │ TaskOutput │    │ Checkpoints  │    │ Completion  │
       │  Events    │    │   (Git)      │    │   Gate      │
       └────────────┘    └──────────────┘    └─────────────┘
```

### 6.2 Autonomous Loop Flow

```
┌─────────────────────────────────────────────────────────┐
│                    AUTONOMOUS LOOP                       │
└─────────────────────────────────────────────────────────┘
                            │
                ┌───────────▼───────────┐
                │    Execute Task       │
                └───────────┬───────────┘
                            │
                ┌───────────▼───────────┐
                │  Parse COMPLETION_GATE │
                └───────────┬───────────┘
                            │
            ┌───────────────┼───────────────┐
            │               │               │
            ▼               ▼               ▼
      ready: true    ready: false    No Gate Found
            │               │               │
            ▼               ▼               ▼
        Complete    ┌───────────┐    ┌───────────┐
                    │  Check    │    │  Circuit  │
                    │  Circuit  │    │  Breaker  │
                    │  Breaker  │    │   Trip?   │
                    └─────┬─────┘    └─────┬─────┘
                          │                │
                   ┌──────┴──────┐  ┌──────┴──────┐
                   ▼             ▼  ▼             ▼
               Continue       Stop          Continue
              with Loop                    with Loop
```

---

## 7. Key Files Reference

### Daemon Core
- `src/daemon/mod.rs` - Module exports
- `src/daemon/task/manager.rs` - Task lifecycle (~1200 lines)
- `src/daemon/task/state.rs` - State machine
- `src/daemon/task/completion_gate.rs` - Ralph-style completion
- `src/daemon/process/claude.rs` - Claude subprocess management
- `src/daemon/worktree/manager.rs` - Git worktree isolation
- `src/daemon/ws/protocol.rs` - Server-daemon protocol

### Server Core
- `src/server/mod.rs` - Route configuration
- `src/server/handlers/mesh_supervisor.rs` - Supervisor operations
- `src/server/handlers/contract_daemon.rs` - Contract CLI interface
- `src/server/handlers/history.rs` - Conversation history

### LLM Integration
- `src/llm/phase_guidance.rs` - Phase deliverables
- `src/llm/contract_tools.rs` - Contract interaction tools
- `src/llm/task_output.rs` - Output analysis

### CLI
- `src/bin/makima.rs` - CLI entry point
- `src/daemon/cli/mod.rs` - CLI commands

---

## 8. Recommendations for Ralph Integration

### Priority 1: Enhanced Context Management
- Add token counting to message parsing
- Implement context summarization triggers
- Create memory extraction pipeline

### Priority 2: Structured Progress Tracking
- Extend `COMPLETION_GATE` format
- Add confidence scoring
- Implement milestone detection

### Priority 3: Learning System
- Create patterns database
- Implement cross-task knowledge sharing
- Add success/failure analysis

### Priority 4: Improved Error Recovery
- Classify error types
- Create recovery playbooks
- Implement automatic retry strategies

---

## Appendix: Related Configuration

### Daemon Configuration (`makima-daemon.toml`)
```toml
[server]
url = "wss://api.makima.jp"
api_key = "..."

[process]
max_concurrent_tasks = 4
claude_command = "claude"
heartbeat_commit_interval_secs = 300

[worktree]
base_dir = "~/.makima/worktrees"
repos_dir = "~/.makima/repos"
```

### Environment Variables
- `MAKIMA_API_KEY` - API authentication
- `MAKIMA_DAEMON_SERVER_URL` - Server WebSocket URL
- `MAKIMA_TASK_ID` - Set in task environment
- `MAKIMA_CONTRACT_ID` - Set in task environment