Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions apps/worker/src/lib/cancel-run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,31 @@ describe("cancelRun", () => {
});
});

it("runs an explicit final fence before releasing the cancelling owner", async () => {
const runRegistry = registry();
const beforeRelease = vi.fn().mockResolvedValue(undefined);

await expect(cancelRunDetailed(
"PROJ-1",
{ ownerToken: "owner-a", runId: "run-1" },
runRegistry,
undefined,
undefined,
undefined,
undefined,
beforeRelease,
)).resolves.toMatchObject({ cancelled: true, released: true });

expect(beforeRelease).toHaveBeenCalledWith(expect.objectContaining({
subjectKey: "ticket:jira:PROJ-1",
ownerToken: "owner-a",
runId: "run-1",
}));
expect(beforeRelease.mock.invocationCallOrder[0]).toBeLessThan(
vi.mocked(runRegistry.releaseCancellation).mock.invocationCallOrder[0],
);
});

it("records the cancellation reason best-effort after a confirmed cancel", async () => {
const runRegistry = registry();
await expect(cancelRun(
Expand Down
9 changes: 8 additions & 1 deletion apps/worker/src/lib/cancel-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export interface ObservedRunClaim {

export type CancelRunTarget = string | ObservedRunClaim;

export type CancelBeforeRelease = (owner: {
subjectKey: string;
ownerToken: string;
runId: string | null;
}) => Promise<void>;

/**
* Result of a cancellation attempt. `alreadyTerminal` distinguishes a run
* that was genuinely still in flight and got cancelled by this call from one
Expand Down Expand Up @@ -102,6 +108,7 @@ export async function cancelRunDetailed(
targetColumn?: IssueTrackerMoveTarget,
onReleased?: (subjectKey: string) => Promise<void> | void,
reason?: string,
beforeRelease?: CancelBeforeRelease,
): Promise<CancelRunResult> {
const subjectKey = ticketSubjectKey("jira", ticketKey);
const confirmTicketMove = issueTracker && targetColumn
Expand All @@ -125,7 +132,7 @@ export async function cancelRunDetailed(
target,
runRegistry,
onReleased,
confirmTicketMove,
beforeRelease ?? confirmTicketMove,
reason,
);
}
Expand Down
Loading
Loading