tandem-server 0.6.9

HTTP server for Tandem engine APIs
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
fn routine_store_index(
    routines: std::collections::HashMap<String, RoutineSpec>,
) -> std::collections::HashMap<String, RoutineSpec> {
    routines
        .into_values()
        .map(|routine| {
            let identity = RoutineIdentity::new(&routine.routine_id, &routine.tenant_context);
            (identity.storage_key(), routine)
        })
        .collect()
}

fn routine_history_index(
    history: std::collections::HashMap<String, Vec<RoutineHistoryEvent>>,
) -> std::collections::HashMap<String, Vec<RoutineHistoryEvent>> {
    let mut indexed = std::collections::HashMap::<String, Vec<RoutineHistoryEvent>>::new();
    for event in history.into_values().flatten() {
        let identity = RoutineIdentity::new(&event.routine_id, &event.tenant_context);
        indexed
            .entry(identity.storage_key())
            .or_default()
            .push(event);
    }
    indexed
}

fn normalize_routine(mut routine: RoutineSpec) -> Result<RoutineSpec, RoutineStoreError> {
    if routine.routine_id.trim().is_empty() {
        return Err(RoutineStoreError::InvalidRoutineId {
            routine_id: routine.routine_id,
        });
    }
    routine.allowed_tools = config::channels::normalize_allowed_tools(routine.allowed_tools);
    routine.output_targets = normalize_non_empty_list(routine.output_targets);
    let next_schedule_fire =
        compute_next_schedule_fire_at_ms(&routine.schedule, &routine.timezone, now_ms())
            .ok_or_else(|| RoutineStoreError::InvalidSchedule {
                detail: "invalid schedule or timezone".to_string(),
            })?;
    if matches!(
        routine.schedule,
        RoutineSchedule::IntervalSeconds { seconds: 0 }
    ) {
        return Err(RoutineStoreError::InvalidSchedule {
            detail: "interval_seconds must be > 0".to_string(),
        });
    }
    if routine.next_fire_at_ms.is_none() {
        routine.next_fire_at_ms = Some(next_schedule_fire);
    }
    Ok(routine)
}

impl AppState {
    pub async fn load_routines(&self) -> anyhow::Result<()> {
        let _operation = self.routine_persistence.lock().await;
        let Some(raw) = read_state_file_with_legacy(&self.routines_path, "routines.json").await?
        else {
            return Ok(());
        };
        match serde_json::from_str::<std::collections::HashMap<String, RoutineSpec>>(&raw) {
            Ok(parsed) => {
                *self.routines.write().await = routine_store_index(parsed);
                Ok(())
            }
            Err(primary_err) => {
                let backup_path = config::paths::sibling_backup_path(&self.routines_path);
                if backup_path.exists() {
                    let backup_raw = fs::read_to_string(&backup_path).await?;
                    if let Ok(parsed) = serde_json::from_str::<
                        std::collections::HashMap<String, RoutineSpec>,
                    >(&backup_raw)
                    {
                        *self.routines.write().await = routine_store_index(parsed);
                        return Ok(());
                    }
                }
                Err(anyhow::anyhow!(
                    "failed to parse routines store {}: {primary_err}",
                    self.routines_path.display()
                ))
            }
        }
    }

    pub async fn load_routine_history(&self) -> anyhow::Result<()> {
        if !self.routine_history_path.exists() {
            return Ok(());
        }
        let raw = fs::read_to_string(&self.routine_history_path).await?;
        let parsed = serde_json::from_str::<
            std::collections::HashMap<String, Vec<RoutineHistoryEvent>>,
        >(&raw)
        .unwrap_or_default();
        *self.routine_history.write().await = routine_history_index(parsed);
        Ok(())
    }

    pub async fn load_routine_runs(&self) -> anyhow::Result<()> {
        let Some(raw) =
            read_state_file_with_legacy(&self.routine_runs_path, "routine_runs.json").await?
        else {
            return Ok(());
        };
        let parsed =
            serde_json::from_str::<std::collections::HashMap<String, RoutineRunRecord>>(&raw)
                .unwrap_or_default();
        *self.routine_runs.write().await = parsed;
        Ok(())
    }

