Skip to content

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

Python

Refresh catalog after pending requests settle

Rach Pradhand41c97647219
diff --git a/apps/native/components/site/GraffHarness.tsx b/apps/native/components/site/GraffHarness.tsx
index 05049401..948e61bc 100644
--- a/apps/native/components/site/GraffHarness.tsx
+++ b/apps/native/components/site/GraffHarness.tsx
@@ -187,7 +187,7 @@ export default function GraffHarness() {
setPinsByChat(pinsRef.current);
};
- const { adoptCatalog, requireSession, refreshStored, projectsReady, unwatchIdle, chatCatalogs, catalogStatus, applyCatalog } = useHarnessSessions({ onPermission: permissions.update,
+ const { adoptCatalog, refreshChangedCatalog, requireSession, refreshStored, projectsReady, unwatchIdle, chatCatalogs, catalogStatus, applyCatalog } = useHarnessSessions({ onPermission: permissions.update,
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,
});
@@ -224,7 +224,7 @@ export default function GraffHarness() {
const runPrompt = createPromptRunner({ onPermission: permissions.update, onStarted: started, onCompleted: completed,
runningRef, steerer, setFollowing, chatsRef, model, msgIdRef, setChats, setBusyFor, setHistory, pinsRef, handleOf, setPins, requireSession,
sessionIsCurrent: (id, sessionId) => sessionsRef.current.get(id) === sessionId && chatsRef.current.some(chat => chat.id === id),
- prepareModel, adoptCatalog, refreshStored, takeQueuedPrompt, setCancelError
+ prepareModel, adoptCatalog, refreshChangedCatalog, refreshStored, takeQueuedPrompt, setCancelError
});
const settings = useQuietSettings({ requireSession, handleOf, running: runningRef.current, apply: applyCatalog });
const send = async (text: string, forChat?: number) => {
diff --git a/apps/native/components/site/harness-prompt-runner.ts b/apps/native/components/site/harness-prompt-runner.ts
index a10df6bb..98864ead 100644
--- a/apps/native/components/site/harness-prompt-runner.ts
+++ b/apps/native/components/site/harness-prompt-runner.ts
@@ -25,10 +25,11 @@ type Props = {
setPins(id: number, pins: BrowserPin[]): void; requireSession(id: number): Promise<string>;
sessionIsCurrent(id: number, sessionId: string): boolean;
prepareModel(id: number): Promise<void>;
- adoptCatalog(id: number): Promise<void>; refreshStored(): Promise<void>;
+ adoptCatalog(id: number): Promise<void>; refreshChangedCatalog(id: number, sessionId: string): Promise<void>;
+ refreshStored(): Promise<void>;
takeQueuedPrompt(id: number): QueuedPrompt | undefined;
};
-export function createPromptRunner({onPermission, onStarted, onCompleted, runningRef, steerer, setFollowing, chatsRef, model, msgIdRef, setChats, setBusyFor, setHistory, pinsRef, handleOf, setPins, requireSession, sessionIsCurrent, prepareModel, adoptCatalog, refreshStored, takeQueuedPrompt, setCancelError}: Props) {
+export function createPromptRunner({onPermission, onStarted, onCompleted, runningRef, steerer, setFollowing, chatsRef, model, msgIdRef, setChats, setBusyFor, setHistory, pinsRef, handleOf, setPins, requireSession, sessionIsCurrent, prepareModel, adoptCatalog, refreshChangedCatalog, refreshStored, takeQueuedPrompt, setCancelError}: Props) {
const patchAssistant = (chatId: number, msgId: number, next: AssistantTurn) => {
setChats((current) =>
current.map((c) =>
@@ -144,8 +145,9 @@ export function createPromptRunner({onPermission, onStarted, onCompleted, runnin
setBusyFor(chatId, false);
// A turn may change effort without a slash command. Refresh once after
// its stream ends, even on cancellation or a later transport error.
- if ((configChanged || (settingsCommand && promptCompleted)) && sessionId && sessionIsCurrent(chatId, sessionId)) {
- void adoptCatalog(chatId).catch(() => undefined);
+ if (sessionId && sessionIsCurrent(chatId, sessionId)) {
+ if (configChanged) void refreshChangedCatalog(chatId, sessionId).catch(() => undefined);
+ else if (settingsCommand && promptCompleted) void adoptCatalog(chatId).catch(() => undefined);
}
void refreshStored();
setTimeout(() => void refreshStored(), 2500);
diff --git a/apps/native/components/site/useHarnessSessions.ts b/apps/native/components/site/useHarnessSessions.ts
index ff17bfaa..bca0bc31 100644
--- a/apps/native/components/site/useHarnessSessions.ts
+++ b/apps/native/components/site/useHarnessSessions.ts
@@ -3,6 +3,7 @@ import { useEffect, useRef, useState, type MutableRefObject, type Dispatch, type
import { bindMcpAppChat, checkHealth, disposePage, ensureSession, fetchModels, type Health } from "@/lib/acp-client";
import { shouldReapPage } from "@/lib/acp-terminate";
import { catalogMayWriteChatModel, catalogMayWriteGlobalKey, sameModels, rememberChatCatalog, sharedModelChoices, startWithSelectedModel } from "@/lib/composer-model";
+import { refreshAfterPendingCatalog } from "@/lib/catalog-refresh";
import { pumpIdlePeerTurns } from "./idle-peer-turns";
import type { AcpCommand } from "@/lib/acp";
import type { PromptModel } from "@/components/primitives/PromptBar";
@@ -99,6 +100,10 @@ export function useHarnessSessions({onPermission, sessionsRef, sessionNamesRef,
catalogPending.current.set(chatId, task);
return task;
};
+ const refreshChangedCatalog = (chatId: number, sessionId: string): Promise<void> =>
+ refreshAfterPendingCatalog(catalogPending.current.get(chatId),
+ () => sessionsRef.current.get(chatId) === sessionId && chatsRef.current.some(chat => chat.id === chatId),
+ () => adoptCatalog(chatId));
// Refresh the catalog on focus. If its agent was
// not up yet, or the page outlived a restart — would show the fallback
@@ -253,5 +258,5 @@ export function useHarnessSessions({onPermission, sessionsRef, sessionNamesRef,
};
catalogRef.current = { adopt: (id: number) => { if (!runningRef.current.has(id)) void adoptCatalog(id).catch(() => undefined); }, activeId };
- return { adoptCatalog, requireSession, refreshStored, projectsReady, unwatchIdle, chatCatalogs, catalogStatus, applyCatalog };
+ return { adoptCatalog, refreshChangedCatalog, requireSession, refreshStored, projectsReady, unwatchIdle, chatCatalogs, catalogStatus, applyCatalog };
}
diff --git a/apps/native/lib/acp-client-stream.test.ts b/apps/native/lib/acp-client-stream.test.ts
index b985f80e..f398bbef 100644
--- a/apps/native/lib/acp-client-stream.test.ts
+++ b/apps/native/lib/acp-client-stream.test.ts
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
import {prompt} from './acp-client';
import {createPromptRunner} from '../components/site/harness-prompt-runner';
import {createQueueSteerer} from './prompt-queue-steer';
+import {refreshAfterPendingCatalog} from './catalog-refresh';
const chunk = {method:'session/update',params:{sessionId:'test',update:{sessionUpdate:'agent_message_chunk',content:{type:'text',text:'Visible partial reply'}}}};
async function withWire(lines:unknown[], body:(received:unknown[])=>Promise<void>) {
const original=globalThis.fetch, received:unknown[]=[];
@@ -89,6 +90,9 @@ test('prompt catalog refresh follows configuration updates once after this chat
prepareModel: async () => {}, adoptCatalog: async () => {
assert.equal(runningRef.current.has(1), false, 'refresh starts after turn cleanup');
refreshes++;
+ }, refreshChangedCatalog: async () => {
+ assert.equal(runningRef.current.has(1), false, 'refresh starts after turn cleanup');
+ refreshes++;
}, refreshStored: async () => {}, takeQueuedPrompt: () => undefined,
});
await runner(1, text);
@@ -109,3 +113,23 @@ test('prompt catalog refresh follows configuration updates once after this chat
globalThis.cancelAnimationFrame = savedCancelFrame;
}
});
+
+test('configuration refresh waits out an older catalog fetch and checks session ownership', async () => {
+ let finish!: () => void;
+ const pending = new Promise<void>(resolve => { finish = resolve; });
+ let refreshed = 0;
+ const next = refreshAfterPendingCatalog(pending, () => true, async () => { refreshed++; });
+ assert.equal(refreshed, 0, 'old focus fetch must finish first');
+ finish();
+ await next;
+ assert.equal(refreshed, 1, 'one fresh fetch follows the old snapshot');
+
+ let owned = true;
+ let finishStale!: () => void;
+ const stale = new Promise<void>(resolve => { finishStale = resolve; });
+ const ignored = refreshAfterPendingCatalog(stale, () => owned, async () => { refreshed++; });
+ owned = false;
+ finishStale();
+ await ignored;
+ assert.equal(refreshed, 1, 'closed or replaced session cannot refresh the picker');
+});
diff --git a/apps/native/lib/catalog-refresh.ts b/apps/native/lib/catalog-refresh.ts
new file mode 100644
index 00000000..62b0a849
--- /dev/null
+++ b/apps/native/lib/catalog-refresh.ts
@@ -0,0 +1,10 @@
+/** An in-flight focus fetch may have sampled settings before this turn changed
+ * them. Wait for it to settle, then request one post-turn snapshot. */
+export async function refreshAfterPendingCatalog(
+ pending: Promise<unknown> | undefined,
+ current: () => boolean,
+ refresh: () => Promise<void>,
+): Promise<void> {
+ if (pending) await pending.catch(() => undefined);
+ if (current()) await refresh();
+}