ito-domain 0.1.29

Domain models and repositories for Ito
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
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
//! Audit event types and builder.
//!
//! Defines the core `AuditEvent` struct and associated enums for the
//! append-only audit log. Events are serialized as single-line JSON objects
//! (JSONL) and are never modified or deleted after creation.

use serde::{Deserialize, Serialize};

/// Current schema version. Bumped only on breaking changes.
pub const SCHEMA_VERSION: u32 = 1;

/// A single audit event recording a domain state transition.
///
/// Events are append-only: once written, they are never modified or deleted.
/// Corrections are recorded as new compensating events.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AuditEvent {
    /// Schema version (currently 1).
    pub v: u32,
    /// UTC timestamp in RFC 3339 format with millisecond precision.
    pub ts: String,
    /// Entity type (task, change, module, wave, planning, config).
    pub entity: String,
    /// Entity identifier (task id, change id, module id, config key, etc.).
    pub entity_id: String,
    /// Scoping context — the change_id for task/wave events, None for global entities.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope: Option<String>,
    /// Operation type (e.g., status_change, create, archive).
    pub op: String,
    /// Previous state value (None for create operations).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,
    /// New state value.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<String>,
    /// Mutation source (cli, reconcile, ralph).
    pub actor: String,
    /// User/agent identity (e.g., @jack).
    pub by: String,
    /// Optional operation-specific metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<serde_json::Value>,
    /// Number of equivalent adjacent events represented by this event.
    #[serde(default = "default_count", skip_serializing_if = "is_default_count")]
    pub count: u64,
    /// Session and git context for traceability.
    pub ctx: EventContext,
}

fn is_default_count(count: &u64) -> bool {
    *count <= 1
}

fn default_count() -> u64 {
    1
}

/// Known entity types for the audit log.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EntityType {
    /// A task within a change.
    Task,
    /// A change (proposal + specs + tasks).
    Change,
    /// A module grouping related changes.
    Module,
    /// A wave within a task plan.
    Wave,
    /// A planning entry (decision, blocker, note, etc.).
    Planning,
    /// A configuration key.
    Config,
}

impl EntityType {
    /// Returns the string representation used in event serialization.
    pub fn as_str(&self) -> &'static str {
        match self {
            EntityType::Task => "task",
            EntityType::Change => "change",
            EntityType::Module => "module",
            EntityType::Wave => "wave",
            EntityType::Planning => "planning",
            EntityType::Config => "config",
        }
    }
}

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

/// Known actor types for the audit log.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Actor {
    /// Event emitted from a normal CLI command.
    Cli,
    /// Compensating event emitted by reconciliation.
    Reconcile,
    /// Event emitted by the Ralph automation loop.
    Ralph,
}

impl Actor {
    /// Returns the string representation used in event serialization.
    pub fn as_str(&self) -> &'static str {
        match self {
            Actor::Cli => "cli",
            Actor::Reconcile => "reconcile",
            Actor::Ralph => "ralph",
        }
    }
}

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

/// Session and git context captured at event-write time.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct EventContext {
    /// Ito-generated UUID v4 per CLI process group.
    pub session_id: String,
    /// Optional harness session ID (from env vars).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub harness_session_id: Option<String>,
    /// Current git branch name (None if detached HEAD).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch: Option<String>,
    /// Worktree name if not the main worktree.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub worktree: Option<String>,
    /// Short HEAD commit hash.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub commit: Option<String>,
}

/// Information about a git worktree for multi-worktree streaming.
#[derive(Debug, Clone, PartialEq)]
pub struct WorktreeInfo {
    /// Worktree filesystem path.
    pub path: std::path::PathBuf,
    /// Branch checked out in this worktree (None if detached).
    pub branch: Option<String>,
    /// Whether this is the main worktree.
    pub is_main: bool,
}

/// An audit event tagged with its source worktree (used during streaming).
#[derive(Debug, Clone)]
pub struct TaggedAuditEvent {
    /// The audit event.
    pub event: AuditEvent,
    /// The worktree this event was read from.
    pub source: WorktreeInfo,
}

/// Operation type constants for each entity.
///
/// These are used as the `op` field in `AuditEvent`. Using constants instead
/// of free-form strings prevents typos and enables exhaustive matching.
pub mod ops {
    // Task operations
    /// Task created.
    pub const TASK_CREATE: &str = "create";
    /// Task status changed.
    pub const TASK_STATUS_CHANGE: &str = "status_change";
    /// Task added to an existing plan.
    pub const TASK_ADD: &str = "add";

