tsk-bin 0.25.0

tsk — work with a clear context
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
//! The SessionStart hook scripts: what they start, and what they print. The scripts run with
//! stand-ins for `claude`, `dolt` and `tsk` that log their arguments, one line per run.

use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

fn workspace() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .to_path_buf()
}

fn have(program: &str) -> bool {
    Command::new(program)
        .arg("--version")
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .is_ok()
}

fn write_script(path: &Path, text: &str) {
    std::fs::write(path, text).unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
    }
}

/// A scratch machine: a home, a state directory, stand-in programs and a repository that
/// holds the scripts.
struct Machine {
    root: tempfile::TempDir,
}

impl Machine {
    fn new() -> Option<Machine> {
        if !(have("git") && have("jq") && cfg!(unix)) {
            eprintln!("note: git or jq is missing; skipping");
            return None;
        }
        let machine = Machine {
            root: tempfile::tempdir().unwrap(),
        };
        let bin = machine.bin();
        std::fs::create_dir_all(&bin).unwrap();
        std::fs::create_dir_all(machine.home()).unwrap();
        // `claude` fails for the plugin step named in CLAUDE_FAIL (for example `install`).
        write_script(
            &bin.join("claude"),
            "#!/bin/sh\necho \"$@\" >> \"$CLAUDE_LOG\"\n\
             if [ -n \"${CLAUDE_FAIL:-}\" ] && [ \"$2\" = \"$CLAUDE_FAIL\" ]; then echo \"it broke\" >&2; exit 1; fi\n\
             exit 0\n",
        );
        write_script(
            &bin.join("dolt"),
            "#!/bin/sh\necho \"$@\" >> \"$DOLT_LOG\"\n\
             case \"$1\" in version) echo \"dolt version 2.5.0\" ;; esac\nexit 0\n",
        );
        write_script(
            &bin.join("tsk"),
            "#!/bin/sh\n\
             case \"$1\" in\n\
               --version) echo \"tsk 0.0.0\" ;;\n\
               thread) cat > /dev/null; echo '{\"hookSpecificOutput\":{\"hookEventName\":\"SessionStart\",\"additionalContext\":\"tsk context\"}}' ;;\n\
             esac\nexit 0\n",
        );
        // The repository the repo script runs in: the scripts, and a workspace version.
        let repo = machine.repo();
        std::fs::create_dir_all(repo.join("plugin").join("hooks")).unwrap();
        std::fs::copy(
            workspace()
                .join("plugin")
                .join("hooks")
                .join("install-dolt.sh"),
            repo.join("plugin").join("hooks").join("install-dolt.sh"),
        )
        .unwrap();
        std::fs::write(repo.join("Cargo.toml"), "version = \"0.0.0\"\n").unwrap();
        let init = Command::new("git")
            .args(["init", "-q"])
            .current_dir(&repo)
            .status()
            .unwrap();
        assert!(init.success());
        Some(machine)
    }

    fn bin(&self) -> PathBuf {
        self.root.path().join("bin")
    }

    fn home(&self) -> PathBuf {
        self.root.path().join("home")
    }

    fn repo(&self) -> PathBuf {
        self.root.path().join("repo")
    }

    fn log(&self, name: &str) -> PathBuf {
        self.root.path().join(format!("{}.log", name))
    }

    fn lines(&self, name: &str) -> Vec<String> {
        std::fs::read_to_string(self.log(name))
            .unwrap_or_default()
            .lines()
            .map(str::to_string)
            .collect()
    }

    fn clear(&self) {
        for name in ["claude", "dolt"] {
            let _ = std::fs::remove_file(self.log(name));
        }
    }

    fn command(&self, script: &Path) -> Command {
        let mut command = Command::new("bash");
        command
            .arg(script)
            .current_dir(self.repo())
            .env("HOME", self.home())
            .env("XDG_STATE_HOME", self.home().join("state"))
            .env("TSK_DOLT_SERVER_IDLE_SECS", "3")
            .env_remove("DOLT_ROOT_PATH")
            .env_remove("TSK_PLUGIN_ENSURE_MINUTES")
            .env_remove("CLAUDE_ENV_FILE")
            .env("CLAUDE_LOG", self.log("claude"))
            .env("DOLT_LOG", self.log("dolt"))
            .env(
                "PATH",
                format!(
                    "{}:{}",
                    self.bin().display(),
                    std::env::var("PATH").unwrap_or_default()
                ),
            );
        command
    }

    fn install_dolt(&self) -> String {
        let script = self
            .repo()
            .join("plugin")
            .join("hooks")
            .join("install-dolt.sh");
        let output = self.command(&script).output().unwrap();
        assert!(output.status.success());
        String::from_utf8_lossy(&output.stdout).into_owned()
    }

