Files
archie/internal/infrastructure/staterpc/server.go
T
sam cc5789eb5f feat(scheduling): schedules and runs in the State Store with grid timing and run history
Schedules leave the schedules control-plane resource for State Store tables
behind a ScheduleService contract. A run is claimed per schedule slot, missed
slots fire once, the next run stays on the schedule's grid, cron is read in the
schedule-settings time zone, and failed runs are recorded and advance. Three
failures in a row raise a schedule.failing alert. A fired run records a
schedule-fired capture. Run history is pruned by age and count.

Removes the chat and spawn-task runners, the dangerous-payload guard and the
old schedules settings page.
2026-10-26 00:49:24 +11:00

256 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"
"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
// 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
// HarnessSecrets persists harness OAuth token sets.
// Optional: nil
// answers both RPCs with codes.Unavailable.
HarnessSecrets storecontract.HarnessSecretStore
// 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
// RootIdentity is the identity a task with no identity name runs as.
RootIdentity identity.IdentityID
// 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.RegisterHarnessSecretServiceServer(registrar, &harnessSecretServer{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 {
if err != nil {
s.deps.Log.Error("staterpc call failed", "rpc", rpc, "err", err)
}
return mapError(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)
}