Files
archie/internal/domain/agent/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

62 lines
2.1 KiB
Go

package agent
import (
"fmt"
"strings"
)
// MaxSoulBytes bounds a SOUL document. Its body renders into the prompt ahead
// of the rules, so an oversized one cannot crowd them out.
const MaxSoulBytes = 8 << 10
// Soul is the control-plane document holding an identity's SOUL text. Empty
// Text means no stored SOUL: the loader falls back to the identity's file and
// then the shipped default, and the first save takes ownership.
type Soul struct {
Text string `json:"text"`
}
// Validate bounds the SOUL document's size. Empty is valid: it means unowned.
func (s Soul) Validate() error {
if len(s.Text) > MaxSoulBytes {
return fmt.Errorf("soul: %d bytes exceeds the %d-byte limit", len(s.Text), MaxSoulBytes)
}
return nil
}
// DefaultSoul is the starter SOUL document: the agent's identity prose in the
// system prompt.
const DefaultSoul = `# Archie
You are Archie, a capable coding and project assistant. Be direct without being
brusque, grounded in the actual workspace state, and use available tools when
asked. Do not claim tools, files, memories, actions, or results you have not
verified. Do not impersonate another assistant, provider, or vendor.
`
// ShippedSoul returns the SOUL document this build ships.
func ShippedSoul() string {
return DefaultSoul
}
// SoulMatchesShipped reports whether content equals shipped after line-ending
// and trailing-whitespace normalisation.
func SoulMatchesShipped(content, shipped string) bool {
return normalizeSoul(content) == normalizeSoul(shipped)
}
// normalizeSoul reduces a SOUL body to the content a user authored: a UTF-8
// BOM is dropped, CRLF and lone CR become LF, and trailing whitespace is
// removed from every line and from the end of the document. None of that
// carries intent, so it must not make an untouched template look edited.
func normalizeSoul(content string) string {
content = strings.TrimPrefix(content, "\ufeff")
content = strings.ReplaceAll(content, "\r\n", "\n")
content = strings.ReplaceAll(content, "\r", "\n")
lines := strings.Split(content, "\n")
for index, line := range lines {
lines[index] = strings.TrimRight(line, " \t")
}
return strings.TrimRight(strings.Join(lines, "\n"), "\n")
}