Skip to content

Commit 2a9ab80

Browse files
committed
fix(app-shell): a first send's mark goes only to an id created after that send
createdConversationId is the row the hook minted last and is never cleared, so a thread an earlier send of this dock created, resolved again or opened from history while a send waited, took the page's record. The mark now keeps the id the hook held when the send was made, and only a newer one takes it. The per-target spend is gone: the held send marks again after a target change, so it never decided anything. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibwivzADop4jGmFZ81y
1 parent fdb5e40 commit 2a9ab80

3 files changed

Lines changed: 96 additions & 44 deletions

File tree

‎ui/packages/app-shell/src/hooks/useChatConversation.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ export interface UseChatConversationReturn {
152152
/**
153153
* objectstack#37861 — the id of the row this hook's own create minted last
154154
* (a first send, through `ensureConversation`), `undefined` before any. It
155-
* tells a host the conversation its first send created from one it resolved
156-
* or was handed: a thread a send made during the resolve joins, a history
157-
* row opened while the create was on the wire.
155+
* is never cleared: a host that reads it as "the row this send created"
156+
* compares it with the value it saw when the send was made, since a thread
157+
* an earlier send created can be resolved again or opened from history.
158158
*/
159159
createdConversationId: string | undefined;
160160
isLoading: boolean;

‎ui/packages/app-shell/src/layout/__tests__/chatFocus-36999.test.tsx‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,65 @@ describe('the console dock — a conversation opened again restores its own focu
445445
expect(pageChip()).toBeInTheDocument();
446446
});
447447

