summaryrefslogtreecommitdiff
path: root/makima/docs/PLAN-resume-history-system.md
blob: 9e81c93ec99a5b2c31fb4ffb68c3705fad366683 (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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
# Resume and History System - Implementation Plan

## Overview

This document provides a detailed, actionable implementation plan for the Resume and History System. The system enables users to view historical conversation data, resume interrupted work, and rewind/restore to previous states in the Makima platform.

**Key Reference Documents:**
- Specification: Resume and History System Specification
- Requirements: Requirements Document
- User Stories: User Stories Document

---

## Phase 1: Database Schema

**Objective:** Create the foundational database structures for storing conversation snapshots, history events, and supporting task forking.

### Task 1.1: Create Database Migrations

**Files to Create:**
- `makima/migrations/20250117000000_history_tables.sql`

**Schema Changes:**

```sql
-- 1. Conversation Snapshots table
-- Stores conversation state at specific points for rewind capability
CREATE TABLE IF NOT EXISTS conversation_snapshots (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    task_id UUID NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
    checkpoint_id UUID REFERENCES task_checkpoints(id) ON DELETE SET NULL,
    snapshot_type VARCHAR(50) NOT NULL,  -- 'auto', 'manual', 'checkpoint'
    message_count INTEGER NOT NULL,
    conversation_state JSONB NOT NULL,  -- Full conversation at this point
    metadata JSONB,  -- Additional context (token count, cost, etc.)
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_conversation_snapshots_task ON conversation_snapshots(task_id);
CREATE INDEX idx_conversation_snapshots_checkpoint ON conversation_snapshots(checkpoint_id);
CREATE INDEX idx_conversation_snapshots_created ON conversation_snapshots(created_at DESC);

-- 2. History Events table
-- Unified event stream for timeline views
CREATE TABLE IF NOT EXISTS history_events (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    contract_id UUID REFERENCES contracts(id) ON DELETE CASCADE,
    task_id UUID REFERENCES tasks(id) ON DELETE CASCADE,
    event_type VARCHAR(50) NOT NULL,  -- 'task', 'chat', 'checkpoint', 'phase', 'file'
    event_subtype VARCHAR(50),  -- Specific event: 'created', 'completed', 'message', etc.
    phase VARCHAR(50),  -- Contract phase when event occurred
    event_data JSONB NOT NULL,  -- Event-specific data
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_history_events_contract ON history_events(contract_id, created_at DESC);
CREATE INDEX idx_history_events_task ON history_events(task_id, created_at DESC);
CREATE INDEX idx_history_events_owner ON history_events(owner_id, created_at DESC);
CREATE INDEX idx_history_events_type ON history_events(event_type, created_at DESC);

-- 3. Alter task_checkpoints - add conversation snapshot reference
ALTER TABLE task_checkpoints
    ADD COLUMN conversation_snapshot_id UUID REFERENCES conversation_snapshots(id) ON DELETE SET NULL;

-- 4. Alter tasks - add forking fields
ALTER TABLE tasks
    ADD COLUMN forked_from_task_id UUID REFERENCES tasks(id) ON DELETE SET NULL,
    ADD COLUMN forked_at_checkpoint_id UUID REFERENCES task_checkpoints(id) ON DELETE SET NULL;

CREATE INDEX idx_tasks_forked_from ON tasks(forked_from_task_id) WHERE forked_from_task_id IS NOT NULL;

-- Comments for documentation
COMMENT ON TABLE conversation_snapshots IS 'Stores conversation state at specific points for rewind/resume capability';
COMMENT ON TABLE history_events IS 'Unified event stream for timeline views across contracts and tasks';
COMMENT ON COLUMN conversation_snapshots.snapshot_type IS 'Type: auto (periodic), manual (user-triggered), checkpoint (at git checkpoint)';
COMMENT ON COLUMN history_events.event_type IS 'Category: task, chat, checkpoint, phase, file';
```

**Complexity:** Medium
**Dependencies:** None
**Estimated Time:** 2-3 hours

---

## Phase 2: Repository Layer

**Objective:** Implement database access functions for conversation snapshots, history events, and enhanced checkpoint operations.

### Task 2.1: Core Models

**Files to Modify:**
- `makima/src/db/models.rs`

**New Types to Add:**

```rust
// ConversationSnapshot model
#[derive(Debug, Clone, FromRow, Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ConversationSnapshot {
    pub id: Uuid,
    pub task_id: Uuid,
    pub checkpoint_id: Option<Uuid>,
    pub snapshot_type: String,  // 'auto', 'manual', 'checkpoint'
    pub message_count: i32,
    #[sqlx(json)]
    pub conversation_state: serde_json::Value,
    #[sqlx(json)]
    pub metadata: Option<serde_json::Value>,
    pub created_at: DateTime<Utc>,
}

// HistoryEvent model
#[derive(Debug, Clone, FromRow, Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct HistoryEvent {
    pub id: Uuid,
    pub owner_id: Uuid,
    pub contract_id: Option<Uuid>,
    pub task_id: Option<Uuid>,
    pub event_type: String,
    pub event_subtype: Option<String>,
    pub phase: Option<String>,
    #[sqlx(json)]
    pub event_data: serde_json::Value,
    pub created_at: DateTime<Utc>,
}

// Unified ConversationMessage for API responses
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ConversationMessage {
    pub id: String,
    pub role: String,  // 'user', 'assistant', 'system', 'tool'
    pub content: String,
    pub timestamp: DateTime<Utc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCallInfo>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_input: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_result: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_error: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_count: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cost_usd: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ToolCallInfo {
    pub id: String,
    pub name: String,
    pub input: serde_json::Value,
}

// Query filters for history
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct HistoryQueryFilters {
    pub phase: Option<String>,
    pub event_types: Option<Vec<String>>,
    pub from: Option<DateTime<Utc>>,
    pub to: Option<DateTime<Utc>>,
    pub limit: Option<i32>,
    pub cursor: Option<String>,
}

// Resume request types
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ResumeSupervisorRequest {
    pub target_daemon_id: Option<Uuid>,
    pub resume_mode: String,  // 'continue', 'restart_phase', 'from_checkpoint'
    pub checkpoint_id: Option<Uuid>,
    pub additional_context: Option<String>,
}

#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ResumeFromCheckpointRequest {
    pub task_name: Option<String>,
    pub plan: String,
    pub include_conversation: Option<bool>,
    pub target_daemon_id: Option<Uuid>,
}

// Rewind request types
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct RewindTaskRequest {
    pub checkpoint_id: Option<Uuid>,
    pub checkpoint_sha: Option<String>,
    pub preserve_mode: String,  // 'discard', 'create_branch', 'stash'
    pub branch_name: Option<String>,
}

#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct RewindConversationRequest {
    pub to_message_id: Option<String>,
    pub to_timestamp: Option<DateTime<Utc>>,
    pub by_message_count: Option<i32>,
    pub rewind_code: Option<bool>,
}

// Fork request type
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ForkTaskRequest {
    pub fork_from_type: String,  // 'checkpoint', 'timestamp', 'message_id'
    pub fork_from_value: String,
    pub new_task_name: String,
    pub new_task_plan: String,
    pub include_conversation: Option<bool>,
    pub create_branch: Option<bool>,
    pub branch_name: Option<String>,
}
```

**Complexity:** Medium
**Dependencies:** Phase 1 complete
**Estimated Time:** 2-3 hours

### Task 2.2: Repository Functions

**Files to Modify:**
- `makima/src/db/repository.rs`

**Functions to Add:**

```rust
// ============================================================================
// Conversation Snapshots
// ============================================================================

/// Create a new conversation snapshot
pub async fn create_conversation_snapshot(
    pool: &PgPool,
    task_id: Uuid,
    checkpoint_id: Option<Uuid>,
    snapshot_type: &str,
    message_count: i32,
    conversation_state: serde_json::Value,
    metadata: Option<serde_json::Value>,
) -> Result<ConversationSnapshot, sqlx::Error>

/// Get a conversation snapshot by ID
pub async fn get_conversation_snapshot(
    pool: &PgPool,
    id: Uuid,
) -> Result<Option<ConversationSnapshot>, sqlx::Error>

/// Get conversation snapshot at a specific checkpoint
pub async fn get_conversation_at_checkpoint(
    pool: &PgPool,
    checkpoint_id: Uuid,
) -> Result<Option<ConversationSnapshot>, sqlx::Error>

/// List conversation snapshots for a task
pub async fn list_conversation_snapshots(
    pool: &PgPool,
    task_id: Uuid,
    limit: Option<i32>,
) -> Result<Vec<ConversationSnapshot>, sqlx::Error>

/// Delete conversation snapshots older than retention period
pub async fn cleanup_old_snapshots(
    pool: &PgPool,
    retention_days: i32,
) -> Result<u64, sqlx::Error>

// ============================================================================
// History Events
// ============================================================================

/// Record a new history event
pub async fn record_history_event(
    pool: &PgPool,
    owner_id: Uuid,
    contract_id: Option<Uuid>,
    task_id: Option<Uuid>,
    event_type: &str,
    event_subtype: Option<&str>,
    phase: Option<&str>,
    event_data: serde_json::Value,
) -> Result<HistoryEvent, sqlx::Error>

/// Get contract history timeline
pub async fn get_contract_history(
    pool: &PgPool,
    contract_id: Uuid,
    owner_id: Uuid,
    filters: &HistoryQueryFilters,
) -> Result<(Vec<HistoryEvent>, i64), sqlx::Error>

/// Get task history
pub async fn get_task_history(
    pool: &PgPool,
    task_id: Uuid,
    owner_id: Uuid,
    filters: &HistoryQueryFilters,
) -> Result<(Vec<HistoryEvent>, i64), sqlx::Error>

/// Get unified timeline for an owner
pub async fn get_timeline(
    pool: &PgPool,
    owner_id: Uuid,
    filters: &HistoryQueryFilters,
) -> Result<(Vec<HistoryEvent>, i64), sqlx::Error>

// ============================================================================
// Task Conversation Retrieval
// ============================================================================

/// Get task conversation messages (reconstructed from task_events)
pub async fn get_task_conversation(
    pool: &PgPool,
    task_id: Uuid,
    include_tool_calls: bool,
    include_tool_results: bool,
    limit: Option<i32>,
) -> Result<Vec<ConversationMessage>, sqlx::Error>

/// Get supervisor conversation (from supervisor_states)
pub async fn get_supervisor_conversation(
    pool: &PgPool,
    contract_id: Uuid,
) -> Result<Option<(SupervisorState, Vec<TaskSummary>)>, sqlx::Error>

// ============================================================================
// Checkpoint Operations
// ============================================================================

/// Create checkpoint with conversation snapshot
pub async fn create_checkpoint_with_snapshot(
    pool: &PgPool,
    task_id: Uuid,
    checkpoint_number: i32,
    commit_sha: &str,
    branch_name: &str,
    message: &str,
    files_changed: Option<serde_json::Value>,
    lines_added: Option<i32>,
    lines_removed: Option<i32>,
    conversation_state: serde_json::Value,
) -> Result<(TaskCheckpoint, ConversationSnapshot), sqlx::Error>

/// Get checkpoint diff information (requires daemon interaction for actual diff)
pub async fn get_checkpoint_info(
    pool: &PgPool,
    checkpoint_id: Uuid,
) -> Result<Option<(TaskCheckpoint, Option<TaskCheckpoint>)>, sqlx::Error>

// ============================================================================
// Fork Operations
// ============================================================================

/// Create forked task
pub async fn create_forked_task(
    pool: &PgPool,
    owner_id: Uuid,
    source_task_id: Uuid,
    checkpoint_id: Option<Uuid>,
    req: &CreateTaskRequest,
) -> Result<Task, sqlx::Error>
```

**Complexity:** Complex
**Dependencies:** Task 2.1 complete
**Estimated Time:** 4-5 hours

---

## Phase 3: API Endpoints

**Objective:** Implement REST API endpoints for history viewing, resume operations, and rewind/fork functionality.

### Task 3.1: History Endpoints

**Files to Create/Modify:**
- `makima/src/server/handlers/history.rs` (new file)
- `makima/src/server/handlers/mod.rs` (add module)
- `makima/src/server/mod.rs` (add routes)

**Endpoints to Implement:**

| Method | Path | Handler Function |
|--------|------|------------------|
| GET | `/api/v1/contracts/{id}/history` | `get_contract_history` |
| GET | `/api/v1/contracts/{id}/supervisor/conversation` | `get_supervisor_conversation` |
| GET | `/api/v1/mesh/tasks/{id}/conversation` | `get_task_conversation` |
| GET | `/api/v1/mesh/tasks/{id}/checkpoints/{cid}/diff` | `get_checkpoint_diff` |
| GET | `/api/v1/timeline` | `get_timeline` |

**Implementation Details:**

```rust
// makima/src/server/handlers/history.rs

/// GET /api/v1/contracts/{id}/history
/// Returns contract history timeline with filtering and pagination
#[utoipa::path(
    get,
    path = "/api/v1/contracts/{id}/history",
    params(
        ("id" = Uuid, Path, description = "Contract ID"),
        HistoryQueryFilters
    ),
    responses(
        (status = 200, body = ContractHistoryResponse),
        (status = 404, description = "Contract not found"),
    ),
    tag = "history"
)]
pub async fn get_contract_history(
    State(state): State<AppState>,
    Path(contract_id): Path<Uuid>,
    Query(filters): Query<HistoryQueryFilters>,
    auth: AuthenticatedUser,
) -> Result<Json<ContractHistoryResponse>, ApiError>

/// GET /api/v1/contracts/{id}/supervisor/conversation
/// Returns full supervisor conversation with spawned task references
#[utoipa::path(
    get,
    path = "/api/v1/contracts/{id}/supervisor/conversation",
    responses(
        (status = 200, body = SupervisorConversationResponse),
        (status = 404, description = "Supervisor not found"),
    ),
    tag = "history"
)]
pub async fn get_supervisor_conversation(
    State(state): State<AppState>,
    Path(contract_id): Path<Uuid>,
    auth: AuthenticatedUser,
) -> Result<Json<SupervisorConversationResponse>, ApiError>

/// GET /api/v1/mesh/tasks/{id}/conversation
/// Returns task conversation history
#[utoipa::path(
    get,
    path = "/api/v1/mesh/tasks/{id}/conversation",
    params(
        ("id" = Uuid, Path, description = "Task ID"),
        ("include_tool_calls" = Option<bool>, Query),
        ("include_tool_results" = Option<bool>, Query),
        ("limit" = Option<i32>, Query),
    ),
    responses(
        (status = 200, body = TaskConversationResponse),
        (status = 404, description = "Task not found"),
    ),
    tag = "history"
)]
pub async fn get_task_conversation(
    State(state): State<AppState>,
    Path(task_id): Path<Uuid>,
    Query(params): Query<TaskConversationParams>,
    auth: AuthenticatedUser,
) -> Result<Json<TaskConversationResponse>, ApiError>

/// GET /api/v1/mesh/tasks/{id}/checkpoints/{cid}/diff
/// Returns checkpoint diff (delegates to daemon for git diff)
pub async fn get_checkpoint_diff(
    State(state): State<AppState>,
    Path((task_id, checkpoint_id)): Path<(Uuid, Uuid)>,
    auth: AuthenticatedUser,
) -> Result<Json<CheckpointDiffResponse>, ApiError>

/// GET /api/v1/timeline
/// Returns unified timeline for authenticated user
pub async fn get_timeline(
    State(state): State<AppState>,
    Query(filters): Query<TimelineQueryFilters>,
    auth: AuthenticatedUser,
) -> Result<Json<TimelineResponse>, ApiError>
```

**Complexity:** Medium
**Dependencies:** Phase 2 complete
**Estimated Time:** 4-5 hours

### Task 3.2: Resume Endpoints

**Files to Modify:**
- `makima/src/server/handlers/mesh_supervisor.rs`
- `makima/src/server/handlers/mesh.rs`

**Endpoints to Implement:**

| Method | Path | Handler Function |
|--------|------|------------------|
| POST | `/api/v1/contracts/{id}/supervisor/resume` | `resume_supervisor` |
| POST | `/api/v1/mesh/tasks/{id}/checkpoints/{cid}/resume` | `resume_from_checkpoint` |
| POST | `/api/v1/mesh/tasks/{id}/continue` | `continue_task` (enhanced) |

**Implementation Details:**

```rust
// makima/src/server/handlers/mesh_supervisor.rs

/// POST /api/v1/contracts/{id}/supervisor/resume
/// Resume interrupted supervisor with specified mode
#[utoipa::path(
    post,
    path = "/api/v1/contracts/{id}/supervisor/resume",
    request_body = ResumeSupervisorRequest,
    responses(
        (status = 200, body = ResumeSupervisorResponse),
        (status = 404, description = "Contract/supervisor not found"),
        (status = 409, description = "Supervisor already running"),
    ),
    tag = "supervisor"
)]
pub async fn resume_supervisor(
    State(state): State<AppState>,
    Path(contract_id): Path<Uuid>,
    auth: AuthenticatedUser,
    Json(req): Json<ResumeSupervisorRequest>,
) -> Result<Json<ResumeSupervisorResponse>, ApiError>

// makima/src/server/handlers/mesh.rs

/// POST /api/v1/mesh/tasks/{id}/checkpoints/{cid}/resume
/// Create new task starting from specific checkpoint
#[utoipa::path(
    post,
    path = "/api/v1/mesh/tasks/{id}/checkpoints/{cid}/resume",
    request_body = ResumeFromCheckpointRequest,
    responses(
        (status = 201, body = ResumeFromCheckpointResponse),
        (status = 404, description = "Task/checkpoint not found"),
    ),
    tag = "mesh"
)]
pub async fn resume_from_checkpoint(
    State(state): State<AppState>,
    Path((task_id, checkpoint_id)): Path<(Uuid, Uuid)>,
    auth: AuthenticatedUser,
    Json(req): Json<ResumeFromCheckpointRequest>,
) -> Result<Json<ResumeFromCheckpointResponse>, ApiError>

/// POST /api/v1/mesh/tasks/{id}/continue (enhanced)
/// Enhanced with resume mode and context options
pub async fn continue_task(
    State(state): State<AppState>,
    Path(task_id): Path<Uuid>,
    auth: AuthenticatedUser,
    Json(req): Json<ContinueTaskRequest>,  // Enhanced request type
) -> Result<Json<Task>, ApiError>
```

**Complexity:** Complex
**Dependencies:** Task 3.1 complete
**Estimated Time:** 5-6 hours

### Task 3.3: Rewind and Fork Endpoints

**Files to Modify:**
- `makima/src/server/handlers/mesh.rs`
- `makima/src/server/handlers/mesh_supervisor.rs`

**Endpoints to Implement:**

| Method | Path | Handler Function |
|--------|------|------------------|
| POST | `/api/v1/mesh/tasks/{id}/rewind` | `rewind_task` |
| POST | `/api/v1/contracts/{id}/supervisor/conversation/rewind` | `rewind_conversation` |
| POST | `/api/v1/mesh/tasks/{id}/fork` | `fork_task` |
| POST | `/api/v1/mesh/tasks/{id}/checkpoints/{cid}/branch` | `branch_from_checkpoint` |

**Implementation Details:**

```rust
// makima/src/server/handlers/mesh.rs

/// POST /api/v1/mesh/tasks/{id}/rewind
/// Rewind task code to specified checkpoint
#[utoipa::path(
    post,
    path = "/api/v1/mesh/tasks/{id}/rewind",
    request_body = RewindTaskRequest,
    responses(
        (status = 200, body = RewindTaskResponse),
        (status = 404, description = "Task/checkpoint not found"),
        (status = 409, description = "Task is running"),
    ),
    tag = "mesh"
)]
pub async fn rewind_task(
    State(state): State<AppState>,
    Path(task_id): Path<Uuid>,
    auth: AuthenticatedUser,
    Json(req): Json<RewindTaskRequest>,
) -> Result<Json<RewindTaskResponse>, ApiError>

/// POST /api/v1/mesh/tasks/{id}/fork
/// Fork task from historical point
#[utoipa::path(
    post,
    path = "/api/v1/mesh/tasks/{id}/fork",
    request_body = ForkTaskRequest,
    responses(
        (status = 201, body = ForkTaskResponse),
        (status = 404, description = "Task not found"),
    ),
    tag = "mesh"
)]
pub async fn fork_task(
    State(state): State<AppState>,
    Path(task_id): Path<Uuid>,
    auth: AuthenticatedUser,
    Json(req): Json<ForkTaskRequest>,
) -> Result<Json<ForkTaskResponse>, ApiError>

/// POST /api/v1/mesh/tasks/{id}/checkpoints/{cid}/branch
/// Create git branch from checkpoint without starting task
pub async fn branch_from_checkpoint(
    State(state): State<AppState>,
    Path((task_id, checkpoint_id)): Path<(Uuid, Uuid)>,
    auth: AuthenticatedUser,
    Json(req): Json<CreateBranchFromCheckpointRequest>,
) -> Result<Json<BranchCreatedResponse>, ApiError>

// makima/src/server/handlers/mesh_supervisor.rs

/// POST /api/v1/contracts/{id}/supervisor/conversation/rewind
/// Rewind supervisor conversation to specified point
pub async fn rewind_conversation(
    State(state): State<AppState>,
    Path(contract_id): Path<Uuid>,
    auth: AuthenticatedUser,
    Json(req): Json<RewindConversationRequest>,
) -> Result<Json<RewindConversationResponse>, ApiError>
```

**Complexity:** Complex
**Dependencies:** Task 3.2 complete
**Estimated Time:** 5-6 hours

---

## Phase 4: CLI Commands

**Objective:** Implement command-line interface for history viewing, resume, and rewind operations.

### Task 4.1: History Commands

**Files to Modify:**
- `makima/src/daemon/cli/mod.rs`
- `makima/src/daemon/cli/contract.rs`
- `makima/src/daemon/cli/supervisor.rs`

**New Commands:**

```rust
// makima/src/daemon/cli/mod.rs

/// Contract subcommands - add history commands
#[derive(Subcommand, Debug)]
pub enum ContractCommand {
    // ... existing commands ...

    /// View contract history timeline
    History(contract::HistoryArgs),
}

/// Supervisor subcommands - add history and resume commands
#[derive(Subcommand, Debug)]
pub enum SupervisorCommand {
    // ... existing commands ...

    /// View task conversation history
    TaskHistory(supervisor::TaskHistoryArgs),

    /// List checkpoints with details
    CheckpointList(supervisor::CheckpointListArgs),

    /// View checkpoint diff
    CheckpointDiff(supervisor::CheckpointDiffArgs),

    /// Resume supervisor after interruption
    Resume(supervisor::ResumeArgs),
}
```

**Implementation - Contract History:**

```rust
// makima/src/daemon/cli/contract.rs

#[derive(Args, Debug)]
pub struct HistoryArgs {
    /// Filter by phase (research, specify, plan, execute, review)
    #[arg(long)]
    pub phase: Option<String>,

    /// Filter from date (ISO 8601 format)
    #[arg(long)]
    pub from: Option<String>,

    /// Filter to date (ISO 8601 format)
    #[arg(long)]
    pub to: Option<String>,

    /// Maximum entries to return
    #[arg(long, default_value = "50")]
    pub limit: i32,

    /// Output format (table, json)
    #[arg(long, default_value = "table")]
    pub format: String,
}

pub async fn handle_history(args: &HistoryArgs) -> Result<()> {
    // 1. Get contract context from environment
    // 2. Call /api/v1/contracts/{id}/history with filters
    // 3. Format and display results
}
```

**Implementation - Task History:**

```rust
// makima/src/daemon/cli/supervisor.rs

#[derive(Args, Debug)]
pub struct TaskHistoryArgs {
    /// Task ID to view history for
    pub task_id: Uuid,

    /// Include tool calls in output
    #[arg(long, default_value = "true")]
    pub tool_calls: bool,

    /// Maximum messages to return
    #[arg(long)]
    pub limit: Option<i32>,

    /// Output format (table, json, chat)
    #[arg(long, default_value = "chat")]
    pub format: String,
}

pub async fn handle_task_history(args: &TaskHistoryArgs) -> Result<()> {
    // 1. Call /api/v1/mesh/tasks/{id}/conversation
    // 2. Format as chat-style output or JSON
}

#[derive(Args, Debug)]
pub struct CheckpointListArgs {
    /// Task ID to list checkpoints for
    pub task_id: Uuid,

    /// Include diff summary
    #[arg(long)]
    pub with_diff: bool,
}

#[derive(Args, Debug)]
pub struct CheckpointDiffArgs {
    /// Task ID
    pub task_id: Uuid,

    /// Checkpoint number
    pub checkpoint_number: i32,
}
```

**Complexity:** Medium
**Dependencies:** Phase 3 complete
**Estimated Time:** 3-4 hours

### Task 4.2: Resume and Rewind Commands

**Files to Modify:**
- `makima/src/daemon/cli/supervisor.rs`
- `makima/src/daemon/cli/contract.rs`

**New Commands:**

```rust
// makima/src/daemon/cli/supervisor.rs

#[derive(Args, Debug)]
pub struct ResumeArgs {
    /// Resume mode: continue, restart_phase, from_checkpoint
    #[arg(long, default_value = "continue")]
    pub mode: String,

    /// Checkpoint ID (required for from_checkpoint mode)
    #[arg(long)]
    pub checkpoint: Option<Uuid>,

    /// Additional context to inject
    #[arg(long)]
    pub context: Option<String>,
}

pub async fn handle_resume(args: &ResumeArgs) -> Result<()> {
    // 1. Get contract context
    // 2. Call /api/v1/contracts/{id}/supervisor/resume
    // 3. Display result and new supervisor task info
}

#[derive(Args, Debug)]
pub struct TaskResumeArgs {
    /// Task ID to resume
    pub task_id: Uuid,

    /// Resume mode: with_context, clean_restart, from_checkpoint
    #[arg(long, default_value = "with_context")]
    pub mode: String,

    /// Checkpoint SHA (for from_checkpoint mode)
    #[arg(long)]
    pub checkpoint: Option<String>,
}

#[derive(Args, Debug)]
pub struct TaskResumeFromArgs {
    /// Source task ID
    pub task_id: Uuid,

    /// Checkpoint number to resume from
    #[arg(long)]
    pub checkpoint: i32,

    /// Plan for the new task
    #[arg(long)]
    pub plan: String,

    /// Name for the new task
    #[arg(long)]
    pub name: Option<String>,
}

#[derive(Args, Debug)]
pub struct TaskRewindArgs {
    /// Task ID to rewind
    pub task_id: Uuid,

    /// Checkpoint number to rewind to
    #[arg(long)]
    pub checkpoint: i32,

    /// Preserve mode: discard, create_branch, stash
    #[arg(long, default_value = "create_branch")]
    pub preserve: String,

    /// Branch name (for create_branch mode)
    #[arg(long)]
    pub branch_name: Option<String>,
}

#[derive(Args, Debug)]
pub struct TaskForkArgs {
    /// Source task ID
    pub task_id: Uuid,

    /// Checkpoint number to fork from
    #[arg(long)]
    pub checkpoint: i32,

    /// Name for the new task
    #[arg(long)]
    pub name: String,

    /// Plan for the new task
    #[arg(long)]
    pub plan: String,

    /// Include conversation history
    #[arg(long, default_value = "true")]
    pub include_conversation: bool,
}

#[derive(Args, Debug)]
pub struct ConversationRewindArgs {
    /// Number of messages to rewind
    #[arg(long)]
    pub by_messages: Option<i32>,

    /// Message ID to rewind to
    #[arg(long)]
    pub to_message: Option<String>,

    /// Also rewind code to matching checkpoint
    #[arg(long)]
    pub rewind_code: bool,
}
```

**Update CLI Commands enum:**

```rust
// makima/src/daemon/cli/mod.rs

#[derive(Subcommand, Debug)]
pub enum SupervisorCommand {
    // ... existing commands ...

    /// Resume supervisor after interruption
    Resume(supervisor::ResumeArgs),

    /// Resume task with context
    TaskResume(supervisor::TaskResumeArgs),

    /// Resume from specific checkpoint
    TaskResumeFrom(supervisor::TaskResumeFromArgs),

    /// Rewind task code to checkpoint
    TaskRewind(supervisor::TaskRewindArgs),

    /// Fork task from historical point
    TaskFork(supervisor::TaskForkArgs),

    /// Rewind supervisor conversation
    RewindConversation(supervisor::ConversationRewindArgs),
}
```

**Complexity:** Medium
**Dependencies:** Task 4.1 complete
**Estimated Time:** 4-5 hours

---

## Phase 5: Daemon Integration

**Objective:** Extend daemon protocol and handlers to support rewind operations and conversation snapshots.

### Task 5.1: Protocol Extensions

**Files to Modify:**
- `makima/src/daemon/ws/protocol.rs`
- `makima/src/server/protocol.rs` (if separate)

**New Commands to Add:**

```rust
// makima/src/daemon/ws/protocol.rs

/// Command from server to daemon
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum DaemonCommand {
    // ... existing commands ...

    /// Rewind task worktree to a specific checkpoint
    RewindToCheckpoint {
        #[serde(rename = "taskId")]
        task_id: Uuid,
        #[serde(rename = "checkpointSha")]
        checkpoint_sha: String,
        /// How to preserve current state: 'discard', 'create_branch', 'stash'
        #[serde(rename = "preserveMode")]
        preserve_mode: String,
        /// Branch name for create_branch mode
        #[serde(rename = "branchName")]
        branch_name: Option<String>,
    },

    /// Create a conversation snapshot for a task
    CreateConversationSnapshot {
        #[serde(rename = "taskId")]
        task_id: Uuid,
    },

    /// Get git diff between two commits
    GetCheckpointDiff {
        #[serde(rename = "taskId")]
        task_id: Uuid,
        /// SHA of the checkpoint to diff
        #[serde(rename = "checkpointSha")]
        checkpoint_sha: String,
        /// SHA of the previous checkpoint (for comparison)
        #[serde(rename = "previousSha")]
        previous_sha: Option<String>,
    },
}

/// Message from daemon to server
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum DaemonMessage {
    // ... existing messages ...

    /// Response to RewindToCheckpoint command
    RewindResult {
        #[serde(rename = "taskId")]
        task_id: Uuid,
        success: bool,
        message: String,
        /// Reference to preserved state (branch name or stash ref)
        #[serde(rename = "preservedAs")]
        preserved_as: Option<PreservedState>,
    },

    /// Response to CreateConversationSnapshot command
    ConversationSnapshotCreated {
        #[serde(rename = "taskId")]
        task_id: Uuid,
        #[serde(rename = "snapshotId")]
        snapshot_id: Uuid,
        #[serde(rename = "messageCount")]
        message_count: i32,
    },

    /// Response to GetCheckpointDiff command
    CheckpointDiffResult {
        #[serde(rename = "taskId")]
        task_id: Uuid,
        success: bool,
        diff: Option<GitDiffInfo>,
        error: Option<String>,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PreservedState {
    /// Type: 'branch' or 'stash'
    #[serde(rename = "type")]
    pub state_type: String,
    /// Branch name or stash reference
    pub reference: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GitDiffInfo {
    pub files: Vec<FileDiffInfo>,
    pub stats: DiffStats,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileDiffInfo {
    pub path: String,
    /// Action: 'added', 'modified', 'deleted'
    pub action: String,
    pub additions: i32,
    pub deletions: i32,
    /// Diff hunks (optional, may be truncated for large files)
    pub hunks: Option<Vec<DiffHunk>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DiffHunk {
    pub header: String,
    pub lines: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DiffStats {
    pub files_changed: i32,
    pub insertions: i32,
    pub deletions: i32,
}
```

**Complexity:** Medium
**Dependencies:** None (can be done in parallel with Phase 3-4)
**Estimated Time:** 2-3 hours

### Task 5.2: Daemon Handlers

**Files to Modify:**
- `makima/src/daemon/task/manager.rs`
- `makima/src/daemon/worktree/manager.rs`

**Handlers to Implement:**

```rust
// makima/src/daemon/task/manager.rs

impl TaskManager {
    /// Handle rewind to checkpoint command
    pub async fn handle_rewind_to_checkpoint(
        &mut self,
        task_id: Uuid,
        checkpoint_sha: &str,
        preserve_mode: &str,
        branch_name: Option<&str>,
    ) -> Result<RewindResult, DaemonError> {
        // 1. Validate task exists and is not running
        // 2. Get worktree path for task
        // 3. Based on preserve_mode:
        //    - 'discard': git reset --hard {sha}
        //    - 'create_branch': git branch {name} && git reset --hard {sha}
        //    - 'stash': git stash && git reset --hard {sha}
        // 4. Return result with preserved reference
    }

    /// Handle conversation snapshot creation
    pub async fn handle_create_conversation_snapshot(
        &mut self,
        task_id: Uuid,
    ) -> Result<SnapshotResult, DaemonError> {
        // 1. Get task state including conversation
        // 2. Send to server for storage
        // 3. Return snapshot ID
    }

    /// Handle checkpoint diff request
    pub async fn handle_get_checkpoint_diff(
        &mut self,
        task_id: Uuid,
        checkpoint_sha: &str,
        previous_sha: Option<&str>,
    ) -> Result<GitDiffInfo, DaemonError> {
        // 1. Get worktree path for task
        // 2. Run git diff command
        // 3. Parse and return diff info
    }
}
```

**Files to Modify:**
- `makima/src/daemon/worktree/manager.rs`

**Helper Functions:**

```rust
// makima/src/daemon/worktree/manager.rs

impl WorktreeManager {
    /// Reset worktree to specific commit
    pub async fn reset_to_commit(
        &self,
        worktree_path: &Path,
        commit_sha: &str,
    ) -> Result<(), WorktreeError>

    /// Create branch from current state
    pub async fn create_branch(
        &self,
        worktree_path: &Path,
        branch_name: &str,
    ) -> Result<(), WorktreeError>

    /// Stash current changes
    pub async fn stash_changes(
        &self,
        worktree_path: &Path,
    ) -> Result<String, WorktreeError>  // Returns stash ref

    /// Get diff between two commits
    pub async fn get_diff(
        &self,
        worktree_path: &Path,
        from_sha: &str,
        to_sha: &str,
    ) -> Result<String, WorktreeError>

    /// Parse git diff output into structured format
    pub fn parse_diff(diff_output: &str) -> Result<GitDiffInfo, ParseError>
}
```

**Complexity:** Complex
**Dependencies:** Task 5.1 complete
**Estimated Time:** 4-5 hours

### Task 5.3: Checkpoint Enhancement

**Files to Modify:**
- `makima/src/daemon/task/manager.rs`
- `makima/src/server/handlers/mesh_daemon.rs`

**Changes:**

1. **Update checkpoint creation to include conversation state:**

```rust
// When creating a checkpoint, also capture current conversation
pub async fn create_checkpoint_with_conversation(
    &mut self,
    task_id: Uuid,
    message: &str,
) -> Result<(TaskCheckpoint, ConversationSnapshot), DaemonError> {
    // 1. Create git commit (existing logic)
    // 2. Capture current conversation state
    // 3. Create checkpoint record with snapshot reference
}
```

2. **Update task spawning to handle fork/resume scenarios:**

```rust
// When spawning task with forked_from_task_id or checkpoint_sha:
// - If checkpoint_sha: create worktree at that specific commit
// - If include_conversation: inject conversation history into plan
```

**Complexity:** Medium
**Dependencies:** Task 5.2 complete
**Estimated Time:** 3-4 hours

---

## Implementation Summary

### Phase Dependencies

```
Phase 1 (Database)
    └─> Phase 2 (Repository)
            └─> Phase 3 (API Endpoints)
                    └─> Phase 4 (CLI Commands)

Phase 5 (Daemon) can be done in parallel with Phases 3-4
```

### Estimated Total Time

| Phase | Tasks | Estimated Hours |
|-------|-------|-----------------|
| Phase 1: Database Schema | 1 task | 2-3 hours |
| Phase 2: Repository Layer | 2 tasks | 6-8 hours |
| Phase 3: API Endpoints | 3 tasks | 14-17 hours |
| Phase 4: CLI Commands | 2 tasks | 7-9 hours |
| Phase 5: Daemon Integration | 3 tasks | 9-12 hours |
| **Total** | **11 tasks** | **38-49 hours** |

### Task Breakdown by Complexity

| Complexity | Count | Tasks |
|------------|-------|-------|
| Simple | 1 | Phase 1 Migration |
| Medium | 6 | Models, History Endpoints, CLI History, CLI Resume, Protocol, Checkpoint Enhancement |
| Complex | 4 | Repository Functions, Resume Endpoints, Rewind/Fork Endpoints, Daemon Handlers |

### Files to Create

1. `makima/migrations/20250117000000_history_tables.sql`
2. `makima/src/server/handlers/history.rs`

### Files to Modify

**Database Layer:**
- `makima/src/db/models.rs`
- `makima/src/db/repository.rs`

**API Layer:**
- `makima/src/server/handlers/mod.rs`
- `makima/src/server/handlers/mesh.rs`
- `makima/src/server/handlers/mesh_supervisor.rs`
- `makima/src/server/mod.rs`

**CLI Layer:**
- `makima/src/daemon/cli/mod.rs`
- `makima/src/daemon/cli/contract.rs`
- `makima/src/daemon/cli/supervisor.rs`

**Daemon Layer:**
- `makima/src/daemon/ws/protocol.rs`
- `makima/src/daemon/task/manager.rs`
- `makima/src/daemon/worktree/manager.rs`
- `makima/src/server/handlers/mesh_daemon.rs`

---

## Testing Strategy

### Unit Tests
- Repository functions: CRUD operations for snapshots and events
- Model serialization/deserialization
- Diff parsing functions

### Integration Tests
- API endpoint tests with mock database
- CLI command execution tests
- Daemon command handling tests

### End-to-End Tests
- Complete workflow: Create task -> Checkpoint -> Rewind -> Resume
- Fork workflow: Create task -> Checkpoint -> Fork -> Verify state
- History viewing across contract lifecycle

### Manual Testing Checklist
- [ ] Create conversation snapshot at checkpoint
- [ ] View contract history timeline with filters
- [ ] View task conversation history
- [ ] Resume interrupted supervisor (all modes)
- [ ] Resume task from specific checkpoint
- [ ] Rewind code with branch preservation
- [ ] Rewind conversation to message
- [ ] Fork task from checkpoint
- [ ] CLI commands output correctly formatted

---

## Security Considerations

1. **Owner verification**: All operations verify owner_id authorization
2. **Tool key authentication**: Supervisor operations require valid tool keys
3. **Data isolation**: Users cannot access other users' history
4. **Audit logging**: Log all rewind/fork operations for traceability

## Performance Considerations

1. **Pagination**: All list endpoints support cursor-based pagination
2. **Index usage**: Queries use appropriate indexes (contract_id, task_id, created_at)
3. **Snapshot cleanup**: Implement retention policy for old snapshots
4. **Response limits**: Cap response sizes for conversation history