refactor(session): fold the session family into packages/session/

git mv the 12 packages from session-persistence/, session-projection/,
session-title/, and telemetry/ into one session/ group per the
regrouping RFC; merge the four group READMEs into one bilingual
triplet; rewrite the group segment in tsconfig references (intra-group
references shorten to ../<pkg>), tsconfig.base.json paths/globs,
knip.json keys, vitest include, gate scripts, and authored doc/note
citations; regenerate module graph, doc graphs, catalogs, and the
lockfile importer keys. No npm names change.

Full unit suite: 8779 passed; the 18 reported failures reproduce as
env flakes (ambient-proxy IPv6 tunneling, watched-dir inotify
timeouts under parallel load) — each passes in isolation with
NO_PROXY set, matching their known pre-existing behavior on master.
This commit is contained in:
Tianyi Cui
2026-07-30 01:52:06 +08:00
parent 645fcf5713
commit 7e445c3a67
220 changed files with 258 additions and 286 deletions

View File

@@ -0,0 +1,6 @@
# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each
# side as of the last confirmed-consistent state. Both languages carry equal authority;
# after editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write packages/session-title/session-title-first-message-llm/README.md
README.md: c24d4de1fa945d11b5a868cad88a94ed14ad3891
README.zh.md: 1f864b16c473654d0a52716cddbe26fe087860dc

View File

