Files
archie/internal/infrastructure/staterpc/server.go
T
sam f92c5b4536
ci/woodpecker/pr/quality Pipeline was successful
Quality / check (pull_request) Canceled after 7m39s
chore(runtime): delete Kit and external agent harness support (#1877)
Archie runs its own agent loop only. Removes Kit resolution and launch,
the harness runner and adapters, the setup terminal and sandbox view,
harness secrets and OAuth capture (table dropped in migration 0075), the
task-tools contract that only Kit containers reached, and the
sandbox-kit-spec dependency. Agent profiles carry image and tools only.
The egress proxy keeps the model-session path; the sandbox relay code
stays for #1867.
2026-11-12 21:29:05 +11:00

263 lines
12 KiB
Go

// Package staterpc adapts the State Store contract
// (internal/domain/storecontract's producer-owned surfaces plus the
// consumer-owned workflow.Store) to gRPC.
package staterpc
import (
"bytes"
"context"
"errors"
"log/slog"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/samcharles93/archie-core/internal/config"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/alert"
"github.com/samcharles93/archie-core/internal/domain/audit"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/scheduling"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/storepkg"
"github.com/samcharles93/archie-core/internal/domain/usage"
"github.com/samcharles93/archie-core/internal/logging"
)
// Unavailable-capability errors, mirroring gatewayrpc's missing-session-store
// pattern: a nil optional Deps field maps to codes.Unavailable rather than a
// nil-pointer panic.
var (
errCaptureUnavailable = status.Error(codes.Unavailable, "capture store unavailable")
errConfigSnapshotsUnavailable = status.Error(codes.Unavailable, "config snapshot store unavailable")
errChannelStatusUnavailable = status.Error(codes.Unavailable, "channel status store unavailable")
errApplyStatusUnavailable = status.Error(codes.Unavailable, "apply status store unavailable")
errPresenceUnavailable = status.Error(codes.Unavailable, "presence store unavailable")
errRefusalsUnavailable = status.Error(codes.Unavailable, "capture refusal store unavailable")
errMappingUnavailable = status.Error(codes.Unavailable, "mapping store unavailable")
errBindingUnavailable = status.Error(codes.Unavailable, "binding store unavailable")
errSourceUnavailable = status.Error(codes.Unavailable, "source store unavailable")
errBindingDispatchUnavailable = status.Error(codes.Unavailable, "binding dispatcher unavailable")
errBindingTaskCreatorUnavailable = status.Error(codes.Unavailable, "binding task creator unavailable")
errPlaybookDispatcherUnavailable = status.Error(codes.Unavailable, "playbook dispatcher unavailable")
errWorkflowCallerUnavailable = status.Error(codes.Unavailable, "workflow caller unavailable")
// errTaskLogsUnavailable reports that this process cannot read task logs.
errTaskLogsUnavailable = status.Error(codes.Unavailable, msgTaskLogsUnavailable)
)
func timeSeconds(s int64) time.Duration { return time.Duration(s) * time.Second }
// taskLogChunkBytes bounds one streamed message. A log file rotates at
// logging.DefaultMaxSizeMB, well past gRPC's 4MiB unary cap, so the download
// is served in pieces (the same reason captures are streamed)
// and the pieces must be small enough to carry.
const taskLogChunkBytes = 256 << 10
// Deps groups the store surfaces the StateStore service fronts. TaskStore is
// required; the rest are optional (nil disables that group's RPCs, returning
// codes.Unavailable) exactly as their daemon-side consumers already treat a
// nil Mappings/Bindings/BindingDispatcher/BindingTaskCreator as "disabled".
type Deps struct {
ControlPlane controlpb.ControlPlaneServiceServer
Identities identity.Repository
Accounts identity.Accounts
// SubjectBindings fronts the subject-binding surface, which only the
// authenticating path needs. Unset means this process cannot resolve a
// verified subject, and the RPCs answer Unavailable rather than pretending.
SubjectBindings identity.SubjectBinding
// PersonalTokens stores the caller-owned personal API tokens. Optional:
// nil answers Unavailable.
PersonalTokens identity.PersonalTokenStore
Grants *TaskGrants
Packages storepkg.Manager
Tasks storecontract.TaskStore
Captures storecontract.CaptureStore
ConfigSnapshots storecontract.ConfigSnapshotStore
// ChannelStatus is the channel runtime state the process hosting the channels
// reports. Optional: nil disables the pair with codes.Unavailable, which is
// the honest answer for a store service no messaging process is writing to.
ChannelStatus storecontract.ChannelStatusStore
ApplyStatus storecontract.ApplyStatusStore
Presence storecontract.PresenceStore
Refusals storecontract.CaptureRefusalStore
Mappings storecontract.MappingStore
EventTypes storecontract.EventTypeStore
// MappingMatches counts the events each mapping resolved. Optional: nil
// answers RecordMappingMatch with codes.Unavailable.
MappingMatches storecontract.MappingMatchRecorder
Bindings storecontract.BindingStore
Sources storecontract.SourceStore
BindingDispatcher storecontract.BindingDispatcher
BindingTaskCreator storecontract.BindingTaskCreator
// WorkflowCalls is the workflow.call surface:
// the workflow engine's task.Caller over this contract. Optional: nil
// answers both RPCs with codes.Unavailable, which is the honest answer
// for a store that owns no task table.
WorkflowCalls storecontract.WorkflowCaller
// Steps is the step-execution write surface:
// the guarded row writes the
// workflow engine's run credential calls. Optional: nil answers both RPCs
// with codes.Unavailable.
Steps storecontract.StepRecorder
// Usage appends model usage records. Optional: nil answers RecordUsage
// with codes.Unavailable.
Usage usage.Recorder
// UsageTotals reads an org's recorded usage. Optional: nil answers
// UsageTotals with codes.Unavailable.
UsageTotals storecontract.UsageStore
// OrgModels answers an org's effective model aliases and own providers.
// Optional: nil answers TaskModels with codes.Unavailable.
OrgModels func(ctx context.Context, orgID string) (map[string]string, map[string]config.Provider, error)
// OrgInstructions answers the instance instructions and orgID's own.
OrgInstructions func(ctx context.Context, orgID string) (instance, organisation string, err error)
// Canceller is the one cancel path:
// an operator action's guarded write over the execution and its steps.
// Optional: nil answers the RPC with codes.Unavailable.
Canceller storecontract.ExecutionCanceller
// StepReader lists an execution's recorded steps for the dashboard/API:
// a plain read, never
// task-scoped-grant callable. Optional: nil answers ListSteps with
// codes.Unavailable.
StepReader storecontract.StepReader
// PlaybookDispatcher is the side-effecting-action idempotency ledger.
// Optional: nil disables the
// two playbook dispatch RPCs with codes.Unavailable.
PlaybookDispatcher storecontract.PlaybookDispatcher
// Principals assembles the access principal for one identity: the org it
// serves and its memberships. Optional: nil answers GetPrincipal with
// codes.Unavailable.
Principals access.PrincipalSource
// Policies is the stored policy chain.
// Optional: nil answers the access RPCs with codes.Unavailable. The reset
// surface is deliberately not an RPC: `archie-state-store access reset` runs on the
// State Store host, never over the network.
Policies access.PolicyStore
// Denials records and lists denial records. Optional: nil disables the
// pair with codes.Unavailable.
Denials access.DenialStore
// Alerts is the operator alert store. Optional: nil answers the alert
// RPCs with codes.Unavailable.
Alerts alert.Store
// Audit is the org-scoped audit feed. Optional: nil answers both RPCs
// with codes.Unavailable. It is deliberately not task-scoped: a task
// grant must never reach it.
Audit audit.Store
// Schedules stores schedules and their runs. Optional: nil answers the
// schedule RPCs with codes.Unavailable.
Schedules scheduling.Store
ScheduleRuns scheduling.RunStore
// ScheduleSettings reads the instance schedule settings. Nil reads cron
// in UTC.
ScheduleSettings func(ctx context.Context) (scheduling.Settings, error)
// Orgs persists orgs, workspaces, memberships and agent assignments.
// Optional: nil answers the org RPCs with codes.Unavailable.
Orgs org.Repository
// TaskLogs reads task attempt logs. Nil answers ReadTaskLog with
// Unavailable.
TaskLogs storecontract.TaskLogStore
LogFeed *logging.Feed
Log *slog.Logger
}
// server holds what every State Store service shares; each contract has its
// own server type embedding it.
type server struct {
deps Deps
}
// RegisterServer registers the State Store gRPC services against registrar,
// backed by deps. Optionally wrap registrar's interceptor chain with
// TokenInterceptor for the bridge-address (agent-consumed) topology; the caller
// decides.
func RegisterServer(registrar grpc.ServiceRegistrar, deps Deps) {
if deps.Log == nil {
deps.Log = slog.New(slog.DiscardHandler)
}
s := &server{deps: deps}
pb.RegisterPackageServiceServer(registrar, &packageServer{server: s})
pb.RegisterTaskGrantServiceServer(registrar, &taskGrantServer{server: s})
pb.RegisterAccountServiceServer(registrar, &accountServer{server: s})
pb.RegisterIdentityServiceServer(registrar, &identityServer{server: s})
pb.RegisterTaskServiceServer(registrar, &taskServer{server: s})
pb.RegisterExecutionServiceServer(registrar, &executionServer{server: s})
pb.RegisterReviewServiceServer(registrar, &reviewServer{server: s})
pb.RegisterTaskEventServiceServer(registrar, &taskEventServer{server: s})
pb.RegisterStatsServiceServer(registrar, &statsServer{server: s})
pb.RegisterTaskLogServiceServer(registrar, &taskLogServer{server: s})
pb.RegisterStatusServiceServer(registrar, &statusServer{server: s})
pb.RegisterCaptureServiceServer(registrar, &captureServer{server: s})
pb.RegisterMappingServiceServer(registrar, &mappingServer{server: s})
pb.RegisterEventTypeServiceServer(registrar, &eventTypeServer{server: s})
pb.RegisterBindingServiceServer(registrar, &bindingServer{server: s})
pb.RegisterDispatchServiceServer(registrar, &dispatchServer{server: s})
pb.RegisterSourceServiceServer(registrar, &sourceServer{server: s})
pb.RegisterAccessServiceServer(registrar, &accessServer{server: s})
pb.RegisterOrgServiceServer(registrar, &orgServer{server: s})
pb.RegisterAlertServiceServer(registrar, &alertServer{server: s})
pb.RegisterAuditServiceServer(registrar, &auditServer{server: s})
pb.RegisterScheduleServiceServer(registrar, &scheduleServer{server: s})
if deps.ControlPlane != nil {
controlpb.RegisterControlPlaneServiceServer(registrar, deps.ControlPlane)
}
}
func (s *server) logErr(rpc string, err error) error {
s.logFailure(rpc, err)
return mapError(err)
}
// logFailure records a failed call. A cancelled context is the caller going
// away, so it is not a fault.
func (s *server) logFailure(rpc string, err error) {
switch {
case err == nil:
case errors.Is(err, context.Canceled):
s.deps.Log.Debug("staterpc call cancelled", "rpc", rpc)
default:
s.deps.Log.Error("staterpc call failed", "rpc", rpc, "err", err)
}
}
// validationFailure maps a domain validation error to InvalidArgument.
func validationFailure(err error) error {
return status.Error(codes.InvalidArgument, err.Error())
}
// Lifecycle
func (w *chunkWriter) Write(p []byte) (int, error) {
w.pending = append(w.pending, p...)
for len(w.pending) >= taskLogChunkBytes {
if err := w.emit(w.pending[:taskLogChunkBytes]); err != nil {
return 0, err
}
w.pending = w.pending[taskLogChunkBytes:]
}
return len(p), nil
}
func (w *chunkWriter) emit(chunk []byte) error {
w.sent = true
// The slice is copied into the message: the reader's buffer is reused
// across writes and must not be aliased by a message in flight.
return w.stream.Send(&pb.StreamTaskLogContentResponse{
Chunk: bytes.Clone(chunk),
Attempt: w.attempt,
Found: true,
})
}
func (w *chunkWriter) flush() error {
if len(w.pending) == 0 {
return nil
}
chunk := w.pending
w.pending = nil
return w.emit(chunk)
}