tsk-bin 0.29.1

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
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
use std::io::{Read, Write};
use std::path::Path;
use std::time::{Duration, SystemTime};

use serde::{Deserialize, Serialize};

use crate::ledger::location::state_root_from_env;
use crate::ledger::store::RefreshOutcome;
use crate::self_update::{session_notice, upgrade_for_cloud, CloudUpgrade};

use crate::ledger::store::dolt::VERSION_1_REFUSAL;
use crate::mission::model::{Mission, Status};
use crate::mission::records::task_line;

use super::binding::{is_skipped, open_mission_lines, resolve, unbound_prompt};
use super::entry::Entry;
use super::ops::{
    entries_of, facts_of, latest_whats_next, takeover_warning, thread_mission, Facts,
};
use super::session::Session;

pub const ENV_FILE_VAR: &str = "CLAUDE_ENV_FILE";
pub const LEDGER_WT_VAR: &str = "TSK_LEDGER_WT";
pub const CLAIM_DIR: &str = "session-start";
pub const CLAIM_WINDOW: Duration = Duration::from_secs(300);
pub const CLAIM_PRUNE_AGE: Duration = Duration::from_secs(24 * 60 * 60);

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum HookFormat {
    Claude,
    Copilot,
}

#[derive(Deserialize, Default)]
pub struct HookInput {
    #[serde(alias = "sessionId")]
    pub session_id: Option<String>,
    pub source: Option<String>,
}

impl HookInput {
    pub fn parse(bytes: &[u8]) -> HookInput {
        serde_json::from_slice(bytes).unwrap_or_default()
    }

    fn claim_name(&self) -> Option<String> {
        let session_id = self.session_id.as_deref().filter(|v| is_safe_name(v))?;
        let source = self
            .source
            .as_deref()
            .filter(|v| is_safe_name(v))
            .unwrap_or("unknown");
        Some(format!("{}.{}", session_id, source))
    }
}

fn is_safe_name(value: &str) -> bool {
    !value.is_empty()
        && value
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}

#[derive(Debug, PartialEq)]
pub enum Claim {
    First,
    Duplicate,
}

/// Two hooks can run this command for one `SessionStart` event: the plugin's and a
/// repo's own. The first to create the claim file for the session ID and source does
/// the work. Any other run inside `CLAIM_WINDOW` of that claim is a duplicate. A claim
/// older than the window belongs to an earlier event (a second compact, say) and is
/// replaced. Any I/O error counts as `First`, so a broken state directory never
/// suppresses session start.
pub fn claim(dir: &Path, input: &HookInput, now: SystemTime) -> Claim {
    let Some(name) = input.claim_name() else {
        return Claim::First;
    };
    if std::fs::create_dir_all(dir).is_err() {
        return Claim::First;
    }
    prune_claims(dir, now);
    let path = dir.join(name);
    match create_claim(&path) {
        Ok(()) => return Claim::First,
        Err(e) if e.kind() != std::io::ErrorKind::AlreadyExists => return Claim::First,
        Err(_) => {}
    }
    if !is_older_than(&path, now, CLAIM_WINDOW) {
        return Claim::Duplicate;
    }
    let _ = std::fs::remove_file(&path);
    match create_claim(&path) {
        Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Claim::Duplicate,
        _ => Claim::First,
    }
}

fn create_claim(path: &Path) -> std::io::Result<()> {
    std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(path)
        .map(|_| ())
}

fn is_older_than(path: &Path, now: SystemTime, age: Duration) -> bool {
    std::fs::metadata(path)
        .and_then(|m| m.modified())
        .ok()
        .and_then(|modified| now.duration_since(modified).ok())
        .is_some_and(|elapsed| elapsed >= age)
}

fn prune_claims(dir: &Path, now: SystemTime) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if is_older_than(&path, now, CLAIM_PRUNE_AGE) {
            let _ = std::fs::remove_file(path);
        }
    }
}

