Skip to content

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

Python
9.4 KB
Raw Blame
//! #761: optional `read_file` fields must stay optional, and a path-only
//! (or null/empty optional) call is a whole-file read. Reached from
//! exec.zig's test block.

const std = @import("std");
const builtin = @import("builtin");
const Value = std.json.Value;

const exec = @import("exec.zig");
const tools = @import("tools.zig");

fn testCtx(client: *std.http.Client, cwd: []const u8, tracker: ?*@import("read_miss.zig").Tracker) tools.ToolCtx {
    return .{
        .gpa = std.testing.allocator,
        .io = std.testing.io,
        .client = client,
        .provider = undefined,
        .registry = null,
        .from_sub = false,
        .approvals = null,
        .tracer = null,
        .agent_cwd = cwd,
        .read_miss = tracker,
    };
}

fn readFile(cwd: []const u8, input_json: []const u8) !tools.ToolOutput {
    return readFileTracked(cwd, input_json, null);
}

fn readFileTracked(cwd: []const u8, input_json: []const u8, tracker: ?*@import("read_miss.zig").Tracker) !tools.ToolOutput {
    var parsed = try std.json.parseFromSlice(Value, std.testing.allocator, input_json, .{});
    defer parsed.deinit();
    var client: std.http.Client = undefined;
    const out = exec.execTool(testCtx(&client, cwd, tracker), .{
        .id = "call_1",
        .name = "read_file",
        .input = parsed.value,
    });
    if (tracker) |t| t.noteOutput("read_file", parsed.value, out.text, out.is_error);
    return out;
}

test "read_file (#761): a call with only path is a whole-file read" {
    if (builtin.os.tag == .windows) return error.SkipZigTest;
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(io, .{ .sub_path = "note.txt", .data = "hello whole file\n" });
    var real_buf: [std.fs.max_path_bytes]u8 = undefined;
    const n = try tmp.dir.realPath(io, &real_buf);
    const cwd = real_buf[0..n];

    const only_path = try readFile(cwd, "{\"path\":\"note.txt\"}");
    defer gpa.free(only_path.text);
    try std.testing.expect(!only_path.is_error);
    try std.testing.expectEqualStrings("hello whole file\n", only_path.text);

    const nulls = try readFile(cwd,
        \\{"path":"note.txt","start_line":null,"end_line":null,"contains":null,"compact":null}
    );
    defer gpa.free(nulls.text);
    try std.testing.expect(!nulls.is_error);
    try std.testing.expectEqualStrings("hello whole file\n", nulls.text);

    const empty_contains = try readFile(cwd, "{\"path\":\"note.txt\",\"contains\":\"\"}");
    defer gpa.free(empty_contains.text);
    try std.testing.expect(!empty_contains.is_error);
    try std.testing.expectEqualStrings("hello whole file\n", empty_contains.text);

    const combined = try readFile(cwd, "{\"path\":\"note.txt\",\"contains\":\"hello\",\"start_line\":1}");
    defer gpa.free(combined.text);
    try std.testing.expect(combined.is_error);
    try std.testing.expect(std.mem.indexOf(u8, combined.text, "cannot be combined") != null);
}

test "#1116: sequential missing reads under one prefix stop and point at list_dir" {
    if (builtin.os.tag == .windows) return error.SkipZigTest;
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tracker: @import("read_miss.zig").Tracker = .{};
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(io, .{ .sub_path = "real.txt", .data = "kept\n" });
    var real_buf: [std.fs.max_path_bytes]u8 = undefined;
    const n = try tmp.dir.realPath(io, &real_buf);
    const cwd = real_buf[0..n];

    const first = try readFileTracked(cwd, "{\"path\":\"docs/adr/0365-one.md\"}", &tracker);
    defer gpa.free(first.text);
    try std.testing.expect(first.is_error);
    try std.testing.expect(std.mem.indexOf(u8, first.text, "does not exist") != null);

    const second = try readFileTracked(cwd, "{\"path\":\"docs/adr/0366-two.md\"}", &tracker);
    defer gpa.free(second.text);
    try std.testing.expect(second.is_error);
    try std.testing.expect(std.mem.indexOf(u8, second.text, "does not exist") != null);

    const third = try readFileTracked(cwd, "{\"path\":\"docs/adr/0367-three.md\"}", &tracker);
    defer gpa.free(third.text);
    try std.testing.expect(third.is_error);
    try std.testing.expect(std.mem.indexOf(u8, third.text, "does not exist") != null);

    const fourth = try readFileTracked(cwd, "{\"path\":\"docs/adr/0397-reshuffle.md\"}", &tracker);
    defer gpa.free(fourth.text);
    try std.testing.expect(fourth.is_error);
    try std.testing.expect(std.mem.indexOf(u8, fourth.text, "refused") != null);
    try std.testing.expect(std.mem.indexOf(u8, fourth.text, "codedb list_dir") != null);

    const other = try readFileTracked(cwd, "{\"path\":\"missing-elsewhere.txt\"}", &tracker);
    defer gpa.free(other.text);
    try std.testing.expect(other.is_error);
    try std.testing.expect(std.mem.indexOf(u8, other.text, "does not exist") != null);

    const kept = try readFileTracked(cwd, "{\"path\":\"real.txt\"}", &tracker);
    defer gpa.free(kept.text);
    try std.testing.expect(!kept.is_error);
    try std.testing.expectEqualStrings("kept\n", kept.text);
}

