| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202 |
- import { describe, expect, it, vi } from "vitest";
- import {
- HookEventName,
- SessionEventType,
- createSessionId,
- createTurnId,
- modelMessageContentToText,
- type BackgroundResultOriginMeta,
- type CreateSessionInput,
- type ModelMessageContent,
- type MessageInfo,
- type MessagePart,
- type MessageWithParts,
- type PermissionRuleset,
- type SessionEvent,
- type SessionGoal,
- type SessionId,
- type SessionInfo,
- type SessionStorePort,
- type TodoItem,
- } from "@zcode/contracts";
- import { formatTaskNotification } from "../src/runtime-task/notification.js";
- import {
- formatLocalAgentTaskNotification,
- localAgentSubagentResult,
- } from "../src/subagent/completion-notification.js";
- import { createTestAgentRuntime } from "./test-agent-runtime.js";
- import { createRuntimeCommandId } from "../src/runtime/command-queue.js";
- import type { PromptRuntimeCommand } from "../src/runtime/command-queue.js";
- import { createTurnFailureError } from "../src/runtime/helpers/turn-errors.js";
- import { createInMemoryHookRunner } from "../src/hooks/index.js";
- import { createExploreSubagentPort } from "../src/subagent/runner.js";
- import { createTestSessionEventStore } from "./test-event-store.js";
- import { createTestModelFactory } from "./test-runtime-model.js";
- // 后台 agent 通知的固定 <note>;测试里写死,锁定字节。
- const LOCAL_AGENT_NOTE =
- "A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.";
- describe("subagent background notifications", () => {
- it("formats local_agent task notification as user-role XML", () => {
- const notification = formatTaskNotification({
- outputFile: "/tmp/zcode-agents/sess/agent/output.txt",
- result: "child result",
- status: "completed",
- summary: 'Agent "Audit tests" completed',
- taskId: "agent_123",
- taskType: "local_agent",
- toolUseId: "toolu_123",
- usage: {
- durationMs: 42,
- toolUseCount: 2,
- totalTokens: 9,
- },
- });
- expect(notification).toBe(
- [
- "<task-notification>",
- "<task-id>agent_123</task-id>",
- "<tool-use-id>toolu_123</tool-use-id>",
- "<output-file>/tmp/zcode-agents/sess/agent/output.txt</output-file>",
- "<status>completed</status>",
- '<summary>Agent "Audit tests" completed</summary>',
- `<note>${LOCAL_AGENT_NOTE}</note>`,
- "<result>child result</result>",
- "<usage><subagent_tokens>9</subagent_tokens><tool_uses>2</tool_uses><duration_ms>42</duration_ms></usage>",
- "</task-notification>",
- ].join("\n"),
- );
- });
- it("omits unavailable local_agent usage fields", () => {
- const notification = formatTaskNotification({
- outputFile: "/tmp/output.txt",
- status: "completed",
- summary: "Agent completed",
- taskId: "agent_456",
- taskType: "local_agent",
- usage: {
- durationMs: 25,
- },
- });
- expect(notification).toContain("<usage><duration_ms>25</duration_ms></usage>");
- expect(notification).not.toContain("subagent_tokens");
- expect(notification).not.toContain("tool_uses");
- });
- // summary 措辞、每条都带 <note>、只转义 & < >、不输出 <error>;
- // failed / killed 不带 usage,killed 按停止者措辞。
- it("formats completed, failed, and killed local-agent notifications with the terminal notification shape", () => {
- const base = {
- agentId: "agent_helper",
- description: 'Review "auth" changes',
- outputFile: "/tmp/agent_helper/output.txt",
- parentToolCallId: "toolu_helper",
- totalDurationMs: 12,
- };
- const header = [
- "<task-notification>",
- "<task-id>agent_helper</task-id>",
- "<tool-use-id>toolu_helper</tool-use-id>",
- "<output-file>/tmp/agent_helper/output.txt</output-file>",
- ];
- const note = `<note>${LOCAL_AGENT_NOTE}</note>`;
- const completed = formatLocalAgentTaskNotification({
- ...base,
- result: 'It\'s done <ok> & "clean".',
- status: "completed",
- totalTokens: 7,
- totalToolUseCount: 1,
- });
- const failed = formatLocalAgentTaskNotification({
- ...base,
- error: "child failed",
- status: "failed",
- });
- const failedWithoutReason = formatLocalAgentTaskNotification({
- ...base,
- error: "",
- status: "failed",
- });
- const stoppedByModel = formatLocalAgentTaskNotification({
- ...base,
- status: "killed",
- stoppedBy: "model",
- });
- const stoppedByUser = formatLocalAgentTaskNotification({
- ...base,
- status: "killed",
- stoppedBy: "user",
- });
- const stoppedBySystem = formatLocalAgentTaskNotification({
- ...base,
- status: "killed",
- });
- expect(completed).toBe(
- [
- ...header,
- "<status>completed</status>",
- '<summary>Agent "Review "auth" changes" finished</summary>',
- note,
- '<result>It\'s done <ok> & "clean".</result>',
- "<usage><subagent_tokens>7</subagent_tokens><tool_uses>1</tool_uses><duration_ms>12</duration_ms></usage>",
- "</task-notification>",
- ].join("\n"),
- );
- expect(failed).toBe(
- [
- ...header,
- "<status>failed</status>",
- '<summary>Agent "Review "auth" changes" failed: child failed</summary>',
- note,
- "</task-notification>",
- ].join("\n"),
- );
- expect(failedWithoutReason).toContain(
- '<summary>Agent "Review "auth" changes" failed: Unknown error</summary>',
- );
- // 只有空白的失败原因与空串相同:summary 和结构化 error 都用 Unknown error。
- const blankError = { error: " \n\t", status: "failed" as const };
- expect(formatLocalAgentTaskNotification({ ...base, ...blankError })).toContain(
- '<summary>Agent "Review "auth" changes" failed: Unknown error</summary>',
- );
- expect(localAgentSubagentResult(blankError)).toEqual({
- status: "failed",
- error: "Unknown error",
- });
- // 结构化 error 进入每一帧快照,需要上限;截断点落在代理对中间时丢掉半个字符。
- const longError = `${"x".repeat(1998)}😀${"y".repeat(600)}`;
- const longResult = localAgentSubagentResult({ error: longError, status: "failed" });
- expect(longResult.error).toBe(`${"x".repeat(1998)}…`);
- expect(
- formatLocalAgentTaskNotification({ ...base, error: longError, status: "failed" }),
- ).toContain(longError);
- expect(stoppedByModel).toBe(
- [
- ...header,
- "<status>killed</status>",
- '<summary>Agent "Review "auth" changes" was stopped by ZCode</summary>',
- note,
- "</task-notification>",
- ].join("\n"),
- );
- expect(stoppedByUser).toContain(
- '<summary>Agent "Review "auth" changes" was stopped by user</summary>',
- );
- expect(stoppedBySystem).toContain(
- '<summary>Agent "Review "auth" changes" was stopped</summary>',
- );
- });
- it("drains pending background notifications as strict snapshot batches", async () => {
- const sessionId = createSessionId("background-notification-idle-wake");
- const sessionStore = new RecordingSessionStore();
- const eventStore = createTestSessionEventStore();
- const hookEvents: string[] = [];
- const requestMessages: Array<readonly { content: ModelMessageContent; role?: string }[]> = [];
- const firstRequestMayFinish = createDeferred<void>();
- const secondRequestMayFinish = createDeferred<void>();
- let requestCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- workingDirectory: "/tmp/zcode-background-notification-idle-wake",
- },
- {
- eventStore,
- hookRunner: createInMemoryHookRunner({
- hooks: [
- {
- event: HookEventName.UserPromptSubmit,
- callback: async (input) => {
- hookEvents.push(input.prompt);
- return {
- hookSpecificOutput: {
- hookEventName: HookEventName.UserPromptSubmit,
- },
- };
- },
- },
- ],
- }),
- modelFactory: createTestModelFactory({
- async generateText(request: {
- messages: readonly { content: ModelMessageContent; role?: string }[];
- }) {
- requestCount += 1;
- requestMessages.push(request.messages);
- if (requestCount === 1) await firstRequestMayFinish.promise;
- if (requestCount === 2) await secondRequestMayFinish.promise;
- return modelTextResult(`processed background notification ${requestCount}`);
- },
- } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as {
- enqueueBackgroundTaskNotification(notification: {
- originMeta: {
- backgroundSource: "bash" | "subagent";
- title: string;
- workId: string;
- };
- taskId?: string;
- text: string;
- traceContext: {
- traceId: string;
- spanId: string;
- sessionId: SessionId;
- };
- }): void;
- };
- const traceContext = {
- traceId: "trace_background_idle_wake",
- spanId: "span_background_idle_wake",
- sessionId,
- };
- runtime.enqueueBackgroundTaskNotification({
- originMeta: {
- backgroundSource: "subagent",
- title: "Inspect renderer state",
- workId: "agent_idle_1",
- },
- taskId: "agent_idle_1",
- text: "<task-notification>\n<task-id>agent_idle_1</task-id>\n</task-notification>",
- traceContext,
- });
- await waitForCondition(() => requestMessages.length === 1);
- runtime.enqueueBackgroundTaskNotification({
- originMeta: {
- backgroundSource: "bash",
- title: "pnpm test",
- workId: "agent_idle_2",
- },
- taskId: "agent_idle_2",
- text: "<task-notification>\n<task-id>agent_idle_2</task-id>\n</task-notification>",
- traceContext,
- });
- runtime.enqueueBackgroundTaskNotification({
- originMeta: {
- backgroundSource: "subagent",
- title: "Inspect service state",
- workId: "agent_idle_3",
- },
- taskId: "agent_idle_3",
- text: "<task-notification>\n<task-id>agent_idle_3</task-id>\n</task-notification>",
- traceContext,
- });
- firstRequestMayFinish.resolve(undefined);
- await waitForCondition(() => requestMessages.length === 2);
- runtime.enqueueBackgroundTaskNotification({
- originMeta: {
- backgroundSource: "bash",
- title: "pnpm lint",
- workId: "agent_idle_4",
- },
- taskId: "agent_idle_4",
- text: "<task-notification>\n<task-id>agent_idle_4</task-id>\n</task-notification>",
- traceContext,
- });
- secondRequestMayFinish.resolve(undefined);
- await waitForCondition(() => requestMessages.length === 3);
- const notificationInputs = requestMessages.map((messages) =>
- [...messages]
- .reverse()
- .find((message) =>
- modelMessageContentToText(message.content).includes("<task-notification>"),
- ),
- );
- const notificationTexts = notificationInputs.map((message) =>
- modelMessageContentToText(message?.content),
- );
- expect(notificationTexts[0]).toContain("<task-id>agent_idle_1</task-id>");
- expect(notificationTexts[0]).not.toContain("<task-id>agent_idle_2</task-id>");
- expect(notificationTexts[1]).toContain("<task-id>agent_idle_2</task-id>");
- expect(notificationTexts[1]).toContain("<task-id>agent_idle_3</task-id>");
- expect(notificationTexts[1]).not.toContain("<task-id>agent_idle_4</task-id>");
- expect(notificationTexts[2]).toContain("<task-id>agent_idle_4</task-id>");
- expect(notificationTexts[2]).not.toContain("<task-id>agent_idle_3</task-id>");
- const firstNotificationMessage = notificationInputs[0];
- expect(firstNotificationMessage?.role).toBe("user");
- expect(modelMessageContentToText(firstNotificationMessage?.content)).toContain(
- "<system-reminder>",
- );
- expect(hookEvents).toEqual([]);
- expect(requestCount).toBe(3);
- const backgroundMessages = sessionStore.savedMessages.filter(
- (message) => message.source === "background_task",
- );
- // 每批一条消息;第二批的两条通知是同一条消息的两个文本 part(存储没有事务入口时逐条写入)。
- const notification = (id: number) =>
- `<task-notification>\n<task-id>agent_idle_${id}</task-id>\n</task-notification>`;
- expect(
- sessionStore.savedParts
- .filter(
- (part) =>
- part.type === "text" &&
- part.metadata?.source === "background_task" &&
- part.metadata?.visibility === "model-only",
- )
- .map((part) => [part.messageID, part.type === "text" ? part.text : ""]),
- ).toEqual([
- [backgroundMessages[0]!.id, notification(1)],
- [backgroundMessages[1]!.id, notification(2)],
- [backgroundMessages[1]!.id, notification(3)],
- [backgroundMessages[2]!.id, notification(4)],
- ]);
- expect(backgroundMessages.map((message) => message.metadata?.originMeta)).toEqual([
- {
- backgroundSource: "subagent",
- title: "Inspect renderer state",
- workId: "agent_idle_1",
- },
- {
- backgroundSource: "bash",
- title: "pnpm test · Inspect service state",
- workId: "agent_idle_2",
- },
- {
- backgroundSource: "bash",
- title: "pnpm lint",
- workId: "agent_idle_4",
- },
- ]);
- const liveBackgroundOriginMetas = (await eventStore.getEvents(sessionId))
- .filter((event) => event.type === SessionEventType.TurnStarted)
- .map(
- (event) =>
- event.payload as {
- inputSource?: string;
- originMeta?: {
- backgroundSource: "bash" | "subagent";
- title: string;
- workId: string;
- };
- },
- )
- .filter((payload) => payload.inputSource === "background_task")
- .map((payload) => payload.originMeta);
- expect(liveBackgroundOriginMetas).toEqual(
- backgroundMessages.map((message) => message.metadata?.originMeta),
- );
- const wakeTurns = (await eventStore.getEvents(sessionId))
- .filter((event) => event.type === SessionEventType.TurnStarted)
- .map(
- (event) => event.payload as { inputSource?: string; inputId?: string; messageId?: string },
- )
- .filter((payload) => payload.inputSource === "background_task");
- expect(wakeTurns).toHaveLength(3);
- // 这一轮的输入文本仍以 "\n\n" 相连,App 据此逐条解析任务卡片。
- expect(wakeTurns.map((turn) => (turn as { input?: string }).input)).toEqual([
- notification(1),
- `${notification(2)}\n\n${notification(3)}`,
- notification(4),
- ]);
- expect(new Set(wakeTurns.map((turn) => turn.inputId)).size).toBe(3);
- for (const turn of wakeTurns) {
- expect(turn.inputId).toMatch(
- /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,
- );
- expect(turn.inputId).not.toBe(turn.messageId);
- expect(backgroundMessages.some((message) => message.id === turn.messageId)).toBe(true);
- }
- expect(sessionStore.savedSessionInputs).toHaveLength(4);
- expect(sessionStore.promotedSessionInputs).toHaveLength(4);
- expect(sessionStore.promotedSessionInputs.map((input) => input.id)).toEqual(
- sessionStore.savedSessionInputs.map((input) => input.id),
- );
- expect(sessionStore.promotedSessionInputs[1]?.promotedMessageID).toBe(
- sessionStore.promotedSessionInputs[2]?.promotedMessageID,
- );
- expect(sessionStore.promotedSessionInputs[0]?.promotedMessageID).not.toBe(
- sessionStore.promotedSessionInputs[1]?.promotedMessageID,
- );
- expect(sessionStore.promotedSessionInputs[2]?.promotedMessageID).not.toBe(
- sessionStore.promotedSessionInputs[3]?.promotedMessageID,
- );
- });
- it("reuses ordinary origin metadata for a composed title without guessing missing members", async () => {
- const scenarios: Array<{
- expectedBackgroundSource?: "bash" | "subagent" | "workflow";
- expectedOriginMeta?: BackgroundResultOriginMeta;
- expectedBackgroundSubagentResultConsumed?: boolean;
- /** 批里有 dynamic-workflow run 的通知(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md「Token telemetry for subagents」)。 */
- expectedWorkflowResultConsumed?: boolean;
- id: string;
- originMetas: Array<BackgroundResultOriginMeta | undefined>;
- }> = [
- {
- // 整批都是 dynamic-workflow run 的通知:backgroundSource 冻结为 workflow,只置 wf 维度。
- expectedBackgroundSource: "workflow",
- expectedOriginMeta: {
- backgroundSource: "workflow",
- title: "Nightly report · Weekly digest",
- workId: "dwfrun-title-1",
- },
- expectedWorkflowResultConsumed: true,
- id: "all-workflow",
- originMetas: [
- { backgroundSource: "workflow", title: "Nightly report", workId: "dwfrun-title-1" },
- { backgroundSource: "workflow", title: "Weekly digest", workId: "dwfrun-title-2" },
- ],
- },
- {
- // 混批:来源不一致时 backgroundSource 不冻结(既有规则),但两个维度各自按成员置位。
- expectedOriginMeta: {
- backgroundSource: "workflow",
- title: "Nightly report · Inspect service",
- workId: "dwfrun-title-3",
- },
- expectedBackgroundSubagentResultConsumed: true,
- expectedWorkflowResultConsumed: true,
- id: "workflow-and-subagent",
- originMetas: [
- { backgroundSource: "workflow", title: "Nightly report", workId: "dwfrun-title-3" },
- {
- backgroundSource: "subagent",
- title: "Inspect service",
- workId: "agent-title-9",
- },
- ],
- },
- {
- expectedOriginMeta: {
- backgroundSource: "subagent",
- title: "Inspect renderer · Run tests · Inspect service · +1",
- workId: "agent-title-1",
- },
- expectedBackgroundSubagentResultConsumed: true,
- id: "complete-metadata",
- originMetas: [
- {
- backgroundSource: "subagent",
- title: "Inspect renderer",
- workId: "agent-title-1",
- },
- { backgroundSource: "bash", title: "Run tests", workId: "bash-title-2" },
- {
- backgroundSource: "subagent",
- title: "Inspect service",
- workId: "agent-title-3",
- },
- { backgroundSource: "bash", title: "Run lint", workId: "bash-title-4" },
- ],
- },
- {
- expectedOriginMeta: {
- backgroundSource: "bash",
- title: "Run tests · Inspect renderer · Run lint · +1",
- workId: "bash-title-1",
- },
- expectedBackgroundSubagentResultConsumed: true,
- id: "complete-metadata-reversed",
- originMetas: [
- { backgroundSource: "bash", title: "Run tests", workId: "bash-title-1" },
- {
- backgroundSource: "subagent",
- title: "Inspect renderer",
- workId: "agent-title-2",
- },
- { backgroundSource: "bash", title: "Run lint", workId: "bash-title-3" },
- {
- backgroundSource: "subagent",
- title: "Inspect service",
- workId: "agent-title-4",
- },
- ],
- },
- {
- expectedBackgroundSource: "subagent",
- expectedOriginMeta: {
- backgroundSource: "subagent",
- title: "Inspect renderer · Inspect service",
- workId: "agent-title-5",
- },
- expectedBackgroundSubagentResultConsumed: true,
- id: "all-subagent",
- originMetas: [
- {
- backgroundSource: "subagent",
- title: "Inspect renderer",
- workId: "agent-title-5",
- },
- {
- backgroundSource: "subagent",
- title: "Inspect service",
- workId: "agent-title-6",
- },
- ],
- },
- {
- expectedBackgroundSource: "bash",
- expectedOriginMeta: {
- backgroundSource: "bash",
- title: "Run tests · Run lint",
- workId: "bash-title-7",
- },
- id: "all-bash",
- originMetas: [
- { backgroundSource: "bash", title: "Run tests", workId: "bash-title-7" },
- { backgroundSource: "bash", title: "Run lint", workId: "bash-title-8" },
- ],
- },
- {
- expectedBackgroundSubagentResultConsumed: true,
- id: "missing-metadata",
- originMetas: [
- {
- backgroundSource: "subagent",
- title: "Inspect renderer",
- workId: "agent-title-legacy-1",
- },
- undefined,
- ],
- },
- ];
- for (const scenario of scenarios) {
- const sessionId = createSessionId(`background-title-${scenario.id}`);
- const sessionStore = new RecordingSessionStore();
- const eventStore = createTestSessionEventStore();
- let requestCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- { workingDirectory: `/tmp/zcode-background-title-${scenario.id}` },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText() {
- requestCount += 1;
- return modelTextResult("processed notification metadata batch");
- },
- } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as AgentRuntime & {
- drainRuntimeCommandQueue(): Promise<void>;
- enqueueBackgroundTaskNotification(notification: {
- originMeta?: BackgroundResultOriginMeta;
- taskId: string;
- text: string;
- traceContext: {
- sessionId: SessionId;
- spanId: string;
- traceId: string;
- };
- }): void;
- runtimeCommandDrainActive: boolean;
- };
- const traceContext = {
- sessionId,
- spanId: `span_${scenario.id}`,
- traceId: `trace_${scenario.id}`,
- };
- runtime.runtimeCommandDrainActive = true;
- scenario.originMetas.forEach((originMeta, index) => {
- const taskId = `${scenario.id}-${index + 1}`;
- runtime.enqueueBackgroundTaskNotification({
- ...(originMeta ? { originMeta } : {}),
- taskId,
- text: `<task-notification><task-id>${taskId}</task-id></task-notification>`,
- traceContext,
- });
- });
- runtime.runtimeCommandDrainActive = false;
- await runtime.drainRuntimeCommandQueue();
- expect(requestCount).toBe(1);
- const backgroundMessages = sessionStore.savedMessages.filter(
- (message) => message.source === "background_task",
- );
- expect(backgroundMessages).toHaveLength(1);
- expect(backgroundMessages[0]?.metadata?.originMeta).toEqual(scenario.expectedOriginMeta);
- const turnStarts = (await eventStore.getEvents(sessionId))
- .filter((event) => event.type === SessionEventType.TurnStarted)
- .map(
- (event) =>
- event.payload as {
- backgroundSource?: "bash" | "subagent" | "workflow";
- inputSource?: string;
- originMeta?: BackgroundResultOriginMeta;
- },
- )
- .filter((payload) => payload.inputSource === "background_task");
- expect(turnStarts).toHaveLength(1);
- expect(turnStarts[0]?.originMeta).toEqual(scenario.expectedOriginMeta);
- expect(turnStarts[0]?.backgroundSource).toBe(scenario.expectedBackgroundSource);
- const turnCompletes = (await eventStore.getEvents(sessionId))
- .filter((event) => event.type === SessionEventType.TurnComplete)
- .map(
- (event) =>
- event.payload as {
- backgroundSubagentResultConsumed?: boolean;
- workflowResultConsumed?: boolean;
- },
- );
- expect(turnCompletes).toHaveLength(1);
- expect(turnCompletes[0]?.backgroundSubagentResultConsumed).toBe(
- scenario.expectedBackgroundSubagentResultConsumed,
- );
- expect(turnCompletes[0]?.workflowResultConsumed).toBe(
- scenario.expectedWorkflowResultConsumed,
- );
- }
- });
- it.each(["provider failure", "stop"] as const)(
- "keeps a claimed notification batch durable without command requeue after %s",
- async (terminalOutcome) => {
- const sessionId = createSessionId(`background-notification-${terminalOutcome}`);
- const sessionStore = new RecordingSessionStore();
- const firstRequestMayFinish = createDeferred<void>();
- const secondRequestStarted = createDeferred<AbortSignal | undefined>();
- const requestMessages: Array<readonly { content: ModelMessageContent }[]> = [];
- let requestCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- workingDirectory: `/tmp/zcode-background-notification-${terminalOutcome}`,
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(request: {
- abortSignal?: AbortSignal;
- messages: readonly { content: ModelMessageContent }[];
- }) {
- requestCount += 1;
- requestMessages.push(request.messages);
- if (requestCount === 1) {
- await firstRequestMayFinish.promise;
- return modelTextResult("processed first background notification");
- }
- if (requestCount === 2) {
- secondRequestStarted.resolve(request.abortSignal);
- if (terminalOutcome === "stop") {
- await waitForAbort(request.abortSignal);
- throw request.abortSignal?.reason ?? new Error("notification batch stopped");
- }
- throw new Error("notification batch provider failure");
- }
- return modelTextResult("continued after terminal notification batch");
- },
- } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as AgentRuntime & {
- enqueueBackgroundTaskNotification(notification: {
- taskId?: string;
- text: string;
- traceContext: {
- traceId: string;
- spanId: string;
- sessionId: SessionId;
- };
- }): void;
- };
- const traceContext = {
- traceId: `trace_background_notification_${terminalOutcome}`,
- spanId: `span_background_notification_${terminalOutcome}`,
- sessionId,
- };
- runtime.enqueueBackgroundTaskNotification({
- taskId: "agent_terminal_1",
- text: "<task-notification><task-id>agent_terminal_1</task-id></task-notification>",
- traceContext,
- });
- await waitForCondition(() => requestCount === 1);
- runtime.enqueueBackgroundTaskNotification({
- taskId: "agent_terminal_2",
- text: "<task-notification><task-id>agent_terminal_2</task-id></task-notification>",
- traceContext,
- });
- runtime.enqueueBackgroundTaskNotification({
- taskId: "agent_terminal_3",
- text: "<task-notification><task-id>agent_terminal_3</task-id></task-notification>",
- traceContext,
- });
- firstRequestMayFinish.resolve(undefined);
- await secondRequestStarted.promise;
- if (terminalOutcome === "stop") {
- expect(
- runtime.stopActiveForegroundExecution({ reason: "stop notification batch test" }),
- ).toMatchObject({ kind: "stopped" });
- }
- const continuation = await runtime.executeTurn("continue after terminal notification batch");
- expect(continuation.response).toBe("continued after terminal notification batch");
- expect(requestCount).toBe(3);
- const secondRequestText = providerMessagesToText(requestMessages[1]!);
- expect(secondRequestText).toContain("<task-id>agent_terminal_2</task-id>");
- expect(secondRequestText).toContain("<task-id>agent_terminal_3</task-id>");
- const continuationText = providerMessagesToText(requestMessages[2]!);
- expect(continuationText.match(/<task-id>agent_terminal_2<\/task-id>/g) ?? []).toHaveLength(1);
- expect(continuationText.match(/<task-id>agent_terminal_3<\/task-id>/g) ?? []).toHaveLength(1);
- expect(
- sessionStore.savedMessages.filter((message) => message.source === "background_task"),
- ).toHaveLength(2);
- expect(sessionStore.promotedSessionInputs).toHaveLength(3);
- expect(sessionStore.promotedSessionInputs[1]?.promotedMessageID).toBe(
- sessionStore.promotedSessionInputs[2]?.promotedMessageID,
- );
- },
- );
- it("wakes idle background notifications without scheduling a timeout", async () => {
- const sessionId = createSessionId("background-notification-idle-wake-ref");
- const sessionStore = new RecordingSessionStore();
- const firstRequest = createDeferred<readonly { content: ModelMessageContent }[]>();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- workingDirectory: "/tmp/zcode-background-notification-idle-wake-ref",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(request: { messages: readonly { content: ModelMessageContent }[] }) {
- firstRequest.resolve(request.messages);
- return modelTextResult("processed referenced background notification");
- },
- } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as {
- enqueueBackgroundTaskNotification(notification: {
- text: string;
- traceContext: {
- traceId: string;
- spanId: string;
- sessionId: SessionId;
- };
- }): void;
- };
- const traceContext = {
- traceId: "trace_background_idle_wake_ref",
- spanId: "span_background_idle_wake_ref",
- sessionId,
- };
- const timeoutRecorder = recordSetTimeoutCalls();
- runtime.enqueueBackgroundTaskNotification({
- text: "<task-notification>\n<task-id>agent_idle_ref</task-id>\n</task-notification>",
- traceContext,
- });
- timeoutRecorder.restore();
- const requestMessages = await firstRequest.promise;
- expect(providerMessagesToText(requestMessages)).toContain("<task-id>agent_idle_ref</task-id>");
- expect(timeoutRecorder.calls).toEqual([]);
- });
- it("保留 loopState 初始化前取消的 background subagent composition 标记", async () => {
- const sessionId = createSessionId("background-notification-pre-loop-cancel");
- const eventStore = createTestSessionEventStore();
- const setupStarted = createDeferred<void>();
- const setupMayFinish = createDeferred<void>();
- const abortController = new AbortController();
- const runtime = createTestAgentRuntime(
- sessionId,
- { workingDirectory: "/tmp/zcode-background-notification-pre-loop-cancel" },
- {
- eventStore,
- modelAdapter: {
- async generateText() {
- throw new Error("provider request must not start");
- },
- } as never,
- },
- );
- (runtime as any).runUserPromptSubmitHooks = async (...args: any[]) => {
- setupStarted.resolve();
- await setupMayFinish.promise;
- throw args[3]?.reason ?? new Error("cancelled before loop state");
- };
- const turn = (runtime as any).executeTurnCommand("consume background result", undefined, {
- abortSignal: abortController.signal,
- backgroundSubagentResultConsumed: true,
- inputSource: "background_task",
- inputVisibility: "model-only",
- recordedInputMessageId: "background-result-message",
- skipInputRecord: true,
- traceContext: {
- sessionId,
- spanId: "span_background_notification_pre_loop_cancel",
- traceId: "trace_background_notification_pre_loop_cancel",
- },
- });
- await setupStarted.promise;
- abortController.abort(new Error("user stopped before model loop"));
- setupMayFinish.resolve();
- await expect(turn).rejects.toBeDefined();
- const cancelled = (await eventStore.getEvents(sessionId)).find(
- (event) => event.type === SessionEventType.TurnComplete,
- );
- expect(cancelled?.payload).toMatchObject({
- backgroundSubagentResultConsumed: true,
- resultType: "cancelled",
- });
- });
- it("drains active-loop background notifications into model-only history without starting a turn", async () => {
- const sessionId = createSessionId("background-active-loop-drain");
- const sessionStore = new RecordingSessionStore();
- const generateText = vi.fn();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- workingDirectory: "/tmp/zcode-background-active-loop-drain",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({ generateText } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as AgentRuntime & {
- drainPendingRuntimeCommandsForActiveLoop(): Promise<{
- consumedCommandIds: readonly string[];
- drained: number;
- messageIds: readonly string[];
- }>;
- messageHistory: {
- toRuntimeEntries(): readonly { message: { content: ModelMessageContent } }[];
- };
- runtimeCommandQueue: {
- enqueue(command: {
- branchGeneration: number;
- createdAt: Date;
- id: string;
- mode: "task-notification";
- priority: "next";
- source: "background_task";
- text: string;
- traceContext: {
- sessionId: SessionId;
- spanId: string;
- traceId: string;
- };
- }): void;
- };
- };
- const traceContext = {
- traceId: "trace_background_active_loop_drain",
- spanId: "span_background_active_loop_drain",
- sessionId,
- };
- runtime.runtimeCommandQueue.enqueue({
- branchGeneration: 0,
- createdAt: new Date(),
- id: createRuntimeCommandId(),
- mode: "task-notification",
- priority: "next",
- source: "background_task",
- text: "<task-notification><task-id>active-drain-1</task-id></task-notification>",
- traceContext,
- });
- runtime.runtimeCommandQueue.enqueue({
- branchGeneration: 0,
- createdAt: new Date(),
- id: createRuntimeCommandId(),
- mode: "task-notification",
- priority: "next",
- source: "background_task",
- text: "<task-notification><task-id>active-drain-2</task-id></task-notification>",
- traceContext,
- });
- const result = await runtime.drainPendingRuntimeCommandsForActiveLoop();
- expect(result.drained).toBe(2);
- expect(result.messageIds).toHaveLength(2);
- expect(result.consumedCommandIds).toHaveLength(2);
- expect(generateText).not.toHaveBeenCalled();
- const persisted = sessionStore.savedMessages.filter(
- (message) => message.source === "background_task",
- );
- expect(persisted).toHaveLength(2);
- expect(persisted.every((message) => message.visibility === "model-only")).toBe(true);
- const runtimeText = runtime.messageHistory
- .toRuntimeEntries()
- .map((entry) => modelMessageContentToText(entry.message.content))
- .join("\n");
- expect(runtimeText).toContain("<task-id>active-drain-1</task-id>");
- expect(runtimeText).toContain("<task-id>active-drain-2</task-id>");
- });
- it("drops active-loop notifications queued by an older rewind branch generation", async () => {
- const sessionId = createSessionId("background-active-loop-stale-branch");
- const sessionStore = new RecordingSessionStore();
- const runtime = createTestAgentRuntime(
- sessionId,
- { workingDirectory: "/tmp/zcode-background-active-loop-stale-branch" },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({ generateText: vi.fn() } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as AgentRuntime & {
- branchGeneration: number;
- drainPendingRuntimeCommandsForActiveLoop(): Promise<{
- consumedCommandIds: readonly string[];
- drained: number;
- messageIds: readonly string[];
- }>;
- runtimeCommandQueue: {
- enqueue(command: {
- branchGeneration: number;
- createdAt: Date;
- id: string;
- mode: "task-notification";
- priority: "next";
- source: "background_task";
- text: string;
- traceContext: {
- sessionId: SessionId;
- spanId: string;
- traceId: string;
- };
- }): void;
- };
- };
- runtime.runtimeCommandQueue.enqueue({
- branchGeneration: 0,
- createdAt: new Date(),
- id: createRuntimeCommandId(),
- mode: "task-notification",
- priority: "next",
- source: "background_task",
- text: "<task-notification><task-id>stale-task</task-id></task-notification>",
- traceContext: {
- traceId: "trace_background_active_loop_stale",
- spanId: "span_background_active_loop_stale",
- sessionId,
- },
- });
- runtime.branchGeneration = 1;
- const result = await runtime.drainPendingRuntimeCommandsForActiveLoop();
- expect(result).toMatchObject({ drained: 0, consumedCommandIds: [], messageIds: [] });
- expect(sessionStore.savedMessages).toEqual([]);
- });
- it("leaves commands arriving after the active-loop snapshot for the next drain", async () => {
- const sessionId = createSessionId("background-active-loop-continuous-drain");
- const firstSaveStarted = createDeferred<void>();
- const firstSaveMayFinish = createDeferred<void>();
- const sessionStore = new RecordingSessionStore();
- const saveMessage = sessionStore.saveMessage.bind(sessionStore);
- let saveMessageCallCount = 0;
- sessionStore.saveMessage = async (input) => {
- saveMessageCallCount++;
- if (saveMessageCallCount === 1) {
- firstSaveStarted.resolve(undefined);
- await firstSaveMayFinish.promise;
- }
- await saveMessage(input);
- };
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- workingDirectory: "/tmp/zcode-background-active-loop-continuous-drain",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({ generateText: vi.fn() } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as AgentRuntime & {
- drainPendingRuntimeCommandsForActiveLoop(): Promise<{
- consumedCommandIds: readonly string[];
- drained: number;
- messageIds: readonly string[];
- }>;
- runtimeCommandQueue: {
- enqueue(command: {
- branchGeneration: number;
- createdAt: Date;
- id: string;
- mode: "task-notification";
- priority: "next";
- source: "background_task";
- text: string;
- traceContext: {
- sessionId: SessionId;
- spanId: string;
- traceId: string;
- };
- }): void;
- };
- };
- const traceContext = {
- traceId: "trace_background_active_loop_continuous_drain",
- spanId: "span_background_active_loop_continuous_drain",
- sessionId,
- };
- const enqueueNotification = (taskId: string) => {
- runtime.runtimeCommandQueue.enqueue({
- branchGeneration: 0,
- createdAt: new Date(),
- id: createRuntimeCommandId(),
- mode: "task-notification",
- priority: "next",
- source: "background_task",
- text: `<task-notification><task-id>${taskId}</task-id></task-notification>`,
- traceContext,
- });
- };
- enqueueNotification("continuous-drain-1");
- const drain = runtime.drainPendingRuntimeCommandsForActiveLoop();
- await firstSaveStarted.promise;
- enqueueNotification("continuous-drain-2");
- firstSaveMayFinish.resolve(undefined);
- const result = await drain;
- expect(result.drained).toBe(1);
- expect(result.messageIds).toHaveLength(1);
- expect(result.consumedCommandIds).toHaveLength(1);
- expect(sessionStore.savedMessages).toHaveLength(1);
- const nextResult = await runtime.drainPendingRuntimeCommandsForActiveLoop();
- expect(nextResult.drained).toBe(1);
- expect(sessionStore.savedMessages).toHaveLength(2);
- });
- it("leaves prompt commands queued when draining active-loop runtime commands", async () => {
- const sessionId = createSessionId("background-active-loop-prompt-drain");
- const sessionStore = new RecordingSessionStore();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- workingDirectory: "/tmp/zcode-background-active-loop-prompt-drain",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({ generateText: vi.fn() } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as AgentRuntime & {
- drainPendingRuntimeCommandsForActiveLoop(): Promise<{
- consumedCommandIds: readonly string[];
- drained: number;
- messageIds: readonly string[];
- }>;
- messageHistory: {
- toRuntimeEntries(): readonly { message: { content: ModelMessageContent } }[];
- };
- runtimeCommandQueue: {
- enqueue(command: PromptRuntimeCommand): void;
- size(): number;
- };
- };
- const traceContext = {
- traceId: "trace_background_active_loop_prompt",
- spanId: "span_background_active_loop_prompt",
- sessionId,
- };
- const command: PromptRuntimeCommand = {
- createdAt: new Date(),
- id: createRuntimeCommandId(),
- input: "queued prompt stays in outer queue",
- mode: "prompt",
- priority: "next",
- reject: vi.fn(),
- resolve: vi.fn(),
- traceContext,
- };
- runtime.runtimeCommandQueue.enqueue(command);
- const result = await runtime.drainPendingRuntimeCommandsForActiveLoop();
- expect(result.drained).toBe(0);
- expect(result.consumedCommandIds).toEqual([]);
- expect(result.messageIds).toEqual([]);
- expect(runtime.runtimeCommandQueue.size()).toBe(1);
- expect(command.resolve).not.toHaveBeenCalled();
- expect(command.reject).not.toHaveBeenCalled();
- const runtimeText = runtime.messageHistory
- .toRuntimeEntries()
- .map((entry) => modelMessageContentToText(entry.message.content))
- .join("\n");
- expect(runtimeText).not.toContain("queued prompt stays in outer queue");
- });
- it("drops queued child Bash task notifications after the subagent runtime is sealed", async () => {
- const sessionId = createSessionId("background-sealed-child-queued-bash");
- const sessionStore = new RecordingSessionStore();
- const generateText = vi.fn();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- taskType: "subagent_child",
- workingDirectory: "/tmp/zcode-background-sealed-child-queued-bash",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({ generateText } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as AgentRuntime & {
- backgroundTaskNotificationsSealed: boolean;
- drainRuntimeCommandQueue(): Promise<void>;
- enqueueBackgroundTaskNotification(notification: {
- taskId?: string;
- text: string;
- toolName?: string;
- traceContext: {
- sessionId: SessionId;
- spanId: string;
- traceId: string;
- };
- }): void;
- runtimeCommandDrainActive: boolean;
- runtimeCommandQueue: {
- size(): number;
- };
- sealBackgroundTaskNotifications(input: {
- reason: string;
- traceContext: {
- sessionId: SessionId;
- spanId: string;
- traceId: string;
- };
- }): void;
- };
- const traceContext = {
- traceId: "trace_background_sealed_child_queued_bash",
- spanId: "span_background_sealed_child_queued_bash",
- sessionId,
- };
- runtime.runtimeCommandDrainActive = true;
- runtime.enqueueBackgroundTaskNotification({
- taskId: "child_bash_queued_before_seal",
- text:
- "<task-notification>\n" +
- "<task-id>child_bash_queued_before_seal</task-id>\n" +
- "<status>completed</status>\n" +
- "</task-notification>",
- toolName: "Bash",
- traceContext,
- });
- expect(runtime.runtimeCommandQueue.size()).toBe(1);
- runtime.sealBackgroundTaskNotifications({
- reason: "subagent_terminal",
- traceContext,
- });
- runtime.runtimeCommandDrainActive = false;
- await runtime.drainRuntimeCommandQueue();
- expect(runtime.runtimeCommandQueue.size()).toBe(0);
- expect(generateText).not.toHaveBeenCalled();
- expect(JSON.stringify(sessionStore.savedMessages)).not.toContain(
- "child_bash_queued_before_seal",
- );
- });
- it("filters stale and suppressed members without dropping a valid notification batch member", async () => {
- const sessionId = createSessionId("background-filtered-batch-members");
- const sessionStore = new RecordingSessionStore();
- const requestMessages: Array<readonly { content: ModelMessageContent }[]> = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- taskType: "subagent_child",
- workingDirectory: "/tmp/zcode-background-filtered-batch-members",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(request: { messages: readonly { content: ModelMessageContent }[] }) {
- requestMessages.push(request.messages);
- return modelTextResult("processed valid notification");
- },
- } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as AgentRuntime & {
- branchGeneration: number;
- drainRuntimeCommandQueue(): Promise<void>;
- enqueueBackgroundTaskNotification(notification: {
- taskId?: string;
- text: string;
- toolName?: string;
- traceContext: {
- sessionId: SessionId;
- spanId: string;
- traceId: string;
- };
- }): void;
- runtimeCommandDrainActive: boolean;
- runtimeCommandQueue: {
- enqueue(command: {
- branchGeneration: number;
- createdAt: Date;
- id: string;
- mode: "task-notification";
- priority: "next";
- source: "background_task";
- text: string;
- traceContext: {
- sessionId: SessionId;
- spanId: string;
- traceId: string;
- };
- }): void;
- };
- sealBackgroundTaskNotifications(input: {
- reason: string;
- traceContext: {
- sessionId: SessionId;
- spanId: string;
- traceId: string;
- };
- }): void;
- };
- const traceContext = {
- traceId: "trace_background_filtered_batch_members",
- spanId: "span_background_filtered_batch_members",
- sessionId,
- };
- runtime.branchGeneration = 1;
- runtime.runtimeCommandDrainActive = true;
- runtime.enqueueBackgroundTaskNotification({
- taskId: "suppressed-bash",
- text: "<task-notification><task-id>suppressed-bash</task-id></task-notification>",
- toolName: "Bash",
- traceContext,
- });
- runtime.enqueueBackgroundTaskNotification({
- taskId: "valid-agent",
- text: "<task-notification><task-id>valid-agent</task-id></task-notification>",
- toolName: "Agent",
- traceContext,
- });
- runtime.runtimeCommandQueue.enqueue({
- branchGeneration: 0,
- createdAt: new Date(),
- id: createRuntimeCommandId(),
- mode: "task-notification",
- priority: "next",
- source: "background_task",
- text: "<task-notification><task-id>stale-agent</task-id></task-notification>",
- traceContext,
- });
- runtime.sealBackgroundTaskNotifications({
- reason: "subagent_terminal",
- traceContext,
- });
- runtime.runtimeCommandDrainActive = false;
- await runtime.drainRuntimeCommandQueue();
- expect(requestMessages).toHaveLength(1);
- const requestText = providerMessagesToText(requestMessages[0]!);
- expect(requestText).toContain("<task-id>valid-agent</task-id>");
- expect(requestText).not.toContain("<task-id>suppressed-bash</task-id>");
- expect(requestText).not.toContain("<task-id>stale-agent</task-id>");
- expect(
- sessionStore.savedMessages.filter((message) => message.source === "background_task"),
- ).toHaveLength(1);
- });
- it("queues task-notification commands behind an active prompt command", async () => {
- const sessionId = createSessionId("background-notification-waits-for-prompt");
- const sessionStore = new RecordingSessionStore();
- const firstPromptRequest = createDeferred<readonly { content: ModelMessageContent }[]>();
- const firstPromptCanFinish = createDeferred<void>();
- const notificationRequest = createDeferred<readonly { content: ModelMessageContent }[]>();
- let requestCount = 0;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- workingDirectory: "/tmp/zcode-background-notification-waits-for-prompt",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(request: { messages: readonly { content: ModelMessageContent }[] }) {
- requestCount += 1;
- if (requestCount === 1) {
- firstPromptRequest.resolve(request.messages);
- await firstPromptCanFinish.promise;
- return modelTextResult("prompt done");
- }
- notificationRequest.resolve(request.messages);
- return modelTextResult("notification done");
- },
- } as never),
- sessionStore: sessionStore as unknown as SessionStorePort,
- },
- ) as unknown as AgentRuntime & {
- enqueueBackgroundTaskNotification(notification: {
- text: string;
- traceContext: {
- traceId: string;
- spanId: string;
- sessionId: SessionId;
- };
- }): void;
- };
- const traceContext = {
- traceId: "trace_background_waits_for_prompt",
- spanId: "span_background_waits_for_prompt",
- sessionId,
- };
- const promptTurn = runtime.executeTurn("user prompt");
- await firstPromptRequest.promise;
- runtime.enqueueBackgroundTaskNotification({
- text: "<task-notification>\n<task-id>agent_waits_for_prompt</task-id>\n</task-notification>",
- traceContext,
- });
- await waitForMacrotask();
- expect(requestCount).toBe(1);
- firstPromptCanFinish.resolve();
- await expect(promptTurn).resolves.toMatchObject({ response: "prompt done" });
- const requestMessages = await notificationRequest.promise;
- expect(providerMessagesToText(requestMessages)).toContain(
- "<task-id>agent_waits_for_prompt</task-id>",
- );
- expect(requestCount).toBe(2);
- });
- it("moves a foreground local agent to background after the configured timeout", async () => {
- const sessionId = createSessionId("subagent-auto-background");
- const turnId = createTurnId("subagent-auto-background-turn");
- const childGate = createDeferred<void>();
- const notifications: Array<Record<string, unknown>> = [];
- const port = createExploreSubagentPort({
- autoBackgroundMs: 1,
- createAgentId: () => "agent_auto_background",
- emitParentEvent: async () => {},
- enqueueParentTaskNotification: (notification) => {
- notifications.push(notification);
- },
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- await childGate.promise;
- return {
- events: [],
- response: "auto background child finished",
- traceId: "trace_subagent_auto_background",
- };
- },
- });
- const output = await port.run(createSubagentRunRequest({ sessionId, turnId }));
- expect(output).toMatchObject({
- status: "async_launched",
- agentId: "agent_auto_background",
- backgroundTaskId: "agent_auto_background",
- });
- childGate.resolve();
- await waitForCondition(() => notifications.length === 1);
- expect(notifications[0]).toMatchObject({
- originMeta: {
- backgroundSource: "subagent",
- title: "Auto background test",
- workId: "agent_auto_background",
- subagentResult: { status: "completed" },
- },
- taskId: "agent_auto_background",
- });
- expect(notifications[0]?.text).toContain("<task-id>agent_auto_background</task-id>");
- expect(notifications[0]?.text).toContain("<status>completed</status>");
- // 框架只用于同步返回的工具结果;后台完成通知里的报告原样放进 <result>。
- expect(notifications[0]?.text).toContain("<result>auto background child finished</result>");
- expect(notifications[0]?.text).not.toContain("[Subagent hand-back]");
- });
- it("preserves the provider cause across failed background local-agent surfaces", async () => {
- const providerMessage = [
- "Requests are too frequent.",
- ` Provider spacing and full payload must remain unchanged: ${"x".repeat(520)}`,
- "Request id: unit-background-subagent-rate-limit",
- ].join("\n");
- const sessionId = createSessionId("subagent-background-rate-limit");
- const turnId = createTurnId("subagent-background-rate-limit-turn");
- const notifications: string[] = [];
- const notificationOriginMetas: unknown[] = [];
- const emittedEvents: SessionEvent[] = [];
- const providerError = Object.assign(new Error(providerMessage), {
- code: "model_rate_limited",
- context: {
- providerCode: "AccountRateLimitExceeded",
- statusCode: 429,
- },
- name: "AiSdkModelAdapterError",
- });
- const turnError = createTurnFailureError(providerError, undefined, "Turn execution failed");
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_background_rate_limit",
- emitParentEvent: async (event) => {
- emittedEvents.push(event);
- },
- enqueueParentTaskNotification: ({ originMeta, text }) => {
- notifications.push(text);
- notificationOriginMetas.push(originMeta);
- },
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- throw turnError;
- },
- });
- await port.start?.(createSubagentRunRequest({ sessionId, turnId }));
- await waitForCondition(() => notifications.length === 1);
- await waitForCondition(() =>
- emittedEvents.some((event) => event.type === SessionEventType.SubagentStopped),
- );
- const notification = notifications[0]!;
- expect(notification).toContain(
- `<summary>Agent "Auto background test" failed: ${providerMessage}</summary>`,
- );
- expect(notification).not.toContain("<error>");
- expect(notification).not.toContain("Turn execution failed");
- // 失败原文结构化下发,App 不从 summary 文本解析。
- expect(notificationOriginMetas).toEqual([
- expect.objectContaining({
- backgroundSource: "subagent",
- subagentResult: { status: "failed", error: providerMessage },
- }),
- ]);
- expect(await port.getTask?.("agent_background_rate_limit")).toMatchObject({
- status: "failed",
- error: providerMessage,
- });
- expect(emittedEvents).toContainEqual(
- expect.objectContaining({
- type: SessionEventType.SubagentStopped,
- payload: expect.objectContaining({
- status: "failed",
- error: providerMessage,
- }),
- }),
- );
- });
- it("queues parent notification before emitting background completion", async () => {
- const sessionId = createSessionId("subagent-notification-before-completion-event");
- const turnId = createTurnId("subagent-notification-before-completion-event-turn");
- const childGate = createDeferred<void>();
- const completionEventStarted = createDeferred<void>();
- const completionEventMayFinish = createDeferred<void>();
- const observed: string[] = [];
- let childRuntimeBackground: boolean | undefined;
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_notification_before_completion_event",
- emitParentEvent: async (event) => {
- observed.push(`event:${event.type}`);
- if (event.type === SessionEventType.BackgroundTaskCompleted) {
- completionEventStarted.resolve();
- await completionEventMayFinish.promise;
- }
- },
- enqueueParentTaskNotification: () => {
- observed.push("notification");
- },
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- childRuntimeBackground = request.background;
- await childGate.promise;
- return {
- events: [],
- response: "child finished before completion event",
- traceId: "trace_subagent_notification_before_completion_event",
- };
- },
- });
- const output = await port.start?.(createSubagentRunRequest({ sessionId, turnId }));
- expect(output).toMatchObject({
- status: "async_launched",
- agentId: "agent_notification_before_completion_event",
- });
- await waitForCondition(() => childRuntimeBackground === true);
- expect(childRuntimeBackground).toBe(true);
- childGate.resolve();
- await completionEventStarted.promise;
- const completedIndex = observed.indexOf(`event:${SessionEventType.BackgroundTaskCompleted}`);
- const notificationIndex = observed.indexOf("notification");
- expect(notificationIndex).toBeGreaterThan(-1);
- expect(notificationIndex).toBeLessThan(completedIndex);
- completionEventMayFinish.resolve();
- await waitForCondition(() => observed.includes(`event:${SessionEventType.SubagentStopped}`));
- const stoppedIndex = observed.indexOf(`event:${SessionEventType.SubagentStopped}`);
- expect(completedIndex).toBeGreaterThan(-1);
- expect(stoppedIndex).toBeGreaterThan(completedIndex);
- });
- it("auto-backgrounds with a referenced timeout and publishes the child result", async () => {
- const sessionId = createSessionId("subagent-auto-background-timeout-ref");
- const turnId = createTurnId("subagent-auto-background-timeout-ref-turn");
- const childGate = createDeferred<void>();
- const notifications: Array<Record<string, unknown>> = [];
- const port = createExploreSubagentPort({
- autoBackgroundMs: 1,
- createAgentId: () => "agent_auto_background_timeout_ref",
- emitParentEvent: async () => {},
- enqueueParentTaskNotification: (notification) => {
- notifications.push(notification);
- },
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- await childGate.promise;
- return {
- events: [],
- response: "auto background timeout ref child finished",
- traceId: "trace_subagent_auto_background_timeout_ref",
- };
- },
- });
- const unrefRecorder = recordSetTimeoutUnrefCalls();
- const outputPromise = port.run(createSubagentRunRequest({ sessionId, turnId }));
- let output: Awaited<typeof outputPromise>;
- try {
- output = await outputPromise;
- } finally {
- unrefRecorder.restore();
- }
- expect(output).toMatchObject({
- status: "async_launched",
- agentId: "agent_auto_background_timeout_ref",
- backgroundTaskId: "agent_auto_background_timeout_ref",
- });
- expect(unrefRecorder.calls).toEqual([]);
- childGate.resolve();
- await waitForCondition(() => notifications.length === 1);
- expect(notifications[0]).toMatchObject({
- originMeta: {
- backgroundSource: "subagent",
- title: "Auto background test",
- workId: "agent_auto_background_timeout_ref",
- },
- taskId: "agent_auto_background_timeout_ref",
- });
- expect(notifications[0]?.text).toContain(
- "<task-id>agent_auto_background_timeout_ref</task-id>",
- );
- expect(notifications[0]?.text).toContain("<status>completed</status>");
- expect(notifications[0]?.text).toContain("auto background timeout ref child finished");
- });
- it("does not mark background local agents notified without a parent notification queue", async () => {
- const sessionId = createSessionId("subagent-notification-queue-missing");
- const turnId = createTurnId("subagent-notification-queue-missing-turn");
- const childGate = createDeferred<void>();
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_notification_queue_missing",
- emitParentEvent: async () => {},
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- await childGate.promise;
- return {
- events: [],
- response: "child finished without parent queue",
- traceId: "trace_subagent_notification_queue_missing",
- };
- },
- });
- const output = await port.start?.(createSubagentRunRequest({ sessionId, turnId }));
- expect(output).toMatchObject({
- status: "async_launched",
- agentId: "agent_notification_queue_missing",
- });
- childGate.resolve();
- await waitForCondition(async () => {
- const task = await port.getTask?.("agent_notification_queue_missing");
- return task?.status === "completed";
- });
- const task = await port.getTask?.("agent_notification_queue_missing");
- expect(task).toMatchObject({
- status: "completed",
- agentId: "agent_notification_queue_missing",
- });
- expect(task?.notified).not.toBe(true);
- });
- it("does not fake-notify background local agents when parent notification enqueue fails", async () => {
- const sessionId = createSessionId("subagent-notification-enqueue-fails");
- const turnId = createTurnId("subagent-notification-enqueue-fails-turn");
- const childGate = createDeferred<void>();
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_notification_enqueue_fails",
- emitParentEvent: async () => {},
- enqueueParentTaskNotification: () => {
- throw new Error("parent queue unavailable");
- },
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- await childGate.promise;
- return {
- events: [],
- response: "child finished but parent queue failed",
- traceId: "trace_subagent_notification_enqueue_fails",
- };
- },
- });
- const output = await port.start?.(createSubagentRunRequest({ sessionId, turnId }));
- expect(output).toMatchObject({
- status: "async_launched",
- agentId: "agent_notification_enqueue_fails",
- });
- childGate.resolve();
- await waitForCondition(async () => {
- const task = await port.getTask?.("agent_notification_enqueue_fails");
- return task?.status === "completed" || task?.status === "failed";
- });
- const task = await port.getTask?.("agent_notification_enqueue_fails");
- expect(task).toMatchObject({
- status: "completed",
- agentId: "agent_notification_enqueue_fails",
- });
- expect(task?.notified).not.toBe(true);
- });
- it("cancels auto-background timeout when a foreground local agent completes first", async () => {
- const sessionId = createSessionId("subagent-auto-background-cancelled");
- const turnId = createTurnId("subagent-auto-background-cancelled-turn");
- const blockedArtifactWrite = createDeferred<void>();
- let outputWriteStarted = false;
- const notifications: string[] = [];
- const actualFs = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
- vi.resetModules();
- vi.doMock("node:fs/promises", () => ({
- ...actualFs,
- writeFile: vi.fn(async (...args: Parameters<typeof actualFs.writeFile>) => {
- const [path] = args;
- if (String(path).endsWith("/output.txt") && !outputWriteStarted) {
- outputWriteStarted = true;
- await blockedArtifactWrite.promise;
- }
- return actualFs.writeFile(...args);
- }),
- }));
- try {
- const { createExploreSubagentPort: createPortWithBlockedArtifactWrite } =
- await import("../src/subagent/runner.js");
- const port = createPortWithBlockedArtifactWrite({
- autoBackgroundMs: 1,
- createAgentId: () => "agent_auto_background_cancelled",
- emitParentEvent: async () => {},
- enqueueParentTaskNotification: ({ text }) => {
- notifications.push(text);
- },
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- return {
- events: [],
- response: "foreground child beat timeout",
- traceId: "trace_subagent_auto_background_cancelled",
- };
- },
- });
- const outputPromise = port.run(createSubagentRunRequest({ sessionId, turnId }));
- await waitForCondition(() => outputWriteStarted);
- await new Promise((resolve) => setTimeout(resolve, 10));
- const taskWhileArtifactWriteIsPending = await port.getTask?.(
- "agent_auto_background_cancelled",
- );
- blockedArtifactWrite.resolve();
- const output = await outputPromise;
- expect(output).toMatchObject({
- status: "completed",
- agentId: "agent_auto_background_cancelled",
- });
- expect(taskWhileArtifactWriteIsPending).toMatchObject({
- status: "running",
- isBackgrounded: false,
- });
- const task = await port.getTask?.("agent_auto_background_cancelled");
- expect(task).toMatchObject({
- status: "completed",
- isBackgrounded: false,
- });
- expect(notifications).toEqual([]);
- } finally {
- blockedArtifactWrite.resolve();
- vi.doUnmock("node:fs/promises");
- vi.resetModules();
- }
- });
- it("keeps foreground local agents inline when auto-background timeout is not configured", async () => {
- const sessionId = createSessionId("subagent-no-auto-background");
- const turnId = createTurnId("subagent-no-auto-background-turn");
- const notifications: string[] = [];
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_no_auto_background",
- emitParentEvent: async () => {},
- enqueueParentTaskNotification: ({ text }) => {
- notifications.push(text);
- },
- runExploreAgent: async (request) => {
- await request.onSessionReady?.();
- return {
- events: [],
- response: "foreground child finished",
- traceId: "trace_subagent_no_auto_background",
- };
- },
- });
- const output = await port.run(createSubagentRunRequest({ sessionId, turnId }));
- expect(output).toMatchObject({
- status: "completed",
- agentId: "agent_no_auto_background",
- });
- expect(notifications).toEqual([]);
- });
- it.each(["explicit", "profile", "timeout"])(
- "supports off-peak background model overrides: %s",
- async (kind) => {
- const gate = createDeferred<void>();
- const calls: unknown[] = [];
- const port = createExploreSubagentPort({
- createAgentId: () => "idle-background",
- ...(kind === "timeout" ? { autoBackgroundMs: 1 } : {}),
- emitParentEvent: async () => {},
- profiles: [
- {
- name: "Idle Reviewer",
- description: "Review",
- source: "user",
- systemPrompt: "Review",
- tools: ["Read"],
- background: kind === "profile",
- },
- ],
- runExploreAgent: async (request, options) => {
- calls.push(options);
- await request.onSessionReady?.();
- await gate.promise;
- return { events: [], response: "done", traceId: "idle-background-trace" };
- },
- });
- const modelOverride = {
- selection: { providerId: "idle", modelId: "model" },
- background: "allow" as const,
- };
- try {
- const output = await port.launch(
- {
- ...createSubagentRunRequest({
- sessionId: createSessionId("idle-parent"),
- turnId: createTurnId("idle-turn"),
- }),
- agentType: "Idle Reviewer",
- runInBackground: kind === "explicit",
- },
- { modelOverride },
- );
- expect(output.status).toBe("async_launched");
- expect(calls).toEqual([expect.objectContaining({ modelOverride })]);
- } finally {
- gate.resolve();
- }
- },
- );
- it("rejects background launch when an execution model override is present", async () => {
- const sessionId = createSessionId("subagent-background-turn-execution-model");
- const turnId = createTurnId("subagent-background-turn-execution-model-turn");
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_background_turn_execution_model",
- emitParentEvent: async () => {},
- runExploreAgent: async () => {
- throw new Error("background child must not start");
- },
- });
- await expect(
- port.launch(
- {
- ...createSubagentRunRequest({ sessionId, turnId }),
- runInBackground: true,
- },
- {
- signal: new AbortController().signal,
- modelOverride: {
- selection: { providerId: "account:zai-offpeak-idle-plan", modelId: "GLM-5.2" },
- background: "deny",
- },
- },
- ),
- ).rejects.toThrow("Idle-time tasks do not support background agents");
- expect(await port.getTask?.("agent_background_turn_execution_model")).toBeUndefined();
- });
- it("rejects a profile-configured background agent when an execution model override is present", async () => {
- const sessionId = createSessionId("subagent-profile-background-turn-execution-model");
- const turnId = createTurnId("subagent-profile-background-turn-execution-model-turn");
- const port = createExploreSubagentPort({
- createAgentId: () => "agent_profile_background_turn_execution_model",
- emitParentEvent: async () => {},
- profiles: [
- {
- background: true,
- description: "Background reviewer",
- name: "Background Reviewer",
- source: "user",
- systemPrompt: "Review in background.",
- tools: ["Read"],
- },
- ],
- runExploreAgent: async () => {
- throw new Error("profile background child must not start");
- },
- });
- await expect(
- port.launch(
- {
- ...createSubagentRunRequest({ sessionId, turnId }),
- agentType: "Background Reviewer",
- },
- {
- modelOverride: {
- selection: { providerId: "account:zai-offpeak-idle-plan", modelId: "GLM-5.2" },
- background: "deny",
- },
- },
- ),
- ).rejects.toThrow("Idle-time tasks do not support background agents");
- expect(await port.getTask?.("agent_profile_background_turn_execution_model")).toBeUndefined();
- });
- });
- function createDeferred<T>(): {
- promise: Promise<T>;
- resolve: (value: T) => void;
- } {
- let resolve!: (value: T) => void;
- const promise = new Promise<T>((innerResolve) => {
- resolve = innerResolve;
- });
- return { promise, resolve };
- }
- function modelTextResult(text: string) {
- return {
- finishReason: "stop",
- model: "test",
- providerMetadata: undefined,
- text,
- usage: {
- inputTokens: 1,
- outputTokens: 1,
- totalTokens: 2,
- },
- };
- }
- function providerMessagesToText(messages: readonly { content: ModelMessageContent }[]): string {
- return messages.map((message) => modelMessageContentToText(message.content)).join("\n");
- }
- async function waitForMacrotask(): Promise<void> {
- await new Promise<void>((resolve) => {
- setTimeout(resolve, 0);
- });
- }
- async function waitForCondition(predicate: () => boolean | Promise<boolean>): Promise<void> {
- const startedAt = Date.now();
- while (Date.now() - startedAt < 1000) {
- if (await predicate()) return;
- await new Promise((resolve) => setTimeout(resolve, 5));
- }
- throw new Error("Timed out waiting for condition");
- }
- async function waitForAbort(signal?: AbortSignal): Promise<void> {
- if (!signal || signal.aborted) return;
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", () => resolve(), { once: true });
- });
- }
- function recordSetTimeoutUnrefCalls(): { calls: number[]; restore: () => void } {
- const calls: number[] = [];
- const originalSetTimeout = globalThis.setTimeout;
- const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout").mockImplementation(((
- handler: Parameters<typeof setTimeout>[0],
- timeout?: Parameters<typeof setTimeout>[1],
- ...args: any[]
- ) => {
- const timer = originalSetTimeout(handler, timeout, ...args);
- if (
- typeof timer === "object" &&
- timer !== null &&
- "unref" in timer &&
- typeof timer.unref === "function"
- ) {
- const originalUnref = timer.unref.bind(timer);
- timer.unref = () => {
- calls.push(typeof timeout === "number" ? timeout : Number(timeout ?? 0));
- return originalUnref();
- };
- }
- return timer;
- }) as typeof setTimeout);
- return {
- calls,
- restore: () => {
- setTimeoutSpy.mockRestore();
- },
- };
- }
- function recordSetTimeoutCalls(): {
- calls: number[];
- restore: () => void;
- } {
- const calls: number[] = [];
- const originalSetTimeout = globalThis.setTimeout;
- const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout").mockImplementation(((
- handler: Parameters<typeof setTimeout>[0],
- timeout?: Parameters<typeof setTimeout>[1],
- ...args: any[]
- ) => {
- calls.push(typeof timeout === "number" ? timeout : Number(timeout ?? 0));
- const timer = originalSetTimeout(handler, timeout, ...args);
- return timer;
- }) as typeof setTimeout);
- return {
- calls,
- restore: () => {
- setTimeoutSpy.mockRestore();
- },
- };
- }
- function createSubagentRunRequest(input: {
- sessionId: SessionId;
- turnId: ReturnType<typeof createTurnId>;
- }) {
- return {
- agentType: "general-purpose",
- description: "Auto background test",
- parentToolCallId: "call_auto_background",
- prompt: "Run slowly",
- sessionId: input.sessionId,
- trace: {
- traceId: "trace_subagent_auto_background_parent",
- spanId: "span_subagent_auto_background_parent",
- sessionId: input.sessionId,
- turnId: input.turnId,
- },
- turnId: input.turnId,
- workingDirectory: "/tmp/zcode-subagent-auto-background",
- workspaceRoot: "/tmp/zcode-subagent-auto-background",
- };
- }
- class RecordingSessionStore {
- readonly createdSessions: CreateSessionInput[] = [];
- readonly projectPermissions = new Map<string, PermissionRuleset>();
- readonly savedMessages: MessageInfo[] = [];
- readonly savedParts: MessagePart[] = [];
- readonly savedSessionInputs: Array<
- Parameters<NonNullable<SessionStorePort["saveSessionInput"]>>[0]
- > = [];
- readonly promotedSessionInputs: Array<
- Parameters<NonNullable<SessionStorePort["markSessionInputPromoted"]>>[0]
- > = [];
- readonly sessions = new Map<string, SessionInfo>();
- readonly targets = new Map<string, SessionGoal>();
- readonly todos = new Map<string, TodoItem[]>();
- async createSession(input: CreateSessionInput): Promise<SessionInfo> {
- this.createdSessions.push(input);
- const now = Date.now();
- const session: SessionInfo = {
- ...input,
- taskType: input.taskType ?? "interactive",
- time: {
- created: input.time?.created ?? now,
- updated: input.time?.updated ?? now,
- },
- };
- this.sessions.set(input.id, session);
- return session;
- }
- async updateSession(
- input: Parameters<SessionStorePort["updateSession"]>[0],
- ): Promise<SessionInfo> {
- const session = this.sessions.get(input.id);
- if (!session) throw new Error(`missing session: ${input.id}`);
- const next: SessionInfo = {
- ...session,
- title: input.title ?? session.title,
- shareURL: input.shareURL === null ? undefined : (input.shareURL ?? session.shareURL),
- summaryAdditions:
- input.summary === null ? undefined : (input.summary?.additions ?? session.summaryAdditions),
- summaryDeletions:
- input.summary === null ? undefined : (input.summary?.deletions ?? session.summaryDeletions),
- summaryFiles:
- input.summary === null ? undefined : (input.summary?.files ?? session.summaryFiles),
- summaryDiffs:
- input.summary === null ? undefined : (input.summary?.diffs ?? session.summaryDiffs),
- permission: input.permission === null ? undefined : (input.permission ?? session.permission),
- time: {
- ...session.time,
- updated: Date.now(),
- compacting:
- input.timeCompacting === null
- ? undefined
- : (input.timeCompacting ?? session.time.compacting),
- },
- };
- this.sessions.set(input.id, next);
- return next;
- }
- async getSession(sessionID: SessionId): Promise<SessionInfo | null> {
- return this.sessions.get(sessionID) ?? null;
- }
- async listSessions(): Promise<SessionInfo[]> {
- return Array.from(this.sessions.values());
- }
- async saveMessage(input: MessageInfo): Promise<void> {
- this.savedMessages.push(input);
- }
- async removeMessage(input: {
- messageID: MessageInfo["id"];
- sessionID: SessionId;
- }): Promise<void> {
- removeWhere(
- this.savedMessages,
- (message) => message.sessionID === input.sessionID && message.id === input.messageID,
- );
- }
- async savePart(input: MessagePart): Promise<void> {
- this.savedParts.push(input);
- }
- async saveSessionInput(
- input: Parameters<NonNullable<SessionStorePort["saveSessionInput"]>>[0],
- ): Promise<void> {
- this.savedSessionInputs.push(input);
- }
- async markSessionInputPromoted(
- input: Parameters<NonNullable<SessionStorePort["markSessionInputPromoted"]>>[0],
- ): Promise<void> {
- this.promotedSessionInputs.push(input);
- }
- async removePart(input: {
- messageID: MessageInfo["id"];
- partID: MessagePart["id"];
- sessionID: SessionId;
- }): Promise<void> {
- removeWhere(
- this.savedParts,
- (part) =>
- part.sessionID === input.sessionID &&
- part.messageID === input.messageID &&
- part.id === input.partID,
- );
- }
- async messages(input: { sessionID: SessionId }): Promise<MessageWithParts[]> {
- return this.savedMessages
- .filter((message) => message.sessionID === input.sessionID)
- .map((info) => ({
- info,
- parts: this.savedParts.filter(
- (part) => part.sessionID === input.sessionID && part.messageID === info.id,
- ),
- }));
- }
- async readTodos(input: { sessionID: SessionId }): Promise<TodoItem[]> {
- return [...(this.todos.get(input.sessionID) ?? [])];
- }
- async updateTodos(input: { sessionID: SessionId; todos: TodoItem[] }): Promise<void> {
- this.todos.set(input.sessionID, [...input.todos]);
- }
- async readTarget(input: { sessionID: SessionId }): Promise<SessionGoal | null> {
- return this.targets.get(input.sessionID) ?? null;
- }
- async updateTarget(input: { sessionID: SessionId; target: SessionGoal | null }): Promise<void> {
- if (input.target === null) {
- this.targets.delete(input.sessionID);
- return;
- }
- this.targets.set(input.sessionID, input.target);
- }
- async readProjectPermissions(input: { projectID: string }): Promise<PermissionRuleset | null> {
- return this.projectPermissions.get(input.projectID) ?? null;
- }
- async updateProjectPermissions(input: {
- projectID: string;
- permission: PermissionRuleset | null;
- }): Promise<void> {
- if (input.permission === null) {
- this.projectPermissions.delete(input.projectID);
- return;
- }
- this.projectPermissions.set(input.projectID, input.permission);
- }
- }
- function removeWhere<T>(items: T[], predicate: (item: T) => boolean): void {
- for (let index = items.length - 1; index >= 0; index--) {
- if (predicate(items[index]!)) items.splice(index, 1);
- }
- }
|