Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
handleGithubReactionPoll,
COMMENT_DELETED_FEEDBACK,
GITHUB_REACTION_POLL_CONCURRENCY,
GITHUB_REACTION_POLL_MAX_AGE_DAYS,
} from '../github-reaction-poll.js';
import { JobType } from '../../types.js';

Expand Down Expand Up @@ -207,6 +208,7 @@ describe('handleGithubReactionPoll', () => {
isAiGenerated: true,
feedback: null,
externalCommentId: { not: null },
createdAt: { gte: expect.any(Date) },
ticket: {
source: { in: ['GITHUB_ISSUE'] },
},
Expand All @@ -222,6 +224,31 @@ describe('handleGithubReactionPoll', () => {
},
});
});

it('bounds the query to messages created within the reaction-poll age window', async () => {
const now = new Date('2026-12-05T12:00:00.000Z');
const expectedCutoff = new Date(
now.getTime() - GITHUB_REACTION_POLL_MAX_AGE_DAYS * 24 * 60 * 60 * 1000,
);
mockMessageFindMany.mockResolvedValue([]);

vi.useFakeTimers({ toFake: ['Date'] });
vi.setSystemTime(now);
try {
await handleGithubReactionPoll({}, context);
} finally {
vi.useRealTimers();
}

expect(GITHUB_REACTION_POLL_MAX_AGE_DAYS).toBe(30);
expect(mockMessageFindMany).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({
createdAt: { gte: expectedCutoff },
}),
}),
);
});
describe('GitHub lookup concurrency', () => {
// Production had 256 pending messages. Read one at a time at ~240ms each,
// the poll ran past its 30s job timeout on every attempt and dead-lettered.
Expand Down
17 changes: 17 additions & 0 deletions packages/outpost/queue/src/handlers/github-reaction-poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
* poll needs one GitHub round trip per pending message, and that list only
* grows. At 256 messages the serial loop overran the 30s job timeout on every
* attempt, and the job was dead-lettered after each scheduled run.
*
* Polling is also bounded to recent AI messages. A message that stays at
* `feedback: null` would otherwise be re-polled every 24 hours forever; a
* reporter who reacts after GITHUB_REACTION_POLL_MAX_AGE_DAYS is an accepted
* miss. The cutoff is row `createdAt`, not actual GitHub post time, so a
* delayed post that succeeds across the boundary may also be missed.
*/

import { prisma } from '@copilotkit/outpost/db';
Expand All @@ -33,6 +39,13 @@ import type { GithubReactionPollPayload, JobResult, JobHandlerContext } from '..
*/
export const GITHUB_REACTION_POLL_CONCURRENCY = 8;

/**
* Maximum age of a message whose reactions the poll will still check. Use an
* exact 24-hour multiple rather than calendar-day arithmetic so the window is
* stable across local time zones and DST changes.
*/
export const GITHUB_REACTION_POLL_MAX_AGE_DAYS = 30;

/**
* Terminal `feedback` value for a message whose GitHub comment no longer exists.
* It takes the message out of the `feedback: null` set this job polls, so a
Expand All @@ -56,11 +69,15 @@ export async function handleGithubReactionPoll(
): Promise<JobResult> {
await context.reportProgress(10);

const reactionWindowStart = new Date(
Date.now() - GITHUB_REACTION_POLL_MAX_AGE_DAYS * 24 * 60 * 60 * 1000,
);
const pendingMessages = await prisma.message.findMany({
where: {
isAiGenerated: true,
feedback: null,
externalCommentId: { not: null },
createdAt: { gte: reactionWindowStart },
ticket: {
// GITHUB_DISCUSSION excluded: discussion comments are created via GraphQL
// and their externalCommentId is a GraphQL node_id, not a numeric REST
Expand Down