    /// The repo script with the hook input on its standard input. Returns its output.
    fn repo_hook(&self, envs: &[(&str, &str)]) -> String {
        let script = workspace()
            .join("ops")
            .join("local")
            .join("claude-session-start.sh");
        let mut command = self.command(&script);
        for (name, value) in envs {
            command.env(name, value);
        }
        let mut child = command
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .unwrap();
        {
            use std::io::Write;
            child
                .stdin
                .take()
                .unwrap()
                .write_all(b"{\"session_id\":\"s1\",\"source\":\"resume\"}")
                .unwrap();
        }
        let output = child.wait_with_output().unwrap();
        assert!(output.status.success());
        String::from_utf8_lossy(&output.stdout).into_owned()
    }

    fn write_identity(&self) {
        let dir = self.home().join(".dolt");
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("config_global.json"),
            "{\"user.email\":\"t@example.test\",\"user.name\":\"t\"}",
        )
        .unwrap();
    }

    fn stamp(&self, name: &str) -> PathBuf {
        self.home().join("state").join("tsk").join(name)
    }
}

#[test]
fn the_dolt_installer_checks_the_version_once_and_trusts_its_stamp() {
    let Some(machine) = Machine::new() else {
        return;
    };
    machine.write_identity();
    assert_eq!(machine.install_dolt(), "");
    assert_eq!(machine.lines("dolt"), vec!["version".to_string()]);
    assert!(machine.stamp("dolt-version-ok").is_file());

    // The second run starts no dolt process: the stamp is newer than the binary.
    machine.clear();
    assert_eq!(machine.install_dolt(), "");
    assert_eq!(machine.lines("dolt"), Vec::<String>::new());

    // A binary newer than the stamp is checked again.
    let past = std::time::SystemTime::now() - std::time::Duration::from_secs(3600);
    std::fs::File::options()
        .write(true)
        .open(machine.stamp("dolt-version-ok"))
        .unwrap()
        .set_modified(past)
        .unwrap();
    machine.clear();
    assert_eq!(machine.install_dolt(), "");
    assert_eq!(machine.lines("dolt"), vec!["version".to_string()]);
}

#[test]
fn the_dolt_installer_reads_the_identity_from_the_file_and_asks_dolt_when_it_is_absent() {
    let Some(machine) = Machine::new() else {
        return;
    };
    // No identity file: dolt is asked for each of the two values, and sets them.
    assert_eq!(machine.install_dolt(), "");
    let lines = machine.lines("dolt");
    assert_eq!(lines[0], "version");
    assert!(lines.contains(&"config --global --get user.name".to_string()));
    assert!(lines.contains(&"config --global --get user.email".to_string()));
    assert!(lines.contains(&"config --global --add user.name tsk".to_string()));

    // With the file, neither the name nor the email costs a process.
    machine.write_identity();
    machine.clear();
    assert_eq!(machine.install_dolt(), "");
    assert!(
        !machine
            .lines("dolt")
            .iter()
            .any(|line| line.starts_with("config")),
        "{:?}",
        machine.lines("dolt")
    );
}

#[test]
fn the_repo_script_adds_and_refreshes_the_marketplace_once_for_both_plugins() {
    let Some(machine) = Machine::new() else {
        return;
    };
    machine.write_identity();
    let out = machine.repo_hook(&[]);
    let context: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(
        context["hookSpecificOutput"]["additionalContext"],
        "tsk context"
    );
    assert_eq!(
        machine.lines("claude"),
        vec![
            "plugin marketplace add jimbarritt/claude-plugins",
            "plugin install swe@jimbarritt-claude-plugins --scope project -y",
            "plugin install tsk@jimbarritt-claude-plugins --scope project -y",
            "plugin marketplace update jimbarritt-claude-plugins",
            "plugin update swe@jimbarritt-claude-plugins --scope project",
            "plugin update tsk@jimbarritt-claude-plugins --scope project",
        ]
    );
    assert!(machine.stamp("plugin-ensure-ok").is_file());
}

#[test]
fn the_repo_script_skips_the_plugin_steps_within_the_hour_and_on_demand_runs_them() {
    let Some(machine) = Machine::new() else {
        return;
    };
    machine.write_identity();
    machine.repo_hook(&[]);
    machine.clear();

    // Within the hour nothing is run, and the output is the same.
    let out = machine.repo_hook(&[]);
    assert_eq!(machine.lines("claude"), Vec::<String>::new());
    let context: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(
        context["hookSpecificOutput"]["additionalContext"],
        "tsk context"
    );

    // Zero minutes means every time.
    machine.repo_hook(&[("TSK_PLUGIN_ENSURE_MINUTES", "0")]);
    assert_eq!(machine.lines("claude").len(), 6);

    // A stamp older than the limit runs them again.
    machine.clear();
    let past = std::time::SystemTime::now() - std::time::Duration::from_secs(2 * 3600);
    std::fs::File::options()
        .write(true)
        .open(machine.stamp("plugin-ensure-ok"))
        .unwrap()
        .set_modified(past)
        .unwrap();
    machine.repo_hook(&[]);
    assert_eq!(machine.lines("claude").len(), 6);
}