#[derive(Serialize)]
struct SpecificOutput<'a> {
    #[serde(rename = "hookEventName")]
    hook_event_name: &'a str,
    #[serde(rename = "additionalContext")]
    additional_context: &'a str,
}

#[derive(Serialize)]
struct HookOutput<'a> {
    #[serde(rename = "hookSpecificOutput")]
    hook_specific_output: SpecificOutput<'a>,
}

pub fn hook_output(context: &str) -> String {
    serde_json::to_string(&HookOutput {
        hook_specific_output: SpecificOutput {
            hook_event_name: "SessionStart",
            additional_context: context,
        },
    })
    .unwrap_or_default()
}

#[derive(Serialize)]
struct CopilotOutput<'a> {
    #[serde(rename = "additionalContext")]
    additional_context: &'a str,
}

pub fn copilot_output(context: &str) -> String {
    serde_json::to_string(&CopilotOutput {
        additional_context: context,
    })
    .unwrap_or_default()
}

pub fn format_output(format: HookFormat, context: &str) -> String {
    match format {
        HookFormat::Claude => hook_output(context),
        HookFormat::Copilot => copilot_output(context),
    }
}

pub fn fetched_context(
    path: &Path,
    read_hint: &str,
    pending: &PendingNote,
    thread_message: &str,
) -> String {
    let mut context = format!(
        "The ledger was fetched and materialised at {} (also exported as ${}).",
        path.display(),
        LEDGER_WT_VAR,
    );
    if !read_hint.is_empty() {
        push_part(&mut context, read_hint);
    }
    if !pending.lines.is_empty() {
        push_part(&mut context, &pending_warning(pending));
    }
    push_part(&mut context, thread_message);
    context
}

/// Add a part after a single space, or directly after a line break when the context ends
/// with one, so that a list of lines never runs into the next sentence.
fn push_part(context: &mut String, part: &str) {
    if !context.ends_with('\n') {
        context.push(' ');
    }
    context.push_str(part);
}

/// Said when the session start leaves uncommitted changes of the ledger store in place.
pub const LOCAL_CHANGES_NOTE: &str = "The ledger store holds local changes that are not pushed (records or documents written since the last push). The refresh left them as they are. The next thread start, pause or stop pushes them with its own change, or run `tsk ledger push`.";

/// Said when the ledger holds mission records and the thread is not bound to one.
pub const MISSION_LIST_HINT: &str = "Run `tsk mission list` to see the missions.";

pub struct PendingNote {
    pub remote_ref: String,
    /// The command that shows what the pending commits change, from the backend.
    pub inspect_hint: String,
    pub lines: Vec<String>,
}

impl PendingNote {
    pub fn from_outcome(outcome: &RefreshOutcome, inspect_hint: String) -> PendingNote {
        PendingNote {
            remote_ref: outcome.remote_label.clone(),
            inspect_hint,
            lines: outcome.pending.clone(),
        }
    }
}

fn pending_warning(pending: &PendingNote) -> String {
    format!(
        "WARNING: the ledger worktree holds a commit that is not on {}, so it was left as it is rather than reset: {}. A worker restart can end a turn between a commit and its push, which leaves exactly this state, so this may be your own work from a turn you hold no record of making. Do not assume another actor made it, and do not discard it on that basis. Read what it changes first, with: {}. Then push it with tsk ledger push, or, once you know what it is, run tsk ledger status to see what would go and tsk ledger discard to drop it deliberately.",
        pending.remote_ref,
        pending.lines.join(";"),
        pending.inspect_hint
    )
}

/// What the session start says about the actor this harness instance registered as
/// (`tsk actor register`), after the thread sentence. `label` is `<name> (<kind>)`.
/// Nothing is said when the instance registered none.
pub fn actor_context(label: &str) -> String {
    format!("Actor: {}.", label)
}

