|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { ExecProcessOutcome } from "./bash-tools.exec-runtime.js"; |
| 3 | + |
| 4 | +const taskRuntime = vi.hoisted(() => ({ |
| 5 | + createRunningTaskRun: vi.fn(), |
| 6 | + finalizeTaskRunByRunId: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("../tasks/detached-task-runtime.js", () => taskRuntime); |
| 10 | + |
| 11 | +import { |
| 12 | + createBackgroundExecTask, |
| 13 | + finalizeBackgroundExecTask, |
| 14 | +} from "./bash-tools.exec-task-tracking.js"; |
| 15 | + |
| 16 | +describe("background exec task tracking", () => { |
| 17 | + beforeEach(() => { |
| 18 | + taskRuntime.createRunningTaskRun.mockReset(); |
| 19 | + taskRuntime.finalizeTaskRunByRunId.mockReset(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("creates a silent CLI ledger row without persisting command text", () => { |
| 23 | + taskRuntime.createRunningTaskRun.mockReturnValue({ taskId: "task-1" }); |
| 24 | + |
| 25 | + const handle = createBackgroundExecTask({ |
| 26 | + processSessionId: "amber-reef", |
| 27 | + sessionKey: "agent:main:main", |
| 28 | + agentId: "main", |
| 29 | + startedAt: 100, |
| 30 | + }); |
| 31 | + |
| 32 | + expect(handle).toEqual({ |
| 33 | + taskId: "task-1", |
| 34 | + runId: "exec:amber-reef", |
| 35 | + sessionKey: "agent:main:main", |
| 36 | + }); |
| 37 | + expect(taskRuntime.createRunningTaskRun).toHaveBeenCalledWith({ |
| 38 | + runtime: "cli", |
| 39 | + taskKind: "exec", |
| 40 | + sourceId: "amber-reef", |
| 41 | + requesterSessionKey: "agent:main:main", |
| 42 | + ownerKey: "agent:main:main", |
| 43 | + scopeKind: "session", |
| 44 | + agentId: "main", |
| 45 | + requesterAgentId: "main", |
| 46 | + runId: "exec:amber-reef", |
| 47 | + label: "CLI command", |
| 48 | + task: "Background CLI command", |
| 49 | + notifyPolicy: "silent", |
| 50 | + deliveryStatus: "not_applicable", |
| 51 | + startedAt: 100, |
| 52 | + lastEventAt: 100, |
| 53 | + progressSummary: "Command running", |
| 54 | + detail: { processSessionId: "amber-reef" }, |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it.each([ |
| 59 | + { |
| 60 | + label: "success", |
| 61 | + outcome: { |
| 62 | + status: "completed", |
| 63 | + exitCode: 0, |
| 64 | + exitSignal: null, |
| 65 | + durationMs: 25, |
| 66 | + aggregated: "secret output", |
| 67 | + timedOut: false, |
| 68 | + } satisfies ExecProcessOutcome, |
| 69 | + status: "succeeded", |
| 70 | + error: undefined, |
| 71 | + }, |
| 72 | + { |
| 73 | + label: "timeout", |
| 74 | + outcome: { |
| 75 | + status: "failed", |
| 76 | + exitCode: null, |
| 77 | + exitSignal: "SIGTERM", |
| 78 | + exitReason: "overall-timeout", |
| 79 | + durationMs: 25, |
| 80 | + aggregated: "secret output", |
| 81 | + timedOut: true, |
| 82 | + failureKind: "overall-timeout", |
| 83 | + reason: "secret output\nCommand timed out", |
| 84 | + } satisfies ExecProcessOutcome, |
| 85 | + status: "timed_out", |
| 86 | + error: "Command timed out", |
| 87 | + }, |
| 88 | + { |
| 89 | + label: "nonzero exit", |
| 90 | + outcome: { |
| 91 | + status: "completed", |
| 92 | + exitCode: 17, |
| 93 | + exitSignal: null, |
| 94 | + durationMs: 25, |
| 95 | + aggregated: "secret output", |
| 96 | + timedOut: false, |
| 97 | + } satisfies ExecProcessOutcome, |
| 98 | + status: "failed", |
| 99 | + error: "Command failed (exit code 17)", |
| 100 | + }, |
| 101 | + { |
| 102 | + label: "operator cancellation", |
| 103 | + outcome: { |
| 104 | + status: "failed", |
| 105 | + exitCode: null, |
| 106 | + exitSignal: "SIGTERM", |
| 107 | + exitReason: "manual-cancel", |
| 108 | + durationMs: 25, |
| 109 | + aggregated: "secret output", |
| 110 | + timedOut: false, |
| 111 | + failureKind: "signal", |
| 112 | + reason: "secret output\nCommand aborted", |
| 113 | + } satisfies ExecProcessOutcome, |
| 114 | + status: "cancelled", |
| 115 | + error: "Cancelled by operator", |
| 116 | + }, |
| 117 | + ])( |
| 118 | + "finalizes $label before wake without persisting process output", |
| 119 | + ({ outcome, status, error }) => { |
| 120 | + finalizeBackgroundExecTask({ |
| 121 | + handle: { |
| 122 | + taskId: "task-1", |
| 123 | + runId: "exec:amber-reef", |
| 124 | + sessionKey: "agent:main:main", |
| 125 | + }, |
| 126 | + outcome, |
| 127 | + }); |
| 128 | + |
| 129 | + expect(taskRuntime.finalizeTaskRunByRunId).toHaveBeenCalledWith( |
| 130 | + expect.objectContaining({ |
| 131 | + runId: "exec:amber-reef", |
| 132 | + runtime: "cli", |
| 133 | + sessionKey: "agent:main:main", |
| 134 | + status, |
| 135 | + ...(error ? { error } : { clearError: true }), |
| 136 | + }), |
| 137 | + ); |
| 138 | + expect(JSON.stringify(taskRuntime.finalizeTaskRunByRunId.mock.calls)).not.toContain( |
| 139 | + "secret output", |
| 140 | + ); |
| 141 | + }, |
| 142 | + ); |
| 143 | +}); |
0 commit comments