nornir 0.4.20

Companion to cargo: dependency tracking, release gating, deploy, benchmarks, and documentation assembly. Project-agnostic.
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
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
//! Iceberg writer + reader for the funnel event log.
//!
//! Every funnel mutation (`IdeaSubmitted`, `NodeAdded`, …) lands as one
//! row in the `funnel_events` Iceberg table. Each append is its own
//! Iceberg snapshot, so the table at snapshot T == the funnel state at
//! time T (free time-travel).
//!
//! The schema is wide and typed (see [`super::iceberg_schema::funnel_events`]):
//!   * `refs`, `targets`, `produced_test_runs` → `list<string>`
//!   * `produced_commits` → `list<struct{repo, sha}>`
//! Only `params_json` is a JSON string, because `NodeAdded.params` is an
//! open-ended `serde_json::Map`.
//!
//! No row-level deletes, no upserts: a "node done" is a new
//! `NodeStatusChanged` row. Folding the rows back into a `Funnel`
//! struct happens in [`super::super::funnel::state::Funnel::apply`].

use std::sync::Arc;

use anyhow::{anyhow, Context, Result};
use arrow::array::{
    Array, ArrayBuilder, ListBuilder, RecordBatch, StringArray, StringBuilder,
    StructBuilder, TimestampMicrosecondArray,
};
use arrow::datatypes::{DataType, FieldRef, Fields, Schema as ArrowSchema, TimeUnit};
use chrono::{DateTime, TimeZone, Utc};
use futures::TryStreamExt;
use iceberg::Catalog;
use iceberg::arrow::schema_to_arrow_schema;
use uuid::Uuid;

use super::iceberg::{IcebergWarehouse, TABLE_FUNNEL_EVENTS, append_batch};
use crate::funnel::event::{
    CommitRef, Event, NodeStatus, PlanStatus, RunOutcome, TriageDecision,
};
use crate::funnel::ids::{IdeaId, NodeId, PlanId, RunId};

// Column indices match the `iceberg_schema::funnel_events` field order.
const COL_EVENT_ID: usize = 0;
const COL_TS_MICROS: usize = 1;
const COL_KIND: usize = 2;
const COL_IDEA_ID: usize = 3;
const COL_PLAN_ID: usize = 4;
const COL_NODE_ID: usize = 5;
const COL_RUN_ID: usize = 6;
const COL_FROM_NODE: usize = 7;
const COL_TO_NODE: usize = 8;
const COL_SOURCE: usize = 9;
const COL_TEXT: usize = 10;
const COL_REFS: usize = 11;
const COL_DECISION: usize = 12;
const COL_NODE_STATUS: usize = 13;
const COL_PLAN_STATUS: usize = 14;
const COL_WHY: usize = 15;
const COL_SUMMARY: usize = 16;
const COL_PLANNER: usize = 17;
const COL_NODE_KIND: usize = 18;
const COL_TARGETS: usize = 19;
const COL_PROMPT_EXCERPT: usize = 20;
const COL_PARAMS_JSON: usize = 21;
const COL_RAN_BY: usize = 22;
const COL_OUTCOME: usize = 23;
const COL_LOG_REF: usize = 24;
const COL_PRODUCED_COMMITS: usize = 25;
const COL_PRODUCED_TEST_RUNS: usize = 26;

const KIND_IDEA_SUBMITTED: &str = "IdeaSubmitted";
const KIND_IDEA_TRIAGED: &str = "IdeaTriaged";
const KIND_PLAN_CREATED: &str = "PlanCreated";
const KIND_NODE_ADDED: &str = "NodeAdded";
const KIND_EDGE_ADDED: &str = "EdgeAdded";
const KIND_NODE_STATUS_CHANGED: &str = "NodeStatusChanged";
const KIND_RUN_RECORDED: &str = "RunRecorded";
const KIND_PLAN_STATUS_CHANGED: &str = "PlanStatusChanged";