/// What the session start says about the thread: the binding to resume, or the prompt
/// that asks which mission to work.
pub fn thread_message(binding: Option<&str>, unbound_prompt: &str) -> String {
    match binding {
        Some(binding) => {
            let thread_id = binding.split_once(':').map(|(_, id)| id).unwrap_or(binding);
            format!(
                "An existing thread binding was found: {}. Run /tsk:resume-thread {} next.",
                binding, thread_id
            )
        }
        None => unbound_prompt.to_string(),
    }
}

/// What the session start says about a bound thread that has no mission, after the
/// binding sentence: it has no tasks, what its latest pause entry says is next, and the
/// command that attaches a mission.
pub fn no_mission_context(latest_whats_next: Option<&str>) -> String {
    let text = latest_whats_next.map_or("none yet", str::trim);
    let stop = if text.ends_with(['.', '!', '?']) {
        ""
    } else {
        "."
    };
    format!(
        "This thread has no mission, so it has no tasks. Its latest what's next: {}{} To attach a mission, run `tsk thread set mission <mission-id>`.",
        text, stop
    )
}

/// Said instead of the unbound prompt when this session or worktree skipped a thread.
pub const SKIPPED_MESSAGE: &str = "This session or worktree works without a thread (tsk thread skip). To bind one, run /tsk:start-thread <mission-id>, or /tsk:resume-thread <thread-id> for an existing thread.";

/// The mission a bound thread works and its open tasks, as `tsk task list --porcelain`
/// prints them. The lines stay tab-separated: they are context for the agent, not for a
/// terminal, and the tabs keep the columns unambiguous. Do not align them.
pub fn mission_context(mission: &Mission) -> String {
    let mut text = format!(
        "This thread works mission {} ({}): {}.",
        mission.id,
        mission.status.as_str(),
        mission.name
    );
    let open: Vec<String> = mission
        .tasks
        .iter()
        .filter(|t| t.status != Status::Done)
        .map(|t| task_line(mission, t))
        .collect();
    if open.is_empty() {
        text.push_str(" It has no open tasks.");
    } else {
        text.push_str(
            " Its open tasks follow, one per line: id, status, blocked-by, description, tab-separated.\n",
        );
        text.push_str(&open.join("\n"));
        text.push('\n');
    }
    text
}

/// What the session start says about the latest continuation entry of a bound thread that
/// has a mission: the task and its description, what is next, who wrote it and when, and a
/// warning when another actor wrote it. The agent writes the resume summary from it.
pub fn latest_entry_context(entry: &Entry, facts: &Facts, warning: &str) -> String {
    let task = match (
        &facts.task,
        entry.task_id.as_deref().filter(|t| !t.is_empty()),
    ) {
        (Some((id, description)), _) => format!("task {} ({})", id, description),
        (None, Some(id)) => format!("task {}", id),
        (None, None) => "no task".to_string(),
    };
    let next = entry.whats_next.as_deref().map_or("none", str::trim);
    let stop = if next.ends_with(['.', '!', '?']) {
        ""
    } else {
        "."
    };
    let mut text = format!(
        "Its latest pause entry is on {}, written by {} at {}. What's next: {}{}",
        task,
        entry.written_by.as_deref().unwrap_or("an unknown actor"),
        entry.timestamp.as_deref().unwrap_or("an unknown time"),
        next,
        stop
    );
    if !warning.is_empty() {
        text.push_str(&format!(" WARNING: {}.", warning));
    }
    text.push_str(
        " Write the resume summary from this and run no command until the person confirms the thread.",
    );
    text
}

/// What a version 1 ledger needs, which no other command can do.
pub fn version_1_context(path: &Path) -> String {
    format!(
        "The ledger at {} (also exported as ${}) is at layout version 1: its documents are still in Dolt. Run `tsk ledger migrate-docs` once to move them to the tsk/docs branch. Until then every tsk command except `tsk ledger fetch` stops with an error that names this command.",
        path.display(),
        LEDGER_WT_VAR
    )
}

