Prohibit new reads of the Cordis root context
禁止新增对 Cordis 根上下文的读取
`ctx[Context.root]` returns the application's root context. A property read through it skips the `inject` check, because the root fiber has no runtime; a listener registered on it skips scope filtering; and it reads the root realm instead of the caller's isolate realm. DSH code uses it to read undeclared services, to listen to every Agent's events, to key per-application state, and to call boot APIs that require the
English
Problem
ctx[Context.root] returns the application's root context. A property read through it skips the inject check, because the root fiber has no runtime; a listener registered on it skips scope filtering; and it reads the root realm instead of the caller's isolate realm. DSH code uses it to read undeclared services, to listen to every Agent's events, to key per-application state, and to call boot APIs that require the root context. Each of these uses has, or needs, a declared replacement: an inject entry, { global: true } on a listener, or a service that owns the root-level operation.
The repository contains such reads across boot, host, preset, protocol, experimental, and test owners. Replacing all of them in one change would combine the enforcement decision with several service designs.
Decision
New reads of the Cordis root context are prohibited in first-party JavaScript and TypeScript, including tests, fixtures, and scripts. The checker, exposed as pnpm run verify-cordis-root, uses the TypeScript syntax tree and a type checker over each file alone. A read names the root symbol through a Cordis value binding of Context or symbols: .root, ['root'], or root destructured in a declaration, a parameter default, or an assignment. A binding comes from @deepseek-ai/cordis or one of its subpaths through a static import, including renamed, default, namespace, and import = forms, or through a destructured or namespace import() or require() result; a local that shadows the import is not a binding. Any string literal whose value is exactly cordis.root, the key behind Symbol.for('cordis.root'), is also a read. Type positions, interfaces, type aliases, ambient declarations, comments, other strings, and source text embedded in a template are not reads.
Existing reads have an exact baseline in scripts/cordis-root.baseline.json, kept by the shared inventory module under the rules of the unknown-cast inventory: 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. The defensive pattern states the rule and its replacements for authors, and the gate's rejection message links it.
Alternatives considered
Replace every existing read before enforcing the rule. Rejected because several replacements need new services, such as a profile-composition service for the boot APIs and an application-lifecycle signal; enforcing first stops new reads while those designs are reviewed separately.
Use the no-restricted-properties lint rule. Rejected because it matches the identifier Context by name, so a renamed import such as CordisContext passes, and it cannot match Symbol.for('cordis.root').
Record the exempt files in the baseline. Rejected because the baseline only shrinks, while the exempt files are expected to keep their reads; keeping them apart lets the baseline reach empty.
Consequences
Owners must remove a recorded read before changing its receiver or moving it into an unlisted file, and prune its entry after removal. Passing the check establishes that no unlisted syntactic root read exists. It does not detect a root context obtained through another value, such as a stored reference, an alias of Context, a service that returns it, or code assembled from strings at runtime.
中文
问题
ctx[Context.root] 返回应用的根上下文。通过它读取属性会跳过 inject 检查,因为根 fiber 没有 runtime;在它上面注册的监听器会跳过 scope 过滤;它读取的是根 realm,而不是调用方所在的 isolate realm。DSH 代码用它读取未声明的服务、监听所有 Agent 的事件、作为应用级状态的键,以及调用要求根上下文的启动 API。这些用法都已有或需要一个显式声明的替代方式:inject 条目、监听器上的 { global: true },或负责根级操作的服务。
仓库中的此类读取分布在 boot、host、preset、protocol、experimental 和测试等多个所有者中。在一次变更中全部替换,会把执行规则的决定与多个服务设计混在一起。
决定
第一方 JavaScript 和 TypeScript(包括测试、fixture 和脚本)禁止新增对 Cordis 根上下文的读取。检查器通过 pnpm run verify-cordis-root 运行,使用 TypeScript 语法树,并对每个文件单独建立类型检查器。读取指通过 Context 或 symbols 的 Cordis 值绑定访问根符号:.root、['root'],或在声明、参数默认值或赋值中解构出的 root。绑定来自 @deepseek-ai/cordis 或其子路径,可以是静态导入(包括重命名、默认、命名空间和 import = 形式),也可以是解构或整体接收的 import() 或 require() 结果;遮蔽导入的局部变量不是绑定。值恰为 cordis.root 的任何字符串字面量也算读取,即 Symbol.for('cordis.root') 背后的键。类型位置、接口、类型别名、ambient 声明、注释、其他字符串以及嵌入模板中的源码文本不算读取。
现有读取使用 scripts/cordis-root.baseline.json 中的精确基线记录,由共享清单模块按 unknown 类型断言清单的规则维护:基线修改只能删除条目或减少次数,--prune 删除已失效的条目并在存在新读取时拒绝写入,且没有任何命令可以添加放行。读取的指纹覆盖以根符号为键的元素访问的 token,因此把已记录的读取换到另一个接收者上会产生新的检查发现。
测试根上下文约定本身的文件在检查器内的 CORDIS_ROOT_EXEMPTIONS 中逐个列出并注明原因,不参与扫描:Cordis rc.12 规范测试和检查器本身。若豁免文件不再读取根上下文,门禁失败。vendored 源码和已冻结的归档 Agent Notes 保留清单的排除规则。检查器参与门禁运行器中的 CI static 和 primary 模式以及本地 hygiene 和 check-all 配置。防御性模式为作者写明该规则及替代方式,门禁的拒绝信息链接到这一节。
考虑过的替代方案
在执行规则前替换所有现有读取。 已否决,因为部分替换需要新的服务,例如供启动 API 使用的 profile 组合服务和应用生命周期信号;先执行规则可以阻止新增读取,同时单独评审这些设计。
使用 no-restricted-properties lint 规则。 已否决,因为它按名称匹配标识符 Context,重命名导入(如 CordisContext)可以绕过,而且无法匹配 Symbol.for('cordis.root')。
把豁免文件记入基线。 已否决,因为基线只会缩小,而豁免文件预期会保留其读取;分开维护可以让基线最终清空。
影响
所有者在更换已记录读取的接收者或将其移入未登记的文件之前,必须先移除该读取,并在移除后裁剪其条目。通过此检查表明不存在未登记的语法层面的根读取。它无法检测通过其他值获得的根上下文,例如保存的引用、Context 的别名、返回根上下文的服务,或运行时由字符串拼装的代码。