Repository navigation
Expand file tree
/
Copy pathretriever.rs
More file actions
1520 lines (1413 loc) · 50.3 KB
/
Copy pathretriever.rs
File metadata and controls
1520 lines (1413 loc) · 50.3 KB
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
//! Content-addressed recall store backing `rtk recall`.
use super::constants::RECALL_DB;
use crate::core::config::Config;
use anyhow::{Context, Result};
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use rusqlite::{params, Connection, OptionalExtension};
use serde::{Deserialize, Serialize};
use std::io::{Read, Write};
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
const DEFAULT_MAX_ENTRY_BYTES: usize = 10 * 1024 * 1024;
const DEFAULT_MAX_ENTRIES: usize = 200;
const DEFAULT_RETENTION_DAYS: u32 = 30;
pub const MIN_FAILURE_BYTES: usize = 500;
const HASH_HEX_LEN: usize = 12;
const DEFAULT_TEE_MAX_FILES: usize = 20;
const DEFAULT_TEE_MAX_FILE_SIZE: usize = 1_048_576;
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RecoveryMode {
#[default]
Sqlite,
Tee,
Disabled,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct RetrieverConfig {
pub mode: RecoveryMode,
pub max_entry_bytes: usize,
pub max_entries: usize,
pub retention_days: u32,
pub compression: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub database_path: Option<PathBuf>,
pub tee_max_files: usize,
pub tee_max_file_size: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub tee_directory: Option<PathBuf>,
/// Legacy `[tee] mode = "always"`: archive successful runs too. Tee mode only.
pub tee_on_success: bool,
}
impl Default for RetrieverConfig {
fn default() -> Self {
Self {
mode: RecoveryMode::Sqlite,
max_entry_bytes: DEFAULT_MAX_ENTRY_BYTES,
max_entries: DEFAULT_MAX_ENTRIES,
retention_days: DEFAULT_RETENTION_DAYS,
compression: true,
database_path: None,
tee_max_files: DEFAULT_TEE_MAX_FILES,
tee_max_file_size: DEFAULT_TEE_MAX_FILE_SIZE,
tee_directory: None,
tee_on_success: false,
}
}
}
pub struct StoredRef {
pub hash: String,
pub hidden_lines: usize,
}
pub enum Stored {
Saved(StoredRef),
Unavailable,
Empty,
}
fn now_secs() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0)
}
fn content_hash(command: &str, content: &[u8]) -> String {
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(command.as_bytes());
hasher.update([0u8]);
hasher.update(content);
let hex = format!("{:x}", hasher.finalize());
hex[..HASH_HEX_LEN].to_string()
}
fn count_lines(bytes: &[u8]) -> usize {
if bytes.is_empty() {
return 0;
}
let newlines = bytes.iter().filter(|&&b| b == b'\n').count();
if *bytes.last().unwrap() == b'\n' {
newlines
} else {
newlines + 1
}
}
fn slice_from_line(bytes: &[u8], from: usize) -> &[u8] {
if from <= 1 {
return bytes;
}
let mut seen = 0usize;
for (i, &b) in bytes.iter().enumerate() {
if b == b'\n' {
seen += 1;
if seen == from - 1 {
return &bytes[i + 1..];
}
}
}
&[]
}
fn slice_first_lines(bytes: &[u8], n: usize) -> &[u8] {
if n == 0 {
return &[];
}
let mut seen = 0usize;
for (i, &b) in bytes.iter().enumerate() {
if b == b'\n' {
seen += 1;
if seen == n {
return &bytes[..=i];
}
}
}
bytes
}
fn grep_bytes(input: &[u8], pattern: &str) -> Vec<u8> {
use regex::bytes::Regex;
let re = Regex::new(pattern)
.or_else(|_| Regex::new(®ex::escape(pattern)))
.ok();
let Some(re) = re else {
return input.to_vec();
};
let mut out = Vec::new();
for line in input.split_inclusive(|&b| b == b'\n') {
let body = line.strip_suffix(b"\n").unwrap_or(line);
if re.is_match(body) {
out.extend_from_slice(line);
}
}
out
}
fn gzip(data: &[u8]) -> Result<Vec<u8>> {
let mut enc = GzEncoder::new(Vec::new(), Compression::default());
enc.write_all(data).context("gzip write")?;
enc.finish().context("gzip finish")
}
fn gunzip(data: &[u8]) -> Result<Vec<u8>> {
let mut out = Vec::new();
GzDecoder::new(data)
.read_to_end(&mut out)
.context("gunzip")?;
Ok(out)
}
fn db_path(cfg: &RetrieverConfig) -> Result<PathBuf> {
if let Ok(p) = std::env::var("RTK_RECALL_DB") {
return Ok(PathBuf::from(p));
}
if let Some(ref p) = cfg.database_path {
return Ok(p.clone());
}
// A test that names no store must never reach the developer's own: its rows
// would ship as real usage through the telemetry ping.
#[cfg(test)]
let base = std::env::temp_dir().join(format!("rtk-test-store-{}", std::process::id()));
#[cfg(not(test))]
let base = dirs::data_local_dir()
.ok_or_else(|| anyhow::anyhow!("no local data directory available"))?
.join(super::constants::RTK_DATA_DIR);
Ok(base.join(RECALL_DB))
}
fn open(cfg: &RetrieverConfig) -> Result<Connection> {
let path = db_path(cfg)?;
if let Some(parent) = path.parent() {
let _ = crate::core::utils::create_private_dir(parent);
}
crate::core::utils::open_private(std::fs::OpenOptions::new().write(true).create(true), &path)
.with_context(|| format!("pre-create private recall DB: {}", path.display()))?;
let conn = Connection::open(&path).with_context(|| format!("open {}", path.display()))?;
// best-effort: NFS / read-only filesystems may reject WAL
let _ = conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000;");
init_schema(&conn)?;
Ok(conn)
}
thread_local! {
static CONN_CACHE: std::cell::RefCell<Option<(PathBuf, Connection)>> =
const { std::cell::RefCell::new(None) };
}
fn with_open<T>(cfg: &RetrieverConfig, f: impl FnOnce(&Connection) -> Result<T>) -> Result<T> {
let path = db_path(cfg)?;
CONN_CACHE.with(|cache| {
let mut cache = cache.borrow_mut();
let reuse = matches!(&*cache, Some((p, _)) if *p == path && path.exists());
if !reuse {
*cache = Some((path.clone(), open(cfg)?));
}
let (_, conn) = cache.as_ref().expect("cache populated above");
f(conn)
})
}
fn open_existing(cfg: &RetrieverConfig) -> Result<Option<Connection>> {
let path = db_path(cfg)?;
if !path.exists() {
return Ok(None);
}
let conn = Connection::open(&path).with_context(|| format!("open {}", path.display()))?;
let _ = conn.execute_batch("PRAGMA busy_timeout=5000;");
init_schema(&conn)?;
Ok(Some(conn))
}
fn init_schema(conn: &Connection) -> Result<()> {
conn.execute_batch(
"CREATE TABLE IF NOT EXISTS recall (
hash TEXT PRIMARY KEY,
command TEXT NOT NULL,
cwd TEXT,
exit_code INTEGER,
created_at INTEGER NOT NULL,
total_lines INTEGER NOT NULL,
shown_upto INTEGER NOT NULL,
byte_size INTEGER NOT NULL,
truncated INTEGER NOT NULL,
codec TEXT NOT NULL,
blob BLOB NOT NULL,
recalled INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS tee_reads (
path TEXT PRIMARY KEY
);
CREATE INDEX IF NOT EXISTS idx_recall_command ON recall(command, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_recall_created ON recall(created_at);
CREATE TABLE IF NOT EXISTS recall_stats (
slug TEXT NOT NULL,
mode TEXT NOT NULL,
elisions INTEGER NOT NULL DEFAULT 0,
recalls INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (slug, mode)
);",
)
.context("init recall schema")?;
let _ = conn.execute(
"ALTER TABLE recall ADD COLUMN recalled INTEGER NOT NULL DEFAULT 0",
[],
);
Ok(())
}
fn stat_family(slug: &str) -> &str {
match slug.find(|c: char| c.is_ascii_digit()) {
Some(i) if i > 0 && slug.as_bytes()[i - 1] == b'_' => &slug[..i - 1],
_ => slug,
}
}
fn strip_shortened_hash(s: &str) -> &str {
let b = s.as_bytes();
if b.len() == 15
&& b[8] == b'_'
&& s[9..]
.chars()
.all(|c| c.is_ascii_digit() || ('a'..='f').contains(&c))
{
&s[..8]
} else {
s
}
}
fn stat_key(slug: &str) -> String {
let sanitized = crate::core::tee_file::sanitize_slug(slug);
stat_family(strip_shortened_hash(&sanitized)).to_string()
}
fn bump_stat(conn: &Connection, slug: &str, mode: &str, column: &str) {
let slug = stat_key(slug);
let slug = slug.as_str();
let sql = match column {
"elisions" => {
"INSERT INTO recall_stats (slug, mode, elisions, recalls) VALUES (?1, ?2, 1, 0)
ON CONFLICT(slug, mode) DO UPDATE SET elisions = elisions + 1"
}
"recalls" => {
"INSERT INTO recall_stats (slug, mode, elisions, recalls) VALUES (?1, ?2, 0, 1)
ON CONFLICT(slug, mode) DO UPDATE SET recalls = recalls + 1"
}
_ => return,
};
let _ = conn.execute(sql, params![slug, mode]);
}
pub struct RecallStat {
pub slug: String,
pub mode: String,
pub elisions: i64,
pub recalls: i64,
}
fn stats_snapshot_with(cfg: &RetrieverConfig) -> Result<Vec<RecallStat>> {
let Some(conn) = open_existing(cfg)? else {
return Ok(Vec::new());
};
let mut stmt = conn.prepare("SELECT slug, mode, elisions, recalls FROM recall_stats")?;
let rows = stmt.query_map([], |r| {
Ok((
r.get::<_, String>(0)?,
r.get::<_, String>(1)?,
r.get::<_, i64>(2)?,
r.get::<_, i64>(3)?,
))
})?;
let mut agg: std::collections::BTreeMap<(String, String), (i64, i64)> =
std::collections::BTreeMap::new();
for row in rows.filter_map(|r| r.ok()) {
let (slug, mode, elisions, recalls) = row;
let entry = agg.entry((mode, stat_key(&slug))).or_insert((0, 0));
entry.0 += elisions;
entry.1 += recalls;
}
let mut stats: Vec<RecallStat> = agg
.into_iter()
.map(|((mode, slug), (elisions, recalls))| RecallStat {
slug,
mode,
elisions,
recalls,
})
.collect();
stats.sort_by(|a, b| {
a.mode
.cmp(&b.mode)
.then(b.elisions.cmp(&a.elisions))
.then(a.slug.cmp(&b.slug))
});
Ok(stats)
}
pub fn stats_snapshot() -> Result<Vec<RecallStat>> {
let cfg = Config::load().unwrap_or_default().retriever;
stats_snapshot_with(&cfg)
}
pub fn reset_stats() -> Result<()> {
let cfg = Config::load().unwrap_or_default().retriever;
reset_stats_with(&cfg)
}
fn reset_stats_with(cfg: &RetrieverConfig) -> Result<()> {
let Some(conn) = open_existing(cfg)? else {
return Ok(());
};
conn.execute_batch(
"BEGIN;
DELETE FROM recall_stats;
DELETE FROM tee_reads;
UPDATE recall SET recalled = 0;
COMMIT;",
)
.context("reset recall stats")?;
Ok(())
}
pub fn record_tee_elision(cfg: &RetrieverConfig, slug: &str) {
if cfg.mode == RecoveryMode::Disabled {
return;
}
// Never create the store from the tee path: choosing tee must leave no
// sqlite artifact behind. Stats are recorded only into an existing store.
if let Ok(Some(conn)) = open_existing(cfg) {
bump_stat(&conn, slug, "tee", "elisions");
}
}
fn mark_recalled(conn: &Connection, hash: &str, command: &str) {
let changed = conn
.execute(
"UPDATE recall SET recalled = 1 WHERE hash = ?1 AND recalled = 0",
params![hash],
)
.unwrap_or(0);
if changed > 0 {
bump_stat(conn, command, "sqlite", "recalls");
}
}
fn record_tee_recall_on(conn: &Connection, slug: &str, path: &str) {
let inserted = conn
.execute(
"INSERT OR IGNORE INTO tee_reads (path) VALUES (?1)",
params![path],
)
.unwrap_or(0);
if inserted > 0 {
bump_stat(conn, slug, "tee", "recalls");
let _ = conn.execute(
"DELETE FROM tee_reads WHERE rowid NOT IN (
SELECT rowid FROM tee_reads ORDER BY rowid DESC LIMIT 500
)",
[],
);
}
}
pub(crate) fn recovery_disabled_by_env() -> bool {
matches!(std::env::var("RTK_RECALL").ok().as_deref(), Some("0"))
|| matches!(std::env::var("RTK_TEE").ok().as_deref(), Some("0"))
}
fn record_tee_recall_with(cfg: &RetrieverConfig, slug: &str, path: &str) {
if recovery_disabled_by_env() || cfg.mode == RecoveryMode::Disabled {
return;
}
if let Ok(Some(conn)) = open_existing(cfg) {
record_tee_recall_on(&conn, slug, path);
}
}
pub fn record_tee_recall(slug: &str, path: &str) {
let cfg = Config::load().unwrap_or_default().retriever;
record_tee_recall_with(&cfg, slug, path);
}
fn evict(conn: &Connection, cfg: &RetrieverConfig, keep_hash: &str) {
if cfg.retention_days > 0 {
let cutoff = now_secs() - (cfg.retention_days as i64) * 86_400;
let _ = conn.execute(
"DELETE FROM recall WHERE created_at < ?1 AND hash != ?2",
params![cutoff, keep_hash],
);
}
if cfg.max_entries > 0 {
let count: i64 = conn
.query_row("SELECT COUNT(*) FROM recall", [], |r| r.get(0))
.unwrap_or(0);
let excess = count - cfg.max_entries as i64;
if excess > 0 {
let _ = conn.execute(
"DELETE FROM recall WHERE rowid IN (
SELECT rowid FROM recall WHERE hash != ?2
ORDER BY created_at ASC, rowid ASC LIMIT ?1
)",
params![excess, keep_hash],
);
}
}
}
pub fn store(
cfg: &RetrieverConfig,
content: &[u8],
command: &str,
exit_code: Option<i32>,
shown_upto: usize,
) -> Stored {
if content.is_empty() {
return Stored::Empty;
}
match store_inner(cfg, content, command, exit_code, shown_upto.max(1)) {
Ok(r) => Stored::Saved(r),
Err(_) => Stored::Unavailable,
}
}
fn store_inner(
cfg: &RetrieverConfig,
content: &[u8],
command: &str,
exit_code: Option<i32>,
shown_upto: usize,
) -> Result<StoredRef> {
let cap = cfg.max_entry_bytes;
let cut_at_line = |slice: &[u8]| {
slice[..cap]
.iter()
.rposition(|&b| b == b'\n')
.map(|i| i + 1)
.unwrap_or(cap)
};
// Over the cap the store keeps the hidden tail rather than the prefix the
// agent already saw, so the hint can never promise more than recall returns.
let (payload, stored_shown_upto, truncated) = if content.len() <= cap {
(content, shown_upto, false)
} else if shown_upto > 1 {
let hidden = slice_from_line(content, shown_upto);
if hidden.len() > cap {
(&hidden[..cut_at_line(hidden)], 1, true)
} else {
(hidden, 1, true)
}
} else {
(&content[..cut_at_line(content)], 1, true)
};
let total_lines = count_lines(payload);
let shown_upto = stored_shown_upto;
let hash = content_hash(command, content);
let (blob, codec): (Vec<u8>, &str) = if cfg.compression {
match gzip(payload) {
Ok(z) => (z, "gzip"),
Err(_) => (payload.to_vec(), "raw"),
}
} else {
(payload.to_vec(), "raw")
};
let cwd = std::env::current_dir()
.ok()
.map(|p| p.to_string_lossy().into_owned());
with_open(cfg, |conn| {
conn.execute(
"INSERT INTO recall
(hash, command, cwd, exit_code, created_at, total_lines, shown_upto, byte_size, truncated, codec, blob)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)
ON CONFLICT(hash) DO UPDATE SET
command = excluded.command,
cwd = excluded.cwd,
exit_code = excluded.exit_code,
created_at = excluded.created_at,
total_lines = excluded.total_lines,
shown_upto = excluded.shown_upto,
byte_size = excluded.byte_size,
truncated = excluded.truncated,
codec = excluded.codec,
blob = excluded.blob",
params![
hash,
command,
cwd,
exit_code,
now_secs(),
total_lines as i64,
shown_upto as i64,
content.len() as i64,
truncated as i64,
codec,
blob
],
)
.context("insert recall row")?;
bump_stat(conn, command, "sqlite", "elisions");
evict(conn, cfg, &hash);
Ok(StoredRef {
hash: hash.clone(),
hidden_lines: total_lines.saturating_sub(shown_upto.saturating_sub(1)),
})
})
}
#[derive(Debug)]
struct Row {
shown_upto: usize,
truncated: bool,
codec: String,
blob: Vec<u8>,
command: String,
hash: String,
}
fn map_row(r: &rusqlite::Row) -> rusqlite::Result<Row> {
Ok(Row {
shown_upto: r.get::<_, i64>(0)? as usize,
truncated: r.get::<_, i64>(1)? != 0,
codec: r.get(2)?,
blob: r.get(3)?,
command: r.get(4)?,
hash: r.get(5)?,
})
}
const SELECT_COLS: &str = "shown_upto, truncated, codec, blob, command, hash";
fn load_by_hash(conn: &Connection, hash: &str) -> Result<Option<Row>> {
let exact = format!("SELECT {SELECT_COLS} FROM recall WHERE hash = ?1");
if let Some(row) = conn.query_row(&exact, params![hash], map_row).optional()? {
return Ok(Some(row));
}
let mut stmt = conn.prepare(
"SELECT hash FROM recall WHERE substr(hash, 1, length(?1)) = ?1 ORDER BY hash ASC",
)?;
let candidates: Vec<String> = stmt
.query_map(params![hash], |r| r.get(0))?
.filter_map(|r| r.ok())
.collect();
match candidates.as_slice() {
[] => Ok(None),
[only] => {
let sql = format!("SELECT {SELECT_COLS} FROM recall WHERE hash = ?1");
Ok(conn.query_row(&sql, params![only], map_row).optional()?)
}
many => anyhow::bail!(
"ambiguous hash prefix '{hash}': matches {}",
many.join(", ")
),
}
}
fn decode(row: &Row) -> Result<Vec<u8>> {
match row.codec.as_str() {
"gzip" => gunzip(&row.blob),
_ => Ok(row.blob.clone()),
}
}
pub struct RecallArgs<'a> {
pub hash: Option<&'a str>,
pub full: bool,
pub from: Option<usize>,
pub lines: Option<usize>,
pub grep: Option<&'a str>,
pub list: bool,
}
pub fn run_recall(args: RecallArgs) -> Result<i32> {
if args.hash.is_none() && !args.list {
eprintln!("rtk recall: provide a <hash> (from a recovery hint) or --list");
return Ok(2);
}
let cfg = Config::load().unwrap_or_default().retriever;
let conn = match open_existing(&cfg) {
Ok(Some(c)) => c,
Ok(None) => {
if args.list {
println!("(no recall entries)");
return Ok(0);
}
eprintln!("rtk recall: no matching entry (try `rtk recall --list`)");
return Ok(1);
}
Err(e) => {
eprintln!("rtk recall: store unavailable: {e}");
return Ok(1);
}
};
if args.list {
return list_entries(&conn);
}
// `args.list` returned above and the usage guard rejected a missing hash,
// so a hash is present here.
let Some(hash) = args.hash.as_ref() else {
return Ok(2);
};
let row = match load_by_hash(&conn, hash) {
Ok(row) => row,
Err(e) => {
eprintln!("rtk recall: {e}");
return Ok(1);
}
};
let Some(row) = row else {
eprintln!("rtk recall: no matching entry (try `rtk recall --list`)");
return Ok(1);
};
let full = decode(&row)?;
let sliced: Vec<u8> = if args.full {
full.clone()
} else if let Some(n) = args.from {
slice_from_line(&full, n).to_vec()
} else if let Some(n) = args.lines {
slice_first_lines(&full, n).to_vec()
} else {
slice_from_line(&full, row.shown_upto).to_vec()
};
let out = match args.grep {
Some(pat) => {
if regex::bytes::Regex::new(pat).is_err() {
eprintln!(
"rtk recall: note: --grep pattern is not a valid regex, matching it literally"
);
}
grep_bytes(&sliced, pat)
}
None => sliced,
};
if out.is_empty() && row.truncated && args.grep.is_none() {
eprintln!(
"rtk recall: the requested lines were lost — this entry was stored truncated at the cap in force at the time"
);
return Ok(1);
}
let stdout = std::io::stdout();
let _ = stdout.lock().write_all(&out);
mark_recalled(&conn, &row.hash, &row.command);
if row.truncated {
eprintln!(
"rtk recall: note: this entry was stored truncated (output exceeded the cap in force at the time)"
);
}
Ok(0)
}
fn list_entries(conn: &Connection) -> Result<i32> {
let mut stmt = conn.prepare(
"SELECT hash, command, total_lines, shown_upto, exit_code, truncated, byte_size \
FROM recall ORDER BY created_at DESC LIMIT 50",
)?;
let rows = stmt.query_map([], |r| {
Ok((
r.get::<_, String>(0)?,
r.get::<_, String>(1)?,
r.get::<_, i64>(2)?,
r.get::<_, i64>(3)?,
r.get::<_, Option<i64>>(4)?,
r.get::<_, i64>(5)?,
r.get::<_, i64>(6)?,
))
})?;
println!(
"{:<14} {:<26} {:>7} {:>7} {:>9} {:>5} TRUNC",
"HASH", "COMMAND", "LINES", "HIDDEN", "ORIG", "EXIT"
);
let mut n = 0;
for row in rows {
let (hash, command, total, shown, exit, truncated, byte_size) = row?;
let hidden = total.saturating_sub(shown.saturating_sub(1)).max(0);
let cmd = if command.chars().count() > 26 {
let head: String = command.chars().take(25).collect();
format!("{head}…")
} else {
command
};
println!(
"{:<14} {:<26} {:>7} {:>7} {:>9} {:>5} {}",
hash,
cmd,
total,
hidden,
crate::core::utils::format_tokens(byte_size.max(0) as usize),
exit.map(|e| e.to_string()).unwrap_or_else(|| "-".into()),
if truncated != 0 { "yes" } else { "" }
);
n += 1;
}
if n == 0 {
println!("(no recall entries)");
}
Ok(0)
}
#[cfg(test)]
mod tests {
use super::*;
fn temp_cfg(dir: &std::path::Path) -> RetrieverConfig {
RetrieverConfig {
database_path: Some(dir.join("recall_test.db")),
..RetrieverConfig::default()
}
}
#[test]
fn test_reset_stats_clears_counters_and_lets_reads_count_again() {
let dir = tempfile::tempdir().unwrap();
let cfg = temp_cfg(dir.path());
let stored = store_inner(&cfg, b"a\nb\nc\n", "vitest", Some(1), 1).unwrap();
let conn = open(&cfg).unwrap();
mark_recalled(&conn, &stored.hash, "vitest");
record_tee_recall_on(&conn, "vitest", "/tmp/vitest.log");
assert!(!stats_snapshot_with(&cfg).unwrap().is_empty());
drop(conn);
reset_stats_with(&cfg).unwrap();
assert!(
stats_snapshot_with(&cfg).unwrap().is_empty(),
"reset must zero every counter"
);
let conn = open(&cfg).unwrap();
mark_recalled(&conn, &stored.hash, "vitest");
let stats = stats_snapshot_with(&cfg).unwrap();
let s = stats
.iter()
.find(|s| s.slug == "vitest" && s.mode == "sqlite")
.expect("a read after the reset counts again");
assert_eq!(s.recalls, 1);
}
#[test]
fn test_reset_stats_keeps_the_stored_output_recoverable() {
let dir = tempfile::tempdir().unwrap();
let cfg = temp_cfg(dir.path());
let stored = store_inner(&cfg, b"a\nb\nc\n", "vitest", Some(1), 1).unwrap();
reset_stats_with(&cfg).unwrap();
let conn = open(&cfg).unwrap();
assert!(
load_by_hash(&conn, &stored.hash).unwrap().is_some(),
"reset clears counters, never the recoverable output"
);
}
#[test]
fn test_reset_stats_never_creates_the_store() {
let dir = tempfile::tempdir().unwrap();
let cfg = temp_cfg(dir.path());
reset_stats_with(&cfg).unwrap();
assert!(!cfg.database_path.as_ref().unwrap().exists());
}
#[test]
fn test_count_lines() {
assert_eq!(count_lines(b""), 0);
assert_eq!(count_lines(b"abc"), 1);
assert_eq!(count_lines(b"a\nb\nc"), 3);
assert_eq!(count_lines(b"a\nb\nc\n"), 3);
assert_eq!(count_lines(b"\n"), 1);
}
#[test]
fn test_slice_from_line() {
let b = b"l1\nl2\nl3\n";
assert_eq!(slice_from_line(b, 1), b);
assert_eq!(slice_from_line(b, 2), b"l2\nl3\n");
assert_eq!(slice_from_line(b, 3), b"l3\n");
assert_eq!(slice_from_line(b, 4), b"");
assert_eq!(slice_from_line(b, 99), b"");
}
#[test]
fn test_slice_first_lines() {
let b = b"l1\nl2\nl3\n";
assert_eq!(slice_first_lines(b, 0), b"");
assert_eq!(slice_first_lines(b, 1), b"l1\n");
assert_eq!(slice_first_lines(b, 2), b"l1\nl2\n");
assert_eq!(slice_first_lines(b, 99), b);
}
#[test]
fn test_content_hash_deterministic() {
let a = content_hash("cmd", b"output");
assert_eq!(a, content_hash("cmd", b"output"));
assert_eq!(a.len(), HASH_HEX_LEN);
assert_ne!(a, content_hash("cmd2", b"output"));
assert_ne!(a, content_hash("cmd", b"output2"));
}
#[test]
fn test_grep_match_all_roundtrips_byte_exact() {
let input = b"a\nb\nc\n".to_vec();
assert_eq!(grep_bytes(&input, "^"), input);
let no_trailing = b"a\nb\nno-eol".to_vec();
assert_eq!(grep_bytes(&no_trailing, "^"), no_trailing);
}
#[test]
fn test_grep_bytes() {
let input = b"alpha\nbeta\ngamma\n";
assert_eq!(grep_bytes(input, "et"), b"beta\n");
assert_eq!(grep_bytes(input, "^g"), b"gamma\n");
}
#[test]
fn test_gzip_roundtrip_arbitrary_bytes() {
let cases: Vec<Vec<u8>> = vec![
b"hello\n".to_vec(),
vec![0xff, 0xfe, 0x00, 0x01, 0x80],
b"crlf\r\nline\r\n".to_vec(),
b"lone\rcr".to_vec(),
"emoji😀漢字".as_bytes().to_vec(),
b"no trailing newline".to_vec(),
(0u8..=255).collect(),
];
for c in cases {
let z = gzip(&c).expect("gzip");
assert_eq!(gunzip(&z).expect("gunzip"), c, "gzip must be byte-exact");
}
}
#[test]
fn test_store_fetch_byte_faithful() {
let dir = tempfile::tempdir().unwrap();
let cfg = temp_cfg(dir.path());
let mut nasty = Vec::new();
nasty.extend_from_slice(b"line1\r\n");
nasty.extend_from_slice(&[0xff, 0x00, 0xfe]);
nasty.extend_from_slice("漢字\n".as_bytes());
nasty.extend_from_slice(b"no-eol-tail");
let stored = store_inner(&cfg, &nasty, "nasty-cmd", Some(0), 1).expect("store");
let conn = open(&cfg).unwrap();
let row = load_by_hash(&conn, &stored.hash).unwrap().expect("row");
assert_eq!(
decode(&row).unwrap(),
nasty,
"stored bytes must round-trip exactly"
);
}
#[test]
fn test_store_fetch_raw_codec() {
let dir = tempfile::tempdir().unwrap();
let cfg = RetrieverConfig {
compression: false,
..temp_cfg(dir.path())
};
let data = vec![0u8, 1, 2, 255, b'\n', b'x'];
let stored = store_inner(&cfg, &data, "c", Some(0), 1).unwrap();
let conn = open(&cfg).unwrap();
let row = load_by_hash(&conn, &stored.hash).unwrap().unwrap();
assert_eq!(row.codec, "raw");
assert_eq!(decode(&row).unwrap(), data);
}
#[test]
fn test_delta_recall_returns_only_missed() {
let dir = tempfile::tempdir().unwrap();
let cfg = temp_cfg(dir.path());
let content = b"i1\ni2\ni3\ni4\ni5\n";
let stored = store_inner(&cfg, content, "list", Some(0), 3).unwrap();
assert_eq!(stored.hidden_lines, 3);
let conn = open(&cfg).unwrap();
let row = load_by_hash(&conn, &stored.hash).unwrap().unwrap();
let full = decode(&row).unwrap();
assert_eq!(slice_from_line(&full, row.shown_upto), b"i3\ni4\ni5\n");
}
#[test]
fn test_truncation_cap_flagged() {
let dir = tempfile::tempdir().unwrap();
let cfg = RetrieverConfig {
max_entry_bytes: 10,
..temp_cfg(dir.path())
};
let big = vec![b'a'; 100];
let stored = store_inner(&cfg, &big, "big", Some(0), 1).unwrap();
let conn = open(&cfg).unwrap();
let row = load_by_hash(&conn, &stored.hash).unwrap().unwrap();
assert!(row.truncated);
assert_eq!(decode(&row).unwrap().len(), 10);
}
#[test]
fn test_hidden_lines_equal_recallable_lines_under_truncation() {
let dir = tempfile::tempdir().unwrap();
let cfg = RetrieverConfig {
max_entry_bytes: 200,
..temp_cfg(dir.path())
};
let content: Vec<u8> = (0..100)
.flat_map(|i| format!("line {i:03} padding\n").into_bytes())
.collect();
let stored = store_inner(&cfg, &content, "cmd", None, 5).unwrap();
let conn = open(&cfg).unwrap();
let row = load_by_hash(&conn, &stored.hash).unwrap().unwrap();
let recallable = count_lines(slice_from_line(&decode(&row).unwrap(), row.shown_upto));
assert_eq!(
stored.hidden_lines, recallable,
"the hint must promise exactly what recall can return"
);
}
#[test]
fn test_truncation_prefers_hidden_over_already_shown() {
let dir = tempfile::tempdir().unwrap();
let cfg = RetrieverConfig {
max_entry_bytes: 400,
..temp_cfg(dir.path())
};
let content: Vec<u8> = (0..200)
.flat_map(|i| format!("line {i:03} padding padding\n").into_bytes())
.collect();
let stored = store_inner(&cfg, &content, "cmd", None, 150).unwrap();
assert!(
stored.hidden_lines > 0,
"a cap that cannot hold the shown prefix must still store the hidden tail"
);
let conn = open(&cfg).unwrap();
let row = load_by_hash(&conn, &stored.hash).unwrap().unwrap();
let delta = slice_from_line(&decode(&row).unwrap(), row.shown_upto).to_vec();
assert_eq!(
count_lines(&delta),
stored.hidden_lines,
"promise must equal what recall returns"
);
assert!(