448+
it('a thread this dock created, resolved again while a send waits, keeps its own ×', async () => {
449+
mountDock();
450+
await composerReady();
451+
expect((await send('first')).focus).toEqual([ACCOUNT_1]);
452+
fireEvent.click(removeButton()!);
453+
expect(readStoredChatFocus('conv-1')).toEqual({ focus: null, dismissed: true });
454+
455+
page.open({ agent: 'ask', fresh: true });
456+
await composerReady();
457+
await waitFor(() => expect(screen.getByTestId('chat-focus-name')).toHaveTextContent('account · rec_1'));
458+
const read = gate();
459+
state.holdRead['conv-1'] = read.wait;
460+
page.open({ agent: 'ask', conversationId: 'conv-1' });
461+
await composerReady();
462+
463+
const before = state.sent.length;
464+
await act(async () => {
465+
state.pane!.onSendMessage!('asked during the resolve');
466+
});
467+
await act(async () => {
468+
read.open();
469+
});
470+
await waitFor(() => expect(state.sent.length).toBe(before + 1), { timeout: 4000 });
471+
expect(state.minted).toBe(1);
472+
expect(state.sent[before].context).not.toHaveProperty('focus');
473+
expect(readStoredChatFocus('conv-1')).toEqual({ focus: null, dismissed: true });
474+
expect(chip()).toBeNull();
475+
});
476+
477+
it("a thread this dock created, opened from history while a later send's create is on the wire, keeps its own ×", async () => {
478+
mountDock();
479+
await composerReady();
480+
expect((await send('first')).focus).toEqual([ACCOUNT_1]);
481+
fireEvent.click(removeButton()!);
482+
483+
page.open({ agent: 'ask', fresh: true });
484+
await composerReady();
485+
await waitFor(() => expect(screen.getByTestId('chat-focus-name')).toHaveTextContent('account · rec_1'));
486+
const create = gate();
487+
state.holdCreate = create.wait;
488+
const before = state.sent.length;
489+
await act(async () => {
490+
state.pane!.onSendMessage!('second thread');
491+
});
492+
await waitFor(() => expect(state.createAttempts).toBe(2));
493+
494+
page.open({ agent: 'ask', conversationId: 'conv-1' });
495+
await waitFor(() => expect(pageChip()).toBeInTheDocument());
496+
await act(async () => {
497+
create.open();
498+
await new Promise((resolve) => setTimeout(resolve, 50));
499+
});
500+
expect(readStoredChatFocus('conv-1')).toEqual({ focus: null, dismissed: true });
501+
expect(chip()).toBeNull();
502+
expect(state.sent.slice(before).map((body) => body.context?.focus)).not.toContainEqual([ACCOUNT_1]);
503+
await composerReady();
504+
expect(await send('and now?')).not.toHaveProperty('focus');
505+
});
506+
448507
it("a history row opened while the first send's create is on the wire keeps its own focus", async () => {
449508
window.sessionStorage.setItem('ai-chat-focus:conv-old', JSON.stringify({ focus: ACCOUNT_9, dismissed: false }));
450509
const create = gate();

‎ui/packages/app-shell/src/layout/chatFocus.ts‎

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ export interface ConversationFocus {
161161
* beside `ensureConversation`, and only while it has a user to create it
162162
* for): the id that create mints takes the focus the conversation had at
163163
* this moment. Only that id: one the host resolves or is handed instead (a
164-
* found thread the send joins, a history row) restores its own. A create
165-
* that fails spends the mark (see the hook's `createError`), and so does
166-
* another target while unsent.
164+
* found thread the send joins, a history row), even one an earlier send of
165+
* this host created, restores its own. A create that fails spends the mark
166+
* (see the hook's `createError`).
167167
*/
168168
markSending: () => void;
169169
}
@@ -177,47 +177,45 @@ interface FocusState {
177177
label?: string;
178178
/** What the host was asked to show when the state last moved (see `origin`). */
179179
origin: string | undefined;
180-
/**
181-
* How many times another target started another unsent conversation. A
182-
* first-send mark is good only for the unsent conversation it was made in.
183-
*/
184-
epoch: number;
185180
}
186181

187-
/** A first send's mark: the entry it fixes, for the unsent conversation it was made in. */
182+
/**
183+
* A first send's mark: the entry it fixes, and the id the host's create had
184+
* minted last when the send was made. Only an id minted after it, by this
185+
* send's own create, takes the entry.
186+
*/
188187
interface SendingMark {
189188
entry: ChatFocusEntry;
190-
epoch: number;
189+
createdBefore: string | undefined;
191190
}
192191

193192
/** The entry a conversation the host arrives at restores: this tab's copy, else its metadata's. */
194193
function restoredState(
195194
key: string | undefined,
196195
origin: string | undefined,
197196
fromMetadata: ChatFocusFromMetadata | null,
198-
epoch: number,
199197
): FocusState {
200198
const stored = readStoredEntry(key);
201-
if (stored) return { key, entry: stored, origin, epoch };
199+
if (stored) return { key, entry: stored, origin };
202200
if (key !== undefined && fromMetadata) {
203201
return {
204202
key,
205203
entry: { focus: { object: fromMetadata.object, id: fromMetadata.id }, dismissed: false },
206204
label: fromMetadata.label,
207205
origin,
208-
epoch,
209206
};
210207
}
211-
return { key, entry: EMPTY_ENTRY, origin, epoch };
208+
return { key, entry: EMPTY_ENTRY, origin };
212209
}
213210

214211
/**
215212
* The state the host's props move `state` to. `undefined` → the id the first
216-
* send's own create minted (`createdId`) takes the mark `sending` holds, when
217-
* that mark was made in this unsent conversation. Every other arrival of an
218-
* id restores that conversation's own entry: a thread the resolve found, which
219-
* a send made during the resolve joins, and a history row opened while the
220-
* create was on the wire, keep the focus they were fixed on.
213+
* send's own create minted takes the mark `sending` holds: `createdId`, the
214+
* host's latest create, when it is newer than the one the mark saw at the
215+
* send. Every other arrival of an id restores that conversation's own entry:
216+
* a thread the resolve found, which a send made during the resolve joins, and
217+
* a history row opened while the create was on the wire, keep the focus they
218+
* were fixed on, a thread this host created at an earlier send included.
221219
*/
222220
function nextFocusState(
223221
state: FocusState,
@@ -228,21 +226,15 @@ function nextFocusState(
228226
sending: { readonly current: SendingMark | null },
229227
): FocusState {
230228
if (state.key !== conversationId) {
231-
const mark =
232-
state.key === undefined && conversationId !== undefined && conversationId === createdId
233-
? sending.current
234-
: null;
235-
return mark && mark.epoch === state.epoch
236-
? { key: conversationId, entry: mark.entry, origin, epoch: state.epoch }
237-
: restoredState(conversationId, origin, fromMetadata, state.epoch);
229+
const mark = state.key === undefined && conversationId !== undefined ? sending.current : null;
230+
return mark && conversationId === createdId && createdId !== mark.createdBefore
231+
? { key: conversationId, entry: mark.entry, origin }
232+
: restoredState(conversationId, origin, fromMetadata);
238233
}
239234
if (state.origin !== origin) {
240235
// Another target while unsent is another conversation: × on the last one
241-
// does not carry over, nor does a first send's mark. A sent conversation
242-
// keeps its own entry.
243-
return state.key === undefined
244-
? { key: undefined, entry: EMPTY_ENTRY, origin, epoch: state.epoch + 1 }
245-
: { ...state, origin };
236+
// does not carry over. A sent conversation keeps its own entry.
237+
return state.key === undefined ? { key: undefined, entry: EMPTY_ENTRY, origin } : { ...state, origin };
246238
}
247239
if (
248240
state.key !== undefined &&
@@ -253,7 +245,7 @@ function nextFocusState(
253245
) {
254246
// The conversation's row arrived after its id: its metadata restores the
255247
// chip this tab never held.
256-
return restoredState(state.key, origin, fromMetadata, state.epoch);
248+
return restoredState(state.key, origin, fromMetadata);
257249
}
258250
return state;
259251
}
@@ -285,13 +277,14 @@ export function useConversationFocus(
285277
origin?: string,
286278
/**
287279
* The id of the row the host's own create minted last
288-
* (`useChatConversation().createdConversationId`): the one id a first
289-
* send's mark goes to.
280+
* (`useChatConversation().createdConversationId`). A first send's mark
281+
* goes only to an id it holds after that send, never to the one it held
282+
* when the send was made.
290283
*/
291284
createdId?: string,
292285
): ConversationFocus {
293286
const fromMetadata = React.useMemo(() => chatFocusFromMetadata(conversationMetadata), [conversationMetadata]);
294-
const [state, setState] = React.useState<FocusState>(() => restoredState(conversationId, origin, fromMetadata, 0));
287+
const [state, setState] = React.useState<FocusState>(() => restoredState(conversationId, origin, fromMetadata));
295288
// The focus a first send in flight fixes: a ref, not state, so marking it
296289
// re-renders nothing while the pane holds the message it is creating the
297290
// row for.
@@ -300,7 +293,7 @@ export function useConversationFocus(
300293
// Derived state set during render: React re-renders before committing, so
301294
// no child of this commit sees the previous conversation's focus.
302295
if (current !== state) setState(current);
303-
const { key, entry, label, epoch } = current;
296+
const { key, entry, label } = current;
304297

305298
React.useEffect(() => {
306299
if (key !== undefined) writeStoredChatFocus(key, entry);
@@ -330,17 +323,17 @@ export function useConversationFocus(
330323
// What the conversation has as of this commit, for `markSending`, which runs
331324
// in an event or a child's effect after it. An insertion effect runs before
332325
// every layout and passive effect of the commit, the children's included.
333-
const effectiveRef = React.useRef<SendingMark>({ entry: EMPTY_ENTRY, epoch: 0 });
326+
const effectiveRef = React.useRef<SendingMark>({ entry: EMPTY_ENTRY, createdBefore: undefined });
334327
React.useInsertionEffect(() => {
335-
effectiveRef.current = { entry: { focus: effective, dismissed: entry.dismissed }, epoch };
336-
}, [effective, entry.dismissed, epoch]);
328+
effectiveRef.current = { entry: { focus: effective, dismissed: entry.dismissed }, createdBefore: createdId };
329+
}, [effective, entry.dismissed, createdId]);
337330

338331
const dismiss = React.useCallback(() => {
339332
setState((s) => (s.entry.dismissed && !s.entry.focus ? s : { ...s, entry: { focus: null, dismissed: true }, label: undefined }));
340333
}, []);
341334
const markSending = React.useCallback(() => {
342-
const { entry: now, epoch: madeIn } = effectiveRef.current;
343-
sendingRef.current = { entry: { ...now }, epoch: madeIn };
335+
const { entry: now, createdBefore } = effectiveRef.current;
336+
sendingRef.current = { entry: { ...now }, createdBefore };
344337
}, []);
345338

346339
return React.useMemo(

0 commit comments

Comments
 (0)