v4-native-model-config.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // L2:v4 原生 switchModelConfig——验证自旧 server-operations.switchModelConfig 搬运的
  2. // 决策语义在原生层保真:previous 在 setModel 之前从 getSessionModelSelection 快照、
  3. // 跨模型由 setModel 解析目标兼容 thought、同模型显式 thought 才调 setThoughtLevel、
  4. // session 不存在拒绝。08-phasing M4 完成定义的 L2 层。
  5. // R-19(10 §4.2.3)追加:同值切换 → V4CommandNoopError(config.unchanged);
  6. // applyRequestedSessionConfig(createSession.config 消费共用件)。
  7. import { describe, expect, it, vi } from "vitest";
  8. import { parseCommandEnvelope, type CommandEnvelope } from "@zcode/shared/zcode-protocol-v4";
  9. import {
  10. V4CommandExecutor,
  11. V4SessionNotFoundError,
  12. } from "../src/zcode-protocol-v4/commands/executor.js";
  13. import { applyRequestedSessionConfig } from "../src/zcode-protocol-v4/commands/handlers/model-config.js";
  14. import { V4CommandNoopError } from "../src/zcode-protocol-v4/index.js";
  15. import type {
  16. V4CommandCoreHost,
  17. V4SessionRecordView,
  18. } from "../src/zcode-protocol-v4/commands/types.js";
  19. /** 切换前的结构化选型。 */
  20. const PREVIOUS_SELECTION = { providerId: "prov-a", modelId: "old-model" };
  21. const TRACE = { traceId: "trace-1", sessionId: "s1" };
  22. const SUPPORTED_THOUGHT_LEVELS = ["low", "medium", "high", "max"];
  23. function makeApp() {
  24. // calls 记录跨方法调用顺序(顺序本身是搬运语义:先快照 previous,再切模型,最后补发事件)。
  25. const calls: string[] = [];
  26. return {
  27. calls,
  28. sessionId: "s1",
  29. runtime: {
  30. getPlanEnabled: vi.fn(() => false),
  31. setExecutionState: vi.fn(async () => {}),
  32. getSessionModelSelection: vi.fn(() => {
  33. calls.push("getSessionModelSelection");
  34. return PREVIOUS_SELECTION;
  35. }),
  36. emitModelSelected: vi.fn(async () => {
  37. calls.push("emitModelSelected");
  38. }),
  39. emitModeChanged: vi.fn(async () => {
  40. calls.push("emitModeChanged");
  41. }),
  42. },
  43. setModel: vi.fn(async () => {
  44. calls.push("setModel");
  45. return { thoughtLevel: undefined };
  46. }),
  47. setThoughtLevel: vi.fn(async (level: string) => {
  48. calls.push("setThoughtLevel");
  49. return { thoughtLevel: level };
  50. }),
  51. getThoughtLevel: vi.fn((): string | undefined => undefined),
  52. listThoughtLevels: vi.fn(() => ["low", "medium", "high", "max"]),
  53. getMode: vi.fn(() => {
  54. calls.push("getMode");
  55. return "build";
  56. }),
  57. setMode: vi.fn(async () => {
  58. calls.push("setMode");
  59. return {};
  60. }),
  61. setFollowupMode: vi.fn(async () => {
  62. calls.push("setFollowupMode");
  63. }),
  64. };
  65. }
  66. function makeRecord(app: ReturnType<typeof makeApp>): V4SessionRecordView {
  67. return {
  68. app: app as unknown as V4SessionRecordView["app"],
  69. workspace: { workspacePath: "/w" },
  70. persistence: "immediate",
  71. traceContext: TRACE,
  72. } as V4SessionRecordView;
  73. }
  74. function makeHost(record: V4SessionRecordView): V4CommandCoreHost {
  75. return {
  76. getRecord: (id) => (id === "s1" ? record : undefined),
  77. };
  78. }
  79. function envelope(sessionId: string, payload: unknown): CommandEnvelope {
  80. return {
  81. type: "switchModelConfig",
  82. payload,
  83. sessionId,
  84. commandId: "cmd-1",
  85. baseRevision: 0,
  86. } as unknown as CommandEnvelope;
  87. }
  88. describe("v4 原生 switchModelConfig", () => {
  89. it("跨模型且 payload thought 不受目标支持:setModel 解析目标实际 thought,不把源模型 thought 再次强写", async () => {
  90. const app = makeApp();
  91. app.setModel.mockImplementationOnce(async () => {
  92. app.calls.push("setModel");
  93. return { thoughtLevel: "high" };
  94. });
  95. const record = makeRecord(app);
  96. await new V4CommandExecutor(makeHost(record)).execute(
  97. envelope("s1", { provider: "prov-a", model: "new-model", thought: "enabled" }),
  98. );
  99. // 顺序:Selection 必须在 setModel 之前快照,否则 previous 会在切换后丢失。
  100. expect(app.calls).toEqual(["getSessionModelSelection", "setModel", "emitModelSelected"]);
  101. // setModel 仍接收协议边界的 "providerId/modelId" 文本选择。
  102. expect(app.setModel).toHaveBeenCalledWith("prov-a/new-model");
  103. expect(app.setThoughtLevel).not.toHaveBeenCalled();
  104. // payload.enabled 是源模型附带值;effective 档位单独投影,稀疏 Selection 不 pin 默认值。
  105. expect(app.runtime.emitModelSelected).toHaveBeenCalledWith({
  106. modelSelection: { providerId: "prov-a", modelId: "new-model" },
  107. effectiveReasoningLevel: "high",
  108. previousModelSelection: PREVIOUS_SELECTION,
  109. supportedThoughtLevels: SUPPORTED_THOUGHT_LEVELS,
  110. traceContext: TRACE,
  111. });
  112. });
  113. it("跨 provider 且源 thought 不兼容:模型切换成功,默认档位不反写 Selection", async () => {
  114. const app = makeApp();
  115. app.setModel.mockImplementationOnce(async () => {
  116. app.calls.push("setModel");
  117. return { thoughtLevel: "max" };
  118. });
  119. const record = makeRecord(app);
  120. await new V4CommandExecutor(makeHost(record)).execute(
  121. envelope("s1", {
  122. provider: "default-deepseek",
  123. model: "deepseek-v4-flash",
  124. thought: "enabled",
  125. }),
  126. );
  127. expect(app.setModel).toHaveBeenCalledWith("default-deepseek/deepseek-v4-flash");
  128. expect(app.setThoughtLevel).not.toHaveBeenCalled();
  129. expect(app.runtime.emitModelSelected).toHaveBeenCalledWith({
  130. modelSelection: {
  131. providerId: "default-deepseek",
  132. modelId: "deepseek-v4-flash",
  133. },
  134. effectiveReasoningLevel: "max",
  135. previousModelSelection: PREVIOUS_SELECTION,
  136. supportedThoughtLevels: SUPPORTED_THOUGHT_LEVELS,
  137. traceContext: TRACE,
  138. });
  139. });
  140. it("跨模型且目标支持 payload thought:切模型后显式收敛到用户选择的 thought", async () => {
  141. const app = makeApp();
  142. app.setModel.mockImplementationOnce(async () => {
  143. app.calls.push("setModel");
  144. return { thoughtLevel: "medium" };
  145. });
  146. const record = makeRecord(app);
  147. await new V4CommandExecutor(makeHost(record)).execute(
  148. envelope("s1", { provider: "prov-a", model: "new-model", thought: "high" }),
  149. );
  150. expect(app.calls).toEqual([
  151. "getSessionModelSelection",
  152. "setModel",
  153. "setThoughtLevel",
  154. "emitModelSelected",
  155. ]);
  156. expect(app.setModel).toHaveBeenCalledWith("prov-a/new-model");
  157. expect(app.setThoughtLevel).toHaveBeenCalledWith("high");
  158. expect(app.runtime.emitModelSelected).toHaveBeenCalledWith({
  159. modelSelection: {
  160. providerId: "prov-a",
  161. modelId: "new-model",
  162. options: { reasoningLevel: "high" },
  163. },
  164. effectiveReasoningLevel: "high",
  165. previousModelSelection: PREVIOUS_SELECTION,
  166. supportedThoughtLevels: SUPPORTED_THOUGHT_LEVELS,
  167. traceContext: TRACE,
  168. });
  169. });
  170. it("同模型显式切 thought:只调 setThoughtLevel,再发布实际档位", async () => {
  171. const app = makeApp();
  172. const record = makeRecord(app);
  173. await new V4CommandExecutor(makeHost(record)).execute(
  174. envelope("s1", { provider: "prov-a", model: "old-model", thought: "max" }),
  175. );
  176. expect(app.setModel).not.toHaveBeenCalled();
  177. expect(app.setThoughtLevel).toHaveBeenCalledWith("max");
  178. expect(app.calls).toEqual(["getSessionModelSelection", "setThoughtLevel", "emitModelSelected"]);
  179. expect(app.runtime.emitModelSelected).toHaveBeenCalledWith({
  180. modelSelection: {
  181. providerId: "prov-a",
  182. modelId: "old-model",
  183. options: { reasoningLevel: "max" },
  184. },
  185. effectiveReasoningLevel: "max",
  186. previousModelSelection: PREVIOUS_SELECTION,
  187. supportedThoughtLevels: SUPPORTED_THOUGHT_LEVELS,
  188. traceContext: TRACE,
  189. });
  190. });
  191. it("同模型显式选择 effective 默认档位仍保存为稀疏 Selection pin", async () => {
  192. const app = makeApp();
  193. app.getThoughtLevel.mockReturnValue("high");
  194. const record = makeRecord(app);
  195. await new V4CommandExecutor(makeHost(record)).execute(
  196. envelope("s1", { provider: "prov-a", model: "old-model", thought: "high" }),
  197. );
  198. expect(app.setThoughtLevel).toHaveBeenCalledWith("high");
  199. expect(app.runtime.emitModelSelected).toHaveBeenCalledWith(
  200. expect.objectContaining({
  201. modelSelection: {
  202. providerId: "prov-a",
  203. modelId: "old-model",
  204. options: { reasoningLevel: "high" },
  205. },
  206. effectiveReasoningLevel: "high",
  207. }),
  208. );
  209. });
  210. it("同模型显式切到不支持的 thought:失败且不触碰模型身份", async () => {
  211. const app = makeApp();
  212. app.setThoughtLevel.mockRejectedValueOnce(new Error("Unsupported reasoning effort: enabled"));
  213. const record = makeRecord(app);
  214. await expect(
  215. new V4CommandExecutor(makeHost(record)).execute(
  216. envelope("s1", { provider: "prov-a", model: "old-model", thought: "enabled" }),
  217. ),
  218. ).rejects.toThrow("Unsupported reasoning effort: enabled");
  219. expect(app.setModel).not.toHaveBeenCalled();
  220. expect(app.runtime.emitModelSelected).not.toHaveBeenCalled();
  221. });
  222. it("thought 缺省(空串):不调 setThoughtLevel,selection 不带 reasoning option", async () => {
  223. const app = makeApp();
  224. const record = makeRecord(app);
  225. await new V4CommandExecutor(makeHost(record)).execute(
  226. envelope("s1", { provider: "prov-a", model: "new-model", thought: "" }),
  227. );
  228. expect(app.setThoughtLevel).not.toHaveBeenCalled();
  229. expect(app.calls).toEqual(["getSessionModelSelection", "setModel", "emitModelSelected"]);
  230. expect(app.runtime.emitModelSelected).toHaveBeenCalledWith({
  231. modelSelection: { providerId: "prov-a", modelId: "new-model" },
  232. previousModelSelection: PREVIOUS_SELECTION,
  233. supportedThoughtLevels: SUPPORTED_THOUGHT_LEVELS,
  234. traceContext: TRACE,
  235. });
  236. });
  237. it("session 不存在:拒绝且不触碰任何 core 方法", async () => {
  238. const app = makeApp();
  239. const record = makeRecord(app);
  240. await expect(
  241. new V4CommandExecutor(makeHost(record)).execute(
  242. envelope("missing", { provider: "p", model: "m", thought: "" }),
  243. ),
  244. ).rejects.toThrow(V4SessionNotFoundError);
  245. expect(app.setModel).not.toHaveBeenCalled();
  246. expect(app.runtime.emitModelSelected).not.toHaveBeenCalled();
  247. });
  248. // R-19:同值切换不得以 accepted 静默吞掉——UI 需要区分「已生效」与「本来就是这个值」。
  249. it("同值切换:抛 V4CommandNoopError(config.unchanged),不切模型不补发事件", async () => {
  250. const app = makeApp();
  251. const record = makeRecord(app);
  252. await expect(
  253. new V4CommandExecutor(makeHost(record)).execute(
  254. envelope("s1", { provider: "prov-a", model: "old-model", thought: "" }),
  255. ),
  256. ).rejects.toThrow(V4CommandNoopError);
  257. expect(app.setModel).not.toHaveBeenCalled();
  258. expect(app.setThoughtLevel).not.toHaveBeenCalled();
  259. expect(app.runtime.emitModelSelected).not.toHaveBeenCalled();
  260. });
  261. it("同模型显式选择 effective 值:仍固定为 option pin", async () => {
  262. const app = makeApp();
  263. app.getThoughtLevel.mockReturnValue("max");
  264. const record = makeRecord(app);
  265. await new V4CommandExecutor(makeHost(record)).execute(
  266. envelope("s1", { provider: "prov-a", model: "old-model", thought: "max" }),
  267. );
  268. expect(app.setModel).not.toHaveBeenCalled();
  269. expect(app.setThoughtLevel).toHaveBeenCalledWith("max");
  270. });
  271. it("supports 分流:switchModelConfig 已原生", () => {
  272. const executor = new V4CommandExecutor(makeHost(makeRecord(makeApp())));
  273. expect(executor.supports("switchModelConfig")).toBe(true);
  274. });
  275. });
  276. // M5 additive:switchCollaborationMode(UI 模式选择器 → app.setMode + SessionModeChanged 补发)。
  277. describe("v4 原生 switchCollaborationMode", () => {
  278. it("guarded 经同一模式命令切换,未知值报错而不降级", async () => {
  279. const app = makeApp();
  280. const executor = new V4CommandExecutor(makeHost(makeRecord(app)));
  281. const requested = {
  282. ...modeEnvelope("s1", { mode: "guarded" }),
  283. clientId: "test",
  284. issuedAt: Date.now(),
  285. };
  286. const parsed = parseCommandEnvelope(requested);
  287. expect(parsed.ok).toBe(true);
  288. if (!parsed.ok) throw parsed.error;
  289. await executor.execute(parsed.envelope);
  290. expect(app.setMode).toHaveBeenCalledWith("guarded");
  291. // executor 只消费协议边界已经校验的 envelope,未知模式应在同一边界被拒绝。
  292. expect(parseCommandEnvelope({ ...requested, payload: { mode: "unsupported" } }).ok).toBe(false);
  293. expect(app.setMode).toHaveBeenCalledTimes(1);
  294. });
  295. function modeEnvelope(sessionId: string, payload: unknown): CommandEnvelope {
  296. return {
  297. type: "switchCollaborationMode",
  298. payload,
  299. sessionId,
  300. commandId: "cmd-mode-1",
  301. baseRevision: 0,
  302. } as unknown as CommandEnvelope;
  303. }
  304. it("正路径:setMode 内部统一更新状态和事件,命令层不重复补发", async () => {
  305. const app = makeApp();
  306. const record = makeRecord(app);
  307. await new V4CommandExecutor(makeHost(record)).execute(modeEnvelope("s1", { mode: "plan" }));
  308. expect(app.calls).toEqual(["getMode", "setMode"]);
  309. expect(app.setMode).toHaveBeenCalledWith("plan");
  310. expect(app.runtime.emitModeChanged).not.toHaveBeenCalled();
  311. });
  312. // R-19:同值切换从静默 return(被当 accepted)改为显式 noop——旧行为与投影种子缺失
  313. // 叠加成过「点完全访问没反应」的用户可见 bug(runtime 已是 yolo、投影种子还是 build)。
  314. it("同值切换:抛 V4CommandNoopError(config.unchanged)——不调 setMode、不补发事件(不 bump revision)", async () => {
  315. const app = makeApp();
  316. const record = makeRecord(app);
  317. await expect(
  318. new V4CommandExecutor(makeHost(record)).execute(modeEnvelope("s1", { mode: "build" })),
  319. ).rejects.toThrow(V4CommandNoopError);
  320. expect(app.setMode).not.toHaveBeenCalled();
  321. expect(app.runtime.emitModeChanged).not.toHaveBeenCalled();
  322. });
  323. it("session 不存在:拒绝且不触碰任何 core 方法", async () => {
  324. const app = makeApp();
  325. const record = makeRecord(app);
  326. await expect(
  327. new V4CommandExecutor(makeHost(record)).execute(modeEnvelope("missing", { mode: "plan" })),
  328. ).rejects.toThrow(V4SessionNotFoundError);
  329. expect(app.setMode).not.toHaveBeenCalled();
  330. });
  331. it("supports 分流:switchCollaborationMode 已原生", () => {
  332. const executor = new V4CommandExecutor(makeHost(makeRecord(makeApp())));
  333. expect(executor.supports("switchCollaborationMode")).toBe(true);
  334. });
  335. });
  336. // R-19:createSession.config 消费共用件——请求 config 覆盖 runtime 缺省,
  337. // 只对差异部分生效并补发与 switch 命令同源的事件(日志自足)。
  338. describe("切模型前校验当前 Environment Provider", () => {
  339. function makeResolvingHost(
  340. record: V4SessionRecordView,
  341. outcome: { available: boolean; reason?: string },
  342. captured: string[],
  343. ): V4CommandCoreHost {
  344. return {
  345. getRecord: (id) => (id === "s1" ? record : undefined),
  346. ensureProviderAvailable: async (_sessionId, providerId) => {
  347. captured.push(providerId);
  348. return outcome;
  349. },
  350. };
  351. }
  352. it("provider 已在 registry:setModel 之前解析放行,跨 provider 切换生效", async () => {
  353. const app = makeApp();
  354. const record = makeRecord(app);
  355. const captured: string[] = [];
  356. await new V4CommandExecutor(makeResolvingHost(record, { available: true }, captured)).execute(
  357. envelope("s1", { provider: "prov-b", model: "new-model", thought: "" }),
  358. );
  359. expect(captured).toEqual(["prov-b"]);
  360. expect(app.setModel).toHaveBeenCalledWith("prov-b/new-model");
  361. expect(app.runtime.emitModelSelected).toHaveBeenCalled();
  362. });
  363. it("provider 不在 registry(applied:false):failed reasonCode=provider.notInRegistry,不切模型/不补发", async () => {
  364. const app = makeApp();
  365. const record = makeRecord(app);
  366. const captured: string[] = [];
  367. await expect(
  368. new V4CommandExecutor(
  369. makeResolvingHost(
  370. record,
  371. { available: false, reason: "provider_not_in_registry" },
  372. captured,
  373. ),
  374. ).execute(envelope("s1", { provider: "prov-x", model: "m", thought: "" })),
  375. ).rejects.toMatchObject({ reasonCode: "provider.notInRegistry" });
  376. expect(app.setModel).not.toHaveBeenCalled();
  377. expect(app.runtime.emitModelSelected).not.toHaveBeenCalled();
  378. });
  379. it("普通模型切换不把 Host runtimeModel 交给 Provider 就绪检查", async () => {
  380. const app = makeApp();
  381. const record = makeRecord(app);
  382. const captured: string[] = [];
  383. const runtimeModel = {
  384. revision: "model-runtime:1",
  385. generatedAt: 1,
  386. model: { providerId: "prov-b", modelId: "new-model" },
  387. provider: {
  388. providerId: "prov-b",
  389. kind: "openai-chat",
  390. models: [{ modelId: "new-model" }],
  391. },
  392. };
  393. await new V4CommandExecutor(makeResolvingHost(record, { available: true }, captured)).execute(
  394. envelope("s1", {
  395. provider: "prov-b",
  396. model: "new-model",
  397. thought: "",
  398. runtimeModel,
  399. }),
  400. );
  401. expect(captured).toEqual(["prov-b"]);
  402. expect(app.setModel).toHaveBeenCalledWith("prov-b/new-model");
  403. });
  404. it("host 无 ensureProviderAvailable(旧 binder/夹具):跳过能力,同 provider 直切不回归", async () => {
  405. const app = makeApp();
  406. const record = makeRecord(app);
  407. await new V4CommandExecutor(makeHost(record)).execute(
  408. envelope("s1", { provider: "prov-a", model: "new-model", thought: "" }),
  409. );
  410. expect(app.setModel).toHaveBeenCalledWith("prov-a/new-model");
  411. expect(app.runtime.emitModelSelected).toHaveBeenCalled();
  412. });
  413. });
  414. describe("applyRequestedSessionConfig(createSession.config 消费)", () => {
  415. it("模型差异:setModel → setThoughtLevel → emitModelSelected(previous 保真)", async () => {
  416. const app = makeApp();
  417. app.setModel.mockImplementationOnce(async () => {
  418. app.calls.push("setModel");
  419. return { thoughtLevel: "medium" };
  420. });
  421. const record = makeRecord(app);
  422. await applyRequestedSessionConfig(makeHost(record), record, {
  423. provider: "prov-b",
  424. model: "new-model",
  425. thought: "high",
  426. });
  427. expect(app.setModel).toHaveBeenCalledWith("prov-b/new-model");
  428. expect(app.setThoughtLevel).toHaveBeenCalledWith("high");
  429. expect(app.runtime.emitModelSelected).toHaveBeenCalledWith({
  430. modelSelection: {
  431. providerId: "prov-b",
  432. modelId: "new-model",
  433. options: { reasoningLevel: "high" },
  434. },
  435. effectiveReasoningLevel: "high",
  436. previousModelSelection: PREVIOUS_SELECTION,
  437. supportedThoughtLevels: SUPPORTED_THOUGHT_LEVELS,
  438. traceContext: TRACE,
  439. });
  440. });
  441. it("模型差异且 thought 不受目标支持:保留 setModel 实际档位并照常发布目标模型", async () => {
  442. const app = makeApp();
  443. app.setModel.mockImplementationOnce(async () => {
  444. app.calls.push("setModel");
  445. return { thoughtLevel: "max" };
  446. });
  447. const record = makeRecord(app);
  448. await applyRequestedSessionConfig(makeHost(record), record, {
  449. provider: "default-deepseek",
  450. model: "deepseek-v4-flash",
  451. thought: "enabled",
  452. });
  453. expect(app.setThoughtLevel).not.toHaveBeenCalled();
  454. expect(app.runtime.emitModelSelected).toHaveBeenCalledWith({
  455. modelSelection: {
  456. providerId: "default-deepseek",
  457. modelId: "deepseek-v4-flash",
  458. },
  459. effectiveReasoningLevel: "max",
  460. previousModelSelection: PREVIOUS_SELECTION,
  461. supportedThoughtLevels: SUPPORTED_THOUGHT_LEVELS,
  462. traceContext: TRACE,
  463. });
  464. });
  465. it("部分字段(只给 thought):provider/model 回落 runtime 当前值", async () => {
  466. const app = makeApp();
  467. const record = makeRecord(app);
  468. await applyRequestedSessionConfig(makeHost(record), record, { thought: "max" });
  469. expect(app.setModel).not.toHaveBeenCalled();
  470. expect(app.setThoughtLevel).toHaveBeenCalledWith("max");
  471. expect(app.runtime.emitModelSelected).toHaveBeenCalledWith({
  472. modelSelection: {
  473. providerId: "prov-a",
  474. modelId: "old-model",
  475. options: { reasoningLevel: "max" },
  476. },
  477. effectiveReasoningLevel: "max",
  478. previousModelSelection: PREVIOUS_SELECTION,
  479. supportedThoughtLevels: SUPPORTED_THOUGHT_LEVELS,
  480. traceContext: TRACE,
  481. });
  482. });
  483. it("与 runtime 全同值:零调用零事件(不产空转 delta)", async () => {
  484. const app = makeApp();
  485. const record = makeRecord(app);
  486. await applyRequestedSessionConfig(makeHost(record), record, {
  487. provider: "prov-a",
  488. model: "old-model",
  489. thought: "",
  490. mode: "build",
  491. followupMode: "queue",
  492. });
  493. expect(app.setModel).not.toHaveBeenCalled();
  494. expect(app.setMode).not.toHaveBeenCalled();
  495. expect(app.setFollowupMode).not.toHaveBeenCalled();
  496. expect(app.runtime.emitModelSelected).not.toHaveBeenCalled();
  497. expect(app.runtime.emitModeChanged).not.toHaveBeenCalled();
  498. });
  499. it("mode 差异由 Runtime 原子更新;非法 mode 值被值域收口忽略", async () => {
  500. const app = makeApp();
  501. const record = makeRecord(app);
  502. await applyRequestedSessionConfig(makeHost(record), record, { mode: "yolo" });
  503. expect(app.runtime.setExecutionState).toHaveBeenCalledWith({ mode: "yolo" }, TRACE);
  504. expect(app.runtime.emitModeChanged).not.toHaveBeenCalled();
  505. const app2 = makeApp();
  506. const record2 = makeRecord(app2);
  507. await applyRequestedSessionConfig(makeHost(record2), record2, { mode: "auto" });
  508. expect(app2.setMode).not.toHaveBeenCalled();
  509. expect(app2.runtime.emitModeChanged).not.toHaveBeenCalled();
  510. });
  511. it("followupMode=guide 显式写;queue(缺省同值)跳过避免空转事件", async () => {
  512. const app = makeApp();
  513. const record = makeRecord(app);
  514. await applyRequestedSessionConfig(makeHost(record), record, { followupMode: "guide" });
  515. expect(app.setFollowupMode).toHaveBeenCalledWith("guide");
  516. });
  517. });