ljos 0.41.6

Memory, a work tracker and a shell-command gate for coding runners (Claude Code, Codex, Grok Build, agy, opencode) over MCP and hooks.
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
//! A day's work: one parent and its checklist, filed in one tracker commit.
//!
//! `ljos sitting --plan` and `ljos_start` with an items list use this. The
//! parent is claimed by the sitting. `ljos ready --mine` and the sitting
//! name the next unfinished child. `ljos finish` on a child ticks it off.

use anyhow::{bail, Context, Result};

/// One filed plan: the parent, the children in the order they were given,
/// and the text a seat reads.
pub struct Opened {
    pub parent: String,
    pub children: Vec<(String, String)>,
    pub text: String,
}

/// File `title` and `items` in one tracker commit.
///
/// The parent carries the `plan` tag and `:ORDERED: t`. Each item is a
/// `checklist` child, appended in the order given. The text is the
/// checklist, the next item, and the commit line.
///
/// # Errors
///
/// The title or the items are blank, or the tracker cannot be written.
pub fn open_plan(project: Option<&str>, title: &str, items: &[String]) -> Result<Opened> {
    let title = title.trim();
    if title.is_empty() {
        bail!("plan: the title is empty");
    }
    let items = clean_items(items)?;
    let project = named_project(project);
    let _held = Batch::enter();
    let layout = vissue_core::Layout::resolve(None, None)?;
    let parent = create(&layout, &project, title, None, "plan")?;
    mark_ordered(&parent)?;
    let mut children = Vec::with_capacity(items.len());
    for item in &items {
        let id = create(&layout, &project, item, Some(parent.as_str()), "checklist")?;
        children.push((id, item.clone()));
    }
    let mut ids = vec![parent.clone()];
    ids.extend(children.iter().map(|(id, _)| id.clone()));
    for id in &ids {
        let _ = crate::persist_tracker(id, "filed");
    }
    let committed = crate::flush_tracker_commits();
    let text = render(&parent, title, &children, &committed);
    Ok(Opened {
        parent,
        children,
        text,
    })
}

/// File `items` under an existing parent, in one tracker commit.
///
/// # Errors
///
/// The parent is not a ticket, the items are blank, or the tracker cannot
/// be written.
pub fn add_items(parent: &str, items: &[String]) -> Result<Opened> {
    let parent = parent.trim();
    if parent.is_empty() {
        bail!("plan: the parent id is empty");
    }
    let items = clean_items(items)?;
    let layout = vissue_core::Layout::resolve(None, None)?;
    let hit = vissue_core::Router::load(layout.clone())
        .context("plan: the tracker could not be read")?
        .find_by_id(parent)
        .with_context(|| format!("plan: {parent} is not a ticket"))?;
    let project = hit.project.clone();
    let title = issue_title(parent).unwrap_or_else(|| parent.to_string());
    let _held = Batch::enter();
    if !is_plan(parent) {
        mark_ordered(parent)?;
    }
    let mut children = Vec::with_capacity(items.len());
    for item in &items {
        let id = create(&layout, &project, item, Some(parent), "checklist")?;
        children.push((id, item.clone()));
    }
    for (id, _) in &children {
        let _ = crate::persist_tracker(id, "filed");
    }
    let committed = crate::flush_tracker_commits();
    let text = render(parent, &title, &children, &committed);
    Ok(Opened {
        parent: parent.to_string(),
        children,
        text,
    })
}

/// The next unfinished child of a plan this seat holds.
///
/// Empty of plans: one line that says so.
///
/// # Errors
///
/// The tracker cannot be read.
pub fn ready_mine() -> Result<String> {
    let layout = vissue_core::Layout::resolve(None, None)?;
    layout.require_tracker()?;
    let recs =
        vissue_core::catalog::load_recs(&layout).context("ready: the tracker could not be read")?;
    let seat = crate::canonical_seat_name(&crate::seat_name());
    let mut out = String::new();
    for rec in &recs {
        if !held_by(&rec.heading, &seat) || !plan_heading(&rec.heading) {
            continue;
        }
        match next_open(&recs, &rec.heading.id) {
            Some((id, title)) => out.push_str(&format!("next: {id}  {title}\n")),
            None => out.push_str(&format!("{}  plan complete\n", rec.heading.id)),
        }
    }
    if out.is_empty() {
        out.push_str("no unfinished item\n");
    }
    Ok(out)
}

