Files
archie/internal/forgerpc/forgerpc.go
T
sam 170fe0250b fix: close the duplicate-work loop and the dead paths around it
Applies the four-reviewer pass over the forge/dashboard-actions work.
The through-line is that several capabilities were wired but inert, and
one of them could cost real work.

DUPLICATE WORK. "Closes #N" was dropped from PR bodies in favour of
LinkBranch, which is sidebar linkage on Gitea and a no-op stub on
GitHub -- neither closes anything, and nothing else did either. So a
merged task left its issue open, labelled and assigned; the next poll
re-enqueued it, and once the dashboard's Clear removed the task row that
became a second implementation and a second PR for work already merged.
reconcilePRs now closes the issue on merge, and a rejected task closes
its issue too. A PR closed without merging deliberately leaves the issue
open: that usually means "try again".

MAX RETRIES. Configured, defaulted and reported by /api/config, and
enforced nowhere -- markDead was unreachable and EffectiveMaxRetries had
no callers, so the dashboard's retry incremented retry_count forever.
The retry action now retires a task to dead at the cap.

DEAD ON ARRIVAL. Tasks() never selected plan or source, so the
"Decision required" panel could not render and chat tasks -- whose issue
number is synthetic -- linked to forge issues that do not exist.

COSMETIC FAILURE, FATAL RESULT. OpenPR failed the whole stage when
LinkBranch errored, so the pull request was never opened and the task
parked; a retry re-links the same branch, which Gitea answers with a
conflict, so every retry parked again. It is best-effort now.

INVISIBLE ACTIONS. Approve/retry/reject wrote store rows and nothing
else, so the timeline and activity stream never showed that a human had
intervened. They emit events now.

Also: store write failures answer 500 rather than 409; the dashboard
surfaces the server's actual reason instead of "409 Conflict"; action
buttons stop propagation (acting on a task no longer also expands it),
disable in flight, keep per-task errors instead of one shared string,
and confirm before rejecting.

Removed: the maybeRetryParked shim and markDead with their now-vacuous
tests, WaitingTasks (no consumers; its tests were rewritten to assert
the same routing through TaskByID), write-only Daemon.Runtime, and
dir/dirs response bloat from /api/skills. skillSource checked workDir
before sharedDir, mislabelling every shared skill when the two are equal.
DefaultRoots now dedupes roots differing only by a trailing separator.

The air build rule claiming to keep ui/dist current never ran -- it
required one file to satisfy include_dir and include_file together, and
ui/ is excluded from the walk. Deleted with the comment that promised it.
task test:ui joins task check and CI; it ran nowhere before, so the
dashboard suite could rot silently.

Docs corrected where they described behaviour that no longer exists:
parks no longer comment on the issue, state labels no longer drive
retries, and forgerpc's doc claimed four proxied methods while listing
three of five.
2026-08-05 03:54:25 +10:00

221 lines
7.6 KiB
Go

