zagens-runtime-orchestrator 0.7.3

Runtime thread/turn persist layer for Zagens sidecar (D16 E1-b)
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
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
//! Thread/turn/item disk store and usage aggregation (R-003 A4.6).
//!
//! ## Blocking I/O policy (A1.3 audit)
//!
//! All **SQLite** and heavy JSONL writes on the HTTP/runtime path MUST run inside
//! `tokio::task::spawn_blocking` (see `append_event`, `thread_crud`, `monitor`).
//! Synchronous helpers such as [`RuntimeThreadStore::events_since`] are for
//! blocking contexts or tests only — do not call them from async handlers without
//! offloading. See [A1_PERSIST_BLOCKING_AUDIT.md](../../../docs/tech/adr/A1_PERSIST_BLOCKING_AUDIT.md).

use std::collections::{BTreeMap, HashMap};
use std::fs::{self, File, OpenOptions};
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::{Context, Result, anyhow, bail};
use chrono::{DateTime, Utc};
use rusqlite::params;
use serde::Serialize;
use serde_json::Value;
use tokio::sync::Mutex;

use crate::models::{ContentBlock, Message};

use super::prompt_inbox::PromptAdmission;
use super::types::*;
use super::{
    CURRENT_RUNTIME_SCHEMA_VERSION, StartTurnRequest, UsageAggregation, UsageBucket, UsageGroupBy,
    UsageTotals, provider_label_for_model,
};

#[derive(Debug, Clone)]
pub struct RuntimeThreadStore {
    pub threads_dir: PathBuf,
    pub turns_dir: PathBuf,
    pub items_dir: PathBuf,
    pub(crate) events_dir: PathBuf,
    pub(crate) state_path: PathBuf,
    pub(crate) state: Arc<Mutex<RuntimeStoreState>>,
    pub(crate) db: Option<Arc<std::sync::Mutex<rusqlite::Connection>>>,
}

impl RuntimeThreadStore {
    fn prepare_dirs(root: &Path) -> Result<(PathBuf, PathBuf, PathBuf, PathBuf, PathBuf)> {
        let threads_dir = root.join("threads");
        let turns_dir = root.join("turns");
        let items_dir = root.join("items");
        let events_dir = root.join("events");
        fs::create_dir_all(&threads_dir)
            .with_context(|| format!("Failed to create {}", threads_dir.display()))?;
        fs::create_dir_all(&turns_dir)
            .with_context(|| format!("Failed to create {}", turns_dir.display()))?;
        fs::create_dir_all(&items_dir)
            .with_context(|| format!("Failed to create {}", items_dir.display()))?;
        fs::create_dir_all(&events_dir)
            .with_context(|| format!("Failed to create {}", events_dir.display()))?;
        Ok((
            threads_dir,
            turns_dir,
            items_dir,
            events_dir,
            root.join("state.json"),
        ))
    }

    /// JSON-per-file store only (tests that write fixture JSON under `threads/`).
    pub fn open_json_only(root: PathBuf) -> Result<Self> {
        let (threads_dir, turns_dir, items_dir, events_dir, state_path) =
            Self::prepare_dirs(&root)?;
        let state = if state_path.exists() {
            let raw = fs::read_to_string(&state_path)
                .with_context(|| format!("Failed to read {}", state_path.display()))?;
            serde_json::from_str::<RuntimeStoreState>(&raw)
                .with_context(|| format!("Failed to parse {}", state_path.display()))?
        } else {
            let default = RuntimeStoreState::default();
            write_json_atomic(&state_path, &default)?;
            default
        };
        Ok(Self {
            threads_dir,
            turns_dir,
            items_dir,
            events_dir,
            state_path,
            state: Arc::new(Mutex::new(state)),
            db: None,
        })
    }

