| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- import { afterEach, describe, expect, it, vi } from 'vitest';
- import { RespDataType } from '@mavis/agent-core/protocol/agent-message';
- import { RuntimeEventStatus, RuntimeEventType, type IRuntimeEvent } from '@mavis/protocol';
- import { LocalRuntimeHost, type LocalTurnRunner } from '../../src/runtime/host.js';
- import { OUTPUT_REVISION_INSTRUCTION } from '../../src/runtime/output-safety-policy.js';
- const baseTurn = {
- sessionId: 'ses',
- turnId: 'turn',
- workspaceDir: '/tmp/ws',
- systemPrompt: 'base',
- userMessage: { text: 'question' },
- reviewUserInput: 'question',
- llm: { model: { api: 'anthropic-messages', provider: 'minimax', id: 'test' } as never },
- };
- const finalMessage = (content: string): IRuntimeEvent =>
- ({
- schema: 'runtime.event/v1',
- event_id: 'final',
- session_id: 'ses',
- turn_id: 'turn',
- type: RuntimeEventType.STREAM_RESP,
- payload: {
- stream_resp: JSON.stringify({
- type: RespDataType.AgentMessage,
- agent_message: { msg_id: 'answer', msg_content: content },
- }),
- },
- }) as IRuntimeEvent;
- const pass = { action: 1 };
- function installInputVerdict(verdict: unknown) {
- return vi.spyOn(globalThis, 'fetch').mockImplementation(async (_url, init) => {
- const body = JSON.parse(String(init?.body));
- return Response.json(body.scene === 110 ? verdict : pass);
- });
- }
- afterEach(() => vi.restoreAllMocks());
- describe('LocalRuntimeHost V2 input safety', () => {
- it('starts generation while input review is pending', async () => {
- let resolveReview!: (response: Response) => void;
- const pending = new Promise<Response>((resolve) => {
- resolveReview = resolve;
- });
- vi.spyOn(globalThis, 'fetch')
- .mockImplementationOnce(() => pending)
- .mockImplementation(async () => Response.json(pass));
- const runTurn = vi.fn(async (input) => {
- await input.eventWriter.pushRuntime(finalMessage('answer'));
- });
- const run = new LocalRuntimeHost({ safetyApiVersion: 'v2', piRunner: { runTurn } }).runTurn(
- baseTurn,
- );
- await vi.waitFor(() => expect(runTurn).toHaveBeenCalledOnce());
- resolveReview(Response.json(pass));
- expect((await run).retracted).toBe(false);
- expect(runTurn).toHaveBeenCalledOnce();
- });
- it('cancels a terminal waiting for input review without waiting for its HTTP response', async () => {
- let resolveReview!: (response: Response) => void;
- const pendingReview = new Promise<Response>((resolve) => {
- resolveReview = resolve;
- });
- vi.spyOn(globalThis, 'fetch').mockImplementation(() => pendingReview);
- const abort = new AbortController();
- const observer = vi.fn();
- const runTurn = vi.fn(async (input) => {
- await input.eventWriter.pushRuntime({
- schema: 'runtime.event/v1',
- event_id: 'held-completed',
- session_id: baseTurn.sessionId,
- turn_id: baseTurn.turnId,
- type: RuntimeEventType.SESSION_STATUS,
- payload: { status: RuntimeEventStatus.COMPLETED },
- });
- });
- let output: Awaited<ReturnType<LocalRuntimeHost['runTurn']>> | undefined;
- const run = (async () => {
- output = await new LocalRuntimeHost({
- safetyApiVersion: 'v2',
- piRunner: { runTurn },
- }).runTurn({
- ...baseTurn,
- signal: abort.signal,
- onInputReviewResolved: observer,
- });
- })();
- try {
- await vi.waitFor(() => expect(runTurn).toHaveBeenCalledOnce());
- abort.abort();
- await vi.waitFor(() => expect(output).toBeDefined(), { timeout: 500 });
- expect(
- output?.events
- .filter((event) => event.type === RuntimeEventType.SESSION_STATUS)
- .map((event) => event.payload?.status),
- ).toEqual([RuntimeEventStatus.ABORTED]);
- expect(observer).not.toHaveBeenCalled();
- expect(output?.retracted).toBe(false);
- } finally {
- resolveReview(
- Response.json({
- action: 4,
- errorCode: 50201,
- guide_prompt: 'LATE_GUIDE',
- }),
- );
- await run;
- }
- expect(observer).not.toHaveBeenCalled();
- expect(runTurn).toHaveBeenCalledOnce();
- expect(JSON.stringify(output?.events)).not.toContain('LATE_GUIDE');
- });
- it('keeps an output Block terminal when an input Guide arrives afterward', async () => {
- let resolveReview!: (response: Response) => void;
- const pendingReview = new Promise<Response>((resolve) => {
- resolveReview = resolve;
- });
- vi.spyOn(globalThis, 'fetch').mockImplementation(async (_url, init) => {
- const body = JSON.parse(String(init?.body));
- return body.scene === 110
- ? pendingReview
- : Response.json({ action: 2, errorCode: 50201 });
- });
- const outputBlocked = vi.fn();
- const runTurn = vi.fn(async (input) => {
- await input.eventWriter.pushRuntime(finalMessage('BLOCKED_DRAFT'));
- expect(input.signal?.aborted).toBe(true);
- outputBlocked();
- });
- const onOutputRecall = vi.fn();
- const rewindPiHistory = vi.fn();
- const run = new LocalRuntimeHost({ safetyApiVersion: 'v2', piRunner: { runTurn } }).runTurn({
- ...baseTurn,
- onOutputRecall,
- rewindPiHistory,
- });
- await vi.waitFor(() => expect(outputBlocked).toHaveBeenCalledOnce());
- resolveReview(
- Response.json({
- action: 4,
- errorCode: 50201,
- guide_prompt: 'LATE_INPUT_GUIDE',
- }),
- );
- const output = await run;
- expect(runTurn).toHaveBeenCalledOnce();
- expect(onOutputRecall).not.toHaveBeenCalled();
- expect(rewindPiHistory).not.toHaveBeenCalled();
- expect(output).toMatchObject({ retracted: true, retractionVariant: 'content' });
- expect(
- output.events
- .filter((event) => event.type === RuntimeEventType.SESSION_STATUS)
- .map((event) => event.payload?.status),
- ).toEqual([RuntimeEventStatus.COMPLETED]);
- expect(JSON.stringify(output.events)).not.toMatch(/BLOCKED_DRAFT|LATE_INPUT_GUIDE/);
- });
- it.each(['chunk', 'final'] as const)(
- 'does not release a late-approved output %s after input Block',
- async (kind) => {
- let resolveInput!: (response: Response) => void;
- let resolveOutput!: (response: Response) => void;
- const pendingInput = new Promise<Response>((resolve) => {
- resolveInput = resolve;
- });
- const pendingOutput = new Promise<Response>((resolve) => {
- resolveOutput = resolve;
- });
- const outputReviewStarted = vi.fn();
- vi.spyOn(globalThis, 'fetch').mockImplementation(async (_url, init) => {
- const body = JSON.parse(String(init?.body));
- if (body.scene === 110) return pendingInput;
- outputReviewStarted();
- return pendingOutput;
- });
- let attemptSignal: AbortSignal | undefined;
- const runTurn = vi.fn(async (input) => {
- attemptSignal = input.signal;
- const event =
- kind === 'final'
- ? finalMessage('LATE_APPROVED_DRAFT')
- : {
- ...finalMessage(''),
- payload: {
- stream_resp: JSON.stringify({
- type: RespDataType.AgentMessageChunk,
- agent_message_chunk: {
- msg_id: 'answer',
- msg_content: 'LATE_APPROVED_DRAFT'.repeat(1024),
- },
- }),
- },
- };
- await input.eventWriter.pushRuntime(event);
- });
- const run = new LocalRuntimeHost({ safetyApiVersion: 'v2', piRunner: { runTurn } }).runTurn(
- baseTurn,
- );
- await vi.waitFor(() => expect(outputReviewStarted).toHaveBeenCalledOnce());
- resolveInput(Response.json({ action: 2, errorCode: 50201 }));
- await vi.waitFor(() => expect(attemptSignal?.aborted).toBe(true));
- resolveOutput(Response.json(pass));
- const output = await run;
- expect(output).toMatchObject({ retracted: true, retractionVariant: 'content' });
- expect(runTurn).toHaveBeenCalledOnce();
- expect(
- output.events.filter((event) => event.type === RuntimeEventType.STREAM_RESP),
- ).toHaveLength(0);
- expect(output.events.at(-1)?.payload?.status).toBe(RuntimeEventStatus.COMPLETED);
- },
- );
- it.each([
- [
- { action: 4, errorCode: 50201, guide_prompt: 'PRIVATE_INPUT_GUIDE' },
- 'PRIVATE_INPUT_GUIDE',
- ],
- ])('restarts a concurrent draft with the input guide once: %j', async (verdict, instruction) => {
- let outputIndex = 0;
- vi.spyOn(globalThis, 'fetch').mockImplementation(async (_url, init) => {
- const body = JSON.parse(String(init?.body));
- return Response.json(
- body.scene === 110
- ? verdict
- : outputIndex++ === 0
- ? { action: 4, errorCode: 50201, guide_prompt: '' }
- : pass,
- );
- });
- const prompts: string[] = [];
- const onInputReviewResolved = vi.fn();
- const piRunner: LocalTurnRunner = {
- async runTurn(input) {
- prompts.push(input.systemPrompt);
- if (prompts.length === 1 && !input.signal?.aborted) {
- await new Promise<void>((resolve) =>
- input.signal?.addEventListener('abort', () => resolve(), { once: true }),
- );
- }
- await input.eventWriter.pushRuntime(finalMessage(`draft-${prompts.length}`));
- },
- };
- const output = await new LocalRuntimeHost({ safetyApiVersion: 'v2', piRunner }).runTurn({
- ...baseTurn,
- onInputReviewResolved,
- rewindPiHistory: vi.fn(),
- onOutputRecall: vi.fn(),
- });
- expect(prompts).toEqual([
- 'base',
- `base\n\n${instruction}`,
- `base\n\n${OUTPUT_REVISION_INSTRUCTION}`,
- ]);
- expect(onInputReviewResolved).toHaveBeenCalledWith(true);
- expect(output.retracted).toBe(false);
- expect(JSON.stringify(output.events)).not.toContain('PRIVATE_INPUT_GUIDE');
- expect(JSON.stringify(output.events)).not.toContain('draft-1');
- });
- it.each([undefined, '', ' '])('passes an input guide without text: %j', async (guide) => {
- installInputVerdict({ action: 4, errorCode: 50201, ...(guide !== undefined ? { guide_prompt: guide } : {}) });
- const onInputReviewResolved = vi.fn();
- const onOutputRecall = vi.fn();
- const rewindPiHistory = vi.fn();
- const runTurn = vi.fn(async (input) => {
- await input.eventWriter.pushRuntime(finalMessage('original answer'));
- expect(input.signal?.aborted).toBe(false);
- });
- const output = await new LocalRuntimeHost({ safetyApiVersion: 'v2', piRunner: { runTurn } }).runTurn({
- ...baseTurn, onInputReviewResolved, onOutputRecall, rewindPiHistory,
- });
- expect(runTurn).toHaveBeenCalledOnce();
- expect(runTurn.mock.calls[0]?.[0].systemPrompt).toBe('base');
- expect(onInputReviewResolved).toHaveBeenCalledWith(false);
- expect(onOutputRecall).not.toHaveBeenCalled();
- expect(rewindPiHistory).not.toHaveBeenCalled();
- expect(output.retracted).toBe(false);
- expect(JSON.stringify(output.events)).toContain('original answer');
- });
- it('replaces the concurrent draft with a persisted input fixed answer', async () => {
- const fetchSpy = installInputVerdict({
- action: 3,
- errorCode: 50201,
- suggestion: 'fixed answer',
- });
- const runTurn = vi.fn();
- const history = vi.fn();
- const output = await new LocalRuntimeHost({
- safetyApiVersion: 'v2',
- piRunner: { runTurn },
- }).runTurn({
- ...baseTurn,
- hooks: { onHistoryChangedHook: [history] },
- });
- expect(runTurn).toHaveBeenCalledOnce();
- expect(fetchSpy).toHaveBeenCalledOnce();
- expect(output.retracted).toBe(false);
- expect(history.mock.calls[0]?.[0]?.messages).toMatchObject([
- { role: 'user', content: [{ type: 'text', text: 'question' }] },
- { role: 'assistant', content: [{ type: 'text', text: 'fixed answer' }] },
- ]);
- expect(JSON.stringify(output.events)).toContain('fixed answer');
- expect(JSON.stringify(output.events)).not.toContain('PRIVATE_UNUSED_GUIDE');
- expect(output.events.at(-1)?.payload?.status).toBe(RuntimeEventStatus.COMPLETED);
- });
- it('retracts a concurrent draft on input block without retry', async () => {
- installInputVerdict({
- action: 2,
- errorCode: 50201,
- });
- const runTurn = vi.fn();
- const onInputReviewResolved = vi.fn();
- const output = await new LocalRuntimeHost({
- safetyApiVersion: 'v2',
- piRunner: { runTurn },
- }).runTurn({
- ...baseTurn,
- onInputReviewResolved,
- });
- expect(runTurn).toHaveBeenCalledOnce();
- expect(onInputReviewResolved).toHaveBeenCalledWith(true);
- expect(output).toMatchObject({ retracted: true, retractionVariant: 'content' });
- expect(output.events.filter((event) => event.type === RuntimeEventType.STREAM_RESP)).toEqual(
- [],
- );
- });
- it.each([401, 503, 200])(
- 'fails closed without a usable input verdict (HTTP %s)',
- async (status) => {
- vi.spyOn(globalThis, 'fetch').mockImplementation(
- async () => new Response('invalid', { status }),
- );
- const runTurn = vi.fn();
- const output = await new LocalRuntimeHost({
- safetyApiVersion: 'v2',
- piRunner: { runTurn },
- }).runTurn(baseTurn);
- expect(runTurn).toHaveBeenCalledOnce();
- expect(output).toMatchObject({ retracted: true, retractionVariant: 'network' });
- },
- );
- it('retracts the concurrent draft when input review is unavailable', async () => {
- vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('offline'));
- const runTurn = vi.fn();
- const onInputReviewResolved = vi.fn();
- const output = await new LocalRuntimeHost({
- safetyApiVersion: 'v2',
- piRunner: { runTurn },
- }).runTurn({
- ...baseTurn,
- onInputReviewResolved,
- });
- expect(onInputReviewResolved).toHaveBeenCalledWith(true);
- expect(runTurn).toHaveBeenCalledOnce();
- expect(output).toMatchObject({ retracted: true, retractionVariant: 'network' });
- });
- it.each([
- { action: 1 },
- { action: 4, errorCode: 50201, guide_prompt: 'PRIVATE_GUIDE' },
- { action: 3, errorCode: 50201, suggestion: 'FIXED' },
- ])('does not deliver a late verdict after cancellation: %j', async (verdict) => {
- let resolveReview!: (response: Response) => void;
- const pending = new Promise<Response>((resolve) => {
- resolveReview = resolve;
- });
- vi.spyOn(globalThis, 'fetch').mockImplementation(() => pending);
- const runTurn = vi.fn();
- const history = vi.fn();
- const observer = vi.fn();
- const abort = new AbortController();
- const run = new LocalRuntimeHost({ safetyApiVersion: 'v2', piRunner: { runTurn } }).runTurn({
- ...baseTurn,
- signal: abort.signal,
- onInputReviewResolved: observer,
- hooks: { onHistoryChangedHook: [history] },
- });
- abort.abort();
- resolveReview(Response.json(verdict));
- const output = await run;
- expect(runTurn).toHaveBeenCalledOnce();
- expect(history).not.toHaveBeenCalled();
- expect(observer).not.toHaveBeenCalled();
- expect(output.events.at(-1)?.payload?.status).toBe(RuntimeEventStatus.ABORTED);
- expect(JSON.stringify(output.events)).not.toMatch(/FIXED|PRIVATE_GUIDE/);
- });
- it('restores canonical history when cancellation wins during fixed-answer persistence', async () => {
- installInputVerdict({ action: 3, errorCode: 50201, suggestion: 'FIXED' });
- let enterHistory!: () => void;
- let releaseHistory!: () => void;
- const entered = new Promise<void>((resolve) => {
- enterHistory = resolve;
- });
- const held = new Promise<void>((resolve) => {
- releaseHistory = resolve;
- });
- const history = vi.fn().mockImplementationOnce(async () => {
- enterHistory();
- await held;
- });
- const runTurn = vi.fn();
- const abort = new AbortController();
- const baseline = [{ role: 'user' as const, content: 'PRIOR', timestamp: 1 }];
- const pending = new LocalRuntimeHost({ safetyApiVersion: 'v2', piRunner: { runTurn } }).runTurn(
- {
- ...baseTurn,
- history: baseline,
- signal: abort.signal,
- hooks: { onHistoryChangedHook: [history] },
- },
- );
- await entered;
- abort.abort();
- releaseHistory();
- const output = await pending;
- expect(history).toHaveBeenCalledTimes(2);
- expect(history.mock.calls[1]?.[0]).toMatchObject({
- reason: 'replaceMessages',
- messages: baseline,
- });
- expect(runTurn).toHaveBeenCalledOnce();
- expect(JSON.stringify(output.events)).not.toContain('FIXED');
- expect(output.events.at(-1)?.payload?.status).toBe(RuntimeEventStatus.ABORTED);
- });
- it('does not review absent input on continuation turns', async () => {
- const fetchSpy = installInputVerdict({ action: 2, errorCode: 50201 });
- const runTurn = vi.fn(async (input) => {
- await input.eventWriter.pushRuntime(finalMessage('answer'));
- });
- const { reviewUserInput: _, ...continuation } = baseTurn;
- const output = await new LocalRuntimeHost({
- safetyApiVersion: 'v2',
- piRunner: { runTurn },
- }).runTurn(continuation);
- expect(output.retracted).toBe(false);
- expect(
- fetchSpy.mock.calls.every(([, init]) => JSON.parse(String(init?.body)).scene === 11),
- ).toBe(true);
- });
- });
|