forked from cflmy/deepseek-harness
docs(defensive-patterns): state the Cordis root context rule
This commit is contained in:
1 parent
531d65f938
commit
c9c0ad58cc
7 files changed
+70
-6
No files matched your search
@@ -9,8 +9,8 @@
|
||||
en: c6935cb7ef54f299
|
||||
zh: 214ed828b3201539
|
||||
/agent-note-prohibit-new-reads-of-the-cordis-root-context/decision:
|
||||
en: f44b447ba08b8572
|
||||
zh: 18347eed362c62d8
|
||||
en: 458cdad9fce26157
|
||||
zh: 6ac3b9d7404b4996
|
||||
/agent-note-prohibit-new-reads-of-the-cordis-root-context/alternatives-considered:
|
||||
en: 81d959fa34f3e682
|
||||
zh: 861f22528134e255
|
||||
|
||||
@@ -16,7 +16,7 @@ New reads of the Cordis root context are prohibited in first-party JavaScript an
|
||||
|
||||
Existing reads have an exact baseline in `scripts/cordis-root.baseline.json`, kept by [the shared inventory module](../../../../scripts/baseline-inventory.ts) under the rules of the [unknown-cast inventory](2026-09-19-no-unknown-casts.md): baseline edits only delete entries or decrease counts, `--prune` removes retired entries and refuses to write while new reads exist, and no command adds an allowance. A read's fingerprint covers the tokens of the element access whose key is the root symbol, so a recorded read moved to another receiver is a new finding.
|
||||
|
||||
Files that exercise the root contract itself are listed with a reason in `CORDIS_ROOT_EXEMPTIONS` inside the checker and are not scanned: the Cordis rc.12 specification and the checker. An exemption whose file no longer reads the root fails the gate. Vendored sources and frozen archived Agent Notes keep their inventory exclusions. The checker runs in CI static and primary modes and the local `hygiene` and `check-all` profiles in [the gate runner](../../../../scripts/run-gates.ts).
|
||||
Files that exercise the root contract itself are listed with a reason in `CORDIS_ROOT_EXEMPTIONS` inside the checker and are not scanned: the Cordis rc.12 specification and the checker. An exemption whose file no longer reads the root fails the gate. Vendored sources and frozen archived Agent Notes keep their inventory exclusions. The checker runs in CI static and primary modes and the local `hygiene` and `check-all` profiles in [the gate runner](../../../../scripts/run-gates.ts). The [defensive pattern](../../../../docs/defensive-patterns/general.md#plugins-do-not-read-the-cordis-root-context) states the rule and its replacements for authors, and the gate's rejection message links it.
|
||||
|
||||
## Alternatives considered
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ Status: implemented
|
||||
|
||||
现有读取使用 `scripts/cordis-root.baseline.json` 中的精确基线记录,由[共享清单模块](../../../../scripts/baseline-inventory.ts)按 [unknown 类型断言清单](2026-09-19-no-unknown-casts.zh.md)的规则维护:基线修改只能删除条目或减少次数,`--prune` 删除已失效的条目并在存在新读取时拒绝写入,且没有任何命令可以添加放行。读取的指纹覆盖以根符号为键的元素访问的 token,因此把已记录的读取换到另一个接收者上会产生新的检查发现。
|
||||
|
||||
测试根上下文约定本身的文件在检查器内的 `CORDIS_ROOT_EXEMPTIONS` 中逐个列出并注明原因,不参与扫描:Cordis rc.12 规范测试和检查器本身。若豁免文件不再读取根上下文,门禁失败。vendored 源码和已冻结的归档 Agent Notes 保留清单的排除规则。检查器参与[门禁运行器](../../../../scripts/run-gates.ts)中的 CI static 和 primary 模式以及本地 `hygiene` 和 `check-all` 配置。
|
||||
测试根上下文约定本身的文件在检查器内的 `CORDIS_ROOT_EXEMPTIONS` 中逐个列出并注明原因,不参与扫描:Cordis rc.12 规范测试和检查器本身。若豁免文件不再读取根上下文,门禁失败。vendored 源码和已冻结的归档 Agent Notes 保留清单的排除规则。检查器参与[门禁运行器](../../../../scripts/run-gates.ts)中的 CI static 和 primary 模式以及本地 `hygiene` 和 `check-all` 配置。[防御性模式](../../../../docs/defensive-patterns/general.zh.md#plugins-do-not-read-the-cordis-root-context)为作者写明该规则及替代方式,门禁的拒绝信息链接到这一节。
|
||||
|
||||
## 考虑过的替代方案
|
||||
|
||||
|
||||
@@ -28,7 +28,10 @@
|
||||
zh: bb6db31a63bcf383
|
||||
/defensive-patterns/shared-state-has-one-owner:
|
||||
en: 053e2c485eec2613
|
||||
zh: 2817da00572c789f
|
||||
zh: 637cb8ed0e6bf664
|
||||
/defensive-patterns/plugins-do-not-read-the-cordis-root-context:
|
||||
en: 88f70e265c218094
|
||||
zh: 975eebb43cbdf4f1
|
||||
/defensive-patterns/a-business-capability-owns-itself:
|
||||
en: f74f2467e507455f
|
||||
zh: fe1bae04be5ad63a
|
||||
|
||||
@@ -121,6 +121,35 @@ globalThis.__dshCache = new Map() // shared across instances, reclaimed never
|
||||
|
||||
The wrong form lets instances overwrite each other, makes reclamation timing unknowable, and leaks state between tests.
|
||||
|
||||
## Plugins do not read the Cordis root context
|
||||
|
||||
A plugin reaches services, listeners, and registrations through its own context, never through the root context that `ctx[Context.root]` returns. A read through the root skips the `inject` check, because the root fiber has no runtime; a listener on the root skips scope filtering; the root resolves services in the root isolate realm instead of the plugin's; and a listener or accessor registered there is not removed when the plugin is disposed.
|
||||
|
||||
The judged condition: name the declared mechanism each use replaces. A service read is an `inject` entry; a listener that must observe every scope passes `{ global: true }`; a per-application key or a root-level boot operation is a service that owns it. The boundary is first-party JavaScript and TypeScript, including tests and fixtures, and covers `Context.root`, `symbols.root`, and `Symbol.for('cordis.root')`, which name the same symbol. [verify-cordis-root.ts](../../scripts/verify-cordis-root.ts), run as `pnpm run verify-cordis-root` and by `scripts/run-gates.ts` as the `cordis-root` gate, rejects new reads. Exception: the files listed in its `CORDIS_ROOT_EXEMPTIONS`, which pin the root contract itself. The reads recorded in `scripts/cordis-root.baseline.json` are remediation work owned by their packages, not a precedent for new code; the [decision record](../../.agents/notes/implemented/process/2026-11-13-no-new-cordis-root-reads.md) states the rationale.
|
||||
|
||||
```ts
|
||||
// Services come from inject; a listener that observes every scope says so.
|
||||
interface Loader { await(): Promise<void> }
|
||||
interface Context {
|
||||
readonly loader: Loader
|
||||
on(name: string, listener: () => void, options?: { global?: boolean }): () => void
|
||||
}
|
||||
|
||||
export const inject = ['loader']
|
||||
|
||||
export function apply(ctx: Context, listener: () => void): void {
|
||||
void ctx.loader.await()
|
||||
ctx.on('agent/inbox/inserted', listener, { global: true })
|
||||
}
|
||||
```
|
||||
|
||||
```ts ignore-check
|
||||
void ctx[Context.root].loader.await() // no inject entry, no wait
|
||||
ctx[Context.root].on('agent/inbox/inserted', listener) // escapes scope filtering and plugin disposal
|
||||
```
|
||||
|
||||
The wrong form starts a plugin before its dependency exists, lets it observe and change state outside its scope and realm, and leaves registrations behind after the plugin is disposed.
|
||||
|
||||
## A business capability owns itself
|
||||
|
||||
A business capability lives in its own package and attaches through a registration surface — a registry, `slots.inject`, a waterfall, or an event subscription. A runtime, controller, manager, or gateway carries generic orchestration: it may hold the registry structure and the disposal lifecycle, while the entries are written by the packages that own them, and it never hardcodes a central table of business rows.
|
||||
|
||||
@@ -137,6 +137,37 @@ globalThis.__dshCache = new Map() // shared across instances, reclaimed never
|
||||
|
||||
反例让实例之间互相覆盖、回收时机不可控,并让状态在测试之间泄漏。
|
||||
|
||||
<a id="plugins-do-not-read-the-cordis-root-context"></a>
|
||||
|
||||
## 插件不读取 Cordis 根上下文
|
||||
|
||||
插件通过自己的上下文获取服务、监听事件和注册内容,绝不经由 `ctx[Context.root]` 返回的根上下文。经由根上下文读取会跳过 `inject` 检查,因为根 fiber 没有 runtime;在根上下文上注册的监听器会跳过 scope 过滤;根上下文在根 isolate realm 中解析服务,而不是插件所在的 realm;在根上下文上注册的监听器或 accessor 不会随插件释放而移除。
|
||||
|
||||
判定条件:为每处用法说出它替代的显式声明机制。读取服务对应一条 `inject` 条目;必须观察所有 scope 的监听器传入 `{ global: true }`;应用级的键或根级启动操作由拥有它的服务提供。边界是第一方 JavaScript 和 TypeScript(包括测试和 fixture),覆盖 `Context.root`、`symbols.root` 和 `Symbol.for('cordis.root')`,三者指向同一个符号。[verify-cordis-root.ts](../../scripts/verify-cordis-root.ts) 通过 `pnpm run verify-cordis-root` 运行,并由 `scripts/run-gates.ts` 作为 `cordis-root` 门禁运行,拒绝新增读取。例外:其 `CORDIS_ROOT_EXEMPTIONS` 中列出的文件,它们测试根上下文约定本身。`scripts/cordis-root.baseline.json` 中记录的读取是各包所有者待清理的工作,不是新代码的先例;理由见[决策记录](../../.agents/notes/implemented/process/2026-11-13-no-new-cordis-root-reads.zh.md)。
|
||||
|
||||
```ts
|
||||
// Services come from inject; a listener that observes every scope says so.
|
||||
interface Loader { await(): Promise<void> }
|
||||
interface Context {
|
||||
readonly loader: Loader
|
||||
on(name: string, listener: () => void, options?: { global?: boolean }): () => void
|
||||
}
|
||||
|
||||
export const inject = ['loader']
|
||||
|
||||
export function apply(ctx: Context, listener: () => void): void {
|
||||
void ctx.loader.await()
|
||||
ctx.on('agent/inbox/inserted', listener, { global: true })
|
||||
}
|
||||
```
|
||||
|
||||
```ts ignore-check
|
||||
void ctx[Context.root].loader.await() // no inject entry, no wait
|
||||
ctx[Context.root].on('agent/inbox/inserted', listener) // escapes scope filtering and plugin disposal
|
||||
```
|
||||
|
||||
反例让插件在依赖存在之前启动,让它观察和修改自身 scope 与 realm 之外的状态,并在插件释放后留下注册内容。
|
||||
|
||||
<a id="a-business-capability-owns-itself"></a>
|
||||
|
||||
## 业务能力自己拥有自己
|
||||
|
||||
@@ -283,7 +283,8 @@ function createGate(exemptions: Readonly<Record<string, string>>): InventoryGate
|
||||
baselinePath: 'scripts/cordis-root.baseline.json',
|
||||
scan: repoRoot => scanCordisRootReads(repoRoot, exemptions),
|
||||
rejection: 'new reads of the Cordis root context are forbidden. Declare the service in `inject`,'
|
||||
+ ' register through the plugin context, or use `{ global: true }` for an unscoped listener.',
|
||||
+ ' register through the plugin context, or use `{ global: true }` for an unscoped listener'
|
||||
+ ' (see docs/defensive-patterns/general.md#plugins-do-not-read-the-cordis-root-context).',
|
||||
describe: read => `${read.file}:${String(read.line)} (${read.fingerprint})`,
|
||||
success: count => `verify-cordis-root: no new root reads; ${String(count)} recorded reads remain.`,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user