dsh-background-task-engine 1.0.0 → 1.0.2

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/CHANGELOG.md CHANGED
@@ -4,6 +4,32 @@ All notable changes to `dsh-background-task-engine` are documented here.
4
4
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and
5
5
  the project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.0.2] — 2026-11-26
8
+
9
+ Documentation release: the default README is now Chinese.
10
+
11
+ ### Changed
12
+
13
+ - `README.md` is now the Chinese document, so the npm package page and the GitHub
14
+ repository home both render in Chinese by default.
15
+ - The previous English README moved to `README.en.md`; both files carry a
16
+ language switch at the top. `README.zh.md` was removed (it is now `README.md`).
17
+ - `package.json` `description` is now Chinese, so the npm listing summary matches
18
+ the README.
19
+ - `files` updated from `README.zh.md` to `README.en.md`.
20
+
21
+ ## [1.0.1] — 2026-11-26
22
+
23
+ Documentation-only release.
24
+
25
+ ### Fixed
26
+
27
+ - README install instructions now put the npm spec first and explain that a
28
+ directory install needs `npm install` run first, because the plugin imports
29
+ `@deepseek-ai/schemastery`. Without it the loader fails with
30
+ `ERR_MODULE_NOT_FOUND` — observed and recorded while installing 1.0.0.
31
+ - Added the npm version badge.
32
+
7
33
  ## [1.0.0] — 2026-11-26
8
34
 
9
35
  First release. Verified against DeepSeek Harness `0.2.2-rc.3`
@@ -58,6 +84,8 @@ First release. Verified against DeepSeek Harness `0.2.2-rc.3`
58
84
 
59
85
  ### Verification
60
86
 
87
+ - Published as `dsh-background-task-engine@1.0.0` on npm; the registry copy was
88
+ installed into a real profile and reports `application: applied`.
61
89
  - 81 tests across domain, storage, rendering and integration suites.
62
90
  - Real-host runs against the installed DSH `0.2.2-rc.3` packages: mount, full
63
91
  manual loop, restart persistence, stale-run recovery, resume.
