Merge pull request #8767 from deepseek-harness/turtle/dsh-core-guidelines-83ad6c5e5a01
docs: add DSH Core Guidelines
This commit is contained in:
commit
4d4e0b6be1
21 files changed
+407
-9
No files matched your search
@@ -12,6 +12,7 @@ description: Use when reviewing a pull request in the deepseek-harness repo —
|
||||
## Sources of truth
|
||||
|
||||
- [AGENTS.md](../../../AGENTS.md) and [packages/AGENTS.md](../../../packages/AGENTS.md): standing repository and package authoring rules.
|
||||
- [DSH Core Guidelines](../../../docs/core-guidelines/README.md): numbered design rules every change follows; cite a violated rule by its ID.
|
||||
- [docs/defensive-patterns/](../../../docs/defensive-patterns/README.md): the architecture requirements a change must satisfy and the habits behind them. Each page owns one artifact; read every page the diff touches:
|
||||
- [general.md](../../../docs/defensive-patterns/general.md) — timing, async state, value representation, shared ownership, decentralization, refactors, migrations, performance evidence, disposal, callbacks, subprocesses.
|
||||
- [package-surface.md](../../../docs/defensive-patterns/package-surface.md) — exports, package shape, build faces, dependency declarations, resolve hooks.
|
||||
|
||||
@@ -169,9 +169,11 @@ Real-API tests/demos read `DEEPSEEK_API_KEY`, optional `DEEPSEEK_BASE_URL`, and
|
||||
- TODO markers: `FIXME`/`TODO`/`XXX` by urgency ([semantics](docs/development.md)).
|
||||
- Files end with exactly one trailing newline; `git diff --cached --check` (pre-commit) gates it.
|
||||
|
||||
## Defensive patterns
|
||||
## Review rules
|
||||
|
||||
Read [docs/defensive-patterns/README.md](docs/defensive-patterns/README.md) before changing a package surface, remote service, client component or state, conversation or Session behavior, or lifecycle, concurrency, subprocess, and teardown code. Its [review principles](docs/defensive-patterns/README.md#why-these-rules) apply per artifact.
|
||||
Before finishing, self-review against [DSH Core Guidelines](docs/core-guidelines/README.md).
|
||||
|
||||
Read [docs/defensive-patterns/README.md](docs/defensive-patterns/README.md) before changing a package surface, remote service, client component or state, conversation or Session behavior, or lifecycle, concurrency, subprocess, and teardown code.
|
||||
|
||||
## Type safety and documentation
|
||||
|
||||
@@ -183,7 +185,7 @@ Docs accompany every code change: update affected README and JSDoc contracts tog
|
||||
|
||||
## Editing these instructions
|
||||
|
||||
`CLAUDE.md` symlinks `AGENTS.md` at root and `packages/`; edit the real file. Keep each rule self-contained while linking high-level docs. Condense when clarity survives; raise a `verify-doc-budgets` ceiling when the required content genuinely needs more space.
|
||||
`CLAUDE.md` symlinks `AGENTS.md` at root and `packages/`; edit the real file. Keep each rule self-contained while linking high-level docs. Condense when clarity survives; raise a `verify-doc-budgets` ceiling when the required content needs more space.
|
||||
|
||||
## Vendoring policy
|
||||
|
||||
|
||||
+3
-2
@@ -23,7 +23,8 @@ Each fact has one home: the tier whose job it is; elsewhere, link there.
|
||||
| [architecture.md](architecture.md) | Ordered map: composition, core packages, loop, seams, extension points; read before changing `packages/` | Type definitions (→ subsystems), per-package detail (→ package READMEs), decision rationale (→ Agent Notes), implementation-status annotations |
|
||||
| [subsystems/](subsystems/README.md) | One reference page per subsystem: type definitions, semantics, and the generated Cordis API | Behavior narration (→ architecture.md) |
|
||||
| [Agent Notes](../.agents/notes/README.md) | Active decision records: the why, what-was-given-up, and required verification; `implemented/` notes describe shipped reality in present tense | Migration plans, acceptance-task checklists, fixture walkthroughs, and spec-speak ("should…") once the decision has shipped; archived notes are frozen history, never current authority |
|
||||
| [defensive-patterns/](defensive-patterns/README.md) | Rules this repository enforces in review, split by artifact: one general bug-class page plus per-domain pages | Type definitions and semantics (→ subsystems), decision rationale (→ Agent Notes) |
|
||||
| [defensive-patterns/](defensive-patterns/README.md) | Rules this repository enforces in review, split by artifact: one general bug-class page plus per-domain pages | Type definitions and semantics (→ subsystems), decision rationale (→ Agent Notes), design rules for writing code (→ core-guidelines/) |
|
||||
| [core-guidelines/](core-guidelines/README.md) | Numbered design rules for writing code, one file per area (general, frontend, backend, data exchange), in the format its rule G.1 defines | Review checks for one artifact's known bug classes (→ defensive-patterns/) |
|
||||
| [postmortem/](postmortem/README.md) | Incident stories — the only tier where war-story narrative belongs | — |
|
||||
| [Persistence history](persistence-changes/README.md) and [format references](persistence-changes/historical-formats/README.md) | Type-change acknowledgements, release comparisons, and complete historical format schemas | Behavior-only changes; current runtime contracts |
|
||||
| [cookbook/](cookbook/adding-a-package.md) | Step-by-step how-tos with numbered verify steps | Design rationale (→ the Agent Note each guide links) |
|
||||
@@ -33,7 +34,7 @@ Each fact has one home: the tier whose job it is; elsewhere, link there.
|
||||
| Generated reference: the per-page `cordis-surface` regions in [subsystems/](subsystems/README.md), the [Cordis core API + inherited tier](cordis-api/context.md), [tool-catalog](tool-catalog.md), [config-catalog](config-catalog.md), [persistence-catalog](persistence-catalog.md), [module-graph.md](module-graph.md), [cli-help](cli-help.md) | Exhaustive English sources regenerated from source and freshness-gated; reviewed Chinese counterparts follow the [pairing workflow](i18n/README.md#scope-and-exclusions) | Hand edits to generated English sources or regions; Chinese counterparts update through pairing only |
|
||||
| Skills (`.agents/skills/`) | Reusable workflows and specialized decision standards | Product and runtime contracts (→ docs or source) |
|
||||
|
||||
Placement: bugs → postmortems; rationale → Agent Notes; procedures → cookbooks; type definitions → subsystems; package contracts → READMEs; standing orders → root `AGENTS.md` with a rationale link; review-enforced rules → `defensive-patterns/`.
|
||||
Placement: bugs → postmortems; rationale → Agent Notes; procedures → cookbooks; type definitions → subsystems; package contracts → READMEs; standing orders → root `AGENTS.md` with a rationale link; artifact-specific review rules → `defensive-patterns/`; design rules for writing code → `core-guidelines/`.
|
||||
|
||||
## Writing rules
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
# Bilingual-pair consistency record for README.md (docs/i18n/README.md): per heading
|
||||
# section, a hash of its English and Chinese blocks outside code blocks and generated regions.
|
||||
# After editing either side, bring the other along and re-record with:
|
||||
# pnpm run verify-translation-pairing --write docs/core-guidelines/README.md
|
||||
/dsh-core-guidelines:
|
||||
en: 98cc70438bfa7733
|
||||
zh: 2d295d0026eae37c
|
||||
/dsh-core-guidelines/summary:
|
||||
en: 60d80dd9a9b0c5ed
|
||||
zh: a110afe0725237f7
|
||||
/dsh-core-guidelines/sections:
|
||||
en: d01356208379d52c
|
||||
zh: 5db9008bd7fd339f
|
||||
/dsh-core-guidelines/introduction:
|
||||
en: 820be59958fa40a7
|
||||
zh: c7eaf1b6174a7662
|
||||
@@ -0,0 +1,22 @@
|
||||
# DSH Core Guidelines
|
||||
|
||||
English | [中文](README.zh.md)
|
||||
|
||||
## Summary
|
||||
|
||||
Rules for writing DeepSeek Harness code, in the style of the [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines). Each rule has a stable ID and the parts that [G.1](general.md#g1) defines.
|
||||
|
||||
## Sections
|
||||
|
||||
- [G: General](general.md)
|
||||
- [G.1: Write every rule in the standard format](general.md#g1)
|
||||
- [G.2: Avoid callbacks in public interface parameters](general.md#g2)
|
||||
- [F: Frontend](frontend.md)
|
||||
- [B: Backend](backend.md)
|
||||
- [D: Data exchange](data-exchange.md)
|
||||
|
||||
## Introduction
|
||||
|
||||
Before you finish a change, review the code it adds or modifies against every rule in these guidelines. Fix each violation, or state in the change why the rule does not apply. A change does not have to migrate existing code it does not otherwise touch.
|
||||
|
||||
Cite a rule by its ID, for example "violates G.2". Artifact-specific review rules live in [defensive patterns](../defensive-patterns/README.md); read them as well.
|
||||
@@ -0,0 +1,22 @@
|
||||
# DSH 核心准则
|
||||
|
||||
[English](README.md) | 中文
|
||||
|
||||
## 概述
|
||||
|
||||
编写 DeepSeek Harness 代码的规则,风格沿用 [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)。每条规则有一个稳定编号,以及 [G.1](general.zh.md#g1) 规定的各个部分。
|
||||
|
||||
## 分组
|
||||
|
||||
- [G:通用](general.zh.md)
|
||||
- [G.1:每条规则都按标准格式编写](general.zh.md#g1)
|
||||
- [G.2:避免在公共接口参数中使用回调](general.zh.md#g2)
|
||||
- [F:前端](frontend.zh.md)
|
||||
- [B:后端](backend.zh.md)
|
||||
- [D:数据交换](data-exchange.zh.md)
|
||||
|
||||
## 引言
|
||||
|
||||
完成变更之前,对照本准则的每条规则审查该变更新增或修改的代码。修复每处违规,或在变更中说明该规则为何不适用。变更不必迁移它本来不改动的已有代码。
|
||||
|
||||
按编号引用规则,例如“违反 G.2”。针对具体产物的审查规则见[防御性模式](../defensive-patterns/README.zh.md),同样需要阅读。
|
||||
@@ -0,0 +1,7 @@
|
||||
# Bilingual-pair consistency record for backend.md (docs/i18n/README.md): per heading
|
||||
# section, a hash of its English and Chinese blocks outside code blocks and generated regions.
|
||||
# After editing either side, bring the other along and re-record with:
|
||||
# pnpm run verify-translation-pairing --write docs/core-guidelines/backend.md
|
||||
/b-backend:
|
||||
en: 86a5f492360c89e3
|
||||
zh: 2c4a7c2b6b1f9b60
|
||||
@@ -0,0 +1,5 @@
|
||||
# B: Backend
|
||||
|
||||
English | [中文](backend.zh.md)
|
||||
|
||||
Rules for Host-side packages: agent runtime, services, tools, and process management. This section has no rules.
|
||||
@@ -0,0 +1,5 @@
|
||||
# B:后端
|
||||
|
||||
[English](backend.md) | 中文
|
||||
|
||||
适用于 Host 端包的规则:agent(智能体)运行时、服务、工具和进程管理。本节没有规则。
|
||||
@@ -0,0 +1,7 @@
|
||||
# Bilingual-pair consistency record for data-exchange.md (docs/i18n/README.md): per heading
|
||||
# section, a hash of its English and Chinese blocks outside code blocks and generated regions.
|
||||
# After editing either side, bring the other along and re-record with:
|
||||
# pnpm run verify-translation-pairing --write docs/core-guidelines/data-exchange.md
|
||||
/d-data-exchange:
|
||||
en: 38fd72b45c5b46c9
|
||||
zh: fcafd7b2fabdb56d
|
||||
@@ -0,0 +1,5 @@
|
||||
# D: Data exchange
|
||||
|
||||
English | [中文](data-exchange.zh.md)
|
||||
|
||||
Rules for data that crosses a process, network, or storage boundary: `@Remote` and SDK messages, events, and persisted Session data. This section has no rules.
|
||||
@@ -0,0 +1,5 @@
|
||||
# D:数据交换
|
||||
|
||||
[English](data-exchange.md) | 中文
|
||||
|
||||
适用于跨越进程、网络或存储边界的数据的规则:`@Remote` 与 SDK 消息、事件以及持久化的 Session 数据。本节没有规则。
|
||||
@@ -0,0 +1,7 @@
|
||||
# Bilingual-pair consistency record for frontend.md (docs/i18n/README.md): per heading
|
||||
# section, a hash of its English and Chinese blocks outside code blocks and generated regions.
|
||||
# After editing either side, bring the other along and re-record with:
|
||||
# pnpm run verify-translation-pairing --write docs/core-guidelines/frontend.md
|
||||
/f-frontend:
|
||||
en: 4316a532a8a13a76
|
||||
zh: 541fe22ee04024bd
|
||||
@@ -0,0 +1,5 @@
|
||||
# F: Frontend
|
||||
|
||||
English | [中文](frontend.zh.md)
|
||||
|
||||
Rules for client packages and apps that render the Web and Desktop UI. This section has no rules.
|
||||
@@ -0,0 +1,5 @@
|
||||
# F:前端
|
||||
|
||||
[English](frontend.md) | 中文
|
||||
|
||||
适用于渲染 Web 和 Desktop UI 的客户端包与应用的规则。本节没有规则。
|
||||
@@ -0,0 +1,49 @@
|
||||
# Bilingual-pair consistency record for general.md (docs/i18n/README.md): per heading
|
||||
# section, a hash of its English and Chinese blocks outside code blocks and generated regions.
|
||||
# After editing either side, bring the other along and re-record with:
|
||||
# pnpm run verify-translation-pairing --write docs/core-guidelines/general.md
|
||||
/g-general:
|
||||
en: eeded9c200e31c82
|
||||
zh: 488049fad75b1944
|
||||
/g-general/g-1-write-every-rule-in-the-standard-format:
|
||||
en: 812e982277f154f9
|
||||
zh: 85d293456add404e
|
||||
/g-general/g-1-write-every-rule-in-the-standard-format/reason:
|
||||
en: 58a1a93bb1664942
|
||||
zh: 83055bf39becba0a
|
||||
/g-general/g-1-write-every-rule-in-the-standard-format/example-bad:
|
||||
en: 0fafa3d89d20b56c
|
||||
zh: f99acb925b2ca7f1
|
||||
/g-general/g-1-write-every-rule-in-the-standard-format/example:
|
||||
en: b9b5449b4e5cc2bf
|
||||
zh: fa9be57f4cf4a267
|
||||
/g-general/g-1-write-every-rule-in-the-standard-format/note:
|
||||
en: bd7d2c77aac162e2
|
||||
zh: 6187fa099e222e66
|
||||
/g-general/g-1-write-every-rule-in-the-standard-format/enforcement:
|
||||
en: 696490996bd95844
|
||||
zh: b8ded650efa9883b
|
||||
/g-general/g-2-avoid-callbacks-in-public-interface-parameters:
|
||||
en: b13d4402873fda38
|
||||
zh: a3fb45bd1563258f
|
||||
/g-general/g-2-avoid-callbacks-in-public-interface-parameters/reason:
|
||||
en: e8a0c671e66da673
|
||||
zh: 86352f61cfd97ac8
|
||||
/g-general/g-2-avoid-callbacks-in-public-interface-parameters/example-bad:
|
||||
en: 6591f36eead7f5e1
|
||||
zh: e79e6a3200e5e889
|
||||
/g-general/g-2-avoid-callbacks-in-public-interface-parameters/example:
|
||||
en: 55c0bd9d10b26852
|
||||
zh: 1b908e550a18e2da
|
||||
/g-general/g-2-avoid-callbacks-in-public-interface-parameters/alternative:
|
||||
en: f0dabc1cf7cd8b19
|
||||
zh: b00ed9e4f57c9111
|
||||
/g-general/g-2-avoid-callbacks-in-public-interface-parameters/exception:
|
||||
en: 8533ab3ff7b2c4b6
|
||||
zh: a05d8df7545508c8
|
||||
/g-general/g-2-avoid-callbacks-in-public-interface-parameters/note:
|
||||
en: 7b73640586b3aeba
|
||||
zh: 577477b5215ee03a
|
||||
/g-general/g-2-avoid-callbacks-in-public-interface-parameters/enforcement:
|
||||
en: 90b1409e10d2a31f
|
||||
zh: c54325d63795657e
|
||||
@@ -0,0 +1,117 @@
|
||||
# G: General
|
||||
|
||||
English | [中文](general.zh.md)
|
||||
|
||||
Rules for all code in this repository, and for these guidelines.
|
||||
|
||||
<a id="g1"></a>
|
||||
## G.1: Write every rule in the standard format
|
||||
|
||||
### Reason
|
||||
|
||||
A fixed format puts the requirement, its justification, and its limits in the same place in every rule. A stable ID lets reviews cite a rule without quoting it.
|
||||
|
||||
### Example, bad
|
||||
|
||||
```md
|
||||
## Callbacks
|
||||
|
||||
Callbacks caused bugs before, so prefer other designs where possible.
|
||||
```
|
||||
|
||||
No ID. No imperative title. The requirement is buried in the rationale, and nothing says how to detect a violation.
|
||||
|
||||
### Example
|
||||
|
||||
```md
|
||||
<a id="g9"></a>
|
||||
## G.9: <One imperative sentence>
|
||||
|
||||
### Reason
|
||||
### Example, bad
|
||||
### Example
|
||||
### Enforcement
|
||||
```
|
||||
|
||||
### Note
|
||||
|
||||
A rule section is a bilingual pair in this directory other than `README.md`, named for the section, such as `general.md` and `general.zh.md`. Its level-1 heading is `<Letter>: <Name>`, followed by one paragraph that states its scope. `<Letter>` is one or more capital letters, unique among sections. [README.md](README.md) lists every section and rule; add each new rule there in the same change.
|
||||
|
||||
A rule is a level-2 heading `<Letter>.<n>: <Title>` in its section file. An `<a id>` anchor, the lowercased ID without the dot, precedes the heading. `<n>` counts from 1 in order of addition. A removed rule's number is never reused.
|
||||
|
||||
The title is one imperative sentence that states the requirement.
|
||||
|
||||
The body is a sequence of level-3 headings in this order. `Reason` and `Enforcement` are required. Give at least one of `Example, bad` and `Example`. Omit any other heading that has no content.
|
||||
|
||||
- `Reason`: the defect or cost the rule prevents.
|
||||
- `Example, bad`: a violation.
|
||||
- `Example`: compliant code for the same case.
|
||||
- `Alternative`: what to use instead of a forbidden construct.
|
||||
- `Exception`: cases the rule does not cover.
|
||||
- `Note`: definitions and details.
|
||||
- `Enforcement`: the check that rejects a violation, or what a reviewer flags.
|
||||
|
||||
Write short, direct sentences.
|
||||
|
||||
### Enforcement
|
||||
|
||||
Review: flag a rule that deviates from this format. `verify-translation-pairing` rejects a section whose two language versions differ in structure.
|
||||
|
||||
<a id="g2"></a>
|
||||
## G.2: Avoid callbacks in public interface parameters
|
||||
|
||||
### Reason
|
||||
|
||||
- Hard to read and reason about. The type of a callback parameter does not say when the callee calls it, how often, or what happens when it throws.
|
||||
- Not serializable. A function cannot cross a process boundary, so the interface cannot be a `@Remote` or SDK method.
|
||||
|
||||
### Example, bad
|
||||
|
||||
```ts
|
||||
export interface UploadProgress { readonly loaded: number; readonly total?: number }
|
||||
|
||||
/** Upload `data`; `onProgress` runs for each chunk. */
|
||||
export declare function upload(
|
||||
data: Uint8Array,
|
||||
onProgress?: (progress: UploadProgress) => void,
|
||||
): Promise<string>
|
||||
```
|
||||
|
||||
Can `onProgress` run after the promise settles? What happens when it throws? The signature does not say.
|
||||
|
||||
### Example
|
||||
|
||||
```ts
|
||||
export type UploadEvent =
|
||||
| { readonly type: 'progress'; readonly loaded: number; readonly total?: number }
|
||||
| { readonly type: 'done'; readonly fileId: string }
|
||||
|
||||
/**
|
||||
* Upload `data`. Yields one `progress` per chunk, then one `done`.
|
||||
* A failure rejects the next read; leaving the loop cancels the upload.
|
||||
*/
|
||||
export declare function upload(data: Uint8Array): AsyncIterable<UploadEvent>
|
||||
```
|
||||
|
||||
Order, failure, and cancellation follow the iteration protocol. The caller's exceptions stay in the caller's loop.
|
||||
|
||||
### Alternative
|
||||
|
||||
- One result: return a `Promise`.
|
||||
- An intermediate result, such as the id of a created child before a later step: split the operation into calls that the caller sequences.
|
||||
- A sequence of values: return an `AsyncIterable`.
|
||||
- A notification for any number of listeners: emit a typed Cordis event that consumers subscribe to with `ctx.on()`.
|
||||
- A replaceable strategy or factory: declare a Cordis service that providers implement.
|
||||
|
||||
### Exception
|
||||
|
||||
- An event handler: a function or object that a caller registers to receive events, such as a `ctx.on()` listener (the registration returns its disposer), or a client component's event prop such as `onClick`. A waterfall `next()` passed to a handler is part of the handler's signature.
|
||||
- A function that the callee finishes calling before the call returns or its promise settles, whose exceptions reach the caller, and that the callee does not keep, such as a comparator or a task run under a lock.
|
||||
|
||||
### Note
|
||||
|
||||
A public interface is a package entry-point export, a Cordis service method, a `@Remote` method, or an SDK method.
|
||||
|
||||
### Enforcement
|
||||
|
||||
Review: flag a function-typed parameter, or a function-typed field of an options parameter, in a new or changed public interface that no exception covers.
|
||||
@@ -0,0 +1,117 @@
|
||||
# G:通用
|
||||
|
||||
[English](general.md) | 中文
|
||||
|
||||
适用于本仓库全部代码以及本准则本身的规则。
|
||||
|
||||
<a id="g1"></a>
|
||||
## G.1:每条规则都按标准格式编写
|
||||
|
||||
### 原因
|
||||
|
||||
固定格式让每条规则的要求、理由和适用范围都出现在同一位置。稳定编号让审查无需引述原文即可引用规则。
|
||||
|
||||
### 反例
|
||||
|
||||
```md
|
||||
## Callbacks
|
||||
|
||||
Callbacks caused bugs before, so prefer other designs where possible.
|
||||
```
|
||||
|
||||
没有编号,标题不是祈使句。要求埋在理由里,也没有说明如何发现违规。
|
||||
|
||||
### 示例
|
||||
|
||||
```md
|
||||
<a id="g9"></a>
|
||||
## G.9: <One imperative sentence>
|
||||
|
||||
### Reason
|
||||
### Example, bad
|
||||
### Example
|
||||
### Enforcement
|
||||
```
|
||||
|
||||
### 注解
|
||||
|
||||
一个规则分组是本目录中除 `README.md` 外、以分组命名的一对双语文件,例如 `general.md` 和 `general.zh.md`。其一级标题为 `<字母>:<名称>`,后接一段说明该分组范围的文字。`<字母>` 由一个或多个大写字母组成,在各分组之间唯一。[README.zh.md](README.zh.md) 列出全部分组和规则;新增规则时在同一变更中把它加入其中。
|
||||
|
||||
一条规则是所属分组文件中的一个二级标题 `<字母>.<n>:<标题>`。标题前放一个 `<a id>` 锚点,取值为去掉点号的小写编号。`<n>` 按添加顺序从 1 开始计数。删除的规则编号不再复用。
|
||||
|
||||
标题是一句陈述要求的祈使句。
|
||||
|
||||
正文是按以下顺序排列的三级标题。`原因` 和 `检查方式` 必需。`反例` 与 `示例` 至少给出一个。其他标题没有内容时省略。括号中是英文版的标题。
|
||||
|
||||
- `原因`(`Reason`):规则防止的缺陷或代价。
|
||||
- `反例`(`Example, bad`):一处违规。
|
||||
- `示例`(`Example`):同一场景下合规的代码。
|
||||
- `替代方案`(`Alternative`):被禁止的写法应替换成什么。
|
||||
- `例外`(`Exception`):规则不覆盖的情形。
|
||||
- `注解`(`Note`):定义与细节。
|
||||
- `检查方式`(`Enforcement`):拒绝违规的检查,或审查者需要标记的情形。
|
||||
|
||||
句子简短直接。
|
||||
|
||||
### 检查方式
|
||||
|
||||
审查:标记偏离此格式的规则。`verify-translation-pairing` 拒绝两种语言版本结构不一致的分组文件。
|
||||
|
||||
<a id="g2"></a>
|
||||
## G.2:避免在公共接口参数中使用回调
|
||||
|
||||
### 原因
|
||||
|
||||
- 难以阅读和推理。回调参数的类型没有说明被调用方何时调用它、调用几次,以及它抛异常时会怎样。
|
||||
- 无法序列化。函数无法跨越进程边界,因此这样的接口不能作为 `@Remote` 方法或 SDK 方法。
|
||||
|
||||
### 反例
|
||||
|
||||
```ts
|
||||
export interface UploadProgress { readonly loaded: number; readonly total?: number }
|
||||
|
||||
/** Upload `data`; `onProgress` runs for each chunk. */
|
||||
export declare function upload(
|
||||
data: Uint8Array,
|
||||
onProgress?: (progress: UploadProgress) => void,
|
||||
): Promise<string>
|
||||
```
|
||||
|
||||
`onProgress` 会不会在 promise 落定后运行?它抛异常时会怎样?签名没有说明。
|
||||
|
||||
### 示例
|
||||
|
||||
```ts
|
||||
export type UploadEvent =
|
||||
| { readonly type: 'progress'; readonly loaded: number; readonly total?: number }
|
||||
| { readonly type: 'done'; readonly fileId: string }
|
||||
|
||||
/**
|
||||
* Upload `data`. Yields one `progress` per chunk, then one `done`.
|
||||
* A failure rejects the next read; leaving the loop cancels the upload.
|
||||
*/
|
||||
export declare function upload(data: Uint8Array): AsyncIterable<UploadEvent>
|
||||
```
|
||||
|
||||
顺序、失败和取消都遵循迭代协议。调用方的异常留在调用方自己的循环里。
|
||||
|
||||
### 替代方案
|
||||
|
||||
- 单个结果:返回 `Promise`。
|
||||
- 中间结果,例如后续步骤之前创建的子项 id:把操作拆成由调用方按顺序发起的多次调用。
|
||||
- 一系列值:返回 `AsyncIterable`。
|
||||
- 面向任意数量监听器的通知:发出类型化 Cordis 事件,由消费方通过 `ctx.on()` 订阅。
|
||||
- 可替换的策略或工厂:声明一个由提供方实现的 Cordis 服务。
|
||||
|
||||
### 例外
|
||||
|
||||
- 事件处理函数:调用方为接收事件而注册的函数或对象,例如 `ctx.on()` 的监听器(注册返回其 disposer),以及客户端组件的事件 prop,例如 `onClick`。传给处理函数的 waterfall `next()` 属于处理函数签名的一部分。
|
||||
- 被调用方在调用返回或其 promise 落定之前就已调用完毕、异常会传回调用方、且不会保留的函数,例如比较函数或在锁内运行的任务。
|
||||
|
||||
### 注解
|
||||
|
||||
公共接口指包入口导出、Cordis 服务方法、`@Remote` 方法或 SDK 方法。
|
||||
|
||||
### 检查方式
|
||||
|
||||
审查:在新增或修改的公共接口中,标记任何不属于例外的函数类型参数,或选项参数中的函数类型字段。
|
||||
@@ -6,5 +6,5 @@
|
||||
en: 7229c49e920aa5b3
|
||||
zh: 1dd52db6b4d84952
|
||||
/defensive-patterns/why-these-rules:
|
||||
en: 7c012071b5af8c98
|
||||
zh: 298aaba83625c7a0
|
||||
en: 3ba292a9f396a390
|
||||
zh: 5b99713fe1169f9c
|
||||
@@ -22,4 +22,4 @@ Five habits cover most of what follows, and each page is their application to on
|
||||
| Session reads, retention, conversation nodes, renderers | [conversation-and-session.md](conversation-and-session.md) |
|
||||
| Timing, state representation, shared ownership, decentralization, refactors, migrations, performance evidence, disposal, callbacks, subprocesses | [general.md](general.md) |
|
||||
|
||||
Type definitions and per-subsystem semantics live in [subsystems/](../subsystems/README.md); this directory carries the rules, and the semantics pages carry what the types mean. [dsh-code-review](../../.agents/skills/dsh-code-review/SKILL.md) reads this directory when judging a pull request, and Development routes through the same table from [AGENTS.md](../../AGENTS.md).
|
||||
Type definitions and per-subsystem semantics live in [subsystems/](../subsystems/README.md); this directory carries the rules, and the semantics pages carry what the types mean. [dsh-code-review](../../.agents/skills/dsh-code-review/SKILL.md) reads this directory when judging a pull request, and Development routes through the same table from [AGENTS.md](../../AGENTS.md). Numbered design rules for writing code live in [DSH Core Guidelines](../core-guidelines/README.md).
|
||||
@@ -24,4 +24,4 @@
|
||||
| Session 读取、持有、会话节点、renderer | [conversation-and-session.zh.md](conversation-and-session.zh.md) |
|
||||
| 时序、状态表示、共享归属、去中心化、重构、迁移、性能证据、清理、回调、子进程 | [general.zh.md](general.zh.md) |
|
||||
|
||||
类型定义与各子系统语义见 [subsystems/](../subsystems/README.zh.md);本目录承载**规则**,语义页承载**类型的含义**。[dsh-code-review](../../.agents/skills/dsh-code-review/SKILL.md) 在评审 pull request 时读本目录,日常修改经 [AGENTS.md](../../AGENTS.md) 路由到同一张表。
|
||||
类型定义与各子系统语义见 [subsystems/](../subsystems/README.zh.md);本目录承载**规则**,语义页承载**类型的含义**。[dsh-code-review](../../.agents/skills/dsh-code-review/SKILL.md) 在评审 pull request 时读本目录,日常修改经 [AGENTS.md](../../AGENTS.md) 路由到同一张表。带编号的代码设计规则见 [DSH 核心准则](../core-guidelines/README.zh.md)。
|
||||
Reference in new issue
Block a user