Files
archie/internal/infrastructure/agenttransport/nats/transport.go
T
sam 3f0071e27b feat(statestore): step_executions, StartStep/FinishStep under the run credential
Plan item 3 of docs/prds/execution-tree-state-machine.md (archie-core-1220), on
top of the transition tables and ErrIllegalTransition enforcement (1214/1215,
archie-core-qdbo.5):

- Migration 0017 adds step_executions -- id, org_id, workspace_id, execution_id,
  attempt, parent_id, depth, kind, name, called_execution_id, status, detail,
  tokens_used, started_at, finished_at, indexed on (execution_id, attempt, id).
  Ownership fields are stamped from the execution's row; depth is derived from
  the parent, never supplied by a caller. Existing rows gain no backfilled
  steps.
- task.Store (the workflow engine's mid-run facade) gains StartStep/FinishStep
  (5 methods, within the 8 cap), with storecontract.StepRecorder fronting them
  on the server; deadlineStore wraps both with its per-call timeout.
- The store enforces each write as one transaction: the guarded row write
  under the shared step transition table plus the transition's own
  stage_start/stage_finish event row. Staleness is decided before legality,
  the way guardTransition decides it, and the store returns the persisted
  event for the caller's post-commit publish -- the EmitDurable convention
  (the daemon's event sink skips already-persisted rows by assigned ID), which
  is why workflow.Run stops emitting stage events beside the store and no
  longer discards step-write errors: a failed start or finish parks the
  execution, and the stage does not run unrecorded. An interrupted stage
  records interrupted rather than failed; a recording failure on that
  shutdown path is logged and left to RecoverStale, which owns the
  interrupted-execution sweep in plan item 6.
- The run credential of docs/prds/orgs-and-access.md authorises StartStep and
  FinishStep on its own execution only: both requests carry the execution ID
  the grant check verifies (FinishStep carries it because the interceptor
  never reads the database), and FinishStep verifies handler-side that the
  step really is that execution's. authorizesTaskScopedCall's sanctioned
  widenings are now held as a lookup table so the next one stops growing a
  switch.
- The PRD's wire contract is StartStep(execution, attempt, parent, kind, name)
  -> step id; the responses also carry the persisted event (needed for the
  post-commit publish) and FinishStep's request carries execution_id (needed
  for the grant check) -- both noted here as deliberate additions to the PRD's
  shorthand. called_execution_id exists in the table but not yet on the wire:
  its named consumer is the workflow-call recording of plan item 4 (#1216).

Conformance drives both adapters; grant tests pin the own-execution scoping;
the wire sentinel set gains ErrInvalidStep (InvalidArgument, "step execution
invalid") for a step kind no consumer names.
2026-09-27 05:42:32 +10:00

358 lines
12 KiB
Go

// Package nats implements the agent worker's broker and RPC boundaries over NATS.
// Raw NATS SDK types and wire encoding stay in this infrastructure package.
package nats
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"strconv"
"strings"
"time"
natsio "github.com/nats-io/nats.go"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/forgerpc"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/taskrun"
"github.com/samcharles93/archie-core/internal/worktreerpc"
)
// Config contains the broker endpoint and credential for the worker
// transport, plus the State Store gRPC target (docs/prds/
// state-store-contract.md §6). StateStoreURL is required: the agent is fully
// on gRPC for its store calls, and the legacy NATS storerpc path is deleted
// (docs/prds/state-store-contract.md §12 step 4).
type Config struct {
URL string
Token string
StateStoreURL string
StateStoreToken string
}
const (
taskSubscriptionFlushTimeout = 5 * time.Second
)
type sdkSubscription interface {
Unsubscribe() error
}
// 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 {
conn *natsio.Conn
// state is the long-lived State Store contract client and closeState its
// connection cleanup. Store() dials nothing per call; it wraps this one
// client with the per-call timeout each caller supplies. Connect refuses
// to construct a Transport without one (the legacy NATS storerpc path is
// deleted), so it is never nil in a real worker.
state *staterpc.Client
closeState func()
subscribe func(string, natsio.MsgHandler) (sdkSubscription, error)
flush func(time.Duration) error
}
// Connect establishes the worker's core-NATS connection and, when
// Config.StateStoreURL is set, a long-lived gRPC connection to the State
// Store (docs/prds/state-store-contract.md §6, §9). A non-loopback
// StateStoreURL with no token fails closed rather than dialing an
// unauthenticated remote store.
func Connect(ctx context.Context, config Config, log *slog.Logger) (*Transport, error) {
if log == nil {
log = slog.New(slog.DiscardHandler)
}
if err := ctx.Err(); err != nil {
return nil, 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())
// The agent is fully on gRPC for its store calls; the legacy NATS
// storerpc path is deleted (docs/prds/state-store-contract.md §12 step
// 4). A missing StateStoreURL fails fast rather than letting Store()
// hand back a client over a nil connection.
if config.StateStoreURL == "" {
conn.Close()
return nil, fmt.Errorf("state store target is required: archie-agent no longer supports the legacy NATS storerpc path (docs/prds/state-store-contract.md §12 step 4)")
}
state, closeState, err := staterpc.Dial(config.StateStoreURL, config.StateStoreToken)
if err != nil {
conn.Close()
return nil, fmt.Errorf("connect state store: %w", err)
}
log.Info("worker state store transport connected", "url", config.StateStoreURL)
return &Transport{
conn: conn,
state: state,
closeState: closeState,
subscribe: func(subject string, handler natsio.MsgHandler) (sdkSubscription, error) {
return conn.Subscribe(subject, handler)
},
flush: conn.FlushTimeout,
}, nil
}
// Close releases all broker resources.
func (t *Transport) Close() {
t.conn.Close()
if t.closeState != nil {
t.closeState()
}
}
// LogPublisher returns the narrow fire-and-forget capability used by system logs.
func (t *Transport) LogPublisher() agentexec.LogPublisher { return t.conn }
// EventPublisher returns the narrow fire-and-forget capability used to ship
// a task's workflow events (stage progress, outcome, parking) back to the
// daemon over NATS.
func (t *Transport) EventPublisher() agentexec.EventPublisher { return t.conn }
// RemoteTrees is the proxied half of workflow.Trees.
type RemoteTrees interface {
Push(ctx context.Context) error
}
// The top-level forgerpc and worktreerpc packages remain legacy infrastructure
// with an open final destination. This adapter is their single worker
// composition point until that broader migration is approved. The State Store
// is the exception: it is a gRPC contract (staterpc), and this adapter's
// Store() returns that client directly.
// Forger constructs the identity-scoped forge RPC client.
func (t *Transport) Forger(identity string, timeout time.Duration) workflow.Forger {
return &forgerpc.Client{Conn: t.conn, Timeout: timeout, Identity: identity}
}
// Store constructs the workflow store RPC client over the long-lived State
// Store client (docs/prds/state-store-contract.md §6). Each call is bounded
// by timeout when the caller's context carries no deadline of its own. The
// legacy NATS storerpc path is deleted (§12 step 4); a transport without a
// State Store client cannot reach Store() because Connect refused to build
// one.
func (t *Transport) Store(timeout time.Duration) workflow.Store {
return deadlineStore{Store: t.state, timeout: timeout}
}
// Calls constructs the workflow.call RPC client over the same long-lived
// State Store client (docs/prds/workflow-calls.md), bounded by timeout the
// same way Store() is.
func (t *Transport) Calls(timeout time.Duration) task.Caller {
return deadlineCaller{Caller: t.state, timeout: timeout}
}
// deadlineCaller applies a default per-call timeout to task.Caller calls
// whose context carries no deadline of its own, matching deadlineStore.
type deadlineCaller struct {
task.Caller
timeout time.Duration
}
func (d deadlineCaller) withDeadline(ctx context.Context) (context.Context, context.CancelFunc) {
if _, ok := ctx.Deadline(); ok || d.timeout <= 0 {
return ctx, func() {}
}
return context.WithTimeout(ctx, d.timeout)
}
func (d deadlineCaller) StartCall(ctx context.Context, callerTaskID int64, wf string, inputs map[string]any) (*task.Task, error) {
ctx, cancel := d.withDeadline(ctx)
defer cancel()
return d.Caller.StartCall(ctx, callerTaskID, wf, inputs)
}
func (d deadlineCaller) CallStatus(ctx context.Context, callerTaskID, callTaskID int64) (string, string, error) {
ctx, cancel := d.withDeadline(ctx)
defer cancel()
return d.Caller.CallStatus(ctx, callerTaskID, callTaskID)
}
// deadlineStore applies a default per-call timeout to workflow.Store calls
// whose context carries no deadline of its own, matching storerpc.Client's
// Timeout field semantics for the gRPC path.
type deadlineStore struct {
workflow.Store
timeout time.Duration
}
func (d deadlineStore) withDeadline(ctx context.Context) (context.Context, context.CancelFunc) {
if _, ok := ctx.Deadline(); ok || d.timeout <= 0 {
return ctx, func() {}
}
return context.WithTimeout(ctx, d.timeout)
}
func (d deadlineStore) Update(ctx context.Context, t *workflow.Task) error {
ctx, cancel := d.withDeadline(ctx)
defer cancel()
return d.Store.Update(ctx, t)
}
func (d deadlineStore) Transition(ctx context.Context, taskID int64, from, to, detail string) error {
ctx, cancel := d.withDeadline(ctx)
defer cancel()
return d.Store.Transition(ctx, taskID, from, to, detail)
}
func (d deadlineStore) InsertEvent(ctx context.Context, e events.Event) (int64, error) {
ctx, cancel := d.withDeadline(ctx)
defer cancel()
return d.Store.InsertEvent(ctx, e)
}
func (d deadlineStore) StartStep(ctx context.Context, s task.StepStart) (int64, events.Event, error) {
ctx, cancel := d.withDeadline(ctx)
defer cancel()
return d.Store.StartStep(ctx, s)
}
func (d deadlineStore) FinishStep(ctx context.Context, s task.StepFinish) (events.Event, error) {
ctx, cancel := d.withDeadline(ctx)
defer cancel()
return d.Store.FinishStep(ctx, s)
}
// Trees constructs the identity-scoped worktree RPC client.
func (t *Transport) Trees(identity, grant string, timeout time.Duration) RemoteTrees {
return &worktreerpc.Client{Conn: t.conn, Timeout: timeout, Identity: identity, Grant: grant}
}
// TaskHandler executes one decoded full-task request.
type TaskHandler func(context.Context, taskrun.Request) (*taskrun.Response, error)
// Subscription is a taskrun subscription owned by the worker lifecycle.
type Subscription interface {
Close() error
}
type subscription struct{ sub sdkSubscription }
func (s subscription) Close() error { return s.sub.Unsubscribe() }
const (
taskRunSubjectPrefix = "archie.taskrun."
)
// SubjectForTask returns the canonical core-NATS subject for one full-task handoff.
func SubjectForTask(taskID int64) string {
return fmt.Sprintf("%s%d", taskRunSubjectPrefix, taskID)
}
func taskIDFromSubject(subject string) (int64, error) {
value, ok := strings.CutPrefix(subject, taskRunSubjectPrefix)
if !ok || value == "" {
return 0, fmt.Errorf("invalid taskrun subject %q", subject)
}
taskID, err := strconv.ParseInt(value, 10, 64)
if err != nil || taskID <= 0 || SubjectForTask(taskID) != subject {
return 0, fmt.Errorf("invalid taskrun subject %q", subject)
}
return taskID, nil
}
// 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)
}
}
flush := t.flush
if flush == nil {
flush = t.conn.FlushTimeout
}
log.Info("taskrun: dedicated per-task subscription", "task", taskID)
sub, err := subscribe(SubjectForTask(taskID), callback)
if err != nil {
return nil, err
}
if err := flush(taskSubscriptionFlushTimeout); err != nil {
if cleanupErr := sub.Unsubscribe(); cleanupErr != nil {
log.Warn("task subscription cleanup failed after flush failure", "err", cleanupErr)
}
return nil, fmt.Errorf("flush task subscription: %w", err)
}
return subscription{sub: sub}, nil
}
func (t *Transport) handleTask(
ctx context.Context,
msg *natsio.Msg,
bootTaskID int64,
handler TaskHandler,
log *slog.Logger,
) {
var request taskrun.Request
if err := json.Unmarshal(msg.Data, &request); err != nil {
log.Error("taskrun decode failed", "err", err)
t.respondTask(msg, nil, fmt.Errorf("decode taskrun request: %w", err), log)
return
}
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
}
response, err := handler(ctx, request)
t.respondTask(msg, response, err, log)
}
func validateTaskRequest(subject string, request taskrun.Request, bootTaskID int64) error {
subjectTaskID, err := taskIDFromSubject(subject)
if err != nil {
return err
}
if err := request.Validate(); err != nil {
return err
}
if request.Task.ID != subjectTaskID {
return fmt.Errorf("task ID %d does not match subject task ID %d", request.Task.ID, subjectTaskID)
}
if subjectTaskID != bootTaskID {
return fmt.Errorf("task ID %d does not match boot task ID %d", subjectTaskID, bootTaskID)
}
return nil
}
func (*Transport) respondTask(msg *natsio.Msg, response *taskrun.Response, runErr error, log *slog.Logger) {
if response == nil {
response = &taskrun.Response{}
}
if runErr != nil {
response.Error = runErr.Error()
}
data, err := json.Marshal(response)
if err != nil {
log.Error("taskrun encode response failed", "err", err)
return
}
if err := msg.Respond(data); err != nil {
log.Error("taskrun respond failed", "err", err)
}
}