    pub fn open(root: PathBuf) -> Result<Self> {
        let (threads_dir, turns_dir, items_dir, events_dir, state_path) =
            Self::prepare_dirs(&root)?;

        // SQLite backend with auto-migration from existing JSON files.
        let db_path = root.join("runtime.db");
        let (state, db) =
            match crate::thread_store_sqlite::open_sqlite_thread_db(&db_path, &threads_dir) {
                Ok((conn, sqlite_state)) => {
                    eprintln!(
                        "[thread-store] SQLite backend active at {}",
                        db_path.display()
                    );
                    (sqlite_state, Some(Arc::new(std::sync::Mutex::new(conn))))
                }
                Err(e) => {
                    eprintln!(
                        "[thread-store] SQLite unavailable ({}): falling back to JSON files",
                        e
                    );
                    let state = if state_path.exists() {
                        let raw = fs::read_to_string(&state_path)
                            .with_context(|| format!("Failed to read {}", state_path.display()))?;
                        serde_json::from_str::<RuntimeStoreState>(&raw)
                            .with_context(|| format!("Failed to parse {}", state_path.display()))?
                    } else {
                        let default = RuntimeStoreState::default();
                        write_json_atomic(&state_path, &default)?;
                        default
                    };
                    (state, None)
                }
            };

        Ok(Self {
            threads_dir,
            turns_dir,
            items_dir,
            events_dir,
            state_path,
            state: Arc::new(Mutex::new(state)),
            db,
        })
    }

    fn thread_path(&self, thread_id: &str) -> PathBuf {
        self.threads_dir.join(format!("{thread_id}.json"))
    }

    fn turn_path(&self, turn_id: &str) -> PathBuf {
        self.turns_dir.join(format!("{turn_id}.json"))
    }

    fn item_path(&self, item_id: &str) -> PathBuf {
        self.items_dir.join(format!("{item_id}.json"))
    }

    fn events_path(&self, thread_id: &str) -> PathBuf {
        self.events_dir.join(format!("{thread_id}.jsonl"))
    }

