Skip to content
Closed
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
65 changes: 64 additions & 1 deletion server/src/__tests__/issue-update-comment-wakeup-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function registerModuleMocks() {
}));
}

async function createApp() {
async function createApp(actorOverrides: Record<string, unknown> = {}) {
const [{ errorHandler }, { issueRoutes }] = await Promise.all([
vi.importActual<typeof import("../middleware/index.js")>("../middleware/index.js"),
vi.importActual<typeof import("../routes/issues.js")>("../routes/issues.js"),
Expand All @@ -118,6 +118,7 @@ async function createApp() {
companyIds: ["company-1"],
source: "local_implicit",
isInstanceAdmin: false,
...actorOverrides,
};
next();
});
Expand Down Expand Up @@ -252,4 +253,66 @@ describe("issue update comment wakeups", () => {
}),
);
});

it("does not self-wake on issue updates when a local implicit run comments on its own task", async () => {
const runId = "run-self-comment";
const existing = makeIssue({
assigneeAgentId: ASSIGNEE_AGENT_ID,
assigneeUserId: null,
status: "in_progress",
executionRunId: runId,
});
const updated = {
...existing,
status: "done",
executionRunId: null,
checkoutRunId: null,
executionAgentNameKey: null,
executionLockedAt: null,
};
mockIssueService.getById.mockResolvedValue(existing);
mockIssueService.update.mockResolvedValue(updated);
mockIssueService.addComment.mockResolvedValue({
id: "comment-3",
issueId: existing.id,
companyId: existing.companyId,
body: "wrapped",
});

const res = await request(await createApp({ runId }))
.patch(`/api/issues/${existing.id}`)
.send({
status: "done",
comment: "wrapped",
});

expect(res.status).toBe(200);
expect(mockHeartbeatService.wakeup).not.toHaveBeenCalled();
});

it("does not self-wake on direct comments when a local implicit run comments on its own task", async () => {
const runId = "run-self-comment-direct";
const existing = makeIssue({
assigneeAgentId: ASSIGNEE_AGENT_ID,
assigneeUserId: null,
status: "in_progress",
executionRunId: runId,
});
mockIssueService.getById.mockResolvedValue(existing);
mockIssueService.addComment.mockResolvedValue({
id: "comment-4",
issueId: existing.id,
companyId: existing.companyId,
body: "still working",
});

const res = await request(await createApp({ runId }))
.post(`/api/issues/${existing.id}/comments`)
.send({
body: "still working",
});

expect(res.status).toBe(201);
expect(mockHeartbeatService.wakeup).not.toHaveBeenCalled();
});
});
35 changes: 32 additions & 3 deletions server/src/routes/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ function shouldImplicitlyReopenCommentForAgent(input: {
return true;
}

function isSelfAuthoredAssigneeComment(input: {
actorType: "agent" | "user";
actorId: string;
assigneeAgentId: string | null | undefined;
runId: string | null;
executionRunId: string | null | undefined;
previousExecutionRunId?: string | null | undefined;
}) {
if (typeof input.assigneeAgentId !== "string" || input.assigneeAgentId.length === 0) return false;
if (input.actorType === "agent") {
return input.actorId === input.assigneeAgentId;
}
return Boolean(input.runId)
&& (input.executionRunId === input.runId || input.previousExecutionRunId === input.runId);
}

function diffExecutionParticipants(
previousPolicy: NormalizedExecutionPolicy | null,
nextPolicy: NormalizedExecutionPolicy | null,
Expand Down Expand Up @@ -1982,8 +1998,14 @@ export function issueRoutes(

if (commentBody && comment) {
const assigneeId = issue.assigneeAgentId;
const actorIsAgent = actor.actorType === "agent";
const selfComment = actorIsAgent && actor.actorId === assigneeId;
const selfComment = isSelfAuthoredAssigneeComment({
actorType: actor.actorType,
actorId: actor.actorId,
assigneeAgentId: assigneeId,
runId: actor.runId,
executionRunId: issue.executionRunId,
previousExecutionRunId: existing.executionRunId,
});
const skipAssigneeCommentWake = selfComment || isClosed;

if (assigneeId && !assigneeChanged && (reopened || !skipAssigneeCommentWake)) {
Expand Down Expand Up @@ -2580,7 +2602,14 @@ export function issueRoutes(
const wakeups = new Map<string, Parameters<typeof heartbeat.wakeup>[1]>();
const assigneeId = currentIssue.assigneeAgentId;
const actorIsAgent = actor.actorType === "agent";
const selfComment = actorIsAgent && actor.actorId === assigneeId;
const selfComment = isSelfAuthoredAssigneeComment({
actorType: actor.actorType,
actorId: actor.actorId,
assigneeAgentId: assigneeId,
runId: actor.runId,
executionRunId: currentIssue.executionRunId,
previousExecutionRunId: issue.executionRunId,
});
const skipWake = selfComment || isClosed;
if (assigneeId && (reopened || !skipWake)) {
if (reopened) {
Expand Down
Loading