/// The next line a sitting prints for `issue`, when it is a plan or one of
/// its items. Empty for any other ticket.
#[must_use]
pub fn next_item_line(issue: &str) -> String {
    let Ok(recs) = load_recs() else {
        return String::new();
    };
    let Some(parent) = plan_root(&recs, issue) else {
        return String::new();
    };
    match next_open(&recs, &parent) {
        Some((id, title)) => format!("next: {id}  {title}\n"),
        None => "plan complete\n".to_string(),
    }
}

/// Mark a checklist item done and name the next one.
///
/// `None` when `issue` is not a checklist item, so an ordinary finish
/// proceeds. A checklist item has no claim of its own: the parent is what
/// the sitting holds.
///
/// # Errors
///
/// The item is a checklist child and the tracker cannot record the tick.
pub fn tick_checklist(issue: &str) -> Result<Option<String>> {
    let recs = match load_recs() {
        Ok(recs) => recs,
        Err(_) => return Ok(None),
    };
    if !checklist_heading(heading_of(&recs, issue)) {
        return Ok(None);
    }
    let layout = vissue_core::Layout::resolve(None, None)?;
    vissue_core::ops::update(&layout, issue, Some("DONE"), None, None, None)
        .with_context(|| format!("finish: could not tick {issue}"))?;
    let next = next_item_line(issue);
    let persisted = crate::persist_tracker(issue, "ticked");
    Ok(Some(format!("ticked {issue}\n{next}{persisted}")))
}

fn clean_items(items: &[String]) -> Result<Vec<String>> {
    let items: Vec<String> = items
        .iter()
        .map(|item| item.trim().to_string())
        .filter(|item| !item.is_empty())
        .collect();
    if items.is_empty() {
        bail!("plan: name at least one item after --");
    }
    Ok(items)
}

fn named_project(project: Option<&str>) -> String {
    if let Some(name) = project.map(str::trim).filter(|name| !name.is_empty()) {
        return name.to_string();
    }
    if let Ok(said) = crate::run_captured("vissue", &["identity"]) {
        if let Some(prefix) = said.stdout.lines().find_map(|line| {
            let value = line.trim().strip_prefix("prefix=")?.trim();
            (!value.is_empty()).then(|| value.to_string())
        }) {
            return prefix;
        }
    }
    "Software".to_string()
}

fn create(
    layout: &vissue_core::Layout,
    project: &str,
    title: &str,
    parent: Option<&str>,
    tag: &str,
) -> Result<String> {
    let dir = layout.project_issues_path(project);
    if let Some(parent_dir) = dir.parent() {
        std::fs::create_dir_all(parent_dir)
            .with_context(|| format!("plan: could not make {}", parent_dir.display()))?;
    }
    let text = vissue_core::ops::create(
        layout,
        project,
        title,
        vissue_core::ops::CreateOpts {
            issue_type: Some("task"),
            tags: Some(tag),
            parent,
            quiet: true,
            ..vissue_core::ops::CreateOpts::default()
        },
    )
    .with_context(|| format!("plan: could not file {title:?}"))?;
    let id = text.trim().to_string();
    if id.is_empty() || !id.contains('-') {
        bail!("plan: filing {title:?} printed no id");
    }
    Ok(id)
}

fn mark_ordered(id: &str) -> Result<()> {
    let layout = vissue_core::Layout::resolve(None, None)?;
    let hit = vissue_core::Router::load(layout)
        .context("plan: the tracker could not be read")?
        .find_by_id(id)
        .with_context(|| format!("plan: {id} is not a ticket"))?;
    let path = hit.path.clone();
    let project = hit.project.clone();
    vissue_core::store::with_issues_lock(&path, || {
        let mut doc = vissue_core::IssueDoc::parse_file(&project, &path)?;
        let heading = doc
            .headings
            .iter_mut()
            .find(|heading| heading.id == id)
            .ok_or_else(|| vissue_core::Error::IssueNotFound { id: id.to_string() })?;
        vissue_core::props::insert(&mut heading.properties, "ORDERED", "t".to_string());
        if !heading.org_tags.iter().any(|tag| tag == "plan") {
            heading.org_tags.push("plan".to_string());
        }
        doc.write()?;
        Ok(())
    })
    .with_context(|| format!("plan: could not mark {id} ordered"))?;
    Ok(())
}

