hank 0.4.0

In-memory, multi-tenant code-analysis engine — the structural signal for the Bobbin × Quipu stack
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
//! The `PostToolUse` advisory — Hank's answer to "what did that edit reach?".
//!
//! `hank hook post-edit` reads the harness's `PostToolUse` JSON on stdin and
//! returns an advisory: which symbols in the edited file have callers elsewhere,
//! so the agent learns the blast radius of its own change synchronously, without
//! calling a tool. **Advisory only** — the blocking companion is
//! [`super::pre_edit`].
//!
//! With `[hank.serve] use_daemon = true` this is a thin client of the resident
//! daemon (FR-31, hank #1 stage 5): the edited file's symbols are still
//! extracted fresh HERE (their content is what just changed), but their callers
//! come from the resident graph — no per-invocation `CodeGraph::build`. The
//! daemon being unusable falls back to the transient build with a stderr note;
//! like the MCP tools and unlike the pre-edit guard, fallback is silent to the
//! model — this is an advisory, not an enforcement surface, and a transient
//! answer is equally correct.

use std::collections::BTreeSet;
use std::ffi::OsStr;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::Duration;

use super::{HookInput, ToolInput};
use crate::config::HankConfig;
use crate::daemon::client::{expected_same_root_daemon, fetch_edit};
use crate::extract::extract_symbols;
use crate::graph::{CodeGraph, Dir};

/// Budget per localhost round-trip, same rationale as the MCP thin client:
/// generous against a resident graph, small enough that a wedged daemon costs
/// one slow query before the transient fallback answers.
const DAEMON_TIMEOUT: Duration = Duration::from_millis(500);

/// How many impacted symbols to list before summarizing the rest.
const MAX_LISTED: usize = 8;

/// Run the `post-edit` hook: read the harness payload from stdin and, if the
/// edit has cross-file impact, print the `PostToolUse` advisory envelope.
/// `tenant` is the session's overlay identity (the global `--tenant` flag).
pub fn run_post_edit(tenant: Option<&str>) -> anyhow::Result<()> {
    let mut buf = String::new();
    std::io::stdin().lock().read_to_string(&mut buf).ok();
    let root = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

    if let Some(text) = advisory_for(&buf, &root, tenant) {
        let envelope = serde_json::json!({
            "hookSpecificOutput": {
                "hookEventName": "PostToolUse",
                "additionalContext": text,
            }
        });
        println!("{envelope}");
    }
    // A hook must never fail the harness: absence of output = nothing to say.
    Ok(())
}

/// Compute the advisory text for a hook payload, or `None` when there is nothing
/// useful to say (unparseable, non-Rust, or no cross-file impact).
#[must_use]
pub fn advisory_for(input_json: &str, default_root: &Path, tenant: Option<&str>) -> Option<String> {
    let input = HookInput::parse(input_json)?;
    let file_path = input.tool_input.file_path.clone()?;
    let file = PathBuf::from(&file_path);
    if file.extension().and_then(OsStr::to_str) != Some("rs") {
        return None;
    }

    let root = input.root(default_root);
    let rel = file
        .strip_prefix(&root)
        .unwrap_or(&file)
        .display()
        .to_string();

    let source = std::fs::read_to_string(&file).ok()?;
    let symbols = extract_symbols(&source, "rust").ok()?;
    if symbols.is_empty() {
        return None;
    }

    // aegis-rcyd / #75: scope the advisory to the symbol(s) the edit actually
    // TOUCHED, not every symbol in the file. Without this, a comment- or
    // whitespace-only edit cries "N callers, re-check!" identically to a
    // signature change — alarm fatigue that trains agents to tune the advisory
    // out (the opposite of the "is it acted on?" goal). The post-edit `source`
    // already contains the replacement text, so locate it to get the changed
    // line span(s) and keep only symbols whose body overlaps. Fall back to the
    // whole file when we cannot localize (Write's full `content`, a pure
    // deletion, or an unlocatable replacement) — conservative: over-advise
    // rather than miss a real breaking change.
    let names: Vec<String> = match edited_line_spans(&input.tool_input, &source) {
        Some(spans) if !spans.is_empty() => {
            let scoped: Vec<String> = symbols
                .into_iter()
                .filter(|sym| {
                    spans
                        .iter()
                        .any(|&(lo, hi)| sym.start_line <= hi && sym.end_line >= lo)
                })
                .map(|sym| sym.name)
                .collect();
            // The edit landed outside every symbol body (e.g. an import or a
            // module-level comment) — nothing with callers to re-check.
            if scoped.is_empty() {
                return None;
            }
            scoped
        }
        _ => symbols.into_iter().map(|s| s.name).collect(),
    };

    let (mut per_symbol, files) =
        resident_feed(&root, &rel, tenant).or_else(|| transient_callers(&root, &rel, &names))?;
    per_symbol.sort();
    per_symbol.dedup();
    if per_symbol.is_empty() {
        return None;
    }

    Some(render(&rel, &per_symbol, &files))
}

