Skip to content

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

Python

Fix Zig 0.17 compile after the read-miss merge

Cursor Agent5925c0c0f8a3
diff --git a/src/read_file_miss.zig b/src/read_file_miss.zig
index b2c19ad1..1b5c7a3a 100644
--- a/src/read_file_miss.zig
+++ b/src/read_file_miss.zig
@@ -24,14 +24,24 @@ const Slot = struct {
}
};
-var mutex: std.Thread.Mutex = .{};
+/// Spin-lock: this Zig has no `std.Thread.Mutex`, and the io-less helpers
+/// (`resetTurn`, `noteMiss`) have no `Io` to park an `Io.Mutex` on.
+var lock: std.atomic.Value(bool) = .init(false);
var slots: [max_slots]Slot = @splat(.{});
var used: u8 = 0;
/// Off until a root turn arms the tracker so unrelated unit tests cannot trip it.
var armed: bool = false;
+fn lockSlots() void {
+ while (lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();
+}
+
+fn unlockSlots() void {
+ lock.store(false, .release);
+}
+
pub fn dirOf(path: []const u8) []const u8 {
- const trimmed = std.mem.trimRight(u8, path, "/\\");
+ const trimmed = std.mem.trimEnd(u8, path, "/\\");
if (trimmed.len == 0) return ".";
if (std.fs.path.dirname(trimmed)) |d| {
if (d.len == 0 or std.mem.eql(u8, d, ".")) return ".";
@@ -52,15 +62,15 @@ pub fn leadingNumber(name: []const u8) ?u32 {
}
pub fn resetTurn() void {
- mutex.lock();
- defer mutex.unlock();
+ lockSlots();
+ defer unlockSlots();
used = 0;
armed = true;
}
pub fn resetForTest() void {
- mutex.lock();
- defer mutex.unlock();
+ lockSlots();
+ defer unlockSlots();
used = 0;
armed = false;
}
@@ -95,8 +105,8 @@ fn takeSlotLocked(dir: []const u8) *Slot {
}
pub fn blocked(path: []const u8) bool {
- mutex.lock();
- defer mutex.unlock();
+ lockSlots();
+ defer unlockSlots();
if (!armed) return false;
const slot = findSlotLocked(dirOf(path)) orelse return false;
return slot.blocked;
@@ -105,8 +115,8 @@ pub fn blocked(path: []const u8) bool {
/// Record a not-found. Returns true once this prefix (or incrementing name
/// pattern) has hit the stop.
pub fn noteMiss(path: []const u8) bool {
- mutex.lock();
- defer mutex.unlock();
+ lockSlots();
+ defer unlockSlots();
if (!armed) return false;
const dir = dirOf(path);
const slot = takeSlotLocked(dir);
@@ -123,8 +133,8 @@ pub fn noteMiss(path: []const u8) bool {
}
pub fn noteHit(path: []const u8) void {
- mutex.lock();
- defer mutex.unlock();
+ lockSlots();
+ defer unlockSlots();
if (!armed) return;
const slot = findSlotLocked(dirOf(path)) orelse return;
slot.misses = 0;
diff --git a/src/rlm_mcp.zig b/src/rlm_mcp.zig
index 02f7bccc..4f8df5b7 100644
--- a/src/rlm_mcp.zig
+++ b/src/rlm_mcp.zig
@@ -31,15 +31,15 @@ pub fn looksMcp(name: []const u8) bool {
pub fn isNativeHost(name: []const u8) bool {
if (@import("shell_tool.zig").isFamily(name)) return true;
const names = [_][]const u8{
- "read_file", "edit_file", "write_file",
- "codedb", "webfetch", "sleep_ms",
- "llm_query", "subagent", "skill",
- "ask_user", "todo_write", "todo_read",
- "eval", "note_constraint", "attempt_completion",
- "load_tool_schemas", "clock_sleep", "workflow",
- "agent_output", "peer_message", "workspace",
- "imagegen", "learn_candidate", "rlm",
- "read_tool_result", "render_html", "mcp_search_tools",
+ "read_file", "edit_file", "write_file",
+ "codedb", "webfetch", "sleep_ms",
+ "llm_query", "subagent", "skill",
+ "ask_user", "todo_write", "todo_read",
+ "eval", "note_constraint", "attempt_completion",
+ "load_tool_schemas", "clock_sleep", "workflow",
+ "agent_output", "peer_message", "workspace",
+ "imagegen", "learn_candidate", "rlm",
+ "read_tool_result", "render_html", "mcp_search_tools",
"mcp_select_tool",
};
for (names) |n| {
@@ -312,7 +312,7 @@ test "#1016: native read_file is not remapped to an unloaded MCP short name" {
var schema: std.json.ObjectMap = .empty;
try schema.put(arena, "type", .{ .string = "object" });
try schema.put(arena, "properties", .{ .object = props });
- const colliding = [_]mcp.Tool{.{
+ var colliding = [_]mcp.Tool{.{
.server_index = 0,
.original_name = "read_file",
.qualified_name = "mcp__wiki__read_file",
diff --git a/src/rlm_tests.zig b/src/rlm_tests.zig
index 9105ce3d..d1757ecf 100644
--- a/src/rlm_tests.zig
+++ b/src/rlm_tests.zig
@@ -419,7 +419,7 @@ test "#1016: literal read_file stays native when an MCP short name collides" {
var schema: std.json.ObjectMap = .empty;
try schema.put(arena, "type", .{ .string = "object" });
try schema.put(arena, "properties", .{ .object = props });
- const colliding = [_]mcp.Tool{.{
+ var colliding = [_]mcp.Tool{.{
.server_index = 0,
.original_name = "read_file",
.qualified_name = "mcp__wiki__read_file",