    pub fn save_thread(&self, thread: &ThreadRecord) -> Result<()> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::save_thread_sqlite(&db.lock().unwrap(), thread)
                .map_err(|e| anyhow!("save_thread sqlite: {e}"));
        }
        write_json_atomic(&self.thread_path(&thread.id), thread)
    }

    pub fn save_turn(&self, turn: &TurnRecord) -> Result<()> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::save_turn_sqlite(&db.lock().unwrap(), turn)
                .map_err(|e| anyhow!("save_turn sqlite: {e}"));
        }
        write_json_atomic(&self.turn_path(&turn.id), turn)
    }

    pub fn save_item(&self, item: &TurnItemRecord) -> Result<()> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::save_item_sqlite(&db.lock().unwrap(), item)
                .map_err(|e| anyhow!("save_item sqlite: {e}"));
        }
        write_json_atomic(&self.item_path(&item.id), item)
    }

    pub fn load_thread(&self, thread_id: &str) -> Result<ThreadRecord> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::load_thread_sqlite(&db.lock().unwrap(), thread_id)
                .map_err(|e| anyhow!("{e}"));
        }
        let path = self.thread_path(thread_id);
        let raw = fs::read_to_string(&path)
            .with_context(|| format!("Failed to read thread {}", path.display()))?;
        let record: ThreadRecord = serde_json::from_str(&raw)
            .with_context(|| format!("Failed to parse thread {}", path.display()))?;
        if record.schema_version > CURRENT_RUNTIME_SCHEMA_VERSION {
            bail!(
                "Thread schema v{} is newer than supported v{}",
                record.schema_version,
                CURRENT_RUNTIME_SCHEMA_VERSION
            );
        }
        Ok(record)
    }

    pub fn load_turn(&self, turn_id: &str) -> Result<TurnRecord> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::load_turn_sqlite(&db.lock().unwrap(), turn_id)
                .map_err(|e| anyhow!("{e}"));
        }
        let path = self.turn_path(turn_id);
        let raw = fs::read_to_string(&path)
            .with_context(|| format!("Failed to read turn {}", path.display()))?;
        let record: TurnRecord = serde_json::from_str(&raw)
            .with_context(|| format!("Failed to parse turn {}", path.display()))?;
        if record.schema_version > CURRENT_RUNTIME_SCHEMA_VERSION {
            bail!(
                "Turn schema v{} is newer than supported v{}",
                record.schema_version,
                CURRENT_RUNTIME_SCHEMA_VERSION
            );
        }
        Ok(record)
    }

    pub fn load_item(&self, item_id: &str) -> Result<TurnItemRecord> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::load_item_sqlite(&db.lock().unwrap(), item_id)
                .map_err(|e| anyhow!("load_item: {e}"));
        }
        let path = self.item_path(item_id);
        let raw = fs::read_to_string(&path)
            .with_context(|| format!("Failed to read item {}", path.display()))?;
        let record: TurnItemRecord = serde_json::from_str(&raw)
            .with_context(|| format!("Failed to parse item {}", path.display()))?;
        if record.schema_version > CURRENT_RUNTIME_SCHEMA_VERSION {
            bail!(
                "Item schema v{} is newer than supported v{}",
                record.schema_version,
                CURRENT_RUNTIME_SCHEMA_VERSION
            );
        }
        Ok(record)
    }

    pub fn list_threads(&self) -> Result<Vec<ThreadRecord>> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::list_threads_sqlite(&db.lock().unwrap())
                .map_err(|e| anyhow!("list_threads: {e}"));
        }
        let mut out = Vec::new();
        for entry in fs::read_dir(&self.threads_dir)
            .with_context(|| format!("Failed to read {}", self.threads_dir.display()))?
        {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_none_or(|ext| ext != "json") {
                continue;
            }
            let raw = fs::read_to_string(&path)
                .with_context(|| format!("Failed to read {}", path.display()))?;
            let thread: ThreadRecord = serde_json::from_str(&raw)
                .with_context(|| format!("Failed to parse {}", path.display()))?;
            if thread.schema_version > CURRENT_RUNTIME_SCHEMA_VERSION {
                bail!(
                    "Thread schema v{} is newer than supported v{}",
                    thread.schema_version,
                    CURRENT_RUNTIME_SCHEMA_VERSION
                );
            }
            out.push(thread);
        }
        out.sort_by_key(|t| std::cmp::Reverse(t.updated_at));
        Ok(out)
    }

    pub fn list_turns_for_thread(&self, thread_id: &str) -> Result<Vec<TurnRecord>> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::list_turns_for_thread_sqlite(
                &db.lock().unwrap(),
                thread_id,
            )
            .map_err(|e| anyhow!("list_turns_for_thread: {e}"));
        }
        let mut out = Vec::new();
        for entry in fs::read_dir(&self.turns_dir)
            .with_context(|| format!("Failed to read {}", self.turns_dir.display()))?
        {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_none_or(|ext| ext != "json") {
                continue;
            }
            let raw = fs::read_to_string(&path)
                .with_context(|| format!("Failed to read {}", path.display()))?;
            let turn: TurnRecord = serde_json::from_str(&raw)
                .with_context(|| format!("Failed to parse {}", path.display()))?;
            if turn.schema_version > CURRENT_RUNTIME_SCHEMA_VERSION {
                bail!(
                    "Turn schema v{} is newer than supported v{}",
                    turn.schema_version,
                    CURRENT_RUNTIME_SCHEMA_VERSION
                );
            }
            if turn.thread_id == thread_id {
                out.push(turn);
            }
        }
        out.sort_by_key(|a| a.created_at);
        Ok(out)
    }

    /// One linear scan of `turns_dir` for startup recovery (avoids O(threads ×
    /// turns) when `list_turns_for_thread` is called per thread).
    pub fn list_incomplete_turns(&self) -> Result<Vec<TurnRecord>> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::list_incomplete_turns_sqlite(&db.lock().unwrap())
                .map_err(|e| anyhow!("list_incomplete_turns: {e}"));
        }
        let mut out = Vec::new();
        for entry in fs::read_dir(&self.turns_dir)
            .with_context(|| format!("Failed to read {}", self.turns_dir.display()))?
        {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_none_or(|ext| ext != "json") {
                continue;
            }
            let raw = fs::read_to_string(&path)
                .with_context(|| format!("Failed to read {}", path.display()))?;
            let turn: TurnRecord = serde_json::from_str(&raw)
                .with_context(|| format!("Failed to parse {}", path.display()))?;
            if turn.schema_version > CURRENT_RUNTIME_SCHEMA_VERSION {
                bail!(
                    "Turn schema v{} is newer than supported v{}",
                    turn.schema_version,
                    CURRENT_RUNTIME_SCHEMA_VERSION
                );
            }
            if matches!(
                turn.status,
                RuntimeTurnStatus::Queued | RuntimeTurnStatus::InProgress
            ) {
                out.push(turn);
            }
        }
        Ok(out)
    }

    pub fn allocate_session_input_seq(&self, thread_id: &str) -> Result<u64> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::allocate_session_input_seq_sqlite(
                &db.lock().unwrap(),
                thread_id,
            )
            .map_err(|e| anyhow!("allocate_session_input_seq: {e}"));
        }
        Ok(1)
    }

    pub fn admit_session_input(
        &self,
        admission: &PromptAdmission,
        request: Option<&StartTurnRequest>,
    ) -> Result<()> {
        if let Some(ref db) = self.db {
            let request_json = request.map(serde_json::to_string).transpose()?;
            return crate::thread_store_sqlite::admit_session_input_sqlite(
                &db.lock().unwrap(),
                admission,
                request_json.as_deref(),
            )
            .map_err(|e| anyhow!("admit_session_input: {e}"));
        }
        Ok(())
    }

    pub fn promote_session_input(
        &self,
        id: &str,
        promoted_seq: u64,
        turn_id: Option<&str>,
    ) -> Result<()> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::promote_session_input_sqlite(
                &db.lock().unwrap(),
                id,
                promoted_seq,
                turn_id,
            )
            .map_err(|e| anyhow!("promote_session_input: {e}"));
        }
        Ok(())
    }

    pub fn next_pending_queue(
        &self,
        thread_id: &str,
    ) -> Result<Option<(PromptAdmission, Option<StartTurnRequest>)>> {
        if let Some(ref db) = self.db {
            let pending = crate::thread_store_sqlite::next_pending_queue_sqlite(
                &db.lock().unwrap(),
                thread_id,
            )
            .map_err(|e| anyhow!("next_pending_queue: {e}"))?;
            return Ok(pending.map(|(admission, request_json)| {
                let req = request_json
                    .as_deref()
                    .and_then(|raw| serde_json::from_str(raw).ok());
                (admission, req)
            }));
        }
        Ok(None)
    }

    pub fn list_pending_session_inputs(&self, thread_id: &str) -> Result<Vec<PromptAdmission>> {
        if let Some(ref db) = self.db {
            let conn = db.lock().unwrap();
            let mut stmt = conn
                .prepare(
                    "SELECT id FROM session_input
                     WHERE thread_id = ?1 AND promoted_seq IS NULL
                     ORDER BY admitted_seq ASC",
                )
                .map_err(|e| anyhow!("list_pending_session_inputs: {e}"))?;
            let ids: Vec<String> = stmt
                .query_map(params![thread_id], |row| row.get(0))
                .map_err(|e| anyhow!("list_pending_session_inputs: {e}"))?
                .filter_map(|r| r.ok())
                .collect();
            let mut out = Vec::new();
            for id in ids {
                if let Some(admission) =
                    crate::thread_store_sqlite::find_session_input_sqlite(&conn, &id)
                        .map_err(|e| anyhow!("list_pending_session_inputs: {e}"))?
                {
                    out.push(admission);
                }
            }
            return Ok(out);
        }
        Ok(Vec::new())
    }

    pub fn list_items_for_turn(&self, turn_id: &str) -> Result<Vec<TurnItemRecord>> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::list_items_for_turn_sqlite(
                &db.lock().unwrap(),
                turn_id,
            )
            .map_err(|e| anyhow!("list_items_for_turn: {e}"));
        }
        let mut out = Vec::new();
        for entry in fs::read_dir(&self.items_dir)
            .with_context(|| format!("Failed to read {}", self.items_dir.display()))?
        {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_none_or(|ext| ext != "json") {
                continue;
            }
            let raw = fs::read_to_string(&path)
                .with_context(|| format!("Failed to read {}", path.display()))?;
            let item: TurnItemRecord = serde_json::from_str(&raw)
                .with_context(|| format!("Failed to parse {}", path.display()))?;
            if item.schema_version > CURRENT_RUNTIME_SCHEMA_VERSION {
                bail!(
                    "Item schema v{} is newer than supported v{}",
                    item.schema_version,
                    CURRENT_RUNTIME_SCHEMA_VERSION
                );
            }
            if item.turn_id == turn_id {
                out.push(item);
            }
        }
        out.sort_by(|a, b| {
            let left = a.started_at.unwrap_or_else(Utc::now);
            let right = b.started_at.unwrap_or_else(Utc::now);
            left.cmp(&right)
        });
        Ok(out)
    }

    pub async fn append_event(
        &self,
        thread_id: &str,
        turn_id: Option<&str>,
        item_id: Option<&str>,
        event: impl Into<String>,
        payload: Value,
    ) -> Result<RuntimeEventRecord> {
        let mut state = self.state.lock().await;
        let seq = state.next_seq;
        state.next_seq = state.next_seq.saturating_add(1);

        let record = RuntimeEventRecord {
            schema_version: CURRENT_RUNTIME_SCHEMA_VERSION,
            seq,
            timestamp: Utc::now(),
            thread_id: thread_id.to_string(),
            turn_id: turn_id.map(ToString::to_string),
            item_id: item_id.map(ToString::to_string),
            event: event.into(),
            payload,
        };

        if let Some(ref db) = self.db {
            let db = Arc::clone(db);
            let record_for_db = record.clone();
            drop(state);
            tokio::task::spawn_blocking(move || {
                crate::thread_store_sqlite::append_event_sqlite(
                    &db.lock().unwrap(),
                    &record_for_db,
                    seq,
                )
            })
            .await
            .map_err(|e| anyhow!("append_event join: {e}"))?
            .map_err(|e| anyhow!("append_event sqlite: {e}"))?;
            return Ok(record);
        }

        let state_path = self.state_path.clone();
        let state_snapshot = state.clone();
        let events_path = self.events_path(thread_id);
        let record_for_disk = record.clone();
        drop(state);

        tokio::task::spawn_blocking(move || {
            write_json_atomic(&state_path, &state_snapshot)?;
            append_event_jsonl_blocking(&events_path, &record_for_disk)
        })
        .await
        .map_err(|e| anyhow!("append_event join: {e}"))??;

        Ok(record)
    }

    pub fn events_since(
        &self,
        thread_id: &str,
        since_seq: Option<u64>,
    ) -> Result<Vec<RuntimeEventRecord>> {
        if let Some(ref db) = self.db {
            let since = since_seq.unwrap_or(0);
            return crate::thread_store_sqlite::events_since_sqlite(
                &db.lock().unwrap(),
                thread_id,
                since,
            )
            .map_err(|e| anyhow!("events_since: {e}"));
        }
        let path = self.events_path(thread_id);
        if !path.exists() {
            return Ok(Vec::new());
        }
        let file =
            File::open(&path).with_context(|| format!("Failed to open {}", path.display()))?;
        let reader = BufReader::new(file);
        let mut out = Vec::new();
        for line in reader.lines() {
            let line = line?;
            if line.trim().is_empty() {
                continue;
            }
            let event: RuntimeEventRecord = serde_json::from_str(&line)
                .with_context(|| format!("Failed to parse event line in {}", path.display()))?;
            if let Some(since) = since_seq
                && event.seq <= since
            {
                continue;
            }
            out.push(event);
        }
        Ok(out)
    }

    pub async fn current_seq(&self) -> u64 {
        let state = self.state.lock().await;
        state.next_seq.saturating_sub(1)
    }
}