@@ -0,0 +1,28 @@
# @deepseek-ai/dsh-session-title-first-message-llm
English | [中文](README.zh.md)
Optional `ctx.sessionTitle` provider that summarizes the first eligible human message through `ctx.llm`. It registers the `first-message` cadence, runs automatically only when a fresh non-fork session first creates its fallback, and attributes the result to that message's exact seq. An automatic failure retains the fallback and is retried only through `ctx.sessionTitle.refresh()`.
The plugin uses the complete required [shared LLM configuration](../session-title-llm/README.md#configuration). Omit both `provider` and `model` to inherit the exact route from the current logged main request, or set both to route title generation independently.
## Model Experience
### First-message title request
#### What the model sees
The title model receives the shared title instruction and a JSON array containing only the first eligible human message. Later prompts and inherited fork history do not trigger another automatic call.
#### Token effect
At most one automatic auxiliary request is made for a fresh session, bounded by `maxInputBytes` and `maxOutputTokens`; explicit refreshes may make additional calls. The main agent request gains zero tokens.
#### KV Cache effect
No main-request invalidation. The auxiliary request uses the configured or logged route and has provider-specific cache behavior.
## Known Limitations and Deferred Work
- The first message alone may cease to represent a long-running session; use the all-messages provider when later prompts should retitle it.
- A fork keeps its inherited title and never runs this provider automatically, even when its seeded first message came from the parent.

View File

@@ -0,0 +1,28 @@
# @deepseek-ai/dsh-session-title-first-message-llm
[English](README.md) | 中文
可选的 `ctx.sessionTitle` 提供方,通过 `ctx.llm` 总结第一条符合条件的用户消息。它注册 `first-message` 节奏,只在全新非 fork 会话首次创建回退时自动运行,并将结果归因于该消息的确切 seq。自动失败会保留回退,之后只能通过 `ctx.sessionTitle.refresh()` 重试。
该插件使用完整且必填的[共享大语言模型(LLM)配置](../session-title-llm/README.md#configuration)。同时省略 `provider` 与 `model` 时,会继承当前已记录主请求的确切路由;也可以同时设置二者,使标题生成使用独立路由。
## 模型体验
### 首消息标题请求
#### 模型看到的内容
标题模型会收到共享标题指令,以及一个只包含第一条符合条件用户消息的 JSON 数组。后续提示词与继承的 fork 历史不会触发再次自动调用。
#### Token 影响
全新会话最多自动发出一次辅助请求,并受 `maxInputBytes` 和 `maxOutputTokens` 约束;显式刷新可能发出额外调用。主 agent(智能体)请求不会增加 token。
#### KV Cache 影响
不会使主请求的 KV Cache 失效。辅助请求使用已配置或已记录路由,其缓存行为由提供方决定。
## 已知限制与暂缓事项
- 对于长期会话,第一条消息可能不再具有代表性;如果后续提示词应触发重新生成标题,请使用全消息提供方。
- fork 会保留继承的标题,绝不会自动运行此提供方,即使其预置的首消息来自父会话。

View File

@@ -0,0 +1,44 @@
{
"name": "@deepseek-ai/dsh-session-title-first-message-llm",
"description": "First-message LLM provider plugin for DeepSeek Harness session titles",
"version": "0.0.1",
"private": true,
"type": "module",
"main": "lib/index.js",
"types": "lib/types/index.d.ts",
"exports": {
".": {
"types": "./lib/types/index.d.ts",
"default": "./lib/index.js"
},
"./invariant": {
"types": "./lib/types/invariant.d.ts",
"default": "./lib/invariant.js"
},
"./package.json": "./package.json"
},
"files": ["lib/index.js", "lib/invariant.js", "lib/types/**/*.d.ts"],
"license": "BSD-3-Clause",
"peerDependencies": {
"@deepseek-ai/dsh-invariants": "^0.0.1",
"@deepseek-ai/dsh-llm": "^0.0.1",
"@deepseek-ai/dsh-session": "^0.0.1",
"@deepseek-ai/dsh-session-title": "^0.0.1",
"@deepseek-ai/dsh-session-title-llm": "^0.0.1",
"cordis": "^4.0.0-rc.7"
},
"dependencies": {
"schemastery": "^3.18.0"
},
"devDependencies": {
"@cordisjs/plugin-include": "workspace:^",
"@cordisjs/plugin-loader": "workspace:^",
"@deepseek-ai/dsh-invariants": "workspace:^",
"@deepseek-ai/dsh-llm": "workspace:^",
"@deepseek-ai/dsh-llm-deepseek": "workspace:^",
"@deepseek-ai/dsh-session": "workspace:^",
"@deepseek-ai/dsh-session-title": "workspace:^",
"@deepseek-ai/dsh-session-title-llm": "workspace:^",
"cordis": "^4.0.0-rc.7"
}
}

View File

@@ -0,0 +1,40 @@
/** First-human-message model provider for `ctx.sessionTitle`. */
import type { Context } from 'cordis'
import z from 'schemastery'
import {
registerSessionTitleLlmProvider,
SessionTitleLlmConfigFields,
} from '@deepseek-ai/dsh-session-title-llm'
import type { SessionTitleLlmConfig } from '@deepseek-ai/dsh-session-title-llm'
export const name = 'session-title-first-message-llm'
export const inject = ['sessionTitle', 'llm', 'sessions']
/** Required LLM policy; this plugin adds no defaults. */
export type Config = SessionTitleLlmConfig
/** Loader schema shared with the all-messages provider. */
/* jscpd:ignore-start -- Loader requires each plugin to export its own statically walkable schema; the field validators remain shared. */
export const Config: z<Config> = z.object({
targetWords: SessionTitleLlmConfigFields.targetWords,
targetCjkCharacters: SessionTitleLlmConfigFields.targetCjkCharacters,
maxInputBytes: SessionTitleLlmConfigFields.maxInputBytes,
maxOutputTokens: SessionTitleLlmConfigFields.maxOutputTokens,
timeoutMs: SessionTitleLlmConfigFields.timeoutMs,
provider: SessionTitleLlmConfigFields.provider,
model: SessionTitleLlmConfigFields.model,
})
/* jscpd:ignore-end */
/**
* Register the first-message model provider.
* @param ctx - context exposing session-title, LLM, and session services.
* @param config - required route, target, byte, token, and timeout policy.
*/
export function apply(ctx: Context, config: Config): void {
registerSessionTitleLlmProvider(ctx, config, name, 'first-message', (messages) => {
const first = messages[0]
if (first === undefined) throw new Error('first-message title provider requires one human message')
return [first]
})
}

View File

@@ -0,0 +1,30 @@
/**
* Package-owned invariant companion for `@deepseek-ai/dsh-session-title-first-message-llm`.
* @module @deepseek-ai/dsh-session-title-first-message-llm/invariant
*/
/* jscpd:ignore-start */
import type { Context } from 'cordis'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
const PACKAGE_NAME = '@deepseek-ai/dsh-session-title-first-message-llm'
/** Cordis companion plugin name. */
export const name = 'session-title-first-message-llm-invariant'
/** Service required before the companion can reserve package ownership. */
export const inject = ['invariants']
/**
* No runtime invariant: this thin provider delegates request and result validation to the shared
* title service and LLM helper and retains no independent mutable state.
*/
const install: InvariantInstaller = () => {}
/**
* Register this package's invariant companion.
* @param ctx - Cordis context carrying the invariant service.
* @returns the installed registration's disposer after setup succeeds.
*/
export const apply = (ctx: Context): Promise<() => void> =>
Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install))
/* jscpd:ignore-end */

View File

