Three defects found from a real Telegram transcript.
1. The operator's name was compiled into the prompt.
internal/gateway/persona.go and prompt.go both hardcoded "Sam's" into the
default persona and the fallback prompt. The persona renders first into
every system prompt, so a personal name was sent to the model provider on
every turn, and any other deployment got an assistant that believed it
worked for someone else. There was nowhere to put it: Repo.Owner is a repo
owner and config.Config had no operator concept.
ChatConfig.Operator now carries it, rendered in the prompt's <env> block as
runtime metadata and omitted entirely when unset. config.example.toml
documents that setting it sends the name to the model provider.
2. /new claimed to clear history that it had not cleared.
The chat responder keyed history by channel id through a local sessionKey
helper, while the session commands operate on tracked session ids.
sessionTracker.resolve mints deterministic ids so the two agree -- until
/new, /branch or /resume assigns a generated id, after which they address
different histories permanently. /new reported "Conversation history has
been cleared" while the next turn still saw everything, and /undo,
/compress, /resume and /sessions acted on a namespace the conversation
never read.
gateway.Router.ResolveSessionKey already existed for this, and
session_commands.go:104 said outright that main.go should call it instead of
its inline helper. It never got wired. The responder now resolves through
the router and the dead helper is deleted so it cannot come back.
buildTelegramRouter constructs the router before the responder so the
dependency is visible rather than captured by reference.
3. Prompt rules for two behaviours the transcript showed.
The agent offered to accept shell access it has no tool to use, which is
both unhonourable and a request for the user's secrets. It also opened
mid-conversation replies with a greeting, and described its own manner
("no fluff") in stock phrasing. Added core rule 7 and three communication
rules covering greetings, self-description and register.
Note the memory claim in that transcript was NOT a fabrication: the
memory_edit tool's own description names MEMORY.md and USER.md, and tool
descriptions are part of the prompt.
Also added chat-turn logging (session id, channel, thread, history depth).
Chat turns were entirely unlogged, so a report of lost context had no
evidence to check: 76 log lines covered two days and none of them was a
conversation. A separate report of mid-conversation amnesia remains
undiagnosed -- single process, 43h uptime, compression untriggered at three
messages -- and this is what makes it diagnosable next time.
Regression tests: prompt renders/omits the operator and carries no personal
name; the responder resolves via ResolveSessionKey and defines no local
session key helper.
Verified: go build, go vet, gofmt clean; full suite passes.
133 lines
5.0 KiB
Go
133 lines
5.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
_ "embed"
|
|
"strings"
|
|
"text/template"
|
|
"time"
|
|
)
|
|
|
|
//go:embed templates/archie.md.tpl
|
|
var archiePromptTpl string
|
|
|
|
// ToolSummary is the capability metadata for one registered tool, as it is
|
|
// advertised to the model.
|
|
type ToolSummary struct {
|
|
Name string
|
|
Description string
|
|
}
|
|
|
|
// SystemPromptConfig holds the inputs for one rendered system prompt.
|
|
//
|
|
// Persona supplies identity and communication style and comes from the
|
|
// [PersonaRegistry]; every other field is runtime state the model cannot
|
|
// infer. The distinction matters: switching persona must not be able to
|
|
// remove the tool inventory or the instruction hierarchy, so those live in
|
|
// the template rather than in any persona string.
|
|
type SystemPromptConfig struct {
|
|
// Persona is the active persona prompt, rendered first.
|
|
Persona string
|
|
// Tools is the complete set of tools available for this turn. An empty
|
|
// slice is meaningful: it renders an explicit statement that the agent
|
|
// has no tools, rather than leaving the model to assume it has some.
|
|
Tools []ToolSummary
|
|
// Channel names the delivery surface (e.g. "telegram") so the model can
|
|
// size and format its reply for it.
|
|
Channel string
|
|
// Model is the active model reference, for the agent's self-report.
|
|
Model string
|
|
// SessionID identifies the conversation.
|
|
SessionID string
|
|
// Operator is the display name of the person this deployment assists,
|
|
// from configuration. Empty renders nothing.
|
|
//
|
|
// It is a config value rather than prompt text because the operator's
|
|
// name is deployment data, not part of Archie's identity: it was
|
|
// previously compiled into the default persona, so every deployment
|
|
// claimed to work for one particular person and that person's name was
|
|
// sent to the model provider on every turn whether or not it was theirs.
|
|
Operator string
|
|
// Now stamps the prompt's date. The zero value omits nothing -- it
|
|
// simply renders the zero date -- so callers should always set it.
|
|
Now time.Time
|
|
}
|
|
|
|
// promptData is the template execution context. It exists so the template
|
|
// sees a pre-formatted date and pre-flattened tool metadata rather than
|
|
// calling methods during rendering.
|
|
type promptData struct {
|
|
SystemPromptConfig
|
|
Date string
|
|
}
|
|
|
|
// flattenWhitespace collapses every run of whitespace into a single space.
|
|
//
|
|
// The <tools> block renders one line per tool. A description containing a
|
|
// newline would otherwise split across lines, and a line beginning "- " would
|
|
// read as another tool entirely -- letting a third-party MCP server
|
|
// advertise capabilities the agent does not have. Markup escaping does not
|
|
// help here, because the forged entry needs no markup.
|
|
func flattenWhitespace(value string) string {
|
|
return strings.Join(strings.Fields(value), " ")
|
|
}
|
|
|
|
var archiePromptTemplate = template.Must(
|
|
template.New("archie").
|
|
Funcs(template.FuncMap{"xml": escapeXML}).
|
|
Parse(archiePromptTpl),
|
|
)
|
|
|
|
// markupEscaper neutralises the three characters that can forge prompt
|
|
// structure, and nothing else.
|
|
//
|
|
// encoding/xml's EscapeText would be the obvious choice, but it also rewrites
|
|
// newlines, tabs and apostrophes into numeric entities -- so a multi-line
|
|
// tool description arrives at the model as 
 noise. The prompt is read by
