DSH / Atlas
2026-11-26implementedarchitecture

Environment runners select the handle per run

环境运行器在每次运行时选择句柄

The [original environment note](2026-11-04-environment.md) bound one environment handle to each definition. Tools that choose the environment per call, such as `read(filename, host)` or a Session-selected host, then had to keep one runner per handle and look it up before every call. The handle took no part in what a definition sends or caches: the definition hash covers only `[source, methods]`, and the registry keys

English

Problem

The original environment note bound one environment handle to each definition. Tools that choose the environment per call, such as read(filename, host) or a Session-selected host, then had to keep one runner per handle and look it up before every call. The handle took no part in what a definition sends or caches: the definition hash covers only [source, methods], and the registry keys registrations and connections by the handle each call resolves.

Decision

ctx.environments.define(fn, { result, handlers }) fixes the function source, its result validation, its origin handlers and their names. The returned EnvironmentRunner takes the environment handle as its first parameter, followed by the arguments and { signal, confirm? }. Each run resolves the registration that holds that handle when the run starts, so one definition made when a plugin loads serves every environment.

Handlers stay part of the definition. A caller whose handlers need host objects of one call still defines per call. This note partially supersedes the original note's decision that a definition binds its handle; the rest of that note remains active and cross-linked.

Alternatives considered

  • Keep the handle in the definition and cache runners per handle in each tool. Rejected: every tool that selects its environment per call repeats the same cache, although the registry already keys connections by handle and the definition cache by hash.
  • Accept both forms. Rejected: two ways to name an environment add a public surface without a consumer that needs the bound form; a caller with a fixed environment passes the same handle on every call.

Consequences

  • Tools with per-call environments keep one runner per function instead of one per handle.
  • The connection definition cache and its hashes are unchanged: equal definitions run in different environments reuse one hash, each connection receiving the definition once.
  • Every call site names its handle, including tools that only use one environment.

中文

问题

最初的执行环境记录将一个环境句柄绑定到每个定义。按调用选择环境的工具(例如 read(filename, host) 或由 Session 选择的 host)因此必须为每个句柄保留一个运行器,并在每次调用前查找它。句柄不影响定义发送或缓存的内容:定义哈希只涵盖 [source, methods],注册表也按每次调用解析的句柄来索引注册项和连接。

决策

ctx.environments.define(fn, { result, handlers }) 固定函数源码、其结果校验、发起端处理函数及其名称。返回的 EnvironmentRunner 以环境句柄作为第一个参数,其后是参数和 { signal, confirm? }。每次运行在开始时解析持有该句柄的注册项,因此插件加载时创建的一个定义可服务所有环境。

处理函数仍属于定义。如果调用方的处理函数需要某次调用的 host 对象,该调用方仍为每次调用分别定义。本记录部分取代原记录中定义绑定其句柄的决策;原记录的其余部分仍然有效,并与本记录互相链接。

考虑过的替代方案

  • 将句柄保留在定义中,并由每个工具按句柄缓存运行器。 否决:每个按调用选择环境的工具都要重复同一缓存,而注册表已按句柄索引连接,并按哈希索引定义缓存。
  • 同时接受两种形式。 否决:两种指定环境的方式会增加公开接口,却没有需要绑定形式的消费方;环境固定的调用方在每次调用时传入同一句柄即可。

影响

  • 按调用选择环境的工具为每个函数保留一个运行器,而不是为每个句柄保留一个。
  • 连接的定义缓存及其哈希不变:在不同环境中运行的相同定义复用同一哈希,每个连接只接收一次该定义。
  • 每个调用点都指定其句柄,包括只使用一个环境的工具。