Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python
8.1 KB
Raw Blame
//! Notable-line excerpt for a #440 handle preview.
//!
//! The first payload a spill returns includes the head and tail. A needle in
//! the middle (a single FAILED line in a 168 KiB log) is invisible there, so
//! the model pays a follow-up page. This file pulls a bounded set of
//! error-shaped lines into that first payload so the common "what broke?"
//! question does not need the pager. The full bytes still live only on disk.

const std = @import("std");
const Allocator = std.mem.Allocator;

/// Substrings that mark a line worth surfacing. Matched case-insensitively
/// as a literal, so `fail` hits `FAILED` and `error` hits `level=ERROR`.
pub const notable_needles = [_][]const u8{
    "error",
    "fail",
    "exception",
    "panic",
    "fatal",
    "traceback",
};

pub const max_notable_lines: usize = 8;
pub const max_notable_bytes: usize = 2048;

/// Inclusive-start, exclusive-end of the line that contains `at`.
pub fn lineBounds(text: []const u8, at: usize) struct { lo: usize, hi: usize } {
    const pos = @min(at, text.len);
    const lo = if (std.mem.lastIndexOfScalar(u8, text[0..pos], '\n')) |n| n + 1 else 0;
    const hi = if (std.mem.indexOfScalar(u8, text[pos..], '\n')) |n| pos + n else text.len;
    return .{ .lo = lo, .hi = hi };
}

pub fn indexOfIgnoreCase(hay: []const u8, needle: []const u8) ?usize {
    if (needle.len == 0 or needle.len > hay.len) return null;
    var i: usize = 0;
    while (i + needle.len <= hay.len) : (i += 1) {
        if (std.ascii.eqlIgnoreCase(hay[i..][0..needle.len], needle)) return i;
    }
    return null;
}

fn lineIsNotable(line: []const u8) bool {
    for (notable_needles) |n| {
        if (indexOfIgnoreCase(line, n) != null) return true;
    }
    return false;
}

/// Lines after `skip_before` that look like failures, capped. Empty when
/// nothing qualifies or `budget` cannot hold the header plus one line.
pub fn notableExcerpt(arena: Allocator, text: []const u8, skip_before: usize, budget: usize) ![]const u8 {
    return notableRange(arena, text, skip_before, text.len, budget);
}

/// Only select lines intersecting the omitted source range. A partially
/// visible boundary line may be repeated in full so its evidence is not lost.
pub fn notableRange(arena: Allocator, text: []const u8, from: usize, until: usize, budget: usize) ![]const u8 {
    const end = @min(until, text.len);
    if (budget < 40 or from >= end) return "";
    var lines: [max_notable_lines][]const u8 = undefined;
    var count: usize = 0;
    const header_reserve = "[notable lines, 8 matches]\n".len;
    var remaining = budget - header_reserve;
    var i = lineBounds(text, from).lo;
    while (i < end and count < max_notable_lines) {
        const nl = std.mem.indexOfScalar(u8, text[i..], '\n');
        const line_end = if (nl) |n| i + n else text.len;
        const line = text[i..line_end];
        if (line_end > from and line.len < remaining and lineIsNotable(line)) {
            lines[count] = line;
            count += 1;
            remaining -= line.len + 1;
        }
        i = if (nl) |n| i + n + 1 else text.len;
    }
    if (count == 0) return "";
    var body: std.Io.Writer.Allocating = .init(arena);
    try body.writer.print("[notable lines, {d} {s}]\n", .{ count, if (count == 1) "match" else "matches" });
    for (lines[0..count]) |line| {
        try body.writer.writeAll(line);
        try body.writer.writeByte('\n');
    }
    return body.writer.buffered();
}

test "lineBounds includes the whole line, not an 80-byte pad" {
    const text = "aaa\nrequest_id=req-1 status=FAILED reason=boom\nzzz";
    const at = std.mem.indexOf(u8, text, "FAILED").?;
    const b = lineBounds(text, at);
    try std.testing.expectEqualStrings("request_id=req-1 status=FAILED reason=boom", text[b.lo..b.hi]);
}

