workflow-actor-model.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * actor 模型面的 driver 侧解析与落库。见 dynamic-workflow/docs/execution-engine.md 的
  3. * 「Subagent model (resolved model pin)」。
  4. *
  5. * 2026-09-11 起 persona 没有模型档位(docs/dynamic-workflow/authoring.md),所以
  6. * 这里只剩两层:
  7. * 1. `workflowActorModelPolicy` —— journal pin → AgentRuntimeConfig 的模型面切片(纯函数);
  8. * 2. `journalActorResolvedModel` —— 实际用上的模型 → dwf_actor 记录(读改写,不整条覆盖)。
  9. *
  10. * 为什么独立成文件而不并入 dynamic-workflow-run-service.test.ts:那份用例在运行时 import
  11. * `@zcode/core`(真实 AgentRuntime),本仓当前环境下会被 `@zcode/zcode-cua` 卡住。这两层都
  12. * 只依赖类型,所以能单独跑——而它们恰是这个特性里唯一有分支的逻辑。
  13. */
  14. import { describe, expect, it } from "vitest";
  15. import type { ModelSelection } from "@zcode/contracts";
  16. import { InMemoryJournalStore } from "@zcode/dynamic-workflow";
  17. import {
  18. WorkflowActorModelUnboundError,
  19. WorkflowActorPinnedModelError,
  20. resolveActorPersonaModel,
  21. workflowActorModelPolicy,
  22. } from "../src/app/workflow-actor-model.js";
  23. import { workflowActorToolPolicy } from "../src/app/workflow-actor-tools.js";
  24. import {
  25. journalActorResolvedModel,
  26. pinnedActorModel,
  27. } from "../src/app/dynamic-workflow-run-launch.js";
  28. function selection(providerId: string, modelId: string): ModelSelection {
  29. return { providerId, modelId };
  30. }
  31. const PARENT = selection("anthropic", "sonnet");
  32. const OTHER = selection("anthropic", "haiku");
  33. /** run 级选择带档位:`subagent_model` 的 `$level` 是选择的一部分,绝不能在策略里掉。 */
  34. const RUN_CHOICE: ModelSelection = {
  35. providerId: "zhipu",
  36. modelId: "glm-5.3-flash",
  37. options: { reasoningLevel: "high" },
  38. };
  39. describe("workflowActorModelPolicy", () => {
  40. it("overrides nothing without a pin: the child inherits the parent session's current model", () => {
  41. // 空切片即「不覆盖」:回落由 child runtime 的基线完成(父会话当前模型),这里不需要任何
  42. // 分支——persona 里已经没有会改变模型的成员。
  43. expect(workflowActorModelPolicy({ parentSelection: PARENT }).configOverrides).toEqual({});
  44. });
  45. it("works without a parent selection at all (parent session not yet bound to a model)", () => {
  46. expect(workflowActorModelPolicy({}).configOverrides).toEqual({});
  47. });
  48. it("shares no config keys with the tool policy", () => {
  49. // create-app 把两个策略展开进**同一个** configOverrides 对象。键一旦相交,后展开的那个
  50. // 会静默吃掉前一个——一个 actor 会安静地丢掉自己的工具档位或模型 pin。
  51. const model = workflowActorModelPolicy({ parentSelection: PARENT }, "anthropic/haiku");
  52. const overlap = Object.keys(workflowActorToolPolicy()).filter(
  53. (key) => key in model.configOverrides,
  54. );
  55. expect(overlap).toEqual([]);
  56. });
  57. });
  58. // ── 本 run 的子代理模型(docs/dynamic-workflow/launch.md)──────────────────────────────
  59. // 优先级:本 run 的 subagentModel > resume pin > 父会话当前模型。省略即继承,显式值即替换:
  60. // pin 只守**隐式缺省**(没有 run 选择时「上次实际跑的模型」),run 选择在场时它无话可说。
  61. describe("workflowActorModelPolicy (run-level subagent model)", () => {
  62. it("no pin + a run selection: overrides with the whole selection, reasoning level included", () => {
  63. // 判别用例:按身份覆盖会把 `$high` 悄悄丢掉,而档位正是用户说出口的那一半。
  64. const policy = workflowActorModelPolicy({ parentSelection: PARENT, runSelection: RUN_CHOICE });
  65. expect(policy.configOverrides).toEqual({ modelSelection: RUN_CHOICE });
  66. });
  67. it("the run selection applies even when the parent session has no model of its own", () => {
  68. expect(workflowActorModelPolicy({ runSelection: RUN_CHOICE }).configOverrides).toEqual({
  69. modelSelection: RUN_CHOICE,
  70. });
  71. });
  72. it("a pin with the run selection's identity yields the run selection, not the pin", () => {
  73. // resume:pin 记的是身份两段(journal 只存这两段),run 选择带着档位。两者同一个模型时
  74. // 采用**更完整**的那一份——否则一次 resume 会静默把 `$high` 降级成模型的默认档位。
  75. const policy = workflowActorModelPolicy(
  76. { parentSelection: PARENT, runSelection: RUN_CHOICE },
  77. "zhipu/glm-5.3-flash",
  78. );
  79. expect(policy.configOverrides).toEqual({ modelSelection: RUN_CHOICE });
  80. });
  81. it("the run selection wins over a differing pin (the amend asked for the switch)", () => {
  82. // 回归用例(2026-09-17 testfield):前驱的子代理跑在 pin 上,用户 AmendWorkflow 带
  83. // `subagent_model` 换成别的模型,确认窗与工具输出都说「Subagents run on Z」——结果 driver
  84. // 仍按 pin 造会话。pin 防的是**静默**换模型;一次带 `subagent_model` 的 amend 恰是那个
  85. // 显式、用户看得见的决定,pin 无权压过它。
  86. const policy = workflowActorModelPolicy(
  87. { parentSelection: PARENT, runSelection: RUN_CHOICE },
  88. "openai/gpt-x",
  89. );
  90. expect(policy.configOverrides).toEqual({ modelSelection: RUN_CHOICE });
  91. });
  92. it("a pin equal to the parent model still yields the run selection", () => {
  93. // 事故的原形:前驱没设 subagent_model,子代理跑在会话模型上(pin = 父模型),amend 给了
  94. // 新选择。run 选择在场时 pin 与谁相等都无关——它只在没有 run 选择时才是缺省。
  95. const policy = workflowActorModelPolicy(
  96. { parentSelection: PARENT, runSelection: RUN_CHOICE },
  97. "anthropic/sonnet",
  98. );
  99. expect(policy.configOverrides).toEqual({ modelSelection: RUN_CHOICE });
  100. });
  101. it("with a run selection a malformed pin is never parsed: the run selection applies, no throw", () => {
  102. // pin 在 run 选择面前无关,所以也不去解析它——畸形 pin 的大声失败只属于「没有 run 选择、
  103. // 只能靠 pin 回答」那条路径(见下面的 resume pin 用例)。
  104. const policy = workflowActorModelPolicy(
  105. { parentSelection: PARENT, runSelection: RUN_CHOICE },
  106. "haiku",
  107. );
  108. expect(policy.configOverrides).toEqual({ modelSelection: RUN_CHOICE });
  109. });
  110. });
  111. // docs/dynamic-workflow/authoring.md「Choosing a model per subagent」:persona 点名的模型经本 run 的
  112. // 绑定表解析,排在 run 选择与 pin 之上。
  113. describe("workflowActorModelPolicy (persona model)", () => {
  114. const ACTOR_CHOICE: ModelSelection = {
  115. providerId: "zhipu",
  116. modelId: "GLM-5.3",
  117. options: { reasoningLevel: "low" },
  118. };
  119. it("the persona's model wins over the run selection and a differing pin, whole", () => {
  120. expect(
  121. workflowActorModelPolicy(
  122. { parentSelection: PARENT, runSelection: RUN_CHOICE, actorSelection: ACTOR_CHOICE },
  123. "anthropic/haiku",
  124. ).configOverrides,
  125. ).toEqual({ modelSelection: ACTOR_CHOICE });
  126. });
  127. it("a malformed pin is never parsed when the persona names a model", () => {
  128. expect(() =>
  129. workflowActorModelPolicy({ actorSelection: ACTOR_CHOICE }, "no-provider-segment"),
  130. ).not.toThrow();
  131. });
  132. });
  133. describe("resolveActorPersonaModel", () => {
  134. const BINDINGS = { "GLM-5.3-Flash": "zhipu/GLM-5.3-Flash", "GLM-5.3$low": "zhipu/GLM-5.3$low" };
  135. it("looks the name up verbatim and returns the whole selection", () => {
  136. expect(resolveActorPersonaModel("GLM-5.3$low", BINDINGS)).toEqual({
  137. providerId: "zhipu",
  138. modelId: "GLM-5.3",
  139. options: { reasoningLevel: "low" },
  140. });
  141. expect(resolveActorPersonaModel("GLM-5.3-Flash", BINDINGS)).toEqual({
  142. providerId: "zhipu",
  143. modelId: "GLM-5.3-Flash",
  144. });
  145. });
  146. it("a persona without a model resolves to nothing (the run selection, pin or parent decide)", () => {
  147. expect(resolveActorPersonaModel(undefined, BINDINGS)).toBeUndefined();
  148. expect(resolveActorPersonaModel(undefined, undefined)).toBeUndefined();
  149. });
  150. it("an unbound name fails loudly, naming it and what the run did bind", () => {
  151. // 只有绕过 9010 的类型断言走得到这里;悄悄退回 run 的模型就是跑一个没人批准过的模型。
  152. expect(() => resolveActorPersonaModel("glm-5.3-flash", BINDINGS)).toThrow(
  153. WorkflowActorModelUnboundError,
  154. );
  155. expect(() => resolveActorPersonaModel("x", undefined)).toThrow(/the script named no models/);
  156. expect(() => resolveActorPersonaModel("x", BINDINGS)).toThrow(
  157. /"GLM-5.3-Flash", "GLM-5.3\$low"/,
  158. );
  159. });
  160. });
  161. describe("workflowActorModelPolicy (resume pin)", () => {
  162. // pin 是 persona 冻结不变式的持久化那一半:没有 run 选择时,resume 重建的 actor 必须跑在
  163. // 上一次实际跑的模型上,而不是父会话此刻漂到的模型上。
  164. it("pin equal to the current parent model overrides nothing", () => {
  165. // 不覆盖是更强的表达:child runtime 的基线连 reasoning 选项一起继承;按身份覆盖会把
  166. // 选项换成一个少了 options 的等价物。
  167. const policy = workflowActorModelPolicy({ parentSelection: PARENT }, "anthropic/sonnet");
  168. expect(policy.configOverrides).toEqual({});
  169. });
  170. it("pin comparison ignores the parent's reasoning options (identity is provider/model)", () => {
  171. const policy = workflowActorModelPolicy(
  172. { parentSelection: { ...PARENT, options: { reasoningLevel: "high" } } },
  173. "anthropic/sonnet",
  174. );
  175. expect(policy.configOverrides).toEqual({});
  176. });
  177. it("pins a model that is not the current parent model (no run selection: the pin is the default)", () => {
  178. // 判别用例:父会话在两次 run 之间换了主模型。没有 pin,这个 actor 会悄悄挪到新模型上——
  179. // 正是 pin 要防的静默身份变更。
  180. const policy = workflowActorModelPolicy({ parentSelection: PARENT }, "openai/gpt-x");
  181. expect(policy.configOverrides.modelSelection).toEqual({
  182. providerId: "openai",
  183. modelId: "gpt-x",
  184. });
  185. expect(workflowActorModelPolicy({}, "anthropic/haiku").configOverrides).toEqual({
  186. modelSelection: OTHER,
  187. });
  188. });
  189. it("a malformed pin fails loudly instead of falling back to the parent model", () => {
  190. // 缺 provider 段的 pin 不是本机写出的格式:拿父会话 provider 去补等于猜出一个新身份。
  191. // 宁可大声失败(v1 的 pin-miss 策略),绝不静默回退。
  192. expect(() => workflowActorModelPolicy({ parentSelection: PARENT }, "haiku")).toThrow(
  193. WorkflowActorPinnedModelError,
  194. );
  195. });
  196. });
  197. describe("pinnedActorModel", () => {
  198. const RUN = "dwfrun-pin";
  199. const ACTOR = { siteId: "actor#1", ordinal: 1 };
  200. it("returns the journaled resolvedModel when a previous run recorded one", () => {
  201. const journal = new InMemoryJournalStore();
  202. journal.createRun({
  203. runId: RUN,
  204. caps: { maxConcurrency: 16 },
  205. spentTokens: 0,
  206. status: "running",
  207. });
  208. journal.putActor({
  209. runId: RUN,
  210. siteId: ACTOR.siteId,
  211. ordinal: ACTOR.ordinal,
  212. persona: { name: "judge" },
  213. resolvedModel: "anthropic/haiku",
  214. });
  215. expect(pinnedActorModel({ actor: ACTOR, journal, runId: RUN })).toBe("anthropic/haiku");
  216. });
  217. it("is undefined for a fresh actor record and for a missing record", () => {
  218. const journal = new InMemoryJournalStore();
  219. journal.createRun({
  220. runId: RUN,
  221. caps: { maxConcurrency: 16 },
  222. spentTokens: 0,
  223. status: "running",
  224. });
  225. // 全新 run:引擎刚落了 persona,宿主还没造会话——runtime 工厂此刻读到的必须是缺席。
  226. journal.putActor({
  227. runId: RUN,
  228. siteId: ACTOR.siteId,
  229. ordinal: ACTOR.ordinal,
  230. persona: { name: "judge" },
  231. });
  232. expect(pinnedActorModel({ actor: ACTOR, journal, runId: RUN })).toBeUndefined();
  233. expect(
  234. pinnedActorModel({ actor: { siteId: "actor#9", ordinal: 1 }, journal, runId: RUN }),
  235. ).toBeUndefined();
  236. });
  237. });
  238. describe("journalActorResolvedModel", () => {
  239. const RUN = "dwfrun-1";
  240. const ACTOR = { siteId: "actor#1", ordinal: 1 };
  241. function journalWithActor() {
  242. const journal = new InMemoryJournalStore();
  243. journal.createRun({
  244. runId: RUN,
  245. caps: { maxConcurrency: 16 },
  246. spentTokens: 0,
  247. status: "running",
  248. });
  249. // 引擎在 createActor 时同步落下的那条记录:冻结 persona,还没有模型记录。
  250. journal.putActor({
  251. runId: RUN,
  252. siteId: ACTOR.siteId,
  253. ordinal: ACTOR.ordinal,
  254. name: "judge",
  255. persona: { name: "judge" },
  256. });
  257. return journal;
  258. }
  259. it("records the model the runtime actually got, without touching the frozen persona", () => {
  260. const journal = journalWithActor();
  261. journalActorResolvedModel({ actor: ACTOR, journal, selection: OTHER, runId: RUN });
  262. expect(journal.getActor(RUN, ACTOR.siteId, ACTOR.ordinal)).toEqual({
  263. runId: RUN,
  264. siteId: ACTOR.siteId,
  265. ordinal: ACTOR.ordinal,
  266. name: "judge",
  267. persona: { name: "judge" },
  268. resolvedModel: "anthropic/haiku",
  269. });
  270. });
  271. it("records the parent model verbatim on the no-override path", () => {
  272. // 「不覆盖」在记录里不是空白,而是父模型本身——run 的成本因此可审计,而 resume 能看出
  273. // 上一次究竟跑在哪个模型上。
  274. const journal = journalWithActor();
  275. journalActorResolvedModel({ actor: ACTOR, journal, selection: PARENT, runId: RUN });
  276. expect(journal.getActor(RUN, ACTOR.siteId, ACTOR.ordinal)?.resolvedModel).toBe(
  277. "anthropic/sonnet",
  278. );
  279. });
  280. it("keeps a sessionId written earlier by the driver", () => {
  281. // putActor 是整条记录的替换,而这条记录有两个 driver 侧作者(会话 id 与模型记录)。
  282. const journal = journalWithActor();
  283. const existing = journal.getActor(RUN, ACTOR.siteId, ACTOR.ordinal)!;
  284. journal.putActor({ ...existing, sessionId: "ses_actor" });
  285. journalActorResolvedModel({ actor: ACTOR, journal, selection: OTHER, runId: RUN });
  286. expect(journal.getActor(RUN, ACTOR.siteId, ACTOR.ordinal)).toMatchObject({
  287. sessionId: "ses_actor",
  288. resolvedModel: "anthropic/haiku",
  289. persona: { name: "judge" },
  290. });
  291. });
  292. });