| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100 |
- import {
- type CreateLocalRuntimeHostOptions,
- type CreatedLocalRuntimeHost,
- } from "@mavis/local-runtime";
- import { DeferredRuntimeConversation } from "@mavis/conversation-contract";
- import type { ThreadGoalState, ThreadGoalStore } from "@mavis/goal";
- import type { AgentReferenceResolver } from "@mavis/shared";
- import { beforeEach, describe, expect, it, vi } from "vitest";
- import {
- cleanupFailedV1Startup,
- createDeferredAgentRuntimeTelemetry,
- createV1RuntimeCompatibility,
- withAgentStorageLock,
- } from "../../../../src/compat/v1/runtime.js";
- type ThreadGoalIntegration = CreatedLocalRuntimeHost["apiHost"]["threadGoal"];
- const mocked = vi.hoisted(() => ({
- adapter: { marker: "adapter" },
- agentHost: { marker: "agent-host-capabilities" },
- attachmentRegistration: { marker: "attachment-registration" },
- channel: { marker: "channel-capabilities" },
- bindCalls: [] as unknown[],
- cleanupBindCalls: [] as unknown[],
- deleteAgentCronTasks: vi.fn(async () => undefined),
- sessionCompatibility: { marker: "session-v2" },
- sessionCompatibilityHosts: [] as unknown[],
- agentResolver: {
- marker: "agent-resolver",
- } as unknown as AgentReferenceResolver,
- closeAgentDb: vi.fn(),
- inspectCollision: vi.fn(),
- applyCollision: vi.fn(),
- }));
- vi.mock("@mavis/local-runtime", async () => {
- const actual = await vi.importActual<Record<string, unknown>>(
- "@mavis/local-runtime",
- );
- return {
- ...actual,
- closeAgentDb: mocked.closeAgentDb,
- withAgentNameConflictMigrationLock: async (
- _dataDir: string,
- operation: (scope: {
- inspectCollision: () => unknown;
- applyCollision: (nowMs?: () => number) => unknown;
- }) => unknown,
- ) =>
- operation({
- inspectCollision: mocked.inspectCollision,
- applyCollision: mocked.applyCollision,
- }),
- };
- });
- vi.mock("../../../../src/compat/v1/cron.js", () => ({
- createV1MavisCronAdapterBridge: () => ({
- adapter: mocked.adapter,
- bindHandleRequest: (handleRequest: unknown) =>
- mocked.bindCalls.push(handleRequest),
- }),
- createV1CronAgentCleanupBridge: () => ({
- deleteAgentCronTasks: mocked.deleteAgentCronTasks,
- bind: (service: unknown) => mocked.cleanupBindCalls.push(service),
- }),
- }));
- vi.mock("../../../../src/compat/v1/agent-host.js", () => ({
- createV1AttachmentRegistration: () => mocked.attachmentRegistration,
- createV1AgentHostProductCapabilities: () => mocked.agentHost,
- createV1ChannelProductCapabilities: () => mocked.channel,
- }));
- vi.mock("../../../../src/compat/v1/session.js", () => ({
- createV1SessionCompatibility: (host: unknown) => {
- mocked.sessionCompatibilityHosts.push(host);
- return mocked.sessionCompatibility;
- },
- }));
- function resetRuntimeCompatibilityMocks(): void {
- mocked.bindCalls.length = 0;
- mocked.cleanupBindCalls.length = 0;
- mocked.deleteAgentCronTasks.mockClear();
- mocked.sessionCompatibilityHosts.length = 0;
- }
- describe("v1 runtime compatibility", () => {
- beforeEach(() => {
- resetRuntimeCompatibilityMocks();
- mocked.closeAgentDb.mockReset();
- mocked.inspectCollision.mockReset();
- mocked.applyCollision.mockReset();
- });
- it.each([true, false])(
- "passes resolved prompt autoUpdate=%s into service compatibility",
- (autoUpdate) => {
- const services = createCompatibility().createServiceCompatibility(
- createdHost({ configGetter: () => ({ promptConfig: { autoUpdate } }) }),
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- );
- expect(services.promptConfig).toEqual({ autoUpdate });
- },
- );
- it("revokes an escaped storage callback without closing a later cached connection", async () => {
- const laterCachedConnection = { closed: false };
- mocked.closeAgentDb.mockImplementation(() => {
- laterCachedConnection.closed = true;
- });
- mocked.applyCollision.mockImplementation(() => {
- throw new Error("underlying scope should not be reached after revoke");
- });
- let escaped: (() => unknown) | undefined;
- await withAgentStorageLock("/tmp/runtime", async (scope) => {
- escaped = scope.applyLegacyAgentStorage;
- });
- const escapedCallback = escaped;
- if (!escapedCallback) throw new Error("storage callback was not captured");
- expect(() => escapedCallback()).toThrow(
- "Agent storage lock scope is no longer active",
- );
- expect(mocked.applyCollision).not.toHaveBeenCalled();
- expect(mocked.closeAgentDb).not.toHaveBeenCalled();
- expect(laterCachedConnection.closed).toBe(false);
- });
- it("exposes the scan/apply seam and closes the cached Agent DB after apply", async () => {
- const inspection = {
- status: "skipped" as const,
- sourceKind: "legacy-agent-sqlite" as const,
- targetKind: "legacy-runtime-references" as const,
- sourceRowCount: 0,
- conflictCount: 0,
- mappings: [],
- referenceCounts: {
- primaryDatabase: 0,
- runtimeDatabase: 0,
- yamlFiles: 0,
- planFiles: 0,
- legacyUnclassified: 0,
- },
- skipCode: "source_missing" as const,
- };
- mocked.inspectCollision.mockReturnValue(inspection);
- mocked.applyCollision.mockReturnValue({
- status: "completed",
- mappings: [],
- });
- await withAgentStorageLock("/tmp/runtime", async (scope) => {
- expect(scope.inspectLegacyAgentStorage()).toBe(inspection);
- expect(scope.applyLegacyAgentStorage()).toEqual({
- status: "completed",
- mappings: [],
- });
- expect(scope).not.toHaveProperty("reportCutover");
- });
- expect(mocked.inspectCollision).toHaveBeenCalledOnce();
- expect(mocked.applyCollision).toHaveBeenCalledOnce();
- expect(mocked.closeAgentDb).toHaveBeenCalledWith("/tmp/runtime");
- });
- it("closes the cached Agent DB when apply throws", async () => {
- mocked.applyCollision.mockImplementation(() => {
- throw new Error("rewrite failed");
- });
- await expect(
- withAgentStorageLock("/tmp/runtime", async (scope) =>
- scope.applyLegacyAgentStorage(),
- ),
- ).rejects.toThrow("rewrite failed");
- expect(mocked.applyCollision).toHaveBeenCalledOnce();
- expect(mocked.closeAgentDb).toHaveBeenCalledWith("/tmp/runtime");
- });
- it("keeps the apply error primary when cached Agent DB cleanup also fails", async () => {
- const applyError = Object.assign(new Error("rewrite failed"), {
- failureStep: "primary_rewrite",
- migrationId: "agent-name-conflicts-17",
- });
- const closeError = new Error("close failed");
- mocked.applyCollision.mockImplementation(() => {
- throw applyError;
- });
- mocked.closeAgentDb.mockImplementation(() => {
- throw closeError;
- });
- let error: unknown;
- try {
- await withAgentStorageLock("/tmp/runtime", async (scope) =>
- scope.applyLegacyAgentStorage(),
- );
- } catch (caught) {
- error = caught;
- }
- expect(error).toMatchObject({
- failureStep: "primary_rewrite",
- migrationId: "agent-name-conflicts-17",
- });
- expect(error).toBeInstanceOf(AggregateError);
- const aggregate = error as AggregateError & { cause?: unknown };
- expect(aggregate.cause).toBe(applyError);
- expect(aggregate.errors).toEqual([applyError, closeError]);
- });
- it("retains the completed migration correlation id when cached Agent DB cleanup fails", async () => {
- const closeError = new Error("close failed");
- mocked.applyCollision.mockReturnValue({
- status: "completed",
- mappings: [],
- migrationId: "agent-name-conflicts-18",
- });
- mocked.closeAgentDb.mockImplementation(() => {
- throw closeError;
- });
- let error: unknown;
- try {
- await withAgentStorageLock("/tmp/runtime", async (scope) =>
- scope.applyLegacyAgentStorage(),
- );
- } catch (caught) {
- error = caught;
- }
- expect(error).toMatchObject({ migrationId: "agent-name-conflicts-18" });
- expect(error).toBeInstanceOf(AggregateError);
- expect((error as AggregateError & { cause?: unknown }).cause).toBe(
- closeError,
- );
- });
- });
- describe("v1 Agent reference compatibility", () => {
- function resolver(
- overrides: Partial<AgentReferenceResolver> = {},
- ): AgentReferenceResolver {
- return {
- resolveAgentReadScope: vi.fn(async (requestedName: string) => ({
- requestedName,
- canonicalName: "coder",
- primaryName: "mavis",
- compatibleNames: ["coder"],
- exact: true,
- source: "explicit_agent" as const,
- exactOwnerName: "coder",
- })),
- resolveAgentWriteTarget: vi.fn(async () => "coder"),
- requireExactAgentKey: vi.fn(async () => "coder"),
- resolveAgentExecutionTarget: vi.fn(async () => "coder"),
- ...overrides,
- };
- }
- function resolveDelegatable(
- targetResolver: AgentReferenceResolver,
- requestRef: string,
- ) {
- return createCompatibility()
- .createServiceCompatibility(
- createdHost({ agentResolver: targetResolver }),
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- )
- .agentReferences.resolveDelegatable(requestRef);
- }
- it("uses the complete Task resolver chain for a delegatable roster target", async () => {
- const targetResolver = resolver();
- await expect(
- resolveDelegatable(targetResolver, "agent:coder"),
- ).resolves.toBe("authorized");
- expect(targetResolver.resolveAgentReadScope).toHaveBeenCalledWith(
- "agent:coder",
- );
- expect(targetResolver.resolveAgentWriteTarget).toHaveBeenCalledWith(
- "agent:coder",
- );
- expect(targetResolver.requireExactAgentKey).toHaveBeenCalledWith("coder");
- expect(targetResolver.resolveAgentExecutionTarget).toHaveBeenCalledWith(
- "coder",
- );
- });
- it("rejects the canonical primary/self target before Task resolution", async () => {
- const targetResolver = resolver({
- resolveAgentReadScope: vi.fn(async (requestedName: string) => ({
- requestedName,
- canonicalName: "mavis",
- primaryName: "mavis",
- compatibleNames: ["mavis", "main"],
- exact: true,
- source: "explicit_agent" as const,
- exactOwnerName: "mavis",
- })),
- });
- await expect(
- resolveDelegatable(targetResolver, "agent:mavis"),
- ).resolves.toBe("unauthorized");
- expect(targetResolver.resolveAgentWriteTarget).not.toHaveBeenCalled();
- expect(targetResolver.requireExactAgentKey).not.toHaveBeenCalled();
- expect(targetResolver.resolveAgentExecutionTarget).not.toHaveBeenCalled();
- });
- it("fails closed when the trusted roster resolver cannot resolve a target", async () => {
- const targetResolver = resolver({
- resolveAgentReadScope: vi.fn(async () => {
- throw Object.assign(new Error("missing"), { code: "AGENT_NOT_FOUND" });
- }),
- });
- await expect(
- resolveDelegatable(targetResolver, "agent:missing"),
- ).resolves.toBe("unknown");
- expect(targetResolver.resolveAgentWriteTarget).not.toHaveBeenCalled();
- });
- });
- describe("v1 runtime compatibility Task recovery", () => {
- beforeEach(() => {
- resetRuntimeCompatibilityMocks();
- mocked.closeAgentDb.mockReset();
- mocked.inspectCollision.mockReset();
- mocked.applyCollision.mockReset();
- });
- it("binds task ownership and forwards exact recovered Turn evidence to the Task service", async () => {
- const bindRuntimeOwner = vi.fn();
- const reconcileStartupLostTasks = vi.fn(async () => []);
- const compatibility = createCompatibility();
- const host = createdHost({
- backgroundTaskService: {
- bindRuntimeOwner,
- reconcileStartupLostTasks,
- hasPendingStartupRecovery: () => true,
- pollStartupLostTasks: async () => false,
- },
- });
- const service = compatibility.createServiceCompatibility(host, {
- dataDir: "/tmp/runtime",
- runtimeOwnerKind: "tui",
- });
- const owner = {
- ownerId: "background-task:current",
- isOwnerAlive: () => true,
- };
- service.backgroundTasks.bindRuntimeOwner(owner);
- await expect(
- service.backgroundTasks.recover(["turn-recovered"]),
- ).resolves.toBe(true);
- await expect(service.backgroundTasks.pollRecovery()).resolves.toBe(false);
- expect(bindRuntimeOwner).toHaveBeenCalledWith(owner);
- expect(reconcileStartupLostTasks).toHaveBeenCalledWith({
- recoveredTurnIds: ["turn-recovered"],
- });
- });
- });
- describe("v1 runtime compatibility host composition", () => {
- beforeEach(() => {
- resetRuntimeCompatibilityMocks();
- mocked.closeAgentDb.mockReset();
- mocked.inspectCollision.mockReset();
- mocked.applyCollision.mockReset();
- });
- it.each([
- { kind: "cli", embedded: true, enabled: false },
- { kind: "tui", embedded: true, enabled: false },
- { kind: "cli", embedded: false, enabled: false },
- { kind: "tui", embedded: false, enabled: false },
- { kind: "electron", embedded: true, enabled: false },
- { kind: "diagnostic", embedded: true, enabled: false },
- ] as const)(
- "disables built-in Matrix while retaining configured MCP for $kind / embedded=$embedded",
- ({ kind, embedded, enabled }) => {
- const compatibility = createCompatibility();
- const host = createdHost({
- configGetter: () => ({ beta: { mcodeTools: true } }),
- });
- const services = compatibility.createServiceCompatibility(host, {
- dataDir: "/tmp/runtime",
- runtimeOwnerKind: kind,
- capabilities: { cliEmbedded: embedded },
- enableLiveMcp: true,
- });
- expect(services.mcp).toMatchObject({
- enableLiveMcp: true,
- builtinMatrix: enabled,
- matrixWebSearchOnly: false,
- });
- },
- );
- it("caps the background task listing at 100 results", async () => {
- const tasks = Array.from({ length: 101 }, (_, index) => ({
- taskId: `bg-${index}`,
- kind: "bash" as const,
- status: "running" as const,
- ownerSessionId: "session-1",
- createdAt: 100,
- updatedAt: 200,
- }));
- const services = createCompatibility().createServiceCompatibility(
- createdHost({
- backgroundTaskService: {
- list: async ({ limit }: { limit: number }) => ({
- items: tasks.slice(0, limit),
- }),
- },
- }),
- { dataDir: "/tmp/runtime", runtimeOwnerKind: "cli" },
- );
- await expect(
- services.peripherals.backgroundTasks.list({
- ownerSessionId: "session-1",
- limit: 200,
- }),
- ).resolves.toEqual(tasks.slice(0, 100));
- });
- it("routes Agent cleanup through the bound V2 Cron owner", async () => {
- const compatibility = createCompatibility();
- const configured = compatibility.configureHostOptions(
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- mocked.agentResolver,
- preparedPorts(),
- );
- const services = compatibility.createServiceCompatibility(createdHost({}), {
- dataDir: "/tmp/runtime",
- } as CreateLocalRuntimeHostOptions);
- const cron = { deleteDefinitionsByAgent: vi.fn() };
- services.cron.bindAgentCleanup(cron);
- await configured.deleteAgentCronTasks?.("coder");
- expect(mocked.cleanupBindCalls).toEqual([cron]);
- expect(mocked.deleteAgentCronTasks).toHaveBeenCalledWith("coder");
- });
- });
- describe("v1 runtime compatibility seams", () => {
- beforeEach(() => {
- resetRuntimeCompatibilityMocks();
- mocked.closeAgentDb.mockReset();
- mocked.inspectCollision.mockReset();
- mocked.applyCollision.mockReset();
- });
- it("shares one deferred conversation bridge across host options and service binding", async () => {
- const compatibility = createCompatibility();
- const configured = compatibility.configureHostOptions(
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- mocked.agentResolver,
- preparedPorts(),
- );
- const serviceCompatibility = compatibility.createServiceCompatibility(
- createdHost({
- subscribeGlobalEvents: vi.fn(() => () => undefined),
- }),
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- );
- const pending =
- configured.runtimeConversation?.query.getSession("session-1");
- const provider = {
- query: {
- getSession: vi.fn(async () => undefined),
- listSessions: vi.fn(async () => []),
- listMessages: vi.fn(async () => ({ messages: [], hasMore: false })),
- },
- lifecycle: {},
- ingress: {},
- maintenance: {},
- } as never;
- await serviceCompatibility.conversation.bind(provider);
- await expect(pending).resolves.toBeUndefined();
- compatibility.shutdownConversation(new Error("runtime close"));
- await expect(
- configured.runtimeConversation?.query.listSessions(),
- ).rejects.toMatchObject({
- code: "RUNTIME_CONVERSATION_SHUTDOWN",
- });
- });
- it("reconciles persisted Goal kickoffs before exposing the conversation provider", async () => {
- const compatibility = createCompatibility();
- const configured = compatibility.configureHostOptions(
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- mocked.agentResolver,
- preparedPorts(),
- );
- const recovery = deferred();
- const recoverThreadGoalKickoffs = vi.fn(async () => recovery.promise);
- const serviceCompatibility = compatibility.createServiceCompatibility(
- createdHost({
- subscribeGlobalEvents: vi.fn(() => () => undefined),
- recoverThreadGoalKickoffs,
- }),
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- );
- const getSession = vi.fn(async () => undefined);
- const provider = {
- query: {
- getSession,
- listSessions: vi.fn(async () => []),
- listMessages: vi.fn(async () => ({ messages: [], hasMore: false })),
- },
- lifecycle: {},
- ingress: {},
- maintenance: {},
- } as never;
- const pendingQuery =
- configured.runtimeConversation?.query.getSession("session-1");
- const binding = serviceCompatibility.conversation.bind(provider);
- await Promise.resolve();
- expect(recoverThreadGoalKickoffs).toHaveBeenCalledWith(provider);
- expect(getSession).not.toHaveBeenCalled();
- recovery.resolve();
- await binding;
- await expect(pendingQuery).resolves.toBeUndefined();
- expect(getSession).toHaveBeenCalledWith("session-1");
- });
- it("does not expose model catalog or provider ownership through v1 compatibility", () => {
- const services = createCompatibility().createServiceCompatibility(
- createdHost({}),
- {
- dataDir: "/tmp/runtime",
- } as CreateLocalRuntimeHostOptions,
- );
- expect(services.peripherals).not.toHaveProperty("models");
- expect(services.peripherals).not.toHaveProperty("modelProviders");
- });
- });
- describe("v1 runtime compatibility adapters and cleanup", () => {
- beforeEach(resetRuntimeCompatibilityMocks);
- it("injects a write-only global event publisher and detaches it without subscribing to v1", () => {
- const compatibility = createCompatibility();
- const configured = compatibility.configureHostOptions(
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- mocked.agentResolver,
- preparedPorts(),
- );
- const apiHost = { subscribeGlobalEvents: vi.fn(() => () => undefined) };
- const host = createdHost(apiHost);
- const publish = vi.fn();
- const event = {
- type: "session.deleted" as const,
- payload: { sessionId: "session-1" },
- };
- const events = compatibility.createServiceCompatibility(host, {
- dataDir: "/tmp/runtime",
- } as CreateLocalRuntimeHostOptions).events;
- const unbind = events.bindPublisher(publish);
- configured.globalEventPublisher?.(event);
- unbind();
- configured.globalEventPublisher?.({
- type: "session.deleted",
- payload: { sessionId: "late-session" },
- });
- expect(apiHost.subscribeGlobalEvents).not.toHaveBeenCalled();
- expect(publish).toHaveBeenCalledOnce();
- expect(publish).toHaveBeenCalledWith(event);
- });
- });
- describe("v1 safety compatibility", () => {
- it("keeps the concrete content-safety transport in the v1 compatibility seam", () => {
- const compatibility = createCompatibility();
- const contentSafetyChecker = vi.fn(async () => ({ pass: true as const }));
- const services = compatibility.createServiceCompatibility(
- createdHost({ contentSafetyChecker }),
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- );
- expect(services.safety.review).toBe(contentSafetyChecker);
- });
- });
- describe("v1 runtime cleanup", () => {
- it("best-effort cleans every acquired v1 resource in dependency order", async () => {
- const events: string[] = [];
- const warnings: string[] = [];
- const v1 = {
- apiHost: {
- cronRuntime: { stop: () => events.push("cron") },
- shutdownChannelSubsystem: () => {
- events.push("channels");
- throw new Error("channel cleanup failed");
- },
- },
- metricsClient: { close: () => events.push("metrics") },
- } as unknown as CreatedLocalRuntimeHost;
- await cleanupFailedV1Startup(
- v1,
- async () => {
- events.push("close");
- },
- (step) => warnings.push(step),
- );
- expect(events).toEqual(["cron", "channels", "close", "metrics"]);
- expect(warnings).toEqual(["stop_channels"]);
- });
- it("continues cleanup when the warning reporter throws", async () => {
- const events: string[] = [];
- const v1 = {
- apiHost: {
- cronRuntime: {
- stop: () => {
- events.push("cron");
- throw new Error("cron cleanup failed");
- },
- },
- shutdownChannelSubsystem: () => events.push("channels"),
- },
- metricsClient: { close: () => events.push("metrics") },
- } as unknown as CreatedLocalRuntimeHost;
- await cleanupFailedV1Startup(
- v1,
- async () => {
- events.push("close");
- },
- () => {
- throw new Error("warning reporter failed");
- },
- );
- expect(events).toEqual(["cron", "channels", "close", "metrics"]);
- });
- });
- describe("v1 Agent telemetry compatibility", () => {
- it("bridges Agent facts to v1 telemetry and remains fail-open", () => {
- const counter = vi.fn();
- const gauge = vi.fn();
- const histogram = vi.fn();
- const emitBusEvent = vi.fn();
- const telemetry = createDeferredAgentRuntimeTelemetry();
- telemetry.bind({
- metricsClient: { counter, gauge, histogram },
- emitBusEvent,
- });
- telemetry.facts.onNameCompatResolve?.({
- intent: "read",
- canonicalClass: "worker",
- source: "canonical_name",
- success: true,
- memberCountBucket: "2",
- });
- telemetry.facts.onAgentRoleObservation?.({
- status: "unsupported",
- source: "sqlite_decode",
- role: "worker",
- });
- expect(counter).toHaveBeenCalledWith("agent_name_compat_resolve_total", 1, {
- intent: "read",
- canonical_class: "worker",
- source: "canonical_name",
- success: "true",
- member_count_bucket: "2",
- });
- expect(counter).toHaveBeenCalledWith("agent_role_observation_total", 1, {
- status: "unsupported",
- source: "sqlite_decode",
- role_class: "worker",
- });
- expect(emitBusEvent).toHaveBeenCalledWith("agent_name_compat.resolve", {
- intent: "read",
- canonical_class: "worker",
- source: "canonical_name",
- success: true,
- member_count_bucket: "2",
- });
- const failingTelemetry = createDeferredAgentRuntimeTelemetry();
- failingTelemetry.bind({
- metricsClient: {
- counter: () => {
- throw new Error("metrics unavailable");
- },
- gauge: () => {
- throw new Error("metrics unavailable");
- },
- histogram: () => {
- throw new Error("metrics unavailable");
- },
- },
- emitBusEvent: () => {
- throw new Error("event bus unavailable");
- },
- });
- expect(() => {
- failingTelemetry.facts.onNameCompatResolve?.({
- intent: "write",
- canonicalClass: "other",
- source: "explicit_agent",
- success: false,
- });
- failingTelemetry.facts.onAgentRoleObservation?.({
- status: "missing",
- source: "builtin_seed",
- role: undefined,
- });
- failingTelemetry.close();
- failingTelemetry.facts.onNameCompatResolve?.({
- intent: "read",
- canonicalClass: "other",
- source: "stable_name",
- success: true,
- });
- }).not.toThrow();
- });
- });
- const goal: ThreadGoalState = {
- goalId: "goal-1",
- sessionId: "session-1",
- objective: "Ship Goal support",
- status: "active",
- createdAt: 10,
- updatedAt: 20,
- tokensUsed: 4,
- turnsUsed: 1,
- timeUsedSeconds: 2,
- tokenBudget: null,
- replyFingerprint: "reply-fingerprint",
- noProgressStreak: 0,
- noToolStreak: 0,
- lastVerification: {
- v: 1,
- backend: "evaluator",
- verdict: "not_met",
- reason: "Acceptance evidence is still missing.",
- missing: ["focused test evidence"],
- notMetStreak: 1,
- turnId: "turn-1",
- objectiveDigest: "objective-digest",
- at: 20,
- },
- statusReason: null,
- kickoffAttachments: [],
- kickoffState: "consumed",
- executionWait: null,
- };
- function createThreadGoalFixture() {
- const isReadOnlyLegacySession = vi.fn(async () => false);
- const store = {
- create: vi.fn(
- async (_input: Parameters<ThreadGoalStore["create"]>[0]) => goal,
- ),
- getBySession: vi.fn(async () => goal),
- patch: vi.fn(async (_goalId: string, patch: Partial<ThreadGoalState>) => ({
- ...goal,
- ...patch,
- })),
- delete: vi.fn(async () => undefined),
- };
- const integration = {
- isEnabled: vi.fn(() => true),
- store,
- getGoalForResponse: vi.fn<ThreadGoalIntegration["getGoalForResponse"]>(
- async () => goal,
- ),
- createGoal: vi.fn(
- async (
- input: Parameters<ThreadGoalStore["create"]>[0],
- _options?: { readonly requireKickoffAdmission?: boolean },
- ) => store.create(input),
- ),
- patchGoal: vi.fn<ThreadGoalIntegration["patchGoal"]>(
- async (_sessionId: string, patch: Partial<ThreadGoalState>) => ({
- ...goal,
- ...patch,
- }),
- ),
- deleteGoal: vi.fn(async (_sessionId: string) => true),
- handleChanged: vi.fn(),
- };
- const application = createCompatibility().createServiceCompatibility(
- createdHost(
- {
- threadGoal: integration,
- isReadOnlyLegacySession,
- skillService: { listRuntimeSkills: async () => ({ skills: [] }) },
- skillHubStore: {
- getInstalledIdentities: async () => ({
- names: new Set<string>(),
- sourceUrls: new Set<string>(),
- }),
- },
- },
- {
- metricsClient: {},
- mcpService: { listServers: async () => [] },
- },
- ),
- { dataDir: "/tmp/runtime" } as CreateLocalRuntimeHostOptions,
- ).peripherals.goals;
- return {
- application,
- integration,
- isReadOnlyLegacySession,
- store,
- };
- }
- describe("createThreadGoalApplication", () => {
- it("creates and reads the Runtime-owned Goal without a transport hop", async () => {
- const fixture = createThreadGoalFixture();
- await expect(
- fixture.application.create({
- sessionId: " session-1 ",
- objective: " Ship Goal support ",
- tokenBudget: 50_000,
- kickoffAttachments: [
- {
- type: "file",
- filePath: "/repo/spec.md",
- fileName: "spec.md",
- mimeType: "text/markdown",
- },
- ],
- }),
- ).resolves.toMatchObject({
- goalId: "goal-1",
- status: "active",
- turnsUsed: 1,
- statusReason: null,
- lastVerification: {
- backend: "evaluator",
- verdict: "not_met",
- missing: ["focused test evidence"],
- },
- });
- expect(fixture.store.create).toHaveBeenCalledWith({
- sessionId: "session-1",
- objective: "Ship Goal support",
- tokenBudget: 50_000,
- kickoffAttachments: [expect.objectContaining({ fileName: "spec.md" })],
- });
- expect(fixture.integration.createGoal).toHaveBeenCalledWith(
- {
- sessionId: "session-1",
- objective: "Ship Goal support",
- tokenBudget: 50_000,
- kickoffAttachments: [expect.objectContaining({ fileName: "spec.md" })],
- },
- { requireKickoffAdmission: true },
- );
- await expect(fixture.application.get("session-1")).resolves.toMatchObject({
- objective: "Ship Goal support",
- hasKickoffAttachments: false,
- });
- });
- it("omits the verification result until the Runtime has produced one", async () => {
- const fixture = createThreadGoalFixture();
- fixture.integration.getGoalForResponse
- .mockResolvedValueOnce({ ...goal, lastVerification: undefined })
- .mockResolvedValueOnce(undefined);
- const projected = await fixture.application.get("session-1");
- expect(projected).not.toHaveProperty("lastVerification");
- await expect(fixture.application.get("session-1")).resolves.toBeUndefined();
- });
- it("rejects an empty Goal session id before reaching the Runtime integration", async () => {
- const fixture = createThreadGoalFixture();
- await expect(fixture.application.get(" ")).rejects.toThrow(
- "sessionId is required.",
- );
- expect(fixture.integration.getGoalForResponse).not.toHaveBeenCalled();
- });
- it("uses Desktop soft-fail kickoff admission for a text-only Goal", async () => {
- const fixture = createThreadGoalFixture();
- await expect(
- fixture.application.create({
- sessionId: "session-1",
- objective: "Ship without attachments",
- }),
- ).resolves.toMatchObject({ goalId: "goal-1", status: "active" });
- expect(fixture.integration.createGoal).toHaveBeenCalledWith({
- sessionId: "session-1",
- objective: "Ship without attachments",
- });
- });
- it("uses live projection for resume and publishes edit and clear changes", async () => {
- const fixture = createThreadGoalFixture();
- const resumed = { ...goal, status: "active" as const, timeUsedSeconds: 12 };
- fixture.integration.patchGoal.mockResolvedValueOnce(resumed);
- await expect(
- fixture.application.patch("session-1", { status: "active" }),
- ).resolves.toMatchObject({
- status: "active",
- timeUsedSeconds: 12,
- });
- expect(fixture.integration.patchGoal).toHaveBeenCalledWith("session-1", {
- status: "active",
- });
- await fixture.application.patch("session-1", {
- objective: "New objective",
- tokenBudget: null,
- });
- expect(fixture.integration.patchGoal).toHaveBeenLastCalledWith(
- "session-1",
- {
- objective: "New objective",
- tokenBudget: null,
- },
- );
- await fixture.application.patch("session-1", { tokenBudget: 75_000 });
- expect(fixture.integration.patchGoal).toHaveBeenLastCalledWith(
- "session-1",
- {
- tokenBudget: 75_000,
- },
- );
- await expect(fixture.application.clear("session-1")).resolves.toBe(true);
- expect(fixture.integration.deleteGoal).toHaveBeenCalledWith("session-1");
- });
- it("rejects a Goal patch after all mutable fields are omitted", async () => {
- const fixture = createThreadGoalFixture();
- await expect(fixture.application.patch("session-1", {})).rejects.toThrow(
- "Goal patch must include status, objective, or tokenBudget.",
- );
- expect(fixture.integration.patchGoal).not.toHaveBeenCalled();
- });
- it("preserves objective validation and the missing-Goal patch contract", async () => {
- const fixture = createThreadGoalFixture();
- await expect(
- fixture.application.patch("session-1", { objective: " " }),
- ).rejects.toThrow("goal objective must not be empty");
- fixture.integration.patchGoal.mockResolvedValueOnce(undefined);
- await expect(
- fixture.application.patch("session-1", { objective: "still shipping" }),
- ).rejects.toMatchObject({
- status: 404,
- code: "GOAL_NOT_FOUND",
- message: "Goal not found for session: session-1",
- });
- });
- it("reports the Desktop GOAL_NOT_FOUND contract when clear loses the Goal race", async () => {
- const fixture = createThreadGoalFixture();
- fixture.integration.deleteGoal.mockResolvedValue(false);
- await expect(fixture.application.clear("session-1")).rejects.toMatchObject({
- status: 404,
- code: "GOAL_NOT_FOUND",
- message: "Goal not found for session: session-1",
- });
- });
- it("rejects every Goal mutation for a read-only legacy session", async () => {
- const fixture = createThreadGoalFixture();
- fixture.isReadOnlyLegacySession.mockResolvedValue(true);
- await expect(
- fixture.application.create({
- sessionId: "session-1",
- objective: "Do not mutate legacy state",
- }),
- ).rejects.toThrow("read-only");
- await expect(
- fixture.application.patch("session-1", { status: "paused" }),
- ).rejects.toThrow("read-only");
- await expect(fixture.application.clear("session-1")).rejects.toThrow(
- "read-only",
- );
- expect(fixture.isReadOnlyLegacySession).toHaveBeenCalledTimes(3);
- expect(fixture.integration.createGoal).not.toHaveBeenCalled();
- expect(fixture.integration.patchGoal).not.toHaveBeenCalled();
- expect(fixture.integration.deleteGoal).not.toHaveBeenCalled();
- });
- it("fails closed when Thread Goal is disabled", async () => {
- const fixture = createThreadGoalFixture();
- fixture.integration.isEnabled.mockReturnValue(false);
- expect(fixture.application.isEnabled()).toBe(false);
- await expect(fixture.application.get("session-1")).rejects.toThrow(
- "disabled",
- );
- expect(fixture.integration.getGoalForResponse).not.toHaveBeenCalled();
- });
- });
- function createCompatibility() {
- const compatibility = createV1RuntimeCompatibility(
- new DeferredRuntimeConversation(),
- {
- createQuestionnaireService: () =>
- ({ marker: "questionnaire-service" }) as never,
- },
- );
- return {
- ...compatibility,
- createServiceCompatibility: (
- host: CreatedLocalRuntimeHost,
- options: CreateLocalRuntimeHostOptions,
- ) => compatibility.createServiceCompatibility(host, options),
- };
- }
- function preparedPorts() {
- return {
- skillEnabledState: {
- getDisabledLocationUris: vi.fn(async () => new Set<string>()),
- setEnabled: vi.fn(async () => undefined),
- forget: vi.fn(async () => undefined),
- },
- runLegacyImCredentialMigration: vi.fn(async (action: () => Promise<void>) =>
- action(),
- ),
- cliSunsetNotice: {
- evaluate: vi.fn(async () => undefined),
- },
- };
- }
- function createdHost(
- apiHost: object,
- host: object = {},
- ): CreatedLocalRuntimeHost {
- return {
- dataDir: "/tmp/runtime",
- apiHost: {
- configGetter: () => ({ promptConfig: { autoUpdate: false } }),
- recoverThreadGoalKickoffs: async () => undefined,
- contentSafetyChecker: vi.fn(async () => ({ pass: true })),
- bindQuestionnaireOwnedActionHandler: vi.fn(),
- questionnaireServiceDeps: () => ({
- store: {},
- nowMs: Date.now,
- primaryAgentName: "mavis",
- configGetter: () => ({}),
- getSessionById: vi.fn(),
- startUserMessageTurn: vi.fn(),
- emitBusEvent: vi.fn(),
- publishGlobalEvent: vi.fn(),
- }),
- ...apiHost,
- },
- ...host,
- } as unknown as CreatedLocalRuntimeHost;
- }
- function deferred() {
- let resolvePromise: (() => void) | undefined;
- const promise = new Promise<void>((resolve) => {
- resolvePromise = resolve;
- });
- return { promise, resolve: () => resolvePromise?.() };
- }
|