context-active-model-source.test.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { describe, expect, it } from "vitest";
  2. import type { EnvInfo } from "@zcode/contracts";
  3. import { createContextBuilder } from "../src/context/builder.js";
  4. import { createSubagentContextBuilder } from "../src/subagent/context-builder.js";
  5. import { createTestRuntimeModel } from "./test-runtime-model.js";
  6. const envInfo: EnvInfo = {
  7. cwd: "/workspace",
  8. platform: "linux",
  9. shell: "bash",
  10. osVersion: "Linux",
  11. nodeVersion: "v24",
  12. };
  13. const model = createTestRuntimeModel({
  14. providerId: "actual-provider",
  15. modelId: "shared-name",
  16. generateText: async () => {
  17. throw new Error("context building must not call the provider");
  18. },
  19. });
  20. describe("Context execution model source", () => {
  21. it("shares environment and model sections with subagents and custom system prompts", () => {
  22. const main = createContextBuilder({ envInfo, model, workingDirectory: envInfo.cwd }).build();
  23. const child = createSubagentContextBuilder({
  24. envInfo,
  25. model,
  26. agentPrompt: "Child-only identity.",
  27. }).build();
  28. const custom = createContextBuilder({
  29. envInfo,
  30. model,
  31. workingDirectory: envInfo.cwd,
  32. customSystemPrompt: "Host override.",
  33. }).build();
  34. const runtimeContext = (result: typeof main) =>
  35. result.sections.filter(
  36. (section) => section.source === "env_info" || section.source === "model",
  37. );
  38. expect(runtimeContext(child)).toEqual(runtimeContext(main));
  39. expect(runtimeContext(child).map((section) => section.injectionTarget)).toEqual([
  40. "meta_user",
  41. "meta_user",
  42. ]);
  43. // 自定义 prompt 只替换 system 正文,环境与模型公告照常发送。
  44. expect(runtimeContext(custom)).toEqual(runtimeContext(main));
  45. expect(child.metaUserAttachments).toEqual([]);
  46. const initial = child.systemMessages.map((message) => message.content).join("");
  47. expect(initial).toMatch(
  48. /Child-only identity\.\n\nMessages from the agent that launched you — .+\n\nNotes:/u,
  49. );
  50. expect(initial).not.toContain("# Environment");
  51. expect(initial).not.toContain("<env>");
  52. expect(initial).not.toContain("You are powered by");
  53. expect(
  54. child.sections.some(
  55. (section) =>
  56. section.source === "session_context" ||
  57. section.source === "memory" ||
  58. section.source === "desktop_context",
  59. ),
  60. ).toBe(false);
  61. });
  62. for (const kind of ["main", "subagent"] as const) {
  63. const build = (source: EnvInfo, withModel: boolean) => {
  64. const config = { envInfo: source, model: withModel ? model : undefined };
  65. const result =
  66. kind === "main"
  67. ? createContextBuilder({ ...config, workingDirectory: "/workspace" }).build()
  68. : createSubagentContextBuilder({
  69. ...config,
  70. agentPrompt: "Inspect the workspace.",
  71. }).build();
  72. // 环境与模型身份只产出 section,由运行时宣告。
  73. return [...result.systemMessages, ...result.sections]
  74. .map((item) => String(item.content))
  75. .join("\n");
  76. };
  77. it(`${kind} renders the actual Model without putting model identity into EnvInfo`, () => {
  78. expect(build(envInfo, true)).toContain(
  79. "powered by the model named actual-provider/shared-name",
  80. );
  81. expect(envInfo).not.toHaveProperty("currentModel");
  82. });
  83. it(`${kind} ignores a legacy environment model even when no execution Model exists`, () => {
  84. // 旧持久化数据可能带额外字段;它不能重新成为模型说明的输入。
  85. const legacyEnvironment = { ...envInfo, currentModel: "legacy/wrong-model" };
  86. expect(build(legacyEnvironment, false)).not.toContain("powered by the model named");
  87. expect(build(legacyEnvironment, true)).not.toContain("legacy/wrong-model");
  88. expect(build(legacyEnvironment, true)).toContain("actual-provider/shared-name");
  89. });
  90. }
  91. });