fn render(parent: &str, title: &str, children: &[(String, String)], committed: &str) -> String {
    let mut out = format!("{parent}  {title}\n");
    for (id, item) in children {
        out.push_str(&format!("  {id}  {item}\n"));
    }
    if let Some((id, item)) = children.first() {
        out.push_str(&format!("next: {id}  {item}\n"));
    }
    out.push_str(committed);
    if !out.ends_with('\n') {
        out.push('\n');
    }
    out
}

fn load_recs() -> Result<Vec<vissue_core::views::IssueRec>> {
    let layout = vissue_core::Layout::resolve(None, None)?;
    Ok(vissue_core::catalog::load_recs(&layout)?)
}

fn heading_of<'a>(
    recs: &'a [vissue_core::views::IssueRec],
    id: &str,
) -> Option<&'a vissue_core::IssueHeading> {
    recs.iter()
        .find(|rec| rec.heading.id == id)
        .map(|rec| &rec.heading)
}

fn plan_heading(heading: &vissue_core::IssueHeading) -> bool {
    heading.org_tags.iter().any(|tag| tag == "plan")
        || vissue_core::org::org_property_is_set(&heading.properties, "ORDERED")
}

fn checklist_heading(heading: Option<&vissue_core::IssueHeading>) -> bool {
    heading.is_some_and(|heading| heading.org_tags.iter().any(|tag| tag == "checklist"))
}

fn is_plan(id: &str) -> bool {
    load_recs()
        .ok()
        .and_then(|recs| heading_of(&recs, id).map(plan_heading))
        .unwrap_or(false)
}

fn held_by(heading: &vissue_core::IssueHeading, seat: &str) -> bool {
    if seat.is_empty() || heading.state != "STARTED" {
        return false;
    }
    heading
        .claimed_by()
        .is_some_and(|who| crate::canonical_seat_name(who) == seat)
}

/// The plan `issue` belongs to: itself when it is the parent, its parent
/// when it is a checklist item.
fn plan_root(recs: &[vissue_core::views::IssueRec], issue: &str) -> Option<String> {
    let heading = heading_of(recs, issue)?;
    if plan_heading(heading) {
        return Some(issue.to_string());
    }
    if !checklist_heading(Some(heading)) {
        return None;
    }
    let parent = heading.parent()?.to_string();
    plan_heading(heading_of(recs, &parent)?).then_some(parent)
}

fn next_open(recs: &[vissue_core::views::IssueRec], parent: &str) -> Option<(String, String)> {
    let mut children: Vec<&vissue_core::views::IssueRec> = recs
        .iter()
        .filter(|rec| rec.heading.parent() == Some(parent))
        .collect();
    children.sort_by(|a, b| {
        a.heading
            .line_start
            .cmp(&b.heading.line_start)
            .then(a.heading.id.cmp(&b.heading.id))
    });
    children.into_iter().find_map(|rec| {
        if matches!(rec.heading.state.as_str(), "DONE" | "CANCELLED") {
            None
        } else {
            Some((rec.heading.id.clone(), rec.heading.title.clone()))
        }
    })
}

fn issue_title(id: &str) -> Option<String> {
    load_recs()
        .ok()
        .and_then(|recs| heading_of(&recs, id).map(|heading| heading.title.clone()))
}

/// Holds tracker commits until drop, so a plan that fails still commits
/// what it managed to file, and a plan that flushes does not commit twice.
struct Batch;

impl Batch {
    fn enter() -> Self {
        crate::batch_tracker_commits();
        Self
    }
}

impl Drop for Batch {
    fn drop(&mut self) {
        let _ = crate::flush_tracker_commits();
    }
}

