Skip to main content

tj_core/
lib.rs

1//! tj-core: append-only event log + derived SQLite state for Task Journal.
2
3#![deny(rust_2018_idioms)]
4
5/// On-disk + on-wire schema version for events and packs. Bump when a
6/// breaking change is made to the JSONL event shape or the pack JSON
7/// envelope. Single source of truth across the workspace — never inline.
8pub const SCHEMA_VERSION: &str = "1.0";
9
10/// Build a fresh task identifier of the form `tj-<10 lowercase base32>`.
11///
12/// 50 bits of entropy from the ULID random suffix → birthday-collision
13/// threshold ≈ 33 million tasks per project. The previous 6-char form
14/// only gave ~4096; old IDs remain valid since storage keys are strings.
15pub fn new_task_id() -> String {
16    format!(
17        "tj-{}",
18        &ulid::Ulid::new().to_string()[10..20].to_lowercase()
19    )
20}
21
22#[cfg(test)]
23mod task_id_tests {
24    use super::new_task_id;
25    use std::collections::HashSet;
26
27    #[test]
28    fn new_task_id_has_expected_shape() {
29        let id = new_task_id();
30        assert!(id.starts_with("tj-"), "{id}");
31        assert_eq!(id.len(), 13, "{id}");
32        assert!(
33            id[3..]
34                .chars()
35                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()),
36            "{id}"
37        );
38    }
39
40    #[test]
41    fn new_task_id_unique_over_ten_thousand() {
42        let mut seen = HashSet::with_capacity(10_000);
43        for _ in 0..10_000 {
44            let id = new_task_id();
45            assert!(seen.insert(id.clone()), "collision: {id}");
46        }
47    }
48}
49
50pub mod artifacts;
51pub mod classifier;
52pub mod completeness;
53pub mod consolidate;
54pub mod db;
55pub mod dream;
56pub mod embed;
57pub mod event;
58pub mod finalize;
59pub mod frontmatter;
60pub mod fts;
61pub mod llm;
62pub mod memory;
63pub mod pack;
64pub mod paths;
65pub mod project_hash;
66pub mod recall;
67pub mod reminder;
68pub mod session;
69pub mod session_id;
70pub mod storage;
71pub mod title;
72
73#[cfg(test)]
74mod schema_version_tests {
75    /// Source-level guard: production sites must reference `SCHEMA_VERSION`
76    /// rather than inlining a literal. If you bump the version, do it in
77    /// the const — never in a struct literal.
78    #[test]
79    fn pack_assembler_does_not_inline_schema_version_literal() {
80        let pack_src = include_str!("pack.rs");
81        assert!(
82            !pack_src.contains("schema_version: \""),
83            "pack.rs has an inline schema_version string literal — use crate::SCHEMA_VERSION"
84        );
85    }
86
87    #[test]
88    fn schema_version_matches_event_default() {
89        let evt = crate::event::Event::new(
90            "tj-x",
91            crate::event::EventType::Open,
92            crate::event::Author::User,
93            crate::event::Source::Cli,
94            "x".into(),
95        );
96        assert_eq!(evt.schema_version, super::SCHEMA_VERSION);
97    }
98}