    async fn persist_routines_inner(&self, allow_empty_overwrite: bool) -> anyhow::Result<()> {
        if let Some(parent) = self.routines_path.parent() {
            fs::create_dir_all(parent).await?;
        }
        let (payload, is_empty) = {
            let guard = self.routines.read().await;
            (serde_json::to_string_pretty(&*guard)?, guard.is_empty())
        };
        if is_empty && !allow_empty_overwrite && self.routines_path.exists() {
            let existing_raw = fs::read_to_string(&self.routines_path)
                .await
                .unwrap_or_default();
            let existing_has_rows = serde_json::from_str::<
                std::collections::HashMap<String, RoutineSpec>,
            >(&existing_raw)
            .map(|rows| !rows.is_empty())
            .unwrap_or(true);
            if existing_has_rows {
                return Err(anyhow::anyhow!(
                    "refusing to overwrite non-empty routines store {} with empty in-memory state",
                    self.routines_path.display()
                ));
            }
        }
        let backup_path = config::paths::sibling_backup_path(&self.routines_path);
        if self.routines_path.exists() {
            let _ = fs::copy(&self.routines_path, &backup_path).await;
        }
        let tmp_path = config::paths::sibling_tmp_path(&self.routines_path);
        fs::write(&tmp_path, payload).await?;
        fs::rename(&tmp_path, &self.routines_path).await?;
        Ok(())
    }

    pub async fn persist_routines(&self) -> anyhow::Result<()> {
        let _operation = self.routine_persistence.lock().await;
        self.persist_routines_inner(false).await
    }

    pub async fn persist_routine_history(&self) -> anyhow::Result<()> {
        if let Some(parent) = self.routine_history_path.parent() {
            fs::create_dir_all(parent).await?;
        }
        let payload = serde_json::to_string_pretty(&*self.routine_history.read().await)?;
        fs::write(&self.routine_history_path, payload).await?;
        Ok(())
    }

    pub async fn persist_routine_runs(&self) -> anyhow::Result<()> {
        if let Some(parent) = self.routine_runs_path.parent() {
            fs::create_dir_all(parent).await?;
        }
        let payload = serde_json::to_string_pretty(&*self.routine_runs.read().await)?;
        fs::write(&self.routine_runs_path, payload).await?;
        Ok(())
    }

    pub async fn put_routine(
        &self,
        routine: RoutineSpec,
    ) -> Result<RoutineSpec, RoutineStoreError> {
        let routine = normalize_routine(routine)?;
        let identity = RoutineIdentity::new(&routine.routine_id, &routine.tenant_context);
        let storage_key = identity.storage_key();
        let _operation = self.routine_persistence.lock().await;
        let previous = self
            .routines
            .write()
            .await
            .insert(storage_key.clone(), routine.clone());
        if let Err(error) = self.persist_routines_inner(false).await {
            let mut rollback = self.routines.write().await;
            if let Some(previous) = previous {
                rollback.insert(storage_key, previous);
            } else {
                rollback.remove(&storage_key);
            }
            return Err(RoutineStoreError::PersistFailed {
                message: error.to_string(),
            });
        }
        Ok(routine)
    }

    pub async fn update_routine_for_tenant<F>(
        &self,
        routine_id: &str,
        tenant_context: &TenantContext,
        update: F,
    ) -> Result<Option<RoutineSpec>, RoutineStoreError>
    where
        F: FnOnce(&mut RoutineSpec),
    {
        let identity = RoutineIdentity::new(routine_id, tenant_context);
        let storage_key = identity.storage_key();
        let _operation = self.routine_persistence.lock().await;
        let Some(previous) = self.routines.read().await.get(&storage_key).cloned() else {
            return Ok(None);
        };
        let mut routine = previous.clone();
        update(&mut routine);
        routine.routine_id = previous.routine_id.clone();
        routine.tenant_context = previous.tenant_context.clone();
        let routine = normalize_routine(routine)?;
        self.routines
            .write()
            .await
            .insert(storage_key.clone(), routine.clone());
        if let Err(error) = self.persist_routines_inner(false).await {
            self.routines.write().await.insert(storage_key, previous);
            return Err(RoutineStoreError::PersistFailed {
                message: error.to_string(),
            });
        }
        Ok(Some(routine))
    }

