Files
archie/internal/agentexec/worker.go
T
sam d3c843e8f5 feat: close final two gaps — agent skill loading + core plugin registry
Agent skill loading: HandleMessage now loads .agents/skills/<workflow>/SKILL.md
from its own workspace before running stages. Skill body prepended to each
stage's Mission. Mirrors daemon-side loadSkillBody but owned by the agent
per PRD section 1. workflowToSkillDir mapping added.

Core plugin registry: internal/plugin package with Plugin interface
(Name, Version), Registry type, and LoadDir function. Yaegi-interprets
.go files from ~/.config/archie/plugins/ at startup. Failed plugins are
skipped. PRD section 5 Layer 2.

All 21 PRD claims now have tests. Zero failing tests.
2026-07-22 03:37:15 +10:00

190 lines
5.6 KiB
Go

package agentexec
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"strings"
"github.com/samcharles93/archie-core/internal/skill"
)
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)
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.
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"
}
// Load the skill body from the worktree — the agent owns skill
// loading per PRD section 1. Mirrors daemon-side loadSkillBody().
if msg.Workflow != "" && msg.Workspace != "" {
body := skill.LoadBody(msg.Workspace, workflowToSkillDir(msg.Workflow))
if body != "" {
msg.Request.Mission = "Follow these project-specific guidelines:\n\n" + body + "\n\n---\n\n" + msg.Request.Mission
for i := range msg.Stages {
msg.Stages[i].Mission = "Follow these project-specific guidelines:\n\n" + body + "\n\n---\n\n" + msg.Stages[i].Mission
}
}
}
// 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)
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)
if runErr != nil {
resp := &AgentResponseEnvelope{
Version: stage.Version,
Result: result,
Error: runErr.Error(),
Channel: channel,
}
return resp, nil
}
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
}
// workflowToSkillDir maps a workflow name to the skill directory name.
// Mirrors the mapping in internal/workflow/agent.go.
func workflowToSkillDir(workflow string) string {
switch workflow {
case "tdd":
return "tdd-bugfix"
case "implement":
return "implement"
case "feasibility":
return "feasibility"
default:
return workflow
}
}
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
}