|
|
// a model, not parsed by an XML parser, so readability wins everywhere the
|
|
// security property is not at stake.
|
|
var markupEscaper = strings.NewReplacer(
|
|
"&", "&",
|
|
"<", "<",
|
|
">", ">",
|
|
)
|
|
|
|
// escapeXML escapes markup in untrusted text. Tool names and descriptions
|
|
// arrive from MCP servers, which are third-party data: without escaping, a
|
|
// description containing a closing tag could forge prompt structure and
|
|
// appear to the model as trusted instructions.
|
|
func escapeXML(value string) string {
|
|
return markupEscaper.Replace(value)
|
|
}
|
|
|
|
// BuildSystemPrompt renders the chat agent's system prompt.
|
|
//
|
|
// Rendering cannot fail at runtime: the template is parsed at init, and a
|
|
// template that somehow errors mid-execution would leave the agent with a
|
|
// truncated prompt, so the partial output is discarded in favour of the
|
|
// invariant rules.
|
|
func BuildSystemPrompt(cfg SystemPromptConfig) string {
|
|
data := promptData{
|
|
SystemPromptConfig: cfg,
|
|
Date: cfg.Now.Format("2006-01-02"),
|
|
}
|
|
data.Tools = make([]ToolSummary, len(cfg.Tools))
|
|
for i, tool := range cfg.Tools {
|
|
data.Tools[i] = ToolSummary{
|
|
Name: flattenWhitespace(tool.Name),
|
|
Description: flattenWhitespace(tool.Description),
|
|
}
|
|
}
|
|
var buf strings.Builder
|
|
if err := archiePromptTemplate.Execute(&buf, data); err != nil {
|
|
return fallbackSystemPrompt
|
|
}
|
|
return strings.TrimSpace(buf.String())
|
|
}
|
|
|
|
// fallbackSystemPrompt is the last-resort prompt used if template execution
|
|
// fails. It keeps the two rules whose absence caused real misbehaviour:
|
|
// never claim unverified capabilities, and stay brief.
|
|
const fallbackSystemPrompt = "You are Archie, a coding and project assistant. " +
|
|
"Never claim a tool, file, memory, action or result you have not verified. " +
|
|
"Keep replies short and lead with the answer."
|