hird 0.2.2

Cross-harness agent work queue and shared assertion memory, backed by one local SQLite database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! Recall: the memory a task arrives with.
//!
//! `mem_search` only helps an agent that thinks to search, and an agent that
//! has just been handed a task does not yet know what it does not know. The
//! queue does: it knows which files this task expects to touch, and it knows
//! which facts were recorded by agents who worked those same files before.
//!
//! So a claim comes back with the assertions that earlier work in this
//! territory produced, each one carrying the reason it surfaced. Nothing is
//! stored for this — recall is derived at read time from the assertion trail,
//! the declared file scopes and [`crate::glob::intersects`], the same exact
//! pattern intersection the collision detector uses. An assertion recorded on
//! `src/config.rs` reaches a task that declared `src/*.rs` without either side
//! having named the other.
//!
//! Territory is not only what an agent declared. The witness records the files
//! that moved under a task whether or not anybody announced them, and those
//! count too — which matters because declaring a scope is exactly the step an
//! agent in a hurry skips, and it would be a poor memory that only remembered
//! the tidy ones.
//!
//! Three things make an assertion relevant, strongest first:
//!
//! 1. it was recorded while working *this* task, before it came back around;
//! 2. it was recorded on a task whose files overlap this one's — declared or
//!    witnessed, on either side;
//! 3. it reads like the title.
//!
//! Superseded assertions never surface: recall is what is true now.

use rusqlite::Connection;

use super::memory::{row_to_assertion, Memory, MemoryQuery, ASSERTION_COLUMNS};
use super::scope::Scopes;
use super::tasks::Tasks;
use super::ProjectScope;
use crate::error::Result;
use crate::glob;
use crate::model::Assertion;

/// How many words of a title are worth searching on.
const MAX_TERMS: usize = 8;

/// Shorter than this and a word carries no signal of its own.
const MIN_TERM_LEN: usize = 3;

/// Words that appear in every other task title.
const STOPWORDS: &[&str] = &[
    "and", "are", "but", "for", "from", "into", "not", "the", "that", "this", "those", "these",
    "with", "when", "why", "how", "its", "our", "out", "over", "under", "onto", "off", "all",
    "any", "can", "has", "have", "was", "were", "will", "should", "must", "then", "than", "there",
    "their", "them", "they", "you", "your",
];

/// Why an assertion was recalled.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecallReason {
    /// Recorded while working this very task, on an earlier pass.
    SameTask,
    /// Recorded on another task that worked an overlapping file.
    SameFiles {
        /// This task's pattern that overlaps.
        pattern: String,
        /// The pattern the earlier task declared, or the path it was seen to
        /// change.
        other_pattern: String,
        /// Whether that path came from the witness rather than a declaration.
        /// An earlier agent that never said where it was going still leaves
        /// its footprint behind, and what it learned is no less relevant for
        /// having gone unannounced.
        observed: bool,
        seq: i64,
        title: String,
    },
    /// The assertion reads like this task's title.
    Wording { terms: Vec<String> },
}

impl RecallReason {
    /// The reason as a sentence, for a model to relay or a human to read.
    pub fn describe(&self) -> String {
        match self {
            RecallReason::SameTask => "recorded while working this task earlier".to_string(),
            RecallReason::SameFiles {
                pattern,
                other_pattern,
                observed,
                seq,
                title,
            } => {
                let where_ = if pattern == other_pattern {
                    pattern.clone()
                } else {
                    format!("{other_pattern}, which your {pattern} overlaps")
                };
                let doing = if *observed {
                    "which changed"
                } else {
                    "working"
                };
                format!("learned on task {seq} ({title}), {doing} {where_}")
            }
            RecallReason::Wording { terms } if terms.is_empty() => {
                "reads like this task's title".to_string()
            }
            RecallReason::Wording { terms } => {
                let listed = terms
                    .iter()
                    .map(|t| format!("\"{t}\""))
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("mentions {listed}")
            }
        }
    }

