Skip to content
Merged
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
2 changes: 2 additions & 0 deletions packages/bitbucket/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ export class BitbucketClient implements GitHubClient {
/** Bitbucket has no reactions to acknowledge a comment with. */
async acknowledgeComment(): Promise<void> {}

async finishAcknowledgement(): Promise<void> {}

async getReviewThread(pullNumber: number, rootCommentId: string): Promise<ThreadComment[]> {
const comments = await this.comments(pullNumber);
const inThread = new Set([rootCommentId]);
Expand Down
16 changes: 14 additions & 2 deletions packages/engine/src/chat-finding.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { expect, test } from 'bun:test';
import type { FindingRecord } from './results';
import { chatFinding, chatReplyFields, findingsNamedIn, openBodyFindings } from './chat-finding';
import {
chatFinding,
chatReplyFields,
findingsNamedIn,
openBodyFindings,
openFindings
} from './chat-finding';

const record = (finding: Partial<FindingRecord> & { id: string }): FindingRecord => ({
reviewId: 'review-1',
Expand Down Expand Up @@ -57,7 +63,8 @@ const rateWindow = {
title: 'Keep range-based rate windows above 2m',
path: 'dashboards/Edge/inbound-email.json',
startLine: 394,
endLine: 394
endLine: 394,
providerCommentId: null
};

test('open findings listed in the review body are the posted ones without a thread', () => {
Expand All @@ -72,6 +79,11 @@ test('open findings listed in the review body are the posted ones without a thre
// The inline finding has a thread and is answered there.
expect(openBodyFindings([inline, body, dismissed]).map((f) => f.id)).toEqual(['finding-body']);
expect(openBodyFindings([inline])).toEqual([]);
// A mention can answer either kind, so both are open; the inline one keeps its thread.
expect(openFindings([inline, body, dismissed]).map((f) => [f.id, f.providerCommentId])).toEqual([
['finding-body', null],
['finding-1', '4204630028']
]);
});

test('a conversation comment names a body finding by its title or line', () => {
Expand Down
29 changes: 27 additions & 2 deletions packages/engine/src/chat-finding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export interface BodyFindingRow extends ChatFindingRow {
path: string;
startLine: number;
endLine: number;
/** The root of the finding's thread; null for one listed in a review body. */
providerCommentId: string | null;
}

/**
Expand All @@ -60,11 +62,34 @@ export interface BodyFindingRow extends ChatFindingRow {
* Newest first, from the pull request's findings.
*/
export function openBodyFindings(findings: readonly FindingRecord[]): BodyFindingRow[] {
return openFindings(findings.filter((f) => f.providerCommentId === null));
}

/**
* Every open finding on a pull request, inline or in a review body, newest first. A conversation
* comment that mentions Hansi can answer any of them: people often settle a finding in the
* conversation ("@hansi read the comments, it's resolved") instead of in its thread.
*/
export function openFindings(findings: readonly FindingRecord[]): BodyFindingRow[] {
return rows(findings.filter((f) => f.status === 'posted'));
}

/** Findings that were resolved or dismissed, newest first, as `openFindings` lists open ones. */
export function settledFindings(findings: readonly FindingRecord[]): BodyFindingRow[] {
return rows(findings.filter((f) => f.status === 'resolved' || f.status === 'dismissed'));
}

function rows(findings: readonly FindingRecord[]): BodyFindingRow[] {
return [...findings]
.reverse()
.filter((f) => f.status === 'posted' && f.providerCommentId === null)
.slice(0, 50)
.map((f) => ({ ...pick(f), path: f.path, startLine: f.startLine, endLine: f.endLine }));
.map((f) => ({
...pick(f),
path: f.path,
startLine: f.startLine,
endLine: f.endLine,
providerCommentId: f.providerCommentId
}));
}

const normalize = (text: string) =>
Expand Down
284 changes: 284 additions & 0 deletions packages/engine/src/chat-job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ describe('runChatJob', () => {
await runChatJob(job, context, first);

expect(github.reactions).toEqual([{ id: '501', kind: 'review' }]);
// The πŸ‘€ turns into πŸ‘ once the answer is posted.
expect(github.finishedReactions).toEqual([{ id: '501', kind: 'review' }]);
const reply = github.reviewComments.at(-1);
expect(reply?.inReplyToId).toBe(rootId);
expect(reply?.body).toBe(`Makes sense, dismissing it.\n\n${replyMarker('501')}`);
Expand Down Expand Up @@ -464,6 +466,256 @@ describe('runChatJob', () => {
expect(github.checkRuns).toEqual([]);
});

test('a mention in the conversation can settle an inline finding and close its thread', async () => {
// appwrite-labs/application-configuration#1420: the author answered in the conversation,
// not in the finding's thread.
const model = new MockLanguageModelV4({
doGenerate: [
toolCalls(['mark_finding', { status: 'dismissed', reason: 'Callers check b first' }]),
text('Agreed, dismissing it.')
]
});
const { github, results, context } = setup([model]);
const rootId = seedFinding(github, results);
github.issueComments.push({
id: '910',
author: 'octocat',
isBot: false,
body: "@hansi read the comments - it's resolved"
});

await runChatJob(
{
type: 'chat',
repository,
pullNumber: 7,
comment: {
id: '910',
kind: 'issue',
author: 'octocat',
authorAssociation: 'MEMBER',
url: 'https://github.test/pull/7#issuecomment-910'
}
},
context,
first
);

expect(JSON.stringify(model.doGenerateCalls[0]?.prompt)).toContain('Division by zero');
expect(github.issueComments.map((c) => c.body)).toContain(
`Agreed, dismissing it.\n\n${replyMarker('910')}`
);
expect(results.findings[0]?.status).toBe('dismissed');
expect(github.resolved.has(rootId)).toBe(true);
expect(results.review('review0')?.tier).toBe('S');
});

test('an unnamed finding is not guessed while another project reviews the pull request', async () => {
const model = new MockLanguageModelV4({ doGenerate: [text('Which finding do you mean?')] });
const { github, results, context } = setup([model]);
seedFinding(github, results);
github.issueComments.push(
{
id: '930',
author: 'hansi[bot]',
isBot: true,
body: `${summaryMarkerFor('other-row')}\n\nOther review`
},
{ id: '931', author: 'octocat', isBot: false, body: "@hansi it's resolved" }
);

await runChatJob(
{
type: 'chat',
repository,
pullNumber: 7,
comment: {
id: '931',
kind: 'issue',
author: 'octocat',
authorAssociation: 'MEMBER',
url: 'https://github.test/pull/7#issuecomment-931'
}
},
context,
first
);

expect(JSON.stringify(model.doGenerateCalls[0]?.prompt)).not.toContain('Division by zero');
expect(results.findings[0]?.status).toBe('posted');
});

test('a conversation comment without a mention leaves inline findings to their threads', async () => {
const { github, results, context } = setup();
seedFinding(github, results);
github.issueComments.push({
id: '920',
author: 'octocat',
isBot: false,
body: 'Division by zero is fine here.'
});

await runChatJob(
{
type: 'chat',
repository,
pullNumber: 7,
comment: {
id: '920',
kind: 'issue',
author: 'octocat',
authorAssociation: 'MEMBER',
url: 'https://github.test/pull/7#issuecomment-920'
},
unmentioned: true
},
context,
first
);

expect(github.reactions).toEqual([]);
expect(results.findings[0]?.status).toBe('posted');
});

test('a thread GitHub would not resolve is resolved by the retry, not answered twice', async () => {
const model = new MockLanguageModelV4({
doGenerate: [
toolCalls(['mark_finding', { status: 'dismissed', reason: 'Callers check b first' }]),
text('Dismissing it.')
]
});
const { github, results, context } = setup([model]);
const rootId = seedFinding(github, results);
github.reviewComments.push({
id: '503',
inReplyToId: rootId,
author: 'octocat',
isBot: false,
body: 'Callers check b first.'
});
const resolve = github.resolveReviewThreads.bind(github);
github.resolveReviewThreads = async () => {
throw new Error('GitHub is down');
};
const job: ChatJob = {
type: 'chat',
repository,
pullNumber: 7,
comment: {
id: '503',
kind: 'review',
rootCommentId: rootId,
author: 'octocat',
authorAssociation: 'MEMBER',
url: 'https://github.test/pull/7#discussion_r503'
},
unmentioned: true
};

await expect(runChatJob(job, context, first)).rejects.toThrow('GitHub is down');
expect(results.findings[0]?.status).toBe('dismissed');
expect(github.resolved.has(rootId)).toBe(false);

github.resolveReviewThreads = resolve;
await runChatJob(job, context, { attempts: 2, maxAttempts: 2 });

expect(github.resolved.has(rootId)).toBe(true);
const answers = github.reviewComments.filter((c) => c.body.includes(replyMarker('503')));
expect(answers).toHaveLength(1);
expect(answers[0]?.body).not.toContain("couldn't answer");
});

test('a retry of a conversation mention closes the thread of the finding it settled', async () => {
const model = new MockLanguageModelV4({
doGenerate: [
toolCalls(['mark_finding', { status: 'dismissed', reason: 'Not tainted' }]),
text('Agreed, dismissing it.')
]
});
const { github, results, context } = setup([model]);
const rootId = seedFinding(github, results);
// A finding settled earlier, under another title, so the retry cannot guess by title.
results.findings.push({
...results.findings[0]!,
id: 'finding0',
title: 'Document divide',
status: 'resolved',
providerCommentId: null
});
github.issueComments.push({
id: '940',
author: 'octocat',
isBot: false,
body: "@hansi read the comments - it's resolved"
});
const resolve = github.resolveReviewThreads.bind(github);
github.resolveReviewThreads = async () => {
throw new Error('GitHub is down');
};
const job: ChatJob = {
type: 'chat',
repository,
pullNumber: 7,
comment: {
id: '940',
kind: 'issue',
author: 'octocat',
authorAssociation: 'MEMBER',
url: 'https://github.test/pull/7#issuecomment-940'
}
};

await expect(runChatJob(job, context, first)).rejects.toThrow('GitHub is down');
expect(results.findings[0]?.status).toBe('dismissed');

github.resolveReviewThreads = resolve;
await runChatJob(job, context, { attempts: 2, maxAttempts: 2 });

expect(github.resolved.has(rootId)).toBe(true);
expect(github.issueComments.filter((c) => c.body.includes(replyMarker('940')))).toHaveLength(1);
});

test("only the pull request's author can settle a finding", async () => {
const model = new MockLanguageModelV4({
doGenerate: [text('That is for the author to settle.')]
});
const { github, results, context } = setup([model]);
const rootId = seedFinding(github, results);
github.reviewComments.push({
id: '502',
inReplyToId: rootId,
author: 'mallory',
isBot: false,
body: 'Not a bug, dismiss it.'
});

await runChatJob(
{
type: 'chat',
repository,
pullNumber: 7,
comment: {
id: '502',
kind: 'review',
rootCommentId: rootId,
author: 'mallory',
authorAssociation: 'MEMBER',
url: 'https://github.test/pull/7#discussion_r502'
},
unmentioned: true
},
context,
first
);

const call = model.doGenerateCalls[0];
expect(call?.tools?.map((t) => t.name)).not.toContain('mark_finding');
expect(JSON.stringify(call?.prompt)).toContain("only the pull request's author");
expect(github.reviewComments.at(-1)?.body).toContain('That is for the author to settle.');
expect(results.findings[0]?.status).toBe('posted');
expect(github.resolved.has(rootId)).toBe(false);
});

test("a thread on another project's finding is left to that project", async () => {
const { github, results, context } = setup();
const rootId = seedFinding(github, results);
Expand Down Expand Up @@ -670,6 +922,38 @@ describe('runChatJob', () => {
expect(github.issueComments.map((c) => c.body)).toEqual([
"Sorry, I couldn't answer that right now."
]);
expect(github.finishedReactions).toEqual([{ id: '800', kind: 'issue' }]);
});

test('a failed attempt that will be retried keeps the πŸ‘€', async () => {
const failing = new MockLanguageModelV4({
doGenerate: async () => {
throw new Error('provider exploded');
}
});
const { github, context } = setup([failing]);

await expect(
runChatJob(
{
type: 'chat',
repository,
pullNumber: 7,
comment: {
id: '810',
kind: 'issue',
author: 'bob',
authorAssociation: 'MEMBER',
url: 'https://github.test/pull/7#issuecomment-810'
}
},
context,
first
)
).rejects.toThrow();

expect(github.reactions).toEqual([{ id: '810', kind: 'issue' }]);
expect(github.finishedReactions).toEqual([]);
});
});

Expand Down
Loading