subagent-listing-assertions.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import assert from "node:assert/strict";
  2. import type { E2ENetworkCaptureArtifact } from "./network-capture-proxy.js";
  3. import type { CapturedPrompt } from "./provider-prefix.js";
  4. const LISTING_HEADING = "Available agent types for the Agent tool:";
  5. const DESCRIPTION_POINTER =
  6. "Available agent types are listed in <system-reminder> messages in the conversation.";
  7. const CONCURRENCY_NOTE =
  8. "When you launch multiple agents for independent work, send them in a single message with multiple tool uses so they run concurrently.";
  9. export function assertSubagentListingTrajectory(
  10. artifact: E2ENetworkCaptureArtifact,
  11. scenario: { parentMarker: string; childMarker: string },
  12. customNames: readonly string[],
  13. mcs: boolean,
  14. ): void {
  15. const records = artifact.records.filter(
  16. (record) => record.status === "complete" && record.path.includes("/messages"),
  17. );
  18. const parents = records.filter((record) => {
  19. const body = JSON.stringify(record.requestJson);
  20. return (
  21. body.includes(scenario.parentMarker) &&
  22. !body.includes("Generate a concise title") &&
  23. !body.includes("create a detailed summary")
  24. );
  25. });
  26. assert.equal(parents.length, 2, "父请求应只有首轮与 Agent tool_result 后续轮");
  27. let originalListing: string | undefined;
  28. for (const record of parents) {
  29. const request = record.requestJson as {
  30. tools: Array<{ name: string; description?: string }>;
  31. messages: Array<{ role: string; content: unknown }>;
  32. system?: unknown;
  33. };
  34. const description = request.tools.find((tool) => tool.name === "Agent")?.description;
  35. assert.ok(description);
  36. assert.ok(description.includes(DESCRIPTION_POINTER));
  37. assert.ok(
  38. description.includes(
  39. "Use SendMessage with the agent's ID to continue a previously spawned agent with its context intact; a new Agent call starts fresh.",
  40. ),
  41. );
  42. assert.ok(
  43. description.includes(
  44. "Each agent type's model, reasoning effort, and tools come from its definition (`.zcode/agents/*.md` frontmatter).",
  45. ),
  46. );
  47. assert.ok(!description.includes("ID or name"));
  48. assert.ok(!description.includes(".claude/agents"));
  49. assert.ok(!description.includes(CONCURRENCY_NOTE));
  50. assert.ok(!description.includes(LISTING_HEADING));
  51. for (const name of customNames) assert.ok(!description.includes(name));
  52. const blocks = readMessageTextBlocks(request);
  53. const listing = blocks.filter((block) => block.text.includes(LISTING_HEADING));
  54. assert.equal(listing.length, 1, "历史 listing 应保留一次,工具后不得重复追加");
  55. assert.equal(listing[0]!.role, mcs ? "system" : "user");
  56. assert.equal(listing[0]!.text.includes("<system-reminder>"), !mcs);
  57. const questionIndex = blocks.findIndex((block) => block.text.includes(scenario.parentMarker));
  58. const listingIndex = blocks.indexOf(listing[0]!);
  59. assert.ok(mcs ? listingIndex > questionIndex : listingIndex < questionIndex);
  60. assertCompleteToolPairs(request.messages);
  61. assert.equal(listing[0]!.text.split(CONCURRENCY_NOTE).length - 1, 1);
  62. for (const name of ["general-purpose", "Explore", ...customNames])
  63. assert.ok(listing[0]!.text.includes(`- ${name}:`));
  64. assert.ok(!JSON.stringify(request.system ?? "").includes(LISTING_HEADING));
  65. const start = listing[0]!.text.indexOf(LISTING_HEADING);
  66. const end = listing[0]!.text.indexOf(CONCURRENCY_NOTE) + CONCURRENCY_NOTE.length;
  67. const listingBody = listing[0]!.text.slice(start, end);
  68. if (originalListing) assert.equal(listingBody, originalListing, "历史目录正文不能改写");
  69. originalListing = listingBody;
  70. }
  71. const children = records.filter((record) => {
  72. const body = JSON.stringify(record.requestJson);
  73. return (
  74. body.includes(scenario.childMarker) &&
  75. !body.includes(scenario.parentMarker) &&
  76. !body.includes("Generate a concise title")
  77. );
  78. });
  79. assert.equal(children.length, 1);
  80. for (const child of children) {
  81. const request = child.requestJson as { tools?: Array<{ name: string }> };
  82. assert.ok(!request.tools?.some((tool) => tool.name === "Agent"));
  83. assert.ok(!JSON.stringify(request).includes(LISTING_HEADING));
  84. }
  85. }
  86. export function readMessageTextBlocks(request: Pick<CapturedPrompt, "messages">) {
  87. return request.messages.flatMap(({ role, content }) =>
  88. (typeof content === "string"
  89. ? [content]
  90. : Array.isArray(content)
  91. ? content.flatMap((block) =>
  92. block?.type === "text" && typeof block.text === "string" ? [block.text] : [],
  93. )
  94. : []
  95. ).map((text: string) => ({ role, text })),
  96. );
  97. }
  98. function assertCompleteToolPairs(messages: Array<{ role: string; content: unknown }>): void {
  99. const pending = new Set<string>();
  100. for (const message of messages) {
  101. if (message.role === "system") assert.equal(pending.size, 0, "MCS 不能插入未完成的工具批次");
  102. if (!Array.isArray(message.content)) continue;
  103. for (const block of message.content) {
  104. if (block.type === "tool_use") pending.add(block.id);
  105. if (block.type === "tool_result") {
  106. assert.ok(pending.delete(block.tool_use_id), "tool_result 必须对应已出现的 tool_use");
  107. }
  108. }
  109. }
  110. assert.equal(pending.size, 0, "请求中工具结果必须完整");
  111. }