Skip to content

The open-source app everyone uses to manage agents at work

fix(execution-policy): final-stage approval terminates the policy instead of rewinding to stage 1 (#7893) (#7936)

Harshit Khemani3e1dc90bf250
diff --git a/server/src/__tests__/issue-execution-policy.test.ts b/server/src/__tests__/issue-execution-policy.test.ts
index c66dde8f53..b3f09d295b 100644
--- a/server/src/__tests__/issue-execution-policy.test.ts
+++ b/server/src/__tests__/issue-execution-policy.test.ts
@@ -1081,6 +1081,178 @@ describe("issue execution policy transitions", () => {
});
});
+ describe("final stage completion terminates the policy (#7893)", () => {
+ function threeStagePolicy() {
+ return makePolicy([
+ { type: "review", participants: [{ type: "agent", agentId: qaAgentId }] },
+ { type: "review", participants: [{ type: "agent", agentId: ctoAgentId }] },
+ { type: "approval", participants: [{ type: "user", userId: ctoUserId }] },
+ ]);
+ }
+
+ it("final-stage approval completes even when earlier completedStageIds are stale", () => {
+ const policy = threeStagePolicy();
+ const approvalStageId = policy.stages[2].id;
+ // completedStageIds reference stage ids from a previous version of the
+ // embedded policy (stage ids regenerate when the policy is re-sent or
+ // edited mid-flow); only the active final stage id still matches.
+ const staleStageIds = [
+ "99999999-9999-4999-8999-999999999991",
+ "99999999-9999-4999-8999-999999999992",
+ ];
+ const result = applyIssueExecutionPolicyTransition({
+ issue: {
+ status: "in_review",
+ assigneeAgentId: null,
+ assigneeUserId: ctoUserId,
+ executionPolicy: policy,
+ executionState: {
+ status: "pending",
+ currentStageId: approvalStageId,
+ currentStageIndex: 2,
+ currentStageType: "approval",
+ currentParticipant: { type: "user", userId: ctoUserId },
+ returnAssignee: { type: "agent", agentId: coderAgentId },
+ completedStageIds: staleStageIds,
+ lastDecisionId: null,
+ lastDecisionOutcome: "approved",
+ },
+ },
+ policy,
+ requestedStatus: "done",
+ requestedAssigneePatch: {},
+ actor: { userId: ctoUserId },
+ commentBody: "Approved, ship it",
+ });
+
+ // Must terminate the policy, not wrap around to the first stage.
+ expect(result.patch.executionState).toMatchObject({
+ status: "completed",
+ completedStageIds: expect.arrayContaining([...staleStageIds, approvalStageId]),
+ lastDecisionOutcome: "approved",
+ });
+ expect(result.patch.status).toBeUndefined();
+ expect(result.patch.assigneeAgentId).toBeUndefined();
+ expect(result.decision).toMatchObject({
+ stageId: approvalStageId,
+ stageType: "approval",
+ outcome: "approved",
+ });
+ });
+
+ it("non-final stage approval still advances forward to the next stage", () => {
+ const policy = threeStagePolicy();
+ const firstStageId = policy.stages[0].id;
+ const result = applyIssueExecutionPolicyTransition({
+ issue: {
+ status: "in_review",
+ assigneeAgentId: qaAgentId,
+ assigneeUserId: null,
+ executionPolicy: policy,
+ executionState: {
+ status: "pending",
+ currentStageId: firstStageId,
+ currentStageIndex: 0,
+ currentStageType: "review",
+ currentParticipant: { type: "agent", agentId: qaAgentId },
+ returnAssignee: { type: "agent", agentId: coderAgentId },
+ completedStageIds: [],
+ lastDecisionId: null,
+ lastDecisionOutcome: null,
+ },
+ },
+ policy,
+ requestedStatus: "done",
+ requestedAssigneePatch: {},
+ actor: { agentId: qaAgentId },
+ commentBody: "QA pass",
+ });
+
+ expect(result.patch.status).toBe("in_review");
+ expect(result.patch.assigneeAgentId).toBe(ctoAgentId);
+ expect(result.patch.executionState).toMatchObject({
+ status: "pending",
+ currentStageId: policy.stages[1].id,
+ currentStageIndex: 1,
+ completedStageIds: [firstStageId],
+ });
+ });
+
+ it("final-stage changes requested still returns to the executor", () => {
+ const policy = threeStagePolicy();
+ const approvalStageId = policy.stages[2].id;
+ const result = applyIssueExecutionPolicyTransition({
+ issue: {
+ status: "in_review",
+ assigneeAgentId: null,
+ assigneeUserId: ctoUserId,
+ executionPolicy: policy,
+ executionState: {
+ status: "pending",
+ currentStageId: approvalStageId,
+ currentStageIndex: 2,
+ currentStageType: "approval",
+ currentParticipant: { type: "user", userId: ctoUserId },
+ returnAssignee: { type: "agent", agentId: coderAgentId },
+ completedStageIds: [policy.stages[0].id, policy.stages[1].id],
+ lastDecisionId: null,
+ lastDecisionOutcome: "approved",
+ },
+ },
+ policy,
+ requestedStatus: "in_progress",
+ requestedAssigneePatch: {},
+ actor: { userId: ctoUserId },
+ commentBody: "Needs rework before release",
+ });
+
+ expect(result.patch.status).toBe("in_progress");
+ expect(result.patch.assigneeAgentId).toBe(coderAgentId);
+ expect(result.patch.executionState).toMatchObject({
+ status: "changes_requested",
+ currentStageId: approvalStageId,
+ lastDecisionOutcome: "changes_requested",
+ });
+ });
+
+ it("a completed execution state does not restart the workflow on done", () => {
+ const policy = threeStagePolicy();
+ // Completed state whose stage ids no longer match the current policy
+ // (e.g. policy re-sent with regenerated ids after the chain finished).
+ const result = applyIssueExecutionPolicyTransition({
+ issue: {
+ status: "in_review",
+ assigneeAgentId: null,
+ assigneeUserId: ctoUserId,
+ executionPolicy: policy,
+ executionState: {
+ status: "completed",
+ currentStageId: null,
+ currentStageIndex: null,
+ currentStageType: null,
+ currentParticipant: null,
+ returnAssignee: { type: "agent", agentId: coderAgentId },
+ completedStageIds: [
+ "99999999-9999-4999-8999-999999999991",
+ "99999999-9999-4999-8999-999999999992",
+ "99999999-9999-4999-8999-999999999993",
+ ],
+ lastDecisionId: null,
+ lastDecisionOutcome: "approved",
+ },
+ },
+ policy,
+ requestedStatus: "done",
+ requestedAssigneePatch: {},
+ actor: { userId: ctoUserId },
+ commentBody: "Closing out",
+ });
+
+ // No rewind to the first stage — the caller's done is allowed through.
+ expect(result.patch).toEqual({});
+ });
+ });
+
describe("changes requested with no return assignee", () => {
it("throws when requesting changes with no return assignee", () => {
const policy = twoStagePolicy();
diff --git a/server/src/services/issue-execution-policy.ts b/server/src/services/issue-execution-policy.ts
index f09f47020e..9fb34e748b 100644
--- a/server/src/services/issue-execution-policy.ts
+++ b/server/src/services/issue-execution-policy.ts
@@ -441,6 +441,16 @@ function nextPendingStage(policy: IssueExecutionPolicy, state: IssueExecutionSta
return policy.stages.find((stage) => !completed.has(stage.id)) ?? null;
}
+function nextPendingStageAfter(
+ policy: IssueExecutionPolicy,
+ completedStage: IssueExecutionStage,
+ state: IssueExecutionState | null,
+) {
+ const completed = new Set(state?.completedStageIds ?? []);
+ const completedIndex = policy.stages.findIndex((stage) => stage.id === completedStage.id);
+ return policy.stages.find((stage, index) => index > completedIndex && !completed.has(stage.id)) ?? null;
+}
+
function selectStageParticipant(
stage: IssueExecutionStage,
opts?: {
@@ -701,10 +711,12 @@ function applyIssueExecutionStageTransition(input: TransitionInput): TransitionR
throw unprocessable("Approving a review or approval stage requires a comment");
}
const approvedState = buildCompletedState(existingState, activeStage);
- const nextStage = nextPendingStage(
- input.policy,
- { ...approvedState, completedStageIds: approvedState.completedStageIds },
- );
+ // Only stages after the stage being approved are advance candidates.
+ // Scanning the whole policy could wrap back to the first stage when
+ // earlier completedStageIds no longer match the policy (e.g. stage ids
+ // were regenerated by a mid-flow policy edit), turning a final-stage
+ // approval into an endless re-review loop (#7893).
+ const nextStage = nextPendingStageAfter(input.policy, activeStage, approvedState);
if (!nextStage) {
patch.executionState = approvedState;
@@ -810,6 +822,12 @@ function applyIssueExecutionStageTransition(input: TransitionInput): TransitionR
return { patch };
}
+ // A workflow whose execution already completed is terminal for approve/done:
+ // closing the issue must not restart the chain at the first stage (#7893).
+ if (requestedStatus === "done" && existingState?.status === COMPLETED_STATUS) {
+ return { patch };
+ }
+
let pendingStage =
existingState?.status === CHANGES_REQUESTED_STATUS && currentStage
? currentStage