fn nativeCall(cwd: []const u8, name: []const u8, json: []const u8) !tools.ToolOutput {
    var parsed = try std.json.parseFromSlice(Value, std.testing.allocator, json, .{});
    defer parsed.deinit();
    var client: std.http.Client = undefined;
    return exec.execTool(testCtx(&client, cwd, null), .{
        .id = "symlink-fixture",
        .name = name,
        .input = parsed.value,
    });
}

fn dependencyFixture(tmp: *std.testing.TmpDir) !void {
    if (builtin.os.tag == .windows) return error.SkipZigTest;
    const io = std.testing.io;
    try tmp.dir.createDirPath(io, "checkout/node_modules/.store/pkg/docs");
    try tmp.dir.createDirPath(io, "outside");
    try tmp.dir.writeFile(io, .{ .sub_path = "checkout/node_modules/.store/pkg/docs/guide.md", .data = "dependency guide\n" });
    try tmp.dir.writeFile(io, .{ .sub_path = "outside/secret.md", .data = "outside\n" });
    try tmp.dir.symLink(io, ".store/pkg", "checkout/node_modules/pkg", .{ .is_directory = true });
    try tmp.dir.symLink(io, "../outside", "checkout/escape", .{ .is_directory = true });
    try tmp.dir.symLink(io, "../outside/secret.md", "checkout/secret.md", .{});
    try tmp.dir.symLink(io, "../outside/missing.md", "checkout/dangling.md", .{});
}

test "native tools #1748: read_file and codedb list_dir accept in-root dependency links only" {
    const gpa = std.testing.allocator;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try dependencyFixture(&tmp);
    const root = try tmp.dir.realPathFileAlloc(std.testing.io, "checkout", gpa);
    defer gpa.free(root);
    const read = try readFile(root, "{\"path\":\"node_modules/pkg/docs/guide.md\"}");
    defer gpa.free(read.text);
    try std.testing.expect(!read.is_error);
    try std.testing.expectEqualStrings("dependency guide\n", read.text);
    const listing = try nativeCall(root, "codedb", "{\"command\":\"list_dir node_modules/pkg\"}");
    defer gpa.free(listing.text);
    try std.testing.expect(!listing.is_error);
    try std.testing.expect(std.mem.indexOf(u8, listing.text, "guide.md") != null);
    const escaped_listing = try nativeCall(root, "codedb", "{\"command\":\"list_dir escape\"}");
    defer gpa.free(escaped_listing.text);
    try std.testing.expect(escaped_listing.is_error);
    try std.testing.expect(std.mem.indexOf(u8, escaped_listing.text, "outside the working directory") != null);
    for ([_][]const u8{ "{\"path\":\"escape/secret.md\"}", "{\"path\":\"secret.md\"}" }) |json| {
        const denied = try readFile(root, json);
        defer gpa.free(denied.text);
        try std.testing.expect(denied.is_error);
        try std.testing.expect(std.mem.indexOf(u8, denied.text, "outside the working directory") != null);
    }
}

test "native tools #1748: new writes beneath internal links work without permitting escapes" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try dependencyFixture(&tmp);
    const root = try tmp.dir.realPathFileAlloc(io, "checkout", gpa);
    defer gpa.free(root);
    const written = try nativeCall(root, "write_file", "{\"path\":\"node_modules/pkg/docs/new/note.md\",\"content\":\"new guide\\n\"}");
    defer gpa.free(written.text);
    try std.testing.expect(!written.is_error);
    const content = try tmp.dir.readFileAlloc(io, "checkout/node_modules/.store/pkg/docs/new/note.md", gpa, .limited(100));
    defer gpa.free(content);
    try std.testing.expectEqualStrings("new guide\n", content);
    for ([_][]const u8{
        "{\"path\":\"escape/new/note.md\",\"content\":\"denied\"}",
        "{\"path\":\"secret.md\",\"content\":\"denied\",\"replace\":true}",
        "{\"path\":\"dangling.md\",\"content\":\"denied\"}",
    }) |json| {
        const denied = try nativeCall(root, "write_file", json);
        defer gpa.free(denied.text);
        try std.testing.expect(denied.is_error);
        try std.testing.expect(std.mem.indexOf(u8, denied.text, "outside the working directory") != null);
    }
    const outside = try tmp.dir.readFileAlloc(io, "outside/secret.md", gpa, .limited(100));
    defer gpa.free(outside);
    try std.testing.expectEqualStrings("outside\n", outside);
    try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "outside/missing.md", .{}));
    try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "outside/new/note.md", .{}));
}