    /// Rank: lower is a stronger reason to surface the assertion.
    pub(crate) fn strength(&self) -> u8 {
        match self {
            RecallReason::SameTask => 0,
            RecallReason::SameFiles { .. } => 1,
            RecallReason::Wording { .. } => 2,
        }
    }
}

/// One recalled assertion and the reason it came back.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Recalled {
    pub assertion: Assertion,
    pub reason: RecallReason,
    /// The task the assertion was recorded on, as a human refers to it.
    pub task_seq: Option<i64>,
    /// Whether the code this was learned from is still the code on disk.
    ///
    /// Left empty here and filled in by [`crate::footing::decorate`], because
    /// answering it means reading the working tree and this module is not
    /// allowed to. `None` is what a project with no witness keeps.
    pub standing: Option<crate::model::Standing>,
    /// One sentence, when more than one agent has stated this.
    pub corroboration: Option<String>,
}

/// Derives the memory relevant to a task. Owns no table of its own.
pub struct Recall<'a> {
    conn: &'a Connection,
}

impl<'a> Recall<'a> {
    pub(crate) fn new(conn: &'a Connection) -> Recall<'a> {
        Recall { conn }
    }

    /// The assertions worth putting in front of whoever picks up task `seq`,
    /// strongest reason first and at most `limit` of them.
    ///
    /// A limit of zero turns recall off and costs nothing.
    pub fn for_task(&self, seq: i64, limit: usize) -> Result<Vec<Recalled>> {
        if limit == 0 {
            return Ok(Vec::new());
        }
        let task = Tasks::new(self.conn).get(seq)?;
        // What this task said it would touch, plus what it has been seen to
        // touch. Both are patterns as far as the intersection is concerned —
        // a literal path is a pattern with nothing wild in it.
        let mut patterns = Scopes::new(self.conn).for_task(seq)?;
        for observed in super::witness::Witnessed::new(self.conn).touched(seq)? {
            if !patterns.contains(&observed.path) {
                patterns.push(observed.path);
            }
        }

        let mut picked: Vec<Recalled> = Vec::new();
        let mut seen: Vec<String> = Vec::new();
        let take = |candidate: Recalled, picked: &mut Vec<Recalled>, seen: &mut Vec<String>| {
            if seen.contains(&candidate.assertion.id) {
                return;
            }
            seen.push(candidate.assertion.id.clone());
            picked.push(candidate);
        };

        // 1. What this task itself learned, if it has been worked before.
        for assertion in Memory::new(self.conn).for_task(&task.id)? {
            if assertion.superseded_by.is_some() {
                continue;
            }
            take(
                Recalled {
                    assertion,
                    reason: RecallReason::SameTask,
                    task_seq: Some(seq),
                    standing: None,
                    corroboration: None,
                },
                &mut picked,
                &mut seen,
            );
        }

        // 2. What other agents learned in the same files.
        for candidate in self.by_files(&task.id, &task.project, &patterns, limit)? {
            take(candidate, &mut picked, &mut seen);
        }

        // 3. Whatever else reads like the title.
        if picked.len() < limit {
            for candidate in self.by_wording(&task.project, &task.title, limit)? {
                take(candidate, &mut picked, &mut seen);
            }
        }

        picked.sort_by_key(|r| r.reason.strength());
        picked.truncate(limit);
        Ok(picked)
    }

    /// Assertions recorded on tasks whose declared files overlap `patterns`.
    ///
    /// Intersection is decided in Rust rather than in SQL, because SQL cannot
    /// answer "could these two patterns name the same path". Rows arrive newest
    /// first and the walk stops as soon as `limit` distinct assertions match, so
    /// it reads the whole scoped memory only when there is little to find —
    /// which, on a local database of a few thousand rows, is still cheap.
    fn by_files(
        &self,
        task_id: &str,
        project: &str,
        patterns: &[String],
        limit: usize,
    ) -> Result<Vec<Recalled>> {
        if patterns.is_empty() {
            return Ok(Vec::new());
        }
        // Territory is whatever an earlier task said it would touch *or* was
        // seen to touch. The second half is what reaches an agent whose
        // predecessor never declared anything — most of them, in practice —
        // and it needs no cooperation from that predecessor at all. Archived
        // tenures count as seen too: a re-claim moves a task's footprint into
        // the archive, and what round one learned in a file must still reach
        // whoever works that file next.
        let sql = format!(
            "SELECT {ASSERTION_COLUMNS}, t.seq, t.title, p.pattern, p.observed
             FROM assertions a
             JOIN tasks t ON t.id = a.task_id
             JOIN (SELECT task_id, pattern, 0 AS observed, rowid AS ord FROM task_paths
                   UNION ALL
                   SELECT task_id, path, 1 AS observed, rowid AS ord FROM task_changes
                   UNION ALL
                   SELECT tt.task_id, tc.path, 1 AS observed, tc.rowid AS ord
                   FROM tenure_changes tc JOIN task_tenures tt ON tt.id = tc.tenure_id) p
               ON p.task_id = t.id
             WHERE a.project = ?1 AND a.superseded_by IS NULL AND a.task_id <> ?2
             ORDER BY a.created_at DESC, a.rowid DESC, p.observed, p.ord"
        );
        let mut stmt = self.conn.prepare(&sql)?;
        let mut rows = stmt.query(rusqlite::params![project, task_id])?;

        let mut out: Vec<Recalled> = Vec::new();
        while let Some(row) = rows.next()? {
            let assertion = row_to_assertion(row)?;
            if out.iter().any(|r| r.assertion.id == assertion.id) {
                continue;
            }
            let other_seq: i64 = row.get(8)?;
            let other_title: String = row.get(9)?;
            let other_pattern: String = row.get(10)?;
            let observed: i64 = row.get(11)?;
            let Some(mine) = patterns
                .iter()
                .find(|mine| glob::intersects(mine, &other_pattern))
            else {
                continue;
            };
            out.push(Recalled {
                assertion,
                standing: None,
                corroboration: None,
                reason: RecallReason::SameFiles {
                    pattern: mine.clone(),
                    other_pattern,
                    observed: observed == 1,
                    seq: other_seq,
                    title: other_title,
                },
                task_seq: Some(other_seq),
            });
            if out.len() >= limit {
                break;
            }
        }
        Ok(out)
    }

    /// Assertions that read like the task's title.
    fn by_wording(&self, project: &str, title: &str, limit: usize) -> Result<Vec<Recalled>> {
        let terms = title_terms(title);
        if terms.is_empty() {
            return Ok(Vec::new());
        }
        let query = fts_any_of(&terms);
        let hits = Memory::new(self.conn).search(
            &MemoryQuery::new(&query, ProjectScope::Only(project.to_string())).limit(limit),
        )?;
        let seqs = Tasks::new(self.conn).seq_index()?;
        Ok(hits
            .into_iter()
            .map(|assertion| {
                let matched = matching_terms(&assertion, &terms);
                let task_seq = assertion
                    .task_id
                    .as_ref()
                    .and_then(|id| seqs.get(id).copied());
                Recalled {
                    assertion,
                    reason: RecallReason::Wording { terms: matched },
                    task_seq,
                    standing: None,
                    corroboration: None,
                }
            })
            .collect())
    }
}

/// The words of a title worth searching on: long enough, not furniture.
fn title_terms(title: &str) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for word in title.split(|c: char| !c.is_alphanumeric() && c != '_') {
        let word = word.to_lowercase();
        if word.len() < MIN_TERM_LEN || STOPWORDS.contains(&word.as_str()) {
            continue;
        }
        if !out.contains(&word) {
            out.push(word);
        }
        if out.len() == MAX_TERMS {
            break;
        }
    }
    out
}

