rigger 0.12.0

One seat for all your projects and tasks: a local record of what is done, what is next and when it ships - read by you and your coding assistant
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
//! The context packet: what an assistant needs to start a session.
//!
//! The packet exists because the alternative - reading a project's hub - costs
//! tens of thousands of tokens and, past a certain size, no longer fits at
//! all. So the packet has a budget, and the budget is a gate: when the record
//! does not fit, the packet drops the oldest events and says how many it left
//! out, rather than silently truncating.
//!
//! Sections are ordered by what a session needs first: where the project
//! stands, what is being built now, what waits for the owner, what happened
//! recently, and the one line the last session left behind.

use anyhow::Result;
use serde::Serialize;

use crate::db::{Db, Project, Task};

/// Tokens are approximated from characters. A tokeniser would be exact for
/// one model and wrong for the next, and this number decides only how much
/// history to include - four characters per token is the usual rule of thumb
/// for English, and Cyrillic runs denser, so the estimate errs on the safe
/// side by counting characters, not bytes.
pub fn estimate_tokens(text: &str) -> usize {
    text.chars().count().div_ceil(4)
}

pub const DEFAULT_BUDGET: usize = 3000;

#[derive(Debug, Serialize)]
pub struct Packet {
    pub project: String,
    pub state: State,
    pub current: Option<Stage>,
    pub questions: Vec<Item>,
    pub wishes: Vec<Item>,
    pub events: Vec<Event>,
    pub next_step: Option<String>,
    /// How many recent events the budget left out.
    pub events_omitted: usize,
}

#[derive(Debug, Serialize)]
pub struct State {
    pub path: String,
    pub remote: Option<String>,
    pub last_shipped: Option<String>,
    pub last_shipped_on: Option<String>,
    pub versions_planned: u64,
    pub tasks_open: u64,
    /// Days since anything was recorded about this project. A project that
    /// has gone quiet is worth noticing at the top of a session.
    pub days_quiet: Option<i64>,
    /// Commits since the newest tag, as the last `sync` read them.
    pub commits_since_tag: Option<u32>,
    /// Days since the last commit - the owner's question: how long has this
    /// project actually been still? Quiet in the record and quiet in git are
    /// different things, and only the second one means nobody has worked.
    pub days_since_commit: Option<i64>,
}

#[derive(Debug, Serialize)]
pub struct Stage {
    pub version: String,
    pub title: Option<String>,
    pub tasks: Vec<Task>,
}

#[derive(Debug, Serialize)]
pub struct Item {
    pub id: i64,
    pub text: String,
}

#[derive(Debug, Serialize)]
pub struct Event {
    pub kind: String,
    pub date: String,
    pub body: String,
}

/// What each section of the packet costs, for `--explain`.
#[derive(Debug, Serialize)]
pub struct Cost {
    pub section: &'static str,
    pub tokens: usize,
}