/// Append one event. Each call produces a new Iceberg snapshot.
pub async fn append_event(wh: &IcebergWarehouse, event: &Event) -> Result<()> {
    let table = wh.catalog().load_table(&wh.table_ident(TABLE_FUNNEL_EVENTS)).await?;
    let batch = build_batch(&table, std::slice::from_ref(event))?;
    append_batch(wh.catalog(), table, batch).await
}

/// Load every funnel event in original write order. Used to
/// reconstruct [`crate::funnel::state::Funnel`] on startup.
pub async fn load_all_events(wh: &IcebergWarehouse) -> Result<Vec<Event>> {
    let table = wh.catalog().load_table(&wh.table_ident(TABLE_FUNNEL_EVENTS)).await?;
    let scan = table.scan().build()?;
    let stream = scan.to_arrow().await?;
    let batches: Vec<RecordBatch> = stream.try_collect().await?;

    let mut events: Vec<Event> = Vec::new();
    for batch in &batches {
        let chunk = parse_batch(batch)?;
        events.extend(chunk);
    }
    // Iceberg doesn't guarantee scan order. Sort by ts so replay is
    // deterministic.
    events.sort_by_key(|e| e.ts());
    Ok(events)
}

// ---------- write path ----------

fn build_batch(
    table: &iceberg::table::Table,
    events: &[Event],
) -> Result<RecordBatch> {
    let schema = Arc::new(schema_to_arrow_schema(table.metadata().current_schema())?);

    // Pull the iceberg-derived list element fields so our ListBuilders
    // produce arrays whose Field (name + nullability + PARQUET:field_id
    // metadata) matches the table schema exactly. Without this,
    // RecordBatch::try_new rejects the column.
    let refs_elem = list_element_field(&schema, COL_REFS)?;
    let targets_elem = list_element_field(&schema, COL_TARGETS)?;
    let test_runs_elem = list_element_field(&schema, COL_PRODUCED_TEST_RUNS)?;
    let commits_elem = list_element_field(&schema, COL_PRODUCED_COMMITS)?;
    let commit_struct_fields: Fields = match commits_elem.data_type() {
        DataType::Struct(fs) => fs.clone(),
        other => return Err(anyhow!("produced_commits element not Struct: {other:?}")),
    };

    // String column accumulators (None = null).
    let mut event_ids: Vec<String> = Vec::with_capacity(events.len());
    let mut ts_vals: Vec<i64> = Vec::with_capacity(events.len());
    let mut kinds: Vec<String> = Vec::with_capacity(events.len());
    let mut idea_ids: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut plan_ids: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut node_ids: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut run_ids: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut from_nodes: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut to_nodes: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut sources: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut texts: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut decisions: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut node_statuses: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut plan_statuses: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut whys: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut summaries: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut planners: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut node_kinds: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut prompt_excerpts: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut params_jsons: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut ran_bys: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut outcomes: Vec<Option<String>> = Vec::with_capacity(events.len());
    let mut log_refs: Vec<Option<String>> = Vec::with_capacity(events.len());

    // List<string> builders, each wearing the iceberg-derived element field.
    let mut refs_b: ListBuilder<StringBuilder> =
        ListBuilder::new(StringBuilder::new()).with_field(refs_elem);
    let mut targets_b: ListBuilder<StringBuilder> =
        ListBuilder::new(StringBuilder::new()).with_field(targets_elem);
    let mut test_runs_b: ListBuilder<StringBuilder> =
        ListBuilder::new(StringBuilder::new()).with_field(test_runs_elem);

    // List<struct{repo,sha}> builder, struct fields driven by the schema.
    let commit_struct_builder = StructBuilder::new(
        commit_struct_fields.clone(),
        vec![
            Box::new(StringBuilder::new()) as Box<dyn ArrayBuilder>,
            Box::new(StringBuilder::new()) as Box<dyn ArrayBuilder>,
        ],
    );
    let mut commits_b: ListBuilder<StructBuilder> =
        ListBuilder::new(commit_struct_builder).with_field(commits_elem);

    for ev in events {
        event_ids.push(Uuid::new_v4().to_string());
        ts_vals.push(ev.ts().timestamp_micros());
        kinds.push(event_kind_str(ev).to_string());

        // Defaults: everything null, then fill per variant.
        let mut idea_id = None;
        let mut plan_id = None;
        let mut node_id = None;
        let mut run_id = None;
        let mut from_node = None;
        let mut to_node = None;
        let mut source = None;
        let mut text = None;
        let mut decision = None;
        let mut node_status = None;
        let mut plan_status = None;
        let mut why = None;
        let mut summary = None;
        let mut planner = None;
        let mut node_kind = None;
        let mut prompt_excerpt = None;
        let mut params_json = None;
        let mut ran_by = None;
        let mut outcome = None;
        let mut log_ref = None;
        let mut refs_list: Option<&Vec<String>> = None;
        let mut targets_list: Option<&Vec<String>> = None;
        let mut test_runs_list: Option<&Vec<String>> = None;
        let mut commits_list: Option<&Vec<CommitRef>> = None;

        match ev {
            Event::IdeaSubmitted { id, source: s, text: t, refs, .. } => {
                idea_id = Some(id.as_str().to_string());
                source = Some(s.clone());
                text = Some(t.clone());
                refs_list = Some(refs);
            }
            Event::IdeaTriaged { idea_id: iid, decision: d, why: w, .. } => {
                idea_id = Some(iid.as_str().to_string());
                decision = Some(triage_str(*d).to_string());
                why = w.clone();
            }
            Event::PlanCreated { id, idea_id: iid, summary: s, planner: p, .. } => {
                plan_id = Some(id.as_str().to_string());
                idea_id = Some(iid.as_str().to_string());
                summary = Some(s.clone());
                planner = Some(p.clone());
            }
            Event::NodeAdded { plan_id: pid, node_id: nid, kind, params, targets, prompt_excerpt: pe, .. } => {
                plan_id = Some(pid.as_str().to_string());
                node_id = Some(nid.as_str().to_string());
                node_kind = Some(kind.clone());
                prompt_excerpt = pe.clone();
                targets_list = Some(targets);
                if !params.is_empty() {
                    params_json = Some(serde_json::to_string(params).unwrap_or_default());
                }
            }
            Event::EdgeAdded { plan_id: pid, from_node: f, to_node: t, .. } => {
                plan_id = Some(pid.as_str().to_string());
                from_node = Some(f.as_str().to_string());
                to_node = Some(t.as_str().to_string());
            }
            Event::NodeStatusChanged { plan_id: pid, node_id: nid, status, why: w, .. } => {
                plan_id = Some(pid.as_str().to_string());
                node_id = Some(nid.as_str().to_string());
                node_status = Some(node_status_str(*status).to_string());
                why = w.clone();
            }
            Event::RunRecorded { plan_id: pid, node_id: nid, run_id: rid, ran_by: rb, outcome: o, log_ref: lr, produced_commits, produced_test_runs, .. } => {
                plan_id = Some(pid.as_str().to_string());
                node_id = Some(nid.as_str().to_string());
                run_id = Some(rid.as_str().to_string());
                ran_by = Some(rb.clone());
                outcome = Some(run_outcome_str(*o).to_string());
                log_ref = lr.clone();
                commits_list = Some(produced_commits);
                test_runs_list = Some(produced_test_runs);
            }
            Event::PlanStatusChanged { plan_id: pid, status, why: w, .. } => {
                plan_id = Some(pid.as_str().to_string());
                plan_status = Some(plan_status_str(*status).to_string());
                why = w.clone();
            }
        }

        idea_ids.push(idea_id);
        plan_ids.push(plan_id);
        node_ids.push(node_id);
        run_ids.push(run_id);
        from_nodes.push(from_node);
        to_nodes.push(to_node);
        sources.push(source);
        texts.push(text);
        decisions.push(decision);
        node_statuses.push(node_status);
        plan_statuses.push(plan_status);
        whys.push(why);
        summaries.push(summary);
        planners.push(planner);
        node_kinds.push(node_kind);
        prompt_excerpts.push(prompt_excerpt);
        params_jsons.push(params_json);
        ran_bys.push(ran_by);
        outcomes.push(outcome);
        log_refs.push(log_ref);

        append_string_list(&mut refs_b, refs_list);
        append_string_list(&mut targets_b, targets_list);
        append_string_list(&mut test_runs_b, test_runs_list);
        append_commit_list(&mut commits_b, commits_list);
    }

    let mut cols: Vec<Arc<dyn Array>> = vec![Arc::new(StringArray::from(Vec::<String>::new())); 27];
    cols[COL_EVENT_ID] = Arc::new(StringArray::from(event_ids));
    cols[COL_TS_MICROS] = Arc::new(
        TimestampMicrosecondArray::from(ts_vals).with_timezone("+00:00"),
    );
    cols[COL_KIND] = Arc::new(StringArray::from(kinds));
    cols[COL_IDEA_ID] = Arc::new(StringArray::from(idea_ids));
    cols[COL_PLAN_ID] = Arc::new(StringArray::from(plan_ids));
    cols[COL_NODE_ID] = Arc::new(StringArray::from(node_ids));
    cols[COL_RUN_ID] = Arc::new(StringArray::from(run_ids));
    cols[COL_FROM_NODE] = Arc::new(StringArray::from(from_nodes));
    cols[COL_TO_NODE] = Arc::new(StringArray::from(to_nodes));
    cols[COL_SOURCE] = Arc::new(StringArray::from(sources));
    cols[COL_TEXT] = Arc::new(StringArray::from(texts));
    cols[COL_REFS] = Arc::new(refs_b.finish());
    cols[COL_DECISION] = Arc::new(StringArray::from(decisions));
    cols[COL_NODE_STATUS] = Arc::new(StringArray::from(node_statuses));
    cols[COL_PLAN_STATUS] = Arc::new(StringArray::from(plan_statuses));
    cols[COL_WHY] = Arc::new(StringArray::from(whys));
    cols[COL_SUMMARY] = Arc::new(StringArray::from(summaries));
    cols[COL_PLANNER] = Arc::new(StringArray::from(planners));
    cols[COL_NODE_KIND] = Arc::new(StringArray::from(node_kinds));
    cols[COL_TARGETS] = Arc::new(targets_b.finish());
    cols[COL_PROMPT_EXCERPT] = Arc::new(StringArray::from(prompt_excerpts));
    cols[COL_PARAMS_JSON] = Arc::new(StringArray::from(params_jsons));
    cols[COL_RAN_BY] = Arc::new(StringArray::from(ran_bys));
    cols[COL_OUTCOME] = Arc::new(StringArray::from(outcomes));
    cols[COL_LOG_REF] = Arc::new(StringArray::from(log_refs));
    cols[COL_PRODUCED_COMMITS] = Arc::new(commits_b.finish());
    cols[COL_PRODUCED_TEST_RUNS] = Arc::new(test_runs_b.finish());

    // Use a TimestampMicrosecondArray with timezone; ensure schema matches.
    // The schema_to_arrow_schema call above already gives us the right field.
    let _ = TimeUnit::Microsecond; // suppress unused-import warning
    Ok(RecordBatch::try_new(schema, cols)?)
}