@@ -0,0 +1,119 @@
import { afterEach, describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import Loader from '@cordisjs/plugin-loader'
import Include from '@cordisjs/plugin-include'
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import LlmService, { createUserMessage, LlmAdapter } from '@deepseek-ai/dsh-llm'
import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
import SessionTitleService from '@deepseek-ai/dsh-session-title'
import * as providerPlugin from '@deepseek-ai/dsh-session-title-first-message-llm'
let root: string | undefined
let context: Context | undefined
class LoaderAdapter extends LlmAdapter {
readonly requests: GenerateOptions[] = []
override async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
this.requests.push(options)
yield { type: 'text-delta', index: 0, text: 'Loader composed title' }
yield { type: 'finish', reason: { kind: 'stop' } }
}
}
afterEach(async () => {
await context?.fiber.dispose()
context = undefined
if (root !== undefined) await rm(root, { recursive: true, force: true })
root = undefined
})
async function loadComposition(): Promise<Context> {
root = await mkdtemp(join(tmpdir(), 'dsh-title-loader-'))
const configPath = join(root, 'cordis.yml')
await writeFile(configPath, [
"- name: '@deepseek-ai/dsh-llm'",
"- name: '@deepseek-ai/dsh-session'",
"- name: '@deepseek-ai/dsh-session-title'",
' config:',
' fallbackMaxWords: 5',
' fallbackMaxBytes: 40',
' maxTitleBytes: 80',
"- name: '@deepseek-ai/dsh-session-title-first-message-llm'",
' config:',
' targetWords: 5',
' targetCjkCharacters: 10',
' maxInputBytes: 1000',
' maxOutputTokens: 32',
' timeoutMs: 1000',
" provider: 'title-route'",
" model: 'title-model'",
'',
].join('\n'))
context = new Context()
context.baseUrl = pathToFileURL(root).href + '/'
await context.plugin(Loader)
context.loader.builtins.include = Include
const modules = new Map<string, unknown>([
['@deepseek-ai/dsh-llm', LlmService],
['@deepseek-ai/dsh-session', SessionStore],
['@deepseek-ai/dsh-session-title', SessionTitleService],
['@deepseek-ai/dsh-session-title-first-message-llm', providerPlugin],
])
context.loader.internal = {
version: 'v2',
async import(specifier: string) {
if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
return modules.get(specifier)
},
} as unknown as NonNullable<typeof context.loader.internal>
await context.loader.create({
name: 'cordis:include',
config: { path: pathToFileURL(configPath).href },
})
await context.loader.await()
return context
}
describe('session-title Loader composition', () => {
it('loads the service and one model provider with required deployment policy', async () => {
const ctx = await loadComposition()
const unloaded = [...ctx.loader.entries()]
.filter(entry => entry.fiber === undefined && !entry.disabled)
.map(entry => entry.options.name)
expect(unloaded).toEqual([])
const adapter = new LoaderAdapter()
ctx.llm.registerAdapter(['title-route'], adapter)
const session = ctx.sessions.create(SessionId('loader-title'))
session.append('turn/start', {
turn: 1,
})
const message = session.append('user/message', createUserMessage({
content: [{ type: 'text', text: 'Compose a title through Loader' }],
source: { kind: 'user' },
}), { surfaceOp: 'append' })
await new Promise(resolve => setTimeout(resolve, 0))
session.append('request/header', {
header: { config: { provider: 'main-route', model: 'main-model' } },
reason: 'initial',
})
await new Promise(resolve => setTimeout(resolve, 0))
expect(adapter.requests[0]).toMatchObject({ provider: 'title-route', model: 'title-model' })
expect(ctx.sessionTitle.get(session)).toMatchObject({
title: 'Loader composed title',
messageSeqs: [message.seq],
source: {
kind: 'provider',
provider: 'session-title-first-message-llm',
model: { provider: 'title-route', model: 'title-model' },
},
})
})
})

View File

