diff --git a/agent/assembly.go b/agent/assembly.go index 4a075e7..c57f558 100644 --- a/agent/assembly.go +++ b/agent/assembly.go @@ -18,6 +18,7 @@ import ( // from history on a resumed session (design doc C2). type DeferredInfo struct { FindingGuidance string // derived from the final permitted tools, including DB overrides + SkillIndex string // names-only skill index for the system prompt ("" when no skills) Deferred []string // all MCP tool names (schema withheld) GlobalNames []string // MCP names to list in the system-prompt block Unlock *actool.UnlockSet // shared call-gate; nil when no MCP tools diff --git a/agent/deferred.go b/agent/deferred.go index 3789680..d846b6e 100644 --- a/agent/deferred.go +++ b/agent/deferred.go @@ -2,6 +2,9 @@ package agent import ( "encoding/json" + "fmt" + "sort" + "strings" "github.com/Autumn-27/norma/llm" actool "github.com/Autumn-27/norma/tool" @@ -16,7 +19,7 @@ import ( // skill loads. Returns a single plain segment + boundary 0 when there is no global // block to add. func deferredSystem(sysText string, def DeferredInfo) (system []string, boundary int) { - sysText += def.FindingGuidance + sysText += def.FindingGuidance + def.SkillIndex block := actool.RenderDeferredToolsBlock(def.GlobalNames) if block == "" { return []string{sysText}, 0 @@ -26,6 +29,49 @@ func deferredSystem(sysText string, def DeferredInfo) (system []string, boundary return system, boundary } +// skillIndexMax bounds the names-only skill index. Skills are meant to be few and +// short-named; the bound only exists so an agent with a hundred skills cannot push +// its own instructions out of the prompt. +const skillIndexMax = 1200 + +// SkillIndexBlock renders the agent's skills as a system-prompt block. +// +// The Skill tool's own description already carries the listing, but a description is +// only read once the model decides to open the tool: on a live run with 36 skills +// assigned, the agent called Skill zero times across 485 trace records while the +// listing sat unread in one tool description — a system prompt is what every turn +// looks at. Names-only keeps it to a few hundred bytes instead of duplicating the +// descriptions. Returns "" for an agent without skills, so its prompt is unchanged. +func SkillIndexBlock(names []string) string { + if len(names) == 0 { + return "" + } + sorted := append([]string(nil), names...) + sort.Strings(sorted) + var b strings.Builder + shown := 0 + for _, n := range sorted { + sep := 0 + if shown > 0 { + sep = 2 // ", " + } + if b.Len()+sep+len(n) > skillIndexMax { + break + } + if shown > 0 { + b.WriteString(", ") + } + b.WriteString(n) + shown++ + } + tail := "" + if hidden := len(sorted) - shown; hidden > 0 { + tail = fmt.Sprintf(" (+%d more, all listed in the Skill tool)", hidden) + } + return "\n\n**Skills available to you** — before hunting a class of weakness, invoke the matching skill with the Skill tool (`Skill {\"name\": \"\"}`) and follow its phases; where the skill and your own plan disagree, the skill wins.\n" + + b.String() + tail +} + // seedUnlockFromHistory replays prior Skill() invocations in the conversation so // their skill-gated MCPs are re-unlocked on a resumed session (design doc C2). The // main agent builds a fresh session each turn; its in-memory unlock set would diff --git a/agent/skill_index_test.go b/agent/skill_index_test.go new file mode 100644 index 0000000..f6b6bf2 --- /dev/null +++ b/agent/skill_index_test.go @@ -0,0 +1,89 @@ +package agent + +import ( + "fmt" + "strings" + "testing" +) + +func TestSkillIndexBlock_ListsNamesAndRule(t *testing.T) { + block := SkillIndexBlock([]string{"web-authz-testing", "api-recon", "pentest-injection-classes"}) + for _, want := range []string{"api-recon", "pentest-injection-classes", "web-authz-testing"} { + if !strings.Contains(block, want) { + t.Fatalf("index misses %q:\n%s", want, block) + } + } + // The names alone are not the point: the rule is what makes the agent open one. + if !strings.Contains(block, "Skill tool") || !strings.Contains(block, `""`) { + t.Fatalf("index misses the invoke rule:\n%s", block) + } + if strings.Index(block, "api-recon") > strings.Index(block, "web-authz-testing") { + t.Fatalf("names are not sorted:\n%s", block) + } +} + +func TestSkillIndexBlock_Empty(t *testing.T) { + if got := SkillIndexBlock(nil); got != "" { + t.Fatalf("no skills must render nothing, got %q", got) + } + // An agent without skills keeps its previous prompt byte-for-byte. + sys, boundary := deferredSystem("SYS", DeferredInfo{}) + if len(sys) != 1 || sys[0] != "SYS" || boundary != 0 { + t.Fatalf("expected [SYS],0 — got %v,%d", sys, boundary) + } +} + +func TestSkillIndexBlock_Caps(t *testing.T) { + var names []string + for i := 0; i < 200; i++ { + names = append(names, fmt.Sprintf("skill-with-a-long-name-%03d", i)) + } + block := SkillIndexBlock(names) + if !strings.Contains(block, "more, all listed in the Skill tool") { + t.Fatalf("cap must announce the hidden names:\n%s", block) + } + if got := len(block); got > skillIndexMax+400 { + t.Fatalf("block not bounded: %d bytes", got) + } + // The cap must cut on a name boundary, never mid-name. + lines := strings.Split(strings.TrimSpace(block), "\n") + if len(lines) != 2 { + t.Fatalf("index must be rule + one names line, got %d lines:\n%s", len(lines), block) + } + parts := strings.Split(strings.Split(lines[1], " (+")[0], ", ") + for _, part := range parts { + if !strings.HasPrefix(part, "skill-with-a-long-name-") { + t.Fatalf("cut mid-name: %q", part) + } + } +} + +func TestDeferredSystem_IncludesSkillIndex(t *testing.T) { + def := DeferredInfo{SkillIndex: SkillIndexBlock([]string{"api-recon"})} + sys, _ := deferredSystem("SYS", def) + if !strings.Contains(sys[0], "api-recon") { + t.Fatalf("skill index must reach the system prompt:\n%s", sys[0]) + } +} + +// The cached-prefix path assembles the same text in two segments; the split must +// stay byte-identical to the unsplit prompt, index included, or sibling workers +// lose the shared prefix they are supposed to share. +func TestAssembleWorkerSystem_SkillIndexKeepsSplitIdentical(t *testing.T) { + def := DeferredInfo{ + FindingGuidance: "\n\nGUIDANCE", + SkillIndex: SkillIndexBlock([]string{"api-recon", "web-authz-testing"}), + GlobalNames: []string{"mcp__browser__navigate"}, + } + unsplit, _ := deferredSystem("STATIC"+"\n\nDYN", def) + split, boundary := assembleWorkerSystem("STATIC", "\n\nDYN", def, true) + if boundary != 1 { + t.Fatalf("boundary=%d want 1", boundary) + } + if got, want := strings.Join(split, "\n\n"), strings.Join(unsplit, "\n\n"); got != want { + t.Fatalf("split prompt differs from unsplit:\n--- split\n%s\n--- unsplit\n%s", got, want) + } + if !strings.Contains(split[1], "api-recon") { + t.Fatalf("index must land in the dynamic segment:\n%s", split[1]) + } +} diff --git a/agent/worker.go b/agent/worker.go index f7d8aa8..9f1fff4 100644 --- a/agent/worker.go +++ b/agent/worker.go @@ -438,7 +438,7 @@ func assembleWorkerSystem(staticPart, dynamicTail string, def DeferredInfo, spli if !split { return deferredSystem(staticPart+dynamicTail, def) } - dyn := strings.TrimPrefix(dynamicTail+def.FindingGuidance, "\n\n") + dyn := strings.TrimPrefix(dynamicTail+def.FindingGuidance+def.SkillIndex, "\n\n") if block := actool.RenderDeferredToolsBlock(def.GlobalNames); block != "" { dyn += "\n\n" + block } diff --git a/server/assembly.go b/server/assembly.go index 5711504..49c5049 100644 --- a/server/assembly.go +++ b/server/assembly.go @@ -181,11 +181,18 @@ func wireAgentAugment(pg *db.DB, skillDir string, hostTools func() ([]actool.Cor _ = c.Close() } } + var skillNames []string + if reg != nil { + for _, s := range reg.List() { + skillNames = append(skillNames, s.Name) + } + } def := agent.DeferredInfo{ Deferred: allNames, GlobalNames: globalNames, Unlock: unlock, UnlockSkill: unlockSkill, + SkillIndex: agent.SkillIndexBlock(skillNames), } return extra, def, cleanup })