pub fn failure_context(error: &str) -> String {
    format!(
        "Warning: the ledger could not be fetched automatically at session start ({}). Run `tsk ledger fetch` manually before reading the missions.",
        error.replace('\n', " ").trim()
    )
}

fn export_to_env_file(path: &Path) -> Result<(), String> {
    let Some(file) = std::env::var_os(ENV_FILE_VAR).filter(|v| !v.is_empty()) else {
        return Ok(());
    };
    let mut handle = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&file)
        .map_err(|e| format!("could not open {}: {}", Path::new(&file).display(), e))?;
    writeln!(handle, "export {}=\"{}\"", LEDGER_WT_VAR, path.display())
        .map_err(|e| format!("could not write {}: {}", Path::new(&file).display(), e))
}

fn context_after_fetch(session: &Session) -> Result<String, String> {
    let outcome = match session.store.refresh_keeping_local() {
        Ok(outcome) => outcome,
        Err(e) if e == VERSION_1_REFUSAL => {
            // The database is refreshed and the documents are not reachable.
            let path = session.store.path()?;
            let mut context = version_1_context(&path);
            if let Err(e) = export_to_env_file(&path) {
                context.push_str(&format!(
                    " WARNING: {} was not exported ({}); use the path above.",
                    LEDGER_WT_VAR, e
                ));
            }
            return Ok(context);
        }
        Err(e) => return Err(e),
    };
    let store = session.store.as_ref();
    let binding = resolve(session).ok().flatten();
    let missions = store.missions_load().unwrap_or_default();
    let no_documents = store
        .doc_list("missions/")
        .is_ok_and(|documents| documents.is_empty());

    // With mission records the context points to `tsk mission list`, or carries the bound
    // mission. A ledger with no missions has nothing to point to.
    let mut read_hint = if missions.is_empty() {
        String::new()
    } else {
        MISSION_LIST_HINT.to_string()
    };
    let mut no_mission = None;
    if let Some(binding) = &binding {
        let id = binding.thread_id();
        let thread_mission = store
            .thread_info(id)
            .ok()
            .and_then(|info| thread_mission(&info));
        match thread_mission.as_deref() {
            Some(mission_id) => {
                if let Some(mission) = missions.iter().find(|m| m.id == mission_id) {
                    read_hint = mission_context(mission);
                    let entries = entries_of(store, id).unwrap_or_default();
                    if let (Some(last), Ok(info)) = (entries.last(), store.thread_info(id)) {
                        let facts = facts_of(&info, Some(&last.entry), &missions);
                        let current = session.written_by().unwrap_or_default();
                        let prior = last.entry.written_by.iter().cloned().collect();
                        let warning = takeover_warning(id, &prior, &current);
                        push_part(
                            &mut read_hint,
                            &latest_entry_context(&last.entry, &facts, &warning),
                        );
                    }
                }
            }
            None if store.thread_exists(id).unwrap_or(false) => {
                let latest = latest_whats_next(store, id).ok().flatten();
                no_mission = Some(no_mission_context(latest.as_deref()));
            }
            None => {}
        }
    }
    let prompt = if is_skipped(session) {
        SKIPPED_MESSAGE.to_string()
    } else {
        unbound_prompt(&open_mission_lines(&missions))
    };
    let mut thread_text =
        thread_message(binding.as_ref().map(|b| b.to_string()).as_deref(), &prompt);
    if let Some(no_mission) = no_mission {
        thread_text.push(' ');
        thread_text.push_str(&no_mission);
    }
    if let Some(actor) = crate::actor::current(session).ok().flatten() {
        push_part(&mut thread_text, &actor_context(&actor.label()));
    }
    let mut context = fetched_context(
        &outcome.path,
        &read_hint,
        &PendingNote::from_outcome(
            &outcome,
            store.pending_inspect_hint(&outcome.path, &outcome.remote_head),
        ),
        &thread_text,
    );
    if outcome.unpublished {
        if let Some(cloud) = crate::ledger::refs::cloud_default_note() {
            push_part(&mut context, cloud);
        }
    }
    if store.has_local_changes().unwrap_or(false) {
        push_part(&mut context, LOCAL_CHANGES_NOTE);
    }
    if missions.is_empty() && no_documents {
        context.push(' ');
        context.push_str(crate::guide::EMPTY_LEDGER_NOTE);
    }
    if let Err(e) = export_to_env_file(&outcome.path) {
        context.push_str(&format!(
            " WARNING: {} was not exported ({}); use the path above.",
            LEDGER_WT_VAR, e
        ));
    }
    Ok(context)
}

