@deepseek-ai/dsh-util-file-operations 0.0.0-stage → 0.2.3-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DeepSeek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # Bilingual-pair consistency record for README.md (docs/i18n/README.md): per heading
2
+ # section, a hash of its English and Chinese blocks outside code blocks and generated regions.
3
+ # After editing either side, bring the other along and re-record with:
4
+ # pnpm run verify-translation-pairing --write packages/util/file-operations/README.md
5
+ /:
6
+ en: fbcd7bdf5e6ac9d5
7
+ zh: 6de6874a8e8b4baf
8
+ /deepseek-ai-dsh-util-file-operations:
9
+ en: da0752d09827c9c1
10
+ zh: 91f156699df34bdb
11
+ /deepseek-ai-dsh-util-file-operations/summary:
12
+ en: ade909c5d9136c6a
13
+ zh: 70821de31caf47a5
14
+ /deepseek-ai-dsh-util-file-operations/table-of-contents:
15
+ en: d152484eb41ac6b4
16
+ zh: 09388d293f9be9cb
17
+ /deepseek-ai-dsh-util-file-operations/use-this-package:
18
+ en: faa407d29df444de
19
+ zh: 2917c431fa883b2d
20
+ /deepseek-ai-dsh-util-file-operations/use-this-package/when-to-use-it:
21
+ en: 3acd8a44a56e9bbe
22
+ zh: 0284a867d13abb52
23
+ /deepseek-ai-dsh-util-file-operations/use-this-package/entry-point:
24
+ en: 7e7b155746965057
25
+ zh: b767da360d88b0a0
26
+ /deepseek-ai-dsh-util-file-operations/understand-the-implementation:
27
+ en: 0aa4116a052c750d
28
+ zh: 2b593ed29f93d7db
29
+ /deepseek-ai-dsh-util-file-operations/understand-the-implementation/source-map:
30
+ en: 06e58bef146dfd65
31
+ zh: 9a99dd5c078391fd
32
+ /deepseek-ai-dsh-util-file-operations/understand-the-implementation/one-function-per-machine:
33
+ en: af52641992e039f9
34
+ zh: 386d89c1df1a405f
35
+ /deepseek-ai-dsh-util-file-operations/understand-the-implementation/methods-on-a-runner:
36
+ en: 08aafb511689397c
37
+ zh: d80b5c82dd3fd3b9
38
+ /deepseek-ai-dsh-util-file-operations/further-exploration:
39
+ en: 7fd5e734da513299
40
+ zh: d622497a21752b83
41
+ /deepseek-ai-dsh-util-file-operations/model-experience:
42
+ en: 2f55856d71ee4af7
43
+ zh: 932c70140e8f08f8
44
+ /deepseek-ai-dsh-util-file-operations/model-experience/kv-cache-effect:
45
+ en: 40294c1c31b74824
46
+ zh: 0857e9b4dd363e0a
47
+ /deepseek-ai-dsh-util-file-operations/known-limitations-and-deferred-work:
48
+ en: 4b5504311c2d3d73
49
+ zh: 648705b126c00b51
50
+ /deepseek-ai-dsh-util-file-operations/known-limitations-and-deferred-work/dev-note:
51
+ en: e67f633dc6fe7773
52
+ zh: 3bb0c20c93f16f98
package/README.md CHANGED
@@ -1,3 +1,133 @@
1
- # Temporary Holding Version
1
+ ---
2
+ description: "The ctx.fs file rules shared by filesystem providers: realpath resolution, version guards, atomic publication, UTF-8 text checks, literal edits, patches, deletes, and moves over a machine's node:fs/promises."
3
+ kind: "package-library"
4
+ ---
2
5
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
6
+ # @deepseek-ai/dsh-util-file-operations
7
+
8
+ English | [中文](README.zh.md)
9
+
10
+ ## Summary
11
+
12
+ Use `dsh-util-file-operations` to give a `ctx.fs` provider the file rules of `dsh-fs-local`: realpath identity, version guards, atomic publication, UTF-8 text checks, literal edits, patches, deletes, and moves. `fileOperation` performs one request with a machine's `node:fs/promises` and `node:path`. It references only its parameters and JavaScript globals, so an execution environment can also run it from its source text on the machine where the files are. `fileOperations` builds the `FileSystem` methods on any runner of that function. Locking, watching, home-directory expansion, and native publication steps stay with the provider.
13
+
14
+ ## Table of Contents
15
+
16
+ - [Use this package](#use-this-package)
17
+ - [Understand the implementation](#understand-the-implementation)
18
+ - [Further Exploration](#further-exploration)
19
+ - [Model Experience](#model-experience)
20
+ - [Known Limitations and Deferred Work](#known-limitations-and-deferred-work)
21
+ - [Dev Note](#dev-note)
22
+
23
+ -----
24
+
25
+ <a id="use-this-package"></a>
26
+ ## Use this package
27
+
28
+ Import this library when a `FileSystem` provider must follow the same resolution, version, publication, edit, and error rules as the host backend instead of reimplementing them. It registers nothing; a provider imports it directly.
29
+
30
+ ### When to use it
31
+
32
+ [`dsh-fs-local`](../../fs/fs-local/README.md) and [`dsh-fs-env`](../../fs/fs-env/README.md) are the current consumers. `dsh-fs-local` runs `fileOperation` in its own process, passes Windows publication hooks and its staging names, and holds a per-target lock around each mutation. `dsh-fs-env` hands `fileOperation` to `ctx.environments.define()`, so each request runs in a registered execution environment, and passes `maxTextUnits` to `fileOperations`. The library supplies only the rules for one machine; the provider decides where the function runs and how concurrent mutations are ordered.
33
+
34
+ ### Entry point
35
+
36
+ A runner calls `fileOperation` with the machine's modules and converts failures with `fileOperationError`; `fileOperations` turns the runner into `FileSystem` methods.
37
+
38
+ ```ts
39
+ import * as fs from 'node:fs/promises'
40
+ import * as path from 'node:path'
41
+ import {
42
+ fileOperation, fileOperationError, fileOperations,
43
+ type FileOperationRequest, type FileOperationResults,
44
+ } from '@deepseek-ai/dsh-util-file-operations'
45
+
46
+ async function run<R extends FileOperationRequest>(request: R, signal?: AbortSignal): Promise<FileOperationResults[R['op']]> {
47
+ try {
48
+ const env = { fs, path, platform: process.platform, cwd: process.cwd(), ...signal === undefined ? {} : { signal } }
49
+ return await fileOperation(request, env)
50
+ } catch (error) {
51
+ throw fileOperationError(error)
52
+ }
53
+ }
54
+
55
+ const files = fileOperations(run, {
56
+ keyOf: real => real, pathOf: target => target.targetKey, cwd: undefined, basisMaxBytes: 10 * 1024 * 1024,
57
+ })
58
+ const target = await files.resolve('notes.txt')
59
+ await files.writeText(target, 'hello\n', { kind: 'createIfAbsent' })
60
+ ```
61
+
62
+ Each method returns the `FileSystem` outcome of [`dsh-fs`](../../fs/fs/README.md). Every filesystem failure rejects as an `FsError` with a stable `FS_*` code; a thrown value without such a code is a defect and propagates unchanged. `FileOperationHooks` in `env.hooks` add native publication steps, such as the Windows DACL copy and security-preserving replacement, override the staging names and the no-replace link, and expose checkpoints for tests; an omitted hook uses portable `node:fs/promises` behavior.
63
+
64
+ Two `env` fields serve a run in an execution environment, and a provider in the serving process omits both. `env.limits.maxMessageBytes` bounds the text that a list, read, edit, or load returns, counted at two bytes per UTF-16 unit plus a 4096-byte margin; a larger result fails with `FS_TOO_LARGE`. `env.sandbox` carries the run's mode and `canWrite(path)`: before a move publishes its destination, it asks whether the source may be removed and fails with `FS_SANDBOX_DENIED` otherwise, so a denied source leaves no copy. A publication or removal that the environment's file guard refuses with code `SANDBOX_DENIED` also fails with `FS_SANDBOX_DENIED` and keeps the guard's message. The `maxTextUnits` option of `fileOperations` bounds, in UTF-16 units, the loaded text plus the patched text of a patch or of a move with hunks; a larger pair fails with `FS_TOO_LARGE`.
65
+
66
+ -----
67
+
68
+ <a id="understand-the-implementation"></a>
69
+ ## Understand the implementation
70
+
71
+ <details>
72
+ <summary>Implementation internals — click to expand</summary>
73
+
74
+ The rules live in one function so that every provider, wherever the function runs, applies the same checks in the same order.
75
+
76
+ ### Source map
77
+
78
+ | File | Role |
79
+ |---|---|
80
+ | [`src/index.ts`](src/index.ts) | `fileOperation`, its request, result, and hook types, `fileOperationError`, and `fileOperations` |
81
+
82
+ ### One function per machine
83
+
84
+ `fileOperation` switches on `request.op`: resolution, metadata, listing, text and byte reads, one decoded step of a streamed read, writes, literal edits, the `load` and `store` pair of a patch, deletes, and moves. It reads its machine only through the `env` parameter and keeps no module state, so every call depends on its arguments alone. Failures leave the function as plain errors carrying an `FS_*` `code`; `fileOperationError` turns them into `FsError`s on the caller's side. An execution environment evaluates the function's source text in another process, where the errors its `fs` throws come from another realm and fail `instanceof Error`, so the function reads an error's `code`, `message`, and `name` as fields.
85
+
86
+ ### Methods on a runner
87
+
88
+ Most methods of `fileOperations` send one request. A patch and a move first `load` the source text; their hunks apply with `applyHunks` from [`dsh-fs-patch`](../../fs/fs-patch/README.md) in the calling process, and the following `store` or `move` request is guarded by the loaded version, so a file that changes in between fails with `FS_STALE_VERSION`.
89
+
90
+ </details>
91
+
92
+ -----
93
+
94
+ <a id="further-exploration"></a>
95
+ ## Further Exploration
96
+
97
+ Read these pages for the contract the rules implement and the provider that runs them.
98
+
99
+ - [dsh-fs](../../fs/fs/README.md) — the `ctx.fs` contract, outcomes, and error codes.
100
+ - [dsh-fs-local](../../fs/fs-local/README.md) — the host backend that runs these rules under per-target locks.
101
+ - [dsh-fs-env](../../fs/fs-env/README.md) — the provider that runs these rules in a registered execution environment.
102
+ - [Filesystem subsystem](../../../docs/subsystems/filesystem.md) — the provider contract, policy events, and error taxonomy.
103
+
104
+ -----
105
+
106
+ <a id="model-experience"></a>
107
+ ## Model Experience
108
+
109
+ Indirectly, through `dsh-tool-fs`, which renders the results and `FsError` messages of the providers that run these rules.
110
+
111
+ #### KV Cache effect
112
+
113
+ No direct invalidation; the named consumer owns any request-prefix changes.
114
+
115
+ ## Known Limitations and Deferred Work
116
+
117
+ <a id="known-limitations-and-deferred-work"></a>
118
+
119
+
120
+ These limits define what a provider still owns. They are current package constraints, not a task backlog.
121
+
122
+ - **No locking** — the methods of `fileOperations` hold no locks; a provider that needs ordered concurrent mutations of one target serializes them itself.
123
+ - **Whole-file text** — `read`, `edit`, and `load` hold the whole file in memory; only the `text` step decodes a stream, and its caller owns reading the chunks.
124
+
125
+ <a id="dev-note"></a>
126
+ ### Dev Note
127
+
128
+ <details>
129
+ <summary>Working context for maintainers — click to expand</summary>
130
+
131
+ None.
132
+
133
+ </details>
package/README.zh.md ADDED
@@ -0,0 +1,133 @@
1
+ ---
2
+ description: "文件系统提供方共享的 ctx.fs 文件规则:基于机器的 node:fs/promises 执行 realpath 解析、版本防护、原子发布、UTF-8 文本检查、字面量编辑、补丁、删除与移动。"
3
+ kind: "package-library"
4
+ ---
5
+
6
+ # @deepseek-ai/dsh-util-file-operations
7
+
8
+ [English](README.md) | 中文
9
+
10
+ ## 概述
11
+
12
+ 使用 `dsh-util-file-operations` 让 `ctx.fs` 提供方遵循 `dsh-fs-local` 的文件规则:realpath 身份、版本防护、原子发布、UTF-8 文本检查、字面量编辑、补丁、删除与移动。`fileOperation` 使用某台机器的 `node:fs/promises` 与 `node:path` 执行一个请求。它只引用自身参数与 JavaScript 全局对象,因此执行环境也可以在文件所在的机器上从其源码文本运行它。`fileOperations` 在该函数的任意 runner 之上构建 `FileSystem` 方法。加锁、监听、主目录展开与原生发布步骤仍归提供方负责。
13
+
14
+ ## 目录
15
+
16
+ - [使用本包](#use-this-package)
17
+ - [理解实现](#understand-the-implementation)
18
+ - [进一步探索](#further-exploration)
19
+ - [模型体验](#model-experience)
20
+ - [已知限制与延期工作](#known-limitations-and-deferred-work)
21
+ - [开发备注](#dev-note)
22
+
23
+ -----
24
+
25
+ <a id="use-this-package"></a>
26
+ ## 使用本包
27
+
28
+ 当 `FileSystem` 提供方需要遵循与宿主后端相同的解析、版本、发布、编辑与错误规则,而不想重新实现它们时,导入本库。本库不注册任何内容;提供方直接导入它。
29
+
30
+ ### 何时使用
31
+
32
+ [`dsh-fs-local`](../../fs/fs-local/README.zh.md) 与 [`dsh-fs-env`](../../fs/fs-env/README.zh.md) 是当前的消费方。`dsh-fs-local` 在自身进程中运行 `fileOperation`,传入 Windows 发布钩子与自己的暂存名称,并在每次变更外持有每目标锁。`dsh-fs-env` 把 `fileOperation` 交给 `ctx.environments.define()`,使每个请求在已注册的执行环境中运行,并向 `fileOperations` 传入 `maxTextUnits`。本库只提供单台机器上的规则;函数在哪里运行、并发变更如何排序,由提供方决定。
33
+
34
+ ### 入口
35
+
36
+ runner 用机器的模块调用 `fileOperation`,并用 `fileOperationError` 转换失败;`fileOperations` 把 runner 变为 `FileSystem` 方法。
37
+
38
+ ```ts
39
+ import * as fs from 'node:fs/promises'
40
+ import * as path from 'node:path'
41
+ import {
42
+ fileOperation, fileOperationError, fileOperations,
43
+ type FileOperationRequest, type FileOperationResults,
44
+ } from '@deepseek-ai/dsh-util-file-operations'
45
+
46
+ async function run<R extends FileOperationRequest>(request: R, signal?: AbortSignal): Promise<FileOperationResults[R['op']]> {
47
+ try {
48
+ const env = { fs, path, platform: process.platform, cwd: process.cwd(), ...signal === undefined ? {} : { signal } }
49
+ return await fileOperation(request, env)
50
+ } catch (error) {
51
+ throw fileOperationError(error)
52
+ }
53
+ }
54
+
55
+ const files = fileOperations(run, {
56
+ keyOf: real => real, pathOf: target => target.targetKey, cwd: undefined, basisMaxBytes: 10 * 1024 * 1024,
57
+ })
58
+ const target = await files.resolve('notes.txt')
59
+ await files.writeText(target, 'hello\n', { kind: 'createIfAbsent' })
60
+ ```
61
+
62
+ 每个方法返回 [`dsh-fs`](../../fs/fs/README.zh.md) 定义的 `FileSystem` 结果。每个文件系统失败都以带稳定 `FS_*` 错误码的 `FsError` 拒绝;不带此类错误码的抛出值属于缺陷,原样传播。`env.hooks` 中的 `FileOperationHooks` 可加入原生发布步骤(如 Windows DACL 复制与保留安全描述符的替换),覆盖暂存名称与不替换的链接发布,并提供测试用的检查点;省略的钩子使用可移植的 `node:fs/promises` 行为。
63
+
64
+ 有两个 `env` 字段服务于执行环境中的运行,在承载进程中的提供方两者都省略。`env.limits.maxMessageBytes` 限制列出、读取、编辑或 `load` 返回的文本,按每个 UTF-16 单元两个字节再加 4096 字节余量计算;更大的结果以 `FS_TOO_LARGE` 失败。`env.sandbox` 携带该次运行的模式与 `canWrite(path)`:移动在发布目标之前询问源文件能否删除,不能则以 `FS_SANDBOX_DENIED` 失败,因此被拒绝的源文件不会留下副本。环境的文件守卫以 `SANDBOX_DENIED` 错误码拒绝的发布或删除同样以 `FS_SANDBOX_DENIED` 失败,并保留守卫的消息。`fileOperations` 的 `maxTextUnits` 选项以 UTF-16 单元计,限制补丁或带 hunk 的移动所加载的文本与打补丁后的文本之和;超出时以 `FS_TOO_LARGE` 失败。
65
+
66
+ -----
67
+
68
+ <a id="understand-the-implementation"></a>
69
+ ## 理解实现
70
+
71
+ <details>
72
+ <summary>实现细节——点击展开</summary>
73
+
74
+ 规则集中在一个函数中,因此每个提供方无论在哪里运行该函数,都以相同顺序执行相同检查。
75
+
76
+ ### 源码地图
77
+
78
+ | 文件 | 职责 |
79
+ |---|---|
80
+ | [`src/index.ts`](src/index.ts) | `fileOperation` 及其请求、结果与钩子类型,`fileOperationError` 与 `fileOperations` |
81
+
82
+ ### 每台机器一个函数
83
+
84
+ `fileOperation` 依据 `request.op` 分支:解析、元数据、列出目录、文本与字节读取、流式读取的单个解码步骤、写入、字面量编辑、补丁的 `load` 与 `store` 两步、删除与移动。它只通过 `env` 参数访问所在机器,且不保存模块状态,因此每次调用只取决于它的参数。失败以带 `FS_*` `code` 的普通错误离开函数;`fileOperationError` 在调用方一侧把它们转为 `FsError`。执行环境在另一个进程中求值该函数的源码文本,那里的 `fs` 抛出的错误来自另一个 realm,`instanceof Error` 判断为假,因此函数以字段方式读取错误的 `code`、`message` 与 `name`。
85
+
86
+ ### runner 上的方法
87
+
88
+ `fileOperations` 的多数方法只发送一个请求。补丁与移动先以 `load` 加载源文本;其 hunk 在调用进程中用 [`dsh-fs-patch`](../../fs/fs-patch/README.zh.md) 的 `applyHunks` 应用,随后的 `store` 或 `move` 请求以加载时的版本为防护,因此中途发生变化的文件以 `FS_STALE_VERSION` 失败。
89
+
90
+ </details>
91
+
92
+ -----
93
+
94
+ <a id="further-exploration"></a>
95
+ ## 进一步探索
96
+
97
+ 阅读以下页面,了解这些规则实现的约定以及运行它们的提供方。
98
+
99
+ - [dsh-fs](../../fs/fs/README.zh.md)——`ctx.fs` 约定、结果与错误码。
100
+ - [dsh-fs-local](../../fs/fs-local/README.zh.md)——在每目标锁下运行这些规则的宿主后端。
101
+ - [dsh-fs-env](../../fs/fs-env/README.zh.md)——在已注册的执行环境中运行这些规则的提供方。
102
+ - [文件系统子系统](../../../docs/subsystems/filesystem.zh.md)——提供方约定、策略事件与错误分类体系。
103
+
104
+ -----
105
+
106
+ <a id="model-experience"></a>
107
+ ## 模型体验
108
+
109
+ 通过 `dsh-tool-fs` 间接产生影响;它渲染运行这些规则的提供方给出的结果与 `FsError` 消息。
110
+
111
+ #### KV Cache 影响
112
+
113
+ 不会直接使缓存失效;具名消费方负责请求前缀的任何变化。
114
+
115
+ ## 已知限制与延期工作
116
+
117
+ <a id="known-limitations-and-deferred-work"></a>
118
+
119
+
120
+ 这些限制说明哪些职责仍归提供方。它们是当前包约束,不是任务积压。
121
+
122
+ - **不加锁**:`fileOperations` 的方法不持有任何锁;需要对同一目标的并发变更排序的提供方自行串行化。
123
+ - **整文件文本**:`read`、`edit` 与 `load` 把整个文件保存在内存中;只有 `text` 步骤解码流,读取分片由其调用方负责。
124
+
125
+ <a id="dev-note"></a>
126
+ ### 开发备注
127
+
128
+ <details>
129
+ <summary>维护者的工作上下文——点击展开</summary>
130
+
131
+ 无。
132
+
133
+ </details>