fn append_string_list(b: &mut ListBuilder<StringBuilder>, list: Option<&Vec<String>>) {
    match list {
        None => b.append(false),
        Some(v) => {
            for s in v {
                b.values().append_value(s);
            }
            b.append(true);
        }
    }
}

/// Pull the inner element `Field` of a list column from an Arrow schema.
/// Iceberg's schema_to_arrow_schema attaches `PARQUET:field_id` metadata
/// that RecordBatch::try_new validates; using this field on the
/// ListBuilder makes our column metadata match the table exactly.
fn list_element_field(schema: &ArrowSchema, idx: usize) -> Result<FieldRef> {
    let field = schema.field(idx);
    match field.data_type() {
        DataType::List(elem) | DataType::LargeList(elem) => Ok(elem.clone()),
        other => Err(anyhow!(
            "column `{}` (idx {idx}) expected List, got {other:?}",
            field.name()
        )),
    }
}

fn append_commit_list(b: &mut ListBuilder<StructBuilder>, list: Option<&Vec<CommitRef>>) {
    match list {
        None => b.append(false),
        Some(v) => {
            let struct_b: &mut StructBuilder = b.values();
            for c in v {
                struct_b
                    .field_builder::<StringBuilder>(0)
                    .expect("repo builder")
                    .append_value(&c.repo);
                struct_b
                    .field_builder::<StringBuilder>(1)
                    .expect("sha builder")
                    .append_value(&c.sha);
                struct_b.append(true);
            }
            b.append(true);
        }
    }
}

