chatCache.test.ts 37 KB

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