subagent-profile-fields.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { describe, expect, it } from "vitest";
  2. import type { SessionId } from "@zcode/contracts";
  3. import type { AgentProfile } from "../src/subagent/profile.js";
  4. import {
  5. setup,
  6. usage,
  7. initial,
  8. done,
  9. spawn,
  10. send,
  11. assertProfile,
  12. } from "./subagent-profile-scenario.js";
  13. describe("single-field profile refresh through parent tools", () => {
  14. it.each(["reasoning", "prompt", "tools"] as const)(
  15. "updates only %s for completed resume and new spawn without changing model",
  16. async (field) => {
  17. const scenario = setup();
  18. await scenario.turn([spawn("ORIGINAL_INPUT")]);
  19. const original = (await scenario.spawned())[0]!;
  20. await scenario.waitCompleted(original.agentId, 1);
  21. const history = structuredClone(
  22. await scenario.store.messages({ sessionID: original.childSessionId }),
  23. );
  24. const timeline = () =>
  25. scenario.store
  26. .messages({ sessionID: original.childSessionId })
  27. .then((messages) =>
  28. messages
  29. .flatMap((m) => m.parts)
  30. .filter((part) => part.type === "timeline" && part.timelineType === "model_change"),
  31. );
  32. const previousTimeline = await timeline();
  33. const updated = initial();
  34. if (field === "reasoning") updated.modelSelection!.options!.reasoningLevel = "high";
  35. if (field === "prompt") updated.systemPrompt = "PROFILE_CHANGED";
  36. if (field === "tools") updated.tools = ["Grep"];
  37. scenario.update(updated);
  38. await scenario.turn([send(original.agentId, "RESUME_INPUT")]);
  39. await scenario.waitCompleted(original.agentId, 2);
  40. const resumed = scenario.children.at(-1)!;
  41. assertProfile(resumed, updated);
  42. expect(resumed.sessionId).toBe(original.childSessionId);
  43. expect(JSON.stringify(resumed.request.messages)).toContain("ORIGINAL_INPUT");
  44. expect(JSON.stringify(resumed.request.messages)).toContain("RESUME_INPUT");
  45. if (field === "prompt")
  46. expect(JSON.stringify(resumed.request.messages)).not.toContain("PROFILE_ORIGINAL");
  47. expect(
  48. (await scenario.store.messages({ sessionID: original.childSessionId })).slice(
  49. 0,
  50. history.length,
  51. ),
  52. ).toEqual(history);
  53. const selection = (
  54. await scenario.store.sessionEntries!({
  55. sessionID: original.childSessionId,
  56. type: "runtime/model_selection",
  57. })
  58. ).at(-1);
  59. expect(selection?.data).toEqual(updated.modelSelection);
  60. expect((await timeline()).length - previousTimeline.length).toBe(
  61. field === "reasoning" ? 1 : 0,
  62. );
  63. if (field === "reasoning")
  64. expect((await timeline()).at(-1)).toMatchObject({
  65. fromModel: { modelId: "fixed", options: { reasoningLevel: "low" } },
  66. toModel: { modelId: "fixed", options: { reasoningLevel: "high" } },
  67. });
  68. await scenario.turn([spawn("NEW_SPAWN")]);
  69. assertProfile(scenario.children.at(-1)!, updated);
  70. expect(scenario.children.at(-1)!.sessionId).not.toBe(original.childSessionId);
  71. // resume 也发布 SubagentSpawned,身份去重后才是实际 child 数量。
  72. expect(new Set((await scenario.spawned()).map((event) => event.childSessionId)).size).toBe(2);
  73. expect(new Set((await scenario.spawned()).map((event) => event.agentId)).size).toBe(2);
  74. },
  75. );
  76. it("keeps running child messages on its launch profile across parent turns, then refreshes terminal resume", async () => {
  77. const blocked = Promise.withResolvers<void>();
  78. const began = Promise.withResolvers<void>();
  79. let runningId: SessionId | undefined,
  80. runningCalls = 0;
  81. const scenario = setup(async (record) => {
  82. runningId ??= record.sessionId;
  83. if (record.sessionId === runningId && ++runningCalls === 1) {
  84. began.resolve();
  85. await blocked.promise;
  86. // 现有 Read 工具形成下一次模型请求;缺文件结果也经过正常工具结果链路。
  87. return {
  88. finishReason: "tool-calls",
  89. text: "",
  90. usage,
  91. toolCalls: [
  92. {
  93. id: "read_running",
  94. name: "Read",
  95. input: { file_path: "/missing-profile-refresh-fixture" },
  96. },
  97. ],
  98. };
  99. }
  100. return done();
  101. });
  102. try {
  103. await scenario.turn([spawn("RUNNING_ORIGINAL", true)]);
  104. await began.promise;
  105. const original = (await scenario.spawned())[0]!;
  106. const updated: AgentProfile = {
  107. ...initial(),
  108. systemPrompt: "PROFILE_NEXT_PARENT",
  109. tools: ["Grep"],
  110. modelSelection: {
  111. providerId: "custom",
  112. modelId: "replacement",
  113. options: { reasoningLevel: "high" },
  114. },
  115. };
  116. scenario.update(updated);
  117. await scenario.turn([send(original.agentId, "RUNNING_MESSAGE"), spawn("NEXT_PARENT_CHILD")]);
  118. expect(scenario.children.filter((r) => r.sessionId === original.childSessionId)).toHaveLength(
  119. 1,
  120. );
  121. assertProfile(
  122. scenario.children.find((r) => r.sessionId !== original.childSessionId)!,
  123. updated,
  124. );
  125. blocked.resolve();
  126. await scenario.waitCompleted(original.agentId, 1);
  127. const continuation = scenario.children
  128. .filter((r) => r.sessionId === original.childSessionId)
  129. .at(-1)!;
  130. assertProfile(continuation, initial());
  131. expect(JSON.stringify(continuation.request.messages)).toContain("RUNNING_MESSAGE");
  132. expect(JSON.stringify(continuation.request.messages)).not.toContain(updated.systemPrompt);
  133. await scenario.turn([send(original.agentId, "TERMINAL_MESSAGE")]);
  134. await scenario.waitCompleted(original.agentId, 2);
  135. const resumed = scenario.children.at(-1)!;
  136. assertProfile(resumed, updated);
  137. expect(resumed.sessionId).toBe(original.childSessionId);
  138. expect(JSON.stringify(resumed.request.messages)).toContain("RUNNING_ORIGINAL");
  139. expect(JSON.stringify(resumed.request.messages)).toContain("TERMINAL_MESSAGE");
  140. } finally {
  141. blocked.resolve();
  142. }
  143. });
  144. });