frontend: provide contextual source repair advice

This commit is contained in:
dev committed 2026-10-09 14:04:40 +03:00
1 parent 008867fe48
commit 2c6c7a3dcd
37 files changed
+562 -24

No files matched your search

+115 -11
View File
@@ -157,6 +157,81 @@ pub const Field = struct {
};
};
pub const RepairCandidate = struct {
kind: []const u8,
hint: []const u8 = "",
alternatives: []const []const u8 = &.{},
};
/// Shared serialization for the envelope and conformance advice goldens.
pub fn writeRepairCandidates(candidates: []const RepairCandidate, s: *std.json.Stringify) std.Io.Writer.Error!void {
try s.beginArray();
for (candidates) |candidate| {
try s.beginObject();
try s.objectField("kind");
try s.write(candidate.kind);
if (candidate.hint.len != 0) {
try s.objectField("hint");
try s.write(candidate.hint);
}
if (candidate.alternatives.len != 0) {
try s.objectField("alternatives");
try s.write(candidate.alternatives);
}
try s.endObject();
}
try s.endArray();
}
/// Closed source advice registry (reference/error-codes.md). Text is fixed;
/// agents select by kind, never by interpreting the hint.
pub const SourceRepairKind = enum {
boolean_negation,
annotate_var_type,
slice_mut_type,
slice_ref_type,
explicit_range_end,
borrow_argument,
borrow_binding,
zero_storage,
numeric_cast_modes,
pub fn hint(kind: SourceRepairKind) []const u8 {
return switch (kind) {
.boolean_negation => "Aji boolean negation is `not`; integer complement is not boolean negation.",
.annotate_var_type => "Write an explicit type: `var name: T = expression;`. `let` may infer its type.",
.slice_mut_type => "Write `slice_mut<T>` for a mutable slice type.",
.slice_ref_type => "Write `slice<T>` for a shared slice type.",
.explicit_range_end => "Write both slice bounds: `receiver[start..end]`; choose the upper bound explicitly.",
.borrow_argument => "Write `ref place` or `mut place` for an inline borrow argument.",
.borrow_binding => "Bind a borrow with `ref name = place;` or `mut name = place;`, rather than a let/var initializer.",
.zero_storage => "`zero` is only a whole var initializer: `var name: T = zero;`; T must be zeroable.",
.numeric_cast_modes => "Choose `as!` for a checked conversion (T0006 on overflow) or `as%` for a wrapping conversion.",
};
}
pub fn code(kind: SourceRepairKind) []const u8 {
return if (kind == .numeric_cast_modes) "E7005" else "E7001";
}
pub fn unit(kind: SourceRepairKind) []const u8 {
return switch (kind) {
.slice_mut_type, .slice_ref_type => "complete type",
.borrow_argument => "prefix",
.numeric_cast_modes => "operator",
else => "none",
};
}
pub fn candidate(kind: SourceRepairKind, alternatives: []const []const u8) RepairCandidate {
return .{ .kind = @tagName(kind), .hint = kind.hint(), .alternatives = alternatives };
}
pub fn one(comptime kind: SourceRepairKind) []const RepairCandidate {
return comptime &.{kind.candidate(if (kind == .numeric_cast_modes) &.{ "as!", "as%" } else &.{})};
}
};
pub const Diagnostic = struct {
code: *const Code,
severity: Severity = .@"error",
@@ -165,7 +240,7 @@ pub const Diagnostic = struct {
message: []const u8 = "",
fields: []const Field = &.{},
/// Non-binding structured suggestions (spec 10 §3).
repair_candidates: []const []const u8 = &.{},
repair_candidates: []const RepairCandidate = &.{},
related_locations: []const RelatedLocation = &.{},
/// Writes the diagnostic as a single JSON object (no trailing newline).
@@ -199,14 +274,7 @@ pub const Diagnostic = struct {
}
if (d.repair_candidates.len != 0) {
try s.objectField("repair_candidates");
try s.beginArray();
for (d.repair_candidates) |kind| {
try s.beginObject();
try s.objectField("kind");
try s.write(kind);
try s.endObject();
}
try s.endArray();
try writeRepairCandidates(d.repair_candidates, &s);
}
if (d.related_locations.len != 0) {
try s.objectField("related_locations");
@@ -249,7 +317,7 @@ test "writeJson is deterministic and structured" {
.{ .name = "value", .value = .{ .str = "%17" } },
.{ .name = "expected_generation", .value = .{ .int = 5 } },
},
.repair_candidates = &.{"move_reset_after_use"},
.repair_candidates = &.{.{ .kind = "move_reset_after_use" }},
};
try d.writeJson(&w);
try std.testing.expectEqualStrings(
@@ -265,7 +333,7 @@ test "related locations are typed, ordered and appended after existing fields" {
.span = .{ .file = "main.aji", .line = 9, .col = 5 },
.fields = &.{.{ .name = "conflicts_with", .value = .{ .str = "%view_4" } }},
.message = "conflict",
.repair_candidates = &.{"example"},
.repair_candidates = &.{.{ .kind = "example" }},
.related_locations = &.{.{
.role = .conflicting_borrow,
.value = "%view_4",
@@ -335,6 +403,42 @@ test "registry ids are unique" {
}
}
test "source repair registry mirrors kinds, codes, hints and units" {
const md = @embedFile("error_codes_md");
const heading = "## Source repair candidate kinds\n";
const start = (std.mem.indexOf(u8, md, heading) orelse return error.RegistryOutOfSync) + heading.len;
const section = md[start..];
const end = std.mem.indexOf(u8, section, "\n## ") orelse section.len;
var lines = std.mem.splitScalar(u8, section[0..end], '\n');
var seen: std.EnumSet(SourceRepairKind) = .empty;
while (lines.next()) |line| {
if (!std.mem.startsWith(u8, line, "| `")) continue;
var cols = std.mem.tokenizeScalar(u8, line, '|');
const name = std.mem.trim(u8, cols.next().?, " `");
const kind = std.meta.stringToEnum(SourceRepairKind, name) orelse return error.RegistryOutOfSync;
try std.testing.expect(!seen.contains(kind));
seen.insert(kind);
try std.testing.expectEqualStrings(kind.code(), std.mem.trim(u8, cols.next().?, " "));
try std.testing.expectEqualStrings(kind.hint(), std.mem.trim(u8, cols.next().?, " "));
try std.testing.expectEqualStrings(kind.unit(), std.mem.trim(u8, cols.next().?, " "));
try std.testing.expect(cols.next() == null);
}
try std.testing.expect(seen.eql(.full));
}
test "repair hints and alternatives escape JSON with deterministic member order" {
var out: std.Io.Writer.Allocating = .init(std.testing.allocator);
defer out.deinit();
const d: Diagnostic = .{
.code = lookup("E7001").?,
.repair_candidates = &.{.{ .kind = "example", .hint = "quote \" and newline\n", .alternatives = &.{ "a\\b", "c" } }},
};
try d.writeJson(&out.writer);
try std.testing.expectEqualStrings(
\\{"code":"E7001","kind":"source.syntax","severity":"error","repair_candidates":[{"kind":"example","hint":"quote \" and newline\n","alternatives":["a\\b","c"]}]}
, out.written());
}
test "E7006 reason registry mirrors documentation" {
const md = @embedFile("error_codes_md");
const heading = "## E7006 reasons\n";