omni-dev 0.28.0

A powerful Git commit message analysis and amendment toolkit
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
//! Pure reconciliation of `events.jsonl` into materialised markdown.
//!
//! The `reconcile()` function is a pure function from event log to
//! markdown + new TTL-expiry events; the `voice review` CLI wrapper
//! (in [`crate::voice::review`]) handles all I/O around it.
//!
//! Reuses [`crate::voice::events::project`] for the bulk of the
//! reconciliation invariants from #799 (sort-by-event-id,
//! last-write-wins, idempotent expiry, unknown-id drop,
//! `reflection.error` skip) and adds TTL eviction on top: any
//! non-expired non-completed item whose effective expiry has elapsed
//! gets a synthetic `item.expire { reason: ttl, reflection_id: "review" }`
//! event minted into `new_expiry_events`. Synthetic events use the
//! injected [`UlidRng`] for ids so snapshot tests can pin them.

use std::collections::HashMap;

use chrono::{DateTime, Utc};

use crate::voice::det::UlidRng;
use crate::voice::events::{
    project, DecisionId, Event, EventKind, ExpireReason, ItemClass, ItemExpire, ItemId,
    ProjectedDecision, ProjectedItem, Provenance, ReflectionId,
};
use crate::voice::render::{render_decisions_md, render_todos_md};
use crate::voice::session::TtlDefaults;
use crate::voice::EventId;

/// Markdown + new events produced by a single reconciliation pass.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReviewOutput {
    /// Contents to write to `todos.md`.
    pub todos_md: String,
    /// Contents to write to `decisions.md`.
    pub decisions_md: String,
    /// Synthetic `item.expire { reason: ttl }` events to append to
    /// `events.jsonl`. Empty when no items have aged out.
    pub new_expiry_events: Vec<Event>,
}

/// Item plus the metadata reconciliation needs but projection drops:
/// the create-event id (for sort order) and the create-event timestamp
/// (for class-default TTL computation).
#[derive(Debug, Clone)]
pub struct ReconciledItem {
    /// The projected item (text, class, priority, valid_until, …).
    pub item: ProjectedItem,
    /// Event id of the `item.create` that minted the item — used as
    /// the within-bucket sort key in `todos.md`.
    pub created_event_id: EventId,
}

/// Decision plus its create-event id.
#[derive(Debug, Clone)]
pub struct ReconciledDecision {
    /// The projected decision (text, alternatives).
    pub decision: ProjectedDecision,
    /// Event id of the `decision.record` — used for the newest-first
    /// sort in `decisions.md`.
    pub created_event_id: EventId,
}

/// Reconciles an event log into materialised markdown and synthesised
/// TTL-expiry events.
///
/// Pure: no I/O, no clock reads. `now` and `rng` are injected so
/// snapshot tests can pin them.
///
/// `defaults` supplies class-default TTLs (used when an `item.create`
/// omits `valid_until` and no `item.update` ever set it).
pub fn reconcile(
    events: &[Event],
    defaults: &TtlDefaults,
    now: DateTime<Utc>,
    rng: &mut dyn UlidRng,
) -> ReviewOutput {
    // Projection handles every #799 invariant except TTL.
    let mut state = project(events.iter().cloned());

    // Walk events once to capture per-item / per-decision creation
    // metadata that projection doesn't surface (created_ts is needed
    // for class-default TTL; created_event_id for sort order).
    let mut item_created: HashMap<ItemId, (DateTime<Utc>, EventId)> = HashMap::new();
    let mut decision_created: HashMap<DecisionId, EventId> = HashMap::new();
    let mut sorted = events.to_vec();
    sorted.sort_by_key(|e| e.event_id);
    for event in &sorted {
        match &event.kind {
            EventKind::ItemCreate(c) => {
                item_created
                    .entry(c.item_id)
                    .or_insert((event.ts, event.event_id));
            }
            EventKind::DecisionRecord(d) => {
                decision_created
                    .entry(d.decision_id)
                    .or_insert(event.event_id);
            }
            _ => {}
        }
    }

    // TTL pass — synthesise `item.expire` for each non-expired,
    // non-completed item whose effective expiry has elapsed.
    let mut new_expiry_events = Vec::new();
    for (id, item) in &mut state.items {
        if item.expired.is_some() || item.completed {
            continue;
        }
        let Some((created_ts, _)) = item_created.get(id) else {
            // Item exists in projection without a create event — only
            // possible if projection's drop-unknown invariant lets a
            // bare update through, but it doesn't. Defensive skip.
            continue;
        };
        let effective_expiry = item
            .valid_until
            .unwrap_or_else(|| *created_ts + ttl_for(&item.class, defaults));
        if effective_expiry > now {
            continue;
        }
        let event = Event {
            event_id: rng.next_ulid(),
            ts: now,
            reflection_id: ReflectionId::Review,
            provenance: Provenance {
                transcript_span: None,
                model: None,
                prompt_version: None,
            },
            kind: EventKind::ItemExpire(ItemExpire {
                item_id: *id,
                reason: ExpireReason::Ttl,
                superseded_by: None,
            }),
        };
        new_expiry_events.push(event);
        item.expired = Some(ExpireReason::Ttl);
    }

    // Render todos: non-expired, non-completed, class ∈ {Todo, Question}.
    let todos: Vec<ReconciledItem> = state
        .items
        .values()
        .filter(|i| i.expired.is_none() && !i.completed)
        .filter(|i| matches!(i.class, ItemClass::Todo | ItemClass::Question))
        .filter_map(|i| {
            item_created.get(&i.id).map(|(_, eid)| ReconciledItem {
                item: i.clone(),
                created_event_id: *eid,
            })
        })
        .collect();
    let todos_md = render_todos_md(&todos);

    // Render decisions in insertion order (which is event-id order, by
    // construction of project()); render_decisions_md applies the
    // newest-first sort.
    let decisions: Vec<ReconciledDecision> = state
        .decisions
        .iter()
        .filter_map(|d| {
            decision_created.get(&d.id).map(|eid| ReconciledDecision {
                decision: d.clone(),
                created_event_id: *eid,
            })
        })
        .collect();
    let decisions_md = render_decisions_md(&decisions);

    ReviewOutput {
        todos_md,
        decisions_md,
        new_expiry_events,
    }
}

