| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /**
- * Unit tests for the Thread Goal wiring helpers — SSE bus projection +
- * the codex `continue_if_idle` kick.
- *
- * Pins:
- * (a) external set-active on an idle session starts a hidden
- * continuation turn with the rendered append-only prompt,
- * (b) a busy session is never double-driven (the in-stream
- * orchestrator owns continuation mid-turn),
- * (c) non-active results and deletes never kick,
- * (d) kick failures are swallowed (busy-lock races are benign),
- * (e) handleThreadGoalChanged projects the bus event for every
- * mutation type and only kicks on active.
- */
- import { describe, expect, it, vi } from "vitest";
- import type { ThreadGoalState } from "@mavis/goal";
- import {
- buildThreadGoalRuntimeTools,
- handleThreadGoalChanged,
- maybeKickThreadGoalContinuation,
- } from "../../src/thread-goal/wiring.js";
- function goal(overrides: Partial<ThreadGoalState> = {}): ThreadGoalState {
- return {
- goalId: "tg_wiring_1",
- sessionId: "sess_wiring",
- objective: "Ship the wiring tests",
- status: "active",
- createdAt: 1_700_000_000_000,
- updatedAt: 1_700_000_000_000,
- tokensUsed: 0,
- turnsUsed: 0,
- timeUsedSeconds: 0,
- tokenBudget: null,
- replyFingerprint: null,
- noProgressStreak: 0,
- noToolStreak: 0,
- lastVerification: undefined,
- statusReason: null,
- kickoffAttachments: [],
- kickoffState: "consumed",
- executionWait: null,
- ...overrides,
- };
- }
- describe("maybeKickThreadGoalContinuation", () => {
- it("starts a continuation turn when the session is idle and the goal is active", async () => {
- const startContinuationTurn = vi.fn(async () => undefined);
- await maybeKickThreadGoalContinuation(
- { type: "updated", goal: goal() },
- { isSessionBusy: () => false, startContinuationTurn },
- );
- expect(startContinuationTurn).toHaveBeenCalledTimes(1);
- const [sessionId, content] = startContinuationTurn.mock.calls[0] as [
- string,
- string,
- ];
- expect(sessionId).toBe("sess_wiring");
- expect(content).toContain("Continue working toward the active thread goal");
- expect(content).not.toContain("Ship the wiring tests");
- expect(content).not.toContain("<objective>");
- });
- it("kicks on created events too (POST /goal while idle = start working)", async () => {
- const startContinuationTurn = vi.fn(async () => undefined);
- await maybeKickThreadGoalContinuation(
- { type: "created", goal: goal() },
- { isSessionBusy: () => false, startContinuationTurn },
- );
- expect(startContinuationTurn).toHaveBeenCalledTimes(1);
- const [, content] = startContinuationTurn.mock.calls[0] as [string, string];
- expect(content).toContain(
- "<objective>\nShip the wiring tests\n</objective>",
- );
- expect(content).toContain("Completion audit:");
- });
- it("does not kick while the session has a running turn", async () => {
- const startContinuationTurn = vi.fn(async () => undefined);
- await maybeKickThreadGoalContinuation(
- { type: "updated", goal: goal() },
- { isSessionBusy: () => true, startContinuationTurn },
- );
- expect(startContinuationTurn).not.toHaveBeenCalled();
- });
- it.each([
- "paused",
- "blocked",
- "complete",
- "budget_limited",
- "usage_limited",
- ] as const)(
- "does not kick when the resulting status is %s",
- async (status) => {
- const startContinuationTurn = vi.fn(async () => undefined);
- await maybeKickThreadGoalContinuation(
- { type: "updated", goal: goal({ status }) },
- { isSessionBusy: () => false, startContinuationTurn },
- );
- expect(startContinuationTurn).not.toHaveBeenCalled();
- },
- );
- it("does not kick on delete events", async () => {
- const startContinuationTurn = vi.fn(async () => undefined);
- await maybeKickThreadGoalContinuation(
- { type: "deleted", goalId: "tg_wiring_1", sessionId: "sess_wiring" },
- { isSessionBusy: () => false, startContinuationTurn },
- );
- expect(startContinuationTurn).not.toHaveBeenCalled();
- });
- it("swallows turn-start failures (busy-lock race is benign)", async () => {
- const startContinuationTurn = vi.fn(async () => {
- throw new Error("local_session_busy");
- });
- await expect(
- maybeKickThreadGoalContinuation(
- { type: "updated", goal: goal() },
- { isSessionBusy: () => false, startContinuationTurn },
- ),
- ).resolves.toBeUndefined();
- });
- });
- describe("handleThreadGoalChanged", () => {
- it("emits thread_goal.updated and kicks for an active mutation", async () => {
- const emitBusEvent = vi.fn();
- const startContinuationTurn = vi.fn(async () => undefined);
- handleThreadGoalChanged(
- { type: "updated", goal: goal() },
- {
- publishGlobalEvent: emitBusEvent,
- isSessionBusy: () => false,
- startContinuationTurn,
- },
- );
- expect(emitBusEvent).toHaveBeenCalledWith(
- expect.objectContaining({
- type: "thread_goal.updated",
- payload: expect.objectContaining({
- goal: expect.objectContaining({
- goalId: "tg_wiring_1",
- status: "active",
- }),
- }),
- }),
- );
- await vi.waitFor(() =>
- expect(startContinuationTurn).toHaveBeenCalledTimes(1),
- );
- });
- it("emits thread_goal.cleared and never kicks for deletes", () => {
- const emitBusEvent = vi.fn();
- const startContinuationTurn = vi.fn(async () => undefined);
- handleThreadGoalChanged(
- { type: "deleted", goalId: "tg_wiring_1", sessionId: "sess_wiring" },
- {
- publishGlobalEvent: emitBusEvent,
- isSessionBusy: () => false,
- startContinuationTurn,
- },
- );
- expect(emitBusEvent).toHaveBeenCalledWith({
- type: "thread_goal.cleared",
- payload: { goalId: "tg_wiring_1", sessionId: "sess_wiring" },
- });
- expect(startContinuationTurn).not.toHaveBeenCalled();
- });
- it("emits the bus event but does not kick for a paused mutation", () => {
- const emitBusEvent = vi.fn();
- const startContinuationTurn = vi.fn(async () => undefined);
- handleThreadGoalChanged(
- { type: "updated", goal: goal({ status: "paused" }) },
- {
- publishGlobalEvent: emitBusEvent,
- isSessionBusy: () => false,
- startContinuationTurn,
- },
- );
- expect(emitBusEvent).toHaveBeenCalledTimes(1);
- expect(startContinuationTurn).not.toHaveBeenCalled();
- });
- it("projects usage, reason, and verifier summary onto thread_goal.updated", () => {
- // MR-1 accounting parity: SSE payload must carry tokensUsed +
- // timeUsedSeconds + (MR-2) tokenBudget so the banner can render
- // `(12.5K / 50K)` or `(12.5K · 2m)` without a manual REST refresh.
- const emitBusEvent = vi.fn();
- const startContinuationTurn = vi.fn(async () => undefined);
- handleThreadGoalChanged(
- {
- type: "updated",
- goal: goal({
- status: "paused",
- tokensUsed: 12_500,
- turnsUsed: 4,
- timeUsedSeconds: 137,
- tokenBudget: 50_000,
- statusReason: "paused(no_progress)",
- lastVerification: {
- v: 1,
- backend: "evaluator",
- verdict: "not_met",
- reason: "Missing tests",
- missing: ["focused tests"],
- notMetStreak: 2,
- turnId: "turn_1",
- objectiveDigest: "digest",
- at: 1_700_000_000_000,
- },
- lastWorkerProposal: {
- v: 1,
- source: "worker",
- type: "blocked",
- turnId: "turn_1",
- summary: "private blocker detail",
- at: 1_700_000_000_001,
- },
- }),
- },
- {
- publishGlobalEvent: emitBusEvent,
- isSessionBusy: () => false,
- startContinuationTurn,
- },
- );
- expect(emitBusEvent).toHaveBeenCalledWith(
- expect.objectContaining({
- type: "thread_goal.updated",
- payload: expect.objectContaining({
- goal: expect.objectContaining({
- tokensUsed: 12_500,
- turnsUsed: 4,
- timeUsedSeconds: 137,
- tokenBudget: 50_000,
- statusReason: "paused(no_progress)",
- lastVerification: {
- backend: "evaluator",
- verdict: "not_met",
- reason: "Missing tests",
- missing: ["focused tests"],
- notMetStreak: 2,
- at: 1_700_000_000_000,
- },
- }),
- }),
- }),
- );
- expect(JSON.stringify(emitBusEvent.mock.calls[0]?.[0])).not.toContain(
- "private blocker detail",
- );
- });
- });
- describe("buildThreadGoalRuntimeTools", () => {
- const signalCollector = { collect: () => "accepted" as const };
- const fakeStore = {
- getBySession: async () => undefined,
- getById: async () => undefined,
- create: async () => goal(),
- patch: async () => goal(),
- updateBreaker: async () => ({
- action: "stale" as const,
- staleReason: "missing_goal" as const,
- }),
- delete: async () => undefined,
- };
- it("does not expose create_goal and returns only lifecycle tools when enabled", () => {
- const tools = buildThreadGoalRuntimeTools(
- fakeStore,
- false,
- signalCollector,
- );
- expect(tools.map((tool) => tool.def.name).sort()).toEqual([
- "get_goal",
- "update_goal",
- ]);
- });
- it("returns no tools when disabled", () => {
- expect(
- buildThreadGoalRuntimeTools(fakeStore, true, signalCollector),
- ).toEqual([]);
- });
- });
|