/// The issues file of `project`, for tests that read the order back.
#[cfg(test)]
fn issues_file(root: &std::path::Path, project: &str) -> std::path::PathBuf {
    let layout = vissue_core::Layout::new(root, "Software");
    layout.project_issues_path(project)
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::*;

    fn git(root: &Path, args: &[&str]) {
        let out = std::process::Command::new("git")
            .arg("-C")
            .arg(root)
            .args(args)
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "git {args:?}: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }

    fn log_subjects(root: &Path) -> Vec<String> {
        let out = std::process::Command::new("git")
            .arg("-C")
            .arg(root)
            .args(["log", "--format=%s"])
            .output()
            .unwrap();
        String::from_utf8_lossy(&out.stdout)
            .lines()
            .map(str::to_string)
            .collect()
    }

    #[test]
    fn a_plan_is_one_commit_and_the_next_item_advances() {
        let _env = crate::test_env_lock();
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path().join("tracker");
        std::fs::create_dir_all(&root).unwrap();
        git(&root, &["init", "-q"]);
        git(&root, &["config", "user.email", "ada@example.invalid"]);
        git(&root, &["config", "user.name", "ada"]);
        unsafe {
            std::env::set_var("VISSUE_ROOT", &root);
            std::env::set_var("LJOS_SEAT", "ada");
            std::env::remove_var("LJOS_TRACKER_GIT");
            std::env::remove_var("ISSUE_ROOT");
        }
        let items = [
            "measure the files".to_string(),
            "pick a default".to_string(),
            "write the note".to_string(),
        ];
        let opened = open_plan(Some("day"), "Ship the codec", &items).unwrap();
        let subjects = log_subjects(&root);
        let filed: Vec<&String> = subjects
            .iter()
            .filter(|line| line.contains("filed"))
            .collect();
        assert_eq!(filed.len(), 1, "{subjects:?}");
        let subject = filed[0];
        assert!(subject.contains(&opened.parent), "{subject}");
        for (id, _) in &opened.children {
            assert!(subject.contains(id), "{subject}");
            let filed_at = subject.find(id).unwrap();
            let parent_at = subject.find(&opened.parent).unwrap();
            assert!(parent_at < filed_at, "{subject}");
        }
        let first = subject.find(&opened.children[0].0).unwrap();
        let second = subject.find(&opened.children[1].0).unwrap();
        let third = subject.find(&opened.children[2].0).unwrap();
        assert!(first < second && second < third, "{subject}");

        let board = std::fs::read_to_string(issues_file(&root, "day")).unwrap();
        let places: Vec<usize> = items
            .iter()
            .map(|item| board.find(item.as_str()).unwrap())
            .collect();
        assert!(places[0] < places[1] && places[1] < places[2], "{board}");
        assert!(board.find("Ship the codec").unwrap() < places[0], "{board}");
        assert!(board.contains(":ORDERED:"), "{board}");

        assert!(opened
            .text
            .contains(&format!("{}  Ship the codec", opened.parent)));
        for (id, item) in &opened.children {
            assert!(
                opened.text.contains(&format!("{id}  {item}")),
                "{}",
                opened.text
            );
        }
        assert!(
            opened.text.contains(&format!(
                "next: {}  {}",
                opened.children[0].0, opened.children[0].1
            )),
            "{}",
            opened.text
        );

        let layout = vissue_core::Layout::new(&root, "Software");
        vissue_core::ops::update(&layout, &opened.parent, Some("STARTED"), None, None, None)
            .unwrap();
        vissue_core::store::with_issues_lock(&issues_file(&root, "day"), || {
            let path = issues_file(&root, "day");
            let mut doc = vissue_core::IssueDoc::parse_file("day", &path)?;
            let heading = doc
                .headings
                .iter_mut()
                .find(|heading| heading.id == opened.parent)
                .unwrap();
            vissue_core::props::insert(&mut heading.properties, "CLAIMED_BY", "ada".to_string());
            doc.write()?;
            Ok::<(), vissue_core::Error>(())
        })
        .unwrap();
        let mine = ready_mine().unwrap();
        assert_eq!(
            mine,
            format!("next: {}  measure the files\n", opened.children[0].0)
        );

        let ticked = tick_checklist(&opened.children[0].0).unwrap().unwrap();
        assert!(ticked.starts_with(&format!("ticked {}", opened.children[0].0)));
        assert!(
            ticked.contains(&format!("next: {}  pick a default", opened.children[1].0)),
            "{ticked}"
        );
        let ticked = tick_checklist(&opened.children[1].0).unwrap().unwrap();
        assert!(
            ticked.contains(&format!("next: {}  write the note", opened.children[2].0)),
            "{ticked}"
        );
        let ticked = tick_checklist(&opened.children[2].0).unwrap().unwrap();
        assert!(ticked.contains("plan complete"), "{ticked}");
        assert!(tick_checklist("day-nope").unwrap().is_none());
    }
}