/// The plugin version this binary expects, the `version` in `plugin/.claude-plugin/plugin.json`.
/// A unit test keeps the two equal.
pub const EXPECTED_PLUGIN_VERSION: &str = "0.3.23";

/// The numbers of a `major.minor.patch` version. A pre-release or build suffix is ignored.
fn parse_version(text: &str) -> Option<(u64, u64, u64)> {
    let core = text.trim().split(['-', '+']).next()?;
    let mut parts = core.split('.');
    let major = parts.next()?.parse().ok()?;
    let minor = parts.next()?.parse().ok()?;
    let patch = parts.next()?.parse().ok()?;
    if parts.next().is_some() {
        return None;
    }
    Some((major, minor, patch))
}

/// The sentence that says the loaded plugin is older than the one this binary expects.
/// None when the versions are equal, the loaded one is higher, or either is absent or
/// not a version.
pub fn plugin_version_note(given: Option<&str>, expected: &str) -> Option<String> {
    let given = given?.trim();
    if parse_version(given)? >= parse_version(expected)? {
        return None;
    }
    Some(format!(
        "The tsk plugin loaded in this session is {}; this tsk expects {}. Update the plugin (Claude Code: /plugin update tsk; or start a new session), because its skills describe an older command set.",
        given, expected
    ))
}

pub const REMOTE_VAR: &str = "CLAUDE_CODE_REMOTE";

pub fn is_remote() -> bool {
    std::env::var(REMOTE_VAR).is_ok_and(|v| v == "true")
}

pub fn install_warning(error: &str) -> String {
    format!(
        "Warning: the tsk plugin could not be installed at session start ({}). Run `tsk install-plugin claude-cli` manually.",
        error.replace('\n', " ").trim()
    )
}

pub fn upgrade_warning(error: &str) -> String {
    format!(
        "Warning: tsk could not be upgraded at session start ({}). Run `tsk self-update` manually.",
        error.replace('\n', " ").trim()
    )
}

/// Runs the plugin install for a cloud session. Step lines go to stderr. Returns the
/// warning sentence when a step fails.
fn install_plugin_warning() -> Option<String> {
    let args = crate::install_plugin::InstallPluginArgs::claude_defaults();
    match crate::install_plugin::install(&args, &mut |line| eprintln!("{}", line)) {
        Ok(()) => None,
        Err(e) => {
            eprintln!("{}", e);
            Some(install_warning(&e))
        }
    }
}

/// Start the Dolt server of this clone's ledger store when the switch is on and there is
/// a store that uses a file remote (the stores `tsk` serves). A clone with no store yet
/// starts nothing. Nothing here can fail the hook: a server that does not start is started by
/// the first command that needs it, or the command falls back to the `dolt` command line.
fn start_server_in_background() {
    if !crate::ledger::store::server::enabled() {
        return;
    }
    let Ok(cwd) = std::env::current_dir() else {
        return;
    };
    if let Ok(store) = crate::ledger::store::server::store_of(&cwd) {
        if !crate::ledger::dolt::is_file_remote_store(&store) {
            return;
        }
        let _ = crate::ledger::store::server::ensure_background(&store);
    }
}

