深入理解 DeepSeek Harness · 第 13 篇
跨进程接缝:Typert / Remote / Gateway
前情回顾
第 12 篇讲完同一进程内如何把插件树拼成五个产品形态。本篇处理跨进程边界:Host 上的业务服务如何被浏览器调用,而调用方不需要把 Host 的实现类 import 进浏览器。这也是批准大纲里第 14 篇启动链路的前置知识——plugin manager 自己就是一个 Remote 服务。
本章要回答的问题
@Remote 与 @RemoteScope 各自把什么暴露给 Client?Host 对象(比如 Agent)怎么跨线?为什么要生成 Host 与 Client 两份契约,而不是两边共用一份类型?一次 Remote 调用走哪条路由?plugin manager 为什么必须先在本篇登场?
你会学到什么 / 先验知识
你会看懂两大声明合并映射、严格生成的产物清单、Host/Client 分离的程序边界、/api 路由与访问控制,以及 TypertRemoteService 如何把管理服务暴露给 Web 与模型工具。先验是第 2 篇的声明合并与第 12 篇的 profile 装配。
核心概念:一份业务方法,两侧契约
graph LR
H[Host 业务服务<br/>@Remote / @RemoteScope] --> G[typert-generator<br/>Host ts.Program 严格分析]
G --> A[Host 产物<br/>typert.host.js/.d.ts]
G --> B[Client 产物<br/>typert.remote-client.js/.d.ts]
A --> HR[Host Gateway<br/>ctx.typertGateway<br/>校验/解析/派发]
B --> CR[Client api-remotes<br/>ctx.remote.$mount]
CR -->|connection.rpc.call /api| HR
HR --> S[Host Cordis 服务]
L[插件管理<br/>extends TypertRemoteService] --> HR
[文档] docs/api-gateway.md:5 划清范围:它描述业务服务如何声明一元与流式 Remote 方法、构建如何生成 Host 与 Client 契约、调用如何复用 Connection RPC 与 /api 路由;session 事件、增量数据与其它流式协议不用 Remote 方法描述符——这正是第 7 篇的 live 帧走另一条路的原因。
源码走读:六处证据
证据一:标注即暴露。 [文档] docs/api-gateway.md:9-17:
Business services use `@Remote` or `@RemoteScope` to select the methods exposed to the Client. Unmarked methods do not enter the generated Client types or runtime contributions and cannot be called through `ctx.remote`. `@Remote` denotes calling a Cordis service registered on the root Host Context. Complex Host objects cannot cross the wire directly; the business package must declare their association with a wire identity through `TypertLookupMap` and register a default resolution provider with `ctx.typert.lookups` at runtime. For example, an `Agent` parameter named `agent` in the Host signature produces an `agentId` wire field, and the Gateway resolves that id to a Host object before invoking the business method. `@RemoteScope(key)` first resolves an identity to a scoped Context through `ctx.typert.contexts`, then obtains the service from that Context and invokes the method. It applies when the method itself depends on scoped composition and does not need to receive objects such as `Agent` explicitly.
@Remote 打在根 Context 的服务方法上;@RemoteScope(key) 先把身份解析成 scoped Context 再取服务。没标注的方法不进 Client 类型、不进运行时贡献、不能通过 ctx.remote 调——暴露面由标注决定,不是整类导出。
证据二:两张空映射承载跨线身份。 [文档] docs/subsystems/typert.md:7-30:
Business-object packages extend two empty maps through declaration merging. A lookup associates one Host object type with its wire identity; a Context declaration associates one scoped Context kind with its wire identity. Generated descriptors name these keys, while runtime providers supply the live resolution behavior.
/** Merge-extensible Host object lookup declarations. */ interface TypertLookupMap {}
/** Merge-extensible scoped Context declarations. */ interface TypertContextMap {}
第 2 篇的声明合并在这里派上用场:业务包扩展两张空映射,声明「Host 对象类型 ↔ wire(线上)身份」与「scoped Context 种类 ↔ wire 身份」。生成器只写描述符名,真正的解析由运行时 provider 提供。:21 补充一条关键约束:注册器在 resolver 卸载后保留 lookup 的 wire 声明,于是 SRC 发现仍把参数分类为 lookup 并失败于 unavailable,而不是把 wire 值误当普通业务对象。SRC(source-running fallback,源码启动回退)指 Host 用 node --import tsx/esm 从源码启动时,不执行 Typert 编译器插件,只靠装饰器在 Service 原型上留下的弱描述符完成派发(docs/api-gateway.md:136-142);它不读 TypeScript 类型、不生成 Zod schema,Client 也拒绝挂载缺严格 codec 的 SRC 描述符(:142)。
证据三:Host 与 Client 永不进同一个 ts.Program。 [文档] docs/api-gateway.md:84-98:
| Shared | `@deepseek-ai/dsh-typert-protocol` | Declares decorators, Gateway bindings, merge-extensible protocol maps, invocation descriptors, and provider types; starts no TypeScript analysis and registers no Cordis services | | Build | `@deepseek-ai/dsh-typert-generator` | Strictly analyzes Remote signatures, the type graph, lookups, Contexts, and source locations from the Host `ts.Program`, then generates Host and Host-for-Client artifacts | | Host | `@deepseek-ai/dsh-api-gateway` | Provides `ctx.typertGateway`, claims Remote endpoints, validates request values, resolves objects or Contexts, and invokes live Cordis services | | Client | `@deepseek-ai/dsh-api-gateway/client` | Provides `ctx.remote` and `remote.<namespace>` child Services, mounts generated descriptors as concrete methods, and initiates and cancels calls through the Connection | | Both | `@deepseek-ai/dsh-client-connection` | Provides the RPC carrier, request correlation, trust boundary, cancellation, response envelope, and the `/api` HTTP bridge |
The API Gateway package owns the Host dispatcher and Client Remote endpoint as peer entries, but the two builds never enter the same `ts.Program`. The Host entry does not import the Client Cordis `Context` merge, and the Client entry does not import the Host Gateway service.
这正是第 1 篇 required 审计、第 2 篇 callback 身份键(精确身份)的同一类约束在编译层的体现:两侧都对 Cordis Context 在相同键下声明合并了不同服务,一个 ts.Program 同时看到两份合并会冲突(docs/development.md:71)。于是生成器从 Host aggregate 读类型、写 Client 声明,Client 消费产物而非 Host 源码。
证据四:严格生成的产物。 [文档] docs/api-gateway.md:100-118:
The root build first runs source-only Host analysis through `scripts/generate-typert.ts`, then compiles and bundles the Host and Client faces and builds Web. The generator uses the Host aggregate as its only `ts.Program` seed and writes Remote declarations before package compilation.
每个贡献包写到自己的 lib/(不是源码目录):typert.host.js(Host Loader 的反射与描述符)、typert.host.d.ts、typert.remote-client.js(可挂载的 TypertRemoteContribution)、typert.remote-client.d.ts(TypertRemoteNamespaceMap / TypertRemoteScopeMap 的声明合并)、以及 :116 的 .d.ts.map——编辑器能把 ctx.remote.goals.create 映射回 Host 源码里带 @Remote 的方法,而不是停在生成的声明上。
:122 列出严格约束:Remote 必须是 public、非 static、有具体实现的实例方法;不能泛型;参数必须必填、命名简单标识符,不能解构、默认值、rest 或可选。复杂对象必须有唯一 TypertLookupMap 声明;静态合并与运行时 provider 缺一边,构建失败或第一次调用失败。
证据五:plugin manager 自己是 Remote 服务。 [源码] packages/boot/plugin-manager/src/index.ts:325-327:
/** Manage profile files and apply their declared reload lifecycle. */ export class PluginManager extends TypertRemoteService { static inject = ['loader', 'profileContext']
extends TypertRemoteService 把管理服务绑到 Cordis 服务键与默认 Remote namespace(docs/api-gateway.md:17 说明这是首选形式;已有基类的服务可用 bindTypertRemote(this, key))。于是 Web 插件页的模型工具与浏览器 UI 共用同一实现——这就是第 14 篇必须先有本篇的原因。
证据六:运行时路由与访问控制。 [文档] docs/api-gateway.md:126-128:
Remote calls use the Connection's `/api` route. The Client Remote calls `connection.rpc.call('/api', '<namespace>/<method>', { args }, signal)`; the HTTP carrier maps this to `POST /api/<namespace>/<method>`, with a payload containing only a named `args` object. The Connection performs the unified trust check for `/api` before the HTTP bridge, then dispatches inside the shared FetchHandler. The Typert Gateway claims only two-segment endpoints that have a strict descriptor or active SRC marker; it projects binary Remote fields into JSON-compatible metadata and result-relative byte attachments, which Connection frames as multipart responses. Feature-owned exact Fetch routes handle responses outside the RPC envelope, and other requests return 404. Connection owns transport, RPC ids, response envelopes, and request cancellation, while Gateway owns the Remote data protocol and business dispatch.
调用形如 <namespace>/<method>,payload 只有 args;Connection 管传输、RPC id、响应信封与取消,Gateway 管数据协议与业务派发;非 owner Peer 的调用还要过 dsh-api-remote-access 的角色策略(:93)。
设计取舍
① 为什么 Client 用具体函数而不是 Proxy? docs/api-gateway.md:62 明确说 Client 用普通对象上的具体函数,不是 JavaScript Proxy。ctx.remote/<namespace> 是 traced Cordis 子服务,命名空间在最后一个方法撤回后卸载。代价是命名空间必须由生成器显式生成,不能运行时随意拼方法名——收益是可追踪、可调试、可静态类型检查。
② 为什么 Host/Client 不进同一个 ts.Program? 因为两侧对 Cordis Context 在相同键下声明合并了不同服务,一个程序看到两份合并会冲突(docs/development.md:71)——不是「键不同」,键不同根本不会碰面。代价是必须维护生成器与产物一致性,但收益是浏览器永远不 import Host 服务实现。
③ 为什么 lookup 声明在 resolver 卸载后保留? 因为 wire 参数在 SRC 发现阶段就要被识别为 lookup;若声明随 provider 一起消失,后续发现会把 agentId 当普通业务对象接受,制造类型漏洞。代价是注册表要保留「僵尸声明」,收益是失败方向始终 fail-closed。
常见误解
「Remote 只是给 JSON-RPC 换了个名字。」 不是。它包含声明合并、严格类型分析、双份产物、资源访问控制,以及二进制 Remote 字段的 multipart 投影(docs/api-gateway.md:128);流式方法另走 /api/remote.mux(docs/api-gateway.md:60,第 7 篇的 live 帧),不属于 Remote 方法描述符。Connection 只管传输。
「给类加 @Remote 就能调所有方法。」 不对。只有标注的方法生成 Client 类型与贡献;生成器还要求 public、非 static、非泛型、参数必填且命名简单。
「Host 与 Client 共用一份类型程序。」 不共用。两侧从不进同一个 ts.Program(docs/api-gateway.md:98)。
小结
跨进程接缝把 Host 业务方法按 @Remote / @RemoteScope 暴露给 Client(docs/api-gateway.md:9-13),Host 对象经 TypertLookupMap / TypertContextMap 两张声明合并映射转成 wire 身份并在运行时解析(docs/subsystems/typert.md:9-30);生成器从 Host ts.Program 严格分析、产出 Host 与 Client 两套工件;两侧永不进同一 ts.Program,因为相同键下的服务声明合并会冲突(docs/api-gateway.md:84-118、docs/development.md:71);调用走 Connection 的 /api 路由,Gateway 负责描述符、资源访问与业务派发(:126-128)。PluginManager 以 extends TypertRemoteService 暴露管理服务(packages/boot/plugin-manager/src/index.ts:325-327),所以它是第 14 篇的前置。
代价落在三处:命名空间必须生成、两侧产物要保持一致、lookup 声明卸载后仍占注册表。第 14 篇回到启动链路,看 dsh web 如何把这棵树真正跑起来。
思考题
- 在
docs/subsystems/typert.md:21找到「lookup 声明在 resolver 卸载后保留」。如果声明随 provider 一起消失,SRC 发现会把agentId当成什么?为什么这是类型漏洞而不是小便利? - 在
docs/api-gateway.md:98找到「the two builds never enter the same ts.Program」。结合第 2 篇的声明合并机制与docs/development.md:71,说明为什么是「同键异服务」而不是「键不同」导致冲突,强行合并会发生什么;生成器因此必须承担什么额外工作? - 在
packages/boot/plugin-manager/src/index.ts:325-327找到extends TypertRemoteService。如果 plugin manager 不是 Remote 服务,Web 插件页与模型工具会分别需要什么?为什么这会影响第 14 篇的启动链路?