diff --git a/apps/desktop-host/src/file-drag.ts b/apps/desktop-host/src/file-drag.ts index f7cf2b8072..4297d804d9 100644 --- a/apps/desktop-host/src/file-drag.ts +++ b/apps/desktop-host/src/file-drag.ts @@ -1,10 +1,8 @@ /** Resolve desktop-only file references through the active Host filesystem and Session. */ import { createHash } from 'node:crypto' import type { Context } from '@deepseek-ai/cordis' -import { resolveWorkspaceFileScope } from '@deepseek-ai/dsh-api-workspace-files' +import type {} from '@deepseek-ai/dsh-api-workspace-files' import type {} from '@deepseek-ai/dsh-fs' -import type {} from '@deepseek-ai/dsh-session' -import type {} from '@deepseek-ai/dsh-sandbox-policy' import type { SessionId } from '@deepseek-ai/dsh-session/types' import { parseFileAddress } from '@deepseek-ai/dsh-util-workspace-path' @@ -27,10 +25,11 @@ export async function localDragResource(ctx: Context, address: string, cancellat const resource = parseFileAddress(address) if (resource?.scope !== 'session') throw new Error('File drag requires a Session resource') const fs = ctx.get('fs') - if (fs === undefined || ctx.get('sessions') === undefined || ctx.get('sandboxPolicy') === undefined) throw new Error('File access unavailable') + const workspaceFiles = ctx.get('workspaceFiles') + if (fs === undefined || workspaceFiles === undefined) throw new Error('File access unavailable') const signal = cancellation === undefined ? AbortSignal.timeout(10_000) : AbortSignal.any([cancellation, AbortSignal.timeout(10_000)]) signal.throwIfAborted() - const scope = await resolveWorkspaceFileScope(ctx, resource.sessionId as SessionId, signal) + const scope = await workspaceFiles.scopeOf(resource.sessionId as SessionId, signal) if (scope === undefined) throw new Error('File drag Session unavailable') const cwd = scope.workspaceRoot const entry = await fs.lstat(resource.path, { cwd }, signal) diff --git a/apps/desktop-host/tests/file-drag.spec.ts b/apps/desktop-host/tests/file-drag.spec.ts index 437a062e05..0ac89b555c 100644 --- a/apps/desktop-host/tests/file-drag.spec.ts +++ b/apps/desktop-host/tests/file-drag.spec.ts @@ -16,9 +16,13 @@ async function fixture() { onTestFinished(async () => { await ctx.fiber.dispose(); await rm(root, { recursive: true, force: true }) }) await ctx.plugin(LocalFileSystem, { cwd: root }) await ctx.plugin(SessionStore) - ctx.provide('sandboxPolicy', { workspaceRoot: root } as never) const id = SessionId('file-drag-test') ctx.sessions.create(id, { meta: { cwd: root, origin: 'subagent' } }) + // The workspace file service resolves a live Session's root; its own tests cover that resolution. + ctx.provide('workspaceFiles', { + scopeOf: (sessionId: SessionId) => + Promise.resolve(ctx.sessions.get(sessionId) === undefined ? undefined : { sessionId, workspaceRoot: root }), + } as never) const path = join(root, '报告.pdf') const bytes = Buffer.from('%PDF-1.4\n原始内容\n%%EOF') await writeFile(path, bytes) diff --git a/docs/subsystems/workspace.md b/docs/subsystems/workspace.md index 665dc1011c..57684907c1 100644 --- a/docs/subsystems/workspace.md +++ b/docs/subsystems/workspace.md @@ -409,6 +409,15 @@ Source: [`packages/api/workspace-controller/src/index.ts`](../../packages/api/wo Host Remote file reads and workspace directory observations over the composed filesystem. ```ts cordis-catalog +/** + * Resolve the scope other Host services use to open the same `session//` addresses: + * a live Session's workspace root, else a stored Session's recorded root, else its header directory. + * @param sessionId - Session identity received on the wire. + * @param signal - cancellation for the persistence read of a stored Session. + * @returns the scope, or undefined when no live or stored Session has that identity. + */ +scopeOf(sessionId: SessionId, signal?: AbortSignal): Promise + /** * Read one page of lines from a UTF-8 file readable by the filesystem backend. * @param workspaceFileScope - header-derived workspace root for the Session identity on the wire. @@ -459,6 +468,8 @@ Host Remote file reads and workspace directory observations over the composed fi @Remote({ mode: 'stream' }) changes(workspaceFileScope: WorkspaceFileScope, path: string, signal: AbortSignal): AsyncIterable ``` +Types: [SessionId](core.md) + Source: [`packages/api/workspace-files/src/index.ts`](../../packages/api/workspace-files/src/index.ts) diff --git a/docs/subsystems/workspace.zh.md b/docs/subsystems/workspace.zh.md index 84c7ee7a62..daa4c30d2f 100644 --- a/docs/subsystems/workspace.zh.md +++ b/docs/subsystems/workspace.zh.md @@ -409,6 +409,15 @@ Source: [`packages/api/workspace-controller/src/index.ts`](../../packages/api/wo Host Remote file reads and workspace directory observations over the composed filesystem. ```ts cordis-catalog +/** + * Resolve the scope other Host services use to open the same `session//` addresses: + * a live Session's workspace root, else a stored Session's recorded root, else its header directory. + * @param sessionId - Session identity received on the wire. + * @param signal - cancellation for the persistence read of a stored Session. + * @returns the scope, or undefined when no live or stored Session has that identity. + */ +scopeOf(sessionId: SessionId, signal?: AbortSignal): Promise + /** * Read one page of lines from a UTF-8 file readable by the filesystem backend. * @param workspaceFileScope - header-derived workspace root for the Session identity on the wire. @@ -459,6 +468,8 @@ Host Remote file reads and workspace directory observations over the composed fi @Remote({ mode: 'stream' }) changes(workspaceFileScope: WorkspaceFileScope, path: string, signal: AbortSignal): AsyncIterable ``` +Types: [SessionId](core.zh.md) + Source: [`packages/api/workspace-files/src/index.ts`](../../packages/api/workspace-files/src/index.ts) diff --git a/packages/api/workspace-files/README.i18n.yaml b/packages/api/workspace-files/README.i18n.yaml index 9a6a12b99b..303e77c16e 100644 --- a/packages/api/workspace-files/README.i18n.yaml +++ b/packages/api/workspace-files/README.i18n.yaml @@ -15,8 +15,8 @@ en: d152484eb41ac6b4 zh: 09388d293f9be9cb /deepseek-ai-dsh-api-workspace-files/use-this-package: - en: 0dcc561a32c95842 - zh: af6355823a56bbf0 + en: 903c1548b14a0ffd + zh: d900442c7445710a /deepseek-ai-dsh-api-workspace-files/use-this-package/addressing-and-paths: en: b9565cfa7a1da451 zh: fc4078a58322fc7f diff --git a/packages/api/workspace-files/README.md b/packages/api/workspace-files/README.md index 16ba112760..f9cb7f67d7 100644 --- a/packages/api/workspace-files/README.md +++ b/packages/api/workspace-files/README.md @@ -25,7 +25,7 @@ Use this package to preview files readable through a Session's filesystem from t ## Use this package -Mount the package beside `dsh-fs`, `dsh-sandbox-policy`, the Session store, and the Typert Gateway; the bundle does so right after the Session Controller. Every method takes the Session identity on the wire, so a Client calls `remote.workspaceFiles.read(sessionId, path, range, signal)`, `stat(sessionId, path, signal)`, `readBytes(sessionId, path, options, signal)`, `list(sessionId, path, signal)`, or `changes(sessionId, path, signal)` and never names a root itself. The Host reads a live Session's workspace root from the working-directory service, or a cold Session's recorded root from the projection cache beside its persisted header, falling back to the header directory; it does not activate an Agent, read the event body, or borrow a parent Session's root. `resolveWorkspaceFileScope(ctx, sessionId, signal)` exports that resolution for other Host services that open the same `session//` addresses. Session persistence is optional for live reads, but without it a cold Session cannot resolve and the Gateway returns `gateway/lookup-not-found`. +Mount the package beside `dsh-fs`, `dsh-sandbox-policy`, the Session store, and the Typert Gateway; the bundle does so right after the Session Controller. Every method takes the Session identity on the wire, so a Client calls `remote.workspaceFiles.read(sessionId, path, range, signal)`, `stat(sessionId, path, signal)`, `readBytes(sessionId, path, options, signal)`, `list(sessionId, path, signal)`, or `changes(sessionId, path, signal)` and never names a root itself. The Host reads a live Session's workspace root from the working-directory service, or a cold Session's recorded root from the projection cache beside its persisted header, falling back to the header directory; it does not activate an Agent, read the event body, or borrow a parent Session's root. `workspaceFiles.scopeOf(sessionId, signal)` offers that resolution to other Host services that open the same `session//` addresses. Session persistence is optional for live reads, but without it a cold Session cannot resolve and the Gateway returns `gateway/lookup-not-found`. | Method | Returns | Purpose | |---|---|---| diff --git a/packages/api/workspace-files/README.zh.md b/packages/api/workspace-files/README.zh.md index c00d30a60d..9033df1746 100644 --- a/packages/api/workspace-files/README.zh.md +++ b/packages/api/workspace-files/README.zh.md @@ -25,7 +25,7 @@ kind: "package-reference" ## 使用本包 -把本包与 `dsh-fs`、`dsh-sandbox-policy`、Session store 和 Typert Gateway 一起挂载;bundle 把它紧随 Session Controller 之后挂载。每个方法都在线路上携带 Session 身份,Client 调用 `remote.workspaceFiles.read(sessionId, path, range, signal)`、`stat(sessionId, path, signal)`、`readBytes(sessionId, path, options, signal)`、`list(sessionId, path, signal)` 或 `changes(sessionId, path, signal)`,从不自己指定根。Host 从工作目录服务读取 live Session 的工作区根,cold Session 则读取持久化 header 旁投影缓存中记录的根,再回退到 header 目录;它不会激活 Agent、读取事件正文或借用父 Session 的根。`resolveWorkspaceFileScope(ctx, sessionId, signal)` 把这一解析导出给打开同一种 `session//` 地址的其他 Host 服务。live 读取不要求挂载 Session persistence;未挂载时 cold Session 无法解析,Gateway 返回 `gateway/lookup-not-found`。 +把本包与 `dsh-fs`、`dsh-sandbox-policy`、Session store 和 Typert Gateway 一起挂载;bundle 把它紧随 Session Controller 之后挂载。每个方法都在线路上携带 Session 身份,Client 调用 `remote.workspaceFiles.read(sessionId, path, range, signal)`、`stat(sessionId, path, signal)`、`readBytes(sessionId, path, options, signal)`、`list(sessionId, path, signal)` 或 `changes(sessionId, path, signal)`,从不自己指定根。Host 从工作目录服务读取 live Session 的工作区根,cold Session 则读取持久化 header 旁投影缓存中记录的根,再回退到 header 目录;它不会激活 Agent、读取事件正文或借用父 Session 的根。`workspaceFiles.scopeOf(sessionId, signal)` 把这一解析提供给打开同一种 `session//` 地址的其他 Host 服务。live 读取不要求挂载 Session persistence;未挂载时 cold Session 无法解析,Gateway 返回 `gateway/lookup-not-found`。 | 方法 | 返回 | 用途 | |---|---|---| diff --git a/packages/api/workspace-files/package.json b/packages/api/workspace-files/package.json index 5f807c3f19..562fbfb7fc 100644 --- a/packages/api/workspace-files/package.json +++ b/packages/api/workspace-files/package.json @@ -59,9 +59,7 @@ "zod": "4.4.3" }, "peerDependencies": { - "@deepseek-ai/cordis": "workspace:~", - "@deepseek-ai/dsh-session-projection-cache": "workspace:*", - "@deepseek-ai/dsh-working-directory": "workspace:*" + "@deepseek-ai/cordis": "workspace:~" }, "devDependencies": { "@deepseek-ai/cordis": "workspace:~", diff --git a/packages/api/workspace-files/src/index.ts b/packages/api/workspace-files/src/index.ts index 427f5f8295..9d31a02b68 100644 --- a/packages/api/workspace-files/src/index.ts +++ b/packages/api/workspace-files/src/index.ts @@ -75,7 +75,7 @@ export interface WorkspaceFileScope { * @param signal - cancellation for the persistence read of a stored Session. * @returns the scope, or undefined when no live or stored Session has that identity. */ -export async function resolveWorkspaceFileScope( +async function workspaceFileScopeOf( ctx: Context, sessionId: SessionId, signal?: AbortSignal, ): Promise { const live = ctx.get('sessions')?.get(sessionId) @@ -237,11 +237,22 @@ export class WorkspaceFiles extends TypertRemoteService { resource: 'session', hostTypeSymbol: '@deepseek-ai/dsh-api-workspace-files#WorkspaceFileScope', wireTypeSymbol: '@deepseek-ai/dsh-session/types#SessionId', - resolve: sessionId => resolveWorkspaceFileScope(scope, sessionId), + resolve: sessionId => workspaceFileScopeOf(scope, sessionId), }) }) } + /** + * Resolve the scope other Host services use to open the same `session//` addresses: + * a live Session's workspace root, else a stored Session's recorded root, else its header directory. + * @param sessionId - Session identity received on the wire. + * @param signal - cancellation for the persistence read of a stored Session. + * @returns the scope, or undefined when no live or stored Session has that identity. + */ + scopeOf(sessionId: SessionId, signal?: AbortSignal): Promise { + return workspaceFileScopeOf(this.ctx, sessionId, signal) + } + /** * Read one page of lines from a UTF-8 file readable by the filesystem backend. * @param workspaceFileScope - header-derived workspace root for the Session identity on the wire. diff --git a/packages/api/workspace-files/tests/scope.spec.ts b/packages/api/workspace-files/tests/scope.spec.ts index 96adb39120..901ff1b301 100644 --- a/packages/api/workspace-files/tests/scope.spec.ts +++ b/packages/api/workspace-files/tests/scope.spec.ts @@ -3,7 +3,7 @@ import { Context } from '@deepseek-ai/cordis' import SessionStore, { SESSION_FORMAT_VERSION, SessionId, type SessionHeader } from '@deepseek-ai/dsh-session' import TypertRegistry from '@deepseek-ai/dsh-typert-registry' import { describe, expect, it, vi } from 'vitest' -import WorkspaceFiles, { resolveWorkspaceFileScope } from '../src/index.ts' +import WorkspaceFiles from '../src/index.ts' const CAPS = { maxBytes: 1024, @@ -53,9 +53,9 @@ describe('Workspace Files Session scope lookup', () => { const workspaceFiles = await ctx.plugin(WorkspaceFiles, CAPS) try { ctx.sessions.create(liveId, { meta: { cwd: liveRoot, origin: 'subagent' } }) - await expect(resolveWorkspaceFileScope(ctx, liveId)).resolves.toEqual({ sessionId: liveId, workspaceRoot: liveWorktree }) - await expect(resolveWorkspaceFileScope(ctx, coldId)).resolves.toEqual({ sessionId: coldId, workspaceRoot: coldWorktree }) - await expect(resolveWorkspaceFileScope(ctx, plainId)).resolves.toEqual({ sessionId: plainId, workspaceRoot: coldRoot }) + await expect(ctx.workspaceFiles.scopeOf(liveId)).resolves.toEqual({ sessionId: liveId, workspaceRoot: liveWorktree }) + await expect(ctx.workspaceFiles.scopeOf(coldId)).resolves.toEqual({ sessionId: coldId, workspaceRoot: coldWorktree }) + await expect(ctx.workspaceFiles.scopeOf(plainId)).resolves.toEqual({ sessionId: plainId, workspaceRoot: coldRoot }) expect(cachedSnapshot).toHaveBeenCalledWith(expect.objectContaining({ id: coldId }), ['workspaceRoot']) const lookup = ctx.typert.lookups.get('workspaceFileScope') if (lookup === undefined) throw new Error('workspaceFileScope lookup did not register') @@ -108,7 +108,7 @@ describe('Workspace Files Session scope lookup', () => { await expect(lookup.resolve(liveFallbackId)).resolves.toEqual({ sessionId: liveFallbackId, workspaceRoot: fallbackRoot }) expect(stat).not.toHaveBeenCalled() const signal = new AbortController().signal - await expect(resolveWorkspaceFileScope(ctx, coldId, signal)).resolves.toEqual({ sessionId: coldId, workspaceRoot: coldRoot }) + await expect(ctx.workspaceFiles.scopeOf(coldId, signal)).resolves.toEqual({ sessionId: coldId, workspaceRoot: coldRoot }) expect(stat).toHaveBeenLastCalledWith(coldId, { signal }) await expect(lookup.resolve(coldId)).resolves.toEqual({ sessionId: coldId, workspaceRoot: coldRoot }) await expect(lookup.resolve(fallbackId)).resolves.toEqual({ sessionId: fallbackId, workspaceRoot: fallbackRoot }) diff --git a/packages/client/ui-deliverables/src/present-open.ts b/packages/client/ui-deliverables/src/present-open.ts index 0b783032eb..1fc567026d 100644 --- a/packages/client/ui-deliverables/src/present-open.ts +++ b/packages/client/ui-deliverables/src/present-open.ts @@ -1,7 +1,7 @@ /** Serve change summaries and comparisons, and open declared or changed workspace files verified by the viewed Session's filesystem. */ import type { Context } from '@deepseek-ai/cordis' import type {} from '@deepseek-ai/dsh-api-session-controller' -import { resolveWorkspaceFileScope } from '@deepseek-ai/dsh-api-workspace-files' +import type {} from '@deepseek-ai/dsh-api-workspace-files' import type {} from '@deepseek-ai/dsh-fs' import type {} from '@deepseek-ai/dsh-sandbox-policy' import { remoteErrorOf } from '@deepseek-ai/dsh-typert-protocol' @@ -116,7 +116,7 @@ async function handlePresentOpen(ctx: Context, request: Request): Promise(), close: vi.fn(), } - const sessionsAt = (cwd: string | null): SessionListState => - ({ byId: cwd === null ? {} : { [SESSION]: { cwd } }, projectionsBySession: {} } as unknown as SessionListState) + const sessionsAt = (cwd: string | null): SessionListState => ({ + ids: cwd === null ? [] : [SESSION], + byId: cwd === null ? {} : { [SESSION]: { id: SESSION, displayTitle: 'files', running: false, retainedBy: {}, blank: false, updatedAt: 0, cwd } }, + phase: 'ready', + projectionsBySession: {}, + }) const useTabInfo = (): SidebarRightTabInfo => ({ sidebar: { expanded: true, fullscreen: false }, panel: { id: PANE, isFloating: placement.isFloating ?? false }, diff --git a/packages/extensions/tool-cordis/src/api-catalog.ts b/packages/extensions/tool-cordis/src/api-catalog.ts index 2c60142c3b..4421709265 100644 --- a/packages/extensions/tool-cordis/src/api-catalog.ts +++ b/packages/extensions/tool-cordis/src/api-catalog.ts @@ -4586,6 +4586,12 @@ export const SERVICE_API: readonly ServiceApiEntry[] = [ summary: 'Host Remote file reads and workspace directory observations over the composed filesystem.', description: 'Host Remote file reads and workspace directory observations over the composed filesystem.', methods: [ + { + signature: 'scopeOf(sessionId: SessionId, signal?: AbortSignal): Promise', + description: 'Resolve the scope other Host services use to open the same `session//` addresses: a live Session\'s workspace root, else a stored Session\'s recorded root, else its header directory.', + parameters: [{ name: 'sessionId', description: 'Session identity received on the wire.' }, { name: 'signal', description: 'cancellation for the persistence read of a stored Session.' }], + returns: 'the scope, or undefined when no live or stored Session has that identity.', + }, { signature: '@Remote async read( workspaceFileScope: WorkspaceFileScope, path: string, range: WorkspaceFileRange, signal: AbortSignal, ): Promise', description: 'Read one page of lines from a UTF-8 file readable by the filesystem backend.', diff --git a/packages/test-support/agent-loop-testkit/src/working-directory-fixture.ts b/packages/test-support/agent-loop-testkit/src/working-directory-fixture.ts index ceac485150..9c77e3ce27 100644 --- a/packages/test-support/agent-loop-testkit/src/working-directory-fixture.ts +++ b/packages/test-support/agent-loop-testkit/src/working-directory-fixture.ts @@ -5,7 +5,8 @@ import type { Session } from '@deepseek-ai/dsh-session' /** * Supply fixed directory values without filesystem checks or durable changes. - * The workspace root equals the directory, which is the Session's only anchor. + * The workspace root equals the directory, which is the Session's only anchor; a Session without a + * recorded directory has no anchor, so consumers fall back as they do with the real service. * @param ctx - context owning this test collaborator. * @param defaultDirectory - fallback for fixtures without a Session directory. */ @@ -15,7 +16,7 @@ export function provideWorkingDirectoryFixture(ctx: Context, defaultDirectory = defaultDirectory, get, rootOf: get, - anchorsOf: (session: Session): readonly string[] => [get(session)], + anchorsOf: (session: Session): readonly string[] => session.header.cwd === undefined ? [] : [get(session)], ensure: (agent: Agent): Promise => Promise.resolve(get(agent.session)), }) } diff --git a/scripts/no-unknown-casts.baseline.json b/scripts/no-unknown-casts.baseline.json index b5bb6da63d..51445ed602 100644 --- a/scripts/no-unknown-casts.baseline.json +++ b/scripts/no-unknown-casts.baseline.json @@ -639,8 +639,7 @@ "1bbd18e22567ce0529d02b770533f9bda84ff379b161dda8b9548053a18a0e97": 1 }, "packages/client/ui-sidebar-files/tests/mount.client.tsx": { - "2caa9ae382deea09b4ac64586c1ab49bd935b1e689218e3d485bd7b918e22066": 1, - "684eae6dc3d4dbe526c5a5b11ebd84e35e335bebd519cf09c0f3a24ba9280fc1": 1 + "2caa9ae382deea09b4ac64586c1ab49bd935b1e689218e3d485bd7b918e22066": 1 }, "packages/client/ui-sidebar-files/tests/watch.client.spec.ts": { "a5cc084c7b08798fed2de12497312800c3d9bbb8911d859364876cfbf4adf8e7": 1