| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- import { digestThreadGoalObjective, type ThreadGoalState } from "@mavis/goal";
- import type { RuntimeConversation } from "@mavis/conversation-contract";
- import { describe, expect, it, vi } from "vitest";
- import type { LocalSessionRecord } from "../../src/sessions/controller.js";
- import type { LocalThreadGoalIntegration } from "../../src/thread-goal/host-integration.js";
- import { LocalThreadGoalKickoffQueue } from "../../src/thread-goal/kickoff-host.js";
- import {
- buildThreadGoalKickoffMessage,
- threadGoalBudgetLimitClientRequestId,
- threadGoalRecoveryClientRequestId,
- } from "../../src/thread-goal/kickoff.js";
- const session = {
- sessionId: "session-a",
- agentName: "mavis",
- } as LocalSessionRecord;
- const goal = {
- goalId: "tg_1",
- sessionId: session.sessionId,
- objective: "Review the spec",
- status: "active",
- createdAt: 1_000,
- updatedAt: 1_000,
- tokensUsed: 0,
- timeUsedSeconds: 0,
- tokenBudget: null,
- kickoffAttachments: [
- {
- type: "file",
- filePath: "/tmp/spec.md",
- fileName: "spec.md",
- mimeType: "text/markdown",
- },
- ],
- kickoffState: "pending",
- } as ThreadGoalState;
- function makeQueueHost(
- options: {
- recoverInitialContinuation?: (goal: ThreadGoalState) => Promise<void>;
- recoverableGoals?: ThreadGoalState[];
- listRecoverableKickoffs?: () => Promise<ThreadGoalState[]>;
- recoverableContinuations?: ThreadGoalState[];
- recoverableBudgetLimitSummaries?: ThreadGoalState[];
- shouldRecoverActiveContinuation?: (
- goal: ThreadGoalState,
- ) => Promise<boolean>;
- clearStaleVerificationWaits?: () => Promise<number>;
- renderRecoveryContinuationPrompt?: (
- goal: ThreadGoalState,
- ) => Promise<string>;
- renderRecoveryContinuationPromptRead?: (goal: ThreadGoalState) => Promise<{
- readonly content: string;
- readonly promptRead?: never;
- }>;
- conversation?: Pick<RuntimeConversation, "query" | "ingress">;
- } = {},
- ) {
- const recoverInitialContinuation = vi.fn(
- options.recoverInitialContinuation ?? (async () => undefined),
- );
- const clearStaleVerificationWaits = vi.fn(
- options.clearStaleVerificationWaits ?? (async () => 0),
- );
- const reportFailure = vi.fn();
- const reportRecoveryFailure = vi.fn();
- const rebindInternalPromptRead = vi.fn();
- const discardInternalPromptRead = vi.fn();
- const host = new LocalThreadGoalKickoffQueue({
- ...(options.conversation ? { conversation: options.conversation } : {}),
- getIntegration: () =>
- ({
- store: {
- listRecoverableKickoffs:
- options.listRecoverableKickoffs ??
- (async () => options.recoverableGoals ?? []),
- listRecoverableActiveGoals: async () =>
- options.recoverableContinuations ?? [],
- listRecoverableBudgetLimitSummaries: async () =>
- options.recoverableBudgetLimitSummaries ?? [],
- },
- recoverInitialContinuation,
- clearStaleVerificationWaits,
- shouldRecoverActiveContinuation:
- options.shouldRecoverActiveContinuation ?? (async () => true),
- preparePrompt: async (
- _recoverableGoal: ThreadGoalState,
- kind: string,
- ) => ({
- content:
- kind === "budget-limit"
- ? "The active thread goal has reached its token budget."
- : kind === "recovery"
- ? "Before taking any other action, call get_goal and recover durable Goal state."
- : "Continue working toward the active thread goal and follow the goal contract from the kickoff context.",
- }),
- prepareRecoveryPrompt: async (recoverableGoal: ThreadGoalState) => ({
- content:
- recoverableGoal.turnsUsed > 0 && recoverableGoal.turnsUsed % 5 === 0
- ? "Call get_goal once for the recovery and scheduled five-Turn checkpoint."
- : "Before taking any other action, call get_goal and recover durable Goal state.",
- kind:
- recoverableGoal.turnsUsed > 0 && recoverableGoal.turnsUsed % 5 === 0
- ? "recovery-terminal-audit"
- : "recovery",
- }),
- recordPromptSubmitted: vi.fn(),
- renderRecoveryContinuationPrompt:
- options.renderRecoveryContinuationPrompt ??
- (async (recoverableGoal) => `Continue: ${recoverableGoal.objective}`),
- renderRecoveryContinuationPromptRead:
- options.renderRecoveryContinuationPromptRead ??
- (async (recoverableGoal) => ({
- content: `Continue: ${recoverableGoal.objective}`,
- })),
- reserveInternalPromptRead: vi.fn(() => undefined),
- rebindInternalPromptRead,
- discardInternalPromptRead,
- }) as unknown as LocalThreadGoalIntegration,
- reportFailure,
- reportRecoveryFailure,
- });
- return {
- host,
- recoverInitialContinuation,
- clearStaleVerificationWaits,
- reportFailure,
- reportRecoveryFailure,
- rebindInternalPromptRead,
- discardInternalPromptRead,
- };
- }
- describe("LocalThreadGoalKickoffQueue", () => {
- it("derives a restart-stable budget summary identity from Goal id and epoch", () => {
- expect(
- threadGoalBudgetLimitClientRequestId({
- goalId: "tg_1",
- updatedAt: 1_700_000_000_123,
- }),
- ).toBe("thread-goal-followup:budget-limit:tg_1:1700000000123");
- });
- it("recovers Goal-owned pending kickoffs and reports scan failures", async () => {
- const recovery = makeQueueHost({ recoverableGoals: [goal] });
- await recovery.host.recover();
- expect(recovery.recoverInitialContinuation).toHaveBeenCalledWith(goal);
- expect(recovery.reportRecoveryFailure).not.toHaveBeenCalled();
- const failed = makeQueueHost({
- listRecoverableKickoffs: async () => {
- throw new Error("goal scan failed");
- },
- });
- await failed.host.recover();
- expect(failed.reportRecoveryFailure).toHaveBeenCalledWith(
- "goal scan failed",
- );
- });
- /**
- * A verification wait cannot survive the process that issued it, so startup
- * must retire it. The cleanup runs ahead of every recovery scan on purpose:
- * continuation recovery skips Goals for unrelated reasons (runtime disabled,
- * a session already started, an existing queue entry), and those are exactly
- * the Goals that would otherwise be left reading "Verifying" forever.
- */
- it("clears stale verification waits before any gated recovery scan", async () => {
- const order: string[] = [];
- const recovery = makeQueueHost({
- recoverableGoals: [goal],
- clearStaleVerificationWaits: async () => {
- order.push("cleanup");
- return 1;
- },
- recoverInitialContinuation: async () => {
- order.push("kickoff-recovery");
- },
- });
- await recovery.host.recover();
- expect(recovery.clearStaleVerificationWaits).toHaveBeenCalledOnce();
- expect(order).toEqual(["cleanup", "kickoff-recovery"]);
- });
- it("still clears stale verification waits when recovery gates skip every Goal", async () => {
- const recovery = makeQueueHost({
- recoverableGoals: [],
- recoverableContinuations: [goal],
- // The gate that made this reachable in the field: recovery declines, so
- // nothing downstream would ever touch the wait.
- shouldRecoverActiveContinuation: async () => false,
- conversation: {
- query: {
- getSession: async () => session,
- listSessions: async () => [session],
- },
- ingress: {
- findQueuedByClientRequestId: async () => undefined,
- submit: vi.fn(),
- dispatchQueue: vi.fn(),
- listQueued: async () => [],
- },
- } as unknown as Pick<RuntimeConversation, "query" | "ingress">,
- });
- await recovery.host.recover();
- expect(recovery.clearStaleVerificationWaits).toHaveBeenCalledOnce();
- expect(recovery.reportRecoveryFailure).not.toHaveBeenCalled();
- });
- it("reports a cleanup failure without stopping the recovery scans behind it", async () => {
- const recovery = makeQueueHost({
- recoverableGoals: [goal],
- clearStaleVerificationWaits: async () => {
- throw new Error("cleanup unavailable");
- },
- });
- await recovery.host.recover();
- expect(recovery.reportRecoveryFailure).toHaveBeenCalledWith(
- "thread_goal_stale_verification_wait_cleanup_failed:cleanup unavailable",
- );
- expect(recovery.recoverInitialContinuation).toHaveBeenCalledWith(goal);
- });
- it("materializes and recovers kickoffs only through the injected queue", async () => {
- const submit = vi.fn(async () => acceptedQueueTurn());
- const dispatchQueue = vi.fn(async () => undefined);
- const findQueuedByClientRequestId = vi.fn(async () => undefined);
- const fixture = makeQueueHost({
- recoverableGoals: [goal],
- conversation: {
- query: {
- getSession: async () => session,
- listSessions: async () => [session],
- },
- ingress: {
- findQueuedByClientRequestId,
- submit,
- dispatchQueue,
- },
- } as Pick<RuntimeConversation, "query" | "ingress">,
- });
- await fixture.host.integrationDeps().enqueueInitialContinuationTurn(
- goal,
- buildThreadGoalKickoffMessage(
- goal,
- "hidden continuation",
- goal.kickoffAttachments.map((attachment) => ({ ...attachment })),
- ),
- `thread-goal-kickoff:${goal.goalId}`,
- );
- expect(submit).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: session.sessionId,
- source: "thread-goal",
- allowQueue: true,
- message: expect.objectContaining({
- hideUserMessage: true,
- displayContent: goal.objective,
- origin: {
- type: "thread-goal-kickoff",
- goalId: goal.goalId,
- goalUpdatedAt: goal.updatedAt,
- objectiveDigest: digestThreadGoalObjective(goal.objective),
- displayContent: goal.objective,
- },
- attachments: [expect.objectContaining({ fileName: "spec.md" })],
- }),
- clientRequestId: `thread-goal-kickoff:${goal.goalId}`,
- }),
- );
- expect(dispatchQueue).not.toHaveBeenCalled();
- await fixture.host.recover();
- expect(fixture.recoverInitialContinuation).toHaveBeenCalledWith(goal);
- expect(dispatchQueue).not.toHaveBeenCalled();
- fixture.host.integrationDeps().requestQueueDispatch(session.sessionId);
- expect(dispatchQueue).toHaveBeenCalledWith(session.sessionId);
- });
- it("keeps an initial continuation prompt snapshot with its admitted Turn", async () => {
- const submit = vi.fn(async () => acceptedQueueTurn());
- const fixture = makeQueueHost({
- conversation: {
- query: {
- getSession: async () => session,
- listSessions: async () => [session],
- },
- ingress: {
- findQueuedByClientRequestId: vi.fn(async () => undefined),
- submit,
- dispatchQueue: vi.fn(async () => undefined),
- },
- } as Pick<RuntimeConversation, "query" | "ingress">,
- });
- const internalPromptRead = {
- requestedTurnId: "turn_goal_requested",
- promptRead: {} as never,
- };
- await fixture.host
- .integrationDeps()
- .enqueueInitialContinuationTurn(
- goal,
- buildThreadGoalKickoffMessage(goal, "hidden continuation", []),
- `thread-goal-kickoff:${goal.goalId}`,
- internalPromptRead,
- );
- expect(submit).toHaveBeenCalledWith(
- expect.objectContaining({
- requestedTurnId: internalPromptRead.requestedTurnId,
- }),
- );
- expect(fixture.rebindInternalPromptRead).toHaveBeenCalledWith(
- internalPromptRead,
- "turn-v2",
- );
- });
- it("recovers a consumed active Goal when no Turn or Goal continuation survived restart", async () => {
- const consumed = {
- ...goal,
- kickoffState: "consumed" as const,
- kickoffAttachments: [],
- };
- const submit = vi.fn(async () => acceptedQueueTurn());
- const listQueued = vi.fn(async () => []);
- const fixture = makeQueueHost({
- recoverableContinuations: [consumed],
- conversation: {
- query: {
- getSession: async () => ({ ...session, status: "idle" }),
- listSessions: async () => [session],
- },
- ingress: {
- findQueuedByClientRequestId: vi.fn(async () => undefined),
- listQueued,
- submit,
- dispatchQueue: vi.fn(async () => undefined),
- },
- } as Pick<RuntimeConversation, "query" | "ingress">,
- });
- await fixture.host.recover();
- expect(listQueued).toHaveBeenCalledWith(consumed.sessionId);
- expect(submit).toHaveBeenCalledWith({
- sessionId: consumed.sessionId,
- source: "thread-goal",
- allowQueue: true,
- message: expect.objectContaining({
- hideUserMessage: true,
- content: expect.stringContaining("call get_goal"),
- origin: expect.objectContaining({
- type: "thread-goal-continuation",
- goalId: consumed.goalId,
- kind: "active",
- }),
- }),
- clientRequestId: threadGoalRecoveryClientRequestId(consumed),
- });
- const recoveredContent = submit.mock.calls[0]?.[0].message.content;
- expect(recoveredContent).toContain("call get_goal");
- expect(recoveredContent).not.toContain(consumed.objective);
- expect(recoveredContent).not.toContain("<objective>");
- });
- it("replays a crashed budget-limit transition once with its stable identity", async () => {
- const limited = {
- ...goal,
- status: "budget_limited" as const,
- kickoffState: "consumed" as const,
- kickoffAttachments: [],
- tokensUsed: 100,
- tokenBudget: 100,
- updatedAt: 2_000,
- };
- let queued: ReturnType<typeof injectedQueueItem> | undefined;
- const submit = vi.fn(async (input: { clientRequestId?: string }) => {
- queued = {
- ...injectedQueueItem("thread-goal"),
- clientRequestId: input.clientRequestId ?? "missing-client-request-id",
- };
- return acceptedQueueTurn();
- });
- const fixture = makeQueueHost({
- recoverableBudgetLimitSummaries: [limited],
- conversation: {
- query: {
- getSession: async () => ({ ...session, status: "idle" }),
- listSessions: async () => [session],
- },
- ingress: {
- findQueuedByClientRequestId: vi.fn(async () => queued),
- submit,
- dispatchQueue: vi.fn(async () => undefined),
- },
- } as Pick<RuntimeConversation, "query" | "ingress">,
- });
- await fixture.host.recover();
- await fixture.host.recover();
- expect(submit).toHaveBeenCalledTimes(1);
- expect(submit).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: limited.sessionId,
- source: "thread-goal",
- allowQueue: true,
- clientRequestId: threadGoalBudgetLimitClientRequestId(limited),
- message: expect.objectContaining({
- hideUserMessage: true,
- content: expect.stringContaining("reached its token budget"),
- origin: expect.objectContaining({
- type: "thread-goal-continuation",
- goalId: limited.goalId,
- goalUpdatedAt: limited.updatedAt,
- kind: "budget-limit",
- }),
- }),
- }),
- );
- });
- it("does not duplicate an active Goal continuation that already survived restart", async () => {
- const consumed = {
- ...goal,
- kickoffState: "consumed" as const,
- kickoffAttachments: [],
- };
- const submit = vi.fn();
- const fixture = makeQueueHost({
- recoverableContinuations: [consumed],
- conversation: {
- query: {
- getSession: async () => ({ ...session, status: "idle" }),
- listSessions: async () => [session],
- },
- ingress: {
- findQueuedByClientRequestId: vi.fn(async () => undefined),
- listQueued: vi.fn(async () => [
- {
- ...injectedQueueItem("thread-goal"),
- message: {
- content: "hidden continuation",
- attachments: [],
- origin: {
- type: "thread-goal-continuation",
- goalId: consumed.goalId,
- goalUpdatedAt: consumed.updatedAt,
- objectiveDigest: digestThreadGoalObjective(
- consumed.objective,
- ),
- kind: "active",
- },
- },
- },
- ]),
- submit,
- dispatchQueue: vi.fn(async () => undefined),
- },
- } as Pick<RuntimeConversation, "query" | "ingress">,
- });
- await fixture.host.recover();
- expect(submit).not.toHaveBeenCalled();
- });
- it("reports an injected queue wake failure without leaking the rejection", async () => {
- const dispatchQueue = vi.fn(async () => {
- throw new Error("dispatch failed");
- });
- const fixture = makeQueueHost({
- conversation: {
- query: {
- getSession: async () => session,
- listSessions: async () => [session],
- },
- ingress: {
- findQueuedByClientRequestId: vi.fn(async () => undefined),
- submit: vi.fn(async () => acceptedQueueTurn()),
- dispatchQueue,
- },
- } as Pick<RuntimeConversation, "query" | "ingress">,
- });
- fixture.host.integrationDeps().requestQueueDispatch(session.sessionId);
- await vi.waitFor(() =>
- expect(fixture.reportFailure).toHaveBeenCalledWith(
- session.sessionId,
- "thread_goal_queue_dispatch_failed:dispatch failed",
- ),
- );
- });
- it("treats an exact claimed kickoff as already materialized", async () => {
- const submit = vi.fn();
- const claimed = {
- ...injectedQueueItem("thread-goal"),
- status: "claimed" as const,
- claimId: "claim-1",
- };
- const findQueuedByClientRequestId = vi.fn(async () => claimed);
- const fixture = makeQueueHost({
- conversation: {
- query: {
- getSession: async () => session,
- listSessions: async () => [session],
- },
- ingress: {
- findQueuedByClientRequestId,
- submit,
- dispatchQueue: vi.fn(),
- },
- } as Pick<RuntimeConversation, "query" | "ingress">,
- });
- await fixture.host
- .integrationDeps()
- .enqueueInitialContinuationTurn(
- goal,
- buildThreadGoalKickoffMessage(
- goal,
- "hidden continuation",
- goal.kickoffAttachments,
- ),
- `thread-goal-kickoff:${goal.goalId}`,
- );
- expect(findQueuedByClientRequestId).toHaveBeenCalledWith(
- session.sessionId,
- `thread-goal-kickoff:${goal.goalId}`,
- );
- expect(submit).not.toHaveBeenCalled();
- });
- });
- function injectedQueueItem(source: "api" | "thread-goal") {
- return {
- itemId: "queue-v2",
- sessionId: session.sessionId,
- agentName: session.agentName,
- source,
- status: "queued" as const,
- message: { content: goal.objective, attachments: [] },
- createdAt: 1_000,
- clientRequestId: `thread-goal-kickoff:${goal.goalId}`,
- };
- }
- function acceptedQueueTurn() {
- return {
- turnId: "turn-v2",
- mode: "queued" as const,
- queue: { itemId: "queue-v2", position: 1, ahead: 0 },
- completion: Promise.resolve({
- turnId: "turn-v2",
- status: "completed" as const,
- messages: [],
- }),
- };
- }
|