bobbin-ai 0.25.2

Local-first context injection engine for AI coding agents
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
//! Feedback storage using SQLite.
//!
//! Stores injection records (what bobbin injected) and agent feedback
//! (useful/noise/harmful ratings) for tuning search quality.

use anyhow::{Context, Result};
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use std::path::Path;

/// Feedback store backed by SQLite.
pub struct FeedbackStore {
    conn: Connection,
}

/// Input for submitting feedback on an injection.
#[derive(Debug, Deserialize)]
pub struct FeedbackInput {
    pub injection_id: String,
    pub agent: String,
    #[serde(default)]
    pub rating: String,
    #[serde(default)]
    pub reason: String,
}

/// A stored feedback record.
#[derive(Debug, Serialize)]
pub struct FeedbackRecord {
    pub id: i64,
    pub injection_id: String,
    pub timestamp: String,
    pub agent: String,
    pub rating: String,
    pub reason: String,
}

/// Query parameters for listing feedback.
#[derive(Debug, Default, Deserialize)]
pub struct FeedbackQuery {
    pub injection_id: Option<String>,
    pub rating: Option<String>,
    pub agent: Option<String>,
    pub limit: Option<usize>,
}

/// Aggregated feedback statistics.
#[derive(Debug, Serialize)]
pub struct FeedbackStats {
    pub total_injections: u64,
    pub total_feedback: u64,
    pub useful: u64,
    pub noise: u64,
    pub harmful: u64,
    /// Feedback records that have at least one lineage action.
    #[serde(default)]
    pub actioned: u64,
    /// Feedback records with no lineage action.
    #[serde(default)]
    pub unactioned: u64,
    /// Total lineage records.
    #[serde(default)]
    pub lineage_records: u64,
}

/// Feedback statistics for a single bundle or bead grouping.
#[derive(Debug, Serialize)]
pub struct GroupedStatsEntry {
    pub key: String,
    pub injections: u64,
    pub feedback: u64,
    pub useful: u64,
    pub noise: u64,
    pub harmful: u64,
}

/// Input for recording a lineage action that resolves feedback.
#[derive(Debug, Deserialize)]
pub struct LineageInput {
    /// Feedback record IDs this action resolves.
    pub feedback_ids: Vec<i64>,
    /// Type of action taken.
    pub action_type: String,
    /// Associated bead ID (e.g., "bo-a94q").
    #[serde(default)]
    pub bead: Option<String>,
    /// Git commit hash.
    #[serde(default)]
    pub commit_hash: Option<String>,
    /// Human-readable description of what was done.
    pub description: String,
    /// Agent that created this record.
    #[serde(default)]
    pub agent: Option<String>,
}

/// A stored lineage record.
#[derive(Debug, Serialize)]
pub struct LineageRecord {
    pub id: i64,
    pub timestamp: String,
    pub action_type: String,
    pub bead: Option<String>,
    pub commit_hash: Option<String>,
    pub description: String,
    pub agent: Option<String>,
    /// Feedback IDs linked to this action.
    pub feedback_ids: Vec<i64>,
}

/// Query parameters for listing lineage records.
#[derive(Debug, Default, Deserialize)]
pub struct LineageQuery {
    pub feedback_id: Option<i64>,
    pub bead: Option<String>,
    pub commit_hash: Option<String>,
    pub limit: Option<usize>,
}

/// Full injection detail with associated feedback.
#[derive(Debug, Serialize)]
pub struct InjectionDetail {
    pub injection_id: String,
    pub timestamp: String,
    pub session_id: Option<String>,
    pub agent: Option<String>,
    pub query: Option<String>,
    pub files: Vec<String>,
    pub total_chunks: i64,
    pub budget_lines: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub faction_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub grounding_id: Option<String>,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub formatted_output: String,
    pub feedback: Vec<FeedbackRecord>,
}

const VALID_RATINGS: &[&str] = &["useful", "noise", "harmful"];

impl FeedbackStore {
    /// Open or create a feedback store at the given path.
    pub fn open(path: &Path) -> Result<Self> {
        let conn = Connection::open(path)
            .with_context(|| format!("Failed to open feedback database: {}", path.display()))?;

        conn.execute_batch("PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON;")
            .context("Failed to set pragmas")?;

        let store = Self { conn };
        store.init_schema()?;
        Ok(store)
    }

