chatCache.test.ts 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // @vitest-environment node
  2. import { mkdtempSync, rmSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { DatabaseSync } from 'node:sqlite'
  6. import { afterEach, describe, expect, it, vi } from 'vitest'
  7. import { ChatCacheCoordinator, ChatCacheStore, chatCacheKey } from '../../src/server/chatCache.js'
  8. import type { UpstreamServiceSession } from '../../src/server/localAuth.js'
  9. import type { UpstreamResponse } from '../../src/server/upstream.js'
  10. const homes: string[] = []
  11. const owner = 'owner-1'
  12. const profile = 'default'
  13. const sessionID = 'session-web'
  14. function response(value: unknown, status = 200, contentType = 'application/json'): UpstreamResponse {
  15. return { status, headers: new Headers({ 'content-type': contentType }), body: Buffer.from(typeof value === 'string' ? value : JSON.stringify(value)) }
  16. }
  17. function fixture() {
  18. const home = mkdtempSync(join(tmpdir(), 'yaoyao-chat-cache-'))
  19. homes.push(home)
  20. const store = new ChatCacheStore(home)
  21. const upstream = { request: vi.fn() } as unknown as UpstreamServiceSession
  22. return { home, store, coordinator: new ChatCacheCoordinator(store, upstream, 'prefer-local') }
  23. }
  24. afterEach(() => {
  25. for (const home of homes.splice(0)) rmSync(home, { recursive: true, force: true })
  26. })
  27. describe('durable source=web chat cache', () => {
  28. it('keeps session metadata when message envelopes only contain session_id and advances activity from messages', () => {
  29. const f = fixture()
  30. f.store.recordRoute(owner, profile, sessionID, 'runtime-web')
  31. f.store.putSnapshot(owner, 'detail', 'detail', profile, sessionID, response({
  32. id: sessionID, profile, source: 'ios', title: '准备 GPT-6 详细 Markdown 文档',
  33. started_at: 1_788_661_637, last_active: 1_788_661_640, pinned: true, message_count: 1,
  34. }))
  35. f.store.putSnapshot(owner, 'tail', 'messages', profile, sessionID, response({
  36. session_id: sessionID, model: 'test-model',
  37. messages: [{ id: 'latest', timestamp: 1_788_663_697.824613, role: 'assistant', content: '完成' }],
  38. pagination: { total: 2, offset: 0 },
  39. }))
  40. f.store.putSnapshot(owner, 'older', 'messages', profile, sessionID, response({
  41. session_id: sessionID,
  42. messages: [{ id: 'first', timestamp: 1_788_661_640_000, role: 'user', content: '开始' }],
  43. pagination: { total: 2, offset: 1 },
  44. }))
  45. const summary = JSON.parse(f.store.localDetail(owner, profile, sessionID)!.response.body.toString())
  46. expect(summary).toMatchObject({
  47. id: sessionID, profile, source: 'ios', title: '准备 GPT-6 详细 Markdown 文档',
  48. started_at: 1_788_661_637, last_active: 1_788_663_697.824613,
  49. pinned: true, model: 'test-model', message_count: 2,
  50. })
  51. expect(summary).not.toHaveProperty('messages')
  52. expect(summary).not.toHaveProperty('pagination')
  53. f.store.close()
  54. })
  55. it('repairs overwritten summaries on restart using message time instead of cache refresh time', () => {
  56. const f = fixture()
  57. f.store.recordRoute(owner, profile, sessionID, 'runtime-web')
  58. f.store.recordCommand(owner, profile, sessionID, 'session.create', { source: 'ios' })
  59. f.store.putSnapshot(owner, 'messages', 'messages', profile, sessionID, response({
  60. session_id: sessionID,
  61. messages: [
  62. { id: 'first', timestamp: '2026-09-06T03:00:00Z', role: 'user', content: '开始' },
  63. { id: 'last', timestamp: 1_788_663_697.824613, role: 'assistant', content: '完成' },
  64. ],
  65. pagination: { total: 2, offset: 0 },
  66. }))
  67. f.store.db.prepare(`UPDATE chat_sessions SET data=?,owned_at=?,last_synced_at=?
  68. WHERE owner=? AND profile=? AND session_id=?`).run(JSON.stringify({
  69. session_id: sessionID, title: '准备 GPT-6 详细 Markdown 文档', model: 'test-model',
  70. messages: [], pagination: { total: 2 },
  71. }), 1_788_661_637_003, 1_900_000_000_000, owner, profile, sessionID)
  72. f.store.db.prepare("DELETE FROM chat_meta WHERE key='session-activity-v1'").run()
  73. f.store.close()
  74. const restored = new ChatCacheStore(f.home)
  75. const summary = JSON.parse(restored.localList(owner, profile, {}).body.toString()).sessions[0]
  76. expect(summary).toMatchObject({
  77. id: sessionID, source: 'ios', title: '准备 GPT-6 详细 Markdown 文档',
  78. started_at: 1_788_663_600, last_active: 1_788_663_697.824613, message_count: 2,
  79. })
  80. expect(summary).not.toHaveProperty('messages')
  81. expect(summary).not.toHaveProperty('pagination')
  82. restored.close()
  83. const again = new ChatCacheStore(f.home)
  84. expect(JSON.parse(again.localList(owner, profile, {}).body.toString()).sessions[0]).toEqual(summary)
  85. again.close()
  86. })
  87. it('orders owned conversations by real time across seconds and milliseconds', () => {
  88. const f = fixture()
  89. for (const [id, activity] of [['older', 1_788_561_050_759], ['newer', 1_788_663_697.824613]] as const) {
  90. f.store.recordRoute(owner, profile, id, `runtime-${id}`)
  91. f.store.putSnapshot(owner, id, 'detail', profile, id, response({ id, last_active: activity }))
  92. }
  93. expect(JSON.parse(f.store.localList(owner, profile, {}).body.toString()).sessions.map((s: { id: string }) => s.id))
  94. .toEqual(['newer', 'older'])
  95. f.store.close()
  96. })
  97. it('hydrates all owned list metadata before paging and keeps upstream activity aliases authoritative', async () => {
  98. const f = fixture()
  99. const sessions = Array.from({ length: 103 }, (_, index) => ({
  100. id: `owned-${index}`, profile, source: 'web', title: `Conversation ${index}`,
  101. started_at: 1_700_000_000 - index, last_active_at: 1_700_000_100 - index,
  102. message_count: 0, pinned: index === 102,
  103. }))
  104. for (const session of sessions) {
  105. f.store.recordCommand(owner, profile, session.id, 'session.create', { source: 'web' })
  106. }
  107. const load = vi.fn(async () => response({ sessions: [
  108. { id: 'unowned-history', profile, source: 'web', title: 'Private native history' }, ...sessions,
  109. ] }))
  110. const first = await f.coordinator.read(owner, 'first', 'list', profile, undefined, load, true, { limit: 100 })
  111. const firstPage = JSON.parse(first.response.body.toString())
  112. expect(firstPage.sessions[0]).toMatchObject({ id: 'owned-102', last_active: sessions[102]!.last_active_at })
  113. expect(firstPage.sessions[1]).toMatchObject({ id: 'owned-0', last_active: sessions[0]!.last_active_at })
  114. expect(f.store.needsListMetadata(owner, profile)).toBe(false)
  115. const second = await f.coordinator.read(owner, 'second', 'list', profile, undefined, load, true, { offset: 100, limit: 100 })
  116. expect(second.source).toBe('local')
  117. expect(load).toHaveBeenCalledTimes(1)
  118. const secondPage = JSON.parse(second.response.body.toString())
  119. const ids = [...firstPage.sessions, ...secondPage.sessions].map((session: { id: string }) => session.id)
  120. expect(ids).toEqual(['owned-102', ...sessions.slice(0, 102).map(session => session.id)])
  121. expect(f.store.snapshot(owner, 'first')!.response.body.toString()).not.toContain('unowned-history')
  122. expect(f.store.localDetail(owner, profile, 'unowned-history')).toBeUndefined()
  123. f.store.close()
  124. })
  125. it('serves a warm list without a second 9119 request and survives restart', async () => {
  126. const f = fixture()
  127. f.store.recordRoute(owner, profile, sessionID, 'runtime-web')
  128. f.store.recordCommand(owner, profile, sessionID, 'session.create', { source: 'web' })
  129. const search = new URLSearchParams({ source: 'web', profile })
  130. const key = chatCacheKey('list', profile, undefined, search)
  131. const load = vi.fn(async () => response({ sessions: [{ id: sessionID, profile, source: 'web', title: '本地会话' }] }))
  132. f.store.putSnapshot(owner, 'seed-detail', 'detail', profile, sessionID, response({ id: sessionID, profile, title: '本地会话', message_count: 0 }))
  133. const first = await f.coordinator.read(owner, key, 'list', profile, undefined, load, true)
  134. const second = await f.coordinator.read(owner, key, 'list', profile, undefined, load, true)
  135. expect(first.source).toBe('local')
  136. expect(second.source).toBe('local')
  137. expect(load).not.toHaveBeenCalled()
  138. f.store.close()
  139. const restored = new ChatCacheStore(f.home)
  140. const cached = restored.localList(owner, profile, {})
  141. expect(JSON.parse(cached.body.toString()).sessions[0].title).toBe('本地会话')
  142. restored.close()
  143. })
  144. it('reprojects a pre-ownership list snapshot after restart before serving it', async () => {
  145. const f = fixture()
  146. const search = new URLSearchParams({ profile, offset: '0', limit: '100' })
  147. const key = chatCacheKey('list', profile, undefined, search)
  148. f.store.recordRoute(owner, profile, sessionID, 'runtime-web')
  149. f.store.recordCommand(owner, profile, sessionID, 'session.create', { source: 'ios' })
  150. f.store.putSnapshot(owner, key, 'list', profile, undefined, response({
  151. sessions: [{ id: sessionID, profile, source: 'ios', title: '旧快照标题' }],
  152. total: 1,
  153. }), true)
  154. f.store.db.prepare(`UPDATE chat_sessions SET authoritative_title=NULL,authoritative_title_at=0
  155. WHERE owner=? AND profile=? AND session_id=?`).run(owner, profile, sessionID)
  156. expect(JSON.parse(f.store.snapshot(owner, key)!.response.body.toString()).sessions[0].owned)
  157. .toBeUndefined()
  158. f.store.close()
  159. const restored = new ChatCacheStore(f.home)
  160. expect(restored.snapshot(owner, key)?.state).toBe('stale')
  161. expect((restored.db.prepare(`SELECT authoritative_title FROM chat_sessions
  162. WHERE owner=? AND profile=? AND session_id=?`).get(owner, profile, sessionID) as { authoritative_title: string }).authoritative_title)
  163. .toBe('旧快照标题')
  164. const coordinator = new ChatCacheCoordinator(
  165. restored,
  166. { request: vi.fn() } as unknown as UpstreamServiceSession,
  167. 'prefer-local',
  168. )
  169. const projected = await coordinator.read(
  170. owner,
  171. key,
  172. 'list',
  173. profile,
  174. undefined,
  175. async () => { throw new Error('9119 offline') },
  176. true,
  177. { offset: 0, limit: 100 },
  178. )
  179. expect(projected).toMatchObject({ source: 'local', state: 'current' })
  180. expect(JSON.parse(projected.response.body.toString()).sessions[0]).toMatchObject({
  181. id: sessionID,
  182. source: 'ios',
  183. owned: true,
  184. })
  185. restored.close()
  186. })
  187. it('adopts the released v0.3.26 registry once without using source for later membership', () => {
  188. const home = mkdtempSync(join(tmpdir(), 'yaoyao-v026-registry-'))
  189. homes.push(home)
  190. const legacy = new DatabaseSync(join(home, 'chat-cache.sqlite3'))
  191. legacy.exec(`CREATE TABLE chat_sessions(
  192. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  193. runtime_id TEXT, source TEXT NOT NULL, data TEXT NOT NULL,
  194. sync_state TEXT NOT NULL DEFAULT 'stale', complete INTEGER NOT NULL DEFAULT 0,
  195. last_event_seq INTEGER NOT NULL DEFAULT 0, last_synced_at INTEGER NOT NULL,
  196. PRIMARY KEY(owner,profile,session_id))`)
  197. legacy.prepare(`INSERT INTO chat_sessions(
  198. owner,profile,session_id,runtime_id,source,data,sync_state,complete,last_event_seq,last_synced_at
  199. ) VALUES(?,?,?,?,?,?,?,?,?,?)`).run(
  200. owner,
  201. profile,
  202. 'v026-session',
  203. null,
  204. 'web',
  205. JSON.stringify({ id: 'v026-session', profile, source: 'web', title: '已发布旧会话' }),
  206. 'current',
  207. 0,
  208. 0,
  209. 1_000,
  210. )
  211. legacy.close()
  212. const migrated = new ChatCacheStore(home, owner)
  213. expect(migrated.ownsSession(owner, profile, 'v026-session')).toBe(true)
  214. expect(migrated.db.prepare(`SELECT ownership_evidence FROM chat_sessions
  215. WHERE owner=? AND profile=? AND session_id=?`).get(owner, profile, 'v026-session'))
  216. .toMatchObject({ ownership_evidence: 'legacy-single-user' })
  217. expect(migrated.putSnapshot(owner, 'new-source-row', 'detail', profile, 'new-source-row',
  218. response({ id: 'new-source-row', profile, source: 'web', title: '未登记新历史' }))).toBe(false)
  219. expect(migrated.ownsSession(owner, profile, 'new-source-row')).toBe(false)
  220. migrated.close()
  221. })
  222. it('does not adopt ambiguous multi-owner legacy cache rows without command evidence', () => {
  223. const home = mkdtempSync(join(tmpdir(), 'yaoyao-v026-multi-owner-'))
  224. homes.push(home)
  225. const legacy = new DatabaseSync(join(home, 'chat-cache.sqlite3'))
  226. legacy.exec(`CREATE TABLE chat_sessions(
  227. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  228. runtime_id TEXT, source TEXT NOT NULL, data TEXT NOT NULL,
  229. sync_state TEXT NOT NULL DEFAULT 'stale', complete INTEGER NOT NULL DEFAULT 0,
  230. last_event_seq INTEGER NOT NULL DEFAULT 0, last_synced_at INTEGER NOT NULL,
  231. PRIMARY KEY(owner,profile,session_id));
  232. CREATE TABLE chat_events(
  233. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  234. event_seq INTEGER NOT NULL, event_type TEXT NOT NULL, payload TEXT NOT NULL,
  235. received_at INTEGER NOT NULL, PRIMARY KEY(owner,profile,session_id,event_seq,event_type));`)
  236. const insertSession = legacy.prepare(`INSERT INTO chat_sessions(
  237. owner,profile,session_id,runtime_id,source,data,sync_state,complete,last_event_seq,last_synced_at
  238. ) VALUES(?,?,?,?,?,?,?,?,?,?)`)
  239. for (const [legacyOwner, id] of [
  240. ['owner-a', 'evidenced'],
  241. ['owner-a', 'source-only-a'],
  242. ['owner-b', 'source-only-b'],
  243. ] as const) {
  244. insertSession.run(
  245. legacyOwner,
  246. profile,
  247. id,
  248. null,
  249. 'web',
  250. JSON.stringify({ id, profile, source: 'web', title: id }),
  251. 'current',
  252. 0,
  253. 0,
  254. 1_000,
  255. )
  256. }
  257. legacy.prepare(`INSERT INTO chat_events(
  258. owner,profile,session_id,event_seq,event_type,payload,received_at
  259. ) VALUES(?,?,?,?,?,?,?)`).run(
  260. 'owner-a', profile, 'evidenced', 1, 'command:session.create', '{}', 900,
  261. )
  262. legacy.close()
  263. const migrated = new ChatCacheStore(home, 'owner-a')
  264. expect(migrated.ownsSession('owner-a', profile, 'evidenced')).toBe(true)
  265. expect(migrated.ownsSession('owner-a', profile, 'source-only-a')).toBe(false)
  266. expect(migrated.ownsSession('owner-b', profile, 'source-only-b')).toBe(false)
  267. migrated.close()
  268. })
  269. it('does not adopt one cached owner when the sole local user has another id', () => {
  270. const home = mkdtempSync(join(tmpdir(), 'yaoyao-v026-owner-mismatch-'))
  271. homes.push(home)
  272. const legacy = new DatabaseSync(join(home, 'chat-cache.sqlite3'))
  273. legacy.exec(`CREATE TABLE chat_sessions(
  274. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  275. runtime_id TEXT, source TEXT NOT NULL, data TEXT NOT NULL,
  276. sync_state TEXT NOT NULL DEFAULT 'stale', complete INTEGER NOT NULL DEFAULT 0,
  277. last_event_seq INTEGER NOT NULL DEFAULT 0, last_synced_at INTEGER NOT NULL,
  278. PRIMARY KEY(owner,profile,session_id))`)
  279. legacy.prepare(`INSERT INTO chat_sessions(
  280. owner,profile,session_id,runtime_id,source,data,sync_state,complete,last_event_seq,last_synced_at
  281. ) VALUES(?,?,?,?,?,?,?,?,?,?)`).run(
  282. 'cached-old-user', profile, 'source-only', null, 'web',
  283. JSON.stringify({ id: 'source-only', profile, source: 'web', title: '旧缓存' }),
  284. 'current', 0, 0, 1_000,
  285. )
  286. legacy.close()
  287. const migrated = new ChatCacheStore(home, 'current-sole-user')
  288. expect(migrated.ownsSession('cached-old-user', profile, 'source-only')).toBe(false)
  289. migrated.close()
  290. })
  291. it('re-enters ownership migration when columns exist but the completion marker is missing', () => {
  292. const home = mkdtempSync(join(tmpdir(), 'yaoyao-v026-interrupted-migration-'))
  293. homes.push(home)
  294. const interrupted = new DatabaseSync(join(home, 'chat-cache.sqlite3'))
  295. interrupted.exec(`CREATE TABLE chat_sessions(
  296. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  297. runtime_id TEXT, source TEXT NOT NULL, data TEXT NOT NULL,
  298. authoritative_title TEXT, authoritative_title_at INTEGER NOT NULL DEFAULT 0,
  299. owned_at INTEGER, ownership_evidence TEXT,
  300. sync_state TEXT NOT NULL DEFAULT 'stale', complete INTEGER NOT NULL DEFAULT 0,
  301. last_event_seq INTEGER NOT NULL DEFAULT 0, last_synced_at INTEGER NOT NULL,
  302. PRIMARY KEY(owner,profile,session_id))`)
  303. interrupted.prepare(`INSERT INTO chat_sessions(
  304. owner,profile,session_id,runtime_id,source,data,sync_state,complete,last_event_seq,last_synced_at
  305. ) VALUES(?,?,?,?,?,?,?,?,?,?)`).run(
  306. owner, profile, 'interrupted-row', null, 'web',
  307. JSON.stringify({ id: 'interrupted-row', profile, source: 'web', title: '可恢复旧会话' }),
  308. 'current', 0, 0, 1_000,
  309. )
  310. interrupted.close()
  311. const recovered = new ChatCacheStore(home, owner)
  312. expect(recovered.ownsSession(owner, profile, 'interrupted-row')).toBe(true)
  313. expect(recovered.db.prepare("SELECT value FROM chat_meta WHERE key='ownership-registry-v1'").get())
  314. .toMatchObject({ value: 'complete' })
  315. recovered.close()
  316. })
  317. it('materializes complete message pages and keeps owners isolated', () => {
  318. const f = fixture()
  319. f.store.recordRoute(owner, profile, sessionID, 'runtime-web')
  320. f.store.recordCommand(owner, profile, sessionID, 'session.create', { source: 'web' })
  321. f.store.putSnapshot(owner, 'detail', 'detail', profile, sessionID,
  322. response({ id: sessionID, profile, source: 'web', title: '对话' }))
  323. f.store.putSnapshot(owner, 'messages', 'messages', profile, sessionID, response({
  324. session: { id: sessionID, profile, source: 'web', title: '对话' },
  325. messages: [
  326. { id: 'm1', role: 'user', content: '开始' },
  327. { id: 'm2', role: 'assistant', content: '完成' },
  328. ],
  329. pagination: { total: 2, returned: 2, offset: 0, limit: 100, hasMore: false },
  330. }))
  331. const page = f.store.messagePage(owner, profile, sessionID, 0, 100)
  332. const payload = JSON.parse(page!.response.body.toString())
  333. expect(payload.session_id).toBe(sessionID)
  334. expect(payload.pagination).toMatchObject({ total: 2, returned: 2, offset: 0, limit: 100, has_more: false })
  335. expect(payload.messages.map((item: { id: string }) => item.id)).toEqual(['m1', 'm2'])
  336. expect(f.store.messagePage('another-owner', profile, sessionID, 0, 100)).toBeUndefined()
  337. f.store.close()
  338. })
  339. it('never persists unregistered history regardless of source metadata', () => {
  340. const f = fixture()
  341. expect(f.store.putSnapshot(owner, 'history', 'detail', profile, 'history-1',
  342. response({ id: 'history-1', profile, source: 'telegram', title: '历史' }))).toBe(false)
  343. expect(f.store.putSnapshot(owner, 'unowned-web', 'detail', profile, 'unowned-web',
  344. response({ id: 'unowned-web', profile, source: 'web', title: '未登记 Web 历史' }))).toBe(false)
  345. expect(f.store.snapshot(owner, 'history')).toBeUndefined()
  346. expect(f.store.snapshot(owner, 'unowned-web')).toBeUndefined()
  347. f.store.close()
  348. })
  349. it('marks event gaps stale, deduplicates events, and falls back while 9119 is offline', async () => {
  350. const f = fixture()
  351. const key = 'detail-key'
  352. f.store.recordRoute(owner, profile, sessionID, 'runtime-1')
  353. f.store.recordCommand(owner, profile, sessionID, 'session.create', { source: 'web' })
  354. f.store.putSnapshot(owner, key, 'detail', profile, sessionID,
  355. response({ id: sessionID, profile, source: 'web', title: '稳定内容' }))
  356. const frame = { type: 'message.delta', seq: 2, payload: { text: '增量' } }
  357. f.store.recordEvent(owner, profile, sessionID, frame)
  358. f.store.recordEvent(owner, profile, sessionID, frame)
  359. const count = f.store.db.prepare("SELECT COUNT(*) count FROM chat_events WHERE event_type='message.delta'").get() as { count: number }
  360. expect(count.count).toBe(1)
  361. expect(f.store.ownsSession(owner, profile, sessionID)).toBe(true)
  362. expect(f.store.ownsSession('another-owner', profile, sessionID)).toBe(false)
  363. const route = f.store.db.prepare('SELECT runtime_id FROM chat_sessions WHERE owner=? AND profile=? AND session_id=?')
  364. .get(owner, profile, sessionID) as { runtime_id: string }
  365. expect(route.runtime_id).toBe('runtime-1')
  366. const fallback = await f.coordinator.read(owner, key, 'detail', profile, sessionID,
  367. async () => { throw new Error('9119 offline') })
  368. expect(fallback).toMatchObject({ source: 'local', state: 'stale' })
  369. const serverFailure = await f.coordinator.read(owner, key, 'detail', profile, sessionID,
  370. async () => response({ error: 'unavailable' }, 503))
  371. expect(serverFailure).toMatchObject({ source: 'local', state: 'stale' })
  372. f.store.close()
  373. })
  374. it.each(['session.title', 'session.title.updated'])(
  375. 'projects %s into the owned summary without changing source metadata',
  376. (eventType) => {
  377. const f = fixture()
  378. const ownedSessionID = `owned-${eventType}`
  379. const listKey = chatCacheKey('list', profile, undefined, new URLSearchParams({ source: 'web', profile }))
  380. f.store.recordRoute(owner, profile, ownedSessionID, 'runtime-owned')
  381. f.store.recordCommand(owner, profile, ownedSessionID, 'session.create', { source: 'ios' })
  382. f.store.putSnapshot(owner, listKey, 'list', profile, undefined, response({ sessions: [] }), true)
  383. f.store.recordEvent(owner, profile, ownedSessionID, {
  384. type: eventType,
  385. payload: { session_id: ownedSessionID, title: '服务端生成的标题' },
  386. })
  387. expect(f.store.snapshot(owner, listKey)?.state).toBe('stale')
  388. const merged = f.store.mergeOwnedSessionsIntoList(owner, profile, response({ sessions: [], total: 0 }))
  389. expect(JSON.parse(merged.body.toString()).sessions).toContainEqual(expect.objectContaining({
  390. id: ownedSessionID,
  391. profile,
  392. source: 'ios',
  393. owned: true,
  394. title: '服务端生成的标题',
  395. }))
  396. const staleList = f.store.mergeOwnedSessionsIntoList(owner, profile, response({ sessions: [{
  397. id: ownedSessionID, profile, source: 'ios', title: '新对话',
  398. }] }))
  399. expect(JSON.parse(staleList.body.toString()).sessions[0].title).toBe('服务端生成的标题')
  400. const titleVersion = f.store.db.prepare(`SELECT authoritative_title_at FROM chat_sessions
  401. WHERE owner=? AND profile=? AND session_id=?`).get(owner, profile, ownedSessionID) as { authoritative_title_at: number }
  402. expect(f.store.putSnapshot(owner, 'delayed-detail', 'detail', profile, ownedSessionID,
  403. response({ id: ownedSessionID, profile, source: 'ios', title: '迟到的旧标题' }), false,
  404. titleVersion.authoritative_title_at - 1)).toBe(true)
  405. const afterDelayedDetail = f.store.markSessionOwnership(owner, profile, ownedSessionID,
  406. response({ id: ownedSessionID, profile, source: 'ios', title: '迟到的旧标题' }))
  407. expect(JSON.parse(afterDelayedDetail.body.toString()).title).toBe('服务端生成的标题')
  408. expect(f.store.putSnapshot(owner, 'newer-detail', 'detail', profile, ownedSessionID,
  409. response({ id: ownedSessionID, profile, source: 'ios', title: '后续详情标题' }), false,
  410. titleVersion.authoritative_title_at + 1)).toBe(true)
  411. expect(f.store.putSnapshot(owner, 'regressed-detail', 'detail', profile, ownedSessionID,
  412. response({ id: ownedSessionID, profile, source: 'ios', title: '新对话' }))).toBe(true)
  413. const afterDetail = f.store.markSessionOwnership(owner, profile, ownedSessionID,
  414. response({ id: ownedSessionID, profile, source: 'ios', title: '新对话' }))
  415. expect(JSON.parse(afterDetail.body.toString()).title).toBe('后续详情标题')
  416. f.store.close()
  417. },
  418. )
  419. it('accepts detail and messages for a registered session regardless of source metadata', () => {
  420. const f = fixture()
  421. const ownedSessionID = 'owned-ios-session'
  422. f.store.recordRoute(owner, profile, ownedSessionID, 'runtime-owned')
  423. f.store.recordCommand(owner, profile, ownedSessionID, 'session.create', { source: 'ios' })
  424. expect(f.store.putSnapshot(owner, 'owned-detail', 'detail', profile, ownedSessionID,
  425. response({ id: ownedSessionID, profile, source: 'ios', title: 'iOS 权威标题' }))).toBe(true)
  426. expect(f.store.putSnapshot(owner, 'owned-messages', 'messages', profile, ownedSessionID, response({
  427. session: { id: ownedSessionID, profile, source: 'ios', title: 'iOS 权威标题' },
  428. messages: [{ id: 'm-ios', role: 'assistant', content: '已同步' }],
  429. pagination: { total: 1, returned: 1, offset: 0, limit: 100, hasMore: false },
  430. }))).toBe(true)
  431. const page = f.store.messagePage(owner, profile, ownedSessionID, 0, 100)
  432. expect(JSON.parse(page!.response.body.toString())).toMatchObject({
  433. session_id: ownedSessionID,
  434. session: { source: 'ios', title: 'iOS 权威标题' },
  435. messages: [{ id: 'm-ios' }],
  436. })
  437. f.store.close()
  438. })
  439. it('caches only attachment paths discovered in source=web messages', () => {
  440. const f = fixture()
  441. f.store.recordRoute(owner, profile, sessionID, 'runtime-web')
  442. f.store.recordCommand(owner, profile, sessionID, 'session.create', { source: 'web' })
  443. const sourcePath = '/Users/test/.hermes/profiles/default/images/result.png'
  444. f.store.putSnapshot(owner, 'messages', 'messages', profile, sessionID, response({
  445. session: { id: sessionID, profile, source: 'web' },
  446. messages: [{ id: 'm1', role: 'assistant', content: `![结果](${sourcePath})` }],
  447. pagination: { total: 1, offset: 0, hasMore: false },
  448. }))
  449. expect(f.store.knowsAttachment(owner, sourcePath)).toBe(true)
  450. const bytes = response('image-data', 200, 'image/png')
  451. expect(f.store.storeAttachment(owner, sourcePath, bytes)).toBeTruthy()
  452. expect(f.store.attachment(owner, sourcePath)?.mimeType).toBe('image/png')
  453. expect(f.store.knowsAttachment(owner, '/Users/test/history.png')).toBe(false)
  454. f.store.close()
  455. })
  456. it('supports shadow and upstream-only rollback modes', async () => {
  457. const home = mkdtempSync(join(tmpdir(), 'yaoyao-chat-cache-modes-')); homes.push(home)
  458. const store = new ChatCacheStore(home)
  459. const upstream = { request: vi.fn() } as unknown as UpstreamServiceSession
  460. store.recordRoute(owner, profile, sessionID, 'runtime-web')
  461. store.recordCommand(owner, profile, sessionID, 'session.create', { source: 'web' })
  462. const load = vi.fn(async () => response({ sessions: [{ id: sessionID, profile, source: 'web' }] }))
  463. const search = new URLSearchParams({ source: 'web' })
  464. const key = chatCacheKey('list', profile, undefined, search)
  465. const shadow = new ChatCacheCoordinator(store, upstream, 'shadow')
  466. await shadow.read(owner, key, 'list', profile, undefined, load, true)
  467. await shadow.read(owner, key, 'list', profile, undefined, load, true)
  468. expect(load).toHaveBeenCalledTimes(2)
  469. const upstreamOnly = new ChatCacheCoordinator(store, upstream, 'upstream-only')
  470. const upstreamResult = await upstreamOnly.read(owner, key, 'list', profile, undefined, load, true)
  471. expect(load).toHaveBeenCalledTimes(3)
  472. expect(JSON.parse(upstreamResult.response.body.toString()).sessions).toEqual([
  473. expect.objectContaining({ id: sessionID, owned: true }),
  474. ])
  475. upstreamOnly.route(owner, profile, 'upstream-only-created', 'runtime-upstream-only')
  476. expect(store.ownsSession(owner, profile, 'upstream-only-created')).toBe(true)
  477. store.close()
  478. })
  479. it('paginates the complete owner registry before slicing regardless of mixed upstream history', () => {
  480. const f = fixture()
  481. for (let index = 0; index < 15; index++) {
  482. const id = `owned-${index}`
  483. const source = index % 2 ? 'ios' : 'web'
  484. f.store.recordRoute(owner, profile, id, `runtime-${index}`)
  485. f.store.recordCommand(owner, profile, id, 'session.create', { source })
  486. expect(f.store.putSnapshot(owner, `detail-${index}`, 'detail', profile, id, response({
  487. id,
  488. profile,
  489. source,
  490. title: `Owned ${index}`,
  491. last_active: 1_000 + index,
  492. }))).toBe(true)
  493. }
  494. const upstreamSessions = [
  495. ...Array.from({ length: 40 }, (_, index) => ({
  496. id: `unowned-${index}`,
  497. profile,
  498. source: index % 2 ? 'ios' : 'web',
  499. title: `Unowned ${index}`,
  500. last_active: 10_000 + index,
  501. })),
  502. ...Array.from({ length: 15 }, (_, index) => ({
  503. id: `owned-${index}`,
  504. profile,
  505. source: index % 2 ? 'ios' : 'web',
  506. title: '新对话',
  507. last_active: 1_000 + index,
  508. })),
  509. ]
  510. const merged = f.store.mergeOwnedSessionsIntoList(
  511. owner,
  512. profile,
  513. response({ sessions: upstreamSessions, total: upstreamSessions.length }),
  514. { offset: 5, limit: 4, archived: 'exclude' },
  515. )
  516. const payload = JSON.parse(merged.body.toString())
  517. expect(payload).toMatchObject({ total: 15, offset: 5, limit: 4 })
  518. expect(payload.sessions.map((session: { id: string }) => session.id))
  519. .toEqual(['owned-9', 'owned-8', 'owned-7', 'owned-6'])
  520. expect(payload.sessions.every((session: { owned?: boolean }) => session.owned === true)).toBe(true)
  521. const history = f.store.excludeOwnedSessionsFromList(
  522. owner,
  523. profile,
  524. response({ sessions: upstreamSessions, total: upstreamSessions.length }),
  525. { offset: 7, limit: 5 },
  526. )
  527. const historyPayload = JSON.parse(history.body.toString())
  528. expect(historyPayload).toMatchObject({ total: 40, offset: 7, limit: 5 })
  529. expect(historyPayload.sessions.map((session: { id: string }) => session.id))
  530. .toEqual(['unowned-7', 'unowned-8', 'unowned-9', 'unowned-10', 'unowned-11'])
  531. expect(historyPayload.sessions.some((session: { source: string }) => session.source === 'web')).toBe(true)
  532. f.store.close()
  533. })
  534. it('keeps an old pinned owned session on the first page after registry projection', () => {
  535. const f = fixture()
  536. for (let index = 0; index < 101; index++) {
  537. const id = `owned-${index}`
  538. f.store.recordRoute(owner, profile, id, `runtime-${index}`)
  539. f.store.recordCommand(owner, profile, id, 'session.create', { source: 'ios' })
  540. expect(f.store.putSnapshot(owner, `detail-${index}`, 'detail', profile, id, response({
  541. id,
  542. profile,
  543. source: 'ios',
  544. title: `Owned ${index}`,
  545. last_active: 1_000 + index,
  546. pinned: index === 0,
  547. }))).toBe(true)
  548. }
  549. const merged = f.store.mergeOwnedSessionsIntoList(
  550. owner,
  551. profile,
  552. response({ sessions: [], total: 0 }),
  553. { offset: 0, limit: 100, archived: 'exclude' },
  554. )
  555. const payload = JSON.parse(merged.body.toString())
  556. expect(payload).toMatchObject({ total: 101, offset: 0, limit: 100 })
  557. expect(payload.sessions).toHaveLength(100)
  558. expect(payload.sessions[0]).toMatchObject({ id: 'owned-0', pinned: true, owned: true })
  559. expect(payload.sessions.map((session: { id: string }) => session.id)).not.toContain('owned-1')
  560. f.store.close()
  561. })
  562. })
  563. describe('local history and bounded tail synchronization', () => {
  564. function cached(count = 1_000) {
  565. const f = fixture()
  566. f.store.recordRoute(owner, profile, sessionID, 'runtime')
  567. const rows = Array.from({ length: count }, (_, i) => ({ id: `row-${i}`, role: i % 2 ? 'assistant' : 'user', content: `message ${i}` }))
  568. const summary = { id: sessionID, profile, title: 'cached', message_count: count }
  569. f.store.putSnapshot(owner, 'seed', 'messages', profile, sessionID, response({ session: summary, messages: rows,
  570. pagination: { total: count, offset: 0, has_more: false } }))
  571. return { ...f, rows, summary }
  572. }
  573. function upstreamFor(f: ReturnType<typeof cached>, rows: typeof f.rows) {
  574. const request = vi.fn(async (path: string, options: { search: URLSearchParams }) => {
  575. if (!path.endsWith('/messages')) return response({ ...f.summary, message_count: rows.length })
  576. const offset = Number(options.search.get('offset'))
  577. const limit = Number(options.search.get('limit'))
  578. const end = Math.max(0, rows.length - offset), start = Math.max(0, end - limit)
  579. return response({ messages: rows.slice(start, end), pagination: { total: rows.length, offset, has_more: start > 0 } })
  580. })
  581. const coordinator = new ChatCacheCoordinator(f.store, { request } as unknown as UpstreamServiceSession)
  582. return { coordinator, request }
  583. }
  584. it('never rereads stored pages while a new turn invalidates the tail', async () => {
  585. const f = cached()
  586. f.store.recordCommand(owner, profile, sessionID, 'prompt.submit', { text: 'next' })
  587. f.store.recordEvent(owner, profile, sessionID, { type: 'message.delta', seq: 1, payload: { delta: 'new' } })
  588. const load = vi.fn(async () => { throw new Error('must remain local') })
  589. for (let i = 0; i < 20; i++) {
  590. const page = await f.coordinator.messages(owner, `page-${i}`, profile, sessionID, (i % 10) * 100, 100, load)
  591. expect(page.source).toBe('local')
  592. expect(JSON.parse(page.response.body.toString()).messages).toHaveLength(100)
  593. }
  594. expect(load).not.toHaveBeenCalled()
  595. f.store.close()
  596. })
  597. it('persists new rows using one tail page and does not rewrite unchanged history', async () => {
  598. const f = cached()
  599. f.store.db.exec('CREATE TABLE changed_rows(id TEXT); CREATE TRIGGER changed_message AFTER UPDATE ON chat_messages BEGIN INSERT INTO changed_rows VALUES(new.message_id); END')
  600. const rows = [...f.rows, { id: 'new-user', role: 'user', content: 'next' }, { id: 'new-answer', role: 'assistant', content: 'answer' }]
  601. const { coordinator, request } = upstreamFor(f, rows)
  602. coordinator.observe(owner, profile, sessionID, { type: 'message.complete', seq: 1 })
  603. const page = await coordinator.messages(owner, 'last', profile, sessionID, 0, 10, async () => { throw new Error('unexpected read') })
  604. expect(JSON.parse(page.response.body.toString()).messages.at(-1).id).toBe('new-answer')
  605. expect(request.mock.calls.filter(([path]) => path.endsWith('/messages'))).toHaveLength(1)
  606. expect(f.store.db.prepare('SELECT * FROM changed_rows').all()).toEqual([])
  607. expect(f.store.messagePage(owner, profile, sessionID, 902, 100)).toBeDefined()
  608. coordinator.observe(owner, profile, sessionID, { type: 'message.complete', seq: 1 })
  609. expect(request).toHaveBeenCalledTimes(2)
  610. f.store.close()
  611. })
  612. it('fetches only the missing suffix after a replay gap', async () => {
  613. const f = cached()
  614. const rows = [...f.rows, ...Array.from({ length: 130 }, (_, i) => ({ id: `new-${i}`, role: 'assistant', content: 'reply' }))]
  615. const { coordinator, request } = upstreamFor(f, rows)
  616. await coordinator.reconcile(owner, profile, sessionID)
  617. expect(request.mock.calls.filter(([path]) => path.endsWith('/messages'))).toHaveLength(2)
  618. expect(JSON.parse(f.store.messagePage(owner, profile, sessionID, 0, 10)!.response.body.toString()).pagination.total).toBe(1130)
  619. f.store.close()
  620. })
  621. it('refreshes old edits and deletions only when explicitly forced', async () => {
  622. const f = cached()
  623. const rows = f.rows.slice(1).map(row => row.id === 'row-2' ? { ...row, content: 'edited old message' } : row)
  624. const { coordinator, request } = upstreamFor(f, rows)
  625. await expect(coordinator.reconcile(owner, profile, sessionID)).rejects.toThrow('explicit refresh')
  626. expect(JSON.parse(f.store.messagePage(owner, profile, sessionID, 900, 100)!.response.body.toString()).messages[0].id).toBe('row-0')
  627. request.mockClear()
  628. await coordinator.reconcile(owner, profile, sessionID, true)
  629. expect(request.mock.calls.filter(([path]) => path.endsWith('/messages'))).toHaveLength(10)
  630. const page = JSON.parse(f.store.messagePage(owner, profile, sessionID, 899, 100)!.response.body.toString())
  631. expect(page.messages[0].id).toBe('row-1')
  632. expect(page.messages[1].content).toBe('edited old message')
  633. expect(page.pagination.total).toBe(999)
  634. f.store.close()
  635. })
  636. it('does not require a full archive before serving a stored page', async () => {
  637. const f = fixture()
  638. f.store.recordRoute(owner, profile, sessionID, 'runtime')
  639. f.store.putSnapshot(owner, 'tail', 'messages', profile, sessionID, response({ messages: [{ id: 'last', role: 'assistant', content: 'tail' }], pagination: { total: 400, offset: 0, has_more: true } }))
  640. expect(f.store.messagePage(owner, profile, sessionID, 0, 1)).toBeDefined()
  641. expect(f.store.messagePage(owner, profile, sessionID, 1, 100)).toBeUndefined()
  642. f.store.close()
  643. })
  644. it('keeps the previous archive when a forced refresh fails halfway', async () => {
  645. const f = cached()
  646. let calls = 0
  647. const coordinator = new ChatCacheCoordinator(f.store, { request: async (path: string) => {
  648. if (!path.endsWith('/messages')) return response(f.summary)
  649. if (++calls > 1) throw new Error('offline')
  650. return response({ messages: f.rows.slice(-100), pagination: { total: 1000, offset: 0, has_more: true } })
  651. } } as unknown as UpstreamServiceSession)
  652. await expect(coordinator.reconcile(owner, profile, sessionID, true)).rejects.toThrow('offline')
  653. expect(f.store.messagePage(owner, profile, sessionID, 900, 100)).toBeDefined()
  654. f.store.close()
  655. })
  656. it('does not commit an older fetch over a newer event', async () => {
  657. const f = cached()
  658. const { coordinator, request } = upstreamFor(f, f.rows)
  659. const original = request.getMockImplementation()!
  660. request.mockImplementation(async (path, options) => {
  661. const result = await original(path, options)
  662. if (path.endsWith('/messages')) f.store.recordEvent(owner, profile, sessionID, { type: 'message.delta', seq: 2, payload: { delta: 'newer' } })
  663. return result
  664. })
  665. await coordinator.reconcile(owner, profile, sessionID)
  666. expect(f.store.localDetail(owner, profile, sessionID)?.state).toBe('stale')
  667. f.store.close()
  668. })
  669. it('joins an in-flight completion and publishes only after the newest revision commits', async () => {
  670. const f = cached()
  671. const rows = [...f.rows, { id: 'newest', role: 'assistant', content: 'newest reply' }]
  672. const { coordinator, request } = upstreamFor(f, rows)
  673. const original = request.getMockImplementation()!
  674. let release!: () => void
  675. const gate = new Promise<void>(resolve => { release = resolve })
  676. let first = true
  677. request.mockImplementation(async (path, options) => {
  678. if (first) { first = false; await gate }
  679. return original(path, options)
  680. })
  681. const published = vi.fn(() => {
  682. const stored = JSON.parse(f.store.messagePage(owner, profile, sessionID, 0, 1)!.response.body.toString())
  683. expect(stored.messages[0].id).toBe('newest')
  684. })
  685. coordinator.onSynchronized = published
  686. f.store.recordEvent(owner, profile, sessionID, { type: 'message.complete', seq: 1 })
  687. const firstSync = coordinator.reconcile(owner, profile, sessionID)
  688. f.store.recordEvent(owner, profile, sessionID, { type: 'message.complete', seq: 2 })
  689. const latestSync = coordinator.reconcile(owner, profile, sessionID)
  690. release()
  691. await Promise.all([firstSync, latestSync])
  692. expect(published).toHaveBeenCalledTimes(1)
  693. expect(request).toHaveBeenCalledTimes(4)
  694. expect(f.store.localDetail(owner, profile, sessionID)?.state).toBe('current')
  695. f.store.close()
  696. })
  697. it('serves a cached empty conversation without requesting it again', async () => {
  698. const f = cached(0)
  699. const load = vi.fn(async () => { throw new Error('unexpected read') })
  700. const page = await f.coordinator.messages(owner, 'empty', profile, sessionID, 0, 100, load)
  701. expect(page.source).toBe('local')
  702. expect(JSON.parse(page.response.body.toString()).messages).toEqual([])
  703. expect(load).not.toHaveBeenCalled()
  704. f.store.close()
  705. })
  706. })