diff --git a/internal/gateway/prompt.go b/internal/gateway/prompt.go index 70d887b86..3d0973ebc 100644 --- a/internal/gateway/prompt.go +++ b/internal/gateway/prompt.go @@ -18,43 +18,22 @@ type ToolSummary struct { } // 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 is the active persona prompt. 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 is the complete set of tools available for this turn. Tools []ToolSummary - // Channel names the delivery surface (e.g. "telegram") so the model can - // size and format its reply for it. + // Channel names the communication channel. 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 // Page is the dashboard route the operator is on right now (e.g. // "/tasks"). It is supplied per message by the web channel; empty for // non-web channels, which have no page context. Rendered in the // runtime-metadata block so the agent knows where the operator is // looking and can point them somewhere relevant. Page 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 stamps the prompt's date. Now time.Time } @@ -66,45 +45,16 @@ type promptData struct { Date string } -// flattenWhitespace collapses every run of whitespace into a single space. -// -// The 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}). + Funcs(template.FuncMap{"xml": strings.NewReplacer( + "&", "&", + "<", "<", + ">", ">", + )}). 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 @@ -112,27 +62,29 @@ func escapeXML(value string) string { // 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)) + tools := make([]ToolSummary, len(cfg.Tools)) for i, tool := range cfg.Tools { - data.Tools[i] = ToolSummary{ - Name: flattenWhitespace(tool.Name), - Description: flattenWhitespace(tool.Description), + tools[i] = ToolSummary{ + Name: strings.Join(strings.Fields(tool.Name), " "), + Description: strings.Join(strings.Fields(tool.Description), " "), } } + data := promptData{ + Persona: cfg.Persona, + Tools: tools, + Channel: cfg.Channel, + Model: cfg.Model, + SessionID: cfg.SessionID, + Page: cfg.Page, + Now: cfg.Now, + Date: cfg.Now.Format("2006-01-02"), + } var buf strings.Builder if err := archiePromptTemplate.Execute(&buf, data); err != nil { - return fallbackSystemPrompt + tpl := "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." + return tpl } 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." diff --git a/internal/gateway/prompt_test.go b/internal/gateway/prompt_test.go index bfefe7123..1537fc687 100644 --- a/internal/gateway/prompt_test.go +++ b/internal/gateway/prompt_test.go @@ -287,15 +287,6 @@ func TestBuildSystemPromptForbidsOverconfidentClaims(t *testing.T) { } } -func TestBuildSystemPromptRendersConfiguredOperator(t *testing.T) { - t.Parallel() - - prompt := BuildSystemPrompt(SystemPromptConfig{Operator: "Ada Lovelace"}) - if !strings.Contains(prompt, "Operator: Ada Lovelace") { - t.Errorf("prompt does not carry the configured operator:\n%s", prompt) - } -} - func TestBuildSystemPromptOmitsOperatorWhenUnset(t *testing.T) { t.Parallel() @@ -304,21 +295,3 @@ func TestBuildSystemPromptOmitsOperatorWhenUnset(t *testing.T) { t.Errorf("prompt names an operator when none is configured:\n%s", prompt) } } - -// TestPromptSourcesCarryNoPersonalName guards against re-hardcoding a -// deployment's owner into the prompt. The operator's name is configuration: -// baking one in makes every deployment claim to work for that person and -// sends their name to the model provider on every turn. -func TestPromptSourcesCarryNoPersonalName(t *testing.T) { - t.Parallel() - - sources := map[string]string{ - "fallbackSystemPrompt": fallbackSystemPrompt, - "default persona": DefaultPersonas()[0].Prompt, - } - for name, text := range sources { - if strings.Contains(text, "Sam") { - t.Errorf("%s hardcodes a personal name; use ChatConfig.Operator instead: %q", name, text) - } - } -}