#[test]
fn a_failed_plugin_step_is_a_warning_in_the_context_and_leaves_no_stamp() {
    let Some(machine) = Machine::new() else {
        return;
    };
    machine.write_identity();
    let out = machine.repo_hook(&[("CLAUDE_FAIL", "install")]);
    let context: serde_json::Value = serde_json::from_str(&out).unwrap();
    let text = context["hookSpecificOutput"]["additionalContext"]
        .as_str()
        .unwrap();
    assert!(text.starts_with("tsk context "), "{}", text);
    assert!(
        text.contains("WARNING: installing the swe plugin failed: it broke"),
        "{}",
        text
    );
    assert!(
        text.contains("WARNING: installing the tsk plugin failed: it broke"),
        "{}",
        text
    );
    // Neither plugin installed, so neither is updated; the marketplace is still refreshed.
    let lines = machine.lines("claude");
    assert!(lines
        .iter()
        .any(|l| l.starts_with("plugin marketplace update")));
    assert!(!lines.iter().any(|l| l.starts_with("plugin update")));
    assert!(!machine.stamp("plugin-ensure-ok").exists());

    // A failed marketplace refresh warns once for each plugin.
    let out = machine.repo_hook(&[("CLAUDE_FAIL", "marketplace")]);
    let context: serde_json::Value = serde_json::from_str(&out).unwrap();
    let text = context["hookSpecificOutput"]["additionalContext"]
        .as_str()
        .unwrap();
    assert!(
        text.contains("WARNING: adding the jimbarritt-claude-plugins marketplace failed: it broke"),
        "{}",
        text
    );
    assert_eq!(
        text.matches("adding the jimbarritt-claude-plugins marketplace failed")
            .count(),
        2
    );
}

/// The SessionEnd script with a stand-in `tsk`. `STATUS` is what `tsk ledger status
/// --local` prints, `STATUS_FAIL` makes it exit 1, and `PUSH_SLEEP` makes
/// `tsk ledger push` run that many seconds. Every call is logged, one line each.
struct EndRun {
    exit_ok: bool,
    calls: Vec<String>,
    seconds: u64,
}

fn session_end(status: &str, status_fails: bool, push_sleep: u32, push_fails: bool) -> EndRun {
    let root = tempfile::tempdir().unwrap();
    let bin = root.path().join("bin");
    std::fs::create_dir_all(&bin).unwrap();
    write_script(
        &bin.join("tsk"),
        "#!/bin/sh\necho \"$@\" >> \"$TSK_LOG\"\n\
         case \"$2\" in\n\
           status) [ -n \"$STATUS_FAIL\" ] && exit 1; printf '%b' \"$STATUS\"; exit 0 ;;\n\
           push) sleep \"$PUSH_SLEEP\"; [ -n \"$PUSH_FAIL\" ] && exit 1; exit 0 ;;\n\
         esac\nexit 0\n",
    );
    let log = root.path().join("tsk.log");
    let script = workspace()
        .join("plugin")
        .join("hooks")
        .join("session-end.sh");
    let started = std::time::Instant::now();
    let mut child = Command::new("bash")
        .arg(script)
        .env(
            "PATH",
            format!(
                "{}:{}",
                bin.display(),
                std::env::var("PATH").unwrap_or_default()
            ),
        )
        .env("TSK_LOG", &log)
        .env("STATUS", status)
        .env("STATUS_FAIL", if status_fails { "1" } else { "" })
        .env("PUSH_SLEEP", push_sleep.to_string())
        .env("PUSH_FAIL", if push_fails { "1" } else { "" })
        .env("TSK_SESSION_END_TIMEOUT_SECS", "1")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    {
        use std::io::Write;
        child
            .stdin
            .take()
            .unwrap()
            .write_all(b"{\"session_id\":\"s1\",\"reason\":\"other\"}")
            .unwrap();
    }
    let output = child.wait_with_output().unwrap();
    assert!(
        output.stdout.is_empty(),
        "{}",
        String::from_utf8_lossy(&output.stdout)
    );
    EndRun {
        exit_ok: output.status.success(),
        calls: std::fs::read_to_string(&log)
            .unwrap_or_default()
            .lines()
            .map(str::to_string)
            .collect(),
        seconds: started.elapsed().as_secs(),
    }
}

