Improve prompt roles and correction repair context

This commit is contained in:
2026-11-11 17:42:30 +00:00
parent cf1fbe5748
commit f42573f8b0
23 changed files with 256 additions and 40 deletions

View File

@@ -19,6 +19,7 @@ func TestComposedValidationBudgetMatchesSerializedPayload(t *testing.T) {
for i := range items {
items[i] = LLMValidationItem{
CorrectionIndex: 100 + i*7,
Speaker: "Guest \"A\" — session host",
OriginalText: "one\u00a0two!",
CorrectedText: "one two",
OriginalSegmentText: "she said \"one\u00a0two!\"\n",

View File

@@ -14,9 +14,9 @@ import (
func TestValidationContextCarriesAdjacentEvidenceAndRelevantDefinitions(t *testing.T) {
req := makeReq([]proposals.EnrichedCorrectionProposal{mk(7, "gestures", "Jesters")})
req.WorkingTranscript = &schema.Transcript{Segments: []schema.Segment{
{ID: 1, Text: "The performers arrived."},
{ID: 2, Text: "Those gestures are back."},
{ID: 3, Text: "Lyra recognized the troupe."},
{ID: 1, Speaker: "Host", Text: "The performers arrived."},
{ID: 2, Speaker: "Guest", Text: "Those gestures are back."},
{ID: 3, Speaker: "Host", Text: "Lyra recognized the troupe."},
{ID: 4, Text: "Elsewhere."},
}}
req.CandidateProposal[0].TargetSegmentID = 2
@@ -44,14 +44,15 @@ func TestValidationContextCarriesAdjacentEvidenceAndRelevantDefinitions(t *testi
if strings.Contains(variables["PayloadJSON"], `"segment_id":`) || rendered[0].CorrectionIndex != 1 || payload.Items[0].CorrectionIndex != 7 {
t.Fatalf("leaked internal identity or mutated request: %s", variables["PayloadJSON"])
}
if len(rendered[0].NeighboringSegments) != 2 || rendered[0].GlossaryEntries[0].Description != "A traveling troupe" {
if rendered[0].Speaker != "Guest" || len(rendered[0].NeighboringSegments) != 2 || rendered[0].NeighboringSegments[0].Speaker != "Host" || rendered[0].GlossaryEntries[0].Description != "A traveling troupe" {
t.Fatalf("rendered context: %+v", rendered)
}
// The context-free endpoint must not reach beyond the current chunk.
req.WorkingTranscript.Segments = req.WorkingTranscript.Segments[1:2]
req.WorkingTranscript.Segments[0].Speaker = ""
req.Glossary = nil
isolated, _ := BuildLLMValidationRequest("editorial_review", LLMValidatorTypeEditorialReview, req)
if len(isolated.Items[0].NeighboringSegments) != 0 || len(isolated.Items[0].GlossaryEntries) != 0 {
if isolated.Items[0].Speaker != "" || len(isolated.Items[0].NeighboringSegments) != 0 || len(isolated.Items[0].GlossaryEntries) != 0 {
t.Fatalf("unexpected external context: %+v", isolated)
}
}
@@ -63,12 +64,18 @@ func TestValidationContextFitsBudgetWithoutDiscardingCandidate(t *testing.T) {
if err != nil {
t.Fatal(err)
}
base.Speaker = "Guest speaker with a long identifying label"
withSpeaker, err := estimateBatchTokens(estimator, []LLMValidationItem{base})
if err != nil || withSpeaker <= required {
t.Fatalf("speaker missing from token accounting: before=%d after=%d err=%v", required, withSpeaker, err)
}
required = withSpeaker
enriched := base
enriched.NeighboringSegments = []NeighboringSegment{{Position: "following", Text: strings.Repeat("long context ", 300)}}
enriched.GlossaryEntries = []glossary.Entry{{Name: "cat", Description: strings.Repeat("long definition ", 300)}}
fitted := fitValidationContext(enriched, required, estimator)
tokens, err := estimateBatchTokens(estimator, []LLMValidationItem{fitted})
if err != nil || tokens > required || fitted.OriginalSegmentText != base.OriginalSegmentText || fitted.CorrectedSegmentText != base.CorrectedSegmentText {
if err != nil || tokens > required || fitted.Speaker != base.Speaker || fitted.OriginalSegmentText != base.OriginalSegmentText || fitted.CorrectedSegmentText != base.CorrectedSegmentText {
t.Fatalf("fitted: %+v tokens=%d err=%v", fitted, tokens, err)
}
full, _ := estimateBatchTokens(estimator, []LLMValidationItem{enriched})

View File

@@ -22,6 +22,7 @@ type StructuredLLMClient = structuredoutput.Client
type LLMValidationItem struct {
CorrectionIndex int `json:"correction_index"`
SegmentID int `json:"-"`
Speaker string `json:"speaker"`
OriginalText string `json:"original_text"`
CorrectedText string `json:"corrected_text"`
OriginalSegmentText string `json:"original_segment_text"`

View File

@@ -269,6 +269,7 @@ func BuildLLMValidationRequest(validatorName string, validatorType LLMValidatorT
items = append(items, LLMValidationItem{
CorrectionIndex: p.ProposalIndex,
SegmentID: p.TargetSegmentID,
Speaker: seg.Speaker,
OriginalText: p.OriginalText,
CorrectedText: p.CorrectedText,
OriginalSegmentText: seg.Text,