/// An FTS5 query matching any of `terms`.
///
/// Every term is quoted: they are alphanumeric by construction, but quoting
/// keeps a word like `or` from being read as an operator if the stoplist ever
/// loses one.
fn fts_any_of(terms: &[String]) -> String {
    terms
        .iter()
        .map(|t| format!("\"{t}\""))
        .collect::<Vec<_>>()
        .join(" OR ")
}

/// Which of `terms` actually appear in the assertion, for the "why" line.
fn matching_terms(assertion: &Assertion, terms: &[String]) -> Vec<String> {
    let haystack = format!("{} {}", assertion.content, assertion.tags).to_lowercase();
    terms
        .iter()
        .filter(|t| haystack.contains(t.as_str()))
        .cloned()
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::Db;
    use crate::model::Status;
    use crate::repo::{NewAssertion, OnConflict};
    use std::time::Duration;

    const PROJECT: &str = "/tmp/project";
    const TTL: Duration = Duration::from_secs(900);

    fn db() -> Db {
        Db::open_in_memory().unwrap()
    }

    /// File a task with a declared scope, the way `hird add --path` does.
    fn task(db: &Db, title: &str, paths: &[&str]) -> i64 {
        let seq = db.tasks().create(PROJECT, title, "", 0, "cli").unwrap().seq;
        if !paths.is_empty() {
            let owned: Vec<String> = paths.iter().map(|p| p.to_string()).collect();
            db.scopes()
                .declare(seq, &owned, "cli", OnConflict::Report)
                .unwrap();
        }
        seq
    }

    /// Put a task on record as having changed `paths`, the way a sweep does.
    fn witness(db: &Db, seq: i64, paths: &[&str]) {
        db.tasks().claim(seq, "codex:9f2c", TTL).unwrap();
        db.witnessed()
            .begin(seq, &crate::witness::Tree::default(), "codex:9f2c")
            .unwrap();
        let changes: Vec<crate::witness::Change> = paths
            .iter()
            .map(|path| crate::witness::Change {
                path: path.to_string(),
                kind: crate::witness::ChangeKind::Modified,
                hash: format!("hash-of-{path}"),
            })
            .collect();
        db.witnessed().record(seq, &changes, "codex:9f2c").unwrap();
    }

    fn learn(db: &Db, seq: Option<i64>, content: &str) {
        db.memory()
            .store(NewAssertion {
                project: PROJECT,
                content,
                tags: "",
                actor: "codex:9f2c",
                task_seq: seq,
            })
            .unwrap();
    }

    fn contents(recalled: &[Recalled]) -> Vec<&str> {
        recalled
            .iter()
            .map(|r| r.assertion.content.as_str())
            .collect()
    }

    #[test]
    fn a_fact_learned_in_the_same_files_reaches_the_next_task() {
        let db = db();
        let first = task(&db, "Port the config loader", &["src/config.rs"]);
        learn(&db, Some(first), "config precedence is env over file");
        let second = task(&db, "Audit the loader", &["src/*.rs"]);

        let recalled = db.recall().for_task(second, 5).unwrap();
        assert_eq!(
            contents(&recalled),
            vec!["config precedence is env over file"]
        );
        assert_eq!(recalled[0].task_seq, Some(first));
        let why = recalled[0].reason.describe();
        assert!(why.contains(&format!("task {first}")), "{why}");
        assert!(why.contains("src/config.rs"), "{why}");
    }

    /// Recall used to depend on the earlier agent having declared its scope,
    /// which is the one thing an agent in a hurry skips. The witness closes
    /// that hole: an agent that said nothing and went ahead and edited
    /// `src/config.rs` still leaves the file behind it, and what it learned
    /// still reaches whoever comes next.
    #[test]
    fn a_fact_reaches_the_next_task_through_files_nobody_declared() {
        let db = db();
        let quiet = task(&db, "Port the config loader", &[]);
        witness(&db, quiet, &["src/config.rs"]);
        learn(&db, Some(quiet), "the loader reads HIRD_DB before the file");
        db.tasks().complete(quiet, "codex:9f2c", "ported").unwrap();

        let next = task(&db, "Audit the loader", &["src/*.rs"]);
        let recalled = db.recall().for_task(next, 5).unwrap();
        assert_eq!(
            contents(&recalled),
            vec!["the loader reads HIRD_DB before the file"]
        );
        let why = recalled[0].reason.describe();
        assert!(why.contains("which changed src/config.rs"), "{why}");
    }

    /// And symmetrically: a task that declared nothing is still handed what
    /// earlier work in the files it has wandered into learned.
    #[test]
    fn a_task_with_no_declaration_recalls_through_where_it_has_been() {
        let db = db();
        let earlier = task(&db, "Port the config loader", &["src/config.rs"]);
        learn(&db, Some(earlier), "config precedence is env over file");
        db.tasks().claim(earlier, "codex:9f2c", TTL).unwrap();
        db.tasks()
            .complete(earlier, "codex:9f2c", "ported")
            .unwrap();

        let now = task(&db, "Something unrelated-sounding", &[]);
        assert!(
            db.recall().for_task(now, 5).unwrap().is_empty(),
            "nothing connects the two tasks yet"
        );

        witness(&db, now, &["src/config.rs"]);
        let recalled = db.recall().for_task(now, 5).unwrap();
        assert_eq!(
            contents(&recalled),
            vec!["config precedence is env over file"]
        );
    }

    /// The intersection is exact, not a string comparison: a task declaring
    /// `src/*.rs` must not be handed what was learned about `tests/`.
    #[test]
    fn facts_from_files_this_task_cannot_touch_stay_away() {
        let db = db();
        let other = task(&db, "Rewrite the renderer", &["src/tui/**"]);
        learn(&db, Some(other), "the renderer redraws on every poll");
        let mine = task(&db, "Something else entirely", &["docs/*.md"]);

        assert!(db.recall().for_task(mine, 5).unwrap().is_empty());
    }

    #[test]
    fn what_this_task_learned_before_comes_back_first() {
        let db = db();
        let neighbour = task(&db, "Port the loader", &["src/config.rs"]);
        learn(&db, Some(neighbour), "the loader is in src/config.rs");
        let mine = task(&db, "Finish the loader", &["src/config.rs"]);
        learn(&db, Some(mine), "half of this was already done");

        let recalled = db.recall().for_task(mine, 5).unwrap();
        assert_eq!(
            contents(&recalled),
            vec![
                "half of this was already done",
                "the loader is in src/config.rs"
            ]
        );
        assert_eq!(recalled[0].reason, RecallReason::SameTask);
        assert_eq!(
            recalled[0].reason.describe(),
            "recorded while working this task earlier"
        );
    }

    #[test]
    fn a_fact_that_reads_like_the_title_surfaces_without_any_file_scope() {
        let db = db();
        learn(&db, None, "the renderer redraws on every poll");
        let mine = task(&db, "Fix the renderer", &[]);

        let recalled = db.recall().for_task(mine, 5).unwrap();
        assert_eq!(
            contents(&recalled),
            vec!["the renderer redraws on every poll"]
        );
        assert_eq!(
            recalled[0].reason,
            RecallReason::Wording {
                terms: vec!["renderer".to_string()]
            }
        );
        assert_eq!(recalled[0].reason.describe(), "mentions \"renderer\"");
    }

    #[test]
    fn a_superseded_fact_is_never_recalled() {
        let db = db();
        let first = task(&db, "Port the loader", &["src/config.rs"]);
        let stale = db
            .memory()
            .store(NewAssertion {
                project: PROJECT,
                content: "the loader reads config.json",
                tags: "",
                actor: "a:1",
                task_seq: Some(first),
            })
            .unwrap();
        db.memory()
            .supersede(&stale.id, "the loader reads config.toml", "cli")
            .unwrap();
        let mine = task(&db, "Audit the loader", &["src/config.rs"]);

        let recalled = db.recall().for_task(mine, 5).unwrap();
        assert_eq!(contents(&recalled), vec!["the loader reads config.toml"]);
    }

    #[test]
    fn one_assertion_is_recalled_once_however_many_reasons_it_has() {
        let db = db();
        let first = task(
            &db,
            "Port the config loader",
            &["src/config.rs", "tests/**"],
        );
        // Matches on both declared patterns, and on the title's wording.
        learn(
            &db,
            Some(first),
            "the config loader is tested in tests/config.rs",
        );
        let mine = task(&db, "Port the config loader again", &["src/**", "tests/**"]);

        let recalled = db.recall().for_task(mine, 5).unwrap();
        assert_eq!(recalled.len(), 1);
    }

    #[test]
    fn the_limit_is_honoured_and_zero_turns_recall_off() {
        let db = db();
        let first = task(&db, "Port the loader", &["src/config.rs"]);
        for i in 0..5 {
            learn(&db, Some(first), &format!("fact {i} about the loader"));
        }
        let mine = task(&db, "Audit the loader", &["src/config.rs"]);

        assert_eq!(db.recall().for_task(mine, 2).unwrap().len(), 2);
        assert!(db.recall().for_task(mine, 0).unwrap().is_empty());
    }

    /// Recall reaches into finished work — that is the whole point — but never
    /// across projects, where the files mean something else entirely.
    #[test]
    fn recall_crosses_time_but_not_projects() {
        let db = db();
        let done = task(&db, "Port the loader", &["src/config.rs"]);
        learn(&db, Some(done), "the loader needs HIRD_DB set");
        db.tasks()
            .claim(done, "codex:9f2c", Duration::from_secs(60))
            .unwrap();
        db.tasks().complete(done, "codex:9f2c", "ported").unwrap();
        assert_eq!(db.tasks().get(done).unwrap().status, Status::Done);

        let elsewhere = db
            .tasks()
            .create("/other/project", "Audit the loader", "", 0, "cli")
            .unwrap()
            .seq;
        db.scopes()
            .declare(
                elsewhere,
                &["src/config.rs".to_string()],
                "cli",
                OnConflict::Report,
            )
            .unwrap();

        let mine = task(&db, "Audit the loader", &["src/config.rs"]);
        assert_eq!(
            contents(&db.recall().for_task(mine, 5).unwrap()),
            vec!["the loader needs HIRD_DB set"]
        );
        assert!(db.recall().for_task(elsewhere, 5).unwrap().is_empty());
    }

    #[test]
    fn titles_lose_their_furniture_words() {
        assert_eq!(
            title_terms("Port the config loader to serde"),
            vec!["port", "config", "loader", "serde"]
        );
        // `to` and `a` are too short, `the` is a stopword: nothing is left.
        assert!(title_terms("To a the").is_empty());
    }

    #[test]
    fn terms_are_quoted_into_an_any_of_query() {
        assert_eq!(
            fts_any_of(&["config".to_string(), "loader".to_string()]),
            "\"config\" OR \"loader\""
        );
    }

    /// A title full of punctuation must not produce a query FTS5 rejects.
    #[test]
    fn a_punctuation_heavy_title_still_searches() {
        let db = db();
        learn(&db, None, "handle_event takes the context");
        let mine = task(&db, "Fix handle_event(ctx) — again!", &[]);
        let recalled = db.recall().for_task(mine, 5).unwrap();
        assert_eq!(contents(&recalled), vec!["handle_event takes the context"]);
    }

    #[test]
    fn a_task_with_nothing_around_it_recalls_nothing() {
        let db = db();
        let mine = task(&db, "Xyzzy", &["src/xyzzy.rs"]);
        assert!(db.recall().for_task(mine, 5).unwrap().is_empty());
    }

    #[test]
    fn recalling_a_task_that_does_not_exist_says_so() {
        let db = db();
        let err = db.recall().for_task(404, 5).unwrap_err();
        assert_eq!(err.to_string(), "task 404 not found");
    }
}