impl RuntimeThreadStore {
    pub fn aggregate_usage_linear(
        &self,
        thread_models: &HashMap<String, String>,
        since: Option<DateTime<Utc>>,
        until: Option<DateTime<Utc>>,
        group_by: UsageGroupBy,
    ) -> Result<UsageAggregation> {
        if let Some(ref db) = self.db {
            return crate::thread_store_sqlite::aggregate_usage_linear_sqlite(
                &db.lock().unwrap(),
                since,
                until,
                group_by,
            )
            .map_err(|e| anyhow!("aggregate_usage sqlite: {e}"));
        }
        let mut buckets: BTreeMap<String, UsageBucket> = BTreeMap::new();
        let mut totals = UsageTotals::default();
        let mut cache_telemetry_incomplete = false;

        for entry in fs::read_dir(&self.turns_dir)
            .with_context(|| format!("Failed to read {}", self.turns_dir.display()))?
        {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_none_or(|ext| ext != "json") {
                continue;
            }
            let raw = fs::read_to_string(&path)
                .with_context(|| format!("Failed to read {}", path.display()))?;
            let turn: TurnRecord = serde_json::from_str(&raw)
                .with_context(|| format!("Failed to parse {}", path.display()))?;
            if turn.schema_version > CURRENT_RUNTIME_SCHEMA_VERSION {
                bail!(
                    "Turn schema v{} is newer than supported v{}",
                    turn.schema_version,
                    CURRENT_RUNTIME_SCHEMA_VERSION
                );
            }
            let Some(model) = thread_models.get(&turn.thread_id) else {
                continue;
            };
            if let Some(s) = since
                && turn.created_at < s
            {
                continue;
            }
            if let Some(u) = until
                && turn.created_at > u
            {
                continue;
            }
            let Some(usage) = turn.usage.as_ref() else {
                continue;
            };

            let key = match group_by {
                UsageGroupBy::Day => turn.created_at.format("%Y-%m-%d").to_string(),
                UsageGroupBy::Model => model.clone(),
                UsageGroupBy::Provider => provider_label_for_model(model).to_string(),
                UsageGroupBy::Thread => turn.thread_id.clone(),
            };
            let bucket = buckets.entry(key.clone()).or_insert_with(|| UsageBucket {
                key,
                ..UsageBucket::default()
            });
            crate::usage_aggregate::accumulate_turn_usage(
                &mut totals,
                bucket,
                model,
                usage,
                &mut cache_telemetry_incomplete,
            );
        }

        crate::usage_aggregate::finalize_usage_totals(&mut totals);
        for bucket in buckets.values_mut() {
            crate::usage_aggregate::finalize_usage_bucket(bucket);
        }

        let group_by_str = match group_by {
            UsageGroupBy::Day => "day",
            UsageGroupBy::Model => "model",
            UsageGroupBy::Provider => "provider",
            UsageGroupBy::Thread => "thread",
        }
        .to_string();

        Ok(UsageAggregation {
            since,
            until,
            group_by: group_by_str,
            totals,
            buckets: buckets.into_values().collect(),
            cache_telemetry_incomplete,
        })
    }
}

