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
54 changes: 0 additions & 54 deletions packages/appwrite/src/results.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,60 +155,6 @@ describe('AppwriteResultsSink', () => {
]);
});

test('reads and records what settled a finding and its reactions', async () => {
const sink = new AppwriteResultsSink(
serve((request) =>
new URL(request.url).pathname.endsWith('/findings')
? Response.json({
findings: [
{ id: 'open', providerCommentId: 41, settlement: '', upvotes: 0, downvotes: 0 },
{ id: 'fixed', providerCommentId: 'cmt_1', settlement: 'commit', upvotes: 2 },
{ id: 'old', providerCommentId: null }
]
})
: new Response(null, { status: 204 })
)
);
const pull = { projectId: 'project-1', repositoryId: 'repo-1', pullNumber: 7 };

expect(await sink.listFindings(pull)).toEqual([
{ id: 'open', providerCommentId: '41', settlement: null, upvotes: 0, downvotes: 0 },
{ id: 'fixed', providerCommentId: 'cmt_1', settlement: 'commit', upvotes: 2 },
{ id: 'old', providerCommentId: null }
] as never);
await sink.updateFindings(pull, ['open'], {
status: 'resolved',
reason: 'Fixed by abc1234',
settlement: 'commit'
});
const reactions = Array.from({ length: 101 }, (_, i) => ({
id: `f${i}`,
upvotes: 1,
downvotes: i % 2
}));
await sink.updateReactions(pull, reactions);
await sink.updateReactions(pull, []);

expect(requests.slice(1).map((r) => `${r.method} ${r.path}`)).toEqual([
'PATCH /v1/reviews/repositories/repo-1/pulls/7/findings/status',
'PATCH /v1/reviews/repositories/repo-1/pulls/7/findings/reactions',
'PATCH /v1/reviews/repositories/repo-1/pulls/7/findings/reactions'
]);
expect(requests[1]?.body).toEqual({
projectId: 'project-1',
findingIds: ['open'],
status: 'resolved',
reason: 'Fixed by abc1234',
settlement: 'commit'
});
const sent = requests
.slice(2)
.map((r) => r.body as { projectId: string; reactions: unknown[] });
expect(sent.map((body) => body.reactions.length)).toEqual([100, 1]);
expect(sent[0]?.projectId).toBe('project-1');
expect(sent[0]?.reactions[1]).toEqual({ findingId: 'f1', upvotes: 1, downvotes: 1 });
});

