Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
perf(web-app): compare Cowork read-backs with one plain serialization
Akarshan Biswas6ed2da24dc31
diff --git a/web-app/src/lib/__tests__/migrateCoworkSessions.test.ts b/web-app/src/lib/__tests__/migrateCoworkSessions.test.tsindex d9920afbb..54e6c9fe8 100644--- a/web-app/src/lib/__tests__/migrateCoworkSessions.test.ts+++ b/web-app/src/lib/__tests__/migrateCoworkSessions.test.ts@@ -1,9 +1,10 @@-import { describe, it, expect, beforeEach } from 'vitest'+import { describe, it, expect, beforeEach, vi } from 'vitest'import {migrateCoworkSessionsToThreads,CODE_SESSIONS_KEY,COWORK_MIGRATION_FLAG_KEY,COWORK_MIGRATION_ABANDONED_KEY,+ sameJson,type Invoke,} from '../migrateCoworkSessions'import type { CoworkSession } from '@/hooks/useCoworkSessions'@@ -469,3 +470,52 @@ describe('migrateCoworkSessionsToThreads', () => {})})})++describe('sameJson', () => {+ it('compares without re-sorting keys on the happy path', () => {+ const entries = vi.spyOn(Object, 'entries')+ try {+ const big = { a: [1, 2, { b: 'x'.repeat(1000), c: null }], d: { e: 1 } }+ expect(sameJson(big, JSON.parse(JSON.stringify(big)))).toBe(true)+ expect(entries).not.toHaveBeenCalled()+ } finally {+ entries.mockRestore()+ }+ })++ it('still accepts a backend that reordered object keys', () => {+ expect(sameJson({ a: 1, b: { c: 2, d: 3 } }, { b: { d: 3, c: 2 }, a: 1 })).toBe(+ true+ )+ })++ it('rejects real differences', () => {+ expect(sameJson({ a: 1 }, { a: 2 })).toBe(false)+ expect(sameJson([1, 2], [2, 1])).toBe(false)+ expect(sameJson({ a: 1 }, { a: 1, b: 2 })).toBe(false)+ })+})++describe('read-back cost', () => {+ it('does not re-canonicalise payloads that already match byte for byte', async () => {+ const b = makeBackend()+ const big = session('big', {+ messages: [+ {+ id: 'm',+ role: 'user',+ parts: [{ type: 'text', text: 'x'.repeat(100_000) }],+ },+ ],+ } as unknown as Partial<CoworkSession>)+ b.settings.set(CODE_SESSIONS_KEY, envelope([big]))+ const entries = vi.spyOn(Object, 'entries')+ try {+ const r = await migrateCoworkSessionsToThreads(b.invoke)+ expect(r.status).toBe('complete')+ expect(entries).not.toHaveBeenCalled()+ } finally {+ entries.mockRestore()+ }+ })+})diff --git a/web-app/src/lib/migrateCoworkSessions.ts b/web-app/src/lib/migrateCoworkSessions.tsindex 4c2ea3189..d7e278954 100644--- a/web-app/src/lib/migrateCoworkSessions.ts+++ b/web-app/src/lib/migrateCoworkSessions.ts@@ -80,6 +80,17 @@ type StoredThread = {metadata?: { mode?: unknown } | null}+/**+ * Whether the backend returned what was sent. The backend keeps key order+ * (serde_json preserve_order), so one plain serialization of each side+ * normally settles it and big payloads (image data URLs) are walked once.+ * Only when those differ is the slower key-order-insensitive form tried.+ */+export function sameJson(a: unknown, b: unknown): boolean {+ if (JSON.stringify(a) === JSON.stringify(b)) return true+ return canonical(a) === canonical(b)+}+// Key-order-insensitive serialization: the backend may reorder object keys.function canonical(v: unknown): string {return JSON.stringify(v, (_k, val) =>@@ -270,7 +281,7 @@ export async function migrateCoworkSessionsToThreads(threadId: session.id,name: COWORK_SIDECAR,})- if (canonical(back) !== canonical(sidecar)) {+ if (!sameJson(back, sidecar)) {throw new Error('sidecar did not read back identically')}@@ -285,7 +296,7 @@ export async function migrateCoworkSessionsToThreads(const stored = await invoke<unknown>('list_messages', {threadId: session.id,})- if (canonical(stored) !== canonical(messages)) {+ if (!sameJson(stored, messages)) {throw new Error('messages did not read back identically')}