Skip to content

The agent that grows with you

fix(tui): inputs queued in the same millisecond on two sessions keep their order

Tekniumdb1fa153fe24
diff --git a/ui-tui/src/__tests__/pendingMigrationRenderer.test.tsx b/ui-tui/src/__tests__/pendingMigrationRenderer.test.tsx
index 13f5d545779..5a3ffd86b93 100644
--- a/ui-tui/src/__tests__/pendingMigrationRenderer.test.tsx
+++ b/ui-tui/src/__tests__/pendingMigrationRenderer.test.tsx
@@ -96,6 +96,46 @@ it('moves compression queues across cached successors without changing attempted
}
})
+it('keeps queue order when inputs queued in the same millisecond on two sessions merge', () => {
+ const home = mkdtempSync(join(tmpdir(), 'ink-order-'))
+ vi.stubEnv('HERMES_HOME', home)
+ vi.spyOn(Date, 'now').mockReturnValue(1_800_000_000_000) // a fast machine: every enqueue in one ms
+ resetUiState()
+ patchUiState({ sid: 'old', info: { model: 'test', profile_name: 'alpha', skills: {}, tools: {} } })
+ let queue!: ReturnType<typeof useQueue>
+
+ function Harness() {
+ queue = useQueue()
+
+ return <Text>{queue.queuedDisplay.join('|')}</Text>
+ }
+
+ const instance = renderSync(<Harness />, {
+ stdin: new PassThrough() as any,
+ stdout: Object.assign(new PassThrough(), { columns: 80, rows: 20, isTTY: false }) as any,
+ stderr: new PassThrough() as any,
+ patchConsole: false
+ })
+
+ try {
+ const original = captureDestination()
+ const first = queue.enqueue('first')
+ patchUiState({ sid: 'next' })
+ const second = queue.enqueue('second')
+ expect(second.createdAt).toBeGreaterThan(first.createdAt!)
+ patchUiState({ sid: 'old' })
+ pending.migratePendingInputs(original, 'next')
+ patchUiState({ sid: 'next' })
+ expect(queue.queueRef.current.map(item => item.text)).toEqual(['first', 'second'])
+ } finally {
+ instance.unmount()
+ vi.restoreAllMocks()
+ resetUiState()
+ vi.unstubAllEnvs()
+ rmSync(home, { recursive: true, force: true })
+ }
+})
+
it('uses the canonical profile home through symlinks, including a missing descendant', () => {
const root = mkdtempSync(join(tmpdir(), 'ink-home-'))
const real = join(root, 'real')
diff --git a/ui-tui/src/hooks/useQueue.ts b/ui-tui/src/hooks/useQueue.ts
index 7568aa1785b..e7253f0295f 100644
--- a/ui-tui/src/hooks/useQueue.ts
+++ b/ui-tui/src/hooks/useQueue.ts
@@ -91,6 +91,17 @@ interface PendingQueue {
items: QueueItem[]
}
+// Restored and migrated queues order by `createdAt`; one process-wide clock keeps it strictly
+// increasing across every session's queue, so two inputs queued in the same millisecond on
+// different sessions keep their order when a resume merges them.
+let lastCreatedAt = 0
+
+function nextQueueCreatedAt(floor = 0): number {
+ lastCreatedAt = Math.max(Date.now(), lastCreatedAt + 1, floor + 1)
+
+ return lastCreatedAt
+}
+
// `gw` owns server-queued rows: the authority's pending fanout is rendered
// after this client's unconfirmed local items, and deleting/editing one goes
// through `prompt.cancel` — the next fanout, not a local splice, retires it.
@@ -270,7 +281,7 @@ export function useQueue(gw?: { request: (method: string, params: Record<string,
...(attachments?.length ? { attachments } : {}),
submissionId: randomUUID(),
destination: owner,
- createdAt: Math.max(Date.now(), (queue.items.at(-1)?.createdAt ?? 0) + 1)
+ createdAt: nextQueueCreatedAt(queue.items.at(-1)?.createdAt)
}
return { queue, item }