pub fn run(format: HookFormat, install_plugin: bool, plugin_version: Option<&str>) -> i32 {
    let mut bytes = Vec::new();
    let _ = std::io::stdin().read_to_end(&mut bytes);
    let input = HookInput::parse(&bytes);
    if let Ok(state_root) = state_root_from_env() {
        if claim(&state_root.join(CLAIM_DIR), &input, SystemTime::now()) == Claim::Duplicate {
            return 0;
        }
    }
    // With the server switch on, a dead or stale server (a container that woke from
    // hibernation, say) is replaced now, in the background, so the first command finds a
    // live one. A live server costs one connection.
    start_server_in_background();
    let cloud_install = install_plugin && is_remote();
    let mut warnings = Vec::new();
    if cloud_install {
        // A cloud session reuses a cached setup result, so the installed tsk can be
        // old. Upgrade it before the plugin install. This run goes on with the old
        // binary already in memory, and the next command finds the new one.
        if let CloudUpgrade::Failed(e) = upgrade_for_cloud() {
            warnings.push(upgrade_warning(&e));
        }
        warnings.extend(install_plugin_warning());
    }
    let mut context = Session::discover()
        .and_then(|session| context_after_fetch(&session))
        .unwrap_or_else(|e| failure_context(&e));
    if !cloud_install {
        if let Some(notice) = session_notice() {
            context.push(' ');
            context.push_str(&notice);
        }
    }
    if let Some(note) = plugin_version_note(plugin_version, EXPECTED_PLUGIN_VERSION) {
        context.push(' ');
        context.push_str(&note);
    }
    for warning in warnings {
        context.push(' ');
        context.push_str(&warning);
    }
    println!("{}", format_output(format, &context));
    0
}

#[cfg(test)]
mod tests {
    use super::super::binding::UNBOUND_PROMPT;
    use super::*;
    use std::path::PathBuf;

    fn no_pending() -> PendingNote {
        PendingNote {
            remote_ref: "origin's refs/heads/tsk/ledger".to_string(),
            inspect_hint: "git -C '/s/ledger' log -p abc123..HEAD".to_string(),
            lines: Vec::new(),
        }
    }

    #[test]
    fn hook_output_is_one_compact_object_in_hook_order() {
        let text = hook_output("a \"b\"");
        assert_eq!(
            text,
            "{\"hookSpecificOutput\":{\"hookEventName\":\"SessionStart\",\"additionalContext\":\"a \\\"b\\\"\"}}"
        );
    }

    #[test]
    fn copilot_output_is_one_compact_object_with_only_the_context() {
        assert_eq!(
            copilot_output("a \"b\""),
            "{\"additionalContext\":\"a \\\"b\\\"\"}"
        );
        assert_eq!(
            format_output(HookFormat::Copilot, "x"),
            "{\"additionalContext\":\"x\"}"
        );
        assert_eq!(format_output(HookFormat::Claude, "x"), hook_output("x"));
    }

    #[test]
    fn hook_input_accepts_the_copilot_camel_case_session_id() {
        let parsed = HookInput::parse(
            b"{\"sessionId\":\"abc-1\",\"timestamp\":1760000000000,\"cwd\":\"/x\",\"source\":\"new\",\"initialPrompt\":null}",
        );
        assert_eq!(parsed.session_id.as_deref(), Some("abc-1"));
        assert_eq!(parsed.source.as_deref(), Some("new"));
        assert_eq!(parsed.claim_name().as_deref(), Some("abc-1.new"));
        let unsafe_id = HookInput::parse(b"{\"sessionId\":\"../x\"}");
        assert!(unsafe_id.claim_name().is_none());
    }

    #[test]
    fn fetched_context_names_the_path_and_the_mission_list_hint() {
        let path = PathBuf::from("/s/ledger");
        let context = fetched_context(&path, MISSION_LIST_HINT, &no_pending(), "Next.");
        assert_eq!(
            context,
            "The ledger was fetched and materialised at /s/ledger (also exported as $TSK_LEDGER_WT). Run `tsk mission list` to see the missions. Next."
        );
        assert_eq!(
            fetched_context(&path, "", &no_pending(), "Next."),
            "The ledger was fetched and materialised at /s/ledger (also exported as $TSK_LEDGER_WT). Next."
        );
    }

