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
//! Config key-value, per-role model overrides, and per-model routing rules
//! stored in `config.db`.
//!
//! Three tables:
//! - `config_kv` — generic key-value string pairs for runtime configuration.
//! - `config_role` — per-role model and reasoning_effort overrides.
//! - `config_model_routing` — per-model provider order and fallback settings.
use crate::config::{ModelRouting, RoleConfig};
use crate::turso::{self};
use anyhow::Result;
crate::define_store! {
/// Global config store.
pub static CONFIG_STORE: ConfigStore,
db_name = "config",
schema = SCHEMA,
expect = "CONFIG_STORE not initialized — call init_global() first",
}
const SCHEMA: &str = "\
CREATE TABLE IF NOT EXISTS config_kv (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS config_role (
role TEXT PRIMARY KEY,
model TEXT,
reasoning_effort TEXT
);
CREATE TABLE IF NOT EXISTS config_model_routing (
model TEXT PRIMARY KEY,
provider_order TEXT,
allow_fallbacks INTEGER
);";
// ── Column index constants ──────────────────────────────────
// config_kv table (2-column SELECT: key, value)
crate::columns! {
KV_COLUMNS [KV] {
KEY => "key",
VALUE => "value",
}
}
// config_role table (3-column SELECT: role, model, reasoning_effort)
crate::columns! {
ROLE_CONFIG_COLUMNS [RC] {
ROLE => "role",
MODEL => "model",
REASONING_EFFORT => "reasoning_effort",
}
}
// config_model_routing table (3-column SELECT: model, provider_order, allow_fallbacks)
crate::columns! {
MODEL_ROUTING_COLUMNS [MR] {
MODEL => "model",
PROVIDER_ORDER => "provider_order",
ALLOW_FALLBACKS => "allow_fallbacks",
}
}
// ── Shared row-parsing helpers ──────────────────────────────────
/// Parse a `RoleConfig` from a `config_role` row.
fn role_config_from_row(row: &turso::Row) -> Result<RoleConfig, ::turso::Error> {
let role = row.get::<String>(COL_RC_ROLE)?;
let model = row.get::<Option<String>>(COL_RC_MODEL)?;
let reasoning_effort = row.get::<Option<String>>(COL_RC_REASONING_EFFORT)?;
Ok(RoleConfig {
role,
model,
reasoning_effort,
})
}
/// Parse a `ModelRouting` from a `config_model_routing` row.
fn model_routing_from_row(row: &turso::Row) -> Result<ModelRouting, ::turso::Error> {
let model = row.get::<String>(COL_MR_MODEL)?;
let provider_order = row.get::<Option<String>>(COL_MR_PROVIDER_ORDER)?;
let allow_fallbacks = row.get::<Option<bool>>(COL_MR_ALLOW_FALLBACKS)?;
Ok(ModelRouting {
model,
provider_order,
allow_fallbacks,
})
}
/// Parse a `(key, value)` pair from a `config_kv` row.
fn kv_from_row(row: &turso::Row) -> Result<(String, String), ::turso::Error> {
let key = row.get::<String>(COL_KV_KEY)?;
let value = row.get::<String>(COL_KV_VALUE)?;
Ok((key, value))
}
// ── UPSERT SQL constants ──────────────────────────────────
const DELETE_KV_SQL: &str = "DELETE FROM config_kv WHERE key = ?1";
impl ConfigStore {
// ── config_kv ────────────────────────────────────────────
/// Upsert a key-value pair.
pub async fn set_kv(&self, key: &str, value: &str) -> Result<()> {
let sql = format!(
"INSERT INTO config_kv ({KV_COLUMNS}) VALUES (?1, ?2) \
ON CONFLICT(key) DO UPDATE SET value = excluded.value"
);
self.conn.execute(&sql, turso::params![key, value]).await?;
Ok(())
}
/// Delete a key-value pair. Succeeds even if the key does not exist.
pub async fn delete_kv(&self, key: &str) -> Result<()> {
self.conn
.execute(DELETE_KV_SQL, turso::params![key])
.await?;
Ok(())
}
/// Get all key-value pairs.
pub async fn get_all_kv(&self) -> Result<Vec<(String, String)>> {
self.get_all_rows(KV_COLUMNS, "config_kv", "key", kv_from_row)
.await
}
// ── config_role ──────────────────────────────────────────
/// Get all role config rows.
pub async fn get_all_role_configs(&self) -> Result<Vec<RoleConfig>> {
self.get_all_rows(
ROLE_CONFIG_COLUMNS,
"config_role",
"role",
role_config_from_row,
)
.await
}
// ── config_model_routing ──────────────────────────────────
/// Get all model routing rows.
pub async fn get_all_model_routings(&self) -> Result<Vec<ModelRouting>> {
self.get_all_rows(
MODEL_ROUTING_COLUMNS,
"config_model_routing",
"model",
model_routing_from_row,
)
.await
}
// ── batch save (role configs + model routings) ──────────────
/// Atomically replace all role configs and model routings in a single
/// transaction.
///
/// Old rows are deleted with a blanket `DELETE` (no per-role iteration),
/// which is both simpler and prevents stale rows from surviving when a role
/// is removed from the enum. The enclosing transaction guarantees that a
/// crash or error before commit rolls back to the prior state — partial
/// writes from the two tables are never visible.
pub async fn save_role_and_routing_configs(
&self,
role_configs: &[RoleConfig],
model_routings: &[ModelRouting],
) -> Result<()> {
let tx = self.conn.begin_tx().await?;
Self::save_role_and_routing_configs_tx(&tx, role_configs, model_routings).await?;
tx.commit().await?;
Ok(())
}
/// Same as [`save_role_and_routing_configs`] but operates on an existing
/// transaction provided by the caller. The caller is responsible for
/// calling `commit()` or `rollback()` on the [`turso::TxGuard`].
///
/// # Deadlock safety
///
/// This method does NOT call `begin_tx()` or `commit()` internally —
/// it uses the supplied `tx` directly, making it safe to call from within
/// an outer transaction without deadlocking on the connection mutex.
pub(crate) async fn save_role_and_routing_configs_tx(
tx: &turso::TxGuard<'_>,
role_configs: &[RoleConfig],
model_routings: &[ModelRouting],
) -> Result<()> {
tx.execute("DELETE FROM config_role", turso::params![])
.await?;
let insert_role_sql =
format!("INSERT INTO config_role ({ROLE_CONFIG_COLUMNS}) VALUES (?1, ?2, ?3)");
for rc in role_configs {
tx.execute(
&insert_role_sql,
turso::params![
rc.role.as_str(),
rc.model.as_deref(),
rc.reasoning_effort.as_deref()
],
)
.await?;
}
tx.execute("DELETE FROM config_model_routing", turso::params![])
.await?;
let insert_routing_sql = format!(
"INSERT INTO config_model_routing ({MODEL_ROUTING_COLUMNS}) VALUES (?1, ?2, ?3)"
);
for mr in model_routings {
let allow_int = mr.allow_fallbacks.map(i32::from);
tx.execute(
&insert_routing_sql,
turso::params![mr.model.as_str(), mr.provider_order.as_deref(), allow_int],
)
.await?;
}
Ok(())
}
// ── config_kv — tx-aware variants ─────────────────────────
/// Upsert a key-value pair within an existing transaction.
/// Like [`set_kv`] but executes on the supplied [`turso::TxGuard`].
pub(crate) async fn set_kv_tx(tx: &turso::TxGuard<'_>, key: &str, value: &str) -> Result<()> {
let sql = format!(
"INSERT INTO config_kv ({KV_COLUMNS}) VALUES (?1, ?2) \
ON CONFLICT(key) DO UPDATE SET value = excluded.value"
);
tx.execute(&sql, turso::params![key, value]).await?;
Ok(())
}
/// Delete a key-value pair within an existing transaction.
/// Like [`delete_kv`] but executes on the supplied [`turso::TxGuard`].
/// Succeeds even if the key does not exist.
pub(crate) async fn delete_kv_tx(tx: &turso::TxGuard<'_>, key: &str) -> Result<()> {
tx.execute(DELETE_KV_SQL, turso::params![key]).await?;
Ok(())
}
/// Execute a read-only query with a row mapper, collecting all results into
/// a `Vec`. Shared implementation for all `get_all_*` methods.
///
/// # Correctness
///
/// `columns`, `table`, and `order_by` are always compile-time string
/// literals supplied by the caller; they are never user-provided, so the
/// `format!` injection is benign.
async fn get_all_rows<T, E>(
&self,
columns: &str,
table: &str,
order_by: &str,
parser: impl FnMut(&turso::Row) -> std::result::Result<T, E> + Send + 'static,
) -> Result<Vec<T>>
where
T: Send + 'static,
E: std::fmt::Display + Send + Sync + 'static,
{
let sql = format!("SELECT {columns} FROM {table} ORDER BY {order_by}");
let rows = self.conn.query_map(&sql, turso::params![], parser).await?;
Ok(rows
.into_iter()
.collect::<std::result::Result<Vec<_>, _>>()?)
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
async fn setup() -> (ConfigStore, TempDir) {
crate::open_test_store!(ConfigStore, "config")
}
// ── Parameterised lifecycle tests (role + routing) ─────────
//
// Both config_role and config_model_routing share the same 6-step lifecycle
// against save_role_and_routing_configs. A macro eliminates the structural
// duplication while keeping the per-type data fully explicit.
//
// Each step tuple is (roles, routings, expected, message):
// roles/routings — data passed to save_role_and_routing_configs
// expected — value that get_all_role_configs / get_all_model_routings
// must return after the save
// message — assertion label for the step
macro_rules! lifecycle_test {
(
$name:ident,
$getter:ident,
[$((
$roles:expr,
$routings:expr,
$expected:expr,
$msg:expr
)),+ $(,)?]
) => {
#[tokio::test]
#[allow(clippy::too_many_lines)]
async fn $name() {
let (store, _dir) = setup().await;
// 1. empty state
let all = store.$getter().await.unwrap();
assert!(
all.is_empty(),
"get_all should return empty vec for empty table"
);
$(
let roles: Vec<RoleConfig> = $roles;
let routings: Vec<ModelRouting> = $routings;
let expected = $expected;
store
.save_role_and_routing_configs(&roles, &routings)
.await
.unwrap();
let all = store.$getter().await.unwrap();
assert_eq!(all, expected, $msg);
)+
}
};
}
lifecycle_test!(
test_config_role_lifecycle,
get_all_role_configs,
[
(
vec![RoleConfig {
role: "engineer".into(),
model: Some("gpt-4".into()),
reasoning_effort: Some("high".into()),
}],
vec![],
vec![RoleConfig {
role: "engineer".into(),
model: Some("gpt-4".into()),
reasoning_effort: Some("high".into()),
}],
"save should persist item"
),
(
vec![RoleConfig {
role: "engineer".into(),
model: Some("gpt-5".into()),
reasoning_effort: None,
}],
vec![],
vec![RoleConfig {
role: "engineer".into(),
model: Some("gpt-5".into()),
reasoning_effort: None,
}],
"save with None nullable should persist NULL"
),
(
vec![RoleConfig {
role: "engineer".into(),
model: Some("claude-4".into()),
reasoning_effort: Some("low".into()),
}],
vec![],
vec![RoleConfig {
role: "engineer".into(),
model: Some("claude-4".into()),
reasoning_effort: Some("low".into()),
}],
"save should fully replace existing row"
),
(
vec![
RoleConfig {
role: "engineer".into(),
model: Some("claude-4".into()),
reasoning_effort: Some("low".into()),
},
RoleConfig {
role: "reviewer".into(),
model: Some("o1".into()),
reasoning_effort: None,
},
],
vec![],
vec![
RoleConfig {
role: "engineer".into(),
model: Some("claude-4".into()),
reasoning_effort: Some("low".into()),
},
RoleConfig {
role: "reviewer".into(),
model: Some("o1".into()),
reasoning_effort: None,
},
],
"get_all should return all rows sorted by key"
),
(
vec![RoleConfig {
role: "reviewer".into(),
model: Some("o1".into()),
reasoning_effort: None,
}],
vec![],
vec![RoleConfig {
role: "reviewer".into(),
model: Some("o1".into()),
reasoning_effort: None,
},],
"only the saved item should remain after replacement"
),
]
);
lifecycle_test!(
test_config_model_routing_lifecycle,
get_all_model_routings,
[
(
vec![],
vec![ModelRouting {
model: "gpt-4".into(),
provider_order: Some("OpenAI".into()),
allow_fallbacks: Some(true),
}],
vec![ModelRouting {
model: "gpt-4".into(),
provider_order: Some("OpenAI".into()),
allow_fallbacks: Some(true),
}],
"save should persist item"
),
(
vec![],
vec![ModelRouting {
model: "gpt-4".into(),
provider_order: Some("Azure".into()),
allow_fallbacks: None,
}],
vec![ModelRouting {
model: "gpt-4".into(),
provider_order: Some("Azure".into()),
allow_fallbacks: None,
}],
"save with None nullable should persist NULL"
),
(
vec![],
vec![ModelRouting {
model: "gpt-4".into(),
provider_order: Some("OpenRouter".into()),
allow_fallbacks: Some(false),
}],
vec![ModelRouting {
model: "gpt-4".into(),
provider_order: Some("OpenRouter".into()),
allow_fallbacks: Some(false),
}],
"save should fully replace existing row"
),
(
vec![],
vec![
ModelRouting {
model: "claude-3".into(),
provider_order: None,
allow_fallbacks: Some(true),
},
ModelRouting {
model: "gpt-4".into(),
provider_order: Some("OpenRouter".into()),
allow_fallbacks: Some(false),
},
],
vec![
ModelRouting {
model: "claude-3".into(),
provider_order: None,
allow_fallbacks: Some(true),
},
ModelRouting {
model: "gpt-4".into(),
provider_order: Some("OpenRouter".into()),
allow_fallbacks: Some(false),
},
],
"get_all should return all rows sorted by key"
),
(
vec![],
vec![ModelRouting {
model: "claude-3".into(),
provider_order: None,
allow_fallbacks: Some(true),
}],
vec![ModelRouting {
model: "claude-3".into(),
provider_order: None,
allow_fallbacks: Some(true),
},],
"only the saved item should remain after replacement"
),
]
);
// ── config_kv lifecycle ──────────────────────────────────
//
// KV storage uses the production set_kv/delete_kv/get_all_kv path.
// For individual value lookups we use an inline get_kv helper (defined below).
#[tokio::test]
async fn test_config_kv_lifecycle() {
// Inline helper for single-key lookup.
async fn get_kv(store: &ConfigStore, key: &str) -> Result<Option<String>> {
store
.conn
.query_optional(
"SELECT value FROM config_kv WHERE key = ?1",
::turso::params![key],
|row| row.get::<String>(0),
)
.await
}
let (store, _dir) = setup().await;
// 1. empty state
let val = get_kv(&store, "nonexistent").await.unwrap();
assert!(val.is_none(), "get_kv should return None for missing key");
// 2. insert
store.set_kv("alpha", "first").await.unwrap();
let val = get_kv(&store, "alpha").await.unwrap();
assert_eq!(
val,
Some("first".to_string()),
"get_kv should return inserted value"
);
// 3. overwrite
store.set_kv("alpha", "updated").await.unwrap();
let val = get_kv(&store, "alpha").await.unwrap();
assert_eq!(
val,
Some("updated".to_string()),
"set_kv should overwrite existing key"
);
// 4. get_all with multiple items (sorted by key)
store.set_kv("beta", "second").await.unwrap();
let all = store.get_all_kv().await.unwrap();
assert_eq!(
all,
vec![
("alpha".to_string(), "updated".to_string()),
("beta".to_string(), "second".to_string()),
],
"get_all_kv should return all pairs sorted by key"
);
// 5. delete
store.delete_kv("alpha").await.unwrap();
let val = get_kv(&store, "alpha").await.unwrap();
assert!(val.is_none(), "get_kv should return None after delete");
// 6. delete non-existent key (no-op, must not error)
store.delete_kv("never-existed").await.unwrap();
// 7. delete already-deleted key (also a no-op)
store.delete_kv("alpha").await.unwrap();
// 8. remaining item still present
let all = store.get_all_kv().await.unwrap();
assert_eq!(
all,
vec![("beta".to_string(), "second".to_string())],
"only the undeleted item should remain"
);
}
// ── save_role_and_routing_configs ──────────────────────────
/// Save role configs and model routings, then verify the saved data
/// matches input. Input slices are sorted to match DB ORDER BY.
async fn assert_save_configs(
store: &ConfigStore,
role_configs: &[RoleConfig],
model_routings: &[ModelRouting],
) {
let mut roles = role_configs.to_vec();
let mut routings = model_routings.to_vec();
roles.sort_by(|a, b| a.role.cmp(&b.role));
routings.sort_by(|a, b| a.model.cmp(&b.model));
store
.save_role_and_routing_configs(&roles, &routings)
.await
.unwrap();
let saved_roles = store.get_all_role_configs().await.unwrap();
assert_eq!(saved_roles, roles, "saved role configs should match input");
let saved_routings = store.get_all_model_routings().await.unwrap();
assert_eq!(
saved_routings, routings,
"saved model routings should match input"
);
}
#[tokio::test]
async fn test_save_role_and_routing_configs_replaces_old_rows() {
let (store, _dir) = setup().await;
// Pre-insert initial data
assert_save_configs(
&store,
&[RoleConfig {
role: "manager".to_string(),
model: Some("old-model".to_string()),
reasoning_effort: Some("low".to_string()),
}],
&[ModelRouting {
model: "old-model".to_string(),
provider_order: Some("OldProvider".to_string()),
allow_fallbacks: Some(false),
}],
)
.await;
// Replace with completely different data
assert_save_configs(
&store,
&[RoleConfig {
role: "qa".to_string(),
model: Some("new-model".to_string()),
reasoning_effort: None,
}],
&[
ModelRouting {
model: "fallback-model".to_string(),
provider_order: None,
allow_fallbacks: None,
},
ModelRouting {
model: "new-model".to_string(),
provider_order: Some("NewProvider".to_string()),
allow_fallbacks: Some(true),
},
],
)
.await;
}
#[tokio::test]
async fn test_save_role_and_routing_configs_empty_slices() {
let (store, _dir) = setup().await;
// Pre-insert some data via the production batch path
store
.save_role_and_routing_configs(
&[RoleConfig {
role: "should-be-cleared".into(),
model: Some("x".into()),
reasoning_effort: None,
}],
&[ModelRouting {
model: "should-be-cleared".into(),
provider_order: Some("y".into()),
allow_fallbacks: Some(true),
}],
)
.await
.unwrap();
// Save with empty slices — should clear both tables
assert_save_configs(&store, &[], &[]).await;
}
#[tokio::test]
async fn test_save_role_and_routing_configs_duplicate_key_returns_err() {
let (store, dir) = setup().await;
// Open a second connection *before* any transaction starts on the
// first one, so the schema DDL write can complete without lock
// contention. Because SQLite's default isolation ensures that a
// separate connection never sees uncommitted changes from another
// connection, reading from `second` after a failed transaction proves
// that no durable trace was left.
let second = ConfigStore::open(dir.path()).await.unwrap();
// Pre-populate with known data.
let mut original_roles = vec![
RoleConfig {
role: "alpha".to_string(),
model: Some("m1".to_string()),
reasoning_effort: None,
},
RoleConfig {
role: "beta".to_string(),
model: Some("m2".to_string()),
reasoning_effort: Some("low".to_string()),
},
];
let mut original_routings = vec![ModelRouting {
model: "m1".to_string(),
provider_order: Some("ProviderX".to_string()),
allow_fallbacks: Some(true),
}];
original_roles.sort_by(|a, b| a.role.cmp(&b.role));
original_routings.sort_by(|a, b| a.model.cmp(&b.model));
store
.save_role_and_routing_configs(&original_roles, &original_routings)
.await
.unwrap();
// Attempt to save rows with a duplicate role key.
// The second INSERT will hit the PRIMARY KEY constraint and fail.
let conflicting_roles = vec![
RoleConfig {
role: "collides".to_string(),
model: Some("first".to_string()),
reasoning_effort: None,
},
RoleConfig {
role: "collides".to_string(),
model: Some("second".to_string()),
reasoning_effort: Some("high".to_string()),
},
];
let result = store
.save_role_and_routing_configs(&conflicting_roles, &[])
.await;
assert!(result.is_err(), "duplicate role key should cause an error");
// Read from the separate connection — only committed state is visible,
// proving the failed transaction had no permanent effect.
let saved_roles = second.get_all_role_configs().await.unwrap();
assert_eq!(
saved_roles, original_roles,
"role configs should be unchanged after rollback"
);
let saved_routings = second.get_all_model_routings().await.unwrap();
assert_eq!(
saved_routings, original_routings,
"model routings should be unchanged after rollback"
);
}
}