Replaces stdin/stdout subprocess transport with NATS JetStream request-reply pattern. The Runner interface and workflow orchestration are unchanged. New NATSRunner (implements Runner): - Publishes AgentRequestMessage to JetStream with X-Archie-Reply header - Blocks on core NATS inbox subscription for response - Same Run(ctx, workspace, req) (Result, error) contract Rewritten archie-agent (cmd/archie-agent): - Long-running NATS-connected process (was 28-line stdin/stdout) - Pull-consumes from ARCHIE_TASKS with archie.agent.> filter - Builds InProcessRunner, executes agent loop, replies to inbox - Graceful shutdown via signal handling Extended internal/nats: - SubjectAgentWildcard, SubjectForAgentRequest, ReplyHeader - Conn() accessor, NewReplyInbox(), PublishMsg() for JetStream - Stream subjects extended to ["archie.task.>", "archie.agent.>"] Config: "nats" added as valid agent.mode alongside inprocess/subprocess Tests: NATSRunner round-trip with embedded NATS server + handler
108 lines
2.9 KiB
Go
108 lines
2.9 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.
|
|
type AgentRequestMessage struct {
|
|
TaskID int64 `json:"task_id"`
|
|
Attempt int `json:"attempt"`
|
|
Stage string `json:"stage"`
|
|
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.
|
|
type AgentResponseEnvelope struct {
|
|
Version int `json:"version"`
|
|
Result Result `json:"result"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
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,
|
|
Workspace: workspace,
|
|
Request: req,
|
|
Providers: r.Providers,
|
|
}
|
|
data, err := json.Marshal(msg)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
|
|
subject := arnats.SubjectForAgentRequest(req.TaskID, req.Stage)
|
|
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
|
|
}
|