// ---------- read path ----------

fn parse_batch(batch: &RecordBatch) -> Result<Vec<Event>> {
    let kinds = string_col(batch, COL_KIND, "kind")?;
    let ts_arr = batch
        .column(COL_TS_MICROS)
        .as_any()
        .downcast_ref::<TimestampMicrosecondArray>()
        .ok_or_else(|| anyhow!("ts_micros column not Timestamp(us)"))?;
    let idea_ids = string_opt_col(batch, COL_IDEA_ID);
    let plan_ids = string_opt_col(batch, COL_PLAN_ID);
    let node_ids = string_opt_col(batch, COL_NODE_ID);
    let run_ids = string_opt_col(batch, COL_RUN_ID);
    let from_nodes = string_opt_col(batch, COL_FROM_NODE);
    let to_nodes = string_opt_col(batch, COL_TO_NODE);
    let sources = string_opt_col(batch, COL_SOURCE);
    let texts = string_opt_col(batch, COL_TEXT);
    let decisions = string_opt_col(batch, COL_DECISION);
    let node_statuses = string_opt_col(batch, COL_NODE_STATUS);
    let plan_statuses = string_opt_col(batch, COL_PLAN_STATUS);
    let whys = string_opt_col(batch, COL_WHY);
    let summaries = string_opt_col(batch, COL_SUMMARY);
    let planners = string_opt_col(batch, COL_PLANNER);
    let node_kinds = string_opt_col(batch, COL_NODE_KIND);
    let prompt_excerpts = string_opt_col(batch, COL_PROMPT_EXCERPT);
    let params_jsons = string_opt_col(batch, COL_PARAMS_JSON);
    let ran_bys = string_opt_col(batch, COL_RAN_BY);
    let outcomes = string_opt_col(batch, COL_OUTCOME);
    let log_refs = string_opt_col(batch, COL_LOG_REF);

    let refs_lists = string_list_col(batch, COL_REFS)?;
    let targets_lists = string_list_col(batch, COL_TARGETS)?;
    let test_runs_lists = string_list_col(batch, COL_PRODUCED_TEST_RUNS)?;
    let commits_lists = commit_list_col(batch, COL_PRODUCED_COMMITS)?;

    let mut out: Vec<Event> = Vec::with_capacity(batch.num_rows());
    for i in 0..batch.num_rows() {
        let ts = micros_to_dt(ts_arr.value(i));
        let kind = kinds.value(i);
        let ev = match kind {
            KIND_IDEA_SUBMITTED => Event::IdeaSubmitted {
                id: IdeaId::new(idea_ids[i].clone().context("IdeaSubmitted needs idea_id")?),
                source: sources[i].clone().unwrap_or_default(),
                text: texts[i].clone().unwrap_or_default(),
                refs: refs_lists[i].clone().unwrap_or_default(),
                ts,
            },
            KIND_IDEA_TRIAGED => Event::IdeaTriaged {
                idea_id: IdeaId::new(idea_ids[i].clone().context("IdeaTriaged needs idea_id")?),
                decision: parse_triage(decisions[i].as_deref().unwrap_or(""))?,
                why: whys[i].clone(),
                ts,
            },
            KIND_PLAN_CREATED => Event::PlanCreated {
                id: PlanId::new(plan_ids[i].clone().context("PlanCreated needs plan_id")?),
                idea_id: IdeaId::new(idea_ids[i].clone().context("PlanCreated needs idea_id")?),
                summary: summaries[i].clone().unwrap_or_default(),
                planner: planners[i].clone().unwrap_or_default(),
                ts,
            },
            KIND_NODE_ADDED => Event::NodeAdded {
                plan_id: PlanId::new(plan_ids[i].clone().context("NodeAdded needs plan_id")?),
                node_id: NodeId::new(node_ids[i].clone().context("NodeAdded needs node_id")?),
                kind: node_kinds[i].clone().unwrap_or_default(),
                params: match params_jsons[i].as_deref() {
                    Some(s) if !s.is_empty() => serde_json::from_str(s).unwrap_or_default(),
                    _ => serde_json::Map::new(),
                },
                targets: targets_lists[i].clone().unwrap_or_default(),
                prompt_excerpt: prompt_excerpts[i].clone(),
                ts,
            },
            KIND_EDGE_ADDED => Event::EdgeAdded {
                plan_id: PlanId::new(plan_ids[i].clone().context("EdgeAdded needs plan_id")?),
                from_node: NodeId::new(from_nodes[i].clone().context("EdgeAdded needs from_node")?),
                to_node: NodeId::new(to_nodes[i].clone().context("EdgeAdded needs to_node")?),
                ts,
            },
            KIND_NODE_STATUS_CHANGED => Event::NodeStatusChanged {
                plan_id: PlanId::new(plan_ids[i].clone().context("NodeStatusChanged needs plan_id")?),
                node_id: NodeId::new(node_ids[i].clone().context("NodeStatusChanged needs node_id")?),
                status: parse_node_status(node_statuses[i].as_deref().unwrap_or(""))?,
                why: whys[i].clone(),
                ts,
            },
            KIND_RUN_RECORDED => Event::RunRecorded {
                plan_id: PlanId::new(plan_ids[i].clone().context("RunRecorded needs plan_id")?),
                node_id: NodeId::new(node_ids[i].clone().context("RunRecorded needs node_id")?),
                run_id: RunId::new(run_ids[i].clone().context("RunRecorded needs run_id")?),
                ran_by: ran_bys[i].clone().unwrap_or_default(),
                outcome: parse_run_outcome(outcomes[i].as_deref().unwrap_or(""))?,
                log_ref: log_refs[i].clone(),
                produced_commits: commits_lists[i].clone().unwrap_or_default(),
                produced_test_runs: test_runs_lists[i].clone().unwrap_or_default(),
                ts,
            },
            KIND_PLAN_STATUS_CHANGED => Event::PlanStatusChanged {
                plan_id: PlanId::new(plan_ids[i].clone().context("PlanStatusChanged needs plan_id")?),
                status: parse_plan_status(plan_statuses[i].as_deref().unwrap_or(""))?,
                why: whys[i].clone(),
                ts,
            },
            other => return Err(anyhow!("unknown funnel event kind `{other}`")),
        };
        out.push(ev);
    }
    Ok(out)
}

