Files
archie/internal/agentexec/nats.go
T
sam e8ffa20b33 test: rewrite regression tests as behavioral (not structural)
Replaces field-existence checks with runtime behavior verification:

Gap 1 (per-task agent): FAILING
  TestHandleMessageReadsWorkflowField verifies HandleMessage produces
  different results when Workflow is set vs. unset. Currently identical
  — HandleMessage ignores the Workflow field. Must run full multi-stage
  workflow per PRD section 1.

Gap 3 (response/system split): PASSING
  TestHandleMessageRoutesSystemMessagesSeparately verifies Channel
  echoes from request to response. HandleMessage now reads input Channel
  and sets it on the response envelope. Subject functions verified
  distinct.

Gap 5 (daemon review): PASSING
  TestReviewResultBlocksOnResultWhenRejected verifies ReviewResult
  hook gates OnResult. Error blocks — OnResult never called. Nil
  allows — OnResult called normally.

Agent processing extracted into agentexec.HandleMessage for testability.
cmd/archie-agent delegates to it.
2026-07-22 03:13:57 +10:00

118 lines
3.6 KiB
Go

package agentexec
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"time"
natsio "github.com/nats-io/nats.go"
arnats "github.com/samcharles93/archie-core/internal/nats"
)
// AgentRequestMessage is the NATS payload for an agent stage execution request.
// The Workflow field tells the agent which workflow to run (implement, tdd,
// feasibility, bootstrap) — the agent may execute multiple stages for one task.
// Channel selects the response routing: "response" (default, daemon-reviewed
// then forwarded to humans) or "system" (internal, never forwarded). PRD §2.
type AgentRequestMessage struct {
TaskID int64 `json:"task_id"`
Attempt int `json:"attempt"`
Stage string `json:"stage"`
Workflow string `json:"workflow,omitempty"`
Channel string `json:"channel,omitempty"` // "response" (default) or "system"
Workspace string `json:"workspace"`
Request Request `json:"request"`
Providers map[string]Provider `json:"providers,omitempty"`
}
// AgentResponseEnvelope is the response payload published to the reply inbox.
// Channel distinguishes agent output for humans ("response") from internal
// messages the daemon never forwards ("system"). PRD §2.
type AgentResponseEnvelope struct {
Version int `json:"version"`
Result Result `json:"result"`
Error string `json:"error,omitempty"`
Channel string `json:"channel,omitempty"` // "response" (default) or "system"
}
const defaultAgentTimeout = 30 * time.Minute
// NATSRunner sends agent stage execution requests over NATS JetStream and
// waits for replies on core NATS inboxes. It implements Runner.
type NATSRunner struct {
Nats *arnats.Client
Providers map[string]Provider
Log *slog.Logger
}
// Run publishes a stage execution request to JetStream with an X-Archie-Reply
// header, blocks on the sync inbox subscription, and returns the result.
func (r *NATSRunner) Run(ctx context.Context, workspace string, req Request) (Result, error) {
if err := req.Validate(); err != nil {
return Result{}, err
}
// 1. Create reply inbox with auto-unsubscribe after one message.
replySub, err := r.Nats.NewReplyInbox()
if err != nil {
return Result{}, fmt.Errorf("nats reply inbox: %w", err)
}
defer replySub.Unsubscribe()
// 2. Compute timeout from budget.
timeout := defaultAgentTimeout
if req.Budget.WallClock > 0 {
timeout = req.Budget.WallClock
}
// 3. Publish request with reply header.
msg := AgentRequestMessage{
TaskID: req.TaskID,
Attempt: req.Attempt,
Stage: req.Stage,
Workflow: req.Workflow,
Workspace: workspace,
Request: req,
Providers: r.Providers,
}
data, err := json.Marshal(msg)
if err != nil {
return Result{}, err
}
subject := arnats.SubjectForAgentRequest(req.TaskID)
headers := natsio.Header{}
headers.Set(arnats.ReplyHeader, replySub.Subject)
if _, err := r.Nats.PublishMsg(ctx, &natsio.Msg{
Subject: subject,
Data: data,
Header: headers,
}); err != nil {
return Result{}, fmt.Errorf("publish agent request: %w", err)
}
// 4. Wait for reply (blocking, matching SubprocessRunner.Run behaviour).
reply, err := replySub.NextMsg(timeout)
if err != nil {
return Result{}, fmt.Errorf("agent reply: %w", err)
}
// 5. Decode response.
var envelope AgentResponseEnvelope
if err := json.Unmarshal(reply.Data, &envelope); err != nil {
return Result{}, fmt.Errorf("decode agent response: %w", err)
}
if envelope.Error != "" {
return envelope.Result, errors.New(envelope.Error)
}
if err := envelope.Result.ValidateFor(req); err != nil {
return envelope.Result, err
}
return envelope.Result, nil
}