    pub async fn list_routines(&self) -> Vec<RoutineSpec> {
        let mut rows = self
            .routines
            .read()
            .await
            .values()
            .cloned()
            .collect::<Vec<_>>();
        rows.sort_by(|a, b| {
            a.routine_id
                .cmp(&b.routine_id)
                .then_with(|| a.tenant_context.org_id.cmp(&b.tenant_context.org_id))
                .then_with(|| {
                    a.tenant_context
                        .workspace_id
                        .cmp(&b.tenant_context.workspace_id)
                })
        });
        rows
    }

    pub async fn list_routines_for_tenant(
        &self,
        tenant_context: &TenantContext,
    ) -> Vec<RoutineSpec> {
        let mut rows = self
            .routines
            .read()
            .await
            .values()
            .filter(|routine| {
                RoutineIdentity::new(&routine.routine_id, &routine.tenant_context)
                    .matches_tenant(tenant_context)
            })
            .cloned()
            .collect::<Vec<_>>();
        rows.sort_by(|a, b| a.routine_id.cmp(&b.routine_id));
        rows
    }

    pub async fn get_routine(&self, routine_id: &str) -> Option<RoutineSpec> {
        self.get_routine_for_tenant(routine_id, &TenantContext::local_implicit())
            .await
    }

    pub async fn get_routine_for_tenant(
        &self,
        routine_id: &str,
        tenant_context: &TenantContext,
    ) -> Option<RoutineSpec> {
        let identity = RoutineIdentity::new(routine_id, tenant_context);
        self.get_routine_by_identity(&identity).await
    }

    pub async fn get_routine_by_identity(&self, identity: &RoutineIdentity) -> Option<RoutineSpec> {
        self.routines
            .read()
            .await
            .get(&identity.storage_key())
            .filter(|routine| {
                RoutineIdentity::new(&routine.routine_id, &routine.tenant_context) == *identity
            })
            .cloned()
    }

    pub async fn delete_routine(
        &self,
        routine_id: &str,
    ) -> Result<Option<RoutineSpec>, RoutineStoreError> {
        self.delete_routine_for_tenant(routine_id, &TenantContext::local_implicit())
            .await
    }

    pub async fn delete_routine_for_tenant(
        &self,
        routine_id: &str,
        tenant_context: &TenantContext,
    ) -> Result<Option<RoutineSpec>, RoutineStoreError> {
        let identity = RoutineIdentity::new(routine_id, tenant_context);
        let storage_key = identity.storage_key();
        let _operation = self.routine_persistence.lock().await;
        let removed = self.routines.write().await.remove(&storage_key);
        let allow_empty_overwrite = self.routines.read().await.is_empty();
        if let Err(error) = self.persist_routines_inner(allow_empty_overwrite).await {
            if let Some(removed) = removed.clone() {
                self.routines.write().await.insert(storage_key, removed);
            }
            return Err(RoutineStoreError::PersistFailed {
                message: error.to_string(),
            });
        }
        Ok(removed)
    }

    pub async fn evaluate_routine_misfires(&self, now_ms: u64) -> Vec<RoutineTriggerPlan> {
        let _operation = self.routine_persistence.lock().await;
        let mut plans = Vec::new();
        let mut guard = self.routines.write().await;
        for routine in guard.values_mut() {
            if routine.status != RoutineStatus::Active {
                continue;
            }
            let Some(next_fire_at_ms) = routine.next_fire_at_ms else {
                continue;
            };
            if now_ms < next_fire_at_ms {
                continue;
            }
            let (run_count, next_fire_at_ms) = compute_misfire_plan_for_schedule(
                now_ms,
                next_fire_at_ms,
                &routine.schedule,
                &routine.timezone,
                &routine.misfire_policy,
            );
            routine.next_fire_at_ms = Some(next_fire_at_ms);
            if run_count == 0 {
                continue;
            }
            plans.push(RoutineTriggerPlan {
                identity: RoutineIdentity::new(&routine.routine_id, &routine.tenant_context),
                tenant_context: routine.tenant_context.clone(),
                run_count,
                scheduled_at_ms: now_ms,
                next_fire_at_ms,
            });
        }
        drop(guard);
        let _ = self.persist_routines_inner(false).await;
        plans
    }

