graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
test(ws): cover concurrent interactive tool loops (#480)
Rach Pradhan676e38053128
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex b21ee987..3ba893ad 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -130,6 +130,9 @@ jobs:- name: Codex WS/SSE transport and mid-turn compaction testrun: python3 scripts/test-pty-codex-ws.py zig-out/bin/graff+ - name: Concurrent Codex WS interactive sessions (#480)+ run: python3 scripts/test-pty-codex-ws-concurrent.py zig-out/bin/graff+- name: PTY /goal autonomous collapse wiring testrun: python3 scripts/test-pty-goal-loop.py zig-out/bin/graffdiff --git a/scripts/codex_ws_test.py b/scripts/codex_ws_test.pyindex 4130366e..e83a6778 100644--- a/scripts/codex_ws_test.py+++ b/scripts/codex_ws_test.py@@ -44,6 +44,8 @@ TRANSACTIONAL_FINAL = "done after transactional compaction failure"TRANSACTIONAL_REASONING_MARKER = "transactional-active-reasoning:"TRANSACTIONAL_CALL_ID = "call_transactional_1"+CONCURRENT_FINAL = "done after concurrent websocket tool loop"+# #391: compaction now spends one extra quiet turn writing a note to self# before the summary. Both synthetic turns are identified by the head of their# instruction so the fixtures below can be keyed on CONTENT rather than on a@@ -228,6 +230,41 @@ def transactional_events(request: RecordedRequest) -> list[dict]:)+def concurrent_tool_events(request: RecordedRequest) -> list[dict]:+ """One native tool round-trip per independent WS connection.++ The second request on a chained connection contains only the new+ function_call_output, so key on that content instead of the mock's global+ ordinal: concurrent handlers may interleave in any order.+ """+ instructions = request.body.get("instructions", "")+ if "You summarize what a coding session is about" in instructions:+ return response_events(+ message_item("Concurrent websocket session", f"msg_title_{request.ordinal}"),+ f"resp_title_{request.ordinal}",+ 200,+ )+ input_items = request.body.get("input") or []+ if any(+ isinstance(item, dict) and item.get("type") == "function_call_output"+ for item in input_items+ ):+ return response_events(+ message_item(CONCURRENT_FINAL, f"msg_concurrent_{request.ordinal}"),+ f"resp_concurrent_{request.ordinal}",+ 1_300,+ )+ call = {+ "type": "function_call",+ "id": f"fc_concurrent_{request.ordinal}",+ "call_id": f"call_concurrent_{request.ordinal}",+ "name": "todo_read",+ "arguments": "{}",+ "status": "completed",+ }+ return response_events(call, f"resp_concurrent_{request.ordinal}", 1_200)++def assert_compaction_meter(label: str, rendered: str) -> None:match = COMPACTING_RE.search(rendered)if match is None:diff --git a/scripts/test-pty-codex-ws-concurrent.py b/scripts/test-pty-codex-ws-concurrent.pynew file mode 100644index 00000000..dc262f76--- /dev/null+++ b/scripts/test-pty-codex-ws-concurrent.py@@ -0,0 +1,104 @@+#!/usr/bin/env python3+"""Reproduce #480 with concurrent interactive Codex WebSocket tool loops."""++import json+import os+import tempfile+from concurrent.futures import ThreadPoolExecutor++from codex_ws_mock import CodexMock+import codex_ws_test as scenario+++def main() -> None:+ with tempfile.TemporaryDirectory(prefix="graff-pty-codex-concurrent-") as tmp:+ codex_home = os.path.join(tmp, "codex-home")+ os.makedirs(codex_home)+ with open(os.path.join(codex_home, "auth.json"), "w", encoding="utf-8") as fh:+ json.dump(+ {+ "tokens": {+ "access_token": "pty-concurrent-mock",+ "account_id": "acct-pty-concurrent",+ }+ },+ fh,+ )++ mock = CodexMock(events_for_request=scenario.concurrent_tool_events)+ port = mock.start()+ try:+ def run_session(index: int) -> None:+ session_dir = os.path.join(tmp, f"session-{index}")+ os.makedirs(os.path.join(session_dir, ".harness"))+ with open(+ os.path.join(session_dir, ".harness", "settings.json"),+ "w",+ encoding="utf-8",+ ) as fh:+ # Keep the detached first-turn title enabled: it overlaps the+ # root WS turn in the interactive-only shape from #480.+ json.dump({"ai_title": True, "session_recap": False}, fh)+ env = {+ "HOME": session_dir,+ "CODEX_HOME": codex_home,+ "CODEGRAFF_API_KEY": "local-pty-test",+ "GRAFF_FLEET": "off",+ "GRAFF_NO_TELEMETRY": "1",+ "GRAFF_CODEX_URL": (+ f"http://127.0.0.1:{port}/backend-api/codex/responses"+ ),+ }+ ambient = tuple(+ name+ for name in os.environ+ if (+ name.startswith("GRAFF_")+ or name.startswith("CODEX_")+ or name == "NO_COLOR"+ )+ and name not in env+ )+ with scenario.PtySession(+ scenario.GRAFF,+ ["--model", "codex", "--no-telemetry"],+ cwd=session_dir,+ env=env,+ unset_env=ambient,+ timeout=20.0,+ ) as session:+ session.wait_for_literal("] ›")+ cursor = len(session.raw)+ session.send_line(f"concurrent feature task {index}")+ session.wait_for_literal(scenario.CONCURRENT_FINAL, start=cursor)+ session.wait_for_literal("] ›", start=cursor)+ session.send_key("ctrl-d")+ result = session.read_until_exit(5.0)+ if result.timed_out or result.exit_code != 0:+ raise AssertionError(+ f"session {index} failed to exit: "+ f"exit={result.exit_code} timed_out={result.timed_out}"+ )++ with ThreadPoolExecutor(max_workers=4) as pool:+ futures = [pool.submit(run_session, index) for index in range(4)]+ for future in futures:+ future.result(timeout=30.0)++ if mock.ws_connections != 4 or mock.ws_turns != 8 or mock.sse_turns != 4:+ raise AssertionError(+ "concurrent WS tool-loop counts changed: "+ f"connections={mock.ws_connections} ws={mock.ws_turns} "+ f"title_sse={mock.sse_turns}"+ )+ finally:+ mock.stop()++ print(+ "ok four concurrent interactive WS sessions: detached title + "+ "native tool loop -> answer -> prompt, without a post-response hang"+ )+++if __name__ == "__main__":+ main()