/// The 1-based inclusive line span(s) the edit changed, located in the POST-edit
/// `source`. `Edit` → the line range(s) of `new_string` as it now sits in the
/// file (all occurrences — the same text may legitimately appear more than
/// once); `MultiEdit` → the union across its edits. Returns `None` when we
/// cannot localize (Write's whole-file `content`, or a pure deletion whose
/// replacement text is empty) so the caller falls back to whole-file scoping.
fn edited_line_spans(tool_input: &ToolInput, source: &str) -> Option<Vec<(usize, usize)>> {
    let mut needles: Vec<&str> = Vec::new();
    if let Some(ns) = tool_input.new_string.as_deref() {
        needles.push(ns);
    }
    for e in &tool_input.edits {
        if let Some(ns) = e.new_string.as_deref() {
            needles.push(ns);
        }
    }
    let mut spans: Vec<(usize, usize)> = Vec::new();
    for needle in needles {
        if needle.is_empty() {
            continue; // deletion: no text to locate post-hoc
        }
        let added_lines = needle.matches('\n').count();
        let mut from = 0usize;
        while let Some(rel) = source[from..].find(needle) {
            let byte = from + rel;
            let start_line = source[..byte].matches('\n').count() + 1; // 1-based
            spans.push((start_line, start_line + added_lines));
            from = byte + needle.len().max(1);
        }
    }
    if spans.is_empty() {
        None
    } else {
        Some(spans)
    }
}

/// Per-symbol external-caller counts and their files. The shared shape both
/// sources produce, so the advisory renders identically either way.
type ExternalCallers = (Vec<(String, usize)>, BTreeSet<String>);

/// The FR-30 cycle against the RESIDENT daemon: ONE `POST /edit` records the
/// just-saved file in this tenant's overlay AND returns the advisory from the
/// fresh composed view. `None` to fall back — daemon not expected (silent), or
/// expected-but-unusable (stderr note; an advisory has no enforcement gap to
/// be loud about). On fallback the edit is not recorded anywhere, which is
/// fine: the overlay caches the tenant's edits, the file on disk is the record.
fn resident_feed(root: &Path, rel: &str, tenant: Option<&str>) -> Option<ExternalCallers> {
    let config = HankConfig::resolve(None, root).ok()?;
    let (host, port) = match expected_same_root_daemon(&config, root, DAEMON_TIMEOUT)? {
        Ok(addr) => addr,
        Err(reason) => {
            eprintln!("hank post-edit: daemon expected but unusable, transient fallback: {reason}");
            return None;
        }
    };
    let tenant = tenant.unwrap_or("single-tenant");
    match fetch_edit(&host, port, tenant, rel, DAEMON_TIMEOUT) {
        Ok(reply) => Some((
            reply
                .advised
                .into_iter()
                .map(|a| (a.symbol, a.external_callers))
                .collect(),
            reply.files.into_iter().collect(),
        )),
        Err(reason) => {
            eprintln!("hank post-edit: daemon edit feed failed, transient fallback: {reason}");
            None
        }
    }
}

/// External callers from a transient whole-root build — the pre-daemon path,
/// kept as the fallback.
fn transient_callers(root: &Path, rel: &str, names: &[String]) -> Option<ExternalCallers> {
    let graph = CodeGraph::build(root).ok()?;
    let mut per_symbol: Vec<(String, usize)> = Vec::new();
    let mut files: BTreeSet<String> = BTreeSet::new();
    for name in names {
        let external: Vec<_> = graph
            .direct(name, Dir::Callers)
            .into_iter()
            .filter(|caller| caller.file != rel)
            .collect();
        if !external.is_empty() {
            per_symbol.push((name.clone(), external.len()));
            for caller in &external {
                files.insert(caller.file.clone());
            }
        }
    }
    Some((per_symbol, files))
}

/// Format the advisory shown to the agent.
fn render(rel: &str, per_symbol: &[(String, usize)], files: &BTreeSet<String>) -> String {
    let mut out = format!(
        "Hank (tree-sitter): your edit to {rel} touches symbol(s) with callers elsewhere \
         — re-check these still compile.\n"
    );
    for (name, count) in per_symbol.iter().take(MAX_LISTED) {
        out.push_str(&format!("  {name} <- {count} caller(s)\n"));
    }
    if per_symbol.len() > MAX_LISTED {
        out.push_str(&format!(
            "  ... and {} more\n",
            per_symbol.len() - MAX_LISTED
        ));
    }
    let file_list: Vec<&str> = files.iter().map(String::as_str).collect();
    out.push_str(&format!("Impacted files: {}", file_list.join(", ")));
    out
}

