fix(archied): share one PR reviewer across channels; harden forge and worktree

Addresses PR #783 review findings:

- Memoize the operator-triggered PR reviewer on boot so the Web UI and
  Telegram share one in-flight set; a concurrent review of the same PR from
  two channels now deduplicates instead of running twice.
- Guard Gitea GetPullRequest against a nil head/base (deleted branch returns
  null from Gitea), returning an error instead of panicking.
- Create the worktree root in CheckoutPR before MkdirTemp so a first review on
  a fresh work_dir does not fail with os.ErrNotExist.
This commit is contained in:
sam committed 2026-09-10 01:09:31 +10:00
1 parent 64ce3925a1
commit 7c1d323b30
6 files changed
+70 -6

No files matched your search

+9 -5
View File
@@ -148,11 +148,15 @@ type boot struct {
// unrelated call.
embeddings domainembedding.Client
toolReg *tools.Registry
chatModels gateway.ModelManager
personas *gateway.PersonaRegistry
chatTasks gateway.TaskCreator
chatController *gateway.StoreTaskController
toolReg *tools.Registry
chatModels gateway.ModelManager
personas *gateway.PersonaRegistry
chatTasks gateway.TaskCreator
chatController *gateway.StoreTaskController
// chatPRReviewer is the operator-triggered PR review capability, built
// once and shared by every channel so a review in flight on one channel
// is deduplicated against the same request on another (single-flight).
chatPRReviewer gateway.ChatPRReviewer
defaultChatIdentity string
updateService *releaseupdate.Service
+9 -1
View File
@@ -231,12 +231,19 @@ func buildReviewAllowlist(cfg config.Config) map[string]map[string]bool {
// the forge cannot read PRs (the noop forge, or a forge with no PR read
// support). A review tool that can never run must not be advertised, matching
// the TaskTools nil-backend rule.
//
// The instance is built once and memoized on the boot struct so every channel
// shares one in-flight set: a review of the same PR requested concurrently
// from the Web UI and Telegram deduplicates instead of running twice.
func (b *boot) prReviewer() gateway.ChatPRReviewer {
if b.chatPRReviewer != nil {
return b.chatPRReviewer
}
forgeReader, _ := b.forgeClient.(forge.PullRequestReader)
if forgeReader == nil {
return nil
}
return &prReviewer{
b.chatPRReviewer = &prReviewer{
forge: forgeReader,
trees: b.trees,
models: b.cfg.Models,
@@ -245,4 +252,5 @@ func (b *boot) prReviewer() gateway.ChatPRReviewer {
allowed: buildReviewAllowlist(b.cfg),
log: b.log,
}
return b.chatPRReviewer
}
+27
View File
@@ -1,6 +1,8 @@
package archied
import (
"context"
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/config"
@@ -161,3 +163,28 @@ func TestBuildReviewAllowlist(t *testing.T) {
t.Errorf("allowlist contains unknown identity nobody: %+v", allowed)
}
}
// reviewCapableForge satisfies both forge.Forge (via embedding) and
// forge.PullRequestReader, so boot.prReviewer can be exercised in isolation.
type reviewCapableForge struct {
forge.Forge
}
func (reviewCapableForge) GetPullRequest(context.Context, string, string, int) (forge.PullRequest, error) {
return forge.PullRequest{}, nil
}
// TestPrReviewerIsMemoized pins the cross-channel single-flight fix: every
// gateway must share one prReviewer so a review of the same PR requested from
// two channels at once deduplicates instead of running twice.
func TestPrReviewerIsMemoized(t *testing.T) {
b := &boot{forgeClient: reviewCapableForge{Forge: forge.NewNoop(slog.New(slog.DiscardHandler))}}
first := b.prReviewer()
if first == nil {
t.Fatal("prReviewer() = nil, want a reviewer")
}
if second := b.prReviewer(); first != second {
t.Fatal("prReviewer() returned distinct instances; channels would not share the in-flight set")
}
}
+5
View File
@@ -169,6 +169,11 @@ func (c *GiteaClient) GetPullRequest(ctx context.Context, owner, repo string, nu
if err != nil {
return PullRequest{}, fmt.Errorf("get pull request %s/%s#%d: %w", owner, repo, number, err)
}
// A merged/closed PR whose head branch was deleted returns nil head/base
// from Gitea; dereferencing it would panic.
if pr.Head == nil || pr.Base == nil {
return PullRequest{}, fmt.Errorf("pull request %s/%s#%d has a missing head or base ref", owner, repo, number)
}
state := string(pr.State)
if pr.HasMerged {
state = "merged"
+15
View File
@@ -177,6 +177,21 @@ func TestGiteaGetPullRequest(t *testing.T) {
}
}
func TestGiteaGetPullRequestMissingHeadDoesNotPanic(t *testing.T) {
c, mux := newTestGiteaClient(t)
// Gitea returns head: null when a PR's head branch was deleted.
mux.HandleFunc("GET /api/v1/repos/o/r/pulls/7", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, map[string]any{
"number": 7, "title": "Add widget", "state": "closed", "merged": true,
"head": nil, "base": nil,
})
})
if _, err := c.GetPullRequest(t.Context(), "o", "r", 7); err == nil {
t.Fatal("GetPullRequest error = nil, want a missing-head/base error rather than a panic")
}
}
func TestGiteaCloseIssueWithComment(t *testing.T) {
c, mux := newTestGiteaClient(t)
var commented, closed bool
+5
View File
@@ -703,6 +703,11 @@ func remoteBase(base string) string {
// repository (archie's own PRs always are); a cross-repo or deleted head is
// refused rather than half-reviewed. The returned cleanup removes the clone.
func (m *Manager) CheckoutPR(ctx context.Context, owner, repo, headRef, baseRef string) (dir string, cleanup func(), err error) {
if m.WorkDir != "" {
if err := os.MkdirAll(m.WorkDir, 0o755); err != nil {
return "", nil, fmt.Errorf("create worktree root directory: %w", err)
}
}
dir, err = os.MkdirTemp(m.WorkDir, "pr-review-*")
if err != nil {
return "", nil, fmt.Errorf("create PR review directory: %w", err)