    #[test]
    fn fetched_context_adds_the_pending_warning() {
        let path = PathBuf::from("/s/ledger");
        let pending = PendingNote {
            lines: vec!["1111111 first".to_string(), "2222222 second".to_string()],
            ..no_pending()
        };
        let context = fetched_context(&path, MISSION_LIST_HINT, &pending, "Next.");
        assert!(context.contains(
            "WARNING: the ledger worktree holds a commit that is not on origin's refs/heads/tsk/ledger"
        ));
        assert!(context.contains("1111111 first;2222222 second"));
        assert!(context.contains("git -C '/s/ledger' log -p abc123..HEAD"));
        assert!(context.contains("push it with tsk ledger push"));
        assert!(context.ends_with(" Next."));
    }

    #[test]
    fn thread_message_names_the_binding_and_the_resume_command() {
        assert_eq!(
            thread_message(Some("worktree:1234abcd"), UNBOUND_PROMPT),
            "An existing thread binding was found: worktree:1234abcd. Run /tsk:resume-thread 1234abcd next."
        );
        assert_eq!(thread_message(None, UNBOUND_PROMPT), UNBOUND_PROMPT);
        assert_eq!(thread_message(None, "Pick."), "Pick.");
    }

    #[test]
    fn no_mission_context_gives_the_latest_whats_next_and_the_attach_command() {
        assert_eq!(
            no_mission_context(Some("Review the draft")),
            "This thread has no mission, so it has no tasks. Its latest what's next: Review the draft. To attach a mission, run `tsk thread set mission <mission-id>`."
        );
        assert_eq!(
            no_mission_context(Some("Done.")),
            "This thread has no mission, so it has no tasks. Its latest what's next: Done. To attach a mission, run `tsk thread set mission <mission-id>`."
        );
        assert_eq!(
            no_mission_context(None),
            "This thread has no mission, so it has no tasks. Its latest what's next: none yet. To attach a mission, run `tsk thread set mission <mission-id>`."
        );
    }

    fn input(session_id: Option<&str>, source: Option<&str>) -> HookInput {
        HookInput {
            session_id: session_id.map(str::to_string),
            source: source.map(str::to_string),
        }
    }

    fn age(path: &Path, by: Duration) {
        std::fs::File::options()
            .write(true)
            .open(path)
            .unwrap()
            .set_modified(SystemTime::now() - by)
            .unwrap();
    }

    #[test]
    fn hook_input_parses_the_session_id_and_source_and_ignores_bad_input() {
        let parsed =
            HookInput::parse(b"{\"session_id\":\"abc\",\"source\":\"clear\",\"cwd\":\"/x\"}");
        assert_eq!(parsed.session_id.as_deref(), Some("abc"));
        assert_eq!(parsed.source.as_deref(), Some("clear"));
        assert!(HookInput::parse(b"").session_id.is_none());
        assert!(HookInput::parse(b"not json").session_id.is_none());
    }

    #[test]
    fn claim_marks_a_second_run_for_the_same_event_as_a_duplicate() {
        let dir = tempfile::tempdir().unwrap();
        let event = input(Some("s-1"), Some("startup"));
        assert_eq!(claim(dir.path(), &event, SystemTime::now()), Claim::First);
        assert_eq!(
            claim(dir.path(), &event, SystemTime::now()),
            Claim::Duplicate
        );
        assert_eq!(
            claim(
                dir.path(),
                &input(Some("s-1"), Some("compact")),
                SystemTime::now()
            ),
            Claim::First
        );
        assert_eq!(
            claim(
                dir.path(),
                &input(Some("s-2"), Some("startup")),
                SystemTime::now()
            ),
            Claim::First
        );
    }

