From d0118756d7b4396a57e88f79231acb349acf9cba Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 13 Nov 2026 03:57:05 +1100 Subject: [PATCH] refactor(gateway): the turn owner ID is made once by the composition root (#1928) --- internal/app/archiegateway/chat_setup.go | 24 +++++++++++++------ internal/app/archiegateway/gateway_runtime.go | 7 +++++- internal/gateway/turn.go | 16 ++++++------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/internal/app/archiegateway/chat_setup.go b/internal/app/archiegateway/chat_setup.go index bebb60e56..409062040 100644 --- a/internal/app/archiegateway/chat_setup.go +++ b/internal/app/archiegateway/chat_setup.go @@ -72,6 +72,9 @@ type chatSetup struct { // message is recorded. Nil disables the capability (no role configured, or // the provider credential did not resolve), leaving the frontend's note. Transcriber messaging.Transcriber + // OwnerID is the turn owner shared by every runner in this process, so + // recovery and persistence agree on which turns are this process's. + OwnerID string } // componentInstallTypeEnricher builds Service.Enrich: it knows how to @@ -110,12 +113,15 @@ func makeChatLLMResponder( s chatSetup, sessionStore gateway.SessionStore, router *gateway.Router, -) (gateway.LLMResponder, gateway.LLMStreamResponder) { +) (gateway.LLMResponder, gateway.LLMStreamResponder, error) { if s.LLM == nil || s.LLM() == nil { - return nil, nil + return nil, nil, nil } - runner := newChatTurnRunner(ctx, channel, s, sessionStore, router) - return runner.Respond, runner.RespondStream + runner, err := newChatTurnRunner(ctx, channel, s, sessionStore, router) + if err != nil { + return nil, nil, err + } + return runner.Respond, runner.RespondStream, nil } func newChatTurnRunner( @@ -124,9 +130,9 @@ func newChatTurnRunner( s chatSetup, sessionStore gateway.SessionStore, router *gateway.Router, -) *gateway.TurnRunner { +) (*gateway.TurnRunner, error) { cfg := s.Cfg.Get() - runner := gateway.NewTurnRunner(gateway.TurnRunnerConfig{ + runner, err := gateway.NewTurnRunner(gateway.TurnRunnerConfig{ Router: router, Sessions: sessionStore, Models: s.ChatModels, @@ -147,12 +153,16 @@ func newChatTurnRunner( MemoryWriter: s.MemoryWriter, UserIdentity: userIdentityResolver(), Transcriber: s.Transcriber, + OwnerID: s.OwnerID, Log: s.Log, }) + if err != nil { + return nil, fmt.Errorf("chat turn runner for %s: %w", channel, err) + } if err := runner.Recover(ctx); err != nil && s.Log != nil { s.Log.ErrorContext(ctx, "recover chat turns", "channel", channel, "err", err) } - return runner + return runner, nil } // chatRepoEnv renders the repositories the chat agent manages, with their diff --git a/internal/app/archiegateway/gateway_runtime.go b/internal/app/archiegateway/gateway_runtime.go index 48c55b6b4..7ed1d0482 100644 --- a/internal/app/archiegateway/gateway_runtime.go +++ b/internal/app/archiegateway/gateway_runtime.go @@ -214,8 +214,13 @@ func (b *server) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskAct // that must record their outcomes for /status. ProviderOutcomes: b.providerOutcomes, Usage: b.usage, + OwnerID: gateway.NewTurnID(), + } + var err error + router.LLM, router.LLMStream, err = makeChatLLMResponder(ctx, "web", setup, b.chatSessionStore, router) + if err != nil { + return nil, err } - router.LLM, router.LLMStream = makeChatLLMResponder(ctx, "web", setup, b.chatSessionStore, router) router.Titles = newChatTitleGenerator(setup) router.Log = b.log return &gateway.LocalChatAdapter{ diff --git a/internal/gateway/turn.go b/internal/gateway/turn.go index 106e926c1..833c90d39 100644 --- a/internal/gateway/turn.go +++ b/internal/gateway/turn.go @@ -76,8 +76,6 @@ type TurnReplayStore interface { FindPriorReply(ctx context.Context, sessionID, sourceID, identity string) (string, error) } -var defaultTurnOwnerID = NewTurnID() - const defaultTurnPersistenceTimeout = 5 * time.Second // TurnRunnerConfig contains the dependencies for one channel's chat turn @@ -136,18 +134,20 @@ type TurnRunner struct { recovered bool } -// NewTurnRunner constructs a chat turn runner. -func NewTurnRunner(cfg TurnRunnerConfig) *TurnRunner { +// NewTurnRunner constructs a chat turn runner. Every runner in a process must +// share one OwnerID: recovery and turn persistence match turns by owner, so a +// second ID would make this runner fail the other runners' turns. +func NewTurnRunner(cfg TurnRunnerConfig) (*TurnRunner, error) { + if cfg.OwnerID == "" { + return nil, errors.New("turn runner: owner ID is required") + } if cfg.Ledger == nil { cfg.Ledger, _ = cfg.Sessions.(TurnLedger) } - if cfg.OwnerID == "" { - cfg.OwnerID = defaultTurnOwnerID - } if cfg.PersistenceTimeout <= 0 { cfg.PersistenceTimeout = defaultTurnPersistenceTimeout } - return &TurnRunner{TurnRunnerConfig: cfg} + return &TurnRunner{TurnRunnerConfig: cfg}, nil } // Recover marks turns from a previous process as failed and retryable. It is