    fn init_schema(&self) -> Result<()> {
        self.conn.execute_batch(
            r#"
            CREATE TABLE IF NOT EXISTS injections (
                injection_id TEXT PRIMARY KEY,
                timestamp TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
                session_id TEXT,
                agent TEXT,
                query TEXT,
                files_json TEXT,
                chunks_json TEXT,
                total_chunks INTEGER,
                budget_lines INTEGER
            );

            CREATE TABLE IF NOT EXISTS feedback (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                injection_id TEXT NOT NULL,
                timestamp TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
                agent TEXT NOT NULL,
                rating TEXT NOT NULL,
                reason TEXT DEFAULT ''
            );

            CREATE INDEX IF NOT EXISTS idx_feedback_injection ON feedback(injection_id);
            CREATE INDEX IF NOT EXISTS idx_feedback_rating ON feedback(rating);
            CREATE INDEX IF NOT EXISTS idx_feedback_agent ON feedback(agent);
        "#,
        )?;

        // Lineage tables for feedback-to-fix traceability
        self.conn.execute_batch(
            r#"
            CREATE TABLE IF NOT EXISTS lineage (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                timestamp TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
                action_type TEXT NOT NULL,
                bead TEXT,
                commit_hash TEXT,
                description TEXT NOT NULL,
                agent TEXT
            );

            CREATE TABLE IF NOT EXISTS lineage_feedback (
                lineage_id INTEGER NOT NULL REFERENCES lineage(id),
                feedback_id INTEGER NOT NULL REFERENCES feedback(id),
                PRIMARY KEY (lineage_id, feedback_id)
            );

            CREATE INDEX IF NOT EXISTS idx_lineage_bead ON lineage(bead);
            CREATE INDEX IF NOT EXISTS idx_lineage_commit ON lineage(commit_hash);
        "#,
        )?;

        // Schema migrations: add columns if missing
        let migrations: &[(&str, &str)] = &[
            (
                "formatted_output",
                "ALTER TABLE injections ADD COLUMN formatted_output TEXT DEFAULT '';",
            ),
            (
                "bundle_name",
                "ALTER TABLE injections ADD COLUMN bundle_name TEXT;",
            ),
            ("bead_id", "ALTER TABLE injections ADD COLUMN bead_id TEXT;"),
            ("scope", "ALTER TABLE injections ADD COLUMN scope TEXT;"),
            (
                "faction_id",
                "ALTER TABLE injections ADD COLUMN faction_id TEXT;",
            ),
            (
                "grounding_id",
                "ALTER TABLE injections ADD COLUMN grounding_id TEXT;",
            ),
        ];
        for (col, ddl) in migrations {
            let has_col: bool = self.conn.query_row(
                &format!("SELECT COUNT(*) > 0 FROM pragma_table_info('injections') WHERE name = '{}'", col),
                [],
                |row| row.get(0),
            ).unwrap_or(false);
            if !has_col {
                self.conn.execute_batch(ddl)?;
            }
        }

        // Index for bundle/bead grouping queries
        self.conn.execute_batch(
            "CREATE INDEX IF NOT EXISTS idx_injections_bundle ON injections(bundle_name);
             CREATE INDEX IF NOT EXISTS idx_injections_bead ON injections(bead_id);",
        )?;

        Ok(())
    }

    /// Store an injection record (what bobbin injected, for later feedback reference).
    pub fn store_injection(
        &self,
        injection_id: &str,
        session_id: Option<&str>,
        agent: Option<&str>,
        query: &str,
        files: &[String],
        total_chunks: usize,
        budget_lines: usize,
    ) -> Result<()> {
        self.store_injection_with_output(
            injection_id,
            session_id,
            agent,
            query,
            files,
            total_chunks,
            budget_lines,
            None,
        )
    }

    /// Store an injection record with the formatted output text agents see.
    pub fn store_injection_with_output(
        &self,
        injection_id: &str,
        session_id: Option<&str>,
        agent: Option<&str>,
        query: &str,
        files: &[String],
        total_chunks: usize,
        budget_lines: usize,
        formatted_output: Option<&str>,
    ) -> Result<()> {
        self.store_scoped_injection_with_output(
            injection_id,
            session_id,
            agent,
            query,
            files,
            total_chunks,
            budget_lines,
            formatted_output,
            None,
            None,
            None,
        )
    }

    /// Store an injection plus its verified knowledge scope.
    #[allow(clippy::too_many_arguments)]
    pub fn store_scoped_injection_with_output(
        &self,
        injection_id: &str,
        session_id: Option<&str>,
        agent: Option<&str>,
        query: &str,
        files: &[String],
        total_chunks: usize,
        budget_lines: usize,
        formatted_output: Option<&str>,
        scope: Option<&str>,
        faction_id: Option<&str>,
        grounding_id: Option<&str>,
    ) -> Result<()> {
        if scope == Some("na") && (faction_id.is_none() || grounding_id.is_none()) {
            anyhow::bail!("NA injections require faction_id and grounding_id");
        }
        if scope != Some("na") && (faction_id.is_some() || grounding_id.is_some()) {
            anyhow::bail!("faction_id and grounding_id require scope=na");
        }
        let files_json = serde_json::to_string(files).unwrap_or_default();
        let now = chrono::Utc::now()
            .format("%Y-%m-%dT%H:%M:%S%.fZ")
            .to_string();
        self.conn.execute(
            "INSERT OR REPLACE INTO injections (injection_id, timestamp, session_id, agent, query, files_json, total_chunks, budget_lines, formatted_output, scope, faction_id, grounding_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
            rusqlite::params![injection_id, now, session_id, agent, query, files_json, total_chunks as i64, budget_lines as i64, formatted_output.unwrap_or(""), scope, faction_id, grounding_id],
        )?;
        Ok(())
    }

    /// Store an injection record with full metadata including bundle/bead context.
    pub fn store_injection_full(
        &self,
        injection_id: &str,
        session_id: Option<&str>,
        agent: Option<&str>,
        query: &str,
        files: &[String],
        total_chunks: usize,
        budget_lines: usize,
        formatted_output: Option<&str>,
        bundle_name: Option<&str>,
        bead_id: Option<&str>,
    ) -> Result<()> {
        let files_json = serde_json::to_string(files).unwrap_or_default();
        let now = chrono::Utc::now()
            .format("%Y-%m-%dT%H:%M:%S%.fZ")
            .to_string();
        self.conn.execute(
            "INSERT OR REPLACE INTO injections (injection_id, timestamp, session_id, agent, query, files_json, total_chunks, budget_lines, formatted_output, bundle_name, bead_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
            rusqlite::params![injection_id, now, session_id, agent, query, files_json, total_chunks as i64, budget_lines as i64, formatted_output.unwrap_or(""), bundle_name, bead_id],
        )?;
        Ok(())
    }

