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, source_line, 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, source_line, 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, source_line: &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    let span_len = source
769        .get(start..end)
770        .map(|text| text.lines().next().unwrap_or(text).chars().count().max(1))
771        .unwrap_or(1);
772    let visible_line_len = source_line
773        .chars()
774        .count()
775        .saturating_sub(span.column.saturating_sub(1))
776        .max(1);
777    span_len.min(visible_line_len)
778}
779
780fn severity_color(severity: &str) -> Color {
781    match severity {
782        "error" => Color::Red,
783        "warning" => Color::Yellow,
784        "note" => Color::Magenta,
785        _ => Color::Cyan,
786    }
787}
788
789fn style_fragment(text: &str, color: Color, bold: bool) -> String {
790    if !colors_enabled() {
791        return text.to_string();
792    }
793
794    let mut paint = Paint::new(text).fg(color);
795    if bold {
796        paint = paint.bold();
797    }
798    paint.to_string()
799}
800
801thread_local! {
802    /// Per-thread override for color output. When `Some`, it wins over the
803    /// `NO_COLOR` env var and TTY detection. This lets tests force colors off
804    /// deterministically without mutating the process-global `NO_COLOR` env
805    /// var, which races across parallel tests (each test runs on its own
806    /// thread, so the override is naturally isolated).
807    static COLOR_OVERRIDE: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
808}
809
810/// Force color output on (`Some(true)`), off (`Some(false)`), or restore the
811/// default env/TTY behavior (`None`) for the current thread only.
812#[cfg(test)]
813pub(crate) fn set_color_override(force: Option<bool>) {
814    COLOR_OVERRIDE.with(|cell| cell.set(force));
815}
816
817fn colors_enabled() -> bool {
818    if let Some(forced) = COLOR_OVERRIDE.with(std::cell::Cell::get) {
819        return forced;
820    }
821    std::env::var_os("NO_COLOR").is_none() && std::io::stderr().is_terminal()
822}
823
824fn fun_note(severity: &str) -> Option<&'static str> {
825    if std::env::var("HARN_FUN").ok().as_deref() != Some("1") {
826        return None;
827    }
828
829    Some(match severity {
830        "error" => "the compiler stepped on a rake here.",
831        "warning" => "this still runs, but it has strong 'double-check me' energy.",
832        _ => "a tiny gremlin has left a note in the margins.",
833    })
834}
835
836pub fn parser_error_message(err: &ParserError) -> String {
837    match err {
838        ParserError::Unexpected { got, expected, .. } => {
839            format!("expected {expected}, found {got}")
840        }
841        ParserError::UnexpectedEof { expected, .. } => {
842            format!("unexpected end of file, expected {expected}")
843        }
844    }
845}
846
847pub fn parser_error_label(err: &ParserError) -> &'static str {
848    match err {
849        ParserError::Unexpected { got, .. } if got == "Newline" => "line break not allowed here",
850        ParserError::Unexpected { .. } => "unexpected token",
851        ParserError::UnexpectedEof { .. } => "file ends here",
852    }
853}
854
855pub fn parser_error_help(err: &ParserError) -> Option<&'static str> {
856    match err {
857        ParserError::UnexpectedEof { expected, .. } | ParserError::Unexpected { expected, .. } => {
858            match expected.as_str() {
859                "}" => Some("add a closing `}` to finish this block"),
860                ")" => Some("add a closing `)` to finish this expression or parameter list"),
861                "]" => Some("add a closing `]` to finish this list or subscript"),
862                "fn, struct, enum, or pipeline after pub" => {
863                    Some("use `pub fn`, `pub pipeline`, `pub enum`, or `pub struct`")
864                }
865                "fn, tool, skill, eval_pack, struct, enum, type, pipeline, const, let, or import after pub" => Some(
866                    "use `pub` with `fn`, `tool`, `skill`, `eval_pack`, `struct`, `enum`, `type`, `pipeline`, `const`, `let`, or `import`",
867                ),
868                _ => None,
869            }
870        }
871    }
872}
873
874#[cfg(test)]
875mod tests {
876    use super::*;
877
878    /// Ensure ANSI colors are off so plain-text assertions work regardless
879    /// of whether the test runner's stderr is a TTY. Uses a thread-local
880    /// override rather than the process-global `NO_COLOR` env var so it can't
881    /// race with color-sensitive assertions in parallel tests.
882    fn disable_colors() {
883        set_color_override(Some(false));
884    }
885
886    #[test]
887    fn test_basic_diagnostic() {
888        disable_colors();
889        let source = "pipeline default(task) {\n    const y = x + 1\n}";
890        let span = Span {
891            start: 28,
892            end: 29,
893            line: 2,
894            column: 13,
895            end_line: 2,
896        };
897        let output = render_diagnostic(
898            source,
899            "example.harn",
900            &span,
901            "error",
902            "undefined variable `x`",
903            Some("not found in this scope"),
904            None,
905        );
906        assert!(output.contains("error: undefined variable `x`"));
907        assert!(output.contains("--> example.harn:2:13"));
908        assert!(output.contains("const y = x + 1"));
909        assert!(output.contains("^ not found in this scope"));
910    }
911
912    #[test]
913    fn test_diagnostic_normalizes_filename() {
914        disable_colors();
915        let source = "const value = thing";
916        let span = Span {
917            start: 12,
918            end: 17,
919            line: 1,
920            column: 13,
921            end_line: 1,
922        };
923        let output = render_diagnostic(
924            source,
925            "/workspace/pipelines/mode/../lib/runtime/loop.harn",
926            &span,
927            "error",
928            "bad value",
929            Some("here"),
930            None,
931        );
932        assert!(output.contains("--> /workspace/pipelines/lib/runtime/loop.harn:1:13"));
933        assert!(!output.contains("/../"));
934    }
935
936    #[test]
937    fn test_diagnostic_with_help() {
938        disable_colors();
939        let source = "const y = xx + 1";
940        let span = Span {
941            start: 8,
942            end: 10,
943            line: 1,
944            column: 9,
945            end_line: 1,
946        };
947        let output = render_diagnostic(
948            source,
949            "test.harn",
950            &span,
951            "error",
952            "undefined variable `xx`",
953            Some("not found in this scope"),
954            Some("did you mean `x`?"),
955        );
956        assert!(output.contains("help: did you mean `x`?"));
957    }
958
959    #[test]
960    fn test_multiline_source() {
961        disable_colors();
962        let source = "line1\nline2\nline3";
963        let span = Span::with_offsets(6, 11, 2, 1); // "line2"
964        let result = render_diagnostic(
965            source,
966            "test.harn",
967            &span,
968            "error",
969            "bad line",
970            Some("here"),
971            None,
972        );
973        assert!(result.contains("line2"));
974        assert!(result.contains("^^^^^"));
975    }
976
977    #[test]
978    fn multiline_span_underline_stops_at_rendered_source_line() {
979        disable_colors();
980        let source =
981            "fn make() -> Config {\n  return {\n    first: true,\n    second: false,\n  }\n}";
982        let start = source.find("return {").expect("return record") + "return ".len();
983        let end = source.rfind('}').expect("record end") + 1;
984        let span = Span {
985            start,
986            end,
987            line: 2,
988            column: 10,
989            end_line: 5,
990        };
991
992        let output = render_diagnostic(
993            source,
994            "test.harn",
995            &span,
996            "error",
997            "return value has the wrong type",
998            Some("found this type"),
999            None,
1000        );
1001
1002        assert!(
1003            output.contains(" 2 |   return {\n   |          ^ found this type\n"),
1004            "{output}"
1005        );
1006
1007        let primary = Span::with_offsets(13, 19, 1, 14);
1008        let related = [RelatedSpanLabel {
1009            span: &span,
1010            label: "return type declared here",
1011        }];
1012        let output = render_diagnostic_with_related(
1013            source,
1014            "test.harn",
1015            &primary,
1016            "error",
1017            "return value has the wrong type",
1018            Some("expected type"),
1019            None,
1020            &related,
1021        );
1022
1023        assert!(
1024            output.contains(
1025                "  = note: return type declared here\n  --> test.harn:2:10\n   |\n 2 |   return {\n   |          ^ return type declared here\n"
1026            ),
1027            "{output}"
1028        );
1029    }
1030
1031    #[test]
1032    fn diagnostic_rendering_tolerates_offsets_inside_utf8_codepoints() {
1033        disable_colors();
1034        let source = "// capability owner — narrow helper";
1035        let em_dash = source.find('—').expect("em dash");
1036        let span = Span::with_offsets(0, em_dash + 1, 1, 1);
1037        let output = render_diagnostic(
1038            source,
1039            "unicode.harn",
1040            &span,
1041            "warning",
1042            "legacy comment",
1043            Some("rewrite this comment"),
1044            None,
1045        );
1046        assert!(output.contains("capability owner — narrow helper"));
1047        assert!(output.contains("rewrite this comment"));
1048    }
1049
1050    #[test]
1051    fn test_single_char_span() {
1052        disable_colors();
1053        let source = "const x = 42";
1054        let span = Span::with_offsets(4, 5, 1, 5); // "x"
1055        let result = render_diagnostic(
1056            source,
1057            "test.harn",
1058            &span,
1059            "warning",
1060            "unused",
1061            Some("never used"),
1062            None,
1063        );
1064        assert!(result.contains('^'));
1065        assert!(result.contains("never used"));
1066    }
1067
1068    #[test]
1069    fn test_with_help() {
1070        disable_colors();
1071        let source = "const y = reponse";
1072        let span = Span::with_offsets(8, 15, 1, 9);
1073        let result = render_diagnostic(
1074            source,
1075            "test.harn",
1076            &span,
1077            "error",
1078            "undefined",
1079            None,
1080            Some("did you mean `response`?"),
1081        );
1082        assert!(result.contains("help:"));
1083        assert!(result.contains("response"));
1084    }
1085
1086    #[test]
1087    fn closest_match_suggests_reordered_snake_case_segments() {
1088        assert_eq!(
1089            find_closest_match("parse_json", ["json_parse"].into_iter(), 2),
1090            Some("json_parse")
1091        );
1092        assert_eq!(
1093            find_closest_match("read_file", ["file_read"].into_iter(), 2),
1094            Some("file_read")
1095        );
1096        assert_eq!(
1097            find_closest_match("parse_json", ["parse_yaml"].into_iter(), 2),
1098            None
1099        );
1100    }
1101
1102    #[test]
1103    fn closest_match_suggests_plain_typo() {
1104        assert_eq!(
1105            find_closest_match("json_pars", ["json_parse"].into_iter(), 2),
1106            Some("json_parse")
1107        );
1108    }
1109
1110    #[test]
1111    fn test_parser_error_helpers_for_eof() {
1112        disable_colors();
1113        let err = ParserError::UnexpectedEof {
1114            expected: "}".into(),
1115            span: Span::with_offsets(10, 10, 3, 1),
1116        };
1117        assert_eq!(
1118            parser_error_message(&err),
1119            "unexpected end of file, expected }"
1120        );
1121        assert_eq!(parser_error_label(&err), "file ends here");
1122        assert_eq!(
1123            parser_error_help(&err),
1124            Some("add a closing `}` to finish this block")
1125        );
1126    }
1127}
1128
1129#[cfg(test)]
1130mod harness_migration_tests {
1131    use super::{
1132        generated_harness_migration, removed_global_replacement, renamed_stdlib_symbol,
1133        HARNESS_MIGRATION_DISAGREEMENTS, HARNESS_REPLACEMENT_FAMILIES,
1134    };
1135
1136    /// The case that motivated harn#6151: the type checker's fuzzy fallback
1137    /// answered `uuid_v5` — a name-based UUID — for the time-ordered `uuid_v7`,
1138    /// while the runtime record one crate away already knew the answer.
1139    #[test]
1140    fn a_migrated_global_resolves_instead_of_falling_through_to_a_guess() {
1141        assert_eq!(
1142            removed_global_replacement("uuid_v7"),
1143            Some("harness.random.uuid_v7")
1144        );
1145        // And the linter's narrower question is unchanged, so `uuid_v7` does
1146        // not start drawing a rename lint on top of its capability lint.
1147        assert_eq!(renamed_stdlib_symbol("uuid_v7"), None);
1148    }
1149
1150    /// The generated table is keyed by builtin name, and a few legacy globals
1151    /// share a name with an unrelated typed method. The hand-written answer has
1152    /// to win, or `harn check` sends a filesystem call to an agent tool.
1153    #[test]
1154    fn a_name_collision_resolves_to_the_migration_not_the_same_named_method() {
1155        for (legacy, migration, reason) in HARNESS_MIGRATION_DISAGREEMENTS {
1156            assert_eq!(
1157                removed_global_replacement(legacy),
1158                Some(*migration),
1159                "`{legacy}` must resolve to its migration — {reason}"
1160            );
1161            assert_ne!(
1162                generated_harness_migration(legacy),
1163                Some(*migration),
1164                "`{legacy}` is pinned as a disagreement but the generated table now \
1165                 agrees; drop it from HARNESS_MIGRATION_DISAGREEMENTS"
1166            );
1167        }
1168    }
1169
1170    /// Every hand-written family entry either matches the generated table or is
1171    /// a pinned disagreement. A fifth divergence fails here rather than shipping
1172    /// a confidently wrong "did you mean".
1173    #[test]
1174    fn no_unreviewed_disagreement_between_the_hand_written_and_generated_tables() {
1175        let pinned: Vec<&str> = HARNESS_MIGRATION_DISAGREEMENTS
1176            .iter()
1177            .map(|(legacy, _, _)| *legacy)
1178            .collect();
1179        let mut unreviewed = Vec::new();
1180        for (legacy, generated) in super::harness_migrations_generated::HARNESS_MIGRATIONS {
1181            if pinned.contains(legacy) {
1182                continue;
1183            }
1184            for family in HARNESS_REPLACEMENT_FAMILIES {
1185                if let Some(hand_written) = family(legacy) {
1186                    if hand_written != *generated {
1187                        unreviewed.push(format!(
1188                            "`{legacy}`: hand-written={hand_written} generated={generated}"
1189                        ));
1190                    }
1191                }
1192            }
1193        }
1194        assert!(
1195            unreviewed.is_empty(),
1196            "these names disagree without a reviewed reason:\n{}",
1197            unreviewed.join("\n")
1198        );
1199    }
1200
1201    /// The generator emits the table sorted; the lookup binary-searches it.
1202    #[test]
1203    fn the_generated_table_is_sorted_and_unique() {
1204        let table = super::harness_migrations_generated::HARNESS_MIGRATIONS;
1205        assert!(table.len() > 100, "expected the whole migrated surface");
1206        for pair in table.windows(2) {
1207            assert!(
1208                pair[0].0 < pair[1].0,
1209                "`{}` and `{}` are out of order or duplicated",
1210                pair[0].0,
1211                pair[1].0
1212            );
1213        }
1214    }
1215}