mirror of
https://github.com/block/buzz.git
synced 2026-11-28 12:12:04 +00:00
Prototype review check diff proposals
Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
This commit is contained in:
@@ -16,11 +16,22 @@ export type ProjectReviewCheckFinding = {
|
||||
|
||||
export type ProjectReviewCheckResult = {
|
||||
requestId?: string;
|
||||
diffEventId?: string;
|
||||
conclusion: ProjectReviewCheckConclusion;
|
||||
summary: string;
|
||||
findings: ProjectReviewCheckFinding[];
|
||||
};
|
||||
|
||||
export type ProjectReviewCheckDiffProposal = {
|
||||
eventId: string;
|
||||
content: string;
|
||||
repoUrl: string;
|
||||
commitSha: string;
|
||||
filePath: string | null;
|
||||
description: string | null;
|
||||
truncated: boolean;
|
||||
};
|
||||
|
||||
export const PROJECT_REVIEW_CHECK_RESULT_MARKER: string;
|
||||
export const PROJECT_REVIEW_CHECKS_CONFIG_PATH: string;
|
||||
export const DEFAULT_PROJECT_REVIEW_CHECKS: ProjectReviewCheckDefinition[];
|
||||
@@ -33,10 +44,22 @@ export function parseProjectReviewCheckResult(
|
||||
content: unknown,
|
||||
): ProjectReviewCheckResult | null;
|
||||
|
||||
export function parseProjectReviewCheckDiffEvent(
|
||||
event: unknown,
|
||||
expected: {
|
||||
eventId: string;
|
||||
agentPubkey: string;
|
||||
repoUrl: string;
|
||||
commit: string | null;
|
||||
},
|
||||
): ProjectReviewCheckDiffProposal | null;
|
||||
|
||||
export function buildProjectReviewCheckPrompt(input: {
|
||||
check: ProjectReviewCheckDefinition;
|
||||
projectName: string;
|
||||
repoAddress: string;
|
||||
repoUrl: string | null;
|
||||
channelId: string;
|
||||
reviewId: string;
|
||||
reviewLink: string;
|
||||
reviewTitle: string;
|
||||
|
||||
@@ -144,6 +144,12 @@ function normalizedConclusion(value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function normalizedEventId(value) {
|
||||
if (typeof value !== "string") return null;
|
||||
const eventId = value.trim().toLowerCase();
|
||||
return eventId && /^[0-9a-f]{64}$/.test(eventId) ? eventId : null;
|
||||
}
|
||||
|
||||
function normalizedFinding(value) {
|
||||
if (typeof value === "string") {
|
||||
const title = normalizedText(value, 240);
|
||||
@@ -200,11 +206,24 @@ export function parseProjectReviewCheckResult(content) {
|
||||
parsed.request_id ?? parsed.requestId,
|
||||
200,
|
||||
);
|
||||
const rawDiffEventId = parsed.diff_event_id ?? parsed.diffEventId;
|
||||
const diffEventId = normalizedEventId(rawDiffEventId);
|
||||
if (
|
||||
(rawDiffEventId !== undefined &&
|
||||
rawDiffEventId !== null &&
|
||||
!diffEventId) ||
|
||||
(conclusion === "approved" && diffEventId)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
const findings = Array.isArray(parsed.findings)
|
||||
? parsed.findings.map(normalizedFinding).filter(Boolean).slice(0, 20)
|
||||
: [];
|
||||
return {
|
||||
...(requestId ? { requestId } : {}),
|
||||
...(conclusion === "fix-recommended" && diffEventId
|
||||
? { diffEventId }
|
||||
: {}),
|
||||
conclusion,
|
||||
summary,
|
||||
findings,
|
||||
@@ -217,10 +236,76 @@ export function parseProjectReviewCheckResult(content) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function eventTag(event, name, maxLength) {
|
||||
if (!Array.isArray(event.tags)) return null;
|
||||
const value = event.tags.find(
|
||||
(tag) => Array.isArray(tag) && tag[0] === name,
|
||||
)?.[1];
|
||||
return normalizedText(value, maxLength);
|
||||
}
|
||||
|
||||
/** Resolve a result's event-id reference to one exact native Buzz diff. */
|
||||
export function parseProjectReviewCheckDiffEvent(event, expected) {
|
||||
if (
|
||||
!event ||
|
||||
typeof event !== "object" ||
|
||||
Array.isArray(event) ||
|
||||
!expected ||
|
||||
typeof expected !== "object" ||
|
||||
Array.isArray(expected)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const eventId = normalizedEventId(event.id);
|
||||
const expectedEventId = normalizedEventId(expected?.eventId);
|
||||
const author = normalizedText(event.pubkey, 64)?.toLowerCase() ?? null;
|
||||
const expectedAuthor = normalizedText(
|
||||
expected?.agentPubkey,
|
||||
64,
|
||||
)?.toLowerCase();
|
||||
if (
|
||||
event.kind !== 40008 ||
|
||||
!eventId ||
|
||||
eventId !== expectedEventId ||
|
||||
!author ||
|
||||
author !== expectedAuthor ||
|
||||
typeof event.content !== "string" ||
|
||||
!event.content.trim() ||
|
||||
!/^diff --git /m.test(event.content) ||
|
||||
new TextEncoder().encode(event.content).byteLength > 60 * 1024
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const repoUrl = eventTag(event, "repo", 2_000);
|
||||
const commitSha = eventTag(event, "commit", 128);
|
||||
if (
|
||||
!repoUrl ||
|
||||
repoUrl !== expected.repoUrl ||
|
||||
!commitSha ||
|
||||
(expected.commit &&
|
||||
commitSha.toLowerCase() !== expected.commit.toLowerCase())
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
eventId,
|
||||
content: event.content,
|
||||
repoUrl,
|
||||
commitSha,
|
||||
filePath: eventTag(event, "file", 1_000),
|
||||
description: eventTag(event, "description", 1_000),
|
||||
truncated: eventTag(event, "truncated", 16) === "true",
|
||||
};
|
||||
}
|
||||
|
||||
export function buildProjectReviewCheckPrompt({
|
||||
check,
|
||||
projectName,
|
||||
repoAddress,
|
||||
repoUrl,
|
||||
channelId,
|
||||
reviewId,
|
||||
reviewLink,
|
||||
reviewTitle,
|
||||
@@ -232,6 +317,8 @@ export function buildProjectReviewCheckPrompt({
|
||||
const scope = [
|
||||
`Project: ${JSON.stringify(projectName)}`,
|
||||
`Repository address: ${JSON.stringify(repoAddress)}`,
|
||||
`Repository URL for diff metadata: ${repoUrl ? JSON.stringify(repoUrl) : "unavailable"}`,
|
||||
`Buzz channel for a diff proposal: ${JSON.stringify(channelId)}`,
|
||||
`Review: ${JSON.stringify(reviewTitle)} (${reviewId})`,
|
||||
`Review link: ${reviewLink}`,
|
||||
`Request id: ${JSON.stringify(requestId)}`,
|
||||
@@ -254,6 +341,7 @@ export function buildProjectReviewCheckPrompt({
|
||||
request_id: requestId,
|
||||
conclusion: "fix-recommended",
|
||||
summary: "short explanation",
|
||||
diff_event_id: null,
|
||||
findings: [
|
||||
{
|
||||
title: "concise actionable fix",
|
||||
@@ -267,6 +355,16 @@ export function buildProjectReviewCheckPrompt({
|
||||
"Return an empty findings array when approved. For every recommended fix, return one finding object.",
|
||||
"Use null for file or line when the finding is not tied to a precise source location.",
|
||||
"Use approved only when no material fix is recommended. Use fix-recommended otherwise.",
|
||||
...(repoUrl && commit
|
||||
? [
|
||||
"When recommending a fix and you can express it safely as code, create one valid unified diff against the exact commit without modifying or committing the review branch.",
|
||||
`Pipe that patch into this command before the final result to publish one native Buzz diff message (kind 40008): buzz messages send-diff --channel ${JSON.stringify(channelId)} --diff - --repo ${JSON.stringify(repoUrl)} --commit ${JSON.stringify(commit)} --description ${JSON.stringify(`Review check ${requestId}`)}`,
|
||||
"Only after send-diff succeeds, copy its exact 64-character event_id into diff_event_id in the final JSON. Never invent or reuse an event id. Do not paste the patch into the JSON.",
|
||||
"Use diff_event_id: null when there is no safe code patch. Approved results must use diff_event_id: null.",
|
||||
]
|
||||
: [
|
||||
"Use diff_event_id: null because this review does not expose the repository URL and exact commit required for a native diff proposal.",
|
||||
]),
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import test from "node:test";
|
||||
import {
|
||||
buildProjectReviewCheckPrompt,
|
||||
DEFAULT_PROJECT_REVIEW_CHECKS,
|
||||
parseProjectReviewCheckDiffEvent,
|
||||
parseProjectReviewCheckResult,
|
||||
parseProjectReviewChecksConfig,
|
||||
PROJECT_REVIEW_CHECK_RESULT_MARKER,
|
||||
@@ -108,10 +109,12 @@ test("normalizes a changes-requested result to fix-recommended", () => {
|
||||
});
|
||||
|
||||
test("parses structured findings with bounded source locations", () => {
|
||||
const diffEventId = "d".repeat(64);
|
||||
const result = parseProjectReviewCheckResult(
|
||||
`${PROJECT_REVIEW_CHECK_RESULT_MARKER}\n${JSON.stringify({
|
||||
conclusion: "fix-recommended",
|
||||
summary: "Two interface fixes are needed.",
|
||||
diff_event_id: diffEventId,
|
||||
findings: [
|
||||
{
|
||||
title: "Use the shared button primitive",
|
||||
@@ -130,6 +133,7 @@ test("parses structured findings with bounded source locations", () => {
|
||||
);
|
||||
|
||||
assert.deepEqual(result, {
|
||||
diffEventId,
|
||||
conclusion: "fix-recommended",
|
||||
summary: "Two interface fixes are needed.",
|
||||
findings: [
|
||||
@@ -149,6 +153,62 @@ test("parses structured findings with bounded source locations", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("resolves only the exact agent diff for the reviewed repository and commit", () => {
|
||||
const eventId = "d".repeat(64);
|
||||
const agentPubkey = "a".repeat(64);
|
||||
const repoUrl = "https://github.com/block/buzz";
|
||||
const commit = "b".repeat(40);
|
||||
const event = {
|
||||
id: eventId,
|
||||
pubkey: agentPubkey,
|
||||
kind: 40008,
|
||||
content:
|
||||
"diff --git a/src/check.ts b/src/check.ts\n--- a/src/check.ts\n+++ b/src/check.ts\n@@ -1 +1 @@\n-old\n+new",
|
||||
tags: [
|
||||
["repo", repoUrl],
|
||||
["commit", commit],
|
||||
["file", "src/check.ts"],
|
||||
["description", "Review check request-123"],
|
||||
],
|
||||
};
|
||||
|
||||
assert.deepEqual(
|
||||
parseProjectReviewCheckDiffEvent(event, {
|
||||
eventId,
|
||||
agentPubkey,
|
||||
repoUrl,
|
||||
commit,
|
||||
}),
|
||||
{
|
||||
eventId,
|
||||
content: event.content,
|
||||
repoUrl,
|
||||
commitSha: commit,
|
||||
filePath: "src/check.ts",
|
||||
description: "Review check request-123",
|
||||
truncated: false,
|
||||
},
|
||||
);
|
||||
assert.equal(
|
||||
parseProjectReviewCheckDiffEvent(event, {
|
||||
eventId,
|
||||
agentPubkey: "c".repeat(64),
|
||||
repoUrl,
|
||||
commit,
|
||||
}),
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
parseProjectReviewCheckDiffEvent(event, {
|
||||
eventId,
|
||||
agentPubkey,
|
||||
repoUrl,
|
||||
commit: "e".repeat(40),
|
||||
}),
|
||||
null,
|
||||
);
|
||||
});
|
||||
|
||||
test("does not treat ordinary agent prose as a completed check", () => {
|
||||
assert.equal(
|
||||
parseProjectReviewCheckResult(
|
||||
@@ -162,6 +222,28 @@ test("does not treat ordinary agent prose as a completed check", () => {
|
||||
),
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
parseProjectReviewCheckResult(
|
||||
`${PROJECT_REVIEW_CHECK_RESULT_MARKER}\n${JSON.stringify({
|
||||
conclusion: "fix-recommended",
|
||||
summary: "Malformed diff reference.",
|
||||
diff_event_id: "not-an-event-id",
|
||||
findings: [],
|
||||
})}`,
|
||||
),
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
parseProjectReviewCheckResult(
|
||||
`${PROJECT_REVIEW_CHECK_RESULT_MARKER}\n${JSON.stringify({
|
||||
conclusion: "approved",
|
||||
summary: "Approved results cannot propose changes.",
|
||||
diff_event_id: "f".repeat(64),
|
||||
findings: [],
|
||||
})}`,
|
||||
),
|
||||
null,
|
||||
);
|
||||
});
|
||||
|
||||
test("builds a review-only prompt pinned to the exact commit", () => {
|
||||
@@ -169,6 +251,8 @@ test("builds a review-only prompt pinned to the exact commit", () => {
|
||||
check: DEFAULT_PROJECT_REVIEW_CHECKS[1],
|
||||
projectName: "Buzz",
|
||||
repoAddress: "30617:owner:buzz",
|
||||
repoUrl: "https://github.com/block/buzz",
|
||||
channelId: "check-channel",
|
||||
reviewId: "review-event",
|
||||
reviewLink: "buzz://pr?id=review-event",
|
||||
reviewTitle: "Add checks",
|
||||
@@ -183,6 +267,9 @@ test("builds a review-only prompt pinned to the exact commit", () => {
|
||||
assert.match(prompt, /Frontend quality/);
|
||||
assert.match(prompt, /"request_id":"request-123"/);
|
||||
assert.match(prompt, /Echo request_id exactly/);
|
||||
assert.match(prompt, /buzz messages send-diff/);
|
||||
assert.match(prompt, /"diff_event_id":null/);
|
||||
assert.match(prompt, /copy its exact 64-character event_id/);
|
||||
assert.match(prompt, new RegExp(PROJECT_REVIEW_CHECK_RESULT_MARKER));
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
import { Check, CheckCircle2, CircleAlert, FileDiff } from "lucide-react";
|
||||
import * as React from "react";
|
||||
|
||||
import type {
|
||||
ProjectReviewCheckDiffProposal,
|
||||
ProjectReviewCheckResult,
|
||||
} from "@/features/projects/lib/projectReviewChecks.mjs";
|
||||
import { Button } from "@/shared/ui/button";
|
||||
|
||||
const DiffMessage = React.lazy(
|
||||
() => import("@/features/messages/ui/DiffMessage"),
|
||||
);
|
||||
const DiffMessageExpanded = React.lazy(
|
||||
() => import("@/features/messages/ui/DiffMessageExpanded"),
|
||||
);
|
||||
|
||||
function ReviewCheckDiffProposal({
|
||||
accepted,
|
||||
canAccept,
|
||||
onAccept,
|
||||
proposal,
|
||||
}: {
|
||||
accepted: boolean;
|
||||
canAccept: boolean;
|
||||
onAccept: () => void;
|
||||
proposal: ProjectReviewCheckDiffProposal;
|
||||
}) {
|
||||
const [expanded, setExpanded] = React.useState(false);
|
||||
const acceptEnabled = canAccept && !proposal.truncated;
|
||||
|
||||
return (
|
||||
<div
|
||||
className="space-y-3 border-t border-border/55 px-4 py-4"
|
||||
data-testid="project-review-check-diff-proposal"
|
||||
>
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<div className="flex min-w-0 items-center gap-2">
|
||||
<FileDiff className="h-4 w-4 shrink-0 text-muted-foreground" />
|
||||
<div className="min-w-0">
|
||||
<h6 className="text-xs font-semibold text-foreground">
|
||||
Proposed code changes
|
||||
</h6>
|
||||
<p className="truncate text-2xs text-muted-foreground">
|
||||
Native Buzz diff · {proposal.commitSha.slice(0, 7)}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
disabled={accepted || !acceptEnabled}
|
||||
onClick={onAccept}
|
||||
size="sm"
|
||||
type="button"
|
||||
variant={accepted ? "secondary" : "default"}
|
||||
>
|
||||
{accepted ? <Check /> : null}
|
||||
{accepted ? "Accepted" : "Accept changes"}
|
||||
</Button>
|
||||
</div>
|
||||
<React.Suspense
|
||||
fallback={
|
||||
<div className="p-3 text-xs text-muted-foreground">
|
||||
Loading proposed changes…
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<DiffMessage
|
||||
commitSha={proposal.commitSha}
|
||||
content={proposal.content}
|
||||
description={proposal.description ?? undefined}
|
||||
filePath={proposal.filePath ?? undefined}
|
||||
onExpand={() => setExpanded(true)}
|
||||
repoUrl={proposal.repoUrl}
|
||||
truncated={proposal.truncated}
|
||||
/>
|
||||
</React.Suspense>
|
||||
<p className="text-2xs text-muted-foreground">
|
||||
{accepted
|
||||
? "Accepted for this prototype. No repository files or review state were changed."
|
||||
: proposal.truncated
|
||||
? "This proposal is truncated and cannot be accepted. No code was changed."
|
||||
: acceptEnabled
|
||||
? "Preview only. Accepting records your choice in this dev build and does not change code."
|
||||
: "This proposal is outdated. Run the check again before accepting it. No code was changed."}
|
||||
</p>
|
||||
{expanded ? (
|
||||
<React.Suspense fallback={null}>
|
||||
<DiffMessageExpanded
|
||||
content={proposal.content}
|
||||
filePath={proposal.filePath ?? undefined}
|
||||
onClose={() => setExpanded(false)}
|
||||
/>
|
||||
</React.Suspense>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function ProjectReviewCheckResultCard({
|
||||
acceptedProposalEventId,
|
||||
canAcceptProposal,
|
||||
onAcceptProposal,
|
||||
proposal,
|
||||
result,
|
||||
}: {
|
||||
acceptedProposalEventId?: string;
|
||||
canAcceptProposal: boolean;
|
||||
onAcceptProposal: (eventId: string) => void;
|
||||
proposal?: ProjectReviewCheckDiffProposal;
|
||||
result: ProjectReviewCheckResult;
|
||||
}) {
|
||||
const isApproved = result.conclusion === "approved";
|
||||
const findingCount = result.findings.length;
|
||||
const title = isApproved
|
||||
? "Approved"
|
||||
: findingCount === 0
|
||||
? "Fix recommended"
|
||||
: `${findingCount} ${findingCount === 1 ? "fix" : "fixes"} recommended`;
|
||||
|
||||
return (
|
||||
<section
|
||||
className="overflow-hidden rounded-xl border border-border/70 bg-background/70"
|
||||
data-testid="project-review-check-result"
|
||||
>
|
||||
<div className="flex items-start gap-3 px-4 py-3.5">
|
||||
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-muted/70">
|
||||
{isApproved ? (
|
||||
<CheckCircle2 className="h-5 w-5 text-emerald-500" />
|
||||
) : (
|
||||
<CircleAlert className="h-5 w-5 text-amber-500" />
|
||||
)}
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<h5 className="text-sm font-semibold text-foreground">{title}</h5>
|
||||
<p className="mt-1 text-xs leading-relaxed text-muted-foreground">
|
||||
{result.summary}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
{findingCount > 0 ? (
|
||||
<div className="divide-y divide-border/55 border-t border-border/55">
|
||||
{result.findings.map((finding) => (
|
||||
<article
|
||||
className="px-4 py-3"
|
||||
data-testid="project-review-check-finding"
|
||||
key={JSON.stringify(finding)}
|
||||
>
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<p className="min-w-0 flex-1 text-xs font-medium text-foreground">
|
||||
{finding.title}
|
||||
</p>
|
||||
{finding.file ? (
|
||||
<span
|
||||
className="max-w-[45%] truncate font-mono text-2xs text-muted-foreground"
|
||||
title={`${finding.file}${finding.line ? `:${finding.line}` : ""}`}
|
||||
>
|
||||
{finding.file}
|
||||
{finding.line ? `:${finding.line}` : ""}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
{finding.detail ? (
|
||||
<p className="mt-1 text-xs leading-relaxed text-muted-foreground">
|
||||
{finding.detail}
|
||||
</p>
|
||||
) : null}
|
||||
</article>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
{proposal ? (
|
||||
<ReviewCheckDiffProposal
|
||||
accepted={acceptedProposalEventId === proposal.eventId}
|
||||
canAccept={canAcceptProposal}
|
||||
onAccept={() => onAcceptProposal(proposal.eventId)}
|
||||
proposal={proposal}
|
||||
/>
|
||||
) : null}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -33,6 +33,7 @@ import { useThreadRepliesForRoots } from "@/features/messages/useThreadReplies";
|
||||
import {
|
||||
buildProjectReviewCheckPrompt,
|
||||
DEFAULT_PROJECT_REVIEW_CHECKS,
|
||||
parseProjectReviewCheckDiffEvent,
|
||||
parseProjectReviewCheckResult,
|
||||
parseProjectReviewChecksConfig,
|
||||
PROJECT_REVIEW_CHECKS_CONFIG_PATH,
|
||||
@@ -40,12 +41,14 @@ import {
|
||||
readProjectReviewCheckRuns,
|
||||
writeProjectReviewCheckRuns,
|
||||
type ProjectReviewCheckDefinition,
|
||||
type ProjectReviewCheckDiffProposal,
|
||||
type ProjectReviewCheckResult,
|
||||
} from "@/features/projects/lib/projectReviewChecks.mjs";
|
||||
import { isAtOrAfterConversationOpener } from "@/features/projects/lib/projectAgentConversation";
|
||||
import type { ProjectsConversationOpener } from "@/features/projects/lib/projectAgentConversationStorage";
|
||||
import { pullRequestShareLink } from "@/features/projects/lib/projectShareLinks";
|
||||
import type { ProjectPullRequest, Repository } from "@/features/projects/hooks";
|
||||
import { ProjectReviewCheckResultCard } from "@/features/projects/ui/ProjectReviewCheckResultCard";
|
||||
import { useIdentityQuery } from "@/shared/api/hooks";
|
||||
import { listManagedAgents } from "@/shared/api/tauri";
|
||||
import { revalidateRelayAgents } from "@/shared/api/tauriRelayAgents";
|
||||
@@ -54,6 +57,7 @@ import { getProjectRepoFileContent } from "@/shared/api/projectGit";
|
||||
import type { Channel, RelayEvent } from "@/shared/api/types";
|
||||
import {
|
||||
KIND_STREAM_MESSAGE,
|
||||
KIND_STREAM_MESSAGE_DIFF,
|
||||
KIND_STREAM_MESSAGE_V2,
|
||||
} from "@/shared/constants/kinds";
|
||||
import { normalizePubkey } from "@/shared/lib/pubkey";
|
||||
@@ -77,6 +81,8 @@ type ReviewCheckRun = {
|
||||
channelId?: string;
|
||||
opener?: ProjectsConversationOpener;
|
||||
result?: ProjectReviewCheckResult;
|
||||
proposal?: ProjectReviewCheckDiffProposal;
|
||||
acceptedProposalEventId?: string;
|
||||
resultCreatedAt?: number;
|
||||
error?: string;
|
||||
};
|
||||
@@ -99,6 +105,51 @@ function isReviewCheckStatus(value: unknown): value is ReviewCheckStatus {
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeStoredProposal(
|
||||
value: unknown,
|
||||
agentPubkey: string | null,
|
||||
): ProjectReviewCheckDiffProposal | undefined {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return undefined;
|
||||
}
|
||||
const raw = value as Record<string, unknown>;
|
||||
if (
|
||||
!agentPubkey ||
|
||||
typeof raw.eventId !== "string" ||
|
||||
typeof raw.content !== "string" ||
|
||||
typeof raw.repoUrl !== "string" ||
|
||||
typeof raw.commitSha !== "string"
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
const tags = [
|
||||
["repo", raw.repoUrl],
|
||||
["commit", raw.commitSha],
|
||||
...(typeof raw.filePath === "string" ? [["file", raw.filePath]] : []),
|
||||
...(typeof raw.description === "string"
|
||||
? [["description", raw.description]]
|
||||
: []),
|
||||
...(raw.truncated === true ? [["truncated", "true"]] : []),
|
||||
];
|
||||
return (
|
||||
parseProjectReviewCheckDiffEvent(
|
||||
{
|
||||
id: raw.eventId,
|
||||
pubkey: agentPubkey,
|
||||
kind: KIND_STREAM_MESSAGE_DIFF,
|
||||
content: raw.content,
|
||||
tags,
|
||||
},
|
||||
{
|
||||
eventId: raw.eventId,
|
||||
agentPubkey,
|
||||
repoUrl: raw.repoUrl,
|
||||
commit: raw.commitSha,
|
||||
},
|
||||
) ?? undefined
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeStoredRuns(value: Record<string, unknown>): ReviewCheckRuns {
|
||||
const runs: ReviewCheckRuns = {};
|
||||
for (const [checkId, candidate] of Object.entries(value)) {
|
||||
@@ -112,6 +163,7 @@ function normalizeStoredRuns(value: Record<string, unknown>): ReviewCheckRuns {
|
||||
const raw = candidate as Record<string, unknown>;
|
||||
const agentPubkey =
|
||||
typeof raw.agentPubkey === "string" ? raw.agentPubkey : null;
|
||||
const proposal = normalizeStoredProposal(raw.proposal, agentPubkey);
|
||||
const status = isReviewCheckStatus(raw.status) ? raw.status : "idle";
|
||||
const parsedResult = parseProjectReviewCheckResult(
|
||||
raw.result && typeof raw.result === "object"
|
||||
@@ -142,6 +194,10 @@ function normalizeStoredRuns(value: Record<string, unknown>): ReviewCheckRuns {
|
||||
: {}),
|
||||
...(opener ? { opener } : {}),
|
||||
...(parsedResult ? { result: parsedResult } : {}),
|
||||
...(proposal ? { proposal } : {}),
|
||||
...(proposal && raw.acceptedProposalEventId === proposal.eventId
|
||||
? { acceptedProposalEventId: proposal.eventId }
|
||||
: {}),
|
||||
...(typeof raw.resultCreatedAt === "number"
|
||||
? { resultCreatedAt: raw.resultCreatedAt }
|
||||
: {}),
|
||||
@@ -273,7 +329,9 @@ function ReviewCheckResultObserver({
|
||||
checkId,
|
||||
onResult,
|
||||
opener,
|
||||
repoUrl,
|
||||
requestId,
|
||||
targetCommit,
|
||||
}: {
|
||||
agentPubkey: string;
|
||||
channel: Channel | null;
|
||||
@@ -282,9 +340,12 @@ function ReviewCheckResultObserver({
|
||||
checkId: string,
|
||||
result: ProjectReviewCheckResult,
|
||||
createdAt: number,
|
||||
proposal?: ProjectReviewCheckDiffProposal,
|
||||
) => void;
|
||||
opener: ProjectsConversationOpener;
|
||||
repoUrl: string | null;
|
||||
requestId: string | null;
|
||||
targetCommit: string | null;
|
||||
}) {
|
||||
useChannelSubscription(channel);
|
||||
const messagesQuery = useChannelMessagesQuery(channel);
|
||||
@@ -301,23 +362,44 @@ function ReviewCheckResultObserver({
|
||||
);
|
||||
const threadReplies = useThreadRepliesForRoots(channel, threadRootIds);
|
||||
const observedResult = React.useMemo(() => {
|
||||
const events = [
|
||||
const agentEvents = [
|
||||
...(messagesQuery.data ?? []),
|
||||
...(threadReplies.events ?? []),
|
||||
]
|
||||
.filter(
|
||||
(event) =>
|
||||
isAgentMessage(event, agentPubkey) &&
|
||||
isAtOrAfterConversationOpener(event, opener),
|
||||
)
|
||||
].filter(
|
||||
(event) =>
|
||||
normalizePubkey(event.pubkey) === normalizePubkey(agentPubkey) &&
|
||||
isAtOrAfterConversationOpener(event, opener),
|
||||
);
|
||||
const resultEvents = agentEvents
|
||||
.filter((event) => isAgentMessage(event, agentPubkey))
|
||||
.sort(
|
||||
(left, right) =>
|
||||
left.created_at - right.created_at || right.id.localeCompare(left.id),
|
||||
);
|
||||
for (const event of events) {
|
||||
for (const event of resultEvents) {
|
||||
const result = parseProjectReviewCheckResult(event.content);
|
||||
if (result && (!requestId || result.requestId === requestId)) {
|
||||
return { createdAt: event.created_at, result };
|
||||
let proposal: ProjectReviewCheckDiffProposal | undefined;
|
||||
if (result.diffEventId) {
|
||||
if (!repoUrl) continue;
|
||||
const diffEvent = agentEvents.find(
|
||||
(candidate) =>
|
||||
candidate.kind === KIND_STREAM_MESSAGE_DIFF &&
|
||||
candidate.id.toLowerCase() === result.diffEventId,
|
||||
);
|
||||
proposal = diffEvent
|
||||
? (parseProjectReviewCheckDiffEvent(diffEvent, {
|
||||
eventId: result.diffEventId,
|
||||
agentPubkey,
|
||||
repoUrl,
|
||||
commit: targetCommit,
|
||||
}) ?? undefined)
|
||||
: undefined;
|
||||
// The agent is instructed to publish the native diff before its
|
||||
// result. Keep observing if relay delivery arrives out of order.
|
||||
if (!proposal) continue;
|
||||
}
|
||||
return { createdAt: event.created_at, proposal, result };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -325,13 +407,20 @@ function ReviewCheckResultObserver({
|
||||
agentPubkey,
|
||||
messagesQuery.data,
|
||||
opener,
|
||||
repoUrl,
|
||||
requestId,
|
||||
targetCommit,
|
||||
threadReplies.events,
|
||||
]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (observedResult) {
|
||||
onResult(checkId, observedResult.result, observedResult.createdAt);
|
||||
onResult(
|
||||
checkId,
|
||||
observedResult.result,
|
||||
observedResult.createdAt,
|
||||
observedResult.proposal,
|
||||
);
|
||||
}
|
||||
}, [checkId, observedResult, onResult]);
|
||||
|
||||
@@ -367,74 +456,6 @@ function CheckStatus({
|
||||
return null;
|
||||
}
|
||||
|
||||
function ReviewCheckResultCard({
|
||||
result,
|
||||
}: {
|
||||
result: ProjectReviewCheckResult;
|
||||
}) {
|
||||
const isApproved = result.conclusion === "approved";
|
||||
const findingCount = result.findings.length;
|
||||
const title = isApproved
|
||||
? "Approved"
|
||||
: findingCount === 0
|
||||
? "Fix recommended"
|
||||
: `${findingCount} ${findingCount === 1 ? "fix" : "fixes"} recommended`;
|
||||
|
||||
return (
|
||||
<section
|
||||
className="overflow-hidden rounded-xl border border-border/70 bg-background/70"
|
||||
data-testid="project-review-check-result"
|
||||
>
|
||||
<div className="flex items-start gap-3 px-4 py-3.5">
|
||||
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-muted/70">
|
||||
{isApproved ? (
|
||||
<CheckCircle2 className="h-5 w-5 text-emerald-500" />
|
||||
) : (
|
||||
<CircleAlert className="h-5 w-5 text-amber-500" />
|
||||
)}
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<h5 className="text-sm font-semibold text-foreground">{title}</h5>
|
||||
<p className="mt-1 text-xs leading-relaxed text-muted-foreground">
|
||||
{result.summary}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
{findingCount > 0 ? (
|
||||
<div className="divide-y divide-border/55 border-t border-border/55">
|
||||
{result.findings.map((finding) => (
|
||||
<article
|
||||
className="px-4 py-3"
|
||||
data-testid="project-review-check-finding"
|
||||
key={JSON.stringify(finding)}
|
||||
>
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<p className="min-w-0 flex-1 text-xs font-medium text-foreground">
|
||||
{finding.title}
|
||||
</p>
|
||||
{finding.file ? (
|
||||
<span
|
||||
className="max-w-[45%] truncate font-mono text-2xs text-muted-foreground"
|
||||
title={`${finding.file}${finding.line ? `:${finding.line}` : ""}`}
|
||||
>
|
||||
{finding.file}
|
||||
{finding.line ? `:${finding.line}` : ""}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
{finding.detail ? (
|
||||
<p className="mt-1 text-xs leading-relaxed text-muted-foreground">
|
||||
{finding.detail}
|
||||
</p>
|
||||
) : null}
|
||||
</article>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
function AgentPicker({
|
||||
candidates,
|
||||
disabled,
|
||||
@@ -527,6 +548,14 @@ export function ProjectReviewChecks({
|
||||
() => new Set(),
|
||||
);
|
||||
const targetCommit = pullRequest.commit ?? pullRequest.initialCommit;
|
||||
const diffRepoUrl = React.useMemo(
|
||||
() =>
|
||||
[project.webUrl, ...project.cloneUrls].find(
|
||||
(url): url is string =>
|
||||
typeof url === "string" && /^https?:\/\//i.test(url),
|
||||
) ?? null,
|
||||
[project.cloneUrls, project.webUrl],
|
||||
);
|
||||
|
||||
React.useEffect(() => {
|
||||
storageKeyRef.current = storageKey;
|
||||
@@ -575,12 +604,19 @@ export function ProjectReviewChecks({
|
||||
);
|
||||
|
||||
const recordResult = React.useCallback(
|
||||
(checkId: string, result: ProjectReviewCheckResult, createdAt: number) => {
|
||||
(
|
||||
checkId: string,
|
||||
result: ProjectReviewCheckResult,
|
||||
createdAt: number,
|
||||
proposal?: ProjectReviewCheckDiffProposal,
|
||||
) => {
|
||||
const expectedStorageKey = storageKeyRef.current;
|
||||
if (!expectedStorageKey) return;
|
||||
updateRun(expectedStorageKey, checkId, (current) => ({
|
||||
...current,
|
||||
error: undefined,
|
||||
acceptedProposalEventId: undefined,
|
||||
proposal,
|
||||
result,
|
||||
resultCreatedAt: createdAt,
|
||||
status: "completed",
|
||||
@@ -594,6 +630,20 @@ export function ProjectReviewChecks({
|
||||
[updateRun],
|
||||
);
|
||||
|
||||
const acceptProposal = React.useCallback(
|
||||
(checkId: string, eventId: string) => {
|
||||
const expectedStorageKey = storageKeyRef.current;
|
||||
if (!expectedStorageKey) return;
|
||||
updateRun(expectedStorageKey, checkId, (current) =>
|
||||
current.proposal?.eventId === eventId
|
||||
? { ...current, acceptedProposalEventId: eventId }
|
||||
: current,
|
||||
);
|
||||
toast.success("Proposal accepted locally. No code was changed.");
|
||||
},
|
||||
[updateRun],
|
||||
);
|
||||
|
||||
const runCheck = React.useCallback(
|
||||
async (check: ProjectReviewCheckDefinition, agent: ReviewCheckAgent) => {
|
||||
const expectedStorageKey = storageKey;
|
||||
@@ -666,6 +716,8 @@ export function ProjectReviewChecks({
|
||||
check,
|
||||
projectName: project.name,
|
||||
repoAddress: pullRequest.repoAddress ?? project.repoAddress,
|
||||
repoUrl: diffRepoUrl,
|
||||
channelId: channel.id,
|
||||
reviewId: pullRequest.id,
|
||||
reviewLink: pullRequestShareLink(pullRequest) ?? "unavailable",
|
||||
reviewTitle: pullRequest.title,
|
||||
@@ -698,6 +750,8 @@ export function ProjectReviewChecks({
|
||||
eventId: sent.eventId,
|
||||
},
|
||||
requestId,
|
||||
acceptedProposalEventId: undefined,
|
||||
proposal: undefined,
|
||||
result: undefined,
|
||||
resultCreatedAt: undefined,
|
||||
status: "running",
|
||||
@@ -728,6 +782,7 @@ export function ProjectReviewChecks({
|
||||
},
|
||||
[
|
||||
openDmMutation,
|
||||
diffRepoUrl,
|
||||
project.name,
|
||||
project.repoAddress,
|
||||
pullRequest,
|
||||
@@ -857,7 +912,15 @@ export function ProjectReviewChecks({
|
||||
<p className="text-xs text-destructive">{run.error}</p>
|
||||
) : null}
|
||||
{run.result ? (
|
||||
<ReviewCheckResultCard result={run.result} />
|
||||
<ProjectReviewCheckResultCard
|
||||
acceptedProposalEventId={run.acceptedProposalEventId}
|
||||
canAcceptProposal={!isStale}
|
||||
onAcceptProposal={(eventId) =>
|
||||
acceptProposal(check.id, eventId)
|
||||
}
|
||||
proposal={run.proposal}
|
||||
result={run.result}
|
||||
/>
|
||||
) : null}
|
||||
{run.status === "running" && run.opener && run.agentPubkey ? (
|
||||
<ReviewCheckResultObserver
|
||||
@@ -866,7 +929,9 @@ export function ProjectReviewChecks({
|
||||
checkId={check.id}
|
||||
onResult={recordResult}
|
||||
opener={run.opener}
|
||||
repoUrl={diffRepoUrl}
|
||||
requestId={run.requestId ?? null}
|
||||
targetCommit={run.targetCommit ?? targetCommit}
|
||||
/>
|
||||
) : null}
|
||||
</article>
|
||||
|
||||
@@ -11,6 +11,7 @@ const RELAY_REVIEW_AGENT_PUBKEY =
|
||||
"554cef57437abac34522ac2c9f0490d685b72c80478cf9f7ed6f9570ee8624ea";
|
||||
const LOCAL_REVIEW_AGENT_PUBKEY = "c".repeat(64);
|
||||
const STOPPED_LOCAL_REVIEW_AGENT_PUBKEY = "d".repeat(64);
|
||||
const PROPOSED_DIFF_EVENT_ID = "e".repeat(64);
|
||||
|
||||
async function expectSinglePrimaryTextColumn(row: Locator) {
|
||||
const primary = row.locator('[data-projects-text-priority="primary"]');
|
||||
@@ -171,8 +172,17 @@ test("review checks dispatch to an agent authorized for the relay identity", asy
|
||||
});
|
||||
expect(sentCheck?.content).toContain("Interface & design system");
|
||||
expect(sentCheck?.content).toContain("Commit under review:");
|
||||
expect(sentCheck?.content).toContain("buzz messages send-diff");
|
||||
expect(sentCheck?.mentionPubkeys).toContain(RELAY_REVIEW_AGENT_PUBKEY);
|
||||
const requestId = requestIdFromCheckPrompt(sentCheck?.content);
|
||||
const reviewedCommit = sentCheck?.content
|
||||
?.match(/Commit under review: ([^\n]+)/)?.[1]
|
||||
?.trim();
|
||||
const diffRepoUrl = sentCheck?.content?.match(
|
||||
/Repository URL for diff metadata: "([^"]+)"/,
|
||||
)?.[1];
|
||||
expect(reviewedCommit).toBeTruthy();
|
||||
expect(diffRepoUrl).toBeTruthy();
|
||||
await expect
|
||||
.poll(() =>
|
||||
page.evaluate(() =>
|
||||
@@ -183,13 +193,36 @@ test("review checks dispatch to an agent authorized for the relay identity", asy
|
||||
|
||||
await waitForMockLiveSubscription(page, "DM");
|
||||
await page.evaluate(
|
||||
({ agentPubkey, requestId }) => {
|
||||
({ agentPubkey, diffEventId, diffRepoUrl, requestId, reviewedCommit }) => {
|
||||
window.__BUZZ_E2E_EMIT_MOCK_MESSAGE__?.({
|
||||
channelName: "DM",
|
||||
content: [
|
||||
"diff --git a/desktop/src/shared/ui/button.tsx b/desktop/src/shared/ui/button.tsx",
|
||||
"--- a/desktop/src/shared/ui/button.tsx",
|
||||
"+++ b/desktop/src/shared/ui/button.tsx",
|
||||
"@@ -1,3 +1,3 @@",
|
||||
"-const focusStyle = 'outline-none';",
|
||||
"+const focusStyle = 'focus-visible:ring-1';",
|
||||
" export { focusStyle };",
|
||||
].join("\n"),
|
||||
createdAt: Math.floor(Date.now() / 1_000) + 1,
|
||||
extraTags: [
|
||||
["repo", diffRepoUrl],
|
||||
["commit", reviewedCommit],
|
||||
["file", "desktop/src/shared/ui/button.tsx"],
|
||||
["description", "Use the shared focus treatment"],
|
||||
],
|
||||
id: diffEventId,
|
||||
kind: 40008,
|
||||
pubkey: agentPubkey,
|
||||
});
|
||||
window.__BUZZ_E2E_EMIT_MOCK_MESSAGE__?.({
|
||||
channelName: "DM",
|
||||
content: `BUZZ_CHECK_RESULT_V1\n${JSON.stringify({
|
||||
request_id: requestId,
|
||||
conclusion: "fix-recommended",
|
||||
summary: "Two interface fixes are needed before approval.",
|
||||
diff_event_id: diffEventId,
|
||||
findings: [
|
||||
{
|
||||
title: "Use the shared button primitive",
|
||||
@@ -205,16 +238,27 @@ test("review checks dispatch to an agent authorized for the relay identity", asy
|
||||
},
|
||||
],
|
||||
})}`,
|
||||
createdAt: Math.floor(Date.now() / 1_000) + 1,
|
||||
createdAt: Math.floor(Date.now() / 1_000) + 2,
|
||||
kind: 9,
|
||||
pubkey: agentPubkey,
|
||||
});
|
||||
},
|
||||
{ agentPubkey: RELAY_REVIEW_AGENT_PUBKEY, requestId },
|
||||
{
|
||||
agentPubkey: RELAY_REVIEW_AGENT_PUBKEY,
|
||||
diffEventId: PROPOSED_DIFF_EVENT_ID,
|
||||
diffRepoUrl: diffRepoUrl ?? "",
|
||||
requestId,
|
||||
reviewedCommit: reviewedCommit ?? "",
|
||||
},
|
||||
);
|
||||
|
||||
const result = interfaceCheck.getByTestId("project-review-check-result");
|
||||
await expect(result.getByRole("heading")).toHaveText("2 fixes recommended");
|
||||
await expect(
|
||||
result.getByRole("heading", {
|
||||
name: "2 fixes recommended",
|
||||
exact: true,
|
||||
}),
|
||||
).toBeVisible();
|
||||
await expect(result).toContainText(
|
||||
"Two interface fixes are needed before approval.",
|
||||
);
|
||||
@@ -222,6 +266,24 @@ test("review checks dispatch to an agent authorized for the relay identity", asy
|
||||
2,
|
||||
);
|
||||
await expect(result).toContainText("ProjectReviewChecks.tsx:742");
|
||||
const proposal = result.getByTestId("project-review-check-diff-proposal");
|
||||
await expect(proposal).toContainText("Proposed code changes");
|
||||
await expect(proposal).toContainText("button.tsx");
|
||||
await expect(proposal).toContainText("Use the shared focus treatment");
|
||||
await expect(proposal).toContainText("focus-visible:ring-1");
|
||||
const commandCountBeforeAccept = await page.evaluate(
|
||||
() => window.__BUZZ_E2E_COMMANDS__?.length ?? 0,
|
||||
);
|
||||
await proposal.getByRole("button", { name: "Accept changes" }).click();
|
||||
await expect(
|
||||
proposal.getByRole("button", { name: "Accepted" }),
|
||||
).toBeVisible();
|
||||
await expect(proposal).toContainText(
|
||||
"No repository files or review state were changed.",
|
||||
);
|
||||
await expect
|
||||
.poll(() => page.evaluate(() => window.__BUZZ_E2E_COMMANDS__?.length ?? 0))
|
||||
.toBe(commandCountBeforeAccept);
|
||||
await waitForAnimations(page);
|
||||
await result.screenshot({ path: `${SHOTS}/review-check-result.png` });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user