    /// Store feedback for an injection.
    pub fn store_feedback(&self, input: &FeedbackInput) -> Result<i64> {
        if !VALID_RATINGS.contains(&input.rating.as_str()) {
            anyhow::bail!(
                "Invalid rating '{}'. Must be one of: {}",
                input.rating,
                VALID_RATINGS.join(", ")
            );
        }
        if input.injection_id.is_empty() {
            anyhow::bail!("injection_id is required");
        }

        // Explicitly set timestamp — older DB schemas may lack DEFAULT clauses.
        let now = chrono::Utc::now()
            .format("%Y-%m-%dT%H:%M:%S%.fZ")
            .to_string();

        // Ensure the injection_id exists in the injections table (auto-create if needed).
        // Include timestamp explicitly for older schemas where it's NOT NULL without DEFAULT.
        self.conn.execute(
            "INSERT OR IGNORE INTO injections (injection_id, timestamp) VALUES (?1, ?2)",
            rusqlite::params![input.injection_id, now],
        )?;

        self.conn.execute(
            "INSERT INTO feedback (injection_id, timestamp, agent, rating, reason) VALUES (?1, ?2, ?3, ?4, ?5)",
            rusqlite::params![input.injection_id, now, input.agent, input.rating, input.reason],
        )?;

        Ok(self.conn.last_insert_rowid())
    }

    /// List feedback records with optional filters.
    pub fn list_feedback(&self, query: &FeedbackQuery) -> Result<Vec<FeedbackRecord>> {
        let limit = query.limit.unwrap_or(20).min(50);
        let mut sql = String::from(
            "SELECT id, injection_id, timestamp, agent, rating, reason FROM feedback WHERE 1=1",
        );
        let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = Vec::new();

        if let Some(ref inj) = query.injection_id {
            sql.push_str(&format!(" AND injection_id = ?{}", params.len() + 1));
            params.push(Box::new(inj.clone()));
        }
        if let Some(ref rating) = query.rating {
            sql.push_str(&format!(" AND rating = ?{}", params.len() + 1));
            params.push(Box::new(rating.clone()));
        }
        if let Some(ref agent) = query.agent {
            sql.push_str(&format!(" AND agent = ?{}", params.len() + 1));
            params.push(Box::new(agent.clone()));
        }

        sql.push_str(&format!(
            " ORDER BY timestamp DESC LIMIT ?{}",
            params.len() + 1
        ));
        params.push(Box::new(limit as i64));

        let param_refs: Vec<&dyn rusqlite::types::ToSql> =
            params.iter().map(|p| p.as_ref()).collect();
        let mut stmt = self.conn.prepare(&sql)?;
        let rows = stmt.query_map(param_refs.as_slice(), |row| {
            Ok(FeedbackRecord {
                id: row.get(0)?,
                injection_id: row.get(1)?,
                timestamp: row.get(2)?,
                agent: row.get(3)?,
                rating: row.get(4)?,
                reason: row.get(5)?,
            })
        })?;

        let mut records = Vec::new();
        for row in rows {
            records.push(row?);
        }
        Ok(records)
    }