fn string_col<'a>(batch: &'a RecordBatch, idx: usize, name: &str) -> Result<&'a StringArray> {
    batch
        .column(idx)
        .as_any()
        .downcast_ref::<StringArray>()
        .ok_or_else(|| anyhow!("column `{name}` (idx {idx}) is not StringArray"))
}

fn string_opt_col(batch: &RecordBatch, idx: usize) -> Vec<Option<String>> {
    let arr = batch
        .column(idx)
        .as_any()
        .downcast_ref::<StringArray>()
        .expect("optional string column");
    (0..arr.len())
        .map(|i| if arr.is_null(i) { None } else { Some(arr.value(i).to_string()) })
        .collect()
}

fn string_list_col(batch: &RecordBatch, idx: usize) -> Result<Vec<Option<Vec<String>>>> {
    use arrow::array::ListArray;
    let arr = batch
        .column(idx)
        .as_any()
        .downcast_ref::<ListArray>()
        .ok_or_else(|| anyhow!("column {idx} is not ListArray"))?;
    let mut out = Vec::with_capacity(arr.len());
    for i in 0..arr.len() {
        if arr.is_null(i) {
            out.push(None);
            continue;
        }
        let values = arr.value(i);
        let strs = values
            .as_any()
            .downcast_ref::<StringArray>()
            .ok_or_else(|| anyhow!("list element not StringArray"))?;
        let mut v = Vec::with_capacity(strs.len());
        for j in 0..strs.len() {
            if !strs.is_null(j) {
                v.push(strs.value(j).to_string());
            }
        }
        out.push(Some(v));
    }
    Ok(out)
}

