From b1cde637a2f048509e0cd9e6b13639565a2592a0 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Thu, 26 Nov 2026 18:14:27 +0000 Subject: [PATCH] Retain selected glossary instructions --- docs/roadmap/implementation.md | 2 ++ internal/appconfig/glossary_test.go | 4 ++++ internal/core/config/config.go | 12 ++++++++-- internal/framework/contracts/contracts.go | 12 ++++++++++ .../framework/contracts/contracts_test.go | 23 +++++++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 0182bae..5394212 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -47,6 +47,8 @@ before editing and use the graph-first discovery instructions in `AGENTS.md`. ## Stage 1: Add collection instructions and retain selected context +Status: Complete. + Work: 1. Add `Instruction string` with YAML key `instruction` to diff --git a/internal/appconfig/glossary_test.go b/internal/appconfig/glossary_test.go index 3dd1e10..67eee0d 100644 --- a/internal/appconfig/glossary_test.go +++ b/internal/appconfig/glossary_test.go @@ -38,6 +38,7 @@ glossary_collections: - path: shared/extra.yml required: true party: + instruction: Use established names. entries: - id: hero name: Original @@ -54,6 +55,9 @@ party: t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics()) } party := result.Config.Application.GlossaryCollections["party"].Entries + if got := result.Config.Application.GlossaryCollections["party"].Instruction; got != "Use established names." { + t.Fatalf("instruction not inherited: %q", got) + } if len(party) != 2 || party[0].Name != "Revised" || len(party[0].Aliases) != 0 || party[1].ID != "other" { t.Fatalf("party=%+v", party) } diff --git a/internal/core/config/config.go b/internal/core/config/config.go index c3ddb9b..78c6cb6 100644 --- a/internal/core/config/config.go +++ b/internal/core/config/config.go @@ -33,7 +33,8 @@ type Config struct { } type GlossaryCollection struct { - Entries []GlossaryEntry `yaml:"entries"` + Instruction string `yaml:"instruction"` + Entries []GlossaryEntry `yaml:"entries"` } type GlossaryEntry struct { ID string `yaml:"id"` @@ -106,11 +107,18 @@ func defaults() Config { } } +// GlossaryEntryIdentity identifies entries beneath a collections mapping. +func GlossaryEntryIdentity(prefix ck.SequenceSelector) ck.SequenceIdentity { + selector := append(ck.SequenceSelector(nil), prefix...) + selector = append(selector, ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")) + return ck.SequenceIdentityMatching(selector, "id") +} + // Definition describes the application settings independently of their sources. func Definition() ck.Definition[Config] { return ck.Definition[Config]{ Defaults: defaults, - SequenceIdentities: []ck.SequenceIdentity{ck.SequenceIdentityMatching(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections"), ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")}, "id")}, + SequenceIdentities: []ck.SequenceIdentity{GlossaryEntryIdentity(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections")})}, Version: &ck.VersionRule{Path: ck.Keys("version"), Check: func(version ck.Version, v *ck.Validation) error { if version.Present && (version.Null || version.Text != "3") { v.Error(ck.Keys("version"), "use application configuration version 3; see docs/migrations/v3.md") diff --git a/internal/framework/contracts/contracts.go b/internal/framework/contracts/contracts.go index 2c15b56..09cf5b0 100644 --- a/internal/framework/contracts/contracts.go +++ b/internal/framework/contracts/contracts.go @@ -31,6 +31,11 @@ type TranscriptModule interface { type StructuredCompletionRequest = structuredoutput.Request type StructuredCompletionResponse = structuredoutput.Response +type GlossaryInstruction struct { + Collection string + Text string +} + // ModuleRunSpec identifies one resolved module instance in a pipeline. type ModuleRunSpec struct { ModuleKey string `json:"module_key"` @@ -40,6 +45,7 @@ type ModuleRunSpec struct { ID *string `json:"-"` Settings config.Stage `json:"-"` Glossary []glossary.Entry `json:"-"` + GlossaryInstructions []GlossaryInstruction `json:"-"` ProtectedTerms []glossary.Term `json:"-"` GlossaryView *validators.GlossaryView `json:"-"` GlossaryVariables *promptcontext.GlossaryVariables `json:"-"` @@ -140,10 +146,16 @@ func ResolveModuleRunSpecs(cfg *config.Config) []ModuleRunSpec { key := GlossarySelection(item) if prior, ok := selections[key]; ok { spec.Glossary = specs[prior].Glossary + spec.GlossaryInstructions = specs[prior].GlossaryInstructions spec.ProtectedTerms = specs[prior].ProtectedTerms continue } selected := glossary.Select(cfg.GlossaryCollections, item.Glossaries) + for _, name := range item.Glossaries { + if collection, ok := cfg.GlossaryCollections[name]; ok { + spec.GlossaryInstructions = append(spec.GlossaryInstructions, GlossaryInstruction{Collection: name, Text: collection.Instruction}) + } + } protected := glossary.Select(cfg.GlossaryCollections, item.ProtectedTerms) var excluded []glossary.Entry if key.exempt { diff --git a/internal/framework/contracts/contracts_test.go b/internal/framework/contracts/contracts_test.go index b38537f..739892f 100644 --- a/internal/framework/contracts/contracts_test.go +++ b/internal/framework/contracts/contracts_test.go @@ -92,6 +92,29 @@ func TestResolveModuleRunSpecs(t *testing.T) { } } +func TestResolveGlossaryInstructions(t *testing.T) { + cfg := &config.Config{ + GlossaryCollections: map[string]config.GlossaryCollection{ + "one": {Instruction: "First"}, "two": {Instruction: "Second"}, "protected": {Instruction: "Protection only"}, + }, + Pipeline: config.Pipeline{Stages: []config.Stage{ + {Module: "grammar", Glossaries: []string{"two", "one"}, ProtectedTerms: []string{"protected"}}, + {Module: "grammar", Glossaries: []string{"two", "one"}, ProtectedTerms: []string{"protected"}}, + {Module: "grammar", Glossaries: []string{"one"}, ProtectedTerms: []string{"protected"}}, + }}, + } + specs := ResolveModuleRunSpecs(cfg) + want := []GlossaryInstruction{{Collection: "two", Text: "Second"}, {Collection: "one", Text: "First"}} + for _, spec := range specs[:2] { + if !reflect.DeepEqual(spec.GlossaryInstructions, want) { + t.Fatalf("instructions = %+v, want %+v", spec.GlossaryInstructions, want) + } + } + if !reflect.DeepEqual(specs[2].GlossaryInstructions, want[1:]) { + t.Fatalf("specific selection = %+v", specs[2].GlossaryInstructions) + } +} + func TestSectionMetadataFromSection(t *testing.T) { section := chunking.Section{ Index: 3,