Skip to content

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

Python

Pause every idle stream during any turn so queued follow-ups keep attachments

Cursor Agent21f22ed3220d
diff --git a/apps/native/components/site/GraffHarness.tsx b/apps/native/components/site/GraffHarness.tsx
index 187d94b3..dc6434fb 100644
--- a/apps/native/components/site/GraffHarness.tsx
+++ b/apps/native/components/site/GraffHarness.tsx
@@ -178,7 +178,7 @@ export default function GraffHarness() {
setPinsByChat(pinsRef.current);
};
- const { adoptCatalog, requireSession, refreshStored, projectsReady } = useHarnessSessions({
+ const { adoptCatalog, requireSession, refreshStored, projectsReady, unwatchIdle } = useHarnessSessions({
sessionsRef, sessionNamesRef, chatsRef, workspacesRef, activePathRef, pageRef, runningRef, model, activeId, handleOf, setModels, setCommands, setCatalogCommands, setChatModel, setModelKey, setSessionIds, setHealth, setWorkspaces, setActivePath, setChats, setStored, setStoredTotal,
pendingPick: () => pendingPickRef.current ?? pendingModel,
});
@@ -346,6 +346,7 @@ export default function GraffHarness() {
pinsRef.current = remainingPins; setPinsByChat(remainingPins);
setCommands(current => { const { [id]: _commands, ...rest } = current; return rest; });
setCancelError(current => { const { [id]: _error, ...rest } = current; return rest; });
+ unwatchIdle(id);
void disposeSession(handleOf(id));
void browserClose(handleOf(id)).catch(() => undefined);
};
diff --git a/apps/native/components/site/idle-peer-turns.ts b/apps/native/components/site/idle-peer-turns.ts
index b4c483e6..85144213 100644
--- a/apps/native/components/site/idle-peer-turns.ts
+++ b/apps/native/components/site/idle-peer-turns.ts
@@ -3,9 +3,10 @@ import { idleUpdates, type ChatHandle } from "@/lib/acp-client";
import { holdWhileIdle, waitWhile } from "@/lib/idle-http";
import type { Chat } from "./harness-types";
-/** Paint unsolicited ACP session/update while a tab is idle (#1007).
- * The idle HTTP stream is dropped for the duration of a prompt turn so
- * attach and other POSTs can use the origin's connection slots (#1068). */
+/** Paint unsolicited ACP session/update while the page is idle (#1007).
+ * Every idle HTTP stream is dropped while ANY prompt turn runs, so an attach
+ * or other POST during one chat's turn can use the origin's connection slots
+ * no matter how many background tabs hold streams (#1068). */
export async function pumpIdlePeerTurns(opts: {
chatId: number;
handle: ChatHandle;
diff --git a/apps/native/components/site/useHarnessSessions.ts b/apps/native/components/site/useHarnessSessions.ts
index a523b068..d1b2eea3 100644
--- a/apps/native/components/site/useHarnessSessions.ts
+++ b/apps/native/components/site/useHarnessSessions.ts
@@ -34,9 +34,18 @@ export function useHarnessSessions({sessionsRef, sessionNamesRef, chatsRef, work
idleCtl.current.set(chatId, ac);
void pumpIdlePeerTurns({
chatId, handle: handleOf(chatId), sessionId, signal: ac.signal,
- running: () => runningRef.current.has(chatId), setChats,
+ // Every pump pauses while ANY turn runs: the six-slot HTTP/1.1 pool is
+ // per origin, so one chat's turn frees the idle streams of all the
+ // others or a mid-turn attach never starts.
+ running: () => runningRef.current.size > 0, setChats,
});
};
+ /** A closed tab's pump would otherwise hold its stream — and its pool slot —
+ * until unload; the server only ends it when the client hangs up. */
+ const unwatchIdle = (chatId: number) => {
+ idleCtl.current.get(chatId)?.abort();
+ idleCtl.current.delete(chatId);
+ };
const adoptCatalog = async (chatId: number) => {
// Pill follows this chat's agent. A transient /api/models process is not that agent.
const handle = sessionsRef.current.has(chatId) ? handleOf(chatId) : undefined;
@@ -192,5 +201,5 @@ export function useHarnessSessions({sessionsRef, sessionNamesRef, chatsRef, work
};
catalogRef.current = { adopt: (id: number) => { if (!runningRef.current.has(id)) void adoptCatalog(id).catch(() => undefined); }, activeId };
- return { adoptCatalog, requireSession, refreshStored, projectsReady };
+ return { adoptCatalog, requireSession, refreshStored, projectsReady, unwatchIdle };
}
diff --git a/apps/native/lib/queued-attachments.test.ts b/apps/native/lib/queued-attachments.test.ts
new file mode 100644
index 00000000..52a82752
--- /dev/null
+++ b/apps/native/lib/queued-attachments.test.ts
@@ -0,0 +1,64 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { splitImageMarkers, withAttachmentMarkers } from "./attachments.ts";
+import { enqueuePrompt, shiftQueuedPrompt } from "./prompt-queue.ts";
+import { holdWhileIdle } from "./idle-http.ts";
+
+const staged = "/tmp/qa/graff-native-attachments/uuid-shot.png";
+const shot = { id: staged, name: "shot.png", path: staged };
+
+test("a queued follow-up carries its attachment markers like an immediate send", () => {
+ // Busy send: the composer appends markers, the harness enqueues the text,
+ // and the drain hands that same text to the queued run.
+ const queued = enqueuePrompt([], withAttachmentMarkers("look at this", [shot]), 1);
+ assert.equal(queued[0]?.text, `look at this @[${staged}]`);
+ const { next, rest } = shiftQueuedPrompt(queued);
+ assert.equal(next?.text, `look at this @[${staged}]`);
+ assert.deepEqual(rest, []);
+});
+
+test("the queued row still finds the staged image behind the marker", () => {
+ const { next } = shiftQueuedPrompt(enqueuePrompt([], withAttachmentMarkers("compare", [shot]), 1));
+ const markers = splitImageMarkers(next?.text ?? "").filter((_, index) => index % 2 === 1);
+ assert.deepEqual(markers, [`@[${staged}]`]);
+});
+
+test("every chat's idle stream drops while any turn runs", async () => {
+ // Two chats share one running set, as the harness passes it: chat B's idle
+ // hold must drop when chat A's turn starts, freeing B's pool slot for A's
+ // mid-turn attach, and reopen once nothing runs.
+ const running = new Set<number>();
+ const busy = () => running.size > 0;
+ const hang = (stream: AbortSignal, onAbort: () => void) =>
+ new Promise<void>((_, reject) => {
+ stream.addEventListener("abort", () => {
+ onAbort();
+ reject(new DOMException("aborted", "AbortError"));
+ }, { once: true });
+ });
+
+ const ac = new AbortController();
+ let dropped = 0;
+ const waiting = holdWhileIdle({ signal: ac.signal, busy, pollMs: 10, open: (stream) => hang(stream, () => { dropped += 1; }) });
+ await new Promise((resolve) => setTimeout(resolve, 15));
+ running.add(1); // chat A's turn starts; B's idle stream must go.
+ assert.equal(await waiting, "paused");
+ assert.equal(dropped, 1);
+
+ running.delete(1);
+ const ac2 = new AbortController();
+ let opened = 0;
+ const idle = holdWhileIdle({
+ signal: ac2.signal,
+ busy,
+ pollMs: 10,
+ open: (stream) => {
+ opened += 1;
+ return hang(stream, () => {});
+ },
+ });
+ await new Promise((resolve) => setTimeout(resolve, 15));
+ assert.equal(opened, 1);
+ ac2.abort();
+ assert.equal(await idle, "aborted");
+});
diff --git a/apps/native/package.json b/apps/native/package.json
index 0a9b7b8f..9f29c89c 100644
--- a/apps/native/package.json
+++ b/apps/native/package.json
@@ -10,7 +10,7 @@
"start": "NEXT_TELEMETRY_DISABLED=1 next start --port 3000",
"mock-serve": "node scripts/mock-serve.mjs",
"lint": "next lint",
- "test": "node --experimental-strip-types --test lib/cite-markup.test.ts lib/graff-events.test.ts lib/acp.test.ts lib/sessions.test.ts lib/session-store.test.ts lib/follow-scroll.test.ts lib/prompt-queue.test.ts lib/prompt-queue-steer.test.ts lib/prompt-queue-resume.test.ts lib/streaming-code.test.ts lib/agents.test.ts lib/mermaid-plugin.test.ts lib/prompt-history.test.ts lib/attachments.test.ts lib/idle-http.test.ts lib/composer-model-pick.test.ts lib/workspaces.test.ts lib/fuzzy.test.ts lib/folder-picker.test.ts lib/browser/annotations.test.ts lib/browser/url.test.ts lib/extension-bridge.test.ts components/site/harness-split.test.ts components/site/appearance-panel.test.ts",
+ "test": "node --experimental-strip-types --test lib/cite-markup.test.ts lib/graff-events.test.ts lib/acp.test.ts lib/sessions.test.ts lib/session-store.test.ts lib/follow-scroll.test.ts lib/prompt-queue.test.ts lib/prompt-queue-steer.test.ts lib/prompt-queue-resume.test.ts lib/streaming-code.test.ts lib/agents.test.ts lib/mermaid-plugin.test.ts lib/prompt-history.test.ts lib/attachments.test.ts lib/queued-attachments.test.ts lib/idle-http.test.ts lib/composer-model-pick.test.ts lib/workspaces.test.ts lib/fuzzy.test.ts lib/folder-picker.test.ts lib/browser/annotations.test.ts lib/browser/url.test.ts lib/extension-bridge.test.ts components/site/harness-split.test.ts components/site/appearance-panel.test.ts",
"electron:build": "bash electron/build.sh",
"test:electron": "bun test electron/policy.test.cjs",
"test:webauthn": "node scripts/test-webauthn.mjs",