feat(gateway): delete in-process ChatContract path, make remote the only mode

archie-gateway is now the sole owner of ChatContract; the daemon always
dials it over gRPC for webui chat, no in-process fallback left. Telegram
and email keep their own in-process routers unchanged (their streaming
responder is a callback that can't cross the wire-safe contract).

- composeChatContract only accepts mode="remote"; config defaults
  Services.Gateway to remote + 127.0.0.1:8585 when unset
- deleted taskActionsActor (dead code once the local branch is gone)
- task dev now launches archie-gateway alongside archied + Vite

Verified end-to-end: archie-gateway + archied booted against a shared
NATS/store, webui's /api/chat/sessions round-tripped through the gRPC hop.
This commit is contained in:
sam committed 2026-09-06 02:07:02 +10:00
1 parent 10e76e7528
commit bec9caa24e
12 files changed
+108 -123

No files matched your search

+6 -20
View File
@@ -486,29 +486,15 @@ func (b *boot) setupContainers(ctx context.Context) func() {
// setupLLMAndChat wires the runtime, tool registry, model management,
// personas and the dashboard's chat service.
func (b *boot) setupLLMAndChat(ctx context.Context) error {
func (b *boot) setupLLMAndChat() error {
cfg := b.cfg
// The daemon owns the local gateway in default mode: the modular
// monolith keeps serving the ChatContract in-process, with the
// standalone archie-gateway binary prepared as a remote alternative
// (mode = "remote"). The local and remote adapters both satisfy
// gateway.ChatContract; composition picks the transport.
var local gateway.ChatContract
if cfg.Services.Gateway.Mode != "remote" {
// Full local contract: router, sessions, models, personas, task
// commands and the LLM responder. The in-process task-actions
// actor applies mutations directly to the store the daemon owns
// -- no HTTP detour through webui, no NATS hop.
local = b.setupGatewayChat(ctx, taskActionsActor{b})
} else {
// The daemon still runs its own Telegram/email routers in Phase 1,
// so the runtime objects survive even though the webui chat goes
// over gRPC to the standalone Gateway.
b.setupChatRuntime(cfg)
}
// Telegram/email keep their own in-process routers rather than going
// through ChatContract: their streaming responder is a callback, and
// ChatContract's wire-safe interface can't carry one.
b.setupChatRuntime(cfg)
contract, cleanup, err := composeChatContract(cfg.Services.Gateway, local)
contract, cleanup, err := composeChatContract(cfg.Services.Gateway)
if err != nil {
return err
}
+17 -25
View File
@@ -12,30 +12,22 @@ import (
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
)
// composeChatContract selects transport without involving network discovery
// in local calls. The returned cleanup belongs to the application lifecycle.
func composeChatContract(settings config.ServiceConnection, local gateway.ChatContract, options ...grpc.DialOption) (gateway.ChatContract, func(), error) {
switch settings.Mode {
case "", "inproc":
if settings.Target != "" {
return nil, nil, fmt.Errorf("gateway target requires remote mode")
}
return local, func() {}, nil
case "remote":
if strings.TrimSpace(settings.Target) == "" {
return nil, nil, fmt.Errorf("remote gateway requires a target")
}
// The first extraction uses a loopback gRPC hop. Deployments that put
// the Gateway on a separate trust boundary supply their own transport
// credentials through options; the local process split must be usable
// without manufacturing a certificate in the config loader.
opts := append([]grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}, options...)
conn, err := grpc.NewClient(settings.Target, opts...)
if err != nil {
return nil, nil, fmt.Errorf("create gateway client: %w", err)
}
return gatewayrpc.NewClient(conn), func() { _ = conn.Close() }, nil
default:
return nil, nil, fmt.Errorf("unknown gateway service mode %q", settings.Mode)
// composeChatContract dials the standalone archie-gateway, the sole owner of
// ChatContract.
func composeChatContract(settings config.ServiceConnection, options ...grpc.DialOption) (gateway.ChatContract, func(), error) {
if settings.Mode != "remote" {
return nil, nil, fmt.Errorf("unsupported gateway service mode %q; archie-gateway is required, see deployments/README.md", settings.Mode)
}
if strings.TrimSpace(settings.Target) == "" {
return nil, nil, fmt.Errorf("remote gateway requires a target")
}
// Deployments that put the Gateway on a separate trust boundary supply
// their own transport credentials through options; the default must be
// usable without manufacturing a certificate in the config loader.
opts := append([]grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}, options...)
conn, err := grpc.NewClient(settings.Target, opts...)
if err != nil {
return nil, nil, fmt.Errorf("create gateway client: %w", err)
}
return gatewayrpc.NewClient(conn), func() { _ = conn.Close() }, nil
}
+4 -24
View File
@@ -17,38 +17,19 @@ import (
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
)
func TestComposeChatContractLocal(t *testing.T) {
for _, mode := range []string{"", "inproc"} {
t.Run("mode="+mode, func(t *testing.T) {
local := &gateway.LocalChatAdapter{}
chat, cleanup, err := composeChatContract(config.ServiceConnection{Mode: mode}, local)
if err != nil {
t.Fatal(err)
}
if cleanup == nil {
t.Fatal("local composition returned no cleanup")
}
t.Cleanup(cleanup)
if chat != local {
t.Fatalf("local composition returned %T instead of the supplied adapter", chat)
}
})
}
}
func TestComposeChatContractRejectsInvalidSettings(t *testing.T) {
for _, tt := range []struct {
name string
settings config.ServiceConnection
}{
{name: "unknown mode", settings: config.ServiceConnection{Mode: "automatic"}},
{name: "default with target", settings: config.ServiceConnection{Target: "localhost:1234"}},
{name: "inproc with target", settings: config.ServiceConnection{Mode: "inproc", Target: "localhost:1234"}},
{name: "default mode", settings: config.ServiceConnection{Target: "localhost:1234"}},
{name: "inproc removed", settings: config.ServiceConnection{Mode: "inproc", Target: "localhost:1234"}},
{name: "remote missing target", settings: config.ServiceConnection{Mode: "remote"}},
{name: "remote blank target", settings: config.ServiceConnection{Mode: "remote", Target: " \t\n"}},
} {
t.Run(tt.name, func(t *testing.T) {
chat, cleanup, err := composeChatContract(tt.settings, &gateway.LocalChatAdapter{})
chat, cleanup, err := composeChatContract(tt.settings)
if cleanup != nil {
t.Cleanup(cleanup)
}
@@ -76,9 +57,8 @@ func TestComposeChatContractRemoteAndCleanup(t *testing.T) {
}
})
local := compositionChat{snapshot: gateway.ChatSnapshot{ActiveModel: "local-model"}}
chat, cleanup, err := composeChatContract(
config.ServiceConnection{Mode: "remote", Target: "passthrough:///chat"}, local,
config.ServiceConnection{Mode: "remote", Target: "passthrough:///chat"},
grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
return listener.DialContext(ctx)
}),
+1 -1
View File
@@ -389,7 +389,7 @@ func Run() int { //nolint:cyclop // the composition root's setup sequence is del
return 1
}
if err := b.setupLLMAndChat(ctx); err != nil {
if err := b.setupLLMAndChat(); err != nil {
return 1
}
if !b.setupGateways(ctx, args.cfgPath, args.overlayPath) {
@@ -2,6 +2,7 @@ package archied
import (
"context"
"fmt"
"log/slog"
"path/filepath"
"strings"
@@ -13,9 +14,25 @@ import (
"github.com/samcharles93/archie-core/internal/taskstate"
)
// newChatTaskActorForTest wires the same production chain the daemon uses in
// default (in-process) mode: chatTaskActorAdapter → LocalChatAdapter →
// taskActionsActor → taskactions.Service over the given store.
// testTaskActor formats taskactions.Service's result the same way the real
// NATS actor (infrastructure/taskactions.Client) does, so this test exercises
// the shared service through the same chatTaskActorAdapter → LocalChatAdapter
// → ChatTaskActor chain production traffic uses.
type testTaskActor struct{ b *boot }
func (a testTaskActor) ApplyChatTaskAction(
ctx context.Context, identity string, taskID int64, action taskstate.Action,
) (gateway.TaskActionResult, error) {
if err := a.b.taskActions().Apply(ctx, &identity, taskID, action); err != nil {
return gateway.TaskActionResult{}, err
}
return gateway.TaskActionResult{
TaskID: taskID,
Action: string(action),
Message: fmt.Sprintf("Applied %s to task %d.", action, taskID),
}, nil
}
func newChatTaskActorForTest(t *testing.T, st store.TaskStore, cfg config.Config) chatTaskActorAdapter {
t.Helper()
b := &boot{
@@ -23,7 +40,7 @@ func newChatTaskActorForTest(t *testing.T, st store.TaskStore, cfg config.Config
cfg: cfg,
log: slog.Default(),
}
return chatTaskActorAdapter{contract: &gateway.LocalChatAdapter{TaskActor: taskActionsActor{b}}}
return chatTaskActorAdapter{contract: &gateway.LocalChatAdapter{TaskActor: testTaskActor{b}}}
}
func TestChatTaskActorAdapterCrossIdentityRefused(t *testing.T) {
-24
View File
@@ -7,33 +7,9 @@ import (
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/taskactions"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/gateway"
taskactionstore "github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// taskActionsActor applies operator actions in-process for the daemon's own
// local gateway. It is the identity-scoping implementation behind the
// ChatContract's ApplyTaskAction when the daemon is the Gateway (default
// mode). The standalone Gateway instead forwards over NATS with
// taskactions.Client; both paths converge on the one taskactions.Service.
type taskActionsActor struct {
b *boot
}
func (a taskActionsActor) ApplyChatTaskAction(
ctx context.Context, identity string, taskID int64, action taskstate.Action,
) (gateway.TaskActionResult, error) {
if err := a.b.taskActions().Apply(ctx, &identity, taskID, action); err != nil {
return gateway.TaskActionResult{}, err
}
return gateway.TaskActionResult{
TaskID: taskID,
Action: string(action),
Message: fmt.Sprintf("Applied %s to task %d.", action, taskID),
}, nil
}
// taskActions assembles the daemon-owned operator action service. The daemon
// retains execution cancellation, retry policy, forge closure and event
// ownership; the dashboard's webui service and the NATS responder both build
@@ -60,7 +60,8 @@ const (
// inspected, and reasoned about independently of whether the result is valid.
func (l *Loader) applyDefaults(cfg *config.Config) {
if cfg.Services.Gateway.Mode == "" {
cfg.Services.Gateway.Mode = "inproc"
cfg.Services.Gateway.Mode = "remote"
cfg.Services.Gateway.Target = "127.0.0.1:8585"
}
l.applyGeneralDefaults(cfg)
applyForgeDefaults(cfg)
@@ -10,13 +10,13 @@ func TestLoadGatewayServiceMode(t *testing.T) {
for _, tt := range []struct {
name, section string
wantError bool
wantTarget string
}{
{name: "default"},
{name: "local", section: "[services.gateway]\nmode = 'inproc'"},
{name: "remote", section: "[services.gateway]\nmode = 'remote'\ntarget = 'dns:///gateway:8443'"},
{name: "default", wantTarget: "127.0.0.1:8585"},
{name: "remote", section: "[services.gateway]\nmode = 'remote'\ntarget = 'dns:///gateway:8443'", wantTarget: "dns:///gateway:8443"},
{name: "invalid mode", section: "[services.gateway]\nmode = 'typo'", wantError: true},
{name: "inproc removed", section: "[services.gateway]\nmode = 'inproc'", wantError: true},
{name: "remote without target", section: "[services.gateway]\nmode = 'remote'", wantError: true},
{name: "local with ignored target", section: "[services.gateway]\nmode = 'inproc'\ntarget = 'gateway:8443'", wantError: true},
} {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
@@ -28,15 +28,11 @@ func TestLoadGatewayServiceMode(t *testing.T) {
t.Fatalf("load error = %v, want error %v", err, tt.wantError)
}
if err == nil {
want := "inproc"
if tt.name == "remote" {
want = "remote"
if cfg.Services.Gateway.Target != "dns:///gateway:8443" {
t.Fatalf("target = %q", cfg.Services.Gateway.Target)
}
if cfg.Services.Gateway.Mode != "remote" {
t.Fatalf("mode = %q, want %q", cfg.Services.Gateway.Mode, "remote")
}
if cfg.Services.Gateway.Mode != want {
t.Fatalf("mode = %q, want %q", cfg.Services.Gateway.Mode, want)
if cfg.Services.Gateway.Target != tt.wantTarget {
t.Fatalf("target = %q, want %q", cfg.Services.Gateway.Target, tt.wantTarget)
}
}
})
@@ -98,16 +98,12 @@ func validate(cfg *config.Config) error {
func validateGatewayService(service config.ServiceConnection) error {
switch service.Mode {
case "", "inproc":
if service.Target != "" {
return fmt.Errorf("%w: services.gateway.target requires remote mode", ErrInvalidInput)
}
case "remote":
if strings.TrimSpace(service.Target) == "" {
return fmt.Errorf("%w: services.gateway.target is required in remote mode", ErrInvalidInput)
}
default:
return fmt.Errorf("%w: services.gateway.mode must be inproc or remote", ErrInvalidInput)
return fmt.Errorf("%w: services.gateway.mode must be remote", ErrInvalidInput)
}
return nil
}
@@ -26,6 +26,9 @@ func minimalValidConfig() config.Config {
Containers: config.ContainerConfig{
Image: "ghcr.io/samcharles93/archie-agent:latest",
},
Services: config.Services{
Gateway: config.ServiceConnection{Mode: "remote", Target: "127.0.0.1:8585"},
},
}
}