    pub async fn mark_routine_fired(
        &self,
        routine_id: &str,
        fired_at_ms: u64,
    ) -> Option<RoutineSpec> {
        self.mark_routine_fired_for_tenant(
            routine_id,
            &TenantContext::local_implicit(),
            fired_at_ms,
        )
        .await
        .ok()
        .flatten()
    }

    pub async fn mark_routine_fired_for_tenant(
        &self,
        routine_id: &str,
        tenant_context: &TenantContext,
        fired_at_ms: u64,
    ) -> Result<Option<RoutineSpec>, RoutineStoreError> {
        let identity = RoutineIdentity::new(routine_id, tenant_context);
        self.mark_routine_fired_by_identity(&identity, fired_at_ms)
            .await
    }

    pub async fn mark_routine_fired_by_identity(
        &self,
        identity: &RoutineIdentity,
        fired_at_ms: u64,
    ) -> Result<Option<RoutineSpec>, RoutineStoreError> {
        let storage_key = identity.storage_key();
        let _operation = self.routine_persistence.lock().await;
        let mut guard = self.routines.write().await;
        let Some(routine) = guard.get_mut(&storage_key) else {
            return Ok(None);
        };
        let previous = routine.clone();
        routine.last_fired_at_ms = Some(fired_at_ms);
        let updated = routine.clone();
        drop(guard);
        if let Err(error) = self.persist_routines_inner(false).await {
            self.routines.write().await.insert(storage_key, previous);
            return Err(RoutineStoreError::PersistFailed {
                message: error.to_string(),
            });
        }
        Ok(Some(updated))
    }

    pub async fn append_routine_history(&self, event: RoutineHistoryEvent) {
        let identity = RoutineIdentity::new(&event.routine_id, &event.tenant_context);
        self.routine_history
            .write()
            .await
            .entry(identity.storage_key())
            .or_default()
            .push(event);
        let _ = self.persist_routine_history().await;
    }

    pub async fn list_routine_history_for_tenant(
        &self,
        routine_id: &str,
        tenant_context: &TenantContext,
        limit: usize,
    ) -> Vec<RoutineHistoryEvent> {
        let identity = RoutineIdentity::new(routine_id, tenant_context);
        let mut rows = self
            .routine_history
            .read()
            .await
            .get(&identity.storage_key())
            .cloned()
            .unwrap_or_default();
        rows.sort_by(|a, b| b.fired_at_ms.cmp(&a.fired_at_ms));
        rows.truncate(limit.clamp(1, 500));
        rows
    }

    pub async fn create_routine_run(
        &self,
        routine: &RoutineSpec,
        trigger_type: &str,
        run_count: u32,
        status: RoutineRunStatus,
        detail: Option<String>,
    ) -> RoutineRunRecord {
        let now = now_ms();
        let record = RoutineRunRecord {
            run_id: format!("routine-run-{}", uuid::Uuid::new_v4()),
            routine_id: routine.routine_id.clone(),
            tenant_context: routine.tenant_context.clone(),
            trigger_type: trigger_type.to_string(),
            run_count,
            status,
            created_at_ms: now,
            updated_at_ms: now,
            fired_at_ms: Some(now),
            started_at_ms: None,
            finished_at_ms: None,
            requires_approval: routine.requires_approval,
            approval_reason: None,
            denial_reason: None,
            paused_reason: None,
            detail,
            entrypoint: routine.entrypoint.clone(),
            args: routine.args.clone(),
            allowed_tools: routine.allowed_tools.clone(),
            output_targets: routine.output_targets.clone(),
            artifacts: Vec::new(),
            active_session_ids: Vec::new(),
            latest_session_id: None,
            prompt_tokens: 0,
            completion_tokens: 0,
            total_tokens: 0,
            estimated_cost_usd: 0.0,
        };
        self.routine_runs
            .write()
            .await
            .insert(record.run_id.clone(), record.clone());
        let _ = self.persist_routine_runs().await;
        record
    }

