chatCache.test.ts 40 KB

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