-
-
Notifications
You must be signed in to change notification settings - Fork 37
feat(action): Handle cancellation gracefully #521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { ActionCancellation, createActionSignalHandler } from './cancellation.js'; | ||
|
|
||
| describe('Action cancellation signals', () => { | ||
| it('aborts gracefully on the first cancellation signal', () => { | ||
| const cancellation = new ActionCancellation(); | ||
| const exit = vi.fn(); | ||
| const handler = createActionSignalHandler({ cancellation, exit }); | ||
|
|
||
| handler('SIGTERM'); | ||
|
|
||
| expect(cancellation.requested).toBe(true); | ||
| expect(cancellation.signalName).toBe('SIGTERM'); | ||
| expect(cancellation.signal.aborted).toBe(true); | ||
| expect(cancellation.exitCode).toBe(143); | ||
| expect(exit).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('ignores duplicate delivery before forcing a later exit', () => { | ||
| const cancellation = new ActionCancellation(); | ||
| const exit = vi.fn(); | ||
| let now = 1_000; | ||
| const handler = createActionSignalHandler({ cancellation, exit, now: () => now }); | ||
|
|
||
| handler('SIGINT'); | ||
| now += 100; | ||
| handler('SIGTERM'); | ||
| expect(exit).not.toHaveBeenCalled(); | ||
|
|
||
| now += 1_000; | ||
| handler('SIGTERM'); | ||
| expect(exit).toHaveBeenCalledWith(143); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| export type ActionCancelSignal = 'SIGINT' | 'SIGTERM'; | ||
|
|
||
| const DEFAULT_DUPLICATE_SIGNAL_WINDOW_MS = 750; | ||
|
|
||
| /** Run-scoped cancellation state shared by the Action entrypoint and workflows. */ | ||
| export class ActionCancellation { | ||
| private readonly controller = new AbortController(); | ||
| readonly signal: AbortSignal = this.controller.signal; | ||
| signalName: ActionCancelSignal | undefined; | ||
|
|
||
| get requested(): boolean { | ||
| return this.signalName !== undefined; | ||
| } | ||
|
|
||
| request(signalName: ActionCancelSignal): boolean { | ||
| if (this.requested) return false; | ||
|
|
||
| this.signalName = signalName; | ||
| this.controller.abort(new Error(`Action cancelled by ${signalName}`)); | ||
| return true; | ||
| } | ||
|
|
||
| get exitCode(): number { | ||
| return this.signalName === 'SIGTERM' ? 143 : 130; | ||
| } | ||
| } | ||
|
|
||
| interface ActionSignalHandlerOptions { | ||
| cancellation: ActionCancellation; | ||
| now?: () => number; | ||
| exit?: (code: number) => void; | ||
| duplicateWindowMs?: number; | ||
| } | ||
|
|
||
| /** Create a shared SIGINT/SIGTERM handler with graceful-first, force-second behavior. */ | ||
| export function createActionSignalHandler( | ||
| options: ActionSignalHandlerOptions | ||
| ): (signalName: ActionCancelSignal) => void { | ||
| const duplicateWindowMs = options.duplicateWindowMs ?? DEFAULT_DUPLICATE_SIGNAL_WINDOW_MS; | ||
| const now = options.now ?? (() => Date.now()); | ||
| const exit = options.exit ?? ((code) => process.exit(code)); | ||
| let lastSignalAt = 0; | ||
|
|
||
| return (signalName) => { | ||
| const receivedAt = now(); | ||
| if (options.cancellation.requested && receivedAt - lastSignalAt < duplicateWindowMs) { | ||
| return; | ||
| } | ||
|
|
||
| lastSignalAt = receivedAt; | ||
| if (!options.cancellation.request(signalName)) { | ||
| exit(signalName === 'SIGTERM' ? 143 : 130); | ||
| return; | ||
| } | ||
|
|
||
| console.warn(`Cancellation requested by ${signalName}; finalizing partial results`); | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,7 @@ export interface TriggerCheckRun { | |
| url?: string; | ||
| checkRunId?: number; | ||
| complete(report: SkillReport, options: TriggerCheckCompleteOptions): Promise<void>; | ||
| cancel(report: SkillReport): Promise<void>; | ||
| fail(error: unknown): Promise<void>; | ||
| } | ||
|
|
||
|
|
@@ -104,6 +105,8 @@ export interface TriggerExecutorDeps { | |
| analysisQueue?: AsyncWorkQueue; | ||
| /** Shared controller for stopping the whole action run */ | ||
| abortController?: AbortController; | ||
| /** User-requested Action cancellation, distinct from provider circuit-breaker aborts. */ | ||
| cancellationSignal?: AbortSignal; | ||
| /** Shared circuit breaker for auth/provider failures */ | ||
| circuitBreaker?: ProviderFailureCircuitBreaker; | ||
| /** Optional context-bound check writer. Omit for analyze mode. */ | ||
|
|
@@ -134,6 +137,8 @@ export interface TriggerResult { | |
| auxiliaryModel?: string; | ||
| synthesisModel?: string; | ||
| error?: unknown; | ||
| /** The trigger matched but cancellation stopped it before dispatch. */ | ||
| pending?: boolean; | ||
| /** Verification/merge events captured during post-processing, for provenance export. */ | ||
| findingProcessingEvents?: FindingProcessingEvent[]; | ||
| /** | ||
|
|
@@ -263,12 +268,16 @@ export async function executeTrigger( | |
| // Update skill check with results | ||
| if (skillCheck && context.pullRequest) { | ||
| try { | ||
| await skillCheck.complete(report, { | ||
| failOn, | ||
| reportOn, | ||
| minConfidence, | ||
| failCheck, | ||
| }); | ||
| if (deps.cancellationSignal?.aborted) { | ||
| await skillCheck.cancel(report); | ||
| } else { | ||
| await skillCheck.complete(report, { | ||
| failOn, | ||
| reportOn, | ||
| minConfidence, | ||
| failCheck, | ||
| }); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cancelled skills still fail checksMedium Severity Cancellation is only honored after a clean skill report. If the task returns Additional Locations (1)Reviewed by Cursor Bugbot for commit d97dff9. Configure here. |
||
| } catch (error) { | ||
| console.error(`::warning::Failed to update skill check for ${trigger.skill}: ${error}`); | ||
| } | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this (or something like it) so that when github interrupts/kills the process, the signal makes it to node.