Skip to content

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

Python

test(stream): end-to-end guard that a stall/drop is never a user Esc (#134/#132)

justrachd1c4be126310
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index ed13618e..0ea23cc5 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -60,6 +60,9 @@ jobs:
- name: PTY model preference and fallback test
run: python3 scripts/test-model-preference.py zig-out/bin/graff
+ - name: Stream stall/drop is not a user Esc (#134/#132)
+ run: python3 scripts/test-stall-drop.py zig-out/bin/graff
+
- name: SDK generation drift
run: |
python3 sdk/generate.py --harness ./zig-out/bin/graff
diff --git a/scripts/test-stall-drop.py b/scripts/test-stall-drop.py
new file mode 100644
index 00000000..e560a7e7
--- /dev/null
+++ b/scripts/test-stall-drop.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+"""#134/#132 end-to-end: a forced stream stall/drop is saved as
+"[response ended early: ...]" and shown as a stream notice — NEVER a user Esc
+interrupt. Drives the GRAFF_FORCE_STALL_ONCE / GRAFF_FORCE_DROP_ONCE test seams,
+which make the next live turn return error.StreamStalled / error.StreamDropped
+before any network call, so no provider/key is needed."""
+import json, os, shutil, subprocess, sys, tempfile
+
+_arg = sys.argv[1] if len(sys.argv) > 1 else "graff"
+GRAFF = os.path.abspath(_arg) if os.sep in _arg else _arg
+
+
+def run(env_flag):
+ tmp = tempfile.mkdtemp(prefix="graff-stall-")
+ try:
+ env = {**os.environ, "HOME": tmp, "CODEGRAFF_API_KEY": "fake-test-key",
+ "GRAFF_NO_TELEMETRY": "1", "GRAFF_FLEET": "off", env_flag: "1"}
+ p = subprocess.run(
+ [GRAFF],
+ input="/model codegraff deepseek-v4-pro\nhello there\n/save teststall\n/exit\n",
+ env=env, cwd=tmp, capture_output=True, text=True, timeout=60)
+ out = (p.stdout + p.stderr).lower()
+ sp = os.path.join(tmp, ".graff", "sessions", "teststall.session.json")
+ msgs = json.load(open(sp)).get("messages", []) if os.path.exists(sp) else []
+ def _text(m):
+ t = m.get("content", m.get("text"))
+ if isinstance(t, list):
+ t = "".join(x.get("text", "") for x in t if isinstance(x, dict))
+ return t or ""
+ assistant = next((_text(m) for m in reversed(msgs)
+ if isinstance(m, dict) and m.get("role") == "assistant"), None)
+ return out, assistant
+ finally:
+ shutil.rmtree(tmp, ignore_errors=True)
+
+
+def check(name, env_flag, marker, notice):
+ out, assistant = run(env_flag)
+ assert assistant is not None, f"{name}: no assistant message saved"
+ assert notice in out, f"{name}: terminal missing {notice!r}"
+ assert assistant == marker, f"{name}: session assistant={assistant!r}, want {marker!r}"
+ # the whole point of #134/#132: it must NOT be a user interruption
+ assert "interrupted by user" not in assistant, f"{name}: mislabeled [response interrupted by user]!"
+ assert "interrupted (esc)" not in out, f"{name}: terminal showed 'interrupted (esc)'!"
+ print(f"ok {name}: saved {marker!r}, notice shown, never a user Esc")
+
+
+def main():
+ check("stall (#134)", "GRAFF_FORCE_STALL_ONCE",
+ "[response ended early: stream stalled]", "stream stalled")
+ check("drop (#132/#133)", "GRAFF_FORCE_DROP_ONCE",
+ "[response ended early: connection dropped]", "connection dropped")
+ print("all clear: a forced stall/drop is [response ended early: ...], never a user Esc interrupt")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/agent_ws.zig b/src/agent_ws.zig
index d92ca30d..b847f525 100644
--- a/src/agent_ws.zig
+++ b/src/agent_ws.zig
@@ -48,6 +48,25 @@ pub fn wsEligible(self: *Agent) bool {
/// A ws transport/handshake failure disables ws for the session and falls back
/// to SSE; a deliberate Esc/stall propagates unchanged.
pub fn postLive(self: *Agent, body: []const u8) ![]u8 {
+ // #134/#132 test seam: force a one-shot stall/drop on a live turn so the
+ // end-to-end "[response ended early: …]" path (never "[response interrupted
+ // by user]") can be exercised without a real provider. Consumed after one use.
+ if (main_mod.g_force_stall_once) {
+ main_mod.g_force_stall_once = false;
+ if (!main_mod.json_mode) if (self.out) |o| {
+ o.writeAll("\n⚠ stream stalled — ending turn\n") catch {};
+ o.flush() catch {};
+ };
+ return error.StreamStalled;
+ }
+ if (main_mod.g_force_drop_once) {
+ main_mod.g_force_drop_once = false;
+ if (!main_mod.json_mode) if (self.out) |o| {
+ o.writeAll("\n⚠ connection dropped — response ended early\n") catch {};
+ o.flush() catch {};
+ };
+ return error.StreamDropped;
+ }
if (!wsEligible(self)) return if (self.ws_off) postStreamFresh(self, body) else self.postStream(body);
return postResponsesWs(self, body) catch |e| {
if (e == error.Interrupted or e == error.StreamStalled) return e;
diff --git a/src/main.zig b/src/main.zig
index 0ebb269a..231a8053 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -58,6 +58,8 @@ pub var show_timing = false;
pub var show_cost = false;
pub var json_mode = false; // --json: structured JSONL events on stdout instead of human text
pub var g_codex_ws = true; // root Codex turns use the WebSocket transport (Responses API over wss) with SSE fallback; GRAFF_CODEX_WS=off|0 forces SSE
+pub var g_force_stall_once: bool = false; // #134 test seam (GRAFF_FORCE_STALL_ONCE=1): the next live turn returns error.StreamStalled — proves the stall path is never labeled a user Esc
+pub var g_force_drop_once: bool = false; // #132/#133 test seam (GRAFF_FORCE_DROP_ONCE=1): the next live turn returns error.StreamDropped
pub var max_tool_calls: ?u64 = null; // --max-tool-calls: hard per-turn root tool budget
pub var dedupe_tool_calls = false; // --dedupe-tool-calls: reject duplicate root calls in a turn
pub var plan_mode = false; // /plan: read-only — mutating tools are denied, the model proposes
diff --git a/src/session_run.zig b/src/session_run.zig
index 9e25e33b..15576108 100644
--- a/src/session_run.zig
+++ b/src/session_run.zig
@@ -340,6 +340,8 @@ pub fn setupSkillsAndTheme(io: Io, arena: Allocator, environ_map: anytype, out:
// auto-detection above it, NOT arbitrary workspace config.
main_mod.g_path_env = try arena.dupe(u8, environ_map.get("PATH") orelse "");
main_mod.g_codedb_guard = environ_map.get("GRAFF_NO_CODEDB_GUARD") == null; // issue #626 guard, opt-out via env
+ main_mod.g_force_stall_once = environ_map.get("GRAFF_FORCE_STALL_ONCE") != null; // #134 test seam
+ main_mod.g_force_drop_once = environ_map.get("GRAFF_FORCE_DROP_ONCE") != null; // #132/#133 test seam
// #134: let a provider that buffers a long reasoning phase in total silence
// raise the mid-stream idle-stall cutoff (default 120s). Seconds; ignored if
// unparseable or 0. A stall is never a user interrupt regardless of the value.