pub fn duration_ms(start: DateTime<Utc>, end: DateTime<Utc>) -> u64 {
    let millis = (end - start).num_milliseconds();
    if millis.is_negative() {
        0
    } else {
        u64::try_from(millis).unwrap_or(u64::MAX)
    }
}

/// Standalone version of `reconstruct_messages_from_turns` that works on a
/// cloned `RuntimeThreadStore` — so it can run inside `spawn_blocking` without
/// holding a reference to the `RuntimeThreadManager` or its `Arc<Mutex<…>>` locks.
pub fn reconstruct_messages_for_store(
    store: &RuntimeThreadStore,
    turns: &[TurnRecord],
) -> Result<Vec<Message>> {
    let mut messages = Vec::new();
    for turn in turns {
        let items = store.list_items_for_turn(&turn.id)?;
        for item in items {
            match item.kind {
                TurnItemKind::UserMessage => {
                    let text = item.detail.unwrap_or(item.summary);
                    messages.push(Message {
                        role: "user".to_string(),
                        content: vec![ContentBlock::Text {
                            text,
                            cache_control: None,
                        }],
                    });
                }
                TurnItemKind::AgentMessage => {
                    let text = item.detail.unwrap_or(item.summary);
                    messages.push(Message {
                        role: "assistant".to_string(),
                        content: vec![ContentBlock::Text {
                            text,
                            cache_control: None,
                        }],
                    });
                }
                _ => {}
            }
        }
    }
    Ok(messages)
}