// Package forgerpc lets archie-agent call the forge methods workflow stages
// invoke mid-run -- CloseIssue, CreatePR and LinkBranch, the workflow.Forger
// set -- over core NATS request/reply, instead of the agent container holding
// a live forge API token. archied remains the sole holder of forge credentials
// and the sole caller of forge.Forge.
//
// The rest of forge.Forge (issue polling, invitations, reactions, PR-state
// reconciliation) is used exclusively by the daemon's own poll/reconcile
// loops, never from inside a workflow stage, so there is nothing for
// archie-agent to call.
//
// The server also still answers Comment and SetStateLabel. No current agent
// calls them; the handlers exist so an older archie-agent image keeps working
// against a newer daemon. The compatibility is one-directional: a NEW agent
// against an OLD daemon has no LinkBranch handler to reach, and the request
// times out, so an image skew that way parks tasks. Note it in release notes
// rather than assuming either side can lag.
package forgerpc
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"time"
"github.com/nats-io/nats.go"
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/natsrpc"
)
const (
SubjectComment = "archie.forge.comment"
SubjectCloseIssue = "archie.forge.close_issue"
SubjectCreatePR = "archie.forge.create_pr"
SubjectLinkBranch = "archie.forge.link_branch"
SubjectSetStateLabel = "archie.forge.set_state_label"
)
type CommentRequest struct {
Owner, Repo string
Number int
Body string
}
type CommentResponse struct {
ID int64 `json:"id"`
natsrpc.Envelope
}
type CloseIssueRequest struct {
Owner, Repo string
Number int
Comment string
}
type CreatePRRequest struct {
Owner, Repo, Title, Head, Base, Body string
}
type CreatePRResponse struct {
Number int `json:"number"`
natsrpc.Envelope
}
type LinkBranchRequest struct {
Owner, Repo string
IssueNumber int
Branch string
}
type SetStateLabelRequest struct {
Owner, Repo string
Number int
Label string
KnownLabels []string
}
// Response is a bare success/error envelope for calls with no return value.
type Response struct {
natsrpc.Envelope
}
// Server proxies forgerpc requests to a real forge.Forge implementation.
type Server struct {
Forge forge.Forge
Log *slog.Logger
}
// Register subscribes all handlers on nc. The returned func
// unsubscribes all of them.
func (s *Server) Register(nc *nats.Conn) (unsubscribe func(), err error) {
return natsrpc.RegisterAll(nc, []natsrpc.Registration{
{Subject: SubjectComment, Handler: s.handleComment},
{Subject: SubjectCloseIssue, Handler: s.handleCloseIssue},
{Subject: SubjectCreatePR, Handler: s.handleCreatePR},
{Subject: SubjectLinkBranch, Handler: s.handleLinkBranch},
{Subject: SubjectSetStateLabel, Handler: s.handleSetStateLabel},
})
}
func (s *Server) handleComment(msg *nats.Msg) {
var req CommentRequest
if err := json.Unmarshal(msg.Data, &req); err != nil {
s.respond(msg, CommentResponse{Envelope: natsrpc.NewEnvelope(fmt.Errorf("decode comment request: %w", err))})
return
}
id, err := s.Forge.Comment(context.Background(), req.Owner, req.Repo, req.Number, req.Body)
s.respond(msg, CommentResponse{ID: id, Envelope: natsrpc.NewEnvelope(err)})
}
func (s *Server) handleCloseIssue(msg *nats.Msg) {
var req CloseIssueRequest
if err := json.Unmarshal(msg.Data, &req); err != nil {
s.respond(msg, Response{Envelope: natsrpc.NewEnvelope(fmt.Errorf("decode close_issue request: %w", err))})
return
}
err := s.Forge.CloseIssue(context.Background(), req.Owner, req.Repo, req.Number, req.Comment)
s.respond(msg, Response{Envelope: natsrpc.NewEnvelope(err)})
}
func (s *Server) handleCreatePR(msg *nats.Msg) {
var req CreatePRRequest
if err := json.Unmarshal(msg.Data, &req); err != nil {
s.respond(msg, CreatePRResponse{Envelope: natsrpc.NewEnvelope(fmt.Errorf("decode create_pr request: %w", err))})
return
}
num, err := s.Forge.CreatePR(context.Background(), req.Owner, req.Repo, req.Title, req.Head, req.Base, req.Body)
s.respond(msg, CreatePRResponse{Number: num, Envelope: natsrpc.NewEnvelope(err)})
}
func (s *Server) handleLinkBranch(msg *nats.Msg) {
var req LinkBranchRequest
if err := json.Unmarshal(msg.Data, &req); err != nil {
s.respond(msg, Response{Envelope: natsrpc.NewEnvelope(fmt.Errorf("decode link_branch request: %w", err))})
return
}
err := s.Forge.LinkBranch(context.Background(), req.Owner, req.Repo, req.IssueNumber, req.Branch)
s.respond(msg, Response{Envelope: natsrpc.NewEnvelope(err)})
}
func (s *Server) handleSetStateLabel(msg *nats.Msg) {
var req SetStateLabelRequest
if err := json.Unmarshal(msg.Data, &req); err != nil {
s.respond(msg, Response{Envelope: natsrpc.NewEnvelope(fmt.Errorf("decode set_state_label request: %w", err))})
return
}
s.Forge.SetStateLabel(context.Background(), req.Owner, req.Repo, req.Number, req.Label, req.KnownLabels)
s.respond(msg, Response{})
}
func (s *Server) respond(msg *nats.Msg, v any) {
natsrpc.Respond(msg, s.Log, "forgerpc", v)
}
// Client calls the forgerpc Server from archie-agent's process. It
// implements the four proxied forge.Forge methods with matching
// signatures so it can be used as a drop-in for workflow stages.
type Client struct {
Conn *nats.Conn
// Timeout bounds each call when ctx has no deadline of its own.
Timeout time.Duration
}
func (c *Client) rpc() *natsrpc.Client { return &natsrpc.Client{Conn: c.Conn, Timeout: c.Timeout} }
// Comment and SetStateLabel are not in workflow.Forger, so no current agent
// calls them. They stay as the client half of handlers kept for image skew
// (see the package doc) -- and as the only way to exercise those handlers
// end to end, which is why removing them would leave the compatibility path
// untested rather than merely unused.
func (c *Client) Comment(ctx context.Context, owner, repo string, number int, body string) (int64, error) {
req := CommentRequest{Owner: owner, Repo: repo, Number: number, Body: body}
resp, err := natsrpc.Call[CommentResponse](ctx, c.rpc(), SubjectComment, req)
if err != nil {
return 0, err
}
if err := resp.Err(); err != nil {
return 0, err
}
return resp.ID, nil
}
func (c *Client) CloseIssue(ctx context.Context, owner, repo string, number int, comment string) error {
req := CloseIssueRequest{Owner: owner, Repo: repo, Number: number, Comment: comment}
resp, err := natsrpc.Call[Response](ctx, c.rpc(), SubjectCloseIssue, req)
if err != nil {
return err
}
return resp.Err()
}
func (c *Client) CreatePR(ctx context.Context, owner, repo, title, head, base, body string) (int, error) {
req := CreatePRRequest{Owner: owner, Repo: repo, Title: title, Head: head, Base: base, Body: body}
resp, err := natsrpc.Call[CreatePRResponse](ctx, c.rpc(), SubjectCreatePR, req)
if err != nil {
return 0, err
}
if err := resp.Err(); err != nil {
return 0, err
}
return resp.Number, nil
}
func (c *Client) LinkBranch(ctx context.Context, owner, repo string, issueNumber int, branch string) error {
req := LinkBranchRequest{Owner: owner, Repo: repo, IssueNumber: issueNumber, Branch: branch}
resp, err := natsrpc.Call[Response](ctx, c.rpc(), SubjectLinkBranch, req)
if err != nil {
return err
}
return resp.Err()
}
// SetStateLabel remains available for RPC compatibility with older workers;
// the current workflow deliberately never calls it.
func (c *Client) SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) {
req := SetStateLabelRequest{Owner: owner, Repo: repo, Number: number, Label: label, KnownLabels: knownLabels}
_, _ = natsrpc.Call[Response](ctx, c.rpc(), SubjectSetStateLabel, req)
}