Skip to content
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ The first tool call opens the browser to sign in and allow the connection. Then:
installed on the agent's side. In Claude Code it is `/mcp__diffity__review`.
- **Sessions.** `create_session` takes a pull request, two commits, or a base commit and a patch
(for work that is not pushed). Its result carries the page's URL. A new session on a pull request
that moved on carries the open findings over.
that moved on carries the open findings over. A pull request session follows its head: the
Sessions page shows the current head, a review page opened on an older session moves there, and
one left open follows while its tab is hidden and nothing typed would be lost, and otherwise offers
the update.
- **Findings** are inline comments with a severity prefix (`P1`/`P2`/`P3` unless the repository
says otherwise); tours give the diff a reading order.
- **Posting.** From the page the user posts the findings to the pull request as one review, and
pulls GitHub's review threads in. Posting is only for pull request sessions, and refused once the
pull request has moved past the session's head. GitHub is reached as the user: through the GitHub
pulls GitHub's review threads in. Posting is only for pull request sessions. Once the pull request
has moved past the session's head the page asks whether to update to the new head or post to the
reviewed commit anyway; an agent's `post_review` is refused. GitHub is reached as the user: through the GitHub
App they connect on **Settings**, or a token pasted there when no App is configured. An agent
posts as the App instead (see Automated reviews).

Expand Down Expand Up @@ -229,8 +233,9 @@ The deploy runs one instance (pending OAuth consents are held in memory), sets
- OAuth client secrets, codes, access and refresh tokens, automation tokens and the web session
cookie are stored as sha256 hashes; GitHub tokens (pasted, or the App's access and refresh tokens) are encrypted with
`DIFFITY_SECRET_KEY`.
- Posting is only for pull request sessions, and refused once the pull request has moved past the
session's head: the user asks for a new session, which the open findings carry over to.
- Posting is only for pull request sessions. Once the pull request has moved past the session's
head, a review goes to the reviewed commit only when the user asks for that; otherwise the session
at the current head, which the open findings carry over to, is where to post from.

## License

Expand Down
10 changes: 10 additions & 0 deletions packages/api/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ export interface ReviewSubmission {
event: ReviewEvent;
body: string;
comments: PrComment[];
/** Posts against the session's head even though the pull request has moved on since. */
allowOutdated?: boolean;
}

/** The body of the 409 a review gets when the pull request has moved on since the session's head. */
export interface MovedOnRefusal {
error: string;
code: 'moved_on';
headSha: string;
sessionHeadSha: string;
}

export interface ReviewResult {
Expand Down
19 changes: 19 additions & 0 deletions packages/api/src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ export interface RepoInfoResponse {
sessionId: string;
review?: ReviewRun | null;
github: GitHubRemote | null;
/** The pull request has a newer head than this session shows. */
movedOn?: MovedOn | null;
}

export interface MovedOn {
headSha: string;
/** The user's session at that head, when there is one already. */
sessionId: string | null;
}

/** What `POST /api/advance` answers: the session at the pull request's current head, which may be this one. */
export interface AdvanceResponse {
sessionId: string;
headSha: string;
}

/** What `POST /api/review-request` answers; the review was asked for on `sessionId`, the session at the pull request's current head. */
export interface ReviewRequestResponse extends ReviewRun {
sessionId: string;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export function parseReviewSubmission(body: unknown): ParseResult<ReviewSubmissi
event: member(obj.event, 'event', REVIEW_EVENTS),
body: obj.body == null ? '' : anyStr(obj.body, 'body'),
comments: prComments(obj.comments),
allowOutdated: optBool(obj.allowOutdated, 'allowOutdated'),
}));
}

Expand Down
8 changes: 8 additions & 0 deletions packages/api/tests/requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ describe('a review submission', () => {
});
});

it('carries allowOutdated, and refuses one that is not a boolean', () => {
expect(parseReviewSubmission({ event: 'APPROVE', allowOutdated: true })).toEqual({
ok: true,
value: { event: 'APPROVE', body: '', comments: [], allowOutdated: true },
});
expect(errorOf(parseReviewSubmission({ event: 'APPROVE', allowOutdated: 'yes' }))).toContain('allowOutdated');
});

