diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 15d8af8..3eae679 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -221,6 +221,8 @@ Acceptance: ## Stage 5: Render instructions in proposals and retain them during repair +Status: Complete. + Work: 1. Extend shared stage glossary variables with an instruction block prepared diff --git a/internal/framework/contracts/contracts.go b/internal/framework/contracts/contracts.go index 09cf5b0..8db3407 100644 --- a/internal/framework/contracts/contracts.go +++ b/internal/framework/contracts/contracts.go @@ -31,10 +31,7 @@ type TranscriptModule interface { type StructuredCompletionRequest = structuredoutput.Request type StructuredCompletionResponse = structuredoutput.Response -type GlossaryInstruction struct { - Collection string - Text string -} +type GlossaryInstruction = promptcontext.GlossaryInstruction // ModuleRunSpec identifies one resolved module instance in a pipeline. type ModuleRunSpec struct { @@ -62,6 +59,7 @@ func (s *ModuleRunSpec) PrepareGlossary() error { if err != nil { return err } + variables.InstructionsBlock = promptcontext.GlossaryInstructionsBlock(s.GlossaryInstructions) s.GlossaryVariables = &variables } if s.GlossaryView == nil { diff --git a/internal/framework/contracts/contracts_test.go b/internal/framework/contracts/contracts_test.go index 739892f..45761d7 100644 --- a/internal/framework/contracts/contracts_test.go +++ b/internal/framework/contracts/contracts_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "reflect" + "strings" "testing" "gitea.maximumdirect.net/eric/audita/v4/internal/core/chunking" @@ -95,24 +96,31 @@ 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"}, + "one": {Instruction: "First {{literal}}"}, "two": {Instruction: "Second"}, "protected": {Instruction: "Protection only"}, "empty": {}, }, 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{"two", "empty", "one"}, ProtectedTerms: []string{"protected"}}, + {Module: "grammar", Glossaries: []string{"two", "empty", "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"}} + want := []GlossaryInstruction{{Collection: "two", Text: "Second"}, {Collection: "empty"}, {Collection: "one", Text: "First {{literal}}"}} 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:]) { + if !reflect.DeepEqual(specs[2].GlossaryInstructions, want[2:]) { t.Fatalf("specific selection = %+v", specs[2].GlossaryInstructions) } + if err := specs[0].PrepareGlossary(); err != nil { + t.Fatal(err) + } + block := specs[0].GlossaryVariables.InstructionsBlock + if !strings.Contains(block, "two") || !strings.Contains(block, "one") || !strings.Contains(block, "First {{literal}}") || strings.Index(block, "Second") > strings.Index(block, "First") || strings.Contains(block, "empty") || strings.Contains(block, "Protection only") { + t.Fatalf("instruction block = %q", block) + } } func TestSectionMetadataFromSection(t *testing.T) { diff --git a/internal/framework/llm/conversation_test.go b/internal/framework/llm/conversation_test.go index 518ee1f..81704af 100644 --- a/internal/framework/llm/conversation_test.go +++ b/internal/framework/llm/conversation_test.go @@ -69,7 +69,7 @@ func TestRoleClientCoordinatesIndependentRepairConversations(t *testing.T) { if err != nil { t.Fatal(err) } - vars := map[string]string{"TranscriptDescriptionBlock": "", "GlossaryJSON": "[]", "SectionJSON": "[]"} + vars := map[string]string{"TranscriptDescriptionBlock": "", "GlossaryJSON": "[]", "SectionJSON": "[]", "GlossaryInstructionsBlock": "party:\nKeep {{literal}} names"} initial, err := proposal.CompleteStructured(context.Background(), structuredoutput.Request{PromptID: prompts.PromptIDModuleGrammarProposal, Variables: vars, CaptureExchange: true}, &map[string]any{}) if err != nil || initial.Exchange == nil { t.Fatalf("capture: %+v %v", initial, err) @@ -95,6 +95,11 @@ func TestRoleClientCoordinatesIndependentRepairConversations(t *testing.T) { t.Fatalf("models = %v", models) } prefix := messages[0] + initialMessages, _ := json.Marshal(prefix) + repairMessages, _ := json.Marshal(messages[1]) + if !strings.Contains(string(initialMessages), "Keep {{literal}} names") || !strings.Contains(string(repairMessages), "Keep {{literal}} names") { + t.Fatalf("guidance missing from proposal or repair: %s %s", initialMessages, repairMessages) + } if len(messages) != 3 || !reflect.DeepEqual(messages[1][:len(prefix)], prefix) || !reflect.DeepEqual(messages[2][:len(prefix)], prefix) { t.Fatalf("prefixes: %+v", messages) } diff --git a/internal/framework/preflight/readiness_test.go b/internal/framework/preflight/readiness_test.go index 528e43d..f17c520 100644 --- a/internal/framework/preflight/readiness_test.go +++ b/internal/framework/preflight/readiness_test.go @@ -74,8 +74,8 @@ func TestProcessingPlanReusesOnlyMatchingGlossaryViews(t *testing.T) { exempt.Module = "glossary" cfg := &config.Config{ GlossaryCollections: map[string]config.GlossaryCollection{ - "a": {Entries: []config.GlossaryEntry{{ID: "a", Name: "A"}}}, - "b": {Entries: []config.GlossaryEntry{{ID: "b", Name: "B"}}}, + "a": {Instruction: "First instruction", Entries: []config.GlossaryEntry{{ID: "a", Name: "A"}}}, + "b": {Instruction: "Second instruction", Entries: []config.GlossaryEntry{{ID: "b", Name: "B"}}}, }, Pipeline: config.Pipeline{Stages: []config.Stage{item, item, reversed, exempt}}, } @@ -93,6 +93,9 @@ func TestProcessingPlanReusesOnlyMatchingGlossaryViews(t *testing.T) { plan[2].Glossary[0].ID != "b" || plan[3].GlossaryVariables.ProtectionJSON != "[]" { t.Fatalf("incorrect glossary reuse or exemptions: %+v", plan) } + if !strings.Contains(plan[1].GlossaryVariables.InstructionsBlock, "First instruction") || strings.Index(plan[2].GlossaryVariables.InstructionsBlock, "Second instruction") > strings.Index(plan[2].GlossaryVariables.InstructionsBlock, "First instruction") { + t.Fatalf("instruction reuse/order: %+v", plan) + } } func TestProcessingPlanPreservesFirstInspectionFailureAndCause(t *testing.T) { diff --git a/internal/framework/promptcontext/glossary.go b/internal/framework/promptcontext/glossary.go index 5bb71f0..db98643 100644 --- a/internal/framework/promptcontext/glossary.go +++ b/internal/framework/promptcontext/glossary.go @@ -3,14 +3,31 @@ package promptcontext import ( "encoding/json" "fmt" + "strings" "gitea.maximumdirect.net/eric/audita/v4/internal/core/glossary" ) // GlossaryVariables contains the glossary inputs shared by a stage's proposals. type GlossaryVariables struct { - ContextJSON string - ProtectionJSON string + ContextJSON string + ProtectionJSON string + InstructionsBlock string +} + +type GlossaryInstruction struct { + Collection string + Text string +} + +func GlossaryInstructionsBlock(instructions []GlossaryInstruction) string { + var blocks []string + for _, instruction := range instructions { + if instruction.Text != "" { + blocks = append(blocks, instruction.Collection+":\n"+instruction.Text) + } + } + return strings.Join(blocks, "\n\n") } func PrepareGlossaryVariables(entries []glossary.Entry, terms []glossary.Term) (GlossaryVariables, error) { diff --git a/internal/framework/proposal_generation/module_proposal.go b/internal/framework/proposal_generation/module_proposal.go index 0f1d5e8..9674e3c 100644 --- a/internal/framework/proposal_generation/module_proposal.go +++ b/internal/framework/proposal_generation/module_proposal.go @@ -35,6 +35,7 @@ func ExecuteModuleProposal(ctx context.Context, req ModuleProposalRequest) (cont if err != nil { return contracts.ProposalResult{}, err } + prepared.InstructionsBlock = promptcontext.GlossaryInstructionsBlock(req.ProposalRequest.RunSpec.GlossaryInstructions) glossary = &prepared } variables, err := buildProposalVariables( diff --git a/internal/framework/proposal_generation/variables.go b/internal/framework/proposal_generation/variables.go index 58b8da9..762502a 100644 --- a/internal/framework/proposal_generation/variables.go +++ b/internal/framework/proposal_generation/variables.go @@ -15,6 +15,7 @@ func buildProposalVariables(transcript *schema.Transcript, glossary promptcontex return map[string]string{ "TranscriptDescriptionBlock": promptcontext.TranscriptDescriptionBlock(transcriptDescription), "GlossaryJSON": glossary.ContextJSON, + "GlossaryInstructionsBlock": glossary.InstructionsBlock, "ProtectedTermsJSON": glossary.ProtectionJSON, "SectionJSON": string(sectionJSON), }, nil diff --git a/internal/framework/proposal_generation/variables_test.go b/internal/framework/proposal_generation/variables_test.go index dffaf38..5e21476 100644 --- a/internal/framework/proposal_generation/variables_test.go +++ b/internal/framework/proposal_generation/variables_test.go @@ -16,11 +16,12 @@ func TestBuildProposalVariablesPreservesSectionGlossaryAndDescription(t *testing if err != nil { t.Fatal(err) } + prepared.InstructionsBlock = "party: use this guidance" variables, err := buildProposalVariables(transcript, prepared, 0, " Fantasy names ") if err != nil { t.Fatal(err) } - if !strings.Contains(variables["SectionJSON"], `"section_index": 0`) || !strings.Contains(variables["SectionJSON"], "gestures") || !strings.Contains(variables["GlossaryJSON"], "Jesters") || !strings.Contains(variables["TranscriptDescriptionBlock"], "Fantasy names") { + if !strings.Contains(variables["SectionJSON"], `"section_index": 0`) || !strings.Contains(variables["SectionJSON"], "gestures") || !strings.Contains(variables["GlossaryJSON"], "Jesters") || !strings.Contains(variables["TranscriptDescriptionBlock"], "Fantasy names") || variables["GlossaryInstructionsBlock"] != prepared.InstructionsBlock { t.Fatalf("encoded variables incomplete: %+v", variables) } empty, err := buildProposalVariables(transcript, prepared, 0, " ") diff --git a/internal/prompts/assets/prompts/modules.glossary.proposal.yaml b/internal/prompts/assets/prompts/modules.glossary.proposal.yaml index 16c23d0..e0ad3bf 100644 --- a/internal/prompts/assets/prompts/modules.glossary.proposal.yaml +++ b/internal/prompts/assets/prompts/modules.glossary.proposal.yaml @@ -31,6 +31,9 @@ collections: {{ .TranscriptDescriptionBlock }}Glossary context: {{ .GlossaryJSON }} + Glossary-use guidance: + {{ .GlossaryInstructionsBlock }} + Protected spellings: {{ .ProtectedTermsJSON }} diff --git a/internal/prompts/assets/prompts/modules.grammar.proposal.yaml b/internal/prompts/assets/prompts/modules.grammar.proposal.yaml index 558fd70..a5275c3 100644 --- a/internal/prompts/assets/prompts/modules.grammar.proposal.yaml +++ b/internal/prompts/assets/prompts/modules.grammar.proposal.yaml @@ -27,6 +27,9 @@ collections: {{ .TranscriptDescriptionBlock }}Glossary context: {{ .GlossaryJSON }} + Glossary-use guidance: + {{ .GlossaryInstructionsBlock }} + Protected spellings: {{ .ProtectedTermsJSON }} diff --git a/internal/prompts/assets/prompts/modules.homophones.proposal.yaml b/internal/prompts/assets/prompts/modules.homophones.proposal.yaml index 11e2425..12056b9 100644 --- a/internal/prompts/assets/prompts/modules.homophones.proposal.yaml +++ b/internal/prompts/assets/prompts/modules.homophones.proposal.yaml @@ -28,6 +28,9 @@ collections: {{ .TranscriptDescriptionBlock }}Glossary context: {{ .GlossaryJSON }} + Glossary-use guidance: + {{ .GlossaryInstructionsBlock }} + Protected spellings: {{ .ProtectedTermsJSON }} diff --git a/internal/prompts/assets/prompts/modules.spoken_word.proposal.yaml b/internal/prompts/assets/prompts/modules.spoken_word.proposal.yaml index 4572941..c5ddde8 100644 --- a/internal/prompts/assets/prompts/modules.spoken_word.proposal.yaml +++ b/internal/prompts/assets/prompts/modules.spoken_word.proposal.yaml @@ -31,6 +31,9 @@ collections: {{ .TranscriptDescriptionBlock }}Glossary context: {{ .GlossaryJSON }} + Glossary-use guidance: + {{ .GlossaryInstructionsBlock }} + Protected spellings: {{ .ProtectedTermsJSON }} diff --git a/internal/prompts/registry.go b/internal/prompts/registry.go index 531a589..10f13bb 100644 --- a/internal/prompts/registry.go +++ b/internal/prompts/registry.go @@ -112,6 +112,9 @@ func mergeSharedVariables(values map[string]string, authoritative bool) map[stri if _, exists := result["ProtectedTermsJSON"]; !exists { result["ProtectedTermsJSON"] = "[]" } + if _, exists := result["GlossaryInstructionsBlock"]; !exists { + result["GlossaryInstructionsBlock"] = "" + } return result } diff --git a/internal/prompts/registry_test.go b/internal/prompts/registry_test.go index cacb4ef..7c0306e 100644 --- a/internal/prompts/registry_test.go +++ b/internal/prompts/registry_test.go @@ -19,6 +19,7 @@ func TestEmbeddedDefinitionsPrepareThroughPromptKit(t *testing.T) { "HardeningPolicy": prompts.HardeningText(), "TranscriptDescriptionBlock": "Transcript description:\nfixture-description\n\n", "GlossaryJSON": "{\"name\":\"fixture-glossary\"}", + "GlossaryInstructionsBlock": "fixture-guidance", "SectionJSON": "{\"text\":\"fixture-section\"}", "PayloadJSON": "[{\"text\":\"fixture-correction\"}]", } @@ -59,6 +60,21 @@ func TestEmbeddedDefinitionsPrepareThroughPromptKit(t *testing.T) { if strings.HasPrefix(tc.id, "audita/modules.") && !strings.Contains(rendered, "fixture-glossary") { t.Error("missing glossary context") } + if strings.HasPrefix(tc.id, "audita/modules.") { + if !strings.Contains(rendered, "fixture-guidance") { + t.Error("missing guidance") + } + without := make(map[string]string, len(vars)) + for k, v := range vars { + if k != "GlossaryInstructionsBlock" { + without[k] = v + } + } + without = prompts.WithSharedVariables(without) + if _, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: tc.id, ProfileID: "fixture", Vars: without}); err != nil { + t.Fatalf("omitted guidance: %v", err) + } + } missing := map[string]string{"HardeningPolicy": prompts.HardeningText()} if _, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: tc.id, ProfileID: "fixture", Vars: missing}); err == nil { t.Error("missing variables accepted")