    pub async fn get_routine_run(&self, run_id: &str) -> Option<RoutineRunRecord> {
        self.routine_runs.read().await.get(run_id).cloned()
    }

    pub async fn get_routine_run_for_tenant(
        &self,
        run_id: &str,
        tenant_context: &TenantContext,
    ) -> Option<RoutineRunRecord> {
        self.routine_runs
            .read()
            .await
            .get(run_id)
            .filter(|run| {
                RoutineIdentity::new(&run.routine_id, &run.tenant_context)
                    .matches_tenant(tenant_context)
            })
            .cloned()
    }

    pub async fn list_routine_runs(
        &self,
        routine_id: Option<&str>,
        limit: usize,
    ) -> Vec<RoutineRunRecord> {
        self.list_routine_runs_matching(routine_id, None, limit)
            .await
    }

    pub async fn list_routine_runs_for_tenant(
        &self,
        routine_id: Option<&str>,
        tenant_context: &TenantContext,
        limit: usize,
    ) -> Vec<RoutineRunRecord> {
        self.list_routine_runs_matching(routine_id, Some(tenant_context), limit)
            .await
    }

    async fn list_routine_runs_matching(
        &self,
        routine_id: Option<&str>,
        tenant_context: Option<&TenantContext>,
        limit: usize,
    ) -> Vec<RoutineRunRecord> {
        let mut rows = self
            .routine_runs
            .read()
            .await
            .values()
            .filter(|row| routine_id.is_none_or(|id| row.routine_id == id))
            .filter(|row| {
                tenant_context.is_none_or(|tenant| {
                    RoutineIdentity::new(&row.routine_id, &row.tenant_context)
                        .matches_tenant(tenant)
                })
            })
            .cloned()
            .collect::<Vec<_>>();
        rows.sort_by(|a, b| b.created_at_ms.cmp(&a.created_at_ms));
        rows.truncate(limit.clamp(1, 500));
        rows
    }

    pub async fn claim_next_queued_routine_run(&self) -> Option<RoutineRunRecord> {
        let mut guard = self.routine_runs.write().await;
        let next_run_id = guard
            .values()
            .filter(|row| row.status == RoutineRunStatus::Queued)
            .min_by(|a, b| {
                a.created_at_ms
                    .cmp(&b.created_at_ms)
                    .then_with(|| a.run_id.cmp(&b.run_id))
            })
            .map(|row| row.run_id.clone())?;
        let now = now_ms();
        let row = guard.get_mut(&next_run_id)?;
        row.status = RoutineRunStatus::Running;
        row.updated_at_ms = now;
        row.started_at_ms = Some(now);
        let claimed = row.clone();
        drop(guard);
        let _ = self.persist_routine_runs().await;
        Some(claimed)
    }

    pub async fn set_routine_session_policy(
        &self,
        session_id: String,
        run_id: String,
        routine_id: String,
        tenant_context: TenantContext,
        allowed_tools: Vec<String>,
    ) {
        let policy = RoutineSessionPolicy {
            session_id: session_id.clone(),
            run_id,
            routine_id,
            tenant_context,
            allowed_tools: config::channels::normalize_allowed_tools(allowed_tools),
        };
        self.routine_session_policies
            .write()
            .await
            .insert(session_id, policy);
    }

    pub async fn routine_session_policy(&self, session_id: &str) -> Option<RoutineSessionPolicy> {
        self.routine_session_policies
            .read()
            .await
            .get(session_id)
            .cloned()
    }

    pub async fn clear_routine_session_policy(&self, session_id: &str) {
        self.routine_session_policies
            .write()
            .await
            .remove(session_id);
    }

    pub async fn update_routine_run_status(
        &self,
        run_id: &str,
        status: RoutineRunStatus,
        reason: Option<String>,
    ) -> Option<RoutineRunRecord> {
        self.update_routine_run_status_matching(run_id, None, status, reason)
            .await
    }

