claudectl 0.35.0

Auto-pilot for Claude Code — a local model watches every session and decides what to approve
Documentation
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
use serde::{Deserialize, Serialize};

// -- Event Types ---------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EventType {
    SessionObserved,
    TaskCreated,
    LeaseAcquired,
    LeaseReleased,
    MemoryWritten,
    InterruptRaised,
    InterruptDelivered,
    InterruptAcknowledged,
    HandoffCreated,
    HandoffAccepted,
    BlockerOpened,
    BlockerResolved,
}

impl EventType {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::SessionObserved => "session_observed",
            Self::TaskCreated => "task_created",
            Self::LeaseAcquired => "lease_acquired",
            Self::LeaseReleased => "lease_released",
            Self::MemoryWritten => "memory_written",
            Self::InterruptRaised => "interrupt_raised",
            Self::InterruptDelivered => "interrupt_delivered",
            Self::InterruptAcknowledged => "interrupt_acknowledged",
            Self::HandoffCreated => "handoff_created",
            Self::HandoffAccepted => "handoff_accepted",
            Self::BlockerOpened => "blocker_opened",
            Self::BlockerResolved => "blocker_resolved",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "session_observed" => Some(Self::SessionObserved),
            "task_created" => Some(Self::TaskCreated),
            "lease_acquired" => Some(Self::LeaseAcquired),
            "lease_released" => Some(Self::LeaseReleased),
            "memory_written" => Some(Self::MemoryWritten),
            "interrupt_raised" => Some(Self::InterruptRaised),
            "interrupt_delivered" => Some(Self::InterruptDelivered),
            "interrupt_acknowledged" => Some(Self::InterruptAcknowledged),
            "handoff_created" => Some(Self::HandoffCreated),
            "handoff_accepted" => Some(Self::HandoffAccepted),
            "blocker_opened" => Some(Self::BlockerOpened),
            "blocker_resolved" => Some(Self::BlockerResolved),
            _ => None,
        }
    }
}

impl std::fmt::Display for EventType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoordEvent {
    pub id: Option<i64>,
    pub event_type: EventType,
    pub timestamp: String,
    pub session_id: Option<String>,
    pub payload: serde_json::Value,
}

// -- Lease ---------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LeaseMode {
    Exclusive,
    SharedRead,
    SharedAppend,
    Advisory,
}

impl LeaseMode {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Exclusive => "exclusive",
            Self::SharedRead => "shared_read",
            Self::SharedAppend => "shared_append",
            Self::Advisory => "advisory",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "exclusive" => Some(Self::Exclusive),
            "shared_read" => Some(Self::SharedRead),
            "shared_append" => Some(Self::SharedAppend),
            "advisory" => Some(Self::Advisory),
            _ => None,
        }
    }
}

impl std::fmt::Display for LeaseMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LeaseStatus {
    Active,
    Released,
    Expired,
}

impl LeaseStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Active => "active",
            Self::Released => "released",
            Self::Expired => "expired",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "active" => Some(Self::Active),
            "released" => Some(Self::Released),
            "expired" => Some(Self::Expired),
            _ => None,
        }
    }
}

impl std::fmt::Display for LeaseStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lease {
    pub id: String,
    pub owner_session_id: String,
    pub owner_agent: String,
    pub resource_kind: String,
    pub resource_value: String,
    pub mode: LeaseMode,
    pub reason: String,
    pub acquired_at: String,
    pub expires_at: Option<String>,
    pub status: LeaseStatus,
}

// -- Blocker -------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BlockerStatus {
    Open,
    Resolved,
}

impl BlockerStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Open => "open",
            Self::Resolved => "resolved",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "open" => Some(Self::Open),
            "resolved" => Some(Self::Resolved),
            _ => None,
        }
    }
}

impl std::fmt::Display for BlockerStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Blocker {
    pub id: String,
    pub task_id: String,
    pub depends_on: Option<String>,
    pub waiting_for: String,
    pub status: BlockerStatus,
    pub owner_session_id: String,
    pub created_at: String,
    pub resolved_at: Option<String>,
}

// -- Handoff -------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HandoffState {
    pub goal: String,
    pub artifacts: Vec<String>,
    pub attempted: Vec<String>,
    pub next_steps: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Handoff {
    pub id: String,
    pub from_session_id: String,
    pub to_session_id: Option<String>,
    pub task_id: String,
    pub summary: String,
    pub state: HandoffState,
    pub priority: String,
    pub created_at: String,
    pub acknowledged_at: Option<String>,
}

// -- Interrupt -----------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InterruptType {
    Nudge,
    RequestInput,
    Pause,
    Compact,
    Reroute,
    ReleaseOwnership,
    Stop,
    Resume,
    DependencyUnblocked,
    HandoffReady,
}

impl InterruptType {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Nudge => "nudge",
            Self::RequestInput => "request_input",
            Self::Pause => "pause",
            Self::Compact => "compact",
            Self::Reroute => "reroute",
            Self::ReleaseOwnership => "release_ownership",
            Self::Stop => "stop",
            Self::Resume => "resume",
            Self::DependencyUnblocked => "dependency_unblocked",
            Self::HandoffReady => "handoff_ready",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "nudge" => Some(Self::Nudge),
            "request_input" => Some(Self::RequestInput),
            "pause" => Some(Self::Pause),
            "compact" => Some(Self::Compact),
            "reroute" => Some(Self::Reroute),
            "release_ownership" => Some(Self::ReleaseOwnership),
            "stop" => Some(Self::Stop),
            "resume" => Some(Self::Resume),
            "dependency_unblocked" => Some(Self::DependencyUnblocked),
            "handoff_ready" => Some(Self::HandoffReady),
            _ => None,
        }
    }
}

