refactor(agentexec): remove legacy execution paths
Autonomous workflows now have exactly one production boundary: a task-scoped archie-agent receives a full-task core-NATS request and runs the worker-local stage loop. The legacy [agent] section remains decode-only for migration; it no longer selects runtime behavior.
This commit is contained in:
1 parent
cc6b093820
commit
5d00ec7b0c
65 files changed
+454
-2678
No files matched your search
@@ -0,0 +1,58 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The task-scoped archie-agent process owns the workflow and its local model
|
||||
// loop. agentexec therefore contains the worker-local stage runner, never a
|
||||
// second process boundary or a stage transport that archied can select.
|
||||
func TestPackageContainsNoLegacyExecutionModes(t *testing.T) {
|
||||
forbidden := map[string]bool{
|
||||
"InProcessRunner": true,
|
||||
"NewInProcessRunner": true,
|
||||
"SubprocessRunner": true,
|
||||
"NATSRunner": true,
|
||||
"ServeOne": true,
|
||||
"Invocation": true,
|
||||
"AgentRequestMessage": true,
|
||||
"AgentResponseEnvelope": true,
|
||||
"StageMessage": true,
|
||||
"StageConsumer": true,
|
||||
}
|
||||
files, err := filepath.Glob("*.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fileset := token.NewFileSet()
|
||||
for _, path := range files {
|
||||
if strings.HasSuffix(path, "_test.go") {
|
||||
continue
|
||||
}
|
||||
file, parseErr := parser.ParseFile(fileset, path, nil, 0)
|
||||
if parseErr != nil {
|
||||
t.Errorf("parse %s: %v", path, parseErr)
|
||||
continue
|
||||
}
|
||||
for _, declaration := range file.Decls {
|
||||
switch declaration := declaration.(type) {
|
||||
case *ast.GenDecl:
|
||||
for _, spec := range declaration.Specs {
|
||||
typeSpec, ok := spec.(*ast.TypeSpec)
|
||||
if ok && forbidden[typeSpec.Name.Name] {
|
||||
t.Errorf("%s declares legacy execution type %s", fileset.Position(typeSpec.Pos()), typeSpec.Name.Name)
|
||||
}
|
||||
}
|
||||
case *ast.FuncDecl:
|
||||
if declaration.Recv == nil && forbidden[declaration.Name.Name] {
|
||||
t.Errorf("%s declares legacy execution constructor %s", fileset.Position(declaration.Pos()), declaration.Name.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,18 +21,16 @@ import (
|
||||
|
||||
// Runner executes one autonomous stage against an already prepared workspace.
|
||||
type Runner interface {
|
||||
// report, when non-nil, is notified once per completed tool call. Only
|
||||
// a runner executing in the same process as its tools (InProcessRunner)
|
||||
// can populate it; others accept and ignore it.
|
||||
// report, when non-nil, is notified once per completed tool call.
|
||||
Run(ctx context.Context, workspace string, req Request, report ToolCallReporter) (Result, error)
|
||||
}
|
||||
|
||||
type loopFunc func(context.Context, agentloop.Config) (agentloop.Result, error)
|
||||
|
||||
// InProcessRunner preserves the current execution behavior behind the v1
|
||||
// agent protocol. It is replaced by a subprocess or container runner without
|
||||
// changing workflow orchestration.
|
||||
type InProcessRunner struct {
|
||||
// LoopRunner executes one workflow stage through ai-sdk's agent loop. It is
|
||||
// worker-local: archie-agent owns the complete workflow and uses this runner
|
||||
// for each stage inside its task-scoped container.
|
||||
type LoopRunner struct {
|
||||
runtime *runtime.Runtime
|
||||
log *slog.Logger
|
||||
run loopFunc
|
||||
@@ -43,12 +41,12 @@ type InProcessRunner struct {
|
||||
Limits ToolLimits
|
||||
}
|
||||
|
||||
func NewInProcessRunner(
|
||||
func NewLoopRunner(
|
||||
rt *runtime.Runtime,
|
||||
log *slog.Logger,
|
||||
registries ...*tools.Registry,
|
||||
) *InProcessRunner {
|
||||
runner := &InProcessRunner{runtime: rt, log: log, run: agentloop.Run}
|
||||
) *LoopRunner {
|
||||
runner := &LoopRunner{runtime: rt, log: log, run: agentloop.Run}
|
||||
if len(registries) > 0 {
|
||||
runner.tools = registries[0]
|
||||
}
|
||||
@@ -67,7 +65,7 @@ func NewInProcessRunner(
|
||||
// it is not configurable.
|
||||
const workspaceToolset = "workspace"
|
||||
|
||||
func (r *InProcessRunner) Run(ctx context.Context, workspace string, req Request, report ToolCallReporter) (Result, error) {
|
||||
func (r *LoopRunner) Run(ctx context.Context, workspace string, req Request, report ToolCallReporter) (Result, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
@@ -194,7 +192,7 @@ func pluginToolSet(req Request, workspace string, occupied ...core.ToolSet) (cor
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func (r *InProcessRunner) logger(req Request) *slog.Logger {
|
||||
func (r *LoopRunner) logger(req Request) *slog.Logger {
|
||||
if r.log == nil {
|
||||
return slog.New(slog.DiscardHandler)
|
||||
}
|
||||
@@ -57,8 +57,8 @@ func TestPluginToolSetRejectsAgentLoopCollisions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInProcessRunnerMapsRequestAndCapturesOutput(t *testing.T) {
|
||||
runner := &InProcessRunner{
|
||||
func TestLoopRunnerMapsRequestAndCapturesOutput(t *testing.T) {
|
||||
runner := &LoopRunner{
|
||||
runtime: runtime.NewRuntime(runtime.Config{}),
|
||||
run: func(ctx context.Context, cfg agentloop.Config) (agentloop.Result, error) {
|
||||
if cfg.WorkDir != "/workspace" || cfg.ModelRef != "provider/model" || cfg.Mission != "mission" {
|
||||
@@ -103,7 +103,7 @@ func TestInProcessRunnerMapsRequestAndCapturesOutput(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInProcessRunnerIncludesAvailableCentralTools(t *testing.T) {
|
||||
func TestLoopRunnerIncludesAvailableCentralTools(t *testing.T) {
|
||||
registry := tools.NewRegistry()
|
||||
if err := registry.Register(tools.ToolEntry{
|
||||
Name: "memory_search",
|
||||
@@ -113,7 +113,7 @@ func TestInProcessRunnerIncludesAvailableCentralTools(t *testing.T) {
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := &InProcessRunner{
|
||||
runner := &LoopRunner{
|
||||
runtime: runtime.NewRuntime(runtime.Config{}),
|
||||
tools: registry,
|
||||
run: func(ctx context.Context, cfg agentloop.Config) (agentloop.Result, error) {
|
||||
@@ -144,7 +144,7 @@ func TestInProcessRunnerIncludesAvailableCentralTools(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInProcessRunnerRejectsInvalidCentralToolSchemas(t *testing.T) {
|
||||
func TestLoopRunnerRejectsInvalidCentralToolSchemas(t *testing.T) {
|
||||
registry := tools.NewRegistry()
|
||||
if err := registry.Register(tools.ToolEntry{
|
||||
Name: "invalid",
|
||||
@@ -155,7 +155,7 @@ func TestInProcessRunnerRejectsInvalidCentralToolSchemas(t *testing.T) {
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := &InProcessRunner{
|
||||
runner := &LoopRunner{
|
||||
runtime: runtime.NewRuntime(runtime.Config{}),
|
||||
tools: registry,
|
||||
run: func(context.Context, agentloop.Config) (agentloop.Result, error) {
|
||||
@@ -280,10 +280,10 @@ func TestMergeToolSetsLaterWinsOnCollision(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInProcessRunnerPreservesCallerCancellation(t *testing.T) {
|
||||
func TestLoopRunnerPreservesCallerCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
runner := &InProcessRunner{
|
||||
runner := &LoopRunner{
|
||||
runtime: runtime.NewRuntime(runtime.Config{}),
|
||||
run: func(context.Context, agentloop.Config) (agentloop.Result, error) {
|
||||
return agentloop.Result{Status: agentloop.StatusParked, StopReason: agentloop.StopTimedOut}, nil
|
||||
@@ -301,8 +301,8 @@ func TestInProcessRunnerPreservesCallerCancellation(t *testing.T) {
|
||||
// tool handler discards the incoming context (the handler is declared as
|
||||
// func(_ context.Context, input string) and calls skillscript.Run without
|
||||
// forwarding ctx). Every other execution path in archie-core respects
|
||||
// context cancellation (SubprocessRunner uses exec.CommandContext,
|
||||
// InProcessRunner checks context.Cause), but a cancelled/timed-out context
|
||||
// context cancellation (LoopRunner checks context.Cause), but a
|
||||
// cancelled/timed-out context
|
||||
// has zero effect on run_go_script — a blocking script wedges the
|
||||
// goroutine permanently.
|
||||
func TestScriptToolHonorsContextCancellation(t *testing.T) {
|
||||
@@ -1,136 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
)
|
||||
|
||||
// AgentRequestMessage is the NATS payload for an agent stage execution request.
|
||||
// When Workflow is set and Stages is populated, the agent runs all stages as a
|
||||
// per-task batch rather than a single stage. PRD §1.
|
||||
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"`
|
||||
Stages []Request `json:"stages,omitempty"` // batch: all agent stages for this task
|
||||
Providers map[string]Provider `json:"providers,omitempty"`
|
||||
// MCPServers carries MCP server definitions so the agent can construct
|
||||
// transports, discover tools, and register them locally. Absent/empty
|
||||
// means no MCP servers (backward compatible).
|
||||
MCPServers []config.MCPServer `json:"mcp_servers,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"`
|
||||
Channel string `json:"channel,omitempty"` // "response" (default) or "system"
|
||||
TaskCompleted bool `json:"task_completed,omitempty"` // true when all stages finished (per-task mode)
|
||||
}
|
||||
|
||||
const defaultAgentTimeout = 30 * time.Minute
|
||||
|
||||
// RequestBus is the messaging this runner needs: publish a request carrying a
|
||||
// reply address, and create the inbox that address points at.
|
||||
//
|
||||
// Declared here rather than taking eventbus.Bus because a domain defines the
|
||||
// smallest interface required to do its work -- this runner never fetches or
|
||||
// subscribes, and should not be able to.
|
||||
type RequestBus interface {
|
||||
PublishRequest(ctx context.Context, subject, replyTo string, payload []byte) error
|
||||
NewReplyInbox() (eventbus.ReplyInbox, error)
|
||||
}
|
||||
|
||||
// NATSRunner sends agent stage execution requests over the bus and waits for
|
||||
// replies on a one-shot inbox. It implements Runner.
|
||||
//
|
||||
// The name is historical: it holds no NATS type and works with any bus
|
||||
// satisfying RequestBus.
|
||||
type NATSRunner struct {
|
||||
Bus RequestBus
|
||||
Providers map[string]Provider
|
||||
MCPServers []config.MCPServer
|
||||
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.
|
||||
//
|
||||
// report is ignored: execution happens on whatever remote worker answers the
|
||||
// request, a different process with no way to call back here per tool call.
|
||||
func (r *NATSRunner) Run(ctx context.Context, workspace string, req Request, _ ToolCallReporter) (Result, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
|
||||
// 1. Create reply inbox with auto-unsubscribe after one message.
|
||||
replySub, err := r.Bus.NewReplyInbox()
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("nats reply inbox: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := replySub.Close(); err != nil {
|
||||
r.Log.Warn("reply inbox close failed", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// 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,
|
||||
MCPServers: r.MCPServers,
|
||||
}
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
|
||||
subject := SubjectForRequest(req.TaskID)
|
||||
if err := r.Bus.PublishRequest(ctx, subject, replySub.Subject(), data); err != nil {
|
||||
return Result{}, fmt.Errorf("publish agent request: %w", err)
|
||||
}
|
||||
|
||||
// 4. Wait for reply (blocking, matching SubprocessRunner.Run behaviour).
|
||||
replyCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
reply, err := replySub.Next(replyCtx)
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("agent reply: %w", err)
|
||||
}
|
||||
|
||||
// 5. Decode response.
|
||||
var envelope AgentResponseEnvelope
|
||||
if err := json.Unmarshal(reply, &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
|
||||
}
|
||||
@@ -1,285 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/nats-io/nats-server/v2/server"
|
||||
natssrv "github.com/nats-io/nats-server/v2/test"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
arnats "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
)
|
||||
|
||||
// ── helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
type mockRunner struct{}
|
||||
|
||||
func (m *mockRunner) Run(_ context.Context, _ string, req Request, _ ToolCallReporter) (Result, error) {
|
||||
return Result{
|
||||
Version: ProtocolVersion,
|
||||
TaskID: req.TaskID,
|
||||
Attempt: req.Attempt,
|
||||
Stage: req.Stage,
|
||||
Status: StatusPassed,
|
||||
Summary: "mock:" + req.Mission,
|
||||
TokensUsed: 10,
|
||||
Iterations: 1,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var mockFactory = func(providers map[string]Provider, log *slog.Logger) Runner {
|
||||
return &mockRunner{}
|
||||
}
|
||||
|
||||
// ── regression: Gap 1 -- per-task agent ──────────────────────────────
|
||||
|
||||
func TestHandleMessageReadsWorkflowField(t *testing.T) {
|
||||
// Gap 1: archie-agent is per-stage, not per-task.
|
||||
// PRD section 1: when the daemon sends a task-level message with
|
||||
// Workflow="implement" and Stages populated, the agent must run all
|
||||
// stages as a batch. Single-stage mode (Request only) is the
|
||||
// backward-compatible fallback.
|
||||
|
||||
ws := t.TempDir()
|
||||
providers := map[string]Provider{"test": {Class: "openai", APIKeyEnv: "FAKE"}}
|
||||
|
||||
// Single-stage mode: uses Request, no Workflow, no Stages.
|
||||
singleMsg := AgentRequestMessage{
|
||||
TaskID: 1,
|
||||
Attempt: 1,
|
||||
Stage: "plan",
|
||||
Workspace: ws,
|
||||
Request: Request{
|
||||
Version: ProtocolVersion, TaskID: 1, Attempt: 1,
|
||||
Stage: "plan", Model: "test/model", Mission: "single",
|
||||
Budget: Budget{MaxSteps: 1},
|
||||
},
|
||||
Providers: providers,
|
||||
}
|
||||
mockFactory := func(providers map[string]Provider, log *slog.Logger) Runner {
|
||||
return &mockRunner{}
|
||||
}
|
||||
singleResp, err := HandleMessage(context.Background(), singleMsg, slog.New(slog.DiscardHandler), mockFactory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if singleResp.TaskCompleted {
|
||||
t.Error("single-stage mode set TaskCompleted=true -- should be false")
|
||||
}
|
||||
|
||||
// Per-task mode: Workflow set, Stages populated with two stages.
|
||||
multiMsg := AgentRequestMessage{
|
||||
TaskID: 2,
|
||||
Attempt: 1,
|
||||
Stage: "plan",
|
||||
Workflow: "implement",
|
||||
Workspace: ws,
|
||||
Request: Request{
|
||||
Version: ProtocolVersion, TaskID: 2, Attempt: 1,
|
||||
Stage: "plan", Model: "test/model", Mission: "multi-1",
|
||||
Budget: Budget{MaxSteps: 1},
|
||||
},
|
||||
Stages: []Request{
|
||||
{
|
||||
Version: ProtocolVersion, TaskID: 2, Attempt: 1,
|
||||
Stage: "plan", Model: "test/model", Mission: "multi-plan",
|
||||
Budget: Budget{MaxSteps: 1},
|
||||
},
|
||||
{
|
||||
Version: ProtocolVersion, TaskID: 2, Attempt: 1,
|
||||
Stage: "build", Model: "test/model", Mission: "multi-build",
|
||||
Budget: Budget{MaxSteps: 1},
|
||||
},
|
||||
},
|
||||
Providers: providers,
|
||||
}
|
||||
multiResp, err := HandleMessage(context.Background(), multiMsg, slog.New(slog.DiscardHandler), mockFactory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !multiResp.TaskCompleted {
|
||||
t.Error("Gap 1: per-task mode did not set TaskCompleted=true. " +
|
||||
"The agent must signal task completion when all stages in a " +
|
||||
"workflow batch have been processed.")
|
||||
}
|
||||
// Batch results must aggregate from both stages.
|
||||
if multiResp.Result.TokensUsed == singleResp.Result.TokensUsed &&
|
||||
multiResp.Result.Iterations == singleResp.Result.Iterations {
|
||||
t.Error("Gap 1: per-task mode produced same token/iteration counts " +
|
||||
"as single-stage mode. Multi-stage batch must accumulate " +
|
||||
"results from all stages. See PRD section 1.")
|
||||
}
|
||||
}
|
||||
|
||||
// ── regression: Gap 3 -- response/system channel split ───────────────
|
||||
|
||||
func TestHandleMessageRoutesSystemMessagesSeparately(t *testing.T) {
|
||||
// Gap 3: no response/system channel split.
|
||||
// PRD section 2: system messages go to archie.agent.<id>.system and
|
||||
// are never forwarded to humans. Response messages go to
|
||||
// archie.agent.<id>.response for daemon review before human delivery.
|
||||
// Currently HandleMessage always returns Channel="response" -- there
|
||||
// is no code path that produces a system-channel response.
|
||||
|
||||
msg := AgentRequestMessage{
|
||||
TaskID: 1,
|
||||
Attempt: 1,
|
||||
Stage: "plan",
|
||||
Workspace: t.TempDir(),
|
||||
Request: Request{
|
||||
Version: ProtocolVersion,
|
||||
TaskID: 1,
|
||||
Attempt: 1,
|
||||
Stage: "plan",
|
||||
Model: "test/model",
|
||||
Mission: "test",
|
||||
Budget: Budget{MaxSteps: 1},
|
||||
},
|
||||
Providers: map[string]Provider{"test": {Class: "openai", APIKeyEnv: "FAKE"}},
|
||||
}
|
||||
|
||||
// Normal stage request -- should route to response channel.
|
||||
resp, err := HandleMessage(context.Background(), msg, slog.New(slog.DiscardHandler), mockFactory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.Channel != "response" {
|
||||
t.Errorf("normal stage request: Channel = %q, want \"response\"", resp.Channel)
|
||||
}
|
||||
|
||||
// System message (health check, log dump request) -- should route to
|
||||
// system channel. Currently HandleMessage always returns "response".
|
||||
// The daemon sets Channel on the request to indicate message type.
|
||||
msg.Channel = "system"
|
||||
sysResp, err := HandleMessage(context.Background(), msg, slog.New(slog.DiscardHandler), mockFactory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sysResp.Channel != "system" {
|
||||
t.Error("Gap 3: HandleMessage ignores the input Channel field. " +
|
||||
"When the daemon sends a system message (Channel=\"system\"), " +
|
||||
"the response must be routed to archie.agent.<id>.system, " +
|
||||
"not to the reply inbox. Currently all responses go to the " +
|
||||
"same inbox regardless of channel. See PRD section 2.")
|
||||
}
|
||||
|
||||
// Verify the subject functions exist and are distinct.
|
||||
respSubj := SubjectForResponse(42)
|
||||
sysSubj := SubjectForSystem(42)
|
||||
if respSubj == sysSubj || respSubj == "" || sysSubj == "" {
|
||||
t.Error("Gap 3: subject functions broken -- verify SubjectForAgentResponse " +
|
||||
"and SubjectForAgentSystem return distinct, non-empty subjects.")
|
||||
}
|
||||
}
|
||||
|
||||
// ── existing tests ───────────────────────────────────────────────────
|
||||
|
||||
func startEmbeddedNATS(t *testing.T) *server.Server {
|
||||
t.Helper()
|
||||
srv := natssrv.RunRandClientPortServer()
|
||||
t.Cleanup(srv.Shutdown)
|
||||
if err := srv.EnableJetStream(&server.JetStreamConfig{StoreDir: t.TempDir()}); err != nil {
|
||||
t.Fatalf("enable jetstream: %v", err)
|
||||
}
|
||||
return srv
|
||||
}
|
||||
|
||||
func startHandler(t *testing.T, client *arnats.Client) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
// Drain the client's own consumer rather than subscribing a second one:
|
||||
// the stream uses work-queue retention, so two consumers with
|
||||
// overlapping filter subjects conflict. This mirrors archie-agent.
|
||||
go func() {
|
||||
for ctx.Err() == nil {
|
||||
msg, err := client.Fetch(ctx)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if err := replyToStage(ctx, client, msg); err != nil {
|
||||
t.Errorf("stage handler: %v", err)
|
||||
_ = msg.Nak()
|
||||
continue
|
||||
}
|
||||
_ = msg.Ack()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// replyToStage answers one stage request with a canned passing result.
|
||||
func replyToStage(ctx context.Context, client *arnats.Client, msg eventbus.Message) error {
|
||||
var req AgentRequestMessage
|
||||
if err := json.Unmarshal(msg.Data(), &req); err != nil {
|
||||
return err
|
||||
}
|
||||
replyTo, err := msg.ReplyAddress()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.Marshal(AgentResponseEnvelope{
|
||||
Version: req.Request.Version,
|
||||
Result: Result{
|
||||
Version: ProtocolVersion,
|
||||
TaskID: req.TaskID,
|
||||
Attempt: req.Attempt,
|
||||
Stage: req.Stage,
|
||||
Status: StatusPassed,
|
||||
Summary: "handler-reply",
|
||||
TokensUsed: 42,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.Respond(ctx, replyTo, data)
|
||||
}
|
||||
|
||||
func TestNATSRunnerRoundTrip(t *testing.T) {
|
||||
srv := startEmbeddedNATS(t)
|
||||
url := srv.ClientURL()
|
||||
|
||||
ctx := context.Background()
|
||||
client, err := arnats.Connect(ctx, arnats.Config{URL: url, Subjects: []string{workintake.SubjectTaskWildcard, SubjectAgentWildcard}, FilterSubject: SubjectAgentWildcard}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
startHandler(t, client)
|
||||
|
||||
runner := &NATSRunner{
|
||||
Bus: client,
|
||||
Providers: map[string]Provider{"openai": {Class: "openai", APIKeyEnv: "TEST_KEY"}},
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
|
||||
req := Request{
|
||||
Version: ProtocolVersion,
|
||||
TaskID: 42,
|
||||
Attempt: 1,
|
||||
Stage: "test",
|
||||
Model: "openai/gpt",
|
||||
Mission: "test mission",
|
||||
Budget: Budget{MaxSteps: 5},
|
||||
}
|
||||
|
||||
result, err := runner.Run(ctx, "/tmp/test-workspace", req, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Status != StatusPassed {
|
||||
t.Fatalf("expected passed, got %s", result.Status)
|
||||
}
|
||||
if result.Summary != "handler-reply" {
|
||||
t.Fatalf("expected 'handler-reply', got %q", result.Summary)
|
||||
}
|
||||
if result.TokensUsed != 42 {
|
||||
t.Fatalf("expected 42 tokens, got %d", result.TokensUsed)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
// Package agentexec defines the unprivileged agent execution boundary.
|
||||
// Workflow orchestration and external side effects remain daemon-owned.
|
||||
// Package agentexec defines worker-local workflow-stage execution. archie-agent
|
||||
// owns workflow orchestration; authority-bearing forge, store, and push effects
|
||||
// remain daemon-owned behind scoped RPC boundaries.
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -133,59 +132,6 @@ type Provider struct {
|
||||
BaseURL string `json:"base_url,omitempty"`
|
||||
}
|
||||
|
||||
func (p Provider) validate() error {
|
||||
if strings.TrimSpace(p.Class) == "" {
|
||||
return fmt.Errorf("provider class is required")
|
||||
}
|
||||
if p.BaseURL == "" {
|
||||
return nil
|
||||
}
|
||||
u, err := url.Parse(p.BaseURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse provider base_url: %w", err)
|
||||
}
|
||||
if u.User != nil || u.RawQuery != "" || u.Fragment != "" {
|
||||
return fmt.Errorf("provider base_url must not contain userinfo, query parameters, or a fragment")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Invocation is the single request accepted by archie-agent on stdin.
|
||||
type Invocation struct {
|
||||
Version int `json:"version"`
|
||||
Workspace string `json:"workspace"`
|
||||
Request Request `json:"request"`
|
||||
Providers map[string]Provider `json:"providers"`
|
||||
}
|
||||
|
||||
// Validate rejects invocations that cannot be executed safely.
|
||||
func (i Invocation) Validate() error {
|
||||
if i.Version != ProtocolVersion {
|
||||
return fmt.Errorf("agent invocation protocol version %d is unsupported (want %d)", i.Version, ProtocolVersion)
|
||||
}
|
||||
if i.Workspace == "" {
|
||||
return fmt.Errorf("agent invocation workspace is required")
|
||||
}
|
||||
if len(i.Providers) == 0 {
|
||||
return fmt.Errorf("agent invocation providers are required")
|
||||
}
|
||||
for name, provider := range i.Providers {
|
||||
if err := provider.validate(); err != nil {
|
||||
return fmt.Errorf("agent invocation provider %q: %w", name, err)
|
||||
}
|
||||
}
|
||||
return i.Request.Validate()
|
||||
}
|
||||
|
||||
// Response is the single envelope written by archie-agent on stdout.
|
||||
// Error carries execution errors after the worker successfully decoded the
|
||||
// invocation; process-level protocol failures use a non-zero exit status.
|
||||
type Response struct {
|
||||
Version int `json:"version"`
|
||||
Result Result `json:"result"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// ValidateFor rejects incompatible or misrouted results before the daemon
|
||||
// applies them to task state.
|
||||
func (r Result) ValidateFor(req Request) error {
|
||||
|
||||
@@ -2,8 +2,8 @@ package agentexec
|
||||
|
||||
import "github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
// NewRuntime builds the provider runtime shared by archied's in-process mode
|
||||
// and the archie-agent worker. Nil means no providers were configured.
|
||||
// NewRuntime builds a provider runtime for interactive chat or a worker-local
|
||||
// workflow stage. Nil means no providers were configured.
|
||||
func NewRuntime(providers map[string]Provider) *runtime.Runtime {
|
||||
if len(providers) == 0 {
|
||||
return nil
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// captureRunner records the Request it receives for test assertions.
|
||||
type captureRunner struct {
|
||||
callback func(Request)
|
||||
}
|
||||
|
||||
func (c *captureRunner) Run(_ context.Context, _ string, req Request, _ ToolCallReporter) (Result, error) {
|
||||
c.callback(req)
|
||||
return Result{
|
||||
Version: ProtocolVersion,
|
||||
TaskID: req.TaskID,
|
||||
Attempt: req.Attempt,
|
||||
Stage: req.Stage,
|
||||
Status: StatusPassed,
|
||||
Summary: "done",
|
||||
TokensUsed: 1,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ── regression: HandleMessage must not duplicate skill body ─────────
|
||||
|
||||
func TestHandleMessageDoesNotDuplicateSkillBody(t *testing.T) {
|
||||
// Regression for issue #65: the daemon already prepends the skill
|
||||
// body to every mission via missionWithSkill() before the Request
|
||||
// reaches HandleMessage. HandleMessage must pass the mission
|
||||
// through unchanged -- a second prepend here would duplicate the
|
||||
// skill body in the agent's context window.
|
||||
|
||||
dir := t.TempDir()
|
||||
skillsDir := filepath.Join(dir, ".agents", "skills", "archie-wf-tdd")
|
||||
if err := os.MkdirAll(skillsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(skillsDir, "SKILL.md"), []byte(`---
|
||||
name: archie-wf-tdd
|
||||
description: TDD bugfix workflow
|
||||
version: 1.0.0
|
||||
metadata:
|
||||
archie:
|
||||
workflow: tdd
|
||||
---
|
||||
Analyse the bug. Write repro tests. Fix.
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Mission already includes the daemon-side prepend (as produced by
|
||||
// missionWithSkill in internal/workflow/agent.go).
|
||||
alreadyPrepended := "Follow these project-specific guidelines:\n\n" +
|
||||
"Analyse the bug. Write repro tests. Fix.\n\n---\n\n" +
|
||||
"Analyse this bug"
|
||||
|
||||
var capturedMission string
|
||||
captureFactory := func(providers map[string]Provider, log *slog.Logger) Runner {
|
||||
return &captureRunner{callback: func(req Request) {
|
||||
capturedMission = req.Mission
|
||||
}}
|
||||
}
|
||||
|
||||
msg := AgentRequestMessage{
|
||||
TaskID: 1,
|
||||
Attempt: 1,
|
||||
Stage: "analyse",
|
||||
Workflow: "tdd",
|
||||
Workspace: dir,
|
||||
Request: Request{
|
||||
Version: ProtocolVersion, TaskID: 1, Attempt: 1,
|
||||
Stage: "analyse", Model: "test/model",
|
||||
Mission: alreadyPrepended,
|
||||
Budget: Budget{MaxSteps: 1},
|
||||
},
|
||||
Providers: map[string]Provider{"test": {Class: "openai", APIKeyEnv: "FAKE"}},
|
||||
}
|
||||
|
||||
resp, err := HandleMessage(context.Background(), msg, slog.New(slog.DiscardHandler), captureFactory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = resp
|
||||
|
||||
// The mission must be passed through unchanged. Any modification
|
||||
// would indicate a double-prepend bug (issue #65).
|
||||
if capturedMission != alreadyPrepended {
|
||||
t.Errorf("HandleMessage modified the mission:\n got: %q\n want: %q\n\n"+
|
||||
"The daemon already prepends the skill body via missionWithSkill(). "+
|
||||
"HandleMessage must not prepend it again -- doing so duplicates content "+
|
||||
"in the agent's context window (issue #65).",
|
||||
capturedMission, alreadyPrepended)
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import "context"
|
||||
|
||||
// StageMessage is one decoded single-stage request together with the transport
|
||||
// operations that complete its delivery. Infrastructure owns the wire payload,
|
||||
// reply inbox, and acknowledgement mechanism; the worker only sees typed data.
|
||||
type StageMessage interface {
|
||||
Request() (AgentRequestMessage, error)
|
||||
Respond(context.Context, *AgentResponseEnvelope) error
|
||||
Ack() error
|
||||
Nak() error
|
||||
LogAttributes() []any
|
||||
}
|
||||
|
||||
// StageConsumer receives decoded single-stage requests.
|
||||
type StageConsumer interface {
|
||||
FetchStage(context.Context) (StageMessage, error)
|
||||
}
|
||||
@@ -5,20 +5,14 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Agent execution subjects.
|
||||
// Agent observability subjects.
|
||||
//
|
||||
// These belong to this package because it defines what an agent request and
|
||||
// response mean. They previously sat in the NATS package, which made the
|
||||
// transport the owner of Archie's agent protocol; see
|
||||
// docs/architecture/dependencies-and-contracts.md -- message schemas and the
|
||||
// subjects addressing them stay with the domain that defines them.
|
||||
//
|
||||
// The stage travels in the payload, not the subject: one subject per task
|
||||
// carrying sequential stage messages.
|
||||
const (
|
||||
// SubjectAgentWildcard matches every agent subject.
|
||||
SubjectAgentWildcard = "archie.agent.>"
|
||||
|
||||
// SubjectSystemWildcard matches every task's system subject, for a
|
||||
// daemon subscribing to all of them at once with a single core NATS
|
||||
// subscription rather than one per running task.
|
||||
@@ -34,19 +28,6 @@ const (
|
||||
subjectEventsSuffix = ".events"
|
||||
)
|
||||
|
||||
// SubjectForRequest returns the subject carrying stage execution requests for
|
||||
// a task.
|
||||
func SubjectForRequest(taskID int64) string {
|
||||
return subjectForTask(taskID, "request")
|
||||
}
|
||||
|
||||
// SubjectForResponse returns the subject for agent output destined for human
|
||||
// channels. The daemon reviews these before forwarding to issue comments,
|
||||
// labels, or pull requests.
|
||||
func SubjectForResponse(taskID int64) string {
|
||||
return subjectForTask(taskID, "response")
|
||||
}
|
||||
|
||||
// SubjectForSystem returns the subject for internal agent messages such as
|
||||
// log dumps, health, and PII warnings. The daemon reads these for
|
||||
// observability and never forwards them.
|
||||
|
||||
@@ -1,170 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const maxDiagnosticBytes = 64 << 10
|
||||
|
||||
// SubprocessRunner executes each agent stage in a fresh archie-agent process.
|
||||
type SubprocessRunner struct {
|
||||
Command string
|
||||
Args []string
|
||||
// Environ is the daemon environment to select from. AdditionalEnv names
|
||||
// operator-approved compatibility variables; the requested provider's key
|
||||
// variable is added automatically for that invocation only.
|
||||
Environ []string
|
||||
AdditionalEnv []string
|
||||
Diagnostics io.Writer
|
||||
Providers map[string]Provider
|
||||
}
|
||||
|
||||
// Run ignores report: the subprocess executes tools in its own process, so
|
||||
// there is nothing here to observe a call as it happens.
|
||||
func (r *SubprocessRunner) Run(ctx context.Context, workspace string, req Request, _ ToolCallReporter) (Result, error) {
|
||||
providerName, _, ok := strings.Cut(req.Model, "/")
|
||||
if !ok || providerName == "" {
|
||||
return Result{}, fmt.Errorf("agent model %q must be provider/model", req.Model)
|
||||
}
|
||||
provider, ok := r.Providers[providerName]
|
||||
if !ok {
|
||||
return Result{}, fmt.Errorf("agent model provider %q is not configured", providerName)
|
||||
}
|
||||
providers := map[string]Provider{providerName: provider}
|
||||
invocation := Invocation{Version: ProtocolVersion, Workspace: workspace, Request: req, Providers: providers}
|
||||
if err := invocation.Validate(); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
if strings.TrimSpace(r.Command) == "" {
|
||||
return Result{}, fmt.Errorf("archie-agent command is required")
|
||||
}
|
||||
payload, err := json.Marshal(invocation)
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("encode invocation: %w", err)
|
||||
}
|
||||
payload = append(payload, '\n')
|
||||
|
||||
cmd := exec.CommandContext(ctx, r.Command, r.Args...)
|
||||
configureProcessCancellation(cmd)
|
||||
cmd.Stdin = bytes.NewReader(payload)
|
||||
cmd.Env = WorkerEnvironment(r.Environ, r.AdditionalEnv, providers)
|
||||
stdout := &cappedBuffer{limit: maxProtocolBytes}
|
||||
stderr := &cappedBuffer{limit: maxDiagnosticBytes}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
if r.Diagnostics != nil {
|
||||
cmd.Stderr = io.MultiWriter(r.Diagnostics, stderr)
|
||||
}
|
||||
if err := cmd.Run(); err != nil {
|
||||
if cause := context.Cause(ctx); cause != nil {
|
||||
if diagnostic := strings.TrimSpace(stderr.String()); diagnostic != "" {
|
||||
return Result{}, fmt.Errorf("%w: archie-agent: %s", cause, diagnostic)
|
||||
}
|
||||
return Result{}, cause
|
||||
}
|
||||
return Result{}, fmt.Errorf("archie-agent process: %w: %s", err, strings.TrimSpace(stderr.String()))
|
||||
}
|
||||
if stdout.truncated {
|
||||
return Result{}, fmt.Errorf("archie-agent response exceeds %d bytes", maxProtocolBytes)
|
||||
}
|
||||
return decodeSubprocessResponse(bytes.NewReader(stdout.Bytes()), stderr.String(), req)
|
||||
}
|
||||
|
||||
// decodeSubprocessResponse reads one Response from r and validates it
|
||||
// against req, returning the Result or an error.
|
||||
func decodeSubprocessResponse(r io.Reader, stderrText string, req Request) (Result, error) {
|
||||
var response Response
|
||||
if err := decodeOne(r, &response); err != nil {
|
||||
return Result{}, fmt.Errorf("decode archie-agent response: %w: %s", err, strings.TrimSpace(stderrText))
|
||||
}
|
||||
if response.Version != ProtocolVersion {
|
||||
return Result{}, fmt.Errorf("archie-agent response protocol version %d is unsupported", response.Version)
|
||||
}
|
||||
if response.Result.Version != 0 {
|
||||
if err := response.Result.ValidateFor(req); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
}
|
||||
if response.Error != "" {
|
||||
return response.Result, errors.New(response.Error)
|
||||
}
|
||||
if err := response.Result.ValidateFor(req); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
return response.Result, nil
|
||||
}
|
||||
|
||||
type cappedBuffer struct {
|
||||
bytes.Buffer
|
||||
limit int
|
||||
truncated bool
|
||||
}
|
||||
|
||||
func (b *cappedBuffer) Write(p []byte) (int, error) {
|
||||
original := len(p)
|
||||
remaining := b.limit - b.Len()
|
||||
if remaining <= 0 {
|
||||
b.truncated = true
|
||||
return original, nil
|
||||
}
|
||||
if len(p) > remaining {
|
||||
_, _ = b.Buffer.Write(p[:remaining])
|
||||
b.truncated = true
|
||||
return original, nil
|
||||
}
|
||||
_, _ = b.Buffer.Write(p)
|
||||
return original, nil
|
||||
}
|
||||
|
||||
// SelectEnvironment copies only named variables from environ. Later entries
|
||||
// win, matching os.Environ and exec.Cmd semantics without accidentally
|
||||
// inheriting unrelated variables. This is not an OS security boundary: a
|
||||
// same-UID subprocess may still access daemon-readable host resources.
|
||||
func SelectEnvironment(environ, names []string) []string {
|
||||
allowed := make(map[string]bool, len(names))
|
||||
for _, name := range names {
|
||||
if name != "" {
|
||||
allowed[name] = true
|
||||
}
|
||||
}
|
||||
values := make(map[string]string, len(allowed))
|
||||
for _, entry := range environ {
|
||||
name, _, ok := strings.Cut(entry, "=")
|
||||
if ok && allowed[name] {
|
||||
values[name] = entry
|
||||
}
|
||||
}
|
||||
out := make([]string, 0, len(values))
|
||||
for _, name := range names {
|
||||
if entry, ok := values[name]; ok {
|
||||
out = append(out, entry)
|
||||
delete(values, name)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// DefaultEnvironmentNames are compatibility variables safe to forward by
|
||||
// default. Additional toolchain variables must be explicitly configured.
|
||||
func DefaultEnvironmentNames() []string {
|
||||
return []string{"PATH", "HOME", "TMPDIR", "TMP", "TEMP", "LANG", "LC_ALL", "SSL_CERT_FILE", "SSL_CERT_DIR"}
|
||||
}
|
||||
|
||||
// WorkerEnvironment selects the default compatibility variables, configured
|
||||
// additions, and each provider's API-key variable. Forge credentials are not
|
||||
// forwarded unless an operator explicitly lists or reuses their variable.
|
||||
func WorkerEnvironment(environ, additional []string, providers map[string]Provider) []string {
|
||||
names := DefaultEnvironmentNames()
|
||||
names = append(names, additional...)
|
||||
for _, provider := range providers {
|
||||
names = append(names, provider.APIKeyEnv)
|
||||
}
|
||||
return SelectEnvironment(environ, names)
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
||||
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// configureProcessCancellation puts the worker and every command it starts in
|
||||
// a dedicated process group. CommandContext cancellation then kills the whole
|
||||
// group so toolchain grandchildren cannot continue mutating the worktree.
|
||||
func configureProcessCancellation(cmd *exec.Cmd) {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
cmd.Cancel = func() error {
|
||||
if cmd.Process == nil {
|
||||
return os.ErrProcessDone
|
||||
}
|
||||
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
if errors.Is(err, syscall.ESRCH) {
|
||||
return os.ErrProcessDone
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package agentexec
|
||||
|
||||
import "os/exec"
|
||||
|
||||
// Windows keeps exec.CommandContext's direct-process cancellation. Production
|
||||
// subprocess and container execution are currently supported on Unix hosts.
|
||||
func configureProcessCancellation(_ *exec.Cmd) {}
|
||||
@@ -1,249 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSubprocessRunnerRoundTrip(t *testing.T) {
|
||||
req := testRequest()
|
||||
result, err := helperRunner("success").Run(context.Background(), "/workspace", req, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Summary != "from child" || result.TaskID != req.TaskID || result.Attempt != req.Attempt {
|
||||
t.Fatalf("result = %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerReturnsStampedWorkerError(t *testing.T) {
|
||||
req := testRequest()
|
||||
result, err := helperRunner("worker-error").Run(context.Background(), "/workspace", req, nil)
|
||||
if err == nil || err.Error() != "provider unavailable" {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
if result.TaskID != req.TaskID || result.Stage != req.Stage {
|
||||
t.Fatalf("partial result lost identity: %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerRejectsMalformedAndOversizedOutput(t *testing.T) {
|
||||
for _, behavior := range []string{"malformed", "oversized", "multiple", "mismatched"} {
|
||||
t.Run(behavior, func(t *testing.T) {
|
||||
_, err := helperRunner(behavior).Run(context.Background(), "/workspace", testRequest(), nil)
|
||||
if err == nil {
|
||||
t.Fatal("Run succeeded")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerReportsExitDiagnostics(t *testing.T) {
|
||||
_, err := helperRunner("exit").Run(context.Background(), "/workspace", testRequest(), nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "child diagnostic") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerCancelsChild(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
||||
defer cancel()
|
||||
_, err := helperRunner("hang").Run(ctx, "/workspace", testRequest(), nil)
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("error = %v, want deadline exceeded", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerCancellationKillsGrandchildren(t *testing.T) {
|
||||
marker := filepath.Join(t.TempDir(), "grandchild-survived")
|
||||
runner := helperRunner("spawn-grandchild")
|
||||
runner.Environ = append(runner.Environ, "AGENT_HELPER_MARKER="+marker)
|
||||
runner.AdditionalEnv = append(runner.AdditionalEnv, "AGENT_HELPER_MARKER")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
||||
defer cancel()
|
||||
_, err := runner.Run(ctx, "/workspace", testRequest(), nil)
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("error = %v, want deadline exceeded", err)
|
||||
}
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
if _, err := os.Stat(marker); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("grandchild survived cancellation: stat error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerForwardsOnlyRequestedProvider(t *testing.T) {
|
||||
runner := helperRunner("provider-scope")
|
||||
runner.Environ = append(runner.Environ, "OTHER_KEY=must-not-leak")
|
||||
runner.Providers["other"] = Provider{Class: "openai", APIKeyEnv: "OTHER_KEY"}
|
||||
if _, err := runner.Run(context.Background(), "/workspace", testRequest(), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvocationRejectsCredentialBearingProviderURL(t *testing.T) {
|
||||
for _, baseURL := range []string{
|
||||
"https://token@example.com/v1",
|
||||
"https://example.com/v1?api_key=secret",
|
||||
"https://example.com/v1#secret",
|
||||
} {
|
||||
t.Run(baseURL, func(t *testing.T) {
|
||||
invocation := Invocation{
|
||||
Version: ProtocolVersion, Workspace: "/workspace", Request: testRequest(),
|
||||
Providers: map[string]Provider{"provider": {Class: "openai", BaseURL: baseURL}},
|
||||
}
|
||||
if err := invocation.Validate(); err == nil {
|
||||
t.Fatal("Validate succeeded")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectEnvironmentDoesNotLeakUnlistedValues(t *testing.T) {
|
||||
got := WorkerEnvironment(
|
||||
[]string{"PATH=/bin", "ARCHIE_GITHUB_TOKEN=secret", "OPENAI_API_KEY=model", "PATH=/usr/bin"},
|
||||
nil,
|
||||
map[string]Provider{"openai": {APIKeyEnv: "OPENAI_API_KEY"}},
|
||||
)
|
||||
want := []string{"PATH=/usr/bin", "OPENAI_API_KEY=model"}
|
||||
if strings.Join(got, "\n") != strings.Join(want, "\n") {
|
||||
t.Fatalf("environment = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeOneRejectsMultipleInvocations(t *testing.T) {
|
||||
input := `{"version":1} {"version":1}`
|
||||
err := ServeOne(context.Background(), strings.NewReader(input), &strings.Builder{}, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "multiple JSON values") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeOneWritesStampedExecutionError(t *testing.T) {
|
||||
invocation := Invocation{
|
||||
Version: ProtocolVersion, Workspace: "/workspace", Request: testRequest(),
|
||||
Providers: map[string]Provider{"provider": {Class: "openai"}},
|
||||
}
|
||||
input, err := json.Marshal(invocation)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output strings.Builder
|
||||
err = serveOne(context.Background(), strings.NewReader(string(input)), &output, func(Invocation) Runner {
|
||||
return runnerFunc(func(_ context.Context, _ string, req Request, _ ToolCallReporter) (Result, error) {
|
||||
return Result{
|
||||
Version: ProtocolVersion, TaskID: req.TaskID, Attempt: req.Attempt,
|
||||
Stage: req.Stage, Status: StatusPassed,
|
||||
}, errors.New("provider unavailable")
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response Response
|
||||
if err := json.Unmarshal([]byte(output.String()), &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Error != "provider unavailable" || response.Result.TaskID != invocation.Request.TaskID {
|
||||
t.Fatalf("response = %#v", response)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeOneRejectsOversizedInput(t *testing.T) {
|
||||
var value any
|
||||
err := decodeOne(strings.NewReader(`"`+strings.Repeat("x", maxProtocolBytes)+`"`), &value)
|
||||
if err == nil || !strings.Contains(err.Error(), "exceeds") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func helperRunner(behavior string) *SubprocessRunner {
|
||||
return &SubprocessRunner{
|
||||
Command: os.Args[0], Args: []string{"-test.run=TestAgentHelperProcess"},
|
||||
Environ: []string{
|
||||
"GO_WANT_AGENT_HELPER=1", "AGENT_HELPER_BEHAVIOR=" + behavior,
|
||||
"PROVIDER_KEY=selected",
|
||||
},
|
||||
AdditionalEnv: []string{"GO_WANT_AGENT_HELPER", "AGENT_HELPER_BEHAVIOR"},
|
||||
Providers: map[string]Provider{"provider": {Class: "openai", APIKeyEnv: "PROVIDER_KEY"}},
|
||||
}
|
||||
}
|
||||
|
||||
type runnerFunc func(context.Context, string, Request, ToolCallReporter) (Result, error)
|
||||
|
||||
func (f runnerFunc) Run(ctx context.Context, workspace string, req Request, report ToolCallReporter) (Result, error) {
|
||||
return f(ctx, workspace, req, report)
|
||||
}
|
||||
|
||||
func testRequest() Request {
|
||||
return Request{
|
||||
Version: ProtocolVersion, TaskID: 42, Attempt: 3, Stage: "build",
|
||||
Model: "provider/model", Mission: "test mission",
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentHelperProcess(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
|
||||
if os.Getenv("GO_WANT_AGENT_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
var invocation Invocation
|
||||
if err := json.NewDecoder(os.Stdin).Decode(&invocation); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
result := Result{
|
||||
Version: ProtocolVersion, TaskID: invocation.Request.TaskID, Attempt: invocation.Request.Attempt,
|
||||
Stage: invocation.Request.Stage, Status: StatusPassed, Summary: "from child",
|
||||
}
|
||||
switch os.Getenv("AGENT_HELPER_BEHAVIOR") {
|
||||
case "success":
|
||||
_ = json.NewEncoder(os.Stdout).Encode(Response{Version: ProtocolVersion, Result: result})
|
||||
case "worker-error":
|
||||
_ = json.NewEncoder(os.Stdout).Encode(Response{Version: ProtocolVersion, Result: result, Error: "provider unavailable"})
|
||||
case "malformed":
|
||||
if _, err := fmt.Fprint(os.Stdout, "not-json"); err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
case "oversized":
|
||||
if _, err := fmt.Fprint(os.Stdout, strings.Repeat("x", maxProtocolBytes+1)); err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
case "multiple":
|
||||
_ = json.NewEncoder(os.Stdout).Encode(Response{Version: ProtocolVersion, Result: result})
|
||||
_ = json.NewEncoder(os.Stdout).Encode(Response{Version: ProtocolVersion, Result: result})
|
||||
case "mismatched":
|
||||
result.Attempt++
|
||||
_ = json.NewEncoder(os.Stdout).Encode(Response{Version: ProtocolVersion, Result: result})
|
||||
case "exit":
|
||||
fmt.Fprintln(os.Stderr, "child diagnostic")
|
||||
os.Exit(2)
|
||||
case "hang":
|
||||
time.Sleep(10 * time.Second)
|
||||
case "spawn-grandchild":
|
||||
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", `sleep 0.2; printf survived > "$AGENT_HELPER_MARKER"`)
|
||||
cmd.Env = os.Environ()
|
||||
if err := cmd.Start(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
time.Sleep(10 * time.Second)
|
||||
case "provider-scope":
|
||||
if len(invocation.Providers) != 1 || invocation.Providers["provider"].APIKeyEnv != "PROVIDER_KEY" ||
|
||||
os.Getenv("PROVIDER_KEY") != "selected" || os.Getenv("OTHER_KEY") != "" {
|
||||
os.Exit(2)
|
||||
}
|
||||
_ = json.NewEncoder(os.Stdout).Encode(Response{Version: ProtocolVersion, Result: result})
|
||||
default:
|
||||
os.Exit(2)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
@@ -23,8 +23,7 @@ type EventPublisher interface {
|
||||
// in-process *events.Bus the daemon cannot see -- the worker and the daemon
|
||||
// are separate processes connected only by NATS. This is the bridge that
|
||||
// makes those Emit calls (stage progress, outcome, parking) reach the
|
||||
// daemon's events table and dashboard timeline the same way an in-process,
|
||||
// daemon-run workflow's Bus already does.
|
||||
// daemon's events table and dashboard timeline.
|
||||
//
|
||||
// A marshal or publish failure is logged and the loop continues: one missing
|
||||
// event must never abort the ones after it, and event shipping must never
|
||||
|
||||
@@ -78,7 +78,7 @@ type ToolSetOptions struct {
|
||||
Approval tools.ApprovalRequester
|
||||
|
||||
// OnToolCall, when non-nil, is notified once per completed tool call.
|
||||
// InProcessRunner populates it so a workflow stage can surface tool
|
||||
// LoopRunner populates it so a workflow stage can surface tool
|
||||
// activity on the task timeline (archie-core-467's task-transcript
|
||||
// counterpart). Nil means no one is listening.
|
||||
OnToolCall ToolCallReporter
|
||||
@@ -94,11 +94,8 @@ type ToolCallReport struct {
|
||||
Failed bool
|
||||
}
|
||||
|
||||
// ToolCallReporter receives one ToolCallReport per completed tool call. Only
|
||||
// a runner that executes in the same process as its tools can populate this
|
||||
// -- SubprocessRunner and NATSRunner dispatch to a different process and have
|
||||
// no visibility into individual calls from here. A nil reporter is valid and
|
||||
// reports nothing.
|
||||
// ToolCallReporter receives one ToolCallReport per completed tool call. A nil
|
||||
// reporter is valid and reports nothing.
|
||||
type ToolCallReporter func(ToolCallReport)
|
||||
|
||||
// toolCallDetailBytes caps the summary ToolCallReport.Detail carries. This
|
||||
|
||||
@@ -1,166 +0,0 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const maxProtocolBytes = 8 << 20
|
||||
|
||||
// DefaultRunnerFactory creates an InProcessRunner backed by the ai-sdk runtime.
|
||||
func DefaultRunnerFactory(providers map[string]Provider, log *slog.Logger) Runner {
|
||||
return NewInProcessRunner(NewRuntime(providers), log)
|
||||
}
|
||||
|
||||
// ServeOne decodes and executes exactly one invocation.
|
||||
func ServeOne(ctx context.Context, in io.Reader, out io.Writer, log *slog.Logger) error {
|
||||
return serveOne(ctx, in, out, func(invocation Invocation) Runner {
|
||||
return DefaultRunnerFactory(invocation.Providers, log)
|
||||
})
|
||||
}
|
||||
|
||||
func serveOne(ctx context.Context, in io.Reader, out io.Writer, newRunner func(Invocation) Runner) error {
|
||||
var invocation Invocation
|
||||
if err := decodeOne(in, &invocation); err != nil {
|
||||
return fmt.Errorf("decode invocation: %w", err)
|
||||
}
|
||||
if err := invocation.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
runner := newRunner(invocation)
|
||||
if runner == nil {
|
||||
return fmt.Errorf("agent runner is not configured")
|
||||
}
|
||||
result, runErr := runner.Run(ctx, invocation.Workspace, invocation.Request, nil)
|
||||
response := Response{Version: ProtocolVersion, Result: result}
|
||||
if runErr != nil {
|
||||
response.Error = runErr.Error()
|
||||
}
|
||||
if err := json.NewEncoder(out).Encode(response); err != nil {
|
||||
return fmt.Errorf("encode response: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReplyFunc sends a response back to the daemon.
|
||||
type ReplyFunc func(data []byte) error
|
||||
|
||||
// RunnerFactory creates a Runner from provider config. In production this
|
||||
// calls NewRuntime + NewInProcessRunner; tests inject a mock.
|
||||
type RunnerFactory func(Providers map[string]Provider, log *slog.Logger) Runner
|
||||
|
||||
// HandleMessage processes one AgentRequestMessage. When msg.Stages is
|
||||
// populated and msg.Workflow is set, it runs all stages as a per-task
|
||||
// batch. Otherwise it falls back to single-stage execution.
|
||||
//
|
||||
// It passes a nil ToolCallReporter to every Run call: this path has no
|
||||
// TaskContext and therefore no events.Bus to report through. The workflow
|
||||
// engine's own AgentStage.Stage().Run (agent.go) is what wires a real
|
||||
// reporter into tc.Emit; this entry point predates that and is not reached
|
||||
// by workflow.Run.
|
||||
func HandleMessage(ctx context.Context, msg AgentRequestMessage, log *slog.Logger, newRunner RunnerFactory) (*AgentResponseEnvelope, error) {
|
||||
runner := newRunner(msg.Providers, log)
|
||||
if runner == nil {
|
||||
return nil, fmt.Errorf("no providers configured in request")
|
||||
}
|
||||
|
||||
channel := msg.Channel
|
||||
if channel == "" {
|
||||
channel = "response"
|
||||
}
|
||||
|
||||
// Per-task mode: run all stages for this workflow as a batch.
|
||||
if msg.Workflow != "" && len(msg.Stages) > 0 {
|
||||
return runStages(ctx, runner, msg, channel, log)
|
||||
}
|
||||
|
||||
// Single-stage mode (backward compatible).
|
||||
result, runErr := runner.Run(ctx, msg.Workspace, msg.Request, nil)
|
||||
resp := &AgentResponseEnvelope{
|
||||
Version: msg.Request.Version,
|
||||
Result: result,
|
||||
Channel: channel,
|
||||
}
|
||||
if runErr != nil {
|
||||
resp.Error = runErr.Error()
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// runStages executes every stage in msg.Stages sequentially, accumulating
|
||||
// tokens, iterations, and notes. The first failure stops the batch and
|
||||
// returns immediately. On success, TaskCompleted is set.
|
||||
func runStages(ctx context.Context, runner Runner, msg AgentRequestMessage, channel string, log *slog.Logger) (*AgentResponseEnvelope, error) {
|
||||
var (
|
||||
totalTokens int
|
||||
totalIter int
|
||||
lastResult Result
|
||||
combinedNotes []string
|
||||
combinedDetail []string
|
||||
)
|
||||
|
||||
for i, stage := range msg.Stages {
|
||||
log.Info("running stage", "stage", stage.Stage, "batch", fmt.Sprintf("%d/%d", i+1, len(msg.Stages)))
|
||||
result, runErr := runner.Run(ctx, msg.Workspace, stage, nil)
|
||||
if runErr != nil {
|
||||
resp := &AgentResponseEnvelope{
|
||||
Version: stage.Version,
|
||||
Result: result,
|
||||
Error: runErr.Error(),
|
||||
Channel: channel,
|
||||
}
|
||||
return resp, nil //nolint:nilerr // runErr is reported to the caller in the response envelope's Error field; returning it here as well would double-report and abort the NATS reply
|
||||
}
|
||||
totalTokens += result.TokensUsed
|
||||
totalIter += result.Iterations
|
||||
combinedNotes = append(combinedNotes, result.AppendedNotes...)
|
||||
if result.Summary != "" {
|
||||
combinedDetail = append(combinedDetail, fmt.Sprintf("[%s] %s", stage.Stage, result.Summary))
|
||||
}
|
||||
lastResult = result
|
||||
if result.Status != StatusPassed {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
lastResult.TokensUsed = totalTokens
|
||||
lastResult.Iterations = totalIter
|
||||
lastResult.AppendedNotes = combinedNotes
|
||||
if len(combinedDetail) > 0 {
|
||||
lastResult.Summary = strings.Join(combinedDetail, "\n")
|
||||
}
|
||||
|
||||
return &AgentResponseEnvelope{
|
||||
Version: lastResult.Version,
|
||||
Result: lastResult,
|
||||
Channel: channel,
|
||||
TaskCompleted: lastResult.Status == StatusPassed,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func decodeOne(r io.Reader, value any) error {
|
||||
data, err := io.ReadAll(io.LimitReader(r, maxProtocolBytes+1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(data) > maxProtocolBytes {
|
||||
return fmt.Errorf("JSON value exceeds %d bytes", maxProtocolBytes)
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
if err := decoder.Decode(value); err != nil {
|
||||
return err
|
||||
}
|
||||
var extra any
|
||||
if err := decoder.Decode(&extra); err != io.EOF {
|
||||
if err == nil {
|
||||
return fmt.Errorf("multiple JSON values are not allowed")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -49,3 +49,18 @@ func TestApplicationOwnsNoTransportWireMechanics(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Full-task request/reply is the worker's only production entry point. A
|
||||
// single-stage loop would recreate the obsolete split where archied owns the
|
||||
// workflow and remotely invokes each stage.
|
||||
func TestApplicationContainsNoSingleStageWorkerLoop(t *testing.T) {
|
||||
for _, path := range []string{"single_stage_request.go", "stage_loop.go"} {
|
||||
matches, err := filepath.Glob(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(matches) != 0 {
|
||||
t.Errorf("legacy single-stage worker file still exists: %s", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
type singleStageRequestDependencies struct {
|
||||
handleMessage func(context.Context, agentexec.AgentRequestMessage, *slog.Logger, agentexec.RunnerFactory) (*agentexec.AgentResponseEnvelope, error)
|
||||
startProviders func(context.Context, []config.MCPServer, *slog.Logger) (*mcpProviderSet, error)
|
||||
newRunner func(map[string]agentexec.Provider, *slog.Logger, ...*tools.Registry) agentexec.Runner
|
||||
}
|
||||
|
||||
func productionSingleStageDependencies() singleStageRequestDependencies {
|
||||
return singleStageRequestDependencies{
|
||||
handleMessage: agentexec.HandleMessage,
|
||||
startProviders: startMCPProviders,
|
||||
newRunner: newSingleStageRunner,
|
||||
}
|
||||
}
|
||||
|
||||
func newSingleStageRunner(
|
||||
providers map[string]agentexec.Provider,
|
||||
log *slog.Logger,
|
||||
registries ...*tools.Registry,
|
||||
) agentexec.Runner {
|
||||
return agentexec.NewInProcessRunner(agentexec.NewRuntime(providers), log, registries...)
|
||||
}
|
||||
|
||||
func handleSingleStageRequest(
|
||||
ctx context.Context,
|
||||
message agentexec.StageMessage,
|
||||
log *slog.Logger,
|
||||
dependencies singleStageRequestDependencies,
|
||||
) error {
|
||||
request, err := message.Request()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
attributes := []any{
|
||||
"task", request.TaskID,
|
||||
"attempt", request.Attempt,
|
||||
"stage", request.Stage,
|
||||
"workflow", request.Workflow,
|
||||
}
|
||||
attributes = append(attributes, message.LogAttributes()...)
|
||||
log.Info("processing stage", attributes...)
|
||||
|
||||
startProviders := dependencies.startProviders
|
||||
if startProviders == nil {
|
||||
startProviders = startMCPProviders
|
||||
}
|
||||
newRunner := dependencies.newRunner
|
||||
if newRunner == nil {
|
||||
newRunner = newSingleStageRunner
|
||||
}
|
||||
|
||||
mcpSet, mcpErr := startProviders(ctx, request.MCPServers, log)
|
||||
if mcpSet != nil {
|
||||
defer mcpSet.cleanup(ctx, log)
|
||||
}
|
||||
if mcpErr != nil {
|
||||
log.Warn("mcp providers had errors, continuing with available tools", "err", mcpErr)
|
||||
}
|
||||
|
||||
factory := func(providers map[string]agentexec.Provider, log *slog.Logger) agentexec.Runner {
|
||||
var registries []*tools.Registry
|
||||
if mcpSet != nil {
|
||||
registries = append(registries, mcpSet.registry)
|
||||
}
|
||||
return newRunner(providers, log, registries...)
|
||||
}
|
||||
|
||||
response, err := dependencies.handleMessage(ctx, request, log, factory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("handle: %w", err)
|
||||
}
|
||||
if err := message.Respond(ctx, response); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info(
|
||||
"stage complete",
|
||||
"task", request.TaskID,
|
||||
"stage", request.Stage,
|
||||
"status", response.Result.Status,
|
||||
)
|
||||
return nil
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
)
|
||||
|
||||
func singleStageLogger() (*slog.Logger, *bytes.Buffer) {
|
||||
var output bytes.Buffer
|
||||
return slog.New(slog.NewTextHandler(&output, nil)), &output
|
||||
}
|
||||
|
||||
func TestHandleSingleStageRequestReturnsTypedBoundaryAndExecutionFailures(t *testing.T) {
|
||||
requestErr := errors.New("decode request: invalid")
|
||||
handleErr := errors.New("agent execution failed")
|
||||
respondErr := errors.New("publish response: unavailable")
|
||||
tests := []struct {
|
||||
name string
|
||||
message *stageMessageStub
|
||||
handleErr error
|
||||
wantErr error
|
||||
wantHandle int
|
||||
wantReply int
|
||||
}{
|
||||
{name: "typed boundary", message: &stageMessageStub{requestErr: requestErr}, wantErr: requestErr},
|
||||
{name: "execution", message: &stageMessageStub{request: agentexec.AgentRequestMessage{TaskID: 17}}, handleErr: handleErr, wantErr: handleErr, wantHandle: 1},
|
||||
{name: "response", message: &stageMessageStub{request: agentexec.AgentRequestMessage{TaskID: 23}, respondErr: respondErr}, wantErr: respondErr, wantHandle: 1, wantReply: 1},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
handleCalls := 0
|
||||
dependencies := singleStageRequestDependencies{
|
||||
handleMessage: func(context.Context, agentexec.AgentRequestMessage, *slog.Logger, agentexec.RunnerFactory) (*agentexec.AgentResponseEnvelope, error) {
|
||||
handleCalls++
|
||||
return &agentexec.AgentResponseEnvelope{Result: agentexec.Result{Status: agentexec.StatusPassed}}, test.handleErr
|
||||
},
|
||||
}
|
||||
err := handleSingleStageRequest(t.Context(), test.message, slog.New(slog.DiscardHandler), dependencies)
|
||||
if !errors.Is(err, test.wantErr) {
|
||||
t.Fatalf("error = %v, want %v", err, test.wantErr)
|
||||
}
|
||||
if handleCalls != test.wantHandle || test.message.respondCalls != test.wantReply {
|
||||
t.Fatalf("handle/respond calls = (%d,%d), want (%d,%d)", handleCalls, test.message.respondCalls, test.wantHandle, test.wantReply)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleSingleStageRequestAttachesAndCleansUpMCPProviders(t *testing.T) {
|
||||
message := &stageMessageStub{request: agentexec.AgentRequestMessage{
|
||||
TaskID: 29,
|
||||
Stage: "implement",
|
||||
MCPServers: []config.MCPServer{{Name: "available"}},
|
||||
}}
|
||||
registry := tools.NewRegistry()
|
||||
if err := registry.Register(testTool("available")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider := &fakeMCPEngine{id: "available"}
|
||||
set := &mcpProviderSet{providers: []toolprovider.Engine{provider}, registry: registry}
|
||||
startCalls := 0
|
||||
factoryCalls := 0
|
||||
dependencies := singleStageRequestDependencies{
|
||||
startProviders: func(_ context.Context, servers []config.MCPServer, _ *slog.Logger) (*mcpProviderSet, error) {
|
||||
startCalls++
|
||||
if len(servers) != 1 || servers[0].Name != "available" {
|
||||
t.Fatalf("MCP servers = %#v", servers)
|
||||
}
|
||||
return set, nil
|
||||
},
|
||||
newRunner: func(_ map[string]agentexec.Provider, _ *slog.Logger, registries ...*tools.Registry) agentexec.Runner {
|
||||
factoryCalls++
|
||||
if len(registries) != 1 || registries[0] != registry {
|
||||
t.Fatalf("runner registries = %#v", registries)
|
||||
}
|
||||
return panicRunner{t: t}
|
||||
},
|
||||
handleMessage: func(_ context.Context, request agentexec.AgentRequestMessage, _ *slog.Logger, factory agentexec.RunnerFactory) (*agentexec.AgentResponseEnvelope, error) {
|
||||
if runner := factory(request.Providers, slog.New(slog.DiscardHandler)); runner == nil {
|
||||
t.Fatal("runner factory returned nil")
|
||||
}
|
||||
return &agentexec.AgentResponseEnvelope{Result: agentexec.Result{Status: agentexec.StatusPassed}}, nil
|
||||
},
|
||||
}
|
||||
if err := handleSingleStageRequest(t.Context(), message, slog.New(slog.DiscardHandler), dependencies); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if startCalls != 1 || factoryCalls != 1 || provider.stops != 1 || message.respondCalls != 1 {
|
||||
t.Fatalf("start/factory/stops/respond = (%d,%d,%d,%d), want all 1", startCalls, factoryCalls, provider.stops, message.respondCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleSingleStageRequestPreservesLifecycleLogs(t *testing.T) {
|
||||
message := &stageMessageStub{request: agentexec.AgentRequestMessage{
|
||||
TaskID: 31, Attempt: 4, Stage: "test", Workflow: "tdd",
|
||||
MCPServers: []config.MCPServer{{Name: "broken", Transport: "stdio"}},
|
||||
}}
|
||||
dependencies := productionSingleStageDependencies()
|
||||
dependencies.handleMessage = func(context.Context, agentexec.AgentRequestMessage, *slog.Logger, agentexec.RunnerFactory) (*agentexec.AgentResponseEnvelope, error) {
|
||||
return &agentexec.AgentResponseEnvelope{Result: agentexec.Result{Status: agentexec.StatusPassed}}, nil
|
||||
}
|
||||
log, output := singleStageLogger()
|
||||
if err := handleSingleStageRequest(t.Context(), message, log, dependencies); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, expected := range []string{
|
||||
"processing stage", "task=31", "attempt=4", "stage=test", "workflow=tdd", "reply_to=_INBOX.test",
|
||||
"mcp providers had errors, continuing with available tools", "stage complete", "status=passed",
|
||||
} {
|
||||
if !strings.Contains(output.String(), expected) {
|
||||
t.Fatalf("log output %q does not contain %q", output.String(), expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
)
|
||||
|
||||
// stageBus is the typed single-stage delivery capability required by the loop.
|
||||
type stageBus interface {
|
||||
agentexec.StageConsumer
|
||||
}
|
||||
|
||||
// runMainLoop runs the fetch-handle-ack loop until the context is cancelled.
|
||||
func runMainLoop(ctx context.Context, bus stageBus, log *slog.Logger) int {
|
||||
return runMainLoopWithDelay(ctx, bus, log, time.Sleep)
|
||||
}
|
||||
|
||||
func runMainLoopWithDelay(ctx context.Context, bus stageBus, log *slog.Logger, delay func(time.Duration)) int {
|
||||
for {
|
||||
if ctx.Err() != nil {
|
||||
log.Info("archie-agent shutting down")
|
||||
return 0
|
||||
}
|
||||
message, err := bus.FetchStage(ctx)
|
||||
switch {
|
||||
case errors.Is(err, eventbus.ErrNoMessage):
|
||||
continue
|
||||
case err != nil:
|
||||
if ctx.Err() != nil {
|
||||
return 0
|
||||
}
|
||||
log.Error("fetch failed", "err", err)
|
||||
delay(time.Second)
|
||||
continue
|
||||
}
|
||||
if err := handleSingleStageRequest(ctx, message, log, productionSingleStageDependencies()); err != nil {
|
||||
log.Error("handle failed", "err", err)
|
||||
if err := message.Nak(); err != nil {
|
||||
log.Warn("nak failed", "err", err)
|
||||
}
|
||||
// A Nak requests redelivery. Without a backoff, a deterministic
|
||||
// failure (a broken gate, a malformed request) refetches and
|
||||
// fails again immediately, flooding the log with one line per
|
||||
// loop iteration instead of one per genuine retry.
|
||||
delay(time.Second)
|
||||
continue
|
||||
}
|
||||
if err := message.Ack(); err != nil {
|
||||
log.Warn("ack failed", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,198 +0,0 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
)
|
||||
|
||||
type stageFetch struct {
|
||||
message agentexec.StageMessage
|
||||
err error
|
||||
before func()
|
||||
}
|
||||
|
||||
type stageBusStub struct {
|
||||
fetches []stageFetch
|
||||
fetchCalls int
|
||||
}
|
||||
|
||||
func (b *stageBusStub) FetchStage(context.Context) (agentexec.StageMessage, error) {
|
||||
if b.fetchCalls >= len(b.fetches) {
|
||||
panic("unexpected FetchStage call")
|
||||
}
|
||||
fetch := b.fetches[b.fetchCalls]
|
||||
b.fetchCalls++
|
||||
if fetch.before != nil {
|
||||
fetch.before()
|
||||
}
|
||||
return fetch.message, fetch.err
|
||||
}
|
||||
|
||||
type stageMessageStub struct {
|
||||
request agentexec.AgentRequestMessage
|
||||
requestErr error
|
||||
respondErr error
|
||||
ackErr error
|
||||
nakErr error
|
||||
respondCancel context.CancelFunc
|
||||
respondCalls int
|
||||
ackCalls int
|
||||
nakCalls int
|
||||
}
|
||||
|
||||
func (m *stageMessageStub) Request() (agentexec.AgentRequestMessage, error) {
|
||||
return m.request, m.requestErr
|
||||
}
|
||||
|
||||
func (m *stageMessageStub) Respond(context.Context, *agentexec.AgentResponseEnvelope) error {
|
||||
m.respondCalls++
|
||||
if m.respondCancel != nil {
|
||||
m.respondCancel()
|
||||
}
|
||||
return m.respondErr
|
||||
}
|
||||
|
||||
func (m *stageMessageStub) Ack() error { m.ackCalls++; return m.ackErr }
|
||||
func (m *stageMessageStub) Nak() error { m.nakCalls++; return m.nakErr }
|
||||
func (*stageMessageStub) LogAttributes() []any { return []any{"reply_to", "_INBOX.test"} }
|
||||
|
||||
func stageLoopLogger() (*slog.Logger, *bytes.Buffer) {
|
||||
var output bytes.Buffer
|
||||
return slog.New(slog.NewTextHandler(&output, nil)), &output
|
||||
}
|
||||
|
||||
func noDelay(t *testing.T) (func(time.Duration), *[]time.Duration) {
|
||||
t.Helper()
|
||||
var calls []time.Duration
|
||||
return func(duration time.Duration) { calls = append(calls, duration) }, &calls
|
||||
}
|
||||
|
||||
func TestRunMainLoopExitsImmediatelyWhenAlreadyCancelled(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
bus := &stageBusStub{}
|
||||
log, output := stageLoopLogger()
|
||||
if code := runMainLoop(ctx, bus, log); code != 0 {
|
||||
t.Fatalf("runMainLoop() = %d, want 0", code)
|
||||
}
|
||||
if bus.fetchCalls != 0 || !strings.Contains(output.String(), "archie-agent shutting down") {
|
||||
t.Fatalf("fetches/log = (%d, %q), want no fetch and shutdown log", bus.fetchCalls, output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMainLoopFetchOutcomes(t *testing.T) {
|
||||
fetchErr := errors.New("temporary fetch failure")
|
||||
tests := []struct {
|
||||
name string
|
||||
firstErr error
|
||||
wantDelay bool
|
||||
wantLog bool
|
||||
}{
|
||||
{name: "idle", firstErr: eventbus.ErrNoMessage},
|
||||
{name: "failure", firstErr: fetchErr, wantDelay: true, wantLog: true},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
bus := &stageBusStub{fetches: []stageFetch{
|
||||
{err: test.firstErr},
|
||||
{err: errors.New("fetch after cancellation"), before: cancel},
|
||||
}}
|
||||
log, output := stageLoopLogger()
|
||||
delay, delays := noDelay(t)
|
||||
if code := runMainLoopWithDelay(ctx, bus, log, delay); code != 0 {
|
||||
t.Fatalf("runMainLoopWithDelay() = %d, want 0", code)
|
||||
}
|
||||
if got := len(*delays) == 1 && (*delays)[0] == time.Second; got != test.wantDelay {
|
||||
t.Fatalf("delayed = %v (%v), want %v", got, *delays, test.wantDelay)
|
||||
}
|
||||
if got := strings.Contains(output.String(), "fetch failed"); got != test.wantLog {
|
||||
t.Fatalf("fetch failure logged = %v, want %v; %q", got, test.wantLog, output.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMainLoopStopsWhenFetchCancelsContext(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
bus := &stageBusStub{fetches: []stageFetch{{err: errors.New("interrupted"), before: cancel}}}
|
||||
log, output := stageLoopLogger()
|
||||
delay, delays := noDelay(t)
|
||||
if code := runMainLoopWithDelay(ctx, bus, log, delay); code != 0 {
|
||||
t.Fatalf("runMainLoopWithDelay() = %d, want 0", code)
|
||||
}
|
||||
if len(*delays) != 0 || output.Len() != 0 {
|
||||
t.Fatalf("delay/log = (%v, %q), want none", *delays, output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMainLoopNaksHandleErrorsAndContinues(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
nakErr error
|
||||
wantNakLog bool
|
||||
}{
|
||||
{name: "nak succeeds"},
|
||||
{name: "nak fails", nakErr: errors.New("nak unavailable"), wantNakLog: true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
message := &stageMessageStub{requestErr: errors.New("decode request: invalid"), nakErr: test.nakErr}
|
||||
bus := &stageBusStub{fetches: []stageFetch{{message: message}, {err: errors.New("cancelled"), before: cancel}}}
|
||||
log, output := stageLoopLogger()
|
||||
delay, delays := noDelay(t)
|
||||
runMainLoopWithDelay(ctx, bus, log, delay)
|
||||
if message.nakCalls != 1 || message.ackCalls != 0 {
|
||||
t.Fatalf("nak/ack = (%d,%d), want (1,0)", message.nakCalls, message.ackCalls)
|
||||
}
|
||||
if got := strings.Contains(output.String(), "nak failed"); got != test.wantNakLog {
|
||||
t.Fatalf("nak failure logged = %v, want %v", got, test.wantNakLog)
|
||||
}
|
||||
// A Nak requests redelivery, so without a backoff the very next
|
||||
// FetchStage hands back the same failing message immediately --
|
||||
// a deterministic failure (e.g. a broken gate) then spins as a
|
||||
// tight log-flooding loop instead of backing off.
|
||||
if len(*delays) != 1 || (*delays)[0] != time.Second {
|
||||
t.Fatalf("delays = %v, want a single %v backoff before refetching a nak'd message", *delays, time.Second)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMainLoopAcksSuccessfulHandleAndContinues(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
ackErr error
|
||||
wantAckLog bool
|
||||
}{
|
||||
{name: "ack succeeds"},
|
||||
{name: "ack fails", ackErr: errors.New("ack unavailable"), wantAckLog: true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
message := &stageMessageStub{
|
||||
request: agentexec.AgentRequestMessage{Providers: map[string]agentexec.Provider{"test": {Class: "openai"}}},
|
||||
ackErr: test.ackErr,
|
||||
respondCancel: cancel,
|
||||
}
|
||||
bus := &stageBusStub{fetches: []stageFetch{{message: message}}}
|
||||
log, output := stageLoopLogger()
|
||||
delay, _ := noDelay(t)
|
||||
runMainLoopWithDelay(ctx, bus, log, delay)
|
||||
if message.respondCalls != 1 || message.ackCalls != 1 || message.nakCalls != 0 {
|
||||
t.Fatalf("respond/ack/nak = (%d,%d,%d), want (1,1,0)", message.respondCalls, message.ackCalls, message.nakCalls)
|
||||
}
|
||||
if got := strings.Contains(output.String(), "ack failed"); got != test.wantAckLog {
|
||||
t.Fatalf("ack failure logged = %v, want %v", got, test.wantAckLog)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -118,10 +118,16 @@ type taskDependencies struct {
|
||||
events agentexec.EventPublisher
|
||||
}
|
||||
|
||||
type runnerFactory func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner
|
||||
|
||||
func newTaskRunner(providers map[string]agentexec.Provider, log *slog.Logger) agentexec.Runner {
|
||||
return agentexec.NewLoopRunner(agentexec.NewRuntime(providers), log)
|
||||
}
|
||||
|
||||
// runTask builds a workflow.Registry from the container's mounted worktree,
|
||||
// routes and runs the entire workflow, and reports its terminal outcome.
|
||||
// Store remains archied's authority; Response.Task is a logging snapshot.
|
||||
func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependencies, newRunner agentexec.RunnerFactory, workDir string, log *slog.Logger) (*taskrun.Response, error) {
|
||||
func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependencies, newRunner runnerFactory, workDir string, log *slog.Logger) (*taskrun.Response, error) {
|
||||
registry, err := skillbuild.BuildRegistry(workDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build registry: %w", err)
|
||||
@@ -150,7 +156,7 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
|
||||
// Build a runner with MCP-discovered tools when available.
|
||||
var agent agentexec.Runner
|
||||
if mcpSet != nil && mcpSet.registry != nil {
|
||||
agent = agentexec.NewInProcessRunner(agentexec.NewRuntime(req.Providers), log, mcpSet.registry)
|
||||
agent = agentexec.NewLoopRunner(agentexec.NewRuntime(req.Providers), log, mcpSet.registry)
|
||||
} else {
|
||||
agent = newRunner(req.Providers, log)
|
||||
}
|
||||
|
||||
@@ -319,7 +319,7 @@ func TestRunTaskExecutesBootstrapWorkflowEndToEnd(t *testing.T) {
|
||||
t.Fatalf("prepare: %v", err)
|
||||
}
|
||||
req := taskrun.Request{Task: task, Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"}, Cfg: config.Config{}.ForTask()}
|
||||
fakeRunner := agentexec.RunnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner { return panicRunner{t} })
|
||||
fakeRunner := runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner { return panicRunner{t} })
|
||||
response, err := runTask(ctx, req, taskDependencies{forge: fg, store: st, trees: remote}, fakeRunner, hostDir, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatalf("runTask: %v", err)
|
||||
@@ -392,10 +392,7 @@ func TestExecuteTaskRequestUsesInfrastructureRPCDependencies(t *testing.T) {
|
||||
}
|
||||
t.Cleanup(unsubTrees)
|
||||
|
||||
transport, err := agentnats.Connect(ctx, agentnats.Config{
|
||||
URL: srv.ClientURL(),
|
||||
ConsumerName: "rpc-bootstrap-test",
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
transport, err := agentnats.Connect(ctx, agentnats.Config{URL: srv.ClientURL()}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -518,10 +515,7 @@ func TestExecuteTaskRequestForwardsWorkflowEventsOverNATS(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
transport, err := agentnats.Connect(ctx, agentnats.Config{
|
||||
URL: srv.ClientURL(),
|
||||
ConsumerName: "rpc-events-test",
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
transport, err := agentnats.Connect(ctx, agentnats.Config{URL: srv.ClientURL()}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
@@ -33,16 +32,14 @@ func (e *StartupError) Unwrap() error { return e.Err }
|
||||
type Settings struct {
|
||||
NATSURL string
|
||||
NATSToken string
|
||||
Consumer string
|
||||
WorkDir string
|
||||
}
|
||||
|
||||
type workerTransport interface {
|
||||
agentexec.StageConsumer
|
||||
Close()
|
||||
LogPublisher() agentexec.LogPublisher
|
||||
EventPublisher() agentexec.EventPublisher
|
||||
SubscribeTasks(context.Context, int64, bool, agentnats.TaskHandler, *slog.Logger) (agentnats.Subscription, error)
|
||||
SubscribeTasks(context.Context, int64, agentnats.TaskHandler, *slog.Logger) (agentnats.Subscription, error)
|
||||
Forger(string, time.Duration) workflow.Forger
|
||||
Store(time.Duration) store.WorkflowStore
|
||||
Trees(string, string, time.Duration) agentnats.RemoteTrees
|
||||
@@ -51,8 +48,8 @@ type workerTransport interface {
|
||||
type workerDependencies struct {
|
||||
connect func(context.Context, agentnats.Config, *slog.Logger) (workerTransport, error)
|
||||
markSafe func(context.Context, string, *slog.Logger) bool
|
||||
bootID func(string) (int64, bool, error)
|
||||
runLoop func(context.Context, stageBus, *slog.Logger) int
|
||||
bootID func(string) (int64, error)
|
||||
wait func(context.Context, *slog.Logger)
|
||||
}
|
||||
|
||||
func productionWorkerDependencies() workerDependencies {
|
||||
@@ -62,12 +59,15 @@ func productionWorkerDependencies() workerDependencies {
|
||||
},
|
||||
markSafe: agentgit.MarkSafe,
|
||||
bootID: agentboot.TaskID,
|
||||
runLoop: runMainLoop,
|
||||
wait: func(ctx context.Context, log *slog.Logger) {
|
||||
<-ctx.Done()
|
||||
log.Info("archie-agent shutting down")
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Run starts the worker, serves full-task and single-stage requests, and
|
||||
// returns after ctx cancellation or a startup failure.
|
||||
// Run starts the worker, serves full-task requests, and returns after ctx
|
||||
// cancellation or a startup failure.
|
||||
func Run(ctx context.Context, settings Settings, log *slog.Logger) error {
|
||||
return run(ctx, settings, log, productionWorkerDependencies())
|
||||
}
|
||||
@@ -84,31 +84,25 @@ func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies
|
||||
dependencies.markSafe(ctx, workDir, log)
|
||||
|
||||
transport, err := dependencies.connect(ctx, agentnats.Config{
|
||||
URL: settings.NATSURL,
|
||||
Token: settings.NATSToken,
|
||||
ConsumerName: settings.Consumer,
|
||||
URL: settings.NATSURL,
|
||||
Token: settings.NATSToken,
|
||||
}, log)
|
||||
if err != nil {
|
||||
operation := "nats connect failed"
|
||||
if errors.Is(err, agentnats.ErrCoreConnectionUnavailable) {
|
||||
operation = "nats connection unavailable"
|
||||
}
|
||||
log.Error(operation, "err", err)
|
||||
return &StartupError{Operation: operation, Err: err}
|
||||
}
|
||||
defer transport.Close()
|
||||
|
||||
taskID, dedicated, err := dependencies.bootID(workDir)
|
||||
taskID, err := dependencies.bootID(workDir)
|
||||
if err != nil {
|
||||
log.Error("task boot failed", "err", err)
|
||||
return &StartupError{Operation: "task boot failed", Err: err}
|
||||
}
|
||||
if dedicated {
|
||||
log = slog.New(agentexec.NewSystemLogHandler(log.Handler(), transport.LogPublisher(), taskID))
|
||||
log.Info("system log publisher attached", "task", taskID)
|
||||
}
|
||||
log = slog.New(agentexec.NewSystemLogHandler(log.Handler(), transport.LogPublisher(), taskID))
|
||||
log.Info("system log publisher attached", "task", taskID)
|
||||
|
||||
subscription, err := transport.SubscribeTasks(ctx, taskID, dedicated, func(ctx context.Context, request taskrun.Request) (*taskrun.Response, error) {
|
||||
subscription, err := transport.SubscribeTasks(ctx, taskID, func(ctx context.Context, request taskrun.Request) (*taskrun.Response, error) {
|
||||
return executeTaskRequest(ctx, request, transport, workDir, log)
|
||||
}, log)
|
||||
if err != nil {
|
||||
@@ -121,8 +115,8 @@ func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies
|
||||
}
|
||||
}()
|
||||
|
||||
log.Info("archie-agent ready", "nats", settings.NATSURL, "consumer", settings.Consumer)
|
||||
dependencies.runLoop(ctx, transport, log)
|
||||
log.Info("archie-agent ready", "nats", settings.NATSURL)
|
||||
dependencies.wait(ctx, log)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -141,7 +135,7 @@ func executeTaskRequest(ctx context.Context, request taskrun.Request, transport
|
||||
trees: transport.Trees(request.Task.Identity, request.WorktreeGrant, rpcTimeout),
|
||||
events: transport.EventPublisher(),
|
||||
}
|
||||
response, err := runTask(ctx, request, dependencies, agentexec.DefaultRunnerFactory, workDir, log)
|
||||
response, err := runTask(ctx, request, dependencies, newTaskRunner, workDir, log)
|
||||
if err != nil {
|
||||
log.Error("task run failed", "task", request.Task.ID, "err", err)
|
||||
return nil, err
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -41,12 +40,9 @@ type workerTransportStub struct {
|
||||
subCloseErr error
|
||||
publisher agentexec.LogPublisher
|
||||
eventPublisher agentexec.EventPublisher
|
||||
dedicated bool
|
||||
taskID int64
|
||||
}
|
||||
|
||||
func (*workerTransportStub) FetchStage(context.Context) (agentexec.StageMessage, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (t *workerTransportStub) Close() { *t.events = append(*t.events, "transport-close") }
|
||||
func (t *workerTransportStub) LogPublisher() agentexec.LogPublisher {
|
||||
*t.events = append(*t.events, "log-publisher")
|
||||
@@ -64,9 +60,9 @@ func (t *workerTransportStub) EventPublisher() agentexec.EventPublisher {
|
||||
return workerPublisher{}
|
||||
}
|
||||
|
||||
func (t *workerTransportStub) SubscribeTasks(_ context.Context, _ int64, dedicated bool, _ agentnats.TaskHandler, _ *slog.Logger) (agentnats.Subscription, error) {
|
||||
func (t *workerTransportStub) SubscribeTasks(_ context.Context, taskID int64, _ agentnats.TaskHandler, _ *slog.Logger) (agentnats.Subscription, error) {
|
||||
*t.events = append(*t.events, "subscribe")
|
||||
t.dedicated = dedicated
|
||||
t.taskID = taskID
|
||||
if t.subscribeErr != nil {
|
||||
return nil, t.subscribeErr
|
||||
}
|
||||
@@ -93,15 +89,15 @@ func TestRunPassesCompleteTransportConfiguration(t *testing.T) {
|
||||
got = config
|
||||
return transport, nil
|
||||
},
|
||||
bootID: func(string) (int64, bool, error) { return 0, false, nil },
|
||||
runLoop: func(context.Context, stageBus, *slog.Logger) int { cancel(); return 0 },
|
||||
bootID: func(string) (int64, error) { return 42, nil },
|
||||
wait: func(context.Context, *slog.Logger) { cancel() },
|
||||
}
|
||||
settings := Settings{NATSURL: "nats://test", NATSToken: "secret", Consumer: "worker", WorkDir: "/worktree"}
|
||||
settings := Settings{NATSURL: "nats://test", NATSToken: "secret", WorkDir: "/worktree"}
|
||||
if err := run(ctx, settings, slog.New(slog.DiscardHandler), dependencies); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.URL != settings.NATSURL || got.Token != settings.NATSToken || got.ConsumerName != settings.Consumer {
|
||||
t.Fatalf("connection config = %#v, want URL/token/consumer from settings", got)
|
||||
if got.URL != settings.NATSURL || got.Token != settings.NATSToken {
|
||||
t.Fatalf("connection config = %#v, want URL/token from settings", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,19 +113,18 @@ func TestRunPreservesStartupAndReverseCleanupOrder(t *testing.T) {
|
||||
events = append(events, "connect")
|
||||
return transport, nil
|
||||
},
|
||||
bootID: func(string) (int64, bool, error) {
|
||||
bootID: func(string) (int64, error) {
|
||||
events = append(events, "boot-id")
|
||||
return 42, true, nil
|
||||
return 42, nil
|
||||
},
|
||||
runLoop: func(context.Context, stageBus, *slog.Logger) int {
|
||||
events = append(events, "stage-loop")
|
||||
return 0
|
||||
wait: func(context.Context, *slog.Logger) {
|
||||
events = append(events, "wait")
|
||||
},
|
||||
}
|
||||
if err := run(t.Context(), Settings{NATSURL: "nats://test", Consumer: "worker", WorkDir: "/worktree"}, slog.New(slog.DiscardHandler), dependencies); err != nil {
|
||||
if err := run(t.Context(), Settings{NATSURL: "nats://test", WorkDir: "/worktree"}, slog.New(slog.DiscardHandler), dependencies); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{"git-safety", "connect", "boot-id", "log-publisher", "subscribe", "stage-loop", "subscription-close", "transport-close"}
|
||||
want := []string{"git-safety", "connect", "boot-id", "log-publisher", "subscribe", "wait", "subscription-close", "transport-close"}
|
||||
if len(events) != len(want) {
|
||||
t.Fatalf("events = %v, want %v", events, want)
|
||||
}
|
||||
@@ -138,8 +133,8 @@ func TestRunPreservesStartupAndReverseCleanupOrder(t *testing.T) {
|
||||
t.Fatalf("events = %v, want %v", events, want)
|
||||
}
|
||||
}
|
||||
if !transport.dedicated {
|
||||
t.Fatal("dedicated boot ID did not select dedicated subscription")
|
||||
if transport.taskID != 42 {
|
||||
t.Fatalf("subscription task ID = %d, want 42", transport.taskID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,11 +147,11 @@ func TestRunWarnsWhenNormalSubscriptionCleanupFails(t *testing.T) {
|
||||
connect: func(context.Context, agentnats.Config, *slog.Logger) (workerTransport, error) {
|
||||
return transport, nil
|
||||
},
|
||||
bootID: func(string) (int64, bool, error) { return 0, false, nil },
|
||||
runLoop: func(context.Context, stageBus, *slog.Logger) int { return 0 },
|
||||
bootID: func(string) (int64, error) { return 42, nil },
|
||||
wait: func(context.Context, *slog.Logger) {},
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := run(t.Context(), Settings{NATSURL: "nats://test", Consumer: "worker"}, slog.New(slog.NewTextHandler(&output, nil)), dependencies); err != nil {
|
||||
if err := run(t.Context(), Settings{NATSURL: "nats://test"}, slog.New(slog.NewTextHandler(&output, nil)), dependencies); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "task run unsubscribe failed") || !strings.Contains(output.String(), closeErr.Error()) {
|
||||
@@ -173,15 +168,13 @@ func TestRunStartupFailuresCleanUpOwnedResources(t *testing.T) {
|
||||
connectErr error
|
||||
bootErr error
|
||||
subscribeErr error
|
||||
dedicated bool
|
||||
wantErr error
|
||||
wantOperation string
|
||||
wantClosed bool
|
||||
}{
|
||||
{name: "connect", connectErr: connectErr, wantErr: connectErr, wantOperation: "nats connect failed"},
|
||||
{name: "core connection", connectErr: fmt.Errorf("%w: unavailable", agentnats.ErrCoreConnectionUnavailable), wantErr: agentnats.ErrCoreConnectionUnavailable, wantOperation: "nats connection unavailable"},
|
||||
{name: "boot metadata", bootErr: bootErr, wantErr: bootErr, wantOperation: "task boot failed", wantClosed: true},
|
||||
{name: "subscribe", subscribeErr: subscribeErr, dedicated: true, wantErr: subscribeErr, wantOperation: "taskrun subscribe failed", wantClosed: true},
|
||||
{name: "subscribe", subscribeErr: subscribeErr, wantErr: subscribeErr, wantOperation: "taskrun subscribe failed", wantClosed: true},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
@@ -193,19 +186,16 @@ func TestRunStartupFailuresCleanUpOwnedResources(t *testing.T) {
|
||||
connect: func(context.Context, agentnats.Config, *slog.Logger) (workerTransport, error) {
|
||||
return transport, test.connectErr
|
||||
},
|
||||
bootID: func(string) (int64, bool, error) {
|
||||
bootID: func(string) (int64, error) {
|
||||
if test.bootErr != nil {
|
||||
return 0, false, test.bootErr
|
||||
return 0, test.bootErr
|
||||
}
|
||||
if test.dedicated {
|
||||
return 42, true, nil
|
||||
}
|
||||
return 0, false, nil
|
||||
return 42, nil
|
||||
},
|
||||
runLoop: func(context.Context, stageBus, *slog.Logger) int { t.Fatal("stage loop called"); return 0 },
|
||||
wait: func(context.Context, *slog.Logger) { t.Fatal("wait called") },
|
||||
}
|
||||
var output bytes.Buffer
|
||||
err := run(t.Context(), Settings{NATSURL: "nats://test", Consumer: "worker"}, slog.New(slog.NewTextHandler(&output, nil)), dependencies)
|
||||
err := run(t.Context(), Settings{NATSURL: "nats://test"}, slog.New(slog.NewTextHandler(&output, nil)), dependencies)
|
||||
if !errors.Is(err, test.wantErr) {
|
||||
t.Fatalf("error = %v, want %v", err, test.wantErr)
|
||||
}
|
||||
|
||||
@@ -20,10 +20,10 @@ func TestConfigCloneDeepCopiesReferenceFields(t *testing.T) {
|
||||
MCPServers: []MCPServer{{Headers: map[string]string{"A": "b"}, Args: []string{"x"}}},
|
||||
WebFetch: WebFetchConfig{Enabled: &enabled},
|
||||
},
|
||||
Memory: MemoryConfig{ProviderConfig: map[string]string{"k": "v"}},
|
||||
Chat: ChatConfig{Telegram: TelegramConfig{AllowedUserIDs: []int64{1}}},
|
||||
Agent: Agent{Env: []string{"HOME"}},
|
||||
Extra: map[string]any{"custom": 1},
|
||||
Memory: MemoryConfig{ProviderConfig: map[string]string{"k": "v"}},
|
||||
Chat: ChatConfig{Telegram: TelegramConfig{AllowedUserIDs: []int64{1}}},
|
||||
LegacyAgent: LegacyAgent{Env: []string{"HOME"}},
|
||||
Extra: map[string]any{"custom": 1},
|
||||
}
|
||||
|
||||
got := orig.Clone()
|
||||
@@ -37,7 +37,7 @@ func TestConfigCloneDeepCopiesReferenceFields(t *testing.T) {
|
||||
got.Tools.MCPServers[0].Args[0] = "changed"
|
||||
got.Memory.ProviderConfig["k"] = "changed"
|
||||
got.Chat.Telegram.AllowedUserIDs[0] = 99
|
||||
got.Agent.Env[0] = "changed"
|
||||
got.LegacyAgent.Env[0] = "changed"
|
||||
got.Extra["custom"] = 2
|
||||
*got.Tools.WebFetch.Enabled = false
|
||||
|
||||
@@ -62,7 +62,7 @@ func TestConfigCloneDeepCopiesReferenceFields(t *testing.T) {
|
||||
if orig.Chat.Telegram.AllowedUserIDs[0] != 1 {
|
||||
t.Error("Telegram.AllowedUserIDs is shared")
|
||||
}
|
||||
if orig.Agent.Env[0] != "HOME" {
|
||||
if orig.LegacyAgent.Env[0] != "HOME" {
|
||||
t.Error("Agent.Env is shared")
|
||||
}
|
||||
if orig.Extra["custom"] != 1 {
|
||||
|
||||
+12
-13
@@ -176,15 +176,14 @@ type Provider struct {
|
||||
BaseURL string `toml:"base_url" yaml:"base_url" json:"base_url"`
|
||||
}
|
||||
|
||||
// Agent configures how archied executes autonomous stages.
|
||||
type Agent struct {
|
||||
// Mode is "inprocess" during migration or "subprocess" to invoke the
|
||||
// standalone archie-agent worker for every autonomous stage.
|
||||
Mode string `toml:"mode" yaml:"mode"`
|
||||
// Command is the archie-agent executable path used in subprocess mode.
|
||||
Command string `toml:"command" yaml:"command"`
|
||||
// Env adds environment variable names to the worker allowlist.
|
||||
Env []string `toml:"env" yaml:"env"`
|
||||
// LegacyAgent decodes the removed [agent] section so existing operator files
|
||||
// continue to load during migration. Its fields have no runtime consumers:
|
||||
// autonomous work always uses a task-scoped archie-agent container over NATS.
|
||||
// Remove the section from new and updated configurations.
|
||||
type LegacyAgent struct {
|
||||
Mode string `toml:"mode" yaml:"mode"`
|
||||
Command string `toml:"command" yaml:"command"`
|
||||
Env []string `toml:"env" yaml:"env"`
|
||||
}
|
||||
|
||||
// Forge intake modes for Forge.Intake. Poll is the default; webhook reacts to
|
||||
@@ -414,8 +413,8 @@ type Config struct {
|
||||
// model ref ("provider/model").
|
||||
Models map[string]string `toml:"models" yaml:"models"`
|
||||
|
||||
Providers map[string]Provider `toml:"providers" yaml:"providers"`
|
||||
Agent Agent `toml:"agent" yaml:"agent"`
|
||||
Providers map[string]Provider `toml:"providers" yaml:"providers"`
|
||||
LegacyAgent LegacyAgent `toml:"agent" yaml:"agent"`
|
||||
|
||||
Budgets Budgets `toml:"budgets" yaml:"budgets"`
|
||||
Web Web `toml:"web" yaml:"web"`
|
||||
@@ -540,7 +539,7 @@ func (c Config) Clone() Config {
|
||||
c.Models = cloneStringMap(c.Models)
|
||||
c.Providers = maps.Clone(c.Providers)
|
||||
c.Dispatch.Labels = cloneStringMap(c.Dispatch.Labels)
|
||||
c.Agent.Env = append([]string(nil), c.Agent.Env...)
|
||||
c.LegacyAgent.Env = append([]string(nil), c.LegacyAgent.Env...)
|
||||
c.Repos = cloneRepos(c.Repos)
|
||||
c.Identities = cloneIdentities(c.Identities)
|
||||
c.Chat.Models = append([]string(nil), c.Chat.Models...)
|
||||
@@ -669,7 +668,7 @@ type CaptureConfig struct {
|
||||
|
||||
// ContainerConfig configures Docker sandbox execution of archie-agent.
|
||||
type ContainerConfig struct {
|
||||
// Enabled activates container execution. Requires agent.mode = "nats".
|
||||
// Enabled activates autonomous workflow execution in managed containers.
|
||||
Enabled bool `toml:"enabled" yaml:"enabled"`
|
||||
// Image is the Docker image to run (e.g. "ghcr.io/sam/archie-agent:latest").
|
||||
Image string `toml:"image" yaml:"image"`
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Package container manages Docker containers running archie-agent.
|
||||
// A Pool acquires and releases containers per task, handling image pull,
|
||||
// creation, startup, health check, and teardown. The daemon's existing
|
||||
// NATSRunner handles agent communication unchanged.
|
||||
// creation, startup, health check, and teardown. The daemon hands each task to
|
||||
// its container as one core-NATS request.
|
||||
package container
|
||||
|
||||
import (
|
||||
|
||||
@@ -74,3 +74,37 @@ func reportLocalWorkflowRun(t *testing.T, fileset *token.FileSet, call *ast.Call
|
||||
position := fileset.Position(call.Pos())
|
||||
t.Errorf("%s calls workflow.Run; autonomous workflows must use the full-task worker handoff", position)
|
||||
}
|
||||
|
||||
// Daemon and IdentityRunner carry routing and authority state only. Keeping a
|
||||
// stage runner or workflow registry on either type makes a host execution path
|
||||
// one call away from returning.
|
||||
func TestDaemonCarriesNoLocalWorkflowExecutionState(t *testing.T) {
|
||||
file, err := parser.ParseFile(token.NewFileSet(), "daemon.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
forbidden := map[string]bool{"Agent": true, "Workflows": true, "CustomStages": true}
|
||||
for _, declaration := range file.Decls {
|
||||
gen, ok := declaration.(*ast.GenDecl)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, spec := range gen.Specs {
|
||||
typeSpec, ok := spec.(*ast.TypeSpec)
|
||||
if !ok || (typeSpec.Name.Name != "Daemon" && typeSpec.Name.Name != "IdentityRunner") {
|
||||
continue
|
||||
}
|
||||
structure, ok := typeSpec.Type.(*ast.StructType)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, field := range structure.Fields.List {
|
||||
for _, name := range field.Names {
|
||||
if forbidden[name.Name] {
|
||||
t.Errorf("%s carries local workflow execution field %s", typeSpec.Name.Name, name.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
"github.com/samcharles93/archie-core/internal/domain/curator"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
@@ -72,9 +71,7 @@ type Daemon struct {
|
||||
Store store.TaskStore
|
||||
Forge forge.Forge
|
||||
Trees *worktree.Manager
|
||||
Agent agentexec.Runner
|
||||
Bus *events.Bus
|
||||
Workflows workflow.Registry
|
||||
Log *slog.Logger
|
||||
// Tasks is the optional message bus for task distribution. Nil means no
|
||||
// bus is configured; the existing SQLite ClaimNext flow is used.
|
||||
@@ -83,7 +80,7 @@ type Daemon struct {
|
||||
// TaskRunReadyTimeout bounds how long runViaAgent retries an initial
|
||||
// taskrun request that fails with nats.ErrNoResponders, giving a
|
||||
// freshly spawned archie-agent container time to connect to NATS, set
|
||||
// up its JetStream stream/consumer, and subscribe before the daemon
|
||||
// up its task-scoped core-NATS subscription, and become reachable before the daemon
|
||||
// gives up and parks the task. ContainerPool.Acquire returns as soon
|
||||
// as Docker has issued the start syscall, not once that setup
|
||||
// finishes, so without this bound the very first request after a
|
||||
@@ -112,11 +109,6 @@ type Daemon struct {
|
||||
// lifecycle. Typed capability registries remain in their domain packages;
|
||||
// the host never exposes daemon internals or an untyped service locator.
|
||||
CapabilityHost *plugin.Host
|
||||
// CustomStages discovers a repo's per-repo Yaegi custom stages
|
||||
// (.archie/stages/*.go) from its prepared worktree. Set by the
|
||||
// composition root (cmd/archied) to wfeval.Discover; nil disables
|
||||
// discovery.
|
||||
CustomStages func(dir string) ([]workflow.Stage, error)
|
||||
// Curators is the curator engine family registry (epic archie-core-yp9).
|
||||
// The runtime loop (archie-core-89x) and reference curators (i7i, gs8)
|
||||
// are driven through it; nil when the family is not composed.
|
||||
@@ -163,9 +155,7 @@ type IdentityRunner struct {
|
||||
// Cfg is the identity-scoped config subset (forge, models, dispatch,
|
||||
// budgets, etc.).
|
||||
Cfg config.IdentityConfig
|
||||
// Agent is built from this identity's provider credentials.
|
||||
Agent agentexec.Runner
|
||||
Log *slog.Logger
|
||||
Log *slog.Logger
|
||||
}
|
||||
|
||||
// NewIdentityRunner constructs an IdentityRunner from an identity config
|
||||
@@ -1081,8 +1071,8 @@ func (d *Daemon) taskRunRetryBackoff() time.Duration {
|
||||
// requestTaskRun publishes the taskrun request and retries while no
|
||||
// archie-agent has subscribed yet (nats.ErrNoResponders): the container
|
||||
// pool's Acquire returns as soon as Docker has issued the start syscall,
|
||||
// not once the spawned container has connected to NATS, set up its
|
||||
// JetStream stream/consumer, and subscribed to this per-task subject --
|
||||
// not once the spawned container has connected to core NATS and subscribed
|
||||
// to this per-task subject --
|
||||
// a gap of hundreds of milliseconds to a few seconds that would otherwise
|
||||
// fail the very first request deterministically, every time, on every
|
||||
// task. Any error other than ErrNoResponders (encode failures, a context
|
||||
@@ -1140,16 +1130,11 @@ func (d *Daemon) containerEnv(task *store.Task) []string {
|
||||
return env
|
||||
}
|
||||
|
||||
func (d *Daemon) executionFor(task *store.Task) (config.Config, agentexec.Runner) {
|
||||
if id := d.identityFor(task); id != nil {
|
||||
return configForIdentity(d.Cfg.Get(), id.Cfg), id.Agent
|
||||
}
|
||||
return d.Cfg.Get(), d.Agent
|
||||
}
|
||||
|
||||
func (d *Daemon) configFor(task *store.Task) config.Config {
|
||||
cfg, _ := d.executionFor(task)
|
||||
return cfg
|
||||
if id := d.identityFor(task); id != nil {
|
||||
return configForIdentity(d.Cfg.Get(), id.Cfg)
|
||||
}
|
||||
return d.Cfg.Get()
|
||||
}
|
||||
|
||||
func configForIdentity(root config.Config, identity config.IdentityConfig) config.Config {
|
||||
|
||||
@@ -16,7 +16,6 @@ import (
|
||||
natssrv "github.com/nats-io/nats-server/v2/test"
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
@@ -36,12 +35,6 @@ type cleanupStorage struct {
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
type identityRunnerStub struct{}
|
||||
|
||||
func (*identityRunnerStub) Run(context.Context, string, agentexec.Request, agentexec.ToolCallReporter) (agentexec.Result, error) {
|
||||
return agentexec.Result{}, nil
|
||||
}
|
||||
|
||||
func (s *cleanupStorage) Setup(context.Context, storage.TaskRef) ([]storage.Mount, error) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -479,7 +472,7 @@ func startEmbeddedNATSForDaemon(t *testing.T) *server.Server {
|
||||
func daemonWithNATS(t *testing.T) (*Daemon, *store.Store, *arnats.Client) {
|
||||
t.Helper()
|
||||
srv := startEmbeddedNATSForDaemon(t)
|
||||
client, err := arnats.Connect(context.Background(), arnats.Config{URL: srv.ClientURL(), Subjects: []string{workintake.SubjectTaskWildcard, agentexec.SubjectAgentWildcard}, FilterSubject: workintake.SubjectTaskWildcard}, slog.New(slog.DiscardHandler))
|
||||
client, err := arnats.Connect(context.Background(), arnats.Config{URL: srv.ClientURL(), Subjects: []string{workintake.SubjectTaskWildcard}, FilterSubject: workintake.SubjectTaskWildcard}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatalf("nats connect: %v", err)
|
||||
}
|
||||
@@ -1023,9 +1016,7 @@ func TestNewIdentityRunnerRejectsEmptyName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecutionForUsesIdentityConfigAgentAndProvider(t *testing.T) {
|
||||
rootAgent := &identityRunnerStub{}
|
||||
identityAgent := &identityRunnerStub{}
|
||||
func TestConfigForUsesIdentityConfigAndProvider(t *testing.T) {
|
||||
d := &Daemon{
|
||||
Cfg: config.NewHolder(config.Config{
|
||||
Models: map[string]string{"builder": "root/model"},
|
||||
@@ -1033,9 +1024,8 @@ func TestExecutionForUsesIdentityConfigAgentAndProvider(t *testing.T) {
|
||||
"root": {Class: "openai", APIKeyEnv: "ROOT_KEY"},
|
||||
},
|
||||
}),
|
||||
Agent: rootAgent,
|
||||
Identities: []*IdentityRunner{{
|
||||
Name: "worker", Agent: identityAgent,
|
||||
Name: "worker",
|
||||
Cfg: config.IdentityConfig{
|
||||
Name: "worker", Models: map[string]string{"builder": "worker/model"},
|
||||
Providers: map[string]config.Provider{
|
||||
@@ -1045,10 +1035,7 @@ func TestExecutionForUsesIdentityConfigAgentAndProvider(t *testing.T) {
|
||||
}},
|
||||
}
|
||||
|
||||
cfg, runner := d.executionFor(&store.Task{Identity: "worker"})
|
||||
if runner != identityAgent {
|
||||
t.Fatal("executionFor returned root agent for identity task")
|
||||
}
|
||||
cfg := d.configFor(&store.Task{Identity: "worker"})
|
||||
if cfg.Models["builder"] != "worker/model" {
|
||||
t.Fatalf("builder model = %q", cfg.Models["builder"])
|
||||
}
|
||||
|
||||
@@ -17,10 +17,6 @@ var (
|
||||
// never established.
|
||||
ErrNotConnected = errors.New("eventbus: not connected")
|
||||
|
||||
// ErrNoReplyAddress reports a message carrying nowhere to send a reply,
|
||||
// so a request cannot be answered.
|
||||
ErrNoReplyAddress = errors.New("eventbus: message has no reply address")
|
||||
|
||||
// ErrNoResponders reports a request sent to a subject nobody is
|
||||
// listening on. It is usually transient -- a worker that has connected
|
||||
// but not yet subscribed -- so callers typically retry within a deadline
|
||||
@@ -37,11 +33,6 @@ type Message interface {
|
||||
// Subject returns the address the message arrived on.
|
||||
Subject() string
|
||||
|
||||
// ReplyAddress returns where a response should be sent, or
|
||||
// ErrNoReplyAddress when the sender expects no reply. How the address
|
||||
// travels is the implementation's concern.
|
||||
ReplyAddress() (string, error)
|
||||
|
||||
// Ack marks the message handled so it is not redelivered.
|
||||
Ack() error
|
||||
|
||||
@@ -62,21 +53,6 @@ type Publisher interface {
|
||||
// rediscovers the same issue on every poll, so at-least-once delivery
|
||||
// without deduplication would enqueue it repeatedly.
|
||||
PublishUnique(ctx context.Context, subject, idempotencyKey string, payload []byte) error
|
||||
|
||||
// PublishRequest sends payload to subject, recording replyTo as the
|
||||
// address for the response. It reports ErrNoReplyAddress when replyTo is
|
||||
// empty, since a request nobody can answer is a caller error rather than
|
||||
// a fire-and-forget publish.
|
||||
PublishRequest(ctx context.Context, subject, replyTo string, payload []byte) error
|
||||
|
||||
// Respond answers a request on the address taken from
|
||||
// Message.ReplyAddress.
|
||||
//
|
||||
// This is distinct from Publish, not a convenience over it: a reply
|
||||
// address is an ephemeral inbox belonging to one waiting caller, not a
|
||||
// durable subject. Sending a reply through the durable path fails,
|
||||
// because the inbox is not part of any stream.
|
||||
Respond(ctx context.Context, replyAddress string, payload []byte) error
|
||||
}
|
||||
|
||||
// Consumer receives messages.
|
||||
@@ -94,24 +70,6 @@ type Consumer interface {
|
||||
type Requester interface {
|
||||
// Request sends payload to subject and waits for a single response.
|
||||
Request(ctx context.Context, subject string, payload []byte) ([]byte, error)
|
||||
|
||||
// NewReplyInbox creates an address for one expected response. Use it when
|
||||
// the reply must be awaited separately from sending, which Request cannot
|
||||
// express -- publishing work and collecting its result are distinct steps
|
||||
// when the responder may not exist yet.
|
||||
NewReplyInbox() (ReplyInbox, error)
|
||||
}
|
||||
|
||||
// ReplyInbox is a one-shot address awaiting a single response.
|
||||
type ReplyInbox interface {
|
||||
// Subject is the address to advertise to the responder.
|
||||
Subject() string
|
||||
|
||||
// Next blocks until the reply arrives or ctx is done.
|
||||
Next(ctx context.Context) ([]byte, error)
|
||||
|
||||
// Close releases the inbox.
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Bus is the full contract. Depend on the narrowest interface that fits:
|
||||
|
||||
@@ -2,9 +2,7 @@ package agentboot
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@@ -12,25 +10,22 @@ import (
|
||||
)
|
||||
|
||||
// TaskID reads the daemon-owned boot brief at <mountDir>/.git/task.json.
|
||||
// An absent brief selects shared-worker mode. Any other read error, malformed
|
||||
// JSON, or non-positive task ID is a startup error: a container with invalid
|
||||
// dedicated-task metadata must never join the shared queue.
|
||||
func TaskID(mountDir string) (taskID int64, dedicated bool, err error) {
|
||||
// The brief is mandatory: every worker belongs to exactly one daemon-created
|
||||
// task container, so missing, unreadable, malformed, or invalid metadata is a
|
||||
// startup error rather than permission to consume work for another task.
|
||||
func TaskID(mountDir string) (int64, error) {
|
||||
path := filepath.Join(mountDir, ".git", "task.json")
|
||||
data, err := os.ReadFile(path)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return 0, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, false, fmt.Errorf("read %s: %w", path, err)
|
||||
return 0, fmt.Errorf("read mandatory task boot metadata %s: %w", path, err)
|
||||
}
|
||||
|
||||
var payload container.TaskPayload
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return 0, false, fmt.Errorf("decode %s: %w", path, err)
|
||||
return 0, fmt.Errorf("decode %s: %w", path, err)
|
||||
}
|
||||
if payload.ID <= 0 {
|
||||
return 0, false, fmt.Errorf("decode %s: task ID must be positive, got %d", path, payload.ID)
|
||||
return 0, fmt.Errorf("decode %s: task ID must be positive, got %d", path, payload.ID)
|
||||
}
|
||||
return payload.ID, true, nil
|
||||
return payload.ID, nil
|
||||
}
|
||||
@@ -18,17 +18,16 @@ func bootGitDir(t *testing.T) string {
|
||||
return dir
|
||||
}
|
||||
|
||||
func TestTaskIDDistinguishesSharedDedicatedAndInvalidBoots(t *testing.T) {
|
||||
func TestTaskIDRequiresValidDedicatedTaskMetadata(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload []byte
|
||||
write bool
|
||||
wantID int64
|
||||
wantOK bool
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "absent uses shared mode"},
|
||||
{name: "valid uses dedicated mode", payload: mustTaskPayload(t, container.TaskPayload{ID: 42}), write: true, wantID: 42, wantOK: true},
|
||||
{name: "absent fails closed", wantErr: true},
|
||||
{name: "valid selects task", payload: mustTaskPayload(t, container.TaskPayload{ID: 42}), write: true, wantID: 42},
|
||||
{name: "malformed fails closed", payload: []byte("not json"), write: true, wantErr: true},
|
||||
{name: "zero ID fails closed", payload: mustTaskPayload(t, container.TaskPayload{}), write: true, wantErr: true},
|
||||
{name: "negative ID fails closed", payload: mustTaskPayload(t, container.TaskPayload{ID: -1}), write: true, wantErr: true},
|
||||
@@ -41,12 +40,12 @@ func TestTaskIDDistinguishesSharedDedicatedAndInvalidBoots(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
id, ok, err := TaskID(dir)
|
||||
id, err := TaskID(dir)
|
||||
if (err != nil) != test.wantErr {
|
||||
t.Fatalf("TaskID() error = %v, wantErr %v", err, test.wantErr)
|
||||
}
|
||||
if id != test.wantID || ok != test.wantOK {
|
||||
t.Fatalf("TaskID() = (%d, %v), want (%d, %v)", id, ok, test.wantID, test.wantOK)
|
||||
if id != test.wantID {
|
||||
t.Fatalf("TaskID() ID = %d, want %d", id, test.wantID)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -59,12 +58,12 @@ func TestTaskIDFailsClosedOnNonMissingReadError(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
id, dedicated, err := TaskID(dir)
|
||||
id, err := TaskID(dir)
|
||||
if err == nil {
|
||||
t.Fatal("TaskID() error = nil for unreadable task.json path")
|
||||
}
|
||||
if id != 0 || dedicated {
|
||||
t.Fatalf("TaskID() = (%d, %v), want (0, false)", id, dedicated)
|
||||
if id != 0 {
|
||||
t.Fatalf("TaskID() ID = %d, want 0", id)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,9 +35,10 @@ func MarkSafe(ctx context.Context, mountDir string, log *slog.Logger) bool {
|
||||
// the agent continues. Returns true only when git accepted the configuration.
|
||||
//
|
||||
// The guard is existence, not mount-ness: os.Stat confirms mountDir exists and
|
||||
// is a directory, which is what stops archie-agent running directly on a host
|
||||
// (shared queue-group mode, no container) from writing to the operator's own
|
||||
// ~/.gitconfig for a path that was never created at all. It does not detect a
|
||||
// is a directory, which prevents a misconfigured archie-agent from writing to
|
||||
// the operator's own ~/.gitconfig for a workspace path that was never mounted.
|
||||
// Mandatory task boot metadata independently rejects non-task workers before
|
||||
// they subscribe. This check does not detect a
|
||||
// stale directory that happens to exist at the same path without actually
|
||||
// being a bind mount -- a real mount check would need platform-specific
|
||||
// parsing (e.g. /proc/self/mountinfo on Linux) that this deliberately does
|
||||
|
||||
@@ -5,7 +5,6 @@ package nats
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
@@ -16,152 +15,69 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
busnats "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/storerpc"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/worktreerpc"
|
||||
)
|
||||
|
||||
// Config contains the process and deployment inputs for the worker transport.
|
||||
// Stream subjects, filtering, and delivery timing are transport invariants.
|
||||
// Config contains the broker endpoint and credential for the worker transport.
|
||||
type Config struct {
|
||||
URL string
|
||||
Token string
|
||||
ConsumerName string
|
||||
URL string
|
||||
Token string
|
||||
}
|
||||
|
||||
const (
|
||||
stagePollTimeout = 5 * time.Second
|
||||
stageAckWait = 30 * time.Minute
|
||||
taskSubscriptionFlushTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
// ErrCoreConnectionUnavailable identifies failure to obtain the core-NATS
|
||||
// connection after the durable bus connected successfully.
|
||||
var ErrCoreConnectionUnavailable = errors.New("core NATS connection unavailable")
|
||||
|
||||
type sdkSubscription interface {
|
||||
Unsubscribe() error
|
||||
}
|
||||
|
||||
// Transport owns the worker's durable event bus and its shared core-NATS connection.
|
||||
// Transport owns the worker's core-NATS connection. Full-task handoff, RPC,
|
||||
// logs, and events are all request/reply or fire-and-forget core subjects; the
|
||||
// worker has no JetStream consumer.
|
||||
type Transport struct {
|
||||
bus *busnats.Client
|
||||
conn *natsio.Conn
|
||||
|
||||
subscribe func(string, natsio.MsgHandler) (sdkSubscription, error)
|
||||
queueSubscribe func(string, string, natsio.MsgHandler) (sdkSubscription, error)
|
||||
flush func(time.Duration) error
|
||||
subscribe func(string, natsio.MsgHandler) (sdkSubscription, error)
|
||||
flush func(time.Duration) error
|
||||
}
|
||||
|
||||
type busConnector func(context.Context, busnats.Config, *slog.Logger) (*busnats.Client, error)
|
||||
|
||||
// Connect establishes both worker transports over one NATS connection.
|
||||
// Connect establishes the worker's core-NATS connection.
|
||||
func Connect(ctx context.Context, config Config, log *slog.Logger) (*Transport, error) {
|
||||
return connect(ctx, config, log, busnats.Connect)
|
||||
}
|
||||
|
||||
func connect(ctx context.Context, config Config, log *slog.Logger, connectBus busConnector) (*Transport, error) {
|
||||
bus, err := connectBus(ctx, busnats.Config{
|
||||
URL: config.URL,
|
||||
Token: config.Token,
|
||||
Subjects: []string{workintake.SubjectTaskWildcard, agentexec.SubjectAgentWildcard},
|
||||
ConsumerName: config.ConsumerName,
|
||||
FilterSubject: agentexec.SubjectAgentWildcard,
|
||||
PollTimeout: stagePollTimeout,
|
||||
AckWait: stageAckWait,
|
||||
}, log)
|
||||
if err != nil {
|
||||
if log == nil {
|
||||
log = slog.New(slog.DiscardHandler)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transportFromBus(bus)
|
||||
}
|
||||
|
||||
func transportFromBus(bus *busnats.Client) (*Transport, error) {
|
||||
conn, err := bus.CoreConn()
|
||||
if err != nil {
|
||||
bus.Close()
|
||||
return nil, fmt.Errorf("%w: %w", ErrCoreConnectionUnavailable, err)
|
||||
options := []natsio.Option{natsio.Name("archie-agent")}
|
||||
if config.Token != "" {
|
||||
options = append(options, natsio.Token(config.Token))
|
||||
}
|
||||
conn, err := natsio.Connect(config.URL, options...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("connect core NATS: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
log.Info("worker transport connected", "url", conn.ConnectedUrl())
|
||||
return &Transport{
|
||||
bus: bus,
|
||||
conn: conn,
|
||||
subscribe: func(subject string, handler natsio.MsgHandler) (sdkSubscription, error) {
|
||||
return conn.Subscribe(subject, handler)
|
||||
},
|
||||
queueSubscribe: func(subject, queue string, handler natsio.MsgHandler) (sdkSubscription, error) {
|
||||
return conn.QueueSubscribe(subject, queue, handler)
|
||||
},
|
||||
flush: conn.FlushTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Close releases all broker resources.
|
||||
func (t *Transport) Close() { t.bus.Close() }
|
||||
|
||||
type stageMessage struct {
|
||||
wire eventbus.Message
|
||||
request agentexec.AgentRequestMessage
|
||||
requestErr error
|
||||
replyAddress string
|
||||
encode func(any) ([]byte, error)
|
||||
respond func(context.Context, string, []byte) error
|
||||
}
|
||||
|
||||
var _ agentexec.StageMessage = (*stageMessage)(nil)
|
||||
|
||||
// FetchStage receives and decodes the next durable single-stage request while
|
||||
// retaining its reply inbox and acknowledgement controls inside infrastructure.
|
||||
func (t *Transport) FetchStage(ctx context.Context) (agentexec.StageMessage, error) {
|
||||
wire, err := t.bus.Fetch(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newStageMessage(wire, t.bus.Respond), nil
|
||||
}
|
||||
|
||||
func newStageMessage(wire eventbus.Message, respond func(context.Context, string, []byte) error) agentexec.StageMessage {
|
||||
message := &stageMessage{wire: wire, encode: json.Marshal, respond: respond}
|
||||
if err := json.Unmarshal(wire.Data(), &message.request); err != nil {
|
||||
message.requestErr = fmt.Errorf("decode request: %w", err)
|
||||
return message
|
||||
}
|
||||
var err error
|
||||
message.replyAddress, err = wire.ReplyAddress()
|
||||
if err != nil {
|
||||
message.requestErr = fmt.Errorf("stage request on %s: %w", wire.Subject(), err)
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
func (m *stageMessage) Request() (agentexec.AgentRequestMessage, error) {
|
||||
return m.request, m.requestErr
|
||||
}
|
||||
|
||||
func (m *stageMessage) Respond(ctx context.Context, response *agentexec.AgentResponseEnvelope) error {
|
||||
data, err := m.encode(response)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode response: %w", err)
|
||||
}
|
||||
if err := m.respond(ctx, m.replyAddress, data); err != nil {
|
||||
return fmt.Errorf("publish response: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *stageMessage) Ack() error { return m.wire.Ack() }
|
||||
func (m *stageMessage) Nak() error { return m.wire.Nak() }
|
||||
|
||||
func (m *stageMessage) LogAttributes() []any {
|
||||
if m.replyAddress == "" {
|
||||
return nil
|
||||
}
|
||||
return []any{"reply_to", m.replyAddress}
|
||||
}
|
||||
func (t *Transport) Close() { t.conn.Close() }
|
||||
|
||||
// LogPublisher returns the narrow fire-and-forget capability used by system logs.
|
||||
func (t *Transport) LogPublisher() agentexec.LogPublisher { return t.conn }
|
||||
@@ -208,9 +124,7 @@ type subscription struct{ sub sdkSubscription }
|
||||
func (s subscription) Close() error { return s.sub.Unsubscribe() }
|
||||
|
||||
const (
|
||||
taskRunSubjectPrefix = "archie.taskrun."
|
||||
taskRunSubjectWildcard = taskRunSubjectPrefix + ">"
|
||||
taskRunQueueGroup = "archie-taskrun-workers"
|
||||
taskRunSubjectPrefix = "archie.taskrun."
|
||||
)
|
||||
|
||||
// SubjectForTask returns the canonical core-NATS subject for one full-task handoff.
|
||||
@@ -230,38 +144,23 @@ func taskIDFromSubject(subject string) (int64, error) {
|
||||
return taskID, nil
|
||||
}
|
||||
|
||||
// SubscribeTasks selects a dedicated task subject when a boot ID is present,
|
||||
// otherwise the shared worker queue. Wire decoding and inbox replies remain here.
|
||||
func (t *Transport) SubscribeTasks(ctx context.Context, taskID int64, dedicated bool, handler TaskHandler, log *slog.Logger) (Subscription, error) {
|
||||
callback := func(msg *natsio.Msg) { t.handleTask(ctx, msg, taskID, dedicated, handler, log) }
|
||||
// SubscribeTasks serves the one task named by the worker's mandatory boot
|
||||
// metadata. Wire decoding, task correlation, and inbox replies remain here.
|
||||
func (t *Transport) SubscribeTasks(ctx context.Context, taskID int64, handler TaskHandler, log *slog.Logger) (Subscription, error) {
|
||||
callback := func(msg *natsio.Msg) { t.handleTask(ctx, msg, taskID, handler, log) }
|
||||
subscribe := t.subscribe
|
||||
if subscribe == nil {
|
||||
subscribe = func(subject string, handler natsio.MsgHandler) (sdkSubscription, error) {
|
||||
return t.conn.Subscribe(subject, handler)
|
||||
}
|
||||
}
|
||||
queueSubscribe := t.queueSubscribe
|
||||
if queueSubscribe == nil {
|
||||
queueSubscribe = func(subject, queue string, handler natsio.MsgHandler) (sdkSubscription, error) {
|
||||
return t.conn.QueueSubscribe(subject, queue, handler)
|
||||
}
|
||||
}
|
||||
flush := t.flush
|
||||
if flush == nil {
|
||||
flush = t.conn.FlushTimeout
|
||||
}
|
||||
|
||||
var (
|
||||
sub sdkSubscription
|
||||
err error
|
||||
)
|
||||
if dedicated {
|
||||
log.Info("taskrun: dedicated per-task subscription", "task", taskID)
|
||||
sub, err = subscribe(SubjectForTask(taskID), callback)
|
||||
} else {
|
||||
log.Info("taskrun: shared queue-group subscription (no .git/task.json found)")
|
||||
sub, err = queueSubscribe(taskRunSubjectWildcard, taskRunQueueGroup, callback)
|
||||
}
|
||||
log.Info("taskrun: dedicated per-task subscription", "task", taskID)
|
||||
sub, err := subscribe(SubjectForTask(taskID), callback)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -278,7 +177,6 @@ func (t *Transport) handleTask(
|
||||
ctx context.Context,
|
||||
msg *natsio.Msg,
|
||||
bootTaskID int64,
|
||||
dedicated bool,
|
||||
handler TaskHandler,
|
||||
log *slog.Logger,
|
||||
) {
|
||||
@@ -288,7 +186,7 @@ func (t *Transport) handleTask(
|
||||
t.respondTask(msg, nil, fmt.Errorf("decode taskrun request: %w", err), log)
|
||||
return
|
||||
}
|
||||
if err := validateTaskRequest(msg.Subject, request, bootTaskID, dedicated); err != nil {
|
||||
if err := validateTaskRequest(msg.Subject, request, bootTaskID); err != nil {
|
||||
log.Error("taskrun validation failed", "subject", msg.Subject, "err", err)
|
||||
t.respondTask(msg, nil, fmt.Errorf("validate taskrun request: %w", err), log)
|
||||
return
|
||||
@@ -297,7 +195,7 @@ func (t *Transport) handleTask(
|
||||
t.respondTask(msg, response, err, log)
|
||||
}
|
||||
|
||||
func validateTaskRequest(subject string, request taskrun.Request, bootTaskID int64, dedicated bool) error {
|
||||
func validateTaskRequest(subject string, request taskrun.Request, bootTaskID int64) error {
|
||||
subjectTaskID, err := taskIDFromSubject(subject)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -308,8 +206,8 @@ func validateTaskRequest(subject string, request taskrun.Request, bootTaskID int
|
||||
if request.Task.ID != subjectTaskID {
|
||||
return fmt.Errorf("task ID %d does not match subject task ID %d", request.Task.ID, subjectTaskID)
|
||||
}
|
||||
if dedicated && subjectTaskID != bootTaskID {
|
||||
return fmt.Errorf("task ID %d does not match dedicated boot task ID %d", subjectTaskID, bootTaskID)
|
||||
if subjectTaskID != bootTaskID {
|
||||
return fmt.Errorf("task ID %d does not match boot task ID %d", subjectTaskID, bootTaskID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -15,16 +15,25 @@ import (
|
||||
natstest "github.com/nats-io/nats-server/v2/test"
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
busnats "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/storerpc"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/worktreerpc"
|
||||
)
|
||||
|
||||
func TestTransportContainsNoSharedTaskWorkerTopology(t *testing.T) {
|
||||
source, err := os.ReadFile("transport.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, forbidden := range []string{"QueueSubscribe", "taskRunSubjectWildcard", "taskRunQueueGroup"} {
|
||||
if strings.Contains(string(source), forbidden) {
|
||||
t.Errorf("transport.go contains forbidden shared-worker mechanism %q", forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testTransport(t *testing.T) (*Transport, *natsio.Conn) {
|
||||
t.Helper()
|
||||
srv := natstest.RunRandClientPortServer()
|
||||
@@ -72,56 +81,56 @@ func TestTaskRunSubjectOwnsCanonicalNATSTopology(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubscribeTasksPreservesDedicatedAndSharedSubjects(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
taskID int64
|
||||
dedicated bool
|
||||
subject string
|
||||
requestID int64
|
||||
}{
|
||||
{name: "dedicated", taskID: 42, dedicated: true, subject: SubjectForTask(42), requestID: 42},
|
||||
{name: "shared queue", subject: SubjectForTask(7), requestID: 7},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
transport, conn := testTransport(t)
|
||||
calls := 0
|
||||
subscription, err := transport.SubscribeTasks(t.Context(), test.taskID, test.dedicated, func(_ context.Context, request taskrun.Request) (*taskrun.Response, error) {
|
||||
calls++
|
||||
if request.Task.ID != test.requestID {
|
||||
t.Fatalf("task ID = %d, want %d", request.Task.ID, test.requestID)
|
||||
}
|
||||
return &taskrun.Response{Status: "done"}, nil
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = subscription.Close() })
|
||||
// Full-task handoff and all worker RPC use core NATS. The worker must connect
|
||||
// to a broker with JetStream disabled; requiring a durable consumer would mean
|
||||
// the deleted per-stage transport still owns startup.
|
||||
func TestConnectRequiresOnlyCoreNATS(t *testing.T) {
|
||||
srv := natstest.RunServer(&server.Options{Port: -1, Authorization: "worker-secret"})
|
||||
t.Cleanup(srv.Shutdown)
|
||||
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &store.Task{ID: test.requestID}, WorktreeGrant: "grant"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
message, err := conn.Request(test.subject, payload, time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response taskrun.Response
|
||||
if err := json.Unmarshal(message.Data, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if calls != 1 || response.Status != "done" {
|
||||
t.Fatalf("handler/response = (%d, %q), want (1, done)", calls, response.Status)
|
||||
}
|
||||
})
|
||||
transport, err := Connect(t.Context(), Config{URL: srv.ClientURL(), Token: "worker-secret"}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatalf("Connect to core-only NATS: %v", err)
|
||||
}
|
||||
transport.Close()
|
||||
}
|
||||
|
||||
func TestSubscribeTasksServesOnlyBootTaskSubject(t *testing.T) {
|
||||
transport, conn := testTransport(t)
|
||||
calls := 0
|
||||
subscription, err := transport.SubscribeTasks(t.Context(), 42, func(_ context.Context, request taskrun.Request) (*taskrun.Response, error) {
|
||||
calls++
|
||||
if request.Task.ID != 42 {
|
||||
t.Fatalf("task ID = %d, want 42", request.Task.ID)
|
||||
}
|
||||
return &taskrun.Response{Status: "done"}, nil
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = subscription.Close() })
|
||||
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &store.Task{ID: 42}, WorktreeGrant: "grant"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
message, err := conn.Request(SubjectForTask(42), payload, time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response taskrun.Response
|
||||
if err := json.Unmarshal(message.Data, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if calls != 1 || response.Status != "done" {
|
||||
t.Fatalf("handler/response = (%d, %q), want (1, done)", calls, response.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubscribeTasksOwnsWireErrors(t *testing.T) {
|
||||
transport, conn := testTransport(t)
|
||||
handlerCalls := 0
|
||||
subscription, err := transport.SubscribeTasks(t.Context(), 9, true, func(context.Context, taskrun.Request) (*taskrun.Response, error) {
|
||||
subscription, err := transport.SubscribeTasks(t.Context(), 9, func(context.Context, taskrun.Request) (*taskrun.Response, error) {
|
||||
handlerCalls++
|
||||
return nil, errors.New("run failed")
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
@@ -169,7 +178,6 @@ func TestHandleTaskRejectsSubjectCorrelationErrors(t *testing.T) {
|
||||
name string
|
||||
subject string
|
||||
bootTaskID int64
|
||||
dedicated bool
|
||||
requestID int64
|
||||
want string
|
||||
}{
|
||||
@@ -180,19 +188,18 @@ func TestHandleTaskRejectsSubjectCorrelationErrors(t *testing.T) {
|
||||
want: `validate taskrun request: invalid taskrun subject "archie.taskrun.invalid"`,
|
||||
},
|
||||
{
|
||||
name: "dedicated boot mismatch",
|
||||
name: "boot mismatch",
|
||||
subject: SubjectForTask(9),
|
||||
bootTaskID: 42,
|
||||
dedicated: true,
|
||||
requestID: 9,
|
||||
want: "validate taskrun request: task ID 9 does not match dedicated boot task ID 42",
|
||||
want: "validate taskrun request: task ID 9 does not match boot task ID 42",
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
transport, conn := testTransport(t)
|
||||
handlerCalls := 0
|
||||
subscription, err := conn.Subscribe(test.subject, func(message *natsio.Msg) {
|
||||
transport.handleTask(t.Context(), message, test.bootTaskID, test.dedicated, func(context.Context, taskrun.Request) (*taskrun.Response, error) {
|
||||
transport.handleTask(t.Context(), message, test.bootTaskID, func(context.Context, taskrun.Request) (*taskrun.Response, error) {
|
||||
handlerCalls++
|
||||
return &taskrun.Response{Status: "unexpected"}, nil
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
@@ -239,24 +246,20 @@ func (s *recordingSDKSubscription) Unsubscribe() error {
|
||||
|
||||
func TestSubscribeTasksReportsSetupFailuresAndCleansUpFlushFailure(t *testing.T) {
|
||||
subscribeErr := errors.New("subscribe failed")
|
||||
queueErr := errors.New("queue subscribe failed")
|
||||
flushErr := errors.New("flush failed")
|
||||
cleanupErr := errors.New("unsubscribe failed")
|
||||
tests := []struct {
|
||||
name string
|
||||
dedicated bool
|
||||
subscribeErr error
|
||||
queueErr error
|
||||
flushErr error
|
||||
cleanupErr error
|
||||
wantErr error
|
||||
wantUnsubscribes int
|
||||
wantCleanupLog bool
|
||||
}{
|
||||
{name: "direct subscribe", dedicated: true, subscribeErr: subscribeErr, wantErr: subscribeErr},
|
||||
{name: "queue subscribe", queueErr: queueErr, wantErr: queueErr},
|
||||
{name: "flush cleanup succeeds", dedicated: true, flushErr: flushErr, wantErr: flushErr, wantUnsubscribes: 1},
|
||||
{name: "flush cleanup fails", dedicated: true, flushErr: flushErr, cleanupErr: cleanupErr, wantErr: flushErr, wantUnsubscribes: 1, wantCleanupLog: true},
|
||||
{name: "direct subscribe", subscribeErr: subscribeErr, wantErr: subscribeErr},
|
||||
{name: "flush cleanup succeeds", flushErr: flushErr, wantErr: flushErr, wantUnsubscribes: 1},
|
||||
{name: "flush cleanup fails", flushErr: flushErr, cleanupErr: cleanupErr, wantErr: flushErr, wantUnsubscribes: 1, wantCleanupLog: true},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
@@ -265,9 +268,6 @@ func TestSubscribeTasksReportsSetupFailuresAndCleansUpFlushFailure(t *testing.T)
|
||||
subscribe: func(string, natsio.MsgHandler) (sdkSubscription, error) {
|
||||
return sub, test.subscribeErr
|
||||
},
|
||||
queueSubscribe: func(string, string, natsio.MsgHandler) (sdkSubscription, error) {
|
||||
return sub, test.queueErr
|
||||
},
|
||||
flush: func(timeout time.Duration) error {
|
||||
if timeout != taskSubscriptionFlushTimeout {
|
||||
t.Fatalf("flush timeout = %s, want %s", timeout, taskSubscriptionFlushTimeout)
|
||||
@@ -276,7 +276,7 @@ func TestSubscribeTasksReportsSetupFailuresAndCleansUpFlushFailure(t *testing.T)
|
||||
},
|
||||
}
|
||||
var output bytes.Buffer
|
||||
_, err := transport.SubscribeTasks(t.Context(), 42, test.dedicated, func(context.Context, taskrun.Request) (*taskrun.Response, error) {
|
||||
_, err := transport.SubscribeTasks(t.Context(), 42, func(context.Context, taskrun.Request) (*taskrun.Response, error) {
|
||||
t.Fatal("handler called")
|
||||
return nil, nil
|
||||
}, slog.New(slog.NewTextHandler(&output, nil)))
|
||||
@@ -293,127 +293,6 @@ func TestSubscribeTasksReportsSetupFailuresAndCleansUpFlushFailure(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectOwnsWorkerTopology(t *testing.T) {
|
||||
connectErr := errors.New("stop after config capture")
|
||||
var got busnats.Config
|
||||
_, err := connect(t.Context(), Config{
|
||||
URL: "nats://worker.test:4222",
|
||||
Token: "secret",
|
||||
ConsumerName: "agent-worker",
|
||||
}, slog.New(slog.DiscardHandler), func(_ context.Context, config busnats.Config, _ *slog.Logger) (*busnats.Client, error) {
|
||||
got = config
|
||||
return nil, connectErr
|
||||
})
|
||||
if !errors.Is(err, connectErr) {
|
||||
t.Fatalf("connect error = %v, want %v", err, connectErr)
|
||||
}
|
||||
if got.URL != "nats://worker.test:4222" || got.Token != "secret" || got.ConsumerName != "agent-worker" {
|
||||
t.Fatalf("deployment config = %#v", got)
|
||||
}
|
||||
wantSubjects := []string{workintake.SubjectTaskWildcard, agentexec.SubjectAgentWildcard}
|
||||
if !reflect.DeepEqual(got.Subjects, wantSubjects) {
|
||||
t.Fatalf("subjects = %v, want %v", got.Subjects, wantSubjects)
|
||||
}
|
||||
if got.FilterSubject != agentexec.SubjectAgentWildcard || got.PollTimeout != stagePollTimeout || got.AckWait != stageAckWait {
|
||||
t.Fatalf("topology config = %#v, want filter=%q poll=%s ack=%s", got, agentexec.SubjectAgentWildcard, stagePollTimeout, stageAckWait)
|
||||
}
|
||||
}
|
||||
|
||||
type rawStageMessage struct {
|
||||
data []byte
|
||||
subject string
|
||||
reply string
|
||||
replyErr error
|
||||
ackErr error
|
||||
nakErr error
|
||||
ackCalls int
|
||||
nakCalls int
|
||||
}
|
||||
|
||||
func (m *rawStageMessage) Data() []byte { return m.data }
|
||||
func (m *rawStageMessage) Subject() string { return m.subject }
|
||||
func (m *rawStageMessage) ReplyAddress() (string, error) { return m.reply, m.replyErr }
|
||||
func (m *rawStageMessage) Ack() error { m.ackCalls++; return m.ackErr }
|
||||
func (m *rawStageMessage) Nak() error { m.nakCalls++; return m.nakErr }
|
||||
|
||||
func TestStageMessageOwnsDecodeReplyAndAcknowledgement(t *testing.T) {
|
||||
replyErr := errors.New("no reply")
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
wire *rawStageMessage
|
||||
wantErr string
|
||||
}{
|
||||
{name: "decode", wire: &rawStageMessage{data: []byte("not json"), subject: "archie.agent.bad"}, wantErr: "decode request:"},
|
||||
{name: "reply", wire: &rawStageMessage{data: []byte(`{"task_id":7}`), subject: "archie.agent.no-reply", replyErr: replyErr}, wantErr: "stage request on archie.agent.no-reply:"},
|
||||
{name: "typed request", wire: &rawStageMessage{data: []byte(`{"task_id":7,"stage":"test"}`), reply: "_INBOX.7"}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
message := newStageMessage(test.wire, func(context.Context, string, []byte) error { return nil })
|
||||
request, err := message.Request()
|
||||
if test.wantErr != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), test.wantErr) {
|
||||
t.Fatalf("Request error = %v, want containing %q", err, test.wantErr)
|
||||
}
|
||||
} else if err != nil || request.TaskID != 7 || request.Stage != "test" {
|
||||
t.Fatalf("Request = (%#v, %v), want typed task 7/test", request, err)
|
||||
}
|
||||
if err := message.Ack(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := message.Nak(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if test.wire.ackCalls != 1 || test.wire.nakCalls != 1 {
|
||||
t.Fatalf("ack/nak calls = (%d,%d), want (1,1)", test.wire.ackCalls, test.wire.nakCalls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStageMessageDelegatesAcknowledgementErrors(t *testing.T) {
|
||||
ackErr := errors.New("ack failed")
|
||||
nakErr := errors.New("nak failed")
|
||||
wire := &rawStageMessage{data: []byte(`{"task_id":7}`), reply: "_INBOX.7", ackErr: ackErr, nakErr: nakErr}
|
||||
message := newStageMessage(wire, func(context.Context, string, []byte) error { return nil })
|
||||
if err := message.Ack(); !errors.Is(err, ackErr) {
|
||||
t.Fatalf("Ack error = %v, want %v", err, ackErr)
|
||||
}
|
||||
if err := message.Nak(); !errors.Is(err, nakErr) {
|
||||
t.Fatalf("Nak error = %v, want %v", err, nakErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStageMessageOwnsResponseEncodingAndInboxDelivery(t *testing.T) {
|
||||
encodeErr := errors.New("encode failed")
|
||||
respondErr := errors.New("respond failed")
|
||||
response := &agentexec.AgentResponseEnvelope{Result: agentexec.Result{Status: agentexec.StatusPassed}}
|
||||
|
||||
wire := &rawStageMessage{data: []byte(`{"task_id":7}`), reply: "_INBOX.7"}
|
||||
message := newStageMessage(wire, func(_ context.Context, reply string, data []byte) error {
|
||||
if reply != "_INBOX.7" || !strings.Contains(string(data), `"status":"passed"`) {
|
||||
t.Fatalf("reply/data = (%q,%s)", reply, data)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err := message.Respond(t.Context(), response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
concrete, ok := message.(*stageMessage)
|
||||
if !ok {
|
||||
t.Fatalf("message type = %T, want *stageMessage", message)
|
||||
}
|
||||
concrete.encode = func(any) ([]byte, error) { return nil, encodeErr }
|
||||
if err := concrete.Respond(t.Context(), response); !errors.Is(err, encodeErr) || !strings.Contains(err.Error(), "encode response") {
|
||||
t.Fatalf("encode error = %v", err)
|
||||
}
|
||||
concrete.encode = json.Marshal
|
||||
concrete.respond = func(context.Context, string, []byte) error { return respondErr }
|
||||
if err := concrete.Respond(t.Context(), response); !errors.Is(err, respondErr) || !strings.Contains(err.Error(), "publish response") {
|
||||
t.Fatalf("respond error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRPCFactoriesPreserveIdentityAndTimeout(t *testing.T) {
|
||||
transport, _ := testTransport(t)
|
||||
timeout := 60 * time.Second
|
||||
@@ -430,32 +309,3 @@ func TestRPCFactoriesPreserveIdentityAndTimeout(t *testing.T) {
|
||||
t.Fatalf("tree client = %#v", treeClient)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportFromBusClassifiesUnavailableCoreConnection(t *testing.T) {
|
||||
transport, err := transportFromBus(&busnats.Client{})
|
||||
if transport != nil {
|
||||
t.Fatalf("transport = %#v, want nil", transport)
|
||||
}
|
||||
if !errors.Is(err, ErrCoreConnectionUnavailable) {
|
||||
t.Fatalf("error = %v, want ErrCoreConnectionUnavailable", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectOwnsJetStreamAndCoreConnection(t *testing.T) {
|
||||
srv := natstest.RunRandClientPortServer()
|
||||
t.Cleanup(srv.Shutdown)
|
||||
if err := srv.EnableJetStream(&server.JetStreamConfig{StoreDir: t.TempDir()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
transport, err := Connect(t.Context(), Config{
|
||||
URL: srv.ClientURL(),
|
||||
ConsumerName: "worker-test",
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
transport.Close()
|
||||
if _, err := transport.FetchStage(t.Context()); err == nil {
|
||||
t.Fatal("FetchStage after Close returned nil error")
|
||||
}
|
||||
}
|
||||
@@ -55,11 +55,11 @@ func TestLoaderApplyOverlayLayersDefaultsValidatesAndRecordsProvenance(t *testin
|
||||
|
||||
// An overlay that fails validation aborts; ApplyOverlay never mutates
|
||||
// its input, so the base document is untouched even without a copy.
|
||||
if _, err := l.ApplyOverlay(doc, map[string]any{"agent": map[string]any{"mode": "bogus"}}); err == nil {
|
||||
if _, err := l.ApplyOverlay(doc, map[string]any{"dispatch": map[string]any{"trigger": "bogus"}}); err == nil {
|
||||
t.Fatal("invalid overlay accepted")
|
||||
}
|
||||
if doc.Config.Agent.Mode != "inprocess" {
|
||||
t.Errorf("base Agent.Mode = %q, want inprocess (defaulted, untouched)", doc.Config.Agent.Mode)
|
||||
if doc.Config.Dispatch.Trigger != "assignee" {
|
||||
t.Errorf("base Dispatch.Trigger = %q, want assignee (defaulted, untouched)", doc.Config.Dispatch.Trigger)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ const (
|
||||
defaultMaxRetries = 3
|
||||
defaultForgeType = "github"
|
||||
defaultForgeHost = "https://github.com"
|
||||
defaultAgentCommand = "archie-agent"
|
||||
defaultDispatchTrigger = "assignee"
|
||||
defaultAckReaction = "eyes"
|
||||
defaultContainerUptime = 60 * time.Minute
|
||||
@@ -61,7 +60,6 @@ const (
|
||||
func (l *Loader) applyDefaults(cfg *config.Config) {
|
||||
l.applyGeneralDefaults(cfg)
|
||||
applyForgeDefaults(cfg)
|
||||
applyAgentDefaults(cfg)
|
||||
applyDispatchDefaults(cfg)
|
||||
applyIdentityDefaults(cfg)
|
||||
applyContainerDefaults(cfg)
|
||||
@@ -206,21 +204,6 @@ func applyNATSDefaults(cfg *config.Config) {
|
||||
}
|
||||
}
|
||||
|
||||
// applyAgentDefaults picks the execution mode. Containers require the NATS
|
||||
// transport, so enabling them selects it unless the operator said otherwise.
|
||||
func applyAgentDefaults(cfg *config.Config) {
|
||||
if cfg.Agent.Mode == "" {
|
||||
if cfg.Containers.Enabled {
|
||||
cfg.Agent.Mode = agentModeNATS
|
||||
} else {
|
||||
cfg.Agent.Mode = agentModeInProcess
|
||||
}
|
||||
}
|
||||
if cfg.Agent.Command == "" {
|
||||
cfg.Agent.Command = defaultAgentCommand
|
||||
}
|
||||
}
|
||||
|
||||
// applyDispatchDefaults fills the dispatch section. "off" is the documented
|
||||
// way to disable the acknowledgement reaction, and normalises to empty.
|
||||
func applyDispatchDefaults(cfg *config.Config) {
|
||||
|
||||
@@ -195,9 +195,6 @@ func TestLoadRejectsInvalidConfigEnumsAndGlobs(t *testing.T) {
|
||||
extra string
|
||||
}{
|
||||
{name: "dispatch trigger", extra: "\n[dispatch]\ntrigger = \"labels\"\n"},
|
||||
{name: "agent mode", extra: "\n[agent]\nmode = \"remote\"\n"},
|
||||
{name: "agent command", extra: "\n[agent]\nmode = \"subprocess\"\ncommand = \" \"\n"},
|
||||
{name: "agent env", extra: "\n[agent]\nenv = [\"TOKEN=value\"]\n"},
|
||||
{name: "provider userinfo", extra: "\n[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n"},
|
||||
{name: "provider query secret", extra: "\n[providers.openai]\nclass = \"openai\"\nbase_url = \"https://example.com/v1?api_key=secret\"\n"},
|
||||
{name: "test glob", extra: "\n[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n"},
|
||||
@@ -222,7 +219,7 @@ func TestLoadRejectsInvalidConfigEnumsAndGlobs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadAgentDefaultsAndOverrides(t *testing.T) {
|
||||
func TestLoadLegacyAgentSectionWithoutApplyingExecutionDefaults(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
agent string
|
||||
@@ -230,10 +227,10 @@ func TestLoadAgentDefaultsAndOverrides(t *testing.T) {
|
||||
wantCommand string
|
||||
wantEnv []string
|
||||
}{
|
||||
{name: "defaults", wantMode: "inprocess", wantCommand: "archie-agent"},
|
||||
{name: "absent section stays empty"},
|
||||
{
|
||||
name: "subprocess", agent: "\n[agent]\nmode = \"subprocess\"\ncommand = \"/opt/archie-agent\"\nenv = [\"GOCACHE\"]\n",
|
||||
wantMode: "subprocess", wantCommand: "/opt/archie-agent", wantEnv: []string{"GOCACHE"},
|
||||
name: "legacy values decode without validation", agent: "\n[agent]\nmode = \"removed-mode\"\ncommand = \"/opt/old-agent\"\nenv = [\"TOKEN=value\"]\n",
|
||||
wantMode: "removed-mode", wantCommand: "/opt/old-agent", wantEnv: []string{"TOKEN=value"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
@@ -247,8 +244,8 @@ func TestLoadAgentDefaultsAndOverrides(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Agent.Mode != tt.wantMode || cfg.Agent.Command != tt.wantCommand || strings.Join(cfg.Agent.Env, ",") != strings.Join(tt.wantEnv, ",") {
|
||||
t.Fatalf("agent config = %#v", cfg.Agent)
|
||||
if cfg.LegacyAgent.Mode != tt.wantMode || cfg.LegacyAgent.Command != tt.wantCommand || strings.Join(cfg.LegacyAgent.Env, ",") != strings.Join(tt.wantEnv, ",") {
|
||||
t.Fatalf("legacy agent config = %#v", cfg.LegacyAgent)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -425,7 +422,6 @@ func TestLoadContainerVolumeTTL(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
contents := "bot_user = \"widget\"\n" +
|
||||
"[agent]\nmode = \"nats\"\n" +
|
||||
"[nats]\nurl = \"nats://localhost:4222\"\n" +
|
||||
"[containers]\nenabled = true\nimage = \"archie-agent:test\"\n" + tt.ttl +
|
||||
"[[repos]]\nowner = \"acme\"\nname = \"app\"\npersistent_storage = true\n"
|
||||
@@ -447,7 +443,6 @@ func TestLoadContainerVolumeTTL(t *testing.T) {
|
||||
func TestLoadRejectsNegativeContainerVolumeTTL(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
contents := "bot_user = \"widget\"\n" +
|
||||
"[agent]\nmode = \"nats\"\n" +
|
||||
"[nats]\nurl = \"nats://localhost:4222\"\n" +
|
||||
"[containers]\nenabled = true\nimage = \"archie-agent:test\"\nvolume_ttl = \"-1h\"\n" +
|
||||
"[[repos]]\nowner = \"acme\"\nname = \"app\"\npersistent_storage = true\n"
|
||||
@@ -508,8 +503,8 @@ func TestOverlay(t *testing.T) {
|
||||
if cfg.WorkDir != "/var/lib/archie/work" {
|
||||
t.Errorf("WorkDir: got %q, want overlay value", cfg.WorkDir)
|
||||
}
|
||||
if cfg.Agent.Mode != "nats" {
|
||||
t.Errorf("Agent.Mode: got %q, want %q", cfg.Agent.Mode, "nats")
|
||||
if cfg.LegacyAgent.Mode != "nats" {
|
||||
t.Errorf("LegacyAgent.Mode: got %q, want %q", cfg.LegacyAgent.Mode, "nats")
|
||||
}
|
||||
// Fields the overlay omits keep the base value.
|
||||
if cfg.BotUser != "widget" {
|
||||
@@ -524,8 +519,8 @@ func TestOverlay(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if baseOnly.Agent.Mode != "inprocess" {
|
||||
t.Errorf("Agent.Mode with empty overlay: got %q, want %q", baseOnly.Agent.Mode, "inprocess")
|
||||
if baseOnly.LegacyAgent.Mode != "inprocess" {
|
||||
t.Errorf("LegacyAgent.Mode with empty overlay: got %q, want %q", baseOnly.LegacyAgent.Mode, "inprocess")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,6 @@ import (
|
||||
// Recognised enum values, named so validation and its error message cannot
|
||||
// disagree about what is allowed.
|
||||
const (
|
||||
agentModeInProcess = "inprocess"
|
||||
agentModeSubprocess = "subprocess"
|
||||
agentModeNATS = "nats"
|
||||
|
||||
forgeTypeGitHub = "github"
|
||||
forgeTypeGitea = "gitea"
|
||||
forgeTypeNone = "none"
|
||||
@@ -30,7 +26,6 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
agentModes = []string{agentModeInProcess, agentModeSubprocess, agentModeNATS}
|
||||
forgeTypes = []string{forgeTypeGitHub, forgeTypeGitea, forgeTypeNone, forgeTypeOff, forgeTypeDisabled}
|
||||
dispatchTriggers = []string{dispatchTriggerAssignee, dispatchTriggerLabel, dispatchTriggerEither}
|
||||
natsModes = []string{config.NATSModeEmbedded, config.NATSModeExternal, config.NATSModeOff}
|
||||
@@ -41,8 +36,8 @@ var (
|
||||
// against a config.Config value already built in memory -- e.g. by archied
|
||||
// setup, before it has written anything to disk.
|
||||
//
|
||||
// Like validate, it does not apply defaults. Several checks (agent.mode,
|
||||
// dispatch.trigger, forge.type) only pass once a default has been filled
|
||||
// Like validate, it does not apply defaults. Several checks (dispatch.trigger,
|
||||
// forge.type) only pass once a default has been filled
|
||||
// in, and defaulting is currently unexported ((*Loader).applyDefaults). A
|
||||
// cfg built by hand, with those fields left zero-valued, will fail
|
||||
// validation that a config loaded through [Loader.File] would pass. Callers
|
||||
@@ -58,9 +53,6 @@ func Validate(cfg *config.Config) error {
|
||||
// validate reports the first problem that would stop the daemon running.
|
||||
// It does not modify cfg -- run applyDefaults first.
|
||||
func validate(cfg *config.Config) error {
|
||||
if err := validateAgent(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateDispatch(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -127,21 +119,6 @@ func validatePollInterval(cfg *config.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAgent(cfg *config.Config) error {
|
||||
if !oneOf(cfg.Agent.Mode, agentModes) {
|
||||
return fmt.Errorf("%w: agent.mode %q (want %s)", ErrInvalidInput, cfg.Agent.Mode, list(agentModes))
|
||||
}
|
||||
if cfg.Agent.Mode == agentModeSubprocess && strings.TrimSpace(cfg.Agent.Command) == "" {
|
||||
return fmt.Errorf("%w: agent.command is required in subprocess mode", ErrInvalidInput)
|
||||
}
|
||||
for i, name := range cfg.Agent.Env {
|
||||
if strings.TrimSpace(name) == "" || strings.Contains(name, "=") {
|
||||
return fmt.Errorf("%w: agent.env[%d] %q is not an environment variable name", ErrInvalidInput, i, name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateDispatch rejects a "label" or "either" trigger left with no label
|
||||
// to match. GitHub's issues-list API treats an empty label filter as no
|
||||
// filter at all and returns every open issue in the repo -- a live incident
|
||||
@@ -297,9 +274,6 @@ func validateNATS(cfg *config.Config) error {
|
||||
return fmt.Errorf("%w: nats.url must be empty when nats.mode is %q", ErrInvalidInput, mode)
|
||||
}
|
||||
}
|
||||
if cfg.Agent.Mode == agentModeNATS && mode == config.NATSModeEmbedded && !cfg.Containers.Enabled {
|
||||
return fmt.Errorf("%w: containers.enabled must be true when agent.mode is %q and nats.mode is %q", ErrInvalidInput, agentModeNATS, config.NATSModeEmbedded)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -313,9 +287,6 @@ func validateContainers(cfg *config.Config) error {
|
||||
if cfg.Containers.Image == "" {
|
||||
return fmt.Errorf("%w: containers.image is required when containers.enabled is true", ErrInvalidInput)
|
||||
}
|
||||
if cfg.Agent.Mode != agentModeNATS {
|
||||
return fmt.Errorf("%w: agent.mode must be %q when containers.enabled is true", ErrInvalidInput, agentModeNATS)
|
||||
}
|
||||
if effectiveNATSMode(cfg) == config.NATSModeOff {
|
||||
return fmt.Errorf("%w: nats.mode must not be %q when containers.enabled is true", ErrInvalidInput, config.NATSModeOff)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ func minimalValidConfig() config.Config {
|
||||
return config.Config{
|
||||
BotUser: "archie-bot",
|
||||
PollInterval: config.Duration(60 * time.Second),
|
||||
Agent: config.Agent{Mode: "inprocess"},
|
||||
Dispatch: config.Dispatch{
|
||||
Trigger: "assignee",
|
||||
},
|
||||
@@ -79,11 +78,6 @@ func TestValidate_RejectsTheSameProblemsAsLoaderLoad(t *testing.T) {
|
||||
mutate: func(cfg *config.Config) { cfg.BotUser = "" },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "unrecognised agent.mode",
|
||||
mutate: func(cfg *config.Config) { cfg.Agent.Mode = "not-a-real-mode" },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "unrecognised dispatch.trigger",
|
||||
mutate: func(cfg *config.Config) { cfg.Dispatch.Trigger = "not-a-real-trigger" },
|
||||
@@ -192,16 +186,14 @@ func TestValidate_RejectsTheSameProblemsAsLoaderLoad(t *testing.T) {
|
||||
|
||||
// TestValidateNATS pins the nats mode contract from docs/prds/embedded-nats.md:
|
||||
// an unset mode resolves from url without mutating cfg, external requires url,
|
||||
// embedded/off forbid a url, and NATS agent execution works with either
|
||||
// embedded or external broker deployment.
|
||||
// embedded/off forbid a url, and full-task workers work with either embedded
|
||||
// or external broker deployment.
|
||||
func TestValidateNATS(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mode string
|
||||
url string
|
||||
agent string
|
||||
containers bool
|
||||
wantErr bool
|
||||
name string
|
||||
mode string
|
||||
url string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "unset mode with url resolves external", url: "nats://localhost:4222"},
|
||||
{name: "unset mode without url resolves embedded"},
|
||||
@@ -212,20 +204,11 @@ func TestValidateNATS(t *testing.T) {
|
||||
{name: "embedded with url", mode: config.NATSModeEmbedded, url: "nats://localhost:4222", wantErr: true},
|
||||
{name: "off with url", mode: config.NATSModeOff, url: "nats://localhost:4222", wantErr: true},
|
||||
{name: "unknown mode", mode: "not-a-mode", wantErr: true},
|
||||
{name: "agent nats with embedded requires containers", mode: config.NATSModeEmbedded, agent: "nats", wantErr: true},
|
||||
{name: "agent nats with embedded containers", mode: config.NATSModeEmbedded, agent: "nats", containers: true},
|
||||
{name: "agent nats with external", mode: config.NATSModeExternal, url: "nats://localhost:4222", agent: "nats"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.NATS = config.NATSConfig{Mode: tc.mode, URL: tc.url}
|
||||
if tc.agent != "" {
|
||||
cfg.Agent.Mode = tc.agent
|
||||
}
|
||||
if tc.containers {
|
||||
cfg.Containers.Enabled = true
|
||||
}
|
||||
err := validateNATS(&cfg)
|
||||
if tc.wantErr && err == nil {
|
||||
t.Fatal("validateNATS() = nil, want an error")
|
||||
@@ -246,7 +229,6 @@ func TestValidateContainersResolvesNATSMode(t *testing.T) {
|
||||
base := func() config.Config {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.Containers = config.ContainerConfig{Enabled: true, Image: "archie-agent:latest"}
|
||||
cfg.Agent.Mode = "nats"
|
||||
return cfg
|
||||
}
|
||||
|
||||
|
||||
@@ -8,14 +8,6 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
)
|
||||
|
||||
// replyHeader carries the requester's reply inbox.
|
||||
//
|
||||
// JetStream consumes a message's Reply field for its own PubAck, so the
|
||||
// address travels as a header instead. That is a NATS detail: the contract
|
||||
// exposes it as [eventbus.Message.ReplyAddress] and callers never name the
|
||||
// header.
|
||||
const replyHeader = "X-Archie-Reply"
|
||||
|
||||
// idempotencyHeader is JetStream's deduplication key. Messages republished
|
||||
// with the same value inside Config.DedupWindow are suppressed by the server.
|
||||
const idempotencyHeader = "Nats-Msg-Id"
|
||||
@@ -37,14 +29,6 @@ func (m message) Data() []byte { return m.msg.Data() }
|
||||
// Subject returns the subject the message arrived on.
|
||||
func (m message) Subject() string { return m.msg.Subject() }
|
||||
|
||||
// ReplyAddress returns the requester's reply inbox.
|
||||
func (m message) ReplyAddress() (string, error) {
|
||||
if addr := m.msg.Headers().Get(replyHeader); addr != "" {
|
||||
return addr, nil
|
||||
}
|
||||
return "", fmt.Errorf("%w: on %s", eventbus.ErrNoReplyAddress, m.Subject())
|
||||
}
|
||||
|
||||
// Ack marks the message handled so JetStream will not redeliver it.
|
||||
func (m message) Ack() error {
|
||||
if err := m.msg.Ack(); err != nil {
|
||||
|
||||
@@ -23,35 +23,6 @@ func (c *Client) PublishUnique(ctx context.Context, subject, idempotencyKey stri
|
||||
return c.publish(ctx, &nats.Msg{Subject: subject, Data: payload, Header: header})
|
||||
}
|
||||
|
||||
// Respond answers a request on an ephemeral reply inbox.
|
||||
//
|
||||
// It uses core NATS rather than the JetStream path the other publish methods
|
||||
// take: the inbox is not a stream subject, so a JetStream publish to it has
|
||||
// no stream to accept the message and fails.
|
||||
func (c *Client) Respond(ctx context.Context, replyAddress string, payload []byte) error {
|
||||
if replyAddress == "" {
|
||||
return fmt.Errorf("%w: nothing to respond to", eventbus.ErrNoReplyAddress)
|
||||
}
|
||||
conn, err := c.connection()
|
||||
if err != nil {
|
||||
return fmt.Errorf("respond on %s: %w", replyAddress, err)
|
||||
}
|
||||
if err := conn.Publish(replyAddress, payload); err != nil {
|
||||
return fmt.Errorf("respond on %s: %w", replyAddress, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PublishRequest sends a payload recording replyTo as the response address.
|
||||
func (c *Client) PublishRequest(ctx context.Context, subject, replyTo string, payload []byte) error {
|
||||
if replyTo == "" {
|
||||
return fmt.Errorf("%w: publishing to %s", eventbus.ErrNoReplyAddress, subject)
|
||||
}
|
||||
header := nats.Header{}
|
||||
header.Set(replyHeader, replyTo)
|
||||
return c.publish(ctx, &nats.Msg{Subject: subject, Data: payload, Header: header})
|
||||
}
|
||||
|
||||
// publish is the single JetStream publish path, so every caller gets the same
|
||||
// connection check and error context.
|
||||
func (c *Client) publish(ctx context.Context, msg *nats.Msg) error {
|
||||
@@ -87,48 +58,3 @@ func (c *Client) Request(ctx context.Context, subject string, payload []byte) ([
|
||||
}
|
||||
return msg.Data, nil
|
||||
}
|
||||
|
||||
// replyInbox is a one-shot subscription awaiting a single response.
|
||||
type replyInbox struct {
|
||||
sub *nats.Subscription
|
||||
}
|
||||
|
||||
var _ eventbus.ReplyInbox = replyInbox{}
|
||||
|
||||
// Subject is the inbox address to advertise to the responder.
|
||||
func (r replyInbox) Subject() string { return r.sub.Subject }
|
||||
|
||||
// Next blocks until the reply arrives or ctx is done.
|
||||
func (r replyInbox) Next(ctx context.Context) ([]byte, error) {
|
||||
msg, err := r.sub.NextMsgWithContext(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("await reply on %s: %w", r.sub.Subject, err)
|
||||
}
|
||||
return msg.Data, nil
|
||||
}
|
||||
|
||||
// Close unsubscribes the inbox.
|
||||
func (r replyInbox) Close() error {
|
||||
if err := r.sub.Unsubscribe(); err != nil {
|
||||
return fmt.Errorf("close reply inbox %s: %w", r.sub.Subject, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewReplyInbox creates a unique inbox that auto-unsubscribes after one
|
||||
// message.
|
||||
func (c *Client) NewReplyInbox() (eventbus.ReplyInbox, error) {
|
||||
conn, err := c.connection()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create reply inbox: %w", err)
|
||||
}
|
||||
sub, err := conn.SubscribeSync(conn.NewInbox())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("subscribe reply inbox: %w", err)
|
||||
}
|
||||
if err := sub.AutoUnsubscribe(1); err != nil {
|
||||
_ = sub.Unsubscribe()
|
||||
return nil, fmt.Errorf("configure reply inbox %s: %w", sub.Subject, err)
|
||||
}
|
||||
return replyInbox{sub: sub}, nil
|
||||
}
|
||||
@@ -98,8 +98,8 @@ func TestRunMissingFile(t *testing.T) {
|
||||
// has no cancellation mechanism. A skill script that blocks forever
|
||||
// (select{}) wedges the calling goroutine permanently. Every other
|
||||
// execution path in archie-core supports context cancellation
|
||||
// (exec.CommandContext in SubprocessRunner, context.Cause checks in
|
||||
// InProcessRunner), but Run cannot be canceled or timed out because it
|
||||
// (context.Cause checks in LoopRunner), but Run cannot be canceled or timed
|
||||
// out because it
|
||||
// takes no context.Context and calls i.EvalPath synchronously.
|
||||
func TestRunBlocksIndefinitelyOnBlockingScript(t *testing.T) {
|
||||
path := writeScript(t, `package main
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Package taskrun defines the wire format for handing an entire task off
|
||||
// from archied to archie-agent in one NATS round trip, replacing the
|
||||
// per-stage AgentRequestMessage protocol for the sandboxed container path.
|
||||
// from archied to archie-agent in one NATS round trip.
|
||||
// archie-agent runs workflow.Route and workflow.Run itself; archied's role
|
||||
// shrinks to worktree prepare, container acquire/release, and answering
|
||||
// the storerpc/forgerpc/worktreerpc calls archie-agent proxies back.
|
||||
|
||||
@@ -65,7 +65,6 @@ func TestArchiedWiresTypedProvidersAndExecutableConsumers(t *testing.T) {
|
||||
// calls live in the chat composition files.
|
||||
"chatGenerateOptions(ctx,",
|
||||
"toolSummaries(options.Tools)",
|
||||
"agentexec.NewInProcessRunner(b.llm, log, b.toolReg)",
|
||||
} {
|
||||
if !strings.Contains(text.String(), required) {
|
||||
t.Errorf("archied wiring not found: %q", required)
|
||||
@@ -77,4 +76,7 @@ func TestArchiedWiresTypedProvidersAndExecutableConsumers(t *testing.T) {
|
||||
if strings.Contains(text.String(), "memManager.GetToolSchemas()") {
|
||||
t.Error("composition bypasses the typed memory tool provider")
|
||||
}
|
||||
if strings.Contains(text.String(), "NewLoopRunner") {
|
||||
t.Error("archied constructs a worker-local autonomous runner")
|
||||
}
|
||||
}
|
||||
@@ -209,7 +209,6 @@ type ConfigView struct {
|
||||
Models map[string]string `json:"models"`
|
||||
Providers map[string]ProviderView `json:"providers"`
|
||||
Budgets BudgetsView `json:"budgets"`
|
||||
Agent AgentView `json:"agent"`
|
||||
Storage StorageView `json:"storage"`
|
||||
Containers ContainersView `json:"containers"`
|
||||
Web WebView `json:"web"`
|
||||
@@ -271,15 +270,6 @@ type BudgetsView struct {
|
||||
GateMaxFailures int `json:"gate_max_failures"`
|
||||
}
|
||||
|
||||
// AgentView is how archied executes autonomous stages. Env lists only
|
||||
// environment variable NAMES admitted to the worker's allowlist, never
|
||||
// their values.
|
||||
type AgentView struct {
|
||||
Mode string `json:"mode"`
|
||||
Command string `json:"command,omitempty"`
|
||||
Env []string `json:"env,omitempty"`
|
||||
}
|
||||
|
||||
// StorageView is where archied keeps its state on disk. All paths, no
|
||||
// secrets.
|
||||
type StorageView struct {
|
||||
@@ -349,11 +339,6 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
|
||||
WallClock: cfg.Budgets.WallClock.Std().String(),
|
||||
GateMaxFailures: cfg.Budgets.GateMaxFailures,
|
||||
},
|
||||
Agent: AgentView{
|
||||
Mode: cfg.Agent.Mode,
|
||||
Command: cfg.Agent.Command,
|
||||
Env: cfg.Agent.Env,
|
||||
},
|
||||
Storage: StorageView{
|
||||
WorkDir: cfg.WorkDir,
|
||||
DBPath: cfg.DBPath,
|
||||
|
||||
@@ -55,8 +55,8 @@ func configWithFakeSecrets() *config.Holder {
|
||||
Repos: []config.Repo{
|
||||
{Owner: "acme", Name: "widget", Base: "main", Gate: [][]string{{"task", "check"}}},
|
||||
},
|
||||
Agent: config.Agent{Mode: "subprocess", Command: "/usr/local/bin/archie-agent", Env: []string{"HOME"}},
|
||||
NATS: config.NATSConfig{URL: "nats://127.0.0.1:4222", TokenEnv: "NATS_TOKEN"},
|
||||
LegacyAgent: config.LegacyAgent{Mode: "subprocess", Command: "/usr/local/bin/archie-agent", Env: []string{"HOME"}},
|
||||
NATS: config.NATSConfig{URL: "nats://127.0.0.1:4222", TokenEnv: "NATS_TOKEN"},
|
||||
Chat: config.ChatConfig{
|
||||
Telegram: config.TelegramConfig{TokenEnv: "TELEGRAM_TOKEN"},
|
||||
},
|
||||
@@ -140,12 +140,12 @@ func TestHandleConfigSafeFieldsPresent(t *testing.T) {
|
||||
if got.Providers["openai"].APIKeyEnv != "OPENAI_API_KEY" {
|
||||
t.Errorf("Providers[openai].APIKeyEnv = %q, want OPENAI_API_KEY", got.Providers["openai"].APIKeyEnv)
|
||||
}
|
||||
if got.Agent.Mode != "subprocess" {
|
||||
t.Errorf("Agent.Mode = %q, want subprocess", got.Agent.Mode)
|
||||
}
|
||||
if got.Storage.WorkDir != "/work/archie" {
|
||||
t.Errorf("Storage.WorkDir = %q", got.Storage.WorkDir)
|
||||
}
|
||||
if strings.Contains(w.Body.String(), `"agent"`) {
|
||||
t.Errorf("removed agent execution selector leaked through config API: %s", w.Body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleConfigIncludesReloadStatus proves a failed reload is surfaced
|
||||
|
||||
Reference in new issue
Block a user