const DIRTY: &str =
    "Local   2 changes not pushed\\n  data  missions  1 added\\nOrigin  not checked (--local)\\n";
const CLEAN: &str = "Local   nothing to push\\nOrigin  not checked (--local)\\n";
const AHEAD: &str = "Local   1 commit not pushed\\nOrigin  not checked (--local)\\n";

#[test]
fn the_session_end_script_pushes_when_the_store_holds_unpushed_work() {
    if !(have("bash") && cfg!(unix)) {
        return;
    }
    for status in [DIRTY, AHEAD] {
        let run = session_end(status, false, 0, false);
        assert!(run.exit_ok);
        assert_eq!(
            run.calls,
            vec![
                "ledger status --local".to_string(),
                "ledger push End of session".to_string()
            ]
        );
    }
}

#[test]
fn the_session_end_script_does_not_push_a_clean_store() {
    if !(have("bash") && cfg!(unix)) {
        return;
    }
    let run = session_end(CLEAN, false, 0, false);
    assert!(run.exit_ok);
    assert_eq!(run.calls, vec!["ledger status --local".to_string()]);
}

#[test]
fn the_session_end_script_exits_0_whatever_tsk_does() {
    if !(have("bash") && cfg!(unix)) {
        return;
    }
    // No ledger store: the status fails, nothing is pushed.
    let run = session_end("", true, 0, false);
    assert!(run.exit_ok);
    assert_eq!(run.calls, vec!["ledger status --local".to_string()]);
    // A push that fails.
    let run = session_end(DIRTY, false, 0, true);
    assert!(run.exit_ok);
    assert_eq!(run.calls.len(), 2);
    // A push that hangs is cut off by the timeout.
    if have("timeout") {
        let run = session_end(DIRTY, false, 30, false);
        assert!(run.exit_ok);
        assert_eq!(run.calls.len(), 2);
        assert!(run.seconds < 10, "took {} s", run.seconds);
    }
}

#[test]
fn the_plugin_registers_the_session_end_hook_for_both_harnesses() {
    let claude: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(workspace().join("plugin/hooks/hooks.json")).unwrap(),
    )
    .unwrap();
    let hook = &claude["hooks"]["SessionEnd"][0]["hooks"][0];
    let command = hook["command"].as_str().unwrap();
    assert!(command.ends_with("hooks/session-end.sh\""), "{}", command);
    assert!(hook["timeout"].is_number());
    let copilot: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(workspace().join("plugin/hooks.json")).unwrap(),
    )
    .unwrap();
    let bash = copilot["hooks"]["sessionEnd"][0]["bash"].as_str().unwrap();
    assert!(bash.contains("hooks/session-end.sh"), "{}", bash);
}

/// The Stop script with a stand-in `tsk` that logs its arguments and its input.
fn stop_hook(with_tsk: bool, input: &str) -> (bool, String, String) {
    let root = tempfile::tempdir().unwrap();
    let bin = root.path().join("bin");
    std::fs::create_dir_all(&bin).unwrap();
    let log = root.path().join("tsk.log");
    if with_tsk {
        write_script(
            &bin.join("tsk"),
            "#!/bin/sh\necho \"$@\" >> \"$TSK_LOG\"\n/bin/cat >> \"$TSK_LOG\"\necho '{\"decision\":\"block\"}'\nexit 0\n",
        );
    }
    let script = workspace().join("plugin").join("hooks").join("stop.sh");
    let mut child = Command::new("/bin/bash")
        .arg(script)
        .env("PATH", bin.display().to_string())
        .env("TSK_LOG", &log)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    {
        use std::io::Write;
        child.stdin.take().unwrap().write_all(input.as_bytes()).ok(); // the script may exit without reading its input
    }
    let output = child.wait_with_output().unwrap();
    (
        output.status.success(),
        String::from_utf8_lossy(&output.stdout).into_owned(),
        std::fs::read_to_string(&log).unwrap_or_default(),
    )
}

#[test]
fn the_stop_script_runs_the_guard_with_the_hook_input_and_adds_nothing() {
    let (ok, out, log) = stop_hook(true, "{\"stop_hook_active\":true}");
    assert!(ok);
    assert_eq!(out, "{\"decision\":\"block\"}\n");
    assert_eq!(log, "thread guard\n{\"stop_hook_active\":true}");
}

#[test]
fn the_stop_script_exits_0_without_tsk() {
    let (ok, out, log) = stop_hook(false, "{}");
    assert!(ok);
    assert_eq!(out, "");
    assert_eq!(log, "");
}