session-rename.host.spec.ts 5.5 KB

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