package/README.en.md ADDED
@@ -0,0 +1,266 @@
1
+ # dsh-background-task-engine
2
+
3
+ [![npm](https://img.shields.io/npm/v/dsh-background-task-engine)](https://www.npmjs.com/package/dsh-background-task-engine)
4
+ [![license](https://img.shields.io/github/license/yzzshanyin-rgb/dsh-background-task-engine)](LICENSE)
5
+
6
+ **English | [中文](README.md)**
7
+
8
+ Durable background task engine for **DeepSeek Harness (DSH)**: reusable task
9
+ templates, ordered linear steps, one persisted run ledger, progress and history
10
+ queries, explicit failure/interruption recovery, and optional linkage to native
11
+ DSH Schedule.
12
+
13
+ It does not reimplement a scheduler, an agent loop, a job registry or a storage
14
+ backend — it composes the Host's own `ctx.schedule`, `ctx.storageDomain` and
15
+ `ctx.tools`.
16
+
17
+ - [Install](#install)
18
+ - [Tools](#tools)
19
+ - [Execution protocol](#execution-protocol)
20
+ - [Data model](#data-model)
21
+ - [Configuration](#configuration)
22
+ - [Errors](#errors)
23
+ - [Security](#security)
24
+ - [Upgrade](#upgrade)
25
+ - [Troubleshooting](#troubleshooting)
26
+ - [Development](#development)
27
+
28
+ ## Install
29
+
30
+ Requirements: DSH **0.2.2-rc.3** or a compatible release with
31
+ `ctx.storageDomain` mounted (the stock base bundle does this with
32
+ `storage-domain backend: json`).
33
+
34
+ ```sh
35
+ # from npm (published 1.0.2)
36
+ dsh plugin --profile <name> add dsh-background-task-engine
37
+
38
+ # or from a local checkout
39
+ dsh plugin --profile <name> add /absolute/path/to/dsh-background-task-engine
40
+ ```
41
+
42
+ When installing from a directory path, run `npm install` in that directory first:
43
+ the plugin imports `@deepseek-ai/schemastery`, and a missing `node_modules` makes
44
+ the loader fail with `ERR_MODULE_NOT_FOUND`. The registry spec carries its own
45
+ dependencies and needs no checkout.
46
+
47
+ Then restart the app. Confirm in **Plugins** that the row
48
+ `background-task-engine` is enabled; the row shows this bundle's title,
49
+ description and icon.
50
+
51
+ The bundle patch is intentionally empty apart from the plugin row, so installing
52
+ it does not change any other setting:
53
+
54
+ ```yaml
55
+ - insert:
56
+ - id: background-task-engine
57
+ name: 'dsh-background-task-engine'
58
+ config: { ... }
59
+ ```
60
+
61
+ ## Tools
62
+
63
+ Twelve model-facing tools, all returning
64
+ `{ success: true, ... }` or `{ success: false, error: { code, message, retryable, relatedRunId? } }`.
65
+
66
+ | Tool | Purpose | Key arguments |
67
+ |---|---|---|
68
+ | `task_create` | Create a template (1–10 steps) | `title`, `description?`, `steps[]`, `copyFromTaskId?` |
69
+ | `task_list` | List this Session's templates | `status?`, `query?`, `limit?`, `cursor?` |
70
+ | `task_get` | Read one template + recent runs | `taskId`, `includeRuns?` |
71
+ | `task_update` | Edit title/description/steps/status | `taskId`, `expectedRevision`, `changes` |
72
+ | `task_archive` | Soft-archive; history preserved | `taskId`, `expectedRevision` |
73
+ | `task_schedule` | Manage the native schedule link | `taskId`, `action`, `timing?`, `scheduleRule?` |
74
+ | `task_run` | Start a manual run now | `taskId`, `manualRunId?`, `defer?` |
75
+ | `task_run_begin` | Claim a run (task or queued run or delivery payload) | `taskId?`, `runId?`, `triggerPayload?`, `scheduleId?`, `occurrenceKey?`, `idempotencyKey?` |
76
+ | `task_step_report` | Report a step | `runId`, `stepId`, `action`, `summary?`, `error?` |
77
+ | `task_run_finish` | Submit the run result | `runId`, `status`, `summary?`, `error?` |
78
+ | `task_run_history` | Page run history | `taskId`, `status?`, `limit?`, `cursor?` |
79
+ | `task_resume` | Recover a failed/interrupted/blocked run | `runId`, `confirmation?` |
80
+
81
+ `task_schedule` actions: `create`, `update`, `pause`, `resume`, `delete`,
82
+ `run_now`, `status`. Timing input accepts `at`, `every`, `rrule`, `cron`,
83
+ `daily`, `weekly`; `daily`/`weekly` are translated to an equivalent `RRULE` so
84
+ the plugin depends on fewer host shapes. The plugin stores only `scheduleId`
85
+ plus a small reference — never a copy of cron state.
86
+
87
+ ## Execution protocol
88
+
89
+ A run is executed **by the calling Agent in its own authorized context**. There
90
+ is no worker process, and a `followup` receipt is never treated as completion.
91
+
92
+ 1. `task_run` or `task_run_begin` → `runId` + snapshot + step list.
93
+ 2. For each step in `order`: `task_step_report(action:"started")`.
94
+ 3. `task_step_report(action:"completed" | "failed" | "skipped")` with a real
95
+ summary. `completed` requires the step's `completionCriteria` to be met.
96
+ 4. Missing input, a pending approval, or an unmet external condition →
97
+ `task_run_finish(status:"blocked", summary:"…")` naming what is awaited.
98
+ 5. All required steps completed → `task_run_finish(status:"succeeded")`.
99
+ 6. A failure → `task_step_report(action:"failed", error:{…})` **then**
100
+ `task_run_finish(status:"failed")`.
101
+
102
+ Progress is always `floor(completed required steps / required steps × 100)` and is
103
+ recomputed from persisted step rows — never from caller claims.
104
+
105
+ ### Scheduled runs
106
+
107
+ A `task_schedule` create stores a prompt carrying `task_id_json` on the host
108
+ schedule. When a delivery arrives:
109
+
110
+ ```text
111
+ task_run_begin({ triggerPayload: "<the delivered message>" })
112
+ ```
113
+
114
+ The tool extracts `task_id_json`, `schedule_id_json` and `occurrence_at`, then
115
+ uses `schedule:<scheduleId>:<occurrence>` as the idempotency key. A duplicated
116
+ delivery returns the existing run with `deduplicated: true` instead of starting a
117
+ second execution. **Delivery proves the message was enqueued — not that the run
118
+ finished.**
119
+
120
+ ## Data model
121
+
122
+ StorageDomain unit `dsh_background_task_engine`, schema version `1`
123
+ (the underscore form is required: the host's unit-name rule is
124
+ `/^[a-z][a-z0-9_]*$/`).
125
+
126
+ | Table | Key | Contents |
127
+ |---|---|---|
128
+ | `tasks` | `taskId` | template, status, revision, owner Session, steps, schedule reference |
129
+ | `task_runs` | `runId` | status, trigger, snapshot, progress, times, summary/error, `resumesRunId` |
130
+ | `task_run_steps` | `runId/stepId` | per-run step status, attempt, times, summary/error |
131
+ | `meta` | `global` | `schemaVersion`, applied-migration ledger, recovery/prune timestamps |
132
+
133
+ Status machines:
134
+
135
+ ```text
136
+ Task active → paused → active active/paused → archived (final)
137
+ TaskRun queued → running → succeeded | failed | blocked | interrupted
138
+ queued → skipped
139
+ blocked → failed | interrupted (never back to running)
140
+ ```
141
+
142
+ `failed`, `succeeded`, `interrupted` and `skipped` are terminal. Recovery always
143
+ creates a **new** run with `resumesRunId`; completed steps are carried over, the
144
+ run continues at the first unfinished step, and a run whose failed step may have
145
+ had side effects requires an explicit `confirmation`.
146
+
147
+ Retention: terminal runs older than `historyRetentionDays` (default 90) are
148
+ pruned with their step rows at startup. Active runs, templates and audit events
149
+ are never pruned.
150
+
151
+ ## Configuration
152
+
153
+ Exported as `Config` (Schemastery) and validated again at load; out-of-range
154
+ values fail the load rather than being clamped.
155
+
156
+ | Key | Default | Range | Meaning |
157
+ |---|---|---|---|
158
+ | `maxTasksPerOwner` | 100 | 1–1000 | unarchived templates per Session |
159
+ | `maxStepsPerTask` | 10 | 1–10 | steps per template (V1.0 is linear) |
160
+ | `maxActiveRunsPerOwner` | 3 | 1–20 | concurrent queued/running runs per Session |
161
+ | `maxParallelRunsPerTask` | 1 | 1 | parallel runs of one task (fixed in V1.0) |
162
+ | `defaultTimeoutMinutes` | 60 | 1–1440 | timeout used for identification only |
163
+ | `staleRunAfterMinutes` | 30 | 1–1440 | age at which a startup scan marks a run `interrupted` |
164
+ | `historyRetentionDays` | 90 | 1–3650 | terminal run retention |
165
+ | `maxSummaryChars` | 2000 | 100–10000 | summary truncation bound |
166
+
167
+ ## Errors
168
+
169
+ | Code | Meaning | Retryable |
170
+ |---|---|---|
171
+ | `TASK_NOT_FOUND` | task missing, or not owned by this Session | no |
172
+ | `TASK_ALREADY_RUNNING` | the task already has an active run | after it ends |
173
+ | `TASK_REVISION_CONFLICT` | `expectedRevision` is stale | re-read, then retry |
174
+ | `TASK_INVALID_STEPS` | bad step definition / count | fix arguments |
175
+ | `TASK_RUN_NOT_FOUND` | run missing or not owned by this Session | no |
176
+ | `TASK_RUN_ALREADY_FINISHED` | run is terminal | start a new run |
177
+ | `TASK_STEP_ORDER_INVALID` | out-of-order, unknown, or duplicate step report | fix order |
178
+ | `TASK_PERMISSION_DENIED` | archived/paused task, or missing resume confirmation | no |
179
+ | `TASK_SCHEDULE_UNAVAILABLE` | Schedule not mounted | after the host provides it |
180
+ | `TASK_SCHEDULE_FAILED` | host schedule operation failed | inspect host state |
181
+ | `TASK_STORAGE_ERROR` | StorageDomain missing, failed, or incompatible | after the host is fixed |
182
+ | `TASK_RUN_INTERRUPTED` | execution lost, marked interrupted | `task_resume` |
183
+ | `TASK_MAX_CONCURRENCY` | per-Session active-run cap reached | after a run ends |
184
+ | `TASK_INVALID_ARGUMENTS` | argument failed validation | fix arguments |
185
+ | `TASK_INTERNAL_ERROR` | unexpected runtime failure | no |
186
+
187
+ The first 13 are the PRD's stable codes; the last two are additive so an
188
+ unexpected failure is never reported as a misleading PRD code.
189
+
190
+ ## Security
191
+
192
+ - **Session isolation.** Every tool entry point resolves the caller Session from
193
+ `exec.agent.session.id` and compares it with the record owner. A record owned by
194
+ another Session is reported as `TASK_NOT_FOUND`, so the error cannot be used to
195
+ probe other Sessions' data.
196
+ - **No privilege escalation.** The engine never adds shell, file, browser or
197
+ network capability. Every action runs through the Agent's already-authorized
198
+ tools and existing approval flow.
199
+ - **Templates are data.** Step `instruction` text is user content with no
200
+ elevated authority; a template cannot change system prompts, permissions or
201
+ tool access. External text is rendered as untrusted single-line text.
202
+ - **No leakage.** Error messages are sanitised: stack frames dropped, absolute
203
+ paths replaced with `<path>`, credential-looking assignments redacted.
204
+ - **No code injection.** Task arguments cannot carry JavaScript, dynamic imports
205
+ or scripts; there is no evaluator anywhere in the plugin.
206
+ - Scheduling is bound to the creating Session, so a scheduled run executes in the
207
+ Session that owns the task.
208
+
209
+ ## Upgrade
210
+
211
+ 1. Bump the bundle; the plugin row keeps its config.
212
+ 2. On next start the engine opens the domain, compares the stored
213
+ `meta.schemaVersion`, and runs any pending migration once each, recording a
214
+ marker **only after** the migration succeeds.
215
+ 3. A stored version **higher** than the plugin supports is refused with
216
+ `TASK_STORAGE_ERROR` — downgrading is not silently allowed.
217
+
218
+ v1.0.0 ships an empty migration set (the baseline shape needs none); the runner
219
+ and ledger exist so a future shape change is a data edit plus one entry.
220
+
221
+ ## Troubleshooting
222
+
223
+ | Symptom | Cause | Fix |
224
+ |---|---|---|
225
+ | Every tool returns `TASK_STORAGE_ERROR` mentioning `ctx.storageDomain` | the Host has no StorageDomain mounted | enable `storage` + `storage-json` + `storage-domain` in the profile |
226
+ | `invalid unit name '…'` | a domain/table name with a hyphen | the unit must match `/^[a-z][a-z0-9_]*$/` (this plugin uses `dsh_background_task_engine`) |
227
+ | `TASK_SCHEDULE_UNAVAILABLE` | `ctx.schedule` is not mounted | scheduling is optional; manual runs still work |
228
+ | Row did not activate: *cannot get property "…" without inject* | a plugin read an optional service as a property | resolve optional services via `ctx.get` |
229
+ | `TASK_REVISION_CONFLICT` | two edits raced | `task_get`, then retry with the current `revision` |
230
+ | A run is `interrupted` after a restart | the Host stopped mid-run; nothing is auto-restarted | `task_resume` with a confirmation if a step may have had side effects |
231
+ | Install rejected: *dsh.bundle.patch must be a file path* | flat manifest form | use `"dsh": { "bundle": { "patch": "./cordis.patch.yml" } }` |
232
+
233
+ ## Development
234
+
235
+ ```sh
236
+ npm install
237
+ npm run build # tsc -> dist/
238
+ npm run typecheck
239
+ npm test # 81 tests
240
+ ```
241
+
242
+ Layout:
243
+
244
+ ```text
245
+ src/
246
+ index.ts plugin entry: name / inject / Config / apply
247
+ host.ts structural host contract (no host package imports)
248
+ config.ts Schemastery Config + resolveConfig
249
+ errors.ts stable codes, payloads, message sanitisation
250
+ domain/records.ts zod record schemas
251
+ domain/state.ts TaskRun/StepRun state machines, progress, ordering
252
+ storage/spec.ts domain declaration, migrations, integrity check
253
+ storage/repository.ts the only reader/writer of domain tables
254
+ services/ audit, task, run, schedule adapter, engine
255
+ tools/ 12 tool definitions + shared plumbing
256
+ prompt/guidance.ts execution protocol and delivery parsing
257
+ tests/ domain, storage, rendering, integration (+ fakes)
258
+ docs/ API compatibility audit, test report, detail page
259
+ ```
260
+
261
+ Verification records: `docs/API-COMPATIBILITY-AUDIT.md` (host capability audit,
262
+ including the real-stack runs) and `docs/TEST-REPORT.md` (all 81 assertions).
263
+
264
+ ## License
265
+
266
+ MIT — see [LICENSE](LICENSE).
package/README.md CHANGED
@@ -1,45 +1,45 @@
1
1
  # dsh-background-task-engine
2
2
 
3
- Durable background task engine for **DeepSeek Harness (DSH)**: reusable task
4
- templates, ordered linear steps, one persisted run ledger, progress and history
5
- queries, explicit failure/interruption recovery, and optional linkage to native
6
- DSH Schedule.
7
-
8
- It does not reimplement a scheduler, an agent loop, a job registry or a storage
9
- backend — it composes the Host's own `ctx.schedule`, `ctx.storageDomain` and
10
- `ctx.tools`.
11
-
12
- - [Install](#install)
13
- - [Tools](#tools)
14
- - [Execution protocol](#execution-protocol)
15
- - [Data model](#data-model)
16
- - [Configuration](#configuration)
17
- - [Errors](#errors)
18
- - [Security](#security)
19
- - [Upgrade](#upgrade)
20
- - [Troubleshooting](#troubleshooting)
21
- - [Development](#development)
22
-
23
- ## Install
24
-
25
- Requirements: DSH **0.2.2-rc.3** or a compatible release with
26
- `ctx.storageDomain` mounted (the stock base bundle does this with
27
- `storage-domain backend: json`).
3
+ [![npm](https://img.shields.io/npm/v/dsh-background-task-engine)](https://www.npmjs.com/package/dsh-background-task-engine)
4
+ [![license](https://img.shields.io/github/license/yzzshanyin-rgb/dsh-background-task-engine)](LICENSE)
5
+
6
+ **[English](README.en.md) | 中文**
7
+
8
+ 给 **DeepSeek Harness(DSH)** 用的持久化后台任务引擎:可复用的任务模板、有序线性步骤、单次运行账本、进度与历史查询、失败/中断后的人工恢复,以及可选的 DSH 原生定时关联。
9
+
10
+ 它不重写调度器、Agent Loop、作业注册表或存储后端,只复用宿主已有的 `ctx.schedule`、`ctx.storageDomain` 和 `ctx.tools`。
11
+
12
+ - [安装](#安装)
13
+ - [工具](#工具)
14
+ - [执行协议](#执行协议)
15
+ - [数据模型](#数据模型)
16
+ - [配置](#配置)
17
+ - [错误码](#错误码)
18
+ - [安全](#安全)
19
+ - [升级](#升级)
20
+ - [排障](#排障)
21
+ - [开发](#开发)
22
+
23
+ ## 安装
24
+
25
+ 要求:DSH **0.2.2-rc.3** 或兼容版本,且已挂载 `ctx.storageDomain`(官方 base bundle 默认以 `storage-domain backend: json` 挂载)。
28
26
 
29
27
  ```sh
30
- # from a local checkout
31
- dsh plugin --profile <name> add /absolute/path/to/dsh-background-task-engine
28
+ # 从 npm 安装(已发布 1.0.2)
29
+ dsh plugin --profile <名称> add dsh-background-task-engine
32
30
 
33
- # or from npm
34
- dsh plugin --profile <name> add dsh-background-task-engine
31
+ # 或从本地目录安装
32
+ dsh plugin --profile <名称> add /绝对路径/dsh-background-task-engine
35
33
  ```
36
34
 
37
- Then restart the app. Confirm in **Plugins** that the row
38
- `background-task-engine` is enabled; the row shows this bundle's title,
39
- description and icon.
35
+ **用目录方式安装时,请先在该目录执行 `npm install`。** 插件会 `import '@deepseek-ai/schemastery'`,缺少 `node_modules` 时宿主会报
36
+ `ERR_MODULE_NOT_FOUND`。走 registry 规格则自带依赖,不需要本地 checkout。
37
+
38
+ 也可以直接在应用内操作:**插件** 页面 → **添加插件** → 粘贴 `dsh-background-task-engine` → 安装 → 启用。
39
+
40
+ 安装后重启应用,在 **插件** 页面确认 `background-task-engine` 一行已启用;该行会显示本包的标题、描述与图标。
40
41
 
41
- The bundle patch is intentionally empty apart from the plugin row, so installing
42
- it does not change any other setting:
42
+ bundle patch 除插件行外不含任何其他内容,因此安装它不会改动任何别的设置:
43
43
 
44
44
  ```yaml
45
45
  - insert:
@@ -48,209 +48,185 @@ it does not change any other setting:
48
48
  config: { ... }
49
49
  ```
50
50
 
51
- ## Tools
51
+ ## 工具
52
52
 
53
- Twelve model-facing tools, all returning
54
- `{ success: true, ... }` or `{ success: false, error: { code, message, retryable, relatedRunId? } }`.
53
+ 12 个模型可见工具,统一返回
54
+ `{ success: true, ... }` 或 `{ success: false, error: { code, message, retryable, relatedRunId? } }`。
55
55
 
56
- | Tool | Purpose | Key arguments |
56
+ | 工具 | 用途 | 关键参数 |
57
57
  |---|---|---|
58
- | `task_create` | Create a template (1–10 steps) | `title`, `description?`, `steps[]`, `copyFromTaskId?` |
59
- | `task_list` | List this Session's templates | `status?`, `query?`, `limit?`, `cursor?` |
60
- | `task_get` | Read one template + recent runs | `taskId`, `includeRuns?` |
61
- | `task_update` | Edit title/description/steps/status | `taskId`, `expectedRevision`, `changes` |
62
- | `task_archive` | Soft-archive; history preserved | `taskId`, `expectedRevision` |
63
- | `task_schedule` | Manage the native schedule link | `taskId`, `action`, `timing?`, `scheduleRule?` |
64
- | `task_run` | Start a manual run now | `taskId`, `manualRunId?`, `defer?` |
65
- | `task_run_begin` | Claim a run (task or queued run or delivery payload) | `taskId?`, `runId?`, `triggerPayload?`, `scheduleId?`, `occurrenceKey?`, `idempotencyKey?` |
66
- | `task_step_report` | Report a step | `runId`, `stepId`, `action`, `summary?`, `error?` |
67
- | `task_run_finish` | Submit the run result | `runId`, `status`, `summary?`, `error?` |
68
- | `task_run_history` | Page run history | `taskId`, `status?`, `limit?`, `cursor?` |
69
- | `task_resume` | Recover a failed/interrupted/blocked run | `runId`, `confirmation?` |
70
-
71
- `task_schedule` actions: `create`, `update`, `pause`, `resume`, `delete`,
72
- `run_now`, `status`. Timing input accepts `at`, `every`, `rrule`, `cron`,
73
- `daily`, `weekly`; `daily`/`weekly` are translated to an equivalent `RRULE` so
74
- the plugin depends on fewer host shapes. The plugin stores only `scheduleId`
75
- plus a small reference — never a copy of cron state.
76
-
77
- ## Execution protocol
78
-
79
- A run is executed **by the calling Agent in its own authorized context**. There
80
- is no worker process, and a `followup` receipt is never treated as completion.
81
-
82
- 1. `task_run` or `task_run_begin` → `runId` + snapshot + step list.
83
- 2. For each step in `order`: `task_step_report(action:"started")`.
84
- 3. `task_step_report(action:"completed" | "failed" | "skipped")` with a real
85
- summary. `completed` requires the step's `completionCriteria` to be met.
86
- 4. Missing input, a pending approval, or an unmet external condition →
87
- `task_run_finish(status:"blocked", summary:"…")` naming what is awaited.
88
- 5. All required steps completed → `task_run_finish(status:"succeeded")`.
89
- 6. A failure → `task_step_report(action:"failed", error:{…})` **then**
90
- `task_run_finish(status:"failed")`.
91
-
92
- Progress is always `floor(completed required steps / required steps × 100)` and is
93
- recomputed from persisted step rows — never from caller claims.
94
-
95
- ### Scheduled runs
96
-
97
- A `task_schedule` create stores a prompt carrying `task_id_json` on the host
98
- schedule. When a delivery arrives:
58
+ | `task_create` | 创建模板(1–10 步) | `title`、`description?`、`steps[]`、`copyFromTaskId?` |
59
+ | `task_list` | 列出本会话的模板 | `status?`、`query?`、`limit?`、`cursor?` |
60
+ | `task_get` | 读取单个模板与最近运行 | `taskId`、`includeRuns?` |
61
+ | `task_update` | 修改标题/描述/步骤/状态 | `taskId`、`expectedRevision`、变更字段 |
62
+ | `task_archive` | 软归档,保留历史 | `taskId`、`expectedRevision` |
63
+ | `task_schedule` | 管理原生定时关联 | `taskId`、`action`、`timing?`、`scheduleRule?` |
64
+ | `task_run` | 立即手动执行 | `taskId`、`manualRunId?`、`defer?` |
65
+ | `task_run_begin` | 认领一次运行(任务 / 排队中的运行 / 投递原文) | `taskId?`、`runId?`、`triggerPayload?`、`scheduleId?`、`occurrenceKey?`、`idempotencyKey?` |
66
+ | `task_step_report` | 上报步骤状态 | `runId`、`stepId`、`action`、`summary?`、`error?` |
67
+ | `task_run_finish` | 提交运行结果 | `runId`、`status`、`summary?`、`error?` |
68
+ | `task_run_history` | 分页查询运行历史 | `taskId`、`status?`、`limit?`、`cursor?` |
69
+ | `task_resume` | 恢复失败/中断/阻塞的运行 | `runId`、`confirmation?` |
70
+
71
+ `task_schedule` 的 action:`create`、`update`、`pause`、`resume`、`delete`、`run_now`、`status`。timing 接受 `at`、`every`、`rrule`、`cron`、`daily`、`weekly`;其中 `daily`/`weekly` 会被转换成等价的 `RRULE`,以减少对宿主 shapes 的依赖。插件只保存 `scheduleId` 与少量引用,绝不复制 Cron 状态。
72
+
73
+ ## 执行协议
74
+
75
+ 运行由**调用方 Agent 在自己的授权上下文中**执行。没有独立 worker 进程,也不会把
76
+ `followup` 回执当作执行完成。
77
+
78
+ 1. `task_run` 或 `task_run_begin` → 取得 `runId`、模板快照与步骤列表。
79
+ 2. 按 `order` 顺序对每一步调用 `task_step_report(action:"started")`。
80
+ 3. 结束时报告 `task_step_report(action:"completed" | "failed" | "skipped")` 并给出真实摘要;
81
+ `completed` 必须满足该步骤的 `completionCriteria`。
82
+ 4. 缺少输入、等待审批或外部条件不满足 → `task_run_finish(status:"blocked", summary:"…")`,并在摘要中写明在等什么。
83
+ 5. 全部必需步骤 completed → `task_run_finish(status:"succeeded")`。
84
+ 6. 失败 → 先 `task_step_report(action:"failed", error:{…})`,**再**
85
+ `task_run_finish(status:"failed")`。
86
+
87
+ 进度恒为 `floor(已完成必需步骤数 / 必需步骤总数 × 100)`,并且始终由已持久化的步骤记录重算——不采信调用方自报。
88
+
89
+ ### 定时触发
90
+
91
+ `task_schedule` 创建时会把带 `task_id_json` 的提示词写入宿主定时任务。投递到达后:
99
92
 
100
93
  ```text
101
- task_run_begin({ triggerPayload: "<the delivered message>" })
94
+ task_run_begin({ triggerPayload: "<投递消息原文>" })
102
95
  ```
103
96
 
104
- The tool extracts `task_id_json`, `schedule_id_json` and `occurrence_at`, then
105
- uses `schedule:<scheduleId>:<occurrence>` as the idempotency key. A duplicated
106
- delivery returns the existing run with `deduplicated: true` instead of starting a
107
- second execution. **Delivery proves the message was enqueued — not that the run
108
- finished.**
97
+ 工具会从中提取 `task_id_json`、`schedule_id_json` 与 `occurrence_at`,并以
98
+ `schedule:<scheduleId>:<occurrence>` 作为幂等键。重复投递会返回已有运行并标记
99
+ `deduplicated: true`,不会重复启动。**投递只证明消息已入队,不代表运行完成。**
109
100
 
110
- ## Data model
101
+ ## 数据模型
111
102
 
112
- StorageDomain unit `dsh_background_task_engine`, schema version `1`
113
- (the underscore form is required: the host's unit-name rule is
114
- `/^[a-z][a-z0-9_]*$/`).
103
+ StorageDomain 单元为 `dsh_background_task_engine`,schema 版本 `1`
104
+ (必须用下划线形式:宿主的单元名规则是 `/^[a-z][a-z0-9_]*$/`)。
115
105
 
116
- | Table | Key | Contents |
106
+ | 表 | 主键 | 内容 |
117
107
  |---|---|---|
118
- | `tasks` | `taskId` | template, status, revision, owner Session, steps, schedule reference |
119
- | `task_runs` | `runId` | status, trigger, snapshot, progress, times, summary/error, `resumesRunId` |
120
- | `task_run_steps` | `runId/stepId` | per-run step status, attempt, times, summary/error |
121
- | `meta` | `global` | `schemaVersion`, applied-migration ledger, recovery/prune timestamps |
108
+ | `tasks` | `taskId` | 模板、状态、revision、所属会话、步骤、定时引用 |
109
+ | `task_runs` | `runId` | 状态、触发来源、快照、进度、时间、摘要/错误、`resumesRunId` |
110
+ | `task_run_steps` | `runId/stepId` | 本次运行的步骤状态、尝试次数、时间、摘要/错误 |
111
+ | `meta` | `global` | `schemaVersion`、迁移台账、恢复/清理时间戳 |
122
112
 
123
- Status machines:
113
+ 状态机:
124
114
 
125
115
  ```text
126
- Task active → paused → active active/paused → archived (final)
116
+ Task active → paused → active active/paused → archived(终态)
127
117
  TaskRun queued → running → succeeded | failed | blocked | interrupted
128
118
  queued → skipped
129
- blocked → failed | interrupted (never back to running)
119
+ blocked → failed | interrupted (绝不回到 running)
130
120
  ```
131
121
 
132
- `failed`, `succeeded`, `interrupted` and `skipped` are terminal. Recovery always
133
- creates a **new** run with `resumesRunId`; completed steps are carried over, the
134
- run continues at the first unfinished step, and a run whose failed step may have
135
- had side effects requires an explicit `confirmation`.
122
+ `failed`、`succeeded`、`interrupted`、`skipped` 均为终态。恢复总是创建**新的**运行并写入
123
+ `resumesRunId`;已完成步骤会被结转,从第一个未完成步骤继续;若失败步骤可能已产生副作用,
124
+ 必须提供显式的 `confirmation`。
136
125
 
137
- Retention: terminal runs older than `historyRetentionDays` (default 90) are
138
- pruned with their step rows at startup. Active runs, templates and audit events
139
- are never pruned.
126
+ 保留策略:终态运行及其步骤行超过 `historyRetentionDays`(默认 90 天)后,会在启动时被清理。
127
+ 活动运行、任务模板与审计事件永不清理。
140
128
 
141
- ## Configuration
129
+ ## 配置
142
130
 
143
- Exported as `Config` (Schemastery) and validated again at load; out-of-range
144
- values fail the load rather than being clamped.
131
+ 以 `Config`(Schemastery)导出,并在加载时二次校验;越界值会让加载失败,而不是被静默截断。
145
132
 
146
- | Key | Default | Range | Meaning |
133
+ | 配置项 | 默认 | 范围 | 含义 |
147
134
  |---|---|---|---|
148
- | `maxTasksPerOwner` | 100 | 1–1000 | unarchived templates per Session |
149
- | `maxStepsPerTask` | 10 | 1–10 | steps per template (V1.0 is linear) |
150
- | `maxActiveRunsPerOwner` | 3 | 1–20 | concurrent queued/running runs per Session |
151
- | `maxParallelRunsPerTask` | 1 | 1 | parallel runs of one task (fixed in V1.0) |
152
- | `defaultTimeoutMinutes` | 60 | 1–1440 | timeout used for identification only |
153
- | `staleRunAfterMinutes` | 30 | 1–1440 | age at which a startup scan marks a run `interrupted` |
154
- | `historyRetentionDays` | 90 | 1–3650 | terminal run retention |
155
- | `maxSummaryChars` | 2000 | 100–10000 | summary truncation bound |
156
-
157
- ## Errors
158
-
159
- | Code | Meaning | Retryable |
135
+ | `maxTasksPerOwner` | 100 | 1–1000 | 每个会话的未归档模板上限 |
136
+ | `maxStepsPerTask` | 10 | 1–10 | 单任务步骤上限(V1.0 为线性) |
137
+ | `maxActiveRunsPerOwner` | 3 | 1–20 | 每个会话并发 queued/running 上限 |
138
+ | `maxParallelRunsPerTask` | 1 | 1 | 同一任务的并行运行数(V1.0 固定) |
139
+ | `defaultTimeoutMinutes` | 60 | 1–1440 | 超时阈值,仅用于识别 |
140
+ | `staleRunAfterMinutes` | 30 | 1–1440 | 启动扫描判定 `interrupted` 的失联时长 |
141
+ | `historyRetentionDays` | 90 | 1–3650 | 终态运行保留天数 |
142
+ | `maxSummaryChars` | 2000 | 100–10000 | 摘要截断上限 |
143
+
144
+ ## 错误码
145
+
146
+ | 错误码 | 含义 | 可重试 |
160
147
  |---|---|---|
161
- | `TASK_NOT_FOUND` | task missing, or not owned by this Session | no |
162
- | `TASK_ALREADY_RUNNING` | the task already has an active run | after it ends |
163
- | `TASK_REVISION_CONFLICT` | `expectedRevision` is stale | re-read, then retry |
164
- | `TASK_INVALID_STEPS` | bad step definition / count | fix arguments |
165
- | `TASK_RUN_NOT_FOUND` | run missing or not owned by this Session | no |
166
- | `TASK_RUN_ALREADY_FINISHED` | run is terminal | start a new run |
167
- | `TASK_STEP_ORDER_INVALID` | out-of-order, unknown, or duplicate step report | fix order |
168
- | `TASK_PERMISSION_DENIED` | archived/paused task, or missing resume confirmation | no |
169
- | `TASK_SCHEDULE_UNAVAILABLE` | Schedule not mounted | after the host provides it |
170
- | `TASK_SCHEDULE_FAILED` | host schedule operation failed | inspect host state |
171
- | `TASK_STORAGE_ERROR` | StorageDomain missing, failed, or incompatible | after the host is fixed |
172
- | `TASK_RUN_INTERRUPTED` | execution lost, marked interrupted | `task_resume` |
173
- | `TASK_MAX_CONCURRENCY` | per-Session active-run cap reached | after a run ends |
174
- | `TASK_INVALID_ARGUMENTS` | argument failed validation | fix arguments |
175
- | `TASK_INTERNAL_ERROR` | unexpected runtime failure | no |
176
-
177
- The first 13 are the PRD's stable codes; the last two are additive so an
178
- unexpected failure is never reported as a misleading PRD code.
179
-
180
- ## Security
181
-
182
- - **Session isolation.** Every tool entry point resolves the caller Session from
183
- `exec.agent.session.id` and compares it with the record owner. A record owned by
184
- another Session is reported as `TASK_NOT_FOUND`, so the error cannot be used to
185
- probe other Sessions' data.
186
- - **No privilege escalation.** The engine never adds shell, file, browser or
187
- network capability. Every action runs through the Agent's already-authorized
188
- tools and existing approval flow.
189
- - **Templates are data.** Step `instruction` text is user content with no
190
- elevated authority; a template cannot change system prompts, permissions or
191
- tool access. External text is rendered as untrusted single-line text.
192
- - **No leakage.** Error messages are sanitised: stack frames dropped, absolute
193
- paths replaced with `<path>`, credential-looking assignments redacted.
194
- - **No code injection.** Task arguments cannot carry JavaScript, dynamic imports
195
- or scripts; there is no evaluator anywhere in the plugin.
196
- - Scheduling is bound to the creating Session, so a scheduled run executes in the
197
- Session that owns the task.
198
-
199
- ## Upgrade
200
-
201
- 1. Bump the bundle; the plugin row keeps its config.
202
- 2. On next start the engine opens the domain, compares the stored
203
- `meta.schemaVersion`, and runs any pending migration once each, recording a
204
- marker **only after** the migration succeeds.
205
- 3. A stored version **higher** than the plugin supports is refused with
206
- `TASK_STORAGE_ERROR` — downgrading is not silently allowed.
207
-
208
- v1.0.0 ships an empty migration set (the baseline shape needs none); the runner
209
- and ledger exist so a future shape change is a data edit plus one entry.
210
-
211
- ## Troubleshooting
212
-
213
- | Symptom | Cause | Fix |
148
+ | `TASK_NOT_FOUND` | 任务不存在,或不属于当前会话 | 否 |
149
+ | `TASK_ALREADY_RUNNING` | 该任务已有活动运行 | 运行结束后 |
150
+ | `TASK_REVISION_CONFLICT` | `expectedRevision` 已过期 | 重新读取后重试 |
151
+ | `TASK_INVALID_STEPS` | 步骤定义或数量非法 | 修正参数 |
152
+ | `TASK_RUN_NOT_FOUND` | 运行不存在,或不属于当前会话 | 否 |
153
+ | `TASK_RUN_ALREADY_FINISHED` | 运行已进入终态 | 创建新运行 |
154
+ | `TASK_STEP_ORDER_INVALID` | 步骤顺序错误、未知步骤或重复上报 | 修正执行顺序 |
155
+ | `TASK_PERMISSION_DENIED` | 任务已归档/暂停,或缺少恢复确认 | 否 |
156
+ | `TASK_SCHEDULE_UNAVAILABLE` | 未挂载 Schedule | 宿主提供后 |
157
+ | `TASK_SCHEDULE_FAILED` | 宿主定时操作失败 | 核对宿主状态 |
158
+ | `TASK_STORAGE_ERROR` | StorageDomain 缺失、失败或不兼容 | 宿主修复后 |
159
+ | `TASK_RUN_INTERRUPTED` | 执行丢失,已标记中断 | 用 `task_resume` |
160
+ | `TASK_MAX_CONCURRENCY` | 达到该会话的活动运行上限 | 运行结束后 |
161
+ | `TASK_INVALID_ARGUMENTS` | 参数校验失败 | 修正参数 |
162
+ | `TASK_INTERNAL_ERROR` | 未预期的运行时错误 | 否 |
163
+
164
+ 前 13 个是 PRD 规定的稳定错误码;后两个为补充码,用来确保意外失败不会被误报成 PRD 错误码。
165
+
166
+ ## 安全
167
+
168
+ - **会话隔离。** 每个工具入口都从 `exec.agent.session.id` 解析调用方会话并与记录归属比对。
169
+ 属于其他会话的记录一律报 `TASK_NOT_FOUND`,因此该错误无法被用来探测他人的数据。
170
+ - **不提升权限。** 引擎不新增 Shell、文件、浏览器或网络能力;所有动作都走 Agent 已授权的
171
+ 工具与既有审批流程。
172
+ - **模板只是数据。** 步骤的 `instruction` 是用户内容,不具更高权限;模板无法改变系统提示、
173
+ 权限或工具可用性。外部文本按不可信单行文本渲染。
174
+ - **不泄露。** 错误信息经过净化:丢弃堆栈、把绝对路径替换为 `<path>`、脱敏疑似凭据的赋值。
175
+ - **不可注入代码。** 任务参数无法携带 JavaScript、动态 import 或脚本;插件内不存在任何求值器。
176
+ - 定时任务绑定创建时的会话,因此到期执行发生在拥有该任务的会话内。
177
+
178
+ ## 升级
179
+
180
+ 1. 升级 bundle;插件行会保留其配置。
181
+ 2. 下次启动时,引擎打开 domain、比对已存 `meta.schemaVersion`,并逐条执行尚未应用的迁移,
182
+ **只有迁移成功后**才写入台账标记。
183
+ 3. 若已存版本**高于**插件支持的版本,直接以 `TASK_STORAGE_ERROR` 拒绝——不允许静默降级。
184
+
185
+ v1.0.0 起附带空迁移集(基线结构无需迁移);运行器与台账已就位,未来结构变更只需新增一条迁移。
186
+
187
+ ## 排障
188
+
189
+ | 现象 | 原因 | 处理 |
214
190
  |---|---|---|
215
- | Every tool returns `TASK_STORAGE_ERROR` mentioning `ctx.storageDomain` | the Host has no StorageDomain mounted | enable `storage` + `storage-json` + `storage-domain` in the profile |
216
- | `invalid unit name '…'` | a domain/table name with a hyphen | the unit must match `/^[a-z][a-z0-9_]*$/` (this plugin uses `dsh_background_task_engine`) |
217
- | `TASK_SCHEDULE_UNAVAILABLE` | `ctx.schedule` is not mounted | scheduling is optional; manual runs still work |
218
- | Row did not activate: *cannot get property "…" without inject* | a plugin read an optional service as a property | resolve optional services via `ctx.get` |
219
- | `TASK_REVISION_CONFLICT` | two edits raced | `task_get`, then retry with the current `revision` |
220
- | A run is `interrupted` after a restart | the Host stopped mid-run; nothing is auto-restarted | `task_resume` with a confirmation if a step may have had side effects |
221
- | Install rejected: *dsh.bundle.patch must be a file path* | flat manifest form | use `"dsh": { "bundle": { "patch": "./cordis.patch.yml" } }` |
191
+ | 所有工具都报 `TASK_STORAGE_ERROR` 并提到 `ctx.storageDomain` | Host 未挂载 StorageDomain | 在 profile 中启用 `storage` + `storage-json` + `storage-domain` |
192
+ | `invalid unit name '…'` | domain/表名里含连字符 | 单元名必须匹配 `/^[a-z][a-z0-9_]*$/`(本插件用 `dsh_background_task_engine`) |
193
+ | `TASK_SCHEDULE_UNAVAILABLE` | 未挂载 `ctx.schedule` | 定时是可选能力,手动执行不受影响 |
194
+ | 插件行未激活:*cannot get property "…" without inject* | 以属性方式读取了可选服务 | 可选服务应通过 `ctx.get` 解析 |
195
+ | `TASK_REVISION_CONFLICT` | 两次修改竞争 | 先 `task_get`,再用最新 `revision` 重试 |
196
+ | 重启后运行变成 `interrupted` | 宿主中途停止,且不会自动重启 | 若某步骤可能有副作用,带 `confirmation` 调用 `task_resume` |
197
+ | 安装被拒:*dsh.bundle.patch must be a file path* | 清单用了扁平写法 | 改用 `"dsh": { "bundle": { "patch": "./cordis.patch.yml" } }` |
222
198
 
223
- ## Development
199
+ ## 开发
224
200
 
225
201
  ```sh
226
202
  npm install
227
203
  npm run build # tsc -> dist/
228
204
  npm run typecheck
229
- npm test # 81 tests
205
+ npm test # 81 个用例
230
206
  ```
231
207
 
232
- Layout:
208
+ 目录结构:
233
209
 
234
210
  ```text
235
211
  src/
236
- index.ts plugin entry: name / inject / Config / apply
237
- host.ts structural host contract (no host package imports)
212
+ index.ts 插件入口:name / inject / Config / apply
213
+ host.ts 结构化宿主契约(不 import 宿主包)
238
214
  config.ts Schemastery Config + resolveConfig
239
- errors.ts stable codes, payloads, message sanitisation
240
- domain/records.ts zod record schemas
241
- domain/state.ts TaskRun/StepRun state machines, progress, ordering
242
- storage/spec.ts domain declaration, migrations, integrity check
243
- storage/repository.ts the only reader/writer of domain tables
244
- services/ audit, task, run, schedule adapter, engine
245
- tools/ 12 tool definitions + shared plumbing
246
- prompt/guidance.ts execution protocol and delivery parsing
247
- tests/ domain, storage, rendering, integration (+ fakes)
248
- docs/ API compatibility audit, test report, detail page
215
+ errors.ts 稳定错误码、错误载荷、消息净化
216
+ domain/records.ts zod 记录 schema
217
+ domain/state.ts TaskRun/StepRun 状态机、进度、顺序守卫
218
+ storage/spec.ts domain 声明、迁移、完整性校验
219
+ storage/repository.ts 唯一读写 domain 表的地方
220
+ services/ 审计、任务、运行、定时适配、引擎
221
+ tools/ 12 个工具定义与共用管道
222
+ prompt/guidance.ts 执行协议与投递解析
223
+ tests/ domain、storage、渲染、集成(含假件)
224
+ docs/ 宿主能力审计、测试报告、详情页
249
225
  ```
250
226
 
251
- Verification records: `docs/API-COMPATIBILITY-AUDIT.md` (host capability audit,
252
- including the real-stack runs) and `docs/TEST-REPORT.md` (all 81 assertions).
227
+ 验证记录:`docs/API-COMPATIBILITY-AUDIT.md`(宿主能力审计,含真实宿主栈运行)与
228
+ `docs/TEST-REPORT.md`(全部 81 条断言)。
253
229
 
254
- ## License
230
+ ## 许可证
255
231
 
256
- MIT — see [LICENSE](LICENSE).
232
+ MIT,见 [LICENSE](LICENSE)。
@@ -101,6 +101,38 @@ must stay a regular dependency (every installed community plugin that defines a
101
101
  `Config` declares it the same way), and the shipped `dist/` is byte-identical to a
102
102
  fresh build, so the published artifact cannot drift from source.
103
103
 
104
+ ## Published artifact verification (npm)
105
+
106
+ After `npm publish` (registry confirms `dsh-background-task-engine@1.0.0`,
107
+ `dist-tags.latest = 1.0.0`, maintainer `anishuodedui`), the **published** package
108
+ was verified independently of the checkout:
109
+
110
+ | Check | Result |
111
+ |---|---|
112
+ | `npm view dsh-background-task-engine version` | `1.0.0`, `latest = 1.0.0` |
113
+ | `npm install dsh-background-task-engine@1.0.0` into a clean project | 5 packages, no errors |
114
+ | Load the installed copy | 26 exports; `name` / `inject` as documented |
115
+ | `apply()` on the installed copy | **12/12 tools register** |
116
+ | `Config({})` on the installed copy | documented defaults |
117
+ | Install into the real desktop profile (`@1.0.0`, not a link) | **`application: applied`**, `warnings: []` |
118
+ | Profile copy shape | real directory from the registry (not a symlink), own `node_modules` |
119
+ | Display resources in the installed copy | `locale/{en,zh}.json`, `icon.svg`, `docs/DETAIL{,.zh}.md`, `docs/preview.svg` all present |
120
+ | Locale titles | `Background task engine` / `后台任务引擎` |
121
+
122
+ ### One installation hazard worth recording
123
+
124
+ `@deepseek-ai/schemastery` resolves only if the dependency is installed. When the
125
+ bundle was installed as a **registry** package immediately after the workspace
126
+ `node_modules` had been removed, the loader resolved through the stale workspace
127
+ path and failed with `ERR_MODULE_NOT_FOUND: Cannot find package
128
+ '@deepseek-ai/schemastery'`. Restoring the workspace dependencies and
129
+ reinstalling from the registry produced a clean `application: applied`.
130
+
131
+ Practical consequence: run `npm install` in the package directory before
132
+ `plugin_manager install_bundle` when handing it a directory path, and prefer the
133
+ registry spec (`dsh-background-task-engine@1.0.0`) once published — it carries its
134
+ own `node_modules` and does not depend on the checkout.
135
+
104
136
  ## Test inventory
105
137
 
106
138
  Every assertion from the last full run is listed below.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-background-task-engine",
3
- "version": "1.0.0",
4
- "description": "Durable background task engine for DeepSeek Harness: task templates, linear step runs, persisted progress, failure recovery and optional DSH Schedule linkage.",
3
+ "version": "1.0.2",
4
+ "description": "DSH 后台任务引擎:可复用任务模板、线性步骤、持久化运行账本、进度与历史查询、失败/中断恢复,可选关联 DSH 原生定时任务。",
5
5
  "keywords": [
6
6
  "deepseek-harness",
7
7
  "dsh",
@@ -31,7 +31,7 @@
31
31
  "docs",
32
32
  "icon.svg",
33
33
  "README.md",
34
- "README.zh.md",
34
+ "README.en.md",
35
35
  "CHANGELOG.md",
36
36
  "LICENSE"
37
37
  ],
package/README.zh.md DELETED
@@ -1,179 +0,0 @@
1
- # dsh-background-task-engine
2
-
3
- DeepSeek Harness 的**后台任务引擎**:可复用的任务模板、有序线性步骤、单次运行账本、进度与历史查询、失败/中断后的人工恢复,以及可选的 DSH 原生定时关联。
4
-
5
- 它不实现调度器、Agent Loop、作业注册表或存储后端,而是复用 Host 自身的 `ctx.schedule`、`ctx.storageDomain` 与 `ctx.tools`。
6
-
7
- - [安装](#安装)
8
- - [工具](#工具)
9
- - [执行协议](#执行协议)
10
- - [数据模型](#数据模型)
11
- - [配置](#配置)
12
- - [错误码](#错误码)
13
- - [安全](#安全)
14
- - [升级](#升级)
15
- - [排障](#排障)
16
- - [开发](#开发)
17
-
18
- ## 安装
19
-
20
- 要求:DSH **0.2.2-rc.3** 或兼容版本,且已挂载 `ctx.storageDomain`(官方 base bundle 默认以 `storage-domain backend: json` 挂载)。
21
-
22
- ```sh
23
- # 本地目录
24
- dsh plugin --profile <名称> add /绝对路径/dsh-background-task-engine
25
-
26
- # 或从 npm
27
- dsh plugin --profile <名称> add dsh-background-task-engine
28
- ```
29
-
30
- 重启应用后,在 **插件** 页面确认 `background-task-engine` 一行已启用,并显示本包的标题、描述与图标。
31
-
32
- ## 工具
33
-
34
- 12 个模型可见工具,统一返回 `{ success: true, ... }` 或 `{ success: false, error: { code, message, retryable, relatedRunId? } }`。
35
-
36
- | 工具 | 用途 | 关键参数 |
37
- |---|---|---|
38
- | `task_create` | 创建模板(1–10 步) | `title`、`description?`、`steps[]`、`copyFromTaskId?` |
39
- | `task_list` | 列出本会话模板 | `status?`、`query?`、`limit?`、`cursor?` |
40
- | `task_get` | 查看模板与最近运行 | `taskId`、`includeRuns?` |
41
- | `task_update` | 修改标题/描述/步骤/状态 | `taskId`、`expectedRevision`、变更字段 |
42
- | `task_archive` | 软归档,保留历史 | `taskId`、`expectedRevision` |
43
- | `task_schedule` | 管理原生定时关联 | `taskId`、`action`、`timing?`、`scheduleRule?` |
44
- | `task_run` | 立即手动执行 | `taskId`、`manualRunId?`、`defer?` |
45
- | `task_run_begin` | 认领一次运行 | `taskId?`、`runId?`、`triggerPayload?`、`scheduleId?`、`occurrenceKey?`、`idempotencyKey?` |
46
- | `task_step_report` | 上报步骤 | `runId`、`stepId`、`action`、`summary?`、`error?` |
47
- | `task_run_finish` | 提交运行结果 | `runId`、`status`、`summary?`、`error?` |
48
- | `task_run_history` | 分页查询运行历史 | `taskId`、`status?`、`limit?`、`cursor?` |
49
- | `task_resume` | 恢复失败/中断/阻塞运行 | `runId`、`confirmation?` |
50
-
51
- `task_schedule` 的 action:`create`、`update`、`pause`、`resume`、`delete`、`run_now`、`status`。timing 支持 `at`、`every`、`rrule`、`cron`、`daily`、`weekly`;其中 `daily`/`weekly` 会转换成等价的 `RRULE`,以减少对宿主 shapes 的依赖。插件只保存 `scheduleId` 与少量引用,不复制任何 Cron 状态。
52
-
53
- ## 执行协议
54
-
55
- 运行由**调用方 Agent 在自己的授权上下文中**执行,没有独立 worker 进程,也不会把 followup 回执当作完成凭证。
56
-
57
- 1. `task_run` 或 `task_run_begin` → 取得 `runId`、模板快照与步骤列表。
58
- 2. 按 `order` 顺序对每一步调用 `task_step_report(action:"started")`。
59
- 3. 结束时报告 `completed` / `failed` / `skipped` 并给出真实摘要;`completed` 必须满足该步骤的 `completionCriteria`。
60
- 4. 缺少输入、等待审批或外部条件不满足 → `task_run_finish(status:"blocked", summary:"…")`,说明在等什么。
61
- 5. 全部必需步骤 completed → `task_run_finish(status:"succeeded")`。
62
- 6. 失败 → 先 `task_step_report(action:"failed", error:{…})`,再 `task_run_finish(status:"failed")`。
63
-
64
- 进度恒为 `floor(已完成必需步骤数 / 必需步骤总数 × 100)`,且始终由已持久化的步骤记录重算,不采信调用方自报。
65
-
66
- ### 定时触发
67
-
68
- `task_schedule` 创建时会把带 `task_id_json` 的提示词写入宿主定时任务。投递到达后:
69
-
70
- ```text
71
- task_run_begin({ triggerPayload: "<投递消息原文>" })
72
- ```
73
-
74
- 工具会从中提取 `task_id_json`、`schedule_id_json` 与 `occurrence_at`,并以 `schedule:<scheduleId>:<occurrence>` 作为幂等键。重复投递返回已有运行并标记 `deduplicated: true`,不会重复启动。**投递只证明消息入队,不代表运行完成。**
75
-
76
- ## 数据模型
77
-
78
- StorageDomain 单元 `dsh_background_task_engine`,schema 版本 `1`(必须使用下划线形式:宿主的单元名规则是 `/^[a-z][a-z0-9_]*$/`)。
79
-
80
- | 表 | 主键 | 内容 |
81
- |---|---|---|
82
- | `tasks` | `taskId` | 模板、状态、revision、所属会话、步骤、定时引用 |
83
- | `task_runs` | `runId` | 状态、触发来源、快照、进度、时间、摘要/错误、`resumesRunId` |
84
- | `task_run_steps` | `runId/stepId` | 本次运行的步骤状态、尝试次数、时间、摘要/错误 |
85
- | `meta` | `global` | `schemaVersion`、迁移台账、恢复/清理时间 |
86
-
87
- 状态机:
88
-
89
- ```text
90
- Task active → paused → active active/paused → archived(终态)
91
- TaskRun queued → running → succeeded | failed | blocked | interrupted
92
- queued → skipped
93
- blocked → failed | interrupted (绝不回到 running)
94
- ```
95
-
96
- `succeeded`、`failed`、`interrupted`、`skipped` 为终态。恢复总是创建**新的**运行并写 `resumesRunId`;已完成步骤被结转,从第一个未完成步骤继续;若失败步骤可能已产生副作用,必须提供 `confirmation`。
97
-
98
- 保留策略:超过 `historyRetentionDays`(默认 90 天)的终态运行及其步骤行会在启动时清理。活动运行、任务模板与审计事件永不清理。
99
-
100
- ## 配置
101
-
102
- 以 `Config`(Schemastery)导出,加载时二次校验;越界值会使加载失败而不是被静默截断。
103
-
104
- | 配置项 | 默认 | 范围 | 含义 |
105
- |---|---|---|---|
106
- | `maxTasksPerOwner` | 100 | 1–1000 | 每个会话未归档模板上限 |
107
- | `maxStepsPerTask` | 10 | 1–10 | 单任务步骤上限(V1.0 为线性) |
108
- | `maxActiveRunsPerOwner` | 3 | 1–20 | 每个会话并发 queued/running 上限 |
109
- | `maxParallelRunsPerTask` | 1 | 1 | 同一任务并行运行数(V1.0 固定) |
110
- | `defaultTimeoutMinutes` | 60 | 1–1440 | 超时识别阈值(仅用于识别) |
111
- | `staleRunAfterMinutes` | 30 | 1–1440 | 启动扫描判定 `interrupted` 的失联时长 |
112
- | `historyRetentionDays` | 90 | 1–3650 | 终态运行保留天数 |
113
- | `maxSummaryChars` | 2000 | 100–10000 | 摘要截断上限 |
114
-
115
- ## 错误码
116
-
117
- | 错误码 | 含义 | 可重试 |
118
- |---|---|---|
119
- | `TASK_NOT_FOUND` | 任务不存在或不属于当前会话 | 否 |
120
- | `TASK_ALREADY_RUNNING` | 已有活动运行 | 运行结束后 |
121
- | `TASK_REVISION_CONFLICT` | `expectedRevision` 已过期 | 重新读取后重试 |
122
- | `TASK_INVALID_STEPS` | 步骤定义或数量非法 | 修正参数 |
123
- | `TASK_RUN_NOT_FOUND` | 运行不存在或不属于当前会话 | 否 |
124
- | `TASK_RUN_ALREADY_FINISHED` | 运行已终态 | 创建新运行 |
125
- | `TASK_STEP_ORDER_INVALID` | 顺序错误、未知步骤或重复上报 | 修正顺序 |
126
- | `TASK_PERMISSION_DENIED` | 任务已归档/暂停,或缺少恢复确认 | 否 |
127
- | `TASK_SCHEDULE_UNAVAILABLE` | Schedule 未挂载 | 宿主提供后 |
128
- | `TASK_SCHEDULE_FAILED` | 宿主定时操作失败 | 核对宿主状态 |
129
- | `TASK_STORAGE_ERROR` | StorageDomain 缺失、失败或不兼容 | 宿主修复后 |
130
- | `TASK_RUN_INTERRUPTED` | 执行丢失,已标记中断 | 用 `task_resume` |
131
- | `TASK_MAX_CONCURRENCY` | 达到会话并发上限 | 运行结束后 |
132
- | `TASK_INVALID_ARGUMENTS` | 参数校验失败 | 修正参数 |
133
- | `TASK_INTERNAL_ERROR` | 未预期的运行时错误 | 否 |
134
-
135
- 前 13 个是 PRD 稳定错误码;后两个为补充,确保意外失败不会被误报成 PRD 错误码。
136
-
137
- ## 安全
138
-
139
- - **会话隔离**:每个工具入口都从 `exec.agent.session.id` 解析调用方并与记录归属比对;他人会话的记录一律报 `TASK_NOT_FOUND`,避免被用来探测。
140
- - **不提升权限**:插件不新增 Shell、文件、浏览器或网络能力;所有动作沿用 Agent 已授权的工具与既有审批流程。
141
- - **模板是数据**:步骤 `instruction` 是用户内容,不具更高权限;模板无法改变系统提示、权限或工具可用性。外部文本按不可信单行文本渲染。
142
- - **不泄露**:错误信息经净化处理——丢弃堆栈、把绝对路径替换为 `<path>`、脱敏疑似凭据。
143
- - **不可注入代码**:任务参数不能携带 JavaScript、动态 import 或脚本,插件内不存在任何求值器。
144
- - 定时任务绑定创建时的会话,因此到期执行发生在拥有该任务的会话内。
145
-
146
- ## 升级
147
-
148
- 1. 升级 bundle,插件行保留其 provenance 与配置。
149
- 2. 下次启动时引擎打开 domain、比对 `meta.schemaVersion`,逐条执行尚未应用的迁移;每条迁移**成功之后**才写入台账标记。
150
- 3. 若存储版本**高于**插件支持版本,直接以 `TASK_STORAGE_ERROR` 拒绝——不允许静默降级。
151
-
152
- v1.0.0 附带空迁移集(基线结构无需迁移),但运行器与台账已就位,未来结构变更只需新增一条迁移。
153
-
154
- ## 排障
155
-
156
- | 现象 | 原因 | 处理 |
157
- |---|---|---|
158
- | 所有工具报 `TASK_STORAGE_ERROR`(提到 `ctx.storageDomain`) | Host 未挂载 StorageDomain | 在 profile 中启用 `storage` + `storage-json` + `storage-domain` |
159
- | `invalid unit name '…'` | domain/表名含连字符 | 单元名必须匹配 `/^[a-z][a-z0-9_]*$/`(本插件用 `dsh_background_task_engine`) |
160
- | `TASK_SCHEDULE_UNAVAILABLE` | 未挂载 `ctx.schedule` | 定时是可选项,手动执行不受影响 |
161
- | 插件行未激活:*cannot get property "…" without inject* | 以属性方式读取可选服务 | 可选服务应通过 `ctx.get` 获取 |
162
- | `TASK_REVISION_CONFLICT` | 两次修改竞争 | 先 `task_get` 取最新 `revision` 再改 |
163
- | 重启后运行变成 `interrupted` | Host 中途停止,且不会自动重启 | 若失败步骤可能有副作用,带 `confirmation` 调用 `task_resume` |
164
- | 安装被拒:*dsh.bundle.patch must be a file path* | 清单用了扁平写法 | 使用 `"dsh": { "bundle": { "patch": "./cordis.patch.yml" } }` |
165
-
166
- ## 开发
167
-
168
- ```sh
169
- npm install
170
- npm run build # tsc -> dist/
171
- npm run typecheck
172
- npm test # 81 个用例
173
- ```
174
-
175
- 目录结构与验证记录见英文 README;宿主能力审计见 `docs/API-COMPATIBILITY-AUDIT.md`,全部 81 条断言见 `docs/TEST-REPORT.md`。
176
-
177
- ## 许可证
178
-
179
- MIT,见 [LICENSE](LICENSE)。