fn commit_list_col(batch: &RecordBatch, idx: usize) -> Result<Vec<Option<Vec<CommitRef>>>> {
    use arrow::array::{ListArray, StructArray};
    let arr = batch
        .column(idx)
        .as_any()
        .downcast_ref::<ListArray>()
        .ok_or_else(|| anyhow!("column {idx} (produced_commits) is not ListArray"))?;
    let mut out = Vec::with_capacity(arr.len());
    for i in 0..arr.len() {
        if arr.is_null(i) {
            out.push(None);
            continue;
        }
        let values = arr.value(i);
        let st = values
            .as_any()
            .downcast_ref::<StructArray>()
            .ok_or_else(|| anyhow!("produced_commits element not StructArray"))?;
        let repos = st
            .column_by_name("repo")
            .and_then(|c| c.as_any().downcast_ref::<StringArray>())
            .ok_or_else(|| anyhow!("commit struct missing repo"))?;
        let shas = st
            .column_by_name("sha")
            .and_then(|c| c.as_any().downcast_ref::<StringArray>())
            .ok_or_else(|| anyhow!("commit struct missing sha"))?;
        let mut v = Vec::with_capacity(st.len());
        for j in 0..st.len() {
            v.push(CommitRef {
                repo: repos.value(j).to_string(),
                sha: shas.value(j).to_string(),
            });
        }
        out.push(Some(v));
    }
    Ok(out)
}