#[cfg(test)]
// Test names here shout the invariant they pin — `is_NEVER_observable`,
// `daemon_EXPECTED_but_DOWN`, `is_DOWN_not_UP`. That capitalisation is the same
// emphasis the prose and comments use throughout this repo, and it is load-
// bearing in a test name: it says which word the assertion turns on. Allowed
// explicitly, and scoped to tests, so the lint stays live everywhere else
// rather than being switched off crate-wide (hank #83).
#[allow(non_snake_case)]
mod tests {
    use super::*;

    #[test]
    fn advises_on_cross_file_impact() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("a.rs"), "fn leaf() {}\n").unwrap();
        std::fs::write(dir.path().join("b.rs"), "fn mid() { leaf(); }\n").unwrap();

        let payload = serde_json::json!({
            "tool_name": "Edit",
            "cwd": dir.path().to_str().unwrap(),
            "tool_input": { "file_path": dir.path().join("a.rs").to_str().unwrap() },
        })
        .to_string();

        let text = advisory_for(&payload, dir.path(), None).expect("expected an advisory");
        assert!(text.contains("leaf"));
        assert!(text.contains("b.rs"));
    }

    #[test]
    fn scopes_advisory_to_the_edited_symbol() {
        // a.rs defines `leaf` (edited) and `other` (untouched), both called from b.rs.
        // The file on disk is POST-edit, as the real hook sees it.
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("a.rs"),
            "fn leaf() {\n    let x = 2;\n}\nfn other() {}\n",
        )
        .unwrap();
        std::fs::write(dir.path().join("b.rs"), "fn m() { leaf(); other(); }\n").unwrap();

        let payload = serde_json::json!({
            "tool_name": "Edit",
            "cwd": dir.path().to_str().unwrap(),
            "tool_input": {
                "file_path": dir.path().join("a.rs").to_str().unwrap(),
                "old_string": "let x = 1;",
                "new_string": "let x = 2;",
            },
        })
        .to_string();

        let text = advisory_for(&payload, dir.path(), None).expect("expected an advisory");
        assert!(
            text.contains("leaf"),
            "advises on the edited symbol: {text}"
        );
        assert!(
            !text.contains("other"),
            "must NOT cry wolf on the untouched symbol: {text}"
        );
    }

    #[test]
    fn no_advice_when_edit_is_outside_every_symbol() {
        // A top-of-file comment edit touches no symbol body — the cry-wolf case.
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("a.rs"), "// header updated\nfn leaf() {}\n").unwrap();
        std::fs::write(dir.path().join("b.rs"), "fn m() { leaf(); }\n").unwrap();

        let payload = serde_json::json!({
            "tool_name": "Edit",
            "cwd": dir.path().to_str().unwrap(),
            "tool_input": {
                "file_path": dir.path().join("a.rs").to_str().unwrap(),
                "old_string": "// header",
                "new_string": "// header updated",
            },
        })
        .to_string();
        assert!(
            advisory_for(&payload, dir.path(), None).is_none(),
            "a comment-only edit must not advise on symbols it did not touch"
        );
    }

    #[test]
    fn edited_line_spans_locates_edit_and_falls_back() {
        let src = "aaa\nbbb\nccc\n";
        let ti = ToolInput {
            new_string: Some("bbb".into()),
            ..Default::default()
        };
        assert_eq!(edited_line_spans(&ti, src), Some(vec![(2, 2)]));
        // Deletion → None (unlocatable post-hoc).
        let ti = ToolInput {
            new_string: Some(String::new()),
            ..Default::default()
        };
        assert_eq!(edited_line_spans(&ti, src), None);
        // No diff info (Write) → None → whole-file fallback.
        assert_eq!(edited_line_spans(&ToolInput::default(), src), None);
    }

    #[test]
    fn quiet_when_no_external_callers() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("a.rs"),
            "fn leaf() {}\nfn mid() { leaf(); }\n",
        )
        .unwrap();
        // leaf's only caller (mid) is in the same file → no cross-file impact.
        let payload = serde_json::json!({
            "cwd": dir.path().to_str().unwrap(),
            "tool_input": { "file_path": dir.path().join("a.rs").to_str().unwrap() },
        })
        .to_string();
        assert!(advisory_for(&payload, dir.path(), None).is_none());
    }

    #[test]
    fn quiet_on_non_rust_or_garbage() {
        assert!(advisory_for("not json", Path::new("."), None).is_none());
        let payload = serde_json::json!({ "tool_input": { "file_path": "README.md" } }).to_string();
        assert!(advisory_for(&payload, Path::new("."), None).is_none());
    }

    // Project config expecting a daemon at 127.0.0.1:port. Written as the
    // PROJECT config so it wins over any developer user config for these keys.
    fn write_daemon_config(root: &Path, port: u16) {
        let bobbin = root.join(".bobbin");
        std::fs::create_dir_all(&bobbin).unwrap();
        std::fs::write(
            bobbin.join("config.toml"),
            format!(
                "[hank.serve]\nuse_daemon = true\nbind_address = \"127.0.0.1\"\n\
                 mcp_http_port = {port}\n"
            ),
        )
        .unwrap();
    }

    fn edit_payload(root: &Path, file: &str) -> String {
        serde_json::json!({
            "tool_name": "Edit",
            "cwd": root.to_str().unwrap(),
            "tool_input": { "file_path": root.join(file).to_str().unwrap() },
        })
        .to_string()
    }

    #[test]
    fn daemon_EXPECTED_but_DOWN_falls_back_to_the_transient_advisory() {
        // Port 1 never listens. The advisory must still be produced (transient
        // fallback) — a down daemon degrades performance, never the advisory.
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("a.rs"), "fn leaf() {}\n").unwrap();
        std::fs::write(dir.path().join("b.rs"), "fn mid() { leaf(); }\n").unwrap();
        write_daemon_config(dir.path(), 1);

        let text = advisory_for(&edit_payload(dir.path(), "a.rs"), dir.path(), None)
            .expect("the transient fallback must still advise");
        assert!(text.contains("leaf"));
        assert!(text.contains("b.rs"));
    }

    // Serving the router needs axum (`mcp` feature); the down/fallback quadrant
    // above runs feature-free.
    #[cfg(feature = "mcp")]
    #[tokio::test(flavor = "multi_thread")]
    async fn daemon_up_and_same_root_advises_from_the_RESIDENT_view_and_feeds_the_overlay() {
        use crate::daemon::{http, ResidentEngine};
        // The tenant layer anchors to a COMMIT, so the fixture is a real repo.
        let dir = tempfile::tempdir().unwrap();
        let run = |args: &[&str]| {
            let out = std::process::Command::new("git")
                .arg("-C")
                .arg(dir.path())
                .args(args)
                .output()
                .unwrap();
            assert!(out.status.success(), "git {args:?} failed");
        };
        run(&["init", "-q", "-b", "main"]);
        run(&["config", "user.email", "t@t"]);
        run(&["config", "user.name", "t"]);
        std::fs::write(dir.path().join("a.rs"), "fn leaf() {}\n").unwrap();
        std::fs::write(dir.path().join("b.rs"), "fn mid() { leaf(); }\n").unwrap();
        run(&["add", "-A"]);
        run(&["commit", "-qm", "base"]);

        let engine = ResidentEngine::build(dir.path(), None).unwrap();
        let observer = engine.clone();
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        tokio::spawn(async move {
            let _ = axum::serve(listener, http::router(engine)).await;
        });
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        write_daemon_config(dir.path(), port);

        // A REAL edit to a.rs (differs from base, still defines `leaf`): the
        // daemon reads it from disk, so it must not match the baseline or the
        // FR-15 base-hit would make it a no-op.
        std::fs::write(dir.path().join("a.rs"), "fn leaf() {}\nfn added() {}\n").unwrap();
        // `late.rs` is UNCOMMITTED and untouched: the tenant view composes
        // over base@HEAD, so it must not appear — a transient build would see
        // it. Its absence below proves who answered.
        std::fs::write(dir.path().join("late.rs"), "fn late() { leaf(); }\n").unwrap();

        let payload = edit_payload(dir.path(), "a.rs");
        let root = dir.path().to_path_buf();
        let text = tokio::task::spawn_blocking(move || advisory_for(&payload, &root, Some("t1")))
            .await
            .unwrap()
            .expect("an up, same-root daemon must advise");
        assert!(text.contains("b.rs"), "{text}");
        assert!(
            !text.contains("late.rs"),
            "`late.rs` is uncommitted and untouched — its presence means a \
             transient build answered, not the tenant view: {text}"
        );

        // And the advisory FED the overlay (FR-30): the daemon now holds the
        // edit as tenant t1's touch of a.rs.
        let reg = observer.registry().expect("repo ⇒ tenant layer");
        let reg = reg.read().unwrap();
        let overlay = reg.overlay("t1").expect("the edit created t1's overlay");
        assert!(overlay.is_touched("a.rs"));
    }
}