refactor(messaging): sever the Messaging Service from the store and workflow runtime
The PRD's deletion gate for the Messaging Service could not pass: go list -deps ./cmd/archie-messaging linked modernc.org/sqlite, internal/store, internal/domain/workflow and internal/agentexec -- all through one import, archiemessaging/run.go -> internal/app/controlplane, from which the service used three symbols. The decision was to sever rather than amend the gate, because a boundary you whitelist around is no longer a boundary (archie-core-1ng1, recorded in docs/architecture/migration-decisions.md). Measured before touching anything: internal/app/controlplane was the ONLY direct import of the service that dragged those runtimes -- every other one was clean -- so severing this one dependency is sufficient for the gate. Moved into the new internal/infrastructure/controlplanerpc: the generic resource client (NewRPCClient, Query), the RuntimeChatConfig projection, the reader interface (now the exported ResourceReader), the wire sentinels, the status mapping (now ClientError), and the channel-settings document with its decode -- ChannelSettings, TelegramSettings, WebhookChannelSettings, EmailSettings, RateLimitSettings, ChannelDuration, DecodeRenamingLegacyKeys. internal/app/controlplane keeps the store-backed server, the Definition registry, every kind's seed/validator/Normalize, the schema derivation, and its resource-specific reads and watches. Its Client embeds the new one, so the read path has one implementation while the workflow, persona and cronstore reads stay with the processes that already link those engines. The documents are type-aliased back into this package so the store-backed side reads them by the names it has always used: one definition, not a copy on each side of the boundary. The gate is now enforced by cmd/archie-messaging/architecture_test.go, measuring the linked binary against the PRD's banned list, with one exception checked in both directions: internal/domain/workflow/task, the Task status and view vocabulary whose own dependency set holds none of the banned runtimes. The UI gate carries the same entry for the same reason, and the fleet skill names it as the precedent. Verified: task check exit 0. The gate was red-proofed twice -- banning internal/channels fails it, and removing the workflow/task exception fails it -- so neither the loop nor the exception is decorative.
This commit is contained in:
10 files changed
+480
-239
No files matched your search
@@ -0,0 +1,125 @@
|
||||
// This file enforces the objective half of the Messaging Service deletion gate
|
||||
// (docs/prds/messaging-service-boundary.md, "Deletion Gate and Verification"): the
|
||||
// linked binary carries no state-store, daemon, workflow, forge, gateway or
|
||||
// dashboard runtime. Go imports are atomic, so a single reference to a type living
|
||||
// beside a runtime relinks the runtime, and the severance holds only while
|
||||
// something re-measures it. That is exactly how this package came to link the
|
||||
// SQLite driver, the workflow engine and agentexec through one import of
|
||||
// internal/app/controlplane (archie-core-1ng1).
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const modulePath = "github.com/samcharles93/archie-core/"
|
||||
|
||||
// bannedCategory is one class of runtime the Messaging Service must not link.
|
||||
// The names are the PRD's wording; the prefixes are how that class appears in
|
||||
// this module.
|
||||
type bannedCategory struct {
|
||||
name string
|
||||
prefixes []string
|
||||
}
|
||||
|
||||
// deletionGate is the PRD's list, one entry per category it names. Two deliberate
|
||||
// absences:
|
||||
//
|
||||
// - internal/channels is NOT here. The Messaging Service hosts those adapters;
|
||||
// it is the process they live in, not a consumer of them.
|
||||
// - internal/gatewayrpc needs no exception. It is the Gateway's contract, not
|
||||
// its runtime, and it does not sit under the internal/gateway prefix -- the
|
||||
// check below would catch it if it ever did.
|
||||
var deletionGate = []bannedCategory{
|
||||
{"state store", []string{
|
||||
"modernc.org/sqlite",
|
||||
modulePath + "internal/store",
|
||||
}},
|
||||
{"daemon runtime", []string{
|
||||
modulePath + "internal/daemon",
|
||||
modulePath + "internal/app/archied",
|
||||
modulePath + "internal/container",
|
||||
modulePath + "internal/worktree",
|
||||
}},
|
||||
{"workflow engine", []string{
|
||||
modulePath + "internal/domain/workflow",
|
||||
modulePath + "internal/agentexec",
|
||||
modulePath + "internal/taskrun",
|
||||
}},
|
||||
{"forge client", []string{
|
||||
modulePath + "internal/forge",
|
||||
modulePath + "internal/forgerpc",
|
||||
}},
|
||||
{"gateway runtime", []string{
|
||||
modulePath + "internal/gateway",
|
||||
}},
|
||||
{"dashboard", []string{
|
||||
modulePath + "internal/webui",
|
||||
}},
|
||||
}
|
||||
|
||||
// gateExceptions are packages under a banned prefix that carry only contract,
|
||||
// vocabulary or view types, so linking them is the intended end state rather
|
||||
// than a tolerated leak. internal/domain/workflow/task is the Task status and
|
||||
// view vocabulary the Messaging Service renders; its own dependency set holds
|
||||
// none of the banned runtimes, which is what makes the exception defensible
|
||||
// rather than a hole. cmd/archie-ui/architecture_test.go carries the same entry
|
||||
// for the same reason.
|
||||
//
|
||||
// Each one must stay linked: an exception nothing needs any more is a hole in
|
||||
// the fence, so this list is checked in both directions.
|
||||
var gateExceptions = []string{
|
||||
modulePath + "internal/domain/workflow/task",
|
||||
}
|
||||
|
||||
// under reports whether importPath is prefix or a package below it.
|
||||
func under(importPath, prefix string) bool {
|
||||
return importPath == prefix || strings.HasPrefix(importPath, prefix+"/")
|
||||
}
|
||||
|
||||
func TestMessagingProcessLinksNoRuntimeItOnlyDials(t *testing.T) {
|
||||
listed, err := exec.CommandContext(t.Context(), "go", "list", "-deps", ".").CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("go list -deps .: %v\n%s", err, listed)
|
||||
}
|
||||
|
||||
linked := make(map[string]bool, len(gateExceptions))
|
||||
for importPath := range strings.FieldsSeq(string(listed)) {
|
||||
if excepted(importPath, linked) {
|
||||
continue
|
||||
}
|
||||
for _, category := range deletionGate {
|
||||
for _, prefix := range category.prefixes {
|
||||
if !under(importPath, prefix) {
|
||||
continue
|
||||
}
|
||||
t.Errorf("archie-messaging links %s (%s); the Messaging Service reaches that capability over a contract, never in-process (docs/prds/messaging-service-boundary.md, deletion gate)", importPath, category.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, exception := range gateExceptions {
|
||||
if !linked[exception] {
|
||||
t.Errorf("%s is excepted from the deletion gate but is no longer linked; drop the exception instead of leaving its prefix open", exception)
|
||||
}
|
||||
}
|
||||
// A gate that cannot fail, and a `go list` that returns nothing usable, both
|
||||
// read as a pass. This pins the one thing the loop above assumes.
|
||||
if !strings.Contains(string(listed), modulePath+"cmd/archie-messaging") {
|
||||
t.Fatal("go list -deps returned a list that does not even name this package: the gate asserted nothing")
|
||||
}
|
||||
}
|
||||
|
||||
// excepted reports whether importPath sits under an excepted package, recording
|
||||
// the ones actually linked so a stale exception fails.
|
||||
func excepted(importPath string, linked map[string]bool) bool {
|
||||
for _, exception := range gateExceptions {
|
||||
if under(importPath, exception) {
|
||||
linked[exception] = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -5,10 +5,10 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
@@ -39,14 +39,14 @@ func Run(ctx context.Context, o Options) error {
|
||||
}
|
||||
closeStateStore = closeClient
|
||||
defer closeStateStore()
|
||||
chatSettings, channelVersion, settingsErr := controlplane.NewRPCClient(stateStore.ControlPlane()).RuntimeChatConfig(ctx, config.ChatConfig{
|
||||
chatSettings, channelVersion, settingsErr := controlplanerpc.NewRPCClient(stateStore.ControlPlane()).RuntimeChatConfig(ctx, config.ChatConfig{
|
||||
Telegram: cfg.Telegram, Email: cfg.Email, Webhook: cfg.Webhook, WebhookAddr: cfg.WebhookAddr, ShowToolCalls: cfg.ShowToolCalls,
|
||||
})
|
||||
if settingsErr != nil {
|
||||
return fmt.Errorf("load channel settings: %w", settingsErr)
|
||||
}
|
||||
reporter := applystatus.New(applystatus.Messaging, stateStore, log)
|
||||
reporter.Report(ctx, controlplane.ChannelSettingsKind, channelVersion, nil)
|
||||
reporter.Report(ctx, controlplanerpc.ChannelSettingsKind, channelVersion, nil)
|
||||
go reporter.Run(ctx)
|
||||
secrets := secret.NewRegistry()
|
||||
cfg.Telegram, cfg.Email, cfg.Webhook, cfg.WebhookAddr, cfg.ShowToolCalls = chatSettings.Telegram, chatSettings.Email, chatSettings.Webhook, chatSettings.WebhookAddr, chatSettings.ShowToolCalls
|
||||
|
||||
@@ -1,155 +1,31 @@
|
||||
package controlplane
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
)
|
||||
|
||||
// channelSettings is the Channel settings resource document. Its shape IS the
|
||||
// contract the Web UI edits, so every nested object here is defined by this
|
||||
// file with explicit snake_case json tags and never handed an internal config
|
||||
// struct. Encoding/json falls back to Go field names when a tag is absent,
|
||||
// which is how "Window", "MaxRequests", "ListenAddr" and "RelayAddr" reached
|
||||
// operators in the first place -- a shape belonging to TOML, not to this
|
||||
// document.
|
||||
type channelSettings struct {
|
||||
Operator string `json:"operator"`
|
||||
ShowToolCalls bool `json:"show_tool_calls"`
|
||||
MaxSteps int `json:"max_steps"`
|
||||
Models []string `json:"models"`
|
||||
Email emailSettings `json:"email"`
|
||||
WebhookAddr string `json:"webhook_addr"`
|
||||
Webhook webhookChannelSettings `json:"webhook"`
|
||||
Telegram telegramSettings `json:"telegram"`
|
||||
RateLimit rateLimitSettings `json:"rate_limit"`
|
||||
UnrestrictedFilesystem bool `json:"unrestricted_filesystem"`
|
||||
Workspace string `json:"workspace"`
|
||||
}
|
||||
// The channel-settings DOCUMENT lives in internal/infrastructure/controlplanerpc,
|
||||
// beside the client that projects it, so a process that only reads stored
|
||||
// settings does not link this package's store-backed server and workflow engine
|
||||
// (archie-core-1ng1). These aliases keep the store-backed side reading the same
|
||||
// types by the same names it always used, so there is one definition of the
|
||||
// document rather than a copy on each side of the boundary.
|
||||
type (
|
||||
channelSettings = controlplanerpc.ChannelSettings
|
||||
telegramSettings = controlplanerpc.TelegramSettings
|
||||
webhookChannelSettings = controlplanerpc.WebhookChannelSettings
|
||||
emailSettings = controlplanerpc.EmailSettings
|
||||
rateLimitSettings = controlplanerpc.RateLimitSettings
|
||||
channelDuration = controlplanerpc.ChannelDuration
|
||||
)
|
||||
|
||||
type telegramSettings struct {
|
||||
AllowedUserIDs []int64 `json:"allowed_user_ids"`
|
||||
Token config.SecretRef `json:"token_ref"`
|
||||
TokenEnv string `json:"token_env,omitempty"`
|
||||
CredentialConfigured bool `json:"credential_configured"`
|
||||
}
|
||||
|
||||
type webhookChannelSettings struct {
|
||||
Path string `json:"path"`
|
||||
Secret config.SecretRef `json:"secret_ref"`
|
||||
CredentialConfigured bool `json:"credential_configured"`
|
||||
Template string `json:"template"`
|
||||
DeliverTo string `json:"deliver_to"`
|
||||
}
|
||||
|
||||
// emailSettings is the email object. It reads the Go-cased keys an earlier
|
||||
// revision stored as well as its own, because json tags alone would not: the
|
||||
// decoder's case-insensitive fallback compares whole keys, so "ListenAddr"
|
||||
// never matches "listen_addr".
|
||||
type emailSettings struct {
|
||||
ListenAddr string `json:"listen_addr"`
|
||||
RelayAddr string `json:"relay_addr"`
|
||||
}
|
||||
|
||||
func (e *emailSettings) UnmarshalJSON(data []byte) error {
|
||||
type document emailSettings
|
||||
var decoded document
|
||||
err := decodeRenamingLegacyKeys(data, map[string]string{
|
||||
"ListenAddr": "listen_addr",
|
||||
"RelayAddr": "relay_addr",
|
||||
}, &decoded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*e = emailSettings(decoded)
|
||||
return nil
|
||||
}
|
||||
|
||||
// rateLimitSettings is the inbound rate-limit object, and it reads the Go-cased
|
||||
// keys for the reason emailSettings does.
|
||||
type rateLimitSettings struct {
|
||||
Window channelDuration `json:"window"`
|
||||
MaxRequests int `json:"max_requests"`
|
||||
}
|
||||
|
||||
func (r *rateLimitSettings) UnmarshalJSON(data []byte) error {
|
||||
type document rateLimitSettings
|
||||
var decoded document
|
||||
err := decodeRenamingLegacyKeys(data, map[string]string{
|
||||
"Window": "window",
|
||||
"MaxRequests": "max_requests",
|
||||
}, &decoded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*r = rateLimitSettings(decoded)
|
||||
return nil
|
||||
}
|
||||
|
||||
// decodeRenamingLegacyKeys decodes an object into v after renaming each key in
|
||||
// legacy to the spelling v documents, and stays strict about everything else.
|
||||
// The strictness is the point: dropping unknown keys would let a typo
|
||||
// ("windows") read as "unset" and silently disable the setting, which is the
|
||||
// class of failure this shape exists to end.
|
||||
func decodeRenamingLegacyKeys(data []byte, legacy map[string]string, v any) error {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
canonical := make(map[string]json.RawMessage, len(raw))
|
||||
for key, value := range raw {
|
||||
if renamed, ok := legacy[key]; ok {
|
||||
key = renamed
|
||||
}
|
||||
if _, duplicate := canonical[key]; duplicate {
|
||||
return fmt.Errorf("duplicate key %q", key)
|
||||
}
|
||||
canonical[key] = value
|
||||
}
|
||||
renamed, err := json.Marshal(canonical)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(renamed))
|
||||
decoder.DisallowUnknownFields()
|
||||
return decoder.Decode(v)
|
||||
}
|
||||
|
||||
// channelDuration is a duration as this document writes it: the human string
|
||||
// the file accepts ("1m0s", the same spelling config.Duration emits), never a
|
||||
// nanosecond count. It still reads the count, because every document written
|
||||
// before this shape existed carries one, and reading both forms is what lets
|
||||
// those documents keep working without being re-saved.
|
||||
type channelDuration time.Duration
|
||||
|
||||
// Std is the standard-library duration, for arithmetic and for the marker
|
||||
// schema.go uses to recognise a string-form duration (see durationLike).
|
||||
func (d channelDuration) Std() time.Duration { return time.Duration(d) }
|
||||
|
||||
func (d channelDuration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Duration(d).String())
|
||||
}
|
||||
|
||||
func (d *channelDuration) UnmarshalJSON(data []byte) error {
|
||||
var text string
|
||||
if err := json.Unmarshal(data, &text); err == nil {
|
||||
value, err := time.ParseDuration(text)
|
||||
if err != nil {
|
||||
return fmt.Errorf("duration must be a Go duration string such as %q: %w", "1m0s", err)
|
||||
}
|
||||
*d = channelDuration(value)
|
||||
return nil
|
||||
}
|
||||
var nanos int64
|
||||
if err := json.Unmarshal(data, &nanos); err != nil {
|
||||
return fmt.Errorf("duration must be a Go duration string such as %q or a nanosecond count: %w", "1m0s", err)
|
||||
}
|
||||
*d = channelDuration(nanos)
|
||||
return nil
|
||||
}
|
||||
// ChannelSettingsKind is re-exported so the store-backed side and its callers
|
||||
// keep naming one kind by one name.
|
||||
const ChannelSettingsKind = controlplanerpc.ChannelSettingsKind
|
||||
|
||||
func seedChannels(cfg config.Config) any {
|
||||
chat := cfg.Chat
|
||||
|
||||
@@ -8,12 +8,11 @@ import (
|
||||
"io"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/agent"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/cronstore"
|
||||
)
|
||||
|
||||
@@ -23,13 +22,25 @@ import (
|
||||
// workflow definition -- archie-messaging, which only loads channel settings,
|
||||
// and archie-gateway, which reads catalog, runtime settings and personas --
|
||||
// needs no provider set and cannot be failed by one.
|
||||
type Client struct{ rpc pb.ControlPlaneServiceClient }
|
||||
// Client is the control-plane client a process that serves the store uses. It
|
||||
// embeds the Messaging Service's client -- the generic resource read path and
|
||||
// the channel-settings projection -- and adds the resource-specific reads and
|
||||
// watches, which need the workflow and cronstore engines that such a process
|
||||
// already links (archie-core-1ng1).
|
||||
type Client struct {
|
||||
*controlplanerpc.Client
|
||||
rpc pb.ControlPlaneServiceClient
|
||||
}
|
||||
|
||||
// NewClient dials the control plane over an existing connection.
|
||||
func NewClient(conn grpc.ClientConnInterface) *Client {
|
||||
return &Client{rpc: pb.NewControlPlaneServiceClient(conn)}
|
||||
}
|
||||
|
||||
func NewRPCClient(client pb.ControlPlaneServiceClient) *Client { return &Client{rpc: client} }
|
||||
// NewRPCClient wraps an already-constructed generated client.
|
||||
func NewRPCClient(client pb.ControlPlaneServiceClient) *Client {
|
||||
return &Client{Client: controlplanerpc.NewRPCClient(client), rpc: client}
|
||||
}
|
||||
|
||||
// WorkflowDefinitionsClient reads and replaces the workflow-definitions
|
||||
// resource, the one control-plane surface whose stored values name workflow
|
||||
@@ -53,7 +64,7 @@ func NewWorkflowDefinitionsClient(client pb.ControlPlaneServiceClient, steps *wo
|
||||
func (c *Client) Catalog(ctx context.Context) ([]*pb.ResourceDescriptor, error) {
|
||||
response, err := c.rpc.Catalog(ctx, &pb.CatalogRequest{})
|
||||
if err != nil {
|
||||
return nil, clientError(err)
|
||||
return nil, controlplanerpc.ClientError(err)
|
||||
}
|
||||
return response.Resources, nil
|
||||
}
|
||||
@@ -61,7 +72,7 @@ func (c *Client) Catalog(ctx context.Context) ([]*pb.ResourceDescriptor, error)
|
||||
func (c *WorkflowDefinitionsClient) WorkflowDefinitions(ctx context.Context) (workflow.WorkflowDefinitionCollection, int64, error) {
|
||||
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: WorkflowDefinitionsKind})
|
||||
if err != nil {
|
||||
return workflow.WorkflowDefinitionCollection{}, 0, clientError(err)
|
||||
return workflow.WorkflowDefinitionCollection{}, 0, controlplanerpc.ClientError(err)
|
||||
}
|
||||
definitions, err := workflow.DecodeDefinitionCollection(response.Resource.ValueJson, c.steps)
|
||||
return definitions, response.Resource.Version, err
|
||||
@@ -81,7 +92,7 @@ func (c *WorkflowDefinitionsClient) ReplaceWorkflowDefinitions(ctx context.Conte
|
||||
}
|
||||
response, err := c.rpc.Command(ctx, &pb.CommandRequest{Kind: WorkflowDefinitionsKind, Command: "replace", ValueJson: value, ExpectedVersion: expectedVersion, Actor: actor, Source: source, RequestId: requestID})
|
||||
if err != nil {
|
||||
return 0, clientError(err)
|
||||
return 0, controlplanerpc.ClientError(err)
|
||||
}
|
||||
return response.Resource.Version, nil
|
||||
}
|
||||
@@ -89,7 +100,7 @@ func (c *WorkflowDefinitionsClient) ReplaceWorkflowDefinitions(ctx context.Conte
|
||||
func (c *Client) WorkflowExecutionSettings(ctx context.Context) (workflow.ExecutionSettings, int64, error) {
|
||||
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: WorkflowExecutionSettingsKind})
|
||||
if err != nil {
|
||||
return workflow.ExecutionSettings{}, 0, clientError(err)
|
||||
return workflow.ExecutionSettings{}, 0, controlplanerpc.ClientError(err)
|
||||
}
|
||||
settings, err := decodeSettings(response.Resource.ValueJson)
|
||||
return settings, response.Resource.Version, err
|
||||
@@ -105,7 +116,7 @@ func (c *Client) ReplaceWorkflowExecutionSettings(ctx context.Context, settings
|
||||
}
|
||||
response, err := c.rpc.Command(ctx, &pb.CommandRequest{Kind: WorkflowExecutionSettingsKind, Command: "replace", ValueJson: value, ExpectedVersion: expectedVersion, Actor: actor, Source: source, RequestId: requestID})
|
||||
if err != nil {
|
||||
return 0, clientError(err)
|
||||
return 0, controlplanerpc.ClientError(err)
|
||||
}
|
||||
return response.Resource.Version, nil
|
||||
}
|
||||
@@ -113,7 +124,7 @@ func (c *Client) ReplaceWorkflowExecutionSettings(ctx context.Context, settings
|
||||
func (c *Client) WatchWorkflowExecutionSettings(ctx context.Context, afterVersion int64) (<-chan AppliedSettings, error) {
|
||||
stream, err := c.rpc.Watch(ctx, &pb.WatchRequest{Kind: WorkflowExecutionSettingsKind, AfterVersion: afterVersion})
|
||||
if err != nil {
|
||||
return nil, clientError(err)
|
||||
return nil, controlplanerpc.ClientError(err)
|
||||
}
|
||||
return watchUpdates(ctx, stream,
|
||||
func(resource *pb.Resource) AppliedSettings {
|
||||
@@ -132,7 +143,7 @@ type AppliedSettings struct {
|
||||
func (c *Client) Personas(ctx context.Context) (agent.PersonaCollection, int64, error) {
|
||||
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: PersonasKind})
|
||||
if err != nil {
|
||||
return agent.PersonaCollection{}, 0, clientError(err)
|
||||
return agent.PersonaCollection{}, 0, controlplanerpc.ClientError(err)
|
||||
}
|
||||
collection, err := decodePersonas(response.Resource.ValueJson)
|
||||
return collection, response.Resource.Version, err
|
||||
@@ -147,7 +158,7 @@ type AppliedPersonas struct {
|
||||
func (c *Client) WatchPersonas(ctx context.Context, afterVersion int64) (<-chan AppliedPersonas, error) {
|
||||
stream, err := c.rpc.Watch(ctx, &pb.WatchRequest{Kind: PersonasKind, AfterVersion: afterVersion})
|
||||
if err != nil {
|
||||
return nil, clientError(err)
|
||||
return nil, controlplanerpc.ClientError(err)
|
||||
}
|
||||
return watchUpdates(ctx, stream,
|
||||
func(resource *pb.Resource) AppliedPersonas {
|
||||
@@ -174,7 +185,7 @@ func watchUpdates[T any](
|
||||
response, err := stream.Recv()
|
||||
if err != nil {
|
||||
if !errors.Is(err, io.EOF) && ctx.Err() == nil {
|
||||
sendUpdate(ctx, out, failed(clientError(err)))
|
||||
sendUpdate(ctx, out, failed(controlplanerpc.ClientError(err)))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -205,7 +216,7 @@ func sendUpdate[T any](ctx context.Context, out chan<- T, update T) bool {
|
||||
func (c *Client) Schedules(ctx context.Context) ([]cronstore.JobSpec, int64, error) {
|
||||
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: SchedulesKind})
|
||||
if err != nil {
|
||||
return nil, 0, clientError(err)
|
||||
return nil, 0, controlplanerpc.ClientError(err)
|
||||
}
|
||||
var jobs []cronstore.JobSpec
|
||||
if err := json.Unmarshal(response.Resource.ValueJson, &jobs); err != nil {
|
||||
@@ -221,22 +232,7 @@ func (c *Client) ReplaceSchedules(ctx context.Context, jobs []cronstore.JobSpec,
|
||||
}
|
||||
response, err := c.rpc.Command(ctx, &pb.CommandRequest{Kind: SchedulesKind, Command: "replace", ValueJson: value, ExpectedVersion: expectedVersion, Actor: actor, Source: source, RequestId: requestID})
|
||||
if err != nil {
|
||||
return 0, clientError(err)
|
||||
return 0, controlplanerpc.ClientError(err)
|
||||
}
|
||||
return response.Resource.Version, nil
|
||||
}
|
||||
|
||||
func clientError(err error) error {
|
||||
switch status.Code(err) {
|
||||
case codes.InvalidArgument:
|
||||
return fmt.Errorf("%w: %w", ErrValidation, err)
|
||||
case codes.NotFound:
|
||||
return fmt.Errorf("%w: %w", ErrNotFound, err)
|
||||
case codes.Aborted:
|
||||
return fmt.Errorf("%w: %w", ErrVersionConflict, err)
|
||||
case codes.Unavailable:
|
||||
return fmt.Errorf("%w: %w", ErrUnavailable, err)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -12,14 +12,19 @@ import (
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
)
|
||||
|
||||
// The control-plane wire sentinels cross gRPC matched on (code, canonical
|
||||
// message), so their definitions live with the contract in
|
||||
// internal/infrastructure/controlplanerpc. These are the same values re-exported,
|
||||
// so every existing caller keeps one name and errors.Is keeps matching identity.
|
||||
var (
|
||||
ErrValidation = errors.New("control-plane validation")
|
||||
ErrNotFound = errors.New("control-plane resource not found")
|
||||
ErrVersionConflict = errors.New("control-plane version conflict")
|
||||
ErrUnavailable = errors.New("control-plane unavailable")
|
||||
ErrValidation = controlplanerpc.ErrValidation
|
||||
ErrNotFound = controlplanerpc.ErrNotFound
|
||||
ErrVersionConflict = controlplanerpc.ErrVersionConflict
|
||||
ErrUnavailable = controlplanerpc.ErrUnavailable
|
||||
)
|
||||
|
||||
// ResourceStore is the persistence the control plane requires. The composition
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
|
||||
const (
|
||||
RepositoryPoliciesKind = "repository-policies"
|
||||
ChannelSettingsKind = "channel-settings"
|
||||
SchedulingPolicyKind = "scheduling-policy"
|
||||
ToolSettingsKind = "tool-settings"
|
||||
PluginSettingsKind = "plugin-settings"
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
)
|
||||
|
||||
@@ -26,9 +26,6 @@ import (
|
||||
// migration has not reached yet. storeReader never reports absent -- it answers
|
||||
// with the seed the State Store would write -- so the offline verdict stays
|
||||
// boot's.
|
||||
type resourceReader interface {
|
||||
query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error)
|
||||
}
|
||||
|
||||
// RuntimeConfig applies restart-scoped database resources over bootstrap
|
||||
// configuration. Values intentionally absent from a control-plane projection,
|
||||
@@ -44,9 +41,6 @@ func (c *Client) RuntimeConfig(ctx context.Context, base config.Config) (config.
|
||||
|
||||
// RuntimeChatConfig layers the stored channel settings over the file document's
|
||||
// chat section.
|
||||
func (c *Client) RuntimeChatConfig(ctx context.Context, base config.ChatConfig) (config.ChatConfig, int64, error) {
|
||||
return runtimeChatConfigFrom(ctx, c, base)
|
||||
}
|
||||
|
||||
// StoredRuntimeConfig is RuntimeConfig's layering over the store this server
|
||||
// owns, for a caller holding the database file rather than a connection to it.
|
||||
@@ -76,7 +70,7 @@ type storeReader struct {
|
||||
seeds map[string][]byte
|
||||
}
|
||||
|
||||
func (r storeReader) query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
||||
func (r storeReader) Query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
||||
resource, err := r.resources.Resource(ctx, kind)
|
||||
if errors.Is(err, store.ErrResourceNotFound) {
|
||||
seed, ok := r.seeds[kind]
|
||||
@@ -100,7 +94,7 @@ func (r storeReader) query(ctx context.Context, kind string, decode func([]byte)
|
||||
return resource.Version, true, nil
|
||||
}
|
||||
|
||||
func runtimeConfigFrom(ctx context.Context, reader resourceReader, base config.Config) (config.Config, map[string]int64, error) {
|
||||
func runtimeConfigFrom(ctx context.Context, reader controlplanerpc.ResourceReader, base config.Config) (config.Config, map[string]int64, error) {
|
||||
out := base.Clone()
|
||||
versions := map[string]int64{}
|
||||
if err := layerResource(ctx, reader, versions, ProviderSettingsKind, func(value []byte) error {
|
||||
@@ -122,7 +116,7 @@ func runtimeConfigFrom(ctx context.Context, reader resourceReader, base config.C
|
||||
if err := layerResourceJSON(ctx, reader, versions, RepositoryPoliciesKind, &out.Repos); err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
chat, chatVersion, err := runtimeChatConfigFrom(ctx, reader, out.Chat)
|
||||
chat, chatVersion, err := controlplanerpc.RuntimeChatConfigFrom(ctx, reader, out.Chat)
|
||||
if err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
@@ -153,7 +147,7 @@ func runtimeConfigFrom(ctx context.Context, reader resourceReader, base config.C
|
||||
return runtimeToolConfigFrom(ctx, reader, versions, out)
|
||||
}
|
||||
|
||||
func runtimeToolConfigFrom(ctx context.Context, reader resourceReader, versions map[string]int64, out config.Config) (config.Config, map[string]int64, error) {
|
||||
func runtimeToolConfigFrom(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, out config.Config) (config.Config, map[string]int64, error) {
|
||||
if err := layerResource(ctx, reader, versions, ToolSettingsKind, func(value []byte) error {
|
||||
var settings toolSettings
|
||||
if err := json.Unmarshal(value, &settings); err != nil {
|
||||
@@ -199,33 +193,6 @@ func runtimeToolConfigFrom(ctx context.Context, reader resourceReader, versions
|
||||
return out, versions, nil
|
||||
}
|
||||
|
||||
func runtimeChatConfigFrom(ctx context.Context, reader resourceReader, base config.ChatConfig) (config.ChatConfig, int64, error) {
|
||||
out := base
|
||||
version, found, err := reader.query(ctx, ChannelSettingsKind, func(value []byte) error {
|
||||
var settings channelSettings
|
||||
if err := json.Unmarshal(value, &settings); err != nil {
|
||||
return err
|
||||
}
|
||||
check, install := base.Telegram.UpdateCheckCommand, base.Telegram.UpdateInstallCommand
|
||||
out = config.ChatConfig{
|
||||
Operator: settings.Operator, ShowToolCalls: settings.ShowToolCalls, MaxSteps: settings.MaxSteps,
|
||||
Models: settings.Models, Email: config.EmailConfig{ListenAddr: settings.Email.ListenAddr, RelayAddr: settings.Email.RelayAddr}, WebhookAddr: settings.WebhookAddr,
|
||||
Webhook: config.WebhookRoute{Path: settings.Webhook.Path, Secret: settings.Webhook.Secret, Template: settings.Webhook.Template, DeliverTo: settings.Webhook.DeliverTo},
|
||||
Telegram: config.TelegramConfig{AllowedUserIDs: settings.Telegram.AllowedUserIDs, Token: settings.Telegram.Token, TokenEnv: settings.Telegram.TokenEnv, UpdateCheckCommand: check, UpdateInstallCommand: install},
|
||||
RateLimit: config.RateLimitConfig{Window: time.Duration(settings.RateLimit.Window), MaxRequests: settings.RateLimit.MaxRequests},
|
||||
UnrestrictedFilesystem: settings.UnrestrictedFilesystem, Workspace: settings.Workspace,
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return out, 0, err
|
||||
}
|
||||
if !found {
|
||||
return out, 0, nil
|
||||
}
|
||||
return out, version, nil
|
||||
}
|
||||
|
||||
// layerResource decodes a resource and records the version it came from, so the
|
||||
// layering ends up holding the version of every kind it applied.
|
||||
//
|
||||
@@ -234,8 +201,8 @@ func runtimeChatConfigFrom(ctx context.Context, reader resourceReader, base conf
|
||||
// validator refused leaves behind (controlplane.Server.ImportConfig skips it),
|
||||
// and failing here instead would stop the process with a database-named error
|
||||
// that editing config.toml cannot clear.
|
||||
func layerResource(ctx context.Context, reader resourceReader, versions map[string]int64, kind string, decode func([]byte) error) error {
|
||||
version, found, err := reader.query(ctx, kind, decode)
|
||||
func layerResource(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, kind string, decode func([]byte) error) error {
|
||||
version, found, err := reader.Query(ctx, kind, decode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -245,7 +212,7 @@ func layerResource(ctx context.Context, reader resourceReader, versions map[stri
|
||||
return nil
|
||||
}
|
||||
|
||||
func layerResourceJSON(ctx context.Context, reader resourceReader, versions map[string]int64, kind string, target any) error {
|
||||
func layerResourceJSON(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, kind string, target any) error {
|
||||
return layerResource(ctx, reader, versions, kind, func(value []byte) error { return json.Unmarshal(value, target) })
|
||||
}
|
||||
|
||||
@@ -253,20 +220,3 @@ func layerResourceJSON(ctx context.Context, reader resourceReader, versions map[
|
||||
// a resource in can report the version they applied. A kind the store holds no
|
||||
// value for is reported as not found rather than as an error: the caller leaves
|
||||
// the file document's value in effect (see resourceReader).
|
||||
func (c *Client) query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
||||
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: kind})
|
||||
if err != nil {
|
||||
mapped := clientError(err)
|
||||
if errors.Is(mapped, ErrNotFound) {
|
||||
return 0, false, nil
|
||||
}
|
||||
return 0, false, mapped
|
||||
}
|
||||
if response.Resource == nil {
|
||||
return 0, false, fmt.Errorf("%s resource missing", kind)
|
||||
}
|
||||
if err := decode(response.Resource.ValueJson); err != nil {
|
||||
return 0, false, fmt.Errorf("decode %s: %w", kind, err)
|
||||
}
|
||||
return response.Resource.Version, true, nil
|
||||
}
|
||||
@@ -247,7 +247,7 @@ func layerToolSettings(t *testing.T, stored []byte, base config.Config) config.C
|
||||
// kind absent.
|
||||
type toolSettingsReader struct{ value []byte }
|
||||
|
||||
func (r toolSettingsReader) query(_ context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
||||
func (r toolSettingsReader) Query(_ context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
||||
if kind != ToolSettingsKind {
|
||||
return 0, false, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
// Package controlplanerpc is the Messaging Service side of the control-plane
|
||||
// contract: the generic resource client, the channel-settings document it
|
||||
// projects, and the wire sentinels both sides match on. It exists so a service
|
||||
// that only reads stored settings does not link the store-backed server, the
|
||||
// workflow engine or the SQLite driver (archie-core-1ng1).
|
||||
package controlplanerpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
)
|
||||
|
||||
// ChannelSettingsKind is the control-plane resource kind holding the channel
|
||||
// settings document. It is declared here because this package owns the document
|
||||
// and the client names the kind; internal/app/controlplane re-exports it, so the
|
||||
// store-backed side and its callers keep one name for one kind.
|
||||
const ChannelSettingsKind = "channel-settings"
|
||||
|
||||
var (
|
||||
ErrValidation = errors.New("control-plane validation")
|
||||
ErrNotFound = errors.New("control-plane resource not found")
|
||||
ErrVersionConflict = errors.New("control-plane version conflict")
|
||||
ErrUnavailable = errors.New("control-plane unavailable")
|
||||
)
|
||||
|
||||
// ResourceStore is the persistence the control plane requires. The composition
|
||||
// root asserts the State Store against it, so this is the single definition of
|
||||
// what a control-plane backing store must offer.
|
||||
|
||||
type Client struct{ rpc pb.ControlPlaneServiceClient }
|
||||
|
||||
func NewClient(conn grpc.ClientConnInterface) *Client {
|
||||
return &Client{rpc: pb.NewControlPlaneServiceClient(conn)}
|
||||
}
|
||||
|
||||
func NewRPCClient(client pb.ControlPlaneServiceClient) *Client { return &Client{rpc: client} }
|
||||
|
||||
// WorkflowDefinitionsClient reads and replaces the workflow-definitions
|
||||
// resource, the one control-plane surface whose stored values name workflow
|
||||
// step types. Resolving those names needs the step vocabulary the process
|
||||
// registered at its composition root, so the vocabulary is a constructor
|
||||
// dependency of this surface and of no other: a caller that cannot reach
|
||||
// workflow definitions cannot ask for one.
|
||||
|
||||
// ResourceReader is the read side of the control-plane client: one resource
|
||||
// query per kind. The store-backed server implements it too, which is why the
|
||||
// layering functions here are shared with internal/app/controlplane.
|
||||
type ResourceReader interface {
|
||||
Query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error)
|
||||
}
|
||||
|
||||
func (c *Client) Query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
||||
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: kind})
|
||||
if err != nil {
|
||||
mapped := ClientError(err)
|
||||
if errors.Is(mapped, ErrNotFound) {
|
||||
return 0, false, nil
|
||||
}
|
||||
return 0, false, mapped
|
||||
}
|
||||
if response.Resource == nil {
|
||||
return 0, false, fmt.Errorf("%s resource missing", kind)
|
||||
}
|
||||
if err := decode(response.Resource.ValueJson); err != nil {
|
||||
return 0, false, fmt.Errorf("decode %s: %w", kind, err)
|
||||
}
|
||||
return response.Resource.Version, true, nil
|
||||
}
|
||||
|
||||
// RuntimeChatConfig layers the stored channel settings over the file document's
|
||||
// chat section.
|
||||
func (c *Client) RuntimeChatConfig(ctx context.Context, base config.ChatConfig) (config.ChatConfig, int64, error) {
|
||||
return RuntimeChatConfigFrom(ctx, c, base)
|
||||
}
|
||||
|
||||
func RuntimeChatConfigFrom(ctx context.Context, reader ResourceReader, base config.ChatConfig) (config.ChatConfig, int64, error) {
|
||||
out := base
|
||||
version, found, err := reader.Query(ctx, ChannelSettingsKind, func(value []byte) error {
|
||||
var settings ChannelSettings
|
||||
if err := json.Unmarshal(value, &settings); err != nil {
|
||||
return err
|
||||
}
|
||||
check, install := base.Telegram.UpdateCheckCommand, base.Telegram.UpdateInstallCommand
|
||||
out = config.ChatConfig{
|
||||
Operator: settings.Operator, ShowToolCalls: settings.ShowToolCalls, MaxSteps: settings.MaxSteps,
|
||||
Models: settings.Models, Email: config.EmailConfig{ListenAddr: settings.Email.ListenAddr, RelayAddr: settings.Email.RelayAddr}, WebhookAddr: settings.WebhookAddr,
|
||||
Webhook: config.WebhookRoute{Path: settings.Webhook.Path, Secret: settings.Webhook.Secret, Template: settings.Webhook.Template, DeliverTo: settings.Webhook.DeliverTo},
|
||||
Telegram: config.TelegramConfig{AllowedUserIDs: settings.Telegram.AllowedUserIDs, Token: settings.Telegram.Token, TokenEnv: settings.Telegram.TokenEnv, UpdateCheckCommand: check, UpdateInstallCommand: install},
|
||||
RateLimit: config.RateLimitConfig{Window: time.Duration(settings.RateLimit.Window), MaxRequests: settings.RateLimit.MaxRequests},
|
||||
UnrestrictedFilesystem: settings.UnrestrictedFilesystem, Workspace: settings.Workspace,
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return out, 0, err
|
||||
}
|
||||
if !found {
|
||||
return out, 0, nil
|
||||
}
|
||||
return out, version, nil
|
||||
}
|
||||
|
||||
// layerResource decodes a resource and records the version it came from, so the
|
||||
// layering ends up holding the version of every kind it applied.
|
||||
//
|
||||
// A kind with no stored value is not an error and records no version: the file
|
||||
// document's value stays in effect. That is the state a seed the resource
|
||||
// validator refused leaves behind (controlplane.Server.ImportConfig skips it),
|
||||
// and failing here instead would stop the process with a database-named error
|
||||
// that editing config.toml cannot clear.
|
||||
|
||||
// ClientError maps a gRPC status onto the wire sentinels both sides match on.
|
||||
// It is exported because the store-backed server's own client methods need the
|
||||
// same mapping, and a second copy would be a second contract.
|
||||
func ClientError(err error) error {
|
||||
switch status.Code(err) {
|
||||
case codes.InvalidArgument:
|
||||
return fmt.Errorf("%w: %w", ErrValidation, err)
|
||||
case codes.NotFound:
|
||||
return fmt.Errorf("%w: %w", ErrNotFound, err)
|
||||
case codes.Aborted:
|
||||
return fmt.Errorf("%w: %w", ErrVersionConflict, err)
|
||||
case codes.Unavailable:
|
||||
return fmt.Errorf("%w: %w", ErrUnavailable, err)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package controlplanerpc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
// ChannelSettings is the Channel settings resource document. Its shape IS the
|
||||
// contract the Web UI edits, so every nested object here is defined by this
|
||||
// file with explicit snake_case json tags and never handed an internal config
|
||||
// struct. Encoding/json falls back to Go field names when a tag is absent,
|
||||
// which is how "Window", "MaxRequests", "ListenAddr" and "RelayAddr" reached
|
||||
// operators in the first place -- a shape belonging to TOML, not to this
|
||||
// document.
|
||||
type ChannelSettings struct {
|
||||
Operator string `json:"operator"`
|
||||
ShowToolCalls bool `json:"show_tool_calls"`
|
||||
MaxSteps int `json:"max_steps"`
|
||||
Models []string `json:"models"`
|
||||
Email EmailSettings `json:"email"`
|
||||
WebhookAddr string `json:"webhook_addr"`
|
||||
Webhook WebhookChannelSettings `json:"webhook"`
|
||||
Telegram TelegramSettings `json:"telegram"`
|
||||
RateLimit RateLimitSettings `json:"rate_limit"`
|
||||
UnrestrictedFilesystem bool `json:"unrestricted_filesystem"`
|
||||
Workspace string `json:"workspace"`
|
||||
}
|
||||
|
||||
type TelegramSettings struct {
|
||||
AllowedUserIDs []int64 `json:"allowed_user_ids"`
|
||||
Token config.SecretRef `json:"token_ref"`
|
||||
TokenEnv string `json:"token_env,omitempty"`
|
||||
CredentialConfigured bool `json:"credential_configured"`
|
||||
}
|
||||
|
||||
type WebhookChannelSettings struct {
|
||||
Path string `json:"path"`
|
||||
Secret config.SecretRef `json:"secret_ref"`
|
||||
CredentialConfigured bool `json:"credential_configured"`
|
||||
Template string `json:"template"`
|
||||
DeliverTo string `json:"deliver_to"`
|
||||
}
|
||||
|
||||
// EmailSettings is the email object. It reads the Go-cased keys an earlier
|
||||
// revision stored as well as its own, because json tags alone would not: the
|
||||
// decoder's case-insensitive fallback compares whole keys, so "ListenAddr"
|
||||
// never matches "listen_addr".
|
||||
type EmailSettings struct {
|
||||
ListenAddr string `json:"listen_addr"`
|
||||
RelayAddr string `json:"relay_addr"`
|
||||
}
|
||||
|
||||
func (e *EmailSettings) UnmarshalJSON(data []byte) error {
|
||||
type document EmailSettings
|
||||
var decoded document
|
||||
err := DecodeRenamingLegacyKeys(data, map[string]string{
|
||||
"ListenAddr": "listen_addr",
|
||||
"RelayAddr": "relay_addr",
|
||||
}, &decoded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*e = EmailSettings(decoded)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RateLimitSettings is the inbound rate-limit object, and it reads the Go-cased
|
||||
// keys for the reason EmailSettings does.
|
||||
type RateLimitSettings struct {
|
||||
Window ChannelDuration `json:"window"`
|
||||
MaxRequests int `json:"max_requests"`
|
||||
}
|
||||
|
||||
func (r *RateLimitSettings) UnmarshalJSON(data []byte) error {
|
||||
type document RateLimitSettings
|
||||
var decoded document
|
||||
err := DecodeRenamingLegacyKeys(data, map[string]string{
|
||||
"Window": "window",
|
||||
"MaxRequests": "max_requests",
|
||||
}, &decoded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*r = RateLimitSettings(decoded)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeRenamingLegacyKeys decodes an object into v after renaming each key in
|
||||
// legacy to the spelling v documents, and stays strict about everything else.
|
||||
// The strictness is the point: dropping unknown keys would let a typo
|
||||
// ("windows") read as "unset" and silently disable the setting, which is the
|
||||
// class of failure this shape exists to end.
|
||||
func DecodeRenamingLegacyKeys(data []byte, legacy map[string]string, v any) error {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
canonical := make(map[string]json.RawMessage, len(raw))
|
||||
for key, value := range raw {
|
||||
if renamed, ok := legacy[key]; ok {
|
||||
key = renamed
|
||||
}
|
||||
if _, duplicate := canonical[key]; duplicate {
|
||||
return fmt.Errorf("duplicate key %q", key)
|
||||
}
|
||||
canonical[key] = value
|
||||
}
|
||||
renamed, err := json.Marshal(canonical)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(renamed))
|
||||
decoder.DisallowUnknownFields()
|
||||
return decoder.Decode(v)
|
||||
}
|
||||
|
||||
// ChannelDuration is a duration as this document writes it: the human string
|
||||
// the file accepts ("1m0s", the same spelling config.Duration emits), never a
|
||||
// nanosecond count. It still reads the count, because every document written
|
||||
// before this shape existed carries one, and reading both forms is what lets
|
||||
// those documents keep working without being re-saved.
|
||||
type ChannelDuration time.Duration
|
||||
|
||||
// Std is the standard-library duration, for arithmetic and for the marker
|
||||
// schema.go uses to recognise a string-form duration (see durationLike).
|
||||
func (d ChannelDuration) Std() time.Duration { return time.Duration(d) }
|
||||
|
||||
func (d ChannelDuration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Duration(d).String())
|
||||
}
|
||||
|
||||
func (d *ChannelDuration) UnmarshalJSON(data []byte) error {
|
||||
var text string
|
||||
if err := json.Unmarshal(data, &text); err == nil {
|
||||
value, err := time.ParseDuration(text)
|
||||
if err != nil {
|
||||
return fmt.Errorf("duration must be a Go duration string such as %q: %w", "1m0s", err)
|
||||
}
|
||||
*d = ChannelDuration(value)
|
||||
return nil
|
||||
}
|
||||
var nanos int64
|
||||
if err := json.Unmarshal(data, &nanos); err != nil {
|
||||
return fmt.Errorf("duration must be a Go duration string such as %q or a nanosecond count: %w", "1m0s", err)
|
||||
}
|
||||
*d = ChannelDuration(nanos)
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user