Files
archie/internal/infrastructure/controlplanerpc/vocabulary.go
T
sam a7d6cc8c9d 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.
2026-09-22 07:26:55 +10:00

153 lines
5.5 KiB
Go

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
}