Files
archie/internal/app/archiegateway/soul.go
T
sam 23307d9462 feat(gateway): load SOUL into the chat prompt per turn
SOUL now owns the prompt identity slot via a per-turn source: a stored soul
resource wins once saved, otherwise the identity's SOUL file, then the
shipped default. The gateway watches the resource so a dashboard edit
reaches the next turn without a restart; a file read error degrades to the
default and logs once.

The persona prompt plumbing is replaced by the SOUL source; the persona
surface itself (registry, wire, page, commands) stays for archie-core-7ftt.
2026-10-05 17:14:20 +11:00

81 lines
2.0 KiB
Go

package archiegateway
import (
"log/slog"
"strings"
"sync"
"github.com/samcharles93/archie-core/internal/domain/agent"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
)
// soulSource resolves the SOUL text a chat turn renders. A stored SOUL owns
// identity once saved; until then the identity's file supplies it, and the
// shipped default is the fallback. It never fails a turn: a read error is
// logged once per failure episode and degrades to the default.
type soulSource struct {
path string
log *slog.Logger
mu sync.RWMutex
owned string
failing bool
}
func newSoulSource(configPath string, log *slog.Logger) *soulSource {
if configPath == "" {
configPath = configuration.DefaultConfigPath()
}
return &soulSource{path: configPath, log: log}
}
// apply stores the text the control plane's soul resource last carried. Empty
// text leaves the SOUL unowned, so the file and default still apply.
func (s *soulSource) apply(owned string) {
s.mu.Lock()
s.owned = owned
s.mu.Unlock()
}
// Soul returns the effective SOUL text for the next turn.
func (s *soulSource) Soul() string {
s.mu.RLock()
owned := s.owned
s.mu.RUnlock()
if strings.TrimSpace(owned) != "" {
return owned
}
text, err := configuration.ReadSoul(s.path)
switch {
case err != nil:
s.fail("soul file unreadable; using the shipped default", err)
case strings.TrimSpace(text) == "":
// No file content: fall through to the shipped default.
default:
if err := (agent.Soul{Text: text}).Validate(); err != nil {
s.fail("soul file invalid; using the shipped default", err)
break
}
s.recover()
return text
}
return agent.ShippedSoul()
}
// fail logs the first failure of an episode; repeats stay quiet until recover.
func (s *soulSource) fail(msg string, err error) {
s.mu.Lock()
first := !s.failing
s.failing = true
s.mu.Unlock()
if first && s.log != nil {
s.log.Warn(msg, "err", err)
}
}
func (s *soulSource) recover() {
s.mu.Lock()
s.failing = false
s.mu.Unlock()
}