realtimeBroker.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import { mkdtempSync, readFileSync } from 'node:fs'
  2. import { join } from 'node:path'
  3. import { tmpdir } from 'node:os'
  4. import { once } from 'node:events'
  5. import { WebSocketServer, type WebSocket } from 'ws'
  6. import { afterEach, describe, expect, it, vi } from 'vitest'
  7. import { RealtimeBroker, type RealtimeActivity, type RealtimePrincipal, type StreamEntry } from '../../src/server/realtimeBroker.js'
  8. import { RealtimeReceipts } from '../../src/server/realtimeReceipts.js'
  9. const cleanup: Array<() => void> = []
  10. afterEach(() => cleanup.splice(0).reverse().forEach(f => f()))
  11. async function fixture() {
  12. const home = mkdtempSync(join(tmpdir(), 'yaoyao-realtime-test-'))
  13. const ws = new WebSocketServer({ port: 0, host: '127.0.0.1' })
  14. await once(ws, 'listening')
  15. cleanup.push(() => { for (const c of ws.clients) c.terminate(); ws.close() })
  16. const commands: any[] = []
  17. let count = 0
  18. let seq = 0, truncate = false, dropPrompt = false
  19. const history: any[] = []
  20. let peer: WebSocket
  21. ws.on('connection', socket => {
  22. peer = socket; count++
  23. socket.send(JSON.stringify({ method: 'event', params: { type: 'gateway.ready', payload: { replay_epoch: 'test' } } }))
  24. socket.on('message', raw => {
  25. const f = JSON.parse(raw.toString()); commands.push(f)
  26. if (f.method === 'prompt.submit' && dropPrompt) { socket.terminate(); return }
  27. if (f.method === 'session.events.since') {
  28. socket.send(JSON.stringify({ id: f.id, result: { epoch: 'test', truncated: truncate,
  29. events: history.filter(e => e.seq > f.params.last_seen) } }))
  30. } else if (f.method === 'session.branch') {
  31. socket.send(JSON.stringify({ id: f.id, result: { session_id: 'runtime-branch', stored_session_id: 'stored-branch' } }))
  32. } else if (f.method === 'session.create' || f.method === 'session.resume') {
  33. socket.send(JSON.stringify({ id: f.id, result: { session_id: 'runtime-1', stored_session_id: 'stored-1', running: false } }))
  34. } else {
  35. setTimeout(() => socket.send(JSON.stringify({ id: f.id, result: { status: 'submitted' } })), 20)
  36. }
  37. })
  38. })
  39. let now = Date.now()
  40. const activities: RealtimeActivity[] = []
  41. const broker = new RealtimeBroker(home, () => now, change => activities.push(change))
  42. cleanup.push(() => broker.close())
  43. const principal = (key: string): RealtimePrincipal => ({ key, upstreamKey: 'one-service', paired: false,
  44. valid: () => true, url: async () => new URL(`ws://127.0.0.1:${(ws.address() as any).port}`) })
  45. return { home, broker, principal, commands, activities, count: () => count, advance: (n: number) => { now += n },
  46. disconnect: () => peer!.terminate(), truncate: () => { truncate = true }, dropPrompt: () => { dropPrompt = true },
  47. emit: (type: string, payload: unknown) => {
  48. const params = { type, session_id: 'runtime-1', seq: ++seq, payload }; history.push(params)
  49. if (peer!.readyState === 1) peer!.send(JSON.stringify({ method: 'event', params }))
  50. } }
  51. }
  52. const resume = { method: 'session.resume', params: { session_id: 'stored-1', profile: 'default' } }
  53. describe('realtime broker', () => {
  54. it('rejects resuming a history-only session before opening an upstream route', async () => {
  55. const f = await fixture()
  56. const routes: string[] = []
  57. f.broker.onNativeRoute = (_owner, _profile, stored) => routes.push(stored)
  58. const principal = { ...f.principal('alice'), canResume: async () => false }
  59. const channel = await f.broker.create(principal, 'chat')
  60. const receipt = await f.broker.command(channel, 'history-resume', resume)
  61. expect(receipt).toMatchObject({ state: 'rejected', response: { error: { code: 'history_session_read_only' } } })
  62. expect(f.commands).toEqual([])
  63. expect(routes).toEqual([])
  64. })
  65. it('registers and resumes sessions only within one stable device owner', async () => {
  66. const f = await fixture()
  67. const owned = new Set<string>()
  68. const commands: Array<{ owner: string; stored: string; method: string }> = []
  69. const events: Array<{ owner: string; stored: string; type: string }> = []
  70. const ownershipKey = (owner: string, profile: string, stored: string) =>
  71. `${owner}\u0000${profile}\u0000${stored}`
  72. f.broker.onNativeRoute = (owner, profile, stored) => {
  73. owned.add(ownershipKey(owner, profile, stored))
  74. }
  75. f.broker.onNativeCommand = (owner, _profile, stored, method) => {
  76. commands.push({ owner, stored, method })
  77. }
  78. f.broker.onNativeEvent = (owner, _profile, stored, frame) => {
  79. events.push({ owner, stored, type: String(frame.type) })
  80. }
  81. const device = (id: string): RealtimePrincipal => ({
  82. ...f.principal(`device:${id}`),
  83. upstreamKey: `device-service:${id}`,
  84. paired: true,
  85. canResume: async (profile, stored) =>
  86. owned.has(ownershipKey(`device:${id}`, profile, stored)),
  87. })
  88. const deviceA = device('device-a')
  89. const first = await f.broker.create(deviceA, 'chat')
  90. const created = await f.broker.command(first, 'device-create', {
  91. method: 'session.create', params: { profile: 'default', source: 'web' },
  92. })
  93. expect(created.state).toBe('confirmed')
  94. expect(owned).toContain(ownershipKey('device:device-a', 'default', 'stored-1'))
  95. expect(commands).toContainEqual({ owner: 'device:device-a', stored: 'stored-1', method: 'session.create' })
  96. f.emit('message.delta', { text: 'device event' })
  97. await vi.waitFor(() => expect(events).toContainEqual({
  98. owner: 'device:device-a', stored: 'stored-1', type: 'message.delta',
  99. }))
  100. const sameDevice = await f.broker.create(device('device-a'), 'chat')
  101. expect((await f.broker.command(sameDevice, 'same-device-resume', resume)).state).toBe('confirmed')
  102. const unowned = await f.broker.command(sameDevice, 'source-web-unowned', {
  103. method: 'session.resume', params: { session_id: 'unowned-source-web', profile: 'default' },
  104. })
  105. expect(unowned).toMatchObject({ state: 'rejected', response: { error: { code: 'history_session_read_only' } } })
  106. const anotherDevice = await f.broker.create(device('device-b'), 'chat')
  107. expect(await f.broker.command(anotherDevice, 'other-device-resume', resume))
  108. .toMatchObject({ state: 'rejected', response: { error: { code: 'history_session_read_only' } } })
  109. const user = {
  110. ...f.principal('user:alice:1'),
  111. upstreamKey: 'user-service',
  112. canResume: async (profile: string, stored: string) =>
  113. owned.has(ownershipKey('alice', profile, stored)),
  114. }
  115. const userChannel = await f.broker.create(user, 'chat')
  116. expect(await f.broker.command(userChannel, 'user-resume-device-session', resume))
  117. .toMatchObject({ state: 'rejected', response: { error: { code: 'history_session_read_only' } } })
  118. })
  119. it('broadcasts a route title as an owner-scoped session list change', async () => {
  120. const f = await fixture()
  121. const globalOwners: string[] = []
  122. f.broker.onNativeGlobalEvent = (owner, type) => {
  123. if (type === 'sessions.changed') globalOwners.push(owner)
  124. }
  125. const routedPrincipal = { ...f.principal('user:alice:1'), canResume: async () => true }
  126. const routed = await f.broker.create(routedPrincipal, 'chat')
  127. const listOnly = await f.broker.create(f.principal('user:alice:1'), 'chat')
  128. const anotherUser = await f.broker.create(f.principal('user:bob:1'), 'chat')
  129. await f.broker.command(routed, 'owner-title-route', resume)
  130. const listFrames: StreamEntry[] = []
  131. const otherFrames: StreamEntry[] = []
  132. f.broker.subscribe(listOnly, undefined, entry => listFrames.push(entry))
  133. f.broker.subscribe(anotherUser, undefined, entry => otherFrames.push(entry))
  134. f.emit('session.title', { session_id: 'stored-1', title: '跨端标题' })
  135. await vi.waitFor(() => expect(listFrames.some(entry =>
  136. entry.data.includes('sessions.changed'))).toBe(true))
  137. expect(otherFrames.some(entry => entry.data.includes('sessions.changed'))).toBe(false)
  138. expect(globalOwners).toEqual(['alice'])
  139. })
  140. it('preserves restricted create provenance and never forwards source on resume', async () => {
  141. const f = await fixture()
  142. const channel = await f.broker.create(f.principal('alice'), 'chat')
  143. await f.broker.command(channel, 'create-ios', {
  144. method: 'session.create', params: { profile: 'default', source: 'ios' },
  145. })
  146. await f.broker.command(channel, 'create-web-default', {
  147. method: 'session.create', params: { profile: 'default' },
  148. })
  149. await f.broker.command(channel, 'resume-with-client-source', {
  150. method: 'session.resume', params: { session_id: 'stored-1', profile: 'default', source: 'ios' },
  151. })
  152. const openFrames = f.commands.filter(frame => frame.method === 'session.create' || frame.method === 'session.resume')
  153. expect(openFrames[0]?.params.source).toBe('ios')
  154. expect(openFrames[1]?.params.source).toBe('web')
  155. expect(openFrames[2]?.params).not.toHaveProperty('source')
  156. expect(() => f.broker.command(channel, 'create-invalid-source', {
  157. method: 'session.create', params: { profile: 'default', source: 'telegram' },
  158. })).toThrow(/source must be web or ios/)
  159. })
  160. it('shares upstream across subscribers and isolates unregistered routes', async () => {
  161. const f = await fixture()
  162. const a = await f.broker.create(f.principal('alice'), 'chat')
  163. const b = await f.broker.create(f.principal('bob'), 'chat')
  164. const c = await f.broker.create(f.principal('alice'), 'chat')
  165. await f.broker.command(a, 'resume-a', resume)
  166. await f.broker.command(c, 'resume-c', resume)
  167. const seenA: StreamEntry[] = [], seenB: StreamEntry[] = [], seenC: StreamEntry[] = []
  168. f.broker.subscribe(a, undefined, x => seenA.push(x)); f.broker.subscribe(b, undefined, x => seenB.push(x)); f.broker.subscribe(c, undefined, x => seenC.push(x))
  169. f.emit('message.delta', { text: '你好' })
  170. await new Promise(r => setTimeout(r, 30))
  171. expect(f.count()).toBe(1)
  172. expect(seenA.some(x => x.data.includes('你好'))).toBe(true)
  173. expect(seenC.some(x => x.data.includes('你好'))).toBe(true)
  174. expect(seenB.some(x => x.data.includes('你好'))).toBe(false)
  175. expect(() => f.broker.get(f.principal('bob'), a.id)).toThrow('Channel not found')
  176. })
  177. it('deduplicates simultaneous commands and rejects a changed payload', async () => {
  178. const f = await fixture(), p = f.principal('alice')
  179. const c = await f.broker.create(p, 'chat')
  180. await f.broker.command(c, 'open', resume)
  181. const prompt = { method: 'prompt.submit', params: { session_id: 'runtime-1', text: 'secret prompt' } }
  182. const [one, two] = await Promise.all([f.broker.command(c, 'outbox-1', prompt), f.broker.command(c, 'outbox-1', prompt)])
  183. expect(one).toEqual(two); expect(one.state).toBe('confirmed')
  184. expect(f.commands.filter(x => x.method === 'prompt.submit')).toHaveLength(1)
  185. expect(() => f.broker.command(c, 'outbox-1', { ...prompt, params: { ...prompt.params, text: 'different' } })).toThrow('conflict')
  186. expect(() => f.broker.receipt(f.principal('bob'), 'outbox-1')).toThrow('Receipt not found')
  187. for (const file of ['realtime-receipts.sqlite3', 'realtime-receipts.sqlite3-wal']) expect(readFileSync(join(f.home, file)).includes(Buffer.from('secret prompt'))).toBe(false)
  188. })
  189. it('keeps upstream alive after downstream detach and replays exact cursor', async () => {
  190. const f = await fixture(), p = f.principal('alice')
  191. const c = await f.broker.create(p, 'chat')
  192. await f.broker.command(c, 'open', resume)
  193. const entries: StreamEntry[] = []
  194. const off = f.broker.subscribe(c, undefined, x => entries.push(x))
  195. const cursor = entries.at(-1)!.id
  196. off(); f.emit('message.start', {}); f.emit('message.delta', { text: 'background' })
  197. await new Promise(r => setTimeout(r, 30))
  198. const replay: StreamEntry[] = []
  199. f.broker.subscribe(c, cursor, x => replay.push(x))
  200. expect(replay).toHaveLength(2); expect(f.count()).toBe(1)
  201. f.advance(600_001)
  202. expect(() => f.broker.subscribe(c, cursor, () => {})).toThrow('Replay window expired')
  203. })
  204. it('does not execute runtime commands without a route subscription', async () => {
  205. const f = await fixture()
  206. const c = await f.broker.create(f.principal('alice'), 'chat')
  207. const result = await f.broker.command(c, 'forbidden', { method: 'session.interrupt', params: { session_id: 'guessed' } })
  208. expect(result.state).toBe('rejected'); expect(f.commands).toHaveLength(0)
  209. })
  210. it('marks write-ahead admissions unknown after a process restart', () => {
  211. const home = mkdtempSync(join(tmpdir(), 'yaoyao-receipts-test-'))
  212. const a = new RealtimeReceipts(home)
  213. a.reserve('alice', 'submitted', '{"text":"private"}'); a.close()
  214. const b = new RealtimeReceipts(home)
  215. expect(b.reserve('alice', 'submitted', '{"text":"private"}')).toMatchObject({ state: 'unknown' })
  216. b.close()
  217. })
  218. it('registers branch identities without losing the original subscription', async () => {
  219. const f = await fixture()
  220. const ownershipCommands: Array<{ owner: string; stored: string; method: string }> = []
  221. f.broker.onNativeCommand = (owner, _profile, stored, method) => {
  222. ownershipCommands.push({ owner, stored, method })
  223. }
  224. const c = await f.broker.create(f.principal('user:alice:1'), 'chat')
  225. await f.broker.command(c, 'open', resume)
  226. const branch = await f.broker.command(c, 'branch', { method: 'session.branch', params: { session_id: 'runtime-1' } })
  227. expect(branch.state).toBe('confirmed')
  228. const sent = await f.broker.command(c, 'branched-prompt', { method: 'prompt.submit', params: { session_id: 'runtime-branch', text: 'branch' } })
  229. expect(sent.state).toBe('confirmed')
  230. expect(c.routes.size).toBe(2)
  231. expect(ownershipCommands).toContainEqual({ owner: 'alice', stored: 'stored-branch', method: 'session.branch' })
  232. })
  233. it('does not let guessed approval IDs escape a subscribed session', async () => {
  234. const f = await fixture(), c = await f.broker.create(f.principal('alice'), 'chat')
  235. await f.broker.command(c, 'open', resume)
  236. const result = await f.broker.command(c, 'approval', { method: 'approval.respond', params: { session_id: 'runtime-1', request_id: 'another-users-approval', approved: true } })
  237. expect(result).toMatchObject({ state: 'rejected', response: { error: { code: 'interaction_forbidden' } } })
  238. expect(f.commands.filter(x => x.method === 'approval.respond')).toHaveLength(0)
  239. })
  240. it('page reloads cannot exhaust live-stream capacity with detached handles', async () => {
  241. const f = await fixture(), p = f.principal('alice')
  242. for (let i = 0; i < 40; i++) await f.broker.create(p, 'chat')
  243. expect(f.broker.channels.size).toBe(32)
  244. expect(f.count()).toBe(1)
  245. })
  246. it('recovers the upstream replay ring without losing offline deltas', async () => {
  247. const f = await fixture(), c = await f.broker.create(f.principal('alice'), 'chat')
  248. await f.broker.command(c, 'open', resume)
  249. const received: StreamEntry[] = []
  250. f.broker.subscribe(c, undefined, e => received.push(e))
  251. f.emit('message.delta', { text: 'before' })
  252. await vi.waitFor(() => expect(received.some(e => e.data.includes('before'))).toBe(true))
  253. f.disconnect(); f.emit('message.delta', { text: 'offline' })
  254. await vi.waitFor(() => expect(received.filter(e => e.data.includes('offline'))).toHaveLength(1), { timeout: 2500 })
  255. expect(f.count()).toBe(2)
  256. expect(received.filter(e => e.event === 'reset')).toHaveLength(0)
  257. })
  258. it('reports reset when upstream history was truncated', async () => {
  259. const f = await fixture(), c = await f.broker.create(f.principal('alice'), 'chat')
  260. await f.broker.command(c, 'open', resume)
  261. const received: StreamEntry[] = []
  262. f.broker.subscribe(c, undefined, e => received.push(e))
  263. f.truncate(); f.disconnect()
  264. await vi.waitFor(() => expect(received.some(e => e.event === 'reset')).toBe(true), { timeout: 2500 })
  265. })
  266. it('never repeats a prompt when the upstream acceptance reply is lost', async () => {
  267. const f = await fixture(), c = await f.broker.create(f.principal('alice'), 'chat')
  268. await f.broker.command(c, 'open', resume)
  269. f.dropPrompt()
  270. const prompt = { method: 'prompt.submit', params: { session_id: 'runtime-1', text: 'do once' } }
  271. const result = await f.broker.command(c, 'lost-ack', prompt)
  272. expect(result.state).toBe('unknown')
  273. expect((await f.broker.command(c, 'lost-ack', prompt)).state).toBe('unknown')
  274. expect(f.commands.filter(f => f.method === 'prompt.submit')).toHaveLength(1)
  275. })
  276. it('invalidates read snapshots before/after mutation and before delivering live events', async () => {
  277. const f = await fixture(), c = await f.broker.create(f.principal('alice'), 'chat')
  278. await f.broker.command(c, 'open', resume)
  279. await f.broker.command(c, 'send', { method: 'prompt.submit', params: { session_id: 'runtime-1', text: 'once' } })
  280. expect(f.activities.filter(c => c.kind === 'command' && c.name === 'prompt.submit')).toEqual([
  281. { kind: 'command', name: 'prompt.submit', sessionId: 'stored-1' },
  282. { kind: 'command', name: 'prompt.submit', sessionId: 'stored-1' },
  283. ])
  284. const seen: StreamEntry[] = []
  285. f.broker.subscribe(c, undefined, entry => {
  286. if (entry.data.includes('message.delta')) expect(f.activities.at(-1)).toMatchObject({ kind: 'event', name: 'message.delta', sessionId: 'stored-1' })
  287. seen.push(entry)
  288. })
  289. f.emit('message.delta', { text: 'fresh' })
  290. await vi.waitFor(() => expect(seen.some(e => e.data.includes('fresh'))).toBe(true))
  291. })
  292. })