Skip to content

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

Python

Scope nested check failures to their worktree

justrach74fe76697390
diff --git a/docs/adr/0126-publication-retains-observed-check-failures.md b/docs/adr/0126-publication-retains-observed-check-failures.md
index 78fe3917..45fdaa44 100644
--- a/docs/adr/0126-publication-retains-observed-check-failures.md
+++ b/docs/adr/0126-publication-retains-observed-check-failures.md
@@ -11,9 +11,10 @@ not establish that runtime publication consumed actual local results.
## Decision
The root records failures and incomplete background starts from recognized test
-commands after tool execution. Records use canonical workspace paths and survive
+commands after tool execution. Literal `cd` prefixes are resolved. Records retain the canonical execution
+directory and Git worktree root, and survive
session saves and resumes. Only a successful foreground rerun of the same command
-clears its record; unrelated successful commands and rewritten prose do not.
+in the same execution directory clears its record; unrelated successful commands and rewritten prose do not.
Non-draft publication is refused while an applicable record remains unresolved.
Draft creation remains available.
diff --git a/scripts/test-pr-local-checks.py b/scripts/test-pr-local-checks.py
index 2ffadb2d..07fa9e6e 100644
--- a/scripts/test-pr-local-checks.py
+++ b/scripts/test-pr-local-checks.py
@@ -29,7 +29,7 @@ def main():
check = bash("python3 -m unittest test_observed -v")
publish = bash("gh pr create --title fixture --body-file notes.md")
final = {"text": "Fixture finished."}
- for case in ("failed", "rerun", "draft", "resume", "batch"):
+ for case in ("failed", "rerun", "draft", "resume", "batch", "nested"):
with tempfile.TemporaryDirectory(prefix="graff-pr-local-checks-") as temp:
work = Path(temp)
env = {k: v for k, v in os.environ.items() if not k.endswith("_API_KEY")}
@@ -47,7 +47,16 @@ def main():
(work / "notes.md").write_text(
"Verification: local checks passed. Dispatch is covered."
)
- if case == "rerun":
+ if case == "nested":
+ nested = work / "checks space"
+ nested.mkdir()
+ (nested / "test_observed.py").write_text(failing)
+ script = [
+ bash("cd 'checks space' && python3 -m unittest test_observed -v"),
+ publish,
+ final,
+ ]
+ elif case == "rerun":
script = [
check,
{
@@ -127,7 +136,7 @@ def main():
mutations,
events,
)
- if case in ("failed", "resume"):
+ if case in ("failed", "resume", "nested"):
assert (
"observed local check has no successful completion"
in json.dumps(events)
diff --git a/src/pr_command.zig b/src/pr_command.zig
index b220205f..8a5a3fbc 100644
--- a/src/pr_command.zig
+++ b/src/pr_command.zig
@@ -79,7 +79,9 @@ fn takesValue(arg: []const u8) bool {
return false;
}
-pub fn parse(a: A, input: []const u8) !Command {
+pub const Literal = struct { argv: []const []const u8, cwd: ?[]const u8 = null };
+
+pub fn literal(a: A, input: []const u8) !Literal {
var args: std.ArrayList([]const u8) = .empty;
var i: usize = 0;
while (i < input.len) {
@@ -120,7 +122,16 @@ pub fn parse(a: A, input: []const u8) !Command {
cwd = t[1];
t = t[3..];
}
- if (t.len == 0 or !std.mem.eql(u8, std.fs.path.basename(t[0]), "gh")) return error.UnsupportedCommand;
+ if (t.len == 0) return error.UnsupportedCommand;
+ for (t) |arg| if (std.mem.eql(u8, arg, "&&") or std.mem.eql(u8, arg, "&")) return error.CompoundCommand;
+ return .{ .argv = t, .cwd = cwd };
+}
+
+pub fn parse(a: A, input: []const u8) !Command {
+ const parsed = try literal(a, input);
+ const t = parsed.argv;
+ const cwd = parsed.cwd;
+ if (!std.mem.eql(u8, std.fs.path.basename(t[0]), "gh")) return error.UnsupportedCommand;
for (t) |arg| if (std.mem.eql(u8, arg, "&&") or std.mem.eql(u8, arg, "&")) return error.CompoundCommand;
var j: usize = 1;
while (j < t.len) : (j += 1) {
diff --git a/src/pr_local_checks.zig b/src/pr_local_checks.zig
index d50cc19c..cbd8d89d 100644
--- a/src/pr_local_checks.zig
+++ b/src/pr_local_checks.zig
@@ -26,15 +26,25 @@ pub fn isCheck(command: []const u8) bool {
return false;
}
+pub fn repositoryRoot(root: anytype, cwd: []const u8) ![]const u8 {
+ const ev = @import("pr_evidence.zig");
+ const found = ev.capture(root.gpa, root.io, root.arena, .{ .cwd = cwd, .selector = "" }, &.{ "git", "rev-parse", "--show-toplevel" }) catch return cwd;
+ return if (std.fs.path.isAbsolute(found)) found else cwd;
+}
+
pub fn record(root: anytype, call: ToolCall, result: ExecResult) !void {
if (root.sub or !std.mem.eql(u8, call.name, "bash") or call.input != .object) return;
const command = call.input.object.get("command") orelse return;
- if (command != .string or !isCheck(command.string)) return;
- var dir = try std.Io.Dir.cwd().openDir(root.io, root.agent_cwd orelse ".", .{});
+ if (command != .string) return;
+ const parsed = @import("pr_command.zig").literal(root.arena, command.string) catch return;
+ if (!isCheck(try std.mem.join(root.arena, " ", parsed.argv))) return;
+ const base = root.agent_cwd orelse ".";
+ const path = if (parsed.cwd) |cwd| if (std.fs.path.isAbsolute(cwd)) cwd else try std.fs.path.join(root.arena, &.{ base, cwd }) else base;
+ var dir = std.Io.Dir.cwd().openDir(root.io, path, .{}) catch try std.Io.Dir.cwd().openDir(root.io, base, .{});
defer dir.close(root.io);
var buffer: [std.fs.max_path_bytes]u8 = undefined;
const cwd = buffer[0..try dir.realPath(root.io, &buffer)];
- try root.publication_checks.observe(root.arena, cwd, call, result);
+ try root.publication_checks.observeKnown(root.arena, cwd, try repositoryRoot(root, cwd), command.string, result);
}
pub fn batchGate(root: anytype, calls: []const ToolCall, call: ToolCall) !?ExecResult {
@@ -50,14 +60,18 @@ pub fn batchGate(root: anytype, calls: []const ToolCall, call: ToolCall) !?ExecR
}
pub const State = struct {
- const Entry = struct { cwd: []const u8, command: []const u8 };
+ const Entry = struct { cwd: []const u8, command: []const u8, repository: ?[]const u8 = null };
failed: std.ArrayList(Entry) = .empty,
pub fn observe(self: *State, arena: Allocator, cwd: []const u8, call: ToolCall, result: ExecResult) !void {
if (!std.mem.eql(u8, call.name, "bash") or call.input != .object) return;
const raw = call.input.object.get("command") orelse return;
if (raw != .string or !isCheck(raw.string)) return;
- const command = std.mem.trim(u8, raw.string, " \t\r\n");
+ try self.observeKnown(arena, cwd, cwd, raw.string, result);
+ }
+
+ fn observeKnown(self: *State, arena: Allocator, cwd: []const u8, repository: []const u8, raw: []const u8, result: ExecResult) !void {
+ const command = std.mem.trim(u8, raw, " \t\r\n");
for (self.failed.items, 0..) |entry, i| {
if (!std.mem.eql(u8, entry.cwd, cwd) or !std.mem.eql(u8, entry.command, command)) continue;
if (!result.is_error and !result.cancelled and std.mem.indexOf(u8, result.text, "[job ") == null)
@@ -65,7 +79,7 @@ pub const State = struct {
return;
}
if (!result.is_error and !result.cancelled and std.mem.indexOf(u8, result.text, "[job ") == null) return;
- try self.failed.append(arena, .{ .cwd = try arena.dupe(u8, cwd), .command = try arena.dupe(u8, command) });
+ try self.failed.append(arena, .{ .cwd = try arena.dupe(u8, cwd), .command = try arena.dupe(u8, command), .repository = try arena.dupe(u8, repository) });
}
pub fn write(self: *const State, writer: anytype) !void {
@@ -78,6 +92,7 @@ pub const State = struct {
for (self.failed.items) |entry| {
fingerprint.text(entry.cwd);
fingerprint.text(entry.command);
+ fingerprint.text(entry.repository orelse entry.cwd);
}
}
@@ -94,13 +109,16 @@ pub const State = struct {
const command = item.object.get("command") orelse return error.InvalidPublicationChecks;
if (cwd != .string or command != .string or !std.fs.path.isAbsolute(cwd.string) or command.string.len == 0)
return error.InvalidPublicationChecks;
- try restored.failed.append(arena, .{ .cwd = try arena.dupe(u8, cwd.string), .command = try arena.dupe(u8, command.string) });
+ const repository = item.object.get("repository") orelse .null;
+ if (repository != .null and (repository != .string or !std.fs.path.isAbsolute(repository.string)))
+ return error.InvalidPublicationChecks;
+ try restored.failed.append(arena, .{ .cwd = try arena.dupe(u8, cwd.string), .command = try arena.dupe(u8, command.string), .repository = if (repository == .string) try arena.dupe(u8, repository.string) else null });
}
self.* = restored;
}
pub fn unresolved(self: *const State, cwd: []const u8) ?[]const u8 {
- for (self.failed.items) |entry| if (std.mem.eql(u8, entry.cwd, cwd)) return entry.command;
+ for (self.failed.items) |entry| if (std.mem.eql(u8, entry.repository orelse entry.cwd, cwd)) return entry.command;
return null;
}
};
@@ -150,3 +168,16 @@ test "failed checks survive serialization and malformed state is not a clean res
try std.testing.expectError(error.InvalidPublicationChecks, resumed.restore(a, malformed.object));
try std.testing.expect(resumed.unresolved("/fixture") != null);
}
+
+test "a sibling directory success cannot clear a failed worktree check" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var state: State = .{};
+ try state.observeKnown(a, "/repo/one", "/repo", "pytest", .{ .text = "FAIL", .is_error = true });
+ try state.observeKnown(a, "/repo/two", "/repo", "pytest", .{ .text = "OK", .is_error = false });
+ try std.testing.expect(state.unresolved("/repo") != null);
+ try std.testing.expect(state.unresolved("/another-repo") == null);
+ try state.observeKnown(a, "/repo/one", "/repo", "pytest", .{ .text = "OK", .is_error = false });
+ try std.testing.expect(state.unresolved("/repo") == null);
+}
diff --git a/src/publish_gate.zig b/src/publish_gate.zig
index 209060e1..6f8aad5b 100644
--- a/src/publish_gate.zig
+++ b/src/publish_gate.zig
@@ -27,7 +27,7 @@ fn observe(self: *Agent, cmd: []const u8) !?ExecResult {
var ev: pr_publish.Evidence = .{};
const draft = creating and command.draft();
if (!draft) {
- if (self.publication_checks.unresolved(target.cwd)) |command_text|
+ if (self.publication_checks.unresolved(try @import("pr_local_checks.zig").repositoryRoot(self, target.cwd))) |command_text|
return .{ .text = try std.fmt.allocPrint(self.arena, "PR publication preflight: observed local check has no successful completion: {s}. Rerun it successfully or publish a draft; write NOT performed.", .{command_text}), .is_error = true };
if (creating) {
ev.head_sha = evmod.localHead(self.gpa, self.io, self.arena, target) catch "";