| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- import { afterEach, expect, it, vi } from "vitest";
- import {
- createSessionId,
- createRootTraceContext,
- CompactTrigger,
- ModelErrorCode,
- SessionEventType,
- type Model,
- type ModelRequest,
- type ModelRequestPayload,
- type ModelResult,
- } from "@zcode/contracts";
- import { TurnMachineImpl } from "../src/agent/turn-machine.js";
- import type { AgentRuntimeInternal } from "../src/runtime/internal.js";
- import type { AgentRuntimeConfig } from "../src/runtime/types.js";
- import { createToolRegistry } from "../src/tool/registry.js";
- import { createTestAgentRuntime } from "./test-agent-runtime.js";
- import { createTestModelFactory } from "./test-runtime-model.js";
- import { createTestSessionEventStore } from "./test-event-store.js";
- import {
- countContinuePrompts,
- OUTPUT_TOKEN_CONTINUE_PROMPT,
- registerReadOnlyTool,
- requestText,
- } from "./runtime-output-token-continuation-test-helpers.js";
- const TARGET = "mcp__test__lookup";
- const SUCCESS_MESSAGE = "Tool loaded.";
- const cases = (["native", "local"] as const).flatMap((mode) =>
- (["off", "on"] as const).map((modelStreaming) => ({ mode, modelStreaming })),
- );
- const done = (): ModelResult => ({ text: "done", finishReason: "stop", usage: {} });
- const search = (): ModelResult => ({
- text: "",
- finishReason: "tool-calls",
- usage: {},
- toolCalls: [{ id: "search", name: "ToolSearch", input: { query: `select:${TARGET}` } }],
- });
- const isCompact = (request: ModelRequest) =>
- requestText(request.messages).includes("create a detailed summary");
- const successCount = (messages: ModelRequest["messages"]) =>
- messages.filter((message) => message.content === SUCCESS_MESSAGE).length;
- const withoutCacheControl = (messages: ModelRequest["messages"]) =>
- messages.map(({ cacheControl: _cacheControl, ...message }) => message);
- afterEach(() => vi.restoreAllMocks());
- function fixture(
- config: Partial<AgentRuntimeConfig>,
- respond: (request: ModelRequest, index: number) => ModelResult,
- ) {
- const sessionId = createSessionId("toolsearch-request-recording");
- const eventStore = createTestSessionEventStore();
- const requests: ModelRequest[] = [];
- const generateText = async (request: ModelRequest) => {
- requests.push(structuredClone(request));
- return respond(request, requests.length);
- };
- const baseFactory = createTestModelFactory({
- generateText,
- async *streamText(request) {
- const result = await generateText(request);
- for (const toolCall of result.toolCalls ?? []) yield { type: "tool_call", toolCall };
- if (result.text) {
- yield { type: "text_start", id: "text" };
- yield { type: "text_delta", id: "text", text: result.text };
- yield { type: "text_end", id: "text" };
- }
- yield { type: "finish", finishReason: result.finishReason, usage: result.usage };
- },
- });
- const wrap = (model: Model): Model => ({
- ...model,
- requestCapabilities: { toolSearch: { protocolSupported: true, defaultEnabled: true } },
- bind: (options) => wrap(model.bind(options)),
- });
- const registry = createToolRegistry();
- registerReadOnlyTool(registry, TARGET, "unused");
- const entry = registry.get(TARGET)!;
- registry.register(
- {
- ...entry,
- metadata: {
- ...entry.metadata,
- shouldDefer: true,
- mcpPresentation: { serverName: "test", toolName: "lookup" },
- },
- },
- { silentDuplicateWarning: true },
- );
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- titleGeneration: { enabled: false },
- systemPrompt: "Test request recording.",
- toolAllowlist: ["ToolSearch", "DeferredToolCall", TARGET],
- ...config,
- },
- { eventStore, toolRegistry: registry, modelFactory: (input) => wrap(baseFactory(input)) },
- );
- const recordedRequests = async () =>
- (await eventStore.getEvents(sessionId))
- .filter((event) => event.type === SessionEventType.ModelRequest)
- .map((event) => event.payload as ModelRequestPayload);
- return { runtime, requests, recordedRequests };
- }
- it.each(cases)(
- "retains success hints in continuation records and turn snapshots ($mode, streaming=$modelStreaming)",
- async ({ mode, modelStreaming }) => {
- const snapshots = vi.spyOn(TurnMachineImpl.prototype, "startModelRequest");
- const { runtime, requests, recordedRequests } = fixture(
- { toolSearch: { mode }, modelStreaming },
- (_request, index) =>
- index === 1
- ? search()
- : index <= 4
- ? { text: `partial ${index}`, finishReason: "length", usage: {} }
- : done(),
- );
- try {
- await runtime.executeTurn("Search for the lookup tool.");
- await runtime.executeTurn("Continue the task.");
- expect(requests.map((request) => successCount(request.messages))).toEqual(
- mode === "native" ? [0, 1, 1, 1, 1, 1] : [0, 0, 0, 0, 0, 0],
- );
- expect(requests.map((request) => countContinuePrompts(request.messages))).toEqual([
- 0, 0, 1, 2, 3, 0,
- ]);
- const records = await recordedRequests();
- expect(records).toHaveLength(requests.length);
- for (const [index, request] of requests.entries()) {
- const expected = withoutCacheControl(
- request.messages.filter((message) => message.content !== OUTPUT_TOKEN_CONTINUE_PROMPT),
- );
- expect(withoutCacheControl(records[index]!.messages)).toEqual(expected);
- expect(
- withoutCacheControl(snapshots.mock.results[index]!.value.modelRequest.messages),
- ).toEqual(expected);
- }
- const internal = runtime as unknown as AgentRuntimeInternal;
- expect(JSON.stringify(internal.messageHistory.toRuntimeEntries())).not.toContain(
- SUCCESS_MESSAGE,
- );
- expect(JSON.stringify(records)).not.toContain("loadedToolCallIds");
- } finally {
- await runtime.closeBrowserSession();
- }
- },
- );
- it.each(
- cases.flatMap((testCase) =>
- (["auto", "reactive"] as const).map((trigger) => ({ ...testCase, trigger })),
- ),
- )(
- "retains success hints in $trigger compact records during continuation ($mode, streaming=$modelStreaming)",
- async ({ mode, modelStreaming, trigger }) => {
- let mainCalls = 0;
- const { runtime, requests, recordedRequests } = fixture(
- {
- toolSearch: { mode },
- modelStreaming,
- ...(trigger === "auto"
- ? { compact: { contextWindow: 40000, bufferTokens: 10, summaryReserveTokens: 0 } }
- : {}),
- },
- (request) => {
- if (isCompact(request)) return { ...done(), text: "<summary>Continue the task.</summary>" };
- mainCalls++;
- if (mainCalls === 1) return search();
- if (mainCalls === 4 || mainCalls === 5)
- return {
- text: `partial ${mainCalls}`,
- finishReason: "length",
- usage: {
- inputTokens: trigger === "auto" && mainCalls === 5 ? 39995 : 1,
- outputTokens: 1,
- },
- };
- if (mainCalls === 6 && trigger === "reactive")
- throw Object.assign(new Error("provider context window exceeded"), {
- code: ModelErrorCode.ModelContextExceeded,
- });
- return done();
- },
- );
- try {
- await runtime.executeTurn("Search for the lookup tool.");
- await runtime.executeTurn("Prepare the next step.");
- const result = await runtime.executeTurn("Continue the task.");
- expect(result.response).toBe("done");
- const compactRequests = requests.filter(isCompact);
- expect(compactRequests).toHaveLength(1);
- const actual = compactRequests[0]!;
- // 两次续写让 Continue 进入待总结历史,真正覆盖 compact 的记录重建分支。
- expect(countContinuePrompts(actual.messages)).toBeGreaterThan(0);
- expect(successCount(actual.messages)).toBe(mode === "native" ? 1 : 0);
- const records = await recordedRequests();
- expect(records.every((record) => countContinuePrompts(record.messages) === 0)).toBe(true);
- const compactRecords = records.filter((record) => record.querySource === "compact");
- expect(compactRecords).toHaveLength(1);
- // 摘要指令紧跟 Continue 时,按相邻 user 规则在 Continue 末尾补换行;比较时忽略这个分隔。
- expect(withoutCacheControl(compactRecords[0]!.messages)).toEqual(
- withoutCacheControl(
- actual.messages.filter(
- (message) =>
- typeof message.content !== "string" ||
- message.content.replace(/\n$/u, "") !== OUTPUT_TOKEN_CONTINUE_PROMPT,
- ),
- ),
- );
- } finally {
- await runtime.closeBrowserSession();
- }
- },
- );
- it.each(cases)(
- "keeps repeated-search feedback stable in Continue, records and snapshots ($mode/$modelStreaming)",
- async ({ mode, modelStreaming }) => {
- const snapshots = vi.spyOn(TurnMachineImpl.prototype, "startModelRequest");
- const { runtime, requests, recordedRequests } = fixture(
- { toolSearch: { mode }, modelStreaming },
- (_request, index) => {
- if (index === 1) return search();
- if (index === 3)
- return {
- ...search(),
- toolCalls: [{ id: "repeat", name: "ToolSearch", input: { query: "select:lookup" } }],
- };
- if (index === 4) return { text: "partial", finishReason: "length", usage: {} };
- return done();
- },
- );
- try {
- await runtime.executeTurn("Load the lookup tool.");
- await runtime.executeTurn("Find lookup again using its short name.");
- expect(requests.map((r) => successCount(r.messages))).toEqual(
- mode === "local" ? [0, 0, 0, 0, 0] : [0, 1, 1, 2, 2],
- );
- const latest = requests.at(-1)!;
- const repeat = latest.messages.find((m) => m.role === "tool" && m.toolCallId === "repeat");
- expect(JSON.stringify(repeat)).toContain(
- mode === "local" ? "already available" : "tool_reference",
- );
- if (mode === "local") expect(JSON.stringify(repeat)).not.toContain("<functions>");
- const records = await recordedRequests();
- for (const [index, request] of requests.entries()) {
- const expected = withoutCacheControl(
- request.messages.filter((m) => m.content !== OUTPUT_TOKEN_CONTINUE_PROMPT),
- );
- expect(withoutCacheControl(records[index]!.messages)).toEqual(expected);
- expect(
- withoutCacheControl(snapshots.mock.results[index]!.value.modelRequest.messages),
- ).toEqual(expected);
- }
- } finally {
- await runtime.closeBrowserSession();
- }
- },
- );
- it.each([CompactTrigger.Manual, CompactTrigger.Reactive])(
- "promotes discovery at successful %s compact and keeps it across the next compact",
- async (trigger) => {
- const { runtime, requests } = fixture({ toolSearch: { mode: "local" } }, (request, index) =>
- isCompact(request)
- ? { ...done(), text: "<summary>Continue.</summary>" }
- : index === 1
- ? search()
- : done(),
- );
- try {
- await runtime.executeTurn("Load lookup.");
- await runtime.executeTurn("Keep working.");
- const internal = runtime as unknown as AgentRuntimeInternal;
- for (let pass = 0; pass < 2; pass++) {
- const result = await internal.compactActiveConversation(
- undefined,
- createRootTraceContext(),
- [],
- { trigger },
- );
- expect(result.outcome).toBe("compacted");
- // 返回时公告就应使用提升后的分类,而不是等待下一次正常收集纠正。
- expect(
- result.entries
- .filter((e) => e.metadata?.toolSearchAnnouncement)
- .flatMap((e) => e.metadata!.toolSearchAnnouncement!.addedNames),
- ).not.toContain(TARGET);
- await runtime.executeTurn("Continue after compact.");
- const request = requests.at(-1)!;
- expect(request.tools?.find((t) => t.name === TARGET)).toMatchObject({ name: TARGET });
- expect(request.tools?.find((t) => t.name === TARGET)?.deferLoading).toBeUndefined();
- expect(request.tools?.map((t) => t.name)).toContain("DeferredToolCall");
- }
- } finally {
- await runtime.closeBrowserSession();
- }
- },
- );
- it.each(["cancel", "persist failure"])(
- "does not promote Local tools on compact %s",
- async (failure) => {
- const controller = new AbortController();
- const { runtime, requests } = fixture({ toolSearch: { mode: "local" } }, (request, index) => {
- if (isCompact(request)) {
- if (failure === "cancel") controller.abort();
- return { ...done(), text: "<summary>Candidate.</summary>" };
- }
- return index === 1 ? search() : done();
- });
- try {
- await runtime.executeTurn("Load lookup.");
- await runtime.executeTurn("Keep working.");
- const internal = runtime as unknown as AgentRuntimeInternal;
- const before = internal.messageHistory.toRuntimeEntries();
- if (failure === "persist failure")
- vi.spyOn(internal, "persistCompactSummary").mockRejectedValue(new Error("persist failed"));
- await expect(
- internal.compactActiveConversation(undefined, createRootTraceContext(), [], {
- abortSignal: controller.signal,
- }),
- ).rejects.toThrow();
- expect(internal.messageHistory.toRuntimeEntries()).toEqual(before);
- await runtime.executeTurn("Still use the search result.");
- expect(requests.at(-1)!.tools?.some((t) => t.name === TARGET)).toBe(false);
- } finally {
- await runtime.closeBrowserSession();
- }
- },
- );
|