Merge pull request #641 from ZhangFengshun/fix/host-load-without-session-log-offset-runtime

fix(host): DSH Desktop 上主机半边无法加载——品牌戳改用类型断言,不再依赖运行时 SessionLogOffset 导出
This commit is contained in:
Atlas Zeng authored and GitHub committed 2026-10-05 00:55:59 +08:00
commit 8babb117fe
2 files changed
+75 -3

No files matched your search

+7 -3
View File
@@ -26,8 +26,7 @@ import { createUserMessage, ReasoningEffortId, type ContentBlock, type UserMessa
import type { Agent, AgentSetup, CreateAgentOptions, ResumeAgentOptions } from '@deepseek-ai/dsh-agent'
import { snapshotSubagentDescriptor } from '@deepseek-ai/dsh-subagent'
import type { Context as CordisContext } from '@deepseek-ai/cordis'
import type { SessionEvent, SessionId } from '@deepseek-ai/dsh-session'
import { SessionLogOffset } from '@deepseek-ai/dsh-session'
import type { SessionEvent, SessionId, SessionLogOffset } from '@deepseek-ai/dsh-session'
import type {
Context,
SidebarAgentPresetsService,
@@ -315,7 +314,12 @@ export function buildSidechatApi(ctx: Context, live?: AssistantLiveBuffer): Side
...(agentPreset === undefined ? {} : { agentPreset }),
},
seed: seed as unknown as readonly SessionEvent[],
inheritedEventCount: SessionLogOffset(seed.length),
// Branded number, compile-time only: DSH Desktop's plugin-facing
// `@deepseek-ai/dsh-session` surface carries the type but not the runtime
// stamp, so importing the value aborts the whole host half at load time.
// `Array#length` is a non-negative safe integer by construction — exactly
// what the upstream stamp asserts — so the cast is lossless here.
inheritedEventCount: seed.length as SessionLogOffset,
agentOptions: childAgentOptions,
setup,
signal: AbortSignal.timeout(CREATE_TIMEOUT_MS),
+68
View File
@@ -0,0 +1,68 @@
/**
* Host runtime-import guard: DSH Desktop serves profile plugins a prebuilt
* module surface, and that surface does not necessarily carry every symbol the
* published npm package exports. `@deepseek-ai/dsh-session` is the known case —
* it exposes `SessionLogOffset` as a *type* (the branded-number declaration)
* while the runtime stamp is absent, so a value import such as
*
* import { SessionLogOffset } from '@deepseek-ai/dsh-session'
*
* fails ESM instantiation with "does not provide an export named
* 'SessionLogOffset'" and takes the whole host half down with it (no fs, git,
* terminal or jobs routes; the sidebar Files tab then only renders the
* "nothing can view this" fallback). The branded number is compile-time only,
* so host sources must import it with `import type` and cast at the use site.
*
* This spec pins that rule for every `@deepseek-ai/dsh-session` import under
* `src/`. Tests may still value-import the symbol: the vitest tree installs the
* full npm package, which does provide the runtime stamp.
*/
import { describe, expect, it } from 'vitest'
import { readFileSync, readdirSync, statSync } from 'node:fs'
import { join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)))
const SRC = resolve(ROOT, 'src')
/** The package whose plugin-facing surface is type-only for the listed symbols. */
const TYPE_ONLY_PACKAGE = '@deepseek-ai/dsh-session'
/** Every `.ts` / `.tsx` file under a directory, recursively. */
function collectSources(dir: string): string[] {
const out: string[] = []
for (const entry of readdirSync(dir)) {
const full = join(dir, entry)
if (statSync(full).isDirectory()) out.push(...collectSources(full))
else if (entry.endsWith('.ts') || entry.endsWith('.tsx')) out.push(full)
}
return out
}
/** Import statements that pull the package in without the `type` modifier. */
function valueImportsOf(source: string): string[] {
const pattern = new RegExp(
String.raw`^\s*import\s+(?!type\b)(?:[^'"]*?\sfrom\s+)?['"]${TYPE_ONLY_PACKAGE.replace(/[/@.]/g, (c) => `\\${c}`)}['"]`,
'gm',
)
return [...source.matchAll(pattern)].map((match) => match[0].trim())
}
describe('host runtime imports', () => {
it(`never value-imports ${TYPE_ONLY_PACKAGE} from src/`, () => {
const offenders: string[] = []
for (const file of collectSources(SRC)) {
for (const statement of valueImportsOf(readFileSync(file, 'utf8'))) {
offenders.push(`${file.slice(ROOT.length + 1)}: ${statement}`)
}
}
expect(offenders).toEqual([])
})
it('still imports the branded type it needs', () => {
const source = readFileSync(resolve(SRC, 'sidechat-routes.ts'), 'utf8')
expect(source).toContain(`import type { SessionEvent, SessionId, SessionLogOffset } from '${TYPE_ONLY_PACKAGE}'`)
// The cast carries the brand; the runtime stamp is what the host surface lacks.
expect(source).toContain('inheritedEventCount: seed.length as SessionLogOffset')
})
})