    // Change operations
    /// Change created.
    pub const CHANGE_CREATE: &str = "create";
    /// Change archived.
    pub const CHANGE_ARCHIVE: &str = "archive";

    // Module operations
    /// Module created.
    pub const MODULE_CREATE: &str = "create";
    /// Change added to a module.
    pub const MODULE_CHANGE_ADDED: &str = "change_added";
    /// Change completed within a module.
    pub const MODULE_CHANGE_COMPLETED: &str = "change_completed";

    // Wave operations
    /// Wave unlocked (all predecessors complete).
    pub const WAVE_UNLOCK: &str = "unlock";

    // Planning operations
    /// Planning decision recorded.
    pub const PLANNING_DECISION: &str = "decision";
    /// Planning blocker recorded.
    pub const PLANNING_BLOCKER: &str = "blocker";
    /// Planning question recorded.
    pub const PLANNING_QUESTION: &str = "question";
    /// Planning note recorded.
    pub const PLANNING_NOTE: &str = "note";
    /// Planning focus changed.
    pub const PLANNING_FOCUS_CHANGE: &str = "focus_change";

    // Config operations
    /// Config key set.
    pub const CONFIG_SET: &str = "set";
    /// Config key unset.
    pub const CONFIG_UNSET: &str = "unset";

    // Reconciliation
    /// Reconciliation compensating event.
    pub const RECONCILED: &str = "reconciled";
}

/// Builder for constructing `AuditEvent` instances.
///
/// Auto-populates `v` (schema version), `ts` (UTC now), and enforces
/// required fields at build time.
pub struct AuditEventBuilder {
    entity: Option<EntityType>,
    entity_id: Option<String>,
    scope: Option<String>,
    op: Option<String>,
    from: Option<String>,
    to: Option<String>,
    actor: Option<Actor>,
    by: Option<String>,
    meta: Option<serde_json::Value>,
    ctx: Option<EventContext>,
}

impl AuditEventBuilder {
    /// Create a new builder with defaults.
    pub fn new() -> Self {
        Self {
            entity: None,
            entity_id: None,
            scope: None,
            op: None,
            from: None,
            to: None,
            actor: None,
            by: None,
            meta: None,
            ctx: None,
        }
    }

    /// Set the entity type.
    pub fn entity(mut self, entity: EntityType) -> Self {
        self.entity = Some(entity);
        self
    }

    /// Set the entity identifier.
    pub fn entity_id(mut self, id: impl Into<String>) -> Self {
        self.entity_id = Some(id.into());
        self
    }

    /// Set the scope (change_id for task/wave events).
    pub fn scope(mut self, scope: impl Into<String>) -> Self {
        self.scope = Some(scope.into());
        self
    }

    /// Set the operation type.
    pub fn op(mut self, op: impl Into<String>) -> Self {
        self.op = Some(op.into());
        self
    }

    /// Set the previous state value.
    pub fn from(mut self, from: impl Into<String>) -> Self {
        self.from = Some(from.into());
        self
    }

    /// Set the new state value.
    pub fn to(mut self, to: impl Into<String>) -> Self {
        self.to = Some(to.into());
        self
    }

    /// Set the actor.
    pub fn actor(mut self, actor: Actor) -> Self {
        self.actor = Some(actor);
        self
    }

    /// Set the user identity.
    pub fn by(mut self, by: impl Into<String>) -> Self {
        self.by = Some(by.into());
        self
    }

    /// Set optional metadata.
    pub fn meta(mut self, meta: serde_json::Value) -> Self {
        self.meta = Some(meta);
        self
    }

    /// Set the event context.
    pub fn ctx(mut self, ctx: EventContext) -> Self {
        self.ctx = Some(ctx);
        self
    }

    /// Build the `AuditEvent`, using the current UTC time for `ts`.
    ///
    /// Returns `None` if required fields (entity, entity_id, op, actor, by, ctx)
    /// are missing.
    pub fn build(self) -> Option<AuditEvent> {
        let entity = self.entity?;
        let entity_id = self.entity_id?;
        let op = self.op?;
        let actor = self.actor?;
        let by = self.by?;
        let ctx = self.ctx?;

        let ts = chrono::Utc::now()
            .format("%Y-%m-%dT%H:%M:%S%.3fZ")
            .to_string();

        Some(AuditEvent {
            v: SCHEMA_VERSION,
            ts,
            entity: entity.as_str().to_string(),
            entity_id,
            scope: self.scope,
            op,
            from: self.from,
            to: self.to,
            actor: actor.as_str().to_string(),
            by,
            meta: self.meta,
            count: 1,
            ctx,
        })
    }
}