fn append_event_jsonl_blocking(path: &Path, record: &RuntimeEventRecord) -> Result<()> {
    let mut file = OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .with_context(|| format!("Failed to open {}", path.display()))?;
    let line = serde_json::to_string(record)?;
    writeln!(file, "{line}").with_context(|| format!("Failed to append {}", path.display()))?;
    file.flush()
        .with_context(|| format!("Failed to flush {}", path.display()))?;
    file.sync_all()
        .with_context(|| format!("Failed to fsync {}", path.display()))?;
    Ok(())
}

pub(super) fn write_json_atomic<T: Serialize>(path: &Path, value: &T) -> Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("Failed to create directory {}", parent.display()))?;
    }
    let payload = serde_json::to_string_pretty(value)?;
    zagens_runtime_adapters::util::write_atomic(path, payload.as_bytes())
        .with_context(|| format!("Failed to write {}", path.display()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime_threads::CURRENT_EVENT_SCHEMA_VERSION;
    use crate::runtime_threads::RuntimeTurnStatus;
    use crate::runtime_threads::types::{
        RuntimeEventRecord, TurnItemKind, TurnItemLifecycleStatus, TurnItemRecord, TurnRecord,
    };
    use chrono::Utc;
    use serde_json::json;

    fn temp_store() -> (tempfile::TempDir, RuntimeThreadStore) {
        let dir = tempfile::tempdir().expect("tempdir");
        let store =
            RuntimeThreadStore::open_json_only(dir.path().to_path_buf()).expect("open store");
        (dir, store)
    }

    fn user_item(turn_id: &str, item_id: &str, text: &str) -> TurnItemRecord {
        TurnItemRecord {
            schema_version: CURRENT_EVENT_SCHEMA_VERSION,
            id: item_id.to_string(),
            turn_id: turn_id.to_string(),
            kind: TurnItemKind::UserMessage,
            status: TurnItemLifecycleStatus::Completed,
            summary: text.to_string(),
            detail: Some(text.to_string()),
            metadata: None,
            artifact_refs: Vec::new(),
            started_at: Some(Utc::now()),
            ended_at: Some(Utc::now()),
        }
    }

    fn agent_item(turn_id: &str, item_id: &str, text: &str) -> TurnItemRecord {
        TurnItemRecord {
            schema_version: CURRENT_EVENT_SCHEMA_VERSION,
            id: item_id.to_string(),
            turn_id: turn_id.to_string(),
            kind: TurnItemKind::AgentMessage,
            status: TurnItemLifecycleStatus::Completed,
            summary: text.to_string(),
            detail: Some(text.to_string()),
            metadata: None,
            artifact_refs: Vec::new(),
            started_at: Some(Utc::now()),
            ended_at: Some(Utc::now()),
        }
    }

    fn message_visible_text(message: &Message) -> String {
        message
            .content
            .iter()
            .filter_map(|block| match block {
                ContentBlock::Text { text, .. } => Some(text.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join("")
    }

    fn jsonl_item_detail(event: &RuntimeEventRecord) -> Option<String> {
        if event.event != "item.completed" {
            return None;
        }
        let kind = event.payload.get("item")?.get("kind")?.as_str()?;
        if kind != "user_message" && kind != "agent_message" {
            return None;
        }
        event
            .payload
            .get("item")?
            .get("detail")?
            .as_str()
            .map(str::to_string)
    }

    /// A1.4 — persisted turn items, JSONL `item.completed`, and `reconstruct_messages` agree.
    #[tokio::test]
    async fn reconstruct_messages_matches_jsonl_item_completed_details() {
        let (_dir, store) = temp_store();
        let thread_id = "thr_iso";
        let turn_id = "turn_iso";

        let user = user_item(turn_id, "item_user", "hello from user");
        let agent = agent_item(turn_id, "item_agent", "hello from agent");
        store.save_item(&user).expect("save user item");
        store.save_item(&agent).expect("save agent item");

        let turn = TurnRecord {
            schema_version: CURRENT_EVENT_SCHEMA_VERSION,
            id: turn_id.to_string(),
            thread_id: thread_id.to_string(),
            status: RuntimeTurnStatus::Completed,
            input_summary: "hello from user".to_string(),
            created_at: Utc::now(),
            started_at: Some(Utc::now()),
            ended_at: Some(Utc::now()),
            duration_ms: Some(1),
            usage: None,
            last_request_input_tokens: None,
            error: None,
            item_ids: vec![user.id.clone(), agent.id.clone()],
            steer_count: 0,
        };
        store.save_turn(&turn).expect("save turn");

        for item in [&user, &agent] {
            let kind = match item.kind {
                TurnItemKind::UserMessage => "user_message",
                TurnItemKind::AgentMessage => "agent_message",
                _ => "status",
            };
            store
                .append_event(
                    thread_id,
                    Some(turn_id),
                    Some(item.id.as_str()),
                    "item.completed",
                    json!({
                        "item": {
                            "id": item.id,
                            "kind": kind,
                            "detail": item.detail,
                        }
                    }),
                )
                .await
                .expect("append jsonl event");
        }

        let turns = store.list_turns_for_thread(thread_id).expect("list turns");
        let reconstructed = reconstruct_messages_for_store(&store, &turns).expect("reconstruct");
        let reconstructed_texts: Vec<String> =
            reconstructed.iter().map(message_visible_text).collect();

        let events = store.events_since(thread_id, None).expect("read jsonl");
        let jsonl_texts: Vec<String> = events.iter().filter_map(jsonl_item_detail).collect();

        assert_eq!(
            reconstructed_texts,
            vec![
                "hello from user".to_string(),
                "hello from agent".to_string()
            ]
        );
        assert_eq!(jsonl_texts, reconstructed_texts);
    }
}