feat(chat): real system prompt for Archie — identity, tools, env (sc5)
The chat agent's entire system prompt was the four-sentence static string in gateway.DefaultPersonas(). It carried no tool inventory, no environment block and no instruction precedence, so the model invented its own capabilities: asked to set something up it announced it "only has the memory editing tool", then offered to walk through installing software it has no tool to install. Renders a template modelled on tau's agent.md.tpl + tau.agent.md: instruction precedence, core rules, communication rules, a <tools> block and an <env> block. The persona stays the style layer on top; the rules and the tool inventory live in the template so switching persona cannot remove them. The tool list is derived from the same core.ToolSet handed to the model, so the prompt cannot drift from what the agent can actually call, and an empty toolset renders an explicit "no tools are available" rather than silence the model fills in for itself. Tool metadata is third-party data from MCP servers, so it is markup-escaped (<, >, & only — not xml.EscapeText, which mangles prose into 
/') and whitespace-flattened, so a description cannot forge a closing tag or a second "- tool: ..." list entry.
This commit is contained in:
1 parent
a7388a0a59
commit
c79c623e9c
9 files changed
+544
-12
No files matched your search
+24
-11
@@ -397,14 +397,30 @@ func run() int {
|
||||
compCfg := gateway.DefaultCompressionConfig()
|
||||
view := gateway.CompressHistory(compressed, compCfg)
|
||||
|
||||
// Prepend persona prompt if one is active for this session.
|
||||
if personaPrompt := personas.GetActive(sessionKey); personaPrompt != "" {
|
||||
view.Messages = append(
|
||||
[]gateway.CompressedMessage{{Role: "system", Content: personaPrompt}},
|
||||
view.Messages...,
|
||||
)
|
||||
// Build the toolset first so the system prompt can advertise
|
||||
// exactly the tools the model is about to be handed. Deriving
|
||||
// the inventory from any other source lets the prompt drift
|
||||
// from reality, which is what made the agent offer to run
|
||||
// commands it had no tool for.
|
||||
options, err := chatGenerateOptions(nil, toolReg)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("build chat tools: %w", err)
|
||||
}
|
||||
|
||||
chatModel := chatModels.ActiveModel()
|
||||
systemPrompt := gateway.BuildSystemPrompt(gateway.SystemPromptConfig{
|
||||
Persona: personas.GetActive(sessionKey),
|
||||
Tools: toolSummaries(options.Tools),
|
||||
Channel: tg.Name(),
|
||||
Model: chatModel,
|
||||
SessionID: sessionKey,
|
||||
Now: time.Now(),
|
||||
})
|
||||
view.Messages = append(
|
||||
[]gateway.CompressedMessage{{Role: "system", Content: systemPrompt}},
|
||||
view.Messages...,
|
||||
)
|
||||
|
||||
// Convert to chat.Message for the LLM.
|
||||
messages := make([]chat.Message, len(view.Messages))
|
||||
for i, cm := range view.Messages {
|
||||
@@ -421,11 +437,8 @@ func run() int {
|
||||
}
|
||||
}
|
||||
|
||||
options, err := chatGenerateOptions(messages, toolReg)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("build chat tools: %w", err)
|
||||
}
|
||||
chatModel := chatModels.ActiveModel()
|
||||
options.Messages = messages
|
||||
|
||||
var text string
|
||||
if onDelta == nil {
|
||||
result, err := llm.Chat(ctx, chatModel, options)
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
aicore "github.com/samcharles93/ai-sdk/core"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
)
|
||||
|
||||
// toolSummaries converts the toolset handed to the model into the capability
|
||||
// metadata advertised in its system prompt, so the two can never disagree.
|
||||
//
|
||||
// core.ToolSet is a map, so the result is sorted by name: an unstable tool
|
||||
// order would change the system prompt on every turn and defeat provider-side
|
||||
// prompt caching.
|
||||
func toolSummaries(set aicore.ToolSet) []gateway.ToolSummary {
|
||||
summaries := make([]gateway.ToolSummary, 0, len(set))
|
||||
for name, tool := range set {
|
||||
if tool == nil {
|
||||
continue
|
||||
}
|
||||
summaries = append(summaries, gateway.ToolSummary{
|
||||
Name: name,
|
||||
Description: tool.Description,
|
||||
})
|
||||
}
|
||||
if len(summaries) == 0 {
|
||||
return nil
|
||||
}
|
||||
slices.SortFunc(summaries, func(a, b gateway.ToolSummary) int {
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
})
|
||||
return summaries
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
aicore "github.com/samcharles93/ai-sdk/core"
|
||||
)
|
||||
|
||||
func TestToolSummaries(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
set aicore.ToolSet
|
||||
want []string // expected "name: description" pairs, in order
|
||||
}{
|
||||
{
|
||||
name: "nil set yields no summaries",
|
||||
set: nil,
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "empty set yields no summaries",
|
||||
set: aicore.ToolSet{},
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "summaries are sorted by name for a stable prompt",
|
||||
set: aicore.ToolSet{
|
||||
"skill_activate": aicore.NewTool("skill_activate", "Load a skill", nil, nil),
|
||||
"memory_edit": aicore.NewTool("memory_edit", "Edit memory", nil, nil),
|
||||
},
|
||||
// ToolSet is a map: without sorting the prompt text would
|
||||
// differ between turns and defeat provider prompt caching.
|
||||
want: []string{"memory_edit: Edit memory", "skill_activate: Load a skill"},
|
||||
},
|
||||
{
|
||||
name: "nil entries are skipped rather than panicking",
|
||||
set: aicore.ToolSet{"broken": nil},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := toolSummaries(tc.set)
|
||||
if len(got) != len(tc.want) {
|
||||
t.Fatalf("toolSummaries() returned %d summaries, want %d: %+v", len(got), len(tc.want), got)
|
||||
}
|
||||
for i, want := range tc.want {
|
||||
joined := got[i].Name + ": " + got[i].Description
|
||||
if joined != want {
|
||||
t.Errorf("summary %d = %q, want %q", i, joined, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user