fn ttl_for(class: &ItemClass, defaults: &TtlDefaults) -> chrono::Duration {
    let std_dur = match class {
        ItemClass::Todo => defaults.todo,
        ItemClass::Research => defaults.research,
        ItemClass::Question => defaults.question,
    };
    // `chrono::Duration::from_std` only fails on durations exceeding
    // ~292 billion years; a malformed config that hits that bound
    // collapses to zero TTL (the corrupt-input-expires-immediately
    // policy).
    chrono::Duration::from_std(std_dur).unwrap_or(chrono::Duration::zero())
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::voice::det::CountingUlidRng;
    use crate::voice::events::{
        DecisionRecord, ItemComplete, ItemCreate, ItemUpdate, Priority, TranscriptSpan,
    };
    use chrono::TimeZone;

    fn now() -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2026, 6, 1, 0, 0, 0).unwrap()
    }

    fn provenance() -> Provenance {
        Provenance {
            transcript_span: Some(TranscriptSpan {
                start_event_id: ulid::Ulid::from_parts(0, 1),
                end_event_id: ulid::Ulid::from_parts(0, 2),
            }),
            model: Some("m".into()),
            prompt_version: Some("p".into()),
        }
    }

    fn event_at(event_id: u128, ts: DateTime<Utc>, kind: EventKind) -> Event {
        Event {
            event_id: ulid::Ulid::from_parts(0, event_id),
            ts,
            reflection_id: ReflectionId::Ulid(ulid::Ulid::from_parts(0, 100)),
            provenance: provenance(),
            kind,
        }
    }

    fn id(n: u128) -> ItemId {
        ulid::Ulid::from_parts(0, n)
    }

    fn create_todo(eid: u128, item: u128, text: &str, ts: DateTime<Utc>) -> Event {
        event_at(
            eid,
            ts,
            EventKind::ItemCreate(ItemCreate {
                item_id: id(item),
                class: ItemClass::Todo,
                text: text.into(),
                priority: None,
                valid_until: None,
                tags: None,
            }),
        )
    }

    #[test]
    fn ttl_expiry_emits_synthetic_event_for_overdue_item() {
        let created_ts = now() - chrono::Duration::days(10);
        let valid_until = now() - chrono::Duration::days(1);
        let events = vec![event_at(
            1,
            created_ts,
            EventKind::ItemCreate(ItemCreate {
                item_id: id(50),
                class: ItemClass::Todo,
                text: "expired".into(),
                priority: None,
                valid_until: Some(valid_until),
                tags: None,
            }),
        )];
        let mut rng = CountingUlidRng::new();
        let out = reconcile(&events, &TtlDefaults::default(), now(), &mut rng);
        assert_eq!(out.new_expiry_events.len(), 1);
        let e = &out.new_expiry_events[0];
        assert_eq!(e.reflection_id, ReflectionId::Review);
        assert!(e.provenance.transcript_span.is_none());
        assert!(e.provenance.model.is_none());
        assert!(e.provenance.prompt_version.is_none());
        match &e.kind {
            EventKind::ItemExpire(ie) => {
                assert_eq!(ie.item_id, id(50));
                assert_eq!(ie.reason, ExpireReason::Ttl);
            }
            other => panic!("expected ItemExpire, got {other:?}"),
        }
    }

    #[test]
    fn ttl_class_default_used_when_valid_until_absent() {
        // Todo default is 7 days; create at T-8d expires, T-5d does not.
        let stale = create_todo(1, 1, "stale", now() - chrono::Duration::days(8));
        let fresh = create_todo(2, 2, "fresh", now() - chrono::Duration::days(5));
        let mut rng = CountingUlidRng::new();
        let out = reconcile(&[stale, fresh], &TtlDefaults::default(), now(), &mut rng);
        assert_eq!(out.new_expiry_events.len(), 1);
        let expired_id = match &out.new_expiry_events[0].kind {
            EventKind::ItemExpire(ie) => ie.item_id,
            _ => panic!(),
        };
        assert_eq!(expired_id, id(1));
    }

    #[test]
    fn ttl_pass_idempotent() {
        let stale = create_todo(1, 1, "stale", now() - chrono::Duration::days(8));
        let mut rng = CountingUlidRng::new();
        let first = reconcile(
            std::slice::from_ref(&stale),
            &TtlDefaults::default(),
            now(),
            &mut rng,
        );
        assert_eq!(first.new_expiry_events.len(), 1);
        // Replay: the original event + the synthesised expiry event,
        // run through reconcile again — no new expiries should fire.
        let mut combined = vec![stale];
        combined.extend(first.new_expiry_events);
        let second = reconcile(&combined, &TtlDefaults::default(), now(), &mut rng);
        assert!(second.new_expiry_events.is_empty());
    }

    #[test]
    fn omits_completed_and_already_expired_items() {
        let e_create = create_todo(1, 1, "do it", now() - chrono::Duration::days(1));
        let e_complete = event_at(
            2,
            now(),
            EventKind::ItemComplete(ItemComplete {
                item_id: id(1),
                note: None,
            }),
        );
        let mut rng = CountingUlidRng::new();
        let out = reconcile(
            &[e_create, e_complete],
            &TtlDefaults::default(),
            now(),
            &mut rng,
        );
        assert!(out.new_expiry_events.is_empty());
        assert!(out.todos_md.lines().all(|l| !l.contains("do it")));
    }

    #[test]
    fn todos_md_groups_by_priority_and_sorts() {
        let high = event_at(
            1,
            now(),
            EventKind::ItemCreate(ItemCreate {
                item_id: id(10),
                class: ItemClass::Todo,
                text: "high one".into(),
                priority: Some(Priority::High),
                valid_until: None,
                tags: None,
            }),
        );
        let normal = create_todo(2, 11, "normal one", now());
        let low = event_at(
            3,
            now(),
            EventKind::ItemCreate(ItemCreate {
                item_id: id(12),
                class: ItemClass::Todo,
                text: "low one".into(),
                priority: Some(Priority::Low),
                valid_until: None,
                tags: None,
            }),
        );
        let mut rng = CountingUlidRng::new();
        let out = reconcile(
            &[normal, low, high],
            &TtlDefaults::default(),
            now(),
            &mut rng,
        );
        let high_pos = out.todos_md.find("high one").unwrap();
        let normal_pos = out.todos_md.find("normal one").unwrap();
        let low_pos = out.todos_md.find("low one").unwrap();
        assert!(high_pos < normal_pos && normal_pos < low_pos);
    }

    #[test]
    fn decisions_md_sorts_newest_first() {
        let older = event_at(
            5,
            now(),
            EventKind::DecisionRecord(DecisionRecord {
                decision_id: id(50),
                text: "older".into(),
                alternatives: None,
            }),
        );
        let newer = event_at(
            7,
            now(),
            EventKind::DecisionRecord(DecisionRecord {
                decision_id: id(51),
                text: "newer".into(),
                alternatives: Some(vec!["alt".into()]),
            }),
        );
        let mut rng = CountingUlidRng::new();
        let out = reconcile(&[older, newer], &TtlDefaults::default(), now(), &mut rng);
        let newer_pos = out.decisions_md.find("newer").unwrap();
        let older_pos = out.decisions_md.find("older").unwrap();
        assert!(newer_pos < older_pos);
    }

    #[test]
    fn update_keeps_valid_until_authoritative_for_ttl() {
        // Create at T-10d with explicit valid_until at T-1d (expired);
        // an update at T-5d bumps valid_until to T+5d. Reconciliation
        // should pick up the update and *not* synthesise an expiry.
        let create = event_at(
            1,
            now() - chrono::Duration::days(10),
            EventKind::ItemCreate(ItemCreate {
                item_id: id(1),
                class: ItemClass::Todo,
                text: "x".into(),
                priority: None,
                valid_until: Some(now() - chrono::Duration::days(1)),
                tags: None,
            }),
        );
        let update = event_at(
            2,
            now() - chrono::Duration::days(5),
            EventKind::ItemUpdate(ItemUpdate {
                item_id: id(1),
                valid_until: Some(now() + chrono::Duration::days(5)),
                ..Default::default()
            }),
        );
        let mut rng = CountingUlidRng::new();
        let out = reconcile(&[create, update], &TtlDefaults::default(), now(), &mut rng);
        assert!(
            out.new_expiry_events.is_empty(),
            "{:?}",
            out.new_expiry_events
        );
    }
}