pub fn build(db: &Db, project: &Project, budget: usize) -> Result<Packet> {
    let activity = db.activity(project.id)?;
    let state = State {
        // A place the record keeps for itself has no location, and its path
        // column holds a marker rather than a directory. The packet opens
        // with this line, so a marker there would be the first thing an
        // assistant read and the first thing it got wrong.
        path: match project.kind {
            crate::db::Kind::Repo => project.path.clone(),
            crate::db::Kind::Service => "(no repository - a place the record keeps for itself)".to_string(),
        },
        remote: project.remote.clone(),
        last_shipped: db.last_shipped_version(project.id)?.map(|(name, _)| name),
        last_shipped_on: db.last_shipped_version(project.id)?.map(|(_, on)| on),
        versions_planned: db.count_versions(project.id, "planned")?,
        tasks_open: db.count_open_tasks(project.id)?,
        days_quiet: db.last_event_at(project.id)?.as_deref().and_then(days_since),
        commits_since_tag: activity.as_ref().map(|a| a.commits_since_tag),
        days_since_commit: activity.as_ref().and_then(|a| a.last_commit_at.as_deref()).and_then(days_since_day),
    };

    let current = db.current_stage(project.id)?.map(|s| Stage {
        version: s.version,
        title: s.title,
        tasks: s.tasks,
    });
    let questions = db
        .open_events(project.id, "question")?
        .into_iter()
        .map(|(id, text)| Item { id, text })
        .collect();
    let wishes = db.open_events(project.id, "wish")?.into_iter().map(|(id, text)| Item { id, text }).collect();
    let next_step = db.latest_event_body(project.id, "next")?;

    let mut packet = Packet {
        project: project.name.clone(),
        state,
        current,
        questions,
        wishes,
        events: Vec::new(),
        next_step,
        events_omitted: 0,
    };

    // Everything above is what a session cannot start without, so it is never
    // dropped. Recent events fill whatever budget is left, newest first, and
    // the count of what did not fit is part of the packet.
    //
    // The window is wider than any packet can hold, so that "left out" counts
    // events the budget refused rather than events this query never asked
    // for: a packet that drops history silently is the failure this whole
    // command exists to avoid. Older events beyond the window are the job of
    // `find` and `why`, not of a session's first screen.
    const WINDOW: u32 = 200;
    let recent = db.recent_events(project.id, WINDOW)?;
    let beyond_window = db.count_recent_events(project.id)?.saturating_sub(recent.len() as u64) as usize;

    // The line that says what was dropped costs tokens of its own, and so
    // does the heading above the events. Both are reserved before anything is
    // added, so that a packet never ends up over the budget it reports.
    let reserve = estimate_tokens("\n## Recent\n(999 older events left out by the budget)\n");
    let mut spent = estimate_tokens(&render(&packet)) + reserve;
    packet.events_omitted = beyond_window;

    // Changes read from commits get a share of what is left; everything
    // written by a person competes for the rest.
    //
    // Without this the chronicle crowds out the reasoning: kasl's packet came
    // back as seventy commit lines against nineteen decisions, with ninety-one
    // events dropped. A commit can always be read again in git; a decision or
    // a pitfall exists nowhere else, and losing one costs a session the
    // argument behind the code it is about to change.
    let mut git_left = budget.saturating_sub(spent) / 4;

    for recent in recent {
        let from_git = recent.from_git;
        let event = Event {
            kind: recent.kind,
            date: recent.date,
            body: summarise(&recent.body),
        };
        let cost = estimate_tokens(&render_event(&event));
        if spent + cost > budget || (from_git && cost > git_left) {
            packet.events_omitted += 1;
            continue;
        }
        if from_git {
            git_left -= cost;
        }
        spent += cost;
        packet.events.push(event);
    }
    Ok(packet)
}

/// The line of an event a session needs, and a pointer to the rest.
///
/// A decision in these hubs averages 1500 characters and reaches 5600 - it is
/// a document, not a note. Three of them at full length crowd out ten others,
/// and a session that needs the whole argument can read it with `why` later.
/// So the packet keeps the heading and the first sentence of the reasoning,
/// which is where these entries state what was decided.
fn summarise(text: &str) -> String {
    let mut out = String::new();
    for para in text.split("\n\n").filter(|p| !p.trim().is_empty()) {
        let para = para.trim().replace('\n', " ");
        if out.is_empty() {
            out = para;
            continue;
        }
        // The heading alone rarely says what was decided; the paragraph after
        // it does. Two is enough, and the second is cut to one sentence.
        out.push_str("");
        out.push_str(&first_sentence(&para));
        break;
    }
    let full = text.chars().count();
    let kept = out.chars().count();
    if full > kept + 40 {
        out.push_str(&format!(" (+{} chars)", full - kept));
    }
    out
}

