feat(gateway): make chat turns durable and idempotent
This commit is contained in:
1 parent
80f9d92e51
commit
b876fc80cc
16 files changed
+2183
-188
No files matched your search
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
// chatTurnModel adapts the provider-specific ai-sdk runtime to the gateway's
|
||||
// provider-neutral TurnModel seam. Tool construction remains here because it
|
||||
// depends on runtime registries and configured execution limits.
|
||||
type chatTurnModel struct {
|
||||
llm *runtime.Runtime
|
||||
registry *tools.Registry
|
||||
maxSteps int
|
||||
limits agentexec.ToolLimits
|
||||
}
|
||||
|
||||
func newChatTurnModel(
|
||||
llm *runtime.Runtime,
|
||||
registry *tools.Registry,
|
||||
maxSteps int,
|
||||
limits agentexec.ToolLimits,
|
||||
) gateway.TurnModel {
|
||||
return &chatTurnModel{
|
||||
llm: llm,
|
||||
registry: registry,
|
||||
maxSteps: maxSteps,
|
||||
limits: limits,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *chatTurnModel) Prepare(
|
||||
ctx context.Context,
|
||||
model string,
|
||||
extra []tools.ToolEntry,
|
||||
) (gateway.PreparedTurnModel, error) {
|
||||
options, err := chatGenerateOptions(ctx, nil, m.registry, m.maxSteps, m.limits, extra)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
toolSchema, err := json.Marshal(options.Tools)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &preparedChatTurnModel{
|
||||
llm: m.llm,
|
||||
model: model,
|
||||
options: options,
|
||||
toolInfo: toolSummaries(options.Tools),
|
||||
toolTokens: gateway.EstimateTokens(string(toolSchema)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type preparedChatTurnModel struct {
|
||||
llm *runtime.Runtime
|
||||
model string
|
||||
options core.GenerateOptions
|
||||
toolInfo []gateway.ToolSummary
|
||||
toolTokens int
|
||||
}
|
||||
|
||||
func (m *preparedChatTurnModel) ToolSummaries() []gateway.ToolSummary {
|
||||
return append([]gateway.ToolSummary(nil), m.toolInfo...)
|
||||
}
|
||||
|
||||
func (m *preparedChatTurnModel) ToolSchemaTokens() int {
|
||||
return m.toolTokens
|
||||
}
|
||||
|
||||
func (m *preparedChatTurnModel) Generate(
|
||||
ctx context.Context,
|
||||
request gateway.TurnModelRequest,
|
||||
onDelta func(string),
|
||||
) (string, error) {
|
||||
options := m.options
|
||||
options.Messages = make([]chat.Message, len(request.Messages))
|
||||
for i, message := range request.Messages {
|
||||
role := chat.RoleUser
|
||||
switch message.Role {
|
||||
case "assistant":
|
||||
role = chat.RoleAssistant
|
||||
case "system":
|
||||
role = chat.RoleSystem
|
||||
}
|
||||
options.Messages[i] = chat.Message{Role: role, Content: message.Content}
|
||||
}
|
||||
options.MaxTokens = request.MaxTokens
|
||||
return sendChatTurn(ctx, m.llm, m.model, options, onDelta)
|
||||
}
|
||||
+44
-108
@@ -7,12 +7,12 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
channelruntime "github.com/samcharles93/archie-core/internal/channels"
|
||||
"github.com/samcharles93/archie-core/internal/channels/telegram"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
@@ -48,8 +48,9 @@ type telegramSetup struct {
|
||||
// Bus carries primary-input events (archie-core-035): a completed
|
||||
// chat turn is published here so input-driven curators can wake. Nil
|
||||
// disables turn events (tests, minimal setups).
|
||||
Bus *events.Bus
|
||||
Log *slog.Logger
|
||||
Bus *events.Bus
|
||||
Log *slog.Logger
|
||||
ChannelManager *channelruntime.Manager
|
||||
}
|
||||
|
||||
// setupTelegramGateway initialises the Telegram chat gateway when a
|
||||
@@ -96,8 +97,14 @@ func setupTelegramGateway(ctx context.Context, s telegramSetup) (start func(), o
|
||||
}
|
||||
router := buildTelegramRouter(tg, s, sessionStore)
|
||||
return func() {
|
||||
if s.ChannelManager != nil {
|
||||
s.ChannelManager.MarkStarting("telegram")
|
||||
}
|
||||
go func() {
|
||||
if err := tg.Start(ctx, router); err != nil && ctx.Err() == nil {
|
||||
if s.ChannelManager != nil {
|
||||
s.ChannelManager.MarkFailed("telegram", err.Error())
|
||||
}
|
||||
s.Log.Error("telegram gateway stopped", "err", err)
|
||||
}
|
||||
}()
|
||||
@@ -127,7 +134,7 @@ func makeUpdateService(s telegramSetup) *releaseupdate.Service {
|
||||
|
||||
func makeTelegramReload(s telegramSetup) func(*telegram.Gateway) error {
|
||||
return func(g *telegram.Gateway) error {
|
||||
doc, err := configuration.New(s.Log).Overlay(s.CfgPath, s.OverlayPath)
|
||||
doc, err := configuration.New(s.Log).Resolve(s.CfgPath, s.OverlayPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reload config: %w", err)
|
||||
}
|
||||
@@ -167,7 +174,11 @@ func buildTelegramRouter(tg *telegram.Gateway, s telegramSetup, sessionStore gat
|
||||
router.Log = s.Log
|
||||
configureTaskCommands(router, s.ChatTasks, s.ChatController, s.DefaultChatIdentity)
|
||||
|
||||
router.LLM, router.LLMStream = makeChatLLMResponder(tg.Name(), s, sessionStore, router)
|
||||
if s.LLM != nil {
|
||||
turnRunner := newChatTurnRunner(tg.Name(), s, sessionStore, router)
|
||||
router.LLM = turnRunner.Respond
|
||||
router.LLMStream = turnRunner.RespondStream
|
||||
}
|
||||
return router
|
||||
}
|
||||
|
||||
@@ -180,110 +191,35 @@ func makeChatLLMResponder(
|
||||
if s.LLM == nil {
|
||||
return nil, nil
|
||||
}
|
||||
// Built once: these carry this gateway's identity, which does not change
|
||||
// between turns, so there is nothing per-turn to rebuild.
|
||||
taskTools := gateway.TaskTools(s.ChatTaskLister, s.ChatTasks, s.DefaultChatIdentity)
|
||||
respond := func(ctx context.Context, msg gateway.Message, onDelta func(string)) (string, error) {
|
||||
// Use the router's session tracker, not a local channel-keyed helper.
|
||||
// Commands like /new, /branch, or /resume may reassign a session to a
|
||||
// generated ID; after that, only the router sees the correct history.
|
||||
sk, err := router.ResolveSessionKey(ctx, msg)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve chat session: %w", err)
|
||||
}
|
||||
history, err := sessionStore.RecentMessages(ctx, sk, 100)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("load chat history: %w", err)
|
||||
}
|
||||
// Deduplicate redelivered updates: the store already rejects the
|
||||
// duplicate message, but that alone doesn't prevent a second model
|
||||
// call with its cost and side effects. Replaying the prior reply
|
||||
// is both cheaper and correct.
|
||||
if prior := gateway.PriorReply(history, sk, msg.SourceID, s.Cfg.BotUser); prior != "" {
|
||||
s.Log.Info("replaying the reply to a redelivered message",
|
||||
"session", sk, "source_id", msg.SourceID)
|
||||
if onDelta != nil {
|
||||
onDelta(prior)
|
||||
}
|
||||
return prior, nil
|
||||
}
|
||||
if err := sessionStore.SaveMessage(ctx, sk, msg); err != nil {
|
||||
return "", fmt.Errorf("save inbound chat message: %w", err)
|
||||
}
|
||||
history = append(history, msg)
|
||||
// Log session id, history depth, and resolved persona to
|
||||
// distinguish "wrong session" bugs from "history not loaded".
|
||||
s.Log.Info("chat turn",
|
||||
"session", sk,
|
||||
"channel", msg.ChannelID,
|
||||
"thread", msg.ThreadID,
|
||||
"history_messages", len(history))
|
||||
compressed := make([]gateway.CompressedMessage, 0, len(history))
|
||||
for _, h := range history {
|
||||
role := "user"
|
||||
if h.From == s.Cfg.BotUser {
|
||||
role = "assistant"
|
||||
}
|
||||
compressed = append(compressed, gateway.CompressedMessage{Role: role, Content: h.Text})
|
||||
}
|
||||
view := gateway.CompressHistory(compressed, gateway.DefaultCompressionConfig())
|
||||
sessionTools := gateway.SessionTools(sessionStore, router.SessionTracker(), channel, msg)
|
||||
allTools := make([]tools.ToolEntry, 0, len(taskTools)+len(sessionTools))
|
||||
allTools = append(allTools, taskTools...)
|
||||
allTools = append(allTools, sessionTools...)
|
||||
options, err := chatGenerateOptions(ctx, nil, s.ToolReg, s.Cfg.Chat.MaxSteps, toolLimits(s.Cfg), allTools)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("build chat tools: %w", err)
|
||||
}
|
||||
chatModel := s.ChatModels.ActiveModel()
|
||||
systemPrompt := gateway.BuildSystemPrompt(gateway.SystemPromptConfig{
|
||||
Persona: s.Personas.GetActive(sk), Tools: toolSummaries(options.Tools),
|
||||
Channel: channel, Model: chatModel, SessionID: sk, Now: time.Now(),
|
||||
Operator: s.Cfg.Chat.Operator,
|
||||
})
|
||||
view.Messages = append(
|
||||
[]gateway.CompressedMessage{{Role: "system", Content: systemPrompt}}, view.Messages...,
|
||||
)
|
||||
messages := make([]chat.Message, len(view.Messages))
|
||||
for i, cm := range view.Messages {
|
||||
role := chat.RoleUser
|
||||
switch cm.Role {
|
||||
case "assistant":
|
||||
role = chat.RoleAssistant
|
||||
case "system":
|
||||
role = chat.RoleSystem
|
||||
}
|
||||
messages[i] = chat.Message{Role: role, Content: cm.Content}
|
||||
}
|
||||
options.Messages = messages
|
||||
text, err := sendChatTurn(ctx, s.LLM, chatModel, options, onDelta)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := sessionStore.SaveMessage(ctx, sk, gateway.Message{From: s.Cfg.BotUser, Text: text}); err != nil {
|
||||
return "", fmt.Errorf("save outbound chat message: %w", err)
|
||||
}
|
||||
// The turn is complete: both messages are persisted. Announce it
|
||||
// as primary input (archie-core-035). Publish is non-blocking
|
||||
// with bounded dropping subscriber buffers, so this never delays
|
||||
// the chat path; a dropped event only delays a curator run to its
|
||||
// next check-in. Curator output never produces this kind, so
|
||||
// derived work cannot feed its own trigger. Redeliveries return
|
||||
// above and never reach here.
|
||||
if s.Bus != nil {
|
||||
s.Bus.Publish(events.Event{
|
||||
Kind: events.KindTurnCompleted,
|
||||
Detail: sk,
|
||||
Data: map[string]any{"session": sk, "channel": channel},
|
||||
})
|
||||
}
|
||||
return text, nil
|
||||
runner := newChatTurnRunner(channel, s, sessionStore, router)
|
||||
return runner.Respond, runner.RespondStream
|
||||
}
|
||||
|
||||
func newChatTurnRunner(
|
||||
channel string,
|
||||
s telegramSetup,
|
||||
sessionStore gateway.SessionStore,
|
||||
router *gateway.Router,
|
||||
) *gateway.TurnRunner {
|
||||
runner := gateway.NewTurnRunner(gateway.TurnRunnerConfig{
|
||||
Router: router,
|
||||
Sessions: sessionStore,
|
||||
Models: s.ChatModels,
|
||||
Personas: s.Personas,
|
||||
Model: newChatTurnModel(s.LLM, s.ToolReg, s.Cfg.Chat.MaxSteps, toolLimits(s.Cfg)),
|
||||
TaskLister: s.ChatTaskLister,
|
||||
Tasks: s.ChatTasks,
|
||||
TaskIdentity: s.DefaultChatIdentity,
|
||||
Bus: s.Bus,
|
||||
BotUser: s.Cfg.BotUser,
|
||||
Channel: channel,
|
||||
Operator: s.Cfg.Chat.Operator,
|
||||
Log: s.Log,
|
||||
})
|
||||
if err := runner.Recover(context.Background()); err != nil && s.Log != nil {
|
||||
s.Log.Error("recover chat turns", "channel", channel, "err", err)
|
||||
}
|
||||
return func(ctx context.Context, msg gateway.Message) (string, error) {
|
||||
return respond(ctx, msg, nil)
|
||||
}, func(ctx context.Context, msg gateway.Message, onDelta func(string)) (string, error) {
|
||||
return respond(ctx, msg, onDelta)
|
||||
}
|
||||
return runner
|
||||
}
|
||||
|
||||
func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, onDelta func(string)) (string, error) {
|
||||
|
||||
@@ -24,6 +24,11 @@ type CompressionConfig struct {
|
||||
// characters). Used with Threshold to decide when to compress.
|
||||
ContextWindow int
|
||||
|
||||
// MaxPromptTokens is the effective history budget after reserving space
|
||||
// for the system prompt, tools, and model output. A positive value takes
|
||||
// precedence over ContextWindow*Threshold.
|
||||
MaxPromptTokens int
|
||||
|
||||
// ProtectFirst keeps the first N messages unmodified (typically the
|
||||
// system greeting and initial instructions).
|
||||
ProtectFirst int
|
||||
@@ -51,10 +56,50 @@ func DefaultCompressionConfig() CompressionConfig {
|
||||
}
|
||||
}
|
||||
|
||||
// CompressionConfigForModel derives the history budget from the selected
|
||||
// model rather than assuming every provider has the default context window.
|
||||
// promptReserve is the estimated system/tool prompt cost already known before
|
||||
// history is assembled.
|
||||
func CompressionConfigForModel(details ModelDetails, promptReserve int) (CompressionConfig, error) {
|
||||
cfg := DefaultCompressionConfig()
|
||||
if details.ContextWindow <= 0 {
|
||||
// ModelDetails is an optional extension on ModelManager. Preserve
|
||||
// compatibility for minimal adapters, while production catalog-backed
|
||||
// managers still take the model-specific path below.
|
||||
cfg.MaxPromptTokens = max(cfg.ContextWindow-max(promptReserve, 0), 1)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
reserve := max(promptReserve, 0) + max(details.MaxOutputTokens, 0)
|
||||
if reserve >= details.ContextWindow {
|
||||
return CompressionConfig{}, fmt.Errorf("model %q context window %d is smaller than reserved prompt/output budget %d", details.Ref, details.ContextWindow, reserve)
|
||||
}
|
||||
cfg.ContextWindow = details.ContextWindow
|
||||
cfg.MaxPromptTokens = max(details.ContextWindow-reserve, 1)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// tokenEstimate returns a rough estimate of the token count for a string.
|
||||
// Uses character / 4 which is a common approximation for English text.
|
||||
// ASCII text keeps the common four-characters-per-token approximation. Every
|
||||
// non-ASCII rune reserves one token because byte/rune averaging substantially
|
||||
// undercounts CJK text and emoji, which providers commonly tokenize more
|
||||
// densely than English text.
|
||||
func tokenEstimate(s string) int {
|
||||
return len(s) / 4
|
||||
ascii, nonASCII := 0, 0
|
||||
for _, r := range s {
|
||||
if r < 128 {
|
||||
ascii++
|
||||
continue
|
||||
}
|
||||
nonASCII++
|
||||
}
|
||||
return ascii/4 + nonASCII
|
||||
}
|
||||
|
||||
// EstimateTokens exposes the same conservative estimate used by compression
|
||||
// so callers can reserve space for system prompts and tool descriptions.
|
||||
func EstimateTokens(s string) int {
|
||||
return tokenEstimate(s)
|
||||
}
|
||||
|
||||
// CompressedView holds the compressed representation of a conversation
|
||||
@@ -109,9 +154,12 @@ func CompressHistory(messages []CompressedMessage, cfg CompressionConfig) Compre
|
||||
}
|
||||
|
||||
totalTokens := estimateTotal(messages)
|
||||
threshold := int(float64(cfg.ContextWindow) * cfg.Threshold)
|
||||
threshold := cfg.MaxPromptTokens
|
||||
if threshold <= 0 {
|
||||
threshold = int(float64(cfg.ContextWindow) * cfg.Threshold)
|
||||
}
|
||||
|
||||
if totalTokens <= threshold || len(messages) <= cfg.ProtectFirst+cfg.ProtectLast {
|
||||
if totalTokens <= threshold || len(messages) == 0 {
|
||||
return CompressedView{
|
||||
Messages: messages,
|
||||
TokensBefore: totalTokens,
|
||||
@@ -119,28 +167,40 @@ func CompressHistory(messages []CompressedMessage, cfg CompressionConfig) Compre
|
||||
}
|
||||
}
|
||||
|
||||
// Compression: keep first N + summary marker + last N.
|
||||
// Compression: keep first N + summary marker + last N. If the protected
|
||||
// tail itself exceeds the effective budget, reduce it until the resulting
|
||||
// view fits. This matters for small-context local models.
|
||||
protectLast := min(cfg.ProtectLast, len(messages))
|
||||
protectFirst := min(cfg.ProtectFirst, len(messages)-protectLast)
|
||||
for {
|
||||
truncated := messages[protectFirst : len(messages)-protectLast]
|
||||
summary := summariseTruncated(truncated, cfg)
|
||||
|
||||
// Build a summary of the truncated middle.
|
||||
truncated := messages[protectFirst : len(messages)-protectLast]
|
||||
summary := summariseTruncated(truncated, cfg)
|
||||
|
||||
compressed := make([]CompressedMessage, 0, protectFirst+1+protectLast)
|
||||
compressed = append(compressed, messages[:protectFirst]...)
|
||||
compressed = append(compressed, CompressedMessage{Role: "system", Content: summary})
|
||||
compressed = append(compressed, messages[len(messages)-protectLast:]...)
|
||||
|
||||
compressedTokens := estimateTotal(compressed)
|
||||
|
||||
return CompressedView{
|
||||
Messages: compressed,
|
||||
WasCompressed: true,
|
||||
TokensBefore: totalTokens,
|
||||
TokensAfter: compressedTokens,
|
||||
ProtectedFirst: protectFirst,
|
||||
ProtectedLast: protectLast,
|
||||
compressed := make([]CompressedMessage, 0, protectFirst+1+protectLast)
|
||||
compressed = append(compressed, messages[:protectFirst]...)
|
||||
compressed = append(compressed, CompressedMessage{Role: "system", Content: summary})
|
||||
compressed = append(compressed, messages[len(messages)-protectLast:]...)
|
||||
compressedTokens := estimateTotal(compressed)
|
||||
if compressedTokens <= threshold || (protectFirst == 0 && protectLast == 0) {
|
||||
if compressedTokens > threshold && protectFirst == 0 && protectLast == 0 {
|
||||
maxSummaryChars := max(threshold*4, 1)
|
||||
compressed[0].Content = truncate(compressed[0].Content, maxSummaryChars)
|
||||
compressedTokens = estimateTotal(compressed)
|
||||
}
|
||||
return CompressedView{
|
||||
Messages: compressed,
|
||||
WasCompressed: true,
|
||||
TokensBefore: totalTokens,
|
||||
TokensAfter: compressedTokens,
|
||||
ProtectedFirst: protectFirst,
|
||||
ProtectedLast: protectLast,
|
||||
}
|
||||
}
|
||||
if protectLast > 0 {
|
||||
protectLast--
|
||||
continue
|
||||
}
|
||||
protectFirst--
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,17 +266,17 @@ func summariseTruncated(messages []CompressedMessage, cfg CompressionConfig) str
|
||||
|
||||
// truncate cuts s to at most n runes.
|
||||
func truncate(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
if n <= 0 {
|
||||
return ""
|
||||
}
|
||||
runes := []rune(s)
|
||||
if len(runes) <= n {
|
||||
return s
|
||||
}
|
||||
// Simple byte truncation is fine for ASCII; for multi-byte we'd
|
||||
// need rune-safe slicing. English text is the primary case.
|
||||
for i := n; i < len(s); i++ {
|
||||
if s[i] < 128 {
|
||||
return s[:i] + "..."
|
||||
}
|
||||
if n <= 3 {
|
||||
return string(runes[:n])
|
||||
}
|
||||
return s[:n] + "..."
|
||||
return string(runes[:n-3]) + "..."
|
||||
}
|
||||
|
||||
// SortBySeq implements sort.Interface for ordering compressed messages
|
||||
|
||||
@@ -5,6 +5,54 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompressionConfigForModelReservesPromptAndOutputBudget(t *testing.T) {
|
||||
details := ModelDetails{ContextWindow: 16384, MaxOutputTokens: 2048}
|
||||
cfg, err := CompressionConfigForModel(details, 1024)
|
||||
if err != nil {
|
||||
t.Fatalf("CompressionConfigForModel() error = %v", err)
|
||||
}
|
||||
|
||||
if got, want := cfg.MaxPromptTokens, 13312; got != want {
|
||||
t.Fatalf("MaxPromptTokens = %d, want %d", got, want)
|
||||
}
|
||||
if got, want := cfg.ContextWindow, 16384; got != want {
|
||||
t.Fatalf("ContextWindow = %d, want %d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompressionConfigForModelUsesCompatibilityFallbackWithoutMetadata(t *testing.T) {
|
||||
cfg, err := CompressionConfigForModel(ModelDetails{Ref: "provider/model"}, 1024)
|
||||
if err != nil {
|
||||
t.Fatalf("CompressionConfigForModel() error = %v", err)
|
||||
}
|
||||
if cfg.MaxPromptTokens != 126976 {
|
||||
t.Fatalf("MaxPromptTokens = %d, want compatibility budget", cfg.MaxPromptTokens)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompressionConfigForModelRejectsOverReservedBudget(t *testing.T) {
|
||||
_, err := CompressionConfigForModel(ModelDetails{Ref: "provider/model", ContextWindow: 4096, MaxOutputTokens: 2048}, 2048)
|
||||
if err == nil {
|
||||
t.Fatal("CompressionConfigForModel() error = nil, want over-reserved-budget error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompressHistoryHonorsPromptBudget(t *testing.T) {
|
||||
cfg := DefaultCompressionConfig()
|
||||
cfg.MaxPromptTokens = 1000
|
||||
cfg.ProtectFirst = 1
|
||||
cfg.ProtectLast = 2
|
||||
msgs := makeMessages(40, 300)
|
||||
|
||||
view := CompressHistory(msgs, cfg)
|
||||
if !view.WasCompressed {
|
||||
t.Fatal("expected compression")
|
||||
}
|
||||
if view.TokensAfter > cfg.MaxPromptTokens {
|
||||
t.Fatalf("TokensAfter = %d, want <= %d", view.TokensAfter, cfg.MaxPromptTokens)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompressHistoryThresholdNotExceeded(t *testing.T) {
|
||||
cfg := DefaultCompressionConfig()
|
||||
cfg.ContextWindow = 1000000 // huge window
|
||||
@@ -69,6 +117,22 @@ func TestCompressHistoryTruncatesMiddle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompressHistorySmallConversationStillHonorsPromptBudget(t *testing.T) {
|
||||
cfg := DefaultCompressionConfig()
|
||||
cfg.MaxPromptTokens = 100
|
||||
cfg.ProtectFirst = 3
|
||||
cfg.ProtectLast = 20
|
||||
msgs := makeMessages(4, 200)
|
||||
|
||||
view := CompressHistory(msgs, cfg)
|
||||
if !view.WasCompressed {
|
||||
t.Fatal("expected compression")
|
||||
}
|
||||
if view.TokensAfter > cfg.MaxPromptTokens {
|
||||
t.Fatalf("TokensAfter = %d, want <= %d", view.TokensAfter, cfg.MaxPromptTokens)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompressHistorySmallConversation(t *testing.T) {
|
||||
cfg := DefaultCompressionConfig()
|
||||
|
||||
@@ -116,6 +180,8 @@ func TestTokenEstimate(t *testing.T) {
|
||||
{"four", 1}, // 4 chars / 4 = 1
|
||||
{"hello world", 2}, // 11 chars / 4 = 2
|
||||
{"12345678", 2}, // 8 chars / 4 = 2
|
||||
{"你好世界", 4}, // non-ASCII runes reserve one token each
|
||||
{"🙂🙂", 2}, // emoji must not be byte-divided to zero
|
||||
{strings.Repeat("x", 100), 25},
|
||||
}
|
||||
|
||||
|
||||
@@ -152,6 +152,7 @@ const MaxSearchResults = nl.MaxTextSearchLimit
|
||||
// nellSessionStore implements SessionStore backed by a NellDB DocDB.
|
||||
type nellSessionStore struct {
|
||||
db *sdk.DocDB // sessions collection
|
||||
turns *sdk.DocDB // durable chat-turn ledger
|
||||
store nl.Store
|
||||
nodeID string
|
||||
mu sync.Mutex
|
||||
@@ -171,6 +172,7 @@ type nellSessionStore struct {
|
||||
func NewSessionStore(st nl.Store, nodeID string) SessionStore {
|
||||
return &nellSessionStore{
|
||||
db: sdk.New(st, nodeID, "sessions"),
|
||||
turns: sdk.New(st, nodeID, "turns"),
|
||||
store: st,
|
||||
nodeID: nodeID,
|
||||
msgDBs: make(map[string]*sdk.DocDB),
|
||||
@@ -184,6 +186,209 @@ func NewSessionStoreMemory(nodeID string) SessionStore {
|
||||
return NewSessionStore(nl.NewMemoryStore(nodeID), nodeID)
|
||||
}
|
||||
|
||||
func (s *nellSessionStore) ClaimTurn(ctx context.Context, initial TurnRecord) (TurnRecord, TurnClaim, error) {
|
||||
if initial.TurnID == "" {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: turn ID is required")
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
now := time.Now().UTC()
|
||||
existing, err := s.turns.Get(ctx, initial.TurnID)
|
||||
if errors.Is(err, sdk.ErrNotFound) {
|
||||
initial.Status = TurnStatusRunning
|
||||
initial.Attempt = 1
|
||||
if initial.CreatedAt.IsZero() {
|
||||
initial.CreatedAt = now
|
||||
}
|
||||
initial.UpdatedAt = now
|
||||
if _, err := s.turns.Put(ctx, turnToDoc(initial)); err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: claim turn: %w", err)
|
||||
}
|
||||
return initial, TurnClaimOwned, nil
|
||||
}
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: read turn: %w", err)
|
||||
}
|
||||
turn := docToTurn(existing)
|
||||
switch turn.Status {
|
||||
case TurnStatusCompleted:
|
||||
return turn, TurnClaimCompleted, nil
|
||||
case TurnStatusAccepted, TurnStatusRunning, TurnStatusPartial:
|
||||
return turn, TurnClaimInProgress, nil
|
||||
case TurnStatusFailed, TurnStatusCancelled:
|
||||
if turn.AssistantMessageID != "" {
|
||||
turn.Status = TurnStatusCompleted
|
||||
turn.Error = ""
|
||||
turn.UpdatedAt = now
|
||||
if _, err := s.turns.Put(ctx, withTurnRevision(existing, turn)); err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: finalize turn: %w", err)
|
||||
}
|
||||
return turn, TurnClaimCompleted, nil
|
||||
}
|
||||
turn.Status = TurnStatusRunning
|
||||
turn.Attempt++
|
||||
turn.OwnerID = initial.OwnerID
|
||||
turn.Error = ""
|
||||
turn.UpdatedAt = now
|
||||
if _, err := s.turns.Put(ctx, withTurnRevision(existing, turn)); err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: retry turn: %w", err)
|
||||
}
|
||||
return turn, TurnClaimOwned, nil
|
||||
default:
|
||||
return turn, TurnClaimInProgress, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *nellSessionStore) RecoverTurns(ctx context.Context, ownerID string) error {
|
||||
if ownerID == "" {
|
||||
return fmt.Errorf("sessionstore: recovery owner ID is required")
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
result, err := s.turns.AllDocs(ctx, sdk.DocRange{IncludeDocs: true})
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: recover turns: list: %w", err)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
for _, row := range result.Rows {
|
||||
if row.Doc == nil {
|
||||
continue
|
||||
}
|
||||
turn := docToTurn(row.Doc)
|
||||
if turn.Status != TurnStatusAccepted && turn.Status != TurnStatusRunning && turn.Status != TurnStatusPartial {
|
||||
continue
|
||||
}
|
||||
if turn.OwnerID == ownerID {
|
||||
continue
|
||||
}
|
||||
turn.Status = TurnStatusFailed
|
||||
turn.Error = "recovered after process restart"
|
||||
turn.UpdatedAt = now
|
||||
if _, err := s.turns.Put(ctx, withTurnRevision(row.Doc, turn)); err != nil {
|
||||
return fmt.Errorf("sessionstore: recover turn %s: %w", turn.TurnID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *nellSessionStore) GetTurn(ctx context.Context, turnID string) (TurnRecord, bool, error) {
|
||||
if turnID == "" {
|
||||
return TurnRecord{}, false, nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
doc, err := s.turns.Get(ctx, turnID)
|
||||
if errors.Is(err, sdk.ErrNotFound) {
|
||||
return TurnRecord{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return TurnRecord{}, false, fmt.Errorf("sessionstore: get turn: %w", err)
|
||||
}
|
||||
return docToTurn(doc), true, nil
|
||||
}
|
||||
|
||||
func (s *nellSessionStore) SaveTurn(ctx context.Context, turn TurnRecord) error {
|
||||
if turn.TurnID == "" {
|
||||
return fmt.Errorf("sessionstore: turn ID is required")
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
current, err := s.turns.Get(ctx, turn.TurnID)
|
||||
if errors.Is(err, sdk.ErrNotFound) {
|
||||
return fmt.Errorf("sessionstore: save turn: turn not found")
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: save turn: read: %w", err)
|
||||
}
|
||||
currentTurn := docToTurn(current)
|
||||
if currentTurn.OwnerID != turn.OwnerID || currentTurn.Attempt != turn.Attempt {
|
||||
return fmt.Errorf("%w: %s", ErrTurnOwnershipLost, turn.TurnID)
|
||||
}
|
||||
if turn.UpdatedAt.IsZero() {
|
||||
turn.UpdatedAt = time.Now().UTC()
|
||||
}
|
||||
if _, err := s.turns.Put(ctx, withTurnRevision(current, turn)); err != nil {
|
||||
return fmt.Errorf("sessionstore: save turn: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *nellSessionStore) ListRecoverableTurns(ctx context.Context) ([]TurnRecord, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
result, err := s.turns.AllDocs(ctx, sdk.DocRange{IncludeDocs: true})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: list recoverable turns: %w", err)
|
||||
}
|
||||
turns := make([]TurnRecord, 0)
|
||||
for _, row := range result.Rows {
|
||||
if row.Doc == nil {
|
||||
continue
|
||||
}
|
||||
turn := docToTurn(row.Doc)
|
||||
switch turn.Status {
|
||||
case TurnStatusAccepted, TurnStatusRunning, TurnStatusPartial:
|
||||
turns = append(turns, turn)
|
||||
}
|
||||
}
|
||||
slices.SortFunc(turns, func(a, b TurnRecord) int {
|
||||
return a.UpdatedAt.Compare(b.UpdatedAt)
|
||||
})
|
||||
return turns, nil
|
||||
}
|
||||
|
||||
func turnToDoc(turn TurnRecord) sdk.Doc {
|
||||
return sdk.Doc{
|
||||
sdk.FieldID: turn.TurnID,
|
||||
"session_id": turn.SessionID,
|
||||
"source_id": turn.SourceID,
|
||||
"status": string(turn.Status),
|
||||
"attempt": turn.Attempt,
|
||||
"owner_id": turn.OwnerID,
|
||||
"input_message_id": turn.InputMessageID,
|
||||
"assistant_message_id": turn.AssistantMessageID,
|
||||
"partial_text": turn.PartialText,
|
||||
"response_text": turn.ResponseText,
|
||||
"error": turn.Error,
|
||||
"created_at": turn.CreatedAt.UnixMilli(),
|
||||
"updated_at": turn.UpdatedAt.UnixMilli(),
|
||||
}
|
||||
}
|
||||
|
||||
func withTurnRevision(current sdk.Doc, turn TurnRecord) sdk.Doc {
|
||||
doc := turnToDoc(turn)
|
||||
if revision, ok := current[sdk.FieldRev]; ok {
|
||||
doc[sdk.FieldRev] = revision
|
||||
}
|
||||
return doc
|
||||
}
|
||||
|
||||
func docToTurn(doc sdk.Doc) TurnRecord {
|
||||
turn := TurnRecord{
|
||||
TurnID: strField(doc, sdk.FieldID),
|
||||
SessionID: strField(doc, "session_id"),
|
||||
SourceID: strField(doc, "source_id"),
|
||||
Status: TurnStatus(strField(doc, "status")),
|
||||
OwnerID: strField(doc, "owner_id"),
|
||||
InputMessageID: strField(doc, "input_message_id"),
|
||||
AssistantMessageID: strField(doc, "assistant_message_id"),
|
||||
PartialText: strField(doc, "partial_text"),
|
||||
ResponseText: strField(doc, "response_text"),
|
||||
Error: strField(doc, "error"),
|
||||
}
|
||||
if attempt, ok := numField(doc, "attempt"); ok {
|
||||
turn.Attempt = int(attempt)
|
||||
}
|
||||
if ms, ok := numField(doc, "created_at"); ok {
|
||||
turn.CreatedAt = time.UnixMilli(ms).UTC()
|
||||
}
|
||||
if ms, ok := numField(doc, "updated_at"); ok {
|
||||
turn.UpdatedAt = time.UnixMilli(ms).UTC()
|
||||
}
|
||||
return turn
|
||||
}
|
||||
|
||||
func (s *nellSessionStore) Save(ctx context.Context, sc SessionContext) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -244,6 +449,19 @@ func (s *nellSessionStore) Delete(ctx context.Context, sessionID string) error {
|
||||
}
|
||||
s.dropMsgDB(sessionID)
|
||||
|
||||
turns, err := s.turns.AllDocs(ctx, sdk.DocRange{IncludeDocs: true})
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: delete turns: %w", err)
|
||||
}
|
||||
for _, row := range turns.Rows {
|
||||
if row.Doc == nil || strField(row.Doc, "session_id") != sessionID {
|
||||
continue
|
||||
}
|
||||
if _, err := s.turns.Remove(ctx, row.ID); err != nil && !errors.Is(err, sdk.ErrNotFound) {
|
||||
return fmt.Errorf("sessionstore: delete turn %s: %w", row.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := s.db.Remove(ctx, sessionID); err != nil && !errors.Is(err, sdk.ErrNotFound) {
|
||||
return fmt.Errorf("sessionstore: delete: %w", err)
|
||||
}
|
||||
@@ -755,6 +973,45 @@ func (s *nellSessionStore) ReplaceMessages(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *nellSessionStore) FindPriorReply(ctx context.Context, sessionID, sourceID, identity string) (string, error) {
|
||||
if sourceID == "" {
|
||||
return "", nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
id := CanonicalMessageID(sessionID, sourceID)
|
||||
db := s.messagesFor(sessionID)
|
||||
if _, err := db.Get(ctx, id); err != nil {
|
||||
if errors.Is(err, sdk.ErrNotFound) {
|
||||
return "", nil
|
||||
}
|
||||
return "", fmt.Errorf("sessionstore: find source message: %w", err)
|
||||
}
|
||||
count, err := s.messageCountLocked(ctx, sessionID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rows, err := s.newestRows(ctx, sessionID, count)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sessionstore: find prior reply: %w", err)
|
||||
}
|
||||
for i := len(rows) - 1; i >= 0; i-- {
|
||||
if rows[i].Doc == nil || strField(rows[i].Doc, sdk.FieldID) != id {
|
||||
continue
|
||||
}
|
||||
if i == 0 || rows[i-1].Doc == nil {
|
||||
return "", nil
|
||||
}
|
||||
reply := docToMessage(rows[i-1].Doc)
|
||||
if reply.From == identity && !isCompressionSummary(reply.Text) {
|
||||
return reply.Text, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (s *nellSessionStore) RecentMessages(ctx context.Context, sessionID string, n int) ([]Message, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
@@ -93,6 +93,24 @@ CREATE TRIGGER IF NOT EXISTS messages_au AFTER UPDATE ON messages BEGIN
|
||||
INSERT INTO messages_fts(messages_fts, rowid, sender, text) VALUES('delete', old.id, old.sender, old.text);
|
||||
INSERT INTO messages_fts(rowid, sender, text) VALUES (new.id, new.sender, new.text);
|
||||
END;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS turns (
|
||||
turn_id TEXT PRIMARY KEY,
|
||||
session_id TEXT NOT NULL,
|
||||
source_id TEXT NOT NULL DEFAULT '',
|
||||
status TEXT NOT NULL,
|
||||
attempt INTEGER NOT NULL DEFAULT 0,
|
||||
owner_id TEXT NOT NULL DEFAULT '',
|
||||
input_message_id TEXT NOT NULL DEFAULT '',
|
||||
assistant_message_id TEXT NOT NULL DEFAULT '',
|
||||
partial_text TEXT NOT NULL DEFAULT '',
|
||||
response_text TEXT NOT NULL DEFAULT '',
|
||||
error TEXT NOT NULL DEFAULT '',
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_turns_session_source ON turns(session_id, source_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_turns_status_updated ON turns(status, updated_at);
|
||||
`
|
||||
|
||||
// OpenSQLiteSessionStore opens (creating if needed) a SQLite-backed
|
||||
@@ -133,6 +151,252 @@ func (s *sqliteSessionStore) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
func (s *sqliteSessionStore) ClaimTurn(ctx context.Context, initial TurnRecord) (TurnRecord, TurnClaim, error) {
|
||||
if initial.TurnID == "" {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: turn ID is required")
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: claim turn: begin: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var turn TurnRecord
|
||||
var createdMS, updatedMS int64
|
||||
err = tx.QueryRowContext(ctx, `
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, error, created_at, updated_at
|
||||
FROM turns WHERE turn_id = ?`, initial.TurnID).Scan(
|
||||
&turn.TurnID, &turn.SessionID, &turn.SourceID, &turn.Status, &turn.Attempt, &turn.OwnerID,
|
||||
&turn.InputMessageID, &turn.AssistantMessageID, &turn.PartialText,
|
||||
&turn.ResponseText, &turn.Error, &createdMS, &updatedMS)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
now := time.Now().UTC()
|
||||
initial.Status = TurnStatusRunning
|
||||
initial.Attempt = 1
|
||||
if initial.CreatedAt.IsZero() {
|
||||
initial.CreatedAt = now
|
||||
}
|
||||
initial.UpdatedAt = now
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO turns (
|
||||
turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, error, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(turn_id) DO NOTHING`, turnValues(initial)...)
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: claim turn: insert: %w", err)
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: claim turn: rows affected: %w", err)
|
||||
}
|
||||
if affected == 1 {
|
||||
if err := tx.Commit(); err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: claim turn: commit: %w", err)
|
||||
}
|
||||
return initial, TurnClaimOwned, nil
|
||||
}
|
||||
// Another connection inserted the same turn between our read and
|
||||
// insert. Read it in this transaction and apply the normal claim
|
||||
// state machine instead of surfacing a uniqueness error.
|
||||
err = tx.QueryRowContext(ctx, `
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, error, created_at, updated_at
|
||||
FROM turns WHERE turn_id = ?`, initial.TurnID).Scan(
|
||||
&turn.TurnID, &turn.SessionID, &turn.SourceID, &turn.Status, &turn.Attempt, &turn.OwnerID,
|
||||
&turn.InputMessageID, &turn.AssistantMessageID, &turn.PartialText,
|
||||
&turn.ResponseText, &turn.Error, &createdMS, &updatedMS)
|
||||
}
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: read turn: %w", err)
|
||||
}
|
||||
turn.CreatedAt = time.UnixMilli(createdMS).UTC()
|
||||
turn.UpdatedAt = time.UnixMilli(updatedMS).UTC()
|
||||
|
||||
switch turn.Status {
|
||||
case TurnStatusCompleted:
|
||||
return turn, TurnClaimCompleted, nil
|
||||
case TurnStatusAccepted, TurnStatusRunning, TurnStatusPartial:
|
||||
return turn, TurnClaimInProgress, nil
|
||||
case TurnStatusFailed, TurnStatusCancelled:
|
||||
if turn.AssistantMessageID != "" {
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
UPDATE turns SET status = ?, error = ?, updated_at = ?
|
||||
WHERE turn_id = ? AND attempt = ? AND owner_id = ?`, string(TurnStatusCompleted), "",
|
||||
time.Now().UTC().UnixMilli(), turn.TurnID, turn.Attempt, turn.OwnerID)
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: finalize turn: %w", err)
|
||||
}
|
||||
if affected, err := result.RowsAffected(); err != nil || affected != 1 {
|
||||
return TurnRecord{}, "", fmt.Errorf("%w: %s", ErrTurnOwnershipLost, turn.TurnID)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: finalize turn: commit: %w", err)
|
||||
}
|
||||
turn.Status = TurnStatusCompleted
|
||||
turn.Error = ""
|
||||
return turn, TurnClaimCompleted, nil
|
||||
}
|
||||
oldOwner, oldAttempt := turn.OwnerID, turn.Attempt
|
||||
turn.Status = TurnStatusRunning
|
||||
turn.Attempt++
|
||||
turn.OwnerID = initial.OwnerID
|
||||
turn.Error = ""
|
||||
turn.UpdatedAt = time.Now().UTC()
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
UPDATE turns SET status = ?, attempt = ?, owner_id = ?, error = ?, updated_at = ?
|
||||
WHERE turn_id = ? AND attempt = ? AND owner_id = ?`, string(turn.Status), turn.Attempt, turn.OwnerID, turn.Error,
|
||||
turn.UpdatedAt.UnixMilli(), turn.TurnID, oldAttempt, oldOwner)
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: retry turn: %w", err)
|
||||
}
|
||||
if affected, err := result.RowsAffected(); err != nil || affected != 1 {
|
||||
return TurnRecord{}, "", fmt.Errorf("%w: %s", ErrTurnOwnershipLost, turn.TurnID)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: retry turn: commit: %w", err)
|
||||
}
|
||||
return turn, TurnClaimOwned, nil
|
||||
default:
|
||||
return turn, TurnClaimInProgress, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *sqliteSessionStore) RecoverTurns(ctx context.Context, ownerID string) error {
|
||||
if ownerID == "" {
|
||||
return fmt.Errorf("sessionstore: recovery owner ID is required")
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
UPDATE turns
|
||||
SET status = ?, error = ?, updated_at = ?
|
||||
WHERE status IN (?, ?, ?) AND owner_id <> ?`,
|
||||
string(TurnStatusFailed), "recovered after process restart", time.Now().UTC().UnixMilli(),
|
||||
string(TurnStatusAccepted), string(TurnStatusRunning), string(TurnStatusPartial), ownerID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: recover turns: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sqliteSessionStore) GetTurn(ctx context.Context, turnID string) (TurnRecord, bool, error) {
|
||||
if turnID == "" {
|
||||
return TurnRecord{}, false, nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
var turn TurnRecord
|
||||
var createdMS, updatedMS int64
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, error, created_at, updated_at
|
||||
FROM turns WHERE turn_id = ?`, turnID).Scan(
|
||||
&turn.TurnID, &turn.SessionID, &turn.SourceID, &turn.Status, &turn.Attempt, &turn.OwnerID,
|
||||
&turn.InputMessageID, &turn.AssistantMessageID, &turn.PartialText,
|
||||
&turn.ResponseText, &turn.Error, &createdMS, &updatedMS)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return TurnRecord{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return TurnRecord{}, false, fmt.Errorf("sessionstore: get turn: %w", err)
|
||||
}
|
||||
turn.CreatedAt = time.UnixMilli(createdMS).UTC()
|
||||
turn.UpdatedAt = time.UnixMilli(updatedMS).UTC()
|
||||
return turn, true, nil
|
||||
}
|
||||
|
||||
func (s *sqliteSessionStore) SaveTurn(ctx context.Context, turn TurnRecord) error {
|
||||
if turn.TurnID == "" {
|
||||
return fmt.Errorf("sessionstore: turn ID is required")
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
var currentAttempt int
|
||||
var currentOwner string
|
||||
if err := s.db.QueryRowContext(ctx,
|
||||
`SELECT attempt, owner_id FROM turns WHERE turn_id = ?`, turn.TurnID).
|
||||
Scan(¤tAttempt, ¤tOwner); errors.Is(err, sql.ErrNoRows) {
|
||||
return fmt.Errorf("sessionstore: save turn: turn not found")
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("sessionstore: save turn: read: %w", err)
|
||||
}
|
||||
if currentAttempt != turn.Attempt || currentOwner != turn.OwnerID {
|
||||
return fmt.Errorf("%w: %s", ErrTurnOwnershipLost, turn.TurnID)
|
||||
}
|
||||
if turn.UpdatedAt.IsZero() {
|
||||
turn.UpdatedAt = time.Now().UTC()
|
||||
}
|
||||
result, err := s.db.ExecContext(ctx, `
|
||||
UPDATE turns SET
|
||||
session_id = ?, source_id = ?, status = ?, attempt = ?, owner_id = ?,
|
||||
input_message_id = ?, assistant_message_id = ?, partial_text = ?,
|
||||
response_text = ?, error = ?, created_at = ?, updated_at = ?
|
||||
WHERE turn_id = ? AND attempt = ? AND owner_id = ?`,
|
||||
turn.SessionID, turn.SourceID, string(turn.Status), turn.Attempt, turn.OwnerID,
|
||||
turn.InputMessageID, turn.AssistantMessageID, turn.PartialText,
|
||||
turn.ResponseText, turn.Error, turn.CreatedAt.UnixMilli(), turn.UpdatedAt.UnixMilli(),
|
||||
turn.TurnID, turn.Attempt, turn.OwnerID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: save turn: %w", err)
|
||||
}
|
||||
if affected, err := result.RowsAffected(); err != nil {
|
||||
return fmt.Errorf("sessionstore: save turn: rows affected: %w", err)
|
||||
} else if affected != 1 {
|
||||
return fmt.Errorf("%w: %s", ErrTurnOwnershipLost, turn.TurnID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sqliteSessionStore) ListRecoverableTurns(ctx context.Context) ([]TurnRecord, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, error, created_at, updated_at
|
||||
FROM turns WHERE status IN (?, ?, ?) ORDER BY updated_at ASC`,
|
||||
string(TurnStatusAccepted), string(TurnStatusRunning), string(TurnStatusPartial))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: list recoverable turns: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
var out []TurnRecord
|
||||
for rows.Next() {
|
||||
var turn TurnRecord
|
||||
var createdMS, updatedMS int64
|
||||
if err := rows.Scan(
|
||||
&turn.TurnID, &turn.SessionID, &turn.SourceID, &turn.Status, &turn.Attempt, &turn.OwnerID,
|
||||
&turn.InputMessageID, &turn.AssistantMessageID, &turn.PartialText,
|
||||
&turn.ResponseText, &turn.Error, &createdMS, &updatedMS); err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: scan recoverable turn: %w", err)
|
||||
}
|
||||
turn.CreatedAt = time.UnixMilli(createdMS).UTC()
|
||||
turn.UpdatedAt = time.UnixMilli(updatedMS).UTC()
|
||||
out = append(out, turn)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: list recoverable turns: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func turnValues(turn TurnRecord) []any {
|
||||
return []any{
|
||||
turn.TurnID, turn.SessionID, turn.SourceID, string(turn.Status), turn.Attempt, turn.OwnerID,
|
||||
turn.InputMessageID, turn.AssistantMessageID, turn.PartialText,
|
||||
turn.ResponseText, turn.Error, turn.CreatedAt.UnixMilli(), turn.UpdatedAt.UnixMilli(),
|
||||
}
|
||||
}
|
||||
|
||||
// ── Sessions ────────────────────────────────────────────────────────────────
|
||||
|
||||
func (s *sqliteSessionStore) Save(ctx context.Context, sc SessionContext) error {
|
||||
@@ -196,6 +460,9 @@ func (s *sqliteSessionStore) Delete(ctx context.Context, sessionID string) error
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM messages WHERE session_id = ?`, sessionID); err != nil {
|
||||
return fmt.Errorf("sessionstore: delete messages: %w", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM turns WHERE session_id = ?`, sessionID); err != nil {
|
||||
return fmt.Errorf("sessionstore: delete turns: %w", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM sessions WHERE session_id = ?`, sessionID); err != nil {
|
||||
return fmt.Errorf("sessionstore: delete session: %w", err)
|
||||
}
|
||||
@@ -442,6 +709,40 @@ func (s *sqliteSessionStore) ReplaceMessages(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sqliteSessionStore) FindPriorReply(ctx context.Context, sessionID, sourceID, identity string) (string, error) {
|
||||
if sourceID == "" {
|
||||
return "", nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
var ts int64
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT ts FROM messages WHERE session_id = ? AND message_id = ?`,
|
||||
sessionID, CanonicalMessageID(sessionID, sourceID)).Scan(&ts)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sessionstore: find source message: %w", err)
|
||||
}
|
||||
var sender, text string
|
||||
err = s.db.QueryRowContext(ctx, `
|
||||
SELECT sender, text FROM messages
|
||||
WHERE session_id = ? AND ts > ? ORDER BY ts ASC LIMIT 1`, sessionID, ts).
|
||||
Scan(&sender, &text)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sessionstore: find prior reply: %w", err)
|
||||
}
|
||||
if sender != identity || isCompressionSummary(text) {
|
||||
return "", nil
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
func (s *sqliteSessionStore) RecentMessages(ctx context.Context, sessionID string, n int) ([]Message, error) {
|
||||
if n <= 0 {
|
||||
return nil, nil
|
||||
|
||||
@@ -0,0 +1,370 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
// TurnModel prepares provider-specific tools and executes one completed chat
|
||||
// generation. The gateway owns the conversation lifecycle; the composition
|
||||
// root implements this seam over the selected model runtime.
|
||||
type TurnModel interface {
|
||||
Prepare(ctx context.Context, model string, extra []tools.ToolEntry) (PreparedTurnModel, error)
|
||||
}
|
||||
|
||||
// PreparedTurnModel is a provider-specific generation plan. Preparation is
|
||||
// separate from generation because the gateway needs tool metadata before it
|
||||
// can calculate the conversation budget and render the system prompt.
|
||||
type PreparedTurnModel interface {
|
||||
ToolSummaries() []ToolSummary
|
||||
ToolSchemaTokens() int
|
||||
Generate(ctx context.Context, request TurnModelRequest, onDelta func(string)) (string, error)
|
||||
}
|
||||
|
||||
// TurnModelRequest is the provider-neutral request assembled by the gateway.
|
||||
// Messages include the system prompt as their first entry.
|
||||
type TurnModelRequest struct {
|
||||
Messages []CompressedMessage
|
||||
MaxTokens int
|
||||
}
|
||||
|
||||
// PersonaPromptSource supplies the active persona prompt for a session.
|
||||
type PersonaPromptSource interface {
|
||||
GetActive(sessionID string) string
|
||||
}
|
||||
|
||||
// TurnEventPublisher receives completed primary chat-turn events.
|
||||
type TurnEventPublisher interface {
|
||||
Publish(events.Event)
|
||||
}
|
||||
|
||||
// TurnReplayStore provides a durable source-ID lookup for redeliveries that
|
||||
// have fallen outside the recent model-context window. It is optional for
|
||||
// compatibility with small test stores; production session stores implement
|
||||
// it so retries do not depend on retaining old context.
|
||||
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
|
||||
// runner. The runner is channel-neutral apart from the channel name used in
|
||||
// prompts, session tools, and completion events.
|
||||
type TurnRunnerConfig struct {
|
||||
Router *Router
|
||||
Sessions SessionStore
|
||||
Models ModelManager
|
||||
Personas PersonaPromptSource
|
||||
Model TurnModel
|
||||
Ledger TurnLedger
|
||||
OwnerID string
|
||||
PersistenceTimeout time.Duration
|
||||
TaskLister ChatTaskLister
|
||||
Tasks TaskCreator
|
||||
TaskIdentity string
|
||||
Bus TurnEventPublisher
|
||||
BotUser string
|
||||
Channel string
|
||||
Operator string
|
||||
Log *slog.Logger
|
||||
}
|
||||
|
||||
// TurnRunner owns one chat turn from session resolution through model
|
||||
// generation and message persistence. Channel adapters delegate to it through
|
||||
// Router.LLM and Router.LLMStream compatibility callbacks.
|
||||
type TurnRunner struct {
|
||||
TurnRunnerConfig
|
||||
recoveryMu sync.Mutex
|
||||
recovered bool
|
||||
}
|
||||
|
||||
// NewTurnRunner constructs a chat turn runner.
|
||||
func NewTurnRunner(cfg TurnRunnerConfig) *TurnRunner {
|
||||
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}
|
||||
}
|
||||
|
||||
// Recover marks turns from a previous process as failed and retryable. It is
|
||||
// intended to run during composition before the channel starts accepting
|
||||
// messages; Run also calls the same guard for small embedders and tests.
|
||||
func (r *TurnRunner) Recover(ctx context.Context) error {
|
||||
return r.recoverTurns(ctx)
|
||||
}
|
||||
|
||||
// Respond adapts the runner to Router.LLM.
|
||||
func (r *TurnRunner) Respond(ctx context.Context, msg Message) (string, error) {
|
||||
return r.Run(ctx, msg, nil)
|
||||
}
|
||||
|
||||
// RespondStream adapts the runner to Router.LLMStream.
|
||||
func (r *TurnRunner) RespondStream(ctx context.Context, msg Message, onDelta func(string)) (string, error) {
|
||||
return r.Run(ctx, msg, onDelta)
|
||||
}
|
||||
|
||||
// Run executes one turn. A completed duplicate source message replays its
|
||||
// durable response without invoking the model again.
|
||||
func (r *TurnRunner) Run(ctx context.Context, msg Message, onDelta func(string)) (string, error) {
|
||||
if r.Router == nil {
|
||||
return "", fmt.Errorf("chat turn router is not configured")
|
||||
}
|
||||
if r.Sessions == nil {
|
||||
return "", fmt.Errorf("chat turn session store is not configured")
|
||||
}
|
||||
if r.Ledger == nil {
|
||||
return "", fmt.Errorf("chat turn ledger is not configured")
|
||||
}
|
||||
if r.Models == nil {
|
||||
return "", fmt.Errorf("chat turn model manager is not configured")
|
||||
}
|
||||
if r.Model == nil {
|
||||
return "", fmt.Errorf("chat turn model is not configured")
|
||||
}
|
||||
if err := r.recoverTurns(ctx); err != nil {
|
||||
return "", fmt.Errorf("recover chat turns: %w", err)
|
||||
}
|
||||
|
||||
sessionID, err := r.Router.ResolveSessionKey(ctx, msg)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve chat session: %w", err)
|
||||
}
|
||||
turnID := CanonicalTurnID(sessionID, msg.SourceID)
|
||||
if turnID == "" {
|
||||
turnID = NewTurnID()
|
||||
}
|
||||
turn, claim, err := r.Ledger.ClaimTurn(ctx, TurnRecord{
|
||||
TurnID: turnID,
|
||||
SessionID: sessionID,
|
||||
SourceID: msg.SourceID,
|
||||
Status: TurnStatusAccepted,
|
||||
OwnerID: r.OwnerID,
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("claim chat turn: %w", err)
|
||||
}
|
||||
switch claim {
|
||||
case TurnClaimCompleted:
|
||||
if onDelta != nil && turn.ResponseText != "" {
|
||||
onDelta(turn.ResponseText)
|
||||
}
|
||||
return turn.ResponseText, nil
|
||||
case TurnClaimInProgress:
|
||||
return "", fmt.Errorf("%w: %s", ErrTurnInProgress, turn.TurnID)
|
||||
}
|
||||
|
||||
history, err := r.Sessions.RecentMessages(ctx, sessionID, 100)
|
||||
if err != nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("load chat history: %w", err))
|
||||
}
|
||||
prior := PriorReply(history, sessionID, msg.SourceID, r.BotUser)
|
||||
if prior == "" && msg.SourceID != "" {
|
||||
if replayStore, ok := r.Sessions.(TurnReplayStore); ok {
|
||||
prior, err = replayStore.FindPriorReply(ctx, sessionID, msg.SourceID, r.BotUser)
|
||||
if err != nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("find prior chat reply: %w", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
if prior != "" {
|
||||
turn.Status = TurnStatusCompleted
|
||||
turn.ResponseText = prior
|
||||
turn.Error = ""
|
||||
turn.UpdatedAt = time.Now().UTC()
|
||||
if err := r.saveTurn(ctx, turn); err != nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("save replayed chat turn: %w", err))
|
||||
}
|
||||
if r.Log != nil {
|
||||
r.Log.Info("replaying the reply to a redelivered message",
|
||||
"session", sessionID, "source_id", msg.SourceID)
|
||||
}
|
||||
if onDelta != nil {
|
||||
onDelta(prior)
|
||||
}
|
||||
return prior, nil
|
||||
}
|
||||
|
||||
msg.MessageID = messageIDForTurn(sessionID, msg)
|
||||
if turn.InputMessageID == "" {
|
||||
if err := r.Sessions.SaveMessage(ctx, sessionID, msg); err != nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("save inbound chat message: %w", err))
|
||||
}
|
||||
turn.InputMessageID = msg.MessageID
|
||||
turn.UpdatedAt = time.Now().UTC()
|
||||
if err := r.saveTurn(ctx, turn); err != nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("save chat turn input: %w", err))
|
||||
}
|
||||
} else {
|
||||
msg.MessageID = turn.InputMessageID
|
||||
}
|
||||
if !messageInHistory(history, msg.SourceID) {
|
||||
history = append(history, msg)
|
||||
}
|
||||
|
||||
compressed := compressTurnHistory(history, r.BotUser)
|
||||
extraTools := append(
|
||||
TaskTools(r.TaskLister, r.Tasks, r.TaskIdentity),
|
||||
SessionTools(r.Sessions, r.Router.SessionTracker(), r.Channel, msg)...,
|
||||
)
|
||||
modelName := r.Models.ActiveModel()
|
||||
prepared, err := r.Model.Prepare(ctx, modelName, extraTools)
|
||||
if err != nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("build chat model: %w", err))
|
||||
}
|
||||
if prepared == nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("build chat model: empty preparation"))
|
||||
}
|
||||
toolInfo := prepared.ToolSummaries()
|
||||
persona := ""
|
||||
if r.Personas != nil {
|
||||
persona = r.Personas.GetActive(sessionID)
|
||||
}
|
||||
systemPrompt := BuildSystemPrompt(SystemPromptConfig{
|
||||
Persona: persona,
|
||||
Tools: toolInfo,
|
||||
Channel: r.Channel,
|
||||
Model: modelName,
|
||||
SessionID: sessionID,
|
||||
Now: time.Now(),
|
||||
Operator: r.Operator,
|
||||
})
|
||||
modelDetails := ModelDetails{}
|
||||
if detailed, ok := r.Models.(DetailedModelManager); ok {
|
||||
modelDetails, _ = detailed.ModelDetails(modelName)
|
||||
}
|
||||
compression, err := CompressionConfigForModel(
|
||||
modelDetails,
|
||||
EstimateTokens(systemPrompt)+prepared.ToolSchemaTokens(),
|
||||
)
|
||||
if err != nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("derive chat context budget: %w", err))
|
||||
}
|
||||
if r.Log != nil && modelDetails.ContextWindow <= 0 {
|
||||
r.Log.Warn("chat model has no context metadata; using compatibility compression budget",
|
||||
"model", modelName)
|
||||
}
|
||||
view := CompressHistory(compressed, compression)
|
||||
if r.Log != nil {
|
||||
r.Log.Info("chat turn",
|
||||
"session", sessionID,
|
||||
"channel", msg.ChannelID,
|
||||
"thread", msg.ThreadID,
|
||||
"history_messages", len(history),
|
||||
"model", modelName,
|
||||
"context_window", modelDetails.ContextWindow,
|
||||
"prompt_budget", compression.MaxPromptTokens)
|
||||
}
|
||||
view.Messages = append(
|
||||
[]CompressedMessage{{Role: "system", Content: systemPrompt}}, view.Messages...,
|
||||
)
|
||||
text, err := prepared.Generate(ctx, TurnModelRequest{
|
||||
Messages: view.Messages,
|
||||
MaxTokens: modelDetails.MaxOutputTokens,
|
||||
}, onDelta)
|
||||
if err != nil {
|
||||
return "", r.failTurn(ctx, turn, err)
|
||||
}
|
||||
assistant := Message{MessageID: assistantMessageIDForTurn(turn.TurnID), From: r.BotUser, Text: text}
|
||||
if err := r.Sessions.SaveMessage(ctx, sessionID, assistant); err != nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("save outbound chat message: %w", err))
|
||||
}
|
||||
turn.Status = TurnStatusCompleted
|
||||
turn.AssistantMessageID = assistant.MessageID
|
||||
turn.ResponseText = text
|
||||
turn.Error = ""
|
||||
turn.UpdatedAt = time.Now().UTC()
|
||||
if err := r.saveTurn(ctx, turn); err != nil {
|
||||
return "", r.failTurn(ctx, turn, fmt.Errorf("save completed chat turn: %w", err))
|
||||
}
|
||||
if r.Bus != nil {
|
||||
r.Bus.Publish(events.Event{
|
||||
Kind: events.KindTurnCompleted,
|
||||
Detail: sessionID,
|
||||
Data: map[string]any{"session": sessionID, "channel": r.Channel},
|
||||
})
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
func (r *TurnRunner) failTurn(ctx context.Context, turn TurnRecord, cause error) error {
|
||||
if errors.Is(cause, context.Canceled) {
|
||||
turn.Status = TurnStatusCancelled
|
||||
} else {
|
||||
turn.Status = TurnStatusFailed
|
||||
}
|
||||
turn.Error = cause.Error()
|
||||
turn.UpdatedAt = time.Now().UTC()
|
||||
if err := r.saveTurn(ctx, turn); err != nil {
|
||||
return errors.Join(cause, fmt.Errorf("save failed chat turn: %w", err))
|
||||
}
|
||||
return cause
|
||||
}
|
||||
|
||||
func (r *TurnRunner) recoverTurns(ctx context.Context) error {
|
||||
r.recoveryMu.Lock()
|
||||
defer r.recoveryMu.Unlock()
|
||||
if r.recovered {
|
||||
return nil
|
||||
}
|
||||
if err := r.Ledger.RecoverTurns(ctx, r.OwnerID); err != nil {
|
||||
return err
|
||||
}
|
||||
r.recovered = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *TurnRunner) saveTurn(ctx context.Context, turn TurnRecord) error {
|
||||
persistCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), r.PersistenceTimeout)
|
||||
defer cancel()
|
||||
return r.Ledger.SaveTurn(persistCtx, turn)
|
||||
}
|
||||
|
||||
func messageIDForTurn(sessionID string, msg Message) string {
|
||||
if msg.MessageID != "" {
|
||||
return msg.MessageID
|
||||
}
|
||||
if id := CanonicalMessageID(sessionID, msg.SourceID); id != "" {
|
||||
return id
|
||||
}
|
||||
return NewTurnID()
|
||||
}
|
||||
|
||||
func messageInHistory(history []Message, sourceID string) bool {
|
||||
if sourceID == "" {
|
||||
return false
|
||||
}
|
||||
for _, stored := range history {
|
||||
if stored.SourceID == sourceID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func compressTurnHistory(history []Message, botUser string) []CompressedMessage {
|
||||
compressed := make([]CompressedMessage, 0, len(history))
|
||||
for _, message := range history {
|
||||
role := "user"
|
||||
if message.From == botUser {
|
||||
role = "assistant"
|
||||
}
|
||||
compressed = append(compressed, CompressedMessage{Role: role, Content: message.Text})
|
||||
}
|
||||
return compressed
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// TurnStatus is the durable lifecycle state of one chat generation.
|
||||
type TurnStatus string
|
||||
|
||||
const (
|
||||
TurnStatusAccepted TurnStatus = "accepted"
|
||||
TurnStatusRunning TurnStatus = "running"
|
||||
TurnStatusPartial TurnStatus = "partial"
|
||||
TurnStatusCompleted TurnStatus = "completed"
|
||||
TurnStatusFailed TurnStatus = "failed"
|
||||
TurnStatusCancelled TurnStatus = "cancelled"
|
||||
)
|
||||
|
||||
// TurnClaim describes what happened when a caller claimed a durable turn.
|
||||
type TurnClaim string
|
||||
|
||||
const (
|
||||
TurnClaimOwned TurnClaim = "owned"
|
||||
TurnClaimInProgress TurnClaim = "in_progress"
|
||||
TurnClaimCompleted TurnClaim = "completed"
|
||||
)
|
||||
|
||||
// ErrTurnInProgress indicates that another worker currently owns the turn.
|
||||
var ErrTurnInProgress = errors.New("chat turn already in progress")
|
||||
|
||||
// ErrTurnOwnershipLost indicates that a stale worker attempted to overwrite a
|
||||
// newer attempt of the same durable turn.
|
||||
var ErrTurnOwnershipLost = errors.New("chat turn ownership lost")
|
||||
|
||||
// TurnRecord is the durable identity and outcome projection for one chat
|
||||
// generation. ResponseText is retained so a completed duplicate can replay
|
||||
// without depending on the bounded model-context history window.
|
||||
type TurnRecord struct {
|
||||
TurnID string
|
||||
SessionID string
|
||||
SourceID string
|
||||
Status TurnStatus
|
||||
Attempt int
|
||||
OwnerID string
|
||||
InputMessageID string
|
||||
AssistantMessageID string
|
||||
PartialText string
|
||||
ResponseText string
|
||||
Error string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// TurnLedger persists chat-turn identity and lifecycle independently from the
|
||||
// conversation message history. Session stores implement it as an optional
|
||||
// capability so existing narrow test stores remain source-compatible.
|
||||
type TurnLedger interface {
|
||||
ClaimTurn(ctx context.Context, initial TurnRecord) (TurnRecord, TurnClaim, error)
|
||||
// RecoverTurns marks recoverable turns owned by another process as failed
|
||||
// so the next delivery can retry them without racing an active owner.
|
||||
RecoverTurns(ctx context.Context, ownerID string) error
|
||||
GetTurn(ctx context.Context, turnID string) (TurnRecord, bool, error)
|
||||
SaveTurn(ctx context.Context, turn TurnRecord) error
|
||||
ListRecoverableTurns(ctx context.Context) ([]TurnRecord, error)
|
||||
}
|
||||
|
||||
// NewTurnID returns a unique ID for an unsourced turn.
|
||||
func NewTurnID() string {
|
||||
return uuid.NewString()
|
||||
}
|
||||
|
||||
// CanonicalTurnID returns a stable ID for a source message within one session.
|
||||
// Empty source IDs intentionally have no canonical ID because unsourced
|
||||
// messages cannot be safely deduplicated.
|
||||
func assistantMessageIDForTurn(turnID string) string {
|
||||
return uuid.NewSHA1(uuid.NameSpaceURL, []byte("archie-assistant-turn:\x00"+turnID)).String()
|
||||
}
|
||||
|
||||
func CanonicalTurnID(sessionID, sourceID string) string {
|
||||
if sourceID == "" {
|
||||
return ""
|
||||
}
|
||||
return uuid.NewSHA1(uuid.NameSpaceURL, []byte("archie-turn:\x00"+sessionID+"\x00"+sourceID)).String()
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/NellDB/logstore"
|
||||
)
|
||||
|
||||
func TestTurnLedgerClaimLifecycleIsIdempotentAcrossBackends(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
open func(t *testing.T) (TurnLedger, func())
|
||||
}{
|
||||
{
|
||||
name: "nell memory",
|
||||
open: func(t *testing.T) (TurnLedger, func()) {
|
||||
store := NewSessionStoreMemory("test-node")
|
||||
return store.(TurnLedger), func() { _ = store.Close() }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sqlite memory",
|
||||
open: func(t *testing.T) (TurnLedger, func()) {
|
||||
store, err := NewSQLiteSessionStoreMemory()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return store.(TurnLedger), func() { _ = store.Close() }
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ledger, cleanup := tt.open(t)
|
||||
t.Cleanup(cleanup)
|
||||
ctx := context.Background()
|
||||
now := time.UnixMilli(1000).UTC()
|
||||
initial := TurnRecord{
|
||||
TurnID: CanonicalTurnID("session-1", "source-1"),
|
||||
SessionID: "session-1",
|
||||
SourceID: "source-1",
|
||||
Status: TurnStatusAccepted,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
claimed, result, err := ledger.ClaimTurn(ctx, initial)
|
||||
if err != nil {
|
||||
t.Fatalf("first ClaimTurn() error = %v", err)
|
||||
}
|
||||
if result != TurnClaimOwned || claimed.Status != TurnStatusRunning || claimed.Attempt != 1 {
|
||||
t.Fatalf("first claim = %#v, result %q", claimed, result)
|
||||
}
|
||||
|
||||
inFlight, result, err := ledger.ClaimTurn(ctx, initial)
|
||||
if err != nil {
|
||||
t.Fatalf("in-flight ClaimTurn() error = %v", err)
|
||||
}
|
||||
if result != TurnClaimInProgress || inFlight.TurnID != initial.TurnID {
|
||||
t.Fatalf("in-flight claim = %#v, result %q", inFlight, result)
|
||||
}
|
||||
|
||||
claimed.Status = TurnStatusCompleted
|
||||
claimed.ResponseText = "answer"
|
||||
claimed.AssistantMessageID = "assistant-1"
|
||||
claimed.UpdatedAt = now.Add(time.Second)
|
||||
if err := ledger.SaveTurn(ctx, claimed); err != nil {
|
||||
t.Fatalf("SaveTurn(completed) error = %v", err)
|
||||
}
|
||||
|
||||
completed, result, err := ledger.ClaimTurn(ctx, initial)
|
||||
if err != nil {
|
||||
t.Fatalf("completed ClaimTurn() error = %v", err)
|
||||
}
|
||||
if result != TurnClaimCompleted || completed.ResponseText != "answer" {
|
||||
t.Fatalf("completed claim = %#v, result %q", completed, result)
|
||||
}
|
||||
|
||||
completed.Status = TurnStatusFailed
|
||||
completed.AssistantMessageID = ""
|
||||
completed.ResponseText = ""
|
||||
completed.Error = "provider unavailable"
|
||||
completed.UpdatedAt = now.Add(2 * time.Second)
|
||||
if err := ledger.SaveTurn(ctx, completed); err != nil {
|
||||
t.Fatalf("SaveTurn(failed) error = %v", err)
|
||||
}
|
||||
retry, result, err := ledger.ClaimTurn(ctx, initial)
|
||||
if err != nil {
|
||||
t.Fatalf("retry ClaimTurn() error = %v", err)
|
||||
}
|
||||
if result != TurnClaimOwned || retry.Status != TurnStatusRunning || retry.Attempt != 2 {
|
||||
t.Fatalf("retry claim = %#v, result %q", retry, result)
|
||||
}
|
||||
|
||||
recoverable, err := ledger.ListRecoverableTurns(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListRecoverableTurns() error = %v", err)
|
||||
}
|
||||
if len(recoverable) != 1 || recoverable[0].TurnID != initial.TurnID {
|
||||
t.Fatalf("recoverable turns = %#v, want retried turn", recoverable)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurnLedgerReclaimsAcrossProcessOwnersAndRejectsStaleWrites(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
open func(t *testing.T) (TurnLedger, func())
|
||||
}{
|
||||
{
|
||||
name: "nell memory",
|
||||
open: func(t *testing.T) (TurnLedger, func()) {
|
||||
store := NewSessionStoreMemory("test-node")
|
||||
return store.(TurnLedger), func() { _ = store.Close() }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sqlite memory",
|
||||
open: func(t *testing.T) (TurnLedger, func()) {
|
||||
store, err := NewSQLiteSessionStoreMemory()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return store.(TurnLedger), func() { _ = store.Close() }
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ledger, cleanup := tt.open(t)
|
||||
t.Cleanup(cleanup)
|
||||
ctx := context.Background()
|
||||
initial := TurnRecord{
|
||||
TurnID: CanonicalTurnID("session-owner", "source-owner"),
|
||||
SessionID: "session-owner",
|
||||
SourceID: "source-owner",
|
||||
OwnerID: "process-a",
|
||||
Status: TurnStatusAccepted,
|
||||
}
|
||||
old, claim, err := ledger.ClaimTurn(ctx, initial)
|
||||
if err != nil || claim != TurnClaimOwned {
|
||||
t.Fatalf("first claim = %#v, %q, %v", old, claim, err)
|
||||
}
|
||||
newOwner := initial
|
||||
newOwner.OwnerID = "process-b"
|
||||
inProgress, claim, err := ledger.ClaimTurn(ctx, newOwner)
|
||||
if err != nil || claim != TurnClaimInProgress {
|
||||
t.Fatalf("cross-owner claim = %#v, %q, %v; want in progress", inProgress, claim, err)
|
||||
}
|
||||
if err := ledger.RecoverTurns(ctx, "process-b"); err != nil {
|
||||
t.Fatalf("RecoverTurns() error = %v", err)
|
||||
}
|
||||
reclaimed, claim, err := ledger.ClaimTurn(ctx, newOwner)
|
||||
if err != nil || claim != TurnClaimOwned {
|
||||
t.Fatalf("reclaim = %#v, %q, %v", reclaimed, claim, err)
|
||||
}
|
||||
if reclaimed.Attempt != 2 || reclaimed.OwnerID != "process-b" {
|
||||
t.Fatalf("reclaimed turn = %#v, want attempt 2 owned by process-b", reclaimed)
|
||||
}
|
||||
old.Status = TurnStatusCompleted
|
||||
old.ResponseText = "stale response"
|
||||
if err := ledger.SaveTurn(ctx, old); !errors.Is(err, ErrTurnOwnershipLost) {
|
||||
t.Fatalf("stale SaveTurn() error = %v, want ErrTurnOwnershipLost", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionDeleteRemovesTurns(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
open func(t *testing.T) SessionStore
|
||||
}{
|
||||
{
|
||||
name: "nell memory",
|
||||
open: func(t *testing.T) SessionStore { return NewSessionStoreMemory("test-node") },
|
||||
},
|
||||
{
|
||||
name: "sqlite memory",
|
||||
open: func(t *testing.T) SessionStore {
|
||||
store, err := NewSQLiteSessionStoreMemory()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return store
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := tt.open(t)
|
||||
t.Cleanup(func() { _ = store.Close() })
|
||||
ledger := store.(TurnLedger)
|
||||
ctx := context.Background()
|
||||
turn := TurnRecord{
|
||||
TurnID: CanonicalTurnID("session-delete", "source-delete"), SessionID: "session-delete",
|
||||
SourceID: "source-delete", OwnerID: "process-a", Status: TurnStatusAccepted,
|
||||
}
|
||||
if _, claim, err := ledger.ClaimTurn(ctx, turn); err != nil || claim != TurnClaimOwned {
|
||||
t.Fatalf("ClaimTurn() = %q, %v", claim, err)
|
||||
}
|
||||
if err := store.Delete(ctx, turn.SessionID); err != nil {
|
||||
t.Fatalf("Delete() error = %v", err)
|
||||
}
|
||||
if _, ok, err := ledger.GetTurn(ctx, turn.TurnID); err != nil || ok {
|
||||
t.Fatalf("GetTurn() after delete = ok %v, err %v; want absent", ok, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNellTurnLedgerSurvivesLogReopen(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "sessions.log")
|
||||
ctx := context.Background()
|
||||
open := func() SessionStore {
|
||||
log, err := logstore.OpenLog(path, "test-node")
|
||||
if err != nil {
|
||||
t.Fatalf("OpenLog() error = %v", err)
|
||||
}
|
||||
return NewSessionStore(log, "test-node")
|
||||
}
|
||||
store := open()
|
||||
turn := TurnRecord{
|
||||
TurnID: CanonicalTurnID("session-nell-reopen", "source-nell-reopen"),
|
||||
SessionID: "session-nell-reopen", SourceID: "source-nell-reopen",
|
||||
OwnerID: "process-a", Status: TurnStatusAccepted,
|
||||
}
|
||||
claimed, claim, err := store.(TurnLedger).ClaimTurn(ctx, turn)
|
||||
if err != nil || claim != TurnClaimOwned {
|
||||
t.Fatalf("ClaimTurn() = %#v, %q, %v", claimed, claim, err)
|
||||
}
|
||||
claimed.Status = TurnStatusCompleted
|
||||
claimed.ResponseText = "durable NellDB response"
|
||||
if err := store.(TurnLedger).SaveTurn(ctx, claimed); err != nil {
|
||||
t.Fatalf("SaveTurn() error = %v", err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatalf("Close() error = %v", err)
|
||||
}
|
||||
|
||||
reopened := open()
|
||||
defer func() { _ = reopened.Close() }()
|
||||
got, ok, err := reopened.(TurnLedger).GetTurn(ctx, turn.TurnID)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("GetTurn() after reopen = %#v, %v, %v", got, ok, err)
|
||||
}
|
||||
if got.Status != TurnStatusCompleted || got.ResponseText != "durable NellDB response" || got.OwnerID != "process-a" {
|
||||
t.Fatalf("reopened turn = %#v, want completed durable record", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteTurnLedgerSurvivesReopen(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "sessions.db")
|
||||
ctx := context.Background()
|
||||
store, err := OpenSQLiteSessionStore(path)
|
||||
if err != nil {
|
||||
t.Fatalf("OpenSQLiteSessionStore() error = %v", err)
|
||||
}
|
||||
ledger := store.(TurnLedger)
|
||||
turn := TurnRecord{
|
||||
TurnID: CanonicalTurnID("session-reopen", "source-reopen"),
|
||||
SessionID: "session-reopen",
|
||||
SourceID: "source-reopen",
|
||||
OwnerID: "process-a",
|
||||
Status: TurnStatusAccepted,
|
||||
}
|
||||
claimed, claim, err := ledger.ClaimTurn(ctx, turn)
|
||||
if err != nil || claim != TurnClaimOwned {
|
||||
t.Fatalf("ClaimTurn() = %#v, %q, %v", claimed, claim, err)
|
||||
}
|
||||
claimed.Status = TurnStatusCompleted
|
||||
claimed.ResponseText = "durable response"
|
||||
if err := ledger.SaveTurn(ctx, claimed); err != nil {
|
||||
t.Fatalf("SaveTurn() error = %v", err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatalf("Close() error = %v", err)
|
||||
}
|
||||
|
||||
reopened, err := OpenSQLiteSessionStore(path)
|
||||
if err != nil {
|
||||
t.Fatalf("reopen error = %v", err)
|
||||
}
|
||||
defer func() { _ = reopened.Close() }()
|
||||
got, ok, err := reopened.(TurnLedger).GetTurn(ctx, turn.TurnID)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("GetTurn() after reopen = %#v, %v, %v", got, ok, err)
|
||||
}
|
||||
if got.Status != TurnStatusCompleted || got.ResponseText != "durable response" || got.OwnerID != "process-a" {
|
||||
t.Fatalf("reopened turn = %#v, want completed durable record", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanonicalTurnIDIsStableAndSessionScoped(t *testing.T) {
|
||||
first := CanonicalTurnID("session-1", "source-1")
|
||||
if first == "" {
|
||||
t.Fatal("CanonicalTurnID() returned empty ID")
|
||||
}
|
||||
if got := CanonicalTurnID("session-1", "source-1"); got != first {
|
||||
t.Fatalf("same key produced %q, want %q", got, first)
|
||||
}
|
||||
if got := CanonicalTurnID("session-2", "source-1"); got == first {
|
||||
t.Fatal("different sessions shared a turn ID")
|
||||
}
|
||||
if got := CanonicalTurnID("session-1", ""); got != "" {
|
||||
t.Fatalf("empty source ID produced %q, want empty", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
type turnTestModel struct {
|
||||
prepared *turnTestPreparedModel
|
||||
prepareErr error
|
||||
gotExtra []tools.ToolEntry
|
||||
}
|
||||
|
||||
func (m *turnTestModel) Prepare(_ context.Context, _ string, extra []tools.ToolEntry) (PreparedTurnModel, error) {
|
||||
m.gotExtra = append([]tools.ToolEntry(nil), extra...)
|
||||
if m.prepareErr != nil {
|
||||
return nil, m.prepareErr
|
||||
}
|
||||
return m.prepared, nil
|
||||
}
|
||||
|
||||
type turnTestPreparedModel struct {
|
||||
reply string
|
||||
toolInfo []ToolSummary
|
||||
toolTokens int
|
||||
generateN int
|
||||
generateErr error
|
||||
generate func(context.Context) (string, error)
|
||||
lastRequest TurnModelRequest
|
||||
}
|
||||
|
||||
func (m *turnTestPreparedModel) ToolSummaries() []ToolSummary {
|
||||
return append([]ToolSummary(nil), m.toolInfo...)
|
||||
}
|
||||
|
||||
func (m *turnTestPreparedModel) ToolSchemaTokens() int {
|
||||
return m.toolTokens
|
||||
}
|
||||
|
||||
func (m *turnTestPreparedModel) Generate(ctx context.Context, req TurnModelRequest, onDelta func(string)) (string, error) {
|
||||
m.generateN++
|
||||
m.lastRequest = req
|
||||
if m.generate != nil {
|
||||
return m.generate(ctx)
|
||||
}
|
||||
if m.generateErr != nil {
|
||||
return "", m.generateErr
|
||||
}
|
||||
if onDelta != nil {
|
||||
onDelta(m.reply)
|
||||
}
|
||||
return m.reply, nil
|
||||
}
|
||||
|
||||
type turnTestEvents struct {
|
||||
published []events.Event
|
||||
}
|
||||
|
||||
func (b *turnTestEvents) Publish(event events.Event) {
|
||||
b.published = append(b.published, event)
|
||||
}
|
||||
|
||||
func TestTurnRunnerPersistsOneTurnAndPublishesCompletion(t *testing.T) {
|
||||
store := NewSessionStoreMemory("test-node")
|
||||
router := NewRouter(nil, nil, "telegram")
|
||||
router.Identity = "archie"
|
||||
router.InitSessions(store)
|
||||
|
||||
prepared := &turnTestPreparedModel{
|
||||
reply: "answer",
|
||||
toolInfo: []ToolSummary{{Name: "turn_tool", Description: "test tool"}},
|
||||
toolTokens: 12,
|
||||
}
|
||||
model := &turnTestModel{prepared: prepared}
|
||||
eventsBus := &turnTestEvents{}
|
||||
runner := NewTurnRunner(TurnRunnerConfig{
|
||||
Router: router,
|
||||
Sessions: store,
|
||||
Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"},
|
||||
Personas: NewPersonaRegistry(DefaultPersonas()),
|
||||
Model: model,
|
||||
Bus: eventsBus,
|
||||
BotUser: "archie",
|
||||
Channel: "telegram",
|
||||
Operator: "Sam",
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
})
|
||||
|
||||
var streamed string
|
||||
reply, err := runner.Run(context.Background(), Message{
|
||||
From: "user",
|
||||
ChannelID: "chat-1",
|
||||
SourceID: "source-1",
|
||||
Text: "hello",
|
||||
}, func(delta string) { streamed += delta })
|
||||
if err != nil {
|
||||
t.Fatalf("Run() error = %v", err)
|
||||
}
|
||||
if reply != "answer" || streamed != "answer" {
|
||||
t.Fatalf("reply = %q, streamed = %q", reply, streamed)
|
||||
}
|
||||
|
||||
history, err := store.RecentMessages(context.Background(), "chat-1", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("RecentMessages() error = %v", err)
|
||||
}
|
||||
if len(history) != 2 {
|
||||
t.Fatalf("history length = %d, want 2", len(history))
|
||||
}
|
||||
if history[0].Text != "hello" || history[1].Text != "answer" {
|
||||
t.Fatalf("history = %#v, want inbound then assistant", history)
|
||||
}
|
||||
if prepared.generateN != 1 {
|
||||
t.Fatalf("Generate calls = %d, want 1", prepared.generateN)
|
||||
}
|
||||
if len(model.gotExtra) == 0 {
|
||||
t.Fatal("Prepare received no session tools")
|
||||
}
|
||||
if len(prepared.lastRequest.Messages) < 2 || prepared.lastRequest.Messages[0].Role != "system" {
|
||||
t.Fatalf("model request messages = %#v, want system prompt and history", prepared.lastRequest.Messages)
|
||||
}
|
||||
if len(eventsBus.published) != 1 || eventsBus.published[0].Kind != events.KindTurnCompleted {
|
||||
t.Fatalf("published events = %#v, want one turn_completed event", eventsBus.published)
|
||||
}
|
||||
turn, ok, err := runner.Ledger.GetTurn(context.Background(), CanonicalTurnID("chat-1", "source-1"))
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("GetTurn() = %#v, %v, %v; want completed turn", turn, ok, err)
|
||||
}
|
||||
if turn.Status != TurnStatusCompleted || turn.ResponseText != "answer" || turn.InputMessageID == "" || turn.AssistantMessageID == "" {
|
||||
t.Fatalf("persisted turn = %#v, want completed IDs and response", turn)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurnRunnerGenerationFailureLeavesInboundWithoutReply(t *testing.T) {
|
||||
store := NewSessionStoreMemory("test-node")
|
||||
router := NewRouter(nil, nil, "telegram")
|
||||
router.Identity = "archie"
|
||||
router.InitSessions(store)
|
||||
prepared := &turnTestPreparedModel{generateErr: errors.New("provider unavailable")}
|
||||
bus := &turnTestEvents{}
|
||||
runner := NewTurnRunner(TurnRunnerConfig{
|
||||
Router: router,
|
||||
Sessions: store,
|
||||
Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"},
|
||||
Personas: NewPersonaRegistry(DefaultPersonas()),
|
||||
Model: &turnTestModel{prepared: prepared},
|
||||
Bus: bus,
|
||||
BotUser: "archie",
|
||||
Channel: "telegram",
|
||||
})
|
||||
|
||||
_, err := runner.Run(context.Background(), Message{
|
||||
From: "user", ChannelID: "chat-1", SourceID: "source-1", Text: "hello",
|
||||
}, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "provider unavailable") {
|
||||
t.Fatalf("Run() error = %v, want provider error", err)
|
||||
}
|
||||
history, err := store.RecentMessages(context.Background(), "chat-1", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("RecentMessages() error = %v", err)
|
||||
}
|
||||
if len(history) != 1 || history[0].Text != "hello" {
|
||||
t.Fatalf("history after failed generation = %#v, want inbound only", history)
|
||||
}
|
||||
if len(bus.published) != 0 {
|
||||
t.Fatalf("published events = %#v, want none", bus.published)
|
||||
}
|
||||
turn, ok, err := runner.Ledger.GetTurn(context.Background(), CanonicalTurnID("chat-1", "source-1"))
|
||||
if err != nil || !ok || turn.Status != TurnStatusFailed || turn.Error == "" {
|
||||
t.Fatalf("failed turn = %#v, %v, %v; want persisted failure", turn, ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurnRunnerCancellationPersistsCancelledState(t *testing.T) {
|
||||
store := NewSessionStoreMemory("test-node")
|
||||
router := NewRouter(nil, nil, "telegram")
|
||||
router.Identity = "archie"
|
||||
router.InitSessions(store)
|
||||
runCtx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
prepared := &turnTestPreparedModel{generate: func(context.Context) (string, error) {
|
||||
cancel()
|
||||
return "", context.Canceled
|
||||
}}
|
||||
runner := NewTurnRunner(TurnRunnerConfig{
|
||||
Router: router,
|
||||
Sessions: store,
|
||||
Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"},
|
||||
Personas: NewPersonaRegistry(DefaultPersonas()),
|
||||
Model: &turnTestModel{prepared: prepared},
|
||||
BotUser: "archie",
|
||||
Channel: "telegram",
|
||||
})
|
||||
|
||||
_, err := runner.Run(runCtx, Message{
|
||||
From: "user", ChannelID: "chat-cancel", SourceID: "source-cancel", Text: "hello",
|
||||
}, nil)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("Run() error = %v, want context.Canceled", err)
|
||||
}
|
||||
turn, ok, err := runner.Ledger.GetTurn(context.Background(), CanonicalTurnID("chat-cancel", "source-cancel"))
|
||||
if err != nil || !ok || turn.Status != TurnStatusCancelled {
|
||||
t.Fatalf("cancelled turn = %#v, %v, %v; want persisted cancellation", turn, ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurnRunnerRecoversTurnOwnedByPreviousProcess(t *testing.T) {
|
||||
store := NewSessionStoreMemory("test-node")
|
||||
router := NewRouter(nil, nil, "telegram")
|
||||
router.Identity = "archie"
|
||||
router.InitSessions(store)
|
||||
turnID := CanonicalTurnID("chat-recover", "source-recover")
|
||||
if _, claim, err := store.(TurnLedger).ClaimTurn(context.Background(), TurnRecord{
|
||||
TurnID: turnID, SessionID: "chat-recover", SourceID: "source-recover",
|
||||
OwnerID: "previous-process", Status: TurnStatusAccepted,
|
||||
}); err != nil || claim != TurnClaimOwned {
|
||||
t.Fatalf("seed ClaimTurn() = %q, %v", claim, err)
|
||||
}
|
||||
runner := NewTurnRunner(TurnRunnerConfig{
|
||||
Router: router,
|
||||
Sessions: store,
|
||||
Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"},
|
||||
Personas: NewPersonaRegistry(DefaultPersonas()),
|
||||
Model: &turnTestModel{prepared: &turnTestPreparedModel{reply: "recovered"}},
|
||||
OwnerID: "new-process",
|
||||
BotUser: "archie",
|
||||
Channel: "telegram",
|
||||
})
|
||||
|
||||
got, err := runner.Run(context.Background(), Message{
|
||||
From: "user", ChannelID: "chat-recover", SourceID: "source-recover", Text: "resume me",
|
||||
}, nil)
|
||||
if err != nil || got != "recovered" {
|
||||
t.Fatalf("Run() = %q, %v; want recovered response", got, err)
|
||||
}
|
||||
turn, ok, err := runner.Ledger.GetTurn(context.Background(), turnID)
|
||||
if err != nil || !ok || turn.Status != TurnStatusCompleted || turn.Attempt != 2 || turn.OwnerID != "new-process" {
|
||||
t.Fatalf("recovered turn = %#v, %v, %v; want attempt 2 completed by new process", turn, ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurnRunnerPreparationFailureLeavesInboundWithoutReply(t *testing.T) {
|
||||
store := NewSessionStoreMemory("test-node")
|
||||
router := NewRouter(nil, nil, "telegram")
|
||||
router.Identity = "archie"
|
||||
router.InitSessions(store)
|
||||
runner := NewTurnRunner(TurnRunnerConfig{
|
||||
Router: router,
|
||||
Sessions: store,
|
||||
Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"},
|
||||
Personas: NewPersonaRegistry(DefaultPersonas()),
|
||||
Model: &turnTestModel{prepareErr: errors.New("tool setup failed")},
|
||||
BotUser: "archie",
|
||||
Channel: "telegram",
|
||||
})
|
||||
|
||||
_, err := runner.Run(context.Background(), Message{
|
||||
From: "user", ChannelID: "chat-1", SourceID: "source-1", Text: "hello",
|
||||
}, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "tool setup failed") {
|
||||
t.Fatalf("Run() error = %v, want tool setup error", err)
|
||||
}
|
||||
history, err := store.RecentMessages(context.Background(), "chat-1", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("RecentMessages() error = %v", err)
|
||||
}
|
||||
if len(history) != 1 || history[0].Text != "hello" {
|
||||
t.Fatalf("history after failed preparation = %#v, want inbound only", history)
|
||||
}
|
||||
turn, ok, err := runner.Ledger.GetTurn(context.Background(), CanonicalTurnID("chat-1", "source-1"))
|
||||
if err != nil || !ok || turn.Status != TurnStatusFailed || turn.Error == "" {
|
||||
t.Fatalf("failed preparation turn = %#v, %v, %v; want persisted failure", turn, ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurnRunnerReplaysCompletedDuplicateOutsideRecentWindow(t *testing.T) {
|
||||
store := NewSessionStoreMemory("test-node")
|
||||
router := NewRouter(nil, nil, "telegram")
|
||||
router.Identity = "archie"
|
||||
router.InitSessions(store)
|
||||
prepared := &turnTestPreparedModel{reply: "answer"}
|
||||
model := &turnTestModel{prepared: prepared}
|
||||
runner := NewTurnRunner(TurnRunnerConfig{
|
||||
Router: router,
|
||||
Sessions: store,
|
||||
Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"},
|
||||
Personas: NewPersonaRegistry(DefaultPersonas()),
|
||||
Model: model,
|
||||
BotUser: "archie",
|
||||
Channel: "telegram",
|
||||
})
|
||||
msg := Message{From: "user", ChannelID: "chat-1", SourceID: "source-1", Text: "hello"}
|
||||
|
||||
if _, err := runner.Run(context.Background(), msg, nil); err != nil {
|
||||
t.Fatalf("first Run() error = %v", err)
|
||||
}
|
||||
for i := range 110 {
|
||||
if err := store.SaveMessage(context.Background(), "chat-1", Message{
|
||||
From: "user", SourceID: fmt.Sprintf("later-%d", i), Text: "later",
|
||||
}); err != nil {
|
||||
t.Fatalf("SaveMessage(%d) error = %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
reply, err := runner.Run(context.Background(), msg, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("duplicate Run() error = %v", err)
|
||||
}
|
||||
if reply != "answer" {
|
||||
t.Fatalf("duplicate reply = %q, want answer", reply)
|
||||
}
|
||||
if model.prepared.generateN != 1 {
|
||||
t.Fatalf("Generate calls = %d, want 1", model.prepared.generateN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurnRunnerReplaysCompletedDuplicateWithoutGenerating(t *testing.T) {
|
||||
store := NewSessionStoreMemory("test-node")
|
||||
router := NewRouter(nil, nil, "telegram")
|
||||
router.Identity = "archie"
|
||||
router.InitSessions(store)
|
||||
prepared := &turnTestPreparedModel{reply: "answer"}
|
||||
runner := NewTurnRunner(TurnRunnerConfig{
|
||||
Router: router,
|
||||
Sessions: store,
|
||||
Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"},
|
||||
Personas: NewPersonaRegistry(DefaultPersonas()),
|
||||
Model: &turnTestModel{prepared: prepared},
|
||||
BotUser: "archie",
|
||||
Channel: "telegram",
|
||||
})
|
||||
msg := Message{From: "user", ChannelID: "chat-1", SourceID: "source-1", Text: "hello"}
|
||||
|
||||
if _, err := runner.Run(context.Background(), msg, nil); err != nil {
|
||||
t.Fatalf("first Run() error = %v", err)
|
||||
}
|
||||
var replay string
|
||||
reply, err := runner.Run(context.Background(), msg, func(delta string) { replay += delta })
|
||||
if err != nil {
|
||||
t.Fatalf("duplicate Run() error = %v", err)
|
||||
}
|
||||
if reply != "answer" || replay != "answer" {
|
||||
t.Fatalf("duplicate reply = %q, replay = %q", reply, replay)
|
||||
}
|
||||
if prepared.generateN != 1 {
|
||||
t.Fatalf("Generate calls = %d, want 1", prepared.generateN)
|
||||
}
|
||||
history, err := store.RecentMessages(context.Background(), "chat-1", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("RecentMessages() error = %v", err)
|
||||
}
|
||||
if len(history) != 2 {
|
||||
t.Fatalf("history length after duplicate = %d, want 2", len(history))
|
||||
}
|
||||
}
|
||||
@@ -37,14 +37,17 @@ func TestArchiedWiresTypedProvidersAndExecutableConsumers(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("read archied main: %v", err)
|
||||
}
|
||||
text := string(source)
|
||||
var text strings.Builder
|
||||
text.WriteString(string(source))
|
||||
|
||||
// Also check telegram_setup.go, which holds the chat-turn wiring
|
||||
// extracted from main.go during the Phase 4 structural refactor.
|
||||
tgPath := filepath.Join("..", "..", "..", "cmd", "archied", "telegram_setup.go")
|
||||
tgSource, tgErr := os.ReadFile(tgPath)
|
||||
if tgErr == nil {
|
||||
text += string(tgSource)
|
||||
// Also check the chat composition files, which hold the chat-turn wiring
|
||||
// extracted from main.go during the structural refactor.
|
||||
for _, name := range []string{"telegram_setup.go", "chat_turn_model.go"} {
|
||||
path := filepath.Join("..", "..", "..", "cmd", "archied", name)
|
||||
source, readErr := os.ReadFile(path)
|
||||
if readErr == nil {
|
||||
text.WriteString(string(source))
|
||||
}
|
||||
}
|
||||
|
||||
for _, required := range []string{
|
||||
@@ -54,20 +57,20 @@ func TestArchiedWiresTypedProvidersAndExecutableConsumers(t *testing.T) {
|
||||
"capabilityHost.Register(providerRegistry)",
|
||||
// The chat turn builds its toolset from the registry before the
|
||||
// system prompt is rendered, so the prompt can advertise exactly
|
||||
// the tools the model is handed. After the Phase 4 structural
|
||||
// refactor these calls live in telegram_setup.go.
|
||||
// the tools the model is handed. After the structural refactor these
|
||||
// calls live in the chat composition files.
|
||||
"chatGenerateOptions(ctx,",
|
||||
"toolSummaries(options.Tools)",
|
||||
"agentexec.NewInProcessRunner(llm, log, toolReg)",
|
||||
} {
|
||||
if !strings.Contains(text, required) {
|
||||
if !strings.Contains(text.String(), required) {
|
||||
t.Errorf("archied wiring not found: %q", required)
|
||||
}
|
||||
}
|
||||
if strings.Contains(text, ".RegisterTools(ctx, toolReg)") {
|
||||
if strings.Contains(text.String(), ".RegisterTools(ctx, toolReg)") {
|
||||
t.Error("composition bypasses the typed provider registry with direct MCP registration")
|
||||
}
|
||||
if strings.Contains(text, "memManager.GetToolSchemas()") {
|
||||
if strings.Contains(text.String(), "memManager.GetToolSchemas()") {
|
||||
t.Error("composition bypasses the typed memory tool provider")
|
||||
}
|
||||
}
|
||||
+55
-19
@@ -19,11 +19,18 @@ func (s *Server) handleSSE(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
|
||||
since, _ := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
|
||||
querySince, _ := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
|
||||
since := querySince
|
||||
if headerValue := r.Header.Get("Last-Event-ID"); headerValue != "" {
|
||||
if headerSince, err := strconv.ParseInt(headerValue, 10, 64); err == nil {
|
||||
since = headerSince
|
||||
}
|
||||
}
|
||||
send := func(e events.Event) bool {
|
||||
b, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return true
|
||||
s.Log.Error("sse event marshal failed", "error", err, "event_id", e.ID)
|
||||
return false
|
||||
}
|
||||
if _, err := w.Write([]byte("id: " + strconv.FormatInt(e.ID, 10) + "\ndata: " + string(b) + "\n\n")); err != nil {
|
||||
return false
|
||||
@@ -32,21 +39,8 @@ func (s *Server) handleSSE(w http.ResponseWriter, r *http.Request) {
|
||||
return true
|
||||
}
|
||||
|
||||
if backlog, err := s.Store.EventsSince(r.Context(), since, 200); err != nil {
|
||||
s.Log.Error("sse backlog fetch failed", "error", err, "since", since)
|
||||
// A comment frame keeps the stream open; the client sees the
|
||||
// backlog gap rather than a silent truncation.
|
||||
_, _ = w.Write([]byte(":error " + err.Error() + "\n"))
|
||||
fl.Flush()
|
||||
} else {
|
||||
for _, e := range backlog {
|
||||
if !send(e) {
|
||||
return
|
||||
}
|
||||
since = e.ID
|
||||
}
|
||||
}
|
||||
|
||||
// Register before reading the backlog. An event published between the
|
||||
// backlog read and subscription would otherwise be lost permanently.
|
||||
c := make(chan events.Event, 64)
|
||||
s.mu.Lock()
|
||||
if s.conns == nil {
|
||||
@@ -60,17 +54,59 @@ func (s *Server) handleSSE(w http.ResponseWriter, r *http.Request) {
|
||||
s.mu.Unlock()
|
||||
}()
|
||||
|
||||
catchUp := func(targetID int64) bool {
|
||||
for {
|
||||
before := since
|
||||
backlog, err := s.Store.EventsSince(r.Context(), since, 200)
|
||||
if err != nil {
|
||||
s.Log.Error("sse backlog fetch failed", "error", err, "since", since)
|
||||
// Comments are invisible to EventSource clients. Close after
|
||||
// surfacing the gap so EventSource invokes its reconnect path.
|
||||
_, _ = w.Write([]byte(":error " + err.Error() + "\n\n"))
|
||||
fl.Flush()
|
||||
return false
|
||||
}
|
||||
for _, e := range backlog {
|
||||
if e.ID <= since {
|
||||
continue
|
||||
}
|
||||
if !send(e) {
|
||||
return false
|
||||
}
|
||||
since = e.ID
|
||||
if targetID > 0 && since >= targetID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if len(backlog) < 200 || since == before || (targetID > 0 && since >= targetID) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !catchUp(0) {
|
||||
return
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-r.Context().Done():
|
||||
return
|
||||
case e := <-c:
|
||||
if e.ID <= since {
|
||||
continue // already replayed from the backlog
|
||||
continue
|
||||
}
|
||||
if !send(e) {
|
||||
if !catchUp(e.ID) {
|
||||
return
|
||||
}
|
||||
// Broadcast may carry an event that has not yet reached the
|
||||
// durable store. Deliver that live event after filling any
|
||||
// persisted gap, while keeping the ID deduplication invariant.
|
||||
if e.ID > since {
|
||||
if !send(e) {
|
||||
return
|
||||
}
|
||||
since = e.ID
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,9 +50,17 @@ type stubStore struct {
|
||||
tokensByDayErr error
|
||||
eventsSinceErr error
|
||||
taskEventsErr error
|
||||
taskByIDErr error
|
||||
requeueErr error
|
||||
}
|
||||
|
||||
func (s *stubStore) TaskByID(ctx context.Context, id int64) (*store.Task, error) {
|
||||
if s.taskByIDErr != nil {
|
||||
return nil, s.taskByIDErr
|
||||
}
|
||||
return s.TaskStore.TaskByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *stubStore) Requeue(ctx context.Context, id int64, from, workflow string) error {
|
||||
if s.requeueErr != nil {
|
||||
return s.requeueErr
|
||||
@@ -317,9 +325,54 @@ func TestHandleSSEEventsSinceError(t *testing.T) {
|
||||
t.Fatal("EventsSince error was silently swallowed — no error sent to SSE client")
|
||||
}
|
||||
|
||||
func TestHandleSSEUsesLastEventIDForCatchUp(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
first, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "first"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "second"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ts := httptest.NewServer(srv.Handler())
|
||||
t.Cleanup(ts.Close)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/events", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Last-Event-ID", strconv.FormatInt(first, 10))
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = resp.Body.Close() })
|
||||
|
||||
reader := bufio.NewReader(resp.Body)
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
line, readErr := reader.ReadString('\n')
|
||||
if readErr != nil {
|
||||
t.Fatalf("second event not received after Last-Event-ID: %v", readErr)
|
||||
}
|
||||
if strings.HasPrefix(line, "data:") {
|
||||
if strings.Contains(line, "first") {
|
||||
t.Fatalf("replayed event before Last-Event-ID: %q", line)
|
||||
}
|
||||
if strings.Contains(line, "second") {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
t.Fatal("second event not received after Last-Event-ID")
|
||||
}
|
||||
|
||||
func TestHandleSSEBacklogAndLive(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
ctx := t.Context()
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
id, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "backlog"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
export function newChatTurn(text) {
|
||||
return { text, sourceID: crypto.randomUUID() };
|
||||
}
|
||||
|
||||
export function retryChatTurn(turn) {
|
||||
return { ...turn };
|
||||
}
|
||||
+43
-17
@@ -4,6 +4,7 @@ import { createChatView } from "./chat-view.js";
|
||||
import { chatBubble } from "./chat-render.js";
|
||||
import { updateStreamingReply } from "./chat-stream.js";
|
||||
import { channelID } from "./chat-state.js";
|
||||
import { newChatTurn, retryChatTurn } from "./chat-retry.js";
|
||||
import { renderCommands, renderModels, renderSelectors, renderSessions } from "./chat-catalog.js";
|
||||
import { renderDangerous, renderUpdate } from "./chat-panels.js";
|
||||
|
||||
@@ -72,7 +73,7 @@ export function chatPage() {
|
||||
{
|
||||
type: "button",
|
||||
role: "option",
|
||||
"aria-selected": index === commandSelection,
|
||||
"aria-selected": String(index === commandSelection),
|
||||
onmousedown: (event) => event.preventDefault(),
|
||||
onclick: () => chooseCommand(index),
|
||||
},
|
||||
@@ -187,29 +188,38 @@ export function chatPage() {
|
||||
if (!transcript.children.length) transcript.append(emptyState);
|
||||
}
|
||||
|
||||
async function sendMessage() {
|
||||
const text = composer.value.trim();
|
||||
async function sendMessage(retry = null) {
|
||||
const text = retry?.turn.text || composer.value.trim();
|
||||
if (!text || sending) return;
|
||||
hideCommandMenu();
|
||||
const turn = retry ? retryChatTurn(retry.turn) : newChatTurn(text);
|
||||
sending = true;
|
||||
send.disabled = true;
|
||||
stop.disabled = false;
|
||||
composer.disabled = true;
|
||||
status.textContent = "Thinking…";
|
||||
appendMessage({ from: "web", text });
|
||||
composer.value = "";
|
||||
const replyBubble = el("div.chat-bubble-row.assistant", el("div.chat-bubble", el("div.chat-bubble-meta", "Archie"), el("div.chat-bubble-text")));
|
||||
transcript.append(replyBubble);
|
||||
status.textContent = retry ? "Retrying…" : "Thinking…";
|
||||
if (!retry) {
|
||||
appendMessage({ from: "web", text });
|
||||
composer.value = "";
|
||||
}
|
||||
const replyBubble = retry?.replyBubble || el("div.chat-bubble-row.assistant", el("div.chat-bubble", el("div.chat-bubble-meta", "Archie"), el("div.chat-bubble-text")));
|
||||
if (!retry) transcript.append(replyBubble);
|
||||
replyBubble.querySelector(".chat-retry")?.remove();
|
||||
let replyText = replyBubble.querySelector(".chat-bubble-text");
|
||||
let streamedText = "";
|
||||
let finished = false;
|
||||
let timedOut = false;
|
||||
let timeout;
|
||||
try {
|
||||
activeController = new AbortController();
|
||||
timeout = setTimeout(() => activeController.abort(), 120000);
|
||||
timeout = setTimeout(() => {
|
||||
timedOut = true;
|
||||
activeController.abort();
|
||||
}, 120000);
|
||||
const response = await fetch("/api/chat/stream", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
|
||||
body: JSON.stringify({ channel_id: channelID(), source_id: crypto.randomUUID(), text }),
|
||||
body: JSON.stringify({ channel_id: channelID(), source_id: turn.sourceID, text: turn.text }),
|
||||
signal: activeController.signal,
|
||||
});
|
||||
if (!response.ok) throw new Error((await response.text()) || `${response.status} ${response.statusText}`);
|
||||
@@ -230,25 +240,34 @@ export function chatPage() {
|
||||
streamedText += event.text;
|
||||
replyText = updateStreamingReply(replyText, streamedText);
|
||||
}
|
||||
if (event.type === "done" && !streamedText) {
|
||||
streamedText = event.text;
|
||||
replyText = updateStreamingReply(replyText, streamedText);
|
||||
if (event.type === "done") {
|
||||
finished = true;
|
||||
if (!streamedText) {
|
||||
streamedText = event.text;
|
||||
replyText = updateStreamingReply(replyText, streamedText);
|
||||
}
|
||||
}
|
||||
if (event.type === "error") throw new Error(event.text);
|
||||
}
|
||||
if (done) break;
|
||||
}
|
||||
if (!finished) throw new Error("chat stream ended before completion");
|
||||
if (replyText.isConnected) replyText = updateStreamingReply(replyText, streamedText);
|
||||
status.textContent = "Ready";
|
||||
await refreshSessions();
|
||||
if (currentSession) await selectSession(currentSession);
|
||||
} catch (err) {
|
||||
if (err.name === "AbortError") {
|
||||
if (err.name === "AbortError" && !timedOut) {
|
||||
replyText = updateStreamingReply(replyText, "Turn stopped.");
|
||||
status.textContent = "Stopped";
|
||||
} else {
|
||||
replyText = updateStreamingReply(replyText, `Unable to complete that turn: ${err.message || err}`);
|
||||
status.textContent = "Error";
|
||||
const failedTurn = retryChatTurn(turn);
|
||||
const retryButton = el("button.btn.chat-retry", {
|
||||
type: "button",
|
||||
onclick: () => sendMessage({ turn: failedTurn, replyBubble }),
|
||||
}, "Retry");
|
||||
replyBubble.append(retryButton);
|
||||
status.textContent = "Error — retry available";
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
@@ -271,6 +290,13 @@ export function chatPage() {
|
||||
composer.oninput = renderCommandMenu;
|
||||
composer.onkeydown = (event) => {
|
||||
if (!commandMenu.hidden && commandMatches.length) {
|
||||
if (event.key === "Enter" && !event.shiftKey && !event.ctrlKey && !event.metaKey
|
||||
&& commandMatches.some((spec) => spec.command === composer.value.trim())) {
|
||||
event.preventDefault();
|
||||
hideCommandMenu();
|
||||
sendMessage();
|
||||
return;
|
||||
}
|
||||
if (event.key === "ArrowDown") {
|
||||
event.preventDefault();
|
||||
commandSelection = (commandSelection + 1) % commandMatches.length;
|
||||
@@ -283,7 +309,7 @@ export function chatPage() {
|
||||
renderCommandMenu();
|
||||
return;
|
||||
}
|
||||
if (event.key === "Enter" && !event.ctrlKey && !event.metaKey) {
|
||||
if (event.key === "Enter" && !event.shiftKey && !event.ctrlKey && !event.metaKey) {
|
||||
event.preventDefault();
|
||||
chooseCommand(commandSelection);
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { newChatTurn, retryChatTurn } from "../src/chat/chat-retry.js";
|
||||
|
||||
test("retry preserves the logical turn identity and text", () => {
|
||||
const turn = newChatTurn("keep this message");
|
||||
const retry = retryChatTurn(turn);
|
||||
|
||||
assert.notEqual(turn.sourceID, undefined);
|
||||
assert.deepEqual(retry, turn);
|
||||
});
|
||||
|
||||
test("new turns receive distinct identities", () => {
|
||||
const first = newChatTurn("same text");
|
||||
const second = newChatTurn("same text");
|
||||
|
||||
assert.notEqual(first.sourceID, second.sourceID);
|
||||
});
|
||||
Reference in new issue
Block a user