2026-04-06 08:40:38 -05:00
|
|
|
import { describe, expect, it } from "vitest";
|
2026-04-06 09:05:39 -05:00
|
|
|
import { applyIssueExecutionPolicyTransition, normalizeIssueExecutionPolicy, parseIssueExecutionState } from "../services/issue-execution-policy.ts";
|
|
|
|
|
import type { IssueExecutionPolicy, IssueExecutionState } from "@paperclipai/shared";
|
2026-04-06 08:40:38 -05:00
|
|
|
|
2026-04-06 09:05:39 -05:00
|
|
|
const coderAgentId = "11111111-1111-4111-8111-111111111111";
|
|
|
|
|
const qaAgentId = "22222222-2222-4222-8222-222222222222";
|
|
|
|
|
const ctoAgentId = "33333333-3333-4333-8333-333333333333";
|
|
|
|
|
const ctoUserId = "cto-user";
|
|
|
|
|
const boardUserId = "board-user";
|
|
|
|
|
|
|
|
|
|
function makePolicy(
|
|
|
|
|
stages: Array<{ type: "review" | "approval"; participants: Array<{ type: "agent" | "user"; agentId?: string; userId?: string }> }>,
|
|
|
|
|
) {
|
|
|
|
|
return normalizeIssueExecutionPolicy({ stages })!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function twoStagePolicy() {
|
|
|
|
|
return makePolicy([
|
|
|
|
|
{ type: "review", participants: [{ type: "agent", agentId: qaAgentId }] },
|
|
|
|
|
{ type: "approval", participants: [{ type: "user", userId: ctoUserId }] },
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reviewOnlyPolicy() {
|
|
|
|
|
return makePolicy([
|
|
|
|
|
{ type: "review", participants: [{ type: "agent", agentId: qaAgentId }] },
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function approvalOnlyPolicy() {
|
|
|
|
|
return makePolicy([
|
|
|
|
|
{ type: "approval", participants: [{ type: "user", userId: ctoUserId }] },
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("normalizeIssueExecutionPolicy", () => {
|
|
|
|
|
it("returns null for null/undefined input", () => {
|
|
|
|
|
expect(normalizeIssueExecutionPolicy(null)).toBeNull();
|
|
|
|
|
expect(normalizeIssueExecutionPolicy(undefined)).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns null when stages are empty", () => {
|
|
|
|
|
expect(normalizeIssueExecutionPolicy({ stages: [] })).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("throws when all participants are invalid (missing agentId)", () => {
|
|
|
|
|
expect(() =>
|
|
|
|
|
normalizeIssueExecutionPolicy({
|
|
|
|
|
stages: [{ type: "review", participants: [{ type: "agent" }] }],
|
|
|
|
|
}),
|
|
|
|
|
).toThrow("Invalid execution policy");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("deduplicates participants within a stage", () => {
|
|
|
|
|
const result = normalizeIssueExecutionPolicy({
|
|
|
|
|
stages: [
|
|
|
|
|
{
|
|
|
|
|
type: "review",
|
|
|
|
|
participants: [
|
|
|
|
|
{ type: "agent", agentId: qaAgentId },
|
|
|
|
|
{ type: "agent", agentId: qaAgentId },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
expect(result!.stages[0].participants).toHaveLength(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("assigns UUIDs to stages and participants", () => {
|
|
|
|
|
const result = normalizeIssueExecutionPolicy({
|
|
|
|
|
stages: [
|
|
|
|
|
{ type: "review", participants: [{ type: "agent", agentId: qaAgentId }] },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
expect(result!.stages[0].id).toBeDefined();
|
|
|
|
|
expect(result!.stages[0].participants[0].id).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("always sets commentRequired to true", () => {
|
|
|
|
|
const result = normalizeIssueExecutionPolicy({
|
|
|
|
|
commentRequired: false,
|
|
|
|
|
stages: [
|
|
|
|
|
{ type: "review", participants: [{ type: "agent", agentId: qaAgentId }] },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
expect(result!.commentRequired).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("defaults mode to normal", () => {
|
|
|
|
|
const result = normalizeIssueExecutionPolicy({
|
|
|
|
|
stages: [
|
|
|
|
|
{ type: "review", participants: [{ type: "agent", agentId: qaAgentId }] },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
expect(result!.mode).toBe("normal");
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-07 17:07:10 -05:00
|
|
|
it("rejects approvalsNeeded values above 1", () => {
|
|
|
|
|
expect(() =>
|
|
|
|
|
normalizeIssueExecutionPolicy({
|
|
|
|
|
stages: [
|
|
|
|
|
{
|
|
|
|
|
type: "review",
|
|
|
|
|
approvalsNeeded: 2,
|
|
|
|
|
participants: [{ type: "agent", agentId: qaAgentId }],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
).toThrow("Invalid execution policy");
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-06 09:05:39 -05:00
|
|
|
it("throws for invalid input", () => {
|
|
|
|
|
expect(() => normalizeIssueExecutionPolicy({ stages: [{ type: "invalid_type" }] })).toThrow();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("parseIssueExecutionState", () => {
|
|
|
|
|
it("returns null for null/undefined", () => {
|
|
|
|
|
expect(parseIssueExecutionState(null)).toBeNull();
|
|
|
|
|
expect(parseIssueExecutionState(undefined)).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns null for invalid shape", () => {
|
|
|
|
|
expect(parseIssueExecutionState({ status: "bogus" })).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("parses a valid state", () => {
|
|
|
|
|
const state = parseIssueExecutionState({
|
2026-04-06 08:40:38 -05:00
|
|
|
status: "pending",
|
2026-04-06 09:05:39 -05:00
|
|
|
currentStageId: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
|
|
|
|
|
currentStageIndex: 0,
|
2026-04-06 08:40:38 -05:00
|
|
|
currentStageType: "review",
|
2026-04-06 09:05:39 -05:00
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
2026-04-06 08:40:38 -05:00
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
2026-04-06 09:05:39 -05:00
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
2026-04-06 08:40:38 -05:00
|
|
|
});
|
2026-04-06 09:05:39 -05:00
|
|
|
expect(state).not.toBeNull();
|
|
|
|
|
expect(state!.status).toBe("pending");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("issue execution policy transitions", () => {
|
|
|
|
|
describe("happy path: executor → review → approval → done", () => {
|
|
|
|
|
const policy = twoStagePolicy();
|
|
|
|
|
|
|
|
|
|
it("routes executor completion into review", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Implemented the feature",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.status).toBe("in_review");
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBe(qaAgentId);
|
|
|
|
|
expect(result.patch.executionState).toMatchObject({
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
});
|
|
|
|
|
expect(result.decision).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("reviewer approves → advances to approval stage", () => {
|
|
|
|
|
const reviewStageId = policy.stages[0].id;
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
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 signoff complete",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.status).toBe("in_review");
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBeNull();
|
|
|
|
|
expect(result.patch.assigneeUserId).toBe(ctoUserId);
|
|
|
|
|
expect(result.patch.executionState).toMatchObject({
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageType: "approval",
|
|
|
|
|
completedStageIds: [reviewStageId],
|
|
|
|
|
currentParticipant: { type: "user", userId: ctoUserId },
|
|
|
|
|
});
|
|
|
|
|
expect(result.decision).toMatchObject({
|
|
|
|
|
stageId: reviewStageId,
|
|
|
|
|
stageType: "review",
|
|
|
|
|
outcome: "approved",
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("approver approves → marks completed (allows done)", () => {
|
|
|
|
|
const reviewStageId = policy.stages[0].id;
|
|
|
|
|
const approvalStageId = policy.stages[1].id;
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: null,
|
|
|
|
|
assigneeUserId: ctoUserId,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: approvalStageId,
|
|
|
|
|
currentStageIndex: 1,
|
|
|
|
|
currentStageType: "approval",
|
|
|
|
|
currentParticipant: { type: "user", userId: ctoUserId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [reviewStageId],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { userId: ctoUserId },
|
|
|
|
|
commentBody: "Approved, ship it",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.executionState).toMatchObject({
|
|
|
|
|
status: "completed",
|
|
|
|
|
completedStageIds: expect.arrayContaining([reviewStageId, approvalStageId]),
|
|
|
|
|
lastDecisionOutcome: "approved",
|
|
|
|
|
});
|
|
|
|
|
expect(result.decision).toMatchObject({
|
|
|
|
|
stageId: approvalStageId,
|
|
|
|
|
stageType: "approval",
|
|
|
|
|
outcome: "approved",
|
|
|
|
|
});
|
|
|
|
|
// status should NOT be overridden — caller can set done
|
|
|
|
|
expect(result.patch.status).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("changes requested flow", () => {
|
|
|
|
|
const policy = twoStagePolicy();
|
|
|
|
|
const reviewStageId = policy.stages[0].id;
|
|
|
|
|
|
|
|
|
|
it("reviewer requests changes → returns to executor", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "in_progress",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: qaAgentId },
|
|
|
|
|
commentBody: "Needs another pass on edge cases",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.status).toBe("in_progress");
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBe(coderAgentId);
|
|
|
|
|
expect(result.patch.executionState).toMatchObject({
|
|
|
|
|
status: "changes_requested",
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
lastDecisionOutcome: "changes_requested",
|
|
|
|
|
});
|
|
|
|
|
expect(result.decision).toMatchObject({
|
|
|
|
|
stageId: reviewStageId,
|
|
|
|
|
stageType: "review",
|
|
|
|
|
outcome: "changes_requested",
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("executor re-submits after changes → returns to same review stage", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "changes_requested",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: "changes_requested",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Fixed edge cases",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.status).toBe("in_review");
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBe(qaAgentId);
|
|
|
|
|
expect(result.patch.executionState).toMatchObject({
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("review-only policy (no approval stage)", () => {
|
|
|
|
|
const policy = reviewOnlyPolicy();
|
|
|
|
|
const reviewStageId = policy.stages[0].id;
|
|
|
|
|
|
|
|
|
|
it("reviewer approval completes the policy", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
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: "LGTM",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.executionState).toMatchObject({
|
|
|
|
|
status: "completed",
|
|
|
|
|
completedStageIds: [reviewStageId],
|
|
|
|
|
lastDecisionOutcome: "approved",
|
|
|
|
|
});
|
|
|
|
|
expect(result.decision).toMatchObject({
|
|
|
|
|
stageType: "review",
|
|
|
|
|
outcome: "approved",
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("approval-only policy (no review stage)", () => {
|
|
|
|
|
const policy = approvalOnlyPolicy();
|
|
|
|
|
|
|
|
|
|
it("executor completion routes directly to approval", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Done",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.status).toBe("in_review");
|
|
|
|
|
expect(result.patch.assigneeUserId).toBe(ctoUserId);
|
|
|
|
|
expect(result.patch.executionState).toMatchObject({
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageType: "approval",
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("access control", () => {
|
|
|
|
|
const policy = twoStagePolicy();
|
|
|
|
|
const reviewStageId = policy.stages[0].id;
|
|
|
|
|
|
2026-04-09 14:48:12 -05:00
|
|
|
it("non-participant cannot advance the active stage", () => {
|
|
|
|
|
expect(() =>
|
|
|
|
|
applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
2026-04-06 09:05:39 -05:00
|
|
|
},
|
2026-04-09 14:48:12 -05:00
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: { assigneeUserId: boardUserId },
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Trying to bypass review",
|
|
|
|
|
}),
|
|
|
|
|
).toThrow("Only the active reviewer or approver can advance");
|
2026-04-06 09:05:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("non-participant can still post non-advancing updates", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: undefined,
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Just a note",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// No error — just no patch modifications
|
|
|
|
|
expect(result.patch).toEqual({});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("comment requirements", () => {
|
|
|
|
|
const policy = twoStagePolicy();
|
|
|
|
|
const reviewStageId = policy.stages[0].id;
|
|
|
|
|
|
|
|
|
|
it("approval without comment throws", () => {
|
|
|
|
|
expect(() =>
|
|
|
|
|
applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
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: "",
|
|
|
|
|
}),
|
|
|
|
|
).toThrow("requires a comment");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("changes requested without comment throws", () => {
|
|
|
|
|
expect(() =>
|
|
|
|
|
applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "in_progress",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: qaAgentId },
|
|
|
|
|
commentBody: null,
|
|
|
|
|
}),
|
|
|
|
|
).toThrow("requires a comment");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("whitespace-only comment is treated as empty", () => {
|
|
|
|
|
expect(() =>
|
|
|
|
|
applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
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: " ",
|
|
|
|
|
}),
|
|
|
|
|
).toThrow("requires a comment");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("policy removal mid-flow", () => {
|
|
|
|
|
it("clears execution state when policy removed and returns to executor", () => {
|
|
|
|
|
// Use a real UUID for currentStageId so parseIssueExecutionState succeeds
|
|
|
|
|
const stageId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: null,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: stageId,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy: null,
|
|
|
|
|
requestedStatus: undefined,
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: qaAgentId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.executionState).toBeNull();
|
|
|
|
|
expect(result.patch.status).toBe("in_progress");
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBe(coderAgentId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("clears execution state without assignee change when not in_review", () => {
|
|
|
|
|
const stageId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: null,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "changes_requested",
|
|
|
|
|
currentStageId: stageId,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: "changes_requested",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy: null,
|
|
|
|
|
requestedStatus: undefined,
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.executionState).toBeNull();
|
|
|
|
|
// Not in_review, so no status/assignee change
|
|
|
|
|
expect(result.patch.status).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("reopening from done/cancelled clears state", () => {
|
|
|
|
|
it("reopening a done issue clears execution state", () => {
|
|
|
|
|
const policy = twoStagePolicy();
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "done",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "completed",
|
|
|
|
|
currentStageId: null,
|
|
|
|
|
currentStageIndex: null,
|
|
|
|
|
currentStageType: null,
|
|
|
|
|
currentParticipant: null,
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [policy.stages[0].id, policy.stages[1].id],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: "approved",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "todo",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { userId: boardUserId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.executionState).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("no-op transitions", () => {
|
|
|
|
|
const policy = twoStagePolicy();
|
2026-04-08 08:05:35 -05:00
|
|
|
const reviewStageId = policy.stages[0].id;
|
2026-04-06 09:05:39 -05:00
|
|
|
|
|
|
|
|
it("non-done status change without review context is a no-op", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "blocked",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch).toEqual({});
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-08 08:05:35 -05:00
|
|
|
it("coerces a malformed executor in_review patch into the first policy stage", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "in_review",
|
|
|
|
|
requestedAssigneePatch: { assigneeUserId: boardUserId },
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch).toMatchObject({
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("reasserts the active stage when issue status drifted out of in_review", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "in_progress",
|
|
|
|
|
requestedAssigneePatch: { assigneeAgentId: coderAgentId },
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch).toMatchObject({
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-06 09:05:39 -05:00
|
|
|
it("no policy and no state is a no-op", () => {
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: null,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy: null,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch).toEqual({});
|
|
|
|
|
});
|
2026-04-08 16:54:43 -05:00
|
|
|
|
|
|
|
|
it("does not auto-start workflow when policy is added to an already in_review issue", () => {
|
|
|
|
|
const reviewOnly = reviewOnlyPolicy();
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: null,
|
|
|
|
|
assigneeUserId: boardUserId,
|
|
|
|
|
executionPolicy: null,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy: reviewOnly,
|
|
|
|
|
requestedStatus: undefined,
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { userId: boardUserId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch).toEqual({});
|
|
|
|
|
});
|
2026-04-06 09:05:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("multi-participant stages", () => {
|
|
|
|
|
it("selects the preferred participant when explicitly requested", () => {
|
|
|
|
|
const policy = makePolicy([
|
|
|
|
|
{
|
|
|
|
|
type: "review",
|
|
|
|
|
participants: [
|
|
|
|
|
{ type: "agent", agentId: qaAgentId },
|
|
|
|
|
{ type: "agent", agentId: ctoAgentId },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: { assigneeAgentId: ctoAgentId },
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Ready for review",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBe(ctoAgentId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("falls back to first participant when no preference given", () => {
|
|
|
|
|
const policy = makePolicy([
|
|
|
|
|
{
|
|
|
|
|
type: "review",
|
|
|
|
|
participants: [
|
|
|
|
|
{ type: "agent", agentId: qaAgentId },
|
|
|
|
|
{ type: "agent", agentId: ctoAgentId },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Ready for review",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBe(qaAgentId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("excludes the return assignee from participant selection", () => {
|
|
|
|
|
const policy = makePolicy([
|
|
|
|
|
{
|
|
|
|
|
type: "review",
|
|
|
|
|
participants: [
|
|
|
|
|
{ type: "agent", agentId: coderAgentId },
|
|
|
|
|
{ type: "agent", agentId: qaAgentId },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Done",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// coderAgentId is the returnAssignee, so QA should be selected
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBe(qaAgentId);
|
|
|
|
|
});
|
[codex] Harden execution reliability and heartbeat tooling (#3679)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - Reliable execution depends on heartbeat routing, issue lifecycle
semantics, telemetry, and a fast enough local verification loop to keep
regressions visible
> - The remaining commits on this branch were mostly server/runtime
correctness fixes plus test and documentation follow-ups in that area
> - Those changes are logically separate from the UI-focused
issue-detail and workspace/navigation branches even when they touch
overlapping issue APIs
> - This pull request groups the execution reliability, heartbeat,
telemetry, and tooling changes into one standalone branch
> - The benefit is a focused review of the control-plane correctness
work, including the follow-up fix that restored the implicit
comment-reopen helpers after branch splitting
## What Changed
- Hardened issue/heartbeat execution behavior, including self-review
stage skipping, deferred mention wakes during active execution, stranded
execution recovery, active-run scoping, assignee resolution, and
blocked-to-todo wake resumption
- Reduced noisy polling/logging overhead by trimming issue run payloads,
compacting persisted run logs, silencing high-volume request logs, and
capping heartbeat-run queries in dashboard/inbox surfaces
- Expanded telemetry and status semantics with adapter/model fields on
task completion plus clearer status guidance in docs/onboarding material
- Updated test infrastructure and verification defaults with faster
route-test module isolation, cheaper default `pnpm test`, e2e isolation
from local state, and repo verification follow-ups
- Included docs/release housekeeping from the branch and added a small
follow-up commit restoring the implicit comment-reopen helpers that were
dropped during branch reconstruction
## Verification
- `pnpm vitest run
server/src/__tests__/issue-comment-reopen-routes.test.ts
server/src/__tests__/issue-telemetry-routes.test.ts`
- `pnpm vitest run server/src/__tests__/http-log-policy.test.ts
server/src/__tests__/heartbeat-run-log.test.ts
server/src/__tests__/health.test.ts`
- `server/src/__tests__/activity-service.test.ts`,
`server/src/__tests__/heartbeat-comment-wake-batching.test.ts`, and
`server/src/__tests__/heartbeat-process-recovery.test.ts` were attempted
on this host but the embedded Postgres harness reported
init-script/data-dir problems and skipped or failed to start, so they
are noted as environment-limited
## Risks
- Medium: this branch changes core issue/heartbeat routing and
reopen/wakeup behavior, so regressions would affect agent execution flow
rather than isolated UI polish
- Because it also updates verification infrastructure, reviewers should
pay attention to whether the new tests are asserting the right failure
modes and not just reshaping harness behavior
## Model Used
- OpenAI Codex coding agent (GPT-5-class runtime in Codex CLI; exact
deployed model ID is not exposed in this environment), reasoning
enabled, tool use and local code execution enabled
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [ ] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] If this change affects the UI, I have included before/after
screenshots
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge
---------
Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-04-14 13:34:52 -05:00
|
|
|
|
|
|
|
|
it("skips a self-review-only stage and completes the workflow", () => {
|
|
|
|
|
const policy = makePolicy([
|
|
|
|
|
{
|
|
|
|
|
type: "review",
|
|
|
|
|
participants: [{ type: "agent", agentId: coderAgentId }],
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Done",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch).toMatchObject({
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "completed",
|
|
|
|
|
currentStageType: null,
|
|
|
|
|
currentParticipant: null,
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [policy.stages[0].id],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
expect(result.patch.status).toBeUndefined();
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("skips a self-review-only review stage and advances to approval", () => {
|
|
|
|
|
const policy = makePolicy([
|
|
|
|
|
{
|
|
|
|
|
type: "review",
|
|
|
|
|
participants: [{ type: "agent", agentId: coderAgentId }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "approval",
|
|
|
|
|
participants: [{ type: "user", userId: ctoUserId }],
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Done",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch).toMatchObject({
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: null,
|
|
|
|
|
assigneeUserId: ctoUserId,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageType: "approval",
|
|
|
|
|
currentParticipant: { type: "user", userId: ctoUserId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [policy.stages[0].id],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-04-06 09:05:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("changes requested with no return assignee", () => {
|
|
|
|
|
it("throws when requesting changes with no return assignee", () => {
|
|
|
|
|
const policy = twoStagePolicy();
|
|
|
|
|
const reviewStageId = policy.stages[0].id;
|
|
|
|
|
expect(() =>
|
|
|
|
|
applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewStageId,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: null,
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "in_progress",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: qaAgentId },
|
|
|
|
|
commentBody: "Changes needed",
|
|
|
|
|
}),
|
|
|
|
|
).toThrow("no return assignee");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("approval stage changes requested → bounces back to executor", () => {
|
|
|
|
|
it("approver requests changes during approval stage", () => {
|
|
|
|
|
const policy = twoStagePolicy();
|
|
|
|
|
const reviewStageId = policy.stages[0].id;
|
|
|
|
|
const approvalStageId = policy.stages[1].id;
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: null,
|
|
|
|
|
assigneeUserId: ctoUserId,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: approvalStageId,
|
|
|
|
|
currentStageIndex: 1,
|
|
|
|
|
currentStageType: "approval",
|
|
|
|
|
currentParticipant: { type: "user", userId: ctoUserId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [reviewStageId],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "in_progress",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { userId: ctoUserId },
|
|
|
|
|
commentBody: "Not happy with the approach, needs rework",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.status).toBe("in_progress");
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBe(coderAgentId);
|
|
|
|
|
expect(result.patch.executionState).toMatchObject({
|
|
|
|
|
status: "changes_requested",
|
|
|
|
|
currentStageType: "approval",
|
|
|
|
|
lastDecisionOutcome: "changes_requested",
|
|
|
|
|
});
|
|
|
|
|
expect(result.decision).toMatchObject({
|
|
|
|
|
stageId: approvalStageId,
|
|
|
|
|
stageType: "approval",
|
|
|
|
|
outcome: "changes_requested",
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("user participants", () => {
|
|
|
|
|
it("handles user-type reviewer participant correctly", () => {
|
|
|
|
|
const policy = makePolicy([
|
|
|
|
|
{ type: "review", participants: [{ type: "user", userId: boardUserId }] },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: null,
|
|
|
|
|
},
|
|
|
|
|
policy,
|
|
|
|
|
requestedStatus: "done",
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { agentId: coderAgentId },
|
|
|
|
|
commentBody: "Done",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch.status).toBe("in_review");
|
|
|
|
|
expect(result.patch.assigneeAgentId).toBeNull();
|
|
|
|
|
expect(result.patch.assigneeUserId).toBe(boardUserId);
|
2026-04-06 08:40:38 -05:00
|
|
|
});
|
|
|
|
|
});
|
2026-04-08 16:54:43 -05:00
|
|
|
|
|
|
|
|
describe("policy edits while a stage is active", () => {
|
|
|
|
|
it("clears the active execution state when its stage is removed from the policy", () => {
|
|
|
|
|
const reviewAndApproval = twoStagePolicy();
|
|
|
|
|
const approvalOnly = approvalOnlyPolicy();
|
|
|
|
|
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: reviewAndApproval,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: reviewAndApproval.stages[0].id,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy: approvalOnly,
|
|
|
|
|
requestedStatus: undefined,
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { userId: boardUserId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch).toMatchObject({
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
assigneeAgentId: coderAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionState: null,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("reassigns the active stage when the current participant is removed", () => {
|
|
|
|
|
const policy = makePolicy([
|
|
|
|
|
{
|
|
|
|
|
type: "review",
|
|
|
|
|
participants: [
|
|
|
|
|
{ type: "agent", agentId: qaAgentId },
|
|
|
|
|
{ type: "agent", agentId: ctoAgentId },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
const updatedPolicy = makePolicy([
|
|
|
|
|
{
|
|
|
|
|
type: "review",
|
|
|
|
|
participants: [{ type: "agent", agentId: ctoAgentId }],
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const result = applyIssueExecutionPolicyTransition({
|
|
|
|
|
issue: {
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: qaAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionPolicy: policy,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: policy.stages[0].id,
|
|
|
|
|
currentStageIndex: 0,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: qaAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
completedStageIds: [],
|
|
|
|
|
lastDecisionId: null,
|
|
|
|
|
lastDecisionOutcome: null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
policy: {
|
|
|
|
|
...updatedPolicy,
|
|
|
|
|
stages: [{ ...updatedPolicy.stages[0], id: policy.stages[0].id }],
|
|
|
|
|
},
|
|
|
|
|
requestedStatus: undefined,
|
|
|
|
|
requestedAssigneePatch: {},
|
|
|
|
|
actor: { userId: boardUserId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.patch).toMatchObject({
|
|
|
|
|
status: "in_review",
|
|
|
|
|
assigneeAgentId: ctoAgentId,
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
executionState: {
|
|
|
|
|
status: "pending",
|
|
|
|
|
currentStageId: policy.stages[0].id,
|
|
|
|
|
currentStageType: "review",
|
|
|
|
|
currentParticipant: { type: "agent", agentId: ctoAgentId },
|
|
|
|
|
returnAssignee: { type: "agent", agentId: coderAgentId },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-04-06 08:40:38 -05:00
|
|
|
});
|