(params: {
flowId: string
rootDir?: string
nowMs?: number
})
| 935 | } |
| 936 | |
| 937 | export async function requestManagedAutonomyFlowCancel(params: { |
| 938 | flowId: string |
| 939 | rootDir?: string |
| 940 | nowMs?: number |
| 941 | }): Promise<ManagedAutonomyFlowCancelResult | null> { |
| 942 | const rootDir = resolve(params.rootDir ?? getProjectRoot()) |
| 943 | const nowMs = params.nowMs ?? Date.now() |
| 944 | return withAutonomyPersistenceLock(rootDir, async () => { |
| 945 | const flows = await listAutonomyFlows(rootDir) |
| 946 | const index = flows.findIndex(flow => flow.flowId === params.flowId) |
| 947 | if (index === -1) { |
| 948 | return null |
| 949 | } |
| 950 | const current = cloneFlowRecord(flows[index]!) |
| 951 | const queuedRunIds = |
| 952 | current.stateJson?.steps |
| 953 | .filter( |
| 954 | step => step.status === 'queued' && typeof step.runId === 'string', |
| 955 | ) |
| 956 | .map(step => step.runId!) ?? [] |
| 957 | |
| 958 | if (!isManagedFlowStatusActive(current.status)) { |
| 959 | return { |
| 960 | flow: current, |
| 961 | queuedRunIds, |
| 962 | accepted: false, |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | const state = cloneManagedState(current.stateJson) |
| 967 | if (!state) { |
| 968 | return { |
| 969 | flow: current, |
| 970 | queuedRunIds, |
| 971 | accepted: false, |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | const next = |
| 976 | current.status === 'running' |
| 977 | ? normalizeFlowRecord({ |
| 978 | ...current, |
| 979 | revision: current.revision + 1, |
| 980 | updatedAt: nowMs, |
| 981 | cancelRequestedAt: current.cancelRequestedAt ?? nowMs, |
| 982 | }) |
| 983 | : normalizeFlowRecord({ |
| 984 | ...current, |
| 985 | revision: current.revision + 1, |
| 986 | status: 'cancelled', |
| 987 | updatedAt: nowMs, |
| 988 | endedAt: nowMs, |
| 989 | currentStep: undefined, |
| 990 | waitJson: undefined, |
| 991 | stateJson: (() => { |
| 992 | markRemainingStepsCancelled(state, state.currentStepIndex, nowMs) |
| 993 | return state |
| 994 | })(), |
no test coverage detected