Skip to content

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

Python

Fix restored chat composer caret position

yxlyx0c8841a1229e
diff --git a/apps/native/components/site/useNavigationPromptFocus.ts b/apps/native/components/site/useNavigationPromptFocus.ts
index 41149fd0..f7291f96 100644
--- a/apps/native/components/site/useNavigationPromptFocus.ts
+++ b/apps/native/components/site/useNavigationPromptFocus.ts
@@ -1,4 +1,5 @@
import { useLayoutEffect, useRef, useState } from "react";
+import { focusNavigationPrompt } from "@/lib/navigation-prompt-focus";
/** Explicit navigation, not active-pane changes or draft renders, owns focus. */
export function useNavigationPromptFocus() {
@@ -10,9 +11,7 @@ export function useNavigationPromptFocus() {
`[data-chat="${request.id}"] textarea[aria-label="Prompt"]`,
);
// Run once in the navigation commit, never retry later during typing.
- if (prompt && !prompt.disabled && prompt.getClientRects().length) {
- prompt.focus({ preventScroll: true });
- }
+ if (prompt) focusNavigationPrompt(prompt);
}, [request]);
return { promptFocusRoot: root, requestPromptFocus: (id: number) => setRequest({ id }) };
}
diff --git a/apps/native/lib/navigation-prompt-focus.test.ts b/apps/native/lib/navigation-prompt-focus.test.ts
new file mode 100644
index 00000000..55eec12b
--- /dev/null
+++ b/apps/native/lib/navigation-prompt-focus.test.ts
@@ -0,0 +1,31 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { focusNavigationPrompt, type NavigationPrompt } from "./navigation-prompt-focus.ts";
+
+function prompt(value: string, options: { disabled?: boolean; visible?: boolean } = {}) {
+ const calls: { focused: boolean; selection: [number, number] | null } = { focused: false, selection: null };
+ const target: NavigationPrompt = {
+ disabled: options.disabled ?? false,
+ value,
+ getClientRects: () => ({ length: options.visible === false ? 0 : 1 }),
+ focus: () => { calls.focused = true; },
+ setSelectionRange: (start, end) => { calls.selection = [start, end]; },
+ };
+ return { target, calls };
+}
+
+test("chat navigation focuses the composer with the caret at the end of its draft", () => {
+ const { target, calls } = prompt("continue here");
+ assert.equal(focusNavigationPrompt(target), true);
+ assert.equal(calls.focused, true);
+ assert.deepEqual(calls.selection, [13, 13]);
+});
+
+test("chat navigation leaves disabled or hidden composers untouched", () => {
+ for (const options of [{ disabled: true }, { visible: false }]) {
+ const { target, calls } = prompt("draft", options);
+ assert.equal(focusNavigationPrompt(target), false);
+ assert.equal(calls.focused, false);
+ assert.equal(calls.selection, null);
+ }
+});
diff --git a/apps/native/lib/navigation-prompt-focus.ts b/apps/native/lib/navigation-prompt-focus.ts
new file mode 100644
index 00000000..c0884f16
--- /dev/null
+++ b/apps/native/lib/navigation-prompt-focus.ts
@@ -0,0 +1,16 @@
+export type NavigationPrompt = {
+ disabled: boolean;
+ value: string;
+ getClientRects(): { length: number };
+ focus(options?: FocusOptions): void;
+ setSelectionRange(start: number, end: number): void;
+};
+
+/** Explicit chat navigation focuses the composer with typing ready at the end. */
+export function focusNavigationPrompt(prompt: NavigationPrompt): boolean {
+ if (prompt.disabled || !prompt.getClientRects().length) return false;
+ prompt.focus({ preventScroll: true });
+ const end = prompt.value.length;
+ prompt.setSelectionRange(end, end);
+ return true;
+}
diff --git a/apps/native/package.json b/apps/native/package.json
index f914cb28..587690b5 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/diff-chip-label.test.ts lib/motion-throttle.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/run-traces.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/composer-keyboard.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 lib/close-confirm.test.ts components/site/harness-split.test.ts components/site/appearance-panel.test.ts lib/ui-sounds.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/diff-chip-label.test.ts lib/motion-throttle.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/run-traces.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/composer-keyboard.test.ts lib/navigation-prompt-focus.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 lib/close-confirm.test.ts components/site/harness-split.test.ts components/site/appearance-panel.test.ts lib/ui-sounds.test.ts",
"electron:build": "bash electron/build.sh",
"test:electron": "bun test electron/policy.test.cjs",
"test:webauthn": "node scripts/test-webauthn.mjs",