2026-07-29 18:59:23 +08:00
|
|
|
|
/**
|
2026-08-22 21:10:44 +08:00
|
|
|
|
* Session Controller rename delegation through the composed SessionTitleService. The
|
2026-07-29 18:59:23 +08:00
|
|
|
|
* agent factory is a structural stub whose createAgent forwards seed/meta into
|
|
|
|
|
|
* the real SessionStore, and whose resume never runs (every source here is
|
2026-07-29 20:11:19 +08:00
|
|
|
|
* already attached). Cold-session resolution is the shared `agentFor` path —
|
2026-08-22 21:10:44 +08:00
|
|
|
|
* remote-proxy-cold.spec.ts owns the resume evidence for every unary that rides
|
2026-07-29 20:11:19 +08:00
|
|
|
|
* it, rename included.
|
2026-07-29 18:59:23 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { describe, expect, it } from 'vitest'
|
build(vendor): rescope the vendored Cordis packages into @deepseek-ai
Machine-produced by `pnpm run rescope-vendor --apply` plus the regeneration it
prints: `pnpm install` for the lockfile, `pnpm run gen-third-party-notices`,
`verify-translation-pairing --write` for the touched bilingual pairs,
`gen-doc-graphs`, and one typert snapshot whose ids embed character offsets.
`pnpm run rescope-vendor --check` verifies the result.
Renames nine vendored packages (cordis, cosmokit, schemastery and the six
@cordisjs plugins) and every reference that resolves them: manifest names and
dependency keys, module specifiers including declare-module merges, cordis.yml
plugin names, tsconfig paths, every Markdown fence, and `docs/` prose.
Directory names, upstream versions, and dependency ranges are unchanged, so
vendor/README.md still reads as an upstream snapshot; its manifest table gains
an upstream-name column so THIRD_PARTY_NOTICES keeps MIT attribution pointed
at each fork's origin.
The tutorial tier follows the rename end to end: its yaml fences named plugins
the Loader can no longer resolve, its `ts ignore-check` fences disagreed with
the compiled fences beside them, and its prose quoted both. The contracts that
told readers to keep upstream names — the root convention and the vendoring
cookbook's tree comment and manifest invariant — now say to rescope instead.
Two rules read `@deepseek-ai/` as "another workspace plugin": the client bundle
purity gate now names the vendored libraries a browser bundle inlines, and the
files where a bare `cordis` is an agent-preset id keep that product data.
2026-08-10 22:04:06 +08:00
|
|
|
|
import { Context } from '@deepseek-ai/cordis'
|
2026-07-29 18:59:23 +08:00
|
|
|
|
import SessionStore from '@deepseek-ai/dsh-session'
|
|
|
|
|
|
import AgentRegistry from '@deepseek-ai/dsh-agent'
|
|
|
|
|
|
import type { Agent, AgentHandle, CreateAgentOptions } from '@deepseek-ai/dsh-agent'
|
|
|
|
|
|
import { createUserMessage } from '@deepseek-ai/dsh-llm'
|
|
|
|
|
|
import SessionTitleService from '@deepseek-ai/dsh-session-title'
|
|
|
|
|
|
import type { Session, SessionId } from '@deepseek-ai/dsh-session'
|
2026-08-22 21:10:44 +08:00
|
|
|
|
import { createSessionTestRemote } from './test-remote.ts'
|
2026-07-29 18:59:23 +08:00
|
|
|
|
|
|
|
|
|
|
const sid = (id: string): SessionId => id as SessionId
|
|
|
|
|
|
|
2026-08-22 21:10:44 +08:00
|
|
|
|
function request<P>(payload: P): P {
|
|
|
|
|
|
return payload
|
2026-07-29 18:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function composed(withTitles = true): Promise<Context> {
|
|
|
|
|
|
const ctx = new Context()
|
|
|
|
|
|
await ctx.plugin(SessionStore)
|
|
|
|
|
|
await ctx.plugin(AgentRegistry)
|
|
|
|
|
|
if (withTitles) {
|
|
|
|
|
|
await ctx.plugin(SessionTitleService, { fallbackMaxWords: 5, fallbackMaxBytes: 40, maxTitleBytes: 40 })
|
|
|
|
|
|
}
|
|
|
|
|
|
// Store-backed structural factory: create builds the session with the
|
|
|
|
|
|
// forwarded seed/meta (the store validates the balanced prefix) and
|
|
|
|
|
|
// registers an idle agent stub over it.
|
|
|
|
|
|
ctx.agents.setFactory({
|
|
|
|
|
|
createAgent: (ownerCtx: Context, options: CreateAgentOptions): Promise<AgentHandle> => {
|
|
|
|
|
|
const session = ctx.sessions.create(options.sessionId, {
|
|
|
|
|
|
...options.seed === undefined ? {} : { seed: [...options.seed] },
|
|
|
|
|
|
...options.meta === undefined ? {} : { meta: options.meta },
|
|
|
|
|
|
})
|
|
|
|
|
|
const agent = { id: session.id, session, status: 'idle', ctx: ownerCtx } as Agent
|
|
|
|
|
|
ctx.agents.register(agent)
|
|
|
|
|
|
return Promise.resolve({ agent, dispose: () => Promise.resolve() })
|
|
|
|
|
|
},
|
|
|
|
|
|
resume: () => Promise.reject(new Error('resume must not run: every source is attached')),
|
|
|
|
|
|
})
|
|
|
|
|
|
return ctx
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Register one live agent whose log holds `turns` completed turns. */
|
|
|
|
|
|
function liveAgent(ctx: Context, id: string, turns: number): Session {
|
|
|
|
|
|
const session = ctx.sessions.create(sid(id), { meta: { cwd: '/proj' } })
|
|
|
|
|
|
for (let turn = 1; turn <= turns; turn++) {
|
2026-07-30 17:28:03 +08:00
|
|
|
|
session.append('turn/start', { turn })
|
2026-07-29 18:59:23 +08:00
|
|
|
|
session.append('user/message', createUserMessage({
|
|
|
|
|
|
content: [{ type: 'text', text: `prompt ${String(turn)}` }],
|
|
|
|
|
|
source: { kind: 'user' },
|
|
|
|
|
|
}), { surfaceOp: 'append' })
|
2026-08-04 14:09:52 +08:00
|
|
|
|
session.append('turn/end', { turn, reason: { kind: 'completed' } })
|
2026-07-29 18:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
ctx.agents.register({ id: session.id, session, status: 'idle', ctx } as Agent)
|
|
|
|
|
|
return session
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-22 21:10:44 +08:00
|
|
|
|
const remote = (ctx: Context) => createSessionTestRemote(ctx, { defaultModelSelection: () => ({ provider: 'p', model: 'm' }), cwd: '/tmp' })
|
2026-07-29 18:59:23 +08:00
|
|
|
|
|
|
|
|
|
|
describe('sessions.rename', () => {
|
|
|
|
|
|
it('accepts through the composed title service: normalized user-source event, echoed seq', async () => {
|
|
|
|
|
|
const ctx = await composed()
|
|
|
|
|
|
const source = liveAgent(ctx, 'session-rename', 1)
|
|
|
|
|
|
|
2026-08-22 21:10:44 +08:00
|
|
|
|
const renamed = await remote(ctx).rename(request({ sessionId: source.id, title: ' new name ' }))
|
|
|
|
|
|
expect(renamed.ok).toBe(true)
|
|
|
|
|
|
if (!renamed.ok) return
|
|
|
|
|
|
expect(renamed.value.title).toBe('new name')
|
2026-07-29 18:59:23 +08:00
|
|
|
|
const event = source.events.findLast(item => item.type === 'session/title')
|
2026-08-22 21:10:44 +08:00
|
|
|
|
expect(event?.seq).toBe(renamed.value.seq)
|
2026-07-29 18:59:23 +08:00
|
|
|
|
expect(event?.data).toMatchObject({ title: 'new name', source: { kind: 'user' } })
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-07-29 20:11:19 +08:00
|
|
|
|
it('maps only an empty-normalizing title to title-invalid, with a presentable message', async () => {
|
2026-07-29 18:59:23 +08:00
|
|
|
|
const ctx = await composed()
|
|
|
|
|
|
const source = liveAgent(ctx, 'session-rename-bad', 1)
|
|
|
|
|
|
|
2026-07-29 20:11:19 +08:00
|
|
|
|
// U+200B passes a client-side trim gate but normalizes to empty host-side.
|
2026-08-22 21:10:44 +08:00
|
|
|
|
const response = await remote(ctx).rename(request({ sessionId: source.id, title: ' ' }))
|
|
|
|
|
|
expect(response.ok).toBe(false)
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
expect(response.error).toMatchObject({
|
2026-07-29 18:59:23 +08:00
|
|
|
|
code: 'title-invalid',
|
|
|
|
|
|
details: { sessionId: source.id },
|
|
|
|
|
|
})
|
2026-07-29 20:11:19 +08:00
|
|
|
|
// The message renders verbatim in the rename dialog's alert.
|
2026-08-22 21:10:44 +08:00
|
|
|
|
expect(response.error.message).toBe('session title must contain visible characters')
|
2026-07-29 18:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-07-29 20:11:19 +08:00
|
|
|
|
it('maps a non-validation rename failure (stale session object) to internal, not title-invalid', async () => {
|
|
|
|
|
|
const ctx = await composed()
|
|
|
|
|
|
// The registered agent holds a session object from another store: the
|
|
|
|
|
|
// title service's liveness check throws a plain Error, which must not
|
|
|
|
|
|
// read as the user's fault.
|
|
|
|
|
|
const foreign = await composed(false)
|
|
|
|
|
|
const stale = liveAgent(foreign, 'session-rename-stale', 1)
|
|
|
|
|
|
ctx.agents.register({ id: stale.id, session: stale, status: 'idle', ctx } as Agent)
|
|
|
|
|
|
|
2026-08-22 21:10:44 +08:00
|
|
|
|
const response = await remote(ctx).rename(request({ sessionId: stale.id, title: 'name' }))
|
|
|
|
|
|
expect(response.ok).toBe(false)
|
|
|
|
|
|
if (!response.ok) expect(response.error.code).toBe('internal')
|
2026-07-29 20:11:19 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2026-07-29 18:59:23 +08:00
|
|
|
|
it('answers internal when the composition mounts no session-title service', async () => {
|
|
|
|
|
|
const ctx = await composed(false)
|
|
|
|
|
|
const source = liveAgent(ctx, 'session-no-titles', 1)
|
|
|
|
|
|
|
2026-08-22 21:10:44 +08:00
|
|
|
|
const response = await remote(ctx).rename(request({ sessionId: source.id, title: 'name' }))
|
|
|
|
|
|
expect(response.ok).toBe(false)
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
expect(response.error.code).toBe('internal')
|
|
|
|
|
|
expect(response.error.message).toMatch(/mounts no session-title service/)
|
2026-07-29 18:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|