graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(codedbpro): resolve tool paths from session cwd
Rach Pradhanbf0b2678a73f
diff --git a/src/additional_tests.zig b/src/additional_tests.zignew file mode 100644index 00000000..e7069139--- /dev/null+++ b/src/additional_tests.zig@@ -0,0 +1,4 @@+test {+ _ = @import("readline_history.zig");+ _ = @import("codedbpro_paths.zig");+}diff --git a/src/codedbpro_paths.zig b/src/codedbpro_paths.zignew file mode 100644index 00000000..7ad6f32b--- /dev/null+++ b/src/codedbpro_paths.zig@@ -0,0 +1,102 @@+//! Client-side path normalization for CodeDB Pro's resident daemon.++const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;++pub const PreparedInput = struct {+ value: std.json.Value,+ owned_object: ?std.json.ObjectMap = null,+ owned_path: ?[]u8 = null,++ pub fn deinit(self: *PreparedInput, gpa: Allocator) void {+ if (self.owned_object) |*obj| obj.deinit(gpa);+ if (self.owned_path) |path| gpa.free(path);+ }+};++fn companionPathKey(tool: []const u8) ?[]const u8 {+ if (std.mem.eql(u8, tool, "mcp__codedbpro__read") or+ std.mem.eql(u8, tool, "mcp__codedbpro__edit") or+ std.mem.eql(u8, tool, "mcp__codedbpro__lint") or+ std.mem.eql(u8, tool, "mcp__codedbpro__diff"))+ return "file";+ if (std.mem.eql(u8, tool, "mcp__codedbpro__search") or+ std.mem.eql(u8, tool, "mcp__codedbpro__faster_search") or+ std.mem.eql(u8, tool, "mcp__codedbpro__meta_search"))+ return "path";+ return null;+}++/// Errors caused by model arguments or session path context should open the+/// native fallback but must not generate upstream CodeDB Pro bug reports.+pub fn callerError(text: []const u8) bool {+ for ([_][]const u8{+ "file not found",+ "path not found",+ "no content provided",+ "missing content",+ "is a directory",+ "permission denied",+ "path too long",+ }) |needle| {+ if (std.mem.indexOf(u8, text, needle) != null) return true;+ }+ return false;+}++/// CodeDB Pro's resident daemon has one launch cwd that can outlive and differ+/// from the Graff session using it. Normalize path-bearing calls at the client+/// boundary so relative model arguments keep their documented session-cwd+/// meaning instead of accidentally resolving inside the daemon's first repo.+pub fn prepareInput(gpa: Allocator, io: Io, agent_cwd: ?[]const u8, tool: []const u8, input: std.json.Value) !PreparedInput {+ const key = companionPathKey(tool) orelse return .{ .value = input };+ if (input != .object) return .{ .value = input };+ const path_value = input.object.get(key) orelse return .{ .value = input };+ if (path_value != .string or std.fs.path.isAbsolute(path_value.string)) return .{ .value = input };++ var cwd_buf: [4096]u8 = undefined;+ const base = if (agent_cwd) |worktree|+ worktree+ else blk: {+ const n = try Io.Dir.cwd().realPathFile(io, ".", &cwd_buf);+ break :blk cwd_buf[0..n];+ };+ const absolute = try std.fs.path.resolve(gpa, &.{ base, path_value.string });+ errdefer gpa.free(absolute);++ var object: std.json.ObjectMap = .empty;+ errdefer object.deinit(gpa);+ var it = input.object.iterator();+ while (it.next()) |entry| try object.put(gpa, entry.key_ptr.*, entry.value_ptr.*);+ try object.put(gpa, key, .{ .string = absolute });+ return .{+ .value = .{ .object = object },+ .owned_object = object,+ .owned_path = absolute,+ };+}++test "prepareInput resolves CodeDB Pro paths against the agent cwd" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();++ const read_input = try std.json.parseFromSliceLeaky(std.json.Value, arena, "{\"file\":\"src/main.zig\",\"mode\":\"full\"}", .{});+ var prepared_read = try prepareInput(std.testing.allocator, std.testing.io, "/tmp/graff-worktree", "mcp__codedbpro__read", read_input);+ defer prepared_read.deinit(std.testing.allocator);+ try std.testing.expectEqualStrings("/tmp/graff-worktree/src/main.zig", prepared_read.value.object.get("file").?.string);+ try std.testing.expectEqualStrings("src/main.zig", read_input.object.get("file").?.string);++ const search_input = try std.json.parseFromSliceLeaky(std.json.Value, arena, "{\"pattern\":\"needle\",\"path\":\"src\"}", .{});+ var prepared_search = try prepareInput(std.testing.allocator, std.testing.io, "/tmp/graff-worktree", "mcp__codedbpro__faster_search", search_input);+ defer prepared_search.deinit(std.testing.allocator);+ try std.testing.expectEqualStrings("/tmp/graff-worktree/src", prepared_search.value.object.get("path").?.string);+}++test "caller errors are not filed as CodeDB Pro defects" {+ try std.testing.expect(callerError("{\"ok\":false,\"error\":\"path not found\"}"));+ try std.testing.expect(callerError("{\"ok\":false,\"error\":\"no content provided\"}"));+ try std.testing.expect(!callerError("StreamTooLong"));+ try std.testing.expect(!callerError("lint: failed to spawn eslint"));+}diff --git a/src/codedbpro_report.zig b/src/codedbpro_report.zigindex 2d8a2ede..1db67798 100644--- a/src/codedbpro_report.zig+++ b/src/codedbpro_report.zig@@ -201,7 +201,9 @@ pub fn nativeRefusal(ctx: tools.ToolCtx, call: tools.ToolCall) ?tools.ToolOutput/// refusal; a failed pro call opens the fallback and the native proceeds.pub fn licensedGate(ctx: tools.ToolCtx, call: tools.ToolCall) ?tools.ToolOutput {if (redirect(ctx, call)) |target| {- const r = ctx.registry.?.call(ctx.gpa, target.name, target.input) catch |err| {+ var prepared = @import("codedbpro_paths.zig").prepareInput(ctx.gpa, ctx.io, ctx.agent_cwd, target.name, target.input) catch return null;+ defer prepared.deinit(ctx.gpa);+ const r = ctx.registry.?.call(ctx.gpa, target.name, prepared.value) catch |err| {onFailure(ctx, target.name, @errorName(err));return null; // fallback opened — let the native run};@@ -332,7 +334,7 @@ pub fn spliceViaDaemon(gpa: Allocator, ctx: tools.ToolCtx, file: []const u8, dis/// resolve against the daemon's launch cwd — not a tool bug, so they never/// reach the issue filer (#475).fn callerError(err_text: []const u8) bool {- return std.mem.indexOf(u8, err_text, "file not found") != null;+ return @import("codedbpro_paths.zig").callerError(err_text);}/// exec.zig's MCP branch calls this on any failed mcp__codedbpro__* calldiff --git a/src/exec.zig b/src/exec.zigindex 8f73b76e..506cedb6 100644--- a/src/exec.zig+++ b/src/exec.zig@@ -16,6 +16,7 @@ const Allocator = std.mem.Allocator;const main_mod = @import("main.zig");const util = @import("util.zig");const codedbpro_report = @import("codedbpro_report.zig"); // licensed-companion failure → redacted issue filer+const codedbpro_paths = @import("codedbpro_paths.zig"); // session-cwd paths for the resident companion daemonconst tool_balance = @import("tool_balance.zig"); // session-wide tool-class tally (/tools)const ToolCall = tools.ToolCall;@@ -240,7 +241,12 @@ fn execToolInner(ctx: ToolCtx, call: ToolCall) !ToolOutput {// (same user direction as the native fold). Still downstream of// agent_tool_gate.gateTool: consent is already settled.mcp_schema_gate.autoLoad(gpa, reg.tools, call.name);- const r = reg.call(gpa, call.name, call.input) catch |err| {+ var prepared = codedbpro_paths.prepareInput(gpa, io, ctx.agent_cwd, call.name, call.input) catch |err| {+ codedbpro_report.onFailure(ctx, call.name, @errorName(err));+ return failure(gpa, err);+ };+ defer prepared.deinit(gpa);+ const r = reg.call(gpa, call.name, prepared.value) catch |err| {codedbpro_report.onFailure(ctx, call.name, @errorName(err));return failure(gpa, err);};diff --git a/src/main.zig b/src/main.zigindex eb13e13b..37d54627 100644--- a/src/main.zig+++ b/src/main.zig@@ -590,7 +590,7 @@ test { // pull in tests from imported modules (mcp.zig)_ = @import("agent_server_compact.zig"); // server-side autocompact (codex Responses)_ = @import("task_outcome.zig"); // goal-outcome telemetry events_ = @import("learn_delete.zig"); // #303: its tests were dead until listed here- _ = @import("readline_history.zig");+ _ = @import("additional_tests.zig");_ = @import("goal_pacing_autonomous_test.zig");_ = @import("goal_state.zig");_ = @import("goal_persist_tests.zig");