test('sends events in batches Appwrite takes, with ISO times', async () => {
const sink = new AppwriteResultsSink(serve());
const at = new Date('2026-10-27T12:00:00Z');
Expand Down
37 changes: 8 additions & 29 deletions packages/appwrite/src/results.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type {
ChatRef,
FindingReactions,
FindingRecord,
FindingSettlement,
FindingStatus,
ModelUsage,
PullRef,
Expand Down Expand Up @@ -41,30 +39,22 @@ export class AppwriteResultsSink implements ResultsSink {

async listFindings(pull: PullRef, options: { limit?: number } = {}): Promise<FindingRecord[]> {
const { findings } = await this.api.request<{
findings: (Omit<FindingRecord, 'providerCommentId' | 'settlement'> & {
findings: (Omit<FindingRecord, 'providerCommentId'> & {
providerCommentId: number | string | null;
settlement?: FindingSettlement | '' | null;
})[];
}>('GET', `${pullPath(pull)}/findings`, { projectId: pull.projectId, limit: options.limit });
return findings.map(({ providerCommentId, settlement, ...finding }) => ({
...finding,
// GitHub's comment ids come back as numbers; the engine keys every provider's by string.
providerCommentId:
typeof providerCommentId === 'number' ? String(providerCommentId) : providerCommentId,
// Appwrite keeps an open finding's settlement empty; one from before has none.
...(settlement === undefined ? {} : { settlement: settlement || null })
}));
// GitHub's comment ids come back as numbers; the engine keys every provider's by string.
return findings.map((finding) =>
typeof finding.providerCommentId === 'number'
? { ...finding, providerCommentId: String(finding.providerCommentId) }
: (finding as FindingRecord)
);
}

async updateFindings(
pull: PullRef,
ids: string[],
update: {
status: FindingStatus;
reason: string;
from?: FindingStatus;
settlement?: FindingSettlement;
}
update: { status: FindingStatus; reason: string; from?: FindingStatus }
) {
if (ids.length === 0) return;
await this.api.request('PATCH', `${pullPath(pull)}/findings/status`, {
Expand All @@ -74,17 +64,6 @@ export class AppwriteResultsSink implements ResultsSink {
});
}

async updateReactions(pull: PullRef, reactions: ({ id: string } & FindingReactions)[]) {
for (let start = 0; start < reactions.length; start += BATCH) {
await this.api.request('PATCH', `${pullPath(pull)}/findings/reactions`, {
projectId: pull.projectId,
reactions: reactions
.slice(start, start + BATCH)
.map(({ id, upvotes, downvotes }) => ({ findingId: id, upvotes, downvotes }))
});
}
}

async listLearnings(repository: { projectId: string; repositoryId: string }) {
const { learnings } = await this.api.request<{ learnings: string[] }>(
'GET',
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/chat-job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('runChatJob', () => {
sourceUrl: 'https://github.test/pull/7#discussion_r501'
}
]);
expect(results.findings[0]).toMatchObject({ status: 'dismissed', settlement: 'chat' });
expect(results.findings[0]?.status).toBe('dismissed');
expect(github.resolved.has(rootId)).toBe(true);
// The summary is rebuilt without the finding, and the change request withdrawn.
expect(github.summary()).toContain('Divides.');
Expand Down
9 changes: 1 addition & 8 deletions packages/engine/src/chat-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { filesUrl } from './github';
import type { ChatJob } from './jobs';
import { chatFailureReply } from './public-failure';
import type { ChatRef, ModelUsage, PullRef } from './results';
import { recordFindingReactions } from './resolved-threads';
import { lazySandbox, openSandbox } from './review-sandbox';
import { scopeToRepository } from './scope';
import { modelUsage, statusImagesOrigin, withWorkdir } from './shared';
Expand Down Expand Up @@ -214,11 +213,7 @@ export async function runChatJob(
},
onMarkFinding: finding
? async (status, reason) => {
await results.updateFindings(pull, findingIds, {
status,
reason,
settlement: 'chat'
});
await results.updateFindings(pull, findingIds, { status, reason });
settled = true;
}
: undefined,
Expand Down Expand Up @@ -248,8 +243,6 @@ export async function runChatJob(
log.warn('could not refresh summary after settlement', { err: error })
);
}
// Someone answering a finding has often reacted to it as well.
await recordFindingReactions(results, github, pull, findings, log);
log.info('chat reply posted');
} catch (error) {
// Tell the person the answer failed, once retries are exhausted. The reply is fixed:
Expand Down
2 changes: 0 additions & 2 deletions packages/engine/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ export interface ReviewThread {
resolvedBy: string | null;
/** Database id of the thread's first comment, which is how findings are linked. */
rootCommentId: string | undefined;
/** 👍 and 👎 on the first comment. Absent where the client does not read reactions. */
reactions?: { upvotes: number; downvotes: number };
}

export interface LinkedIssueRef {
Expand Down
26 changes: 1 addition & 25 deletions packages/engine/src/resolved-threads.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'bun:test';
import { reactionChanges, threadsResolvedByPeople } from './resolved-threads';
import { threadsResolvedByPeople } from './resolved-threads';

const thread = (rootCommentId: string, resolvedBy: string | null, isResolved = true) => ({
id: `T${rootCommentId}`,
Expand Down Expand Up @@ -31,27 +31,3 @@ test('a resolved thread whose resolver GitHub no longer knows is skipped', () =>
threadsResolvedByPeople([{ id: 'a', providerCommentId: '1' }], [thread('1', null)], 'x[bot]')
).toEqual([]);
});

test('reactions are written only where they changed', () => {
const findings = [
{ id: 'new-vote', providerCommentId: '1', upvotes: 0, downvotes: 0 },
{ id: 'same', providerCommentId: '2', upvotes: 2, downvotes: 1 },
// Read from an Appwrite that does not keep reactions yet.
{ id: 'never-stored', providerCommentId: '3' },
{ id: 'unreacted', providerCommentId: '4' },
{ id: 'body-only', providerCommentId: null },
{ id: 'no-reactions-read', providerCommentId: '5', upvotes: 1, downvotes: 0 }
];
const threads = [
{ ...thread('1', null, false), reactions: { upvotes: 0, downvotes: 1 } },
{ ...thread('2', null, false), reactions: { upvotes: 2, downvotes: 1 } },
{ ...thread('3', null, false), reactions: { upvotes: 1, downvotes: 0 } },
{ ...thread('4', null, false), reactions: { upvotes: 0, downvotes: 0 } },
// A provider whose client does not read reactions.
thread('5', null, false)
];
expect(reactionChanges(findings, threads)).toEqual([
{ id: 'new-vote', upvotes: 0, downvotes: 1 },
{ id: 'never-stored', upvotes: 1, downvotes: 0 }
]);
});
82 changes: 6 additions & 76 deletions packages/engine/src/resolved-threads.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Log } from './context';
import type { GitHubConnection, ReviewThread } from './github';
import type { FindingReactions, FindingRecord, PullRef, ResultsSink } from './results';
import type { FindingRecord, PullRef, ResultsSink } from './results';

/**
* Open findings whose GitHub thread a person resolved, with who resolved it.
Expand All @@ -26,47 +26,22 @@ export function threadsResolvedByPeople(
}

/**
* Findings whose comment's reactions differ from what was stored, with the counts read now.
* Only threads whose client read reactions count, so a provider without them writes nothing.
*/
export function reactionChanges(
findings: readonly Pick<FindingRecord, 'id' | 'providerCommentId' | 'upvotes' | 'downvotes'>[],
threads: readonly ReviewThread[]
): ({ id: string } & FindingReactions)[] {
const byRoot = new Map(
threads.flatMap((t) =>
t.rootCommentId && t.reactions ? [[t.rootCommentId, t.reactions] as const] : []
)
);
return findings.flatMap((f) => {
const reactions = f.providerCommentId ? byRoot.get(f.providerCommentId) : undefined;
if (!reactions) return [];
if (reactions.upvotes === (f.upvotes ?? 0) && reactions.downvotes === (f.downvotes ?? 0)) {
return [];
}
return [{ id: f.id, upvotes: reactions.upvotes, downvotes: reactions.downvotes }];
});
}

/**
* Brings the pull request's findings in step with their threads on GitHub: dismisses open
* findings whose thread someone with write access resolved, and stores the 👍 and 👎 on every
* posted finding's comment, settled ones included, since people react after the fact too.
* Dismisses open findings whose thread someone with write access resolved on GitHub.
*
* Resolving the thread is how a maintainer closes a discussion. Without this, the next review
* still saw the finding as open, checked whether the code "fixed" it, and kept it open, so the
* grade, the summary, and the verdict ignored the decision. Dismissed findings are also not
* reported again. Returns how many findings it dismissed.
*/
export async function syncFindingsWithThreads(
export async function dismissFindingsResolvedOnGitHub(
results: ResultsSink,
{ github, app }: GitHubConnection,
pull: PullRef,
findings: readonly FindingRecord[],
log: Log
): Promise<number> {
const posted = findings.filter((f) => f.status !== 'dropped' && f.providerCommentId !== null);
if (posted.length === 0) return 0;
const open = findings.filter((f) => f.status === 'posted' && f.providerCommentId !== null);
if (open.length === 0) return 0;

let threads: ReviewThread[];
try {
Expand All @@ -75,10 +50,6 @@ export async function syncFindingsWithThreads(
log.warn('could not read review threads', { err: error });
return 0;
}

await storeReactions(results, pull, posted, threads, log);

const open = posted.filter((f) => f.status === 'posted');
const candidates = threadsResolvedByPeople(
open.map((f) => ({ id: f.id, providerCommentId: f.providerCommentId! })),
threads,
Expand All @@ -97,51 +68,10 @@ export async function syncFindingsWithThreads(
await results.updateFindings(pull, [findingId], {
status: 'dismissed',
reason: `Thread resolved on GitHub by @${resolvedBy}`,
from: 'posted',
settlement: 'thread'
from: 'posted'
});
dismissed++;
}
if (dismissed) log.info('dismissed findings whose thread was resolved on GitHub', { dismissed });
return dismissed;
}

/**
* Stores the 👍 and 👎 on the pull request's posted findings, e.g. after answering in a thread,
* where people react too. Never throws: reactions are a signal for the Console, not for the job.
*/
export async function recordFindingReactions(
results: ResultsSink,
github: GitHubConnection['github'],
pull: PullRef,
findings: readonly FindingRecord[],
log: Log
): Promise<void> {
const posted = findings.filter((f) => f.status !== 'dropped' && f.providerCommentId !== null);
if (posted.length === 0) return;
try {
await storeReactions(
results,
pull,
posted,
await github.listReviewThreads(pull.pullNumber),
log
);
} catch (error) {
log.warn('could not read review threads', { err: error });
}
}

async function storeReactions(
results: ResultsSink,
pull: PullRef,
findings: readonly FindingRecord[],
threads: readonly ReviewThread[],
log: Log
): Promise<void> {
const reactions = reactionChanges(findings, threads);
if (reactions.length === 0) return;
await results
.updateReactions(pull, reactions)
.catch((error: unknown) => log.warn('could not store finding reactions', { err: error }));
}
27 changes: 1 addition & 26 deletions packages/engine/src/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ export interface ReviewRecord {
*/
export type FindingStatus = 'posted' | 'dropped' | 'resolved' | 'dismissed';

/**
* What settled a finding: a later `commit` that fixed it, the `chat` in its thread (the author
* said it is fixed, or it was declined), or a maintainer resolving its `thread` without a change.
* Together with the status this is the finding's outcome, which tells whether it was useful.
*/
export type FindingSettlement = 'commit' | 'chat' | 'thread';

/** 👍 and 👎 on the comment a finding was posted as. */
export interface FindingReactions {
upvotes: number;
downvotes: number;
}

/** A finding of a completed review of the pull request, as stored. */
export interface FindingRecord {
id: string;
Expand All @@ -64,11 +51,6 @@ export interface FindingRecord {
dropReason: string | null;
/** The GitHub comment it was posted as: the root of its thread. Null when it has none. */
providerCommentId: string | null;
/** What settled it. Absent while open, and on findings settled before it was recorded. */
settlement?: FindingSettlement | null;
/** Reactions on its comment when last read. Absent on an Appwrite that does not keep them. */
upvotes?: number;
downvotes?: number;
/** ISO 8601. */
createdAt: string;
}
Expand Down Expand Up @@ -148,15 +130,8 @@ export interface ResultsSink {
updateFindings(
pull: PullRef,
ids: string[],
update: {
status: FindingStatus;
reason: string;
from?: FindingStatus;
settlement?: FindingSettlement;
}
update: { status: FindingStatus; reason: string; from?: FindingStatus }
): Promise<void>;
/** Stores the reactions on findings' comments, by finding id. */
updateReactions(pull: PullRef, reactions: ({ id: string } & FindingReactions)[]): Promise<void>;
/** Team preferences remembered from conversations, oldest first. */
listLearnings(repository: { projectId: string; repositoryId: string }): Promise<string[]>;
createLearning(
Expand Down
Loading