// ---------- enum string helpers ----------

fn event_kind_str(ev: &Event) -> &'static str {
    match ev {
        Event::IdeaSubmitted { .. } => KIND_IDEA_SUBMITTED,
        Event::IdeaTriaged { .. } => KIND_IDEA_TRIAGED,
        Event::PlanCreated { .. } => KIND_PLAN_CREATED,
        Event::NodeAdded { .. } => KIND_NODE_ADDED,
        Event::EdgeAdded { .. } => KIND_EDGE_ADDED,
        Event::NodeStatusChanged { .. } => KIND_NODE_STATUS_CHANGED,
        Event::RunRecorded { .. } => KIND_RUN_RECORDED,
        Event::PlanStatusChanged { .. } => KIND_PLAN_STATUS_CHANGED,
    }
}

fn triage_str(d: TriageDecision) -> &'static str {
    match d {
        TriageDecision::Accept => "accept",
        TriageDecision::Drop => "drop",
    }
}

fn parse_triage(s: &str) -> Result<TriageDecision> {
    match s {
        "accept" => Ok(TriageDecision::Accept),
        "drop" => Ok(TriageDecision::Drop),
        other => Err(anyhow!("bad triage decision `{other}`")),
    }
}

fn node_status_str(s: NodeStatus) -> &'static str {
    match s {
        NodeStatus::Pending => "pending",
        NodeStatus::Ready => "ready",
        NodeStatus::InProgress => "in_progress",
        NodeStatus::Done => "done",
        NodeStatus::Blocked => "blocked",
        NodeStatus::Failed => "failed",
    }
}

fn parse_node_status(s: &str) -> Result<NodeStatus> {
    match s {
        "pending" => Ok(NodeStatus::Pending),
        "ready" => Ok(NodeStatus::Ready),
        "in_progress" => Ok(NodeStatus::InProgress),
        "done" => Ok(NodeStatus::Done),
        "blocked" => Ok(NodeStatus::Blocked),
        "failed" => Ok(NodeStatus::Failed),
        other => Err(anyhow!("bad node status `{other}`")),
    }
}

fn plan_status_str(s: PlanStatus) -> &'static str {
    match s {
        PlanStatus::Draft => "draft",
        PlanStatus::Active => "active",
        PlanStatus::Done => "done",
        PlanStatus::Abandoned => "abandoned",
    }
}