/// The first sentence, or the whole text when it has none within reach.
fn first_sentence(text: &str) -> String {
    let mut end = None;
    for (i, c) in text.char_indices() {
        if matches!(c, '.' | '!' | '?')
            && text[i + c.len_utf8()..].starts_with(|n: char| n.is_whitespace())
            // An abbreviation or a version number is not a sentence end.
            && !text[..i].ends_with(|p: char| p.is_ascii_digit())
        {
            end = Some(i + c.len_utf8());
            break;
        }
    }
    match end {
        Some(end) if end <= 400 => text[..end].to_string(),
        _ => shorten(text, 400),
    }
}

/// "today", "yesterday" or "N days ago" - a session reads a word faster
/// than it reads a number it has to subtract from the date.
fn days_ago(days: i64) -> String {
    match days {
        0 => "today".to_string(),
        1 => "yesterday".to_string(),
        n => format!("{n} days ago"),
    }
}

/// Whole days between a recorded day (`YYYY-MM-DD`) and today.
fn days_since_day(day: &str) -> Option<i64> {
    days_since(&format!("{day}T00:00:00Z"))
}

/// Whole days between a recorded timestamp and now.
fn days_since(timestamp: &str) -> Option<i64> {
    let then: jiff::Timestamp = timestamp.parse().ok()?;
    let seconds = jiff::Timestamp::now().as_second() - then.as_second();
    Some((seconds / 86_400).max(0))
}

/// Trims at a word boundary, saying how much was left out.
fn shorten(text: &str, limit: usize) -> String {
    if text.chars().count() <= limit {
        return text.to_string();
    }
    let cut: String = text.chars().take(limit).collect();
    let cut = match cut.rsplit_once(char::is_whitespace) {
        Some((head, _)) => head.to_string(),
        None => cut,
    };
    format!("{cut}")
}

pub fn costs(packet: &Packet) -> Vec<Cost> {
    let mut costs = vec![
        Cost {
            section: "state",
            tokens: estimate_tokens(&render_state(packet)),
        },
        Cost {
            section: "current stage",
            tokens: packet.current.as_ref().map(|s| estimate_tokens(&render_stage(s))).unwrap_or(0),
        },
        Cost {
            section: "questions",
            tokens: estimate_tokens(&render_items("", &packet.questions)),
        },
        Cost {
            section: "wishes",
            tokens: estimate_tokens(&render_items("", &packet.wishes)),
        },
        Cost {
            section: "events",
            tokens: packet.events.iter().map(|e| estimate_tokens(&render_event(e))).sum(),
        },
        Cost {
            section: "next step",
            tokens: packet.next_step.as_deref().map(estimate_tokens).unwrap_or(0),
        },
    ];
    costs.retain(|c| c.tokens > 0);
    costs
}

fn render_state(p: &Packet) -> String {
    let mut out = format!("# {}\n\n{}\n", p.project, p.state.path);
    if let Some(remote) = &p.state.remote {
        out.push_str(remote);
        out.push('\n');
    }
    match (&p.state.last_shipped, &p.state.last_shipped_on) {
        (Some(v), Some(on)) => out.push_str(&format!("Last shipped: {v} on {on}\n")),
        _ => out.push_str("Nothing shipped yet\n"),
    }
    out.push_str(&format!("{} versions planned, {} tasks open\n", p.state.versions_planned, p.state.tasks_open));
    // What git says, which is a different question from what the record
    // says: a project can be busy in commits and silent in events, and the
    // owner's question is how long it has actually been still.
    match (p.state.commits_since_tag, p.state.days_since_commit) {
        (Some(0), Some(days)) if days > 0 => out.push_str(&format!("Nothing committed since the last release, {}\n", days_ago(days))),
        (Some(commits), Some(days)) if commits > 0 => {
            let plural = if commits == 1 { "commit" } else { "commits" };
            out.push_str(&format!("{commits} {plural} since the last release, the last one {}\n", days_ago(days)))
        }
        _ => {}
    }
    // Said only when it means something. A day or two of quiet is the normal
    // rhythm of a project; a fortnight is worth seeing before starting work.
    if let Some(days) = p.state.days_quiet
        && days >= 7
    {
        out.push_str(&format!("Quiet for {days} days\n"));
    }
    out
}