it('rejects an unknown verdict instead of coercing it', () => {
expect(errorOf(parseReviewSubmission({ event: 'SHIP_IT' })))
.toBe('event must be one of: COMMENT, APPROVE, REQUEST_CHANGES');
Expand Down
9 changes: 6 additions & 3 deletions packages/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,9 @@ export function createApp(deps: AppDeps): express.Express {
const rows = found.rows.map(request => {
const slug = `${request.owner}/${request.repo}`;
const action = request.session
? `<a href="/s/${escapeHtml(request.session.id)}/" target="_blank" rel="noopener">open · ${escapeHtml(request.session.headSha.slice(0, 7))}</a>${reachable
? `<a href="/s/${escapeHtml(request.session.id)}/" target="_blank" rel="noopener"${request.movedTo
? ` title="${escapeHtml(`Reviewed at ${request.session.headSha.slice(0, 7)}; opens on the current head, ${request.movedTo.slice(0, 7)}`)}"`
: ''}>open · ${escapeHtml((request.movedTo ?? request.session.headSha).slice(0, 7))}</a>${reachable
? ` <form class="inline" method="post" action="/sessions/${escapeHtml(request.session.id)}/review-request"><button type="submit">review</button></form>`
: ''}`
: `<form class="inline" method="post" action="/sessions" target="_blank">
Expand Down Expand Up @@ -797,8 +799,9 @@ export function createApp(deps: AppDeps): express.Express {
sendPage(res, 404, 'Not found', '<h1>No such session</h1><p><a href="/">Your sessions</a></p>', user);
return;
}
await live.queueReview(user.id, session.id);
await routine.fire(user.id, session, 'review');
const current = await service.sessionToReview(session);
await live.queueReview(user.id, current.id);
await routine.fire(user.id, current, 'review');
res.redirect(303, '/');
});

Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface PullInfo {
headSha: string;
headRef: string;
baseRef: string;
/** Closed or merged. */
closed: boolean;
}

export interface ReviewComment {
Expand Down Expand Up @@ -273,6 +275,7 @@ export class GitHubApi {
html_url: string;
created_at: string;
body: string | null;
state?: string;
user: { login: string } | null;
base: { sha: string; ref: string };
head: { sha: string; ref: string };
Expand All @@ -287,6 +290,7 @@ export class GitHubApi {
headSha: json.head.sha,
headRef: json.head.ref,
baseRef: json.base.ref,
closed: json.state === 'closed',
};
}

Expand Down
19 changes: 16 additions & 3 deletions packages/server/src/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,11 @@ export class Reviews {
}

/** The newest session of each of these pull requests that has one, keyed by `prKey`. */
/** With `openWork`, only sessions still holding open threads count. */
async latestPrSessions(
userId: string,
pulls: { owner: string; repo: string; number: number }[],
{ openWork = false }: { openWork?: boolean } = {},
): Promise<Map<string, { id: string; headSha: string; createdAt: string }>> {
const found = new Map<string, { id: string; headSha: string; createdAt: string }>();
if (pulls.length === 0) {
Expand All @@ -341,6 +343,7 @@ export class Reviews {
FROM sessions s JOIN repos r ON r.id = s.repo_id
WHERE s.user_id = $1 AND s.pr_number IS NOT NULL
AND lower(r.owner) || '/' || lower(r.name) || '#' || s.pr_number = ANY($2::text[])
${openWork ? "AND EXISTS (SELECT 1 FROM threads t WHERE t.session_id = s.id AND t.status = 'open')" : ''}
ORDER BY lower(r.owner), lower(r.name), s.pr_number, s.created_at DESC, s.seq DESC`,
[userId, pulls.map(pull => prKey(pull.owner, pull.repo, pull.number))],
);
Expand All @@ -350,6 +353,16 @@ export class Reviews {
return found;
}

/** The user's newest session of the pull request at this head. */
async prSessionAt(userId: string, pull: { owner: string; repo: string; number: number }, headSha: string): Promise<ReviewSessionRecord | null> {
const row = await this.db.one<SessionRow>(
`${SESSION_SELECT} WHERE s.user_id = $1 AND lower(r.owner) = lower($2) AND lower(r.name) = lower($3) AND s.pr_number = $4 AND s.head_sha = $5
ORDER BY s.created_at DESC, s.seq DESC LIMIT 1`,
[userId, pull.owner, pull.repo, pull.number, headSha],
);
return row ? rowToSession(row) : null;
}

/** Earlier sessions of the same pull request, newest first — where carried work comes from. */
async priorPrSessions(userId: string, repoId: number, prNumber: number, excludeId: string): Promise<ReviewSessionRecord[]> {
const rows = await this.db.query<SessionRow>(
Expand All @@ -364,9 +377,9 @@ export class Reviews {
* Moves rather than copies, so ids stay stable. Resolved and dismissed threads stay with the
* commit where they were dealt with.
*/
async moveOpenWork(userId: string, fromSessionIds: string[], toSessionId: string): Promise<number> {
async moveOpenWork(userId: string, fromSessionIds: string[], toSessionId: string): Promise<string[]> {
if (fromSessionIds.length === 0) {
return 0;
return [];
}
return this.db.transaction(async tx => {
const moved = await tx.query<{ id: string }>(
Expand All @@ -383,7 +396,7 @@ export class Reviews {
userId,
fromSessionIds,
]);
return moved.length;
return moved.map(row => row.id);
});
}

Expand Down
Loading
Loading