session-projections.host.spec.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /**
  2. * Session Controller projection paths: the history tail page's
  3. * projections block reads the registry's watermark snapshot (asOfSeq = last
  4. * event seq, one consistent cut); loadOlder pages never carry the block; a
  5. * composition without the registry serves histories without it; a disposed
  6. * registration's key leaves subsequent responses; and every unit change is
  7. * pushed through the control stream.
  8. */
  9. import { describe, expect, it, vi } from 'vitest'
  10. import { Context } from '@deepseek-ai/cordis'
  11. import { z } from 'zod'
  12. import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent'
  13. import { AttachmentStore } from '@deepseek-ai/dsh-attachment'
  14. import { agentPresetProjectionDefinition } from '@deepseek-ai/dsh-agent-presets'
  15. import type { Agent } from '@deepseek-ai/dsh-agent'
  16. import { createUserMessage } from '@deepseek-ai/dsh-llm'
  17. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  18. import type { Session } from '@deepseek-ai/dsh-session'
  19. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  20. import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection'
  21. import { SessionControlController } from '@deepseek-ai/dsh-api-session-controller/src/control.ts'
  22. import type { SessionControlFrame, SessionFollowFrame } from '@deepseek-ai/dsh-api-session-controller/types'
  23. import { createSessionTestRemote, type TestSessionRemote } from './test-remote.ts'
  24. declare module '@deepseek-ai/dsh-session-projection/types' {
  25. interface SessionProjectionStateMap {
  26. 'test/last-user': LastUserState
  27. 'test/internal-count': number
  28. }
  29. interface SessionProjectionMap {
  30. 'test/last-user': { text: string } | null
  31. }
  32. }
  33. function request<P>(payload: P): P {
  34. return payload
  35. }
  36. function page(
  37. remote: TestSessionRemote,
  38. request: { sessionId: SessionId; throughSeq: number; beforeSeq?: number; maxMessages?: number },
  39. ) {
  40. return remote.page({
  41. address: { kind: 'session', sessionId: request.sessionId },
  42. throughSeq: request.throughSeq,
  43. ...(request.beforeSeq === undefined ? {} : { beforeSeq: request.beforeSeq }),
  44. ...(request.maxMessages === undefined ? {} : { maxMessages: request.maxMessages }),
  45. })
  46. }
  47. /** Read and close one snapshot-first follow generation. */
  48. async function opening(
  49. remote: TestSessionRemote,
  50. sessionId: SessionId,
  51. maxMessages?: number,
  52. ): Promise<Extract<SessionFollowFrame, { type: 'snapshot' }>> {
  53. const abort = new AbortController()
  54. const iterator = remote.follow({
  55. address: { kind: 'session', sessionId },
  56. ...(maxMessages === undefined ? {} : { maxMessages }),
  57. }, abort.signal)[Symbol.asyncIterator]()
  58. const first = await iterator.next()
  59. abort.abort()
  60. await iterator.return?.()
  61. if (first.done || first.value.type !== 'snapshot') throw new Error('follow did not open with a snapshot')
  62. return first.value
  63. }
  64. /** Whole-value unit folding the latest user/message text; null before the first. */
  65. type LastUserState = { text: string } | null
  66. const lastUserUnit = () => ({
  67. key: 'test/last-user',
  68. stateSchema: z.union([z.object({ text: z.string() }), z.null()]),
  69. init: () => null,
  70. apply: (state, event) => (event.type === 'user/message'
  71. ? { text: (event.data.content[0] as { text?: string }).text ?? '' }
  72. : state),
  73. wire: {
  74. viewSchema: z.union([z.object({ text: z.string() }), z.null()]),
  75. view: state => state,
  76. },
  77. stateVersion: 1,
  78. }) satisfies ProjectionDefinition<'test/last-user', LastUserState>
  79. const internalCountUnit = () => ({
  80. key: 'test/internal-count',
  81. stateSchema: z.number().int().nonnegative(),
  82. init: () => 0,
  83. apply: (state: number) => state + 1,
  84. stateVersion: 1,
  85. }) satisfies ProjectionDefinition<'test/internal-count', number>
  86. async function harness(withRegistry: boolean): Promise<{ ctx: Context; session: Session }> {
  87. const ctx = new Context()
  88. await ctx.plugin(SessionStore)
  89. await ctx.plugin(AgentRegistry)
  90. if (withRegistry) await ctx.plugin(SessionProjectionRegistry)
  91. const session = ctx.sessions.create(undefined, { meta: { cwd: '/workspace' } })
  92. // The gateway reads both the session and durable inbox baseline.
  93. ctx.agents.register({ id: session.id, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }), status: 'idle', ctx } as Agent)
  94. return { ctx, session }
  95. }
  96. /** Append `count` user messages so the log has paginable message boundaries. */
  97. function seedMessages(session: Session, count: number): void {
  98. for (let i = 0; i < count; i++) {
  99. session.append('user/message', createUserMessage({
  100. content: [{ type: 'text', text: `m${i}` }],
  101. source: { kind: 'user' },
  102. }), { surfaceOp: 'append' })
  103. }
  104. }
  105. const remote = (ctx: Context) => createSessionTestRemote(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/tmp' })
  106. describe('session.history projections block', () => {
  107. it('tracks pending and used model selections across repeated request headers', async () => {
  108. const { ctx, session } = await harness(true)
  109. remote(ctx)
  110. await new Promise(resolve => setTimeout(resolve, 0))
  111. const selected = { provider: 'p', model: 'next' }
  112. session.append('model/selection', selected)
  113. session.append('model/selection', selected)
  114. session.append('request/header', {
  115. header: { config: { provider: 'p', model: 'used' } }, reason: 'initial',
  116. })
  117. session.append('request/header', {
  118. header: { config: { provider: 'p', model: 'used' } }, reason: 'initial',
  119. })
  120. expect(ctx.sessionProjections.snapshot(session).values.modelSelection).toEqual({
  121. lastUsed: { provider: 'p', model: 'used' },
  122. next: selected,
  123. })
  124. session.append('request/header', {
  125. header: { config: selected }, reason: 'initial',
  126. })
  127. expect(ctx.sessionProjections.snapshot(session).values.modelSelection).toEqual({
  128. lastUsed: selected,
  129. next: selected,
  130. })
  131. })
  132. it('serves the unit value on the tail page with asOfSeq = last event seq', async () => {
  133. const { ctx, session } = await harness(true)
  134. ctx.sessionProjections.register(lastUserUnit())
  135. seedMessages(session, 3)
  136. const snapshot = await opening(remote(ctx), session.id)
  137. const { records, projections } = snapshot
  138. expect(projections.asOfSeq).toBe(session.seq - 1)
  139. expect(projections.values['test/last-user']).toEqual({ text: 'm2' })
  140. // asOfSeq IS the window tail: the last served event carries it.
  141. const last = records.at(-1)
  142. expect(last?.event.seq).toBe(projections.asOfSeq)
  143. })
  144. it('returns a complete current replacement cut on each follow generation', async () => {
  145. const { ctx, session } = await harness(true)
  146. ctx.sessionProjections.register(lastUserUnit())
  147. seedMessages(session, 2)
  148. const snapshot = await opening(remote(ctx), session.id)
  149. expect(snapshot.records.map(record => record.event.seq)).toEqual([0, 1])
  150. expect(snapshot.projections.asOfSeq).toBe(1)
  151. expect(snapshot.projections.values).toEqual(
  152. expect.objectContaining({ 'test/last-user': { text: 'm1' } }),
  153. )
  154. })
  155. it('projects an empty log at cursor -1', async () => {
  156. const { ctx, session } = await harness(true)
  157. ctx.sessionProjections.register(lastUserUnit())
  158. const snapshot = await opening(remote(ctx), session.id)
  159. expect(snapshot.records).toEqual([])
  160. expect(snapshot.projections.asOfSeq).toBe(-1)
  161. expect(snapshot.projections.values).toEqual(
  162. expect.objectContaining({ 'test/last-user': null }),
  163. )
  164. })
  165. it('publishes the attachments imageLimits as a constant unit while both seams are composed', async () => {
  166. const { ctx, session } = await harness(true)
  167. const limits = {
  168. maxImageBytes: 5 * 1024 * 1024,
  169. maxImagesPerMessage: 20,
  170. maxMessageImageBytes: 100 * 1024 * 1024,
  171. maxImagePixels: 40_000_000,
  172. maxImageDimension: 2000,
  173. mediaTypes: ['image/png'] as const,
  174. }
  175. await ctx.plugin(class extends AttachmentStore {
  176. readonly imageLimits = limits
  177. validateImage(): Promise<void> { return Promise.resolve() }
  178. saveImage(): Promise<never> { return Promise.reject(new Error('unused')) }
  179. readImage(): Promise<never> { return Promise.reject(new Error('unused')) }
  180. })
  181. const gateway = remote(ctx)
  182. await new Promise(resolve => setTimeout(resolve, 0))
  183. seedMessages(session, 2)
  184. const snapshot = await opening(gateway, session.id)
  185. expect(snapshot.projections.values['imageLimits']).toEqual(limits)
  186. // Constant unit: appending events must never broadcast an imageLimits projection.
  187. await new Promise(resolve => setTimeout(resolve, 0))
  188. const abort = new AbortController()
  189. const iterator = gateway.control(abort.signal)[Symbol.asyncIterator]()
  190. await iterator.next()
  191. const next = iterator.next()
  192. seedMessages(session, 1)
  193. await new Promise(resolve => setTimeout(resolve, 0))
  194. await expect(next).resolves.toMatchObject({
  195. done: false,
  196. value: { type: 'projection', key: 'sessionListMetadata' },
  197. })
  198. const extra = iterator.next()
  199. const quiet = Symbol('quiet')
  200. expect(await Promise.race([
  201. extra,
  202. new Promise<typeof quiet>(resolve => setTimeout(() => { resolve(quiet) }, 0)),
  203. ])).toBe(quiet)
  204. abort.abort()
  205. await expect(extra).resolves.toEqual({ done: true, value: undefined })
  206. })
  207. it('leaves the imageLimits key absent while no attachment service is composed', async () => {
  208. const { ctx, session } = await harness(true)
  209. seedMessages(session, 1)
  210. const snapshot = await opening(remote(ctx), session.id)
  211. expect('imageLimits' in snapshot.projections.values).toBe(false)
  212. })
  213. it('never carries the block on loadOlder pages (beforeSeq present)', async () => {
  214. const { ctx, session } = await harness(true)
  215. ctx.sessionProjections.register(lastUserUnit())
  216. seedMessages(session, 5)
  217. const older = await page(remote(ctx), request({
  218. sessionId: session.id, throughSeq: session.seq - 1, beforeSeq: 3, maxMessages: 2,
  219. }))
  220. expect(older.ok).toBe(true)
  221. if (!older.ok) throw new Error('unreachable')
  222. expect('projections' in older.value).toBe(false)
  223. })
  224. it('serves no block when the composition has no projection registry', async () => {
  225. const { ctx, session } = await harness(false)
  226. seedMessages(session, 2)
  227. const response = await page(remote(ctx), request({ sessionId: session.id, throughSeq: session.seq - 1 }))
  228. expect(response.ok).toBe(true)
  229. if (!response.ok) throw new Error('unreachable')
  230. expect('projections' in response.value).toBe(false)
  231. })
  232. it('never exposes a host-only unit through history, listing, or push frames', async () => {
  233. const { ctx, session } = await harness(true)
  234. ctx.sessionProjections.register(internalCountUnit())
  235. const proxy = remote(ctx)
  236. await new Promise(resolve => setTimeout(resolve, 0))
  237. const abort = new AbortController()
  238. const iterator = proxy.control(abort.signal)[Symbol.asyncIterator]()
  239. const baseline = await iterator.next()
  240. if (baseline.done || baseline.value.type !== 'baseline') {
  241. throw new Error('control stream ended before its baseline')
  242. }
  243. expect('test/internal-count' in (baseline.value.value.projections[session.id]?.values ?? {}))
  244. .toBe(false)
  245. seedMessages(session, 1)
  246. const changed = await iterator.next()
  247. expect(changed).toMatchObject({
  248. done: false,
  249. value: { type: 'projection', key: 'sessionListMetadata' },
  250. })
  251. abort.abort()
  252. await iterator.return?.()
  253. const history = await opening(proxy, session.id)
  254. expect('test/internal-count' in history.projections.values).toBe(false)
  255. const listing = await proxy.list(request({}))
  256. if (!listing.ok) throw new Error('listing failed')
  257. const row = listing.value.items.find(item => item.sessionId === session.id)
  258. expect('test/internal-count' in (row?.projections?.values ?? {})).toBe(false)
  259. })
  260. it('drops a disposed registration from subsequent tail pages (empty block, key absent)', async () => {
  261. const { ctx, session } = await harness(true)
  262. const dispose = ctx.sessionProjections.register(lastUserUnit())
  263. seedMessages(session, 1)
  264. const proxy = remote(ctx)
  265. const before = await opening(proxy, session.id)
  266. expect(before.projections.values['test/last-user']).toEqual({ text: 'm0' })
  267. dispose()
  268. const after = await opening(proxy, session.id)
  269. // The registry stays mounted; only the disposed key leaves while the
  270. // gateway-owned Session-list unit remains.
  271. expect(after.projections.asOfSeq).toBe(session.seq - 1)
  272. expect('test/last-user' in after.projections.values).toBe(false)
  273. expect(after.projections.values.sessionListMetadata).toEqual({
  274. blank: true,
  275. lastPromptAt: session.events.at(-1)?.time,
  276. })
  277. })
  278. it('removes the gateway-owned Session-list unit when the gateway fiber unloads', async () => {
  279. const { ctx, session } = await harness(true)
  280. expect('sessionListMetadata' in ctx.sessionProjections.snapshot(session).values).toBe(false)
  281. const fiber = ctx.plugin(Object.assign((gatewayCtx: Context) => {
  282. createSessionTestRemote(gatewayCtx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/tmp' })
  283. }, { inject: ['sessions', 'agents', 'sessionProjections'] }))
  284. await fiber.await()
  285. await vi.waitFor(() => {
  286. expect(ctx.sessionProjections.snapshot(session).values.sessionListMetadata)
  287. .toEqual({ blank: true, lastPromptAt: null })
  288. })
  289. await fiber.dispose()
  290. expect('sessionListMetadata' in ctx.sessionProjections.snapshot(session).values).toBe(false)
  291. })
  292. })
  293. describe('session.list projections column', () => {
  294. it('serves every already-materialized wire value from the live registry without folding', async () => {
  295. const { ctx, session } = await harness(true)
  296. ctx.sessionProjections.register(lastUserUnit())
  297. const gateway = remote(ctx)
  298. await new Promise(resolve => setTimeout(resolve, 0))
  299. session.append('turn/start', { turn: 1 })
  300. seedMessages(session, 1)
  301. const response = await gateway.list(request({}))
  302. if (!response.ok) throw new Error('unreachable')
  303. const row = response.value.items.find(item => item.sessionId === session.id)
  304. expect(row?.projections?.values['test/last-user']).toEqual({ text: 'm0' })
  305. expect(row?.projections?.values.sessionListMetadata).toEqual({
  306. blank: false,
  307. lastPromptAt: session.events.at(-1)?.time,
  308. })
  309. expect(row?.projections?.asOfSeq).toBe(session.seq - 1)
  310. })
  311. it('lists the latest preset selected by a blank Session instead of its creation preset', async () => {
  312. const { ctx } = await harness(true)
  313. const session = ctx.sessions.create(SessionId('preset-list'), {
  314. meta: { cwd: '/workspace', agentPreset: 'standard' },
  315. })
  316. ctx.sessionProjections.register(agentPresetProjectionDefinition)
  317. const gateway = remote(ctx)
  318. await new Promise(resolve => setTimeout(resolve, 0))
  319. session.append('agent-preset/selected', { agentPreset: 'minimal' })
  320. const response = await gateway.list(request({}))
  321. if (!response.ok) throw new Error('unreachable')
  322. const row = response.value.items.find(item => item.sessionId === session.id)
  323. expect(row?.projections?.values.agentPreset).toBe('minimal')
  324. })
  325. it('omits an unmaterialized live projection instead of folding history for listing', async () => {
  326. const { ctx, session } = await harness(true)
  327. seedMessages(session, 1)
  328. const unit = lastUserUnit()
  329. const apply = vi.fn(unit.apply)
  330. ctx.sessionProjections.register({ ...unit, apply })
  331. const response = await remote(ctx).list(request({}))
  332. if (!response.ok) throw new Error('unreachable')
  333. const row = response.value.items.find(item => item.sessionId === session.id)
  334. expect(row).toBeDefined()
  335. expect('test/last-user' in (row?.projections?.values ?? {})).toBe(false)
  336. expect(apply).not.toHaveBeenCalled()
  337. })
  338. it('omits the column entirely when no registry is mounted', async () => {
  339. const { ctx, session } = await harness(false)
  340. seedMessages(session, 1)
  341. const response = await remote(ctx).list(request({}))
  342. if (!response.ok) throw new Error('unreachable')
  343. const row = response.value.items.find(item => item.sessionId === session.id)
  344. expect(row).toBeDefined()
  345. expect(row !== undefined && 'projections' in row).toBe(false)
  346. })
  347. it('serves every available cold projection hint from the cache with zero log loads', async () => {
  348. const { ctx } = await harness(true)
  349. const coldId = SessionId('session-cold-listing')
  350. const load = () => { throw new Error('list must not load event logs') }
  351. ctx.provide('sessionPersistence', {
  352. list: async () => [{ version: 0, id: coldId, createdAt: 5, cwd: '/tmp' }],
  353. locate: () => undefined,
  354. load,
  355. inspect: load,
  356. readFrom: load,
  357. } as never)
  358. ctx.provide('sessionProjectionCache', {
  359. // The carrier hands the listed header through as the identity witness.
  360. cachedSnapshot: (meta: { id: unknown; createdAt: number }) =>
  361. (meta.id === coldId && meta.createdAt === 5
  362. ? {
  363. asOfSeq: 7,
  364. values: {
  365. 'test/last-user': { text: 'cached' },
  366. sessionListMetadata: { blank: false, lastPromptAt: 6 },
  367. title: 'Cached title',
  368. },
  369. }
  370. : undefined),
  371. } as never)
  372. const response = await remote(ctx).list(request({}))
  373. if (!response.ok) throw new Error('unreachable')
  374. const row = response.value.items.find(item => item.sessionId === coldId)
  375. expect(row?.running).toBe(false)
  376. expect(row?.projections).toEqual({
  377. asOfSeq: 7,
  378. values: {
  379. 'test/last-user': { text: 'cached' },
  380. sessionListMetadata: { blank: false, lastPromptAt: 6 },
  381. title: 'Cached title',
  382. },
  383. })
  384. })
  385. it('cold rows without a cache plugin (or without a stored row) just lack the column', async () => {
  386. const { ctx } = await harness(true)
  387. const coldId = SessionId('session-cold-uncached')
  388. ctx.provide('sessionPersistence', {
  389. list: async () => [{ version: 0, id: coldId, createdAt: 5, cwd: '/tmp' }],
  390. locate: () => undefined,
  391. } as never)
  392. const response = await remote(ctx).list(request({}))
  393. if (!response.ok) throw new Error('unreachable')
  394. const row = response.value.items.find(item => item.sessionId === coldId)
  395. expect(row).toBeDefined()
  396. expect(row !== undefined && 'projections' in row).toBe(false)
  397. })
  398. it('a throwing column read degrades that row, never the listing', async () => {
  399. const { ctx, session } = await harness(true)
  400. ctx.sessionProjections.register({
  401. ...lastUserUnit(),
  402. wire: {
  403. viewSchema: z.union([z.object({ text: z.string() }), z.null()]),
  404. view: () => { throw new Error('unit exploded') },
  405. },
  406. })
  407. seedMessages(session, 1)
  408. const response = await remote(ctx).list(request({}))
  409. if (!response.ok) throw new Error('unreachable')
  410. const row = response.value.items.find(item => item.sessionId === session.id)
  411. expect(row).toBeDefined()
  412. expect(row !== undefined && 'projections' in row).toBe(false)
  413. })
  414. })
  415. describe('Session control projection frames', () => {
  416. /** Drain frames until `count` projection replacements arrive. */
  417. async function collect(
  418. iterable: AsyncIterable<SessionControlFrame>,
  419. count: number,
  420. abort: AbortController,
  421. ): Promise<SessionControlFrame[]> {
  422. const frames: SessionControlFrame[] = []
  423. for await (const frame of iterable) {
  424. frames.push(frame)
  425. if (frames.filter(candidate => candidate.type === 'projection').length >= count) abort.abort()
  426. }
  427. return frames
  428. }
  429. it('broadcasts a frame per changed unit with the causing seq, and none for same-reference applies', async () => {
  430. const { ctx, session } = await harness(true)
  431. ctx.sessionProjections.register(lastUserUnit())
  432. const proxy = remote(ctx)
  433. // The controller's onChanged subscription lives in an inject child whose
  434. // fiber activates asynchronously; yield until it lands before appending.
  435. await new Promise(resolve => setTimeout(resolve, 0))
  436. const abort = new AbortController()
  437. const stream = proxy.control(abort.signal)
  438. const collected = collect(stream, 5, abort)
  439. const now = vi.spyOn(Date, 'now').mockReturnValue(100)
  440. seedMessages(session, 1)
  441. now.mockReturnValue(200)
  442. session.append('turn/start', { turn: 1 })
  443. now.mockReturnValue(300)
  444. seedMessages(session, 1)
  445. now.mockRestore()
  446. const frames = await collected
  447. const pushes = frames.filter(
  448. (f): f is Extract<SessionControlFrame, { type: 'projection' }> =>
  449. f.type === 'projection' && f.key === 'test/last-user',
  450. )
  451. expect(pushes).toEqual([
  452. { type: 'projection', sessionId: session.id, key: 'test/last-user', value: { text: 'm0' }, seq: 0 },
  453. { type: 'projection', sessionId: session.id, key: 'test/last-user', value: { text: 'm0' }, seq: 2 },
  454. ])
  455. expect(frames.filter(
  456. (f): f is Extract<SessionControlFrame, { type: 'projection' }> =>
  457. f.type === 'projection' && f.key === 'sessionListMetadata',
  458. )).toEqual([
  459. { type: 'projection', sessionId: session.id, key: 'sessionListMetadata', value: { blank: true, lastPromptAt: 100 }, seq: 0 },
  460. { type: 'projection', sessionId: session.id, key: 'sessionListMetadata', value: { blank: false, lastPromptAt: 100 }, seq: 1 },
  461. { type: 'projection', sessionId: session.id, key: 'sessionListMetadata', value: { blank: false, lastPromptAt: 300 }, seq: 2 },
  462. ])
  463. // Frame seq aligns with the tail block's asOfSeq vocabulary (higher-seq-wins compatible).
  464. const tail = await opening(proxy, session.id)
  465. expect(tail.projections.asOfSeq).toBe(pushes.at(-1)?.seq)
  466. })
  467. it('emits no projection frames when the composition has no registry', async () => {
  468. const { ctx, session } = await harness(false)
  469. const control = new SessionControlController(ctx)
  470. const abort = new AbortController()
  471. const iterator = control.control(abort.signal)[Symbol.asyncIterator]()
  472. const baseline = await iterator.next()
  473. const next = iterator.next()
  474. seedMessages(session, 2)
  475. await new Promise(resolve => setTimeout(resolve, 0))
  476. abort.abort()
  477. if (baseline.done) throw new Error('Control stream ended before its baseline')
  478. expect(baseline.value.type).toBe('baseline')
  479. await expect(next).resolves.toEqual({ done: true, value: undefined })
  480. })
  481. })