Files
deepseek-harness/packages/fs/tool-fs/tests/subpaths.spec.ts
Dudu-0223 ef37ce3b9d refactor(fs): split filesystem seam into provider ctx.fs + policy ctx.fileContext
Implements the split-the-filesystem-seam RFC. ctx.fs shrinks to a text-storage
provider seam (resolve/stat/readText/streamText/writeText/editText with branded
FsTargetKey/FsVersion and an explicit FsWriteExpectation); the new
dsh-file-context package owns the model-facing policy (read windowing,
observed-state, write/edit freshness) as the concrete ctx.fileContext service.

Authorization is now freshness-based rather than full/partial view: a windowed
read records the file version and authorizes a later edit when the file is
unchanged, removing the dead-end where reading lines 100-150 of a large file
could not edit line 120. editText stays a provider primitive so version guard +
literal match + atomic rewrite remain one critical section, and the stale check
runs before matching so a stale edit reports FS_STALE_VERSION. tool-fs injects
fileContext, never reaching around to ctx.fs (the no-bypass contract).
2026-06-26 17:23:18 +08:00

83 lines
2.8 KiB
TypeScript

/**
* Tests for the per-tool subpath plugins (`@deepseek-ai/dsh-tool-fs/read`,
* `/write`, `/edit`): each registers exactly one tool, injects the same
* services (`tools`, `fileContext`, `systemPrompt`), and cleans up on disposal.
*/
import { describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
import ToolRegistry from '@deepseek-ai/dsh-tools'
import { FileSystem, FsTargetKey, FsVersion } from '@deepseek-ai/dsh-fs'
import type {
FsEditOutcome,
FsInfo,
FsTarget,
FsWriteOutcome,
} from '@deepseek-ai/dsh-fs'
import FileContext from '@deepseek-ai/dsh-file-context'
import * as readPlugin from '@deepseek-ai/dsh-tool-fs/read'
import * as writePlugin from '@deepseek-ai/dsh-tool-fs/write'
import * as editPlugin from '@deepseek-ai/dsh-tool-fs/edit'
class StubFs extends FileSystem {
override async resolve(path: string): Promise<FsTarget> {
return { inputPath: path, targetKey: FsTargetKey(path), displayPath: path }
}
override async stat(): Promise<FsInfo | undefined> {
return { version: FsVersion('v'), type: 'file', size: 0 }
}
override async readText(): Promise<string> {
return ''
}
override async streamText(): Promise<AsyncIterable<string>> {
return (async function* () { yield '' })()
}
override async writeText(): Promise<FsWriteOutcome> {
return { operation: 'create', version: FsVersion('v') }
}
override async editText(): Promise<FsEditOutcome> {
return { replacements: 1, replaceAll: false, version: FsVersion('v') }
}
}
async function base() {
const ctx = new Context()
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRegistry)
await ctx.plugin(StubFs)
await ctx.plugin(FileContext)
return ctx
}
describe('subpath plugins', () => {
it('each registers exactly its one tool', async () => {
const cases: Array<[unknown, string]> = [
[readPlugin, 'read'],
[writePlugin, 'write'],
[editPlugin, 'edit'],
]
for (const [plugin, toolName] of cases) {
const ctx = await base()
await ctx.plugin(plugin as Parameters<Context['plugin']>[0])
expect(ctx.tools.schemas().map(s => s.name)).toEqual([toolName])
}
})
it('cleans up on disposal (HMR safety)', async () => {
const ctx = await base()
const fiber = await ctx.plugin(readPlugin as Parameters<Context['plugin']>[0])
expect(ctx.tools.schemas()).toHaveLength(1)
await fiber.dispose()
expect(ctx.tools.schemas()).toHaveLength(0)
})
it('stays pending without a ctx.fileContext provider', async () => {
const ctx = new Context()
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRegistry)
await ctx.plugin(writePlugin as Parameters<Context['plugin']>[0])
expect(ctx.tools.schemas()).toHaveLength(0)
})
})