From ec0786e099e487526785e4bdf870868ed640e9aa Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Tue, 28 Jul 2026 17:30:12 +0800 Subject: [PATCH 01/17] feat(settings): add user-settings seam (ctx.settings) + file provider MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two-package capability family mirroring session-persistence/: - dsh-settings: abstract Settings service — namespace registry with caller-fiber effect registrations, layered resolution (schema defaults < composition base < user document), schemastery validation, per-namespace deep-equal commit detection, and the settings/updated event. Boot/registration validation fails loud; provider publishes keep last-good per namespace. - dsh-settings-local: settings.yaml/.json provider — resolveSpec defaulting to $DSH_HOME/settings.yaml, chokidar hot reload, content-equality self-write suppression, atomic 0600 tmp+rename writes, comment-preserving YAML namespace patching. Consumers register inside ctx.inject(['settings'], …), so every composition works unchanged without a mounted provider. Real Loader + Include composition test proves cordis.yml boot and external-edit hot propagation; HMR disposal test proves registry cleanup. Both packages hold per-file 100% coverage. Doc budgets rise 1705→1710 (AGENTS.md) and 835→845 (packages/README.md): one structural line per file for the new package group. Agent Note: .agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md --- .../2026-07-28-user-settings-seam.i18n.yaml | 6 + .../2026-07-28-user-settings-seam.md | 35 ++ .../2026-07-28-user-settings-seam.zh.md | 35 ++ AGENTS.md | 1 + docs/capability-seams.md | 6 + docs/config-catalog.md | 19 ++ docs/cordis-catalog/events.md | 22 ++ docs/cordis-catalog/services.md | 42 +++ docs/event-producer-consumer.md | 1 + packages/README.i18n.yaml | 4 +- packages/README.md | 1 + packages/README.zh.md | 1 + .../cordis/tool-cordis/src/api-catalog.ts | 49 +++ packages/settings/README.i18n.yaml | 6 + packages/settings/README.md | 12 + packages/settings/README.zh.md | 12 + .../settings/settings-local/README.i18n.yaml | 6 + packages/settings/settings-local/README.md | 36 ++ packages/settings/settings-local/README.zh.md | 36 ++ packages/settings/settings-local/package.json | 46 +++ packages/settings/settings-local/src/index.ts | 226 +++++++++++++ .../settings/settings-local/src/invariant.ts | 31 ++ .../tests/loader-composition.spec.ts | 112 +++++++ .../settings-local/tests/local.spec.ts | 254 +++++++++++++++ .../settings-local/tests/watcher.spec.ts | 118 +++++++ .../settings/settings-local/tsconfig.json | 30 ++ packages/settings/settings/README.i18n.yaml | 6 + packages/settings/settings/README.md | 35 ++ packages/settings/settings/README.zh.md | 35 ++ packages/settings/settings/package.json | 41 +++ packages/settings/settings/src/index.ts | 304 +++++++++++++++++ packages/settings/settings/src/invariant.ts | 41 +++ .../settings/settings/tests/invariant.spec.ts | 41 +++ packages/settings/settings/tests/memory.ts | 53 +++ .../settings/settings/tests/settings.spec.ts | 307 ++++++++++++++++++ packages/settings/settings/tsconfig.json | 27 ++ pnpm-lock.yaml | 40 +++ scripts/doc-budgets.manifest.json | 4 +- scripts/gen-cordis-catalog.ts | 6 + scripts/gen-doc-graphs.ts | 9 + .../verify-package-readme-model-experience.ts | 2 + tsconfig.base.json | 2 + tsconfig.host.json | 2 + 43 files changed, 2098 insertions(+), 4 deletions(-) create mode 100644 .agents/notes/implemented/architecture/2026-07-28-user-settings-seam.i18n.yaml create mode 100644 .agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md create mode 100644 .agents/notes/implemented/architecture/2026-07-28-user-settings-seam.zh.md create mode 100644 packages/settings/README.i18n.yaml create mode 100644 packages/settings/README.md create mode 100644 packages/settings/README.zh.md create mode 100644 packages/settings/settings-local/README.i18n.yaml create mode 100644 packages/settings/settings-local/README.md create mode 100644 packages/settings/settings-local/README.zh.md create mode 100644 packages/settings/settings-local/package.json create mode 100644 packages/settings/settings-local/src/index.ts create mode 100644 packages/settings/settings-local/src/invariant.ts create mode 100644 packages/settings/settings-local/tests/loader-composition.spec.ts create mode 100644 packages/settings/settings-local/tests/local.spec.ts create mode 100644 packages/settings/settings-local/tests/watcher.spec.ts create mode 100644 packages/settings/settings-local/tsconfig.json create mode 100644 packages/settings/settings/README.i18n.yaml create mode 100644 packages/settings/settings/README.md create mode 100644 packages/settings/settings/README.zh.md create mode 100644 packages/settings/settings/package.json create mode 100644 packages/settings/settings/src/index.ts create mode 100644 packages/settings/settings/src/invariant.ts create mode 100644 packages/settings/settings/tests/invariant.spec.ts create mode 100644 packages/settings/settings/tests/memory.ts create mode 100644 packages/settings/settings/tests/settings.spec.ts create mode 100644 packages/settings/settings/tsconfig.json diff --git a/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.i18n.yaml b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.i18n.yaml new file mode 100644 index 0000000000..cc409d8403 --- /dev/null +++ b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.i18n.yaml @@ -0,0 +1,6 @@ +# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each +# side as of the last confirmed-consistent state. Both languages carry equal authority; +# after editing either side, bring the other along and re-record with: +# pnpm run verify-translation-pairing --write .agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md +2026-07-28-user-settings-seam.md: f0f45d77c8f98fc15625b1a1bf116ec10b965676 +2026-07-28-user-settings-seam.zh.md: eb562099b1ff0b5b0a019e9956d6e36e71857236 diff --git a/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md new file mode 100644 index 0000000000..f0f45d77c8 --- /dev/null +++ b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md @@ -0,0 +1,35 @@ +# Agent Note: user-settings seam (`ctx.settings`) and the file provider + +Status: implemented + +English | [中文](2026-07-28-user-settings-seam.zh.md) + +> Scope: the `packages/settings/` capability family — the abstract seam, the file-backed provider, and the composition boundary between user settings and `cordis.yml`. The [web config-tree note](2026-07-24-web-config-tree-boot-and-transport-layering.md) recorded "the profile write path" as a deferral; this seam is that write path's owner. Consumer migrations (theme, locale, default model route) and the web `settings.*` RPC surface are follow-ups, not part of this note's shipped scope. + +## Problem + +User-editable configuration had no owner: `dsh web` read a cwd-anchored profile json through a static whitelist with no write path, the TUI read `$DSH_HOME/config.yaml` raw loader patches, and both froze at boot. A personal-settings page (web GUI) needs one cross-surface user layer with schema validation, a write path, and hot propagation — and peer products (Codex, Claude Code, Kimi, OpenCode, Pi) all converged on separating user preferences from extension composition. Loader-reactive config updates cannot carry this: `fiber.update` swaps entry config in place, so a plugin that read config at construction observes nothing and no callback tells it otherwise. + +## Decision + +**Two planes with a litmus test.** `cordis.yml` (+ Include patches) stays the composition plane: which plugins exist, wiring, deployment config, owned by the orchestrator and upgraded with the product. A settings namespace carries only the user-editable subset; the test is "should the personal config page edit it?" Values live in both planes without ambiguity because layering is the contract: schema defaults, then the registrant's composition `base` (its entry-config subset), then the user document section. + +**Three-package seam mirroring `session-persistence/`.** `dsh-settings` owns the abstract `Settings` service: namespace registry, layered resolution, schema validation, per-namespace deep-equal change detection, and the `settings/updated` commit event. Providers implement only `writable`/`load()`/`persist(ns, section)` and push externally observed documents through the protected `publish(doc)` — so hot-update semantics are identical across providers, and a network configuration-center backend (nacos-style, possibly read-only) is a sibling package away. `dsh-settings-local` is the file provider: YAML/JSON under `resolveSpec` (explicit defaulting to `/settings.yaml`), chokidar watch, atomic `0600` tmp+rename writes, comment-preserving YAML patching of exactly one namespace key, and content-equality self-write suppression. + +**Registrations are caller-fiber effects.** `register()` runs through the service proxy, so `this.ctx` is the registrant's context and the registration rides `ctx.effect`: disposing the registrant removes the namespace and its watchers (proven by the HMR disposal test), while the user's section keeps living in storage for the next owner. + +**Fail loud at rest, last-good in motion.** Boot-time and registration-time validation throw (invalid stored section fails the registering plugin; an existing-but-unparsable document fails provider load). Once live, a bad external edit warns and keeps the last good state per namespace — a hot reload must never take the process down. This asymmetry mirrors `Include.refresh()` and Kimi's safe runtime reload. + +**Consumers stay optional-by-construction.** A consumer registers inside `ctx.inject(['settings'], …)`; without a mounted provider it keeps resolving entry config alone, so every existing composition, demo, and snapshot works unchanged and migration is per-plugin. + +## Alternatives considered + +- **Include write-back as the user layer** (per-plugin config pages writing loader entry files, cordis-webui style): write-back would target per-composition files, binding user preferences to one `cordis.yml`; a per-user layer must survive template upgrades and serve TUI and web from one document. +- **Loader-reactive `fiber.update` as the propagation channel**: constructor-time reads observe nothing; the seam's explicit `watch()` makes hot-update a consumer contract instead of framework magic. +- **A domain-aware settings service** (getters per product area): the coupling objection from design review stands; the service stores, validates, and publishes — domain meaning stays with the registrant that owns the schema. +- **Multi-layer precedence now** (system/managed/project tiers à la Codex/Claude Code): deferred until a real second layer exists; the resolve step is the single place layering would extend. +- **A cross-process lockfile now** (Pi's proper-lockfile): atomic replace plus watcher convergence (last write wins) is documented behavior until real contention shows up. + +## Consequences + +Deferred, in dependency order: the web `settings.raw`/`settings.describe`/`settings.update` RPC surface (which must redact `role('secret')` fields before exposure); first consumer migrations (`ui-theme`, locale, api-gateway default route) retiring `PROFILE_MAPPINGS` and the profile json; `${env:VAR}` value indirection for secrets; provider-side layering. The keyless snapshot obligation lands with the first model- or product-user-visible consumer, not with this infrastructure step. diff --git a/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.zh.md b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.zh.md new file mode 100644 index 0000000000..eb562099b1 --- /dev/null +++ b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.zh.md @@ -0,0 +1,35 @@ +# Agent Note:用户设置 seam(`ctx.settings`)与文件 provider + +Status: implemented + +[English](2026-07-28-user-settings-seam.md) | 中文 + +> 范围:`packages/settings/` 能力族——抽象 seam、文件 provider,以及用户设置与 `cordis.yml` 的组合边界。[web config-tree note](2026-07-24-web-config-tree-boot-and-transport-layering.md) 曾把"profile 写路径"记为延后项;本 seam 就是该写路径的归属。消费者迁移(主题、语言、默认模型路由)与 web `settings.*` RPC 面是后续工作,不在本 note 已交付范围内。 + +## 问题 + +用户可编辑配置没有归属:`dsh web` 经静态白名单读 cwd 锚定的 profile json 且无写路径,TUI 读 `$DSH_HOME/config.yaml` 裸 loader patch,两者都在启动时冻结。个人设置页(web GUI)需要一个跨 surface 的用户层,带 schema 校验、写路径与热传导——同类产品(Codex、Claude Code、Kimi、OpenCode、Pi)也全部收敛于"用户偏好与扩展组合分离"。Loader 的 reactive 配置更新承载不了这件事:`fiber.update` 原地替换 entry config,构造期读过配置的插件毫无感知,也没有任何回调通知它。 + +## 决策 + +**两个面,一条判定。**`cordis.yml`(+ Include patches)仍是组合面:有哪些插件、接线、部署配置,归 orchestrator 所有并随产品升级。settings namespace 只承载用户可编辑子集;判定是"个人配置页应该能改它吗?"值可同时存在于两个面而不歧义,因为分层就是契约:schema 默认值,然后注册方的组合 `base`(其 entry 配置子集),最后用户文档分节。 + +**镜像 `session-persistence/` 的三包 seam。**`dsh-settings` 拥有抽象 `Settings` 服务:namespace 注册表、分层解析、schema 校验、按 namespace 深相等变更检测,以及 `settings/updated` 提交事件。provider 只实现 `writable`/`load()`/`persist(ns, section)`,并通过受保护的 `publish(doc)` 推入外部观察到的文档——因此热更新语义对所有 provider 一致,网络配置中心后端(nacos 类,可能只读)只是一个平级包的距离。`dsh-settings-local` 是文件 provider:`resolveSpec` 显式默认到 `/settings.yaml` 的 YAML/JSON、chokidar 监听、`0600` tmp+rename 原子写、只修补目标 namespace 键的保注释 YAML 写回、按内容相等抑制自写。 + +**注册是调用方 fiber 上的 effect。**`register()` 经服务代理调用,`this.ctx` 即注册方 context,注册挂在 `ctx.effect` 上:dispose 注册方即移除 namespace 及其观察者(HMR disposal 测试证明),而用户的分节继续留在存储中等待下一任 owner。 + +**静止时响亮报错,运行中保留最后可用值。**启动期与注册期校验直接抛错(非法存量分节使注册插件加载失败;存在但不可解析的文档使 provider 加载失败)。运行中坏的外部编辑只告警并按 namespace 保留最后可用状态——热重载绝不拖垮进程。该不对称镜像 `Include.refresh()` 与 Kimi 的安全运行时重载。 + +**消费者天然可选。**消费者在 `ctx.inject(['settings'], …)` 内注册;不挂 provider 时仍只按 entry 配置解析,因此所有既有组合、demo、snapshot 原样工作,迁移按插件渐进。 + +## Alternatives considered + +- **以 Include 写回为用户层**(cordis-webui 式的按插件配置页写 loader entry 文件):写回目标是按组合的文件,会把用户偏好绑死在某个 `cordis.yml` 上;用户层必须在模板升级中存活,并以同一文档服务 TUI 与 web。 +- **以 Loader reactive `fiber.update` 为传导通道**:构造期读取毫无感知;seam 的显式 `watch()` 把热更新变成消费者契约而非框架魔法。 +- **领域化的 settings 服务**(按产品域的 getter):设计评审中的耦合反对成立;服务只做存储、校验、发布——领域含义留给拥有 schema 的注册方。 +- **现在就做多层优先级**(Codex/Claude Code 式 system/managed/project 层级):延后到真实第二层出现;resolve 步骤是分层未来唯一的扩展点。 +- **现在就上跨进程锁**(Pi 的 proper-lockfile):原子替换加 watcher 收敛(后写胜出)是已记录的行为,真实冲突出现再说。 + +## 后果 + +按依赖顺序延后:web `settings.raw`/`settings.describe`/`settings.update` RPC 面(暴露前必须对 `role('secret')` 字段脱敏);首批消费者迁移(`ui-theme`、语言、api-gateway 默认路由)并退役 `PROFILE_MAPPINGS` 与 profile json;面向密钥的 `${env:VAR}` 值间接引用;provider 侧分层。keyless snapshot 义务随第一个模型或产品用户可见的消费者落地,而非本基础设施步骤。 diff --git a/AGENTS.md b/AGENTS.md index c987801c17..7112dd3aee 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -30,6 +30,7 @@ packages/ @deepseek-ai/dsh- workspaces at packages/// cordis/ self-referential toolset: the agent inspects/mounts plugins in its own runtime hooks/ Claude Code/Codex hook bridges + shared wire-protocol library session-persistence/ persistence seam + JSONL/SQLite backends + settings/ user-settings seam + file-backed provider acp/ automation-only Agent Client Protocol server ui/ TUI/JSON-RPC bridges; boot, approval, interaction plugins examples/ demo bundles (agent-spine + TUI/CLI/ACP/JSON-RPC bins) leaves load diff --git a/docs/capability-seams.md b/docs/capability-seams.md index 9093f41f15..74f7c63c0d 100644 --- a/docs/capability-seams.md +++ b/docs/capability-seams.md @@ -35,6 +35,9 @@ flowchart LR pkg_tool_bash["tool-bash"] pkg_hooks_claude["hooks-claude"] pkg_hooks_codex["hooks-codex"] + pkg_settings["settings"] + svc_settings["ctx.settings
User-settings seam"] + pkg_settings_local["settings-local"] pkg_session_telemetry["session-telemetry"] svc_telemetry["ctx.telemetry
Session telemetry seam"] pkg_session_telemetry_otel["session-telemetry-otel"] @@ -188,6 +191,8 @@ flowchart LR pkg_session_title --> svc_sessionTitle pkg_session_title_all_messages_llm --> svc_sessionTitle pkg_session_title_first_message_llm --> svc_sessionTitle + pkg_settings --> svc_settings + pkg_settings_local --> svc_settings pkg_skill --> svc_skills pkg_skill_local --> svc_skills pkg_spill --> svc_spillStore @@ -316,6 +321,7 @@ flowchart LR | `ctx.sessions` | `core` | [`session`](../packages/core/session) | - | [`agent-loop`](../packages/core/agent-loop), [`agent`](../packages/core/agent), [`cli-demo`](../packages/examples/cli-demo), [`session-persistence`](../packages/session-persistence/session-persistence), [`session-query`](../packages/session-query/session-query), [`session-query-sqlite`](../packages/session-query/session-query-sqlite), [`subagent-inprocess`](../packages/subagent/subagent-inprocess), [`invariants`](../packages/support/invariants) | - | Owns append-only Session instances and emits the durable session event feed. | | `ctx.invariants` | `core` | [`invariants`](../packages/support/invariants) | - | [`session`](../packages/core/session), [`agent`](../packages/core/agent), [`scope`](../packages/core/scope), [`agent-loop`](../packages/core/agent-loop) | - | Companion subpaths register owner-local checks; the service owns selection, uniqueness, child fibers, and package-attributed failures. | | `ctx.sessionPersistence` | `seam` | [`session-persistence`](../packages/session-persistence/session-persistence) | [`session-persistence-jsonl`](../packages/session-persistence/session-persistence-jsonl), [`session-persistence-sqlite`](../packages/session-persistence/session-persistence-sqlite) | [`agent-loop`](../packages/core/agent-loop), [`tool-bash`](../packages/bash/tool-bash), [`hooks-claude`](../packages/hooks/hooks-claude), [`hooks-codex`](../packages/hooks/hooks-codex), [`session-query`](../packages/session-query/session-query), [`session-query-sqlite`](../packages/session-query/session-query-sqlite) | - | Backends persist the same SessionEvent vocabulary; apps choose a backend at composition time. | +| `ctx.settings` | `seam` | [`settings`](../packages/settings/settings) | [`settings-local`](../packages/settings/settings-local) | - | - | Plugins register namespace schemas and resolve layered values; providers store the raw document. No production consumer is migrated yet. | | `ctx.telemetry` | `seam` | [`session-telemetry`](../packages/telemetry/session-telemetry) | [`session-telemetry-otel`](../packages/telemetry/session-telemetry-otel) | - | - | The seam captures, redacts, and hands session records to one backend; nothing else consumes the service — its output leaves the process. | | `ctx.storage` | `seam` | [`storage`](../packages/storage/storage) | [`storage-json`](../packages/storage/storage-json), [`storage-sqlite`](../packages/storage/storage-sqlite) | [`storage-domain`](../packages/storage/storage-domain) | - | Backends register side by side under names; data forms (domain first) mount on the hub and translate typed operations into opaque KV-unit primitives. | | `ctx.storageDomain` | `core` | [`storage-domain`](../packages/storage/storage-domain) | - | [`workspace`](../packages/workspace/workspace) | - | Waits for every configured backend, then publishes the domain form as one lifecycle-bound service for typed durable state. | diff --git a/docs/config-catalog.md b/docs/config-catalog.md index dcb4fa69e7..1305dd7270 100644 --- a/docs/config-catalog.md +++ b/docs/config-catalog.md @@ -1177,6 +1177,24 @@ Depends on: [`SessionTitleLlmConfig`](../packages/session-title/session-title-ll Source: [`packages/session-title/session-title-first-message-llm/src/index.ts:15`](../packages/session-title/session-title-first-message-llm/src/index.ts) +## `@deepseek-ai/dsh-settings-local` + +```ts config-catalog +/** Plugin config: file location and hot-reload behavior. */ +export interface Config { + /** Settings document path; defaults to `settings.yaml` under the harness home. */ + path?: string + /** Harness home used when `path` is omitted; defaults to `$DSH_HOME` or `~/.dsh`. */ + dshHome?: string + /** Watch the document and hot-publish external edits; defaults to true. */ + watch?: boolean + /** Watcher write-settle window in milliseconds; defaults to 100. */ + debounceMs?: number +} +``` + +Source: [`packages/settings/settings-local/src/index.ts:18`](../packages/settings/settings-local/src/index.ts) + ## `@deepseek-ai/dsh-skill` ```ts config-catalog @@ -2192,6 +2210,7 @@ Abstract service classes — a deployment loads a concrete implementation packag - `@deepseek-ai/dsh-sandbox` — abstract `SandboxProvider` ([`packages/sandbox/sandbox/src/index.ts`](../packages/sandbox/sandbox/src/index.ts)) - `@deepseek-ai/dsh-session-persistence` — abstract `SessionPersistence` ([`packages/session-persistence/session-persistence/src/index.ts`](../packages/session-persistence/session-persistence/src/index.ts)) - `@deepseek-ai/dsh-session-query` — abstract `SessionQueryService` ([`packages/session-query/session-query/src/index.ts`](../packages/session-query/session-query/src/index.ts)) +- `@deepseek-ai/dsh-settings` — abstract `Settings` ([`packages/settings/settings/src/index.ts`](../packages/settings/settings/src/index.ts)) - `@deepseek-ai/dsh-spill` — abstract `SpillStore` ([`packages/spill/spill/src/index.ts`](../packages/spill/spill/src/index.ts)) - `@deepseek-ai/dsh-subprocess` — abstract `SubprocessService` ([`packages/subprocess/subprocess/src/index.ts`](../packages/subprocess/subprocess/src/index.ts)) - `@deepseek-ai/dsh-tasks` — abstract `TaskService` ([`packages/tasks/tasks/src/index.ts`](../packages/tasks/tasks/src/index.ts)) diff --git a/docs/cordis-catalog/events.md b/docs/cordis-catalog/events.md index ca0e9b82fc..aea04f4acd 100644 --- a/docs/cordis-catalog/events.md +++ b/docs/cordis-catalog/events.md @@ -640,6 +640,28 @@ Types: [Scoped](../core-data-structures/scope.md) · [Session](../core-data-stru Source: [`packages/core/session/src/index.ts:102`](../../packages/core/session/src/index.ts) +## `settings/*` + +### `settings/updated` — emit + +Committed change to one registered namespace's resolved value. Emitted after the provider persisted (for `update`) or published (`provider`) the change; never emitted when the resolved value is deep-equal. + +```ts cordis-catalog +/** + * Committed change to one registered namespace's resolved value. Emitted + * after the provider persisted (for `update`) or published (`provider`) + * the change; never emitted when the resolved value is deep-equal. + * @param ns - the namespace whose resolved value changed. + * @param next - the new resolved value. + * @param prev - the previous resolved value. + * @param source - whether the change entered through `update()` or the provider. + * @mode emit + */ +'settings/updated'(ns: SettingsNamespace, next: unknown, prev: unknown, source: SettingsUpdateSource): void +``` + +Source: [`packages/settings/settings/src/index.ts:90`](../../packages/settings/settings/src/index.ts) + ## `slash/*` ### `slash/input-begin-command` — bail diff --git a/docs/cordis-catalog/services.md b/docs/cordis-catalog/services.md index 19d7d7765d..a0271083d6 100644 --- a/docs/cordis-catalog/services.md +++ b/docs/cordis-catalog/services.md @@ -1388,6 +1388,48 @@ Types: [Session](../core-data-structures/session.md) · [SessionTitleProvider](. Source: [`packages/session-title/session-title/src/index.ts:232`](../../packages/session-title/session-title/src/index.ts) +## `ctx.settings` — `Settings` (abstract seam) + +Abstract settings service. Providers implement raw-document storage (`load`/`persist`) and push external changes through Settings.publish; the base class owns namespace registration, resolution, validation, change detection, and the `settings/updated` commit event. + +```ts cordis-catalog +/** + * Register a namespace schema and receive its owner scope. The registration + * is an effect on the calling plugin's fiber: disposing that fiber removes + * the namespace and its observers. An invalid stored section fails the + * registration itself — the earliest point where the schema can judge it. + * @param ns - unique namespace; duplicate registration fails loud. + * @param schema - schemastery schema resolving this namespace's value. + * @param options - composition `base` layer and effect timing. + * @returns the owner scope for reads, observation, and updates. + */ +register(ns: SettingsNamespace, schema: z, options?: SettingsRegisterOptions): SettingsScope + +/** + * Describe every registered namespace for configuration surfaces. + * @returns one descriptor per registered namespace, in registration order. + */ +describe(): SettingsDescriptor[] + +/** + * Read one registered namespace's resolved value. + * @param ns - the namespace to read. + * @returns the resolved value, or `undefined` while unregistered. + */ +get(ns: SettingsNamespace): unknown + +/** + * Merge a patch into one registered namespace's user layer, validate the + * resolved candidate, persist through the provider, then commit and emit. + * A validation failure rejects before anything is persisted. + * @param ns - the registered namespace to update. + * @param patch - plain-object patch over the user section. + */ +async update(ns: SettingsNamespace, patch: object): Promise +``` + +Source: [`packages/settings/settings/src/index.ts:140`](../../packages/settings/settings/src/index.ts) + ## `ctx.skills` — `SkillService` Registry of skill providers. It merges provider catalogs with stable first-wins duplicate handling, exposes sorted model-visible summaries, and loads full skill bodies on demand. diff --git a/docs/event-producer-consumer.md b/docs/event-producer-consumer.md index e9e5bb6e6a..49bdf3fe35 100644 --- a/docs/event-producer-consumer.md +++ b/docs/event-producer-consumer.md @@ -35,6 +35,7 @@ This matrix shows which packages dispatch each harness-owned event and which pac | `session/disposed` | `emit` | [`packages/core/session/src/index.ts:80`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`agent-loop`](../packages/core/agent-loop), `apiproxy`, [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title) | | `session/event` | `emit` | [`packages/core/session/src/index.ts:92`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`acp`](../packages/acp/acp), `apiproxy`, [`cli-demo`](../packages/examples/cli-demo), [`compact`](../packages/compact/compact), [`compact-basic`](../packages/compact/compact-basic), [`goal`](../packages/goal/goal), [`goal-session`](../packages/goal/goal-session), [`hook-protocol`](../packages/hooks/hook-protocol), [`jsonrpc`](../packages/ui/jsonrpc), [`plan-mode`](../packages/plan/plan-mode), [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title), [`token-meter`](../packages/llm/token-meter), [`tools`](../packages/core/tools), [`tui`](../packages/ui/tui), [`user-approval`](../packages/ui/user-approval), [`workspace-context`](../packages/context/workspace-context) | | `session/flush` | `parallel` | [`packages/core/session/src/index.ts:102`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry) | +| `settings/updated` | `emit` | [`packages/settings/settings/src/index.ts:90`](../packages/settings/settings/src/index.ts) | [`settings`](../packages/settings/settings) (`emit`) | [`settings`](../packages/settings/settings) | | `slash/input-begin-command` | `bail` | [`packages/client/ui-slash/src/types.ts:230`](../packages/client/ui-slash/src/types.ts) | - | `ui-conversation` | | `slash/input-consume-token` | `bail` | [`packages/client/ui-slash/src/types.ts:244`](../packages/client/ui-slash/src/types.ts) | - | `ui-conversation` | | `slash/input-insert-reference` | `bail` | [`packages/client/ui-slash/src/types.ts:237`](../packages/client/ui-slash/src/types.ts) | - | `ui-conversation` | diff --git a/packages/README.i18n.yaml b/packages/README.i18n.yaml index 5dd168f03e..360687f197 100644 --- a/packages/README.i18n.yaml +++ b/packages/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/README.md -README.md: d16e395a42e491461c0862227205931894c27e39 -README.zh.md: 3fb4181ce7ae7b0d79a13ca4358b9df39d83ef1f +README.md: 7b415bdbbd3e442c952da49b6f9f8823a636f643 +README.zh.md: d3347ecd6db68d99bc00e340c632da51cf825c63 diff --git a/packages/README.md b/packages/README.md index d16e395a42..7b415bdbbd 100644 --- a/packages/README.md +++ b/packages/README.md @@ -37,6 +37,7 @@ Packages live at `packages///`; groups are containers, while names r | [`session-persistence/`](session-persistence/README.md) | Persistence capability family: the seam + JSONL/SQLite backends | Product — stable surface | | [`session-query/`](session-query/README.md) | Session retrieval family: logical corpus, bounded reads, lineage, event relationships, semantic filtering, and SQLite full-text search | Product — stable surface | | [`session-title/`](session-title/README.md) | Log-backed session titles: fallback service, shared LLM policy, and opt-in providers | Product — stable surface | +| [`settings/`](settings/README.md) | User-settings capability family: the seam + file-backed provider | Product — stable surface | | [`telemetry/`](telemetry/README.md) | Session reporting: capture/redact seam, OTel backend | Product — stable surface | | [`storage/`](storage/README.md) | Non-session storage hub + backends + domain form | Product — stable surface | | [`workspace/`](workspace/README.md) | Workspace entity | Product — stable surface | diff --git a/packages/README.zh.md b/packages/README.zh.md index 3fb4181ce7..d3347ecd6d 100644 --- a/packages/README.zh.md +++ b/packages/README.zh.md @@ -37,6 +37,7 @@ | [`session-persistence/`](session-persistence/README.md) | 持久化能力系列:seam + JSONL/SQLite 后端 | 产品:稳定表面 | | [`session-query/`](session-query/README.md) | 会话检索系列:逻辑语料库、有界读取、血缘、事件关系、语义过滤和 SQLite 全文搜索 | 产品:稳定表面 | | [`session-title/`](session-title/README.md) | 日志支撑的会话标题:回退服务、共享 LLM 策略和选用提供方 | 产品:稳定表面 | +| [`settings/`](settings/README.md) | 用户设置能力族:seam + 文件 provider | 产品:稳定表面 | | [`telemetry/`](telemetry/README.md) | 会话上报:捕获/脱敏 seam、OTel 后端 | 产品:稳定表面 | | [`storage/`](storage/README.md) | 非会话存储中枢 + 后端 + 领域形式 | 产品:稳定表面 | | [`workspace/`](workspace/README.md) | Workspace 实体 | 产品:稳定表面 | diff --git a/packages/cordis/tool-cordis/src/api-catalog.ts b/packages/cordis/tool-cordis/src/api-catalog.ts index 47f8a1a54f..81e3c79d05 100644 --- a/packages/cordis/tool-cordis/src/api-catalog.ts +++ b/packages/cordis/tool-cordis/src/api-catalog.ts @@ -666,6 +666,28 @@ export const SERVICE_API: readonly ServiceApiEntry[] = [ }, ], }, + { + key: 'settings', + summary: 'Abstract settings service.', + methods: [ + { + signature: 'register(ns: SettingsNamespace, schema: z, options?: SettingsRegisterOptions): SettingsScope', + jsDoc: '/**\n * Register a namespace schema and receive its owner scope. The registration\n * is an effect on the calling plugin\'s fiber: disposing that fiber removes\n * the namespace and its observers. An invalid stored section fails the\n * registration itself — the earliest point where the schema can judge it.\n * @param ns - unique namespace; duplicate registration fails loud.\n * @param schema - schemastery schema resolving this namespace\'s value.\n * @param options - composition `base` layer and effect timing.\n * @returns the owner scope for reads, observation, and updates.\n */', + }, + { + signature: 'describe(): SettingsDescriptor[]', + jsDoc: '/**\n * Describe every registered namespace for configuration surfaces.\n * @returns one descriptor per registered namespace, in registration order.\n */', + }, + { + signature: 'get(ns: SettingsNamespace): unknown', + jsDoc: '/**\n * Read one registered namespace\'s resolved value.\n * @param ns - the namespace to read.\n * @returns the resolved value, or `undefined` while unregistered.\n */', + }, + { + signature: 'async update(ns: SettingsNamespace, patch: object): Promise', + jsDoc: '/**\n * Merge a patch into one registered namespace\'s user layer, validate the\n * resolved candidate, persist through the provider, then commit and emit.\n * A validation failure rejects before anything is persisted.\n * @param ns - the registered namespace to update.\n * @param patch - plain-object patch over the user section.\n */', + }, + ], + }, { key: 'skills', summary: 'Registry of skill providers.', @@ -1188,6 +1210,13 @@ export const EVENT_API: readonly EventApiEntry[] = [ jsDoc: '/**\n * Awaited parallel durability checkpoint: every listener runs and the\n * caller awaits all of them, with no waterfall veto. Dispatch through\n * {@link SessionStore.flush}. Scope-filtered dispatch\n * (`@deepseek-ai/dsh-scope`) reuses the session\'s owner scope.\n * @param session - the session whose buffered events must reach durable storage.\n * @dshScopeScan unsupported\n * @mode parallel\n */', summary: 'Awaited parallel durability checkpoint: every listener runs and the caller awaits all of them, with no waterfall veto.', }, + { + name: 'settings/updated', + mode: 'emit', + signature: '\'settings/updated\'(ns: SettingsNamespace, next: unknown, prev: unknown, source: SettingsUpdateSource): void', + jsDoc: '/**\n * Committed change to one registered namespace\'s resolved value. Emitted\n * after the provider persisted (for `update`) or published (`provider`)\n * the change; never emitted when the resolved value is deep-equal.\n * @param ns - the namespace whose resolved value changed.\n * @param next - the new resolved value.\n * @param prev - the previous resolved value.\n * @param source - whether the change entered through `update()` or the provider.\n * @mode emit\n */', + summary: 'Committed change to one registered namespace\'s resolved value.', + }, { name: 'slash/input-begin-command', mode: 'bail', @@ -2181,6 +2210,26 @@ export const TYPE_API: readonly TypeApiEntry[] = [ name: 'SessionTitleUserMessage', declaration: 'export interface SessionTitleUserMessage {\n readonly seq: number;\n readonly text: string;\n}', }, + { + name: 'SettingsApplies', + declaration: 'export type SettingsApplies = \'live\' | \'restart\';', + }, + { + name: 'SettingsDescriptor', + declaration: 'export interface SettingsDescriptor {\n ns: SettingsNamespace;\n schema: unknown;\n value: unknown;\n applies: SettingsApplies;\n}', + }, + { + name: 'SettingsNamespace', + declaration: 'export type SettingsNamespace = Branded<\'SettingsNamespace\'>;', + }, + { + name: 'SettingsRegisterOptions', + declaration: 'export interface SettingsRegisterOptions {\n base?: Partial;\n applies?: SettingsApplies;\n}', + }, + { + name: 'SettingsScope', + declaration: 'export interface SettingsScope {\n get(): T;\n watch(callback: (next: T, prev: T) => void): () => void;\n update(patch: object): Promise;\n}', + }, { name: 'SkillCandidate', declaration: 'export interface SkillCandidate extends SkillSummary {\n readonly rank: number;\n readonly locator: unknown;\n readonly path?: string;\n readonly metadata?: Readonly>;\n}', diff --git a/packages/settings/README.i18n.yaml b/packages/settings/README.i18n.yaml new file mode 100644 index 0000000000..7c78505615 --- /dev/null +++ b/packages/settings/README.i18n.yaml @@ -0,0 +1,6 @@ +# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each +# side as of the last confirmed-consistent state. Both languages carry equal authority; +# after editing either side, bring the other along and re-record with: +# pnpm run verify-translation-pairing --write packages/settings/README.md +README.md: 7a91355dd01805938944f0abce77765021288e6d +README.zh.md: 2df40b67eb8ce6cfc693ed6bf3574815c0219ec0 diff --git a/packages/settings/README.md b/packages/settings/README.md new file mode 100644 index 0000000000..7a91355dd0 --- /dev/null +++ b/packages/settings/README.md @@ -0,0 +1,12 @@ +# settings/ — user-settings capability family + +English | [中文](README.zh.md) + +The user-settings seam and its providers. The interface package owns the abstract `Settings` service — namespace registration, layered resolution, and change commits; providers implement raw-document storage and push external edits through the seam. All **product** packages. + +| Package | Role | ctx key | +|---|---|---| +| `settings/` | Settings seam: namespace registry, layered resolution, commit events | `ctx.settings` | +| `settings-local/` | File-backed provider (`settings.yaml`/`.json`) with hot reload and comment-preserving write-back | (registers `ctx.settings`) | + +The interface lives at `settings/settings/`; providers are flat siblings. A network configuration-center provider (for example a nacos-style backend) joins here and registers on `ctx.settings`. Composition config stays in `cordis.yml`: a settings namespace carries only the user-editable subset, resolved as schema defaults, then the registrant's composition `base`, then the user document. diff --git a/packages/settings/README.zh.md b/packages/settings/README.zh.md new file mode 100644 index 0000000000..2df40b67eb --- /dev/null +++ b/packages/settings/README.zh.md @@ -0,0 +1,12 @@ +# settings/ — 用户设置能力族 + +[English](README.md) | 中文 + +用户设置 seam 及其 provider。接口包拥有抽象 `Settings` 服务——namespace 注册、分层解析与变更提交;provider 实现原始文档存储并把外部修改推入 seam。全部为**产品**包。 + +| 包 | 角色 | ctx key | +|---|---|---| +| `settings/` | 设置 seam:namespace 注册表、分层解析、提交事件 | `ctx.settings` | +| `settings-local/` | 文件 provider(`settings.yaml`/`.json`),热重载与保留注释的写回 | (注册 `ctx.settings`) | + +接口位于 `settings/settings/`;provider 平级并列。网络配置中心 provider(例如 nacos 类后端)加入本组并注册到 `ctx.settings`。组合配置仍留在 `cordis.yml`:settings namespace 只承载用户可编辑子集,解析顺序为 schema 默认值、注册方的组合 `base`、用户文档。 diff --git a/packages/settings/settings-local/README.i18n.yaml b/packages/settings/settings-local/README.i18n.yaml new file mode 100644 index 0000000000..f7ca57e86e --- /dev/null +++ b/packages/settings/settings-local/README.i18n.yaml @@ -0,0 +1,6 @@ +# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each +# side as of the last confirmed-consistent state. Both languages carry equal authority; +# after editing either side, bring the other along and re-record with: +# pnpm run verify-translation-pairing --write packages/settings/settings-local/README.md +README.md: 90428f98055d8e49fa1ec54e571453db2e0a5054 +README.zh.md: 3532d6cee99cb46f54e23889ef1bb54b2548ecfa diff --git a/packages/settings/settings-local/README.md b/packages/settings/settings-local/README.md new file mode 100644 index 0000000000..90428f9805 --- /dev/null +++ b/packages/settings/settings-local/README.md @@ -0,0 +1,36 @@ +# @deepseek-ai/dsh-settings-local + +English | [中文](README.zh.md) + +File-backed settings provider. One YAML or JSON document carries every namespace section; external edits hot-publish through `ctx.settings`, and `update()` writes back atomically while preserving the user's YAML comments and any section owned by a plugin that is not currently loaded. + +## Config + +| Field | Meaning | Default | +|---|---|---| +| `path` | Settings document path; extension picks the format (`.yaml`/`.yml`/`.json`) | `settings.yaml` under the harness home | +| `dshHome` | Harness home used when `path` is omitted | `$DSH_HOME` or `~/.dsh` | +| `watch` | Watch the document and hot-publish external edits | `true` | +| `debounceMs` | Watcher write-settle window in milliseconds | `100` | + +Defaulting is one explicit `resolveSpec(config)` step; an unsupported extension fails at load. + +## Behavior + +- **Boot fails loud, reload keeps last-good.** An existing-but-invalid document fails plugin load; once live, an unreadable or unparsable edit warns and keeps the last good sections. A missing document resolves every namespace from defaults and `base`; deleting it publishes the same empty state. +- **Write-back is atomic and owner-only.** `persist` writes `.tmp` with mode `0600` and renames over the target. YAML writes patch one namespace in the comment-preserving document; JSON re-serializes. +- **Self-write suppression by content.** The provider caches the last good text; a watcher event whose content equals the cache (its own write included) is a no-op. + +## Model Experience + +Indirectly, through consumers of `ctx.settings`: this provider only stores and publishes namespace sections, and each consumer's own surface documents any model effect. + +#### KV Cache effect + +No direct invalidation; the consuming plugin owns any request-prefix changes. + +## Known Limitations and Deferred Work + +- **No cross-process write lock** — concurrent writers (for example TUI and web on one home) converge by atomic replace plus watcher reload, last write wins; a lockfile is deferred until real contention shows up. +- **Comment preservation is YAML-only** — JSON documents re-serialize without comments (JSON has none) and lose hand formatting. +- **No value indirection** — sections hold literal values; `${env:VAR}`-style references for secrets are a deferred seam-level feature. diff --git a/packages/settings/settings-local/README.zh.md b/packages/settings/settings-local/README.zh.md new file mode 100644 index 0000000000..3532d6cee9 --- /dev/null +++ b/packages/settings/settings-local/README.zh.md @@ -0,0 +1,36 @@ +# @deepseek-ai/dsh-settings-local + +[English](README.md) | 中文 + +文件 settings provider。一个 YAML 或 JSON 文档承载全部 namespace 分节;外部编辑经 `ctx.settings` 热发布,`update()` 原子写回,并保留用户的 YAML 注释以及当前未加载插件所拥有的分节。 + +## 配置 + +| 字段 | 含义 | 默认 | +|---|---|---| +| `path` | 设置文档路径;扩展名决定格式(`.yaml`/`.yml`/`.json`) | harness home 下的 `settings.yaml` | +| `dshHome` | `path` 省略时使用的 harness home | `$DSH_HOME` 或 `~/.dsh` | +| `watch` | 监听文档并热发布外部编辑 | `true` | +| `debounceMs` | watcher 写入稳定窗口(毫秒) | `100` | + +默认值解析是一步显式的 `resolveSpec(config)`;不支持的扩展名在加载时报错。 + +## 行为 + +- **启动报错响亮,重载保留最后可用值。** 存在但非法的文档使插件加载失败;运行中不可读或不可解析的编辑只告警并保留最后可用分节。文档缺失时所有 namespace 按默认值与 `base` 解析;删除文档发布同样的空状态。 +- **写回原子且仅属主可读。** `persist` 以 `0600` 权限写 `.tmp` 后 rename 覆盖目标。YAML 写回在保留注释的文档里只修补目标 namespace;JSON 重新序列化。 +- **按内容抑制自写。** provider 缓存最后可用文本;watcher 事件内容与缓存相同(含自己的写入)即为 no-op。 + +## Model Experience + +间接生效:本 provider 只存储并发布 namespace 分节,模型效果经由 `ctx.settings` 的消费插件产生,由各消费者自己的文档描述。 + +#### KV Cache effect + +无直接失效;请求前缀的变更由消费插件拥有。 + +## Known Limitations and Deferred Work + +- **无跨进程写锁** — 并发写入者(例如同一 home 上的 TUI 与 web)靠原子替换加 watcher 重载收敛,后写胜出;lockfile 等真实冲突出现再做。 +- **注释保留仅限 YAML** — JSON 文档重新序列化,无注释(JSON 本身没有)且丢失手工排版。 +- **无值间接引用** — 分节存字面值;面向密钥的 `${env:VAR}` 式引用是 seam 层的延后特性。 diff --git a/packages/settings/settings-local/package.json b/packages/settings/settings-local/package.json new file mode 100644 index 0000000000..aefb1ccd33 --- /dev/null +++ b/packages/settings/settings-local/package.json @@ -0,0 +1,46 @@ +{ + "name": "@deepseek-ai/dsh-settings-local", + "description": "File-backed settings provider (settings.yaml) for the DeepSeek Harness", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./invariant": { + "types": "./lib/types/invariant.d.ts", + "default": "./lib/invariant.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/invariant.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "@deepseek-ai/dsh-invariants": "^0.0.1", + "@deepseek-ai/dsh-paths": "^0.0.1", + "@deepseek-ai/dsh-settings": "^0.0.1", + "cordis": "^4.0.0-rc.7" + }, + "dependencies": { + "chokidar": "^4.0.3", + "schemastery": "^3.18.0", + "yaml": "^2.9.0" + }, + "devDependencies": { + "@deepseek-ai/dsh-invariants": "workspace:^", + "@deepseek-ai/dsh-paths": "workspace:^", + "@deepseek-ai/dsh-settings": "workspace:^", + "cordis": "^4.0.0-rc.7" + } +} diff --git a/packages/settings/settings-local/src/index.ts b/packages/settings/settings-local/src/index.ts new file mode 100644 index 0000000000..421411ea50 --- /dev/null +++ b/packages/settings/settings-local/src/index.ts @@ -0,0 +1,226 @@ +/** + * File-backed settings provider. One YAML or JSON document under the user's + * harness home carries every namespace section; external edits hot-publish + * through the seam and `update()` writes back preserving the user's comments. + * @module @deepseek-ai/dsh-settings-local + */ + +import { Context, Service } from 'cordis' +import z from 'schemastery' +import { watch as chokidarWatch } from 'chokidar' +import { mkdir, readFile, rename, writeFile } from 'node:fs/promises' +import { dirname, extname, join, resolve } from 'node:path' +import { Document, parseDocument } from 'yaml' +import { resolveDshHome } from '@deepseek-ai/dsh-paths' +import { Settings, type SettingsNamespace } from '@deepseek-ai/dsh-settings' + +/** Plugin config: file location and hot-reload behavior. */ +export interface Config { + /** Settings document path; defaults to `settings.yaml` under the harness home. */ + path?: string + /** Harness home used when `path` is omitted; defaults to `$DSH_HOME` or `~/.dsh`. */ + dshHome?: string + /** Watch the document and hot-publish external edits; defaults to true. */ + watch?: boolean + /** Watcher write-settle window in milliseconds; defaults to 100. */ + debounceMs?: number +} + +/** Document format derived from the configured file extension. */ +type SettingsFormat = 'yaml' | 'json' + +const FORMATS: Record = { + '.yaml': 'yaml', + '.yml': 'yaml', + '.json': 'json', +} + +/** Fully resolved provider parameters; defaulting happens here, never inline. */ +interface ResolvedSpec { + filename: string + format: SettingsFormat + watch: boolean + debounceMs: number +} + +/** + * Resolve the runtime spec from plugin config: an explicit `path` wins, + * otherwise the document lives at `/settings.yaml`. + * @param config - raw plugin config. + * @returns the resolved file location, format, and watch behavior. + */ +export function resolveSpec(config: Config): ResolvedSpec { + const filename = resolve(config.path ?? join(resolveDshHome(config.dshHome), 'settings.yaml')) + const format = FORMATS[extname(filename)] + if (format === undefined) { + throw new Error(`settings-local: extension "${extname(filename)}" is not supported (use .yaml, .yml, or .json)`) + } + return { + filename, + format, + watch: config.watch ?? true, + debounceMs: config.debounceMs ?? 100, + } +} + +/** Whether a filesystem error means absence; every non-ENOENT failure must surface. */ +function isENOENT(error: unknown): boolean { + return (error as NodeJS.ErrnoException | null)?.code === 'ENOENT' +} + +/** File-backed settings provider (`settings.yaml`/`.json`). */ +export class SettingsLocal extends Settings { + static Config: z = z.object({ + path: z.string(), + dshHome: z.string(), + watch: z.boolean().default(true), + debounceMs: z.number().min(0).default(100), + }) + + private readonly spec: ResolvedSpec + /** + * Raw text of the last successfully parsed or persisted document; + * `undefined` while the file is absent. Watcher events whose content equals + * this cache are no-ops, which is also the self-write suppression. + */ + private text: string | undefined + /** Serializes watcher-triggered reloads so reads never interleave. */ + private refreshTask: Promise = Promise.resolve() + + constructor(ctx: Context, public config: Config) { + super(ctx) + // Programmatic construction may bypass Schemastery normalization; resolve + // the same defaults in one explicit step either way. + this.spec = resolveSpec(config) + } + + /** The local document is always writable through {@link Settings.update}. */ + get writable(): boolean { + return true + } + + protected async load(): Promise> { + let text: string + try { + text = await readFile(this.spec.filename, 'utf8') + } catch (error) { + if (!isENOENT(error)) throw error + this.text = undefined + return {} + } + const doc = this.parse(text) + this.text = text + return doc + } + + protected async persist(ns: SettingsNamespace, section: Record): Promise { + const output = this.spec.format === 'yaml' + ? this.renderYaml(ns, section) + : this.renderJson(ns, section) + await mkdir(dirname(this.spec.filename), { recursive: true }) + const temp = `${this.spec.filename}.tmp` + // Owner-only permissions apply to the temp file and survive the rename, so + // a document that may carry personal values is never world-readable. + await writeFile(temp, output, { mode: 0o600 }) + await rename(temp, this.spec.filename) + this.text = output + } + + async* [Service.init](): AsyncGenerator<() => Promise | void, void, void> { + // A parse failure here is a boot failure: an existing-but-invalid document + // must fail loud, never be silently ignored or overwritten. + this.publish(await this.load()) + if (!this.spec.watch) return + const watcher = chokidarWatch(this.spec.filename, { + ignoreInitial: true, + awaitWriteFinish: { + stabilityThreshold: this.spec.debounceMs, + pollInterval: Math.max(1, Math.min(this.spec.debounceMs, 10)), + }, + }) + watcher.on('all', () => { + this.refreshTask = this.refreshTask.then(() => this.refresh()) + }) + watcher.on('error', (error) => { + this.ctx.logger.warn('settings-local: watcher error on %s', this.spec.filename) + this.ctx.logger.warn(error) + }) + yield () => watcher.close() + } + + /** Parse one document text into raw sections, failing on a non-map root. */ + private parse(text: string): Record { + let root: unknown + if (this.spec.format === 'yaml') { + const document = parseDocument(text, { prettyErrors: true }) + if (document.errors.length > 0) { + throw new Error(`settings-local: invalid document at ${this.spec.filename}: ${ + document.errors.map(error => error.message).join('; ')}`) + } + root = document.toJS() ?? {} + } else { + root = text.trim().length === 0 ? {} : JSON.parse(text) + } + if (typeof root !== 'object' || root === null || Array.isArray(root)) { + throw new TypeError(`settings-local: ${this.spec.filename} must be a map of namespace sections`) + } + return root as Record + } + + /** + * Re-read the document after a watcher event. Unchanged content (including + * this provider's own writes) is a no-op; an unreadable or unparsable + * document keeps the last good sections and warns — a live hot-reload must + * never take the process down. + */ + private async refresh(): Promise { + let text: string + try { + text = await readFile(this.spec.filename, 'utf8') + } catch (error) { + if (!isENOENT(error)) { + this.ctx.logger.warn('settings-local: reload failed at %s; keeping the last good document', this.spec.filename) + this.ctx.logger.warn(error) + return + } + if (this.text === undefined) return + this.text = undefined + this.publish({}) + return + } + if (text === this.text) return + let doc: Record + try { + doc = this.parse(text) + } catch (error) { + this.ctx.logger.warn('settings-local: reload failed at %s; keeping the last good document', this.spec.filename) + this.ctx.logger.warn(error) + return + } + this.text = text + this.publish(doc) + } + + /** Render the next YAML text by patching one namespace in the comment-preserving document. */ + private renderYaml(ns: SettingsNamespace, section: Record): string { + if (this.text === undefined) { + return new Document({ [ns]: section }).toString() + } + // this.text only ever caches content that parsed successfully, so this + // re-parse (for the mutable comment-preserving tree) cannot fail. + const document = parseDocument(this.text) + document.set(ns, section) + return document.toString() + } + + /** Render the next JSON text by replacing one namespace key. */ + private renderJson(ns: SettingsNamespace, section: Record): string { + const root = this.text === undefined + ? {} + : this.parse(this.text) + root[ns] = section + return `${JSON.stringify(root, null, 2)}\n` + } +} + +export default SettingsLocal diff --git a/packages/settings/settings-local/src/invariant.ts b/packages/settings/settings-local/src/invariant.ts new file mode 100644 index 0000000000..b59b798298 --- /dev/null +++ b/packages/settings/settings-local/src/invariant.ts @@ -0,0 +1,31 @@ +/** + * Package-owned invariant companion for `@deepseek-ai/dsh-settings-local`. + * @module @deepseek-ai/dsh-settings-local/invariant + */ + +/* jscpd:ignore-start */ +import type { Context } from 'cordis' +import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants' + +const PACKAGE_NAME = '@deepseek-ai/dsh-settings-local' + +/** Cordis companion plugin name. */ +export const name = 'settings-local-invariant' +/** Service required before the companion can reserve package ownership. */ +export const inject = ['invariants'] + +/** + * No runtime invariant: this provider's contracts are file round-trip, + * watcher timing, and atomic-write behavior — IO effects proven by package + * tests; the in-process commit relation is owned by `@deepseek-ai/dsh-settings`. + */ +const install: InvariantInstaller = () => {} + +/** + * Register this package's invariant companion. + * @param ctx - Cordis context carrying the invariant service. + * @returns the installed registration's disposer after setup succeeds. + */ +export const apply = (ctx: Context): Promise<() => void> => + Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install)) +/* jscpd:ignore-end */ diff --git a/packages/settings/settings-local/tests/loader-composition.spec.ts b/packages/settings/settings-local/tests/loader-composition.spec.ts new file mode 100644 index 0000000000..5744e92f46 --- /dev/null +++ b/packages/settings/settings-local/tests/loader-composition.spec.ts @@ -0,0 +1,112 @@ +/** + * Real-composition guard: the provider and a consumer plugin boot from a + * test-only cordis.yml through the actual Loader + Include path, and an + * external edit of settings.yaml hot-publishes into the consumer's scope. + */ + +import { mkdtemp, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { pathToFileURL } from 'node:url' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import Loader from '@cordisjs/plugin-loader' +import Include from '@cordisjs/plugin-include' +import z from 'schemastery' +import { settingsNamespace, type SettingsScope } from '@deepseek-ai/dsh-settings' +import SettingsLocal from '../src/index.ts' + +interface ThemeConfig { + theme: 'dark' | 'light' + fontSize: number +} + +const ThemeSchema: z = z.object({ + theme: z.union(['dark', 'light']).default('dark'), + fontSize: z.number().default(14), +}) + +let root: string | undefined +let context: Context | undefined + +afterEach(async () => { + await context?.fiber.dispose() + context = undefined + if (root !== undefined) await rm(root, { recursive: true, force: true }) + root = undefined +}) + +interface ConsumerState { + scope: SettingsScope | undefined + seen: ThemeConfig[] +} + +async function loadComposition(): Promise<{ ctx: Context; state: ConsumerState; settingsPath: string }> { + root = await mkdtemp(join(tmpdir(), 'dsh-settings-composition-')) + const settingsPath = join(root, 'settings.yaml') + await writeFile(settingsPath, 'ui-theme:\n theme: light\n') + + const state: ConsumerState = { scope: undefined, seen: [] } + const consumer = { + name: 'settings-consumer', + inject: ['settings'], + apply: (ctx: Context) => { + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema, { + base: { fontSize: 16 }, + }) + state.scope = scope + scope.watch(next => state.seen.push(next)) + }, + } + + const configPath = join(root, 'cordis.yml') + await writeFile(configPath, [ + '- id: settings', + " name: '@deepseek-ai/dsh-settings-local'", + ' config:', + ` path: ${JSON.stringify(settingsPath)}`, + ' debounceMs: 10', + '- id: consumer', + ' name: test-settings-consumer', + '', + ].join('\n')) + + const ctx = new Context() + context = ctx + ctx.baseUrl = pathToFileURL(root).href + '/' + await ctx.plugin(Loader) + ctx.loader.builtins.include = Include + const modules = new Map([ + ['@deepseek-ai/dsh-settings-local', SettingsLocal], + ['test-settings-consumer', consumer], + ]) + ctx.loader.internal = { + version: 'v2', + async import(specifier: string) { + if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`) + return modules.get(specifier) + }, + } as unknown as NonNullable + await ctx.loader.create({ + name: 'cordis:include', + config: { path: pathToFileURL(configPath).href }, + }) + await ctx.loader.await() + return { ctx, state, settingsPath } +} + +describe('settings-local real composition', () => { + it('boots from cordis.yml and hot-publishes an external settings edit', async () => { + const { ctx, state, settingsPath } = await loadComposition() + + // Composition resolution: user layer over the consumer's composition base. + expect(state.scope!.get()).toEqual({ theme: 'light', fontSize: 16 }) + expect(ctx.get('settings')!.describe().map(entry => entry.ns)).toEqual(['ui-theme']) + + await writeFile(settingsPath, 'ui-theme:\n theme: dark\n fontSize: 20\n') + await vi.waitFor(() => { + expect(state.scope!.get()).toEqual({ theme: 'dark', fontSize: 20 }) + }, { timeout: 5000 }) + expect(state.seen.at(-1)).toEqual({ theme: 'dark', fontSize: 20 }) + }) +}) diff --git a/packages/settings/settings-local/tests/local.spec.ts b/packages/settings/settings-local/tests/local.spec.ts new file mode 100644 index 0000000000..9c9d472bac --- /dev/null +++ b/packages/settings/settings-local/tests/local.spec.ts @@ -0,0 +1,254 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import z from 'schemastery' +import { chmod, mkdtemp, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { settingsNamespace } from '@deepseek-ai/dsh-settings' +import { SettingsLocal, resolveSpec } from '../src/index.ts' + +interface ThemeConfig { + theme: 'dark' | 'light' + fontSize: number +} + +const ThemeSchema: z = z.object({ + theme: z.union(['dark', 'light']).default('dark'), + fontSize: z.number().default(14), +}) + +const cleanups: Array<() => Promise> = [] + +afterEach(async () => { + while (cleanups.length > 0) await cleanups.pop()!() +}) + +async function tempDir(): Promise { + const dir = await mkdtemp(join(tmpdir(), 'dsh-settings-local-')) + cleanups.push(() => rm(dir, { recursive: true, force: true })) + return dir +} + +async function boot(config: ConstructorParameters[1]): Promise { + const ctx = new Context() + const fiber = ctx.plugin(SettingsLocal, config) + cleanups.push(async () => { await fiber.dispose() }) + await fiber + return ctx +} + +describe('resolveSpec', () => { + it('defaults watch and debounce when construction bypasses schema normalization', () => { + const spec = resolveSpec({ path: '/tmp/anywhere/settings.yaml' }) + expect(spec.watch).toBe(true) + expect(spec.debounceMs).toBe(100) + }) +}) + +describe('boot and reads', () => { + it('resolves defaults over an absent file and reports writable', async () => { + const dir = await tempDir() + const ctx = await boot({ path: join(dir, 'settings.yaml'), watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema, { + base: { fontSize: 16 }, + }) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 16 }) + expect(ctx.settings.writable).toBe(true) + }) + + it('reads sections from an existing yaml document', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(scope.get()).toEqual({ theme: 'light', fontSize: 14 }) + }) + + it('reads sections from a json document', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.json') + await writeFile(path, JSON.stringify({ 'ui-theme': { fontSize: 18 } })) + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 18 }) + }) + + it('defaults the file location under the configured harness home', async () => { + const dir = await tempDir() + const ctx = await boot({ dshHome: dir, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ theme: 'light' }) + const written = await readFile(join(dir, 'settings.yaml'), 'utf8') + expect(written).toContain('theme: light') + }) + + it('reads an empty yaml document as no sections', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, '') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 14 }) + }) + + it('reads an empty json document as no sections', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.json') + await writeFile(path, '') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 14 }) + }) + + it('fails loud at boot when the document exists but is unreadable', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + await chmod(path, 0o000) + cleanups.push(() => chmod(path, 0o600)) + await expect(boot({ path, watch: false })).rejects.toThrow(/EACCES|permission/i) + }) + + it('fails loud on an unsupported extension', async () => { + const dir = await tempDir() + await expect(boot({ path: join(dir, 'settings.toml'), watch: false })) + .rejects.toThrow(/not supported/) + }) + + it('fails loud at boot on unparsable yaml', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme: [unclosed\n') + await expect(boot({ path, watch: false })).rejects.toThrow() + }) + + it('fails loud at boot when the root is not a map of sections', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, '- just\n- a list\n') + await expect(boot({ path, watch: false })).rejects.toThrow(/map of namespace sections/) + }) +}) + +describe('persist', () => { + it('writes the merged section, creating the file with owner-only permissions', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ theme: 'light' }) + + const written = await readFile(path, 'utf8') + expect(written).toContain('theme: light') + expect((await stat(path)).mode & 0o777).toBe(0o600) + // Atomic replace leaves no temp artifact behind. + expect((await readdir(dir)).sort()).toEqual(['settings.yaml']) + }) + + it('preserves comments and unregistered sections across updates', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, [ + '# personal settings', + 'ui-theme:', + ' theme: light', + '# owned by a plugin that is not loaded right now', + 'future-plugin:', + ' keep: me', + '', + ].join('\n')) + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ fontSize: 18 }) + + const written = await readFile(path, 'utf8') + expect(written).toContain('# personal settings') + expect(written).toContain('# owned by a plugin that is not loaded right now') + expect(written).toContain('keep: me') + expect(written).toContain('fontSize: 18') + expect(written).toContain('theme: light') + }) + + it('creates a json document from scratch', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.json') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ theme: 'light' }) + const written = JSON.parse(await readFile(path, 'utf8')) as Record + expect(written).toEqual({ 'ui-theme': { theme: 'light' } }) + }) + + it('round-trips a json document', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.json') + await writeFile(path, JSON.stringify({ other: { keep: true } }, null, 2)) + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ theme: 'light' }) + const written = JSON.parse(await readFile(path, 'utf8')) as Record + expect(written).toEqual({ other: { keep: true }, 'ui-theme': { theme: 'light' } }) + }) +}) + +describe('watch', () => { + it('publishes an external edit to registered scopes', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, debounceMs: 10 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(scope.get().theme).toBe('light') + + await writeFile(path, 'ui-theme:\n theme: dark\n fontSize: 20\n') + await vi.waitFor(() => { + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 20 }) + }, { timeout: 5000 }) + }) + + it('keeps the last good document over an invalid edit, then recovers', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, debounceMs: 10 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + + await writeFile(path, 'ui-theme: [unclosed\n') + // The bad edit must never take the live tree down or reset the value. + await new Promise(resolve => setTimeout(resolve, 300)) + expect(scope.get()).toEqual({ theme: 'light', fontSize: 14 }) + + await writeFile(path, 'ui-theme:\n theme: dark\n') + await vi.waitFor(() => { + expect(scope.get().theme).toBe('dark') + }, { timeout: 5000 }) + }) + + it('treats file removal as an empty document', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, debounceMs: 10 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + + await rm(path) + await vi.waitFor(() => { + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 14 }) + }, { timeout: 5000 }) + }) + + it('does not republish its own persisted write', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, debounceMs: 10 }) + const events: unknown[] = [] + ctx.on('settings/updated', (ns, _next, _prev, source) => { + events.push({ ns, source }) + }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ theme: 'light' }) + await new Promise(resolve => setTimeout(resolve, 300)) + expect(events).toEqual([{ ns: 'ui-theme', source: 'update' }]) + }) +}) diff --git a/packages/settings/settings-local/tests/watcher.spec.ts b/packages/settings/settings-local/tests/watcher.spec.ts new file mode 100644 index 0000000000..00c67eacc7 --- /dev/null +++ b/packages/settings/settings-local/tests/watcher.spec.ts @@ -0,0 +1,118 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import z from 'schemastery' +import { chmod, mkdtemp, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { settingsNamespace } from '@deepseek-ai/dsh-settings' +import { SettingsLocal } from '../src/index.ts' + +// chokidar is the nondeterministic OS boundary: faking it lets these tests +// drive the event pipeline (error events, races with unreadable files) +// deterministically. Real end-to-end watching stays covered by local.spec.ts. +vi.mock('chokidar', async () => { + const { EventEmitter } = await import('node:events') + class FakeWatcher extends EventEmitter { + close = vi.fn(() => Promise.resolve()) + } + const instances: Array<{ path: string; options: unknown; watcher: InstanceType }> = [] + return { + watch: vi.fn((path: string, options: unknown) => { + const watcher = new FakeWatcher() + instances.push({ path, options, watcher }) + return watcher + }), + __instances: instances, + } +}) + +interface FakeChokidar { + __instances: Array<{ + path: string + options: { awaitWriteFinish: { stabilityThreshold: number; pollInterval: number } } + watcher: import('node:events').EventEmitter + }> +} + +async function fakeInstances(): Promise { + const chokidar = await import('chokidar') as unknown as FakeChokidar + return chokidar.__instances +} + +const ThemeSchema: z<{ theme: string }> = z.object({ + theme: z.string().default('dark'), +}) + +const cleanups: Array<() => Promise> = [] + +afterEach(async () => { + while (cleanups.length > 0) await cleanups.pop()!() + ;(await fakeInstances()).length = 0 +}) + +async function tempDir(): Promise { + const dir = await mkdtemp(join(tmpdir(), 'dsh-settings-watch-')) + cleanups.push(() => rm(dir, { recursive: true, force: true })) + return dir +} + +async function boot(config: ConstructorParameters[1]): Promise { + const ctx = new Context() + const fiber = ctx.plugin(SettingsLocal, config) + cleanups.push(async () => { await fiber.dispose() }) + await fiber + return ctx +} + +describe('watcher pipeline', () => { + it('clamps the write-settle poll interval for a zero debounce', async () => { + const dir = await tempDir() + await boot({ path: join(dir, 'settings.yaml'), debounceMs: 0 }) + const [instance] = await fakeInstances() + expect(instance!.options.awaitWriteFinish).toEqual({ stabilityThreshold: 0, pollInterval: 1 }) + }) + + it('survives a watcher error and keeps publishing later edits', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, debounceMs: 5 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const [instance] = await fakeInstances() + + instance!.watcher.emit('error', new Error('watch backend failure')) + expect(scope.get()).toEqual({ theme: 'dark' }) + + await writeFile(path, 'ui-theme:\n theme: light\n') + instance!.watcher.emit('all', 'change', path) + await vi.waitFor(() => { + expect(scope.get()).toEqual({ theme: 'light' }) + }) + }) + + it('keeps the last good document when the file turns unreadable at runtime', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, debounceMs: 5 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + + await chmod(path, 0o000) + cleanups.push(() => chmod(path, 0o600)) + const [instance] = await fakeInstances() + instance!.watcher.emit('all', 'change', path) + // The warn-and-keep path is asynchronous; give the serialized refresh a turn. + await new Promise(resolve => setTimeout(resolve, 50)) + expect(scope.get()).toEqual({ theme: 'light' }) + }) + + it('treats an event for a still-absent file as a no-op', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, debounceMs: 5 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const [instance] = await fakeInstances() + instance!.watcher.emit('all', 'add', path) + await new Promise(resolve => setTimeout(resolve, 50)) + expect(scope.get()).toEqual({ theme: 'dark' }) + }) +}) diff --git a/packages/settings/settings-local/tsconfig.json b/packages/settings/settings-local/tsconfig.json new file mode 100644 index 0000000000..67a746c982 --- /dev/null +++ b/packages/settings/settings-local/tsconfig.json @@ -0,0 +1,30 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": [ + "src" + ], + "references": [ + { + "path": "../../../vendor/cosmokit" + }, + { + "path": "../../../vendor/cordis" + }, + { + "path": "../../../vendor/schemastery" + }, + { + "path": "../../util/paths" + }, + { + "path": "../settings" + }, + { + "path": "../../support/invariants" + } + ] +} diff --git a/packages/settings/settings/README.i18n.yaml b/packages/settings/settings/README.i18n.yaml new file mode 100644 index 0000000000..51f30f2ca6 --- /dev/null +++ b/packages/settings/settings/README.i18n.yaml @@ -0,0 +1,6 @@ +# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each +# side as of the last confirmed-consistent state. Both languages carry equal authority; +# after editing either side, bring the other along and re-record with: +# pnpm run verify-translation-pairing --write packages/settings/settings/README.md +README.md: e57db00c095a87f9f0b51397e030dec364f48e62 +README.zh.md: 8733823106f0ef3880cbe2c567de2edcdeff2c86 diff --git a/packages/settings/settings/README.md b/packages/settings/settings/README.md new file mode 100644 index 0000000000..e57db00c09 --- /dev/null +++ b/packages/settings/settings/README.md @@ -0,0 +1,35 @@ +# @deepseek-ai/dsh-settings + +English | [中文](README.zh.md) + +Abstract user-settings seam (`ctx.settings`). One provider holds a raw document of per-namespace sections; plugins register a namespace schema and read a resolved value layered as schema defaults, then the registrant's composition `base` (its cordis.yml entry-config subset), then the user document section. Without a mounted provider nothing changes for consumers: they keep resolving entry config alone, so every composition works with or without settings. + +## Service API + +- `register(ns, schema, { base?, applies? })` — returns the owner `SettingsScope` (`get`/`watch`/`update`). The registration is an effect on the calling plugin's fiber: disposing that fiber removes the namespace and its observers. A stored section the schema rejects fails the registration itself; a duplicate namespace fails loud. +- `describe()` — one descriptor per namespace (`schema.toJSON()` envelope, resolved value, `applies`) for configuration surfaces. +- `get(ns)` — resolved value, `undefined` while unregistered. +- `update(ns, patch)` — deep-merges the plain-object patch into the user section only (never the `base`), validates the resolved candidate, persists through the provider, then commits. Validation failure rejects before anything is persisted; a read-only provider (`writable: false`) rejects every update. +- Resolved values are deep-frozen snapshots; watchers receive `(next, prev)` after each commit, and watcher failures are contained. + +## Provider contract + +Subclasses implement `writable`, `load()`, and `persist(ns, section)`, and push externally observed documents through the protected `publish(doc)`. At publish, each registered namespace re-resolves independently: an invalid section keeps that namespace's last good value and warns — a live reload never takes the process down — while boot-time and registration-time validation fail loud. + +## Events + +`settings/updated (ns, next, prev, source)` fires after each commit; `source` is `update` (in-process write) or `provider` (external change). It never fires for a deep-equal resolved value. + +## Model Experience + +Indirectly, through consumer plugins that resolve model-affecting values (for example a default model route) from their namespaces; each consumer's own surface documents the effect. + +#### KV Cache effect + +No direct invalidation; a consumer that folds a settings value into the request prefix owns that change. + +## Known Limitations and Deferred Work + +- **Single user layer** — resolution knows schema defaults, one composition `base`, and one user document; there is no project/managed layering or per-value provenance yet. +- **Cross-process concurrency is provider-defined** — the seam serializes nothing across processes; concurrent writers converge by provider behavior (the local file provider is last-write-wins). +- **No secret-field redaction** — `describe()` returns resolved values verbatim; a wire surface (RPC/UI) must redact `role('secret')` fields before exposure. diff --git a/packages/settings/settings/README.zh.md b/packages/settings/settings/README.zh.md new file mode 100644 index 0000000000..8733823106 --- /dev/null +++ b/packages/settings/settings/README.zh.md @@ -0,0 +1,35 @@ +# @deepseek-ai/dsh-settings + +[English](README.md) | 中文 + +抽象用户设置 seam(`ctx.settings`)。一个 provider 持有按 namespace 分节的原始文档;插件注册 namespace schema 并读取分层解析值:schema 默认值,然后注册方的组合 `base`(其 cordis.yml entry 配置子集),最后用户文档分节。不挂载 provider 时消费者行为不变:仍只按 entry 配置解析,因此任何组合有无 settings 都能工作。 + +## 服务 API + +- `register(ns, schema, { base?, applies? })` — 返回 owner 的 `SettingsScope`(`get`/`watch`/`update`)。注册是调用方插件 fiber 上的 effect:dispose 该 fiber 即移除 namespace 及其观察者。schema 拒绝的存量分节会使注册本身失败;重复 namespace 立即报错。 +- `describe()` — 每个 namespace 一条描述(`schema.toJSON()` 信封、解析值、`applies`),供配置界面使用。 +- `get(ns)` — 解析值;未注册时为 `undefined`。 +- `update(ns, patch)` — 把普通对象 patch 深合并进用户分节(绝不合并进 `base`),校验解析候选值,经 provider 持久化后提交。校验失败在持久化前拒绝;只读 provider(`writable: false`)拒绝一切更新。 +- 解析值是深冻结快照;每次提交后观察者收到 `(next, prev)`,观察者异常被隔离。 + +## Provider 契约 + +子类实现 `writable`、`load()`、`persist(ns, section)`,并通过受保护的 `publish(doc)` 推入外部观察到的文档。publish 时每个已注册 namespace 独立重解析:非法分节保留该 namespace 的最后可用值并告警——热重载绝不拖垮进程;启动期与注册期校验则立即报错。 + +## 事件 + +`settings/updated (ns, next, prev, source)` 在每次提交后触发;`source` 为 `update`(进程内写入)或 `provider`(外部变更)。解析值深相等时绝不触发。 + +## Model Experience + +间接生效:消费插件从各自 namespace 解析影响模型的值(例如默认模型路由);效果由各消费者自己的文档描述。 + +#### KV Cache effect + +无直接失效;把设置值折叠进请求前缀的消费者拥有该变更。 + +## Known Limitations and Deferred Work + +- **单一用户层** — 解析只认识 schema 默认值、一个组合 `base` 与一个用户文档;尚无 project/managed 分层或按值溯源。 +- **跨进程并发由 provider 定义** — seam 不做跨进程串行化;并发写入者按 provider 行为收敛(本地文件 provider 为后写胜出)。 +- **无 secret 字段脱敏** — `describe()` 原样返回解析值;wire 面(RPC/UI)在暴露前必须对 `role('secret')` 字段脱敏。 diff --git a/packages/settings/settings/package.json b/packages/settings/settings/package.json new file mode 100644 index 0000000000..7586b4bb24 --- /dev/null +++ b/packages/settings/settings/package.json @@ -0,0 +1,41 @@ +{ + "name": "@deepseek-ai/dsh-settings", + "description": "Abstract user-settings seam (ctx.settings) for the DeepSeek Harness", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./invariant": { + "types": "./lib/types/invariant.d.ts", + "default": "./lib/invariant.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/invariant.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "@deepseek-ai/dsh-brand": "^0.0.1", + "@deepseek-ai/dsh-invariants": "^0.0.1", + "cordis": "^4.0.0-rc.7", + "schemastery": "^3.18.0" + }, + "devDependencies": { + "@deepseek-ai/dsh-brand": "workspace:^", + "@deepseek-ai/dsh-invariants": "workspace:^", + "cordis": "^4.0.0-rc.7", + "schemastery": "^3.18.0" + } +} diff --git a/packages/settings/settings/src/index.ts b/packages/settings/settings/src/index.ts new file mode 100644 index 0000000000..bec60dfdea --- /dev/null +++ b/packages/settings/settings/src/index.ts @@ -0,0 +1,304 @@ +/** + * User-settings seam (`ctx.settings`). Providers store one raw document of + * per-namespace sections; plugins register a namespace schema and read the + * resolved value, which layers schema defaults, the registrant's composition + * `base`, and the user document section, in that order. + * @module @deepseek-ai/dsh-settings + */ + +import { Context, Service } from 'cordis' +import { deepEqual } from 'cosmokit' +import type z from 'schemastery' +import type { Branded } from '@deepseek-ai/dsh-brand' + +/** Nominal id of one registered settings namespace. */ +export type SettingsNamespace = Branded<'SettingsNamespace'> + +const NAMESPACE_PATTERN = /^[a-z][a-z0-9-]*$/ + +/** + * Brand a raw string as a {@link SettingsNamespace}. + * @param value - candidate namespace; lowercase kebab-case, as in plugin short names. + * @returns the branded namespace. + */ +export function settingsNamespace(value: string): SettingsNamespace { + if (!NAMESPACE_PATTERN.test(value)) { + throw new TypeError(`settings namespace "${value}" must match ${String(NAMESPACE_PATTERN)}`) + } + return value as SettingsNamespace +} + +/** When a namespace's changes take effect for its owner. */ +export type SettingsApplies = 'live' | 'restart' + +/** Origin of one committed settings change. */ +export type SettingsUpdateSource = 'update' | 'provider' + +/** Registration options beyond the namespace schema. */ +export interface SettingsRegisterOptions { + /** Composition-layer values resolved below the user layer (entry-config subset). */ + base?: Partial + /** Owner's effect timing, surfaced to configuration UIs; defaults to `live`. */ + applies?: SettingsApplies +} + +/** One registered namespace as surfaced to configuration UIs. */ +export interface SettingsDescriptor { + /** The registered namespace. */ + ns: SettingsNamespace + /** Serialized schemastery schema (`schema.toJSON()`). */ + schema: unknown + /** Current resolved value. */ + value: unknown + /** Owner's declared effect timing. */ + applies: SettingsApplies +} + +/** Owner-facing handle for one registered namespace. */ +export interface SettingsScope { + /** Current resolved value: schema defaults, then `base`, then the user layer. */ + get(): T + /** + * Observe committed changes to this namespace's resolved value. + * @param callback - invoked after each commit with the next and previous values. + * @returns the disposer removing this observer. + */ + watch(callback: (next: T, prev: T) => void): () => void + /** + * Merge a partial patch into this namespace's user layer and persist it. + * @param patch - plain-object patch over the user section. + */ + update(patch: object): Promise +} + +declare module 'cordis' { + interface Context { + settings: Settings + } + + interface Events { + /** + * Committed change to one registered namespace's resolved value. Emitted + * after the provider persisted (for `update`) or published (`provider`) + * the change; never emitted when the resolved value is deep-equal. + * @param ns - the namespace whose resolved value changed. + * @param next - the new resolved value. + * @param prev - the previous resolved value. + * @param source - whether the change entered through `update()` or the provider. + * @mode emit + */ + 'settings/updated'(ns: SettingsNamespace, next: unknown, prev: unknown, source: SettingsUpdateSource): void + } +} + +/** Whether a value is a plain data object (not an array, null, or class instance). */ +function isPlainObject(value: unknown): value is Record { + if (typeof value !== 'object' || value === null || Array.isArray(value)) return false + const proto: unknown = Object.getPrototypeOf(value) + return proto === Object.prototype || proto === null +} + +/** + * Layer `over` onto `under`: plain objects merge recursively, every other + * value (arrays included) replaces the lower layer wholesale, and `undefined` + * entries in `over` are ignored so a sparse patch cannot erase lower keys. + */ +function mergeLayers(under: unknown, over: unknown): unknown { + if (over === undefined) return under + if (!isPlainObject(under) || !isPlainObject(over)) return over + const merged: Record = { ...under } + for (const [key, value] of Object.entries(over)) { + if (value === undefined) continue + merged[key] = key in merged ? mergeLayers(merged[key], value) : value + } + return merged +} + +/** Recursively freeze one resolved value so handed-out snapshots stay immutable. */ +function deepFreeze(value: T): T { + if (typeof value !== 'object' || value === null || Object.isFrozen(value)) return value + for (const entry of Object.values(value)) deepFreeze(entry) + return Object.freeze(value) +} + +/** One live namespace registration owned by a registrant fiber. */ +interface SettingsRegistration { + ns: SettingsNamespace + schema: z + base: unknown + applies: SettingsApplies + resolved: unknown + watchers: Set<(next: never, prev: never) => void> +} + +/** + * Abstract settings service. Providers implement raw-document storage + * (`load`/`persist`) and push external changes through {@link Settings.publish}; + * the base class owns namespace registration, resolution, validation, change + * detection, and the `settings/updated` commit event. + */ +export abstract class Settings extends Service { + private readonly registrations = new Map() + /** Latest published raw document; empty until the provider's first publish. */ + private document: Record = {} + + constructor(ctx: Context) { + super(ctx, 'settings') + } + + /** Whether {@link update} may persist through this provider. */ + abstract readonly writable: boolean + + /** + * Read the provider's current raw document (namespace to raw section). + * @returns the detached raw document. + */ + protected abstract load(): Promise> + + /** + * Durably store one namespace's merged user section. + * @param ns - the namespace being written. + * @param section - the complete merged user section to store. + */ + protected abstract persist(ns: SettingsNamespace, section: Record): Promise + + /** + * Register a namespace schema and receive its owner scope. The registration + * is an effect on the calling plugin's fiber: disposing that fiber removes + * the namespace and its observers. An invalid stored section fails the + * registration itself — the earliest point where the schema can judge it. + * @param ns - unique namespace; duplicate registration fails loud. + * @param schema - schemastery schema resolving this namespace's value. + * @param options - composition `base` layer and effect timing. + * @returns the owner scope for reads, observation, and updates. + */ + register(ns: SettingsNamespace, schema: z, options?: SettingsRegisterOptions): SettingsScope { + if (this.registrations.has(ns)) { + throw new Error(`settings namespace "${ns}" is already registered`) + } + const registration: SettingsRegistration = { + ns, + schema: schema as z, + base: options?.base, + applies: options?.applies ?? 'live', + resolved: deepFreeze(this.resolve(schema, options?.base, this.section(ns))), + watchers: new Set(), + } + this.ctx.effect(() => { + this.registrations.set(ns, registration) + return () => this.registrations.delete(ns) + }, `settings.register(${JSON.stringify(String(ns))})`) + return { + get: () => registration.resolved as T, + watch: (callback) => { + registration.watchers.add(callback) + return () => registration.watchers.delete(callback) + }, + update: patch => this.update(ns, patch), + } + } + + /** + * Describe every registered namespace for configuration surfaces. + * @returns one descriptor per registered namespace, in registration order. + */ + describe(): SettingsDescriptor[] { + return [...this.registrations.values()].map(registration => ({ + ns: registration.ns, + schema: registration.schema.toJSON(), + value: registration.resolved, + applies: registration.applies, + })) + } + + /** + * Read one registered namespace's resolved value. + * @param ns - the namespace to read. + * @returns the resolved value, or `undefined` while unregistered. + */ + get(ns: SettingsNamespace): unknown { + return this.registrations.get(ns)?.resolved + } + + /** + * Merge a patch into one registered namespace's user layer, validate the + * resolved candidate, persist through the provider, then commit and emit. + * A validation failure rejects before anything is persisted. + * @param ns - the registered namespace to update. + * @param patch - plain-object patch over the user section. + */ + async update(ns: SettingsNamespace, patch: object): Promise { + const registration = this.registrations.get(ns) + if (registration === undefined) { + throw new Error(`settings namespace "${ns}" is not registered`) + } + if (!this.writable) { + throw new Error(`settings provider is read-only: "${ns}" cannot be updated in-process`) + } + if (!isPlainObject(patch)) { + throw new TypeError(`settings update for "${ns}" must be a plain object patch`) + } + const section = mergeLayers(this.section(ns) ?? {}, patch) as Record + const next = deepFreeze(this.resolve(registration.schema, registration.base, section)) + await this.persist(ns, section) + this.document[ns] = section + this.commit(registration, next, 'update') + } + + /** + * Provider hook: commit a complete raw document observed in storage. Each + * registered namespace re-resolves; an invalid section keeps that + * namespace's last good value and warns, other namespaces still commit. + * @param doc - the detached raw document (unregistered sections preserved). + * @param source - change origin; defaults to `provider`. + */ + protected publish(doc: Record, source: SettingsUpdateSource = 'provider'): void { + this.document = doc + for (const registration of this.registrations.values()) { + let next: unknown + try { + next = deepFreeze(this.resolve(registration.schema, registration.base, this.section(registration.ns))) + } catch (error) { + this.ctx.logger.warn('settings: keeping last good "%s" after invalid stored section', registration.ns) + this.ctx.logger.warn(error) + continue + } + this.commit(registration, next, source) + } + } + + /** Read one namespace's raw user section, rejecting non-object sections. */ + private section(ns: SettingsNamespace): Record | undefined { + const section = this.document[ns] + if (section === undefined) return undefined + if (!isPlainObject(section)) { + throw new TypeError(`settings section "${ns}" must be an object of keys`) + } + return section + } + + /** Resolve one namespace value: schema defaults, then `base`, then the user layer. */ + private resolve(schema: z, base: unknown, section: Record | undefined): T { + // The merged candidate is untyped by construction; the schema call is the + // runtime validation that admits it into T. + return schema(mergeLayers(base, section) as never) + } + + /** Commit a resolved value when changed: swap, notify watchers, emit the event. */ + private commit(registration: SettingsRegistration, next: unknown, source: SettingsUpdateSource): void { + const prev = registration.resolved + if (deepEqual(next, prev)) return + registration.resolved = next + for (const watcher of [...registration.watchers]) { + try { + watcher(next as never, prev as never) + } catch (error) { + this.ctx.logger.warn('settings: watcher for "%s" failed', registration.ns) + this.ctx.logger.warn(error) + } + } + this.ctx.emit('settings/updated', registration.ns, next, prev, source) + } +} + +export default Settings diff --git a/packages/settings/settings/src/invariant.ts b/packages/settings/settings/src/invariant.ts new file mode 100644 index 0000000000..235f413aae --- /dev/null +++ b/packages/settings/settings/src/invariant.ts @@ -0,0 +1,41 @@ +/** + * Package-owned invariant companion for `@deepseek-ai/dsh-settings`. + * @module @deepseek-ai/dsh-settings/invariant + */ + +import type { Context } from 'cordis' +import type { InvariantFailure, InvariantInstaller } from '@deepseek-ai/dsh-invariants' + +const PACKAGE_NAME = '@deepseek-ai/dsh-settings' + +/** Cordis companion plugin name. */ +export const name = 'settings-invariant' +/** Service required before the companion can reserve package ownership. */ +export const inject = ['invariants'] + +/** + * Install the commit-event contract: `settings/updated` fires only for a + * currently registered namespace and only when the resolved value changed. + */ +const install: InvariantInstaller = (ctx: Context, fail: InvariantFailure) => { + ctx.on('settings/updated', (ns, next, prev) => { + const settings = ctx.get('settings') + if (settings === undefined) { + fail(`settings/updated for "${ns}" emitted without a live settings service`) + } + if (settings.get(ns) === undefined) { + fail(`settings/updated for "${ns}" emitted while the namespace is unregistered`) + } + if (JSON.stringify(next) === JSON.stringify(prev)) { + fail(`settings/updated for "${ns}" emitted without a resolved-value change`) + } + }) +} + +/** + * Register this package's invariant companion. + * @param ctx - Cordis context carrying the invariant service. + * @returns the installed registration's disposer after setup succeeds. + */ +export const apply = (ctx: Context): Promise<() => void> => + Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install)) diff --git a/packages/settings/settings/tests/invariant.spec.ts b/packages/settings/settings/tests/invariant.spec.ts new file mode 100644 index 0000000000..f12ff6126c --- /dev/null +++ b/packages/settings/settings/tests/invariant.spec.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import z from 'schemastery' +import InvariantService from '@deepseek-ai/dsh-invariants' +import * as SettingsInvariant from '../src/invariant.ts' +import { settingsNamespace } from '../src/index.ts' +import { MemorySettings } from './memory.ts' + +async function setup(withProvider: boolean): Promise { + const ctx = new Context() + await ctx.plugin(InvariantService) + await ctx.plugin(SettingsInvariant) + if (withProvider) await ctx.plugin(MemorySettings) + return ctx +} + +describe('settings invariants', () => { + it('fails a settings/updated emission without a live settings service', async () => { + const ctx = await setup(false) + expect(() => { + ctx.emit('settings/updated', settingsNamespace('ghost'), { a: 1 }, { a: 2 }, 'provider') + }).toThrow(/without a live settings service/) + }) + + it('fails a settings/updated emission for an unregistered namespace', async () => { + const ctx = await setup(true) + expect(() => { + ctx.emit('settings/updated', settingsNamespace('ghost'), { a: 1 }, { a: 2 }, 'provider') + }).toThrow(/unregistered/) + }) + + it('fails a settings/updated emission without a resolved-value change', async () => { + const ctx = await setup(true) + ctx.settings.register(settingsNamespace('ui-theme'), z.object({ + theme: z.string().default('dark'), + })) + expect(() => { + ctx.emit('settings/updated', settingsNamespace('ui-theme'), { theme: 'dark' }, { theme: 'dark' }, 'update') + }).toThrow(/without a resolved-value change/) + }) +}) diff --git a/packages/settings/settings/tests/memory.ts b/packages/settings/settings/tests/memory.ts new file mode 100644 index 0000000000..0b20771cbe --- /dev/null +++ b/packages/settings/settings/tests/memory.ts @@ -0,0 +1,53 @@ +/** + * In-memory settings provider fixture: the smallest real subclass of the seam, + * used by the base-class behavior suite in place of a file- or network-backed + * provider. Kept in `tests/` because production providers live in their own + * packages. + */ + +import { Service } from 'cordis' +import { Settings, type SettingsNamespace } from '../src/index.ts' + +/** In-memory provider exposing the protected seam hooks to tests. */ +export class MemorySettings extends Settings { + /** Raw document the provider "storage" currently holds. */ + doc: Record + /** Every persist() call observed, in order. */ + persisted: Array<{ ns: SettingsNamespace; section: Record }> = [] + /** When false, update() must reject before reaching persist(). */ + writableFlag: boolean + + constructor(ctx: ConstructorParameters[0], options?: { + doc?: Record + writable?: boolean + }) { + super(ctx) + this.doc = structuredClone(options?.doc ?? {}) + this.writableFlag = options?.writable ?? true + } + + get writable(): boolean { + return this.writableFlag + } + + protected load(): Promise> { + return Promise.resolve(structuredClone(this.doc)) + } + + protected persist(ns: SettingsNamespace, section: Record): Promise { + this.persisted.push({ ns, section: structuredClone(section) }) + this.doc[ns] = structuredClone(section) + return Promise.resolve() + } + + /** Simulate an external storage change reaching the provider. */ + pushExternal(doc: Record): void { + this.doc = structuredClone(doc) + this.publish(structuredClone(doc)) + } + + async* [Service.init](): AsyncGenerator<() => void, void, void> { + this.publish(await this.load()) + yield () => { this.persisted.length = 0 } + } +} diff --git a/packages/settings/settings/tests/settings.spec.ts b/packages/settings/settings/tests/settings.spec.ts new file mode 100644 index 0000000000..a3d223c3f7 --- /dev/null +++ b/packages/settings/settings/tests/settings.spec.ts @@ -0,0 +1,307 @@ +import { describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import z from 'schemastery' +import { settingsNamespace, type SettingsScope, type SettingsUpdateSource } from '../src/index.ts' +import { MemorySettings } from './memory.ts' + +interface ThemeConfig { + theme: 'dark' | 'light' + fontSize: number +} + +const ThemeSchema: z = z.object({ + theme: z.union(['dark', 'light']).default('dark'), + fontSize: z.number().default(14), +}) + +interface NestedConfig { + retry: { attempts: number; delayMs: number } + tags: string[] +} + +const NestedSchema: z = z.object({ + retry: z.object({ + attempts: z.number().default(2), + delayMs: z.number().default(100), + }), + tags: z.array(z.string()).default(['default']), +}) + +async function boot(options?: ConstructorParameters[1]) { + const ctx = new Context() + await ctx.plugin(MemorySettings, options) + const provider = ctx.get('settings') as MemorySettings + return { ctx, provider } +} + +/** Record every settings/updated emission. */ +function recordUpdates(ctx: Context) { + const events: Array<{ ns: string; next: unknown; prev: unknown; source: SettingsUpdateSource }> = [] + ctx.on('settings/updated', (ns, next, prev, source) => { + events.push({ ns, next, prev, source }) + }) + return events +} + +describe('settingsNamespace', () => { + it('brands lowercase kebab-case names', () => { + expect(settingsNamespace('ui-theme')).toBe('ui-theme') + }) + + it.each(['', 'UI', '9lives', 'a_b', '-lead'])('rejects %j', (value) => { + expect(() => settingsNamespace(value)).toThrow(TypeError) + }) +}) + +describe('registration', () => { + it('resolves schema defaults, then composition base, then the user layer', async () => { + const { ctx } = await boot({ doc: { 'ui-theme': { theme: 'light' } } }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema, { + base: { fontSize: 16 }, + }) + // theme: user layer wins; fontSize: base wins over the schema default. + expect(scope.get()).toEqual({ theme: 'light', fontSize: 16 }) + }) + + it('rejects a duplicate namespace loud', async () => { + const { ctx } = await boot() + ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(() => ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema)) + .toThrow(/already registered/) + }) + + it('fails registration when the stored section is invalid for the schema', async () => { + const { ctx } = await boot({ doc: { 'ui-theme': { fontSize: 'big' } } }) + expect(() => ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema)).toThrow() + }) + + it('fails registration when the stored section is not an object', async () => { + const { ctx } = await boot({ doc: { 'ui-theme': 'dark' } }) + expect(() => ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema)) + .toThrow(/must be an object/) + }) + + it('describes registered namespaces with schema JSON, value, and applies', async () => { + const { ctx } = await boot() + ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + ctx.settings.register(settingsNamespace('workspace'), NestedSchema, { applies: 'restart' }) + const descriptors = ctx.settings.describe() + expect(descriptors.map(entry => [entry.ns, entry.applies])).toEqual([ + ['ui-theme', 'live'], + ['workspace', 'restart'], + ]) + expect(descriptors[0]!.value).toEqual({ theme: 'dark', fontSize: 14 }) + // schemastery's canonical wire form: a { uid, refs } envelope whose root ref + // is the object schema — the shape schema-driven form UIs reconstruct from. + const serialized = descriptors[0]!.schema as { uid: number; refs: Record } + expect(serialized.refs[String(serialized.uid)]?.type).toBe('object') + }) + + it('reads undefined for an unregistered namespace', async () => { + const { ctx } = await boot() + expect(ctx.settings.get(settingsNamespace('missing'))).toBeUndefined() + }) + + it('hands out frozen resolved values', async () => { + const { ctx } = await boot({ doc: { workspace: { retry: { attempts: 5 } } } }) + const scope = ctx.settings.register(settingsNamespace('workspace'), NestedSchema) + const value = scope.get() + expect(Object.isFrozen(value)).toBe(true) + expect(Object.isFrozen(value.retry)).toBe(true) + expect(() => { (value.retry as { attempts: number }).attempts = 0 }).toThrow(TypeError) + }) + + it('removes the namespace and its observers when the registrant fiber disposes', async () => { + const { ctx, provider } = await boot() + const seen: unknown[] = [] + let scope: SettingsScope | undefined + const fiber = ctx.plugin({ + inject: ['settings'], + apply: (child: Context) => { + scope = child.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + scope.watch(next => seen.push(next)) + }, + }) + await fiber + expect(ctx.settings.get(settingsNamespace('ui-theme'))).toEqual({ theme: 'dark', fontSize: 14 }) + + await fiber.dispose() + expect(ctx.settings.get(settingsNamespace('ui-theme'))).toBeUndefined() + expect(ctx.settings.describe()).toEqual([]) + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + expect(seen).toEqual([]) + + // The namespace is free again, and re-registration resolves the user layer + // that kept living in storage while nobody owned the namespace. + const again = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(again.get()).toEqual({ theme: 'light', fontSize: 14 }) + }) +}) + +describe('update', () => { + it('persists the merged user section without baking in the base layer', async () => { + const { ctx, provider } = await boot({ doc: { 'ui-theme': { theme: 'light' } } }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema, { + base: { fontSize: 16 }, + }) + await scope.update({ theme: 'dark' }) + expect(provider.persisted).toEqual([ + { ns: 'ui-theme', section: { theme: 'dark' } }, + ]) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 16 }) + }) + + it('deep-merges nested objects and replaces arrays wholesale', async () => { + const { ctx, provider } = await boot({ + doc: { workspace: { retry: { attempts: 5, delayMs: 300 }, tags: ['a', 'b'] } }, + }) + const scope = ctx.settings.register(settingsNamespace('workspace'), NestedSchema) + await scope.update({ retry: { attempts: 7 }, tags: ['c'] }) + expect(provider.persisted[0]!.section).toEqual({ + retry: { attempts: 7, delayMs: 300 }, + tags: ['c'], + }) + expect(scope.get()).toEqual({ retry: { attempts: 7, delayMs: 300 }, tags: ['c'] }) + }) + + it('commits, notifies watchers, and emits with source update', async () => { + const { ctx } = await boot() + const events = recordUpdates(ctx) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const watcher = vi.fn() + scope.watch(watcher) + await scope.update({ theme: 'light' }) + expect(watcher).toHaveBeenCalledWith( + { theme: 'light', fontSize: 14 }, + { theme: 'dark', fontSize: 14 }, + ) + expect(events).toEqual([{ + ns: 'ui-theme', + next: { theme: 'light', fontSize: 14 }, + prev: { theme: 'dark', fontSize: 14 }, + source: 'update', + }]) + }) + + it('rejects an invalid patch before persisting anything', async () => { + const { ctx, provider } = await boot() + const events = recordUpdates(ctx) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await expect(scope.update({ fontSize: 'big' })).rejects.toThrow() + expect(provider.persisted).toEqual([]) + expect(events).toEqual([]) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 14 }) + }) + + it('ignores explicit undefined entries so a sparse patch cannot erase keys', async () => { + const { ctx, provider } = await boot({ doc: { 'ui-theme': { theme: 'light' } } }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ theme: undefined, fontSize: 18 }) + expect(provider.persisted[0]!.section).toEqual({ theme: 'light', fontSize: 18 }) + expect(scope.get()).toEqual({ theme: 'light', fontSize: 18 }) + }) + + it('rejects a non-object patch', async () => { + const { ctx } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await expect(scope.update([1])).rejects.toThrow(TypeError) + await expect(scope.update(new Date() as unknown as object)).rejects.toThrow(TypeError) + }) + + it('accepts a null-prototype patch object', async () => { + const { ctx } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const patch: { fontSize?: number } = Object.create(null) as { fontSize?: number } + patch.fontSize = 18 + await scope.update(patch) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 18 }) + }) + + it('rejects an unregistered namespace', async () => { + const { ctx } = await boot() + await expect(ctx.settings.update(settingsNamespace('missing'), {})) + .rejects.toThrow(/not registered/) + }) + + it('rejects on a read-only provider before reaching persist', async () => { + const { ctx, provider } = await boot({ writable: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await expect(scope.update({ theme: 'light' })).rejects.toThrow(/read-only/) + expect(provider.persisted).toEqual([]) + }) +}) + +describe('publish', () => { + it('notifies watchers of an external change with source provider', async () => { + const { ctx, provider } = await boot() + const events = recordUpdates(ctx) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const watcher = vi.fn() + scope.watch(watcher) + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + expect(watcher).toHaveBeenCalledWith( + { theme: 'light', fontSize: 14 }, + { theme: 'dark', fontSize: 14 }, + ) + expect(events[0]!.source).toBe('provider') + }) + + it('stays silent when the resolved value is deep-equal', async () => { + const { ctx, provider } = await boot({ doc: { 'ui-theme': { theme: 'light' } } }) + const events = recordUpdates(ctx) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const watcher = vi.fn() + scope.watch(watcher) + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + expect(watcher).not.toHaveBeenCalled() + expect(events).toEqual([]) + }) + + it('keeps the last good value for an invalid section while other namespaces commit', async () => { + const { ctx, provider } = await boot() + const events = recordUpdates(ctx) + const theme = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const workspace = ctx.settings.register(settingsNamespace('workspace'), NestedSchema) + provider.pushExternal({ + 'ui-theme': { fontSize: 'broken' }, + workspace: { retry: { attempts: 9 } }, + }) + expect(theme.get()).toEqual({ theme: 'dark', fontSize: 14 }) + expect(workspace.get()).toEqual({ retry: { attempts: 9, delayMs: 100 }, tags: ['default'] }) + expect(events.map(event => event.ns)).toEqual(['workspace']) + }) + + it('recovers from a bad section once storage turns valid again', async () => { + const { ctx, provider } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + provider.pushExternal({ 'ui-theme': { fontSize: 'broken' } }) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 14 }) + provider.pushExternal({ 'ui-theme': { fontSize: 18 } }) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 18 }) + }) +}) + +describe('watch', () => { + it('stops after its disposer runs', async () => { + const { ctx, provider } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const watcher = vi.fn() + const dispose = scope.watch(watcher) + dispose() + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + expect(watcher).not.toHaveBeenCalled() + }) + + it('contains a throwing watcher without blocking the commit or other watchers', async () => { + const { ctx, provider } = await boot() + const events = recordUpdates(ctx) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + scope.watch(() => { throw new Error('watcher boom') }) + const second = vi.fn() + scope.watch(second) + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + expect(second).toHaveBeenCalledTimes(1) + expect(events).toHaveLength(1) + expect(scope.get()).toEqual({ theme: 'light', fontSize: 14 }) + }) +}) diff --git a/packages/settings/settings/tsconfig.json b/packages/settings/settings/tsconfig.json new file mode 100644 index 0000000000..dd18d27fc3 --- /dev/null +++ b/packages/settings/settings/tsconfig.json @@ -0,0 +1,27 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": [ + "src" + ], + "references": [ + { + "path": "../../../vendor/cosmokit" + }, + { + "path": "../../../vendor/cordis" + }, + { + "path": "../../../vendor/schemastery" + }, + { + "path": "../../util/brand" + }, + { + "path": "../../support/invariants" + } + ] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b9538051cd..fce89446a6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3636,6 +3636,46 @@ importers: specifier: ^4.0.0-rc.7 version: 4.0.0-rc.7(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.5) + packages/settings/settings: + devDependencies: + '@deepseek-ai/dsh-brand': + specifier: workspace:^ + version: link:../../util/brand + '@deepseek-ai/dsh-invariants': + specifier: workspace:^ + version: link:../../support/invariants + cordis: + specifier: ^4.0.0-rc.7 + version: 4.0.0-rc.7(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.5) + schemastery: + specifier: ^3.18.0 + version: 3.18.0 + + packages/settings/settings-local: + dependencies: + chokidar: + specifier: ^4.0.3 + version: 4.0.3 + schemastery: + specifier: ^3.18.0 + version: 3.18.0 + yaml: + specifier: ^2.9.0 + version: 2.9.0 + devDependencies: + '@deepseek-ai/dsh-invariants': + specifier: workspace:^ + version: link:../../support/invariants + '@deepseek-ai/dsh-paths': + specifier: workspace:^ + version: link:../../util/paths + '@deepseek-ai/dsh-settings': + specifier: workspace:^ + version: link:../settings + cordis: + specifier: ^4.0.0-rc.7 + version: 4.0.0-rc.7(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.5) + packages/skill/skill: dependencies: schemastery: diff --git a/scripts/doc-budgets.manifest.json b/scripts/doc-budgets.manifest.json index 4cae854147..a27d6af22b 100644 --- a/scripts/doc-budgets.manifest.json +++ b/scripts/doc-budgets.manifest.json @@ -1,5 +1,5 @@ { - "AGENTS.md": 1705, + "AGENTS.md": 1710, "docs/AGENTS.md": 1150, "docs/architecture.md": 1800, "docs/cordis-primer.md": 600, @@ -7,5 +7,5 @@ "docs/testing.md": 1100, "examples/AGENTS.md": 310, "packages/AGENTS.md": 675, - "packages/README.md": 835 + "packages/README.md": 845 } diff --git a/scripts/gen-cordis-catalog.ts b/scripts/gen-cordis-catalog.ts index b10ad9dfac..b7df3dc622 100644 --- a/scripts/gen-cordis-catalog.ts +++ b/scripts/gen-cordis-catalog.ts @@ -217,6 +217,12 @@ const FOUNDATION_TYPE_NAMES = new Set([ /** Project types deliberately documented outside the core-data catalog. */ const TYPE_LINK_EXEMPTIONS: Readonly> = { AgentFactory: 'agent creation seam is owned by packages/core/agent/README.md', + SettingsNamespace: 'settings seam vocabulary is owned by packages/settings/settings/README.md', + SettingsUpdateSource: 'settings seam vocabulary is owned by packages/settings/settings/README.md', + SettingsRegisterOptions: 'settings seam vocabulary is owned by packages/settings/settings/README.md', + SettingsScope: 'settings seam vocabulary is owned by packages/settings/settings/README.md', + SettingsDescriptor: 'settings seam vocabulary is owned by packages/settings/settings/README.md', + z: 'schemastery schema constructor is owned by vendor/schemastery (vendored upstream)', BeginCommandRequest: 'event-local request contract is owned by packages/client/ui-slash/src/types.ts', InsertReferenceRequest: 'event-local request contract is owned by packages/client/ui-slash/src/types.ts', ConsumeTokenRequest: 'event-local request contract is owned by packages/client/ui-slash/src/types.ts', diff --git a/scripts/gen-doc-graphs.ts b/scripts/gen-doc-graphs.ts index 1d4a7e447d..a077042d3c 100644 --- a/scripts/gen-doc-graphs.ts +++ b/scripts/gen-doc-graphs.ts @@ -137,6 +137,15 @@ const SERVICE_ROLES: ServiceRole[] = [ consumers: ['agent-loop', 'tool-bash', 'hooks-claude', 'hooks-codex', 'session-query', 'session-query-sqlite'], note: 'Backends persist the same SessionEvent vocabulary; apps choose a backend at composition time.', }, + { + key: 'settings', + pkg: 'settings', + title: 'User-settings seam', + mode: 'seam', + implementations: ['settings-local'], + consumers: [], + note: 'Plugins register namespace schemas and resolve layered values; providers store the raw document. No production consumer is migrated yet.', + }, { key: 'telemetry', pkg: 'session-telemetry', diff --git a/scripts/verify-package-readme-model-experience.ts b/scripts/verify-package-readme-model-experience.ts index 13dc2def8d..dc02127a17 100644 --- a/scripts/verify-package-readme-model-experience.ts +++ b/scripts/verify-package-readme-model-experience.ts @@ -90,6 +90,8 @@ const SENTENCE_MODEL_EXPERIENCE: Readonly> = { 'packages/sdk/telemetry': { kind: 'none', reason: 'The launcher-side reporter sends developer-cycle telemetry and registers no live agent or model surface.' }, 'packages/session-query/session-query': { kind: 'none', reason: 'The trusted query service exposes cloned records only to callers and registers no model surface.' }, 'packages/session-query/session-query-sqlite': { kind: 'none', reason: 'The search backend returns hits only to callers and registers no model surface.' }, + 'packages/settings/settings': { kind: 'indirect', reason: 'The seam stores and resolves user settings; consumer plugins own any model surface a value feeds.' }, + 'packages/settings/settings-local': { kind: 'indirect', reason: 'The file provider stores and publishes namespace sections; consumers of ctx.settings own any model surface.' }, 'packages/telemetry/session-telemetry': { kind: 'none', reason: 'The seam observes the session stream and hands redacted copies outward; it registers no model surface.' }, 'packages/telemetry/session-telemetry-otel': { kind: 'none', reason: 'The backend forwards seam records into the OTel SDK pipeline and registers no model surface.' }, 'packages/skill/skill': { kind: 'indirect', reason: 'The provider registry delegates model rendering to dsh-tool-skill.' }, diff --git a/tsconfig.base.json b/tsconfig.base.json index d4beec7afa..8f39d3ec5f 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -75,6 +75,7 @@ "./packages/hooks/*/src/invariant.ts", "./packages/session-persistence/*/src/invariant.ts", "./packages/session-query/*/src/invariant.ts", + "./packages/settings/*/src/invariant.ts", "./packages/telemetry/*/src/invariant.ts", "./packages/acp/*/src/invariant.ts", "./packages/storage/*/src/invariant.ts", @@ -156,6 +157,7 @@ "./packages/session-persistence/*/src", "./packages/session-query/*/src", "./packages/session-title/*/src", + "./packages/settings/*/src", "./packages/telemetry/*/src", "./packages/acp/*/src", "./packages/storage/*/src", diff --git a/tsconfig.host.json b/tsconfig.host.json index 9cf2a86bda..2c9bc2a652 100644 --- a/tsconfig.host.json +++ b/tsconfig.host.json @@ -58,6 +58,8 @@ { "path": "./packages/session-persistence/session-persistence-sqlite" }, { "path": "./packages/session-query/session-query" }, { "path": "./packages/session-query/session-query-sqlite" }, + { "path": "./packages/settings/settings" }, + { "path": "./packages/settings/settings-local" }, { "path": "./packages/session-query/tool-session-query" }, { "path": "./packages/storage/storage" }, { "path": "./packages/storage/storage-json" }, From f44b4db1f22b44b5d280065ad0bd8170d36b8d1a Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Tue, 28 Jul 2026 18:18:34 +0800 Subject: [PATCH 02/17] fix(settings): harden seam and provider per review findings Confirmed and fixed, each with a regression test that failed first: - Concurrent update() lost patches (merge over one stale snapshot): per-namespace serialized write queues; a failed write cannot poison the queue for later writers. - Fixed-name .tmp write followed planted symlinks and kept stale modes: random-suffix sibling, exclusive-create (wx), 0600, cleanup on failure, then rename. - A throwing settings/updated listener escaped commit and permanently wedged the provider reload chain (rejected refreshTask): commit now contains listener failures (INVARIANT-coded errors still propagate), async watcher rejections are adopted and contained (watch callbacks are officially void | Promise), and the provider chains refreshes on a settled tail with an error log. - No way to remove a user override: scope/service replace(section) sets the user section wholesale; replace({}) re-inherits base and schema defaults. - The three-primitive provider contract did not hold (base never called load()): the base Service.init loads and publishes once; settings-local delegates via yield* super[Service.init](). - Dispose did not quiesce: teardown flags closed, closes the watcher, then awaits queued/in-flight reloads; closed is re-checked across await points. - Invariant now checks the authoritative relation with the seam's own deepEqualJson: emitted next must equal settings.get(ns), and next/prev must differ structurally (cosmokit dependency dropped). - New docs/core-data-structures/settings.{md,zh.md} with type-equiv blocks + manifest entries; catalog types moved from exemptions to LINK_MAP; website page registered. Both packages stay at per-file 100% coverage. --- docs/config-catalog.md | 2 +- docs/cordis-catalog/events.md | 4 +- docs/cordis-catalog/services.md | 18 ++- docs/core-data-structures/settings.i18n.yaml | 6 + docs/core-data-structures/settings.md | 94 +++++++++++++ docs/core-data-structures/settings.zh.md | 94 +++++++++++++ docs/event-producer-consumer.md | 2 +- .../cordis/tool-cordis/src/api-catalog.ts | 8 +- .../settings/settings-local/README.i18n.yaml | 4 +- packages/settings/settings-local/README.md | 3 +- packages/settings/settings-local/README.zh.md | 3 +- packages/settings/settings-local/src/index.ts | 58 +++++++-- .../tests/loader-composition.spec.ts | 2 +- .../settings-local/tests/local.spec.ts | 33 ++++- .../settings-local/tests/watcher.spec.ts | 54 ++++++++ packages/settings/settings/README.i18n.yaml | 4 +- packages/settings/settings/README.md | 9 +- packages/settings/settings/README.zh.md | 9 +- packages/settings/settings/src/index.ts | 123 +++++++++++++++--- packages/settings/settings/src/invariant.ts | 13 +- .../settings/settings/tests/invariant.spec.ts | 11 ++ packages/settings/settings/tests/memory.ts | 17 +-- .../settings/settings/tests/settings.spec.ts | 114 +++++++++++++++- scripts/gen-cordis-catalog.ts | 10 +- scripts/project-doc-site.spec.ts | 2 +- scripts/type-equiv.manifest.json | 30 +++++ website/docs.ts | 1 + 27 files changed, 655 insertions(+), 73 deletions(-) create mode 100644 docs/core-data-structures/settings.i18n.yaml create mode 100644 docs/core-data-structures/settings.md create mode 100644 docs/core-data-structures/settings.zh.md diff --git a/docs/config-catalog.md b/docs/config-catalog.md index 1305dd7270..2a1eb47da5 100644 --- a/docs/config-catalog.md +++ b/docs/config-catalog.md @@ -1193,7 +1193,7 @@ export interface Config { } ``` -Source: [`packages/settings/settings-local/src/index.ts:18`](../packages/settings/settings-local/src/index.ts) +Source: [`packages/settings/settings-local/src/index.ts:19`](../packages/settings/settings-local/src/index.ts) ## `@deepseek-ai/dsh-skill` diff --git a/docs/cordis-catalog/events.md b/docs/cordis-catalog/events.md index aea04f4acd..74e1e6cfda 100644 --- a/docs/cordis-catalog/events.md +++ b/docs/cordis-catalog/events.md @@ -660,7 +660,9 @@ Committed change to one registered namespace's resolved value. Emitted after the 'settings/updated'(ns: SettingsNamespace, next: unknown, prev: unknown, source: SettingsUpdateSource): void ``` -Source: [`packages/settings/settings/src/index.ts:90`](../../packages/settings/settings/src/index.ts) +Types: [SettingsNamespace](../core-data-structures/settings.md) · [SettingsUpdateSource](../core-data-structures/settings.md) + +Source: [`packages/settings/settings/src/index.ts:96`](../../packages/settings/settings/src/index.ts) ## `slash/*` diff --git a/docs/cordis-catalog/services.md b/docs/cordis-catalog/services.md index a0271083d6..e915952ffb 100644 --- a/docs/cordis-catalog/services.md +++ b/docs/cordis-catalog/services.md @@ -1421,14 +1421,28 @@ get(ns: SettingsNamespace): unknown /** * Merge a patch into one registered namespace's user layer, validate the * resolved candidate, persist through the provider, then commit and emit. - * A validation failure rejects before anything is persisted. + * A validation failure rejects before anything is persisted. Writes to one + * namespace are serialized: concurrent updates apply in call order, each + * merging over the previous write's committed section. * @param ns - the registered namespace to update. * @param patch - plain-object patch over the user section. */ async update(ns: SettingsNamespace, patch: object): Promise + +/** + * Replace one registered namespace's user section wholesale, validate, + * persist, then commit and emit. Keys absent from `section` fall back to the + * composition `base` and schema defaults — this is the removal/reset path a + * merge-only patch cannot express (`replace({})` re-inherits everything). + * @param ns - the registered namespace to replace. + * @param section - the complete next user section. + */ +async replace(ns: SettingsNamespace, section: object): Promise ``` -Source: [`packages/settings/settings/src/index.ts:140`](../../packages/settings/settings/src/index.ts) +Types: [SettingsDescriptor](../core-data-structures/settings.md) · [SettingsNamespace](../core-data-structures/settings.md) · [SettingsRegisterOptions](../core-data-structures/settings.md) · [SettingsScope](../core-data-structures/settings.md) + +Source: [`packages/settings/settings/src/index.ts:168`](../../packages/settings/settings/src/index.ts) ## `ctx.skills` — `SkillService` diff --git a/docs/core-data-structures/settings.i18n.yaml b/docs/core-data-structures/settings.i18n.yaml new file mode 100644 index 0000000000..6dc8750dfb --- /dev/null +++ b/docs/core-data-structures/settings.i18n.yaml @@ -0,0 +1,6 @@ +# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each +# side as of the last confirmed-consistent state. Both languages carry equal authority; +# after editing either side, bring the other along and re-record with: +# pnpm run verify-translation-pairing --write docs/core-data-structures/settings.md +settings.md: 851087065c627f2390041dd6e69273e31be3917d +settings.zh.md: 955c4fbf7c147b3d0a8be62c33c031d7bd2c4ba2 diff --git a/docs/core-data-structures/settings.md b/docs/core-data-structures/settings.md new file mode 100644 index 0000000000..851087065c --- /dev/null +++ b/docs/core-data-structures/settings.md @@ -0,0 +1,94 @@ +# User Settings + +English | [中文](settings.zh.md) + +The user-settings seam of [dsh-settings](../../packages/settings/settings) holds one user-owned document of per-namespace sections and resolves each registered namespace as schema defaults, then the registrant's composition `base`, then the user section. Providers such as [dsh-settings-local](../../packages/settings/settings-local) store the raw document and push external edits; consumer plugins register a schema and read or observe the resolved value. Composition config stays in `cordis.yml` — a namespace carries only the user-editable subset. + +Source: [`packages/settings/settings/src/index.ts`](../../packages/settings/settings/src/index.ts) + +## Identity + +A namespace names one plugin-owned section of the user document. The brand keeps namespaces from mixing with other cross-boundary ids; construction validates the lowercase kebab-case shape. + +```ts type-equiv +/** Nominal id of one registered settings namespace. */ +type SettingsNamespace = Branded<'SettingsNamespace'> +``` + +## Registration + +Registration binds a schemastery schema to a namespace on the calling plugin's fiber — disposing that fiber removes the namespace and its observers. The options carry the composition layer and the owner's effect timing. + +```ts type-equiv +/** Registration options beyond the namespace schema. */ +interface SettingsRegisterOptions { + /** Composition-layer values resolved below the user layer (entry-config subset). */ + base?: Partial + /** Owner's effect timing, surfaced to configuration UIs; defaults to `live`. */ + applies?: SettingsApplies +} +``` + +`applies` is a UI hint, not a mechanism: a `restart` owner simply never watches, so its value is read once at construction and configuration surfaces can badge the pending change. + +```ts type-equiv +/** When a namespace's changes take effect for its owner. */ +type SettingsApplies = 'live' | 'restart' +``` + +## Owner scope + +The scope is the owner-facing handle. `update` merges a sparse patch over the user section only (never into `base`); `replace` sets the section wholesale, which is the removal/reset path — keys absent from the replacement re-inherit `base` and schema defaults. Writes to one namespace are serialized in call order, and resolved values are deep-frozen snapshots. + +```ts type-equiv +/** Owner-facing handle for one registered namespace. */ +interface SettingsScope { + /** Current resolved value: schema defaults, then `base`, then the user layer. */ + get(): T + /** + * Observe committed changes to this namespace's resolved value. A callback + * may be async; a rejection is contained and logged like a sync throw. + * @param callback - invoked after each commit with the next and previous values. + * @returns the disposer removing this observer. + */ + watch(callback: (next: T, prev: T) => void | Promise): () => void + /** + * Merge a partial patch into this namespace's user layer and persist it. + * @param patch - plain-object patch over the user section. + */ + update(patch: object): Promise + /** + * Replace this namespace's user section wholesale; absent keys re-inherit + * the composition `base` and schema defaults (`replace({})` resets all). + * @param section - the complete next user section. + */ + replace(section: object): Promise +} +``` + +## Descriptors + +`describe()` serializes every registered namespace for configuration surfaces: the schemastery `toJSON()` envelope drives schema-rendered forms, and the resolved value fills them. + +```ts type-equiv +/** One registered namespace as surfaced to configuration UIs. */ +interface SettingsDescriptor { + /** The registered namespace. */ + ns: SettingsNamespace + /** Serialized schemastery schema (`schema.toJSON()`). */ + schema: unknown + /** Current resolved value. */ + value: unknown + /** Owner's declared effect timing. */ + applies: SettingsApplies +} +``` + +## Change commits + +Every committed change — an in-process write or an externally observed provider edit — emits `settings/updated (ns, next, prev, source)` after the new value is authoritative, and never when the resolved value is deep-equal. The source tag separates the two entry paths. + +```ts type-equiv +/** Origin of one committed settings change. */ +type SettingsUpdateSource = 'update' | 'provider' +``` diff --git a/docs/core-data-structures/settings.zh.md b/docs/core-data-structures/settings.zh.md new file mode 100644 index 0000000000..955c4fbf7c --- /dev/null +++ b/docs/core-data-structures/settings.zh.md @@ -0,0 +1,94 @@ +# 用户设置 + +[English](settings.md) | 中文 + +[dsh-settings](../../packages/settings/settings) 的用户设置 seam 持有一份按 namespace 分节的用户文档,并把每个已注册 namespace 解析为:schema 默认值,然后注册方的组合 `base`,最后用户分节。[dsh-settings-local](../../packages/settings/settings-local) 这类 provider 存储原始文档并推送外部编辑;消费插件注册 schema 后读取或观察解析值。组合配置仍留在 `cordis.yml`——namespace 只承载用户可编辑子集。 + +Source: [`packages/settings/settings/src/index.ts`](../../packages/settings/settings/src/index.ts) + +## 标识 + +namespace 命名用户文档中一个插件所有的分节。brand 使其不与其他跨边界 id 混用;构造时校验小写 kebab-case 形态。 + +```ts type-equiv +/** Nominal id of one registered settings namespace. */ +type SettingsNamespace = Branded<'SettingsNamespace'> +``` + +## 注册 + +注册把 schemastery schema 绑定到调用方插件 fiber 上的 namespace——dispose 该 fiber 即移除 namespace 及其观察者。options 携带组合层与 owner 的生效时机。 + +```ts type-equiv +/** Registration options beyond the namespace schema. */ +interface SettingsRegisterOptions { + /** Composition-layer values resolved below the user layer (entry-config subset). */ + base?: Partial + /** Owner's effect timing, surfaced to configuration UIs; defaults to `live`. */ + applies?: SettingsApplies +} +``` + +`applies` 是 UI 提示而非机制:`restart` 的 owner 只是从不 watch,其值在构造期读取一次,配置界面可为待生效变更加标。 + +```ts type-equiv +/** When a namespace's changes take effect for its owner. */ +type SettingsApplies = 'live' | 'restart' +``` + +## Owner scope + +scope 是面向 owner 的句柄。`update` 把稀疏 patch 只合并进用户分节(绝不进 `base`);`replace` 整体替换分节,是删除/重置路径——替换中缺席的键重新继承 `base` 与 schema 默认值。同一 namespace 的写入按调用顺序串行,解析值是深冻结快照。 + +```ts type-equiv +/** Owner-facing handle for one registered namespace. */ +interface SettingsScope { + /** Current resolved value: schema defaults, then `base`, then the user layer. */ + get(): T + /** + * Observe committed changes to this namespace's resolved value. A callback + * may be async; a rejection is contained and logged like a sync throw. + * @param callback - invoked after each commit with the next and previous values. + * @returns the disposer removing this observer. + */ + watch(callback: (next: T, prev: T) => void | Promise): () => void + /** + * Merge a partial patch into this namespace's user layer and persist it. + * @param patch - plain-object patch over the user section. + */ + update(patch: object): Promise + /** + * Replace this namespace's user section wholesale; absent keys re-inherit + * the composition `base` and schema defaults (`replace({})` resets all). + * @param section - the complete next user section. + */ + replace(section: object): Promise +} +``` + +## 描述符 + +`describe()` 为配置界面序列化每个已注册 namespace:schemastery 的 `toJSON()` 信封驱动 schema 渲染的表单,解析值填充表单。 + +```ts type-equiv +/** One registered namespace as surfaced to configuration UIs. */ +interface SettingsDescriptor { + /** The registered namespace. */ + ns: SettingsNamespace + /** Serialized schemastery schema (`schema.toJSON()`). */ + schema: unknown + /** Current resolved value. */ + value: unknown + /** Owner's declared effect timing. */ + applies: SettingsApplies +} +``` + +## 变更提交 + +每次提交的变更——进程内写入或 provider 观察到的外部编辑——在新值成为权威值之后发出 `settings/updated (ns, next, prev, source)`,解析值深相等时绝不发出。source 标记区分两条入口路径。 + +```ts type-equiv +/** Origin of one committed settings change. */ +type SettingsUpdateSource = 'update' | 'provider' +``` diff --git a/docs/event-producer-consumer.md b/docs/event-producer-consumer.md index 49bdf3fe35..e3eb1d9435 100644 --- a/docs/event-producer-consumer.md +++ b/docs/event-producer-consumer.md @@ -35,7 +35,7 @@ This matrix shows which packages dispatch each harness-owned event and which pac | `session/disposed` | `emit` | [`packages/core/session/src/index.ts:80`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`agent-loop`](../packages/core/agent-loop), `apiproxy`, [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title) | | `session/event` | `emit` | [`packages/core/session/src/index.ts:92`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`acp`](../packages/acp/acp), `apiproxy`, [`cli-demo`](../packages/examples/cli-demo), [`compact`](../packages/compact/compact), [`compact-basic`](../packages/compact/compact-basic), [`goal`](../packages/goal/goal), [`goal-session`](../packages/goal/goal-session), [`hook-protocol`](../packages/hooks/hook-protocol), [`jsonrpc`](../packages/ui/jsonrpc), [`plan-mode`](../packages/plan/plan-mode), [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title), [`token-meter`](../packages/llm/token-meter), [`tools`](../packages/core/tools), [`tui`](../packages/ui/tui), [`user-approval`](../packages/ui/user-approval), [`workspace-context`](../packages/context/workspace-context) | | `session/flush` | `parallel` | [`packages/core/session/src/index.ts:102`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry) | -| `settings/updated` | `emit` | [`packages/settings/settings/src/index.ts:90`](../packages/settings/settings/src/index.ts) | [`settings`](../packages/settings/settings) (`emit`) | [`settings`](../packages/settings/settings) | +| `settings/updated` | `emit` | [`packages/settings/settings/src/index.ts:96`](../packages/settings/settings/src/index.ts) | [`settings`](../packages/settings/settings) (`emit`) | [`settings`](../packages/settings/settings) | | `slash/input-begin-command` | `bail` | [`packages/client/ui-slash/src/types.ts:230`](../packages/client/ui-slash/src/types.ts) | - | `ui-conversation` | | `slash/input-consume-token` | `bail` | [`packages/client/ui-slash/src/types.ts:244`](../packages/client/ui-slash/src/types.ts) | - | `ui-conversation` | | `slash/input-insert-reference` | `bail` | [`packages/client/ui-slash/src/types.ts:237`](../packages/client/ui-slash/src/types.ts) | - | `ui-conversation` | diff --git a/packages/cordis/tool-cordis/src/api-catalog.ts b/packages/cordis/tool-cordis/src/api-catalog.ts index 81e3c79d05..4190a94a90 100644 --- a/packages/cordis/tool-cordis/src/api-catalog.ts +++ b/packages/cordis/tool-cordis/src/api-catalog.ts @@ -684,7 +684,11 @@ export const SERVICE_API: readonly ServiceApiEntry[] = [ }, { signature: 'async update(ns: SettingsNamespace, patch: object): Promise', - jsDoc: '/**\n * Merge a patch into one registered namespace\'s user layer, validate the\n * resolved candidate, persist through the provider, then commit and emit.\n * A validation failure rejects before anything is persisted.\n * @param ns - the registered namespace to update.\n * @param patch - plain-object patch over the user section.\n */', + jsDoc: '/**\n * Merge a patch into one registered namespace\'s user layer, validate the\n * resolved candidate, persist through the provider, then commit and emit.\n * A validation failure rejects before anything is persisted. Writes to one\n * namespace are serialized: concurrent updates apply in call order, each\n * merging over the previous write\'s committed section.\n * @param ns - the registered namespace to update.\n * @param patch - plain-object patch over the user section.\n */', + }, + { + signature: 'async replace(ns: SettingsNamespace, section: object): Promise', + jsDoc: '/**\n * Replace one registered namespace\'s user section wholesale, validate,\n * persist, then commit and emit. Keys absent from `section` fall back to the\n * composition `base` and schema defaults — this is the removal/reset path a\n * merge-only patch cannot express (`replace({})` re-inherits everything).\n * @param ns - the registered namespace to replace.\n * @param section - the complete next user section.\n */', }, ], }, @@ -2228,7 +2232,7 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'SettingsScope', - declaration: 'export interface SettingsScope {\n get(): T;\n watch(callback: (next: T, prev: T) => void): () => void;\n update(patch: object): Promise;\n}', + declaration: 'export interface SettingsScope {\n get(): T;\n watch(callback: (next: T, prev: T) => void | Promise): () => void;\n update(patch: object): Promise;\n replace(section: object): Promise;\n}', }, { name: 'SkillCandidate', diff --git a/packages/settings/settings-local/README.i18n.yaml b/packages/settings/settings-local/README.i18n.yaml index f7ca57e86e..455255941a 100644 --- a/packages/settings/settings-local/README.i18n.yaml +++ b/packages/settings/settings-local/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/settings/settings-local/README.md -README.md: 90428f98055d8e49fa1ec54e571453db2e0a5054 -README.zh.md: 3532d6cee99cb46f54e23889ef1bb54b2548ecfa +README.md: 9d0aa3982507ca16b382aaa918ca1536a98e29ea +README.zh.md: 075de7ee5b3e0ef0a4ee27eeb8cb098ca0d73456 diff --git a/packages/settings/settings-local/README.md b/packages/settings/settings-local/README.md index 90428f9805..9d0aa39825 100644 --- a/packages/settings/settings-local/README.md +++ b/packages/settings/settings-local/README.md @@ -18,7 +18,8 @@ Defaulting is one explicit `resolveSpec(config)` step; an unsupported extension ## Behavior - **Boot fails loud, reload keeps last-good.** An existing-but-invalid document fails plugin load; once live, an unreadable or unparsable edit warns and keeps the last good sections. A missing document resolves every namespace from defaults and `base`; deleting it publishes the same empty state. -- **Write-back is atomic and owner-only.** `persist` writes `.tmp` with mode `0600` and renames over the target. YAML writes patch one namespace in the comment-preserving document; JSON re-serializes. +- **Write-back is atomic, owner-only, and symlink-proof.** `persist` exclusive-creates a random-suffix temp sibling with mode `0600` (`wx` refuses to follow a planted symlink) and renames over the target, cleaning the temp up on failure. YAML writes patch one namespace in the comment-preserving document; JSON re-serializes. +- **Dispose quiesces.** Teardown stops accepting watcher events, closes the watcher, then waits out any queued or in-flight reload, so nothing publishes after disposal. - **Self-write suppression by content.** The provider caches the last good text; a watcher event whose content equals the cache (its own write included) is a no-op. ## Model Experience diff --git a/packages/settings/settings-local/README.zh.md b/packages/settings/settings-local/README.zh.md index 3532d6cee9..075de7ee5b 100644 --- a/packages/settings/settings-local/README.zh.md +++ b/packages/settings/settings-local/README.zh.md @@ -18,7 +18,8 @@ ## 行为 - **启动报错响亮,重载保留最后可用值。** 存在但非法的文档使插件加载失败;运行中不可读或不可解析的编辑只告警并保留最后可用分节。文档缺失时所有 namespace 按默认值与 `base` 解析;删除文档发布同样的空状态。 -- **写回原子且仅属主可读。** `persist` 以 `0600` 权限写 `.tmp` 后 rename 覆盖目标。YAML 写回在保留注释的文档里只修补目标 namespace;JSON 重新序列化。 +- **写回原子、仅属主可读、抗符号链接。** `persist` 以 `0600` 权限独占创建随机后缀临时同级文件(`wx` 拒绝跟随预埋符号链接)后 rename 覆盖目标,失败时清理临时文件。YAML 写回在保留注释的文档里只修补目标 namespace;JSON 重新序列化。 +- **Dispose 保证静止。** 卸载先停止接收 watcher 事件、关闭 watcher,再等完排队与进行中的重载,之后不再有任何发布。 - **按内容抑制自写。** provider 缓存最后可用文本;watcher 事件内容与缓存相同(含自己的写入)即为 no-op。 ## Model Experience diff --git a/packages/settings/settings-local/src/index.ts b/packages/settings/settings-local/src/index.ts index 421411ea50..2c020da63c 100644 --- a/packages/settings/settings-local/src/index.ts +++ b/packages/settings/settings-local/src/index.ts @@ -8,7 +8,8 @@ import { Context, Service } from 'cordis' import z from 'schemastery' import { watch as chokidarWatch } from 'chokidar' -import { mkdir, readFile, rename, writeFile } from 'node:fs/promises' +import { randomBytes } from 'node:crypto' +import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises' import { dirname, extname, join, resolve } from 'node:path' import { Document, parseDocument } from 'yaml' import { resolveDshHome } from '@deepseek-ai/dsh-paths' @@ -86,6 +87,13 @@ export class SettingsLocal extends Settings { private text: string | undefined /** Serializes watcher-triggered reloads so reads never interleave. */ private refreshTask: Promise = Promise.resolve() + /** Set at dispose: refuse new watcher events and let in-flight work no-op. */ + private closed = false + + /** Opaque read of {@link closed}: control flow cannot narrow it across awaits. */ + private isClosed(): boolean { + return this.closed + } constructor(ctx: Context, public config: Config) { super(ctx) @@ -118,18 +126,26 @@ export class SettingsLocal extends Settings { ? this.renderYaml(ns, section) : this.renderJson(ns, section) await mkdir(dirname(this.spec.filename), { recursive: true }) - const temp = `${this.spec.filename}.tmp` - // Owner-only permissions apply to the temp file and survive the rename, so - // a document that may carry personal values is never world-readable. - await writeFile(temp, output, { mode: 0o600 }) - await rename(temp, this.spec.filename) + // Exclusive-create (`wx`) a random-suffix sibling: the open refuses to + // follow any planted symlink at a guessable temp path, and the fresh inode + // carries owner-only permissions that survive the rename — a document that + // may hold personal values is never world-readable and never a symlink. + const temp = `${this.spec.filename}.${randomBytes(6).toString('hex')}.tmp` + try { + await writeFile(temp, output, { mode: 0o600, flag: 'wx' }) + await rename(temp, this.spec.filename) + } catch (error) { + await rm(temp, { force: true }) + throw error + } this.text = output } - async* [Service.init](): AsyncGenerator<() => Promise | void, void, void> { - // A parse failure here is a boot failure: an existing-but-invalid document - // must fail loud, never be silently ignored or overwritten. - this.publish(await this.load()) + override async* [Service.init](): AsyncGenerator<() => Promise | void, void, void> { + // The base init loads and publishes; a parse failure there is a boot + // failure: an existing-but-invalid document must fail loud, never be + // silently ignored or overwritten. + yield* super[Service.init]() if (!this.spec.watch) return const watcher = chokidarWatch(this.spec.filename, { ignoreInitial: true, @@ -139,13 +155,26 @@ export class SettingsLocal extends Settings { }, }) watcher.on('all', () => { - this.refreshTask = this.refreshTask.then(() => this.refresh()) + if (this.closed) return + this.refreshTask = this.refreshTask.then(() => this.refresh()).catch((error: unknown) => { + // Only an invariant violation escaping the commit path can reject a + // refresh; keep the reload queue alive and surface it as an error so + // one poisoned commit cannot silently end hot reloading forever. + this.ctx.logger.error('settings-local: reload commit failed at %s', this.spec.filename) + this.ctx.logger.error(error) + }) }) watcher.on('error', (error) => { this.ctx.logger.warn('settings-local: watcher error on %s', this.spec.filename) this.ctx.logger.warn(error) }) - yield () => watcher.close() + yield async () => { + // Quiesce: stop accepting events, close the watcher, then wait out any + // queued or in-flight refresh so nothing publishes after disposal. + this.closed = true + await watcher.close() + await this.refreshTask + } } /** Parse one document text into raw sections, failing on a non-map root. */ @@ -174,6 +203,7 @@ export class SettingsLocal extends Settings { * never take the process down. */ private async refresh(): Promise { + if (this.closed) return let text: string try { text = await readFile(this.spec.filename, 'utf8') @@ -183,12 +213,12 @@ export class SettingsLocal extends Settings { this.ctx.logger.warn(error) return } - if (this.text === undefined) return + if (this.text === undefined || this.isClosed()) return this.text = undefined this.publish({}) return } - if (text === this.text) return + if (text === this.text || this.isClosed()) return let doc: Record try { doc = this.parse(text) diff --git a/packages/settings/settings-local/tests/loader-composition.spec.ts b/packages/settings/settings-local/tests/loader-composition.spec.ts index 5744e92f46..d584c11899 100644 --- a/packages/settings/settings-local/tests/loader-composition.spec.ts +++ b/packages/settings/settings-local/tests/loader-composition.spec.ts @@ -55,7 +55,7 @@ async function loadComposition(): Promise<{ ctx: Context; state: ConsumerState; base: { fontSize: 16 }, }) state.scope = scope - scope.watch(next => state.seen.push(next)) + scope.watch((next) => { state.seen.push(next) }) }, } diff --git a/packages/settings/settings-local/tests/local.spec.ts b/packages/settings/settings-local/tests/local.spec.ts index 9c9d472bac..1c74429153 100644 --- a/packages/settings/settings-local/tests/local.spec.ts +++ b/packages/settings/settings-local/tests/local.spec.ts @@ -1,7 +1,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import { Context } from 'cordis' import z from 'schemastery' -import { chmod, mkdtemp, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises' +import { chmod, lstat, mkdtemp, readFile, readdir, rm, stat, symlink, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { settingsNamespace } from '@deepseek-ai/dsh-settings' @@ -146,6 +146,23 @@ describe('persist', () => { expect((await readdir(dir)).sort()).toEqual(['settings.yaml']) }) + it('never follows a planted symlink at a temp path and never leaves the document a symlink', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const victim = join(dir, 'victim.txt') + await writeFile(victim, 'precious') + // A hostile sibling plants the historic fixed temp name as a symlink. + await symlink(victim, `${path}.tmp`) + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ theme: 'light' }) + + expect(await readFile(victim, 'utf8')).toBe('precious') + expect((await lstat(path)).isSymbolicLink()).toBe(false) + expect((await stat(path)).mode & 0o777).toBe(0o600) + expect(await readFile(path, 'utf8')).toContain('theme: light') + }) + it('preserves comments and unregistered sections across updates', async () => { const dir = await tempDir() const path = join(dir, 'settings.yaml') @@ -180,6 +197,20 @@ describe('persist', () => { expect(written).toEqual({ 'ui-theme': { theme: 'light' } }) }) + it('rejects and leaves no temp residue when the directory turns unwritable', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await chmod(dir, 0o500) + cleanups.push(() => chmod(dir, 0o700)) + await expect(scope.update({ theme: 'dark' })).rejects.toThrow() + await chmod(dir, 0o700) + expect((await readdir(dir)).sort()).toEqual(['settings.yaml']) + expect(scope.get().theme).toBe('light') + }) + it('round-trips a json document', async () => { const dir = await tempDir() const path = join(dir, 'settings.json') diff --git a/packages/settings/settings-local/tests/watcher.spec.ts b/packages/settings/settings-local/tests/watcher.spec.ts index 00c67eacc7..439f213473 100644 --- a/packages/settings/settings-local/tests/watcher.spec.ts +++ b/packages/settings/settings-local/tests/watcher.spec.ts @@ -105,6 +105,60 @@ describe('watcher pipeline', () => { expect(scope.get()).toEqual({ theme: 'light' }) }) + it('keeps the reload queue alive after an invariant violation escapes a commit', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, debounceMs: 5 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + let arm = true + ctx.on('settings/updated', () => { + if (!arm) return + throw Object.assign(new Error('forged relation'), { code: 'INVARIANT' }) + }) + const [instance] = await fakeInstances() + + await writeFile(path, 'ui-theme:\n theme: broken-commit\n') + instance!.watcher.emit('all', 'change', path) + await vi.waitFor(() => { + expect(scope.get().theme).toBe('broken-commit') + }) + + arm = false + await writeFile(path, 'ui-theme:\n theme: recovered\n') + instance!.watcher.emit('all', 'change', path) + await vi.waitFor(() => { + expect(scope.get().theme).toBe('recovered') + }) + }) + + it('quiesces the refresh pipeline before dispose completes', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = new Context() + const fiber = ctx.plugin(SettingsLocal, { path, debounceMs: 5 }) + await fiber + ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + let disposed = false + let postDisposeCommits = 0 + ctx.on('settings/updated', () => { + if (disposed) postDisposeCommits += 1 + }) + + await writeFile(path, 'ui-theme:\n theme: darker\n') + const [instance] = await fakeInstances() + // Two queued refreshes: dispose interrupts one mid-flight and the other + // before it starts, so both closed guards must hold. + instance!.watcher.emit('all', 'change', path) + instance!.watcher.emit('all', 'change', path) + await fiber.dispose() + disposed = true + instance!.watcher.emit('all', 'change', path) + await new Promise(resolve => setTimeout(resolve, 100)) + expect(postDisposeCommits).toBe(0) + }) + it('treats an event for a still-absent file as a no-op', async () => { const dir = await tempDir() const path = join(dir, 'settings.yaml') diff --git a/packages/settings/settings/README.i18n.yaml b/packages/settings/settings/README.i18n.yaml index 51f30f2ca6..6f5e5c6beb 100644 --- a/packages/settings/settings/README.i18n.yaml +++ b/packages/settings/settings/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/settings/settings/README.md -README.md: e57db00c095a87f9f0b51397e030dec364f48e62 -README.zh.md: 8733823106f0ef3880cbe2c567de2edcdeff2c86 +README.md: f7858a247f6011cd0654a73b5325d81c118441e5 +README.zh.md: 67ecba695066389bfe3a69f517d52f91b48b6c75 diff --git a/packages/settings/settings/README.md b/packages/settings/settings/README.md index e57db00c09..f7858a247f 100644 --- a/packages/settings/settings/README.md +++ b/packages/settings/settings/README.md @@ -9,12 +9,13 @@ Abstract user-settings seam (`ctx.settings`). One provider holds a raw document - `register(ns, schema, { base?, applies? })` — returns the owner `SettingsScope` (`get`/`watch`/`update`). The registration is an effect on the calling plugin's fiber: disposing that fiber removes the namespace and its observers. A stored section the schema rejects fails the registration itself; a duplicate namespace fails loud. - `describe()` — one descriptor per namespace (`schema.toJSON()` envelope, resolved value, `applies`) for configuration surfaces. - `get(ns)` — resolved value, `undefined` while unregistered. -- `update(ns, patch)` — deep-merges the plain-object patch into the user section only (never the `base`), validates the resolved candidate, persists through the provider, then commits. Validation failure rejects before anything is persisted; a read-only provider (`writable: false`) rejects every update. -- Resolved values are deep-frozen snapshots; watchers receive `(next, prev)` after each commit, and watcher failures are contained. +- `update(ns, patch)` — deep-merges the plain-object patch into the user section only (never the `base`), validates the resolved candidate, persists through the provider, then commits. Validation failure rejects before anything is persisted; a read-only provider (`writable: false`) rejects every write. Writes to one namespace are serialized in call order. +- `replace(ns, section)` — sets the user section wholesale: the removal/reset path a merge cannot express (`replace({})` re-inherits `base` and schema defaults). +- Resolved values are deep-frozen snapshots; watchers receive `(next, prev)` after each commit, and watcher failures — sync throws and async rejections alike — are contained. ## Provider contract -Subclasses implement `writable`, `load()`, and `persist(ns, section)`, and push externally observed documents through the protected `publish(doc)`. At publish, each registered namespace re-resolves independently: an invalid section keeps that namespace's last good value and warns — a live reload never takes the process down — while boot-time and registration-time validation fail loud. +Subclasses implement `writable`, `load()`, and `persist(ns, section)`, and push externally observed documents through the protected `publish(doc)`. The base service init loads and publishes the document once before the service becomes injectable; a provider with its own init (watcher, connection) delegates first via `yield* super[Service.init]()`. At publish, each registered namespace re-resolves independently: an invalid section keeps that namespace's last good value and warns — a live reload never takes the process down — while boot-time and registration-time validation fail loud. ## Events @@ -31,5 +32,5 @@ No direct invalidation; a consumer that folds a settings value into the request ## Known Limitations and Deferred Work - **Single user layer** — resolution knows schema defaults, one composition `base`, and one user document; there is no project/managed layering or per-value provenance yet. -- **Cross-process concurrency is provider-defined** — the seam serializes nothing across processes; concurrent writers converge by provider behavior (the local file provider is last-write-wins). +- **Cross-process concurrency is provider-defined** — the seam serializes writes per namespace in-process only; concurrent processes converge by provider behavior (the local file provider is last-write-wins). - **No secret-field redaction** — `describe()` returns resolved values verbatim; a wire surface (RPC/UI) must redact `role('secret')` fields before exposure. diff --git a/packages/settings/settings/README.zh.md b/packages/settings/settings/README.zh.md index 8733823106..67ecba6950 100644 --- a/packages/settings/settings/README.zh.md +++ b/packages/settings/settings/README.zh.md @@ -9,12 +9,13 @@ - `register(ns, schema, { base?, applies? })` — 返回 owner 的 `SettingsScope`(`get`/`watch`/`update`)。注册是调用方插件 fiber 上的 effect:dispose 该 fiber 即移除 namespace 及其观察者。schema 拒绝的存量分节会使注册本身失败;重复 namespace 立即报错。 - `describe()` — 每个 namespace 一条描述(`schema.toJSON()` 信封、解析值、`applies`),供配置界面使用。 - `get(ns)` — 解析值;未注册时为 `undefined`。 -- `update(ns, patch)` — 把普通对象 patch 深合并进用户分节(绝不合并进 `base`),校验解析候选值,经 provider 持久化后提交。校验失败在持久化前拒绝;只读 provider(`writable: false`)拒绝一切更新。 -- 解析值是深冻结快照;每次提交后观察者收到 `(next, prev)`,观察者异常被隔离。 +- `update(ns, patch)` — 把普通对象 patch 深合并进用户分节(绝不合并进 `base`),校验解析候选值,经 provider 持久化后提交。校验失败在持久化前拒绝;只读 provider(`writable: false`)拒绝一切写入。同一 namespace 的写入按调用顺序串行。 +- `replace(ns, section)` — 整体替换用户分节:merge 表达不了的删除/重置路径(`replace({})` 重新继承 `base` 与 schema 默认值)。 +- 解析值是深冻结快照;每次提交后观察者收到 `(next, prev)`;观察者异常——同步抛出与异步拒绝——均被隔离。 ## Provider 契约 -子类实现 `writable`、`load()`、`persist(ns, section)`,并通过受保护的 `publish(doc)` 推入外部观察到的文档。publish 时每个已注册 namespace 独立重解析:非法分节保留该 namespace 的最后可用值并告警——热重载绝不拖垮进程;启动期与注册期校验则立即报错。 +子类实现 `writable`、`load()`、`persist(ns, section)`,并通过受保护的 `publish(doc)` 推入外部观察到的文档。基类 service init 在服务可注入前加载并发布一次文档;自有 init(watcher、连接)的 provider 先经 `yield* super[Service.init]()` 委托。publish 时每个已注册 namespace 独立重解析:非法分节保留该 namespace 的最后可用值并告警——热重载绝不拖垮进程;启动期与注册期校验则立即报错。 ## 事件 @@ -31,5 +32,5 @@ ## Known Limitations and Deferred Work - **单一用户层** — 解析只认识 schema 默认值、一个组合 `base` 与一个用户文档;尚无 project/managed 分层或按值溯源。 -- **跨进程并发由 provider 定义** — seam 不做跨进程串行化;并发写入者按 provider 行为收敛(本地文件 provider 为后写胜出)。 +- **跨进程并发由 provider 定义** — seam 仅在进程内按 namespace 串行化写入;跨进程并发按 provider 行为收敛(本地文件 provider 为后写胜出)。 - **无 secret 字段脱敏** — `describe()` 原样返回解析值;wire 面(RPC/UI)在暴露前必须对 `role('secret')` 字段脱敏。 diff --git a/packages/settings/settings/src/index.ts b/packages/settings/settings/src/index.ts index bec60dfdea..10b21c2154 100644 --- a/packages/settings/settings/src/index.ts +++ b/packages/settings/settings/src/index.ts @@ -7,7 +7,6 @@ */ import { Context, Service } from 'cordis' -import { deepEqual } from 'cosmokit' import type z from 'schemastery' import type { Branded } from '@deepseek-ai/dsh-brand' @@ -59,16 +58,23 @@ export interface SettingsScope { /** Current resolved value: schema defaults, then `base`, then the user layer. */ get(): T /** - * Observe committed changes to this namespace's resolved value. + * Observe committed changes to this namespace's resolved value. A callback + * may be async; a rejection is contained and logged like a sync throw. * @param callback - invoked after each commit with the next and previous values. * @returns the disposer removing this observer. */ - watch(callback: (next: T, prev: T) => void): () => void + watch(callback: (next: T, prev: T) => void | Promise): () => void /** * Merge a partial patch into this namespace's user layer and persist it. * @param patch - plain-object patch over the user section. */ update(patch: object): Promise + /** + * Replace this namespace's user section wholesale; absent keys re-inherit + * the composition `base` and schema defaults (`replace({})` resets all). + * @param section - the complete next user section. + */ + replace(section: object): Promise } declare module 'cordis' { @@ -91,6 +97,28 @@ declare module 'cordis' { } } +/** + * Deep equality over JSON-shaped data (objects, arrays, primitives) — the + * seam's single change-detection predicate, exported so the invariant + * companion checks exactly the implementation's relation. + * @param a - one JSON-shaped value. + * @param b - the other JSON-shaped value. + * @returns whether the two values are structurally equal. + */ +export function deepEqualJson(a: unknown, b: unknown): boolean { + if (a === b) return true + if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false + if (Array.isArray(a) || Array.isArray(b)) { + if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false + return a.every((entry, index) => deepEqualJson(entry, b[index])) + } + const left = a as Record + const right = b as Record + const keys = Object.keys(left) + if (keys.length !== Object.keys(right).length) return false + return keys.every(key => key in right && deepEqualJson(left[key], right[key])) +} + /** Whether a value is a plain data object (not an array, null, or class instance). */ function isPlainObject(value: unknown): value is Record { if (typeof value !== 'object' || value === null || Array.isArray(value)) return false @@ -128,7 +156,7 @@ interface SettingsRegistration { base: unknown applies: SettingsApplies resolved: unknown - watchers: Set<(next: never, prev: never) => void> + watchers: Set<(next: never, prev: never) => void | Promise> } /** @@ -141,11 +169,22 @@ export abstract class Settings extends Service { private readonly registrations = new Map() /** Latest published raw document; empty until the provider's first publish. */ private document: Record = {} + /** Per-namespace write chains; settled tails, so a failure never poisons the queue. */ + private readonly writeQueues = new Map>() constructor(ctx: Context) { super(ctx, 'settings') } + /** + * Load the provider's document once and publish it before the service + * becomes injectable. Providers with their own init (watchers, connections) + * delegate here first via `yield* super[Service.init]()`. + */ + async* [Service.init](): AsyncGenerator<() => void, void, void> { + this.publish(await this.load()) + } + /** Whether {@link update} may persist through this provider. */ abstract readonly writable: boolean @@ -195,6 +234,7 @@ export abstract class Settings extends Service { return () => registration.watchers.delete(callback) }, update: patch => this.update(ns, patch), + replace: section => this.replace(ns, section), } } @@ -223,11 +263,30 @@ export abstract class Settings extends Service { /** * Merge a patch into one registered namespace's user layer, validate the * resolved candidate, persist through the provider, then commit and emit. - * A validation failure rejects before anything is persisted. + * A validation failure rejects before anything is persisted. Writes to one + * namespace are serialized: concurrent updates apply in call order, each + * merging over the previous write's committed section. * @param ns - the registered namespace to update. * @param patch - plain-object patch over the user section. */ async update(ns: SettingsNamespace, patch: object): Promise { + return this.write(ns, patch, 'merge') + } + + /** + * Replace one registered namespace's user section wholesale, validate, + * persist, then commit and emit. Keys absent from `section` fall back to the + * composition `base` and schema defaults — this is the removal/reset path a + * merge-only patch cannot express (`replace({})` re-inherits everything). + * @param ns - the registered namespace to replace. + * @param section - the complete next user section. + */ + async replace(ns: SettingsNamespace, section: object): Promise { + return this.write(ns, section, 'replace') + } + + /** Validate a write, then queue it on the namespace's serialized write chain. */ + private write(ns: SettingsNamespace, input: object, mode: 'merge' | 'replace'): Promise { const registration = this.registrations.get(ns) if (registration === undefined) { throw new Error(`settings namespace "${ns}" is not registered`) @@ -235,14 +294,23 @@ export abstract class Settings extends Service { if (!this.writable) { throw new Error(`settings provider is read-only: "${ns}" cannot be updated in-process`) } - if (!isPlainObject(patch)) { - throw new TypeError(`settings update for "${ns}" must be a plain object patch`) + if (!isPlainObject(input)) { + throw new TypeError(`settings ${mode === 'merge' ? 'update' : 'replace'} for "${ns}" must be a plain object`) } - const section = mergeLayers(this.section(ns) ?? {}, patch) as Record - const next = deepFreeze(this.resolve(registration.schema, registration.base, section)) - await this.persist(ns, section) - this.document[ns] = section - this.commit(registration, next, 'update') + const previous = this.writeQueues.get(ns) ?? Promise.resolve() + // Chain past a failed predecessor: one rejected write must not poison the + // namespace queue for every later caller. + const run = previous.catch(() => undefined).then(async () => { + const section = mode === 'merge' + ? mergeLayers(this.section(ns) ?? {}, input) as Record + : structuredClone(input) + const next = deepFreeze(this.resolve(registration.schema, registration.base, section)) + await this.persist(ns, section) + this.document[ns] = section + this.commit(registration, next, 'update') + }) + this.writeQueues.set(ns, run) + return run } /** @@ -287,17 +355,38 @@ export abstract class Settings extends Service { /** Commit a resolved value when changed: swap, notify watchers, emit the event. */ private commit(registration: SettingsRegistration, next: unknown, source: SettingsUpdateSource): void { const prev = registration.resolved - if (deepEqual(next, prev)) return + if (deepEqualJson(next, prev)) return registration.resolved = next for (const watcher of [...registration.watchers]) { try { - watcher(next as never, prev as never) + // A watcher may be async: adopt its promise so a rejection is contained + // here instead of surfacing as an unhandled rejection. + const outcome = watcher(next as never, prev as never) as unknown + if (outcome instanceof Promise) { + outcome.catch((error: unknown) => { + this.warnWatcherFailure(registration.ns, error) + }) + } } catch (error) { - this.ctx.logger.warn('settings: watcher for "%s" failed', registration.ns) - this.ctx.logger.warn(error) + this.warnWatcherFailure(registration.ns, error) } } - this.ctx.emit('settings/updated', registration.ns, next, prev, source) + try { + this.ctx.emit('settings/updated', registration.ns, next, prev, source) + } catch (error) { + // Invariant violations are harness-fatal by design; any other listener + // failure is contained so one broken observer cannot wedge the commit + // path (and, through it, a provider's reload loop). + if ((error as { code?: unknown } | null)?.code === 'INVARIANT') throw error + this.ctx.logger.warn('settings: a settings/updated listener for "%s" failed', registration.ns) + this.ctx.logger.warn(error) + } + } + + /** Contained-watcher diagnostic shared by the sync and async failure paths. */ + private warnWatcherFailure(ns: SettingsNamespace, error: unknown): void { + this.ctx.logger.warn('settings: watcher for "%s" failed', ns) + this.ctx.logger.warn(error) } } diff --git a/packages/settings/settings/src/invariant.ts b/packages/settings/settings/src/invariant.ts index 235f413aae..d8db41bce4 100644 --- a/packages/settings/settings/src/invariant.ts +++ b/packages/settings/settings/src/invariant.ts @@ -5,6 +5,7 @@ import type { Context } from 'cordis' import type { InvariantFailure, InvariantInstaller } from '@deepseek-ai/dsh-invariants' +import { deepEqualJson } from './index.ts' const PACKAGE_NAME = '@deepseek-ai/dsh-settings' @@ -15,7 +16,9 @@ export const inject = ['invariants'] /** * Install the commit-event contract: `settings/updated` fires only for a - * currently registered namespace and only when the resolved value changed. + * currently registered namespace, only when the resolved value changed, and + * only with the service's authoritative resolved value — all judged with the + * seam's own equality predicate. */ const install: InvariantInstaller = (ctx: Context, fail: InvariantFailure) => { ctx.on('settings/updated', (ns, next, prev) => { @@ -23,10 +26,14 @@ const install: InvariantInstaller = (ctx: Context, fail: InvariantFailure) => { if (settings === undefined) { fail(`settings/updated for "${ns}" emitted without a live settings service`) } - if (settings.get(ns) === undefined) { + const current = settings.get(ns) + if (current === undefined) { fail(`settings/updated for "${ns}" emitted while the namespace is unregistered`) } - if (JSON.stringify(next) === JSON.stringify(prev)) { + if (!deepEqualJson(current, next)) { + fail(`settings/updated for "${ns}" does not match the authoritative resolved value`) + } + if (deepEqualJson(next, prev)) { fail(`settings/updated for "${ns}" emitted without a resolved-value change`) } }) diff --git a/packages/settings/settings/tests/invariant.spec.ts b/packages/settings/settings/tests/invariant.spec.ts index f12ff6126c..0976827368 100644 --- a/packages/settings/settings/tests/invariant.spec.ts +++ b/packages/settings/settings/tests/invariant.spec.ts @@ -38,4 +38,15 @@ describe('settings invariants', () => { ctx.emit('settings/updated', settingsNamespace('ui-theme'), { theme: 'dark' }, { theme: 'dark' }, 'update') }).toThrow(/without a resolved-value change/) }) + + it('fails a settings/updated emission whose value diverges from the authoritative state', async () => { + const ctx = await setup(true) + ctx.settings.register(settingsNamespace('ui-theme'), z.object({ + theme: z.string().default('dark'), + })) + // Fabricated next ≠ the service's current resolved value ({theme: 'dark'}). + expect(() => { + ctx.emit('settings/updated', settingsNamespace('ui-theme'), { theme: 'forged' }, { theme: 'dark' }, 'update') + }).toThrow(/authoritative/) + }) }) diff --git a/packages/settings/settings/tests/memory.ts b/packages/settings/settings/tests/memory.ts index 0b20771cbe..bb310c939d 100644 --- a/packages/settings/settings/tests/memory.ts +++ b/packages/settings/settings/tests/memory.ts @@ -5,7 +5,6 @@ * packages. */ -import { Service } from 'cordis' import { Settings, type SettingsNamespace } from '../src/index.ts' /** In-memory provider exposing the protected seam hooks to tests. */ @@ -17,13 +16,18 @@ export class MemorySettings extends Settings { /** When false, update() must reject before reaching persist(). */ writableFlag: boolean + /** Artificial persist latency so tests can interleave concurrent updates. */ + persistDelayMs: number + constructor(ctx: ConstructorParameters[0], options?: { doc?: Record writable?: boolean + persistDelayMs?: number }) { super(ctx) this.doc = structuredClone(options?.doc ?? {}) this.writableFlag = options?.writable ?? true + this.persistDelayMs = options?.persistDelayMs ?? 0 } get writable(): boolean { @@ -34,10 +38,12 @@ export class MemorySettings extends Settings { return Promise.resolve(structuredClone(this.doc)) } - protected persist(ns: SettingsNamespace, section: Record): Promise { + protected async persist(ns: SettingsNamespace, section: Record): Promise { + if (this.persistDelayMs > 0) { + await new Promise(resolve => setTimeout(resolve, this.persistDelayMs)) + } this.persisted.push({ ns, section: structuredClone(section) }) this.doc[ns] = structuredClone(section) - return Promise.resolve() } /** Simulate an external storage change reaching the provider. */ @@ -45,9 +51,4 @@ export class MemorySettings extends Settings { this.doc = structuredClone(doc) this.publish(structuredClone(doc)) } - - async* [Service.init](): AsyncGenerator<() => void, void, void> { - this.publish(await this.load()) - yield () => { this.persisted.length = 0 } - } } diff --git a/packages/settings/settings/tests/settings.spec.ts b/packages/settings/settings/tests/settings.spec.ts index a3d223c3f7..c413e948ed 100644 --- a/packages/settings/settings/tests/settings.spec.ts +++ b/packages/settings/settings/tests/settings.spec.ts @@ -1,9 +1,32 @@ import { describe, expect, it, vi } from 'vitest' import { Context } from 'cordis' import z from 'schemastery' -import { settingsNamespace, type SettingsScope, type SettingsUpdateSource } from '../src/index.ts' +import { Settings, deepEqualJson, settingsNamespace, type SettingsNamespace, type SettingsScope, type SettingsUpdateSource } from '../src/index.ts' import { MemorySettings } from './memory.ts' +/** A provider implementing only the three primitives: the seam owns init. */ +class BareProvider extends Settings { + doc: Record + + constructor(ctx: ConstructorParameters[0], options?: { doc?: Record }) { + super(ctx) + this.doc = structuredClone(options?.doc ?? {}) + } + + get writable(): boolean { + return true + } + + protected load(): Promise> { + return Promise.resolve(structuredClone(this.doc)) + } + + protected persist(ns: SettingsNamespace, section: Record): Promise { + this.doc[ns] = structuredClone(section) + return Promise.resolve() + } +} + interface ThemeConfig { theme: 'dark' | 'light' fontSize: number @@ -119,7 +142,7 @@ describe('registration', () => { inject: ['settings'], apply: (child: Context) => { scope = child.settings.register(settingsNamespace('ui-theme'), ThemeSchema) - scope.watch(next => seen.push(next)) + scope.watch((next) => { seen.push(next) }) }, }) await fiber @@ -191,6 +214,9 @@ describe('update', () => { expect(provider.persisted).toEqual([]) expect(events).toEqual([]) expect(scope.get()).toEqual({ theme: 'dark', fontSize: 14 }) + // The failed write must not poison the namespace queue for later writers. + await scope.update({ fontSize: 18 }) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 18 }) }) it('ignores explicit undefined entries so a sparse patch cannot erase keys', async () => { @@ -206,6 +232,7 @@ describe('update', () => { const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) await expect(scope.update([1])).rejects.toThrow(TypeError) await expect(scope.update(new Date() as unknown as object)).rejects.toThrow(TypeError) + await expect(scope.replace([1])).rejects.toThrow(/replace for "ui-theme"/) }) it('accepts a null-prototype patch object', async () => { @@ -231,6 +258,89 @@ describe('update', () => { }) }) +describe('deepEqualJson', () => { + it.each([ + [{ a: [1, 2] }, { a: [1, 2] }, true], + [{ a: [1, 2] }, { a: [1] }, false], + [{ a: [1] }, { a: { 0: 1 } }, false], + [{ a: 1 }, { b: 1 }, false], + [{ a: 1 }, {}, false], + [{ a: null }, { a: null }, true], + [{ a: null }, { a: {} }, false], + ])('compares %j vs %j as %s', (a, b, equal) => { + expect(deepEqualJson(a, b)).toBe(equal) + }) +}) + +describe('review regressions', () => { + it('propagates an invariant-coded listener failure instead of containing it', async () => { + const { ctx, provider } = await boot() + ctx.on('settings/updated', () => { + throw Object.assign(new Error('forged relation'), { code: 'INVARIANT' }) + }) + ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(() => { provider.pushExternal({ 'ui-theme': { theme: 'light' } }) }) + .toThrow(/forged relation/) + }) + + it('serializes concurrent updates so neither patch is lost', async () => { + const { ctx, provider } = await boot({ persistDelayMs: 10 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await Promise.all([ + scope.update({ theme: 'light' }), + scope.update({ fontSize: 20 }), + ]) + expect(provider.doc['ui-theme']).toEqual({ theme: 'light', fontSize: 20 }) + expect(scope.get()).toEqual({ theme: 'light', fontSize: 20 }) + }) + + it('contains a throwing settings/updated listener and keeps later commits alive', async () => { + const { ctx, provider } = await boot() + ctx.on('settings/updated', () => { + throw new Error('listener boom') + }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(() => { provider.pushExternal({ 'ui-theme': { theme: 'light' } }) }).not.toThrow() + expect(scope.get().theme).toBe('light') + provider.pushExternal({ 'ui-theme': { theme: 'dark' } }) + expect(scope.get().theme).toBe('dark') + }) + + it('contains an async watcher rejection', async () => { + const { ctx, provider } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + scope.watch(async () => { + throw new Error('async watcher boom') + }) + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + expect(scope.get().theme).toBe('light') + // Give the rejected watcher promise a microtask turn; containment means + // vitest observes no unhandled rejection out of this test. + await new Promise(resolve => setTimeout(resolve, 10)) + }) + + it('loads the provider document through the base init without provider boilerplate', async () => { + const ctx = new Context() + await ctx.plugin(BareProvider, { doc: { 'ui-theme': { fontSize: 7 } } }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 7 }) + }) + + it('replaces the user section wholesale so overrides can be removed', async () => { + const { ctx, provider } = await boot({ doc: { 'ui-theme': { theme: 'light', fontSize: 20 } } }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema, { + base: { fontSize: 16 }, + }) + await scope.replace({ theme: 'light' }) + // fontSize override is gone: resolution falls back to the base layer. + expect(scope.get()).toEqual({ theme: 'light', fontSize: 16 }) + expect(provider.doc['ui-theme']).toEqual({ theme: 'light' }) + await scope.replace({}) + expect(scope.get()).toEqual({ theme: 'dark', fontSize: 16 }) + expect(provider.doc['ui-theme']).toEqual({}) + }) +}) + describe('publish', () => { it('notifies watchers of an external change with source provider', async () => { const { ctx, provider } = await boot() diff --git a/scripts/gen-cordis-catalog.ts b/scripts/gen-cordis-catalog.ts index b7df3dc622..971576f35f 100644 --- a/scripts/gen-cordis-catalog.ts +++ b/scripts/gen-cordis-catalog.ts @@ -189,6 +189,11 @@ export const LINK_MAP: Record = { ToolRegistry: 'tools.md', ToolRestriction: 'tools.md', ToolSchema: 'tools.md', + SettingsNamespace: 'settings.md', + SettingsRegisterOptions: 'settings.md', + SettingsScope: 'settings.md', + SettingsDescriptor: 'settings.md', + SettingsUpdateSource: 'settings.md', AskUserQuestionAnswer: 'user-interaction.md', AskUserQuestionRequest: 'user-interaction.md', UserInteractionProvider: 'user-interaction.md', @@ -217,11 +222,6 @@ const FOUNDATION_TYPE_NAMES = new Set([ /** Project types deliberately documented outside the core-data catalog. */ const TYPE_LINK_EXEMPTIONS: Readonly> = { AgentFactory: 'agent creation seam is owned by packages/core/agent/README.md', - SettingsNamespace: 'settings seam vocabulary is owned by packages/settings/settings/README.md', - SettingsUpdateSource: 'settings seam vocabulary is owned by packages/settings/settings/README.md', - SettingsRegisterOptions: 'settings seam vocabulary is owned by packages/settings/settings/README.md', - SettingsScope: 'settings seam vocabulary is owned by packages/settings/settings/README.md', - SettingsDescriptor: 'settings seam vocabulary is owned by packages/settings/settings/README.md', z: 'schemastery schema constructor is owned by vendor/schemastery (vendored upstream)', BeginCommandRequest: 'event-local request contract is owned by packages/client/ui-slash/src/types.ts', InsertReferenceRequest: 'event-local request contract is owned by packages/client/ui-slash/src/types.ts', diff --git a/scripts/project-doc-site.spec.ts b/scripts/project-doc-site.spec.ts index e6cc11ae6e..28f854fe9b 100644 --- a/scripts/project-doc-site.spec.ts +++ b/scripts/project-doc-site.spec.ts @@ -197,7 +197,7 @@ describe('docsPages locale routes', () => { const translated = rootPages.filter(page => page.contentLocale === 'zh-CN') const fallbacks = rootPages.filter(page => page.contentLocale === 'en-US') - expect(translated).toHaveLength(18) + expect(translated).toHaveLength(19) expect(translated.every(page => page.source.endsWith('.zh.md'))).toBe(true) expect(fallbacks.map(page => page.source).sort()).toEqual([ 'docs/core-data-structures/commands.md', diff --git a/scripts/type-equiv.manifest.json b/scripts/type-equiv.manifest.json index b607995003..7bd9c09177 100644 --- a/scripts/type-equiv.manifest.json +++ b/scripts/type-equiv.manifest.json @@ -1308,6 +1308,36 @@ "doc": "docs/core-data-structures/subprocess.md", "symbol": "SubprocessCollectedOutputs", "source": "packages/subprocess/subprocess/src/types.ts" + }, + { + "doc": "docs/core-data-structures/settings.md", + "symbol": "SettingsNamespace", + "source": "packages/settings/settings/src/index.ts" + }, + { + "doc": "docs/core-data-structures/settings.md", + "symbol": "SettingsRegisterOptions", + "source": "packages/settings/settings/src/index.ts" + }, + { + "doc": "docs/core-data-structures/settings.md", + "symbol": "SettingsApplies", + "source": "packages/settings/settings/src/index.ts" + }, + { + "doc": "docs/core-data-structures/settings.md", + "symbol": "SettingsScope", + "source": "packages/settings/settings/src/index.ts" + }, + { + "doc": "docs/core-data-structures/settings.md", + "symbol": "SettingsDescriptor", + "source": "packages/settings/settings/src/index.ts" + }, + { + "doc": "docs/core-data-structures/settings.md", + "symbol": "SettingsUpdateSource", + "source": "packages/settings/settings/src/index.ts" } ] } diff --git a/website/docs.ts b/website/docs.ts index 1888cd908c..cbc8d9e627 100644 --- a/website/docs.ts +++ b/website/docs.ts @@ -256,6 +256,7 @@ const coreDataReference = pairedPages(([ ['sandbox.md', '沙箱', 'Sandboxing', 18], ['web.md', 'Web 访问', 'Web access', 19], ['persistence.md', '会话持久化', 'Session persistence', 20], + ['settings.md', '用户设置', 'User settings', 21], ] as const).map(([file, rootLabel, enLabel, order]): PairedPage => ({ source: `docs/core-data-structures/${file}`, route: `reference/core-data-structures/${file}`, From 1010291fe6cd764deff8e5e3056bb57f94c3fa4f Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Wed, 29 Jul 2026 10:07:28 +0800 Subject: [PATCH 03/17] fix(settings): close cross-namespace, dispatch, and lifecycle races from second review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Confirmed and fixed, each with a regression test that failed first: - Concurrent writes to different namespaces lost whole sections on disk (each persist rendered the full document from a stale text): the local provider serializes render->write->rename->text-commit on one internal persist chain shared by every namespace queue. - One throwing settings/updated listener starved the rest (cordis emit stops at the first throw): commit fans out per listener via events.dispatch, contains individual failures, and rethrows the first INVARIANT-coded error only after every listener ran. - Write queues ignored fiber/service lifecycle: the base init now registers a teardown that refuses new writes and drains queued chains; queued tasks re-verify service liveness and namespace ownership before running and again before committing, so a registrant disposed mid-flight is never notified and a disposed service never commits. - Async watcher invocations could interleave (a slow stale call applied last): each watcher carries a serialized invocation chain — one call at a time, in commit order; JSDoc/doc pages state the async timing. - update/replace borrowed the caller's object until the queued task ran: inputs are structured-clone snapshotted at call time; non-cloneable plain objects reject with a typed error. - Composition guard now proves the documented fallback: the consumer uses the optional scoped-inject shape and boots both with the settings entry (hot publish) and without it (entry-config resolution, no scope). - core-data-structures index: settings.md row added to the sub-page table in core.md/core.zh.md. Both packages hold per-file 100% coverage across repeated runs. --- docs/cordis-catalog/events.md | 2 +- docs/cordis-catalog/services.md | 2 +- docs/core-data-structures/core.i18n.yaml | 4 +- docs/core-data-structures/core.md | 1 + docs/core-data-structures/core.zh.md | 1 + docs/core-data-structures/settings.i18n.yaml | 4 +- docs/core-data-structures/settings.md | 5 +- docs/core-data-structures/settings.zh.md | 5 +- docs/event-producer-consumer.md | 2 +- .../settings/settings-local/README.i18n.yaml | 4 +- packages/settings/settings-local/README.md | 1 + packages/settings/settings-local/README.zh.md | 1 + packages/settings/settings-local/src/index.ts | 14 +- .../tests/loader-composition.spec.ts | 63 +++++-- .../settings-local/tests/local.spec.ts | 20 +++ packages/settings/settings/README.i18n.yaml | 4 +- packages/settings/settings/README.md | 3 +- packages/settings/settings/README.zh.md | 3 +- packages/settings/settings/src/index.ts | 114 +++++++++---- .../settings/settings/tests/settings.spec.ts | 157 +++++++++++++++++- 20 files changed, 339 insertions(+), 71 deletions(-) diff --git a/docs/cordis-catalog/events.md b/docs/cordis-catalog/events.md index 74e1e6cfda..44fcc62e88 100644 --- a/docs/cordis-catalog/events.md +++ b/docs/cordis-catalog/events.md @@ -662,7 +662,7 @@ Committed change to one registered namespace's resolved value. Emitted after the Types: [SettingsNamespace](../core-data-structures/settings.md) · [SettingsUpdateSource](../core-data-structures/settings.md) -Source: [`packages/settings/settings/src/index.ts:96`](../../packages/settings/settings/src/index.ts) +Source: [`packages/settings/settings/src/index.ts:97`](../../packages/settings/settings/src/index.ts) ## `slash/*` diff --git a/docs/cordis-catalog/services.md b/docs/cordis-catalog/services.md index e915952ffb..c2e5451699 100644 --- a/docs/cordis-catalog/services.md +++ b/docs/cordis-catalog/services.md @@ -1442,7 +1442,7 @@ async replace(ns: SettingsNamespace, section: object): Promise Types: [SettingsDescriptor](../core-data-structures/settings.md) · [SettingsNamespace](../core-data-structures/settings.md) · [SettingsRegisterOptions](../core-data-structures/settings.md) · [SettingsScope](../core-data-structures/settings.md) -Source: [`packages/settings/settings/src/index.ts:168`](../../packages/settings/settings/src/index.ts) +Source: [`packages/settings/settings/src/index.ts:176`](../../packages/settings/settings/src/index.ts) ## `ctx.skills` — `SkillService` diff --git a/docs/core-data-structures/core.i18n.yaml b/docs/core-data-structures/core.i18n.yaml index f0d078c123..d0bdbc5dd2 100644 --- a/docs/core-data-structures/core.i18n.yaml +++ b/docs/core-data-structures/core.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write docs/core-data-structures/core.md -core.md: 647c7f273183e0890ef191d98ab009ad129db572 -core.zh.md: 8b0f439f09a6e6609dbe69c3056aa74d553a0943 +core.md: ca8426e6fbece18a277cc31f5e86d3058b3feb32 +core.zh.md: 6b297ca233428d38dbe022e58344e98fab7a15ad diff --git a/docs/core-data-structures/core.md b/docs/core-data-structures/core.md index 647c7f2731..ca8426e6fb 100644 --- a/docs/core-data-structures/core.md +++ b/docs/core-data-structures/core.md @@ -24,6 +24,7 @@ Everything else is documented on a **sub-page**, not here. The rule that draws t | [commands.md](commands.md) | the human-command seam: definitions, adapter discovery, direct invocation, results, and parsing views | | [session.md](session.md) | the full `SessionEventMap` variant catalog, `TurnTrigger`/`TurnEndReason`, `deriveMessages()`, execution enclosure, and standalone events | | [persistence.md](persistence.md) | the durability seam: `SessionPersistence`, JSONL + SQLite backends, `session/flush`, crash recovery, `SessionHeader` | +| [settings.md](settings.md) | the user-settings seam: `SettingsNamespace` registration, layered resolution (defaults → composition `base` → user document), owner scopes, hot commits | | [session-query.md](session-query.md) | logical records, bounded exact-event reads, relationship traces, semantic filters/documents, and full-text result pages | | [session-title.md](session-title.md) | durable title snapshots, source provenance, and the asynchronous provider contract | | [system-prompt.md](system-prompt.md) | per-assembly context, tool-provider results, prompt sections, and cooperative assembly | diff --git a/docs/core-data-structures/core.zh.md b/docs/core-data-structures/core.zh.md index 8b0f439f09..6b297ca233 100644 --- a/docs/core-data-structures/core.zh.md +++ b/docs/core-data-structures/core.zh.md @@ -24,6 +24,7 @@ harness 是一个微内核:一个极小的核心加上众多插件。大多数 | [commands.md](commands.md) | 人类命令 seam:定义、适配器发现、直接调用、结果与解析视图 | | [session.md](session.md) | 完整的 `SessionEventMap` 变体目录、`TurnTrigger`/`TurnEndReason`、`deriveMessages()`、执行封闭与独立事件 | | [persistence.md](persistence.md) | 持久性 seam:`SessionPersistence`、JSONL + SQLite 后端、`session/flush`、崩溃恢复、`SessionHeader` | +| [settings.md](settings.md) | 用户设置 seam:`SettingsNamespace` 注册、分层解析(默认值 → 组合 `base` → 用户文档)、owner scope、热提交 | | [session-query.md](session-query.md) | 逻辑记录、有界精确事件读取、关系追踪、语义筛选器/文档与全文检索结果页 | | [session-title.md](session-title.md) | 持久标题快照、来源 provenance 与异步提供方契约 | | [system-prompt.md](system-prompt.md) | 逐次组装的上下文、工具提供方结果、提示词段落与协作式组装 | diff --git a/docs/core-data-structures/settings.i18n.yaml b/docs/core-data-structures/settings.i18n.yaml index 6dc8750dfb..cca43c251b 100644 --- a/docs/core-data-structures/settings.i18n.yaml +++ b/docs/core-data-structures/settings.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write docs/core-data-structures/settings.md -settings.md: 851087065c627f2390041dd6e69273e31be3917d -settings.zh.md: 955c4fbf7c147b3d0a8be62c33c031d7bd2c4ba2 +settings.md: abbfecb35f67b27e16dffb9558a35cf368c90beb +settings.zh.md: c746e3cc181f8347cbe634b9231cfee1b3beecd3 diff --git a/docs/core-data-structures/settings.md b/docs/core-data-structures/settings.md index 851087065c..abbfecb35f 100644 --- a/docs/core-data-structures/settings.md +++ b/docs/core-data-structures/settings.md @@ -46,8 +46,9 @@ interface SettingsScope { /** Current resolved value: schema defaults, then `base`, then the user layer. */ get(): T /** - * Observe committed changes to this namespace's resolved value. A callback - * may be async; a rejection is contained and logged like a sync throw. + * Observe committed changes to this namespace's resolved value. Invocations + * of one callback run asynchronously, one at a time, in commit order; a + * rejection is contained and logged like a sync throw. * @param callback - invoked after each commit with the next and previous values. * @returns the disposer removing this observer. */ diff --git a/docs/core-data-structures/settings.zh.md b/docs/core-data-structures/settings.zh.md index 955c4fbf7c..c746e3cc18 100644 --- a/docs/core-data-structures/settings.zh.md +++ b/docs/core-data-structures/settings.zh.md @@ -46,8 +46,9 @@ interface SettingsScope { /** Current resolved value: schema defaults, then `base`, then the user layer. */ get(): T /** - * Observe committed changes to this namespace's resolved value. A callback - * may be async; a rejection is contained and logged like a sync throw. + * Observe committed changes to this namespace's resolved value. Invocations + * of one callback run asynchronously, one at a time, in commit order; a + * rejection is contained and logged like a sync throw. * @param callback - invoked after each commit with the next and previous values. * @returns the disposer removing this observer. */ diff --git a/docs/event-producer-consumer.md b/docs/event-producer-consumer.md index e3eb1d9435..74645a1674 100644 --- a/docs/event-producer-consumer.md +++ b/docs/event-producer-consumer.md @@ -35,7 +35,7 @@ This matrix shows which packages dispatch each harness-owned event and which pac | `session/disposed` | `emit` | [`packages/core/session/src/index.ts:80`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`agent-loop`](../packages/core/agent-loop), `apiproxy`, [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title) | | `session/event` | `emit` | [`packages/core/session/src/index.ts:92`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`acp`](../packages/acp/acp), `apiproxy`, [`cli-demo`](../packages/examples/cli-demo), [`compact`](../packages/compact/compact), [`compact-basic`](../packages/compact/compact-basic), [`goal`](../packages/goal/goal), [`goal-session`](../packages/goal/goal-session), [`hook-protocol`](../packages/hooks/hook-protocol), [`jsonrpc`](../packages/ui/jsonrpc), [`plan-mode`](../packages/plan/plan-mode), [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title), [`token-meter`](../packages/llm/token-meter), [`tools`](../packages/core/tools), [`tui`](../packages/ui/tui), [`user-approval`](../packages/ui/user-approval), [`workspace-context`](../packages/context/workspace-context) | | `session/flush` | `parallel` | [`packages/core/session/src/index.ts:102`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry) | -| `settings/updated` | `emit` | [`packages/settings/settings/src/index.ts:96`](../packages/settings/settings/src/index.ts) | [`settings`](../packages/settings/settings) (`emit`) | [`settings`](../packages/settings/settings) | +| `settings/updated` | `emit` | [`packages/settings/settings/src/index.ts:97`](../packages/settings/settings/src/index.ts) | [`settings`](../packages/settings/settings) (`events.dispatch`) | [`settings`](../packages/settings/settings) | | `slash/input-begin-command` | `bail` | [`packages/client/ui-slash/src/types.ts:230`](../packages/client/ui-slash/src/types.ts) | - | `ui-conversation` | | `slash/input-consume-token` | `bail` | [`packages/client/ui-slash/src/types.ts:244`](../packages/client/ui-slash/src/types.ts) | - | `ui-conversation` | | `slash/input-insert-reference` | `bail` | [`packages/client/ui-slash/src/types.ts:237`](../packages/client/ui-slash/src/types.ts) | - | `ui-conversation` | diff --git a/packages/settings/settings-local/README.i18n.yaml b/packages/settings/settings-local/README.i18n.yaml index 455255941a..5d44f50f9d 100644 --- a/packages/settings/settings-local/README.i18n.yaml +++ b/packages/settings/settings-local/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/settings/settings-local/README.md -README.md: 9d0aa3982507ca16b382aaa918ca1536a98e29ea -README.zh.md: 075de7ee5b3e0ef0a4ee27eeb8cb098ca0d73456 +README.md: af8df7c030757b330e034a1c46507fbe75c9bab8 +README.zh.md: fc8943263b339baad1a92a1d0b0977b926e40f6e diff --git a/packages/settings/settings-local/README.md b/packages/settings/settings-local/README.md index 9d0aa39825..af8df7c030 100644 --- a/packages/settings/settings-local/README.md +++ b/packages/settings/settings-local/README.md @@ -19,6 +19,7 @@ Defaulting is one explicit `resolveSpec(config)` step; an unsupported extension - **Boot fails loud, reload keeps last-good.** An existing-but-invalid document fails plugin load; once live, an unreadable or unparsable edit warns and keeps the last good sections. A missing document resolves every namespace from defaults and `base`; deleting it publishes the same empty state. - **Write-back is atomic, owner-only, and symlink-proof.** `persist` exclusive-creates a random-suffix temp sibling with mode `0600` (`wx` refuses to follow a planted symlink) and renames over the target, cleaning the temp up on failure. YAML writes patch one namespace in the comment-preserving document; JSON re-serializes. +- **Cross-namespace writes serialize on one document.** Every namespace shares the file, so persists from different namespace queues chain internally; each render sees the text the previous write committed. - **Dispose quiesces.** Teardown stops accepting watcher events, closes the watcher, then waits out any queued or in-flight reload, so nothing publishes after disposal. - **Self-write suppression by content.** The provider caches the last good text; a watcher event whose content equals the cache (its own write included) is a no-op. diff --git a/packages/settings/settings-local/README.zh.md b/packages/settings/settings-local/README.zh.md index 075de7ee5b..fc8943263b 100644 --- a/packages/settings/settings-local/README.zh.md +++ b/packages/settings/settings-local/README.zh.md @@ -19,6 +19,7 @@ - **启动报错响亮,重载保留最后可用值。** 存在但非法的文档使插件加载失败;运行中不可读或不可解析的编辑只告警并保留最后可用分节。文档缺失时所有 namespace 按默认值与 `base` 解析;删除文档发布同样的空状态。 - **写回原子、仅属主可读、抗符号链接。** `persist` 以 `0600` 权限独占创建随机后缀临时同级文件(`wx` 拒绝跟随预埋符号链接)后 rename 覆盖目标,失败时清理临时文件。YAML 写回在保留注释的文档里只修补目标 namespace;JSON 重新序列化。 +- **跨 namespace 写入在同一文档上串行。** 所有 namespace 共享一个文件,来自不同 namespace 队列的 persist 在内部串联;每次渲染都基于上一次写入提交后的文本。 - **Dispose 保证静止。** 卸载先停止接收 watcher 事件、关闭 watcher,再等完排队与进行中的重载,之后不再有任何发布。 - **按内容抑制自写。** provider 缓存最后可用文本;watcher 事件内容与缓存相同(含自己的写入)即为 no-op。 diff --git a/packages/settings/settings-local/src/index.ts b/packages/settings/settings-local/src/index.ts index 2c020da63c..b305f61fe7 100644 --- a/packages/settings/settings-local/src/index.ts +++ b/packages/settings/settings-local/src/index.ts @@ -87,6 +87,8 @@ export class SettingsLocal extends Settings { private text: string | undefined /** Serializes watcher-triggered reloads so reads never interleave. */ private refreshTask: Promise = Promise.resolve() + /** Serializes whole-document writes across namespace queues; settled tail. */ + private persistChain: Promise = Promise.resolve() /** Set at dispose: refuse new watcher events and let in-flight work no-op. */ private closed = false @@ -121,7 +123,17 @@ export class SettingsLocal extends Settings { return doc } - protected async persist(ns: SettingsNamespace, section: Record): Promise { + protected persist(ns: SettingsNamespace, section: Record): Promise { + // One document backs every namespace, so writes from different namespace + // queues must serialize here: each render must see the text the previous + // write committed, or the loser's section silently vanishes from disk. + // The stored tail is settled on both outcomes, so chaining needs no catch. + const task = this.persistChain.then(() => this.persistSection(ns, section)) + this.persistChain = task.then(() => undefined, () => undefined) + return task + } + + private async persistSection(ns: SettingsNamespace, section: Record): Promise { const output = this.spec.format === 'yaml' ? this.renderYaml(ns, section) : this.renderJson(ns, section) diff --git a/packages/settings/settings-local/tests/loader-composition.spec.ts b/packages/settings/settings-local/tests/loader-composition.spec.ts index d584c11899..c7cea89e41 100644 --- a/packages/settings/settings-local/tests/loader-composition.spec.ts +++ b/packages/settings/settings-local/tests/loader-composition.spec.ts @@ -1,7 +1,9 @@ /** * Real-composition guard: the provider and a consumer plugin boot from a - * test-only cordis.yml through the actual Loader + Include path, and an - * external edit of settings.yaml hot-publishes into the consumer's scope. + * test-only cordis.yml through the actual Loader + Include path, an external + * edit of settings.yaml hot-publishes into the consumer's scope, and the same + * consumer booted WITHOUT a settings entry keeps its entry-config resolution — + * the documented optional-inject fallback. */ import { mkdtemp, rm, writeFile } from 'node:fs/promises' @@ -39,33 +41,50 @@ afterEach(async () => { interface ConsumerState { scope: SettingsScope | undefined seen: ThemeConfig[] + /** What the consumer is actually running with, settings or not. */ + applied: ThemeConfig | undefined } -async function loadComposition(): Promise<{ ctx: Context; state: ConsumerState; settingsPath: string }> { +async function loadComposition( + options?: { withSettings?: boolean }, +): Promise<{ ctx: Context; state: ConsumerState; settingsPath: string }> { + const withSettings = options?.withSettings ?? true root = await mkdtemp(join(tmpdir(), 'dsh-settings-composition-')) const settingsPath = join(root, 'settings.yaml') await writeFile(settingsPath, 'ui-theme:\n theme: light\n') - const state: ConsumerState = { scope: undefined, seen: [] } + const state: ConsumerState = { scope: undefined, seen: [], applied: undefined } const consumer = { name: 'settings-consumer', - inject: ['settings'], apply: (ctx: Context) => { - const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema, { - base: { fontSize: 16 }, + // The documented consumer shape: no hard dependency — entry config alone + // is the running state, and the scoped inject overlays the user layer + // only while a settings service exists. + const base: Partial = { fontSize: 16 } + state.applied = ThemeSchema(base as ThemeConfig) + ctx.inject(['settings'], (child: Context) => { + const scope = child.settings.register(settingsNamespace('ui-theme'), ThemeSchema, { base }) + state.scope = scope + state.applied = scope.get() + scope.watch((next) => { + state.seen.push(next) + state.applied = next + }) }) - state.scope = scope - scope.watch((next) => { state.seen.push(next) }) }, } const configPath = join(root, 'cordis.yml') await writeFile(configPath, [ - '- id: settings', - " name: '@deepseek-ai/dsh-settings-local'", - ' config:', - ` path: ${JSON.stringify(settingsPath)}`, - ' debounceMs: 10', + ...withSettings + ? [ + '- id: settings', + " name: '@deepseek-ai/dsh-settings-local'", + ' config:', + ` path: ${JSON.stringify(settingsPath)}`, + ' debounceMs: 10', + ] + : [], '- id: consumer', ' name: test-settings-consumer', '', @@ -100,7 +119,9 @@ describe('settings-local real composition', () => { const { ctx, state, settingsPath } = await loadComposition() // Composition resolution: user layer over the consumer's composition base. - expect(state.scope!.get()).toEqual({ theme: 'light', fontSize: 16 }) + await vi.waitFor(() => { + expect(state.scope!.get()).toEqual({ theme: 'light', fontSize: 16 }) + }) expect(ctx.get('settings')!.describe().map(entry => entry.ns)).toEqual(['ui-theme']) await writeFile(settingsPath, 'ui-theme:\n theme: dark\n fontSize: 20\n') @@ -109,4 +130,16 @@ describe('settings-local real composition', () => { }, { timeout: 5000 }) expect(state.seen.at(-1)).toEqual({ theme: 'dark', fontSize: 20 }) }) + + it('boots the same consumer without a settings entry and keeps entry-config resolution', async () => { + const { ctx, state } = await loadComposition({ withSettings: false }) + + // No settings service anywhere in the composition… + expect(ctx.get('settings')).toBeUndefined() + // …so the consumer runs on schema defaults plus its composition base, and + // never receives a scope. + expect(state.applied).toEqual({ theme: 'dark', fontSize: 16 }) + expect(state.scope).toBeUndefined() + expect(state.seen).toEqual([]) + }) }) diff --git a/packages/settings/settings-local/tests/local.spec.ts b/packages/settings/settings-local/tests/local.spec.ts index 1c74429153..4c3c24ccd9 100644 --- a/packages/settings/settings-local/tests/local.spec.ts +++ b/packages/settings/settings-local/tests/local.spec.ts @@ -146,6 +146,23 @@ describe('persist', () => { expect((await readdir(dir)).sort()).toEqual(['settings.yaml']) }) + it('serializes cross-namespace writes into one on-disk document', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, watch: false }) + const alpha = ctx.settings.register(settingsNamespace('alpha'), ThemeSchema) + const beta = ctx.settings.register(settingsNamespace('beta'), ThemeSchema) + await Promise.all([ + alpha.update({ theme: 'light' }), + beta.update({ fontSize: 20 }), + ]) + const text = await readFile(path, 'utf8') + expect(text).toContain('alpha:') + expect(text).toContain('beta:') + expect(alpha.get().theme).toBe('light') + expect(beta.get().fontSize).toBe(20) + }) + it('never follows a planted symlink at a temp path and never leaves the document a symlink', async () => { const dir = await tempDir() const path = join(dir, 'settings.yaml') @@ -209,6 +226,9 @@ describe('persist', () => { await chmod(dir, 0o700) expect((await readdir(dir)).sort()).toEqual(['settings.yaml']) expect(scope.get().theme).toBe('light') + // The failed persist must not poison the document write chain. + await scope.update({ theme: 'dark' }) + expect(scope.get().theme).toBe('dark') }) it('round-trips a json document', async () => { diff --git a/packages/settings/settings/README.i18n.yaml b/packages/settings/settings/README.i18n.yaml index 6f5e5c6beb..63a274dd4d 100644 --- a/packages/settings/settings/README.i18n.yaml +++ b/packages/settings/settings/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/settings/settings/README.md -README.md: f7858a247f6011cd0654a73b5325d81c118441e5 -README.zh.md: 67ecba695066389bfe3a69f517d52f91b48b6c75 +README.md: ff6cdeb57a265dbaa9d5f50de1d558f1e3cb581f +README.zh.md: d820a5c1fa804455c439f1a155e7628f5118a49b diff --git a/packages/settings/settings/README.md b/packages/settings/settings/README.md index f7858a247f..ff6cdeb57a 100644 --- a/packages/settings/settings/README.md +++ b/packages/settings/settings/README.md @@ -11,7 +11,8 @@ Abstract user-settings seam (`ctx.settings`). One provider holds a raw document - `get(ns)` — resolved value, `undefined` while unregistered. - `update(ns, patch)` — deep-merges the plain-object patch into the user section only (never the `base`), validates the resolved candidate, persists through the provider, then commits. Validation failure rejects before anything is persisted; a read-only provider (`writable: false`) rejects every write. Writes to one namespace are serialized in call order. - `replace(ns, section)` — sets the user section wholesale: the removal/reset path a merge cannot express (`replace({})` re-inherits `base` and schema defaults). -- Resolved values are deep-frozen snapshots; watchers receive `(next, prev)` after each commit, and watcher failures — sync throws and async rejections alike — are contained. +- Resolved values are deep-frozen snapshots. Watchers receive `(next, prev)` after each commit: invocations of one callback run asynchronously, one at a time, in commit order (a slow stale invocation can never apply after a newer one), and failures — sync throws and async rejections alike — are contained. The `settings/updated` event fans out one listener at a time, so one throwing listener cannot starve the rest. +- Service teardown refuses new writes and drains every queued write before disposal completes; a write whose registrant fiber was disposed mid-flight still reaches storage but commits and notifies nobody. ## Provider contract diff --git a/packages/settings/settings/README.zh.md b/packages/settings/settings/README.zh.md index 67ecba6950..d820a5c1fa 100644 --- a/packages/settings/settings/README.zh.md +++ b/packages/settings/settings/README.zh.md @@ -11,7 +11,8 @@ - `get(ns)` — 解析值;未注册时为 `undefined`。 - `update(ns, patch)` — 把普通对象 patch 深合并进用户分节(绝不合并进 `base`),校验解析候选值,经 provider 持久化后提交。校验失败在持久化前拒绝;只读 provider(`writable: false`)拒绝一切写入。同一 namespace 的写入按调用顺序串行。 - `replace(ns, section)` — 整体替换用户分节:merge 表达不了的删除/重置路径(`replace({})` 重新继承 `base` 与 schema 默认值)。 -- 解析值是深冻结快照;每次提交后观察者收到 `(next, prev)`;观察者异常——同步抛出与异步拒绝——均被隔离。 +- 解析值是深冻结快照。每次提交后观察者收到 `(next, prev)`:同一回调的调用异步、逐次、按提交顺序执行(慢的旧调用绝不会覆盖更新的结果),异常——同步抛出与异步拒绝——均被隔离。`settings/updated` 事件逐 listener 扇出,一个抛错的 listener 不会饿死其余 listener。 +- 服务卸载先拒绝新写入并排干全部排队写入后才完成;registrant fiber 在写入途中被 dispose 时,该写入仍到达存储,但不向任何人提交或通知。 ## Provider 契约 diff --git a/packages/settings/settings/src/index.ts b/packages/settings/settings/src/index.ts index 10b21c2154..a7e9366048 100644 --- a/packages/settings/settings/src/index.ts +++ b/packages/settings/settings/src/index.ts @@ -58,8 +58,9 @@ export interface SettingsScope { /** Current resolved value: schema defaults, then `base`, then the user layer. */ get(): T /** - * Observe committed changes to this namespace's resolved value. A callback - * may be async; a rejection is contained and logged like a sync throw. + * Observe committed changes to this namespace's resolved value. Invocations + * of one callback run asynchronously, one at a time, in commit order; a + * rejection is contained and logged like a sync throw. * @param callback - invoked after each commit with the next and previous values. * @returns the disposer removing this observer. */ @@ -149,6 +150,13 @@ function deepFreeze(value: T): T { return Object.freeze(value) } +/** One registered watcher and its serialized invocation chain. */ +interface SettingsWatcher { + callback: (next: never, prev: never) => void | Promise + /** Settled tail: invocations of this callback run one at a time, in commit order. */ + tail: Promise +} + /** One live namespace registration owned by a registrant fiber. */ interface SettingsRegistration { ns: SettingsNamespace @@ -156,7 +164,7 @@ interface SettingsRegistration { base: unknown applies: SettingsApplies resolved: unknown - watchers: Set<(next: never, prev: never) => void | Promise> + watchers: Set } /** @@ -171,6 +179,13 @@ export abstract class Settings extends Service { private document: Record = {} /** Per-namespace write chains; settled tails, so a failure never poisons the queue. */ private readonly writeQueues = new Map>() + /** Set at service dispose: refuse new writes while queued ones drain. */ + private stopped = false + + /** Opaque read of {@link stopped}: control flow cannot narrow it across awaits. */ + private isStopped(): boolean { + return this.stopped + } constructor(ctx: Context) { super(ctx, 'settings') @@ -178,10 +193,17 @@ export abstract class Settings extends Service { /** * Load the provider's document once and publish it before the service - * becomes injectable. Providers with their own init (watchers, connections) - * delegate here first via `yield* super[Service.init]()`. + * becomes injectable, and register the write-drain teardown. Providers with + * their own init (watchers, connections) delegate here first via + * `yield* super[Service.init]()`; their disposers then run before the drain. */ - async* [Service.init](): AsyncGenerator<() => void, void, void> { + async* [Service.init](): AsyncGenerator<() => Promise | void, void, void> { + yield async () => { + // Teardown: refuse new writes, then wait until every queued write chain + // settles so disposal completes only once storage is quiescent. + this.stopped = true + await Promise.allSettled([...this.writeQueues.values()]) + } this.publish(await this.load()) } @@ -230,8 +252,9 @@ export abstract class Settings extends Service { return { get: () => registration.resolved as T, watch: (callback) => { - registration.watchers.add(callback) - return () => registration.watchers.delete(callback) + const watcher: SettingsWatcher = { callback: callback, tail: Promise.resolve() } + registration.watchers.add(watcher) + return () => registration.watchers.delete(watcher) }, update: patch => this.update(ns, patch), replace: section => this.replace(ns, section), @@ -287,27 +310,50 @@ export abstract class Settings extends Service { /** Validate a write, then queue it on the namespace's serialized write chain. */ private write(ns: SettingsNamespace, input: object, mode: 'merge' | 'replace'): Promise { + const verb = mode === 'merge' ? 'update' : 'replace' const registration = this.registrations.get(ns) if (registration === undefined) { throw new Error(`settings namespace "${ns}" is not registered`) } + if (this.isStopped()) { + throw new Error(`settings service is disposed: "${ns}" cannot be written`) + } if (!this.writable) { throw new Error(`settings provider is read-only: "${ns}" cannot be updated in-process`) } if (!isPlainObject(input)) { - throw new TypeError(`settings ${mode === 'merge' ? 'update' : 'replace'} for "${ns}" must be a plain object`) + throw new TypeError(`settings ${verb} for "${ns}" must be a plain object`) + } + // Snapshot at call time: the queue must never read a caller-owned object + // the caller may keep mutating while the write waits its turn. + let snapshot: Record + try { + snapshot = structuredClone(input) + } catch { + throw new TypeError(`settings ${verb} for "${ns}" must be JSON-shaped (structured-cloneable) data`) } const previous = this.writeQueues.get(ns) ?? Promise.resolve() // Chain past a failed predecessor: one rejected write must not poison the // namespace queue for every later caller. const run = previous.catch(() => undefined).then(async () => { + if (this.isStopped()) { + throw new Error(`settings service was disposed before the queued "${ns}" ${verb} ran`) + } + if (this.registrations.get(ns) !== registration) { + throw new Error(`settings namespace "${ns}" registration was disposed before the queued ${verb} ran`) + } const section = mode === 'merge' - ? mergeLayers(this.section(ns) ?? {}, input) as Record - : structuredClone(input) + ? mergeLayers(this.section(ns) ?? {}, snapshot) as Record + : snapshot const next = deepFreeze(this.resolve(registration.schema, registration.base, section)) await this.persist(ns, section) + // The write reached storage either way; the cache must say so. Commit + // only when this registration is still the namespace owner — a fiber + // disposed (or replaced) mid-persist must not receive the notification. this.document[ns] = section - this.commit(registration, next, 'update') + if (this.registrations.get(ns) === registration && !this.isStopped()) { + this.commit(registration, next, 'update') + } }) this.writeQueues.set(ns, run) return run @@ -358,29 +404,35 @@ export abstract class Settings extends Service { if (deepEqualJson(next, prev)) return registration.resolved = next for (const watcher of [...registration.watchers]) { + // Serialize per watcher: invocations of one callback run one at a time + // in commit order, so a slow stale invocation can never apply after a + // newer one. Sync throws and async rejections land in the same handler. + watcher.tail = watcher.tail + .then(() => watcher.callback(next as never, prev as never)) + .then(() => undefined, (error: unknown) => { + this.warnWatcherFailure(registration.ns, error) + }) + } + // Fan the event out one listener at a time (the plain emit stops at the + // first throwing listener, starving the rest). Invariant violations are + // harness-fatal by design and rethrow after every listener ran; any other + // failure is contained so one broken observer cannot wedge the commit + // path (and, through it, a provider's reload loop). + let invariantFailure: unknown + const args = ['settings/updated', registration.ns, next, prev, source] + for (const listener of this.ctx.events.dispatch('emit', args) as Array<(...listenerArgs: unknown[]) => unknown>) { try { - // A watcher may be async: adopt its promise so a rejection is contained - // here instead of surfacing as an unhandled rejection. - const outcome = watcher(next as never, prev as never) as unknown - if (outcome instanceof Promise) { - outcome.catch((error: unknown) => { - this.warnWatcherFailure(registration.ns, error) - }) - } + listener(registration.ns, next, prev, source) } catch (error) { - this.warnWatcherFailure(registration.ns, error) + if ((error as { code?: unknown } | null)?.code === 'INVARIANT') { + invariantFailure ??= error + continue + } + this.ctx.logger.warn('settings: a settings/updated listener for "%s" failed', registration.ns) + this.ctx.logger.warn(error) } } - try { - this.ctx.emit('settings/updated', registration.ns, next, prev, source) - } catch (error) { - // Invariant violations are harness-fatal by design; any other listener - // failure is contained so one broken observer cannot wedge the commit - // path (and, through it, a provider's reload loop). - if ((error as { code?: unknown } | null)?.code === 'INVARIANT') throw error - this.ctx.logger.warn('settings: a settings/updated listener for "%s" failed', registration.ns) - this.ctx.logger.warn(error) - } + if (invariantFailure !== undefined) throw invariantFailure as Error } /** Contained-watcher diagnostic shared by the sync and async failure paths. */ diff --git a/packages/settings/settings/tests/settings.spec.ts b/packages/settings/settings/tests/settings.spec.ts index c413e948ed..a989d9a5cc 100644 --- a/packages/settings/settings/tests/settings.spec.ts +++ b/packages/settings/settings/tests/settings.spec.ts @@ -52,9 +52,10 @@ const NestedSchema: z = z.object({ async function boot(options?: ConstructorParameters[1]) { const ctx = new Context() - await ctx.plugin(MemorySettings, options) + const fiber = ctx.plugin(MemorySettings, options) + await fiber const provider = ctx.get('settings') as MemorySettings - return { ctx, provider } + return { ctx, provider, fiber } } /** Record every settings/updated emission. */ @@ -341,6 +342,144 @@ describe('review regressions', () => { }) }) +describe('second review regressions', () => { + it('runs every settings/updated listener even when an earlier one throws', async () => { + const { ctx, provider } = await boot() + ctx.on('settings/updated', () => { + throw new Error('first listener boom') + }) + const second = vi.fn() + ctx.on('settings/updated', second) + ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + expect(second).toHaveBeenCalledTimes(1) + }) + + it('rejects an update queued after the registrant fiber disposed', async () => { + const { ctx } = await boot() + let scope: SettingsScope | undefined + const fiber = ctx.plugin({ + inject: ['settings'], + apply: (child: Context) => { + scope = child.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + }, + }) + await fiber + await fiber.dispose() + await expect(scope!.update({ theme: 'light' })).rejects.toThrow(/disposed|not registered/) + }) + + it('does not notify a registrant disposed while its update was in flight', async () => { + const { ctx, provider } = await boot({ persistDelayMs: 30 }) + const events = recordUpdates(ctx) + let scope: SettingsScope | undefined + const watcher = vi.fn() + const fiber = ctx.plugin({ + inject: ['settings'], + apply: (child: Context) => { + scope = child.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + scope.watch(watcher) + }, + }) + await fiber + const pending = scope!.update({ theme: 'light' }) + await new Promise(resolve => setTimeout(resolve, 5)) + await fiber.dispose() + await pending.catch(() => undefined) + await new Promise(resolve => setTimeout(resolve, 10)) + expect(watcher).not.toHaveBeenCalled() + expect(events).toEqual([]) + // The persist was already in flight, so storage keeps the write — but no + // commit reached the disposed registration. + expect(provider.doc['ui-theme']).toEqual({ theme: 'light' }) + }) + + it('drains in-flight writes at service dispose and rejects later ones', async () => { + const { ctx, provider, fiber } = await boot({ persistDelayMs: 20 }) + const service = ctx.settings + const scope = service.register(settingsNamespace('ui-theme'), ThemeSchema) + const pending = scope.update({ theme: 'light' }) + await new Promise(resolve => setTimeout(resolve, 5)) + await fiber.dispose() + // The teardown drained the in-flight write before completing… + await pending.catch(() => undefined) + const persistedAtDispose = provider.persisted.length + expect(persistedAtDispose).toBe(1) + // …and afterwards nothing writes and new writes reject. + await expect(service.update(settingsNamespace('ui-theme'), { theme: 'dark' })) + .rejects.toThrow(/disposed|not registered/) + await new Promise(resolve => setTimeout(resolve, 40)) + expect(provider.persisted.length).toBe(persistedAtDispose) + }) + + it('serializes invocations of one async watcher in commit order', async () => { + const { ctx, provider } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const applied: number[] = [] + let firstCall = true + scope.watch(async (next) => { + // The first (stale) invocation is slow; unserialised it would finish + // last and clobber the newer applied state. + const delay = firstCall ? 30 : 0 + firstCall = false + await new Promise(resolve => setTimeout(resolve, delay)) + applied.push(next.fontSize) + }) + provider.pushExternal({ 'ui-theme': { fontSize: 1 } }) + provider.pushExternal({ 'ui-theme': { fontSize: 2 } }) + await vi.waitFor(() => { + expect(applied).toHaveLength(2) + }) + expect(applied).toEqual([1, 2]) + }) + + it('rejects a plain object that is not structured-cloneable', async () => { + const { ctx } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await expect(scope.update({ theme: () => 'dark' })) + .rejects.toThrow(/JSON-shaped/) + }) + + it('rejects a write still queued when the service disposes', async () => { + const { ctx, fiber } = await boot({ persistDelayMs: 20 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const first = scope.update({ theme: 'light' }) + const second = scope.update({ fontSize: 20 }) + await new Promise(resolve => setTimeout(resolve, 5)) + await fiber.dispose() + await first + await expect(second).rejects.toThrow(/disposed before the queued/) + }) + + it('rejects a write still queued when the registrant disposes', async () => { + const { ctx } = await boot({ persistDelayMs: 20 }) + let scope: SettingsScope | undefined + const fiber = ctx.plugin({ + inject: ['settings'], + apply: (child: Context) => { + scope = child.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + }, + }) + await fiber + const first = scope!.update({ theme: 'light' }) + const second = scope!.update({ fontSize: 20 }) + await new Promise(resolve => setTimeout(resolve, 5)) + await fiber.dispose() + await first + await expect(second).rejects.toThrow(/registration was disposed before the queued/) + }) + + it('snapshots the patch at call time so caller mutation cannot leak in', async () => { + const { ctx } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const patch = { fontSize: 18 } + const pending = scope.update(patch) + patch.fontSize = 99 + await pending + expect(scope.get().fontSize).toBe(18) + }) +}) + describe('publish', () => { it('notifies watchers of an external change with source provider', async () => { const { ctx, provider } = await boot() @@ -349,10 +488,12 @@ describe('publish', () => { const watcher = vi.fn() scope.watch(watcher) provider.pushExternal({ 'ui-theme': { theme: 'light' } }) - expect(watcher).toHaveBeenCalledWith( - { theme: 'light', fontSize: 14 }, - { theme: 'dark', fontSize: 14 }, - ) + await vi.waitFor(() => { + expect(watcher).toHaveBeenCalledWith( + { theme: 'light', fontSize: 14 }, + { theme: 'dark', fontSize: 14 }, + ) + }) expect(events[0]!.source).toBe('provider') }) @@ -410,7 +551,9 @@ describe('watch', () => { const second = vi.fn() scope.watch(second) provider.pushExternal({ 'ui-theme': { theme: 'light' } }) - expect(second).toHaveBeenCalledTimes(1) + await vi.waitFor(() => { + expect(second).toHaveBeenCalledTimes(1) + }) expect(events).toHaveLength(1) expect(scope.get()).toEqual({ theme: 'light', fontSize: 14 }) }) From 42de60347acbf5953747f4ad4208f5bb53011a9b Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Wed, 29 Jul 2026 10:26:29 +0800 Subject: [PATCH 04/17] docs: raise packages/README.md budget ceiling to 850 The group table legitimately gained one row for the new settings group; the row itself is already condensed to the minimum. The intended raise missed the merge commit because a pipeline swallowed the failing edit's exit status. --- scripts/doc-budgets.manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/doc-budgets.manifest.json b/scripts/doc-budgets.manifest.json index 80766e314d..48a570a20d 100644 --- a/scripts/doc-budgets.manifest.json +++ b/scripts/doc-budgets.manifest.json @@ -7,5 +7,5 @@ "docs/testing.md": 1100, "examples/AGENTS.md": 310, "packages/AGENTS.md": 675, - "packages/README.md": 845 + "packages/README.md": 850 } From 8cec74748bb4b894a7a2c139b0cd227d232ecaa1 Mon Sep 17 00:00:00 2001 From: creatixchu Date: Wed, 29 Jul 2026 18:34:06 +0800 Subject: [PATCH 05/17] feat(host): adaptive directory-picker default via -auto chooser Add @deepseek-ai/dsh-host-directory-picker-auto, a node-half-only chooser that samples the host situation once at boot (bind host via a new httpServer.host getter, SSH markers, platform, DISPLAY/WAYLAND_DISPLAY) and mounts the matching dual-face backend (-native or -browse) as a real Loader entry in the in-memory root tree; the effect disposer removes it. Entry-level mounting keeps the seam's one-row-swaps-both-faces invariant: the client module table discovers the mounted backend's browser half exactly as a config row's. apps/cli now composes -auto as its directory-picker row; composing a backend row directly remains the pin. --- ...directory-picker-capability-seam.i18n.yaml | 4 +- ...-07-28-directory-picker-capability-seam.md | 2 +- ...-28-directory-picker-capability-seam.zh.md | 2 +- ...irectory-picker-adaptive-default.i18n.yaml | 6 + ...07-29-directory-picker-adaptive-default.md | 29 ++++ ...29-directory-picker-adaptive-default.zh.md | 29 ++++ apps/cli/cordis.yml | 13 +- apps/cli/package.json | 1 + docs/config-catalog.md | 1 + packages/host/README.i18n.yaml | 4 +- packages/host/README.md | 1 + packages/host/README.zh.md | 1 + .../directory-picker-auto/README.i18n.yaml | 6 + packages/host/directory-picker-auto/README.md | 20 +++ .../host/directory-picker-auto/README.zh.md | 20 +++ .../host/directory-picker-auto/package.json | 47 ++++++ .../host/directory-picker-auto/src/index.ts | 52 +++++++ .../directory-picker-auto/src/invariant.ts | 25 ++++ .../host/directory-picker-auto/src/resolve.ts | 46 ++++++ .../tests/loader-composition.spec.ts | 139 ++++++++++++++++++ .../tests/resolve.spec.ts | 37 +++++ .../host/directory-picker-auto/tsconfig.json | 27 ++++ .../host/directory-picker/README.i18n.yaml | 4 +- packages/host/directory-picker/README.md | 2 +- packages/host/directory-picker/README.zh.md | 2 +- packages/host/webserver/README.i18n.yaml | 4 +- packages/host/webserver/README.md | 2 +- packages/host/webserver/README.zh.md | 2 +- packages/host/webserver/src/index.ts | 5 + pnpm-lock.yaml | 30 ++++ .../verify-package-readme-model-experience.ts | 1 + tsconfig.host.json | 8 + 32 files changed, 553 insertions(+), 19 deletions(-) create mode 100644 .agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.i18n.yaml create mode 100644 .agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md create mode 100644 .agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.zh.md create mode 100644 packages/host/directory-picker-auto/README.i18n.yaml create mode 100644 packages/host/directory-picker-auto/README.md create mode 100644 packages/host/directory-picker-auto/README.zh.md create mode 100644 packages/host/directory-picker-auto/package.json create mode 100644 packages/host/directory-picker-auto/src/index.ts create mode 100644 packages/host/directory-picker-auto/src/invariant.ts create mode 100644 packages/host/directory-picker-auto/src/resolve.ts create mode 100644 packages/host/directory-picker-auto/tests/loader-composition.spec.ts create mode 100644 packages/host/directory-picker-auto/tests/resolve.spec.ts create mode 100644 packages/host/directory-picker-auto/tsconfig.json diff --git a/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.i18n.yaml b/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.i18n.yaml index bb9425fa64..0500ccfb3f 100644 --- a/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.i18n.yaml +++ b/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write .agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.md -2026-07-28-directory-picker-capability-seam.md: 7c8f8cb67690cb4c5858cefb52b8cd79e649ec38 -2026-07-28-directory-picker-capability-seam.zh.md: 05545fc3cd758523814b31afa705249972d86464 +2026-07-28-directory-picker-capability-seam.md: c44717d46445a992e563497ed011930a6044a1bb +2026-07-28-directory-picker-capability-seam.zh.md: 42fedf64ba97fed032c4847b68ce321ae32dc463 diff --git a/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.md b/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.md index 7c8f8cb676..c44717d464 100644 --- a/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.md +++ b/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.md @@ -33,7 +33,7 @@ Placement and policy rulings folded into this decision: ## Consequences -- `cordis.yml` chooses the interaction; `apps/cli` mounts `-browse` (the shipped default — remote-capable picking out of the box), one row having swapped backend and UI together; `-native` remains the host-display alternative. +- `cordis.yml` chooses the interaction; `apps/cli` mounts the [`-auto` chooser](../feature/2026-07-29-directory-picker-adaptive-default.md), which resolves the host's situation at boot and mounts `-native` or `-browse` itself, one row still swapping backend and UI together; composing a backend row directly pins the interaction. - The wire gains `host.listDirectory`/`host.createDirectory` and four error codes; the connection fixture serves a deterministic browse tree and a deterministic `pickDirectory` path for keyless assembled tests. - A future interaction (or an Electron provider of the `native` interaction) is one dual-face backend package — no gateway surgery, no ui-workspace edits. - `ApiProxyDefaults.pickDirectory` (test-only injection) is gone; tests provide a stub `ctx.directoryPicker` like any other service. diff --git a/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.zh.md b/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.zh.md index 05545fc3cd..42fedf64ba 100644 --- a/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.zh.md +++ b/.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.zh.md @@ -33,7 +33,7 @@ web GUI 的"打开本地文件夹"流程被焊死在一种交互上:`host.pick ## 后果 -- `cordis.yml` 决定交互形态;`apps/cli` 挂 `-browse`(随附默认——开箱即得可远程的选取),一行同时切换了后端与 UI;`-native` 仍是宿主屏幕方案。 +- `cordis.yml` 决定交互形态;`apps/cli` 挂 [`-auto` 选择器](../feature/2026-07-29-directory-picker-adaptive-default.md),它在启动时判定宿主处境并自行挂载 `-native` 或 `-browse`,一行仍同时切换后端与 UI;直接组合某个后端行即固定交互。 - 协议新增 `host.listDirectory`/`host.createDirectory` 与四个错误码;connection fixture 提供确定性浏览树与确定性 `pickDirectory` 路径供无密钥组装测试使用。 - 未来的新交互(或提供 `native` 交互的 Electron 实现)只是一个双面后端包——无需网关手术,也不动 ui-workspace。 - `ApiProxyDefaults.pickDirectory`(仅测试注入)删除;测试像提供其他服务一样提供 stub `ctx.directoryPicker`。 diff --git a/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.i18n.yaml b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.i18n.yaml new file mode 100644 index 0000000000..cd921886f6 --- /dev/null +++ b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.i18n.yaml @@ -0,0 +1,6 @@ +# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each +# side as of the last confirmed-consistent state. Both languages carry equal authority; +# after editing either side, bring the other along and re-record with: +# pnpm run verify-translation-pairing --write .agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md +2026-07-29-directory-picker-adaptive-default.md: ae5748259f162503300360d6cca43bec996afdb6 +2026-07-29-directory-picker-adaptive-default.zh.md: acabeb80604b473289fa441e6d9d913e4a8f87d0 diff --git a/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md new file mode 100644 index 0000000000..ae5748259f --- /dev/null +++ b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md @@ -0,0 +1,29 @@ +# Agent Note: Adaptive default for the directory-picker interaction + +Status: implemented + +English | [中文](2026-07-29-directory-picker-adaptive-default.zh.md) + +## Problem + +The [directory-picker seam](../architecture/2026-07-28-directory-picker-capability-seam.md) made the interaction a `cordis.yml` swap point, but the shipped composition still had to pin one backend: `-browse` everywhere meant a local operator never got the OS chooser, `-native` everywhere breaks every remote deployment. The right default depends on facts only the running host knows — where the server binds, whether the process was launched over SSH, whether a display session exists — so no static row is correct for all deployments. + +## Decision + +A third sibling package, **`dsh-host-directory-picker-auto`**: a node-half-only *chooser* that owns no picking code and no UI. Its `apply` samples the host facts exactly once at boot — bind host from the injected `httpServer` (a new `host` getter mirrors the existing `port`), `SSH_CONNECTION`/`SSH_TTY`, platform, `DISPLAY`/`WAYLAND_DISPLAY` — resolves them through one exported pure function, and mounts the chosen dual-face backend with `ctx.loader.create({name})` into the Loader's **in-memory root tree**; the effect's disposer removes the entry again. `native` requires every attended-host signal (loopback bind ∧ no SSH markers ∧ display session, assumed on darwin/win32); anything ambiguous resolves to `browse`, which works everywhere. `apps/cli` now mounts `-auto` as its `directory-picker` row; composing `-native` or `-browse` directly remains the pin. + +Why entry-level mounting is the load-bearing mechanism: the client module table (`dsh-client-modules`) reconciles **Loader entries** reactively over `internal/plugin`, so a backend mounted as a real entry gets its browser half discovered exactly as a config-row's would be — the seam's one-row-swaps-both-faces invariant survives adaptivity with zero duplicated client code. The dev HMR row (`AppCLIEntry`) is the mechanism precedent. Root-tree targeting matters: the root tree's `write()` is a no-op, so the resolved row can never be persisted back into `cordis.yml` (the Include subtree *does* write). + +## Alternatives considered + +- **Boot-glue resolution in `AppCLIEntry`** (ship both rows with static `disabled`, patch `disabled` from a `--directory-picker=auto|native|browse` flag). Works — `PatchOptions` patches metadata, and the modules scan skips disabled rows — but leaves the decision app-private where every future composition re-implements it; the chooser plugin gives any `cordis.yml` the same one-row adaptivity. Reintroduce the flag only when a deployment needs to *force* a backend without editing its yml. +- **One merged plugin branching per call** (client tries `pick`, falls back to the browse dialog on `directory-picker-unavailable`). Rejected: the client would need both flows in one bundle — the bundle-purity gate forbids cross-plugin value imports and jscpd forbids copying the dialog — and per-call probing pays a doomed RPC on every open of a browse host. +- **Resurrecting the wire advertisement** so both client flows mount and branch on the host's kind. Rejected: reverses the seam note's deletion for no consumer the chooser doesn't already serve, and collides with the `single` directory-flow holes. +- **Per-connection adaptivity** (native for a loopback browser, browse for a remote one, same server). Deferred: needs a per-client capability, the advertisement above, and both flows mounted; no deployment serves both operator shapes at once today. + +## Consequences + +- The shipped web GUI adapts out of the box: attended local host → OS chooser; SSH launch, all-interfaces bind, or headless host → in-app browser. Detection is a heuristic (a detached tmux session loses `SSH_*`; a non-Aqua darwin process still counts as displayed) — a wrong `native` choice degrades to the backend's existing retryable failure dialog, and composing `-browse` pins the safe interaction. +- One resolution per boot keeps the seam's capability-stability contract; per-connection shapes remain out of scope until a deployment demands them. +- Mounting the chooser **and** a backend row together fails loud (duplicate `directoryPicker` service; duplicate flow in the `single` holes). +- The host typecheck aggregate now references the two backend projects (declarations only, node entries carry no client merge) so the chooser's REAL-composition test can mount them — the mirror of the client aggregate's `webserver` reference. diff --git a/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.zh.md b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.zh.md new file mode 100644 index 0000000000..acabeb8060 --- /dev/null +++ b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.zh.md @@ -0,0 +1,29 @@ +# Agent Note:目录选择交互的自适应默认值 + +状态:已实现 + +[English](2026-07-29-directory-picker-adaptive-default.md) | 中文 + +## 问题 + +[目录选择 seam](../architecture/2026-07-28-directory-picker-capability-seam.md)把交互形态做成了 `cordis.yml` 的切换点,但随附的组合仍必须固定一个后端:处处用 `-browse` 意味着本地操作者永远得不到 OS 选择器,处处用 `-native` 则弄坏所有远程部署。正确的默认值取决于只有运行中的宿主才知道的事实——服务器绑定在哪里、进程是否经 SSH 启动、是否存在显示会话——因此没有哪一静态行对所有部署都正确。 + +## 决策 + +第三个同级包 **`dsh-host-directory-picker-auto`**:一个只有 node 半侧的*选择器*,不持有任何选取代码,也没有 UI。它的 `apply` 在启动时恰好采样一次宿主事实——从注入的 `httpServer` 读绑定宿主(新增的 `host` getter 与既有的 `port` 对称)、`SSH_CONNECTION`/`SSH_TTY`、平台、`DISPLAY`/`WAYLAND_DISPLAY`——经由一个导出的纯函数判定,再用 `ctx.loader.create({name})` 把选中的双面后端挂进 Loader 的**内存根树**;该 effect 的 disposer 会再次移除该条目。`native` 要求全部有人值守宿主信号(回环绑定 ∧ 无 SSH 标记 ∧ 显示会话,darwin/win32 上视为存在);任何含糊情形都判定为处处可用的 `browse`。`apps/cli` 现在把 `-auto` 挂为它的 `directory-picker` 行;直接组合 `-native` 或 `-browse` 仍是固定交互的方式。 + +条目级挂载之所以是承重机制:client 模块表(`dsh-client-modules`)基于 `internal/plugin` 对 **Loader 条目**做响应式协调,因此以真实条目挂载的后端,其 browser half 被发现的方式与配置行完全相同——seam 的“一行同时换两面”不变式在自适应下依然成立,且没有一行重复的 client 代码。开发环境的 HMR 行(`AppCLIEntry`)是该机制的先例。瞄准根树很关键:根树的 `write()` 是 no-op,因此判定出的行绝不会被持久化回 `cordis.yml`(Include 子树*会*写回)。 + +## 曾考虑的替代方案 + +- **在 `AppCLIEntry` 里做启动胶水判定**(随附两行并带静态 `disabled`,由 `--directory-picker=auto|native|browse` 标志修补 `disabled`)。可行——`PatchOptions` 能修补元数据,模块扫描也会跳过禁用行——但把决策留成应用私有,此后每个组合都要重新实现;选择器插件让任何 `cordis.yml` 都获得同样的一行自适应。只有当某个部署需要不改自己的 yml 就*强制*指定后端时,才重新引入该标志。 +- **合并成一个按调用分支的插件**(client 先试 `pick`,收到 `directory-picker-unavailable` 再回退到浏览对话框)。否决:client 得把两套流程装进同一个 bundle——bundle 纯净门禁禁止跨插件的值导入,jscpd 禁止复制对话框——而且按调用探测让 browse 宿主每次打开都付出一次注定失败的 RPC。 +- **复活 wire 广播**,让两套 client 流程都挂载并按宿主的 kind 分支。否决:推翻 seam Agent Note 的那次删除,却服务不了任何选择器尚未服务的消费方,还与 `single` 目录流洞相冲突。 +- **按连接自适应**(同一台服务器,回环浏览器用 native、远程浏览器用 browse)。延期:需要按客户端的能力对象、上述广播,以及同时挂载两套流程;今天没有部署同时服务两种操作者形态。 + +## 后果 + +- 随附的 web GUI 开箱即自适应:有人值守的本地宿主 → OS 选择器;SSH 启动、全网卡绑定或无头宿主 → 应用内浏览器。探测是启发式的(脱离的 tmux 会话会丢失 `SSH_*`;非 Aqua 的 darwin 进程仍被算作有显示)——错误的 `native` 选择会退化为后端既有的可重试失败对话框,组合 `-browse` 即固定住安全的交互。 +- 每次启动只判定一次,维持 seam 的能力稳定性契约;按连接的形态在有部署提出需求前仍不在范围内。 +- 同时挂载选择器**和**某个后端行会大声失败(重复的 `directoryPicker` 服务;`single` 洞中的重复流程)。 +- host 类型检查聚合现在引用两个后端项目(仅声明,node 入口不携带 client 合并),使选择器的 REAL-composition 测试能挂载它们——与 client 聚合对 `webserver` 的引用互为镜像。 diff --git a/apps/cli/cordis.yml b/apps/cli/cordis.yml index ed44f75c7f..d9cbd4b08a 100644 --- a/apps/cli/cordis.yml +++ b/apps/cli/cordis.yml @@ -314,12 +314,15 @@ # The API gateway: the transport-agnostic dispatch face every client shape # shares. provider/model are the host default routing — the profile json's # mapping target (user config overrides these engineering defaults). -# Directory-picking package, dual-face: the node half serves the gateway's -# host.* picker RPCs, the browser half fills ui-workspace's directory-flow -# slots — one row composes the whole interaction. Swap point: mount -# '-native' instead for the host-display OS chooser. +# Directory-picking composition, adaptive default: the chooser resolves the +# host's situation once at boot (bind host, SSH launch, display session) and +# mounts the matching dual-face backend row — its node half serves the +# gateway's host.* picker RPCs, its browser half fills ui-workspace's +# directory-flow slots. Swap point: mount '-native' (host-display OS chooser) +# or '-browse' (in-app browsing, remote-capable) directly to pin the +# interaction. - id: directory-picker - name: '@deepseek-ai/dsh-host-directory-picker-browse' + name: '@deepseek-ai/dsh-host-directory-picker-auto' - id: api-gateway name: '@deepseek-ai/dsh-host-apiproxy' diff --git a/apps/cli/package.json b/apps/cli/package.json index 0914aaf5aa..4538d326cf 100644 --- a/apps/cli/package.json +++ b/apps/cli/package.json @@ -54,6 +54,7 @@ "@deepseek-ai/dsh-goal": "workspace:^", "@deepseek-ai/dsh-goal-session": "workspace:^", "@deepseek-ai/dsh-host-apiproxy": "workspace:^", + "@deepseek-ai/dsh-host-directory-picker-auto": "workspace:^", "@deepseek-ai/dsh-host-directory-picker-browse": "workspace:^", "@deepseek-ai/dsh-host-directory-picker-native": "workspace:^", "@deepseek-ai/dsh-host-webserver": "workspace:^", diff --git a/docs/config-catalog.md b/docs/config-catalog.md index 3c0543e4ca..8a169a4dbe 100644 --- a/docs/config-catalog.md +++ b/docs/config-catalog.md @@ -2224,6 +2224,7 @@ These load from a `cordis.yml` entry with no `config:` block; they declare no co - `@deepseek-ai/dsh-commands` ([`packages/ui/commands/src/index.ts`](../packages/ui/commands/src/index.ts)) - `@deepseek-ai/dsh-fs-policy` ([`packages/fs/fs-policy/src/index.ts`](../packages/fs/fs-policy/src/index.ts)) - `@deepseek-ai/dsh-goal-session` — requires `agents` · `goals` · `sessions` ([`packages/goal/goal-session/src/index.ts`](../packages/goal/goal-session/src/index.ts)) +- `@deepseek-ai/dsh-host-directory-picker-auto` — requires `httpServer` · `loader` ([`packages/host/directory-picker-auto/src/index.ts`](../packages/host/directory-picker-auto/src/index.ts)) - `@deepseek-ai/dsh-host-directory-picker-native` ([`packages/host/directory-picker-native/src/index.ts`](../packages/host/directory-picker-native/src/index.ts)) - `@deepseek-ai/dsh-llm` ([`packages/llm/llm/src/index.ts`](../packages/llm/llm/src/index.ts)) - `@deepseek-ai/dsh-lsp` ([`packages/lsp/lsp/src/index.ts`](../packages/lsp/lsp/src/index.ts)) diff --git a/packages/host/README.i18n.yaml b/packages/host/README.i18n.yaml index afc0e2a695..cd09dca013 100644 --- a/packages/host/README.i18n.yaml +++ b/packages/host/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/host/README.md -README.md: d44770f70be16c12f44b78155089e092a3e9bba0 -README.zh.md: 2b6878b08be6489dcd510a0a0e0f0e833c2a8014 +README.md: 4f56afc45d594bf1f3f0784b848958cf62f52ae5 +README.zh.md: 888301282e272966b9d99e299b893afc90670906 diff --git a/packages/host/README.md b/packages/host/README.md index d44770f70b..4f56afc45d 100644 --- a/packages/host/README.md +++ b/packages/host/README.md @@ -11,5 +11,6 @@ The host side of the dsh web GUI: the API gateway every client shape shares, and | `directory-picker/` | Workspace-directory picking seam: discriminated `native`/`browse` capability the gateway's picker RPCs delegate to | `ctx.directoryPicker` | | `directory-picker-native/` | Dual-face native interaction: OS-chooser backend (osascript / PowerShell / Zenity+KDialog, host-display only) + the browser half filling ui-workspace's directory-flow slots | (registers `ctx.directoryPicker`) | | `directory-picker-browse/` | Dual-face browse interaction: listing/creation primitives over Node stdlib (remote-capable) + the browser half rendering the in-app Select Workspace Directory dialog | (registers `ctx.directoryPicker`) | +| `directory-picker-auto/` | Adaptive chooser: resolves the host's situation once at boot (bind host, SSH, display) and mounts the matching dual-face backend as an in-memory Loader entry | (mounts a backend row) | `apiproxy` is transport-agnostic by design — it registers no routes; carriers wrap `ctx.apiProxy` themselves. The HTTP carrier route (with its `/api` browser-trust fence) is mounted by [`client/connection`](../client/connection/README.md)'s node half, which is why that package lives in the client group: it owns both ends of the wire. diff --git a/packages/host/README.zh.md b/packages/host/README.zh.md index 2b6878b08b..888301282e 100644 --- a/packages/host/README.zh.md +++ b/packages/host/README.zh.md @@ -11,5 +11,6 @@ dsh web GUI 的宿主侧:所有客户端形态共用的 API 网关,以及承 | `directory-picker/` | 工作区目录选择 seam:网关的 picker RPC 委托的可辨识 `native`/`browse` 能力 | `ctx.directoryPicker` | | `directory-picker-native/` | 双面原生交互:OS 选择器后端(osascript/PowerShell/Zenity+KDialog,仅宿主屏幕可用)+ 填入 ui-workspace 目录流 slot 的 browser half | (注册 `ctx.directoryPicker`) | | `directory-picker-browse/` | 双面浏览交互:基于 Node 标准库的列举/创建原语(可远程)+ 渲染应用内选择工作区目录对话框的 browser half | (注册 `ctx.directoryPicker`) | +| `directory-picker-auto/` | 自适应选择器:启动时一次性判定宿主处境(绑定宿主、SSH、显示),并把匹配的双面后端挂载为内存中的 Loader 条目 | (挂载一个后端行) | `apiproxy` 在设计上与传输方式无关——它不注册任何路由;载体自行包装 `ctx.apiProxy`。HTTP 载体路由(连同其 `/api` 浏览器信任栅栏)由 [`client/connection`](../client/connection/README.md) 的 node 半侧挂载,这正是该包住在 client 组的原因:它拥有这条线的两端。 diff --git a/packages/host/directory-picker-auto/README.i18n.yaml b/packages/host/directory-picker-auto/README.i18n.yaml new file mode 100644 index 0000000000..fa4907698b --- /dev/null +++ b/packages/host/directory-picker-auto/README.i18n.yaml @@ -0,0 +1,6 @@ +# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each +# side as of the last confirmed-consistent state. Both languages carry equal authority; +# after editing either side, bring the other along and re-record with: +# pnpm run verify-translation-pairing --write packages/host/directory-picker-auto/README.md +README.md: 73692d1fb5af1e7b23b2a99d0a5cd48507f68ce5 +README.zh.md: 5bc9ced01f86d021db256df2c9e69e97ec6a7ab1 diff --git a/packages/host/directory-picker-auto/README.md b/packages/host/directory-picker-auto/README.md new file mode 100644 index 0000000000..73692d1fb5 --- /dev/null +++ b/packages/host/directory-picker-auto/README.md @@ -0,0 +1,20 @@ +# @deepseek-ai/dsh-host-directory-picker-auto + +English | [中文](README.zh.md) + +The **adaptive chooser** of the [directory-picker seam](../directory-picker/README.md): a node-half-only plugin that resolves the host's situation once at boot and mounts the matching dual-face backend — [`-native`](../directory-picker-native/README.md) or [`-browse`](../directory-picker-browse/README.md) — as a real Loader entry in the in-memory root tree (never persisted to a config file; the root tree's `write()` is a no-op). Because the backend arrives as an ordinary entry, its browser half is discovered by the client module table exactly as a config-row's would be, so the seam's one-row-swaps-both-faces invariant holds for the resolved choice. Unloading the chooser removes the entry again, unloading both faces with it. + +Resolution is one pure boot-time sample (`resolveDirectoryPickerBackend`), exported for reuse and tests. `native` requires every signal that the operator can see the host display: a loopback-only bind (read from the injected `httpServer`; an all-interfaces bind admits remote browsers no OS chooser can reach), no SSH launch (`SSH_CONNECTION`/`SSH_TTY` unset or blank — under SSH port-forwarding the chooser would open on the unattended server), and a display session (assumed on darwin/win32; `DISPLAY`/`WAYLAND_DISPLAY` elsewhere). Anything ambiguous resolves to `browse`, which works everywhere. The sample happens exactly once per boot so the mounted capability stays stable for the service lifetime, as the seam requires. Pinning an interaction is not a config field here — compose the `-native` or `-browse` row directly instead of this one, the seam's documented swap point; mounting the chooser **and** a backend row together fails loud (duplicate `directoryPicker` service, duplicate client flow in the `single` holes). + +## Model Experience + +None, as the chooser only composes the GUI host's directory selection; nothing here reaches a model request. + +#### KV Cache effect + +None; this package neither assembles nor sends a provider request. + +## Known Limitations and Deferred Work + +- **Detection is a heuristic, not a proof** — a tmux session detached from its SSH launch loses the `SSH_*` markers, and a darwin process outside an Aqua session still counts as displayed; a wrong `native` choice degrades to the backend's existing retryable failure dialog, and composing `-browse` directly pins the safe interaction. +- **Boot-time only** — one resolution serves every client of the boot; per-connection adaptivity (native for a local browser, browse for a remote one, same server) would need a per-client capability and the wire advertisement the seam deliberately deleted, and waits for a deployment that serves both at once. diff --git a/packages/host/directory-picker-auto/README.zh.md b/packages/host/directory-picker-auto/README.zh.md new file mode 100644 index 0000000000..5bc9ced01f --- /dev/null +++ b/packages/host/directory-picker-auto/README.zh.md @@ -0,0 +1,20 @@ +# @deepseek-ai/dsh-host-directory-picker-auto + +[English](README.md) | 中文 + +[目录选择 seam](../directory-picker/README.md) 的**自适应选择器**:一个只有 node 半侧的插件,在启动时一次性判定宿主处境,并把匹配的双面后端——[`-native`](../directory-picker-native/README.md) 或 [`-browse`](../directory-picker-browse/README.md)——作为真实的 Loader 条目挂进内存根树(绝不持久化到配置文件;根树的 `write()` 是 no-op)。由于后端以普通条目的形式到达,其 browser half 被 client 模块表发现的方式与配置行完全相同,因此对判定出的选择,seam 的“一行同时换两面”不变式依然成立。卸载该选择器会再次移除该条目,连同两面一起卸载。 + +判定是一次纯函数的启动时采样(`resolveDirectoryPickerBackend`),已导出供复用与测试。`native` 要求“操作者看得到宿主屏幕”的全部信号:仅回环的绑定(从注入的 `httpServer` 读取;全网卡绑定会接入任何 OS 选择器都触及不到的远程浏览器);非 SSH 启动(`SSH_CONNECTION`/`SSH_TTY` 未设置或为空——SSH 端口转发下选择器会弹在无人值守的服务器上);以及存在显示会话(darwin/win32 上视为存在,其余平台看 `DISPLAY`/`WAYLAND_DISPLAY`)。任何含糊情形都判定为处处可用的 `browse`。采样每次启动恰好发生一次,因此挂载的能力在服务生命周期内保持稳定,符合 seam 的要求。固定某种交互在这里不是配置字段——直接组合 `-native` 或 `-browse` 行来替代本行,那才是 seam 文档化的切换点;同时挂载选择器**和**某个后端行会大声失败(重复的 `directoryPicker` 服务、`single` 洞中的重复 client 流程)。 + +## 模型体验 + +无。该选择器仅组合 GUI 宿主的目录选择;这里没有任何内容进入模型请求。 + +#### KV 缓存影响 + +无;该包既不组装也不发送提供方请求。 + +## 已知限制与延期工作 + +- **探测是启发式,不是证明**——从 SSH 启动中脱离的 tmux 会话会丢失 `SSH_*` 标记,Aqua 会话之外的 darwin 进程也仍被算作有显示;错误的 `native` 选择会退化为后端既有的可重试失败对话框,而直接组合 `-browse` 即固定住安全的交互。 +- **仅在启动时判定**——一次判定服务本次启动的所有客户端;按连接自适应(同一台服务器,本地浏览器用 native、远程浏览器用 browse)需要按客户端的能力对象以及 seam 有意删除的 wire 广播,等到出现同时服务两种形态的部署再做。 diff --git a/packages/host/directory-picker-auto/package.json b/packages/host/directory-picker-auto/package.json new file mode 100644 index 0000000000..888637231e --- /dev/null +++ b/packages/host/directory-picker-auto/package.json @@ -0,0 +1,47 @@ +{ + "name": "@deepseek-ai/dsh-host-directory-picker-auto", + "description": "Adaptive chooser of the directory-picker seam: resolves the host situation at boot and mounts the native or browse backend for the DeepSeek Harness web GUI host", + "version": "0.0.1", + "private": true, + "type": "module", + "main": "lib/index.js", + "types": "lib/types/index.d.ts", + "exports": { + ".": { + "types": "./lib/types/index.d.ts", + "default": "./lib/index.js" + }, + "./invariant": { + "types": "./lib/types/invariant.d.ts", + "default": "./lib/invariant.js" + }, + "./src/*": "./src/*", + "./package.json": "./package.json" + }, + "files": [ + "lib/index.js", + "lib/invariant.js", + "lib/types/**/*.d.ts", + "lib/types/**/*.d.ts.map", + "src" + ], + "license": "BSD-3-Clause", + "peerDependencies": { + "@cordisjs/plugin-loader": "^1.0.0-rc.5", + "@deepseek-ai/dsh-host-directory-picker-browse": "^0.0.1", + "@deepseek-ai/dsh-host-directory-picker-native": "^0.0.1", + "@deepseek-ai/dsh-host-webserver": "^0.0.1", + "@deepseek-ai/dsh-invariants": "^0.0.1", + "cordis": "^4.0.0-rc.7" + }, + "devDependencies": { + "@cordisjs/plugin-include": "workspace:^", + "@cordisjs/plugin-loader": "workspace:^", + "@deepseek-ai/dsh-host-directory-picker": "workspace:^", + "@deepseek-ai/dsh-host-directory-picker-browse": "workspace:^", + "@deepseek-ai/dsh-host-directory-picker-native": "workspace:^", + "@deepseek-ai/dsh-host-webserver": "workspace:^", + "@deepseek-ai/dsh-invariants": "workspace:^", + "cordis": "^4.0.0-rc.7" + } +} diff --git a/packages/host/directory-picker-auto/src/index.ts b/packages/host/directory-picker-auto/src/index.ts new file mode 100644 index 0000000000..12568d8f8b --- /dev/null +++ b/packages/host/directory-picker-auto/src/index.ts @@ -0,0 +1,52 @@ +/** + * Adaptive chooser of the directory-picker seam: resolves the host's + * situation once at boot (bind host, SSH launch, display session) and mounts + * the matching dual-face backend — `-native` or `-browse` — as a real Loader + * entry in the in-memory root tree. Because the backend arrives as an + * ordinary entry, its browser half is discovered exactly as a config-row's + * would be, so the seam's one-row-swaps-both-faces invariant holds for the + * resolved choice; pinning an interaction remains composing that backend row + * directly instead of this one. + * @module @deepseek-ai/dsh-host-directory-picker-auto + */ + +import type { Context } from 'cordis' +// Empty type imports carry the `loader` and `httpServer` Context merges for the reads below. +import type {} from '@cordisjs/plugin-loader' +import type {} from '@deepseek-ai/dsh-host-webserver' +import type { DirectoryPickerBackendKind } from './resolve.ts' +import { resolveDirectoryPickerBackend } from './resolve.ts' + +export type { DirectoryPickerBackendKind, DirectoryPickerEnv, DirectoryPickerHostFacts } from './resolve.ts' +export { resolveDirectoryPickerBackend } from './resolve.ts' + +/** Cordis plugin name. */ +export const name = 'directory-picker-auto' +/** Required services: the effective bind host (`httpServer`) and the entry tree the backend mounts into (`loader`). */ +export const inject = ['httpServer', 'loader'] + +/** Backend package per resolved kind — fixed composition vocabulary, not a tunable. */ +const BACKEND_PACKAGES: Record = { + native: '@deepseek-ai/dsh-host-directory-picker-native', + browse: '@deepseek-ai/dsh-host-directory-picker-browse', +} + +/** + * Resolve the backend from one boot-time sample and mount it as a Loader + * entry; the effect's disposer removes the entry, so unloading this plugin + * unloads both faces of the mounted backend with it. + * @param ctx - cordis context carrying the injected `httpServer` and `loader`. + */ +export async function apply(ctx: Context): Promise { + const backend = resolveDirectoryPickerBackend({ + bindHost: ctx.httpServer.host, + platform: process.platform, + env: process.env, + }) + await ctx.effect(async () => { + // Root-tree create: the Loader root is in-memory (write() is a no-op), so + // the mounted row can never be persisted back into a config file. + const id = await ctx.loader.create({ name: BACKEND_PACKAGES[backend] }) + return () => { ctx.loader.remove(id) } + }, 'directory-picker-auto: backend entry') +} diff --git a/packages/host/directory-picker-auto/src/invariant.ts b/packages/host/directory-picker-auto/src/invariant.ts new file mode 100644 index 0000000000..8b3f251447 --- /dev/null +++ b/packages/host/directory-picker-auto/src/invariant.ts @@ -0,0 +1,25 @@ +/** + * Package-owned invariant companion for the adaptive directory-picker chooser. + * @module @deepseek-ai/dsh-host-directory-picker-auto/invariant + */ + +import type { Context } from 'cordis' +import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants' + +const PACKAGE_NAME = '@deepseek-ai/dsh-host-directory-picker-auto' + +/** Cordis companion plugin name. */ +export const name = 'host-directory-picker-auto-invariant' +/** Service required before the companion can reserve package ownership. */ +export const inject = ['invariants'] + +/** No runtime invariant: the sole effect is one boot-time Loader-entry mount owned by the plugin fiber; the store is authoritative. */ +const install: InvariantInstaller = () => {} + +/** + * Register the adaptive directory-picker invariant companion. + * @param ctx - Cordis context carrying the invariant service. + * @returns the installed registration's disposer after setup succeeds. + */ +export const apply = (ctx: Context): Promise<() => void> => + Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install)) diff --git a/packages/host/directory-picker-auto/src/resolve.ts b/packages/host/directory-picker-auto/src/resolve.ts new file mode 100644 index 0000000000..d2a22e2fa8 --- /dev/null +++ b/packages/host/directory-picker-auto/src/resolve.ts @@ -0,0 +1,46 @@ +/** + * Boot-time backend resolution for the adaptive directory-picker composition: + * one pure decision from sampled host facts to a concrete backend kind. The + * caller samples exactly once per boot, so the mounted capability stays + * stable for the service lifetime as the seam requires. + * @module @deepseek-ai/dsh-host-directory-picker-auto/resolve + */ + +/** Concrete interaction backend the resolver chooses between. */ +export type DirectoryPickerBackendKind = 'native' | 'browse' + +/** Environment keys the resolution reads (a `process.env` subset). */ +export type DirectoryPickerEnv = Readonly< + Partial> +> + +/** Host facts the backend choice is a pure function of, sampled once at boot. */ +export interface DirectoryPickerHostFacts { + /** Effective webserver bind host (`127.0.0.1` or `0.0.0.0`). */ + bindHost: string + /** Host process platform. */ + platform: NodeJS.Platform + /** Environment sample; SSH marks a remote operator, DISPLAY/WAYLAND_DISPLAY a Linux display. */ + env: DirectoryPickerEnv +} + +/** An env value counts only when set and non-blank (an empty export is "unset" by shell convention). */ +const present = (value: string | undefined): boolean => value !== undefined && value !== '' + +/** + * Resolve which backend serves this boot. `native` requires every signal that + * the operator can see the host display: a loopback-only bind (an + * all-interfaces bind admits remote browsers no OS chooser can reach), no SSH + * launch (under SSH port-forwarding the chooser would open on the unattended + * server), and a display session (assumed on darwin/win32, `DISPLAY`/ + * `WAYLAND_DISPLAY` elsewhere). Anything ambiguous resolves to `browse`, + * which works everywhere. + * @param facts - the sampled host facts. + * @returns the backend kind to mount. + */ +export function resolveDirectoryPickerBackend(facts: DirectoryPickerHostFacts): DirectoryPickerBackendKind { + if (facts.bindHost !== '127.0.0.1') return 'browse' + if (present(facts.env.SSH_CONNECTION) || present(facts.env.SSH_TTY)) return 'browse' + if (facts.platform === 'darwin' || facts.platform === 'win32') return 'native' + return present(facts.env.DISPLAY) || present(facts.env.WAYLAND_DISPLAY) ? 'native' : 'browse' +} diff --git a/packages/host/directory-picker-auto/tests/loader-composition.spec.ts b/packages/host/directory-picker-auto/tests/loader-composition.spec.ts new file mode 100644 index 0000000000..7e094035fb --- /dev/null +++ b/packages/host/directory-picker-auto/tests/loader-composition.spec.ts @@ -0,0 +1,139 @@ +/** + * REAL-composition coverage: a test-only cordis.yml booted through the + * vendored Loader mounts the webserver row plus the adaptive chooser, and the + * assertions observe the durable outcome — which backend entry the chooser + * mounted into the Loader store, the capability the seam then serves, and + * that disposing the chooser removes the mounted entry again (HMR safety). + */ + +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { pathToFileURL } from 'node:url' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import Loader from '@cordisjs/plugin-loader' +import Include from '@cordisjs/plugin-include' +import HttpServer from '@deepseek-ai/dsh-host-webserver' +import type { DirectoryPicker } from '@deepseek-ai/dsh-host-directory-picker' +import BrowseDirectoryPicker from '@deepseek-ai/dsh-host-directory-picker-browse' +import NativeDirectoryPicker from '@deepseek-ai/dsh-host-directory-picker-native' +import * as DirectoryPickerAuto from '../src/index.ts' + +const AUTO = '@deepseek-ai/dsh-host-directory-picker-auto' +const NATIVE = '@deepseek-ai/dsh-host-directory-picker-native' +const BROWSE = '@deepseek-ai/dsh-host-directory-picker-browse' + +let root: string | undefined +let context: Context | undefined + +afterEach(async () => { + vi.unstubAllEnvs() + await context?.fiber.dispose() + context = undefined + if (root !== undefined) await rm(root, { recursive: true, force: true }) + root = undefined +}) + +/** Write a dist fixture and a two-row cordis.yml (webserver + chooser), then boot it through the real Loader. */ +async function loadComposition(bindHost: '127.0.0.1' | '0.0.0.0'): Promise<{ ctx: Context; configPath: string }> { + root = await mkdtemp(join(tmpdir(), 'dsh-directory-picker-auto-')) + const dist = join(root, 'dist') + await mkdir(dist) + const distIndex = join(dist, 'index.html') + await writeFile(distIndex, 'shell') + const configPath = join(root, 'cordis.yml') + await writeFile(configPath, [ + "- name: '@deepseek-ai/dsh-host-webserver'", + ' config:', + ` host: '${bindHost}'`, + ' port: 0', + ` distIndex: '${distIndex}'`, + `- name: '${AUTO}'`, + '', + ].join('\n')) + + context = new Context() + context.baseUrl = pathToFileURL(root).href + '/' + await context.plugin(Loader) + context.loader.builtins.include = Include + const modules = new Map([ + ['@deepseek-ai/dsh-host-webserver', HttpServer], + [AUTO, DirectoryPickerAuto], + [NATIVE, NativeDirectoryPicker], + [BROWSE, BrowseDirectoryPicker], + ]) + context.loader.internal = { + version: 'v2', + async import(specifier: string) { + if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`) + return modules.get(specifier) + }, + } as unknown as NonNullable + await context.loader.create({ + name: 'cordis:include', + config: { path: pathToFileURL(configPath).href }, + }) + await context.loader.await() + return { ctx: context, configPath } +} + +/** Entry names currently present in the loader store (root tree plus subtrees). */ +function entryNames(ctx: Context): string[] { + return [...ctx.loader.entries()].map(entry => entry.options.name) +} + +/** Force every signal of an attended host: no SSH launch, a display on any platform. */ +function stubAttendedHost(): void { + vi.stubEnv('SSH_CONNECTION', '') + vi.stubEnv('SSH_TTY', '') + vi.stubEnv('DISPLAY', ':0') +} + +describe('real Loader composition', () => { + // Real-Loader composition resolves workspace packages through tsx at test + // time; first resolution after the host/client program split is slow enough + // to trip the default 5s budget on cold caches. + it('mounts the native backend for an attended loopback host and unmounts it on disposal', { timeout: 60_000 }, async () => { + stubAttendedHost() + const { ctx, configPath } = await loadComposition('127.0.0.1') + + const unloaded = [...ctx.loader.entries()] + .filter(entry => entry.fiber === undefined && !entry.disabled) + .map(entry => entry.options.name) + expect(unloaded).toEqual([]) + expect(entryNames(ctx)).toContain(NATIVE) + expect(entryNames(ctx)).not.toContain(BROWSE) + const picker = ctx.get('directoryPicker') as DirectoryPicker + expect(picker.capability().kind).toBe('native') + // The mounted row lives in the Loader's in-memory root tree only — the + // booted config file must never gain the resolved backend row. + expect(await readFile(configPath, 'utf8')).not.toContain(NATIVE) + + // HMR safety: disposing the chooser's fiber removes the entry it created. + const autoEntry = [...ctx.loader.entries()].find(entry => entry.options.name === AUTO)! + await autoEntry.fiber!.dispose() + await ctx.loader.await() + expect(entryNames(ctx)).not.toContain(NATIVE) + expect(ctx.get('directoryPicker')).toBeUndefined() + }) + + it('mounts the browse backend under an SSH launch', { timeout: 60_000 }, async () => { + stubAttendedHost() + vi.stubEnv('SSH_CONNECTION', '10.0.0.2 55 10.0.0.9 22') + const { ctx } = await loadComposition('127.0.0.1') + + expect(entryNames(ctx)).toContain(BROWSE) + expect(entryNames(ctx)).not.toContain(NATIVE) + const picker = ctx.get('directoryPicker') as DirectoryPicker + expect(picker.capability().kind).toBe('browse') + }) + + it('mounts the browse backend for an all-interfaces bind even on an attended host', { timeout: 60_000 }, async () => { + stubAttendedHost() + const { ctx } = await loadComposition('0.0.0.0') + + expect(entryNames(ctx)).toContain(BROWSE) + expect(entryNames(ctx)).not.toContain(NATIVE) + }) +}) diff --git a/packages/host/directory-picker-auto/tests/resolve.spec.ts b/packages/host/directory-picker-auto/tests/resolve.spec.ts new file mode 100644 index 0000000000..3beac44961 --- /dev/null +++ b/packages/host/directory-picker-auto/tests/resolve.spec.ts @@ -0,0 +1,37 @@ +import { describe, expect, it } from 'vitest' +import { resolveDirectoryPickerBackend } from '../src/resolve.ts' +import type { DirectoryPickerHostFacts } from '../src/resolve.ts' + +/** Baseline facts that resolve to `native`; each case overrides one signal. */ +const attended: DirectoryPickerHostFacts = { + bindHost: '127.0.0.1', + platform: 'darwin', + env: {}, +} + +describe('resolveDirectoryPickerBackend', () => { + it('resolves native for a loopback bind on a display platform', () => { + expect(resolveDirectoryPickerBackend(attended)).toBe('native') + expect(resolveDirectoryPickerBackend({ ...attended, platform: 'win32' })).toBe('native') + }) + + it('resolves browse for an all-interfaces bind regardless of other signals', () => { + expect(resolveDirectoryPickerBackend({ ...attended, bindHost: '0.0.0.0' })).toBe('browse') + }) + + it('resolves browse under an SSH launch (either env marker)', () => { + expect(resolveDirectoryPickerBackend({ ...attended, env: { SSH_CONNECTION: '10.0.0.2 55 10.0.0.9 22' } })).toBe('browse') + expect(resolveDirectoryPickerBackend({ ...attended, env: { SSH_TTY: '/dev/pts/3' } })).toBe('browse') + }) + + it('requires a display session on platforms without an implied one', () => { + expect(resolveDirectoryPickerBackend({ ...attended, platform: 'linux' })).toBe('browse') + expect(resolveDirectoryPickerBackend({ ...attended, platform: 'linux', env: { DISPLAY: ':0' } })).toBe('native') + expect(resolveDirectoryPickerBackend({ ...attended, platform: 'linux', env: { WAYLAND_DISPLAY: 'wayland-1' } })).toBe('native') + }) + + it('treats blank env exports as unset', () => { + expect(resolveDirectoryPickerBackend({ ...attended, env: { SSH_CONNECTION: '', SSH_TTY: '' } })).toBe('native') + expect(resolveDirectoryPickerBackend({ ...attended, platform: 'linux', env: { DISPLAY: '', WAYLAND_DISPLAY: '' } })).toBe('browse') + }) +}) diff --git a/packages/host/directory-picker-auto/tsconfig.json b/packages/host/directory-picker-auto/tsconfig.json new file mode 100644 index 0000000000..4e9a1955a7 --- /dev/null +++ b/packages/host/directory-picker-auto/tsconfig.json @@ -0,0 +1,27 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "lib/types" + }, + "include": [ + "src" + ], + "references": [ + { + "path": "../../../vendor/cosmokit" + }, + { + "path": "../../../vendor/cordis" + }, + { + "path": "../../../vendor/loader" + }, + { + "path": "../webserver" + }, + { + "path": "../../support/invariants" + } + ] +} diff --git a/packages/host/directory-picker/README.i18n.yaml b/packages/host/directory-picker/README.i18n.yaml index 3e5bae41b5..8bb3c0afec 100644 --- a/packages/host/directory-picker/README.i18n.yaml +++ b/packages/host/directory-picker/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/host/directory-picker/README.md -README.md: 8ef8889c875f5b1d07c015ddef819591041c8d7f -README.zh.md: 8aefffa7b29a47205ea42d0d1df742d1e1b2502d +README.md: 3749b238b56578ec68610bc13550760aa084bad6 +README.zh.md: 488da5129ec211c2a064156c22a9d0abf04d99be diff --git a/packages/host/directory-picker/README.md b/packages/host/directory-picker/README.md index 8ef8889c87..3749b238b5 100644 --- a/packages/host/directory-picker/README.md +++ b/packages/host/directory-picker/README.md @@ -2,7 +2,7 @@ English | [中文](README.zh.md) -The **workspace-directory picking seam** for the web-GUI host: an abstract `DirectoryPicker` service (`ctx.directoryPicker`) whose single contract method `capability()` returns a discriminated capability describing how an operator selects a directory. Backends differ in interaction shape, not just mechanism, so the seam models the shapes explicitly instead of one method set: `{ kind: 'native', pick(signal) }` opens one native OS chooser on the host display ([`-native`](../directory-picker-native/README.md)); `{ kind: 'browse', list(path?), createDirectory(path, name) }` serves listing/creation primitives an in-app browser drives, which works for remote clients no OS chooser can reach ([`-browse`](../directory-picker-browse/README.md)). Consumers switch on `capability().kind`; the union derives from the merge-extensible `DirectoryPickerCapabilities` map (a new backend declaration-merges its shape there), and the documented default for an unknown kind is to hide the picking affordance rather than fail. The capability object must be stable for the service lifetime. The client side mirrors the seam without a wire advertisement: each backend package is dual-face, its browser half registering the matching picking interaction into ui-workspace's directory-flow slots — so one composition row swaps both the host capability and the client flow together. +The **workspace-directory picking seam** for the web-GUI host: an abstract `DirectoryPicker` service (`ctx.directoryPicker`) whose single contract method `capability()` returns a discriminated capability describing how an operator selects a directory. Backends differ in interaction shape, not just mechanism, so the seam models the shapes explicitly instead of one method set: `{ kind: 'native', pick(signal) }` opens one native OS chooser on the host display ([`-native`](../directory-picker-native/README.md)); `{ kind: 'browse', list(path?), createDirectory(path, name) }` serves listing/creation primitives an in-app browser drives, which works for remote clients no OS chooser can reach ([`-browse`](../directory-picker-browse/README.md)). Consumers switch on `capability().kind`; the union derives from the merge-extensible `DirectoryPickerCapabilities` map (a new backend declaration-merges its shape there), and the documented default for an unknown kind is to hide the picking affordance rather than fail. The capability object must be stable for the service lifetime. The client side mirrors the seam without a wire advertisement: each backend package is dual-face, its browser half registering the matching picking interaction into ui-workspace's directory-flow slots — so one composition row swaps both the host capability and the client flow together. A composition that should not pin an interaction mounts the [`-auto`](../directory-picker-auto/README.md) chooser instead, which resolves the host's situation once at boot and mounts the matching backend row itself. Browse primitives fail with the typed `DirectoryPickerError` (`directory-unreadable` / `directory-exists` / `directory-create-failed`, each carrying the subject `path`), which the consuming gateway maps 1:1 onto wire error codes. `DirectoryEntry` rows carry a host-owned `hidden` flag (POSIX dot convention) so display policy stays client-side; `DirectoryListing.crumbs` is the ancestor chain from the filesystem root, every crumb a jump target. Design rationale, the `ctx.fs` separation, and the policy decisions live in [the directory-picker capability seam Agent Note](../../../.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.md). diff --git a/packages/host/directory-picker/README.zh.md b/packages/host/directory-picker/README.zh.md index 8aefffa7b2..488da5129e 100644 --- a/packages/host/directory-picker/README.zh.md +++ b/packages/host/directory-picker/README.zh.md @@ -2,7 +2,7 @@ [English](README.md) | 中文 -web GUI 宿主的**工作区目录选择 seam**:抽象服务 `DirectoryPicker`(`ctx.directoryPicker`),唯一契约方法 `capability()` 返回一个可辨识能力对象,描述操作者以何种方式选择目录。后端之间的差异在交互形态而不只是机制,因此 seam 显式建模形态而非统一方法集:`{ kind: 'native', pick(signal) }` 在宿主屏幕上打开一个原生 OS 选择器([`-native`](../directory-picker-native/README.md));`{ kind: 'browse', list(path?), createDirectory(path, name) }` 提供应用内浏览器驱动的列举/创建原语,可服务任何 OS 对话框都触及不到的远程客户端([`-browse`](../directory-picker-browse/README.md))。消费方按 `capability().kind` 分支;联合类型由可合并扩展的 `DirectoryPickerCapabilities` 映射派生(新后端在其中声明合并自己的形态),未知 kind 的文档化默认行为是隐藏选择入口而非失败。能力对象在服务生命周期内必须保持稳定。client 侧以镜像方式承接该 seam 而不经 wire 广播:每个后端包都是双面包,其 browser half 把匹配的选取交互注册进 ui-workspace 的目录流 slot——因此一行组合同时切换宿主能力与 client 流程。 +web GUI 宿主的**工作区目录选择 seam**:抽象服务 `DirectoryPicker`(`ctx.directoryPicker`),唯一契约方法 `capability()` 返回一个可辨识能力对象,描述操作者以何种方式选择目录。后端之间的差异在交互形态而不只是机制,因此 seam 显式建模形态而非统一方法集:`{ kind: 'native', pick(signal) }` 在宿主屏幕上打开一个原生 OS 选择器([`-native`](../directory-picker-native/README.md));`{ kind: 'browse', list(path?), createDirectory(path, name) }` 提供应用内浏览器驱动的列举/创建原语,可服务任何 OS 对话框都触及不到的远程客户端([`-browse`](../directory-picker-browse/README.md))。消费方按 `capability().kind` 分支;联合类型由可合并扩展的 `DirectoryPickerCapabilities` 映射派生(新后端在其中声明合并自己的形态),未知 kind 的文档化默认行为是隐藏选择入口而非失败。能力对象在服务生命周期内必须保持稳定。client 侧以镜像方式承接该 seam 而不经 wire 广播:每个后端包都是双面包,其 browser half 把匹配的选取交互注册进 ui-workspace 的目录流 slot——因此一行组合同时切换宿主能力与 client 流程。不应固定某种交互的组合改为挂载 [`-auto`](../directory-picker-auto/README.md) 选择器,它在启动时一次性判定宿主处境,并自行挂载匹配的后端行。 浏览原语以带类型的 `DirectoryPickerError` 失败(`directory-unreadable`/`directory-exists`/`directory-create-failed`,各自携带主体 `path`),消费网关将其 1:1 映射为协议错误码。`DirectoryEntry` 行携带宿主判定的 `hidden` 标志(POSIX 点前缀约定),展示策略留在客户端;`DirectoryListing.crumbs` 是从文件系统根开始的祖先链,每个 crumb 都是跳转目标。设计依据、与 `ctx.fs` 的切分、策略裁决见[目录选择能力 seam Agent Note](../../../.agents/notes/implemented/architecture/2026-07-28-directory-picker-capability-seam.md)。 diff --git a/packages/host/webserver/README.i18n.yaml b/packages/host/webserver/README.i18n.yaml index 40d5fcf9d3..0160db9f01 100644 --- a/packages/host/webserver/README.i18n.yaml +++ b/packages/host/webserver/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/host/webserver/README.md -README.md: e715e4452ddb808f36e6b097eee0fda7b8d0bfb0 -README.zh.md: 05e7e10d7815c8f26bb90597b38b7c6b83a86dbc +README.md: ace8c09e43dd8544a28d300f97b04610be78bc69 +README.zh.md: b9948e3d387a5da393ff62b9eeacfe310516f46a diff --git a/packages/host/webserver/README.md b/packages/host/webserver/README.md index e715e4452d..ace8c09e43 100644 --- a/packages/host/webserver/README.md +++ b/packages/host/webserver/README.md @@ -2,7 +2,7 @@ English | [中文](README.zh.md) -Plain HTTP route-registration plugin (default-exported `HttpServerService`, config `{host, port, distIndex}`): a `node:http` server that listens on activation and provides `ctx.httpServer` — `register(route)` adds a named `exact`/`prefix` route (duplicate `(kind, path)` throws: route patterns are a composition-level contract, so a collision is a misconfiguration; the returned disposer removes the route), `tapIndex(transform)` adds an index.html transform applied in registration order, and `port` reads the listening port (the OS-assigned value when `port` is 0). The match order is fixed — exact over the whole table, then longest prefix, then the static dist fallback with the locked semantics: traversal outside the dist root is 403, any miss falls back to `index.html` with HTTP 200 (SPA routing), unknown extensions ship as octet-stream, non-GET/HEAD is 405. Registration order carries no request-facing semantics. +Plain HTTP route-registration plugin (default-exported `HttpServerService`, config `{host, port, distIndex}`): a `node:http` server that listens on activation and provides `ctx.httpServer` — `register(route)` adds a named `exact`/`prefix` route (duplicate `(kind, path)` throws: route patterns are a composition-level contract, so a collision is a misconfiguration; the returned disposer removes the route), `tapIndex(transform)` adds an index.html transform applied in registration order, `port` reads the listening port (the OS-assigned value when `port` is 0), and `host` reads the configured bind host (composition-time facts other plugins adapt to, e.g. the directory-picker chooser). The match order is fixed — exact over the whole table, then longest prefix, then the static dist fallback with the locked semantics: traversal outside the dist root is 403, any miss falls back to `index.html` with HTTP 200 (SPA routing), unknown extensions ship as octet-stream, non-GET/HEAD is 405. Registration order carries no request-facing semantics. The package knows no harness concepts: the `/api` bridge is the connection plugin's route, plugin bundles and the HMR event stream are the modules/hmr plugins' routes. `host` accepts only `127.0.0.1` (default posture) and `0.0.0.0` (deliberate network exposure); `distIndex` is an assembly fact the composing app resolves and injects, never self-resolved (dist location is workspace knowledge of the app). Web (browser) shape only — Electron loads dist over `file://` and carries fetch over an IPC bridge, not this server. This package never prints; the URL line belongs to the shell. diff --git a/packages/host/webserver/README.zh.md b/packages/host/webserver/README.zh.md index 05e7e10d78..b9948e3d38 100644 --- a/packages/host/webserver/README.zh.md +++ b/packages/host/webserver/README.zh.md @@ -2,7 +2,7 @@ [English](README.md) | 中文 -朴素的 HTTP 路由注册插件(默认导出 `HttpServerService`,配置为 `{host, port, distIndex}`):一个在激活时开始监听的 `node:http` 服务器,提供 `ctx.httpServer`。`register(route)` 添加具名的 `exact`/`prefix` 路由;重复的 `(kind, path)` 会抛错,因为路由模式是组合层契约,冲突即配置错误;返回的 disposer 会移除该路由。`tapIndex(transform)` 添加按注册顺序应用的 index.html 转换,`port` 读取正在监听的端口(当 `port` 为 0 时读取 OS 分配的值)。匹配顺序固定不变:先在整张表中匹配精确路由,再匹配最长前缀,最后回退到静态 dist,并遵循固定语义:越出 dist 根目录的遍历返回 403,任何未命中项都以 HTTP 200 回退到 `index.html`(SPA 路由),未知扩展名按 octet-stream 提供,GET/HEAD 之外的方法返回 405。注册顺序不承载任何面向请求的语义。 +朴素的 HTTP 路由注册插件(默认导出 `HttpServerService`,配置为 `{host, port, distIndex}`):一个在激活时开始监听的 `node:http` 服务器,提供 `ctx.httpServer`。`register(route)` 添加具名的 `exact`/`prefix` 路由;重复的 `(kind, path)` 会抛错,因为路由模式是组合层契约,冲突即配置错误;返回的 disposer 会移除该路由。`tapIndex(transform)` 添加按注册顺序应用的 index.html 转换,`port` 读取正在监听的端口(当 `port` 为 0 时读取 OS 分配的值),`host` 读取配置的绑定宿主(这些是其他插件据以自适应的组合期事实,例如 directory-picker 选择器)。匹配顺序固定不变:先在整张表中匹配精确路由,再匹配最长前缀,最后回退到静态 dist,并遵循固定语义:越出 dist 根目录的遍历返回 403,任何未命中项都以 HTTP 200 回退到 `index.html`(SPA 路由),未知扩展名按 octet-stream 提供,GET/HEAD 之外的方法返回 405。注册顺序不承载任何面向请求的语义。 该包不了解任何 harness 概念:`/api` 桥接是 connection 插件的路由,插件 bundle 与 HMR(热模块替换)事件流则是 modules/hmr 插件的路由。`host` 只接受 `127.0.0.1`(默认姿态)和 `0.0.0.0`(有意向网络开放);`distIndex` 是由组合应用解析并注入的组装事实,绝不会自行解析,因为 dist 位置属于应用的工作区知识。该服务器只服务 Web(浏览器)形态;Electron 通过 `file://` 加载 dist,并经 IPC 桥接承载 fetch,而不使用本服务器。该包从不打印内容;URL 行属于 shell。 diff --git a/packages/host/webserver/src/index.ts b/packages/host/webserver/src/index.ts index 936dd4f5a1..37298cb178 100644 --- a/packages/host/webserver/src/index.ts +++ b/packages/host/webserver/src/index.ts @@ -78,6 +78,11 @@ export class HttpServerService extends Service { return this.listenedPort } + /** The configured bind host (the loopback or all-interfaces literal). */ + get host(): Config['host'] { + return this.config.host + } + /** * Register a named route. Duplicate (kind, path) throws — route patterns are * a composition-level contract, so a collision is a misconfiguration. diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66ea5bea26..d4813589a3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -230,6 +230,9 @@ importers: '@deepseek-ai/dsh-host-apiproxy': specifier: workspace:^ version: link:../../packages/host/apiproxy + '@deepseek-ai/dsh-host-directory-picker-auto': + specifier: workspace:^ + version: link:../../packages/host/directory-picker-auto '@deepseek-ai/dsh-host-directory-picker-browse': specifier: workspace:^ version: link:../../packages/host/directory-picker-browse @@ -2934,6 +2937,33 @@ importers: specifier: ^4.0.0-rc.7 version: 4.0.0-rc.7(@cordisjs/plugin-include@1.0.4)(@cordisjs/plugin-loader@1.0.0-rc.5) + packages/host/directory-picker-auto: + devDependencies: + '@cordisjs/plugin-include': + specifier: workspace:^ + version: link:../../../vendor/include + '@cordisjs/plugin-loader': + specifier: workspace:^ + version: link:../../../vendor/loader + '@deepseek-ai/dsh-host-directory-picker': + specifier: workspace:^ + version: link:../directory-picker + '@deepseek-ai/dsh-host-directory-picker-browse': + specifier: workspace:^ + version: link:../directory-picker-browse + '@deepseek-ai/dsh-host-directory-picker-native': + specifier: workspace:^ + version: link:../directory-picker-native + '@deepseek-ai/dsh-host-webserver': + specifier: workspace:^ + version: link:../webserver + '@deepseek-ai/dsh-invariants': + specifier: workspace:^ + version: link:../../support/invariants + cordis: + specifier: ^4.0.0-rc.7 + version: 4.0.0-rc.7(@cordisjs/plugin-include@vendor+include)(@cordisjs/plugin-loader@vendor+loader) + packages/host/directory-picker-browse: dependencies: '@deepseek-ai/dsh-host-directory-picker': diff --git a/scripts/verify-package-readme-model-experience.ts b/scripts/verify-package-readme-model-experience.ts index fd74c6d13e..0e48167920 100644 --- a/scripts/verify-package-readme-model-experience.ts +++ b/scripts/verify-package-readme-model-experience.ts @@ -78,6 +78,7 @@ const SENTENCE_MODEL_EXPERIENCE: Readonly> = { 'packages/hooks/hook-protocol': { kind: 'indirect', reason: 'Only the hook bridge plugins render decoded hook output to a model.' }, 'packages/host/apiproxy': { kind: 'none', reason: 'The wire contract and fetch carriers move already-composed messages and register no model surface.' }, 'packages/host/directory-picker': { kind: 'none', reason: 'The GUI-host picking seam registers no model surface.' }, + 'packages/host/directory-picker-auto': { kind: 'none', reason: 'The GUI-host picking chooser only mounts a backend row; registers no model surface.' }, 'packages/host/directory-picker-browse': { kind: 'none', reason: 'The GUI-host picking backend registers no model surface.' }, 'packages/host/directory-picker-native': { kind: 'none', reason: 'The GUI-host picking backend registers no model surface.' }, 'packages/host/webserver': { kind: 'none', reason: 'The HTTP carrier bridges browser and API handler and registers no model surface.' }, diff --git a/tsconfig.host.json b/tsconfig.host.json index 2287d21a9a..69a7550593 100644 --- a/tsconfig.host.json +++ b/tsconfig.host.json @@ -172,6 +172,14 @@ { "path": "./packages/mcp/mcp-client" }, { "path": "./packages/host/apiproxy" }, { "path": "./packages/host/directory-picker" }, + { "path": "./packages/host/directory-picker-auto" }, + // Dual-face backend leaves stay client-registered (their tests and client + // halves are excluded above); these references only let the adaptive + // chooser's composition test import each backend's NODE entry, whose + // declarations carry no client-side Context merge — the mirror of the + // client aggregate's webserver reference. + { "path": "./packages/host/directory-picker-browse" }, + { "path": "./packages/host/directory-picker-native" }, { "path": "./packages/host/webserver" }, { "path": "./packages/sdk/sdk-client" }, { "path": "./packages/sdk/helper" }, From f0f897ef029a448172c48de4cf48e3c066a4ea35 Mon Sep 17 00:00:00 2001 From: creatixchu Date: Wed, 29 Jul 2026 21:09:02 +0800 Subject: [PATCH 06/17] fix(host): address ds-review-bot v7 on the adaptive picker chooser - resolve.ts: gate the display branch on linux (the native backend drives exactly darwin/win32/linux) and require a zenity/kdialog binary on PATH, probed once at boot (new probe.ts, injected predicate for tests); type bindHost as the webserver schema's closed union. - index.ts: the disposer now joins the removed entry's fiber teardown so unloading the chooser settles only after the backend quiesced; export BACKEND_PACKAGES as the runtime-string source of truth. - verify-cordis-config: a composition mounting -auto must declare both backends as dependencies (negative-tested), since keyless Linux CI only ever resolves browse and would hide a dropped -native dep. - apps/web scaffold: pin -browse via disable+insert (goldens are interaction-specific); fix the stale workspace-flow comment. - docs/module-graph.md regenerated; README + Agent Note document the ssh -L shape, the PATH-only probe, and the new gate (zh pairs re-paired). - composition spec: assert teardown quiescence without a loader await, cover external entry removal, and await the loader's self-dispose disabled-persist so it cannot race temp-dir teardown. --- ...irectory-picker-adaptive-default.i18n.yaml | 4 +- ...07-29-directory-picker-adaptive-default.md | 5 +- ...29-directory-picker-adaptive-default.zh.md | 5 +- apps/web/tests/scaffold.ts | 8 +++ apps/web/tests/workspace-flow.snapshot.ts | 3 +- docs/module-graph.md | 6 ++ .../directory-picker-auto/README.i18n.yaml | 4 +- packages/host/directory-picker-auto/README.md | 5 +- .../host/directory-picker-auto/README.zh.md | 5 +- .../host/directory-picker-auto/src/index.ts | 43 ++++++++---- .../host/directory-picker-auto/src/probe.ts | 44 ++++++++++++ .../host/directory-picker-auto/src/resolve.ts | 23 ++++--- .../tests/loader-composition.spec.ts | 57 +++++++++++++--- .../tests/resolve.spec.ts | 68 +++++++++++++++++-- scripts/verify-cordis-config.ts | 26 ++++++- 15 files changed, 253 insertions(+), 53 deletions(-) create mode 100644 packages/host/directory-picker-auto/src/probe.ts diff --git a/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.i18n.yaml b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.i18n.yaml index cd921886f6..3ade5b93e1 100644 --- a/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.i18n.yaml +++ b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write .agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md -2026-07-29-directory-picker-adaptive-default.md: ae5748259f162503300360d6cca43bec996afdb6 -2026-07-29-directory-picker-adaptive-default.zh.md: acabeb80604b473289fa441e6d9d913e4a8f87d0 +2026-07-29-directory-picker-adaptive-default.md: 7ff6529bb8e445f63343b1019ac520f56b19d5e4 +2026-07-29-directory-picker-adaptive-default.zh.md: a2a2d8ec4c91a347eedfc3aa3413b091ee847934 diff --git a/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md index ae5748259f..7ff6529bb8 100644 --- a/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md +++ b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.md @@ -10,7 +10,7 @@ The [directory-picker seam](../architecture/2026-07-28-directory-picker-capabili ## Decision -A third sibling package, **`dsh-host-directory-picker-auto`**: a node-half-only *chooser* that owns no picking code and no UI. Its `apply` samples the host facts exactly once at boot — bind host from the injected `httpServer` (a new `host` getter mirrors the existing `port`), `SSH_CONNECTION`/`SSH_TTY`, platform, `DISPLAY`/`WAYLAND_DISPLAY` — resolves them through one exported pure function, and mounts the chosen dual-face backend with `ctx.loader.create({name})` into the Loader's **in-memory root tree**; the effect's disposer removes the entry again. `native` requires every attended-host signal (loopback bind ∧ no SSH markers ∧ display session, assumed on darwin/win32); anything ambiguous resolves to `browse`, which works everywhere. `apps/cli` now mounts `-auto` as its `directory-picker` row; composing `-native` or `-browse` directly remains the pin. +A third sibling package, **`dsh-host-directory-picker-auto`**: a node-half-only *chooser* that owns no picking code and no UI. Its `apply` samples the host facts exactly once at boot — bind host from the injected `httpServer` (a new `host` getter mirrors the existing `port`), `SSH_CONNECTION`/`SSH_TTY`, platform, `DISPLAY`/`WAYLAND_DISPLAY`, and a `PATH` probe for a Linux chooser binary (zenity/kdialog) — resolves them through one exported pure function, and mounts the chosen dual-face backend with `ctx.loader.create({name})` into the Loader's **in-memory root tree**; the effect's disposer removes the entry and joins the backend fiber's teardown (`remove()` alone only starts it), so unloading the chooser settles only after the backend quiesced. `native` requires every attended-and-servable signal: loopback bind ∧ no SSH markers ∧ a display session the native backend can drive — assumed on darwin/win32, requiring `DISPLAY`/`WAYLAND_DISPLAY` plus a chooser binary on linux, and never true elsewhere (the native backend supports exactly darwin/win32/linux). Anything ambiguous resolves to `browse`, which works everywhere. `apps/cli` now mounts `-auto` as its `directory-picker` row; composing `-native` or `-browse` directly remains the pin. Why entry-level mounting is the load-bearing mechanism: the client module table (`dsh-client-modules`) reconciles **Loader entries** reactively over `internal/plugin`, so a backend mounted as a real entry gets its browser half discovered exactly as a config-row's would be — the seam's one-row-swaps-both-faces invariant survives adaptivity with zero duplicated client code. The dev HMR row (`AppCLIEntry`) is the mechanism precedent. Root-tree targeting matters: the root tree's `write()` is a no-op, so the resolved row can never be persisted back into `cordis.yml` (the Include subtree *does* write). @@ -23,7 +23,8 @@ Why entry-level mounting is the load-bearing mechanism: the client module table ## Consequences -- The shipped web GUI adapts out of the box: attended local host → OS chooser; SSH launch, all-interfaces bind, or headless host → in-app browser. Detection is a heuristic (a detached tmux session loses `SSH_*`; a non-Aqua darwin process still counts as displayed) — a wrong `native` choice degrades to the backend's existing retryable failure dialog, and composing `-browse` pins the safe interaction. +- The shipped web GUI adapts out of the box: attended local host → OS chooser; SSH launch, all-interfaces bind, headless host, unsupported platform, or Linux without a chooser binary → in-app browser. Detection infers operator location from launch context, which no launch-side signal can prove: a detached tmux session loses `SSH_*`; a non-Aqua darwin process still counts as displayed; and the `ssh -L` shape (a workstation-local launch later reached through a forwarded port, arriving from `127.0.0.1`) resolves `native` and opens the chooser on the unattended workstation — per-connection adaptivity could not fix that last case either. A wrong `native` choice degrades to the backend's existing retryable failure dialog; deployments in these shapes compose `-browse` directly. +- The chooser mounts backends by runtime string (`BACKEND_PACKAGES`, exported), which yml-row scanning cannot see; `verify-cordis-config` therefore requires every composition mounting `-auto` to declare both backends as dependencies, so keyless Linux CI (which only ever resolves `browse`) cannot hide a dropped `-native` dependency. The shipped-tree web e2e/snapshot lane (`apps/web/tests/scaffold.ts`) pins `-browse` by disable+insert patch — its goldens are interaction-specific and must not depend on the host running the suite. - One resolution per boot keeps the seam's capability-stability contract; per-connection shapes remain out of scope until a deployment demands them. - Mounting the chooser **and** a backend row together fails loud (duplicate `directoryPicker` service; duplicate flow in the `single` holes). - The host typecheck aggregate now references the two backend projects (declarations only, node entries carry no client merge) so the chooser's REAL-composition test can mount them — the mirror of the client aggregate's `webserver` reference. diff --git a/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.zh.md b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.zh.md index acabeb8060..a2a2d8ec4c 100644 --- a/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.zh.md +++ b/.agents/notes/implemented/feature/2026-07-29-directory-picker-adaptive-default.zh.md @@ -10,7 +10,7 @@ ## 决策 -第三个同级包 **`dsh-host-directory-picker-auto`**:一个只有 node 半侧的*选择器*,不持有任何选取代码,也没有 UI。它的 `apply` 在启动时恰好采样一次宿主事实——从注入的 `httpServer` 读绑定宿主(新增的 `host` getter 与既有的 `port` 对称)、`SSH_CONNECTION`/`SSH_TTY`、平台、`DISPLAY`/`WAYLAND_DISPLAY`——经由一个导出的纯函数判定,再用 `ctx.loader.create({name})` 把选中的双面后端挂进 Loader 的**内存根树**;该 effect 的 disposer 会再次移除该条目。`native` 要求全部有人值守宿主信号(回环绑定 ∧ 无 SSH 标记 ∧ 显示会话,darwin/win32 上视为存在);任何含糊情形都判定为处处可用的 `browse`。`apps/cli` 现在把 `-auto` 挂为它的 `directory-picker` 行;直接组合 `-native` 或 `-browse` 仍是固定交互的方式。 +第三个同级包 **`dsh-host-directory-picker-auto`**:一个只有 node 半侧的*选择器*,不持有任何选取代码,也没有 UI。它的 `apply` 在启动时恰好采样一次宿主事实——从注入的 `httpServer` 读绑定宿主(新增的 `host` getter 与既有的 `port` 对称)、`SSH_CONNECTION`/`SSH_TTY`、平台、`DISPLAY`/`WAYLAND_DISPLAY`、以及对 Linux 选择器二进制(zenity/kdialog)的一次 `PATH` 探查——经由一个导出的纯函数判定,再用 `ctx.loader.create({name})` 把选中的双面后端挂进 Loader 的**内存根树**;该 effect 的 disposer 会移除该条目并汇入后端 fiber 的拆卸(单靠 `remove()` 只是启动拆卸),因此卸载选择器要到后端静止之后才落定。`native` 要求全部“有人值守且可服务”信号:回环绑定 ∧ 无 SSH 标记 ∧ native 后端能驱动的显示会话——darwin/win32 上视为存在,linux 上要求 `DISPLAY`/`WAYLAND_DISPLAY` 外加一个选择器二进制,其余平台一律不成立(native 后端恰好支持 darwin/win32/linux)。任何含糊情形都判定为处处可用的 `browse`。`apps/cli` 现在把 `-auto` 挂为它的 `directory-picker` 行;直接组合 `-native` 或 `-browse` 仍是固定交互的方式。 条目级挂载之所以是承重机制:client 模块表(`dsh-client-modules`)基于 `internal/plugin` 对 **Loader 条目**做响应式协调,因此以真实条目挂载的后端,其 browser half 被发现的方式与配置行完全相同——seam 的“一行同时换两面”不变式在自适应下依然成立,且没有一行重复的 client 代码。开发环境的 HMR 行(`AppCLIEntry`)是该机制的先例。瞄准根树很关键:根树的 `write()` 是 no-op,因此判定出的行绝不会被持久化回 `cordis.yml`(Include 子树*会*写回)。 @@ -23,7 +23,8 @@ ## 后果 -- 随附的 web GUI 开箱即自适应:有人值守的本地宿主 → OS 选择器;SSH 启动、全网卡绑定或无头宿主 → 应用内浏览器。探测是启发式的(脱离的 tmux 会话会丢失 `SSH_*`;非 Aqua 的 darwin 进程仍被算作有显示)——错误的 `native` 选择会退化为后端既有的可重试失败对话框,组合 `-browse` 即固定住安全的交互。 +- 随附的 web GUI 开箱即自适应:有人值守的本地宿主 → OS 选择器;SSH 启动、全网卡绑定、无头宿主、不支持的平台,或没有选择器二进制的 Linux → 应用内浏览器。探测是从启动上下文推断操作者位置,而任何启动侧信号都无法证明这一点:脱离的 tmux 会话会丢失 `SSH_*`;非 Aqua 的 darwin 进程仍被算作有显示;而 `ssh -L` 形态(在工作站本地启动、之后经转发端口访问,从 `127.0.0.1` 到达)会判定 `native`,把选择器弹在无人值守的工作站上——即便按连接自适应也修不了最后这一情形。错误的 `native` 选择会退化为后端既有的可重试失败对话框;处于这些形态的部署直接组合 `-browse`。 +- 选择器按运行时字符串(已导出的 `BACKEND_PACKAGES`)挂载后端,yml 行扫描看不到这一点;因此 `verify-cordis-config` 要求每个挂载 `-auto` 的组合把两个后端都声明为依赖,使无密钥的 Linux CI(它永远只会判定出 `browse`)无法掩盖被丢掉的 `-native` 依赖。随附树的 web e2e/快照通道(`apps/web/tests/scaffold.ts`)以 disable+insert 补丁固定 `-browse`——其 golden 是交互特定的,绝不能依赖运行该套件的宿主。 - 每次启动只判定一次,维持 seam 的能力稳定性契约;按连接的形态在有部署提出需求前仍不在范围内。 - 同时挂载选择器**和**某个后端行会大声失败(重复的 `directoryPicker` 服务;`single` 洞中的重复流程)。 - host 类型检查聚合现在引用两个后端项目(仅声明,node 入口不携带 client 合并),使选择器的 REAL-composition 测试能挂载它们——与 client 聚合对 `webserver` 的引用互为镜像。 diff --git a/apps/web/tests/scaffold.ts b/apps/web/tests/scaffold.ts index 753cdc2953..d7970036f9 100644 --- a/apps/web/tests/scaffold.ts +++ b/apps/web/tests/scaffold.ts @@ -175,6 +175,14 @@ export async function launchWebScaffold(options: LaunchOptions = {}): Promise pkg_invariants pkg_code_runtime_worker --> pkg_session pkg_code_runtime_worker --> pkg_timeout + pkg_host_directory_picker_auto --> pkg_host_directory_picker_browse + pkg_host_directory_picker_auto --> pkg_host_directory_picker_native + pkg_host_directory_picker_auto --> pkg_host_webserver + pkg_host_directory_picker_auto --> pkg_invariants pkg_lsp_local --> pkg_brand pkg_lsp_local --> pkg_invariants pkg_lsp_local --> pkg_llm @@ -1041,6 +1046,7 @@ flowchart TD | [`client-ui-command`](../packages/client/ui-command) | `client` | [`client-connection`](../packages/client/connection), [`client-runtime`](../packages/client/runtime), [`client-ui-conversation`](../packages/client/ui-conversation), [`client-ui-primitives`](../packages/client/ui-primitives), [`client-ui-slash`](../packages/client/ui-slash), [`client-ui-slots`](../packages/client/ui-slots), [`invariants`](../packages/support/invariants) | | [`client-ui-layout`](../packages/client/ui-layout) | `client` | [`client-runtime`](../packages/client/runtime), [`client-ui-slots`](../packages/client/ui-slots), [`client-ui-theme`](../packages/client/ui-theme), [`invariants`](../packages/support/invariants) | | [`code-runtime-worker`](../packages/code-runtime/code-runtime-worker) | `code-runtime` | [`code-runtime`](../packages/code-runtime/code-runtime), [`invariants`](../packages/support/invariants), [`session`](../packages/core/session), [`timeout`](../packages/util/timeout) | +| [`host-directory-picker-auto`](../packages/host/directory-picker-auto) | `host` | [`host-directory-picker-browse`](../packages/host/directory-picker-browse), [`host-directory-picker-native`](../packages/host/directory-picker-native), [`host-webserver`](../packages/host/webserver), [`invariants`](../packages/support/invariants) | | [`lsp-local`](../packages/lsp/lsp-local) | `lsp` | [`brand`](../packages/util/brand), [`invariants`](../packages/support/invariants), [`llm`](../packages/llm/llm), [`lsp`](../packages/lsp/lsp), [`subprocess`](../packages/subprocess/subprocess), [`timeout`](../packages/util/timeout) | | [`sandbox-local`](../packages/sandbox/sandbox-local) | `sandbox` | [`invariants`](../packages/support/invariants), [`llm`](../packages/llm/llm), [`sandbox`](../packages/sandbox/sandbox) | | [`sandbox-policy`](../packages/sandbox/sandbox-policy) | `sandbox` | [`invariants`](../packages/support/invariants), [`sandbox`](../packages/sandbox/sandbox), [`session`](../packages/core/session) | diff --git a/packages/host/directory-picker-auto/README.i18n.yaml b/packages/host/directory-picker-auto/README.i18n.yaml index fa4907698b..ea430abe70 100644 --- a/packages/host/directory-picker-auto/README.i18n.yaml +++ b/packages/host/directory-picker-auto/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/host/directory-picker-auto/README.md -README.md: 73692d1fb5af1e7b23b2a99d0a5cd48507f68ce5 -README.zh.md: 5bc9ced01f86d021db256df2c9e69e97ec6a7ab1 +README.md: 10d1784590b79fdfef3cf6683d389182cd8437b6 +README.zh.md: 86ec9f2c3a87557e86038ce7d3f89887c5bb3546 diff --git a/packages/host/directory-picker-auto/README.md b/packages/host/directory-picker-auto/README.md index 73692d1fb5..10d1784590 100644 --- a/packages/host/directory-picker-auto/README.md +++ b/packages/host/directory-picker-auto/README.md @@ -4,7 +4,7 @@ English | [中文](README.zh.md) The **adaptive chooser** of the [directory-picker seam](../directory-picker/README.md): a node-half-only plugin that resolves the host's situation once at boot and mounts the matching dual-face backend — [`-native`](../directory-picker-native/README.md) or [`-browse`](../directory-picker-browse/README.md) — as a real Loader entry in the in-memory root tree (never persisted to a config file; the root tree's `write()` is a no-op). Because the backend arrives as an ordinary entry, its browser half is discovered by the client module table exactly as a config-row's would be, so the seam's one-row-swaps-both-faces invariant holds for the resolved choice. Unloading the chooser removes the entry again, unloading both faces with it. -Resolution is one pure boot-time sample (`resolveDirectoryPickerBackend`), exported for reuse and tests. `native` requires every signal that the operator can see the host display: a loopback-only bind (read from the injected `httpServer`; an all-interfaces bind admits remote browsers no OS chooser can reach), no SSH launch (`SSH_CONNECTION`/`SSH_TTY` unset or blank — under SSH port-forwarding the chooser would open on the unattended server), and a display session (assumed on darwin/win32; `DISPLAY`/`WAYLAND_DISPLAY` elsewhere). Anything ambiguous resolves to `browse`, which works everywhere. The sample happens exactly once per boot so the mounted capability stays stable for the service lifetime, as the seam requires. Pinning an interaction is not a config field here — compose the `-native` or `-browse` row directly instead of this one, the seam's documented swap point; mounting the chooser **and** a backend row together fails loud (duplicate `directoryPicker` service, duplicate client flow in the `single` holes). +Resolution is one pure boot-time sample (`resolveDirectoryPickerBackend`), exported for reuse and tests. `native` requires every signal that the operator can see the host display and the native backend can serve it: a loopback-only bind (read from the injected `httpServer`; an all-interfaces bind admits remote browsers no OS chooser can reach), no SSH launch (`SSH_CONNECTION`/`SSH_TTY` unset or blank — under SSH port-forwarding the chooser would open on the unattended server), and a servable display session — assumed on darwin/win32; on linux `DISPLAY`/`WAYLAND_DISPLAY` plus a zenity or kdialog binary on `PATH` (the probe is one more boot-time fact); never on any other platform, since the native backend drives exactly darwin/win32/linux. Anything ambiguous resolves to `browse`, which works everywhere. The sample happens exactly once per boot so the mounted capability stays stable for the service lifetime, as the seam requires. Pinning an interaction is not a config field here — compose the `-native` or `-browse` row directly instead of this one, the seam's documented swap point; mounting the chooser **and** a backend row together fails loud (duplicate `directoryPicker` service, duplicate client flow in the `single` holes). ## Model Experience @@ -16,5 +16,6 @@ None; this package neither assembles nor sends a provider request. ## Known Limitations and Deferred Work -- **Detection is a heuristic, not a proof** — a tmux session detached from its SSH launch loses the `SSH_*` markers, and a darwin process outside an Aqua session still counts as displayed; a wrong `native` choice degrades to the backend's existing retryable failure dialog, and composing `-browse` directly pins the safe interaction. +- **Detection infers operator location from launch context, which no launch-side signal can prove** — a tmux session detached from its SSH launch loses the `SSH_*` markers; a darwin process outside an Aqua session still counts as displayed; and the `ssh -L` shape (a workstation-local launch later reached through a forwarded port, which arrives from `127.0.0.1`) resolves `native` and opens the chooser on the unattended workstation. A wrong `native` choice degrades to the backend's existing retryable failure dialog, and composing `-browse` directly pins the safe interaction for such deployments. +- **The Linux chooser probe reads `PATH` only** — a zenity/kdialog reachable some other way (shell alias, non-PATH install) still resolves `browse`; installing either binary on `PATH` restores `native` eligibility at the next boot. - **Boot-time only** — one resolution serves every client of the boot; per-connection adaptivity (native for a local browser, browse for a remote one, same server) would need a per-client capability and the wire advertisement the seam deliberately deleted, and waits for a deployment that serves both at once. diff --git a/packages/host/directory-picker-auto/README.zh.md b/packages/host/directory-picker-auto/README.zh.md index 5bc9ced01f..86ec9f2c3a 100644 --- a/packages/host/directory-picker-auto/README.zh.md +++ b/packages/host/directory-picker-auto/README.zh.md @@ -4,7 +4,7 @@ [目录选择 seam](../directory-picker/README.md) 的**自适应选择器**:一个只有 node 半侧的插件,在启动时一次性判定宿主处境,并把匹配的双面后端——[`-native`](../directory-picker-native/README.md) 或 [`-browse`](../directory-picker-browse/README.md)——作为真实的 Loader 条目挂进内存根树(绝不持久化到配置文件;根树的 `write()` 是 no-op)。由于后端以普通条目的形式到达,其 browser half 被 client 模块表发现的方式与配置行完全相同,因此对判定出的选择,seam 的“一行同时换两面”不变式依然成立。卸载该选择器会再次移除该条目,连同两面一起卸载。 -判定是一次纯函数的启动时采样(`resolveDirectoryPickerBackend`),已导出供复用与测试。`native` 要求“操作者看得到宿主屏幕”的全部信号:仅回环的绑定(从注入的 `httpServer` 读取;全网卡绑定会接入任何 OS 选择器都触及不到的远程浏览器);非 SSH 启动(`SSH_CONNECTION`/`SSH_TTY` 未设置或为空——SSH 端口转发下选择器会弹在无人值守的服务器上);以及存在显示会话(darwin/win32 上视为存在,其余平台看 `DISPLAY`/`WAYLAND_DISPLAY`)。任何含糊情形都判定为处处可用的 `browse`。采样每次启动恰好发生一次,因此挂载的能力在服务生命周期内保持稳定,符合 seam 的要求。固定某种交互在这里不是配置字段——直接组合 `-native` 或 `-browse` 行来替代本行,那才是 seam 文档化的切换点;同时挂载选择器**和**某个后端行会大声失败(重复的 `directoryPicker` 服务、`single` 洞中的重复 client 流程)。 +判定是一次纯函数的启动时采样(`resolveDirectoryPickerBackend`),已导出供复用与测试。`native` 要求“操作者看得到宿主屏幕、且 native 后端能服务它”的全部信号:仅回环的绑定(从注入的 `httpServer` 读取;全网卡绑定会接入任何 OS 选择器都触及不到的远程浏览器);非 SSH 启动(`SSH_CONNECTION`/`SSH_TTY` 未设置或为空——SSH 端口转发下选择器会弹在无人值守的服务器上);以及可服务的显示会话——darwin/win32 上视为存在;linux 上要求 `DISPLAY`/`WAYLAND_DISPLAY`,外加 `PATH` 上有 zenity 或 kdialog 二进制(该探查是又一项启动时事实);其余任何平台上都不成立,因为 native 后端驱动的平台恰为 darwin/win32/linux。任何含糊情形都判定为处处可用的 `browse`。采样每次启动恰好发生一次,因此挂载的能力在服务生命周期内保持稳定,符合 seam 的要求。固定某种交互在这里不是配置字段——直接组合 `-native` 或 `-browse` 行来替代本行,那才是 seam 文档化的切换点;同时挂载选择器**和**某个后端行会大声失败(重复的 `directoryPicker` 服务、`single` 洞中的重复 client 流程)。 ## 模型体验 @@ -16,5 +16,6 @@ ## 已知限制与延期工作 -- **探测是启发式,不是证明**——从 SSH 启动中脱离的 tmux 会话会丢失 `SSH_*` 标记,Aqua 会话之外的 darwin 进程也仍被算作有显示;错误的 `native` 选择会退化为后端既有的可重试失败对话框,而直接组合 `-browse` 即固定住安全的交互。 +- **探测是从启动上下文推断操作者位置,而任何启动侧信号都无法证明这一点**——从 SSH 启动中脱离的 tmux 会话会丢失 `SSH_*` 标记;Aqua 会话之外的 darwin 进程仍被算作有显示;而 `ssh -L` 形态(在工作站本地启动、之后经转发端口访问,从 `127.0.0.1` 到达)会判定 `native`,把选择器弹在无人值守的工作站上。错误的 `native` 选择会退化为后端既有的可重试失败对话框,而对这类部署,直接组合 `-browse` 即固定住安全的交互。 +- **Linux 选择器探查只读 `PATH`**——以其他途径可用的 zenity/kdialog(shell 别名、未装在 PATH 上)仍判定为 `browse`;把任一二进制装到 `PATH` 上,下次启动即恢复 `native` 资格。 - **仅在启动时判定**——一次判定服务本次启动的所有客户端;按连接自适应(同一台服务器,本地浏览器用 native、远程浏览器用 browse)需要按客户端的能力对象以及 seam 有意删除的 wire 广播,等到出现同时服务两种形态的部署再做。 diff --git a/packages/host/directory-picker-auto/src/index.ts b/packages/host/directory-picker-auto/src/index.ts index 12568d8f8b..5766e36b98 100644 --- a/packages/host/directory-picker-auto/src/index.ts +++ b/packages/host/directory-picker-auto/src/index.ts @@ -1,12 +1,12 @@ /** * Adaptive chooser of the directory-picker seam: resolves the host's - * situation once at boot (bind host, SSH launch, display session) and mounts - * the matching dual-face backend — `-native` or `-browse` — as a real Loader - * entry in the in-memory root tree. Because the backend arrives as an - * ordinary entry, its browser half is discovered exactly as a config-row's - * would be, so the seam's one-row-swaps-both-faces invariant holds for the - * resolved choice; pinning an interaction remains composing that backend row - * directly instead of this one. + * situation once at boot (bind host, SSH launch, display session, Linux + * chooser binary) and mounts the matching dual-face backend — `-native` or + * `-browse` — as a real Loader entry in the in-memory root tree. Because the + * backend arrives as an ordinary entry, its browser half is discovered + * exactly as a config-row's would be, so the seam's one-row-swaps-both-faces + * invariant holds for the resolved choice; pinning an interaction remains + * composing that backend row directly instead of this one. * @module @deepseek-ai/dsh-host-directory-picker-auto */ @@ -14,9 +14,11 @@ import type { Context } from 'cordis' // Empty type imports carry the `loader` and `httpServer` Context merges for the reads below. import type {} from '@cordisjs/plugin-loader' import type {} from '@deepseek-ai/dsh-host-webserver' +import { canExecute, hasLinuxChooserBinary } from './probe.ts' import type { DirectoryPickerBackendKind } from './resolve.ts' import { resolveDirectoryPickerBackend } from './resolve.ts' +export { canExecute, hasLinuxChooserBinary } from './probe.ts' export type { DirectoryPickerBackendKind, DirectoryPickerEnv, DirectoryPickerHostFacts } from './resolve.ts' export { resolveDirectoryPickerBackend } from './resolve.ts' @@ -25,16 +27,22 @@ export const name = 'directory-picker-auto' /** Required services: the effective bind host (`httpServer`) and the entry tree the backend mounts into (`loader`). */ export const inject = ['httpServer', 'loader'] -/** Backend package per resolved kind — fixed composition vocabulary, not a tunable. */ -const BACKEND_PACKAGES: Record = { +/** + * Backend package per resolved kind — fixed composition vocabulary, not a + * tunable. Exported because the reference is a runtime string the static + * config gate cannot see in a yml row: `verify-cordis-config` requires every + * app composing this chooser to declare both values as dependencies. + */ +export const BACKEND_PACKAGES: Record = { native: '@deepseek-ai/dsh-host-directory-picker-native', browse: '@deepseek-ai/dsh-host-directory-picker-browse', } /** * Resolve the backend from one boot-time sample and mount it as a Loader - * entry; the effect's disposer removes the entry, so unloading this plugin - * unloads both faces of the mounted backend with it. + * entry; the effect's disposer removes the entry and joins the backend + * fiber's teardown, so unloading this plugin returns only after both faces + * of the mounted backend (and their dependents) quiesced. * @param ctx - cordis context carrying the injected `httpServer` and `loader`. */ export async function apply(ctx: Context): Promise { @@ -42,11 +50,22 @@ export async function apply(ctx: Context): Promise { bindHost: ctx.httpServer.host, platform: process.platform, env: process.env, + linuxChooser: hasLinuxChooserBinary(process.env.PATH, canExecute), }) await ctx.effect(async () => { // Root-tree create: the Loader root is in-memory (write() is a no-op), so // the mounted row can never be persisted back into a config file. const id = await ctx.loader.create({ name: BACKEND_PACKAGES[backend] }) - return () => { ctx.loader.remove(id) } + return async () => { + // Tree teardown (group.stop) can have removed the entry already; + // nothing is left to unmount or await then. + const entry = ctx.loader.store[id] + if (entry === undefined) return + const fiber = entry.fiber + ctx.loader.remove(id) + // remove() only starts the fiber's dispose; join it so the chooser's + // unload signals completion only after the backend quiesced. + await fiber?.dispose() + } }, 'directory-picker-auto: backend entry') } diff --git a/packages/host/directory-picker-auto/src/probe.ts b/packages/host/directory-picker-auto/src/probe.ts new file mode 100644 index 0000000000..17fd0c99f8 --- /dev/null +++ b/packages/host/directory-picker-auto/src/probe.ts @@ -0,0 +1,44 @@ +/** + * PATH probe for the native backend's Linux chooser binaries: one boot-time + * sampled fact for the resolver, so an attended Linux host without + * zenity/kdialog keeps the working `browse` interaction instead of a backend + * whose every pick fails. + * @module @deepseek-ai/dsh-host-directory-picker-auto/probe + */ + +import { accessSync, constants } from 'node:fs' +import { delimiter, join } from 'node:path' + +/** The chooser binaries the native backend can drive on Linux (zenity, KDialog fallback). */ +const LINUX_CHOOSER_BINARIES = ['zenity', 'kdialog'] as const + +/** + * Whether the current process may execute the candidate path. + * @param candidate - absolute or PATH-joined file path. + * @returns true only for an existing executable file. + */ +export function canExecute(candidate: string): boolean { + try { + accessSync(candidate, constants.X_OK) + } catch { + // Absent or non-executable candidate — the only signals accessSync(X_OK) emits. + return false + } + return true +} + +/** + * Scan a PATH value for one of the native backend's Linux chooser binaries. + * @param pathValue - the `PATH` environment value (absent or empty scans nothing). + * @param isExecutable - executability predicate ({@link canExecute} in production; injected for deterministic tests). + * @returns whether any PATH directory holds an executable chooser binary. + */ +export function hasLinuxChooserBinary(pathValue: string | undefined, isExecutable: (candidate: string) => boolean): boolean { + for (const dir of (pathValue ?? '').split(delimiter)) { + if (dir === '') continue + for (const name of LINUX_CHOOSER_BINARIES) { + if (isExecutable(join(dir, name))) return true + } + } + return false +} diff --git a/packages/host/directory-picker-auto/src/resolve.ts b/packages/host/directory-picker-auto/src/resolve.ts index d2a22e2fa8..395e2da55f 100644 --- a/packages/host/directory-picker-auto/src/resolve.ts +++ b/packages/host/directory-picker-auto/src/resolve.ts @@ -6,6 +6,8 @@ * @module @deepseek-ai/dsh-host-directory-picker-auto/resolve */ +import type { Config as HttpServerConfig } from '@deepseek-ai/dsh-host-webserver' + /** Concrete interaction backend the resolver chooses between. */ export type DirectoryPickerBackendKind = 'native' | 'browse' @@ -16,12 +18,14 @@ export type DirectoryPickerEnv = Readonly< /** Host facts the backend choice is a pure function of, sampled once at boot. */ export interface DirectoryPickerHostFacts { - /** Effective webserver bind host (`127.0.0.1` or `0.0.0.0`). */ - bindHost: string + /** Effective webserver bind host (the schema's closed loopback/all-interfaces union). */ + bindHost: HttpServerConfig['host'] /** Host process platform. */ platform: NodeJS.Platform /** Environment sample; SSH marks a remote operator, DISPLAY/WAYLAND_DISPLAY a Linux display. */ env: DirectoryPickerEnv + /** Whether a Linux chooser binary the native backend can drive (zenity/kdialog) is on PATH; consulted only when `platform` is linux. */ + linuxChooser: boolean } /** An env value counts only when set and non-blank (an empty export is "unset" by shell convention). */ @@ -29,12 +33,14 @@ const present = (value: string | undefined): boolean => value !== undefined && v /** * Resolve which backend serves this boot. `native` requires every signal that - * the operator can see the host display: a loopback-only bind (an - * all-interfaces bind admits remote browsers no OS chooser can reach), no SSH - * launch (under SSH port-forwarding the chooser would open on the unattended - * server), and a display session (assumed on darwin/win32, `DISPLAY`/ - * `WAYLAND_DISPLAY` elsewhere). Anything ambiguous resolves to `browse`, - * which works everywhere. + * the operator can see the host display and the native backend can serve it: + * a loopback-only bind (an all-interfaces bind admits remote browsers no OS + * chooser can reach), no SSH launch (under SSH port-forwarding the chooser + * would open on the unattended server), and a servable display session — + * assumed on darwin/win32, requiring `DISPLAY`/`WAYLAND_DISPLAY` plus a + * chooser binary on linux, and never true elsewhere (the native backend + * drives exactly darwin/win32/linux). Anything ambiguous resolves to + * `browse`, which works everywhere. * @param facts - the sampled host facts. * @returns the backend kind to mount. */ @@ -42,5 +48,6 @@ export function resolveDirectoryPickerBackend(facts: DirectoryPickerHostFacts): if (facts.bindHost !== '127.0.0.1') return 'browse' if (present(facts.env.SSH_CONNECTION) || present(facts.env.SSH_TTY)) return 'browse' if (facts.platform === 'darwin' || facts.platform === 'win32') return 'native' + if (facts.platform !== 'linux' || !facts.linuxChooser) return 'browse' return present(facts.env.DISPLAY) || present(facts.env.WAYLAND_DISPLAY) ? 'native' : 'browse' } diff --git a/packages/host/directory-picker-auto/tests/loader-composition.spec.ts b/packages/host/directory-picker-auto/tests/loader-composition.spec.ts index 7e094035fb..ce86a8d4ec 100644 --- a/packages/host/directory-picker-auto/tests/loader-composition.spec.ts +++ b/packages/host/directory-picker-auto/tests/loader-composition.spec.ts @@ -3,10 +3,12 @@ * vendored Loader mounts the webserver row plus the adaptive chooser, and the * assertions observe the durable outcome — which backend entry the chooser * mounted into the Loader store, the capability the seam then serves, and - * that disposing the chooser removes the mounted entry again (HMR safety). + * that disposing the chooser removes the mounted entry again (HMR safety), + * joining the backend's own teardown before the disposer settles. */ -import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' +import { chmodSync, mkdirSync, mkdtempSync, writeFileSync } from 'node:fs' +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { pathToFileURL } from 'node:url' @@ -25,21 +27,27 @@ const NATIVE = '@deepseek-ai/dsh-host-directory-picker-native' const BROWSE = '@deepseek-ai/dsh-host-directory-picker-browse' let root: string | undefined +let fakeBin: string | undefined let context: Context | undefined afterEach(async () => { vi.unstubAllEnvs() await context?.fiber.dispose() context = undefined - if (root !== undefined) await rm(root, { recursive: true, force: true }) + for (const dir of [root, fakeBin]) { + // maxRetries absorbs teardown stragglers (e.g. an unawaited fiber's late + // file handle) that can otherwise race the recursive scan into ENOTEMPTY. + if (dir !== undefined) await rm(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 50 }) + } root = undefined + fakeBin = undefined }) /** Write a dist fixture and a two-row cordis.yml (webserver + chooser), then boot it through the real Loader. */ async function loadComposition(bindHost: '127.0.0.1' | '0.0.0.0'): Promise<{ ctx: Context; configPath: string }> { root = await mkdtemp(join(tmpdir(), 'dsh-directory-picker-auto-')) const dist = join(root, 'dist') - await mkdir(dist) + mkdirSync(dist) const distIndex = join(dist, 'index.html') await writeFile(distIndex, 'shell') const configPath = join(root, 'cordis.yml') @@ -83,17 +91,26 @@ function entryNames(ctx: Context): string[] { return [...ctx.loader.entries()].map(entry => entry.options.name) } -/** Force every signal of an attended host: no SSH launch, a display on any platform. */ +/** + * Force every signal of an attended host on any platform: no SSH launch, a + * display, and a PATH holding one executable chooser binary so the real + * probe resolves identically on hosts with and without zenity/kdialog. + */ function stubAttendedHost(): void { + fakeBin = mkdtempSync(join(tmpdir(), 'dsh-picker-bin-')) + const zenity = join(fakeBin, 'zenity') + writeFileSync(zenity, '#!/bin/sh\n') + chmodSync(zenity, 0o755) + vi.stubEnv('PATH', fakeBin) vi.stubEnv('SSH_CONNECTION', '') vi.stubEnv('SSH_TTY', '') vi.stubEnv('DISPLAY', ':0') } describe('real Loader composition', () => { - // Real-Loader composition resolves workspace packages through tsx at test - // time; first resolution after the host/client program split is slow enough - // to trip the default 5s budget on cold caches. + // The 60s budget covers this file's static imports (webserver plus both + // backend node halves through tsx), which dominate on cold caches; the + // Loader itself resolves nothing here — `loader.internal` is a module map. it('mounts the native backend for an attended loopback host and unmounts it on disposal', { timeout: 60_000 }, async () => { stubAttendedHost() const { ctx, configPath } = await loadComposition('127.0.0.1') @@ -110,12 +127,19 @@ describe('real Loader composition', () => { // booted config file must never gain the resolved backend row. expect(await readFile(configPath, 'utf8')).not.toContain(NATIVE) - // HMR safety: disposing the chooser's fiber removes the entry it created. + // HMR safety: disposing the chooser's fiber removes the entry it created, + // and the disposer joins the backend's teardown — the service is gone the + // moment dispose() settles, with no further loader await. const autoEntry = [...ctx.loader.entries()].find(entry => entry.options.name === AUTO)! await autoEntry.fiber!.dispose() - await ctx.loader.await() expect(entryNames(ctx)).not.toContain(NATIVE) expect(ctx.get('directoryPicker')).toBeUndefined() + // Self-disposing an include-tree entry persists `disabled: true` (loader + // behavior, not the chooser's); await that debounced write so it cannot + // race the temp-dir removal, and pin that the persisted row is the + // chooser itself — the resolved backend still never reaches the file. + await expect.poll(async () => await readFile(configPath, 'utf8')).toContain('disabled: true') + expect(await readFile(configPath, 'utf8')).not.toContain(NATIVE) }) it('mounts the browse backend under an SSH launch', { timeout: 60_000 }, async () => { @@ -136,4 +160,17 @@ describe('real Loader composition', () => { expect(entryNames(ctx)).toContain(BROWSE) expect(entryNames(ctx)).not.toContain(NATIVE) }) + + it('tolerates the mounted entry being removed by the tree before the chooser unloads', { timeout: 60_000 }, async () => { + stubAttendedHost() + const { ctx, configPath } = await loadComposition('127.0.0.1') + + const backendEntry = [...ctx.loader.entries()].find(entry => entry.options.name === NATIVE)! + ctx.loader.remove(backendEntry.id) + const autoEntry = [...ctx.loader.entries()].find(entry => entry.options.name === AUTO)! + await expect(autoEntry.fiber!.dispose()).resolves.not.toThrow() + expect(entryNames(ctx)).not.toContain(NATIVE) + // Same self-dispose persistence as above: let the write land before teardown. + await expect.poll(async () => await readFile(configPath, 'utf8')).toContain('disabled: true') + }) }) diff --git a/packages/host/directory-picker-auto/tests/resolve.spec.ts b/packages/host/directory-picker-auto/tests/resolve.spec.ts index 3beac44961..bf8792cfa9 100644 --- a/packages/host/directory-picker-auto/tests/resolve.spec.ts +++ b/packages/host/directory-picker-auto/tests/resolve.spec.ts @@ -1,12 +1,17 @@ -import { describe, expect, it } from 'vitest' +import { chmodSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { delimiter, join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { canExecute, hasLinuxChooserBinary } from '../src/probe.ts' import { resolveDirectoryPickerBackend } from '../src/resolve.ts' import type { DirectoryPickerHostFacts } from '../src/resolve.ts' -/** Baseline facts that resolve to `native`; each case overrides one signal. */ +/** Baseline facts that resolve to `native`; each case overrides one signal (darwin never consults `linuxChooser`). */ const attended: DirectoryPickerHostFacts = { bindHost: '127.0.0.1', platform: 'darwin', env: {}, + linuxChooser: false, } describe('resolveDirectoryPickerBackend', () => { @@ -24,14 +29,63 @@ describe('resolveDirectoryPickerBackend', () => { expect(resolveDirectoryPickerBackend({ ...attended, env: { SSH_TTY: '/dev/pts/3' } })).toBe('browse') }) - it('requires a display session on platforms without an implied one', () => { - expect(resolveDirectoryPickerBackend({ ...attended, platform: 'linux' })).toBe('browse') - expect(resolveDirectoryPickerBackend({ ...attended, platform: 'linux', env: { DISPLAY: ':0' } })).toBe('native') - expect(resolveDirectoryPickerBackend({ ...attended, platform: 'linux', env: { WAYLAND_DISPLAY: 'wayland-1' } })).toBe('native') + it('requires a display session and a chooser binary on linux', () => { + const linux: DirectoryPickerHostFacts = { ...attended, platform: 'linux', linuxChooser: true } + expect(resolveDirectoryPickerBackend(linux)).toBe('browse') + expect(resolveDirectoryPickerBackend({ ...linux, env: { DISPLAY: ':0' } })).toBe('native') + expect(resolveDirectoryPickerBackend({ ...linux, env: { WAYLAND_DISPLAY: 'wayland-1' } })).toBe('native') + expect(resolveDirectoryPickerBackend({ ...linux, env: { DISPLAY: ':0' }, linuxChooser: false })).toBe('browse') + }) + + it('resolves browse on platforms the native backend cannot serve, display or not', () => { + expect(resolveDirectoryPickerBackend({ ...attended, platform: 'freebsd', env: { DISPLAY: ':0' }, linuxChooser: true })).toBe('browse') + expect(resolveDirectoryPickerBackend({ ...attended, platform: 'openbsd', env: { WAYLAND_DISPLAY: 'wayland-1' } })).toBe('browse') }) it('treats blank env exports as unset', () => { expect(resolveDirectoryPickerBackend({ ...attended, env: { SSH_CONNECTION: '', SSH_TTY: '' } })).toBe('native') - expect(resolveDirectoryPickerBackend({ ...attended, platform: 'linux', env: { DISPLAY: '', WAYLAND_DISPLAY: '' } })).toBe('browse') + expect(resolveDirectoryPickerBackend({ + ...attended, platform: 'linux', linuxChooser: true, env: { DISPLAY: '', WAYLAND_DISPLAY: '' }, + })).toBe('browse') + }) +}) + +let probeRoot: string | undefined + +afterEach(() => { + if (probeRoot !== undefined) rmSync(probeRoot, { recursive: true, force: true }) + probeRoot = undefined +}) + +describe('hasLinuxChooserBinary', () => { + it('finds a chooser binary in any PATH segment, skipping empty segments', () => { + const seen: string[] = [] + const path = ['', '/opt/none', '/usr/local/bin'].join(delimiter) + const found = hasLinuxChooserBinary(path, (candidate) => { + seen.push(candidate) + return candidate === join('/usr/local/bin', 'kdialog') + }) + expect(found).toBe(true) + expect(seen).toEqual([ + join('/opt/none', 'zenity'), join('/opt/none', 'kdialog'), + join('/usr/local/bin', 'zenity'), join('/usr/local/bin', 'kdialog'), + ]) + }) + + it('reports absence when no segment holds a chooser binary', () => { + expect(hasLinuxChooserBinary(['/a', '/b'].join(delimiter), () => false)).toBe(false) + expect(hasLinuxChooserBinary('', () => true)).toBe(false) + expect(hasLinuxChooserBinary(undefined, () => true)).toBe(false) + }) +}) + +describe('canExecute', () => { + it('accepts an executable file and rejects an absent one', () => { + probeRoot = mkdtempSync(join(tmpdir(), 'dsh-picker-probe-')) + const binary = join(probeRoot, 'zenity') + writeFileSync(binary, '#!/bin/sh\n') + chmodSync(binary, 0o755) + expect(canExecute(binary)).toBe(true) + expect(canExecute(join(probeRoot, 'kdialog'))).toBe(false) }) }) diff --git a/scripts/verify-cordis-config.ts b/scripts/verify-cordis-config.ts index 66d6e0f2a3..5ff429a2f4 100644 --- a/scripts/verify-cordis-config.ts +++ b/scripts/verify-cordis-config.ts @@ -30,6 +30,20 @@ interface PluginReference { const root = resolve(import.meta.dirname, '..') const metadataFields = ['id', 'name', 'group', 'disabled', 'inject', 'intercept', 'isolate'] as const + +/** The adaptive directory-picker chooser package (mounts a backend row at boot). */ +const CHOOSER_PACKAGE = '@deepseek-ai/dsh-host-directory-picker-auto' + +/** + * The backends the chooser mounts by runtime string (mirror of its exported + * `BACKEND_PACKAGES`), invisible to yml-row scanning: a composition mounting + * the chooser must resolve both, or keyless Linux CI (which only ever + * resolves `browse`) hides a dropped `-native` dependency until a macOS boot. + */ +const CHOOSER_BACKEND_PACKAGES = [ + '@deepseek-ai/dsh-host-directory-picker-native', + '@deepseek-ai/dsh-host-directory-picker-browse', +] const jsExprType = new yaml.Type('tag:yaml.org,2002:js', { kind: 'scalar', resolve: data => typeof data === 'string', @@ -134,12 +148,18 @@ function missingPluginDependencies( manifestPath: string, ): string[] { const requiredPackages = new Map>() + const require = (packageName: string, file: string): void => { + const locations = requiredPackages.get(packageName) ?? new Set() + locations.add(file) + requiredPackages.set(packageName, locations) + } for (const reference of references) { const packageName = packageNameFromSpecifier(reference.name) if (packageName === undefined) continue - const locations = requiredPackages.get(packageName) ?? new Set() - locations.add(reference.file) - requiredPackages.set(packageName, locations) + require(packageName, reference.file) + if (packageName === CHOOSER_PACKAGE) { + for (const backend of CHOOSER_BACKEND_PACKAGES) require(backend, reference.file) + } } return [...requiredPackages].flatMap(([packageName, locations]) => packageName in dependencies ? [] From b6bb24bfa1da69abce3c17c1f1b9ffb0bef5cfe5 Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Thu, 30 Jul 2026 13:20:47 +0800 Subject: [PATCH 07/17] docs: raise AGENTS.md and packages/README.md budget ceilings after the master merge Both files fit their ceilings on each parent; the merge union of this branch's settings rows with master's typert row and source-launch rewrite overflows by 6 and 2 words. Every added row is a fixed-format layout or package-table entry with nothing to relocate, so the ceilings move to the union size. --- scripts/doc-budgets.manifest.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/doc-budgets.manifest.json b/scripts/doc-budgets.manifest.json index 0682f78640..537abeff00 100644 --- a/scripts/doc-budgets.manifest.json +++ b/scripts/doc-budgets.manifest.json @@ -1,5 +1,5 @@ { - "AGENTS.md": 1755, + "AGENTS.md": 1765, "docs/AGENTS.md": 1150, "docs/architecture.md": 1920, "docs/cordis-primer.md": 600, @@ -7,5 +7,5 @@ "docs/testing.md": 1100, "examples/AGENTS.md": 310, "packages/AGENTS.md": 675, - "packages/README.md": 900 + "packages/README.md": 905 } From bdc6d95d561b400a08e34307520c4bdb0c838b57 Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Thu, 30 Jul 2026 13:29:52 +0800 Subject: [PATCH 08/17] fix(settings): close third-review watcher-lifecycle and write-boundary gaps Review round three found four seam defects: - A watch() disposer only removed the observer from the set; an invocation already chained onto the watcher tail still ran after disposal. Watchers now carry an active flag checked when a queued invocation would start, and the service dispose drain awaits started invocations (pendingTails) beside the write queues, so disposal is quiescent. - The settings/updated manual fan-out caught only synchronous throws; an async listener rejection escaped as an unhandled rejection. Thenable returns are now contained through the shared listener diagnostic, and the event contract documents that the INVARIANT rethrow serves synchronous listeners only. - structuredClone admitted Dates, Maps, BigInts, and cycles that YAML/JSON storage silently distorts on reload (a Date lands as a timestamp string, a Map as a plain map, a BigInt as a number). The write snapshot is now a single-pass cloneJsonShaped walk that rejects non-JSON values with their path before anything persists. - mergeLayers' per-entry undefined guard became dead code once the clone strips undefined entries at the boundary; removed, with the sparse-patch contract restated at its enforcement point. --- packages/settings/settings/src/index.ts | 142 +++++++++++++++--- .../settings/settings/tests/settings.spec.ts | 105 ++++++++++++- 2 files changed, 222 insertions(+), 25 deletions(-) diff --git a/packages/settings/settings/src/index.ts b/packages/settings/settings/src/index.ts index a7e9366048..27decc7db5 100644 --- a/packages/settings/settings/src/index.ts +++ b/packages/settings/settings/src/index.ts @@ -60,20 +60,24 @@ export interface SettingsScope { /** * Observe committed changes to this namespace's resolved value. Invocations * of one callback run asynchronously, one at a time, in commit order; a - * rejection is contained and logged like a sync throw. + * rejection is contained and logged like a sync throw. After the disposer + * returns, no further invocation starts — one already queued is skipped; + * one already started still settles, and service disposal waits for it. * @param callback - invoked after each commit with the next and previous values. * @returns the disposer removing this observer. */ watch(callback: (next: T, prev: T) => void | Promise): () => void /** * Merge a partial patch into this namespace's user layer and persist it. - * @param patch - plain-object patch over the user section. + * @param patch - plain-object patch over the user section; JSON-shaped data + * only (non-JSON values reject with their path before anything persists). */ update(patch: object): Promise /** * Replace this namespace's user section wholesale; absent keys re-inherit * the composition `base` and schema defaults (`replace({})` resets all). - * @param section - the complete next user section. + * @param section - the complete next user section; JSON-shaped data only, + * as for {@link update}. */ replace(section: object): Promise } @@ -88,6 +92,11 @@ declare module 'cordis' { * Committed change to one registered namespace's resolved value. Emitted * after the provider persisted (for `update`) or published (`provider`) * the change; never emitted when the resolved value is deep-equal. + * Listener failures are contained and logged — a sync throw and an async + * rejection alike — except `INVARIANT`-coded failures, which rethrow + * after every listener ran; that rethrow reaches the emitter only from + * synchronous listeners, so invariant checks on this event must not be + * async functions. * @param ns - the namespace whose resolved value changed. * @param next - the new resolved value. * @param prev - the previous resolved value. @@ -127,17 +136,76 @@ function isPlainObject(value: unknown): value is Record { return proto === Object.prototype || proto === null } +/** Human label for a value rejected by the JSON-shape boundary (numbers reject inline). */ +function describeRejected(value: unknown): string { + if (value === undefined) return 'undefined' + if (typeof value === 'object' && value !== null) { + const proto = Object.getPrototypeOf(value) as { constructor?: { name?: string } } | null + const name = proto?.constructor?.name + return name === undefined || name === 'Object' ? 'a non-plain object' : `a ${name}` + } + return `a ${typeof value}` +} + +/** + * Detach one write input in a single walk that doubles as the durable-boundary + * shape check: only JSON data (plain objects, arrays, strings, finite numbers, + * booleans, `null`) may reach a provider document. `structuredClone` alone + * would admit Dates, Maps, BigInts, and cycles that YAML/JSON storage then + * silently distorts on the reload round-trip. `undefined` entries in objects + * are skipped — the same sparse-patch semantics as {@link mergeLayers} — while + * an `undefined` array entry is rejected rather than coerced. + * @param root - plain-object write input (caller-checked). + * @param reject - builds the boundary error from a value label and its `$`-rooted path. + * @returns the detached JSON-shaped clone. + */ +function cloneJsonShaped( + root: Record, + reject: (label: string, path: string) => TypeError, +): Record { + const visiting = new WeakSet() + const clone = (value: unknown, path: string): unknown => { + if (value === null || typeof value === 'string' || typeof value === 'boolean') return value + if (typeof value === 'number') { + if (!Number.isFinite(value)) throw reject('a non-finite number', path) + return value + } + if (Array.isArray(value)) { + if (visiting.has(value)) throw reject('a circular reference', path) + visiting.add(value) + const entries = value.map((entry, index) => clone(entry, `${path}[${index}]`)) + // Un-mark on exit so one object referenced twice without a cycle passes. + visiting.delete(value) + return entries + } + if (isPlainObject(value)) { + if (visiting.has(value)) throw reject('a circular reference', path) + visiting.add(value) + const out: Record = {} + for (const [key, entry] of Object.entries(value)) { + if (entry === undefined) continue + out[key] = clone(entry, `${path}.${key}`) + } + visiting.delete(value) + return out + } + throw reject(describeRejected(value), path) + } + return clone(root, '$') as Record +} + /** * Layer `over` onto `under`: plain objects merge recursively, every other - * value (arrays included) replaces the lower layer wholesale, and `undefined` - * entries in `over` are ignored so a sparse patch cannot erase lower keys. + * value (arrays included) replaces the lower layer wholesale. `over` never + * carries `undefined` entries — sections come from parsed documents and write + * snapshots pass {@link cloneJsonShaped}, which strips them so a sparse patch + * cannot erase lower keys. */ function mergeLayers(under: unknown, over: unknown): unknown { if (over === undefined) return under if (!isPlainObject(under) || !isPlainObject(over)) return over const merged: Record = { ...under } for (const [key, value] of Object.entries(over)) { - if (value === undefined) continue merged[key] = key in merged ? mergeLayers(merged[key], value) : value } return merged @@ -155,6 +223,8 @@ interface SettingsWatcher { callback: (next: never, prev: never) => void | Promise /** Settled tail: invocations of this callback run one at a time, in commit order. */ tail: Promise + /** Cleared by the disposer: a queued invocation checks this before starting. */ + active: boolean } /** One live namespace registration owned by a registrant fiber. */ @@ -179,6 +249,8 @@ export abstract class Settings extends Service { private document: Record = {} /** Per-namespace write chains; settled tails, so a failure never poisons the queue. */ private readonly writeQueues = new Map>() + /** In-flight watcher invocation segments, drained by the dispose teardown. */ + private readonly pendingTails = new Set>() /** Set at service dispose: refuse new writes while queued ones drain. */ private stopped = false @@ -199,10 +271,12 @@ export abstract class Settings extends Service { */ async* [Service.init](): AsyncGenerator<() => Promise | void, void, void> { yield async () => { - // Teardown: refuse new writes, then wait until every queued write chain - // settles so disposal completes only once storage is quiescent. + // Teardown: refuse new writes and new watcher starts, then wait until + // every queued write chain and every started watcher invocation settles + // so disposal completes only once storage and observers are quiescent. + // Invocations queued but not yet started skip via the stopped check. this.stopped = true - await Promise.allSettled([...this.writeQueues.values()]) + await Promise.allSettled([...this.writeQueues.values(), ...this.pendingTails]) } this.publish(await this.load()) } @@ -252,9 +326,12 @@ export abstract class Settings extends Service { return { get: () => registration.resolved as T, watch: (callback) => { - const watcher: SettingsWatcher = { callback: callback, tail: Promise.resolve() } + const watcher: SettingsWatcher = { callback: callback, tail: Promise.resolve(), active: true } registration.watchers.add(watcher) - return () => registration.watchers.delete(watcher) + return () => { + watcher.active = false + registration.watchers.delete(watcher) + } }, update: patch => this.update(ns, patch), replace: section => this.replace(ns, section), @@ -325,13 +402,10 @@ export abstract class Settings extends Service { throw new TypeError(`settings ${verb} for "${ns}" must be a plain object`) } // Snapshot at call time: the queue must never read a caller-owned object - // the caller may keep mutating while the write waits its turn. - let snapshot: Record - try { - snapshot = structuredClone(input) - } catch { - throw new TypeError(`settings ${verb} for "${ns}" must be JSON-shaped (structured-cloneable) data`) - } + // the caller may keep mutating while the write waits its turn. The same + // walk is the JSON-shape boundary check (see cloneJsonShaped). + const snapshot = cloneJsonShaped(input, (label, path) => + new TypeError(`settings ${verb} for "${ns}" must be JSON-shaped data (found ${label} at ${path})`)) const previous = this.writeQueues.get(ns) ?? Promise.resolve() // Chain past a failed predecessor: one rejected write must not poison the // namespace queue for every later caller. @@ -407,11 +481,20 @@ export abstract class Settings extends Service { // Serialize per watcher: invocations of one callback run one at a time // in commit order, so a slow stale invocation can never apply after a // newer one. Sync throws and async rejections land in the same handler. - watcher.tail = watcher.tail - .then(() => watcher.callback(next as never, prev as never)) + // The activity check runs when the queued invocation would start, so a + // disposer (or service stop) that ran while it waited prevents the + // start entirely; started invocations drain at service dispose. + const segment = watcher.tail + .then(() => { + if (!watcher.active || this.isStopped()) return + return watcher.callback(next as never, prev as never) + }) .then(() => undefined, (error: unknown) => { this.warnWatcherFailure(registration.ns, error) }) + watcher.tail = segment + this.pendingTails.add(segment) + void segment.then(() => this.pendingTails.delete(segment)) } // Fan the event out one listener at a time (the plain emit stops at the // first throwing listener, starving the rest). Invariant violations are @@ -422,14 +505,21 @@ export abstract class Settings extends Service { const args = ['settings/updated', registration.ns, next, prev, source] for (const listener of this.ctx.events.dispatch('emit', args) as Array<(...listenerArgs: unknown[]) => unknown>) { try { - listener(registration.ns, next, prev, source) + const returned = listener(registration.ns, next, prev, source) + if (returned != null && typeof (returned as PromiseLike).then === 'function') { + // An emit listener may still be an async function; its rejection + // cannot reach the synchronous INVARIANT rethrow below, so it is + // contained here instead of becoming an unhandled rejection. + void Promise.resolve(returned as PromiseLike).then(undefined, (error: unknown) => { + this.warnListenerFailure(registration.ns, error) + }) + } } catch (error) { if ((error as { code?: unknown } | null)?.code === 'INVARIANT') { invariantFailure ??= error continue } - this.ctx.logger.warn('settings: a settings/updated listener for "%s" failed', registration.ns) - this.ctx.logger.warn(error) + this.warnListenerFailure(registration.ns, error) } } if (invariantFailure !== undefined) throw invariantFailure as Error @@ -440,6 +530,12 @@ export abstract class Settings extends Service { this.ctx.logger.warn('settings: watcher for "%s" failed', ns) this.ctx.logger.warn(error) } + + /** Contained-listener diagnostic shared by the sync and async failure paths. */ + private warnListenerFailure(ns: SettingsNamespace, error: unknown): void { + this.ctx.logger.warn('settings: a settings/updated listener for "%s" failed', ns) + this.ctx.logger.warn(error) + } } export default Settings diff --git a/packages/settings/settings/tests/settings.spec.ts b/packages/settings/settings/tests/settings.spec.ts index a989d9a5cc..cfd88b166f 100644 --- a/packages/settings/settings/tests/settings.spec.ts +++ b/packages/settings/settings/tests/settings.spec.ts @@ -228,6 +228,14 @@ describe('update', () => { expect(scope.get()).toEqual({ theme: 'light', fontSize: 18 }) }) + it('ignores an explicit undefined entry in the composition base layer', async () => { + const { ctx } = await boot({ doc: { 'ui-theme': { theme: 'light' } } }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema, { + base: { theme: undefined, fontSize: 16 }, + }) + expect(scope.get()).toEqual({ theme: 'light', fontSize: 16 }) + }) + it('rejects a non-object patch', async () => { const { ctx } = await boot() const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) @@ -433,11 +441,11 @@ describe('second review regressions', () => { expect(applied).toEqual([1, 2]) }) - it('rejects a plain object that is not structured-cloneable', async () => { + it('rejects a function value as not JSON-shaped', async () => { const { ctx } = await boot() const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) await expect(scope.update({ theme: () => 'dark' })) - .rejects.toThrow(/JSON-shaped/) + .rejects.toThrow(/JSON-shaped.*function at \$\.theme/) }) it('rejects a write still queued when the service disposes', async () => { @@ -532,6 +540,99 @@ describe('publish', () => { }) }) +describe('third review regressions', () => { + it('skips a queued watch invocation whose disposer ran before it started', async () => { + const { ctx, provider } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const watcher = vi.fn() + const dispose = scope.watch(watcher) + // The commit chains the invocation as a microtask; the disposer runs in + // the same synchronous frame, before that invocation could start. + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + dispose() + await new Promise(resolve => setTimeout(resolve, 10)) + expect(watcher).not.toHaveBeenCalled() + }) + + it('waits for an in-flight watch invocation at service dispose', async () => { + const { ctx, provider, fiber } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + let release: (() => void) | undefined + let finished = false + scope.watch(async () => { + await new Promise((resolve) => { release = resolve }) + finished = true + }) + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + await vi.waitFor(() => { expect(release).toBeDefined() }) + let disposed = false + const disposal = fiber.dispose().then(() => { disposed = true }) + await new Promise(resolve => setTimeout(resolve, 15)) + expect(disposed).toBe(false) + release!() + await disposal + expect(finished).toBe(true) + }) + + it('rejects a Date at its path before anything persists', async () => { + const { ctx, provider } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), z.object({ value: z.any() })) + await expect(scope.update({ value: { at: new Date(0) } })) + .rejects.toThrow(/JSON-shaped.*Date at \$\.value\.at/) + expect(provider.persisted).toEqual([]) + }) + + it.each([ + ['a Map', { value: new Map() }, /Map at \$\.value/], + ['a bigint', { value: [10n] }, /bigint at \$\.value\[0\]/], + ['a symbol', { value: Symbol('x') }, /symbol at \$\.value/], + ['a non-finite number', { value: Number.NaN }, /non-finite number at \$\.value/], + ['an undefined array entry', { value: [undefined] }, /undefined at \$\.value\[0\]/], + ['a class instance', { value: Object.create({ marker: true }) as object }, /non-plain object at \$\.value/], + ])('rejects %s that structuredClone would admit', async (_label, patch, message) => { + const { ctx } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), z.object({ value: z.any() })) + await expect(scope.update(patch)).rejects.toThrow(message) + }) + + it('rejects a circular patch instead of storing an alias-looped document', async () => { + const { ctx } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), z.object({ value: z.any() })) + const cyclic: Record = {} + cyclic['self'] = cyclic + await expect(scope.update({ value: cyclic })).rejects.toThrow(/circular reference at \$\.value\.self/) + const loop: unknown[] = [] + loop.push(loop) + await expect(scope.update({ value: loop })).rejects.toThrow(/circular reference at \$\.value\[0\]/) + }) + + it('accepts one object referenced twice without a cycle', async () => { + const { ctx } = await boot() + const scope = ctx.settings.register(settingsNamespace('ui-theme'), z.object({ value: z.any() })) + const shared = { leaf: 1 } + await scope.update({ value: { left: shared, right: shared } }) + expect(scope.get()).toEqual({ value: { left: { leaf: 1 }, right: { leaf: 1 } } }) + }) + + it('contains an async settings/updated listener rejection and keeps other listeners running', async () => { + const { ctx, provider } = await boot() + // An async listener violates the event's synchronous signature (typed + // consumers get a lint error for it), but an unlinted JS plugin can still + // register one; the cast simulates exactly that caller. + ctx.on('settings/updated', async () => { + throw new Error('async listener boom') + }) + const second = vi.fn() + ctx.on('settings/updated', second) + ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + provider.pushExternal({ 'ui-theme': { theme: 'light' } }) + expect(second).toHaveBeenCalledTimes(1) + // Containment gives the rejection a handler; vitest observes no unhandled + // rejection out of this test. + await new Promise(resolve => setTimeout(resolve, 10)) + }) +}) + describe('watch', () => { it('stops after its disposer runs', async () => { const { ctx, provider } = await boot() From 85a3a158dde4e532e83a93c3889172518a950b6a Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Thu, 30 Jul 2026 13:39:22 +0800 Subject: [PATCH 09/17] fix(settings-local): one operation chain, read-modify-write under a writer lock, and diff-shaped YAML edits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round three found the provider's write path could destroy state it never observed: - Watcher reloads and document writes ran on two independent promise chains, and a write rendered the whole next document from the cached text. An external edit still inside the debounce window (or missed outright) was overwritten, and the follow-up reload no-oped because the post-rename content matched the cache — the edit vanished without a trace. Reloads and writes now share one operation chain, and every write starts by reconciling the on-disk text into the seam before rendering, so unobserved sibling sections survive and publish first. An unparsable on-disk document fails the write loud instead of being overwritten. - The initial load raced the watcher's own setup: a change written between that read and the watcher becoming active never fired an event. The watcher's ready signal now queues one reconcile, closing the gap. - Two processes sharing a harness home rendered from independent caches, last writer winning. Writes now hold a wx-created .lock sibling around the read-render-rename cycle with bounded backoff, a crashed- holder stale takeover, and a deadline failure; readers stay lock-free because the rename commit is atomic. - renderYaml replaced the whole namespace node, dropping every comment inside the section. The next section now lands as a leaf-level diff (set changed values, delete removed keys), so comments, anchors, and formatting survive on every untouched node and on the key of every changed pair; arrays still replace wholesale when unequal. --- packages/settings/settings-local/src/index.ts | 247 ++++++++++++++---- .../settings-local/tests/concurrency.spec.ts | 103 ++++++++ .../settings-local/tests/local.spec.ts | 96 +++++++ .../settings-local/tests/lock-race.spec.ts | 100 +++++++ .../settings-local/tests/watcher.spec.ts | 55 +++- 5 files changed, 545 insertions(+), 56 deletions(-) create mode 100644 packages/settings/settings-local/tests/concurrency.spec.ts create mode 100644 packages/settings/settings-local/tests/lock-race.spec.ts diff --git a/packages/settings/settings-local/src/index.ts b/packages/settings/settings-local/src/index.ts index b305f61fe7..b9df71f9e1 100644 --- a/packages/settings/settings-local/src/index.ts +++ b/packages/settings/settings-local/src/index.ts @@ -9,11 +9,11 @@ import { Context, Service } from 'cordis' import z from 'schemastery' import { watch as chokidarWatch } from 'chokidar' import { randomBytes } from 'node:crypto' -import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises' +import { mkdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises' import { dirname, extname, join, resolve } from 'node:path' import { Document, parseDocument } from 'yaml' import { resolveDshHome } from '@deepseek-ai/dsh-paths' -import { Settings, type SettingsNamespace } from '@deepseek-ai/dsh-settings' +import { Settings, deepEqualJson, type SettingsNamespace } from '@deepseek-ai/dsh-settings' /** Plugin config: file location and hot-reload behavior. */ export interface Config { @@ -64,11 +64,53 @@ export function resolveSpec(config: Config): ResolvedSpec { } } +/** Whether a parsed YAML value is a map for diffing purposes. */ +function isMapLike(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +/** + * Apply the difference between one node's stored and next value as minimal + * `setIn`/`deleteIn` edits, recursing through maps, so every untouched node — + * and the key node of every changed pair — keeps its comments, anchors, and + * formatting. Non-map values (arrays and scalars) replace wholesale when + * unequal, taking any comments inside them along. + */ +function patchNode(document: Document, path: readonly string[], current: unknown, next: unknown): void { + if (isMapLike(current) && isMapLike(next)) { + for (const key of Object.keys(current)) { + if (!(key in next)) document.deleteIn([...path, key]) + } + for (const [key, value] of Object.entries(next)) { + patchNode(document, [...path, key], current[key], value) + } + return + } + if (!deepEqualJson(current, next)) document.setIn([...path], next) +} + /** Whether a filesystem error means absence; every non-ENOENT failure must surface. */ function isENOENT(error: unknown): boolean { return (error as NodeJS.ErrnoException | null)?.code === 'ENOENT' } +/** Whether an exclusive create failed because the path already exists. */ +function isEEXIST(error: unknown): boolean { + return (error as NodeJS.ErrnoException | null)?.code === 'EEXIST' +} + +/** + * Writer-lock protocol constants. These are robustness invariants of the + * cross-process write protocol, not deployment tunables: a holder rewrites one + * small document in milliseconds, so contention resolves well inside the + * retry deadline, and a lock older than the stale age can only belong to a + * crashed holder. + */ +const LOCK_RETRY_INITIAL_MS = 20 +const LOCK_RETRY_MAX_MS = 200 +const LOCK_TIMEOUT_MS = 2_000 +const LOCK_STALE_MS = 5_000 + /** File-backed settings provider (`settings.yaml`/`.json`). */ export class SettingsLocal extends Settings { static Config: z = z.object({ @@ -85,10 +127,13 @@ export class SettingsLocal extends Settings { * this cache are no-ops, which is also the self-write suppression. */ private text: string | undefined - /** Serializes watcher-triggered reloads so reads never interleave. */ - private refreshTask: Promise = Promise.resolve() - /** Serializes whole-document writes across namespace queues; settled tail. */ - private persistChain: Promise = Promise.resolve() + /** + * Single exclusive operation chain: watcher reloads and document writes run + * one at a time in queue order (settled tail), so a write can never render + * from text a concurrent reload is busy replacing, and a reload can never + * read a half-committed write. + */ + private operations: Promise = Promise.resolve() /** Set at dispose: refuse new watcher events and let in-flight work no-op. */ private closed = false @@ -125,32 +170,107 @@ export class SettingsLocal extends Settings { protected persist(ns: SettingsNamespace, section: Record): Promise { // One document backs every namespace, so writes from different namespace - // queues must serialize here: each render must see the text the previous - // write committed, or the loser's section silently vanishes from disk. - // The stored tail is settled on both outcomes, so chaining needs no catch. - const task = this.persistChain.then(() => this.persistSection(ns, section)) - this.persistChain = task.then(() => undefined, () => undefined) + // queues serialize with each other and with watcher reloads on the one + // operation chain: each render must see the text the previous operation + // committed, or a sibling section silently vanishes from disk. + return this.enqueue(() => this.persistSection(ns, section)) + } + + /** Queue one exclusive document operation behind every earlier one. */ + private enqueue(operation: () => Promise): Promise { + const task = this.operations.then(operation) + this.operations = task.then(() => undefined, () => undefined) return task } + /** Queue a reload; only an invariant violation escaping a commit can reject it. */ + private queueRefresh(): void { + void this.enqueue(() => this.refresh()).catch((error: unknown) => { + // Only an invariant violation escaping the commit path can reject a + // refresh; keep the operation queue alive and surface it as an error so + // one poisoned commit cannot silently end hot reloading forever. + this.ctx.logger.error('settings-local: reload commit failed at %s', this.spec.filename) + this.ctx.logger.error(error) + }) + } + private async persistSection(ns: SettingsNamespace, section: Record): Promise { - const output = this.spec.format === 'yaml' - ? this.renderYaml(ns, section) - : this.renderJson(ns, section) await mkdir(dirname(this.spec.filename), { recursive: true }) - // Exclusive-create (`wx`) a random-suffix sibling: the open refuses to - // follow any planted symlink at a guessable temp path, and the fresh inode - // carries owner-only permissions that survive the rename — a document that - // may hold personal values is never world-readable and never a symlink. - const temp = `${this.spec.filename}.${randomBytes(6).toString('hex')}.tmp` - try { - await writeFile(temp, output, { mode: 0o600, flag: 'wx' }) - await rename(temp, this.spec.filename) - } catch (error) { - await rm(temp, { force: true }) - throw error + await this.withWriterLock(async () => { + // Read-modify-write: fold in any on-disk state this process has not + // observed yet — an external edit still inside the watcher debounce + // window, a change the watcher missed, or another process's write — so + // the render below can never resurrect a stale document. An unparsable + // on-disk document fails the write loud instead of silently overwriting + // a user's manual edit. + await this.reconcileFromDisk() + const output = this.spec.format === 'yaml' + ? this.renderYaml(ns, section) + : this.renderJson(ns, section) + // Exclusive-create (`wx`) a random-suffix sibling: the open refuses to + // follow any planted symlink at a guessable temp path, and the fresh inode + // carries owner-only permissions that survive the rename — a document that + // may hold personal values is never world-readable and never a symlink. + const temp = `${this.spec.filename}.${randomBytes(6).toString('hex')}.tmp` + try { + await writeFile(temp, output, { mode: 0o600, flag: 'wx' }) + await rename(temp, this.spec.filename) + } catch (error) { + await rm(temp, { force: true }) + throw error + } + this.text = output + }) + } + + /** + * Hold the cross-process writer lock around one read-render-rename cycle. + * The lock is a `wx`-created sibling (`.lock`); the rename-based + * commit keeps readers lock-free, so only writers contend. A lock older + * than {@link LOCK_STALE_MS} is a crashed holder and is broken with a + * warning; a live holder past {@link LOCK_TIMEOUT_MS} fails the write. + */ + private async withWriterLock(operation: () => Promise): Promise { + const lockPath = `${this.spec.filename}.lock` + const deadline = Date.now() + LOCK_TIMEOUT_MS + let delay = LOCK_RETRY_INITIAL_MS + for (;;) { + try { + await writeFile(lockPath, `${process.pid}\n`, { mode: 0o600, flag: 'wx' }) + break + } catch (error) { + if (!isEEXIST(error)) throw error + } + const ageMs = await this.lockAgeMs(lockPath) + // The holder released between the failed create and the stat: the lock + // is free right now, so retry without burning backoff or deadline. + if (ageMs === undefined) continue + if (ageMs > LOCK_STALE_MS) { + this.ctx.logger.warn('settings-local: breaking a stale writer lock at %s', lockPath) + await rm(lockPath, { force: true }) + continue + } + if (Date.now() >= deadline) { + throw new Error(`settings-local: timed out waiting for the writer lock at ${lockPath}`) + } + await new Promise(resolve => setTimeout(resolve, delay)) + delay = Math.min(delay * 2, LOCK_RETRY_MAX_MS) + } + try { + return await operation() + } finally { + await rm(lockPath, { force: true }) + } + } + + /** Age of the writer lock, or `undefined` when it vanished after a failed create. */ + private async lockAgeMs(lockPath: string): Promise { + try { + return Date.now() - (await stat(lockPath)).mtimeMs + } catch (error) { + if (!isENOENT(error)) throw error + return undefined } - this.text = output } override async* [Service.init](): AsyncGenerator<() => Promise | void, void, void> { @@ -168,13 +288,14 @@ export class SettingsLocal extends Settings { }) watcher.on('all', () => { if (this.closed) return - this.refreshTask = this.refreshTask.then(() => this.refresh()).catch((error: unknown) => { - // Only an invariant violation escaping the commit path can reject a - // refresh; keep the reload queue alive and surface it as an error so - // one poisoned commit cannot silently end hot reloading forever. - this.ctx.logger.error('settings-local: reload commit failed at %s', this.spec.filename) - this.ctx.logger.error(error) - }) + this.queueRefresh() + }) + watcher.on('ready', () => { + // The base init's load raced the watcher's own setup: a change written + // between that read and the watcher becoming active never fires an + // event. One reconcile at ready closes the gap. + if (this.closed) return + this.queueRefresh() }) watcher.on('error', (error) => { this.ctx.logger.warn('settings-local: watcher error on %s', this.spec.filename) @@ -182,10 +303,10 @@ export class SettingsLocal extends Settings { }) yield async () => { // Quiesce: stop accepting events, close the watcher, then wait out any - // queued or in-flight refresh so nothing publishes after disposal. + // queued or in-flight operation so nothing publishes after disposal. this.closed = true await watcher.close() - await this.refreshTask + await this.operations } } @@ -212,46 +333,62 @@ export class SettingsLocal extends Settings { * Re-read the document after a watcher event. Unchanged content (including * this provider's own writes) is a no-op; an unreadable or unparsable * document keeps the last good sections and warns — a live hot-reload must - * never take the process down. + * never take the process down. An invariant violation escaping a commit is + * not a reload failure and propagates to the queue's error surface. */ private async refresh(): Promise { if (this.closed) return - let text: string + try { + await this.reconcileFromDisk() + } catch (error) { + if ((error as { code?: unknown } | null)?.code === 'INVARIANT') throw error + this.ctx.logger.warn('settings-local: reload failed at %s; keeping the last good document', this.spec.filename) + this.ctx.logger.warn(error) + } + } + + /** + * Compare the on-disk text against the cache and publish any difference + * into the seam. Absence publishes the empty document; an unreadable or + * unparsable file throws, so each caller picks its policy — a reload warns + * and keeps the last good document, a write fails loud. + */ + private async reconcileFromDisk(): Promise { + let text: string | undefined try { text = await readFile(this.spec.filename, 'utf8') } catch (error) { - if (!isENOENT(error)) { - this.ctx.logger.warn('settings-local: reload failed at %s; keeping the last good document', this.spec.filename) - this.ctx.logger.warn(error) - return - } - if (this.text === undefined || this.isClosed()) return + if (!isENOENT(error)) throw error + text = undefined + } + if (text === this.text || this.isClosed()) return + if (text === undefined) { this.text = undefined this.publish({}) return } - if (text === this.text || this.isClosed()) return - let doc: Record - try { - doc = this.parse(text) - } catch (error) { - this.ctx.logger.warn('settings-local: reload failed at %s; keeping the last good document', this.spec.filename) - this.ctx.logger.warn(error) - return - } + const doc = this.parse(text) this.text = text this.publish(doc) } - /** Render the next YAML text by patching one namespace in the comment-preserving document. */ + /** + * Render the next YAML text by patching one namespace in the + * comment-preserving document. The next section lands as a leaf-level diff + * against the stored one — only changed values set, only removed keys + * delete — so comments inside the section survive edits to their siblings, + * not just comments outside it. + */ private renderYaml(ns: SettingsNamespace, section: Record): string { if (this.text === undefined) { return new Document({ [ns]: section }).toString() } // this.text only ever caches content that parsed successfully, so this - // re-parse (for the mutable comment-preserving tree) cannot fail. + // re-parse (for the mutable comment-preserving tree) cannot fail, and + // parse() already rejected any non-map root. const document = parseDocument(this.text) - document.set(ns, section) + const root: unknown = document.toJS() + patchNode(document, [ns], isMapLike(root) ? root[ns] : undefined, section) return document.toString() } diff --git a/packages/settings/settings-local/tests/concurrency.spec.ts b/packages/settings/settings-local/tests/concurrency.spec.ts new file mode 100644 index 0000000000..ab09866819 --- /dev/null +++ b/packages/settings/settings-local/tests/concurrency.spec.ts @@ -0,0 +1,103 @@ +// Cross-instance and writer-lock behavior: two providers on one document are +// the in-process equivalent of two dsh processes sharing a harness home — +// neither knows the other's cache, so only the read-modify-write cycle under +// the `.lock` sibling keeps both namespaces alive on disk. +import { afterEach, describe, expect, it } from 'vitest' +import { Context } from 'cordis' +import z from 'schemastery' +import { chmod, mkdtemp, readFile, rm, utimes, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { settingsNamespace } from '@deepseek-ai/dsh-settings' +import { SettingsLocal } from '../src/index.ts' + +const AlphaSchema: z<{ value: number }> = z.object({ value: z.number().default(0) }) +const BetaSchema: z<{ value: number }> = z.object({ value: z.number().default(0) }) + +const cleanups: Array<() => Promise> = [] + +afterEach(async () => { + while (cleanups.length > 0) await cleanups.pop()!() +}) + +async function tempDir(): Promise { + const dir = await mkdtemp(join(tmpdir(), 'dsh-settings-lock-')) + cleanups.push(() => rm(dir, { recursive: true, force: true })) + return dir +} + +async function boot(config: ConstructorParameters[1]): Promise { + const ctx = new Context() + const fiber = ctx.plugin(SettingsLocal, config) + cleanups.push(async () => { await fiber.dispose() }) + await fiber + return ctx +} + +describe('cross-instance writes', () => { + it('keeps both namespaces when two providers write the same document concurrently', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const first = await boot({ path, watch: false }) + const second = await boot({ path, watch: false }) + const alpha = first.settings.register(settingsNamespace('alpha'), AlphaSchema) + const beta = second.settings.register(settingsNamespace('beta'), BetaSchema) + const rounds = [1, 2, 3, 4, 5] + await Promise.all([ + (async () => { for (const value of rounds) await alpha.update({ value }) })(), + (async () => { for (const value of rounds) await beta.update({ value }) })(), + ]) + const text = await readFile(path, 'utf8') + expect(text).toContain('alpha:') + expect(text).toContain('beta:') + // A third instance resolves both final values from the shared document. + const third = await boot({ path, watch: false }) + expect(third.settings.register(settingsNamespace('alpha'), AlphaSchema).get()).toEqual({ value: 5 }) + expect(third.settings.register(settingsNamespace('beta'), BetaSchema).get()).toEqual({ value: 5 }) + }) +}) + +describe('writer lock', () => { + it('waits for a busy writer lock instead of failing', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('alpha'), AlphaSchema) + await writeFile(`${path}.lock`, 'holder\n') + const release = setTimeout(() => { void rm(`${path}.lock`, { force: true }) }, 120) + cleanups.push(async () => { clearTimeout(release) }) + await scope.update({ value: 7 }) + expect(await readFile(path, 'utf8')).toContain('value: 7') + }) + + it('breaks a stale writer lock with a warning and writes through', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('alpha'), AlphaSchema) + await writeFile(`${path}.lock`, 'crashed-holder\n') + const past = (Date.now() - 60_000) / 1000 + await utimes(`${path}.lock`, past, past) + await scope.update({ value: 9 }) + expect(await readFile(path, 'utf8')).toContain('value: 9') + }) + + it('times out on a lock a live holder never releases', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('alpha'), AlphaSchema) + await writeFile(`${path}.lock`, 'busy-holder\n') + await expect(scope.update({ value: 1 })).rejects.toThrow(/timed out waiting for the writer lock/) + }, 10_000) + + it('surfaces a non-contention lock failure as the write error', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('alpha'), AlphaSchema) + await chmod(dir, 0o500) + cleanups.push(() => chmod(dir, 0o700)) + await expect(scope.update({ value: 1 })).rejects.toThrow(/EACCES|permission/) + }) +}) diff --git a/packages/settings/settings-local/tests/local.spec.ts b/packages/settings/settings-local/tests/local.spec.ts index 4c3c24ccd9..0b753675f5 100644 --- a/packages/settings/settings-local/tests/local.spec.ts +++ b/packages/settings/settings-local/tests/local.spec.ts @@ -204,6 +204,102 @@ describe('persist', () => { expect(written).toContain('theme: light') }) + it('keeps comments inside the section when a sibling key changes', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, [ + 'ui-theme:', + ' # chosen during onboarding', + ' theme: light', + ' fontSize: 12', + '', + ].join('\n')) + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ fontSize: 18 }) + const written = await readFile(path, 'utf8') + expect(written).toContain('# chosen during onboarding') + expect(written).toContain('theme: light') + expect(written).toContain('fontSize: 18') + }) + + it('keeps a changed key\'s own-line comment while replacing its value', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, [ + 'ui-theme:', + ' # chosen during onboarding', + ' theme: light', + '', + ].join('\n')) + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ theme: 'dark' }) + const written = await readFile(path, 'utf8') + expect(written).toContain('# chosen during onboarding') + expect(written).toContain('theme: dark') + }) + + it('deletes only the removed key on replace, keeping sibling comments', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, [ + 'ui-theme:', + ' # chosen during onboarding', + ' theme: light', + ' fontSize: 12', + '', + ].join('\n')) + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.replace({ theme: 'light' }) + const written = await readFile(path, 'utf8') + expect(written).toContain('# chosen during onboarding') + expect(written).toContain('theme: light') + expect(written).not.toContain('fontSize') + }) + + it('keeps an unchanged array\'s comments and replaces a changed array wholesale', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const TagsSchema: z<{ tags: string[]; label: string }> = z.object({ + tags: z.array(z.string()).default([]), + label: z.string().default(''), + }) + await writeFile(path, [ + 'workspace:', + ' tags:', + ' # pinned by hand', + ' - alpha', + ' label: draft', + '', + ].join('\n')) + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('workspace'), TagsSchema) + await scope.update({ label: 'final' }) + const untouched = await readFile(path, 'utf8') + expect(untouched).toContain('# pinned by hand') + expect(untouched).toContain('label: final') + // A changed array replaces wholesale; comments inside it go with it. + await scope.update({ tags: ['beta'] }) + const replaced = await readFile(path, 'utf8') + expect(replaced).not.toContain('# pinned by hand') + expect(replaced).toContain('- beta') + }) + + it('keeps a comment-only document\'s comment when the first section lands', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + // Parses to a null root: the document exists but holds no sections yet. + await writeFile(path, '# reserved for future settings\n') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + await scope.update({ theme: 'light' }) + const written = await readFile(path, 'utf8') + expect(written).toContain('# reserved for future settings') + expect(written).toContain('theme: light') + }) + it('creates a json document from scratch', async () => { const dir = await tempDir() const path = join(dir, 'settings.json') diff --git a/packages/settings/settings-local/tests/lock-race.spec.ts b/packages/settings/settings-local/tests/lock-race.spec.ts new file mode 100644 index 0000000000..09eb025654 --- /dev/null +++ b/packages/settings/settings-local/tests/lock-race.spec.ts @@ -0,0 +1,100 @@ +// Writer-lock races that cannot be timed from outside: a contender whose lock +// vanishes between the failed exclusive create and the stat, a stat failing +// for a reason other than absence, and a temp-file write failing mid-cycle. +// The fs/promises seam is partially mocked to inject exactly one failure at a +// chosen path suffix; everything else passes through to the real filesystem. +import { afterEach, describe, expect, it, vi } from 'vitest' +import { Context } from 'cordis' +import z from 'schemastery' +import { access, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { settingsNamespace } from '@deepseek-ai/dsh-settings' +import { SettingsLocal } from '../src/index.ts' + +const state = vi.hoisted(() => ({ + /** One-shot failure injections keyed by operation, matched on a path suffix. */ + failures: [] as Array<{ op: 'writeFile' | 'stat'; suffix: string; code: string }>, +})) + +vi.mock('node:fs/promises', async (importOriginal) => { + const actual = await importOriginal() + const inject = (op: 'writeFile' | 'stat', path: unknown): void => { + const index = state.failures.findIndex(f => f.op === op && String(path).endsWith(f.suffix)) + if (index === -1) return + const [failure] = state.failures.splice(index, 1) + throw Object.assign(new Error(`${failure!.code}: injected ${op} failure`), { code: failure!.code }) + } + return { + ...actual, + writeFile: (async (path: unknown, ...rest: never[]) => { + inject('writeFile', path) + return (actual.writeFile as (path: unknown, ...args: never[]) => Promise)(path, ...rest) + }) as typeof actual.writeFile, + stat: (async (path: unknown, ...rest: never[]) => { + inject('stat', path) + return (actual.stat as (path: unknown, ...args: never[]) => Promise)(path, ...rest) + }) as typeof actual.stat, + } +}) + +const AlphaSchema: z<{ value: number }> = z.object({ value: z.number().default(0) }) + +const cleanups: Array<() => Promise> = [] + +afterEach(async () => { + state.failures.length = 0 + while (cleanups.length > 0) await cleanups.pop()!() +}) + +async function tempDir(): Promise { + const dir = await mkdtemp(join(tmpdir(), 'dsh-settings-lockrace-')) + cleanups.push(() => rm(dir, { recursive: true, force: true })) + return dir +} + +async function boot(config: ConstructorParameters[1]): Promise { + const ctx = new Context() + const fiber = ctx.plugin(SettingsLocal, config) + cleanups.push(async () => { await fiber.dispose() }) + await fiber + return ctx +} + +describe('writer-lock races', () => { + it('retries immediately when the contending lock vanished before the stat', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('alpha'), AlphaSchema) + // The exclusive create loses to a holder that releases before the stat: + // no lock file actually exists, so the stat sees honest absence and the + // very next attempt takes the lock. + state.failures.push({ op: 'writeFile', suffix: '.lock', code: 'EEXIST' }) + await scope.update({ value: 3 }) + expect(await readFile(path, 'utf8')).toContain('value: 3') + }) + + it('propagates a stat failure that does not mean absence', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('alpha'), AlphaSchema) + state.failures.push({ op: 'writeFile', suffix: '.lock', code: 'EEXIST' }) + state.failures.push({ op: 'stat', suffix: '.lock', code: 'EACCES' }) + await expect(scope.update({ value: 3 })).rejects.toThrow(/EACCES/) + }) + + it('cleans up the temp file and releases the lock when the write fails mid-cycle', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'alpha:\n value: 1\n') + const ctx = await boot({ path, watch: false }) + const scope = ctx.settings.register(settingsNamespace('alpha'), AlphaSchema) + state.failures.push({ op: 'writeFile', suffix: '.tmp', code: 'ENOSPC' }) + await expect(scope.update({ value: 9 })).rejects.toThrow(/ENOSPC/) + // The document is untouched and the writer lock was released on the way out. + expect(await readFile(path, 'utf8')).toContain('value: 1') + await expect(access(`${path}.lock`)).rejects.toThrow() + }) +}) diff --git a/packages/settings/settings-local/tests/watcher.spec.ts b/packages/settings/settings-local/tests/watcher.spec.ts index 439f213473..7b289b22a3 100644 --- a/packages/settings/settings-local/tests/watcher.spec.ts +++ b/packages/settings/settings-local/tests/watcher.spec.ts @@ -1,7 +1,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import { Context } from 'cordis' import z from 'schemastery' -import { chmod, mkdtemp, rm, writeFile } from 'node:fs/promises' +import { chmod, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { settingsNamespace } from '@deepseek-ai/dsh-settings' @@ -155,6 +155,7 @@ describe('watcher pipeline', () => { await fiber.dispose() disposed = true instance!.watcher.emit('all', 'change', path) + instance!.watcher.emit('ready') await new Promise(resolve => setTimeout(resolve, 100)) expect(postDisposeCommits).toBe(0) }) @@ -169,4 +170,56 @@ describe('watcher pipeline', () => { await new Promise(resolve => setTimeout(resolve, 50)) expect(scope.get()).toEqual({ theme: 'dark' }) }) + + it('folds an unobserved external edit into a write instead of overwriting it', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, debounceMs: 5 }) + const theme = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const editor = ctx.settings.register(settingsNamespace('editor'), z.object({ + tabWidth: z.number().default(2), + })) + // The external edit has landed on disk but its watcher event has not + // fired yet (a debounce window, or a missed event): the write must fold + // it in, not resurrect the stale document. + await writeFile(path, 'ui-theme:\n theme: light\neditor:\n tabWidth: 8\n') + await theme.update({ theme: 'darker' }) + const text = await readFile(path, 'utf8') + expect(text).toContain('tabWidth: 8') + expect(text).toContain('theme: darker') + // The fold published the unobserved section before the write committed. + expect(editor.get()).toEqual({ tabWidth: 8 }) + }) + + it('reconciles at watcher ready so a change during setup is not missed', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, debounceMs: 5 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + // Written after the initial load but before the watcher became active: + // no 'all' event will ever fire for it. + await writeFile(path, 'ui-theme:\n theme: written-before-ready\n') + const [instance] = await fakeInstances() + instance!.watcher.emit('ready') + await vi.waitFor(() => { + expect(scope.get().theme).toBe('written-before-ready') + }) + }) + + it('fails a write loud when the on-disk document turned invalid unobserved', async () => { + const dir = await tempDir() + const path = join(dir, 'settings.yaml') + await writeFile(path, 'ui-theme:\n theme: light\n') + const ctx = await boot({ path, debounceMs: 5 }) + const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) + const broken = 'ui-theme: [unclosed\n flow: {\n' + await writeFile(path, broken) + await expect(scope.update({ theme: 'darker' })).rejects.toThrow(/invalid document/) + // The user's manual edit stays on disk untouched and the cache keeps the + // last good value. + expect(await readFile(path, 'utf8')).toBe(broken) + expect(scope.get()).toEqual({ theme: 'light' }) + }) }) From 3b1b9125180c23c70f8a090b215a7f1d3f05692d Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Thu, 30 Jul 2026 14:09:04 +0800 Subject: [PATCH 10/17] docs(settings): third-review contracts across READMEs, catalogs, and the write-path integrity note The seam README states the JSON-shaped write boundary, watch-disposer quiescence, async listener containment, and the drained teardown; the provider README rewrites Behavior around the operation chain, read-modify-write, writer lock, ready reconcile, and leaf-level YAML diffs, and updates Known Limitations to the residual guarantees. A new Agent Note records the round's decisions and supersedes the original note's deferred-lockfile alternative (cross-linked in place). Chinese counterparts updated pair-by-pair (three briefed minimal updates, one whole-document translation); type-equiv, config, cordis, and module-graph catalogs re-recorded. --- .../2026-07-28-user-settings-seam.i18n.yaml | 4 +- .../2026-07-28-user-settings-seam.md | 4 +- .../2026-07-28-user-settings-seam.zh.md | 4 +- ...30-settings-write-path-integrity.i18n.yaml | 6 +++ ...026-07-30-settings-write-path-integrity.md | 35 ++++++++++++++++ ...-07-30-settings-write-path-integrity.zh.md | 41 +++++++++++++++++++ docs/config-catalog.md | 2 +- docs/cordis-catalog/events.md | 9 +++- docs/cordis-catalog/services.md | 2 +- docs/core-data-structures/settings.i18n.yaml | 4 +- docs/core-data-structures/settings.md | 10 +++-- docs/core-data-structures/settings.zh.md | 10 +++-- docs/event-producer-consumer.md | 2 +- .../cordis/tool-cordis/src/api-catalog.ts | 2 +- .../settings/settings-local/README.i18n.yaml | 4 +- packages/settings/settings-local/README.md | 17 +++++--- packages/settings/settings-local/README.zh.md | 17 +++++--- packages/settings/settings-local/src/index.ts | 4 +- packages/settings/settings/README.i18n.yaml | 4 +- packages/settings/settings/README.md | 8 ++-- packages/settings/settings/README.zh.md | 8 ++-- 21 files changed, 152 insertions(+), 45 deletions(-) create mode 100644 .agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.i18n.yaml create mode 100644 .agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.md create mode 100644 .agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.zh.md diff --git a/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.i18n.yaml b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.i18n.yaml index cc409d8403..736a372f27 100644 --- a/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.i18n.yaml +++ b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write .agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md -2026-07-28-user-settings-seam.md: f0f45d77c8f98fc15625b1a1bf116ec10b965676 -2026-07-28-user-settings-seam.zh.md: eb562099b1ff0b5b0a019e9956d6e36e71857236 +2026-07-28-user-settings-seam.md: bf93f95168b1b6d0dec5a9fc2c9aac5531f0564a +2026-07-28-user-settings-seam.zh.md: 8cd4dfcbb2facdd590b2c24d453ad79e9badda4d diff --git a/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md index f0f45d77c8..bf93f95168 100644 --- a/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md +++ b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.md @@ -14,7 +14,7 @@ User-editable configuration had no owner: `dsh web` read a cwd-anchored profile **Two planes with a litmus test.** `cordis.yml` (+ Include patches) stays the composition plane: which plugins exist, wiring, deployment config, owned by the orchestrator and upgraded with the product. A settings namespace carries only the user-editable subset; the test is "should the personal config page edit it?" Values live in both planes without ambiguity because layering is the contract: schema defaults, then the registrant's composition `base` (its entry-config subset), then the user document section. -**Three-package seam mirroring `session-persistence/`.** `dsh-settings` owns the abstract `Settings` service: namespace registry, layered resolution, schema validation, per-namespace deep-equal change detection, and the `settings/updated` commit event. Providers implement only `writable`/`load()`/`persist(ns, section)` and push externally observed documents through the protected `publish(doc)` — so hot-update semantics are identical across providers, and a network configuration-center backend (nacos-style, possibly read-only) is a sibling package away. `dsh-settings-local` is the file provider: YAML/JSON under `resolveSpec` (explicit defaulting to `/settings.yaml`), chokidar watch, atomic `0600` tmp+rename writes, comment-preserving YAML patching of exactly one namespace key, and content-equality self-write suppression. +**Three-package seam mirroring `session-persistence/`.** `dsh-settings` owns the abstract `Settings` service: namespace registry, layered resolution, schema validation, per-namespace deep-equal change detection, and the `settings/updated` commit event. Providers implement only `writable`/`load()`/`persist(ns, section)` and push externally observed documents through the protected `publish(doc)` — so hot-update semantics are identical across providers, and a network configuration-center backend (nacos-style, possibly read-only) is a sibling package away. `dsh-settings-local` is the file provider: YAML/JSON under `resolveSpec` (explicit defaulting to `/settings.yaml`), chokidar watch, read-modify-write persists under a cross-process writer lock with atomic `0600` tmp+rename commits, leaf-level diff patching of the written namespace (comments survive untouched nodes), and content-equality self-write suppression ([write-path integrity note](2026-07-30-settings-write-path-integrity.md)). **Registrations are caller-fiber effects.** `register()` runs through the service proxy, so `this.ctx` is the registrant's context and the registration rides `ctx.effect`: disposing the registrant removes the namespace and its watchers (proven by the HMR disposal test), while the user's section keeps living in storage for the next owner. @@ -28,7 +28,7 @@ User-editable configuration had no owner: `dsh web` read a cwd-anchored profile - **Loader-reactive `fiber.update` as the propagation channel**: constructor-time reads observe nothing; the seam's explicit `watch()` makes hot-update a consumer contract instead of framework magic. - **A domain-aware settings service** (getters per product area): the coupling objection from design review stands; the service stores, validates, and publishes — domain meaning stays with the registrant that owns the schema. - **Multi-layer precedence now** (system/managed/project tiers à la Codex/Claude Code): deferred until a real second layer exists; the resolve step is the single place layering would extend. -- **A cross-process lockfile now** (Pi's proper-lockfile): atomic replace plus watcher convergence (last write wins) is documented behavior until real contention shows up. +- **A cross-process lockfile now** (Pi's proper-lockfile): initially deferred as "atomic replace plus watcher convergence until real contention shows up" — review showed convergence loses unobserved sibling namespaces, so the deferral is superseded by the [write-path integrity note](2026-07-30-settings-write-path-integrity.md)'s hand-rolled writer lock. ## Consequences diff --git a/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.zh.md b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.zh.md index eb562099b1..8cd4dfcbb2 100644 --- a/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.zh.md +++ b/.agents/notes/implemented/architecture/2026-07-28-user-settings-seam.zh.md @@ -14,7 +14,7 @@ Status: implemented **两个面,一条判定。**`cordis.yml`(+ Include patches)仍是组合面:有哪些插件、接线、部署配置,归 orchestrator 所有并随产品升级。settings namespace 只承载用户可编辑子集;判定是"个人配置页应该能改它吗?"值可同时存在于两个面而不歧义,因为分层就是契约:schema 默认值,然后注册方的组合 `base`(其 entry 配置子集),最后用户文档分节。 -**镜像 `session-persistence/` 的三包 seam。**`dsh-settings` 拥有抽象 `Settings` 服务:namespace 注册表、分层解析、schema 校验、按 namespace 深相等变更检测,以及 `settings/updated` 提交事件。provider 只实现 `writable`/`load()`/`persist(ns, section)`,并通过受保护的 `publish(doc)` 推入外部观察到的文档——因此热更新语义对所有 provider 一致,网络配置中心后端(nacos 类,可能只读)只是一个平级包的距离。`dsh-settings-local` 是文件 provider:`resolveSpec` 显式默认到 `/settings.yaml` 的 YAML/JSON、chokidar 监听、`0600` tmp+rename 原子写、只修补目标 namespace 键的保注释 YAML 写回、按内容相等抑制自写。 +**镜像 `session-persistence/` 的三包 seam。**`dsh-settings` 拥有抽象 `Settings` 服务:namespace 注册表、分层解析、schema 校验、按 namespace 深相等变更检测,以及 `settings/updated` 提交事件。provider 只实现 `writable`/`load()`/`persist(ns, section)`,并通过受保护的 `publish(doc)` 推入外部观察到的文档——因此热更新语义对所有 provider 一致,网络配置中心后端(nacos 类,可能只读)只是一个平级包的距离。`dsh-settings-local` 是文件 provider:`resolveSpec` 显式默认到 `/settings.yaml` 的 YAML/JSON、chokidar 监听、跨进程写锁下以 `0600` tmp+rename 原子提交的读-改-写 persist、对被写 namespace 的叶子级 diff 修补(未触碰节点的注释得以保留)、按内容相等抑制自写([write-path integrity note](2026-07-30-settings-write-path-integrity.md))。 **注册是调用方 fiber 上的 effect。**`register()` 经服务代理调用,`this.ctx` 即注册方 context,注册挂在 `ctx.effect` 上:dispose 注册方即移除 namespace 及其观察者(HMR disposal 测试证明),而用户的分节继续留在存储中等待下一任 owner。 @@ -28,7 +28,7 @@ Status: implemented - **以 Loader reactive `fiber.update` 为传导通道**:构造期读取毫无感知;seam 的显式 `watch()` 把热更新变成消费者契约而非框架魔法。 - **领域化的 settings 服务**(按产品域的 getter):设计评审中的耦合反对成立;服务只做存储、校验、发布——领域含义留给拥有 schema 的注册方。 - **现在就做多层优先级**(Codex/Claude Code 式 system/managed/project 层级):延后到真实第二层出现;resolve 步骤是分层未来唯一的扩展点。 -- **现在就上跨进程锁**(Pi 的 proper-lockfile):原子替换加 watcher 收敛(后写胜出)是已记录的行为,真实冲突出现再说。 +- **现在就上跨进程锁**(Pi 的 proper-lockfile):最初以"原子替换加 watcher 收敛,真实冲突出现再说"为由延后——评审发现收敛会丢失未观察到的同级 namespace,因此该延后已被 [write-path integrity note](2026-07-30-settings-write-path-integrity.md) 的手写写锁取代。 ## 后果 diff --git a/.agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.i18n.yaml b/.agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.i18n.yaml new file mode 100644 index 0000000000..fa54d657a9 --- /dev/null +++ b/.agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.i18n.yaml @@ -0,0 +1,6 @@ +# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each +# side as of the last confirmed-consistent state. Both languages carry equal authority; +# after editing either side, bring the other along and re-record with: +# pnpm run verify-translation-pairing --write .agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.md +2026-07-30-settings-write-path-integrity.md: 07bd095162879c8e7866846cf562f6a13307e5fc +2026-07-30-settings-write-path-integrity.zh.md: 5d02177073d482b61750d7bdfbbd0866bc227a6a diff --git a/.agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.md b/.agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.md new file mode 100644 index 0000000000..07bd095162 --- /dev/null +++ b/.agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.md @@ -0,0 +1,35 @@ +# Agent Note: settings write-path integrity and observer lifecycle + +Status: implemented + +English | [中文](2026-07-30-settings-write-path-integrity.zh.md) + +> Scope: the third review round over `packages/settings/` — write-path data integrity in `dsh-settings-local` (operation chain, read-modify-write, cross-process writer lock, diff-shaped YAML edits) and observer lifecycle in `dsh-settings` (watch disposal, async listener containment, the JSON-shape write boundary). This note reverses one deferral recorded in the [user-settings seam note](2026-07-28-user-settings-seam.md): the cross-process lockfile now ships. + +## Problem + +Review found the provider's write path could destroy state it never observed, and the seam's observer lifecycle leaked past disposal. Concretely: watcher reloads and document writes ran on two independent promise chains while every write rendered the whole next document from the cached text, so an external edit still inside the debounce window was overwritten — and the follow-up reload no-oped because the post-rename content matched the cache, erasing the edit without a trace. The initial `load()` raced the watcher's own setup, leaving a startup window whose changes never fire an event. Two processes sharing a harness home rendered from independent caches, last writer winning whole namespaces. On the seam side, a `watch()` disposer only removed the observer from its set — an invocation already chained onto the watcher tail still ran after disposal, and nothing drained started invocations at service dispose; the `settings/updated` manual fan-out caught only synchronous throws, so an async listener's rejection escaped as an unhandled rejection; and `structuredClone` admitted Dates, Maps, BigInts, and cycles that YAML/JSON storage silently distorts on the reload round-trip (a Date lands as a timestamp string, a BigInt as a plain number). YAML writes replaced the whole namespace node, deleting every comment inside the section a comment-preserving provider had promised to keep. + +## Decision + +**One operation chain, and every write is a read-modify-write.** Watcher refreshes and persists from every namespace queue share a single settled chain, and `persistSection` begins by reconciling the on-disk text into the seam — publishing any unobserved difference first — before rendering against that fresh text. A write can no longer resurrect a stale document, and an on-disk document that turned invalid fails the write loud rather than being overwritten (the reload path keeps its warn-and-keep-last-good policy; the shared `reconcileFromDisk` throws and each caller picks its policy). The watcher's `ready` signal queues one extra reconcile, closing the startup gap between the initial load and the watcher becoming active. + +**Writes hold a `wx`-created `.lock` sibling.** The read-render-rename cycle runs under a cross-process writer lock with exponential backoff, a 2 s acquisition deadline, and stale takeover after 5 s (a crashed holder, broken with a warning). Readers never lock — the rename commit is atomic — so contention is writer-only and resolves in milliseconds. The lock constants are protocol invariants, not config: a holder rewrites one small document, so the deadline and stale age derive from that bound, not from deployment taste. + +**Observer disposal is quiescent.** Watchers carry an `active` flag checked when a queued invocation would start, so a disposer that ran while the invocation waited prevents the start entirely; started invocations register in a service-level `pendingTails` set that the dispose drain awaits beside the write queues. The `settings/updated` fan-out contains a returned thenable's rejection through the same listener diagnostic as a sync throw, and the event contract now states that the `INVARIANT` rethrow serves synchronous listeners only — invariant companions must stay sync, which the shipped companion already is. + +**The write boundary admits JSON data only.** The call-time snapshot is a single `cloneJsonShaped` walk that detaches the patch and rejects any non-JSON value — Date, Map, BigInt, non-finite number, function, symbol, class instance, `undefined` array entry, circular reference — with its `$`-rooted path before anything persists. Object entries that are explicitly `undefined` still skip (the sparse-patch contract), now enforced at the boundary instead of inside `mergeLayers`. + +**YAML edits are leaf-level diffs.** `renderYaml` diffs the stored section against the next one and applies only `setIn` for changed values and `deleteIn` for removed keys, recursing through maps. Comments, anchors, and formatting survive on every untouched node and on the key node of every changed pair; arrays and other non-map values replace wholesale when unequal (`deepEqualJson` is the shared predicate), taking comments inside them along. + +## Alternatives considered + +- **`proper-lockfile` instead of a hand-rolled lock** — the dependency-over-hand-rolling policy was weighed: the library is barely maintained, its stale/retry policy is broader than this one-file protocol needs, and the shipped lock is ~40 lines with deterministic tests (including injected `EEXIST`/`stat` races). The policy favors dependencies that delete owned code; this one would replace 40 explained lines with an opaque peer. +- **Revision/CAS instead of a lock** — rename cannot express compare-and-swap, so a CAS needs a version sidecar or content re-hash and a retry loop in every writer; the lock achieves the same serialization with one primitive and keeps readers free. +- **Merging external edits into the in-flight write's own section** — the seam merges patches over the state visible at call time, so a same-namespace external edit racing a write still resolves last-write-wins; folding it in would need three-way merge semantics no consumer has asked for. The write publishes the external state first, so the loser is at least observed before being superseded. +- **Declaring async `settings/updated` listeners unsupported** — the typed signature is `void` and lint flags misused promises, but an unlinted JS plugin can still register an async listener; a contract note cannot un-throw an unhandled rejection, so containment is the only defense that holds at runtime. +- **Keeping `structuredClone` and validating in the provider** — the seam is the durable boundary's owner (every provider stores JSON-shaped documents), and rejecting at call time gives the caller the offending path; a provider-side check would reject after merge, blaming the merged section instead of the caller's value. + +## Consequences + +`update()` gained a documented failure mode (lock deadline, invalid on-disk document) and the rejection messages carry `$`-rooted paths. Remaining, documented in the provider README: same-namespace concurrent edits stay last-write-wins (no per-value merge or revision check), a watcher event the OS never delivers leaves the cache stale until the next signal or write, and comments inside replaced arrays or attached inline to changed scalar values go with the value they described. The [user-settings seam note](2026-07-28-user-settings-seam.md)'s deferred-lockfile alternative is superseded by this note. The same defect classes exist in `dsh-credentials-local` (two chains over one `.env`, cached whole-file write-back, post-persist emit) and in the `llm/adapters-updated` fan-out on the stacked branches; those fixes belong to the PRs that introduce the packages and follow this template on merge-up. diff --git a/.agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.zh.md b/.agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.zh.md new file mode 100644 index 0000000000..5d02177073 --- /dev/null +++ b/.agents/notes/implemented/architecture/2026-07-30-settings-write-path-integrity.zh.md @@ -0,0 +1,41 @@ +# Agent Note: settings 写路径完整性与观察者生命周期 + +Status: implemented + +[English](2026-07-30-settings-write-path-integrity.md) | 中文 + +> 范围:对 `packages/settings/` 的第三轮评审——`dsh-settings-local` 的写路径数据完整性(操作链、读-改-写、跨进程写锁、diff 形态的 YAML 编辑)与 `dsh-settings` 的观察者生命周期(watch 的 dispose(资源释放)、异步监听器收容、JSON 形态写入边界)。本 note 推翻了[用户设置 seam note](2026-07-28-user-settings-seam.md)所记录的一项延后决定:跨进程锁文件现已交付。 + +## 问题 + +评审发现,提供方的写路径可能销毁它从未观察到的状态,而 seam 的观察者生命周期会泄漏到 dispose 之后。具体而言:watcher 重载与文档写入跑在两条相互独立的 promise 链上,而每次写入都从缓存文本渲染出完整的下一份文档,于是仍处于防抖窗口内的外部编辑会被覆盖——随后的重载又因 rename 后的内容与缓存一致而成为空操作,这次编辑就被无痕抹去。初始 `load()` 与 watcher 自身的建立过程存在竞态,留下一个启动窗口:落在这个窗口内的变更永远不会触发事件。共享同一 harness home 的两个进程各自从独立的缓存渲染,后写者以整个 namespace 为单位胜出。 + +在 seam 一侧,`watch()` 的释放器只把观察者从集合中移除——已经接到 watcher 链尾的调用在 dispose 之后照常运行,服务 dispose 时也没有任何环节排空已启动的调用;`settings/updated` 的手动扇出只捕获同步抛错,异步监听器的 rejection 会以 unhandled rejection 的形式逃逸;`structuredClone` 则放行 Date、Map、BigInt 与循环引用,而 YAML/JSON 存储会在重载往返中悄悄扭曲这些值(Date 会变成时间戳字符串,BigInt 会变成普通数字)。 + +YAML 写入则整体替换 namespace 节点,把分节内的每条注释都删掉——而这个保注释的提供方承诺过要保住它们。 + +## 决策 + +**单一操作链,且每次写入都是读-改-写。**watcher 的刷新与来自各 namespace 队列的持久化共享同一条结算链;`persistSection` 会先把磁盘上的文本对账进 seam——任何未被观察到的差异都先发布出去——然后才对照这份新鲜文本渲染。写入不再可能复活一份陈旧文档;磁盘上已变非法的文档会让写入响亮失败,而不是被覆盖(重载路径保持其“告警并保留最后可用值”策略;共享的 `reconcileFromDisk` 抛错,各调用方自选策略)。watcher 的 `ready` 信号会额外排入一次对账,弥合初始加载与 watcher 生效之间的启动缺口。 + +**写入持有以 `wx` 创建的同目录 `.lock`。**读-渲染-rename 循环在一把跨进程写锁下运行:指数退避、2 s 获取截止时间、5 s 后陈旧接管(持有者已崩溃;打破旧锁时给出告警)。读方从不加锁——rename 提交是原子的——因此竞争只发生在写方之间,毫秒级即可化解。锁的各项常量是协议不变式,不是配置:持有者只是重写一份小文档,截止时间与陈旧时限都从这一上界推得,而非出自部署偏好。 + +**观察者 dispose 达到完全停稳。**watcher 携带一个 `active` 标志,排队的调用即将启动时先检查它,因此在调用等待期间已经运行过的释放器能让这次启动彻底不发生;已启动的调用会登记进服务级的 `pendingTails` 集合,dispose 排空除了等待各写队列,还会等待该集合。`settings/updated` 扇出会把监听器返回的 thenable 的 rejection 收容进与同步抛错相同的监听器诊断;事件契约现已写明 `INVARIANT` 重抛只服务同步监听器——不变式配套插件必须保持同步,而已交付的那个配套插件本就是同步的。 + +**写入边界只放行 JSON 数据。**调用时刻的快照就是一次 `cloneJsonShaped` 遍历:它把 patch 从调用方分离出来,并在任何内容持久化之前拒绝一切非 JSON 值——Date、Map、BigInt、非有限数值、函数、symbol、类实例、值为 `undefined` 的数组元素、循环引用——拒绝时附带该值以 `$` 为根的路径。显式为 `undefined` 的对象条目仍会跳过(稀疏 patch 契约),这一契约如今在边界处强制执行,而不再放在 `mergeLayers` 内部。 + +**YAML 编辑是叶子级 diff。**`renderYaml` 对比已存储分节与下一份分节,只对变化的值应用 `setIn`、对移除的键应用 `deleteIn`,并沿 map 递归。注释、锚点与格式在每个未触碰节点上、以及每个被改键值对的键节点上全部保留;数组等非 map 值在不相等时整体替换(`deepEqualJson` 是共享的判定谓词),其内部注释随之一并被带走。 + +## 曾考虑的替代方案 + +- **用 `proper-lockfile` 取代手写锁**——按“依赖优先于手写”政策做过权衡:该库几乎无人维护,其陈旧/重试策略比这个单文件协议所需的更宽泛,而已交付的锁约 40 行并带确定性测试(含注入的 `EEXIST`/`stat` 竞态)。该政策偏向能删除自有代码的依赖;这个依赖只会把 40 行带解释的代码换成一个不透明的等价物。 +- **用修订号/CAS 取代锁**——rename 表达不了 compare-and-swap,因此 CAS 需要一个版本伴随文件或内容重哈希,外加每个写方里的一个重试循环;锁用一个原语实现同样的串行化,还让读方完全免锁。 +- **把外部编辑合并进正在进行的写入自身的分节**——seam 是在调用时刻可见的状态之上合并 patch 的,因此与写入竞态的同 namespace 外部编辑仍按后写胜出解决;要把外部编辑并进来,需要三方合并语义,而没有任何消费方提出过这种需求。写入会先发布外部状态,落败一方至少在被取代之前被观察到。 +- **宣布不支持异步 `settings/updated` 监听器**——类型签名是 `void`,lint 也会标记误用的 promise,但未经 lint 的 JS 插件仍能注册异步监听器;契约里的一句说明无法收回已经抛出的 unhandled rejection,收容是唯一在运行时守得住的防线。 +- **保留 `structuredClone`、在提供方里做校验**——seam 才是持久化边界的所有者(每个提供方存储的都是 JSON 形态文档),而且在调用时刻拒绝能把违规值的路径给到调用方;提供方侧的检查要到合并之后才拒绝,归咎的是合并后的分节,而不是调用方传入的值。 + +## 后果 + +`update()` 有了成文的失败模式(锁截止时间到期、磁盘文档非法),rejection 消息携带以 `$` 为根的路径。仍然存在、且已记录在提供方 README 中的有:同 namespace 并发编辑仍是后写胜出(没有逐值合并,也没有修订号检查);OS 从未投递的 watcher 事件会让缓存保持陈旧,直到下一个信号或下一次写入;被替换数组内部的注释、以及行内附着在被改标量值上的注释,会随其描述的值一起消失。 + +[用户设置 seam note](2026-07-28-user-settings-seam.md)里“延后锁文件”那条替代方案已被本 note 取代。同类缺陷还存在于 `dsh-credentials-local`(两条链共用一个 `.env`、按缓存整文件写回、持久化之后才发事件)与堆叠分支上的 `llm/adapters-updated` 扇出;这些修复归引入相应包(package)的那些 PR(Pull Request)所有,向上合并时按本模板处理。 diff --git a/docs/config-catalog.md b/docs/config-catalog.md index 8a960f7c04..ce8fd03d49 100644 --- a/docs/config-catalog.md +++ b/docs/config-catalog.md @@ -1247,7 +1247,7 @@ export interface Config { } ``` -Source: [`packages/settings/settings-local/src/index.ts:19`](../packages/settings/settings-local/src/index.ts) +Source: [`packages/settings/settings-local/src/index.ts:21`](../packages/settings/settings-local/src/index.ts) ## `@deepseek-ai/dsh-skill` diff --git a/docs/cordis-catalog/events.md b/docs/cordis-catalog/events.md index 6cd530e4fd..a1a7efadf4 100644 --- a/docs/cordis-catalog/events.md +++ b/docs/cordis-catalog/events.md @@ -663,13 +663,18 @@ Source: [`packages/core/session/src/index.ts:103`](../../packages/core/session/s ### `settings/updated` — emit -Committed change to one registered namespace's resolved value. Emitted after the provider persisted (for `update`) or published (`provider`) the change; never emitted when the resolved value is deep-equal. +Committed change to one registered namespace's resolved value. Emitted after the provider persisted (for `update`) or published (`provider`) the change; never emitted when the resolved value is deep-equal. Listener failures are contained and logged — a sync throw and an async rejection alike — except `INVARIANT`-coded failures, which rethrow after every listener ran; that rethrow reaches the emitter only from synchronous listeners, so invariant checks on this event must not be async functions. ```ts cordis-catalog /** * Committed change to one registered namespace's resolved value. Emitted * after the provider persisted (for `update`) or published (`provider`) * the change; never emitted when the resolved value is deep-equal. + * Listener failures are contained and logged — a sync throw and an async + * rejection alike — except `INVARIANT`-coded failures, which rethrow + * after every listener ran; that rethrow reaches the emitter only from + * synchronous listeners, so invariant checks on this event must not be + * async functions. * @param ns - the namespace whose resolved value changed. * @param next - the new resolved value. * @param prev - the previous resolved value. @@ -681,7 +686,7 @@ Committed change to one registered namespace's resolved value. Emitted after the Types: [SettingsNamespace](../core-data-structures/settings.md) · [SettingsUpdateSource](../core-data-structures/settings.md) -Source: [`packages/settings/settings/src/index.ts:97`](../../packages/settings/settings/src/index.ts) +Source: [`packages/settings/settings/src/index.ts:106`](../../packages/settings/settings/src/index.ts) ## `skills/*` diff --git a/docs/cordis-catalog/services.md b/docs/cordis-catalog/services.md index 3fe2e69334..e53d981d03 100644 --- a/docs/cordis-catalog/services.md +++ b/docs/cordis-catalog/services.md @@ -1693,7 +1693,7 @@ async replace(ns: SettingsNamespace, section: object): Promise Types: [SettingsDescriptor](../core-data-structures/settings.md) · [SettingsNamespace](../core-data-structures/settings.md) · [SettingsRegisterOptions](../core-data-structures/settings.md) · [SettingsScope](../core-data-structures/settings.md) -Source: [`packages/settings/settings/src/index.ts:176`](../../packages/settings/settings/src/index.ts) +Source: [`packages/settings/settings/src/index.ts:246`](../../packages/settings/settings/src/index.ts) ## `ctx.skills` — `SkillService` diff --git a/docs/core-data-structures/settings.i18n.yaml b/docs/core-data-structures/settings.i18n.yaml index cca43c251b..7a971156e7 100644 --- a/docs/core-data-structures/settings.i18n.yaml +++ b/docs/core-data-structures/settings.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write docs/core-data-structures/settings.md -settings.md: abbfecb35f67b27e16dffb9558a35cf368c90beb -settings.zh.md: c746e3cc181f8347cbe634b9231cfee1b3beecd3 +settings.md: 381b36b3ff2f45a2090a2a2eac0f700bd00270c4 +settings.zh.md: bc6547db3b05c5a78f112462ae205d848f93da60 diff --git a/docs/core-data-structures/settings.md b/docs/core-data-structures/settings.md index abbfecb35f..381b36b3ff 100644 --- a/docs/core-data-structures/settings.md +++ b/docs/core-data-structures/settings.md @@ -48,20 +48,24 @@ interface SettingsScope { /** * Observe committed changes to this namespace's resolved value. Invocations * of one callback run asynchronously, one at a time, in commit order; a - * rejection is contained and logged like a sync throw. + * rejection is contained and logged like a sync throw. After the disposer + * returns, no further invocation starts — one already queued is skipped; + * one already started still settles, and service disposal waits for it. * @param callback - invoked after each commit with the next and previous values. * @returns the disposer removing this observer. */ watch(callback: (next: T, prev: T) => void | Promise): () => void /** * Merge a partial patch into this namespace's user layer and persist it. - * @param patch - plain-object patch over the user section. + * @param patch - plain-object patch over the user section; JSON-shaped data + * only (non-JSON values reject with their path before anything persists). */ update(patch: object): Promise /** * Replace this namespace's user section wholesale; absent keys re-inherit * the composition `base` and schema defaults (`replace({})` resets all). - * @param section - the complete next user section. + * @param section - the complete next user section; JSON-shaped data only, + * as for {@link update}. */ replace(section: object): Promise } diff --git a/docs/core-data-structures/settings.zh.md b/docs/core-data-structures/settings.zh.md index c746e3cc18..bc6547db3b 100644 --- a/docs/core-data-structures/settings.zh.md +++ b/docs/core-data-structures/settings.zh.md @@ -48,20 +48,24 @@ interface SettingsScope { /** * Observe committed changes to this namespace's resolved value. Invocations * of one callback run asynchronously, one at a time, in commit order; a - * rejection is contained and logged like a sync throw. + * rejection is contained and logged like a sync throw. After the disposer + * returns, no further invocation starts — one already queued is skipped; + * one already started still settles, and service disposal waits for it. * @param callback - invoked after each commit with the next and previous values. * @returns the disposer removing this observer. */ watch(callback: (next: T, prev: T) => void | Promise): () => void /** * Merge a partial patch into this namespace's user layer and persist it. - * @param patch - plain-object patch over the user section. + * @param patch - plain-object patch over the user section; JSON-shaped data + * only (non-JSON values reject with their path before anything persists). */ update(patch: object): Promise /** * Replace this namespace's user section wholesale; absent keys re-inherit * the composition `base` and schema defaults (`replace({})` resets all). - * @param section - the complete next user section. + * @param section - the complete next user section; JSON-shaped data only, + * as for {@link update}. */ replace(section: object): Promise } diff --git a/docs/event-producer-consumer.md b/docs/event-producer-consumer.md index c3a30a2ba7..9638ecfedf 100644 --- a/docs/event-producer-consumer.md +++ b/docs/event-producer-consumer.md @@ -36,7 +36,7 @@ This matrix shows which packages dispatch each harness-owned event and which pac | `session/disposed` | `emit` | [`packages/core/session/src/index.ts:81`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`agent-loop`](../packages/core/agent-loop), `apiproxy`, [`session-persistence`](../packages/session-persistence/session-persistence), [`session-projection-cache`](../packages/session-projection/session-projection-cache), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title) | | `session/event` | `emit` | [`packages/core/session/src/index.ts:93`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`acp`](../packages/acp/acp), `apiproxy`, [`cli-demo`](../packages/examples/cli-demo), [`compact`](../packages/compact/compact), [`compact-basic`](../packages/compact/compact-basic), [`goal`](../packages/goal/goal), [`goal-session`](../packages/goal/goal-session), [`hook-protocol`](../packages/hooks/hook-protocol), [`jsonrpc`](../packages/ui/jsonrpc), [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence), [`session-projection`](../packages/session-projection/session-projection), [`session-projection-cache`](../packages/session-projection/session-projection-cache), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title), [`token-meter`](../packages/llm/token-meter), [`tools`](../packages/core/tools), [`tui`](../packages/ui/tui), [`user-approval`](../packages/ui/user-approval), [`workspace-context`](../packages/context/workspace-context) | | `session/flush` | `parallel` | [`packages/core/session/src/index.ts:103`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry) | -| `settings/updated` | `emit` | [`packages/settings/settings/src/index.ts:97`](../packages/settings/settings/src/index.ts) | [`settings`](../packages/settings/settings) (`events.dispatch`) | [`settings`](../packages/settings/settings) | +| `settings/updated` | `emit` | [`packages/settings/settings/src/index.ts:106`](../packages/settings/settings/src/index.ts) | [`settings`](../packages/settings/settings) (`events.dispatch`) | [`settings`](../packages/settings/settings) | | `skills/change` | `emit` | [`packages/skill/skill/src/index.ts:188`](../packages/skill/skill/src/index.ts) | [`skill`](../packages/skill/skill) (`events.dispatch`) | [`tui`](../packages/ui/tui) | | `subagent/end` | `emit` | [`packages/subagent/subagent/src/index.ts:140`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`jsonrpc`](../packages/ui/jsonrpc), [`subagent`](../packages/subagent/subagent) | | `subagent/provider-added` | `emit` | [`packages/subagent/subagent/src/index.ts:114`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`emit`) | [`subagent`](../packages/subagent/subagent), [`tool-subagent`](../packages/subagent/tool-subagent) | diff --git a/packages/cordis/tool-cordis/src/api-catalog.ts b/packages/cordis/tool-cordis/src/api-catalog.ts index e2e08d757f..811be436b7 100644 --- a/packages/cordis/tool-cordis/src/api-catalog.ts +++ b/packages/cordis/tool-cordis/src/api-catalog.ts @@ -1345,7 +1345,7 @@ export const EVENT_API: readonly EventApiEntry[] = [ name: 'settings/updated', mode: 'emit', signature: '\'settings/updated\'(ns: SettingsNamespace, next: unknown, prev: unknown, source: SettingsUpdateSource): void', - jsDoc: '/**\n * Committed change to one registered namespace\'s resolved value. Emitted\n * after the provider persisted (for `update`) or published (`provider`)\n * the change; never emitted when the resolved value is deep-equal.\n * @param ns - the namespace whose resolved value changed.\n * @param next - the new resolved value.\n * @param prev - the previous resolved value.\n * @param source - whether the change entered through `update()` or the provider.\n * @mode emit\n */', + jsDoc: '/**\n * Committed change to one registered namespace\'s resolved value. Emitted\n * after the provider persisted (for `update`) or published (`provider`)\n * the change; never emitted when the resolved value is deep-equal.\n * Listener failures are contained and logged — a sync throw and an async\n * rejection alike — except `INVARIANT`-coded failures, which rethrow\n * after every listener ran; that rethrow reaches the emitter only from\n * synchronous listeners, so invariant checks on this event must not be\n * async functions.\n * @param ns - the namespace whose resolved value changed.\n * @param next - the new resolved value.\n * @param prev - the previous resolved value.\n * @param source - whether the change entered through `update()` or the provider.\n * @mode emit\n */', summary: 'Committed change to one registered namespace\'s resolved value.', }, { diff --git a/packages/settings/settings-local/README.i18n.yaml b/packages/settings/settings-local/README.i18n.yaml index 5d44f50f9d..9638a62f96 100644 --- a/packages/settings/settings-local/README.i18n.yaml +++ b/packages/settings/settings-local/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/settings/settings-local/README.md -README.md: af8df7c030757b330e034a1c46507fbe75c9bab8 -README.zh.md: fc8943263b339baad1a92a1d0b0977b926e40f6e +README.md: 2c0817afd2f2fd35fda2d22cd7f7ef3772fe2257 +README.zh.md: 547abb035368f07d4478a5c3a1793cdaa6743c68 diff --git a/packages/settings/settings-local/README.md b/packages/settings/settings-local/README.md index af8df7c030..2c0817afd2 100644 --- a/packages/settings/settings-local/README.md +++ b/packages/settings/settings-local/README.md @@ -2,7 +2,7 @@ English | [中文](README.zh.md) -File-backed settings provider. One YAML or JSON document carries every namespace section; external edits hot-publish through `ctx.settings`, and `update()` writes back atomically while preserving the user's YAML comments and any section owned by a plugin that is not currently loaded. +File-backed settings provider. One YAML or JSON document carries every namespace section; external edits hot-publish through `ctx.settings`, and `update()` re-reads the document under a writer lock before writing back atomically, preserving the user's YAML comments, any section owned by a plugin that is not currently loaded, and any on-disk change this process has not observed yet. ## Config @@ -18,9 +18,13 @@ Defaulting is one explicit `resolveSpec(config)` step; an unsupported extension ## Behavior - **Boot fails loud, reload keeps last-good.** An existing-but-invalid document fails plugin load; once live, an unreadable or unparsable edit warns and keeps the last good sections. A missing document resolves every namespace from defaults and `base`; deleting it publishes the same empty state. -- **Write-back is atomic, owner-only, and symlink-proof.** `persist` exclusive-creates a random-suffix temp sibling with mode `0600` (`wx` refuses to follow a planted symlink) and renames over the target, cleaning the temp up on failure. YAML writes patch one namespace in the comment-preserving document; JSON re-serializes. -- **Cross-namespace writes serialize on one document.** Every namespace shares the file, so persists from different namespace queues chain internally; each render sees the text the previous write committed. -- **Dispose quiesces.** Teardown stops accepting watcher events, closes the watcher, then waits out any queued or in-flight reload, so nothing publishes after disposal. +- **Every write is a read-modify-write.** A persist first re-reads the document and publishes any difference into the seam — an external edit still inside the watcher debounce window, a change the watcher missed, or another process's write — then renders against that fresh text, so a write can never resurrect a stale document or drop an unobserved sibling section. If the on-disk document turned invalid, the write rejects loud instead of overwriting the user's manual edit. +- **Writes hold a cross-process writer lock.** The read-render-rename cycle runs under a `wx`-created `.lock` sibling with exponential backoff, a 2 s acquisition deadline (the write rejects), and stale-lock takeover after 5 s (a crashed holder, broken with a warning). Readers never take the lock: the rename commit is atomic, so reloads are always consistent. +- **Write-back is atomic, owner-only, and symlink-proof.** The render exclusive-creates a random-suffix temp sibling with mode `0600` (`wx` refuses to follow a planted symlink) and renames over the target, cleaning the temp up on failure. +- **YAML edits are leaf-level diffs.** A write sets only the values that changed and deletes only the keys that were removed, so comments, anchors, and formatting survive on every untouched node and on the key of every changed pair; a changed array (or other non-map value) replaces wholesale, taking comments inside it along. JSON re-serializes without comments. +- **Reloads and writes share one operation chain.** Watcher refreshes and persists from every namespace queue run one at a time in queue order; each render sees the text the previous operation committed. +- **The watcher's ready signal reconciles once.** The initial load races the watcher's own setup, so a change written in between never fires an event; the reconcile at ready closes that startup gap. +- **Dispose quiesces.** Teardown stops accepting watcher events, closes the watcher, then waits out any queued or in-flight operation, so nothing publishes after disposal. - **Self-write suppression by content.** The provider caches the last good text; a watcher event whose content equals the cache (its own write included) is a no-op. ## Model Experience @@ -33,6 +37,7 @@ No direct invalidation; the consuming plugin owns any request-prefix changes. ## Known Limitations and Deferred Work -- **No cross-process write lock** — concurrent writers (for example TUI and web on one home) converge by atomic replace plus watcher reload, last write wins; a lockfile is deferred until real contention shows up. -- **Comment preservation is YAML-only** — JSON documents re-serialize without comments (JSON has none) and lose hand formatting. +- **Same-namespace conflicts stay last-write-wins** — the writer lock and read-modify-write keep concurrent writers from dropping each other's namespaces, but two writers editing one namespace still resolve to the later write; there is no per-value merge or revision check. +- **A missed watcher event stays unseen until the next signal** — reads never re-stat the file, so a change the watcher fails to report is only folded in by the next event, the next write, or a restart. +- **Comment preservation is YAML-only and map-shaped** — JSON documents re-serialize without comments (JSON has none), and comments inside a changed array (or attached inline to a changed scalar value) go with the value they described. - **No value indirection** — sections hold literal values; `${env:VAR}`-style references for secrets are a deferred seam-level feature. diff --git a/packages/settings/settings-local/README.zh.md b/packages/settings/settings-local/README.zh.md index fc8943263b..547abb0353 100644 --- a/packages/settings/settings-local/README.zh.md +++ b/packages/settings/settings-local/README.zh.md @@ -2,7 +2,7 @@ [English](README.md) | 中文 -文件 settings provider。一个 YAML 或 JSON 文档承载全部 namespace 分节;外部编辑经 `ctx.settings` 热发布,`update()` 原子写回,并保留用户的 YAML 注释以及当前未加载插件所拥有的分节。 +文件 settings provider。一个 YAML 或 JSON 文档承载全部 namespace 分节;外部编辑经 `ctx.settings` 热发布,`update()` 在写锁下先重读文档再原子写回,保留用户的 YAML 注释、当前未加载插件所拥有的分节,以及任何本进程尚未观察到的磁盘变更。 ## 配置 @@ -18,9 +18,13 @@ ## 行为 - **启动报错响亮,重载保留最后可用值。** 存在但非法的文档使插件加载失败;运行中不可读或不可解析的编辑只告警并保留最后可用分节。文档缺失时所有 namespace 按默认值与 `base` 解析;删除文档发布同样的空状态。 -- **写回原子、仅属主可读、抗符号链接。** `persist` 以 `0600` 权限独占创建随机后缀临时同级文件(`wx` 拒绝跟随预埋符号链接)后 rename 覆盖目标,失败时清理临时文件。YAML 写回在保留注释的文档里只修补目标 namespace;JSON 重新序列化。 -- **跨 namespace 写入在同一文档上串行。** 所有 namespace 共享一个文件,来自不同 namespace 队列的 persist 在内部串联;每次渲染都基于上一次写入提交后的文本。 -- **Dispose 保证静止。** 卸载先停止接收 watcher 事件、关闭 watcher,再等完排队与进行中的重载,之后不再有任何发布。 +- **每次写入都是一次读-改-写。** persist 先重读文档并把任何差异发布进 seam——无论是仍在 watcher 防抖窗口内的外部编辑、watcher 漏掉的变更,还是另一个进程的写入——再基于这份新鲜文本渲染,因此写入绝不会复活陈旧文档,也不会丢掉未观察到的同级分节。若磁盘上的文档已变为非法,写入响亮拒绝,而不是覆盖用户的手工编辑。 +- **写入持有跨进程写锁。** 读-渲染-rename 流程在 `wx` 创建的 `.lock` 同级文件下运行,带指数退避、2 s 的获取期限(到期则写入拒绝)与 5 s 后的陈旧锁接管(持有者已崩溃,破锁并告警)。读取方从不取锁:rename 提交是原子的,重载因此始终一致。 +- **写回原子、仅属主可读、抗符号链接。** 渲染以 `0600` 权限独占创建随机后缀临时同级文件(`wx` 拒绝跟随预埋符号链接)后 rename 覆盖目标,失败时清理临时文件。 +- **YAML 编辑是叶子级 diff。** 写入只设置发生变化的值、只删除被移除的键,因此注释、锚点与排版在每个未触碰的节点上以及每个被改键值对的键上都得以保留;被改的数组(或其他非 map 值)整体替换,其中的注释随之一同被换掉。JSON 重新序列化,无注释。 +- **重载与写入共享一条操作链。** watcher 刷新与来自各 namespace 队列的 persist 按队列顺序逐个执行;每次渲染都基于上一次操作提交后的文本。 +- **watcher 的 ready 信号做一次对账。** 初始加载与 watcher 自身的建立存在竞态,因此其间写入的变更绝不会触发事件;ready 时的对账补上这个启动缺口。 +- **Dispose 保证静止。** 卸载先停止接收 watcher 事件、关闭 watcher,再等完排队与进行中的操作,之后不再有任何发布。 - **按内容抑制自写。** provider 缓存最后可用文本;watcher 事件内容与缓存相同(含自己的写入)即为 no-op。 ## Model Experience @@ -33,6 +37,7 @@ ## Known Limitations and Deferred Work -- **无跨进程写锁** — 并发写入者(例如同一 home 上的 TUI 与 web)靠原子替换加 watcher 重载收敛,后写胜出;lockfile 等真实冲突出现再做。 -- **注释保留仅限 YAML** — JSON 文档重新序列化,无注释(JSON 本身没有)且丢失手工排版。 +- **同 namespace 冲突仍是后写胜出** — 写锁加读-改-写让并发写入者不会丢掉彼此的 namespace,但两个写入者编辑同一个 namespace 时仍以较后的写入为准;没有按值合并,也没有修订检查。 +- **漏掉的 watcher 事件在下一个信号前保持不可见** — 读取从不重新 stat 文件,因此 watcher 漏报的变更只会在下一个事件、下一次写入或重启时被并入。 +- **注释保留仅限 YAML 且仅限 map 形状** — JSON 文档重新序列化,无注释(JSON 本身没有),且被改数组内部的注释(或行内附着在被改标量值上的注释)随其所描述的值一同被换掉。 - **无值间接引用** — 分节存字面值;面向密钥的 `${env:VAR}` 式引用是 seam 层的延后特性。 diff --git a/packages/settings/settings-local/src/index.ts b/packages/settings/settings-local/src/index.ts index b9df71f9e1..04ba6808a3 100644 --- a/packages/settings/settings-local/src/index.ts +++ b/packages/settings/settings-local/src/index.ts @@ -1,7 +1,9 @@ /** * File-backed settings provider. One YAML or JSON document under the user's * harness home carries every namespace section; external edits hot-publish - * through the seam and `update()` writes back preserving the user's comments. + * through the seam, and every write re-reads the document under a + * cross-process writer lock before patching it as a comment-preserving + * leaf-level diff. * @module @deepseek-ai/dsh-settings-local */ diff --git a/packages/settings/settings/README.i18n.yaml b/packages/settings/settings/README.i18n.yaml index 63a274dd4d..4f03498b06 100644 --- a/packages/settings/settings/README.i18n.yaml +++ b/packages/settings/settings/README.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write packages/settings/settings/README.md -README.md: ff6cdeb57a265dbaa9d5f50de1d558f1e3cb581f -README.zh.md: d820a5c1fa804455c439f1a155e7628f5118a49b +README.md: ec9f0e09c47015edd8495dac48beb610e0b5cdc5 +README.zh.md: 6d0a760f9b1bbef21881a03933d0fe5b9fc3cd0d diff --git a/packages/settings/settings/README.md b/packages/settings/settings/README.md index ff6cdeb57a..ec9f0e09c4 100644 --- a/packages/settings/settings/README.md +++ b/packages/settings/settings/README.md @@ -9,10 +9,10 @@ Abstract user-settings seam (`ctx.settings`). One provider holds a raw document - `register(ns, schema, { base?, applies? })` — returns the owner `SettingsScope` (`get`/`watch`/`update`). The registration is an effect on the calling plugin's fiber: disposing that fiber removes the namespace and its observers. A stored section the schema rejects fails the registration itself; a duplicate namespace fails loud. - `describe()` — one descriptor per namespace (`schema.toJSON()` envelope, resolved value, `applies`) for configuration surfaces. - `get(ns)` — resolved value, `undefined` while unregistered. -- `update(ns, patch)` — deep-merges the plain-object patch into the user section only (never the `base`), validates the resolved candidate, persists through the provider, then commits. Validation failure rejects before anything is persisted; a read-only provider (`writable: false`) rejects every write. Writes to one namespace are serialized in call order. +- `update(ns, patch)` — deep-merges the plain-object patch into the user section only (never the `base`), validates the resolved candidate, persists through the provider, then commits. Patches must be JSON-shaped data: a Date, Map, BigInt, non-finite number, or circular reference rejects with its `$`-rooted path before anything persists (YAML/JSON storage would silently distort such values on reload). Validation failure rejects before anything is persisted; a read-only provider (`writable: false`) rejects every write. Writes to one namespace are serialized in call order. - `replace(ns, section)` — sets the user section wholesale: the removal/reset path a merge cannot express (`replace({})` re-inherits `base` and schema defaults). -- Resolved values are deep-frozen snapshots. Watchers receive `(next, prev)` after each commit: invocations of one callback run asynchronously, one at a time, in commit order (a slow stale invocation can never apply after a newer one), and failures — sync throws and async rejections alike — are contained. The `settings/updated` event fans out one listener at a time, so one throwing listener cannot starve the rest. -- Service teardown refuses new writes and drains every queued write before disposal completes; a write whose registrant fiber was disposed mid-flight still reaches storage but commits and notifies nobody. +- Resolved values are deep-frozen snapshots. Watchers receive `(next, prev)` after each commit: invocations of one callback run asynchronously, one at a time, in commit order (a slow stale invocation can never apply after a newer one), and failures — sync throws and async rejections alike — are contained. After a watch disposer returns, no further invocation starts (one already queued is skipped); an invocation already started still settles. The `settings/updated` event fans out one listener at a time, so one throwing listener cannot starve the rest; an async listener's rejection is contained and logged, which is why `INVARIANT`-coded failures rethrow only from synchronous listeners. +- Service teardown refuses new writes and watcher starts, then drains every queued write and every started watcher invocation before disposal completes; a write whose registrant fiber was disposed mid-flight still reaches storage but commits and notifies nobody. ## Provider contract @@ -33,5 +33,5 @@ No direct invalidation; a consumer that folds a settings value into the request ## Known Limitations and Deferred Work - **Single user layer** — resolution knows schema defaults, one composition `base`, and one user document; there is no project/managed layering or per-value provenance yet. -- **Cross-process concurrency is provider-defined** — the seam serializes writes per namespace in-process only; concurrent processes converge by provider behavior (the local file provider is last-write-wins). +- **Cross-process concurrency is provider-defined** — the seam serializes writes per namespace in-process only; concurrent processes converge by provider behavior (the local file provider read-modify-writes under a writer lock, so namespaces survive concurrent writers and same-namespace conflicts resolve last-write-wins). - **No secret-field redaction** — `describe()` returns resolved values verbatim; a wire surface (RPC/UI) must redact `role('secret')` fields before exposure. diff --git a/packages/settings/settings/README.zh.md b/packages/settings/settings/README.zh.md index d820a5c1fa..6d0a760f9b 100644 --- a/packages/settings/settings/README.zh.md +++ b/packages/settings/settings/README.zh.md @@ -9,10 +9,10 @@ - `register(ns, schema, { base?, applies? })` — 返回 owner 的 `SettingsScope`(`get`/`watch`/`update`)。注册是调用方插件 fiber 上的 effect:dispose 该 fiber 即移除 namespace 及其观察者。schema 拒绝的存量分节会使注册本身失败;重复 namespace 立即报错。 - `describe()` — 每个 namespace 一条描述(`schema.toJSON()` 信封、解析值、`applies`),供配置界面使用。 - `get(ns)` — 解析值;未注册时为 `undefined`。 -- `update(ns, patch)` — 把普通对象 patch 深合并进用户分节(绝不合并进 `base`),校验解析候选值,经 provider 持久化后提交。校验失败在持久化前拒绝;只读 provider(`writable: false`)拒绝一切写入。同一 namespace 的写入按调用顺序串行。 +- `update(ns, patch)` — 把普通对象 patch 深合并进用户分节(绝不合并进 `base`),校验解析候选值,经 provider 持久化后提交。patch 必须是 JSON 形状的数据:Date、Map、BigInt、非有限数或循环引用会在任何内容持久化前带着以 `$` 为根的路径拒绝(YAML/JSON 存储在重载时会静默扭曲这类值)。校验失败在持久化前拒绝;只读 provider(`writable: false`)拒绝一切写入。同一 namespace 的写入按调用顺序串行。 - `replace(ns, section)` — 整体替换用户分节:merge 表达不了的删除/重置路径(`replace({})` 重新继承 `base` 与 schema 默认值)。 -- 解析值是深冻结快照。每次提交后观察者收到 `(next, prev)`:同一回调的调用异步、逐次、按提交顺序执行(慢的旧调用绝不会覆盖更新的结果),异常——同步抛出与异步拒绝——均被隔离。`settings/updated` 事件逐 listener 扇出,一个抛错的 listener 不会饿死其余 listener。 -- 服务卸载先拒绝新写入并排干全部排队写入后才完成;registrant fiber 在写入途中被 dispose 时,该写入仍到达存储,但不向任何人提交或通知。 +- 解析值是深冻结快照。每次提交后观察者收到 `(next, prev)`:同一回调的调用异步、逐次、按提交顺序执行(慢的旧调用绝不会覆盖更新的结果),异常——同步抛出与异步拒绝——均被隔离。watch 的 disposer 返回后不再启动新的调用(已排队的那一次会被跳过);已启动的调用仍会结算。`settings/updated` 事件逐 listener 扇出,一个抛错的 listener 不会饿死其余 listener;异步 listener 的拒绝会被隔离并记入日志,这正是 `INVARIANT` 编码的失败只从同步 listener 重新抛出的原因。 +- 服务卸载先拒绝新写入与观察者调用的启动,再排干全部排队写入与已启动的观察者调用后才完成;registrant fiber 在写入途中被 dispose 时,该写入仍到达存储,但不向任何人提交或通知。 ## Provider 契约 @@ -33,5 +33,5 @@ ## Known Limitations and Deferred Work - **单一用户层** — 解析只认识 schema 默认值、一个组合 `base` 与一个用户文档;尚无 project/managed 分层或按值溯源。 -- **跨进程并发由 provider 定义** — seam 仅在进程内按 namespace 串行化写入;跨进程并发按 provider 行为收敛(本地文件 provider 为后写胜出)。 +- **跨进程并发由 provider 定义** — seam 仅在进程内按 namespace 串行化写入;跨进程并发按 provider 行为收敛(本地文件 provider 在写锁下读-改-写,因此 namespace 在并发写入者下不会丢失,同 namespace 冲突按后写胜出解决)。 - **无 secret 字段脱敏** — `describe()` 原样返回解析值;wire 面(RPC/UI)在暴露前必须对 `role('secret')` 字段脱敏。 From 2b379799ba5e38b378e11c8ae2ad0c5b860a6969 Mon Sep 17 00:00:00 2001 From: Yichen Jiang Date: Thu, 30 Jul 2026 14:13:15 +0800 Subject: [PATCH 11/17] test(settings): make third-review specs conform to strict optional and misused-promise contracts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drop the explicit-undefined base fixture exactOptionalPropertyTypes forbids (the repository trusts TypeScript at typed same-process seams — no test for an input the static interface excludes; coverage holds), and reshape the async-listener containment fixture as an unknown-returning function: the earlier inline cast was silently stripped by the staged oxlint fixer, leaving a shape the next lint pass rejects. --- .../settings/settings/tests/settings.spec.ts | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/settings/settings/tests/settings.spec.ts b/packages/settings/settings/tests/settings.spec.ts index cfd88b166f..379ce02d08 100644 --- a/packages/settings/settings/tests/settings.spec.ts +++ b/packages/settings/settings/tests/settings.spec.ts @@ -228,14 +228,6 @@ describe('update', () => { expect(scope.get()).toEqual({ theme: 'light', fontSize: 18 }) }) - it('ignores an explicit undefined entry in the composition base layer', async () => { - const { ctx } = await boot({ doc: { 'ui-theme': { theme: 'light' } } }) - const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema, { - base: { theme: undefined, fontSize: 16 }, - }) - expect(scope.get()).toEqual({ theme: 'light', fontSize: 16 }) - }) - it('rejects a non-object patch', async () => { const { ctx } = await boot() const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) @@ -616,12 +608,13 @@ describe('third review regressions', () => { it('contains an async settings/updated listener rejection and keeps other listeners running', async () => { const { ctx, provider } = await boot() - // An async listener violates the event's synchronous signature (typed - // consumers get a lint error for it), but an unlinted JS plugin can still - // register one; the cast simulates exactly that caller. - ctx.on('settings/updated', async () => { - throw new Error('async listener boom') - }) + // An async listener violates the event's synchronous signature, but an + // unlinted JS plugin can still register one. Declaring the return as + // unknown keeps this file's typed surface legal (unknown-returning + // functions are assignable to void positions) while the runtime value is + // still the rejected promise the containment guard must handle. + const boom = (): unknown => Promise.reject(new Error('async listener boom')) + ctx.on('settings/updated', boom) const second = vi.fn() ctx.on('settings/updated', second) ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema) From 07e3d15d4250d0c42ccae9ebefda61a7518a26b4 Mon Sep 17 00:00:00 2001 From: Tianyi Cui <53024+tianyicui@users.noreply.github.com> Date: Thu, 30 Jul 2026 17:07:29 +0800 Subject: [PATCH 12/17] docs(settings): mark deferred review follow-ups --- packages/settings/settings-local/src/index.ts | 4 ++++ packages/settings/settings/src/index.ts | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/packages/settings/settings-local/src/index.ts b/packages/settings/settings-local/src/index.ts index 04ba6808a3..d0d1497b16 100644 --- a/packages/settings/settings-local/src/index.ts +++ b/packages/settings/settings-local/src/index.ts @@ -214,6 +214,8 @@ export class SettingsLocal extends Settings { // carries owner-only permissions that survive the rename — a document that // may hold personal values is never world-readable and never a symlink. const temp = `${this.spec.filename}.${randomBytes(6).toString('hex')}.tmp` + // TODO(settings-atomic-durability): Use a replacement that fsyncs the file + // and parent directory and preserves owner-only permissions on Windows. try { await writeFile(temp, output, { mode: 0o600, flag: 'wx' }) await rename(temp, this.spec.filename) @@ -248,6 +250,8 @@ export class SettingsLocal extends Settings { // is free right now, so retry without burning backoff or deadline. if (ageMs === undefined) continue if (ageMs > LOCK_STALE_MS) { + // TODO(settings-lock-ownership): Replace age-only takeover with ownership-safe + // acquisition and release so a slow writer cannot remove a successor's lock. this.ctx.logger.warn('settings-local: breaking a stale writer lock at %s', lockPath) await rm(lockPath, { force: true }) continue diff --git a/packages/settings/settings/src/index.ts b/packages/settings/settings/src/index.ts index 27decc7db5..76c1082f3f 100644 --- a/packages/settings/settings/src/index.ts +++ b/packages/settings/settings/src/index.ts @@ -43,6 +43,8 @@ export interface SettingsRegisterOptions { /** One registered namespace as surfaced to configuration UIs. */ export interface SettingsDescriptor { + // TODO(settings-namespace-vocabulary): Rename `ns` to `namespace` across the + // public seam, provider contract, implementations, tests, and consumers. /** The registered namespace. */ ns: SettingsNamespace /** Serialized schemastery schema (`schema.toJSON()`). */ @@ -181,6 +183,8 @@ function cloneJsonShaped( if (isPlainObject(value)) { if (visiting.has(value)) throw reject('a circular reference', path) visiting.add(value) + // TODO(settings-json-properties): Use property-safe construction here and + // in mergeLayers so valid JSON keys such as "__proto__" remain own data. const out: Record = {} for (const [key, entry] of Object.entries(value)) { if (entry === undefined) continue @@ -321,6 +325,8 @@ export abstract class Settings extends Service { } this.ctx.effect(() => { this.registrations.set(ns, registration) + // TODO(settings-registration-quiescence): Deactivate every watcher and await + // its tail on disposal so callbacks cannot outlive the registrant fiber. return () => this.registrations.delete(ns) }, `settings.register(${JSON.stringify(String(ns))})`) return { @@ -425,6 +431,8 @@ export abstract class Settings extends Service { // only when this registration is still the namespace owner — a fiber // disposed (or replaced) mid-persist must not receive the notification. this.document[ns] = section + // TODO(settings-replacement-resync): Re-resolve any replacement registration + // from this persisted section so an old in-flight write cannot leave it stale. if (this.registrations.get(ns) === registration && !this.isStopped()) { this.commit(registration, next, 'update') } From 86ddef6c01ea56477152f656deb47bd9eadd9b2f Mon Sep 17 00:00:00 2001 From: Tianyi Cui <53024+tianyicui@users.noreply.github.com> Date: Thu, 30 Jul 2026 18:35:32 +0800 Subject: [PATCH 13/17] docs: refresh settings catalog locations --- docs/cordis-catalog/events.md | 2 +- docs/cordis-catalog/services.md | 2 +- docs/event-producer-consumer.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/cordis-catalog/events.md b/docs/cordis-catalog/events.md index f7383d1212..5e4521bd77 100644 --- a/docs/cordis-catalog/events.md +++ b/docs/cordis-catalog/events.md @@ -686,7 +686,7 @@ Committed change to one registered namespace's resolved value. Emitted after the Types: [SettingsNamespace](../core-data-structures/settings.md) · [SettingsUpdateSource](../core-data-structures/settings.md) -Source: [`packages/settings/settings/src/index.ts:106`](../../packages/settings/settings/src/index.ts) +Source: [`packages/settings/settings/src/index.ts:108`](../../packages/settings/settings/src/index.ts) ## `skills/*` diff --git a/docs/cordis-catalog/services.md b/docs/cordis-catalog/services.md index 5d8705f23d..9dc24cd8b5 100644 --- a/docs/cordis-catalog/services.md +++ b/docs/cordis-catalog/services.md @@ -1693,7 +1693,7 @@ async replace(ns: SettingsNamespace, section: object): Promise Types: [SettingsDescriptor](../core-data-structures/settings.md) · [SettingsNamespace](../core-data-structures/settings.md) · [SettingsRegisterOptions](../core-data-structures/settings.md) · [SettingsScope](../core-data-structures/settings.md) -Source: [`packages/settings/settings/src/index.ts:246`](../../packages/settings/settings/src/index.ts) +Source: [`packages/settings/settings/src/index.ts:250`](../../packages/settings/settings/src/index.ts) ## `ctx.skills` — `SkillService` diff --git a/docs/event-producer-consumer.md b/docs/event-producer-consumer.md index 0d88723d22..c82ffb5883 100644 --- a/docs/event-producer-consumer.md +++ b/docs/event-producer-consumer.md @@ -36,7 +36,7 @@ This matrix shows which packages dispatch each harness-owned event and which pac | `session/disposed` | `emit` | [`packages/core/session/src/index.ts:81`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`agent-loop`](../packages/core/agent-loop), `apiproxy`, [`session-persistence`](../packages/session-persistence/session-persistence), [`session-projection-cache`](../packages/session-projection/session-projection-cache), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title) | | `session/event` | `emit` | [`packages/core/session/src/index.ts:93`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`acp`](../packages/acp/acp), `apiproxy`, [`cli-demo`](../packages/examples/cli-demo), [`compact`](../packages/compact/compact), [`compact-basic`](../packages/compact/compact-basic), [`goal`](../packages/goal/goal), [`goal-session`](../packages/goal/goal-session), [`hook-protocol`](../packages/hooks/hook-protocol), [`jsonrpc`](../packages/ui/jsonrpc), [`session`](../packages/core/session), [`session-persistence`](../packages/session-persistence/session-persistence), [`session-projection`](../packages/session-projection/session-projection), [`session-projection-cache`](../packages/session-projection/session-projection-cache), [`session-telemetry`](../packages/telemetry/session-telemetry), [`session-title`](../packages/session-title/session-title), [`token-meter`](../packages/llm/token-meter), [`tools`](../packages/core/tools), [`tui`](../packages/ui/tui), [`user-approval`](../packages/ui/user-approval), [`workspace-context`](../packages/context/workspace-context) | | `session/flush` | `parallel` | [`packages/core/session/src/index.ts:103`](../packages/core/session/src/index.ts) | [`session`](../packages/core/session) (`events.dispatch`) | [`session-persistence`](../packages/session-persistence/session-persistence), [`session-telemetry`](../packages/telemetry/session-telemetry) | -| `settings/updated` | `emit` | [`packages/settings/settings/src/index.ts:106`](../packages/settings/settings/src/index.ts) | [`settings`](../packages/settings/settings) (`events.dispatch`) | [`settings`](../packages/settings/settings) | +| `settings/updated` | `emit` | [`packages/settings/settings/src/index.ts:108`](../packages/settings/settings/src/index.ts) | [`settings`](../packages/settings/settings) (`events.dispatch`) | [`settings`](../packages/settings/settings) | | `skills/change` | `emit` | [`packages/skill/skill/src/index.ts:188`](../packages/skill/skill/src/index.ts) | [`skill`](../packages/skill/skill) (`events.dispatch`) | [`tui`](../packages/ui/tui) | | `subagent/end` | `emit` | [`packages/subagent/subagent/src/index.ts:140`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`events.dispatch`) | [`hooks-claude`](../packages/hooks/hooks-claude), [`jsonrpc`](../packages/ui/jsonrpc), [`subagent`](../packages/subagent/subagent) | | `subagent/provider-added` | `emit` | [`packages/subagent/subagent/src/index.ts:114`](../packages/subagent/subagent/src/index.ts) | [`subagent`](../packages/subagent/subagent) (`emit`) | [`subagent`](../packages/subagent/subagent), [`tool-subagent`](../packages/subagent/tool-subagent) | From 6d6c146f8105f09ed0b8626729ed7fbe26654e17 Mon Sep 17 00:00:00 2001 From: Tianyi Cui <53024+tianyicui@users.noreply.github.com> Date: Thu, 30 Jul 2026 19:02:10 +0800 Subject: [PATCH 14/17] ci: allocate consumer runner independently --- ...ence-based-larger-hosted-runners.i18n.yaml | 4 +- ...22-evidence-based-larger-hosted-runners.md | 8 +-- ...evidence-based-larger-hosted-runners.zh.md | 10 ++-- ...30-independent-ci-consumer-build.i18n.yaml | 6 +++ ...026-07-30-independent-ci-consumer-build.md | 35 +++++++++++++ ...-07-30-independent-ci-consumer-build.zh.md | 35 +++++++++++++ ...6-07-24-web-gui-browser-e2e-lane.i18n.yaml | 4 +- .../2026-07-24-web-gui-browser-e2e-lane.md | 2 +- .../2026-07-24-web-gui-browser-e2e-lane.zh.md | 2 +- ...-30-web-browser-snapshot-ci-gate.i18n.yaml | 4 +- ...2026-07-30-web-browser-snapshot-ci-gate.md | 6 +-- ...6-07-30-web-browser-snapshot-ci-gate.zh.md | 6 +-- .github/workflows/ci.yml | 26 +--------- scripts/run-gates.spec.ts | 28 ++++++++--- scripts/run-gates.ts | 49 ++++++++++++------- 15 files changed, 152 insertions(+), 73 deletions(-) create mode 100644 .agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.i18n.yaml create mode 100644 .agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.md create mode 100644 .agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.zh.md diff --git a/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.i18n.yaml b/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.i18n.yaml index ba77f5f044..88d32203c0 100644 --- a/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.i18n.yaml +++ b/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write .agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.md -2026-07-22-evidence-based-larger-hosted-runners.md: 983d5520bd73fc3cf82c37bf0d4a9ff1c6e6f51c -2026-07-22-evidence-based-larger-hosted-runners.zh.md: a86dcf2c60d7b950e7557e84ef6993e712a2ce09 +2026-07-22-evidence-based-larger-hosted-runners.md: d46b8291ec05e997728da76354354f9e36bd2fb4 +2026-07-22-evidence-based-larger-hosted-runners.zh.md: e05ad30a713258ed7bc3d8099f8d6fab3d7c0c5d diff --git a/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.md b/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.md index 983d5520bd..d46b8291ec 100644 --- a/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.md +++ b/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.md @@ -18,9 +18,9 @@ The required primary path depends on those enterprise pools. Standard GitHub-hos The former gate-level and coarse primary shard jobs are absent from the workflow. Their static, lint, coverage, snapshot, and scenario shard selectors are also absent from the repository, so an unused diagnostic path cannot preserve a second CI architecture. -Linux primary work uses three independent 32-core jobs. Coverage runs alone with its own worker bound, and the static scheduler runs alone so its result has no post-build consumer tail. After static gates finish, that job publishes its emitted `apps/*/lib`, `packages/*/*/lib`, and `vendor/*/lib` tree as a run-scoped artifact. The third job restores that exact tree, then starts lint, Node 24 runtime compatibility, build-backed snapshots, and all artifact consumers without repeating the build. Generated NodeNext consumer directories are excluded from Oxlint discovery because the artifact check removes them while these processes overlap. The pnpm store is restored without putting cache uploads on the pull-request critical path; Oxlint has no repository-managed result cache. Performance reports use each job's `startedAt` to `completedAt` interval; runner queue delay is capacity evidence, not repository execution time. +Linux primary work uses three independent 32-core jobs. Coverage runs alone with its own worker bound, and the static scheduler owns source and documentation gates that do not consume emitted output. The third job owns the single Linux build, then starts lint, Node 24 runtime compatibility, build-backed snapshots, documentation typechecking, and all artifact consumers against that tree. This [independent consumer build](2026-07-30-independent-ci-consumer-build.md) lets all three jobs request runners immediately without duplicating compilation or transferring a run-scoped artifact. Generated NodeNext consumer directories are excluded from Oxlint discovery because the artifact check removes them while these processes overlap. The pnpm store is restored without putting cache uploads on the pull-request critical path; Oxlint has no repository-managed result cache. Performance reports use each job's `startedAt` to `completedAt` interval; runner queue delay is capacity evidence, not repository execution time. -The gate dependencies remain explicit. Coverage consumes source and does not wait for build. Documentation typechecking builds its complete project-reference graph once. Snapshot replay and publication consumers wait for emitted output, while Node-version compatibility jobs exercise runtime-sensitive source loading without repeating the primary source-graph typecheck. PTY and subprocess suites keep their bounded inner concurrency rather than inheriting the runner's core count. +The gate dependencies remain explicit. Coverage consumes source and does not wait for build. Documentation typechecking consumes the consumer lane's complete project-reference output. Snapshot replay and publication consumers wait for emitted output, while Node-version compatibility jobs exercise runtime-sensitive source loading without repeating the primary source-graph typecheck. PTY and subprocess suites keep their bounded inner concurrency rather than inheriting the runner's core count. The artifact boundary remains explicit. `scripts/publint-all.ts` calls publint's supported API against an in-memory publication view formed from each manifest's declared files plus npm's mandatory metadata, avoiding one package-manager pack process per package. `scripts/verify-built-package-invariants.mjs` stages the declared `lib/` files below the real package and imports its compiled self-reference through plain Node and Cordis Loader normalization; a runtime chunk omitted from the publication contract still fails. @@ -68,7 +68,7 @@ An additional serial Linux reference runs on the in-house self-hosted pool (`vm- **Keep build behind typecheck.** This orders independent compiler invocations and turns snapshot replay into a three-stage critical chain. Build output has its own success dependency, so only snapshot and publication consumers wait for it. -**Keep static gates and post-build consumers on one runner.** Reusing one workspace avoids a setup wave and artifact transfer, but build-duration variance delays every consumer and leaves their lint and snapshot tails after the static result. A run-scoped built tree preserves one exact build while independent jobs keep both complete paths within the observed target. +**Publish the static job's build to post-build consumers.** A run-scoped artifact preserves one exact build, but the workflow can only consume it by waiting for the entire static job and then requesting another runner. The [independent consumer build](2026-07-30-independent-ci-consumer-build.md) assigns the single Linux build to its actual consumers instead. **Keep the complete required path on standard GitHub-hosted capacity.** This avoids repository-external runner configuration, but exact-head standard-runner runs remain materially slower and can spend longer queued behind shared capacity. Standard-hosted compatibility and serial references preserve portable evidence without making that slower topology the ordinary primary path. @@ -80,7 +80,7 @@ An additional serial Linux reference runs on the in-house self-hosted pool (`vm- The required topology pays one setup wave per 32-core lane and retains no shard selectors. Every ordinary pull request consumes paid enterprise Linux and Windows minutes; manual benchmarks add other sizes only when remeasurement is useful. -GitHub rounds each larger-runner execution up to a whole minute, so complete-job measurement exposes both billed time and workflow complexity. Splitting Linux repeats setup twice and transfers one built tree, but isolates coverage, static gates, and post-build consumers from each other's critical paths without repeating the build; consolidating Windows avoids repeating its slower setup. +GitHub rounds each larger-runner execution up to a whole minute, so complete-job measurement exposes both billed time and workflow complexity. Splitting Linux repeats setup twice, but the consumer lane owns the only built tree and coverage, static gates, and post-build consumers enter runner allocation independently; consolidating Windows avoids repeating its slower setup. Performance targets are observations, not cancellation deadlines or correctness requirements. Manual all-size and serial suites remain available when image, dependency, scheduler, or pricing changes need remeasurement. diff --git a/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.zh.md b/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.zh.md index a86dcf2c60..e05ad30a71 100644 --- a/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.zh.md +++ b/.agents/notes/implemented/process/2026-07-22-evidence-based-larger-hosted-runners.zh.md @@ -18,11 +18,11 @@ Status: implemented 原有的门禁级和粗粒度主流程分片作业已从工作流中移除。相应的静态、lint、覆盖率、快照和场景分片选择器也已从仓库中移除,因此未使用的诊断路径无法继续维系第二套 CI 架构。 -Linux 主流程使用 3 个相互独立的 32 核作业。覆盖率单独运行,并设有自己的工作线程上限;静态调度器也单独运行,因此构建后的消费方不会拖延其结果。静态门禁完成后,该作业将其生成的 `apps/*/lib`、`packages/*/*/lib` 和 `vendor/*/lib` 目录树作为仅供本次运行使用的产物发布。第三个作业恢复完全相同的目录树,再让 lint、Node 24 运行时兼容性、依赖构建产物的快照和所有产物消费方基于构建完成后的工作树启动,而不重复构建。生成的 NodeNext 消费方目录不会纳入 Oxlint 的文件发现范围,因为这些进程重叠执行时,产物检查会删除这些目录。pnpm store 会得到恢复,但缓存上传不会进入拉取请求关键路径;Oxlint 没有由仓库管理的结果缓存。性能报告采用每个作业从 `startedAt` 到 `completedAt` 的区间;运行器排队延迟是容量证据,而非仓库执行时间。 +Linux 主流程使用 3 个相互独立的 32 核作业。覆盖率单独运行,并设有自己的工作线程上限;静态调度器负责不消费生成输出的源码和文档门禁。第三个作业负责唯一一次 Linux 构建,随后让 lint、Node 24 运行时兼容性、依赖构建产物的快照、文档类型检查和所有产物消费方基于该目录树启动。这种[消费方独立构建](2026-07-30-independent-ci-consumer-build.md)使 3 个作业都能立即请求运行器,而无需重复编译或传输仅供本次运行使用的产物。生成的 NodeNext 消费方目录不会纳入 Oxlint 的文件发现范围,因为这些进程重叠执行时,产物检查会删除这些目录。pnpm store 会得到恢复,但缓存上传不会进入拉取请求关键路径;Oxlint 没有由仓库管理的结果缓存。性能报告采用每个作业从 `startedAt` 到 `completedAt` 的区间;运行器排队延迟是容量证据,而非仓库执行时间。 -门禁依赖关系保持显式。覆盖率消费源码,不等待构建。文档类型检查只构建一次完整的 project-reference 图。快照回放和发布消费方等待生成的输出,而 Node 版本兼容性作业会验证对运行时敏感的源码加载,且不重复主源码项目图的类型检查。PTY 和子进程套件继续使用自身有界的内部并发,不继承运行器的核心数。 +门禁依赖关系保持显式。覆盖率消费源码,不等待构建。文档类型检查以消费方通道的完整 project-reference 输出为输入。快照回放和发布消费方等待生成的输出,而 Node 版本兼容性作业会验证对运行时敏感的源码加载,且不重复主源码项目图的类型检查。PTY 和子进程套件继续使用自身有界的内部并发,不继承运行器的核心数。 -产物边界保持显式。`scripts/publint-all.ts` 对内存中的发布视图调用 publint 支持的 API;该视图由每个 manifest(元数据清单)声明的文件和 npm 强制要求的元数据组成,从而避免为每个包启动一次包管理器 pack 进程。`scripts/verify-built-package-invariants.mjs` 将已声明的 `lib/` 文件暂存到真实包下,并通过普通 Node 和 Cordis Loader 规范化导入其已编译的自身引用;发布契约只要遗漏一个运行时分片,检查仍会失败。 +产物边界保持显式。`scripts/publint-all.ts` 对内存中的发布视图调用 publint 支持的 API;该视图由每个 manifest(元数据清单)声明的文件和 npm 强制要求的元数据组成,从而避免为每个包(package)启动一次包管理器 pack 进程。`scripts/verify-built-package-invariants.mjs` 将已声明的 `lib/` 文件暂存到真实包下,并通过普通 Node 和 Cordis Loader 规范化导入其已编译的自身引用;发布契约只要遗漏一个运行时分片,检查仍会失败。 Windows 以一次 32 核环境设置同时承载阻塞性构建、生产网站和观测性的构建产物契约。重复的 lint、覆盖率和快照清单由 Linux 承担,因为在 Windows 上运行这些观测性副本会延长付费关键路径,却不会新增任何阻塞性平台契约。 @@ -68,7 +68,7 @@ Windows 仓库工作在超过 16 核后收益很小,但 32 核池可以让完 **让构建继续等待类型检查。** 此方案会给相互独立的编译器调用排定先后顺序,并把快照回放变成 3 阶段关键链。构建输出本身有独立的成功依赖关系,因此只有快照和发布消费方需要等待它。 -**将静态门禁和构建后消费方保留在同一台运行器上。** 复用同一个工作区可以省去一轮设置和一次产物传输,但构建耗时的波动会延迟每个消费方,并使消费方的 lint 和快照尾段延续到静态结果之后。仅供本次运行使用的已构建目录树可以保留同一份构建结果,而相互独立的作业能让两条完整路径都保持在实测目标内。 +**将静态作业的构建发布给构建后消费方。** 仅供本次运行使用的产物能保留同一份构建结果,但工作流要消费它,只能先等待整个静态作业完成,再请求另一台运行器。[消费方独立构建](2026-07-30-independent-ci-consumer-build.md)则转而让实际消费方负责唯一一次 Linux 构建。 **将完整必需路径保留在 GitHub 标准托管容量上。** 此方案可以避免依赖仓库外部的运行器配置,但标准运行器上的分支头精确运行仍明显更慢,也可能因共享容量而排队更久。标准托管兼容性作业和串行参考流程保留可移植证据,无需让这套较慢的拓扑成为普通主路径。 @@ -80,7 +80,7 @@ Windows 仓库工作在超过 16 核后收益很小,但 32 核池可以让完 必需拓扑中的每个 32 核通道只承担 1 轮设置开销,且不保留分片选择器。每个普通拉取请求都会消耗付费的企业级 Linux 和 Windows 运行器分钟数;只有在重新测量有价值时,手动基准测试才会加入其他规格。 -GitHub 会把每次大型运行器执行向上取整到整分钟计费,因此完整作业测量能同时呈现计费时长与工作流复杂度。拆分 Linux 会重复两轮设置并传输一份已构建目录树,但能使覆盖率、静态门禁与构建后消费方不再相互进入关键路径,且无需重复构建;合并 Windows 则避免重复其耗时更长的设置。 +GitHub 会把每次大型运行器执行向上取整到整分钟计费,因此完整作业测量能同时呈现计费时长与工作流复杂度。拆分 Linux 会重复两轮设置,但消费方通道拥有唯一一份已构建目录树,且覆盖率、静态门禁与构建后消费方分别进入运行器分配;合并 Windows 则避免重复其耗时更长的设置。 性能目标是观测结果,而非取消截止时间或正确性要求。当映像、依赖、调度器或定价发生变化而需要重新测量时,仍可使用手动全规格和串行套件。 diff --git a/.agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.i18n.yaml b/.agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.i18n.yaml new file mode 100644 index 0000000000..663b074d59 --- /dev/null +++ b/.agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.i18n.yaml @@ -0,0 +1,6 @@ +# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each +# side as of the last confirmed-consistent state. Both languages carry equal authority; +# after editing either side, bring the other along and re-record with: +# pnpm run verify-translation-pairing --write .agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.md +2026-07-30-independent-ci-consumer-build.md: ea87d8051a30c282bdf57ddc3226072be8cb7f24 +2026-07-30-independent-ci-consumer-build.zh.md: 1b5faf73711bd522ddf0ae04f870e0401a481c0e diff --git a/.agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.md b/.agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.md new file mode 100644 index 0000000000..ea87d8051a --- /dev/null +++ b/.agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.md @@ -0,0 +1,35 @@ +# Agent Note: Independent CI consumer build + +Status: implemented + +English | [中文](2026-07-30-independent-ci-consumer-build.zh.md) + +## Problem + +The [larger-runner topology](2026-07-22-evidence-based-larger-hosted-runners.md) gave the static and built-consumer inventories separate jobs, but the static job owned their shared build. It uploaded the emitted tree only after every static gate completed, and the consumer job declared a job-level dependency before restoring that tree. Compiled-output snapshots and publication checks genuinely require a complete build; they do not require runtime-closure checks, documentation generation, module-graph verification, or Knip. + +That wider dependency made runner availability part of the required critical chain. In one failover run, static waited 8 minutes 1 second for a runner and ran for 1 minute 41 seconds; only then could consumers enter the same shared pool, where they waited another 10 minutes 34 seconds before running for 1 minute 58 seconds. Reusing the static build saved repository work but serialized two independent runner allocations. + +## Decision + +The three required Linux jobs enter runner allocation independently. Coverage remains source-only. Static owns source and documentation checks that do not consume emitted output. The consumer job owns the single Linux build together with documentation typechecking, compiled-output snapshots, publication checks, NodeNext checks, and built-bin smokes. + +The consumer's internal gate graph preserves the real dependency. Build and source-only Node compatibility start first; publint waits for build, built-package invariants validate that publication view, and every compiled-output consumer waits for that validation. Example and Web snapshots therefore continue to exercise current `lib/` output under plain Node, while no GitHub job waits for an unrelated job or transfers a built-tree artifact. + +Windows and serial reference aggregates retain their own build ownership. The change is confined to the required pull-request Linux topology; `all checks passed` still aggregates the same named jobs and fails for any unsuccessful dependency. + +## Alternatives considered + +**Keep publishing the static job's build.** This preserves one build but cannot express the actual step-level dependency: GitHub makes the consumer wait for the whole static job before it can request a runner. The saved build time is smaller than the repeated queue delay during failover saturation. + +**Build independently in both jobs.** Removing the job dependency while leaving build in static would restore parallel allocation, but every pull request would compile the same tree twice. Moving documentation typechecking and build ownership to the consumer preserves one build. + +**Add a dedicated build job.** A narrow producer would make the dependency name accurate, but it would add a fourth setup and runner-allocation stage before consumers. The consumer already owns every long-lived use of emitted output, so a separate producer has no second independent consumer. + +**Combine static and consumers only during failover.** One long job would avoid the second allocation, but conditional job inventories and result aggregation would create a second CI topology. Independent jobs preserve the same graph on hosted and failover pools. + +## Consequences + +Static and consumer queue delays overlap instead of accumulating. The consumer's active time includes the build, while the static job becomes shorter and artifact upload, download, compression, and extraction disappear. Total Linux build count remains one. + +A static failure no longer prevents the consumer inventory from producing its own evidence; the final verdict still fails. Build and documentation-typecheck failures appear under `node 24 / snapshots and artifacts` rather than `node 24 / static`, matching the job that owns their output dependency. diff --git a/.agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.zh.md b/.agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.zh.md new file mode 100644 index 0000000000..1b5faf7371 --- /dev/null +++ b/.agents/notes/implemented/process/2026-07-30-independent-ci-consumer-build.zh.md @@ -0,0 +1,35 @@ +# Agent Note: 消费方独立构建 + +Status: implemented + +[English](2026-07-30-independent-ci-consumer-build.md) | 中文 + +## 问题 + +[大型运行器拓扑](2026-07-22-evidence-based-larger-hosted-runners.md)将静态门禁清单和构建后消费方清单分配给不同作业,但二者共用的构建由静态作业负责。静态作业要等所有静态门禁完成后才上传生成的目录树,消费方作业则在恢复该目录树前声明了作业级依赖。基于编译输出的快照与发布校验确实需要完整构建,但不依赖运行时依赖闭包检查、文档生成、模块图验证或 Knip。 + +这项过宽的依赖使运行器可用性成为必需关键链的一环。一次故障切换运行中,静态作业等待运行器 8 分 1 秒,随后运行 1 分 41 秒;直到此时,消费方作业才能进入同一个共享池,它又等待 10 分 34 秒,随后运行 1 分 58 秒。复用静态作业的构建省去了部分仓库工作,却让两次原本相互独立的运行器分配串行发生。 + +## 决策 + +3 个必需 Linux 作业分别进入运行器分配。覆盖率仍只消费源码。静态作业负责无需消费生成输出的源码检查与文档检查。消费方作业负责唯一一次 Linux 构建,以及文档类型检查、基于编译输出的快照、发布校验、NodeNext 检查和 built-bin 冒烟测试。 + +消费方内部的门禁图保留实际依赖关系。构建和只消费源码的 Node 兼容性检查率先启动;publint 等待构建完成,已构建包不变式检查会验证该发布视图,所有编译输出消费方都等待这项验证完成。因此,示例和 Web 快照仍会在普通 Node 下验证当前 `lib/` 输出;同时,没有任何 GitHub 作业需要等待无关作业或传输已构建目录树产物。 + +Windows 与串行参考聚合流程仍各自负责自身构建。本变更仅涉及拉取请求的必需 Linux 拓扑;`all checks passed` 仍聚合同一批具名作业,任一依赖未成功时都会失败。 + +## 曾考虑的替代方案 + +**继续发布静态作业的构建。** 此方案只需构建一次,却无法表达实际的步骤级依赖:GitHub 会让消费方等到整个静态作业结束后才可请求运行器。故障切换池饱和时,再次排队的延迟超过了省下的构建时间。 + +**在两个作业中分别独立构建。** 在静态作业中保留构建、同时移除作业依赖,可以恢复并行分配,但每个拉取请求都会对同一目录树编译两次。将文档类型检查和构建职责移给消费方,则仍只需构建一次。 + +**新增专用构建作业。** 职责单一的生产方能让依赖名称与实际关系相符,但会在消费方之前新增第 4 个需要设置和分配运行器的阶段。所有需要持续使用生成输出的任务都已由消费方作业负责,因此单独增设生产方也没有第二个相互独立的消费方。 + +**仅在故障切换期间合并静态作业与消费方作业。** 单个长作业可以避免第二次分配,但带条件分支的作业清单与结果聚合会形成第二套 CI 拓扑。独立作业能让托管池与故障切换池使用同一作业图。 + +## 后果 + +静态作业与消费方作业的排队延迟会相互重叠,不再累加。消费方的活动耗时包含构建;静态作业则变短,产物上传、下载、压缩和解压步骤全部消失。Linux 构建总次数仍为 1 次。 + +静态作业失败不再阻止消费方清单生成自身证据;最终判定仍会失败。构建与文档类型检查失败会归入 `node 24 / snapshots and artifacts` 而非 `node 24 / static`,这一归类与输出依赖的实际归属一致。 diff --git a/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.i18n.yaml b/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.i18n.yaml index 14990e32c8..6b950f9fc8 100644 --- a/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.i18n.yaml +++ b/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write .agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.md -2026-07-24-web-gui-browser-e2e-lane.md: fb28b7013550a853b92e50810f5bc34f2c02d2e4 -2026-07-24-web-gui-browser-e2e-lane.zh.md: b9a7d050031c3d08269a3b971cd1a84f082efba7 +2026-07-24-web-gui-browser-e2e-lane.md: 898b8b5fe1b8d65b108b4afa95b782a1ce1e5c71 +2026-07-24-web-gui-browser-e2e-lane.zh.md: 966a9854aee8f3b63b2ae8f1362f91a40b9ba894 diff --git a/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.md b/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.md index fb28b70135..898b8b5fe1 100644 --- a/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.md +++ b/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.md @@ -46,7 +46,7 @@ The lane covers three behavior families. Live-turn scenarios pin ordinary tool e ### CI stance -The lane is a required compare-only gate for Linux pull requests under the [browser snapshot CI decision](2026-07-30-web-browser-snapshot-ci-gate.md). The static job publishes `apps/web/dist` with the package build artifacts; the `node 24 / snapshots and artifacts` consumer job installs the lockfile-selected Chromium, restores its OS-and-lockfile-keyed cache, and runs the lane with `DSH_SNAPSHOT=replay`. This is an intentional plane split: the host and specs use the [tsx source-launch contract](../architecture/2026-07-29-dsh-source-launch-tsx-esm.md), while the browser consumes `apps/web/dist` and package `lib/client.js` artifacts, so the gate depends on `built-package-invariants` for those client artifacts. The hosted and self-hosted default-branch Linux serial jobs run the same gate; the hosted job produces the browser cache consumed by pull requests, while the persistent self-hosted pool needs no hosted cache. CI never records or refreshes goldens. Scenarios remain POSIX-oriented and stay outside the Windows and macOS matrices. +The lane is a required compare-only gate for Linux pull requests under the [browser snapshot CI decision](2026-07-30-web-browser-snapshot-ci-gate.md). The `node 24 / snapshots and artifacts` consumer job owns the [single Linux build](../process/2026-07-30-independent-ci-consumer-build.md), installs the lockfile-selected Chromium, restores its OS-and-lockfile-keyed cache, and runs the lane with `DSH_SNAPSHOT=replay`. This is an intentional plane split: the host and specs use the [tsx source-launch contract](../architecture/2026-07-29-dsh-source-launch-tsx-esm.md), while the browser consumes `apps/web/dist` and package `lib/client.js` artifacts, so the gate depends on `built-package-invariants` for those client artifacts. The hosted and self-hosted default-branch Linux serial jobs run the same gate; the hosted job produces the browser cache consumed by pull requests, while the persistent self-hosted pool needs no hosted cache. CI never records or refreshes goldens. Scenarios remain POSIX-oriented and stay outside the Windows and macOS matrices. ## Prior art diff --git a/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.zh.md b/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.zh.md index b9a7d05003..966a9854ae 100644 --- a/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.zh.md +++ b/.agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.zh.md @@ -46,7 +46,7 @@ Web GUI 以一条真实组装链交付——chromium 页面 → client 插件 bu ### CI 立场 -根据[浏览器快照 CI 决策](2026-07-30-web-browser-snapshot-ci-gate.md),该车道是 Linux 拉取请求必需的只比较门禁。static 任务会把 `apps/web/dist` 与包构建产物一同发布;`node 24 / snapshots and artifacts` 消费方任务安装锁文件选定的 Chromium,恢复以操作系统和锁文件为键的缓存,并用 `DSH_SNAPSHOT=replay` 运行该车道。这是有意的平面切分:host 与 spec 使用 [tsx 源码启动契约](../architecture/2026-07-29-dsh-source-launch-tsx-esm.md),浏览器则消费 `apps/web/dist` 和包的 `lib/client.js` 产物,因此门禁依赖 `built-package-invariants` 提供这些客户端产物。托管和自托管的默认分支 Linux 串行任务运行同一门禁;托管任务生成供 PR 消费的浏览器缓存,持久化自托管池则不需要托管侧缓存。CI 从不录制或刷新预期输出。场景仍面向 POSIX,并继续置于 Windows 和 macOS 矩阵之外。 +根据[浏览器快照 CI 决策](2026-07-30-web-browser-snapshot-ci-gate.md),该车道是 Linux 拉取请求必需的只比较门禁。`node 24 / snapshots and artifacts` 消费方任务在[消费方独立构建](../process/2026-07-30-independent-ci-consumer-build.md)中负责唯一一次 Linux 构建,安装锁文件选定的 Chromium,恢复以操作系统和锁文件为键的缓存,并用 `DSH_SNAPSHOT=replay` 运行该车道。这是有意的平面切分:host 与 spec 使用 [tsx 源码启动契约](../architecture/2026-07-29-dsh-source-launch-tsx-esm.md),浏览器则消费 `apps/web/dist` 和包的 `lib/client.js` 产物,因此门禁依赖 `built-package-invariants` 提供这些客户端产物。托管和自托管的默认分支 Linux 串行任务运行同一门禁;托管任务生成供 PR 消费的浏览器缓存,持久化自托管池则不需要托管侧缓存。CI 从不录制或刷新预期输出。场景仍面向 POSIX,并继续置于 Windows 和 macOS 矩阵之外。 ## 业界先例 diff --git a/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.i18n.yaml b/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.i18n.yaml index d16d412559..2e8e53fdd7 100644 --- a/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.i18n.yaml +++ b/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.i18n.yaml @@ -2,5 +2,5 @@ # side as of the last confirmed-consistent state. Both languages carry equal authority; # after editing either side, bring the other along and re-record with: # pnpm run verify-translation-pairing --write .agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.md -2026-07-30-web-browser-snapshot-ci-gate.md: 3f87bb0f3d936bcee7ba7c3d84ae808c6ede1a97 -2026-07-30-web-browser-snapshot-ci-gate.zh.md: af563f0e2a1c20f7b53d371e97e41b7ffa52a1d1 +2026-07-30-web-browser-snapshot-ci-gate.md: 14402485034cd85ec5781477ce67481165d47e62 +2026-07-30-web-browser-snapshot-ci-gate.zh.md: f214c524253d2ad8a43e8543dc65ddfcfd7c065c diff --git a/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.md b/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.md index 3f87bb0f3d..1440248503 100644 --- a/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.md +++ b/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.md @@ -12,7 +12,7 @@ The [keyless web browser e2e lane](2026-07-24-web-gui-browser-e2e-lane.md) runs For Linux PRs, the `node 24 / snapshots and artifacts` job must run the full web browser replay/compare suite. `scripts/run-gates.ts` registers `test:web:built` as a `ci-consumers` gate and explicitly injects `DSH_SNAPSHOT=replay`; CI never runs in `record` or `refresh` mode, so when the committed goldens disagree with the currently assembled application, the tests fail directly instead of silently rewriting them on the runner and then passing. -The static CI job already builds all publishable artifacts; it puts `apps/web/dist` and the package `lib/` directories in the built-tree artifact, which the consumer job reuses without rebuilding the entire repository. On hosted runners, CI installs Chromium and its system dependencies at the Playwright version in the lockfile. On the persistent failover VM, the image owns the Linux system packages and CI installs only Chromium, avoiding per-run `apt` mutation. The hosted default-branch Linux serial job runs the suite and produces the operating-system-and-lockfile-keyed browser cache; pull requests restore it without paying compression and upload on the required path, with an operating-system prefix fallback across lockfile changes. The self-hosted standby runs the same comparison without hosted cache actions. +The consumer job owns the [single Linux build](../process/2026-07-30-independent-ci-consumer-build.md), so `apps/web/dist` and the package `lib/` directories remain in its workspace for the browser suite. On hosted runners, CI installs Chromium and its system dependencies at the Playwright version in the lockfile. On the persistent failover VM, the image owns the Linux system packages and CI installs only Chromium, avoiding per-run `apt` mutation. The hosted default-branch Linux serial job runs the suite and produces the operating-system-and-lockfile-keyed browser cache; pull requests restore it without paying compression and upload on the required path, with an operating-system prefix fallback across lockfile changes. The self-hosted standby runs the same comparison without hosted cache actions. Local `pnpm run test:web` continues to build first and then run the full browser suite; `test:web:built` is the entry point for existing build artifacts. Developers explicitly run `DSH_SNAPSHOT=refresh pnpm run test:web` only after confirming that user-visible output changed intentionally, review every expected-output diff, and then verify again in replay mode that no files are written. @@ -26,10 +26,10 @@ An observed self-hosted consumer run measured `web-snapshot` at 112.15 seconds a **Run CI in `refresh` mode and then check the working tree.** Rejected: checking after writing turns the assertion mechanism into a generator; if the working-tree check is wired incorrectly, it can turn a regression into a passing expected-output update. Replay compares the existing goldens directly and has a smaller failure surface. -**Create a standalone browser job and rebuild the entire repository.** Rejected: it would duplicate dependency installation and the publishable build. The existing Linux consumer job already consumes the same built-tree artifact and is part of the unified required verdict. +**Create a standalone browser job and rebuild the entire repository.** Rejected: it would duplicate dependency installation and the publishable build. The existing Linux consumer job already owns that build and is part of the unified required verdict. **Replace real Chromium with jsdom snapshots.** Rejected: jsdom does not cover the browser, HTTP/SSE carriage, or the composition of real client plugin bundles. It remains useful for fast lower-layer feedback, but cannot replace the assembled browser chain. ## Consequences -Before merge, every PR proves that the current web assembly matches all committed browser expected outputs, turning a missed refresh from an “unrelated change in a later PR” into a failure in the PR that introduced it. The cost is Chromium provisioning and one serial pass through the browser scenarios in the consumer job; built-artifact reuse and the browser cache avoid duplicate builds and downloads on reruns. The gate still makes no claim of cross-platform browser consistency, and if a Playwright/Chromium upgrade changes the ARIA format, the upgrade PR must explicitly refresh the expected outputs and review the churn. +Before merge, every PR proves that the current web assembly matches all committed browser expected outputs, turning a missed refresh from an “unrelated change in a later PR” into a failure in the PR that introduced it. The cost is Chromium provisioning and one serial pass through the browser scenarios in the consumer job; the consumer-owned build and browser cache avoid duplicate builds and downloads on reruns. The gate still makes no claim of cross-platform browser consistency, and if a Playwright/Chromium upgrade changes the ARIA format, the upgrade PR must explicitly refresh the expected outputs and review the churn. diff --git a/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.zh.md b/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.zh.md index af563f0e2a..f214c52425 100644 --- a/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.zh.md +++ b/.agents/notes/implemented/testing/2026-07-30-web-browser-snapshot-ci-gate.zh.md @@ -12,7 +12,7 @@ Status: implemented Linux PR 的 `node 24 / snapshots and artifacts` 必须运行完整 Web 浏览器 replay/compare。`scripts/run-gates.ts` 把 `test:web:built` 作为 `ci-consumers` 的一个 gate,并显式注入 `DSH_SNAPSHOT=replay`;CI 永不以 `record` 或 `refresh` 模式运行,因此提交的 golden 与当前组装应用不一致时测试直接失败,不会在 runner 内静默改写后通过。 -静态 CI job 已经构建全部发布产物;它把 `apps/web/dist` 和包的 `lib/` 目录放进 built-tree 产物,消费方 job 复用该产物而不重复全仓构建。在托管运行器上,CI 按锁文件中的 Playwright 版本安装 Chromium 及其系统依赖。在持久化故障切换 VM 上,镜像负责预装 Linux 系统软件包,CI 只安装 Chromium,避免每次运行都通过 `apt` 改动系统。托管的默认分支 Linux 串行 job 运行该套件,并生成以操作系统和锁文件为键的浏览器缓存;PR 恢复该缓存,使必需路径无需承担压缩和上传开销,并可在锁文件变化时按操作系统前缀回退。自托管热备运行相同的比较,但不执行托管缓存操作。 +消费方 job 在[消费方独立构建](../process/2026-07-30-independent-ci-consumer-build.md)中负责唯一一次 Linux 构建,因此 `apps/web/dist` 和包的 `lib/` 目录会保留在其工作区中,供浏览器套件使用。在托管运行器上,CI 按锁文件中的 Playwright 版本安装 Chromium 及其系统依赖。在持久化故障切换 VM 上,镜像负责预装 Linux 系统软件包,CI 只安装 Chromium,避免每次运行都通过 `apt` 改动系统。托管的默认分支 Linux 串行 job 运行该套件,并生成以操作系统和锁文件为键的浏览器缓存;PR 恢复该缓存,使必需路径无需承担压缩和上传开销,并可在锁文件变化时按操作系统前缀回退。自托管热备运行相同的比较,但不执行托管缓存操作。 本地 `pnpm run test:web` 仍先构建再运行浏览器全集;`test:web:built` 是已有构建产物的执行入口。开发者只在确认用户可见输出有意变化后显式运行 `DSH_SNAPSHOT=refresh pnpm run test:web`,评审每一处 expected diff,再以 replay 模式复验不再写文件。 @@ -26,10 +26,10 @@ Linux PR 的 `node 24 / snapshots and artifacts` 必须运行完整 Web 浏览 **让 CI 以 `refresh` 模式运行后检查工作树。** 已否决:写后比较把断言机制变成生成器,若工作树检查接线失效就会把回归更新成绿色;replay 直接比较已有 golden,失败面更小。 -**新建独立 browser job 并重新构建全仓。** 已否决:它会重复依赖安装和发布构建。现有 Linux consumer job 已消费同一 built-tree artifact,并已被统一的 required verdict 聚合。 +**新建独立 browser job 并重新构建全仓。** 已否决:它会重复依赖安装和发布构建。现有 Linux 消费方 job 已负责该构建,并已被统一的 required verdict 聚合。 **用 jsdom 快照代替真实 Chromium。** 已否决:jsdom 不覆盖浏览器、HTTP/SSE 承载及真实 client plugin bundle 组合;它保留为快速的下层反馈,不能替代 assembled browser chain。 ## 后果 -每个 PR 都在合并前证明当前 Web 组装与所有已提交的浏览器 expected 一致,漏刷从“后续 PR 的无关变化”变成引入 PR 自己的失败。成本是消费方 job 需要供给 Chromium,并串行运行一轮浏览器场景;built artifact 复用与浏览器缓存避免重跑时重复构建和下载。门禁仍不声称跨平台浏览器一致性,Playwright/Chromium 升级若改变 aria 格式,升级 PR 必须显式 refresh 并评审 churn。 +每个 PR 都在合并前证明当前 Web 组装与所有已提交的浏览器 expected 一致,漏刷从“后续 PR 的无关变化”变成引入 PR 自己的失败。成本是消费方 job 需要供给 Chromium,并串行运行一轮浏览器场景;消费方独立构建与浏览器缓存避免重跑时重复构建和下载。门禁仍不声称跨平台浏览器一致性,Playwright/Chromium 升级若改变 aria 格式,升级 PR 必须显式 refresh 并评审 churn。 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c9800a0d62..c6a9aa04f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,8 +28,8 @@ env: jobs: # Three enterprise jobs isolate coverage, static analysis, and the - # build-backed consumer tail. The static job publishes its exact build so - # consumers do not repeat the longest part of their critical path. + # build-backed consumer tail. The consumer job owns the only Linux build so + # all three jobs enter runner allocation independently. # # FAILOVER: each Linux enterprise job resolves its pool through the # DSH_CI_FAILOVER repository variable. Unset (normal), the expressions @@ -93,19 +93,6 @@ jobs: DSH_ARCHIVE_BASE_REF: ${{ github.event.pull_request.base.sha }} run: pnpm run check:ci:static - - name: Pack built tree - run: >- - tar -czf "$RUNNER_TEMP/node-24-built-tree.tar.gz" - apps/*/lib apps/web/dist packages/*/*/lib vendor/*/lib - - - uses: actions/upload-artifact@v7 - with: - name: node-24-built-tree - path: ${{ runner.temp }}/node-24-built-tree.tar.gz - if-no-files-found: error - retention-days: 1 - compression-level: 0 - node-24-coverage: if: github.event_name == 'pull_request' runs-on: >- @@ -171,7 +158,6 @@ jobs: run: pnpm run check:ci:coverage node-24-consumers: - needs: node-24 if: github.event_name == 'pull_request' runs-on: >- ${{ vars.DSH_CI_FAILOVER == 'selfhosted' @@ -191,14 +177,6 @@ jobs: with: persist-credentials: false - - uses: actions/download-artifact@v8 - with: - name: node-24-built-tree - path: ${{ runner.temp }} - - - name: Restore built tree - run: tar -xzf "$RUNNER_TEMP/node-24-built-tree.tar.gz" - - uses: pnpm/action-setup@v4 with: dest: ${{ runner.temp }}/setup-pnpm diff --git a/scripts/run-gates.spec.ts b/scripts/run-gates.spec.ts index 9a67e64929..a4ea97ce31 100644 --- a/scripts/run-gates.spec.ts +++ b/scripts/run-gates.spec.ts @@ -135,31 +135,43 @@ describe('Oxlint gate', () => { }) }) -describe('Node 24 consumer graph', () => { - it('owns the eight-command pool and orders restored-artifact consumers', () => { +describe('Node 24 lane ownership', () => { + it('keeps the static lane source-only', () => { + const subject = withPnpmEntrypoint(() => gatesForMode('ci-static')) + + expect(subject.map(item => item.id)).not.toContain('build') + expect(subject.map(item => item.id)).not.toContain('doc-typecheck') + }) + + it('owns the build and orders its artifact consumers', () => { const subject = withPnpmEntrypoint(() => gatesForMode('ci-consumers')) expect(defaultConcurrency('ci-consumers', subject.length, 4)).toEqual({ - workers: 8, + workers: 10, source: 'ci-consumers gate count', }) expect(subject.map(item => item.id)).toEqual([ - 'lint-and-duplication', + 'build', 'node-compat', + 'publint', + 'built-package-invariants', + 'lint-and-duplication', 'snapshot', 'web-snapshot', - 'publint', + 'doc-typecheck', 'node-next-types', - 'built-package-invariants', 'built-bin-smoke', ]) - expect(subject.find(item => item.id === 'publint')?.needs).toBeUndefined() + expect(subject.find(item => item.id === 'publint')?.needs).toEqual(['build']) expect(subject.find(item => item.id === 'built-package-invariants')?.needs).toEqual(['publint']) expect(subject.find(item => item.id === 'lint-and-duplication')?.needs).toEqual(['built-package-invariants']) - for (const id of ['snapshot', 'web-snapshot', 'node-next-types', 'built-bin-smoke']) { + for (const id of ['snapshot', 'web-snapshot', 'doc-typecheck', 'node-next-types', 'built-bin-smoke']) { expect(subject.find(item => item.id === id)?.needs).toEqual(['built-package-invariants']) } expect(subject.find(item => item.id === 'snapshot')?.env).toEqual({ DSH_EXAMPLE_MODE: 'lib' }) + expect(subject.find(item => item.id === 'doc-typecheck')?.env).toEqual({ + DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1', + }) expect(subject.find(item => item.id === 'web-snapshot')).toMatchObject({ displayCommand: 'DSH_SNAPSHOT=replay pnpm run test:web:built', env: { DSH_SNAPSHOT: 'replay' }, diff --git a/scripts/run-gates.ts b/scripts/run-gates.ts index 167d226ff8..46bc72c833 100644 --- a/scripts/run-gates.ts +++ b/scripts/run-gates.ts @@ -195,7 +195,7 @@ export function gatesForMode(selected: Mode): Gate[] { case 'ci-linux-primary': return [...ciPrimaryGates(), webSnapshotGate(['built-package-invariants'])] case 'ci-static': - return ciStaticGates() + return ciStaticGates({ ownsBuild: false }) case 'ci-lint': return [ lintGate(), @@ -295,16 +295,21 @@ function nodeCompatSmokeGates(): Gate[] { ] } -function ciStaticGates(): Gate[] { +function ciStaticGates(options: { ownsBuild: boolean }): Gate[] { return [ pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }), pnpmScript('constraints', 'constraints'), pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }), pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }), - pnpmScript('build', 'build'), + ...options.ownsBuild ? [pnpmScript('build', 'build')] : [], ...docSyncLeafGates({ - docTypecheckNeeds: ['build'], - docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' }, + includeDocTypecheck: options.ownsBuild, + ...options.ownsBuild + ? { + docTypecheckNeeds: ['build'], + docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' }, + } + : {}, docsBuildScript: 'docs:build:mpa', }), pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }), @@ -326,23 +331,28 @@ function ciArtifactGates(): Gate[] { } function ciConsumerGates(): Gate[] { - const publicArtifacts = ['publint'] - const restoredBuild = ['built-package-invariants'] + const builtTree = ['build'] + const validatedBuild = ['built-package-invariants'] return [ + pnpmScript('build', 'build'), + pnpmScript('node-compat', 'check:node-compat', { label: 'Node compatibility' }), + pnpmScript('publint', 'publint', { needs: builtTree }), + builtPackageInvariantsGate(['publint']), pnpmScript('lint-and-duplication', 'check:ci:lint', { label: 'lint and duplication', - needs: restoredBuild, + needs: validatedBuild, + }), + snapshotGate(validatedBuild), + webSnapshotGate(validatedBuild), + pnpmScript('doc-typecheck', 'doc-typecheck', { + needs: validatedBuild, + env: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' }, }), - pnpmScript('node-compat', 'check:node-compat', { label: 'Node compatibility' }), - snapshotGate(restoredBuild), - webSnapshotGate(restoredBuild), - pnpmScript('publint', 'publint'), pnpmScript('node-next-types', 'verify-node-next-types', { label: 'node-next types', - needs: restoredBuild, + needs: validatedBuild, }), - builtPackageInvariantsGate(publicArtifacts), - builtBinSmokeGate(restoredBuild), + builtBinSmokeGate(validatedBuild), ] } @@ -377,7 +387,7 @@ function ciWindowsCompleteGates(): Gate[] { function ciWindowsObservationalGates(): Gate[] { return [ - ...ciStaticGates(), + ...ciStaticGates({ ownsBuild: true }), // Linux owns required lint, coverage, and snapshots; Windows omits those duplicates. pnpmScript('duplication', 'duplication'), pnpmScript('publint', 'publint', { needs: ['build'] }), @@ -410,7 +420,7 @@ function coverageGate(): Gate { // Example and package snapshots boot their bins in `lib` mode (built artifacts under plain Node, // plugins via real exports); repository-script snapshots execute their real source entry path. -// Build-owning modes wait on `build`; a restored-artifact mode passes its validation dependency. +// Callers wait either on `build` or on a validation gate that transitively owns that build. function snapshotGate(needs: string[] = ['build']): Gate { return pnpmScript('snapshot', 'test:snapshot', { env: { DSH_EXAMPLE_MODE: 'lib' }, @@ -458,6 +468,7 @@ function hygieneLeafGates(options: { artifactNeeds?: string[] } = {}): Gate[] { } function docSyncLeafGates(options: { + includeDocTypecheck?: boolean docTypecheckNeeds?: string[] docTypecheckEnv?: Record docsBuildScript?: 'docs:build' | 'docs:build:mpa' @@ -466,7 +477,9 @@ function docSyncLeafGates(options: { if (options.docTypecheckNeeds !== undefined) docTypecheckOptions.needs = options.docTypecheckNeeds if (options.docTypecheckEnv !== undefined) docTypecheckOptions.env = options.docTypecheckEnv return [ - pnpmScript('doc-typecheck', 'doc-typecheck', docTypecheckOptions), + ...options.includeDocTypecheck === false + ? [] + : [pnpmScript('doc-typecheck', 'doc-typecheck', docTypecheckOptions)], pnpmScript('cordis-catalog', 'verify-cordis-catalog', { label: 'cordis catalog' }), pnpmScript('export-jsdoc', 'verify-export-jsdoc', { label: 'export jsdoc' }), pnpmScript('tool-catalog', 'verify-tool-catalog', { label: 'tool catalog' }), From 49678f38aedf3314182c6a1ddd4321ecd5e759e0 Mon Sep 17 00:00:00 2001 From: Tianyi Cui <53024+tianyicui@users.noreply.github.com> Date: Thu, 30 Jul 2026 18:59:06 +0800 Subject: [PATCH 15/17] test(typert): allow catalog analysis under coverage --- .../typert/generator/tests/cordis-catalog-contract.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/typert/generator/tests/cordis-catalog-contract.spec.ts b/packages/typert/generator/tests/cordis-catalog-contract.spec.ts index 4092ac7e63..1bfe0f5e46 100644 --- a/packages/typert/generator/tests/cordis-catalog-contract.spec.ts +++ b/packages/typert/generator/tests/cordis-catalog-contract.spec.ts @@ -125,7 +125,7 @@ afterEach(() => { while (roots.length) rmSync(roots.pop()!, { recursive: true, force: true }) }) -describe('gen-cordis-catalog collectEvents', () => { +describe('gen-cordis-catalog collectEvents', { timeout: 30_000 }, () => { it('extracts a well-formed event with its @mode and JSDoc', () => { const events = collectEvents(make( ' /**\n * A thing happened.\n * @param id - which thing.\n * @mode emit\n */\n \'fix/happened\'(id: string): void', @@ -239,7 +239,7 @@ describe('gen-cordis-catalog collectEvents', () => { }) }) -describe('gen-cordis-catalog collectServices', () => { +describe('gen-cordis-catalog collectServices', { timeout: 30_000 }, () => { const WELL_FORMED = `/** Fixture service. */ export class FixService { /** From 7dff9c3ad093c398f7f918abe866f51579dd79d3 Mon Sep 17 00:00:00 2001 From: Tianyi Cui <53024+tianyicui@users.noreply.github.com> Date: Thu, 30 Jul 2026 21:38:04 +0800 Subject: [PATCH 16/17] ci: temporarily disable serial reference runners --- .github/workflows/ci.yml | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ffe59ead9..04d4a54131 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,9 @@ env: jobs: + # TEMPORARY: the four serial reference jobs remain defined but cannot run. + # Restore their master-push conditions to re-enable them. + # Three enterprise jobs isolate coverage, static analysis, and the # build-backed consumer tail. The static job publishes its exact build so # consumers do not repeat the longest part of their critical path. @@ -39,8 +42,8 @@ jobs: # three onto the in-house # vm-backup pool and re-running the failed jobs is the entire switch — # see .agents/notes/implemented/process/2026-07-26-ci-failover-runbook.md. The - # in-house pool's readiness is re-proven on every master push by the - # serial-linux-selfhosted standby lane below. + # Normally, the in-house pool's readiness is re-proven on every master push + # by the serial-linux-selfhosted standby lane below. node-24: if: github.event_name == 'pull_request' runs-on: >- @@ -225,8 +228,8 @@ jobs: restore-keys: | ${{ runner.os }}-node-${{ env.PRIMARY_NODE_VERSION }}-pnpm- - # Pull requests restore the cache produced by serial-linux on master; - # they do not pay compression and upload on the required path. + # Pull requests restore the cache normally produced by serial-linux on + # master; they do not pay compression and upload on the required path. - uses: actions/cache/restore@v4 if: vars.DSH_CI_FAILOVER != 'selfhosted' || github.event.pull_request.user.login == 'dependabot[bot]' with: @@ -321,8 +324,9 @@ jobs: # The required pull-request Windows signal: the two blocking win32 surfaces # (workspace build, production site) execute with real, checksum-verified # Windows Node under Wine on standard hosted Linux. The master - # serial-windows job below keeps the complete native-kernel inventory — - # including the observational portability gates this lane does not run — + # serial-windows job below normally keeps the complete native-kernel + # inventory — including the observational portability gates this lane does + # not run — # on real windows-2025. This job only provisions runner state (caches, # apt); scripts/wine-windows-gates.sh owns the gate logic and is the same # script the optional local gate `pnpm run check:windows-wine` runs. @@ -427,12 +431,12 @@ jobs: cp /var/cache/apt/archives/*.deb "$HOME/wine-debs/" du -sh "$HOME/wine-debs" - # Master pushes run only the serial reference jobs below. - # Each host executes the complete, unsharded primary Node aggregate with one - # gate worker, giving reviewers a simple cross-platform oracle for completeness - # and timing. + # The disabled definitions below normally run on master pushes. When + # enabled, each host executes the complete, unsharded primary Node aggregate + # with one gate worker, giving reviewers a simple cross-platform oracle for + # completeness and timing. serial-linux: - if: github.event_name == 'push' && github.ref == 'refs/heads/master' + if: false name: serial / linux runs-on: ubuntu-latest steps: @@ -511,7 +515,7 @@ jobs: # tool caches make them redundant (and saving here would poison the hosted # cache namespace with self-hosted paths). serial-linux-selfhosted: - if: github.event_name == 'push' && github.ref == 'refs/heads/master' + if: false name: serial / linux (self-hosted standby) runs-on: [self-hosted, linux, x64, vm-backup] steps: @@ -557,7 +561,7 @@ jobs: run: pnpm run check:ci:linux-primary serial-macos: - if: github.event_name == 'push' && github.ref == 'refs/heads/master' + if: false name: serial / macos runs-on: macos-latest steps: @@ -584,7 +588,7 @@ jobs: run: pnpm run check:ci serial-windows: - if: github.event_name == 'push' && github.ref == 'refs/heads/master' + if: false name: serial / windows runs-on: windows-2025 steps: From 85d721458a5ffc847c152dc1dfb9c542a2d26e25 Mon Sep 17 00:00:00 2001 From: Tianyi Cui <53024+tianyicui@users.noreply.github.com> Date: Thu, 30 Jul 2026 21:45:43 +0800 Subject: [PATCH 17/17] ci: keep self-hosted serial standby enabled --- .github/workflows/ci.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 04d4a54131..7cf55653f2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,8 +27,8 @@ env: jobs: - # TEMPORARY: the four serial reference jobs remain defined but cannot run. - # Restore their master-push conditions to re-enable them. + # FIXME: Re-enable the three hosted serial reference jobs before cutting a release. + # The self-hosted standby remains active on every master push. # Three enterprise jobs isolate coverage, static analysis, and the # build-backed consumer tail. The static job publishes its exact build so @@ -42,8 +42,8 @@ jobs: # three onto the in-house # vm-backup pool and re-running the failed jobs is the entire switch — # see .agents/notes/implemented/process/2026-07-26-ci-failover-runbook.md. The - # Normally, the in-house pool's readiness is re-proven on every master push - # by the serial-linux-selfhosted standby lane below. + # in-house pool's readiness is re-proven on every master push by the + # serial-linux-selfhosted standby lane below. node-24: if: github.event_name == 'pull_request' runs-on: >- @@ -431,10 +431,10 @@ jobs: cp /var/cache/apt/archives/*.deb "$HOME/wine-debs/" du -sh "$HOME/wine-debs" - # The disabled definitions below normally run on master pushes. When - # enabled, each host executes the complete, unsharded primary Node aggregate - # with one gate worker, giving reviewers a simple cross-platform oracle for - # completeness and timing. + # The hosted reference jobs below are temporarily disabled; the self-hosted + # standby remains active. Each enabled host executes the complete, unsharded + # primary Node aggregate with one gate worker, giving reviewers a simple + # cross-platform oracle for completeness and timing. serial-linux: if: false name: serial / linux @@ -515,7 +515,7 @@ jobs: # tool caches make them redundant (and saving here would poison the hosted # cache namespace with self-hosted paths). serial-linux-selfhosted: - if: false + if: github.event_name == 'push' && github.ref == 'refs/heads/master' name: serial / linux (self-hosted standby) runs-on: [self-hosted, linux, x64, vm-backup] steps: