session-rename.host.spec.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Session Controller rename delegation through the composed SessionTitleService. The
  3. * agent factory is a structural stub whose createAgent forwards seed/meta into
  4. * the real SessionStore, and whose resume never runs (every source here is
  5. * already attached). Cold-session resolution is the shared `agentFor` path —
  6. * remote-proxy-cold.spec.ts owns the resume evidence for every unary that rides
  7. * it, rename included.
  8. */
  9. import { describe, expect, it } from 'vitest'
  10. import { Context } from '@deepseek-ai/cordis'
  11. import SessionStore from '@deepseek-ai/dsh-session'
  12. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  13. import AgentRegistry from '@deepseek-ai/dsh-agent'
  14. import type { Agent, AgentHandle, CreateAgentOptions } from '@deepseek-ai/dsh-agent'
  15. import { createUserMessage } from '@deepseek-ai/dsh-llm'
  16. import SessionTitleService from '@deepseek-ai/dsh-session-title'
  17. import type { Session, SessionId } from '@deepseek-ai/dsh-session'
  18. import { createSessionTestRemote } from './test-remote.ts'
  19. const sid = (id: string): SessionId => id as SessionId
  20. function request<P>(payload: P): P {
  21. return payload
  22. }
  23. async function composed(withTitles = true): Promise<Context> {
  24. const ctx = new Context()
  25. await ctx.plugin(SessionStore)
  26. await ctx.plugin(SessionProjectionRegistry)
  27. await ctx.plugin(AgentRegistry)
  28. if (withTitles) {
  29. await ctx.plugin(SessionTitleService, { fallbackMaxWords: 5, fallbackMaxBytes: 40, maxTitleBytes: 40 })
  30. }
  31. // Store-backed structural factory: create builds the session with the
  32. // forwarded seed/meta (the store validates the balanced prefix) and
  33. // registers an idle agent stub over it.
  34. ctx.agents.setFactory({
  35. createAgent: (ownerCtx: Context, options: CreateAgentOptions): Promise<AgentHandle> => {
  36. const session = ctx.sessions.create(options.sessionId, {
  37. ...options.seed === undefined ? {} : { seed: [...options.seed] },
  38. ...options.meta === undefined ? {} : { meta: options.meta },
  39. })
  40. const agent = { id: session.id, session, status: 'idle', ctx: ownerCtx } as Agent
  41. ctx.agents.register(agent)
  42. return Promise.resolve({ agent, dispose: () => Promise.resolve() })
  43. },
  44. resume: () => Promise.reject(new Error('resume must not run: every source is attached')),
  45. })
  46. return ctx
  47. }
  48. /** Register one live agent whose log holds `turns` completed turns. */
  49. function liveAgent(ctx: Context, id: string, turns: number): Session {
  50. const session = ctx.sessions.create(sid(id), { meta: { cwd: '/proj' } })
  51. for (let turn = 1; turn <= turns; turn++) {
  52. session.append('turn/start', { turn })
  53. session.append('user/message', createUserMessage({
  54. content: [{ type: 'text', text: `prompt ${String(turn)}` }],
  55. source: { kind: 'user' },
  56. }), { surfaceOp: 'append' })
  57. session.append('turn/end', { turn, reason: { kind: 'completed' } })
  58. }
  59. ctx.agents.register({ id: session.id, session, status: 'idle', ctx } as Agent)
  60. return session
  61. }
  62. const remote = (ctx: Context) => createSessionTestRemote(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/tmp' })
  63. describe('sessions.rename', () => {
  64. it('accepts through the composed title service: normalized user-source event, echoed seq', async () => {
  65. const ctx = await composed()
  66. const source = liveAgent(ctx, 'session-rename', 1)
  67. const renamed = await remote(ctx).rename(request({ sessionId: source.id, title: ' new name ' }))
  68. expect(renamed.ok).toBe(true)
  69. if (!renamed.ok) return
  70. expect(renamed.value.title).toBe('new name')
  71. const event = source.snapshotEvents().findLast(item => item.type === 'session/title')
  72. expect(event?.seq).toBe(renamed.value.seq)
  73. expect(event?.data).toMatchObject({ title: 'new name', source: { kind: 'user' } })
  74. })
  75. it('maps only an empty-normalizing title to title-invalid, with a presentable message', async () => {
  76. const ctx = await composed()
  77. const source = liveAgent(ctx, 'session-rename-bad', 1)
  78. // U+200B passes a client-side trim gate but normalizes to empty host-side.
  79. const response = await remote(ctx).rename(request({ sessionId: source.id, title: ' ​ ' }))
  80. expect(response.ok).toBe(false)
  81. if (!response.ok) {
  82. expect(response.error).toMatchObject({
  83. code: 'session/title-invalid',
  84. details: { sessionId: source.id },
  85. })
  86. // The message renders verbatim in the rename dialog's alert.
  87. expect(response.error.message).toBe('session title must contain visible characters')
  88. }
  89. })
  90. it('maps a non-validation rename failure (stale session object) to internal, not title-invalid', async () => {
  91. const ctx = await composed()
  92. // The registered agent holds a session object from another store: the
  93. // title service's liveness check throws a plain Error, which must not
  94. // read as the user's fault.
  95. const foreign = await composed(false)
  96. const stale = liveAgent(foreign, 'session-rename-stale', 1)
  97. ctx.agents.register({ id: stale.id, session: stale, status: 'idle', ctx } as Agent)
  98. const response = await remote(ctx).rename(request({ sessionId: stale.id, title: 'name' }))
  99. expect(response.ok).toBe(false)
  100. if (!response.ok) expect(response.error.code).toBe('gateway/internal')
  101. })
  102. it('answers internal when the composition mounts no session-title service', async () => {
  103. const ctx = await composed(false)
  104. const source = liveAgent(ctx, 'session-no-titles', 1)
  105. const response = await remote(ctx).rename(request({ sessionId: source.id, title: 'name' }))
  106. expect(response.ok).toBe(false)
  107. if (!response.ok) {
  108. expect(response.error.code).toBe('gateway/internal')
  109. expect(response.error.message).toMatch(/mounts no session-title service/)
  110. }
  111. })
  112. })