Skip to main content

harn_parser/
diagnostic.rs

1use std::io::IsTerminal;
2
3use harn_lexer::Span;
4use yansi::{Color, Paint};
5
6use crate::diagnostic_codes::Repair;
7use crate::ParserError;
8
9mod harness_migrations_generated;
10
11/// The typed harness path that replaced a removed global, from the generated
12/// projection of `harn_vm::stdlib::harness_migration_for_builtin`.
13///
14/// `harn-parser` compiles below `harn-vm`, so it cannot query the registry;
15/// `make gen-harness-migrations` emits the table and
16/// `make check-harness-migrations` keeps it from drifting.
17///
18/// **This is the fallback, not the first answer.** The hand-written tables
19/// above it encode migration decisions the registry cannot express, because the
20/// registry is keyed by name and some legacy globals collide with unrelated
21/// typed methods — the ambient `read_file` migrated to `harness.fs.read_text`,
22/// but a *different* `harness.tools.read_file` also exists, and that is what a
23/// name lookup finds. [`removed_global_replacement`] composes the sources in
24/// the right order; [`HARNESS_MIGRATION_DISAGREEMENTS`] is the audited list.
25pub fn generated_harness_migration(name: &str) -> Option<&'static str> {
26    harness_migrations_generated::HARNESS_MIGRATIONS
27        .binary_search_by_key(&name, |(legacy, _)| *legacy)
28        .ok()
29        .map(|index| harness_migrations_generated::HARNESS_MIGRATIONS[index].1)
30}
31
32pub struct RelatedSpanLabel<'a> {
33    pub span: &'a Span,
34    pub label: &'a str,
35}
36
37/// Normalize diagnostic filenames lexically for display.
38///
39/// This deliberately does not touch the filesystem: diagnostics should cancel
40/// `.` and `..` path components even when the path points at a file that no
41/// longer exists, without resolving symlinks.
42pub fn normalize_diagnostic_path(path: &str) -> String {
43    let posix = path.replace('\\', "/");
44    if posix.is_empty() {
45        return String::new();
46    }
47
48    let bytes = posix.as_bytes();
49    let mut drive = "";
50    let mut rest = posix.as_str();
51    #[expect(
52        clippy::string_slice,
53        reason = "bytes 0 and 1 are ASCII, so 2 is a char boundary"
54    )]
55    if bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':' {
56        drive = &posix[..2];
57        rest = &posix[2..];
58    }
59
60    let absolute = rest.starts_with('/');
61    let mut stack: Vec<&str> = Vec::new();
62    for segment in rest.split('/').filter(|segment| !segment.is_empty()) {
63        match segment {
64            "." => {}
65            ".." => {
66                if let Some(top) = stack.last() {
67                    if *top != ".." {
68                        stack.pop();
69                        continue;
70                    }
71                }
72                if !absolute {
73                    stack.push("..");
74                }
75            }
76            _ => stack.push(segment),
77        }
78    }
79
80    let mut normalized = String::new();
81    normalized.push_str(drive);
82    if absolute {
83        normalized.push('/');
84    }
85    normalized.push_str(&stack.join("/"));
86    if normalized.is_empty() {
87        ".".to_string()
88    } else {
89        normalized
90    }
91}
92
93fn has_same_snake_case_segments(a: &str, b: &str) -> bool {
94    if !a.contains('_') || !b.contains('_') {
95        return false;
96    }
97    let mut a_segments: Vec<_> = a.split('_').collect();
98    let mut b_segments: Vec<_> = b.split('_').collect();
99    if a_segments.len() < 2
100        || a_segments.len() != b_segments.len()
101        || a_segments.iter().any(|segment| segment.is_empty())
102        || b_segments.iter().any(|segment| segment.is_empty())
103    {
104        return false;
105    }
106    a_segments.sort_unstable();
107    b_segments.sort_unstable();
108    a_segments == b_segments
109}
110
111/// Find the closest match to `name` among `candidates`, within `max_dist` edits
112/// or by reordering its non-empty underscore-separated segments. Candidates
113/// within the edit-distance threshold rank ahead of reorder-only matches.
114pub fn find_closest_match<'a>(
115    name: &str,
116    candidates: impl Iterator<Item = &'a str>,
117    max_dist: usize,
118) -> Option<&'a str> {
119    candidates
120        .filter(|candidate| *candidate != name)
121        .filter_map(|candidate| {
122            let reordered = has_same_snake_case_segments(name, candidate);
123            if candidate.len().abs_diff(name.len()) > max_dist && !reordered {
124                return None;
125            }
126            let distance = strsim::levenshtein(name, candidate);
127            (distance <= max_dist || reordered).then_some((distance, candidate))
128        })
129        .min_by_key(|(distance, _)| *distance)
130        .map(|(_, candidate)| candidate)
131}
132
133/// Return the replacement for stdlib symbols that were directly renamed.
134pub fn renamed_stdlib_symbol(name: &str) -> Option<&'static str> {
135    match name {
136        "retry_with_backoff" => Some("retry_predicate_with_backoff"),
137        "print" => Some("harness.stdio.print"),
138        "println" => Some("harness.stdio.println"),
139        "eprint" => Some("harness.stdio.eprint"),
140        "eprintln" => Some("harness.stdio.eprintln"),
141        "read_line" => Some("harness.stdio.read_line"),
142        "prompt_user" => Some("harness.stdio.prompt"),
143        "agent_session_open" => Some("harness.agent.open"),
144        "agent_session_workspace_anchor" => Some("harness.agent.workspace_anchor"),
145        "agent_session_set_workspace_anchor" => Some("harness.agent.set_workspace_anchor"),
146        "agent_session_workspace_policy" => Some("harness.agent.workspace_policy"),
147        "agent_session_set_workspace_policy" => Some("harness.agent.set_workspace_policy"),
148        "agent_session_add_root" => Some("harness.agent.add_root"),
149        "agent_session_remove_root" => Some("harness.agent.remove_root"),
150        "agent_session_list_roots" => Some("harness.agent.list_roots"),
151        "agent_session_exists" => Some("harness.agent.exists"),
152        "agent_session_length" => Some("harness.agent.length"),
153        "agent_session_snapshot" => Some("harness.agent.snapshot"),
154        "agent_session_ancestry" => Some("harness.agent.ancestry"),
155        "agent_session_current_id" => Some("harness.agent.current_id"),
156        "agent_session_record_changed_path" => Some("harness.agent.record_changed_path"),
157        "agent_session_actor_chain" => Some("harness.agent.actor_chain"),
158        "agent_session_tool_format" => Some("harness.agent.tool_format"),
159        "agent_session_system_prompt" => Some("harness.agent.system_prompt"),
160        "agent_session_scratchpad" => Some("harness.agent.scratchpad"),
161        "agent_session_set_scratchpad" => Some("harness.agent.set_scratchpad"),
162        "agent_session_clear_scratchpad" => Some("harness.agent.clear_scratchpad"),
163        "agent_session_claim_tool_format" => Some("harness.agent.claim_tool_format"),
164        "agent_session_reset" => Some("harness.agent.reset"),
165        "agent_session_fork" => Some("harness.agent.fork"),
166        "agent_session_fork_at" => Some("harness.agent.fork_at"),
167        "agent_session_rollback" => Some("harness.agent.rollback"),
168        "agent_session_redo" => Some("harness.agent.redo"),
169        "agent_session_close" => Some("harness.agent.close"),
170        "agent_session_trim" => Some("harness.agent.trim"),
171        "agent_session_attach" => Some("harness.agent.attach"),
172        "agent_session_takeover" => Some("harness.agent.takeover"),
173        "agent_session_detach" => Some("harness.agent.detach"),
174        "agent_session_heartbeat" => Some("harness.agent.heartbeat"),
175        "agent_session_live_clients" => Some("harness.agent.live_clients"),
176        "agent_session_client_inject_prompt" => Some("harness.agent.client_inject_prompt"),
177        "agent_session_route_permission" => Some("harness.agent.route_permission"),
178        "agent_session_inject" => Some("harness.agent.inject"),
179        "agent_session_post_event" => Some("harness.agent.post_event"),
180        "agent_session_drain_inbox" => Some("harness.agent.drain_inbox"),
181        "agent_session_seed_from_jsonl" => Some("harness.agent.seed_from_jsonl"),
182        "agent_session_reanchor" => Some("harness.agent.reanchor"),
183        "agent_session_compact" => Some("harness.agent.compact"),
184        _ => None,
185    }
186}
187
188/// What an undefined name *should* have been, for the type checker's
189/// "did you mean".
190///
191/// Three sources, narrowest first: the in-place renames above, then the
192/// `harness_*_replacement` families, then [`generated_harness_migration`] for
193/// the long tail — 417 generated rows against roughly 60 hand-written ones.
194///
195/// Deliberately separate from [`renamed_stdlib_symbol`], which the linter uses
196/// to decide whether to raise `HARN-LNT-001`. Widening *that* makes every
197/// migrated global draw two warnings: the rename lint and the capability-family
198/// lint that already claims the name (`HARN-LNT-052`/`053`/`054`/`057`/`071`).
199/// Measured before splitting them — a four-call probe went from four findings to
200/// eight. Which lint should own a name is a policy question; answering "what
201/// replaced it" is not, and only the type checker asks this one.
202///
203/// Order matters, and not for style. The generated table is keyed by builtin
204/// name, and some legacy globals share a name with an unrelated typed method —
205/// the ambient `read_file` migrated to `harness.fs.read_text`, while a separate
206/// `harness.tools.read_file` agent tool is what a name lookup finds.
207/// [`HARNESS_MIGRATION_DISAGREEMENTS`] pins those, so a new one is a build
208/// failure rather than a confidently wrong suggestion.
209pub fn removed_global_replacement(name: &str) -> Option<&'static str> {
210    if let Some(replacement) = renamed_stdlib_symbol(name) {
211        return Some(replacement);
212    }
213    for family in HARNESS_REPLACEMENT_FAMILIES {
214        if let Some(replacement) = family(name) {
215            return Some(replacement);
216        }
217    }
218    generated_harness_migration(name)
219}
220
221/// The `harness_*_replacement` families, in one list so anything meaning "any
222/// capability migration" walks the same set.
223///
224/// A new family belongs here as well as beside its siblings. Nothing proves
225/// that mechanically — Rust has no reflection over free functions — but this
226/// list is the only way anything reaches them in bulk, so forgetting it leaves
227/// the new family unreachable rather than half-wired.
228pub const HARNESS_REPLACEMENT_FAMILIES: &[fn(&str) -> Option<&'static str>] = &[
229    harness_clock_replacement,
230    harness_stdio_replacement,
231    harness_fs_replacement,
232    harness_env_replacement,
233    harness_random_replacement,
234    harness_net_replacement,
235];
236
237/// Legacy globals whose hand-written migration deliberately differs from what a
238/// name lookup in the generated table finds, with the reason.
239///
240/// Reviewed rather than discovered: a new entry means the runtime registry and
241/// the migration record disagree about a name, which is a finding to
242/// investigate — not a list to extend casually.
243pub const HARNESS_MIGRATION_DISAGREEMENTS: &[(&str, &str, &str)] = &[
244    (
245        "read_file",
246        "harness.fs.read_text",
247        "`harness.tools.read_file` is an agent tool, not the filesystem capability",
248    ),
249    (
250        "write_file",
251        "harness.fs.write_text",
252        "`harness.tools.write_file` is an agent tool, not the filesystem capability",
253    ),
254    (
255        "delete_file",
256        "harness.fs.delete",
257        "`harness.tools.delete_file` is an agent tool, not the filesystem capability",
258    ),
259    (
260        "elapsed",
261        "harness.clock.monotonic_ms",
262        "`elapsed` was renamed, so the same-named `harness.clock.elapsed` is not its successor",
263    ),
264];
265
266/// Map an ambient clock-capability builtin to its `harness.clock.*`
267/// replacement. Returns the new identifier text (including the receiver
268/// path) so the `bindings/thread-harness-clock` repair can replace the
269/// call-site identifier in place. The mapping is the source of truth for
270/// the E4.3 → E4.6 migration; downstream replatform agents query it via
271/// [`Code::repair_template`].
272pub fn harness_clock_replacement(name: &str) -> Option<&'static str> {
273    match name {
274        "now_ms" => Some("harness.clock.now_ms"),
275        "monotonic_ms" => Some("harness.clock.monotonic_ms"),
276        "sleep_ms" => Some("harness.clock.sleep_ms"),
277        "sleep" => Some("harness.clock.sleep_ms"),
278        "timestamp" => Some("harness.clock.timestamp"),
279        "elapsed" => Some("harness.clock.monotonic_ms"),
280        "date_now" => Some("harness.clock.now"),
281        "date_now_iso" => Some("harness.clock.date_iso"),
282        _ => None,
283    }
284}
285
286/// Map an ambient stdio-capability builtin to its `harness.stdio.*`
287/// replacement so `harn fix` can replace the call in place once the
288/// relevant harness binding is available.
289pub fn harness_stdio_replacement(name: &str) -> Option<&'static str> {
290    match name {
291        "print" => Some("harness.stdio.print"),
292        "println" => Some("harness.stdio.println"),
293        "eprint" => Some("harness.stdio.eprint"),
294        "eprintln" => Some("harness.stdio.eprintln"),
295        "read_line" => Some("harness.stdio.read_line"),
296        "prompt_user" => Some("harness.stdio.prompt"),
297        _ => None,
298    }
299}
300
301/// Map an ambient fs-capability builtin to its `harness.fs.*` replacement.
302/// Backs the `bindings/thread-harness-fs` repair the E4.4 → E4.6
303/// migration uses to rewrite `.harn` scripts off the legacy surface.
304pub fn harness_fs_replacement(name: &str) -> Option<&'static str> {
305    match name {
306        "read_file" => Some("harness.fs.read_text"),
307        "read_file_result" => Some("harness.fs.read_text_result"),
308        "read_file_bytes" => Some("harness.fs.read_bytes"),
309        "write_file" => Some("harness.fs.write_text"),
310        "write_file_bytes" => Some("harness.fs.write_bytes"),
311        "replace_file" => Some("harness.fs.replace_text"),
312        "replace_file_result" => Some("harness.fs.replace_text_result"),
313        "replace_file_bytes" => Some("harness.fs.replace_bytes"),
314        "replace_file_bytes_result" => Some("harness.fs.replace_bytes_result"),
315        "file_exists" => Some("harness.fs.exists"),
316        "path_status" => Some("harness.fs.status"),
317        "delete_file" => Some("harness.fs.delete"),
318        "append_file" => Some("harness.fs.append"),
319        "append_file_locked" => Some("harness.fs.append_locked"),
320        "list_dir" => Some("harness.fs.list_dir"),
321        "mkdir" => Some("harness.fs.mkdir"),
322        "copy_file" => Some("harness.fs.copy"),
323        "temp_dir" => Some("harness.fs.temp_dir"),
324        "workspace_temp_dir" => Some("harness.fs.workspace_temp_dir"),
325        "mkdtemp" => Some("harness.fs.mkdtemp"),
326        "mkdtemp_in_workspace" => Some("harness.fs.mkdtemp_in_workspace"),
327        "stat" => Some("harness.fs.stat"),
328        "move_file" => Some("harness.fs.rename"),
329        "read_lines" => Some("harness.fs.read_lines"),
330        "read_lines_page_result" => Some("harness.fs.read_lines_page_result"),
331        "read_lines_append_page_result" => Some("harness.fs.read_lines_append_page_result"),
332        "walk_dir" => Some("harness.fs.walk"),
333        "glob" => Some("harness.fs.glob"),
334        "find_text" => Some("harness.fs.find_text"),
335        "find_evidence" => Some("harness.fs.find_evidence"),
336        "cwd" => Some("harness.fs.cwd"),
337        _ => None,
338    }
339}
340
341/// Map an ambient env-capability builtin to its `harness.env.*` replacement.
342/// Backs the `bindings/thread-harness-env` repair.
343pub fn harness_env_replacement(name: &str) -> Option<&'static str> {
344    match name {
345        "env" => Some("harness.env.get"),
346        "env_or" => Some("harness.env.get_or"),
347        _ => None,
348    }
349}
350
351/// Map an ambient random-capability builtin to its `harness.random.*`
352/// replacement. Backs the `bindings/thread-harness-random` repair.
353pub fn harness_random_replacement(name: &str) -> Option<&'static str> {
354    match name {
355        "random" => Some("harness.random.f64"),
356        "random_int" => Some("harness.random.range"),
357        "random_choice" => Some("harness.random.choice"),
358        "random_shuffle" => Some("harness.random.shuffle"),
359        _ => None,
360    }
361}
362
363/// Map an ambient net-capability builtin to its `harness.net.*`
364/// replacement. Backs the `bindings/thread-harness-net` repair. Every
365/// script-facing network effect is exposed only through `HarnessNet`; response
366/// constructors and event encoders remain pure globals.
367pub fn harness_net_replacement(name: &str) -> Option<&'static str> {
368    match name {
369        "http_get" => Some("harness.net.get"),
370        "http_post" => Some("harness.net.post"),
371        "http_put" => Some("harness.net.put"),
372        "http_patch" => Some("harness.net.patch"),
373        "http_delete" => Some("harness.net.delete"),
374        "http_request" => Some("harness.net.request"),
375        "http_download" => Some("harness.net.download"),
376        "http_server" => Some("harness.net.server"),
377        "http_server_after" => Some("harness.net.server_after"),
378        "http_server_before" => Some("harness.net.server_before"),
379        "http_server_on_shutdown" => Some("harness.net.server_on_shutdown"),
380        "http_server_readiness" => Some("harness.net.server_readiness"),
381        "http_server_ready" => Some("harness.net.server_ready"),
382        "http_server_request" => Some("harness.net.server_request"),
383        "http_server_route" => Some("harness.net.server_route"),
384        "http_server_security_headers" => Some("harness.net.server_security_headers"),
385        "http_server_set_ready" => Some("harness.net.server_set_ready"),
386        "http_server_shutdown" => Some("harness.net.server_shutdown"),
387        "http_server_test" => Some("harness.net.server_test"),
388        "http_server_tls_edge" => Some("harness.net.server_tls_edge"),
389        "http_server_tls_pem" => Some("harness.net.server_tls_pem"),
390        "http_server_tls_plain" => Some("harness.net.server_tls_plain"),
391        "http_server_tls_self_signed_dev" => Some("harness.net.server_tls_self_signed_dev"),
392        "http_session" => Some("harness.net.session"),
393        "http_session_close" => Some("harness.net.session_close"),
394        "http_session_request" => Some("harness.net.session_request"),
395        "http_stream_close" => Some("harness.net.stream_close"),
396        "http_stream_info" => Some("harness.net.stream_info"),
397        "http_stream_open" => Some("harness.net.stream_open"),
398        "http_stream_read" => Some("harness.net.stream_read"),
399        "sse_close" => Some("harness.net.sse_close"),
400        "sse_connect" => Some("harness.net.sse_connect"),
401        "sse_receive" => Some("harness.net.sse_receive"),
402        "sse_server_cancel" => Some("harness.net.sse_server_cancel"),
403        "sse_server_cancelled" => Some("harness.net.sse_server_cancelled"),
404        "sse_server_close" => Some("harness.net.sse_server_close"),
405        "sse_server_disconnected" => Some("harness.net.sse_server_disconnected"),
406        "sse_server_flush" => Some("harness.net.sse_server_flush"),
407        "sse_server_heartbeat" => Some("harness.net.sse_server_heartbeat"),
408        "sse_server_response" => Some("harness.net.sse_server_response"),
409        "sse_server_send" => Some("harness.net.sse_server_send"),
410        "sse_server_status" => Some("harness.net.sse_server_status"),
411        "websocket_accept" => Some("harness.net.websocket_accept"),
412        "websocket_close" => Some("harness.net.websocket_close"),
413        "websocket_connect" => Some("harness.net.websocket_connect"),
414        "websocket_receive" => Some("harness.net.websocket_receive"),
415        "websocket_route" => Some("harness.net.websocket_route"),
416        "websocket_send" => Some("harness.net.websocket_send"),
417        "websocket_server" => Some("harness.net.websocket_server"),
418        "websocket_server_close" => Some("harness.net.websocket_server_close"),
419        _ => None,
420    }
421}
422
423/// Render a Rust-style diagnostic message.
424///
425/// Example output:
426/// ```text
427/// error: undefined variable `x`
428///   --> example.harn:5:12
429///    |
430///  5 |     let y = x + 1
431///    |             ^ not found in this scope
432/// ```
433pub fn render_diagnostic(
434    source: &str,
435    filename: &str,
436    span: &Span,
437    severity: &str,
438    message: &str,
439    label: Option<&str>,
440    help: Option<&str>,
441) -> String {
442    render_diagnostic_inner(RenderDiagnostic {
443        source,
444        filename,
445        span,
446        severity,
447        code: None,
448        message,
449        label,
450        help,
451        related: &[],
452        repair: None,
453    })
454}
455
456pub fn render_diagnostic_with_code(
457    source: &str,
458    filename: &str,
459    span: &Span,
460    severity: &str,
461    code: crate::diagnostic_codes::Code,
462    message: &str,
463    label: Option<&str>,
464    help: Option<&str>,
465) -> String {
466    let repair_owned = code.repair_template().map(Repair::from_template);
467    render_diagnostic_inner(RenderDiagnostic {
468        source,
469        filename,
470        span,
471        severity,
472        code: Some(code.as_str()),
473        message,
474        label,
475        help,
476        related: &[],
477        repair: repair_owned.as_ref(),
478    })
479}
480
481pub fn render_diagnostic_with_related(
482    source: &str,
483    filename: &str,
484    span: &Span,
485    severity: &str,
486    message: &str,
487    label: Option<&str>,
488    help: Option<&str>,
489    related: &[RelatedSpanLabel<'_>],
490) -> String {
491    render_diagnostic_inner(RenderDiagnostic {
492        source,
493        filename,
494        span,
495        severity,
496        code: None,
497        message,
498        label,
499        help,
500        related,
501        repair: None,
502    })
503}
504
505struct RenderDiagnostic<'a> {
506    source: &'a str,
507    filename: &'a str,
508    span: &'a Span,
509    severity: &'a str,
510    code: Option<&'a str>,
511    message: &'a str,
512    label: Option<&'a str>,
513    help: Option<&'a str>,
514    related: &'a [RelatedSpanLabel<'a>],
515    repair: Option<&'a Repair>,
516}
517
518fn render_diagnostic_inner(input: RenderDiagnostic<'_>) -> String {
519    let mut out = String::new();
520    let source = input.source;
521    let span = input.span;
522    let severity = input.severity;
523    let message = input.message;
524    let label = input.label;
525    let help = input.help;
526    let related = input.related;
527    let filename = normalize_diagnostic_path(input.filename);
528    let severity_color = severity_color(severity);
529    let gutter = style_fragment("|", Color::Blue, false);
530    let arrow = style_fragment("-->", Color::Blue, true);
531    let help_prefix = style_fragment("help", Color::Cyan, true);
532    let note_prefix = style_fragment("note", Color::Magenta, true);
533
534    out.push_str(&style_fragment(severity, severity_color, true));
535    if let Some(code) = input.code {
536        out.push('[');
537        out.push_str(code);
538        out.push(']');
539    }
540    out.push_str(": ");
541    out.push_str(message);
542    out.push('\n');
543
544    let line_num = span.line;
545    let col_num = span.column;
546
547    let gutter_width = line_num.to_string().len();
548
549    out.push_str(&format!(
550        "{:>width$}{arrow} {filename}:{line_num}:{col_num}\n",
551        " ",
552        width = gutter_width + 1,
553    ));
554
555    out.push_str(&format!(
556        "{:>width$} {gutter}\n",
557        " ",
558        width = gutter_width + 1,
559    ));
560
561    let source_line_opt = line_num.checked_sub(1).and_then(|n| source.lines().nth(n));
562    if let Some(source_line) = source_line_opt {
563        out.push_str(&format!(
564            "{:>width$} {gutter} {source_line}\n",
565            line_num,
566            width = gutter_width + 1,
567        ));
568
569        if let Some(label_text) = label {
570            // Span width must use char count, not byte offsets, so carets align with the source text.
571            let span_len = diagnostic_span_char_len(source, span);
572            let col_num = col_num.max(1);
573            let padding = " ".repeat(col_num - 1);
574            let carets = style_fragment(&"^".repeat(span_len), severity_color, true);
575            out.push_str(&format!(
576                "{:>width$} {gutter} {padding}{carets} {label_text}\n",
577                " ",
578                width = gutter_width + 1,
579            ));
580        }
581    }
582
583    if let Some(help_text) = help {
584        out.push_str(&format!(
585            "{:>width$} = {help_prefix}: {help_text}\n",
586            " ",
587            width = gutter_width + 1,
588        ));
589    }
590
591    if let Some(repair) = input.repair {
592        let repair_prefix = style_fragment("repair", Color::Cyan, true);
593        out.push_str(&format!(
594            "{:>width$} = {repair_prefix}: {} [{}] — {}\n",
595            " ",
596            repair.id,
597            repair.safety,
598            repair.summary,
599            width = gutter_width + 1,
600        ));
601    }
602
603    for item in related {
604        out.push_str(&format!(
605            "{:>width$} = {note_prefix}: {}\n",
606            " ",
607            item.label,
608            width = gutter_width + 1,
609        ));
610        render_related_span(
611            &mut out,
612            source,
613            &filename,
614            item.span,
615            item.label,
616            gutter_width,
617        );
618    }
619
620    if let Some(note_text) = fun_note(severity) {
621        out.push_str(&format!(
622            "{:>width$} = {note_prefix}: {note_text}\n",
623            " ",
624            width = gutter_width + 1,
625        ));
626    }
627
628    out
629}
630
631pub fn render_type_diagnostic(
632    source: &str,
633    filename: &str,
634    diag: &crate::typechecker::TypeDiagnostic,
635) -> String {
636    let severity = match diag.severity {
637        crate::typechecker::DiagnosticSeverity::Error => "error",
638        crate::typechecker::DiagnosticSeverity::Warning => "warning",
639    };
640    let related = diag
641        .related
642        .iter()
643        .map(|related| RelatedSpanLabel {
644            span: &related.span,
645            label: &related.message,
646        })
647        .collect::<Vec<_>>();
648    let primary_label = type_diagnostic_primary_label(diag);
649    match &diag.span {
650        Some(span) => render_diagnostic_inner(RenderDiagnostic {
651            source,
652            filename,
653            span,
654            severity,
655            code: Some(diag.code.as_str()),
656            message: &diag.message,
657            label: primary_label.as_deref(),
658            help: diag.help.as_deref(),
659            related: &related,
660            repair: diag.repair.as_ref(),
661        }),
662        None => match diag.repair.as_ref() {
663            Some(repair) => format!(
664                "{severity}[{}]: {}\n  = repair: {} [{}] — {}\n",
665                diag.code, diag.message, repair.id, repair.safety, repair.summary,
666            ),
667            None => format!("{severity}[{}]: {}\n", diag.code, diag.message),
668        },
669    }
670}
671
672pub fn lexer_error_code(err: &harn_lexer::LexerError) -> crate::diagnostic_codes::Code {
673    match err {
674        harn_lexer::LexerError::UnexpectedCharacter(_, _) => {
675            crate::diagnostic_codes::Code::ParserUnexpectedCharacter
676        }
677        harn_lexer::LexerError::UnterminatedString(_) => {
678            crate::diagnostic_codes::Code::ParserUnterminatedString
679        }
680        harn_lexer::LexerError::UnterminatedBlockComment(_) => {
681            crate::diagnostic_codes::Code::ParserUnterminatedBlockComment
682        }
683        harn_lexer::LexerError::IntegerLiteralOutOfRange(_, _) => {
684            crate::diagnostic_codes::Code::ParserIntegerLiteralOutOfRange
685        }
686    }
687}
688
689pub fn parser_error_code(err: &crate::parser::ParserError) -> crate::diagnostic_codes::Code {
690    match err {
691        crate::parser::ParserError::Unexpected { .. } => {
692            crate::diagnostic_codes::Code::ParserUnexpectedToken
693        }
694        crate::parser::ParserError::UnexpectedEof { .. } => {
695            crate::diagnostic_codes::Code::ParserUnexpectedEof
696        }
697    }
698}
699
700fn type_diagnostic_primary_label(diag: &crate::typechecker::TypeDiagnostic) -> Option<String> {
701    match &diag.details {
702        Some(crate::typechecker::DiagnosticDetails::LintRule { rule }) => {
703            Some(format!("lint[{rule}]"))
704        }
705        Some(crate::typechecker::DiagnosticDetails::TypeMismatch { .. }) => {
706            Some("found this type".to_string())
707        }
708        _ => None,
709    }
710}
711
712fn render_related_span(
713    out: &mut String,
714    source: &str,
715    filename: &str,
716    span: &Span,
717    label: &str,
718    primary_gutter_width: usize,
719) {
720    let filename = normalize_diagnostic_path(filename);
721    let severity_color = Color::Magenta;
722    let gutter = style_fragment("|", Color::Blue, false);
723    let arrow = style_fragment("-->", Color::Blue, true);
724    let line_num = span.line;
725    let col_num = span.column;
726    let gutter_width = primary_gutter_width.max(line_num.to_string().len());
727
728    out.push_str(&format!(
729        "{:>width$}{arrow} {filename}:{line_num}:{col_num}\n",
730        " ",
731        width = gutter_width + 1,
732    ));
733    out.push_str(&format!(
734        "{:>width$} {gutter}\n",
735        " ",
736        width = gutter_width + 1,
737    ));
738
739    if let Some(source_line) = line_num.checked_sub(1).and_then(|n| source.lines().nth(n)) {
740        out.push_str(&format!(
741            "{:>width$} {gutter} {source_line}\n",
742            line_num,
743            width = gutter_width + 1,
744        ));
745        let span_len = diagnostic_span_char_len(source, span);
746        let padding = " ".repeat(col_num.max(1) - 1);
747        let carets = style_fragment(&"^".repeat(span_len), severity_color, true);
748        out.push_str(&format!(
749            "{:>width$} {gutter} {padding}{carets} {label}\n",
750            " ",
751            width = gutter_width + 1,
752        ));
753    }
754}
755
756fn diagnostic_span_char_len(source: &str, span: &Span) -> usize {
757    if span.end <= span.start || span.start >= source.len() {
758        return 1;
759    }
760    let mut start = span.start.min(source.len());
761    while start > 0 && !source.is_char_boundary(start) {
762        start -= 1;
763    }
764    let mut end = span.end.min(source.len());
765    while end < source.len() && !source.is_char_boundary(end) {
766        end += 1;
767    }
768    source
769        .get(start..end)
770        .map(|text| text.chars().count().max(1))
771        .unwrap_or(1)
772}
773
774fn severity_color(severity: &str) -> Color {
775    match severity {
776        "error" => Color::Red,
777        "warning" => Color::Yellow,
778        "note" => Color::Magenta,
779        _ => Color::Cyan,
780    }
781}
782
783fn style_fragment(text: &str, color: Color, bold: bool) -> String {
784    if !colors_enabled() {
785        return text.to_string();
786    }
787
788    let mut paint = Paint::new(text).fg(color);
789    if bold {
790        paint = paint.bold();
791    }
792    paint.to_string()
793}
794
795thread_local! {
796    /// Per-thread override for color output. When `Some`, it wins over the
797    /// `NO_COLOR` env var and TTY detection. This lets tests force colors off
798    /// deterministically without mutating the process-global `NO_COLOR` env
799    /// var, which races across parallel tests (each test runs on its own
800    /// thread, so the override is naturally isolated).
801    static COLOR_OVERRIDE: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
802}
803
804/// Force color output on (`Some(true)`), off (`Some(false)`), or restore the
805/// default env/TTY behavior (`None`) for the current thread only.
806#[cfg(test)]
807pub(crate) fn set_color_override(force: Option<bool>) {
808    COLOR_OVERRIDE.with(|cell| cell.set(force));
809}
810
811fn colors_enabled() -> bool {
812    if let Some(forced) = COLOR_OVERRIDE.with(std::cell::Cell::get) {
813        return forced;
814    }
815    std::env::var_os("NO_COLOR").is_none() && std::io::stderr().is_terminal()
816}
817
818fn fun_note(severity: &str) -> Option<&'static str> {
819    if std::env::var("HARN_FUN").ok().as_deref() != Some("1") {
820        return None;
821    }
822
823    Some(match severity {
824        "error" => "the compiler stepped on a rake here.",
825        "warning" => "this still runs, but it has strong 'double-check me' energy.",
826        _ => "a tiny gremlin has left a note in the margins.",
827    })
828}
829
830pub fn parser_error_message(err: &ParserError) -> String {
831    match err {
832        ParserError::Unexpected { got, expected, .. } => {
833            format!("expected {expected}, found {got}")
834        }
835        ParserError::UnexpectedEof { expected, .. } => {
836            format!("unexpected end of file, expected {expected}")
837        }
838    }
839}
840
841pub fn parser_error_label(err: &ParserError) -> &'static str {
842    match err {
843        ParserError::Unexpected { got, .. } if got == "Newline" => "line break not allowed here",
844        ParserError::Unexpected { .. } => "unexpected token",
845        ParserError::UnexpectedEof { .. } => "file ends here",
846    }
847}
848
849pub fn parser_error_help(err: &ParserError) -> Option<&'static str> {
850    match err {
851        ParserError::UnexpectedEof { expected, .. } | ParserError::Unexpected { expected, .. } => {
852            match expected.as_str() {
853                "}" => Some("add a closing `}` to finish this block"),
854                ")" => Some("add a closing `)` to finish this expression or parameter list"),
855                "]" => Some("add a closing `]` to finish this list or subscript"),
856                "fn, struct, enum, or pipeline after pub" => {
857                    Some("use `pub fn`, `pub pipeline`, `pub enum`, or `pub struct`")
858                }
859                "fn, tool, skill, eval_pack, struct, enum, type, pipeline, const, let, or import after pub" => Some(
860                    "use `pub` with `fn`, `tool`, `skill`, `eval_pack`, `struct`, `enum`, `type`, `pipeline`, `const`, `let`, or `import`",
861                ),
862                _ => None,
863            }
864        }
865    }
866}
867
868#[cfg(test)]
869mod tests {
870    use super::*;
871
872    /// Ensure ANSI colors are off so plain-text assertions work regardless
873    /// of whether the test runner's stderr is a TTY. Uses a thread-local
874    /// override rather than the process-global `NO_COLOR` env var so it can't
875    /// race with color-sensitive assertions in parallel tests.
876    fn disable_colors() {
877        set_color_override(Some(false));
878    }
879
880    #[test]
881    fn test_basic_diagnostic() {
882        disable_colors();
883        let source = "pipeline default(task) {\n    const y = x + 1\n}";
884        let span = Span {
885            start: 28,
886            end: 29,
887            line: 2,
888            column: 13,
889            end_line: 2,
890        };
891        let output = render_diagnostic(
892            source,
893            "example.harn",
894            &span,
895            "error",
896            "undefined variable `x`",
897            Some("not found in this scope"),
898            None,
899        );
900        assert!(output.contains("error: undefined variable `x`"));
901        assert!(output.contains("--> example.harn:2:13"));
902        assert!(output.contains("const y = x + 1"));
903        assert!(output.contains("^ not found in this scope"));
904    }
905
906    #[test]
907    fn test_diagnostic_normalizes_filename() {
908        disable_colors();
909        let source = "const value = thing";
910        let span = Span {
911            start: 12,
912            end: 17,
913            line: 1,
914            column: 13,
915            end_line: 1,
916        };
917        let output = render_diagnostic(
918            source,
919            "/workspace/pipelines/mode/../lib/runtime/loop.harn",
920            &span,
921            "error",
922            "bad value",
923            Some("here"),
924            None,
925        );
926        assert!(output.contains("--> /workspace/pipelines/lib/runtime/loop.harn:1:13"));
927        assert!(!output.contains("/../"));
928    }
929
930    #[test]
931    fn test_diagnostic_with_help() {
932        disable_colors();
933        let source = "const y = xx + 1";
934        let span = Span {
935            start: 8,
936            end: 10,
937            line: 1,
938            column: 9,
939            end_line: 1,
940        };
941        let output = render_diagnostic(
942            source,
943            "test.harn",
944            &span,
945            "error",
946            "undefined variable `xx`",
947            Some("not found in this scope"),
948            Some("did you mean `x`?"),
949        );
950        assert!(output.contains("help: did you mean `x`?"));
951    }
952
953    #[test]
954    fn test_multiline_source() {
955        disable_colors();
956        let source = "line1\nline2\nline3";
957        let span = Span::with_offsets(6, 11, 2, 1); // "line2"
958        let result = render_diagnostic(
959            source,
960            "test.harn",
961            &span,
962            "error",
963            "bad line",
964            Some("here"),
965            None,
966        );
967        assert!(result.contains("line2"));
968        assert!(result.contains("^^^^^"));
969    }
970
971    #[test]
972    fn diagnostic_rendering_tolerates_offsets_inside_utf8_codepoints() {
973        disable_colors();
974        let source = "// capability owner — narrow helper";
975        let em_dash = source.find('—').expect("em dash");
976        let span = Span::with_offsets(0, em_dash + 1, 1, 1);
977        let output = render_diagnostic(
978            source,
979            "unicode.harn",
980            &span,
981            "warning",
982            "legacy comment",
983            Some("rewrite this comment"),
984            None,
985        );
986        assert!(output.contains("capability owner — narrow helper"));
987        assert!(output.contains("rewrite this comment"));
988    }
989
990    #[test]
991    fn test_single_char_span() {
992        disable_colors();
993        let source = "const x = 42";
994        let span = Span::with_offsets(4, 5, 1, 5); // "x"
995        let result = render_diagnostic(
996            source,
997            "test.harn",
998            &span,
999            "warning",
1000            "unused",
1001            Some("never used"),
1002            None,
1003        );
1004        assert!(result.contains('^'));
1005        assert!(result.contains("never used"));
1006    }
1007
1008    #[test]
1009    fn test_with_help() {
1010        disable_colors();
1011        let source = "const y = reponse";
1012        let span = Span::with_offsets(8, 15, 1, 9);
1013        let result = render_diagnostic(
1014            source,
1015            "test.harn",
1016            &span,
1017            "error",
1018            "undefined",
1019            None,
1020            Some("did you mean `response`?"),
1021        );
1022        assert!(result.contains("help:"));
1023        assert!(result.contains("response"));
1024    }
1025
1026    #[test]
1027    fn closest_match_suggests_reordered_snake_case_segments() {
1028        assert_eq!(
1029            find_closest_match("parse_json", ["json_parse"].into_iter(), 2),
1030            Some("json_parse")
1031        );
1032        assert_eq!(
1033            find_closest_match("read_file", ["file_read"].into_iter(), 2),
1034            Some("file_read")
1035        );
1036        assert_eq!(
1037            find_closest_match("parse_json", ["parse_yaml"].into_iter(), 2),
1038            None
1039        );
1040    }
1041
1042    #[test]
1043    fn closest_match_suggests_plain_typo() {
1044        assert_eq!(
1045            find_closest_match("json_pars", ["json_parse"].into_iter(), 2),
1046            Some("json_parse")
1047        );
1048    }
1049
1050    #[test]
1051    fn test_parser_error_helpers_for_eof() {
1052        disable_colors();
1053        let err = ParserError::UnexpectedEof {
1054            expected: "}".into(),
1055            span: Span::with_offsets(10, 10, 3, 1),
1056        };
1057        assert_eq!(
1058            parser_error_message(&err),
1059            "unexpected end of file, expected }"
1060        );
1061        assert_eq!(parser_error_label(&err), "file ends here");
1062        assert_eq!(
1063            parser_error_help(&err),
1064            Some("add a closing `}` to finish this block")
1065        );
1066    }
1067}
1068
1069#[cfg(test)]
1070mod harness_migration_tests {
1071    use super::{
1072        generated_harness_migration, removed_global_replacement, renamed_stdlib_symbol,
1073        HARNESS_MIGRATION_DISAGREEMENTS, HARNESS_REPLACEMENT_FAMILIES,
1074    };
1075
1076    /// The case that motivated harn#6151: the type checker's fuzzy fallback
1077    /// answered `uuid_v5` — a name-based UUID — for the time-ordered `uuid_v7`,
1078    /// while the runtime record one crate away already knew the answer.
1079    #[test]
1080    fn a_migrated_global_resolves_instead_of_falling_through_to_a_guess() {
1081        assert_eq!(
1082            removed_global_replacement("uuid_v7"),
1083            Some("harness.random.uuid_v7")
1084        );
1085        // And the linter's narrower question is unchanged, so `uuid_v7` does
1086        // not start drawing a rename lint on top of its capability lint.
1087        assert_eq!(renamed_stdlib_symbol("uuid_v7"), None);
1088    }
1089
1090    /// The generated table is keyed by builtin name, and a few legacy globals
1091    /// share a name with an unrelated typed method. The hand-written answer has
1092    /// to win, or `harn check` sends a filesystem call to an agent tool.
1093    #[test]
1094    fn a_name_collision_resolves_to_the_migration_not_the_same_named_method() {
1095        for (legacy, migration, reason) in HARNESS_MIGRATION_DISAGREEMENTS {
1096            assert_eq!(
1097                removed_global_replacement(legacy),
1098                Some(*migration),
1099                "`{legacy}` must resolve to its migration — {reason}"
1100            );
1101            assert_ne!(
1102                generated_harness_migration(legacy),
1103                Some(*migration),
1104                "`{legacy}` is pinned as a disagreement but the generated table now \
1105                 agrees; drop it from HARNESS_MIGRATION_DISAGREEMENTS"
1106            );
1107        }
1108    }
1109
1110    /// Every hand-written family entry either matches the generated table or is
1111    /// a pinned disagreement. A fifth divergence fails here rather than shipping
1112    /// a confidently wrong "did you mean".
1113    #[test]
1114    fn no_unreviewed_disagreement_between_the_hand_written_and_generated_tables() {
1115        let pinned: Vec<&str> = HARNESS_MIGRATION_DISAGREEMENTS
1116            .iter()
1117            .map(|(legacy, _, _)| *legacy)
1118            .collect();
1119        let mut unreviewed = Vec::new();
1120        for (legacy, generated) in super::harness_migrations_generated::HARNESS_MIGRATIONS {
1121            if pinned.contains(legacy) {
1122                continue;
1123            }
1124            for family in HARNESS_REPLACEMENT_FAMILIES {
1125                if let Some(hand_written) = family(legacy) {
1126                    if hand_written != *generated {
1127                        unreviewed.push(format!(
1128                            "`{legacy}`: hand-written={hand_written} generated={generated}"
1129                        ));
1130                    }
1131                }
1132            }
1133        }
1134        assert!(
1135            unreviewed.is_empty(),
1136            "these names disagree without a reviewed reason:\n{}",
1137            unreviewed.join("\n")
1138        );
1139    }
1140
1141    /// The generator emits the table sorted; the lookup binary-searches it.
1142    #[test]
1143    fn the_generated_table_is_sorted_and_unique() {
1144        let table = super::harness_migrations_generated::HARNESS_MIGRATIONS;
1145        assert!(table.len() > 100, "expected the whole migrated surface");
1146        for pair in table.windows(2) {
1147            assert!(
1148                pair[0].0 < pair[1].0,
1149                "`{}` and `{}` are out of order or duplicated",
1150                pair[0].0,
1151                pair[1].0
1152            );
1153        }
1154    }
1155}