@@ -0,0 +1,59 @@
import { createUserMessage } from '@deepseek-ai/dsh-llm'
import { afterEach, describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import LlmService from '@deepseek-ai/dsh-llm'
import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
import SessionTitleService from '@deepseek-ai/dsh-session-title'
import * as FirstMessageTitleProvider from '@deepseek-ai/dsh-session-title-first-message-llm'
const contexts: Context[] = []
afterEach(async () => {
await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
})
describe.skipIf(!process.env.DEEPSEEK_API_KEY)('first-message title provider with real DeepSeek API', () => {
it('replaces the fallback with a short model title', async () => {
const ctx = new Context()
contexts.push(ctx)
await ctx.plugin(LlmService)
await ctx.plugin(LlmDeepSeek, { thinking: 'disabled' })
await ctx.plugin(SessionStore)
await ctx.plugin(SessionTitleService, {
fallbackMaxWords: 5,
fallbackMaxBytes: 40,
maxTitleBytes: 80,
})
await ctx.plugin(FirstMessageTitleProvider, {
targetWords: 5,
targetCjkCharacters: 10,
maxInputBytes: 4_096,
maxOutputTokens: 64,
timeoutMs: 60_000,
provider: 'deepseek-official',
model: 'deepseek-v4-flash',
})
const session = ctx.sessions.create(SessionId('real-title-provider'))
session.append('turn/start', {
turn: 1,
})
const message = session.append('user/message', createUserMessage({
content: [{ type: 'text', text: 'Explain why append-only logs make session titles durable.' }],
source: { kind: 'user' },
}), { surfaceOp: 'append' })
const title = await ctx.sessionTitle.refresh(session)
expect(title).toMatchObject({
messageSeqs: [message.seq],
source: {
kind: 'provider',
provider: 'session-title-first-message-llm',
model: { provider: 'deepseek-official', model: 'deepseek-v4-flash' },
},
})
expect(title?.title.length).toBeGreaterThan(0)
expect(Buffer.byteLength(title?.title ?? '', 'utf8')).toBeLessThanOrEqual(80)
})
})

View File

@@ -0,0 +1,86 @@
import { Context } from 'cordis'
import { describe, expect, it, vi } from 'vitest'
import LlmService, { createUserMessage, LlmAdapter } from '@deepseek-ai/dsh-llm'
import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session'
import SessionTitleService, { type SessionTitleProvider } from '@deepseek-ai/dsh-session-title'
import * as providerPlugin from '@deepseek-ai/dsh-session-title-first-message-llm'
class RecordingAdapter extends LlmAdapter {
readonly requests: GenerateOptions[] = []
override async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
this.requests.push(options)
yield { type: 'text-delta', index: 0, text: 'First-message model title' }
yield { type: 'finish', reason: { kind: 'stop' } }
}
}
const TITLE_CONFIG = { fallbackMaxWords: 5, fallbackMaxBytes: 40, maxTitleBytes: 80 } as const
const LLM_CONFIG = {
targetWords: 5,
targetCjkCharacters: 10,
maxInputBytes: 1_000,
maxOutputTokens: 32,
timeoutMs: 1_000,
provider: 'title-route',
model: 'title-model',
} as const
async function settle(): Promise<void> {
await new Promise(resolve => setTimeout(resolve, 0))
}
describe('first-message LLM title provider', () => {
it('rejects an impossible empty provider request at its own boundary', async () => {
const ctx = new Context()
await ctx.plugin(LlmService)
await ctx.plugin(SessionStore)
await ctx.plugin(SessionTitleService, TITLE_CONFIG)
let registered: SessionTitleProvider | undefined
vi.spyOn(ctx.sessionTitle, 'register').mockImplementation((provider) => {
registered = provider
return async () => undefined
})
providerPlugin.apply(ctx, LLM_CONFIG)
await expect(registered!.generate({
session: Session.create(SessionId('empty-first-provider')),
messages: [],
signal: new AbortController().signal,
})).rejects.toThrow(/requires one human message/)
})
it('always selects only the first eligible human message, including explicit refresh', async () => {
const ctx = new Context()
await ctx.plugin(LlmService)
await ctx.plugin(SessionStore)
await ctx.plugin(SessionTitleService, TITLE_CONFIG)
const adapter = new RecordingAdapter()
ctx.llm.registerAdapter(['title-route'], adapter)
await ctx.plugin(providerPlugin, LLM_CONFIG)
const session = ctx.sessions.create(SessionId('first-plugin'))
session.append('turn/start', { turn: 1 })
const first = session.append('user/message', createUserMessage({
content: [{ type: 'text', text: 'first input' }], source: { kind: 'user' },
}), { surfaceOp: 'append' })
await settle()
session.append('request/header', {
header: { config: { provider: 'main', model: 'main-model' } }, reason: 'initial',
})
await settle()
session.append('user/message', createUserMessage({
content: [{ type: 'text', text: 'second input must be ignored' }], source: { kind: 'user' },
}), { surfaceOp: 'append' })
await ctx.sessionTitle.refresh(session)
expect(adapter.requests).toHaveLength(2)
for (const options of adapter.requests) {
const content = options.messages[0]?.content[0]
expect(content?.type === 'text' && content.text).toContain('first input')
expect(content?.type === 'text' && content.text).not.toContain('second input must be ignored')
}
expect(ctx.sessionTitle.get(session)).toMatchObject({ messageSeqs: [first.seq] })
})
})

View File

@@ -0,0 +1,14 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": { "rootDir": "src", "outDir": "lib/types" },
"include": ["src"],
"references": [
{ "path": "../../../vendor/cosmokit" },
{ "path": "../../../vendor/cordis" },
{ "path": "../../../vendor/schemastery" },
{ "path": "../../support/invariants" },
{ "path": "../../llm/llm" },
{ "path": "../session-title" },
{ "path": "../session-title-llm" }
]
}