Skip to content

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

TypeScript

diff: a docked pane reads the repository before it opens, so it never lands on Loading diff (#95488)

Alice T'Poteat92ec78f288a5
diff --git a/mods/diff/README.md b/mods/diff/README.md
index 95ba4e35..6cb76534 100644
--- a/mods/diff/README.md
+++ b/mods/diff/README.md
@@ -20,8 +20,10 @@ drawing's `viewport` says), the terminal is wide enough (144 columns when
the person never chose, 110 when they kept it open before; a person who
closed it is left alone) and file checkpointing is on; a subagent's edit
opens nothing, and where the surface does not say, nothing opens by
-itself. An open the engine leaves waiting undrawn is withdrawn, so no
-later resize seats it, and the next edit asks again.
+itself. A docked pane fetches before it opens, as the built-in panel
+primes its data, so it never lands on `Loading diff…`; an open the engine
+leaves waiting undrawn is withdrawn, so no later resize seats it, and the
+next edit asks again.
Under the fullscreen layout a terminal under 110 columns gets the
built-in's line asking for a wider one and nothing opens. Without that
diff --git a/mods/diff/hooks/register.ts b/mods/diff/hooks/register.ts
index 05607b37..7718b9ac 100644
--- a/mods/diff/hooks/register.ts
+++ b/mods/diff/hooks/register.ts
@@ -34,7 +34,7 @@ import Views from './views'
*
* Git runs when the built-in's would: `session.start` binds the host and
* registers `/diff`; `/diff` or the main loop's first checkpointed edit with
- * room pins the backend, until `/clear`; only a placed, open pane fetches.
+ * room pins the backend, until `/clear`; a docked pane fetches, then opens.
*
* @param on the engine's registrar
*/
@@ -53,6 +53,7 @@ export function register(on: On) {
let isRefreshing = false
let isRefreshQueued = false
let generation = 0
+ let landed = 0
let bodyStamp: string | null = null
let bodyBase: string | null = null
@@ -409,6 +410,12 @@ export function register(on: On) {
dialogRows = isDialog ? Views.dialogRowsOf(model) : null
+ const landedBefore = landed
+
+ if (!isDialog) {
+ await refresh(engine).catch(() => undefined)
+ }
+
const opened = await engine.openPane(
isDialog
? { ...dialogPane(), focus: true }
@@ -432,7 +439,11 @@ export function register(on: On) {
Record.recorderOf(engine).shown(trigger, Record.widthBucketOf(columns))
}
- void refresh(engine)
+ const isStale = isDialog || landed !== landedBefore
+
+ if (isStale) {
+ void refresh(engine)
+ }
return true
}
@@ -860,11 +871,15 @@ export function register(on: On) {
result.deny === undefined &&
result.isError !== true
- const isStale =
- isPaneOpen &&
- (isEdit ? hasEdited : result === undefined || result.deny === undefined)
+ const hasLanded = isEdit
+ ? hasEdited
+ : result === undefined || result.deny === undefined
- if (isStale) {
+ if (hasLanded) {
+ landed += 1
+ }
+
+ if (hasLanded && isPaneOpen) {
scheduleRefresh(engine)
}
diff --git a/mods/diff/tests/fixtures/beneath/index.ts b/mods/diff/tests/fixtures/beneath/index.ts
index a39d0383..14b085b5 100644
--- a/mods/diff/tests/fixtures/beneath/index.ts
+++ b/mods/diff/tests/fixtures/beneath/index.ts
@@ -1,4 +1,5 @@
export type * from './beneath.js'
export * from './left-waiting.js'
+export * from './slow-diff-ms.js'
export * as default from '.'
diff --git a/mods/diff/tests/fixtures/beneath/slow-diff-ms.ts b/mods/diff/tests/fixtures/beneath/slow-diff-ms.ts
new file mode 100644
index 00000000..2acc1484
--- /dev/null
+++ b/mods/diff/tests/fixtures/beneath/slow-diff-ms.ts
@@ -0,0 +1,5 @@
+/**
+ * How long a slow repository takes to answer `git diff` in the tests that
+ * time the pane's opening against its first fetch: well inside git's timeout.
+ */
+export const SLOW_DIFF_MS = 3000
diff --git a/mods/diff/tests/register.test.ts b/mods/diff/tests/register.test.ts
index a5c840b4..9ddb256c 100644
--- a/mods/diff/tests/register.test.ts
+++ b/mods/diff/tests/register.test.ts
@@ -564,10 +564,16 @@ describe('register', () => {
'and withdrew the pane the engine left waiting, so no resize seats it',
).toEqual(['diff'])
+ expect(
+ world.runs.map(run => Fixtures.gitWordOf(run.argv)).slice(0, 2),
+ 'the repository was found and read once before the open, as the ' +
+ 'built-in primes; nothing polls for a pane no one sees',
+ ).toEqual(['rev-parse --show-toplevel', 'status'])
+
expect(
world.runs.map(run => Fixtures.gitWordOf(run.argv)),
- 'the repository was found, nothing fetched for a pane no one sees',
- ).toEqual(['rev-parse --show-toplevel'])
+ 'no HEAD poll for a withdrawn pane',
+ ).not.toContain(Fixtures.POLL_WORD)
isNarrow = false
@@ -622,6 +628,59 @@ describe('register', () => {
).toEqual(['diff'])
})
+ test('a docked pane opens once its first fetch settled', async ($, on) => {
+ const opened: string[] = []
+ const clock = Fixtures.startsSession(on)
+
+ on('process.run', async ($, e) => {
+ if (e.argv.includes('--shortstat')) {
+ await clock.sleep(Fixtures.SLOW_DIFF_MS)
+ }
+
+ return { value: Fixtures.gitIn(e.argv) }
+ })
+
+ on('ui.open', ($, e) => {
+ opened.push(e.id)
+
+ return { value: undefined }
+ })
+
+ on('ui.close', () => ({ value: undefined }))
+ on('ui.invalidate', () => ({ value: undefined }))
+ on('ui.render', { component: 'PromptHint' }, () => Fixtures.HINT_DRAWN)
+ on('session.messages', () => ({ value: [] }))
+ on('settings.read', () => ({ value: {} }))
+ on('tool.call', () => ({ result: 'edited' }))
+ mock.store(on)
+ mock.env(on, {})
+
+ await $.session.start(Fixtures.SESSION)
+ await $.ui.render(Fixtures.HINT)
+
+ await $.tool.call({
+ tool: 'Edit',
+ file_path: '/work/app.ts',
+ old_string: '1',
+ new_string: '2',
+ })
+
+ await clock.advance(Fixtures.SLOW_DIFF_MS - 1)
+
+ expect(opened, 'git has not answered: no pane, no Loading frame').toEqual(
+ [],
+ )
+
+ await clock.advance(Fixtures.SETTLE_MS)
+
+ expect(opened, 'the fetch settled: the pane opens filled').toEqual(['diff'])
+
+ const drawn = Fixtures.textOf(await $.ui.render(Fixtures.PANE))
+
+ expect(drawn).toContain('1 file changed')
+ expect(drawn).not.toContain('Loading diff')
+ })
+
test('/clear closes the pane it finds open', async ($, on) => {
const world = Fixtures.inRepository(on)