    pub async fn update_routine_run_status_for_tenant(
        &self,
        run_id: &str,
        tenant_context: &TenantContext,
        status: RoutineRunStatus,
        reason: Option<String>,
    ) -> Option<RoutineRunRecord> {
        self.update_routine_run_status_matching(run_id, Some(tenant_context), status, reason)
            .await
    }

    async fn update_routine_run_status_matching(
        &self,
        run_id: &str,
        tenant_context: Option<&TenantContext>,
        status: RoutineRunStatus,
        reason: Option<String>,
    ) -> Option<RoutineRunRecord> {
        let mut guard = self.routine_runs.write().await;
        let row = guard.get_mut(run_id)?;
        if tenant_context.is_some_and(|tenant| {
            !RoutineIdentity::new(&row.routine_id, &row.tenant_context).matches_tenant(tenant)
        }) {
            return None;
        }
        row.status = status.clone();
        row.updated_at_ms = now_ms();
        match status {
            RoutineRunStatus::PendingApproval => row.approval_reason = reason,
            RoutineRunStatus::Running => {
                row.started_at_ms.get_or_insert_with(now_ms);
                if let Some(detail) = reason {
                    row.detail = Some(detail);
                }
            }
            RoutineRunStatus::Denied => row.denial_reason = reason,
            RoutineRunStatus::Paused => row.paused_reason = reason,
            RoutineRunStatus::Completed
            | RoutineRunStatus::Failed
            | RoutineRunStatus::Cancelled => {
                row.finished_at_ms = Some(now_ms());
                if let Some(detail) = reason {
                    row.detail = Some(detail);
                }
            }
            _ => {
                if let Some(detail) = reason {
                    row.detail = Some(detail);
                }
            }
        }
        let updated = row.clone();
        drop(guard);
        let _ = self.persist_routine_runs().await;
        Some(updated)
    }

    pub async fn append_routine_run_artifact(
        &self,
        run_id: &str,
        artifact: RoutineRunArtifact,
    ) -> Option<RoutineRunRecord> {
        self.append_routine_run_artifact_matching(run_id, None, artifact)
            .await
    }

    pub async fn append_routine_run_artifact_for_tenant(
        &self,
        run_id: &str,
        tenant_context: &TenantContext,
        artifact: RoutineRunArtifact,
    ) -> Option<RoutineRunRecord> {
        self.append_routine_run_artifact_matching(run_id, Some(tenant_context), artifact)
            .await
    }

    async fn append_routine_run_artifact_matching(
        &self,
        run_id: &str,
        tenant_context: Option<&TenantContext>,
        artifact: RoutineRunArtifact,
    ) -> Option<RoutineRunRecord> {
        let mut guard = self.routine_runs.write().await;
        let row = guard.get_mut(run_id)?;
        if tenant_context.is_some_and(|tenant| {
            !RoutineIdentity::new(&row.routine_id, &row.tenant_context).matches_tenant(tenant)
        }) {
            return None;
        }
        row.updated_at_ms = now_ms();
        row.artifacts.push(artifact);
        let updated = row.clone();
        drop(guard);
        let _ = self.persist_routine_runs().await;
        Some(updated)
    }

    pub async fn add_active_session_id(
        &self,
        run_id: &str,
        session_id: String,
    ) -> Option<RoutineRunRecord> {
        let mut guard = self.routine_runs.write().await;
        let row = guard.get_mut(run_id)?;
        if !row.active_session_ids.iter().any(|id| id == &session_id) {
            row.active_session_ids.push(session_id);
        }
        row.latest_session_id = row.active_session_ids.last().cloned();
        row.updated_at_ms = now_ms();
        let updated = row.clone();
        drop(guard);
        let _ = self.persist_routine_runs().await;
        Some(updated)
    }

    pub async fn clear_active_session_id(
        &self,
        run_id: &str,
        session_id: &str,
    ) -> Option<RoutineRunRecord> {
        let mut guard = self.routine_runs.write().await;
        let row = guard.get_mut(run_id)?;
        row.active_session_ids.retain(|id| id != session_id);
        row.updated_at_ms = now_ms();
        let updated = row.clone();
        drop(guard);
        let _ = self.persist_routine_runs().await;
        Some(updated)
    }
}