|
| 1 | +import { |
| 2 | + ExecutionOutcome, |
| 3 | + ExecutionState, |
| 4 | +} from "@rivet-dev/agentos-runtime-core/protocol"; |
| 5 | +import { describe, expect, it, vi } from "vitest"; |
| 6 | +import { AgentOs } from "../src/agent-os.js"; |
| 7 | +import type { CodeExecutionResult } from "../src/language-execution.js"; |
| 8 | + |
| 9 | +const EXECUTION_ID = "execution-1"; |
| 10 | + |
| 11 | +type ExecutionCompletedHandler = (event: { |
| 12 | + executionId: string; |
| 13 | + generation: number; |
| 14 | + outcome: "cancelled"; |
| 15 | +}) => void; |
| 16 | + |
| 17 | +type ExecutionAgent = { |
| 18 | + _executionOutputHandlers: Map<string, Set<(event: unknown) => void>>; |
| 19 | + _executionCompletedHandlers: Map<string, Set<ExecutionCompletedHandler>>; |
| 20 | + _sidecarClient: { |
| 21 | + sendVmRequest: ReturnType<typeof vi.fn>; |
| 22 | + }; |
| 23 | + _sidecarSession: unknown; |
| 24 | + _sidecarVm: unknown; |
| 25 | + _executionOperation( |
| 26 | + payload: unknown, |
| 27 | + options: { signal?: AbortSignal }, |
| 28 | + ): Promise<CodeExecutionResult>; |
| 29 | +}; |
| 30 | + |
| 31 | +function createExecutionAgent() { |
| 32 | + const agent = Object.create(AgentOs.prototype) as ExecutionAgent; |
| 33 | + agent._executionOutputHandlers = new Map(); |
| 34 | + agent._executionCompletedHandlers = new Map(); |
| 35 | + agent._sidecarSession = {}; |
| 36 | + agent._sidecarVm = {}; |
| 37 | + agent._sidecarClient = { |
| 38 | + sendVmRequest: vi.fn(async (_session, _vm, payload) => { |
| 39 | + switch (payload.type) { |
| 40 | + case "javascript_execution": |
| 41 | + return { |
| 42 | + type: "execution_accepted", |
| 43 | + response: { |
| 44 | + operationId: EXECUTION_ID, |
| 45 | + execution: { |
| 46 | + executionId: EXECUTION_ID, |
| 47 | + pid: 123, |
| 48 | + createdAtMs: 0n, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }; |
| 52 | + case "cancel_execution": |
| 53 | + setTimeout(() => { |
| 54 | + for (const handler of agent._executionCompletedHandlers.get("*") ?? |
| 55 | + []) { |
| 56 | + handler({ |
| 57 | + executionId: EXECUTION_ID, |
| 58 | + generation: 1, |
| 59 | + outcome: "cancelled", |
| 60 | + }); |
| 61 | + } |
| 62 | + }, 0); |
| 63 | + return { |
| 64 | + type: "execution_descriptor", |
| 65 | + response: { |
| 66 | + execution: { |
| 67 | + executionId: EXECUTION_ID, |
| 68 | + state: ExecutionState.Idle, |
| 69 | + retainedLanguage: null, |
| 70 | + createdAtMs: 0n, |
| 71 | + lastStartedAtMs: 0n, |
| 72 | + lastCompletedAtMs: 1n, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }; |
| 76 | + case "wait_execution": |
| 77 | + return { |
| 78 | + type: "execution_completed", |
| 79 | + response: { |
| 80 | + execution: null, |
| 81 | + outcome: ExecutionOutcome.Cancelled, |
| 82 | + exitCode: null, |
| 83 | + error: null, |
| 84 | + stdout: null, |
| 85 | + stderr: null, |
| 86 | + stdoutTruncated: null, |
| 87 | + stderrTruncated: null, |
| 88 | + evaluationValue: null, |
| 89 | + typeScriptCheckResult: null, |
| 90 | + }, |
| 91 | + }; |
| 92 | + default: |
| 93 | + throw new Error(`unexpected request: ${payload.type}`); |
| 94 | + } |
| 95 | + }), |
| 96 | + }; |
| 97 | + return agent; |
| 98 | +} |
| 99 | + |
| 100 | +describe("AgentOs execution abort", () => { |
| 101 | + it("rejects without admission when the signal is already aborted", async () => { |
| 102 | + const agent = createExecutionAgent(); |
| 103 | + const controller = new AbortController(); |
| 104 | + const reason = new DOMException("stop", "AbortError"); |
| 105 | + controller.abort(reason); |
| 106 | + |
| 107 | + await expect( |
| 108 | + agent._executionOperation( |
| 109 | + { type: "javascript_execution" }, |
| 110 | + { signal: controller.signal }, |
| 111 | + ), |
| 112 | + ).rejects.toBe(reason); |
| 113 | + expect(agent._sidecarClient.sendVmRequest).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it("returns the structured cancellation result after admission", async () => { |
| 117 | + const agent = createExecutionAgent(); |
| 118 | + const controller = new AbortController(); |
| 119 | + const execution = agent._executionOperation( |
| 120 | + { type: "javascript_execution" }, |
| 121 | + { signal: controller.signal }, |
| 122 | + ); |
| 123 | + const settled = execution.then( |
| 124 | + (result) => ({ status: "resolved" as const, result }), |
| 125 | + (error: unknown) => ({ status: "rejected" as const, error }), |
| 126 | + ); |
| 127 | + |
| 128 | + await Promise.resolve(); |
| 129 | + controller.abort(new DOMException("stop", "AbortError")); |
| 130 | + |
| 131 | + expect(await settled).toEqual({ |
| 132 | + status: "resolved", |
| 133 | + result: { |
| 134 | + outcome: "cancelled", |
| 135 | + error: { |
| 136 | + code: "execution_failed", |
| 137 | + name: "ExecutionError", |
| 138 | + message: "execution completed with cancelled", |
| 139 | + }, |
| 140 | + }, |
| 141 | + }); |
| 142 | + expect(agent._sidecarClient.sendVmRequest).toHaveBeenCalledWith( |
| 143 | + agent._sidecarSession, |
| 144 | + agent._sidecarVm, |
| 145 | + { type: "cancel_execution", request: { executionId: EXECUTION_ID } }, |
| 146 | + ); |
| 147 | + }); |
| 148 | +}); |
0 commit comments