fn parse_plan_status(s: &str) -> Result<PlanStatus> {
    match s {
        "draft" => Ok(PlanStatus::Draft),
        "active" => Ok(PlanStatus::Active),
        "done" => Ok(PlanStatus::Done),
        "abandoned" => Ok(PlanStatus::Abandoned),
        other => Err(anyhow!("bad plan status `{other}`")),
    }
}

fn run_outcome_str(o: RunOutcome) -> &'static str {
    match o {
        RunOutcome::Ok => "ok",
        RunOutcome::Failed => "failed",
        RunOutcome::Aborted => "aborted",
    }
}

fn parse_run_outcome(s: &str) -> Result<RunOutcome> {
    match s {
        "ok" => Ok(RunOutcome::Ok),
        "failed" => Ok(RunOutcome::Failed),
        "aborted" => Ok(RunOutcome::Aborted),
        other => Err(anyhow!("bad run outcome `{other}`")),
    }
}

fn micros_to_dt(micros: i64) -> DateTime<Utc> {
    let secs = micros.div_euclid(1_000_000);
    let nanos = (micros.rem_euclid(1_000_000) * 1_000) as u32;
    Utc.timestamp_opt(secs, nanos).single().unwrap_or_else(Utc::now)
}

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

    #[test]
    fn roundtrip_all_event_kinds() {
        let dir = tempfile::tempdir().unwrap();
        let wh = IcebergWarehouse::open(dir.path()).unwrap();

        let t = Utc.with_ymd_and_hms(2026, 5, 31, 12, 0, 0).unwrap();
        let events = vec![
            Event::IdeaSubmitted {
                id: IdeaId::seq(1),
                source: "test".into(),
                text: "first idea".into(),
                refs: vec!["a".into(), "b".into()],
                ts: t,
            },
            Event::PlanCreated {
                id: PlanId::seq(1),
                idea_id: IdeaId::seq(1),
                summary: "the plan".into(),
                planner: "tester".into(),
                ts: t,
            },
            Event::NodeAdded {
                plan_id: PlanId::seq(1),
                node_id: NodeId::seq(1),
                kind: "rust:impl".into(),
                params: serde_json::Map::new(),
                targets: vec!["src/foo.rs".into()],
                prompt_excerpt: Some("do the thing".into()),
                ts: t,
            },
            Event::EdgeAdded {
                plan_id: PlanId::seq(1),
                from_node: NodeId::seq(1),
                to_node: NodeId::seq(2),
                ts: t,
            },
            Event::NodeStatusChanged {
                plan_id: PlanId::seq(1),
                node_id: NodeId::seq(1),
                status: NodeStatus::Done,
                why: Some("done".into()),
                ts: t,
            },
            Event::RunRecorded {
                plan_id: PlanId::seq(1),
                node_id: NodeId::seq(1),
                run_id: RunId::seq(1),
                ran_by: "tester".into(),
                outcome: RunOutcome::Ok,
                log_ref: None,
                produced_commits: vec![CommitRef { repo: "nornir".into(), sha: "abc123".into() }],
                produced_test_runs: vec!["smoke-1".into()],
                ts: t,
            },
            Event::PlanStatusChanged {
                plan_id: PlanId::seq(1),
                status: PlanStatus::Active,
                why: None,
                ts: t,
            },
        ];

        wh.block_on(async {
            for ev in &events {
                append_event(&wh, ev).await.unwrap();
            }
        });

        let loaded = wh.block_on(async { load_all_events(&wh).await.unwrap() });
        assert_eq!(loaded.len(), events.len(), "round-trip count");
        // Spot-check the IdeaSubmitted refs and RunRecorded commits made it.
        let mut saw_refs = false;
        let mut saw_commits = false;
        for ev in &loaded {
            if let Event::IdeaSubmitted { refs, .. } = ev {
                assert_eq!(refs.len(), 2);
                saw_refs = true;
            }
            if let Event::RunRecorded { produced_commits, .. } = ev {
                assert_eq!(produced_commits.len(), 1);
                assert_eq!(produced_commits[0].repo, "nornir");
                saw_commits = true;
            }
        }
        assert!(saw_refs && saw_commits, "refs and commits should round-trip");
    }
}