Skip to content

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

Python

Reset prompt history across scope round trips

yxlyx36c2eb2c1cd8
diff --git a/gui/src/app/sessionStoreAttachments.test.ts b/gui/src/app/sessionStoreAttachments.test.ts
index cf624fee..85e8c368 100644
--- a/gui/src/app/sessionStoreAttachments.test.ts
+++ b/gui/src/app/sessionStoreAttachments.test.ts
@@ -31,8 +31,21 @@ test("attachment replacement removes the previous history entry atomically", ()
];
sessionStore.getState().addAttachments(key, [previous]);
+ const replacementNotifications: string[][] = [];
+ const unsubscribe = sessionStore.subscribe((current, prior) => {
+ if (current.attachmentsByKey[key] !== prior.attachmentsByKey[key]) {
+ replacementNotifications.push(
+ (current.attachmentsByKey[key] ?? []).map((item) => item.path),
+ );
+ }
+ });
+
sessionStore.getState().replaceAttachments(key, next);
+ unsubscribe();
+ expect(replacementNotifications).toEqual([
+ next.map((item) => item.path),
+ ]);
expect(getAttachments(key)).toEqual(next);
expect(getAttachments(key)).not.toContainEqual(previous);
diff --git a/gui/src/components/PromptInputCard.navigation.test.tsx b/gui/src/components/PromptInputCard.navigation.test.tsx
index a56162c7..a81cdd2c 100644
--- a/gui/src/components/PromptInputCard.navigation.test.tsx
+++ b/gui/src/components/PromptInputCard.navigation.test.tsx
@@ -51,17 +51,26 @@ afterAll(async () => {
const workspacePath = "/workspace/history-tests";
+function getBinding(
+ scope: string,
+ bindings?: Record<string, ChatBinding>,
+): ChatBinding {
+ return bindings?.[scope] ?? { conversationId: scope, workspacePath };
+}
+
function ComposerHarness({
+ bindings,
histories,
initialDrafts,
scope,
}: {
+ bindings?: Record<string, ChatBinding>;
histories: Record<string, string[]>;
initialDrafts: Record<string, string>;
scope: string;
}) {
const [drafts, setDrafts] = useState(initialDrafts);
- const binding: ChatBinding = { conversationId: scope, workspacePath };
+ const binding = getBinding(scope, bindings);
const promptDraft = drafts[scope] ?? "";
return (
@@ -84,12 +93,13 @@ function ComposerHarness({
stopPrompt={async () => {}}
submitPrompt={async () => {}}
updatePromptSettings={async () => {}}
- workspacePath={workspacePath}
+ workspacePath={binding.workspacePath}
/>
);
}
function mountComposer(input: {
+ bindings?: Record<string, ChatBinding>;
histories: Record<string, string[]>;
initialAttachments?: Record<string, Attachment[]>;
initialDrafts: Record<string, string>;
@@ -99,7 +109,11 @@ function mountComposer(input: {
for (const [conversationId, attachments] of Object.entries(
input.initialAttachments ?? {},
)) {
- const key = getPromptDraftKey(workspacePath, conversationId)!;
+ const binding = getBinding(conversationId, input.bindings);
+ const key = getPromptDraftKey(
+ binding.workspacePath,
+ binding.conversationId,
+ )!;
sessionStore.getState().addAttachments(key, attachments);
}
const container = document.createElement("div");
@@ -111,6 +125,7 @@ function mountComposer(input: {
flushSync(() => {
root.render(
<ComposerHarness
+ bindings={input.bindings}
histories={input.histories}
initialDrafts={input.initialDrafts}
scope={scope}
@@ -228,6 +243,78 @@ test("equal-length chat switches cannot restore another chat's draft or attachme
}
});
+test("scope round trips cannot revive an earlier cursor or attachment snapshot", () => {
+ const firstAttachment = image("/images/first-live.png");
+ const composer = mountComposer({
+ histories: {
+ first: ["first old", "first new"],
+ second: ["second old", "second new"],
+ },
+ initialAttachments: { first: [firstAttachment] },
+ initialDrafts: {
+ first: "first draft",
+ second: "second draft",
+ },
+ scope: "first",
+ });
+
+ try {
+ composer.arrow("ArrowUp");
+ expect(composer.textareaValue()).toBe("first new");
+ expect(attachmentTitles(composer.container)).toEqual([]);
+
+ composer.switchScope("second");
+ composer.switchScope("first");
+ composer.arrow("ArrowDown");
+
+ expect(composer.textareaValue()).toBe("first new");
+ expect(attachmentTitles(composer.container)).toEqual([]);
+ } finally {
+ composer.close();
+ }
+});
+
+test("same conversation ids keep attachments isolated across workspaces", () => {
+ const firstAttachment = image("/images/workspace-first.png");
+ const secondAttachment = image("/images/workspace-second.png");
+ const composer = mountComposer({
+ bindings: {
+ first: {
+ conversationId: "shared-conversation",
+ workspacePath: "/workspace/first",
+ },
+ second: {
+ conversationId: "shared-conversation",
+ workspacePath: "/workspace/second",
+ },
+ },
+ histories: { first: [], second: [] },
+ initialAttachments: {
+ first: [firstAttachment],
+ second: [secondAttachment],
+ },
+ initialDrafts: {
+ first: "first workspace draft",
+ second: "second workspace draft",
+ },
+ scope: "first",
+ });
+
+ try {
+ expect(attachmentTitles(composer.container)).toEqual([
+ firstAttachment.path,
+ ]);
+
+ composer.switchScope("second");
+ expect(composer.textareaValue()).toBe("second workspace draft");
+ expect(attachmentTitles(composer.container)).toEqual([
+ secondAttachment.path,
+ ]);
+ } finally {
+ composer.close();
+ }
+});
+
test("each recalled prompt replaces attachments and exiting restores only the live draft", () => {
const live = image("/images/live.png");
const newest = image("/images/newest.png");
diff --git a/gui/src/components/PromptInputCard.tsx b/gui/src/components/PromptInputCard.tsx
index 3e00da66..c8e307f7 100644
--- a/gui/src/components/PromptInputCard.tsx
+++ b/gui/src/components/PromptInputCard.tsx
@@ -130,8 +130,10 @@ export function PromptInputCard({
const [attachmentsBeforeHistory, setAttachmentsBeforeHistory] = useState<
Attachment[]
>([]);
- const promptHistoryScopeRef = useRef(attachmentScopeKey);
- const promptHistoryLengthRef = useRef(promptHistory.length);
+ const [historyScope, setHistoryScope] = useState(() => ({
+ attachmentScopeKey,
+ historyLength: promptHistory.length,
+ }));
function resetHistoryNavigation() {
setHistoryCursor(null);
@@ -139,6 +141,19 @@ export function PromptInputCard({
setAttachmentsBeforeHistory([]);
}
+ // Adjust state during render so a chat/workspace switch invalidates the
+ // previous scope's cursor and snapshots before another key event can run.
+ if (
+ historyScope.attachmentScopeKey !== attachmentScopeKey ||
+ historyScope.historyLength !== promptHistory.length
+ ) {
+ setHistoryScope({
+ attachmentScopeKey,
+ historyLength: promptHistory.length,
+ });
+ resetHistoryNavigation();
+ }
+
// Whether /fast actually changes harness behavior. The harness only applies
// the priority service tier on the codex provider (the `responses` kind); it
// is a no-op everywhere else. The runtime reports this authoritatively via
@@ -254,37 +269,19 @@ export function PromptInputCard({
return false;
}
- const didHistoryScopeChange =
- promptHistoryScopeRef.current !== attachmentScopeKey;
- const didHistoryLengthChange =
- promptHistoryLengthRef.current !== promptHistory.length;
- const didHistoryChange = didHistoryScopeChange || didHistoryLengthChange;
- const effectiveCursor = didHistoryChange ? null : historyCursor;
- const effectiveDraftBeforeHistory = didHistoryChange
- ? ""
- : draftBeforeHistory;
- const effectiveAttachmentsBeforeHistory = didHistoryChange
- ? []
- : attachmentsBeforeHistory;
- promptHistoryScopeRef.current = attachmentScopeKey;
- promptHistoryLengthRef.current = promptHistory.length;
- if (didHistoryChange) {
- resetHistoryNavigation();
- }
-
// Once history browsing has started, arrows always move between recalled
// prompts. The line-boundary guard only protects multiline drafts before
// entering history; otherwise a recalled multiline prompt appears stuck.
const textarea = event.currentTarget;
if (
- effectiveCursor == null &&
+ historyCursor == null &&
direction === "previous" &&
!isCaretOnFirstLine(textarea)
) {
return false;
}
if (
- effectiveCursor == null &&
+ historyCursor == null &&
direction === "next" &&
!isCaretOnLastLine(textarea)
) {
@@ -294,9 +291,9 @@ export function PromptInputCard({
const result = getPromptHistoryNavigationResult({
direction,
promptHistory,
- cursor: effectiveCursor,
+ cursor: historyCursor,
currentDraft: promptDraft,
- draftBeforeHistory: effectiveDraftBeforeHistory,
+ draftBeforeHistory,
});
if (result == null) {
return false;
@@ -307,7 +304,7 @@ export function PromptInputCard({
// Mirror the draft preservation for attachments: snapshot the live tray when
// entering history so ArrowDown back out restores it, and rehydrate the
// attachment cards from the stored `Attached files:` block on each entry.
- const enteringHistory = effectiveCursor == null && result.cursor != null;
+ const enteringHistory = historyCursor == null && result.cursor != null;
if (enteringHistory) {
setAttachmentsBeforeHistory(attachments);
}
@@ -318,7 +315,7 @@ export function PromptInputCard({
if (result.cursor == null) {
// Exited history — restore the live draft and its attachments.
setPromptDraft(result.draft);
- replaceAttachments(effectiveAttachmentsBeforeHistory);
+ replaceAttachments(attachmentsBeforeHistory);
setAttachmentsBeforeHistory([]);
} else {
const { body, paths } = parseAttachmentBlock(result.draft);