Skip to content

Commit f68a2aa

Browse files
committed
refactor(mothership): tidy the forwarding of late queue writes
- one lookup instead of a hop loop: only a new-chat key moves, and only to its chat's key, which never does; liveQueueKey derives from liveQueuePosition - migrate keeps the first record, so a repeat cannot rewrite what was ahead - the history check drops its no-op forwarding: it never runs on a new-chat key - the dead-key DOM test waits on the queue instead of a fixed sleep
1 parent e30a941 commit f68a2aa

3 files changed

Lines changed: 18 additions & 24 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.dom.test.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,9 +2866,12 @@ describe('useChat remount send recovery', () => {
28662866
.catch(() => {})
28672867
})
28682868
await waitFor(() => state.postBodies.length === 2)
2869-
await act(async () => {
2870-
await sleep(200)
2871-
})
2869+
/** The refusal goes back to a queue; the assertions below say which one. */
2870+
await waitFor(() =>
2871+
Object.values(useMothershipQueueStore.getState().queues).some((queue) =>
2872+
queue.some((message) => message.content === 'Follow-up')
2873+
)
2874+
)
28722875

28732876
const queues = useMothershipQueueStore.getState().queues
28742877
expect(queues[DEDUPED_CHAT_ID]?.map((message) => message.content)).toEqual(['Follow-up'])

‎apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5077,7 +5077,7 @@ export function useChat(
50775077
}
50785078
clearQueuedSendHandoffState(msg.id)
50795079
clearQueuedSendHandoffClaim(msg.id)
5080-
useMothershipQueueStore.getState().remove(liveQueueKey(chatKey), msg.id)
5080+
useMothershipQueueStore.getState().remove(chatKey, msg.id)
50815081
return true
50825082
},
50835083
[queryClient]

‎apps/sim/stores/mothership-queue/store.ts‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,6 @@ const setQueueForChat = (
135135
): Record<string, QueuedMothershipMessage[]> =>
136136
next.length === 0 ? omitKey(queues, chatKey) : { ...queues, [chatKey]: next }
137137

138-
/**
139-
* The queue key a write captured before an `await` should use now: the key a
140-
* new-chat queue migrated to once its chat became known, if it did.
141-
*/
142-
export function liveQueueKey(chatKey: string): string {
143-
const { migratedTo } = useMothershipQueueStore.getState()
144-
let key = chatKey
145-
for (let hops = 0; hops < 8 && migratedTo[key] !== undefined; hops++) key = migratedTo[key].key
146-
return key
147-
}
148-
149138
/**
150139
* Where a message goes back into its queue after a write captured before an
151140
* `await`: in the queue's live key, right after the last message still there
@@ -158,14 +147,10 @@ export function liveQueuePosition(
158147
aheadIds: readonly string[]
159148
): { chatKey: string; index: number } {
160149
const { migratedTo, queues } = useMothershipQueueStore.getState()
161-
const ahead = new Set(aheadIds)
162-
let key = chatKey
163-
for (let hops = 0; hops < 8; hops++) {
164-
const migration = migratedTo[key]
165-
if (!migration) break
166-
for (const id of migration.ahead) ahead.add(id)
167-
key = migration.key
168-
}
150+
/** One lookup: only a new-chat key moves, and only to its chat's key, which never does. */
151+
const migration = migratedTo[chatKey]
152+
const key = migration?.key ?? chatKey
153+
const ahead = new Set([...aheadIds, ...(migration?.ahead ?? [])])
169154
const queue = queues[key] ?? []
170155
let index = 0
171156
queue.forEach((message, position) => {
@@ -174,6 +159,11 @@ export function liveQueuePosition(
174159
return { chatKey: key, index }
175160
}
176161

162+
/** The queue key a write captured before an `await` should use now (see `liveQueuePosition`). */
163+
export function liveQueueKey(chatKey: string): string {
164+
return liveQueuePosition(chatKey, []).chatKey
165+
}
166+
177167
export const useMothershipQueueStore = create<MothershipQueueState>()(
178168
devtools(
179169
persist(
@@ -259,7 +249,8 @@ export const useMothershipQueueStore = create<MothershipQueueState>()(
259249
if (fromKey === toKey) return state
260250
const migratedTo = {
261251
...state.migratedTo,
262-
[fromKey]: {
252+
/** The first move is the real one; a repeat must not rewrite what was ahead. */
253+
[fromKey]: state.migratedTo[fromKey] ?? {
263254
key: toKey,
264255
ahead: (state.queues[toKey] ?? []).map((message) => message.id),
265256
},

0 commit comments

Comments
 (0)