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
//! The `hank` binary entrypoint. See the `hank` library crate and
//! `docs/hank-spec.md` for the design.
use ErrorKind;
use Parser;
/// Parse argv, but never let a *hook* invocation exit `2`.
///
/// Clap exits `2` on an argument error, and exit `2` is Claude Code's
/// fail-**closed** channel. The guard's whole contract is that no failure can
/// block an edit — but "argument didn't parse" is a failure clap resolves
/// before any of our code runs, so the fail-open logic inside the guard never
/// gets a say.
///
/// This is not hypothetical. A `hank` predating `hook pre-edit` answers that
/// command with clap's "invalid value" error and exit `2` — so rolling out the
/// hook against a stale binary would block every `Edit`/`Write` in the fleet,
/// which is precisely the outcome the fail-open clause exists to prevent.
/// Absence fails open (exit `127`); it was *staleness* that failed closed.
///
/// So an unparseable hook invocation degrades to a silent allow. Every other
/// command keeps clap's ordinary behaviour, including `--help`/`--version`,
/// which clap reports as errors but which must still print and exit `0`.
/// Whether argv looks like `hank hook …`.
///
/// Deliberately a loose match rather than positional parsing: the argument that
/// fails to parse may be the subcommand itself, so there is nothing structured
/// left to inspect. A false positive (say, a path literally named `hook`) only
/// costs a malformed command exit `0` instead of exit `2`; a false negative
/// would let the fail-closed exit through, which is the outcome that matters.
/// Clap's [`ErrorKind`] has no stable [`Display`](std::fmt::Display); render a
/// short tag so the stderr line says *why* without dumping clap's full usage
/// block into the agent's debug log.