workflow-actor-model.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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("no pin + no run selection: overrides nothing (the child inherits the session model)", () => {
  68. expect(workflowActorModelPolicy({ parentSelection: PARENT }).configOverrides).toEqual({});
  69. expect(workflowActorModelPolicy({}).configOverrides).toEqual({});
  70. });
  71. it("the run selection applies even when the parent session has no model of its own", () => {
  72. expect(workflowActorModelPolicy({ runSelection: RUN_CHOICE }).configOverrides).toEqual({
  73. modelSelection: RUN_CHOICE,
  74. });
  75. });
  76. it("a pin with the run selection's identity yields the run selection, not the pin", () => {
  77. // resume:pin 记的是身份两段(journal 只存这两段),run 选择带着档位。两者同一个模型时
  78. // 采用**更完整**的那一份——否则一次 resume 会静默把 `$high` 降级成模型的默认档位。
  79. const policy = workflowActorModelPolicy(
  80. { parentSelection: PARENT, runSelection: RUN_CHOICE },
  81. "zhipu/glm-5.3-flash",
  82. );
  83. expect(policy.configOverrides).toEqual({ modelSelection: RUN_CHOICE });
  84. });
  85. it("a pin with the parent's identity and no run selection overrides nothing", () => {
  86. const policy = workflowActorModelPolicy({ parentSelection: PARENT }, "anthropic/sonnet");
  87. expect(policy.configOverrides).toEqual({});
  88. });
  89. it("the run selection wins over a differing pin (the amend asked for the switch)", () => {
  90. // 回归用例(2026-09-17 testfield):前驱的子代理跑在 pin 上,用户 AmendWorkflow 带
  91. // `subagent_model` 换成别的模型,确认窗与工具输出都说「Subagents run on Z」——结果 driver
  92. // 仍按 pin 造会话。pin 防的是**静默**换模型;一次带 `subagent_model` 的 amend 恰是那个
  93. // 显式、用户看得见的决定,pin 无权压过它。
  94. const policy = workflowActorModelPolicy(
  95. { parentSelection: PARENT, runSelection: RUN_CHOICE },
  96. "openai/gpt-x",
  97. );
  98. expect(policy.configOverrides).toEqual({ modelSelection: RUN_CHOICE });
  99. });
  100. it("a pin equal to the parent model still yields the run selection", () => {
  101. // 事故的原形:前驱没设 subagent_model,子代理跑在会话模型上(pin = 父模型),amend 给了
  102. // 新选择。run 选择在场时 pin 与谁相等都无关——它只在没有 run 选择时才是缺省。
  103. const policy = workflowActorModelPolicy(
  104. { parentSelection: PARENT, runSelection: RUN_CHOICE },
  105. "anthropic/sonnet",
  106. );
  107. expect(policy.configOverrides).toEqual({ modelSelection: RUN_CHOICE });
  108. });
  109. it("with a run selection a malformed pin is never parsed: the run selection applies, no throw", () => {
  110. // pin 在 run 选择面前无关,所以也不去解析它——畸形 pin 的大声失败只属于「没有 run 选择、
  111. // 只能靠 pin 回答」那条路径(见下面的 resume pin 用例)。
  112. const policy = workflowActorModelPolicy(
  113. { parentSelection: PARENT, runSelection: RUN_CHOICE },
  114. "haiku",
  115. );
  116. expect(policy.configOverrides).toEqual({ modelSelection: RUN_CHOICE });
  117. });
  118. });
  119. // docs/dynamic-workflow/authoring.md「Choosing a model per subagent」:persona 点名的模型经本 run 的
  120. // 绑定表解析,排在 run 选择与 pin 之上。
  121. describe("workflowActorModelPolicy (persona model)", () => {
  122. const ACTOR_CHOICE: ModelSelection = {
  123. providerId: "zhipu",
  124. modelId: "GLM-5.3",
  125. options: { reasoningLevel: "low" },
  126. };
  127. it("the persona's model wins over the run selection and a differing pin, whole", () => {
  128. expect(
  129. workflowActorModelPolicy(
  130. { parentSelection: PARENT, runSelection: RUN_CHOICE, actorSelection: ACTOR_CHOICE },
  131. "anthropic/haiku",
  132. ).configOverrides,
  133. ).toEqual({ modelSelection: ACTOR_CHOICE });
  134. });
  135. it("a malformed pin is never parsed when the persona names a model", () => {
  136. expect(() =>
  137. workflowActorModelPolicy({ actorSelection: ACTOR_CHOICE }, "no-provider-segment"),
  138. ).not.toThrow();
  139. });
  140. it("without a persona model the run selection still applies (the default for the rest)", () => {
  141. expect(
  142. workflowActorModelPolicy({ parentSelection: PARENT, runSelection: RUN_CHOICE })
  143. .configOverrides,
  144. ).toEqual({ modelSelection: RUN_CHOICE });
  145. });
  146. });
  147. describe("resolveActorPersonaModel", () => {
  148. const BINDINGS = { "GLM-5.3-Flash": "zhipu/GLM-5.3-Flash", "GLM-5.3$low": "zhipu/GLM-5.3$low" };
  149. it("looks the name up verbatim and returns the whole selection", () => {
  150. expect(resolveActorPersonaModel("GLM-5.3$low", BINDINGS)).toEqual({
  151. providerId: "zhipu",
  152. modelId: "GLM-5.3",
  153. options: { reasoningLevel: "low" },
  154. });
  155. expect(resolveActorPersonaModel("GLM-5.3-Flash", BINDINGS)).toEqual({
  156. providerId: "zhipu",
  157. modelId: "GLM-5.3-Flash",
  158. });
  159. });
  160. it("a persona without a model resolves to nothing (the run selection, pin or parent decide)", () => {
  161. expect(resolveActorPersonaModel(undefined, BINDINGS)).toBeUndefined();
  162. expect(resolveActorPersonaModel(undefined, undefined)).toBeUndefined();
  163. });
  164. it("an unbound name fails loudly, naming it and what the run did bind", () => {
  165. // 只有绕过 9010 的类型断言走得到这里;悄悄退回 run 的模型就是跑一个没人批准过的模型。
  166. expect(() => resolveActorPersonaModel("glm-5.3-flash", BINDINGS)).toThrow(
  167. WorkflowActorModelUnboundError,
  168. );
  169. expect(() => resolveActorPersonaModel("x", undefined)).toThrow(/the script named no models/);
  170. expect(() => resolveActorPersonaModel("x", BINDINGS)).toThrow(
  171. /"GLM-5.3-Flash", "GLM-5.3\$low"/,
  172. );
  173. });
  174. });
  175. describe("workflowActorModelPolicy (resume pin)", () => {
  176. // pin 是 persona 冻结不变式的持久化那一半:没有 run 选择时,resume 重建的 actor 必须跑在
  177. // 上一次实际跑的模型上,而不是父会话此刻漂到的模型上。
  178. it("pin equal to the current parent model overrides nothing", () => {
  179. // 不覆盖是更强的表达:child runtime 的基线连 reasoning 选项一起继承;按身份覆盖会把
  180. // 选项换成一个少了 options 的等价物。
  181. const policy = workflowActorModelPolicy({ parentSelection: PARENT }, "anthropic/sonnet");
  182. expect(policy.configOverrides).toEqual({});
  183. });
  184. it("pin comparison ignores the parent's reasoning options (identity is provider/model)", () => {
  185. const policy = workflowActorModelPolicy(
  186. { parentSelection: { ...PARENT, options: { reasoningLevel: "high" } } },
  187. "anthropic/sonnet",
  188. );
  189. expect(policy.configOverrides).toEqual({});
  190. });
  191. it("pins a model that is not the current parent model (no run selection: the pin is the default)", () => {
  192. // 判别用例:父会话在两次 run 之间换了主模型。没有 pin,这个 actor 会悄悄挪到新模型上——
  193. // 正是 pin 要防的静默身份变更。
  194. const policy = workflowActorModelPolicy({ parentSelection: PARENT }, "openai/gpt-x");
  195. expect(policy.configOverrides.modelSelection).toEqual({
  196. providerId: "openai",
  197. modelId: "gpt-x",
  198. });
  199. expect(workflowActorModelPolicy({}, "anthropic/haiku").configOverrides).toEqual({
  200. modelSelection: OTHER,
  201. });
  202. });
  203. it("a malformed pin fails loudly instead of falling back to the parent model", () => {
  204. // 缺 provider 段的 pin 不是本机写出的格式:拿父会话 provider 去补等于猜出一个新身份。
  205. // 宁可大声失败(v1 的 pin-miss 策略),绝不静默回退。
  206. expect(() => workflowActorModelPolicy({ parentSelection: PARENT }, "haiku")).toThrow(
  207. WorkflowActorPinnedModelError,
  208. );
  209. });
  210. });
  211. describe("pinnedActorModel", () => {
  212. const RUN = "dwfrun-pin";
  213. const ACTOR = { siteId: "actor#1", ordinal: 1 };
  214. it("returns the journaled resolvedModel when a previous run recorded one", () => {
  215. const journal = new InMemoryJournalStore();
  216. journal.createRun({
  217. runId: RUN,
  218. caps: { maxConcurrency: 16 },
  219. spentTokens: 0,
  220. status: "running",
  221. });
  222. journal.putActor({
  223. runId: RUN,
  224. siteId: ACTOR.siteId,
  225. ordinal: ACTOR.ordinal,
  226. persona: { name: "judge" },
  227. resolvedModel: "anthropic/haiku",
  228. });
  229. expect(pinnedActorModel({ actor: ACTOR, journal, runId: RUN })).toBe("anthropic/haiku");
  230. });
  231. it("is undefined for a fresh actor record and for a missing record", () => {
  232. const journal = new InMemoryJournalStore();
  233. journal.createRun({
  234. runId: RUN,
  235. caps: { maxConcurrency: 16 },
  236. spentTokens: 0,
  237. status: "running",
  238. });
  239. // 全新 run:引擎刚落了 persona,宿主还没造会话——runtime 工厂此刻读到的必须是缺席。
  240. journal.putActor({
  241. runId: RUN,
  242. siteId: ACTOR.siteId,
  243. ordinal: ACTOR.ordinal,
  244. persona: { name: "judge" },
  245. });
  246. expect(pinnedActorModel({ actor: ACTOR, journal, runId: RUN })).toBeUndefined();
  247. expect(
  248. pinnedActorModel({ actor: { siteId: "actor#9", ordinal: 1 }, journal, runId: RUN }),
  249. ).toBeUndefined();
  250. });
  251. });
  252. describe("journalActorResolvedModel", () => {
  253. const RUN = "dwfrun-1";
  254. const ACTOR = { siteId: "actor#1", ordinal: 1 };
  255. function journalWithActor() {
  256. const journal = new InMemoryJournalStore();
  257. journal.createRun({
  258. runId: RUN,
  259. caps: { maxConcurrency: 16 },
  260. spentTokens: 0,
  261. status: "running",
  262. });
  263. // 引擎在 createActor 时同步落下的那条记录:冻结 persona,还没有模型记录。
  264. journal.putActor({
  265. runId: RUN,
  266. siteId: ACTOR.siteId,
  267. ordinal: ACTOR.ordinal,
  268. name: "judge",
  269. persona: { name: "judge" },
  270. });
  271. return journal;
  272. }
  273. it("records the model the runtime actually got, without touching the frozen persona", () => {
  274. const journal = journalWithActor();
  275. journalActorResolvedModel({ actor: ACTOR, journal, selection: OTHER, runId: RUN });
  276. expect(journal.getActor(RUN, ACTOR.siteId, ACTOR.ordinal)).toEqual({
  277. runId: RUN,
  278. siteId: ACTOR.siteId,
  279. ordinal: ACTOR.ordinal,
  280. name: "judge",
  281. persona: { name: "judge" },
  282. resolvedModel: "anthropic/haiku",
  283. });
  284. });
  285. it("records the parent model verbatim on the no-override path", () => {
  286. // 「不覆盖」在记录里不是空白,而是父模型本身——run 的成本因此可审计,而 resume 能看出
  287. // 上一次究竟跑在哪个模型上。
  288. const journal = journalWithActor();
  289. journalActorResolvedModel({ actor: ACTOR, journal, selection: PARENT, runId: RUN });
  290. expect(journal.getActor(RUN, ACTOR.siteId, ACTOR.ordinal)?.resolvedModel).toBe(
  291. "anthropic/sonnet",
  292. );
  293. });
  294. it("keeps a sessionId written earlier by the driver", () => {
  295. // putActor 是整条记录的替换,而这条记录有两个 driver 侧作者(会话 id 与模型记录)。
  296. const journal = journalWithActor();
  297. const existing = journal.getActor(RUN, ACTOR.siteId, ACTOR.ordinal)!;
  298. journal.putActor({ ...existing, sessionId: "ses_actor" });
  299. journalActorResolvedModel({ actor: ACTOR, journal, selection: OTHER, runId: RUN });
  300. expect(journal.getActor(RUN, ACTOR.siteId, ACTOR.ordinal)).toMatchObject({
  301. sessionId: "ses_actor",
  302. resolvedModel: "anthropic/haiku",
  303. persona: { name: "judge" },
  304. });
  305. });
  306. });