feat(pr-review): operator/chat trigger queues a pr-review task
review_pr self-omitted since 7299b923 deleted the synchronous reviewer it
called. It is rebuilt on the task enqueue path everything else uses: the
tool resolves the identity's configured repo, then queues a pr-review task
naming the pull request through the workflow's declared pr_number input,
which is the same generic input mechanism a watched-repos binding uses.
That input has to reach the store with the task, so
EnqueueChatTaskRequest gains inputs_json and the tasks insert writes the
inputs column directly. StampTaskBinding no longer writes it: the insert is
now the column's only writer, and a chat task must not acquire binding
provenance just to carry an input.
ChatPRReviewer, its result shape and the reviewer wiring are deleted with
the path they served.
This commit is contained in:
1 parent
a6627954bd
commit
5c91575166
39 files changed
+227
-192
No files matched your search
@@ -8,69 +8,33 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
// PRReviewResult is the structured outcome of an operator-triggered PR
|
||||
// review, returned to the channel that requested it. It mirrors
|
||||
// workflow.ReviewReport without importing the domain package: the gateway
|
||||
// states what it needs and the daemon adapts between the two (the
|
||||
// ChatTaskActor pattern). Status is "completed", "not_run", or "skipped";
|
||||
// a zero-finding "completed" result is a review that ran and cleared, and
|
||||
// is structurally distinct from "not_run".
|
||||
type PRReviewResult struct {
|
||||
Repo string `json:"repo"`
|
||||
Number int `json:"number"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Summary string `json:"summary,omitempty"`
|
||||
// Reason explains why a not_run/skipped review did not execute.
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Findings []PRReviewFinding `json:"findings"`
|
||||
// ArtifactURL is the editor deep link for the published review artifact,
|
||||
// empty when artifact publishing is disabled or failed.
|
||||
ArtifactURL string `json:"artifact_url,omitempty"`
|
||||
}
|
||||
|
||||
// PRReviewFinding is one defect from an operator-triggered review.
|
||||
type PRReviewFinding struct {
|
||||
File string `json:"file"`
|
||||
Line int `json:"line"`
|
||||
Defect string `json:"defect"`
|
||||
FailureScenario string `json:"failure_scenario,omitempty"`
|
||||
Verdict string `json:"verdict"`
|
||||
Level string `json:"level"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Blocking bool `json:"blocking"`
|
||||
}
|
||||
|
||||
// ChatPRReviewer is the surface review_pr needs. The gateway states what it
|
||||
// needs and the daemon supplies an adapter over the forge, worktree manager
|
||||
// and reviewer runtime, so this package keeps its independence from
|
||||
// internal/forge, internal/worktree and internal/domain/workflow.
|
||||
// ReviewTools builds the review_pr chat tool. A nil creator omits the tool
|
||||
// rather than registering one that always fails, so a daemon without a task
|
||||
// store advertises nothing.
|
||||
//
|
||||
// identity follows the ChatTaskActor convention: nil denotes an
|
||||
// authenticated dashboard operator who may review any configured
|
||||
// repository; a non-nil identity is a chat identity scoped to its own
|
||||
// configured repositories.
|
||||
type ChatPRReviewer interface {
|
||||
ReviewPR(ctx context.Context, identity *string, repo string, number int) (PRReviewResult, error)
|
||||
}
|
||||
|
||||
// ReviewTools builds the review_pr chat tool. A nil reviewer omits the tool
|
||||
// rather than registering one that always fails, so a daemon without
|
||||
// operator review support advertises nothing.
|
||||
func ReviewTools(reviewer ChatPRReviewer, identity string) []tools.ToolEntry {
|
||||
if reviewer == nil {
|
||||
// The tool queues a pr-review task; it does not review in the turn. The
|
||||
// pipeline needs a sandbox, a prefetched snapshot and minutes of model calls,
|
||||
// so the operator gets a task ID back and watches the task like any other
|
||||
// (docs/prds/pr-review-agent.md, "Triggers").
|
||||
func ReviewTools(creator TaskCreator, identity string) []tools.ToolEntry {
|
||||
if creator == nil {
|
||||
return nil
|
||||
}
|
||||
return []tools.ToolEntry{reviewPRTool(reviewer, identity)}
|
||||
return []tools.ToolEntry{reviewPRTool(creator, identity)}
|
||||
}
|
||||
|
||||
func reviewPRTool(reviewer ChatPRReviewer, identity string) tools.ToolEntry {
|
||||
// prReviewWorkflow is the workflow a queued operator review runs. Its
|
||||
// declared interface requires pr_number, so the tool that names it is also
|
||||
// the only chat path that can satisfy it.
|
||||
const prReviewWorkflow = "pr-review"
|
||||
|
||||
func reviewPRTool(creator TaskCreator, identity string) tools.ToolEntry {
|
||||
return tools.ToolEntry{
|
||||
Name: "review_pr",
|
||||
Toolset: "tasks",
|
||||
Description: "Run the adversarial reviewer against an existing pull request and return its structured findings. " +
|
||||
"Use it when the user asks to review a PR, re-check one after a push, or see what the reviewer found. " +
|
||||
"Only a confirmed error-level finding blocks; a clean result reports zero findings explicitly.",
|
||||
Description: "Queue a review of an existing pull request. Use it when the user asks to review a PR, " +
|
||||
"re-check one after a push, or see what the reviewer found. The review runs as a task in its own " +
|
||||
"sandbox and posts its findings to the pull request; this returns the task ID immediately.",
|
||||
Classification: tools.ClassMutating,
|
||||
Schema: tools.JSONSchema{
|
||||
"type": "object",
|
||||
@@ -95,11 +59,26 @@ func reviewPRTool(reviewer ChatPRReviewer, identity string) tools.ToolEntry {
|
||||
if !ok || number <= 0 {
|
||||
return nil, fmt.Errorf("review_pr: pr_number must be a positive integer")
|
||||
}
|
||||
result, err := reviewer.ReviewPR(ctx, &identity, repo, int(number))
|
||||
id, err := creator.CreateTask(ctx, SpawnRequest{
|
||||
Title: fmt.Sprintf("review %s#%d", repo, number),
|
||||
Body: fmt.Sprintf("Operator-requested review of pull request %s#%d.", repo, number),
|
||||
Repo: repo,
|
||||
Workflow: prReviewWorkflow,
|
||||
// int, not int64: task.Task.EffectivePRNumber reads the
|
||||
// input as json.Number, float64 or int, and an int64 would
|
||||
// resolve to 0 for a task that names a real PR.
|
||||
Inputs: map[string]any{"pr_number": int(number)},
|
||||
// The bound identity, never input["identity"]: CreateTask
|
||||
// enforces the repository allow-list for it.
|
||||
Identity: identity,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("review_pr: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
return TaskSpawnResult{
|
||||
ID: id,
|
||||
Message: fmt.Sprintf("queued pr-review task %d for %s#%d", id, repo, number),
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -6,29 +6,14 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakePRReviewer struct {
|
||||
gotIdentity *string
|
||||
gotRepo string
|
||||
gotNumber int
|
||||
result PRReviewResult
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakePRReviewer) ReviewPR(_ context.Context, identity *string, repo string, number int) (PRReviewResult, error) {
|
||||
f.gotIdentity = identity
|
||||
f.gotRepo = repo
|
||||
f.gotNumber = number
|
||||
return f.result, f.err
|
||||
}
|
||||
|
||||
func TestReviewToolOmittedWhenReviewerNil(t *testing.T) {
|
||||
func TestReviewToolOmittedWhenCreatorNil(t *testing.T) {
|
||||
if entries := ReviewTools(nil, "archie"); len(entries) != 0 {
|
||||
t.Fatalf("ReviewTools(nil) = %d entries, want 0 -- a tool that cannot run must not be advertised", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
func TestReviewToolAppearsWhenReviewerNonNil(t *testing.T) {
|
||||
entries := ReviewTools(&fakePRReviewer{}, "archie")
|
||||
func TestReviewToolAppearsWhenCreatorNonNil(t *testing.T) {
|
||||
entries := ReviewTools(&fakeCreator{}, "archie")
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("ReviewTools() = %d entries, want 1", len(entries))
|
||||
}
|
||||
@@ -37,14 +22,9 @@ func TestReviewToolAppearsWhenReviewerNonNil(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReviewPRToolBindsIdentityAndReturnsFindings(t *testing.T) {
|
||||
reviewer := &fakePRReviewer{
|
||||
result: PRReviewResult{
|
||||
Repo: "acme/widget", Number: 7, Model: "openai/gpt-5", Status: "completed",
|
||||
Findings: []PRReviewFinding{{File: "a.go", Line: 3, Defect: "nil deref", FailureScenario: "x", Verdict: "confirmed", Level: "error", Category: "nil-risk", Blocking: true}},
|
||||
},
|
||||
}
|
||||
entry := toolNamed(t, ReviewTools(reviewer, "archie"), "review_pr")
|
||||
func TestReviewPRToolQueuesReviewTask(t *testing.T) {
|
||||
creator := &fakeCreator{id: 42}
|
||||
entry := toolNamed(t, ReviewTools(creator, "archie"), "review_pr")
|
||||
|
||||
out, err := entry.Handler(context.Background(), map[string]any{
|
||||
"repo": "acme/widget", "pr_number": float64(7), "identity": "someone-else",
|
||||
@@ -53,27 +33,37 @@ func TestReviewPRToolBindsIdentityAndReturnsFindings(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got := creator.got
|
||||
if got.Workflow != "pr-review" {
|
||||
t.Errorf("workflow = %q, want pr-review -- the operator trigger runs the pipeline, it does not review in the turn", got.Workflow)
|
||||
}
|
||||
if got.Repo != "acme/widget" {
|
||||
t.Errorf("repo = %q, want acme/widget -- CreateTask authorizes against it", got.Repo)
|
||||
}
|
||||
// The model supplied an identity; it must be ignored in favour of the
|
||||
// bound identity.
|
||||
if reviewer.gotIdentity == nil || *reviewer.gotIdentity != "archie" {
|
||||
t.Fatalf("identity = %v, want bound \"archie\"", reviewer.gotIdentity)
|
||||
if got.Identity != "archie" {
|
||||
t.Errorf("identity = %q, want bound %q", got.Identity, "archie")
|
||||
}
|
||||
if reviewer.gotRepo != "acme/widget" || reviewer.gotNumber != 7 {
|
||||
t.Fatalf("ReviewPR called with (%q, %d), want (acme/widget, 7)", reviewer.gotRepo, reviewer.gotNumber)
|
||||
if n, ok := got.Inputs["pr_number"].(int); !ok || n != 7 {
|
||||
t.Errorf("inputs[pr_number] = %#v, want int 7", got.Inputs["pr_number"])
|
||||
}
|
||||
if got.Title == "" || got.Body == "" {
|
||||
t.Errorf("title/body = %q/%q, want both set so the queued task is identifiable", got.Title, got.Body)
|
||||
}
|
||||
|
||||
result, ok := out.(PRReviewResult)
|
||||
result, ok := out.(TaskSpawnResult)
|
||||
if !ok {
|
||||
t.Fatalf("handler returned %T, want PRReviewResult", out)
|
||||
t.Fatalf("handler returned %T, want TaskSpawnResult", out)
|
||||
}
|
||||
if result.Status != "completed" || len(result.Findings) != 1 || !result.Findings[0].Blocking {
|
||||
t.Fatalf("result = %+v, want completed with one blocking finding", result)
|
||||
if result.ID != 42 {
|
||||
t.Errorf("result.ID = %d, want 42", result.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReviewPRToolValidatesInput(t *testing.T) {
|
||||
reviewer := &fakePRReviewer{}
|
||||
entry := toolNamed(t, ReviewTools(reviewer, "archie"), "review_pr")
|
||||
creator := &fakeCreator{}
|
||||
entry := toolNamed(t, ReviewTools(creator, "archie"), "review_pr")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -91,15 +81,18 @@ func TestReviewPRToolValidatesInput(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
if len(creator.got.Inputs) != 0 || creator.got.Repo != "" {
|
||||
t.Errorf("rejected input still reached CreateTask: %+v", creator.got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReviewPRToolSurfacesReviewerError(t *testing.T) {
|
||||
reviewer := &fakePRReviewer{err: errors.New("review already in progress")}
|
||||
entry := toolNamed(t, ReviewTools(reviewer, "archie"), "review_pr")
|
||||
func TestReviewPRToolSurfacesCreatorError(t *testing.T) {
|
||||
creator := &fakeCreator{err: errors.New("repo not configured")}
|
||||
entry := toolNamed(t, ReviewTools(creator, "archie"), "review_pr")
|
||||
|
||||
if _, err := entry.Handler(context.Background(), map[string]any{
|
||||
"repo": "acme/widget", "pr_number": float64(7),
|
||||
}); err == nil {
|
||||
t.Fatal("handler error = nil, want the reviewer failure surfaced")
|
||||
t.Fatal("handler error = nil, want the creator failure surfaced")
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ type taskProfile struct {
|
||||
// internal/domain/workflow; the daemon supplies an adapter closure over
|
||||
// the store.TaskStore method of the same name.
|
||||
type chatTaskWriter interface {
|
||||
EnqueueChatTask(ctx context.Context, owner, repo, title, body, workflow, identity string) (taskID int64, err error)
|
||||
EnqueueChatTask(ctx context.Context, owner, repo, title, body, workflow, identity string, inputs map[string]any) (taskID int64, err error)
|
||||
}
|
||||
|
||||
// NewStoreTaskCreator returns a TaskCreator that enqueues chat-spawned
|
||||
@@ -103,7 +103,7 @@ func (c *StoreTaskCreator) CreateTask(ctx context.Context, req SpawnRequest) (in
|
||||
if owner == "" || repo == "" {
|
||||
return 0, fmt.Errorf("no repo configured for chat-spawned tasks")
|
||||
}
|
||||
return c.store.EnqueueChatTask(ctx, owner, repo, req.Title, req.Body, req.Workflow, req.Identity)
|
||||
return c.store.EnqueueChatTask(ctx, owner, repo, req.Title, req.Body, req.Workflow, req.Identity, req.Inputs)
|
||||
}
|
||||
|
||||
func splitOwnerRepo(s string) (owner, repo string, ok bool) {
|
||||
|
||||
@@ -13,13 +13,15 @@ import (
|
||||
|
||||
type fakeChatTaskWriter struct {
|
||||
owner, repo, title, body, workflow, identity string
|
||||
inputs map[string]any
|
||||
calls int
|
||||
nextID int64
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeChatTaskWriter) EnqueueChatTask(ctx context.Context, owner, repo, title, body, workflow, identity string) (int64, error) {
|
||||
func (f *fakeChatTaskWriter) EnqueueChatTask(ctx context.Context, owner, repo, title, body, workflow, identity string, inputs map[string]any) (int64, error) {
|
||||
f.owner, f.repo, f.title, f.body, f.workflow, f.identity = owner, repo, title, body, workflow, identity
|
||||
f.inputs = inputs
|
||||
f.calls++
|
||||
if f.err != nil {
|
||||
return 0, f.err
|
||||
@@ -46,6 +48,26 @@ func TestStoreTaskCreatorCreatesTask(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreTaskCreatorForwardsInputs(t *testing.T) {
|
||||
sw := &fakeChatTaskWriter{}
|
||||
tc := NewStoreTaskCreator(sw, "acme", "example-service", []string{"acme/example-service"})
|
||||
_, err := tc.CreateTask(context.Background(), SpawnRequest{
|
||||
Title: "review",
|
||||
Repo: "acme/example-service",
|
||||
Workflow: "pr-review",
|
||||
Inputs: map[string]any{"pr_number": 7},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateTask: %v", err)
|
||||
}
|
||||
if sw.workflow != "pr-review" {
|
||||
t.Errorf("workflow = %q, want pr-review", sw.workflow)
|
||||
}
|
||||
if n, ok := sw.inputs["pr_number"].(int); !ok || n != 7 {
|
||||
t.Errorf("inputs = %#v, want pr_number 7", sw.inputs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreTaskCreatorRejectsEmptyRepo(t *testing.T) {
|
||||
tc := NewStoreTaskCreator(&fakeChatTaskWriter{}, "", "", nil)
|
||||
_, err := tc.CreateTask(context.Background(), SpawnRequest{Title: "something"})
|
||||
|
||||
@@ -88,7 +88,6 @@ type TurnRunnerConfig struct {
|
||||
TaskLogs ChatTaskLogReader
|
||||
TaskActor ChatTaskActor
|
||||
TaskIdentity string
|
||||
PRReviewer ChatPRReviewer
|
||||
Bus TurnEventPublisher
|
||||
BotUser string
|
||||
Channel string
|
||||
@@ -314,7 +313,7 @@ func (r *TurnRunner) prepareTurn(ctx context.Context, sessionID string, in Inbou
|
||||
TaskTools(r.TaskLister, r.Tasks, r.TaskLogs, r.TaskActor, r.TaskIdentity),
|
||||
SessionTools(r.Sessions, r.Router.SessionTracker(), r.Router.sessionPlatform(in), in.Message)...,
|
||||
)
|
||||
extraTools = append(extraTools, ReviewTools(r.PRReviewer, r.TaskIdentity)...)
|
||||
extraTools = append(extraTools, ReviewTools(r.Tasks, r.TaskIdentity)...)
|
||||
// The dashboard tools (page_index, dashboard_navigate) belong to the web
|
||||
// UI only: a non-web channel has no dashboard to point at.
|
||||
extraTools = append(extraTools, PageIndexTools(r.Channel)...)
|
||||
|
||||
Reference in new issue
Block a user