DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(dsh-tauri-ui): 未分组新会话落到 DSH_HOME/ungrouped,不再继承核心安装目录
hairyfd0662901add9
diff --git a/packages/dsh-tauri-ui/src/client/apis/index.ts b/packages/dsh-tauri-ui/src/client/apis/index.tsindex bc184663..6d978a03 100644--- a/packages/dsh-tauri-ui/src/client/apis/index.ts+++ b/packages/dsh-tauri-ui/src/client/apis/index.ts@@ -39,3 +39,8 @@ export function getPresets(params?: Types.GetPresetsQuery, options?: FetchOptionexport function postSessionResume(body: Types.PostSessionResumeBody, options?: FetchOptions) {return ofetch<Types.SessionResumeResponse>("/session/resume", { baseURL, method: "post", body, ...options });}++/** @method get */+export function getUngrouped(options?: FetchOptions) {+ return ofetch<Types.UngroupedResponse>("/ungrouped", { baseURL, method: "get", ...options });+}diff --git a/packages/dsh-tauri-ui/src/client/apis/index.type.ts b/packages/dsh-tauri-ui/src/client/apis/index.type.tsindex a640bf4d..2204c28d 100644--- a/packages/dsh-tauri-ui/src/client/apis/index.type.ts+++ b/packages/dsh-tauri-ui/src/client/apis/index.type.ts@@ -43,6 +43,10 @@ export type PresetsResponse = {presets?: Record<string, readonly number[]>;error?: string;};+export type UngroupedResponse = {+ cwd?: string;+ error?: string;+};export interface EditorPreferenceBody {preference: EditorPreference;diff --git a/packages/dsh-tauri-ui/src/client/service/ungrouped-session.test.ts b/packages/dsh-tauri-ui/src/client/service/ungrouped-session.test.tsindex 37e6caa8..698f3b23 100644--- a/packages/dsh-tauri-ui/src/client/service/ungrouped-session.test.ts+++ b/packages/dsh-tauri-ui/src/client/service/ungrouped-session.test.ts@@ -1,20 +1,29 @@/*** service/ungrouped-session.test.ts — 「未分组」新建会话的领域契约回归测试。*- * 锁住的契约:`sessions.create()` 必须**不带** workspaceId/cwd(宿主才会走默认 cwd 且不 attach- * 任何工作区,侧边栏据此把它归入「未分组」);当前会话已经是「未分组且空白」时复用而不新造;- * 失败只告警,不把 rejection 漏给 UI 调用点。+ * 锁住的契约:`sessions.create()` 必须**不带** workspaceId、只带宿主解析出的未分组目录+ * (宿主才会走该 cwd 且不 attach 任何工作区,侧边栏据此把它归入「未分组」,而不是把会话+ * 落进桌面壳进程的 cwd——核心安装目录);当前会话已是「未分组目录下的空白会话」时复用而+ * 不新造;目录解析失败时退回无参创建;失败只告警,不把 rejection 漏给 UI 调用点。*/import type { ClientContext, SessionId } from 'dsh-tauri/client'import { afterEach, describe, expect, it, vi } from 'vitest'+import { getUngrouped } from '../apis'import { startUngroupedSession } from './ungrouped-session'const UNGROUPED: SessionId = 'session-ungrouped' as SessionIdconst GROUPED: SessionId = 'session-grouped' as SessionId+const UNGROUPED_CWD = 'C:\\Users\\test\\.dsh\\ungrouped'+const OTHER_CWD = 'C:\\Users\\test\\AppData\\Roaming\\dsh-tauri\\dependencies\\dsh'++vi.mock('../apis', () => ({+ getUngrouped: vi.fn(async () => ({ cwd: UNGROUPED_CWD })),+}))interface CtxOptions {current?: SessionIdblank?: boolean+ cwd?: stringsessionIds?: readonly SessionId[]workspacesAvailable?: booleanworkspacesThrows?: boolean@@ -33,7 +42,7 @@ function createCtx(options: CtxOptions = {}) {current: options.current,byId: options.current === undefined? {}- : { [options.current]: { blank: options.blank ?? true } },+ : { [options.current]: { blank: options.blank ?? true, cwd: options.cwd ?? UNGROUPED_CWD } },}),},create,@@ -53,21 +62,21 @@ function createCtx(options: CtxOptions = {}) {}afterEach(() => {- vi.restoreAllMocks()+ vi.mocked(getUngrouped).mockImplementation(async () => ({ cwd: UNGROUPED_CWD }))})describe('startUngroupedSession', () => {- it('没有当前会话时新建(不传 workspaceId / cwd)并打开', async () => {+ it('没有当前会话时新建:只带未分组目录,不带 workspaceId', async () => {const { ctx, create, open, selectPanel } = createCtx()startUngroupedSession(ctx)await vi.waitFor(() => expect(open).toHaveBeenCalledWith('session-new'))- expect(create, '必须无参创建:带 workspaceId 就不再是「未分组」').toHaveBeenCalledWith()+ expect(create, '必须落到 $DSH_HOME/ungrouped,而不是桌面壳进程的 cwd').toHaveBeenCalledWith({ cwd: UNGROUPED_CWD })expect(selectPanel, '必须退出面板,落到会话视图').toHaveBeenCalledWith(null)})- it('当前会话已是「未分组且空白」时复用,不再新造', async () => {+ it('当前会话已是「未分组目录下的空白会话」时复用,不再新造', async () => {const { ctx, create, open } = createCtx({ current: UNGROUPED, blank: true })startUngroupedSession(ctx)@@ -76,13 +85,22 @@ describe('startUngroupedSession', () => {expect(create).not.toHaveBeenCalled()})+ it('当前空白会话的 cwd 是核心安装目录(历史遗留)时另起一条,不复用', async () => {+ const { ctx, create, open } = createCtx({ current: UNGROUPED, blank: true, cwd: OTHER_CWD })++ startUngroupedSession(ctx)++ await vi.waitFor(() => expect(open).toHaveBeenCalledWith('session-new'))+ expect(create).toHaveBeenCalledWith({ cwd: UNGROUPED_CWD })+ })+it('当前空白会话已归属某工作区时另起一条未分组会话', async () => {const { ctx, create, open } = createCtx({ current: GROUPED, blank: true, sessionIds: [GROUPED] })startUngroupedSession(ctx)await vi.waitFor(() => expect(open).toHaveBeenCalledWith('session-new'))- expect(create).toHaveBeenCalledTimes(1)+ expect(create).toHaveBeenCalledWith({ cwd: UNGROUPED_CWD })})it('当前会话非空白时另起一条未分组会话', async () => {@@ -111,6 +129,29 @@ describe('startUngroupedSession', () => {expect(create).not.toHaveBeenCalled()})+ it('未分组目录解析失败时退回无参创建(沿用核心默认目录),仍能开会话', async () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})+ const failure = new Error('route down')+ vi.mocked(getUngrouped).mockRejectedValue(failure)+ const { ctx, create, open } = createCtx()++ startUngroupedSession(ctx)++ await vi.waitFor(() => expect(open).toHaveBeenCalledWith('session-new'))+ expect(create).toHaveBeenCalledWith()+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('未分组目录解析失败'), failure)+ })++ it('未分组目录为空串时同样退回无参创建', async () => {+ vi.mocked(getUngrouped).mockImplementation(async () => ({ cwd: ' ' }))+ const { ctx, create, open } = createCtx()++ startUngroupedSession(ctx)++ await vi.waitFor(() => expect(open).toHaveBeenCalledWith('session-new'))+ expect(create).toHaveBeenCalledWith()+ })+it('创建失败只告警:不打开会话、不把 rejection 漏给调用点', async () => {const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})const failure = new Error('boom')diff --git a/packages/dsh-tauri-ui/src/client/service/ungrouped-session.ts b/packages/dsh-tauri-ui/src/client/service/ungrouped-session.tsindex 7037d5f9..127fa7c8 100644--- a/packages/dsh-tauri-ui/src/client/service/ungrouped-session.ts+++ b/packages/dsh-tauri-ui/src/client/service/ungrouped-session.ts@@ -1,14 +1,20 @@import type { ClientAdapter, ClientContext, SessionId } from 'dsh-tauri/client'import { PLUGIN_ID } from '../../shared/constants'+import { getUngrouped } from '../apis'/** 会话列表快照中本服务读取的字段(`current` 在 0.1.7 由适配层投影补回)。 */interface ListSnapshotLike {current?: SessionId- byId?: Record<string, { blank?: boolean } | undefined>+ byId?: Record<string, { blank?: boolean, cwd?: string } | undefined>}/**- * 未分组新建会话:不传 workspaceId/cwd,宿主按默认 cwd 建会话且不 attach 任何工作区。+ * 未分组新建会话:不传 workspaceId、只传宿主解析出的未分组目录(`$DSH_HOME/ungrouped`),+ * 宿主据此建会话且不 attach 任何工作区。+ *+ * 不显式传 `cwd` 时宿主退回 `process.cwd()`——桌面壳把该进程的 cwd 固定为核心安装目录+ * (版本切换时需持住目录句柄),未分组会话就会落进核心目录这一伪项目。目录由宿主在建会话+ * 前 `mkdir -p`,无需客户端预热;路由不可用时退回旧行为并告警。** `adapter` 负责跨内核代的会话能力:`open` 在 ≤0.1.6 位于 `sessions`,0.1.7 收进* `uiWorkspace.openSession`;列表快照的 `current` 在 0.1.7 也改由 `uiSession` 投影补回。@@ -27,20 +33,42 @@ export function startUngroupedSession(ctx: ClientContext, adapter?: ClientAdapte}async function connectUngroupedSession(ctx: ClientContext, adapter?: ClientAdapter): Promise<SessionId> {+ const cwd = await resolveUngroupedCwd()const current = snapshotOf(ctx, adapter).current- if (current !== undefined && isUngroupedBlank(ctx, adapter, current))+ if (current !== undefined && isUngroupedBlank(ctx, adapter, current, cwd))return current- return ctx.sessions.create()+ return cwd === undefined ? ctx.sessions.create() : ctx.sessions.create({ cwd })}-function isUngroupedBlank(ctx: ClientContext, adapter: ClientAdapter | undefined, sessionId: SessionId): boolean {+function isUngroupedBlank(+ ctx: ClientContext,+ adapter: ClientAdapter | undefined,+ sessionId: SessionId,+ cwd: string | undefined,+): boolean {const summary = snapshotOf(ctx, adapter).byId?.[sessionId]if (summary === undefined || !summary.blank)return false+ // 复用只认「已经落在未分组目录」的空白会话:核心目录 cwd 的历史会话不得被继续沿用。+ if (cwd !== undefined && summary.cwd !== cwd)+ return falseconst workspaces = readWorkspaces(ctx)return workspaces === undefined || workspaces.every(workspace => !workspace.sessionIds.includes(sessionId))}+/** 宿主解析未分组目录;取不到时按「无 cwd」处理,退回核心默认目录,绝不阻断开会话。 */+async function resolveUngroupedCwd(): Promise<string | undefined> {+ try {+ const response = await getUngrouped()+ const cwd = response?.cwd+ return typeof cwd === 'string' && cwd.trim().length > 0 ? cwd : undefined+ }+ catch (reason) {+ console.warn(`[${PLUGIN_ID}] 未分组目录解析失败,沿用核心默认目录:`, reason)+ return undefined+ }+}+/** 打开已有会话:适配层优先(0.1.7 走 `uiWorkspace.openSession`),缺席时回退原生成员。 */function openSession(ctx: ClientContext, adapter: ClientAdapter | undefined, sessionId: SessionId): void {if (adapter !== undefined) {diff --git a/packages/dsh-tauri-ui/src/host/routes/index.ts b/packages/dsh-tauri-ui/src/host/routes/index.tsindex 990c3352..0f1e5c23 100644--- a/packages/dsh-tauri-ui/src/host/routes/index.ts+++ b/packages/dsh-tauri-ui/src/host/routes/index.ts@@ -5,6 +5,7 @@ import openConfig from './config/open/post'import endpointModels from './endpoint/models/get'import presets from './presets/get'import resume from './session/resume/post'+import ungrouped from './ungrouped/get'export const routes = defineRoutes((disposer) => {disposer.post({ kind: 'exact', path: '/api/desktop/dsh-tauri-ui/session/resume' }, resume)@@ -13,4 +14,5 @@ export const routes = defineRoutes((disposer) => {disposer.post({ kind: 'exact', path: '/api/desktop/dsh-tauri-ui/config/open' }, openConfig)disposer.get({ kind: 'exact', path: '/api/desktop/dsh-tauri-ui/endpoint/models' }, endpointModels)disposer.get({ kind: 'exact', path: '/api/desktop/dsh-tauri-ui/presets' }, presets)+ disposer.get({ kind: 'exact', path: '/api/desktop/dsh-tauri-ui/ungrouped' }, ungrouped)})diff --git a/packages/dsh-tauri-ui/src/host/routes/index.types.ts b/packages/dsh-tauri-ui/src/host/routes/index.types.tsindex 9d1617fc..560c5abe 100644--- a/packages/dsh-tauri-ui/src/host/routes/index.types.ts+++ b/packages/dsh-tauri-ui/src/host/routes/index.types.ts@@ -50,3 +50,8 @@ export interface PresetsResponse {presets?: Record<string, readonly number[]>error?: string}++export interface UngroupedResponse {+ cwd?: string+ error?: string+}diff --git a/packages/dsh-tauri-ui/src/host/routes/ungrouped/get.ts b/packages/dsh-tauri-ui/src/host/routes/ungrouped/get.tsnew file mode 100644index 00000000..70a12335--- /dev/null+++ b/packages/dsh-tauri-ui/src/host/routes/ungrouped/get.ts@@ -0,0 +1,13 @@+import type { EventHandlerRequest } from 'dsh-tauri'+import type { UngroupedResponse } from '../index.types'+import { defineEventHandler } from 'dsh-tauri'+import { ungrouped } from '../../service/ungrouped'++export default defineEventHandler<EventHandlerRequest, UngroupedResponse>(() => {+ try {+ return { cwd: ungrouped.resolve() }+ }+ catch (error) {+ return { error: error instanceof Error ? error.message : String(error) }+ }+})diff --git a/packages/dsh-tauri-ui/src/host/service/ungrouped.ts b/packages/dsh-tauri-ui/src/host/service/ungrouped.tsnew file mode 100644index 00000000..d48cafb0--- /dev/null+++ b/packages/dsh-tauri-ui/src/host/service/ungrouped.ts@@ -0,0 +1,8 @@+import { defineService } from 'dsh-tauri'+import { resolveUngroupedSessionPath } from '../utils/paths'++export const ungrouped = defineService({+ resolve(): string {+ return resolveUngroupedSessionPath()+ },+})diff --git a/packages/dsh-tauri-ui/src/host/utils/paths.test.ts b/packages/dsh-tauri-ui/src/host/utils/paths.test.tsindex 4d4fa764..a8f49072 100644--- a/packages/dsh-tauri-ui/src/host/utils/paths.test.ts+++ b/packages/dsh-tauri-ui/src/host/utils/paths.test.ts@@ -1,7 +1,7 @@import { homedir } from 'node:os'import { join, resolve } from 'node:path'import { describe, expect, it } from 'vitest'-import { resolvePresetCachePath, resolveSettingsFilePath } from './paths'+import { resolvePresetCachePath, resolveSettingsFilePath, resolveUngroupedSessionPath } from './paths'describe('resolveSettingsFilePath', () => {it('uses a non-blank DSH_HOME', () => {@@ -27,3 +27,15 @@ describe('resolvePresetCachePath', () => {.toBe(resolve('D:\\harness', 'dsh-tauri-ui', 'model-presets.json'))})})++describe('resolveUngroupedSessionPath', () => {+ it('resolves to the ungrouped directory inside DSH_HOME, not the core install dir', () => {+ expect(resolveUngroupedSessionPath({ DSH_HOME: 'D:\\harness' } as NodeJS.ProcessEnv))+ .toBe(resolve('D:\\harness', 'ungrouped'))+ })++ it('defaults to ~/.dsh/ungrouped when DSH_HOME is unset', () => {+ expect(resolveUngroupedSessionPath({} as NodeJS.ProcessEnv))+ .toBe(join(homedir(), '.dsh', 'ungrouped'))+ })+})diff --git a/packages/dsh-tauri-ui/src/host/utils/paths.ts b/packages/dsh-tauri-ui/src/host/utils/paths.tsindex 657454f5..9d9cd0cc 100644--- a/packages/dsh-tauri-ui/src/host/utils/paths.ts+++ b/packages/dsh-tauri-ui/src/host/utils/paths.ts@@ -28,3 +28,7 @@ export function resolveSettingsFilePath(env: NodeJS.ProcessEnv = process.env): sexport function resolvePresetCachePath(env: NodeJS.ProcessEnv = process.env): string {return join(resolveDshHome(env), PLUGIN_ID, PRESET_CACHE_FILE_NAME)}++export function resolveUngroupedSessionPath(env: NodeJS.ProcessEnv = process.env): string {+ return join(resolveDshHome(env), 'ungrouped')+}diff --git a/test/e2e/plugins/dsh-tauri-ui.e2e.ts b/test/e2e/plugins/dsh-tauri-ui.e2e.tsindex cc19c496..9c2dfe4e 100644--- a/test/e2e/plugins/dsh-tauri-ui.e2e.ts+++ b/test/e2e/plugins/dsh-tauri-ui.e2e.ts@@ -11,6 +11,8 @@*/import type { Browser } from 'playwright'+import { existsSync, readdirSync } from 'node:fs'+import { join } from 'node:path'import { afterAll, beforeAll, describe, expect, inject, it } from 'vitest'import {COMPOSER_CARD,@@ -26,6 +28,9 @@ import {/** 与 `packages/dsh-tauri-ui/src/host/routes/index.ts:5` 的唯一路由对齐。 */const RESUME_PATH = '/api/desktop/dsh-tauri-ui/session/resume'+/** 未分组目录解析路由(`packages/dsh-tauri-ui/src/host/routes/ungrouped/get.ts`)。 */+const UNGROUPED_PATH = '/api/desktop/dsh-tauri-ui/ungrouped'+/** `/api/**` 要求浏览器会话;Cookie 由编排在根路径用一次性 token 换得。 */function headers(): Record<string, string> {return { 'cookie': inject('dshCookie'), 'content-type': 'application/json' }@@ -57,6 +62,11 @@ interface ResumeBody {ok?: boolean}+interface UngroupedBody {+ cwd?: string+ error?: string+}+describe('L2 宿主路由', () => {it('[反向] 验证续跑缺 sessionId 返回 400', async () => {const response = await fetch(url(), { method: 'POST', headers: headers(), body: '{}' })@@ -81,6 +91,27 @@ describe('L2 宿主路由', () => {expect(body.error, '会话不存在文案必须逐字相等').toBe('会话不存在或尚未运行')expect(JSON.stringify(body), '未知会话不得走到注入成功分支').not.toContain('"ok":true')})++ it('验证未分组目录解析路由落在 scratch DSH_HOME 下的 ungrouped,且不是核心安装目录', async () => {+ const home = inject('dshHome')+ const response = await fetch(`${inject('dshBaseUrl')}${UNGROUPED_PATH}`, { headers: headers() })++ expect(response.status, '目录解析是只读推导,必须 200').toBe(200)++ const body = await response.json() as UngroupedBody+ expect(Object.keys(body).sort(), '成功响应必须恰为 cwd 一个字段').toEqual(['cwd'])+ expect(typeof body.cwd, 'cwd 必须是字符串路径').toBe('string')+ expect(body.cwd, 'cwd 必须是绝对路径').toMatch(/^[A-Z]:\\|^\//)+ // 症状本身:桌面壳把 dsh 进程的 cwd 固定为核心安装目录(`dependencies/dsh`),+ // 不显式给 cwd 的未分组会话就会落进那个目录,形成伪项目分组。+ expect(body.cwd, '必须落在宿主自己的 DSH_HOME 之下,而不是进程 cwd(核心安装目录)').not.toMatch(/dependencies[\\/]dsh$/)+ expect(+ String(body.cwd).startsWith(home),+ `cwd 必须位于 scratch DSH_HOME 之下(实测 cwd=${String(body.cwd)} home=${home})`,+ ).toBe(true)+ expect(String(body.cwd).split(/[\\/]/).at(-1), '未分组目录名必须是 ungrouped').toBe('ungrouped')+ expect(body.error, '成功响应不得带 error').toBeUndefined()+ })})describe('L2 客户端', () => {@@ -311,6 +342,7 @@ describe('L2 客户端', () => {async () => await app.frame.evaluate(() => document.querySelector('[data-composer-card] [aria-label="选择工作区"]') !== null),{ timeout: 20_000, message: '会话已就位,composer 不得再停在「必须先选工作区」的不可用态' },).toBe(false)+expectNoSyntheticFallbacks(app)expect(app.errors, '未分组新建会话不得抛出应用级错误').toEqual([])}@@ -318,4 +350,35 @@ describe('L2 客户端', () => {await app.close()}})++ // chip 文案依赖官方列表快照的 `current` 投影,与本用例的观测面(磁盘)无关,因此独立成条:+ // 即便 chip 断言在别的环境里失败,cwd 的端到端证据仍然会执行。+ it('验证未分组新会话的 cwd 落在 scratch DSH_HOME/ungrouped,而非宿主进程 cwd', async () => {+ const home = inject('dshHome')+ const app = await newDshPage(browser, { ready: HERO_WORKSPACE_CHIP })+ try {+ await app.frame.locator(SIDEBAR_NEW_SESSION).first().click()++ // 会话的项目目录由 cwd 决定,而空白会话尚未落盘(persistence 的 materialize 只在+ // 首个事件批次时发生),因此磁盘上能观测到的最早产物是宿主建会话前的+ // `mkdir -p cwd`(`dsh-api-session-controller` 的 ensureSession)。不显式传 cwd 时+ // 宿主退回 `process.cwd()`——那是既有目录,绝不会新建出 ungrouped。+ await expect.poll(+ () => existsSync(join(home, 'ungrouped')),+ { timeout: 20_000, message: '未分组会话的 cwd 必须是 DSH_HOME/ungrouped(宿主据此目录建会话)' },+ ).toBe(true)++ // 反向证据:宿主进程 cwd(scratch 车道里是 profile 目录 `<home>/profiles/web`)不得+ // 出现会话痕迹。空白会话尚未 materialize,所以这里断言的是「没有它的项目目录」。+ const sessionsRoot = join(home, 'sessions')+ const projectDirs = existsSync(sessionsRoot) ? readdirSync(sessionsRoot) : []+ expect(projectDirs.filter(entry => entry.includes('profiles-web')), '未分组会话不得落进宿主进程 cwd 对应的项目目录').toEqual([])++ expectNoSyntheticFallbacks(app)+ expect(app.errors, '未分组新会话不得抛出应用级错误').toEqual([])+ }+ finally {+ await app.close()+ }+ })})