    #[test]
    fn claim_replaces_a_claim_older_than_the_window() {
        let dir = tempfile::tempdir().unwrap();
        let event = input(Some("s-1"), Some("compact"));
        assert_eq!(claim(dir.path(), &event, SystemTime::now()), Claim::First);
        age(&dir.path().join("s-1.compact"), CLAIM_WINDOW);
        assert_eq!(claim(dir.path(), &event, SystemTime::now()), Claim::First);
        assert_eq!(
            claim(dir.path(), &event, SystemTime::now()),
            Claim::Duplicate
        );
    }

    #[test]
    fn claim_needs_a_safe_session_id() {
        let dir = tempfile::tempdir().unwrap();
        for event in [input(None, Some("startup")), input(Some("../x"), None)] {
            assert_eq!(claim(dir.path(), &event, SystemTime::now()), Claim::First);
            assert_eq!(claim(dir.path(), &event, SystemTime::now()), Claim::First);
        }
        assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0);
    }

    #[test]
    fn claim_prunes_claims_older_than_a_day() {
        let dir = tempfile::tempdir().unwrap();
        let old = dir.path().join("old.startup");
        std::fs::write(&old, "").unwrap();
        age(&old, CLAIM_PRUNE_AGE);
        claim(
            dir.path(),
            &input(Some("new"), Some("startup")),
            SystemTime::now(),
        );
        assert!(!old.exists());
        assert!(dir.path().join("new.startup").exists());
    }

    #[test]
    fn plugin_version_note_fires_only_for_an_older_plugin() {
        assert_eq!(
            plugin_version_note(Some("0.3.11"), "0.3.12").as_deref(),
            Some("The tsk plugin loaded in this session is 0.3.11; this tsk expects 0.3.12. Update the plugin (Claude Code: /plugin update tsk; or start a new session), because its skills describe an older command set.")
        );
        assert!(plugin_version_note(Some("0.3.9"), "0.3.10").is_some());
        assert!(plugin_version_note(Some("0.2.99"), "0.3.0").is_some());
        assert!(plugin_version_note(Some("0.3.12"), "0.3.12").is_none());
        assert!(plugin_version_note(Some("0.3.13"), "0.3.12").is_none());
        assert!(plugin_version_note(Some("1.0.0"), "0.3.12").is_none());
        assert!(plugin_version_note(None, "0.3.12").is_none());
        assert!(plugin_version_note(Some(""), "0.3.12").is_none());
        assert!(plugin_version_note(Some("latest"), "0.3.12").is_none());
        assert!(plugin_version_note(Some("0.3"), "0.3.12").is_none());
        assert!(plugin_version_note(Some("0.3.1.2"), "0.3.12").is_none());
    }

    #[test]
    fn parse_version_ignores_a_pre_release_and_build_suffix() {
        assert_eq!(parse_version(" 0.3.12-rc.1+b5 "), Some((0, 3, 12)));
        assert_eq!(parse_version("a.b.c"), None);
    }

    #[test]
    fn the_expected_plugin_version_is_the_version_in_the_plugin_manifest() {
        let manifest =
            Path::new(env!("CARGO_MANIFEST_DIR")).join("../plugin/.claude-plugin/plugin.json");
        let Ok(text) = std::fs::read_to_string(&manifest) else {
            return; // not in this repo, for example a crates.io checkout
        };
        let json: serde_json::Value = serde_json::from_str(&text).unwrap();
        assert_eq!(json["version"], EXPECTED_PLUGIN_VERSION);
    }

    #[test]
    fn upgrade_warning_names_the_manual_command() {
        let warning = upgrade_warning("error: curl failed\nexit 22");
        assert!(warning.contains("(error: curl failed exit 22)"));
        assert!(warning.ends_with("Run `tsk self-update` manually."));
    }

    #[test]
    fn failure_context_asks_for_a_manual_fetch() {
        let context = failure_context("error: git fetch failed\nfatal: no remote");
        assert!(context.contains("error: git fetch failed fatal: no remote"));
        assert!(context.contains("Run `tsk ledger fetch` manually"));
    }
}