File size: 990 Bytes
e249c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { toErrorObject } from "../../infra/errors.js";
import { recordModelFallbackStop } from "../failover-error.js";
import { runOwnedAgentCleanup } from "../run-cleanup-timeout.js";
import { cliBackendLog } from "./log.js";
import type { RunCliAgentParams } from "./types.js";

/** Join CLI cleanup; replacement requires closure before any provider can proceed. */
export async function runCliCleanup(
  params: Pick<RunCliAgentParams, "runId" | "sessionId" | "oneShotCliRun">,
  step: string,
  cleanup: () => Promise<void>,
  settlement?: "required",
): Promise<void> {
  try {
    await runOwnedAgentCleanup({ ...params, step, cleanup, settlement, log: cliBackendLog });
  } catch (error) {
    if (settlement !== "required") {
      throw error;
    }
    const failure = toErrorObject(error, "CLI resource cleanup failed");
    // A different provider must not replace resources whose previous owner is still unclosed.
    recordModelFallbackStop(failure);
    throw failure;
  }
}