fn render_stage(stage: &Stage) -> String {
    let mut out = format!("\n## Current stage: {}", stage.version);
    if let Some(title) = &stage.title {
        out.push_str(&format!(" · {title}"));
    }
    out.push('\n');
    for task in &stage.tasks {
        out.push_str(&format!("- {}\n", task.title));
    }
    out
}

fn render_items(heading: &str, items: &[Item]) -> String {
    if items.is_empty() {
        return String::new();
    }
    let mut out = if heading.is_empty() { String::new() } else { format!("\n## {heading}\n") };
    for item in items {
        out.push_str(&format!("- [{}] {}\n", item.id, item.text));
    }
    out
}

fn render_event(event: &Event) -> String {
    format!("- {} · {} · {}\n", event.date, event.kind, event.body)
}

/// The packet as the assistant reads it.
pub fn render(packet: &Packet) -> String {
    let mut out = render_state(packet);
    if let Some(stage) = &packet.current {
        out.push_str(&render_stage(stage));
    }
    out.push_str(&render_items("Waiting for the owner", &packet.questions));
    out.push_str(&render_items("Wishes, not yet sorted", &packet.wishes));
    if !packet.events.is_empty() || packet.events_omitted > 0 {
        out.push_str("\n## Recent\n");
        for event in &packet.events {
            out.push_str(&render_event(event));
        }
        // Said even when nothing fit at all: a section that is simply absent
        // reads as "nothing happened", which is the opposite of the truth.
        if packet.events_omitted > 0 {
            out.push_str(&format!("({} older events left out by the budget)\n", packet.events_omitted));
        }
    }
    if let Some(next) = &packet.next_step {
        out.push_str(&format!("\n## Next step\n{next}\n"));
    }
    out
}

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

    #[test]
    fn a_short_body_is_left_alone() {
        assert_eq!(shorten("short enough", 600), "short enough");
    }

    #[test]
    fn a_long_body_is_cut_at_a_word_boundary() {
        let text = "word ".repeat(200);
        let cut = shorten(&text, 50);
        assert!(cut.len() < text.len());
        assert!(cut.trim_end_matches('').trim_end().ends_with("word"), "{cut}");
    }

    #[test]
    fn a_summary_keeps_the_heading_and_the_first_sentence_of_the_reason() {
        let text =
            "**The record is the database.**\n\nProse cannot be filtered. And a second sentence that the packet does not need.\n\nA third paragraph entirely.";
        let summary = summarise(text);
        assert!(summary.starts_with("**The record is the database.**"), "{summary}");
        assert!(summary.contains("Prose cannot be filtered."), "{summary}");
        assert!(!summary.contains("second sentence"), "{summary}");
        assert!(summary.ends_with("chars)"), "{summary}");
    }

    #[test]
    fn a_short_event_is_kept_whole_without_a_pointer() {
        let summary = summarise("Renamed the npm package.");
        assert_eq!(summary, "Renamed the npm package.");
    }

    #[test]
    fn a_version_number_does_not_end_a_sentence() {
        // "v0.3.0" would otherwise cut the sentence at its first dot.
        let sentence = first_sentence("The rule holds since v0.3.0 for every project. And then more.");
        assert_eq!(sentence, "The rule holds since v0.3.0 for every project.");
    }

    #[test]
    fn tokens_are_counted_in_characters_not_bytes() {
        // Cyrillic is two bytes per character; counting bytes would double
        // the estimate and shrink every packet of a Russian-language hub.
        assert_eq!(estimate_tokens("решение"), 2);
        assert_eq!(estimate_tokens("abcd"), 1);
        assert_eq!(estimate_tokens("abcde"), 2);
    }
}