mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 20:06:10 +00:00
fix(git): honor commit message cancel before the agent spawns (#10937)
Cancel only reached the lane token installed at spawn, so a Cancel during the staged-context read or agent-env preparation was ignored and the agent still ran. Register a pending lane token for the whole request: it settles the request as canceled immediately and blocks the later spawn; a spawned agent replaces it with its own kill token as before, and the latest request on a lane wins. Closes #10855 Co-authored-by: Neil <neil@stably.ai>
This commit is contained in:
co-authored by
Neil
parent
ef77eaf699
commit
458b0f0a3a
@@ -19,6 +19,10 @@ import { getLocalGitOptionsForRegisteredWorktree } from '../local-worktree-runti
|
||||
import { withLinkedIssueDraftContext } from '../../../shared/source-control/ai/recipes/source-control-ai-action-variables'
|
||||
import { resolveSourceControlAiLinkedIssue } from '../source-control/source-control-ai-linked-issue'
|
||||
import { prepareLocalCommitMessageAgentEnv } from '../../text-generation/execution/commit-message-agent-environment'
|
||||
import {
|
||||
CANCELED_GENERATION_RESULT,
|
||||
runCancelableLocalGeneration
|
||||
} from '../../text-generation/execution/source-control-generation-lanes'
|
||||
import type { FilesystemHandlerContext } from './filesystem-handler-context'
|
||||
import {
|
||||
getLocalAgentRuntimeTarget,
|
||||
@@ -78,38 +82,47 @@ export function registerFilesystemGitCommitGenerationHandlers(
|
||||
args.worktreePath,
|
||||
worktreePath
|
||||
)
|
||||
let context
|
||||
try {
|
||||
context = await getStagedCommitContext(worktreePath, {
|
||||
...gitOptions,
|
||||
admissionTier: 'interactive'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('[filesystem] Failed to read staged commit context:', error)
|
||||
return {
|
||||
success: false,
|
||||
...textGenerationFailure('stagedChangesReadFailed')
|
||||
return runCancelableLocalGeneration<GenerateCommitMessageResult>(
|
||||
'commit-message',
|
||||
worktreePath,
|
||||
async (isCanceled) => {
|
||||
let context
|
||||
try {
|
||||
context = await getStagedCommitContext(worktreePath, {
|
||||
...gitOptions,
|
||||
admissionTier: 'interactive'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('[filesystem] Failed to read staged commit context:', error)
|
||||
return {
|
||||
success: false,
|
||||
...textGenerationFailure('stagedChangesReadFailed')
|
||||
}
|
||||
}
|
||||
if (!context) {
|
||||
return { success: false, ...textGenerationFailure('noStagedChanges') }
|
||||
}
|
||||
context = withLinkedIssueDraftContext(
|
||||
context,
|
||||
resolveSourceControlAiLinkedIssue(store, args, worktreePath)
|
||||
)
|
||||
const localEnv = await prepareLocalCommitMessageAgentEnv(
|
||||
resolvedSettings.params.agentId,
|
||||
commitMessageAgentEnv,
|
||||
getLocalAgentRuntimeTarget(gitOptions)
|
||||
)
|
||||
if (!localEnv.ok) {
|
||||
return { success: false, error: localEnv.error }
|
||||
}
|
||||
if (isCanceled()) {
|
||||
return CANCELED_GENERATION_RESULT
|
||||
}
|
||||
return generateCommitMessageFromContext(
|
||||
context,
|
||||
resolvedSettings.params,
|
||||
getLocalTextGenerationTarget(worktreePath, gitOptions, localEnv.env)
|
||||
)
|
||||
}
|
||||
}
|
||||
if (!context) {
|
||||
return { success: false, ...textGenerationFailure('noStagedChanges') }
|
||||
}
|
||||
context = withLinkedIssueDraftContext(
|
||||
context,
|
||||
resolveSourceControlAiLinkedIssue(store, args, worktreePath)
|
||||
)
|
||||
const localEnv = await prepareLocalCommitMessageAgentEnv(
|
||||
resolvedSettings.params.agentId,
|
||||
commitMessageAgentEnv,
|
||||
getLocalAgentRuntimeTarget(gitOptions)
|
||||
)
|
||||
if (!localEnv.ok) {
|
||||
return { success: false, error: localEnv.error }
|
||||
}
|
||||
return generateCommitMessageFromContext(
|
||||
context,
|
||||
resolvedSettings.params,
|
||||
getLocalTextGenerationTarget(worktreePath, gitOptions, localEnv.env)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -36,10 +36,15 @@ vi.mock('../../../source-control/pull-request-linked-issue', () => ({
|
||||
vi.mock('../../../text-generation/execution/commit-message-agent-environment', () => ({
|
||||
prepareLocalCommitMessageAgentEnv: mocks.prepareLocalCommitMessageAgentEnv
|
||||
}))
|
||||
vi.mock('../../../text-generation/commit-message-text-generation', () => ({
|
||||
generateCommitMessageFromContext: mocks.generateCommitMessageFromContext,
|
||||
generatePullRequestFieldsFromContext: mocks.generatePullRequestFieldsFromContext
|
||||
}))
|
||||
vi.mock('../../../text-generation/commit-message-text-generation', async () => {
|
||||
const { cancelLocalGeneration } =
|
||||
await import('../../../text-generation/execution/source-control-generation-lanes')
|
||||
return {
|
||||
cancelGenerateCommitMessageLocal: (cwd: string) => cancelLocalGeneration('commit-message', cwd),
|
||||
generateCommitMessageFromContext: mocks.generateCommitMessageFromContext,
|
||||
generatePullRequestFieldsFromContext: mocks.generatePullRequestFieldsFromContext
|
||||
}
|
||||
})
|
||||
vi.mock('../../../source-control/pull-request-template', () => ({
|
||||
resolveHostedReviewBodyForGeneration: mocks.resolveHostedReviewBodyForGeneration
|
||||
}))
|
||||
@@ -91,6 +96,25 @@ describe('RuntimeGitGenerationCommands admission', () => {
|
||||
mocks.generatePullRequestFieldsFromContext.mockResolvedValue({ success: true, fields: {} })
|
||||
})
|
||||
|
||||
it('honors a commit message cancel issued while staged context is loading', async () => {
|
||||
const stagedContext = Promise.withResolvers<unknown>()
|
||||
mocks.getStagedCommitContext.mockReturnValue(stagedContext.promise)
|
||||
const commands = makeCommands(makeTarget('/repo'))
|
||||
|
||||
const result = commands.generateRuntimeCommitMessage('id:wt-1', settingsOverride)
|
||||
await vi.waitFor(() => expect(mocks.getStagedCommitContext).toHaveBeenCalledOnce())
|
||||
await commands.cancelRuntimeGenerateCommitMessage('id:wt-1')
|
||||
|
||||
await expect(result).resolves.toEqual({
|
||||
success: false,
|
||||
error: 'Generation canceled.',
|
||||
canceled: true
|
||||
})
|
||||
stagedContext.resolve({ branch: 'main', stagedSummary: 'M\tREADME.md', stagedPatch: '+hello' })
|
||||
await nextTurn()
|
||||
expect(mocks.generateCommitMessageFromContext).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
describe('linked-issue lifetime', () => {
|
||||
function commands(): RuntimeGitGenerationCommands {
|
||||
return makeCommands(makeTarget('/repo'))
|
||||
|
||||
@@ -26,6 +26,10 @@ import type {
|
||||
} from '../../../text-generation/source-control-text-generation-types'
|
||||
import type { GeneratePullRequestFieldsResult } from '../../../text-generation/commit-message-text-generation'
|
||||
import { getPullRequestDraftContext } from '../../../text-generation/pull-request-context/pull-request-context'
|
||||
import {
|
||||
CANCELED_GENERATION_RESULT,
|
||||
runCancelableLocalGeneration
|
||||
} from '../../../text-generation/execution/source-control-generation-lanes'
|
||||
import {
|
||||
localGitOptionsForTarget,
|
||||
runtimeGitRouteForTarget,
|
||||
@@ -80,35 +84,44 @@ export class RuntimeGitGenerationCommands {
|
||||
return { success: false, error: SSH_HOST_NOT_SERVED_MESSAGE }
|
||||
}
|
||||
|
||||
let context: CommitMessageDraftContext | null
|
||||
try {
|
||||
context = await getStagedCommitContext(target.worktree.path, {
|
||||
...localGitOptionsForTarget(target),
|
||||
admissionTier: 'interactive'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('[runtime-git] Failed to read staged commit context:', error)
|
||||
return {
|
||||
success: false,
|
||||
...textGenerationFailure('stagedChangesReadFailed')
|
||||
return runCancelableLocalGeneration<GenerateCommitMessageResult>(
|
||||
'commit-message',
|
||||
target.worktree.path,
|
||||
async (isCanceled) => {
|
||||
let context: CommitMessageDraftContext | null
|
||||
try {
|
||||
context = await getStagedCommitContext(target.worktree.path, {
|
||||
...localGitOptionsForTarget(target),
|
||||
admissionTier: 'interactive'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('[runtime-git] Failed to read staged commit context:', error)
|
||||
return {
|
||||
success: false,
|
||||
...textGenerationFailure('stagedChangesReadFailed')
|
||||
}
|
||||
}
|
||||
if (!context) {
|
||||
return { success: false, ...textGenerationFailure('noStagedChanges') }
|
||||
}
|
||||
context = withLinkedIssueDraftContext(context, linkedIssueForTarget(this.host, target))
|
||||
const localEnv = await prepareLocalCommitMessageAgentEnv(
|
||||
resolvedSettings.params.agentId,
|
||||
this.host.getCommitMessageAgentEnvironment?.(),
|
||||
localAgentRuntimeTargetForTarget(target)
|
||||
)
|
||||
if (!localEnv.ok) {
|
||||
return { success: false, error: localEnv.error }
|
||||
}
|
||||
if (isCanceled()) {
|
||||
return CANCELED_GENERATION_RESULT
|
||||
}
|
||||
return generateCommitMessageFromContext(
|
||||
context,
|
||||
resolvedSettings.params,
|
||||
localTextGenerationTargetForTarget(target, localEnv.env)
|
||||
)
|
||||
}
|
||||
}
|
||||
if (!context) {
|
||||
return { success: false, ...textGenerationFailure('noStagedChanges') }
|
||||
}
|
||||
context = withLinkedIssueDraftContext(context, linkedIssueForTarget(this.host, target))
|
||||
const localEnv = await prepareLocalCommitMessageAgentEnv(
|
||||
resolvedSettings.params.agentId,
|
||||
this.host.getCommitMessageAgentEnvironment?.(),
|
||||
localAgentRuntimeTargetForTarget(target)
|
||||
)
|
||||
if (!localEnv.ok) {
|
||||
return { success: false, error: localEnv.error }
|
||||
}
|
||||
return generateCommitMessageFromContext(
|
||||
context,
|
||||
resolvedSettings.params,
|
||||
localTextGenerationTargetForTarget(target, localEnv.env)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
CANCELED_GENERATION_RESULT,
|
||||
cancelLocalGeneration,
|
||||
clearLocalGenerationCancelToken,
|
||||
localGenerationLaneKey,
|
||||
runCancelableLocalGeneration,
|
||||
setLocalGenerationCancelToken
|
||||
} from './source-control-generation-lanes'
|
||||
|
||||
describe('runCancelableLocalGeneration', () => {
|
||||
it('settles as canceled when cancel lands before the agent spawns', async () => {
|
||||
const preparation = Promise.withResolvers<void>()
|
||||
const spawn = vi.fn()
|
||||
const run = runCancelableLocalGeneration('commit-message', '/repo', async (isCanceled) => {
|
||||
await preparation.promise
|
||||
if (isCanceled()) {
|
||||
return 'skipped'
|
||||
}
|
||||
spawn()
|
||||
return 'spawned'
|
||||
})
|
||||
|
||||
cancelLocalGeneration('commit-message', '/repo')
|
||||
|
||||
await expect(run).resolves.toBe(CANCELED_GENERATION_RESULT)
|
||||
preparation.resolve()
|
||||
await preparation.promise
|
||||
expect(spawn).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('hands cancel to the token a spawned agent installs over the pending one', async () => {
|
||||
const laneKey = localGenerationLaneKey('commit-message', '/repo')
|
||||
const killAgent = vi.fn()
|
||||
const agent = Promise.withResolvers<string>()
|
||||
const run = runCancelableLocalGeneration('commit-message', '/repo', () => {
|
||||
setLocalGenerationCancelToken(laneKey, killAgent)
|
||||
return agent.promise.finally(() => clearLocalGenerationCancelToken(laneKey, killAgent))
|
||||
})
|
||||
|
||||
cancelLocalGeneration('commit-message', '/repo')
|
||||
expect(killAgent).toHaveBeenCalledOnce()
|
||||
agent.resolve('agent canceled')
|
||||
await expect(run).resolves.toBe('agent canceled')
|
||||
})
|
||||
|
||||
it('leaves a newer request cancelable after an older one on the lane finishes', async () => {
|
||||
const older = runCancelableLocalGeneration('commit-message', '/repo', async () => 'older')
|
||||
const newerPreparation = Promise.withResolvers<string>()
|
||||
const newer = runCancelableLocalGeneration(
|
||||
'commit-message',
|
||||
'/repo',
|
||||
() => newerPreparation.promise
|
||||
)
|
||||
await expect(older).resolves.toBe('older')
|
||||
|
||||
cancelLocalGeneration('commit-message', '/repo')
|
||||
|
||||
await expect(newer).resolves.toBe(CANCELED_GENERATION_RESULT)
|
||||
newerPreparation.resolve('late')
|
||||
})
|
||||
})
|
||||
@@ -29,3 +29,28 @@ export function clearLocalGenerationCancelToken(laneKey: string, cancel: () => v
|
||||
cancelTokensByLane.delete(laneKey)
|
||||
}
|
||||
}
|
||||
|
||||
export const CANCELED_GENERATION_RESULT = {
|
||||
success: false,
|
||||
error: 'Generation canceled.',
|
||||
canceled: true
|
||||
} as const
|
||||
|
||||
/** Holds the lane's cancel token across pre-spawn awaits; a spawned agent replaces it with its own kill token. */
|
||||
export function runCancelableLocalGeneration<T>(
|
||||
operation: TextGenerationOperation,
|
||||
cwd: string,
|
||||
run: (isCanceled: () => boolean) => Promise<T>
|
||||
): Promise<T | typeof CANCELED_GENERATION_RESULT> {
|
||||
const laneKey = localGenerationLaneKey(operation, cwd)
|
||||
let canceled = false
|
||||
const canceledBeforeSpawn = Promise.withResolvers<typeof CANCELED_GENERATION_RESULT>()
|
||||
const cancel = (): void => {
|
||||
canceled = true
|
||||
canceledBeforeSpawn.resolve(CANCELED_GENERATION_RESULT)
|
||||
}
|
||||
setLocalGenerationCancelToken(laneKey, cancel)
|
||||
return Promise.race([run(() => canceled), canceledBeforeSpawn.promise]).finally(() =>
|
||||
clearLocalGenerationCancelToken(laneKey, cancel)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user