diff --git a/src/sidechat-routes.ts b/src/sidechat-routes.ts index 8285520a..f1c1705e 100644 --- a/src/sidechat-routes.ts +++ b/src/sidechat-routes.ts @@ -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), diff --git a/tests/host-runtime-imports.spec.ts b/tests/host-runtime-imports.spec.ts new file mode 100644 index 00000000..b3c1271c --- /dev/null +++ b/tests/host-runtime-imports.spec.ts @@ -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') + }) +})