impl std::fmt::Display for InterruptType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InterruptState {
    Pending,
    Delivered,
    Acknowledged,
    Resolved,
    Expired,
    Dismissed,
}

impl InterruptState {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Pending => "pending",
            Self::Delivered => "delivered",
            Self::Acknowledged => "acknowledged",
            Self::Resolved => "resolved",
            Self::Expired => "expired",
            Self::Dismissed => "dismissed",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "pending" => Some(Self::Pending),
            "delivered" => Some(Self::Delivered),
            "acknowledged" => Some(Self::Acknowledged),
            "resolved" => Some(Self::Resolved),
            "expired" => Some(Self::Expired),
            "dismissed" => Some(Self::Dismissed),
            _ => None,
        }
    }
}

impl std::fmt::Display for InterruptState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Interrupt {
    pub id: String,
    pub interrupt_type: InterruptType,
    pub priority: String,
    pub target_session_id: String,
    pub reason: String,
    pub payload: Option<serde_json::Value>,
    pub delivery_mode: String,
    pub max_retries: u32,
    #[serde(default)]
    pub retry_count: u32,
    #[serde(default)]
    pub next_retry_at: Option<String>,
    pub expires_at: Option<String>,
    pub dedupe_key: Option<String>,
    pub state: InterruptState,
    pub created_at: String,
    pub delivered_at: Option<String>,
    pub acknowledged_at: Option<String>,
}

// -- Memory Record -------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Subject {
    pub kind: String,
    pub value: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryRecord {
    pub id: String,
    pub mem_type: String,
    pub scope: serde_json::Value,
    pub subjects: Vec<Subject>,
    pub summary: String,
    pub evidence: Vec<Subject>,
    pub source: Option<serde_json::Value>,
    pub confidence: f64,
    pub created_at: String,
    pub updated_at: String,
    pub expires_at: Option<String>,
    pub tags: Vec<String>,
}

// -- Tests ---------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn event_type_serde_roundtrip() {
        let val = EventType::LeaseAcquired;
        let json = serde_json::to_string(&val).unwrap();
        assert_eq!(json, "\"lease_acquired\"");
        let back: EventType = serde_json::from_str(&json).unwrap();
        assert_eq!(back, val);
    }

    #[test]
    fn event_type_all_variants_roundtrip() {
        let variants = [
            EventType::SessionObserved,
            EventType::TaskCreated,
            EventType::LeaseAcquired,
            EventType::LeaseReleased,
            EventType::MemoryWritten,
            EventType::InterruptRaised,
            EventType::InterruptDelivered,
            EventType::InterruptAcknowledged,
            EventType::HandoffCreated,
            EventType::HandoffAccepted,
            EventType::BlockerOpened,
            EventType::BlockerResolved,
        ];
        for v in variants {
            let s = v.as_str();
            assert_eq!(EventType::parse(s), Some(v));
            let json = serde_json::to_string(&v).unwrap();
            let back: EventType = serde_json::from_str(&json).unwrap();
            assert_eq!(back, v);
        }
    }

    #[test]
    fn lease_mode_serde_roundtrip() {
        let val = LeaseMode::SharedRead;
        let json = serde_json::to_string(&val).unwrap();
        assert_eq!(json, "\"shared_read\"");
        let back: LeaseMode = serde_json::from_str(&json).unwrap();
        assert_eq!(back, val);
    }

    #[test]
    fn interrupt_type_display() {
        assert_eq!(
            InterruptType::DependencyUnblocked.to_string(),
            "dependency_unblocked"
        );
        assert_eq!(
            InterruptType::ReleaseOwnership.to_string(),
            "release_ownership"
        );
    }

    #[test]
    fn interrupt_state_lifecycle_ordering() {
        // Verify all lifecycle states parse correctly
        let states = [
            "pending",
            "delivered",
            "acknowledged",
            "resolved",
            "expired",
            "dismissed",
        ];
        for s in states {
            assert!(InterruptState::parse(s).is_some(), "failed to parse: {s}");
        }
    }

    #[test]
    fn unknown_strings_return_none() {
        assert_eq!(EventType::parse("bogus"), None);
        assert_eq!(LeaseMode::parse("bogus"), None);
        assert_eq!(LeaseStatus::parse("bogus"), None);
        assert_eq!(BlockerStatus::parse("bogus"), None);
        assert_eq!(InterruptType::parse("bogus"), None);
        assert_eq!(InterruptState::parse("bogus"), None);
    }

    #[test]
    fn coord_event_json_roundtrip() {
        let event = CoordEvent {
            id: Some(1),
            event_type: EventType::HandoffCreated,
            timestamp: "2026-04-20T10:00:00Z".into(),
            session_id: Some("sess_1".into()),
            payload: serde_json::json!({"task": "fix_tests"}),
        };
        let json = serde_json::to_string(&event).unwrap();
        let back: CoordEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(back.event_type, EventType::HandoffCreated);
        assert_eq!(back.session_id.as_deref(), Some("sess_1"));
    }
}