test "notableExcerpt pulls a FAILED line that sits past the head" {
    var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena_state.deinit();
    const a = arena_state.allocator();
    const head = try a.alloc(u8, 600);
    @memset(head, 'x');
    head[head.len - 1] = '\n';
    const fail = "request_id=req-8c41de07 status=FAILED reason=undefined_symbol\n";
    const text = try std.fmt.allocPrint(a, "{s}{s}tail\n", .{ head, fail });
    const excerpt = try notableExcerpt(a, text, head.len, 512);
    try std.testing.expect(std.mem.indexOf(u8, excerpt, "req-8c41de07") != null);
    try std.testing.expect(std.mem.indexOf(u8, excerpt, "FAILED") != null);
    try std.testing.expect(std.mem.indexOf(u8, excerpt, "1 match") != null);
}

test "notableExcerpt stays empty when nothing looks like a failure" {
    var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena_state.deinit();
    const a = arena_state.allocator();
    const excerpt = try notableExcerpt(a, "cache=miss status=OK\nall good\n", 0, 512);
    try std.testing.expectEqualStrings("", excerpt);
}

pub const head_lines: usize = 24;
pub const tail_lines: usize = 12;
pub const head_bytes: usize = 2048;
pub const tail_bytes: usize = 1024;

fn takeLines(text: []const u8, from_start: bool, max_lines: usize, max_bytes: usize) []const u8 {
    if (text.len == 0 or max_lines == 0 or max_bytes == 0) return "";
    if (from_start) {
        var lines: usize = 0;
        var i: usize = 0;
        while (i < text.len and i < max_bytes and lines < max_lines) {
            if (text[i] == '\n') lines += 1;
            i += 1;
        }
        while (i > 0 and i < text.len and (text[i] & 0xC0) == 0x80) i -= 1;
        return text[0..i];
    }
    var lines: usize = 0;
    var i = text.len;
    while (i > 0 and (text.len - i) < max_bytes and lines < max_lines) {
        i -= 1;
        if (text[i] == '\n') lines += 1;
    }
    if (i > 0 and text[i] == '\n') i += 1;
    while (i < text.len and (text[i] & 0xC0) == 0x80) i += 1;
    return text[i..];
}

/// First/last excerpt for a spilled result. Empty when `budget` cannot hold
/// a useful head. The full bytes stay on the handle.
pub const Preview = struct {
    text: []const u8,
    omitted_start: usize,
    omitted_end: usize,
};

pub fn prefixPreview(text: []const u8, budget: usize) Preview {
    const prefix = utilUtf8Prefix(text, budget);
    return .{ .text = prefix, .omitted_start = prefix.len, .omitted_end = text.len };
}

pub fn compactPreview(arena: Allocator, text: []const u8, budget: usize) ![]const u8 {
    return (try compactPreviewRanges(arena, text, budget)).text;
}

pub fn compactPreviewRanges(arena: Allocator, text: []const u8, budget: usize) !Preview {
    if (budget < 80) return prefixPreview(text, budget);
    const head = takeLines(text, true, head_lines, @min(head_bytes, budget / 2));
    const tail = takeLines(text, false, tail_lines, @min(tail_bytes, budget / 4));
    if (tail.len == 0 or std.mem.endsWith(u8, head, tail) or head.len + tail.len >= text.len)
        return prefixPreview(text, budget);
    const gap = "[... {d} bytes omitted; page with read_tool_result(handle, offset, limit) or query ...]\n";
    const omitted = text.len - head.len - tail.len;
    var aw: std.Io.Writer.Allocating = .init(arena);
    try aw.writer.writeAll(head);
    if (head.len == 0 or head[head.len - 1] != '\n') try aw.writer.writeByte('\n');
    try aw.writer.print(gap, .{omitted});
    try aw.writer.writeAll(tail);
    const out = aw.writer.buffered();
    if (out.len > budget) return prefixPreview(text, budget);
    return .{ .text = out, .omitted_start = head.len, .omitted_end = text.len - tail.len };
}

fn utilUtf8Prefix(text: []const u8, n: usize) []const u8 {
    return @import("util.zig").utf8Prefix(text, n);
}

test "compactPreview keeps the head and a tail window (#808)" {
    const a = std.testing.allocator;
    var arena_state = std.heap.ArenaAllocator.init(a);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    const big = try arena.alloc(u8, 8000);
    @memset(big, 'x');
    @memcpy(big[0..5], "HEAD\n");
    @memcpy(big[big.len - 5 ..], "\nTAIL");
    const got = try compactPreview(arena, big, 4096);
    try std.testing.expect(std.mem.startsWith(u8, got, "HEAD"));
    try std.testing.expect(std.mem.indexOf(u8, got, "TAIL") != null);
    try std.testing.expect(std.mem.indexOf(u8, got, "read_tool_result") != null);
    try std.testing.expect(got.len < big.len);
}