    /// Get aggregated feedback statistics.
    pub fn stats(&self) -> Result<FeedbackStats> {
        // rusqlite 0.40 dropped FromSql for u64 (SQLite integers are i64);
        // COUNT(*) is non-negative, so the casts are lossless.
        let total_injections: u64 =
            self.conn
                .query_row("SELECT COUNT(*) FROM injections", [], |row| {
                    row.get::<_, i64>(0)
                })? as u64;

        let total_feedback: u64 =
            self.conn
                .query_row("SELECT COUNT(*) FROM feedback", [], |row| {
                    row.get::<_, i64>(0)
                })? as u64;

        let useful: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM feedback WHERE rating = 'useful'",
            [],
            |row| row.get::<_, i64>(0),
        )? as u64;
        let noise: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM feedback WHERE rating = 'noise'",
            [],
            |row| row.get::<_, i64>(0),
        )? as u64;
        let harmful: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM feedback WHERE rating = 'harmful'",
            [],
            |row| row.get::<_, i64>(0),
        )? as u64;

        // Lineage stats
        let lineage_records: u64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM lineage", [], |row| {
                row.get::<_, i64>(0)
            })
            .unwrap_or(0) as u64;
        let actioned: u64 = self
            .conn
            .query_row(
                "SELECT COUNT(DISTINCT feedback_id) FROM lineage_feedback",
                [],
                |row| row.get::<_, i64>(0),
            )
            .unwrap_or(0) as u64;
        let unactioned = total_feedback.saturating_sub(actioned);

        Ok(FeedbackStats {
            total_injections,
            total_feedback,
            useful,
            noise,
            harmful,
            actioned,
            unactioned,
            lineage_records,
        })
    }

    /// Get feedback statistics grouped by bundle name.
    pub fn stats_by_bundle(&self) -> Result<Vec<GroupedStatsEntry>> {
        self.stats_grouped("bundle_name")
    }

    /// Get feedback statistics grouped by bead ID.
    pub fn stats_by_bead(&self) -> Result<Vec<GroupedStatsEntry>> {
        self.stats_grouped("bead_id")
    }

    fn stats_grouped(&self, group_col: &str) -> Result<Vec<GroupedStatsEntry>> {
        let sql = format!(
            r#"SELECT
                COALESCE(i.{col}, '(none)') as group_key,
                COUNT(DISTINCT i.injection_id) as injections,
                COUNT(DISTINCT f.id) as feedback,
                COUNT(DISTINCT CASE WHEN f.rating = 'useful' THEN f.id END) as useful,
                COUNT(DISTINCT CASE WHEN f.rating = 'noise' THEN f.id END) as noise,
                COUNT(DISTINCT CASE WHEN f.rating = 'harmful' THEN f.id END) as harmful
            FROM injections i
            LEFT JOIN feedback f ON i.injection_id = f.injection_id
            GROUP BY group_key
            ORDER BY injections DESC"#,
            col = group_col,
        );
        let mut stmt = self.conn.prepare(&sql)?;
        let rows = stmt.query_map([], |row| {
            // rusqlite 0.40 dropped FromSql for u64; the counts are
            // non-negative, so i64 -> u64 is lossless.
            Ok(GroupedStatsEntry {
                key: row.get(0)?,
                injections: row.get::<_, i64>(1)? as u64,
                feedback: row.get::<_, i64>(2)? as u64,
                useful: row.get::<_, i64>(3)? as u64,
                noise: row.get::<_, i64>(4)? as u64,
                harmful: row.get::<_, i64>(5)? as u64,
            })
        })?;
        let mut entries = Vec::new();
        for row in rows {
            entries.push(row?);
        }
        Ok(entries)
    }

    /// Record a lineage action that resolves one or more feedback records.
    pub fn store_lineage(&self, input: &LineageInput) -> Result<i64> {
        const VALID_ACTIONS: &[&str] = &[
            "access_rule",
            "tag_effect",
            "config_change",
            "code_fix",
            "exclusion_rule",
        ];
        if !VALID_ACTIONS.contains(&input.action_type.as_str()) {
            anyhow::bail!(
                "Invalid action_type '{}'. Must be one of: {}",
                input.action_type,
                VALID_ACTIONS.join(", ")
            );
        }
        if input.feedback_ids.is_empty() {
            anyhow::bail!("feedback_ids must not be empty");
        }

        // Validate that all feedback IDs exist before creating the lineage record.
        // Without this, INSERT OR IGNORE silently drops rows that violate the FK
        // constraint, leading to lineage records with no linked feedback.
        let mut missing = Vec::new();
        for &fid in &input.feedback_ids {
            let exists: bool = self
                .conn
                .query_row(
                    "SELECT COUNT(*) > 0 FROM feedback WHERE id = ?1",
                    rusqlite::params![fid],
                    |row| row.get(0),
                )
                .unwrap_or(false);
            if !exists {
                missing.push(fid);
            }
        }
        if !missing.is_empty() {
            anyhow::bail!(
                "Feedback IDs not found: {:?}. Use bobbin_feedback_list to find valid IDs.",
                missing
            );
        }

        let now = chrono::Utc::now()
            .format("%Y-%m-%dT%H:%M:%S%.fZ")
            .to_string();

        self.conn.execute(
            "INSERT INTO lineage (timestamp, action_type, bead, commit_hash, description, agent) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            rusqlite::params![now, input.action_type, input.bead, input.commit_hash, input.description, input.agent],
        )?;
        let lineage_id = self.conn.last_insert_rowid();

        for &fid in &input.feedback_ids {
            self.conn.execute(
                "INSERT INTO lineage_feedback (lineage_id, feedback_id) VALUES (?1, ?2)",
                rusqlite::params![lineage_id, fid],
            )?;
        }

        Ok(lineage_id)
    }

    /// List lineage records with optional filters.
    pub fn list_lineage(&self, query: &LineageQuery) -> Result<Vec<LineageRecord>> {
        let limit = query.limit.unwrap_or(20).min(50);
        let mut sql = String::from("SELECT l.id, l.timestamp, l.action_type, l.bead, l.commit_hash, l.description, l.agent FROM lineage l");
        let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = Vec::new();
        let mut where_added = false;

        if let Some(fid) = query.feedback_id {
            sql.push_str(
                " JOIN lineage_feedback lf ON l.id = lf.lineage_id WHERE lf.feedback_id = ?1",
            );
            params.push(Box::new(fid));
            where_added = true;
        }

        if let Some(ref bead) = query.bead {
            if where_added {
                sql.push_str(&format!(" AND l.bead = ?{}", params.len() + 1));
            } else {
                sql.push_str(&format!(" WHERE l.bead = ?{}", params.len() + 1));
                where_added = true;
            }
            params.push(Box::new(bead.clone()));
        }

        if let Some(ref commit) = query.commit_hash {
            if where_added {
                sql.push_str(&format!(" AND l.commit_hash = ?{}", params.len() + 1));
            } else {
                sql.push_str(&format!(" WHERE l.commit_hash = ?{}", params.len() + 1));
            }
            params.push(Box::new(commit.clone()));
        }

        sql.push_str(&format!(
            " ORDER BY l.timestamp DESC LIMIT ?{}",
            params.len() + 1
        ));
        params.push(Box::new(limit as i64));

        let param_refs: Vec<&dyn rusqlite::types::ToSql> =
            params.iter().map(|p| p.as_ref()).collect();
        let mut stmt = self.conn.prepare(&sql)?;
        let rows = stmt.query_map(param_refs.as_slice(), |row| {
            Ok((
                row.get::<_, i64>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, String>(2)?,
                row.get::<_, Option<String>>(3)?,
                row.get::<_, Option<String>>(4)?,
                row.get::<_, String>(5)?,
                row.get::<_, Option<String>>(6)?,
            ))
        })?;

        let mut records = Vec::new();
        for row in rows {
            let (id, timestamp, action_type, bead, commit_hash, description, agent) = row?;
            // Fetch linked feedback IDs
            let feedback_ids = self.lineage_feedback_ids(id)?;
            records.push(LineageRecord {
                id,
                timestamp,
                action_type,
                bead,
                commit_hash,
                description,
                agent,
                feedback_ids,
            });
        }
        Ok(records)
    }

    /// Get feedback IDs linked to a lineage record.
    fn lineage_feedback_ids(&self, lineage_id: i64) -> Result<Vec<i64>> {
        let mut stmt = self.conn.prepare(
            "SELECT feedback_id FROM lineage_feedback WHERE lineage_id = ?1 ORDER BY feedback_id",
        )?;
        let rows = stmt.query_map(rusqlite::params![lineage_id], |row| row.get(0))?;
        let mut ids = Vec::new();
        for row in rows {
            ids.push(row?);
        }
        Ok(ids)
    }

    /// Get full injection detail by ID, including associated feedback.
    pub fn get_injection(&self, injection_id: &str) -> Result<Option<InjectionDetail>> {
        let mut stmt = self.conn.prepare(
            "SELECT injection_id, timestamp, session_id, agent, query, files_json, total_chunks, budget_lines, COALESCE(formatted_output, ''), scope, faction_id, grounding_id FROM injections WHERE injection_id = ?1",
        )?;

        let mut rows = stmt.query(rusqlite::params![injection_id])?;
        let row = match rows.next()? {
            Some(r) => r,
            None => return Ok(None),
        };

        let files_json: Option<String> = row.get(5)?;
        let files: Vec<String> = files_json
            .and_then(|j| serde_json::from_str(&j).ok())
            .unwrap_or_default();
        let formatted_output: String = row.get(8)?;

        let feedback = self.list_feedback(&FeedbackQuery {
            injection_id: Some(injection_id.to_string()),
            limit: Some(50),
            ..Default::default()
        })?;

        Ok(Some(InjectionDetail {
            injection_id: row.get(0)?,
            timestamp: row.get(1)?,
            session_id: row.get(2)?,
            agent: row.get(3)?,
            query: row.get(4)?,
            files,
            total_chunks: row.get::<_, Option<i64>>(6)?.unwrap_or(0),
            budget_lines: row.get::<_, Option<i64>>(7)?.unwrap_or(0),
            formatted_output,
            scope: row.get(9)?,
            faction_id: row.get(10)?,
            grounding_id: row.get(11)?,
            feedback,
        }))
    }
    /// Get injection IDs from a session that have no feedback yet.
    pub fn unrated_injections_for_session(&self, session_id: &str) -> Result<Vec<String>> {
        let mut stmt = self.conn.prepare(
            "SELECT i.injection_id FROM injections i
             LEFT JOIN feedback f ON i.injection_id = f.injection_id
             WHERE i.session_id = ?1 AND f.id IS NULL
             ORDER BY i.timestamp DESC
             LIMIT 10",
        )?;
        let rows = stmt.query_map(rusqlite::params![session_id], |row| row.get::<_, String>(0))?;
        let mut ids = Vec::new();
        for row in rows {
            ids.push(row?);
        }
        Ok(ids)
    }

    /// Compute per-file feedback scores based on cross-agent ratings.
    ///
    /// For each file that appeared in a rated injection, accumulates a score:
    /// - "useful" ratings add +1.0 (scaled by query keyword overlap)
    /// - "noise" ratings add -0.3
    /// - "harmful" ratings add -1.0
    ///
    /// Query overlap is measured by splitting both the current query and the
    /// injection query into lowercase keywords and computing Jaccard similarity.
    /// This ensures files rated useful for similar topics get boosted more than
    /// files rated useful for unrelated queries.
    ///
    /// Returns a map of file_path → aggregate score. Only files with non-zero
    /// scores are included. Scores are NOT normalized — callers should apply
    /// their own clamping/scaling.
    pub fn file_feedback_scores(
        &self,
        query: &str,
        min_overlap: f32,
    ) -> Result<std::collections::HashMap<String, f32>> {
        use std::collections::{HashMap, HashSet};

        let query_words: HashSet<String> = query
            .to_lowercase()
            .split_whitespace()
            .filter(|w| w.len() >= 3)
            .map(|w| w.to_string())
            .collect();

        if query_words.is_empty() {
            return Ok(HashMap::new());
        }

        // Join injections + feedback, get the query + files + rating
        let mut stmt = self.conn.prepare(
            "SELECT i.query, i.files_json, f.rating
             FROM feedback f
             JOIN injections i ON f.injection_id = i.injection_id
             WHERE i.files_json IS NOT NULL AND i.query IS NOT NULL
             ORDER BY f.timestamp DESC
             LIMIT 500",
        )?;

        let mut scores: HashMap<String, f32> = HashMap::new();

        let rows = stmt.query_map([], |row| {
            Ok((
                row.get::<_, String>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, String>(2)?,
            ))
        })?;

        for row in rows {
            let (inj_query, files_json, rating) = row?;

            // Compute keyword overlap (Jaccard similarity)
            let inj_words: HashSet<String> = inj_query
                .to_lowercase()
                .split_whitespace()
                .filter(|w| w.len() >= 3)
                .map(|w| w.to_string())
                .collect();
            if inj_words.is_empty() {
                continue;
            }
            let intersection = query_words.intersection(&inj_words).count() as f32;
            let union = query_words.union(&inj_words).count() as f32;
            let overlap = intersection / union;

            if overlap < min_overlap {
                continue;
            }

            let weight = match rating.as_str() {
                "useful" => 1.0 * overlap,
                "noise" => -0.3 * overlap,
                "harmful" => -1.0 * overlap,
                _ => continue,
            };

            // Parse files_json
            if let Ok(files) = serde_json::from_str::<Vec<String>>(&files_json) {
                for file in files {
                    *scores.entry(file).or_insert(0.0) += weight;
                }
            }
        }

        // Remove zero-score entries
        scores.retain(|_, v| *v != 0.0);
        Ok(scores)
    }

    /// Get feedback auto-tags (feedback:hot, feedback:cold) keyed by file path.
    /// Returns empty map if the feedback_tags table doesn't exist yet.
    pub fn get_feedback_tags(&self) -> Result<std::collections::HashMap<String, Vec<String>>> {
        let mut map = std::collections::HashMap::new();
        let has_table: bool = self.conn.query_row(
            "SELECT COUNT(*) > 0 FROM sqlite_master WHERE type='table' AND name='feedback_tags'",
            [],
            |row| row.get(0),
        )?;
        if !has_table {
            return Ok(map);
        }
        let mut stmt = self
            .conn
            .prepare("SELECT file_path, tag FROM feedback_tags")?;
        let rows = stmt.query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
        })?;
        for row in rows {
            let (path, tag) = row?;
            map.entry(path).or_insert_with(Vec::new).push(tag);
        }
        Ok(map)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;

    fn temp_store() -> (FeedbackStore, NamedTempFile) {
        let f = NamedTempFile::new().unwrap();
        let store = FeedbackStore::open(f.path()).unwrap();
        (store, f)
    }

    #[test]
    fn test_store_and_list_feedback() {
        let (store, _f) = temp_store();
        let input = FeedbackInput {
            injection_id: "inj-abc123".to_string(),
            agent: "aegis/crew/test".to_string(),
            rating: "useful".to_string(),
            reason: "helped find the right file".to_string(),
        };
        let id = store.store_feedback(&input).unwrap();
        assert!(id > 0);

        let records = store.list_feedback(&FeedbackQuery::default()).unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].injection_id, "inj-abc123");
        assert_eq!(records[0].rating, "useful");
    }

    #[test]
    fn test_invalid_rating() {
        let (store, _f) = temp_store();
        let input = FeedbackInput {
            injection_id: "inj-abc123".to_string(),
            agent: "test".to_string(),
            rating: "bad".to_string(),
            reason: String::new(),
        };
        assert!(store.store_feedback(&input).is_err());
    }

    #[test]
    fn test_stats() {
        let (store, _f) = temp_store();
        for (inj, rating) in [
            ("inj-1", "useful"),
            ("inj-1", "noise"),
            ("inj-2", "harmful"),
        ] {
            store
                .store_feedback(&FeedbackInput {
                    injection_id: inj.to_string(),
                    agent: "test".to_string(),
                    rating: rating.to_string(),
                    reason: String::new(),
                })
                .unwrap();
        }
        let stats = store.stats().unwrap();
        assert_eq!(stats.total_injections, 2);
        assert_eq!(stats.total_feedback, 3);
        assert_eq!(stats.useful, 1);
        assert_eq!(stats.noise, 1);
        assert_eq!(stats.harmful, 1);
    }

    #[test]
    fn test_filter_by_rating() {
        let (store, _f) = temp_store();
        for rating in ["useful", "noise", "useful"] {
            store
                .store_feedback(&FeedbackInput {
                    injection_id: "inj-1".to_string(),
                    agent: "test".to_string(),
                    rating: rating.to_string(),
                    reason: String::new(),
                })
                .unwrap();
        }
        let q = FeedbackQuery {
            rating: Some("useful".to_string()),
            ..Default::default()
        };
        let records = store.list_feedback(&q).unwrap();
        assert_eq!(records.len(), 2);
    }

    #[test]
    fn test_react_injection_id() {
        let (store, _f) = temp_store();
        let input = FeedbackInput {
            injection_id: "inj-react-abc123".to_string(),
            agent: "test".to_string(),
            rating: "useful".to_string(),
            reason: "reaction context was helpful".to_string(),
        };
        store.store_feedback(&input).unwrap();
        let records = store
            .list_feedback(&FeedbackQuery {
                injection_id: Some("inj-react-abc123".to_string()),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].injection_id, "inj-react-abc123");
    }

    #[test]
    fn test_store_injection() {
        let (store, _f) = temp_store();
        let files = vec!["src/main.rs".to_string(), "src/lib.rs".to_string()];
        store
            .store_injection(
                "inj-test1234",
                Some("session-abc"),
                Some("aegis/crew/test"),
                "how does auth work?",
                &files,
                5,
                300,
            )
            .unwrap();

        // Verify injection was stored
        let stats = store.stats().unwrap();
        assert_eq!(stats.total_injections, 1);

        // Feedback referencing this injection should work
        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-test1234".to_string(),
                agent: "test".to_string(),
                rating: "useful".to_string(),
                reason: "found the auth module".to_string(),
            })
            .unwrap();
        let stats = store.stats().unwrap();
        assert_eq!(stats.total_feedback, 1);
    }

    #[test]
    fn test_get_injection_detail() {
        let (store, _f) = temp_store();
        let files = vec!["src/main.rs".to_string(), "src/lib.rs".to_string()];
        store
            .store_injection(
                "inj-detail",
                Some("sess-1"),
                Some("aegis/crew/ian"),
                "how does auth work?",
                &files,
                5,
                300,
            )
            .unwrap();
        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-detail".to_string(),
                agent: "aegis/crew/ellie".to_string(),
                rating: "useful".to_string(),
                reason: "great context".to_string(),
            })
            .unwrap();

        let detail = store.get_injection("inj-detail").unwrap().unwrap();
        assert_eq!(detail.injection_id, "inj-detail");
        assert_eq!(detail.query.as_deref(), Some("how does auth work?"));
        assert_eq!(detail.files.len(), 2);
        assert_eq!(detail.total_chunks, 5);
        assert_eq!(detail.budget_lines, 300);
        assert_eq!(detail.feedback.len(), 1);
        assert_eq!(detail.feedback[0].rating, "useful");

        // Non-existent injection returns None
        assert!(store.get_injection("inj-nope").unwrap().is_none());
    }

    #[test]
    fn test_lineage_store_and_list() {
        let (store, _f) = temp_store();
        // Create feedback records first
        let fid1 = store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-1".to_string(),
                agent: "test".to_string(),
                rating: "noise".to_string(),
                reason: "init function noise".to_string(),
            })
            .unwrap();
        let fid2 = store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-2".to_string(),
                agent: "test".to_string(),
                rating: "noise".to_string(),
                reason: "init function noise".to_string(),
            })
            .unwrap();

        // Record lineage action
        let lid = store
            .store_lineage(&LineageInput {
                feedback_ids: vec![fid1, fid2],
                action_type: "tag_effect".to_string(),
                bead: Some("bo-a94q".to_string()),
                commit_hash: Some("4ded620".to_string()),
                description: "Deployed auto:init exclude".to_string(),
                agent: Some("aegis/crew/ian".to_string()),
            })
            .unwrap();
        assert!(lid > 0);

        // List all lineage
        let records = store.list_lineage(&LineageQuery::default()).unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].action_type, "tag_effect");
        assert_eq!(records[0].bead.as_deref(), Some("bo-a94q"));
        assert_eq!(records[0].feedback_ids, vec![fid1, fid2]);

        // Query by bead
        let records = store
            .list_lineage(&LineageQuery {
                bead: Some("bo-a94q".to_string()),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(records.len(), 1);

        // Query by feedback_id
        let records = store
            .list_lineage(&LineageQuery {
                feedback_id: Some(fid1),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(records.len(), 1);
    }

    #[test]
    fn test_lineage_stats() {
        let (store, _f) = temp_store();
        let fid1 = store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-1".to_string(),
                agent: "test".to_string(),
                rating: "noise".to_string(),
                reason: "noise".to_string(),
            })
            .unwrap();
        let _fid2 = store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-2".to_string(),
                agent: "test".to_string(),
                rating: "noise".to_string(),
                reason: "noise".to_string(),
            })
            .unwrap();

        // Before lineage: 0 actioned, 2 unactioned
        let stats = store.stats().unwrap();
        assert_eq!(stats.actioned, 0);
        assert_eq!(stats.unactioned, 2);
        assert_eq!(stats.lineage_records, 0);

        // Record lineage for fid1 only
        store
            .store_lineage(&LineageInput {
                feedback_ids: vec![fid1],
                action_type: "code_fix".to_string(),
                bead: None,
                commit_hash: Some("abc123".to_string()),
                description: "Fixed it".to_string(),
                agent: None,
            })
            .unwrap();

        let stats = store.stats().unwrap();
        assert_eq!(stats.actioned, 1);
        assert_eq!(stats.unactioned, 1);
        assert_eq!(stats.lineage_records, 1);
    }

    #[test]
    fn test_lineage_invalid_action_type() {
        let (store, _f) = temp_store();
        let fid = store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-1".to_string(),
                agent: "test".to_string(),
                rating: "noise".to_string(),
                reason: "test".to_string(),
            })
            .unwrap();
        let result = store.store_lineage(&LineageInput {
            feedback_ids: vec![fid],
            action_type: "invalid".to_string(),
            bead: None,
            commit_hash: None,
            description: "test".to_string(),
            agent: None,
        });
        assert!(result.is_err());
    }

    #[test]
    fn test_lineage_rejects_nonexistent_feedback_ids() {
        let (store, _f) = temp_store();
        let fid = store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-1".to_string(),
                agent: "test".to_string(),
                rating: "noise".to_string(),
                reason: "test".to_string(),
            })
            .unwrap();

        // Mix of valid and invalid feedback IDs should be rejected
        let result = store.store_lineage(&LineageInput {
            feedback_ids: vec![fid, 9999],
            action_type: "code_fix".to_string(),
            bead: None,
            commit_hash: None,
            description: "test".to_string(),
            agent: None,
        });
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("9999"),
            "Error should mention the missing ID: {}",
            err_msg
        );

        // Verify no lineage record was created (transaction-like behavior)
        let records = store.list_lineage(&LineageQuery::default()).unwrap();
        assert_eq!(
            records.len(),
            0,
            "No lineage should be created when feedback IDs are invalid"
        );
    }

    #[test]
    fn test_file_feedback_scores_basic() {
        let (store, _f) = temp_store();

        // Create an injection with files and query
        store
            .store_injection_with_output(
                "inj-score1",
                None,
                Some("agent-a"),
                "authentication login handler",
                &["src/auth.rs".to_string(), "src/session.rs".to_string()],
                2,
                100,
                None,
            )
            .unwrap();

        // Rate it useful
        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-score1".to_string(),
                agent: "agent-b".to_string(),
                rating: "useful".to_string(),
                reason: String::new(),
            })
            .unwrap();

        // Query with overlapping keywords
        let scores = store
            .file_feedback_scores("authentication middleware handler", 0.1)
            .unwrap();
        assert!(!scores.is_empty(), "Should have scores for similar query");
        assert!(
            scores.get("src/auth.rs").copied().unwrap_or(0.0) > 0.0,
            "auth.rs should be boosted"
        );
        assert!(
            scores.get("src/session.rs").copied().unwrap_or(0.0) > 0.0,
            "session.rs should be boosted"
        );
    }

    #[test]
    fn test_file_feedback_scores_harmful_penalty() {
        let (store, _f) = temp_store();

        store
            .store_injection_with_output(
                "inj-harm1",
                None,
                Some("agent-a"),
                "database connection pooling",
                &["src/db.rs".to_string()],
                1,
                100,
                None,
            )
            .unwrap();

        // Rate it harmful
        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-harm1".to_string(),
                agent: "agent-c".to_string(),
                rating: "harmful".to_string(),
                reason: "wrong file".to_string(),
            })
            .unwrap();

        let scores = store
            .file_feedback_scores("database connection pooling", 0.1)
            .unwrap();
        assert!(
            scores.get("src/db.rs").copied().unwrap_or(0.0) < 0.0,
            "db.rs should have negative score from harmful rating"
        );
    }

    #[test]
    fn test_file_feedback_scores_no_overlap() {
        let (store, _f) = temp_store();

        store
            .store_injection_with_output(
                "inj-nooverlap",
                None,
                Some("agent-a"),
                "authentication login handler",
                &["src/auth.rs".to_string()],
                1,
                100,
                None,
            )
            .unwrap();

        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-nooverlap".to_string(),
                agent: "agent-b".to_string(),
                rating: "useful".to_string(),
                reason: String::new(),
            })
            .unwrap();

        // Query with completely different keywords — should have no overlap
        let scores = store
            .file_feedback_scores("database migration schema", 0.1)
            .unwrap();
        assert!(
            scores.is_empty(),
            "Should have no scores for unrelated query"
        );
    }

    #[test]
    fn test_file_feedback_scores_cross_agent() {
        let (store, _f) = temp_store();

        // Two different agents rate the same injection
        store
            .store_injection_with_output(
                "inj-cross1",
                None,
                Some("agent-a"),
                "error handling retry logic",
                &["src/retry.rs".to_string()],
                1,
                100,
                None,
            )
            .unwrap();

        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-cross1".to_string(),
                agent: "agent-b".to_string(),
                rating: "useful".to_string(),
                reason: String::new(),
            })
            .unwrap();

        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-cross1".to_string(),
                agent: "agent-c".to_string(),
                rating: "useful".to_string(),
                reason: String::new(),
            })
            .unwrap();

        let scores = store
            .file_feedback_scores("error handling retry mechanism", 0.1)
            .unwrap();
        let retry_score = scores.get("src/retry.rs").copied().unwrap_or(0.0);
        assert!(
            retry_score > 0.5,
            "retry.rs should have high score from 2 useful ratings, got {}",
            retry_score
        );
    }

    #[test]
    fn test_stats_by_bundle() {
        let (store, _f) = temp_store();
        let files = vec!["src/main.rs".to_string()];

        // Create injections with bundle_name metadata
        store
            .store_injection_full(
                "inj-b1",
                None,
                None,
                "auth query",
                &files,
                3,
                100,
                None,
                Some("auth"),
                None,
            )
            .unwrap();
        store
            .store_injection_full(
                "inj-b2",
                None,
                None,
                "auth login",
                &files,
                2,
                100,
                None,
                Some("auth"),
                None,
            )
            .unwrap();
        store
            .store_injection_full(
                "inj-b3",
                None,
                None,
                "search query",
                &files,
                4,
                200,
                None,
                Some("search"),
                None,
            )
            .unwrap();
        store
            .store_injection_full(
                "inj-b4",
                None,
                None,
                "no bundle",
                &files,
                1,
                50,
                None,
                None,
                None,
            )
            .unwrap();

        // Add feedback
        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-b1".into(),
                agent: "test".into(),
                rating: "useful".into(),
                reason: String::new(),
            })
            .unwrap();
        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-b2".into(),
                agent: "test".into(),
                rating: "noise".into(),
                reason: String::new(),
            })
            .unwrap();
        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-b3".into(),
                agent: "test".into(),
                rating: "useful".into(),
                reason: String::new(),
            })
            .unwrap();

        let by_bundle = store.stats_by_bundle().unwrap();
        assert!(
            by_bundle.len() >= 2,
            "should have at least auth and search groups"
        );

        let auth = by_bundle.iter().find(|e| e.key == "auth").unwrap();
        assert_eq!(auth.injections, 2);
        assert_eq!(auth.feedback, 2);
        assert_eq!(auth.useful, 1);
        assert_eq!(auth.noise, 1);

        let search = by_bundle.iter().find(|e| e.key == "search").unwrap();
        assert_eq!(search.injections, 1);
        assert_eq!(search.useful, 1);
    }

    #[test]
    fn test_stats_by_bead() {
        let (store, _f) = temp_store();
        let files = vec!["src/main.rs".to_string()];

        store
            .store_injection_full(
                "inj-d1",
                None,
                None,
                "q1",
                &files,
                1,
                100,
                None,
                None,
                Some("aegis-abc"),
            )
            .unwrap();
        store
            .store_injection_full(
                "inj-d2",
                None,
                None,
                "q2",
                &files,
                1,
                100,
                None,
                None,
                Some("aegis-abc"),
            )
            .unwrap();
        store
            .store_injection_full(
                "inj-d3",
                None,
                None,
                "q3",
                &files,
                1,
                100,
                None,
                None,
                Some("aegis-xyz"),
            )
            .unwrap();

        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-d1".into(),
                agent: "test".into(),
                rating: "useful".into(),
                reason: String::new(),
            })
            .unwrap();
        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-d3".into(),
                agent: "test".into(),
                rating: "harmful".into(),
                reason: String::new(),
            })
            .unwrap();

        let by_bead = store.stats_by_bead().unwrap();
        let abc = by_bead.iter().find(|e| e.key == "aegis-abc").unwrap();
        assert_eq!(abc.injections, 2);
        assert_eq!(abc.feedback, 1);
        assert_eq!(abc.useful, 1);

        let xyz = by_bead.iter().find(|e| e.key == "aegis-xyz").unwrap();
        assert_eq!(xyz.injections, 1);
        assert_eq!(xyz.harmful, 1);
    }

    #[test]
    fn scoped_na_injection_persists_scope_for_feedback_join() {
        let (store, _f) = temp_store();
        store
            .store_scoped_injection_with_output(
                "inj-na",
                Some("session"),
                None,
                "turn grounding",
                &[],
                1,
                8,
                Some("grounded"),
                Some("na"),
                Some("gaia"),
                Some("sha256:abc"),
            )
            .unwrap();
        store
            .store_feedback(&FeedbackInput {
                injection_id: "inj-na".into(),
                agent: "agent".into(),
                rating: "useful".into(),
                reason: "matched the turn".into(),
            })
            .unwrap();
        let detail = store.get_injection("inj-na").unwrap().unwrap();
        assert_eq!(detail.scope.as_deref(), Some("na"));
        assert_eq!(detail.faction_id.as_deref(), Some("gaia"));
        assert_eq!(detail.feedback.len(), 1);
    }

    #[test]
    fn unscoped_na_injection_is_rejected() {
        let (store, _f) = temp_store();
        let result = store.store_scoped_injection_with_output(
            "inj-na",
            None,
            None,
            "turn grounding",
            &[],
            1,
            8,
            None,
            Some("na"),
            None,
            Some("sha256:abc"),
        );
        assert!(result.is_err());
    }
}