Skip to content

The agent that grows with you

perf(desktop): track a pending submission without scanning the journal

unsupportedpastels27595b5dd2f3
diff --git a/apps/desktop/src/store/pending-submissions.test.ts b/apps/desktop/src/store/pending-submissions.test.ts
index 071228d1f1f..c1ecc9b9b12 100644
--- a/apps/desktop/src/store/pending-submissions.test.ts
+++ b/apps/desktop/src/store/pending-submissions.test.ts
@@ -220,3 +220,40 @@ it('a tip-keyed tracked receipt keeps its status, text and Discard across root p
$sessions.set([])
}
})
+
+// Every send tracks its admission before the request and again after the reply, on the renderer
+// thread. That must cost the one admission's record, not a parse of every saved session's journal.
+it('tracking a submission reads its own record without enumerating other sessions', () => {
+ for (let index = 0; index < 20; index += 1) {
+ trackPendingSubmission(`other-${index}`, { id: `a-${index}`, text: 'elsewhere', status: 'queued' })
+ }
+
+ window.localStorage.setItem('hermes.desktop.pendingSubmissions.v1', JSON.stringify({ solo: { old: { id: 'old', text: 'legacy', displayText: '/old' } } }))
+ const enumerate = vi.spyOn(window.localStorage, 'key')
+
+ try {
+ trackPendingSubmission('solo', { id: 'mine', text: 'hi', displayText: '/hi' })
+ trackPendingSubmission('solo', { id: 'mine', text: 'hi', status: 'queued' })
+ // A legacy receipt still merges into its tracked successor.
+ trackPendingSubmission('solo', { id: 'old', text: '', status: 'unknown' })
+ expect(enumerate).not.toHaveBeenCalled()
+
+ // A newly known tip->root lineage adopts the tip's journal once; later sends read directly.
+ trackPendingSubmission('late-tip', { id: 'early', text: 'early', displayText: '/early', status: 'unknown' })
+ $sessions.set([{ id: 'late-tip', _lineage_root_id: 'late-root' } as SessionInfo])
+ trackPendingSubmission('late-tip', { id: 'next', text: 'next' })
+ expect(readPendingSubmissions('late-root').early).toMatchObject({ status: 'unknown', displayText: '/early' })
+ enumerate.mockClear()
+ trackPendingSubmission('late-tip', { id: 'next', text: 'next', status: 'queued' })
+ expect(enumerate).not.toHaveBeenCalled()
+ } finally {
+ enumerate.mockRestore()
+ $sessions.set([])
+ }
+
+ expect(readPendingSubmissions('solo')).toMatchObject({
+ mine: { text: 'hi', displayText: '/hi', status: 'queued' },
+ old: { text: 'legacy', displayText: '/old', status: 'unknown' }
+ })
+ expect(readPendingSubmissions('late-root').next).toMatchObject({ status: 'queued' })
+})
diff --git a/apps/desktop/src/store/pending-submissions.ts b/apps/desktop/src/store/pending-submissions.ts
index 85b6d41871b..9c2685f9db9 100644
--- a/apps/desktop/src/store/pending-submissions.ts
+++ b/apps/desktop/src/store/pending-submissions.ts
@@ -52,6 +52,12 @@ export function readPendingSubmissions(session: string): Record<string, PendingS
return entries
}
+/** One admission's record without enumerating the journal: its v2 key, else the legacy blob. */
+function readPendingSubmission(session: string, id: string): PendingSubmission | undefined {
+ return parse<null | PendingSubmission>(window.localStorage.getItem(entryKey(session, id)), null) ??
+ parse(window.localStorage.getItem(LEGACY_KEY), {} as Record<string, Record<string, PendingSubmission>>)[session]?.[id]
+}
+
function writeEntries(session: string, before: Record<string, PendingSubmission>, after: Record<string, PendingSubmission>): void {
for (const id of Object.keys(before)) {
if (!(id in after)) { window.localStorage.removeItem(entryKey(session, id)) }
@@ -133,11 +139,22 @@ function adoptJournal(from: string, to: string): void {
writeEntries(from, stray, {})
}
+// Tip->root lineages whose tip journal tracking already folded. Adoption enumerates the journal,
+// so a send pays for it only when it first observes the lineage; reconciliation still adopts
+// whatever another window journals under the tip afterwards.
+const adoptedLineages = new Set<string>()
+
// Not an outbox: an uncertain accepted input must never be replayed automatically.
export function trackPendingSubmission(storedId: string, entry: PendingSubmission): void {
const key = journalKey(storedId)
- adoptJournal(storedId, key)
- const held = readPendingSubmissions(key)[entry.id]
+ const lineage = JSON.stringify([storedId, key])
+
+ if (storedId !== key && !adoptedLineages.has(lineage)) {
+ adoptJournal(storedId, key)
+ adoptedLineages.add(lineage)
+ }
+
+ const held = readPendingSubmission(key, entry.id)
window.localStorage.setItem(entryKey(key, entry.id), JSON.stringify(mergeReceipt(held, entry)))
}