impl Default for AuditEventBuilder {
    fn default() -> Self {
        Self::new()
    }
}

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

    fn test_ctx() -> EventContext {
        EventContext {
            session_id: "test-session-id".to_string(),
            harness_session_id: None,
            branch: Some("main".to_string()),
            worktree: None,
            commit: Some("abc12345".to_string()),
        }
    }

    #[test]
    fn audit_event_round_trip_serialization() {
        let event = AuditEvent {
            v: 1,
            ts: "2026-02-08T14:30:00.000Z".to_string(),
            entity: "task".to_string(),
            entity_id: "2.1".to_string(),
            scope: Some("009-02_audit-log".to_string()),
            op: "status_change".to_string(),
            from: Some("pending".to_string()),
            to: Some("in-progress".to_string()),
            actor: "cli".to_string(),
            by: "@jack".to_string(),
            meta: None,
            count: 1,
            ctx: test_ctx(),
        };

        let json = serde_json::to_string(&event).expect("serialize");
        let parsed: AuditEvent = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(event, parsed);
    }

    #[test]
    fn audit_event_serializes_to_single_line() {
        let event = AuditEvent {
            v: 1,
            ts: "2026-02-08T14:30:00.000Z".to_string(),
            entity: "task".to_string(),
            entity_id: "1.1".to_string(),
            scope: Some("test-change".to_string()),
            op: "create".to_string(),
            from: None,
            to: Some("pending".to_string()),
            actor: "cli".to_string(),
            by: "@test".to_string(),
            meta: None,
            count: 1,
            ctx: test_ctx(),
        };

        let json = serde_json::to_string(&event).expect("serialize");
        assert!(!json.contains('\n'));
    }

    #[test]
    fn optional_fields_omitted_when_none() {
        let event = AuditEvent {
            v: 1,
            ts: "2026-02-08T14:30:00.000Z".to_string(),
            entity: "change".to_string(),
            entity_id: "test".to_string(),
            scope: None,
            op: "create".to_string(),
            from: None,
            to: None,
            actor: "cli".to_string(),
            by: "@test".to_string(),
            meta: None,
            count: 1,
            ctx: EventContext {
                session_id: "sid".to_string(),
                harness_session_id: None,
                branch: None,
                worktree: None,
                commit: None,
            },
        };

        let json = serde_json::to_string(&event).expect("serialize");
        assert!(!json.contains("scope"));
        assert!(!json.contains("from"));
        assert!(!json.contains("\"to\""));
        assert!(!json.contains("meta"));
        assert!(!json.contains("count"));
        assert!(!json.contains("harness_session_id"));
        assert!(!json.contains("branch"));
        assert!(!json.contains("worktree"));
        assert!(!json.contains("commit"));
    }

    #[test]
    fn missing_count_deserializes_as_one() {
        let json = r#"{
            "v": 1,
            "ts": "2026-02-08T14:30:00.000Z",
            "entity": "task",
            "entity_id": "1.1",
            "op": "create",
            "actor": "cli",
            "by": "@test",
            "ctx": { "session_id": "sid" }
        }"#;

        let event: AuditEvent = serde_json::from_str(json).expect("deserialize");
        assert_eq!(event.count, 1);
    }

    #[test]
    fn count_serializes_when_greater_than_one() {
        let event = AuditEvent {
            v: 1,
            ts: "2026-02-08T14:30:00.000Z".to_string(),
            entity: "task".to_string(),
            entity_id: "1.1".to_string(),
            scope: None,
            op: "reconciled".to_string(),
            from: None,
            to: None,
            actor: "reconcile".to_string(),
            by: "@reconcile".to_string(),
            meta: None,
            count: 2,
            ctx: EventContext {
                session_id: "sid".to_string(),
                harness_session_id: None,
                branch: None,
                worktree: None,
                commit: None,
            },
        };

        let json = serde_json::to_string(&event).expect("serialize");
        assert!(json.contains("\"count\":2"));
    }

    #[test]
    fn entity_type_serializes_to_lowercase() {
        let json = serde_json::to_string(&EntityType::Task).expect("serialize");
        assert_eq!(json, "\"task\"");

        let json = serde_json::to_string(&EntityType::Config).expect("serialize");
        assert_eq!(json, "\"config\"");
    }

    #[test]
    fn entity_type_round_trip() {
        let variants = [
            EntityType::Task,
            EntityType::Change,
            EntityType::Module,
            EntityType::Wave,
            EntityType::Planning,
            EntityType::Config,
        ];
        for variant in variants {
            let json = serde_json::to_string(&variant).expect("serialize");
            let parsed: EntityType = serde_json::from_str(&json).expect("deserialize");
            assert_eq!(variant, parsed);
        }
    }

    #[test]
    fn actor_serializes_to_lowercase() {
        assert_eq!(Actor::Cli.as_str(), "cli");
        assert_eq!(Actor::Reconcile.as_str(), "reconcile");
        assert_eq!(Actor::Ralph.as_str(), "ralph");
    }

    #[test]
    fn actor_round_trip() {
        let variants = [Actor::Cli, Actor::Reconcile, Actor::Ralph];
        for variant in variants {
            let json = serde_json::to_string(&variant).expect("serialize");
            let parsed: Actor = serde_json::from_str(&json).expect("deserialize");
            assert_eq!(variant, parsed);
        }
    }

    #[test]
    fn builder_produces_valid_event() {
        let event = AuditEventBuilder::new()
            .entity(EntityType::Task)
            .entity_id("1.1")
            .scope("test-change")
            .op(ops::TASK_STATUS_CHANGE)
            .from("pending")
            .to("in-progress")
            .actor(Actor::Cli)
            .by("@jack")
            .ctx(test_ctx())
            .build()
            .expect("should build");

        assert_eq!(event.v, SCHEMA_VERSION);
        assert_eq!(event.entity, "task");
        assert_eq!(event.entity_id, "1.1");
        assert_eq!(event.scope, Some("test-change".to_string()));
        assert_eq!(event.op, "status_change");
        assert_eq!(event.from, Some("pending".to_string()));
        assert_eq!(event.to, Some("in-progress".to_string()));
        assert_eq!(event.actor, "cli");
        assert_eq!(event.by, "@jack");
        assert!(!event.ts.is_empty());
    }

    #[test]
    fn builder_returns_none_without_required_fields() {
        assert!(AuditEventBuilder::new().build().is_none());

        // Missing entity_id
        assert!(
            AuditEventBuilder::new()
                .entity(EntityType::Task)
                .op("create")
                .actor(Actor::Cli)
                .by("@test")
                .ctx(test_ctx())
                .build()
                .is_none()
        );
    }

    #[test]
    fn builder_with_meta() {
        let meta = serde_json::json!({"wave": 2, "name": "Test task"});
        let event = AuditEventBuilder::new()
            .entity(EntityType::Task)
            .entity_id("2.1")
            .scope("change-1")
            .op(ops::TASK_ADD)
            .to("pending")
            .actor(Actor::Cli)
            .by("@test")
            .meta(meta.clone())
            .ctx(test_ctx())
            .build()
            .expect("should build");

        assert_eq!(event.meta, Some(meta));
    }

    #[test]
    fn schema_version_is_one() {
        assert_eq!(SCHEMA_VERSION, 1);
    }

    #[test]
    fn event_context_round_trip() {
        let ctx = EventContext {
            session_id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890".to_string(),
            harness_session_id: Some("ses_abc123".to_string()),
            branch: Some("feat/audit-log".to_string()),
            worktree: Some("audit-log".to_string()),
            commit: Some("3a7f2b1c".to_string()),
        };

        let json = serde_json::to_string(&ctx).expect("serialize");
        let parsed: EventContext = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(ctx, parsed);
    }

    #[test]
    fn entity_type_display() {
        assert_eq!(EntityType::Task.to_string(), "task");
        assert_eq!(EntityType::Planning.to_string(), "planning");
    }

    #[test]
    fn entity_type_as_str_matches_serde() {
        let variants = [
            EntityType::Task,
            EntityType::Change,
            EntityType::Module,
            EntityType::Wave,
            EntityType::Planning,
            EntityType::Config,
        ];
        for variant in variants {
            let serde_str = serde_json::to_string(&variant)
                .expect("serialize")
                .trim_matches('"')
                .to_string();
            assert_eq!(variant.as_str(), serde_str);
        }
    }
}