Skip to content

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

Python

fix: free /bash slash-command tool result + add regression tests

codegraff-bot833bc0702f5a
diff --git a/gui/src-tauri/src/runtime/simple.rs b/gui/src-tauri/src/runtime/simple.rs
index 5ef2b2d2..6a071f44 100644
--- a/gui/src-tauri/src/runtime/simple.rs
+++ b/gui/src-tauri/src/runtime/simple.rs
@@ -3337,6 +3337,56 @@ fn saved_detail(
mod tests {
use super::*;
+ #[cfg(unix)]
+ fn mk_output(stdout: &[u8], stderr: &[u8], raw_status: i32) -> std::process::Output {
+ use std::os::unix::process::ExitStatusExt;
+ std::process::Output {
+ status: std::process::ExitStatus::from_raw(raw_status),
+ stdout: stdout.to_vec(),
+ stderr: stderr.to_vec(),
+ }
+ }
+
+ #[test]
+ #[cfg(unix)]
+ fn format_command_output_empty_is_no_output() {
+ // stdout+stderr empty, exit 0 -> the "(no output)" placeholder.
+ assert_eq!(format_command_output(&mk_output(b"", b"", 0)), "(no output)");
+ }
+
+ #[test]
+ #[cfg(unix)]
+ fn format_command_output_stdout_only_unchanged() {
+ assert_eq!(format_command_output(&mk_output(b"hello\n", b"", 0)), "hello\n");
+ }
+
+ #[test]
+ #[cfg(unix)]
+ fn format_command_output_stderr_only_gets_label() {
+ // No stdout, so no leading newline; "[stderr]" label prefixes stderr.
+ assert_eq!(
+ format_command_output(&mk_output(b"", b"oops\n", 0)),
+ "[stderr]\noops\n"
+ );
+ }
+
+ #[test]
+ #[cfg(unix)]
+ fn format_command_output_stderr_separator_and_nonzero_exit() {
+ // stdout present -> "[stderr]" goes on its own line after a separator.
+ let out = format_command_output(&mk_output(b"out\n", b"err\n", 1 << 8));
+ assert!(out.contains("out\n[stderr]\nerr\n"));
+ assert!(out.ends_with("[exit code 1]"));
+ }
+
+ #[test]
+ #[cfg(unix)]
+ fn format_command_output_signal_terminated_is_abnormal() {
+ // raw status 2 -> WIFSIGNALED -> status.code() is None.
+ assert!(format_command_output(&mk_output(b"", b"", 2)).contains("[terminated abnormally]"));
+ }
+
+
fn followup(
id: &str,
kind: FollowupKind,
diff --git a/gui/src/hooks/useCommandRouter.test.ts b/gui/src/hooks/useCommandRouter.test.ts
new file mode 100644
index 00000000..d9206a03
--- /dev/null
+++ b/gui/src/hooks/useCommandRouter.test.ts
@@ -0,0 +1,49 @@
+import { describe, expect, test } from "bun:test";
+
+import { parseSlashCommand } from "./useCommandRouter";
+
+describe("parseSlashCommand", () => {
+ test("returns null for a plain prompt with no slash", () => {
+ expect(parseSlashCommand("hello world")).toBeNull();
+ expect(parseSlashCommand("")).toBeNull();
+ });
+
+ test("parses a bare command with no args", () => {
+ const parsed = parseSlashCommand("/help");
+ expect(parsed).not.toBeNull();
+ expect(parsed?.name).toBe("help");
+ expect(parsed?.args).toEqual([]);
+ });
+
+ test("bash preserves the full raw argument string as a single arg", () => {
+ // Regression guard for PR #38: /bash must NOT split on whitespace, or
+ // quoted/compound commands (`echo "a b"`, `sh -c "x && y"`) get mangled.
+ const parsed = parseSlashCommand('/bash echo "hello world"');
+ expect(parsed?.name).toBe("bash");
+ expect(parsed?.args).toEqual(['echo "hello world"']);
+ });
+
+ test("bash preserves shell metacharacters and pipes inside the arg", () => {
+ const parsed = parseSlashCommand("/bash ls -la | grep src");
+ expect(parsed?.name).toBe("bash");
+ expect(parsed?.args).toEqual(["ls -la | grep src"]);
+ });
+
+ test("non-bash commands still split args on whitespace", () => {
+ const parsed = parseSlashCommand("/rename My Cool Chat");
+ expect(parsed?.name).toBe("rename");
+ expect(parsed?.args).toEqual(["My", "Cool", "Chat"]);
+ });
+
+ test("bash with no args yields an empty args array", () => {
+ const parsed = parseSlashCommand("/bash");
+ expect(parsed?.name).toBe("bash");
+ expect(parsed?.args).toEqual([]);
+ });
+
+ test("bash preserves multiline command bodies", () => {
+ const parsed = parseSlashCommand("/bash echo line1\necho line2");
+ expect(parsed?.name).toBe("bash");
+ expect(parsed?.args).toEqual(["echo line1\necho line2"]);
+ });
+});
diff --git a/gui/src/hooks/useCommandRouter.ts b/gui/src/hooks/useCommandRouter.ts
index 1a200e86..88367c10 100644
--- a/gui/src/hooks/useCommandRouter.ts
+++ b/gui/src/hooks/useCommandRouter.ts
@@ -31,7 +31,7 @@ interface UseCommandRouterOptions {
onCommandResult?: (result: CommandRunResult) => void;
}
-function parseSlashCommand(draft: string): ParsedCommand | null {
+export function parseSlashCommand(draft: string): ParsedCommand | null {
const match = /^\/([\w-]+)(?:\s+([\s\S]*))?$/.exec(draft.trim());
if (match == null) {
return null;
diff --git a/src/main.zig b/src/main.zig
index 4cf6c406..56fa7318 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -5121,6 +5121,7 @@ fn handleCommand(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
.snapshots = root.snapshots,
.tools_used = &root.tools_used,
}, call);
+ defer root.gpa.free(result.text);
try out.writeAll(result.text);
if (result.text.len == 0 or result.text[result.text.len - 1] != '\n') try out.writeAll("\n");
try out.flush();
@@ -12525,3 +12526,50 @@ test "providerTakesEffort: effort-honoring providers, but never for grok models"
// grok via the codegraff gateway must NOT get reasoning_effort (grok rejects it)
try std.testing.expect(!providerTakesEffort(.openai, "codegraff", "grok-build"));
}
+
+test "/bash slash command runs the bash tool and frees its gpa-allocated result" {
+ // Regression guard for PR #38: the /bash slash handler routes through
+ // execTool, whose result.text is gpa-owned (NOT arena-owned — every other
+ // caller frees it). Forgetting `defer root.gpa.free(result.text)` in
+ // handleCommand leaks on every /bash invocation; std.testing.allocator
+ // catches that here.
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+
+ var client: std.http.Client = .{ .allocator = gpa, .io = io };
+ defer client.deinit();
+
+ var root: Agent = .{
+ .gpa = gpa,
+ .arena = arena,
+ .io = io,
+ .client = &client,
+ .provider = .{
+ .id = "test",
+ .kind = .openai,
+ .auth = .bearer,
+ .url = "",
+ .api_key = "",
+ .model = "m",
+ .context = 100_000,
+ },
+ .messages = std.json.Array.init(arena),
+ .sub = false,
+ .label = "test",
+ .out = null,
+ };
+ var keys: Keys = .{ .values = [_]?[]const u8{null} ** provider_specs.len };
+
+ var aw: Io.Writer.Allocating = .init(gpa);
+ defer aw.deinit();
+
+ defer root.tools_used.deinit(gpa);
+ try handleCommand(&root, &keys, arena, "/bash printf leak-guard-XYZ", &aw.writer);
+
+ const written = aw.writer.buffered();
+ try std.testing.expect(std.mem.indexOf(u8, written, "leak-guard-XYZ") != null);
+}