diff --git a/cmd/audita/correction_repair_integration_test.go b/cmd/audita/correction_repair_integration_test.go index b23b2bb..d17d1ca 100644 --- a/cmd/audita/correction_repair_integration_test.go +++ b/cmd/audita/correction_repair_integration_test.go @@ -40,6 +40,7 @@ type correctionProvider struct { entered chan struct{} wait bool invalidFirstRevision bool + decimalReferences bool } func (p *correctionProvider) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -52,6 +53,7 @@ func (p *correctionProvider) ServeHTTP(w http.ResponseWriter, r *http.Request) { _, validation := req.ResponseFormat.JSONSchema.Schema.Properties["validations"] p.mu.Lock() p.requests = append(p.requests, req) + decimalReferences := p.decimalReferences p.mu.Unlock() content := `{"corrections":[{"id":1,"original_text":"hello","corrected_text":"Hello"},{"id":2,"original_text":"second","corrected_text":"Second"},{"id":3,"original_text":"third","corrected_text":"Third"}]}` if validation { @@ -103,6 +105,14 @@ func (p *correctionProvider) ServeHTTP(w http.ResponseWriter, r *http.Request) { } content = fmt.Sprintf(`{"proposal_ref":2,"decision":{"action":"revise","correction":{"id":%d,"original_text":"second","corrected_text":"Second"}}}`, id) } + if decimalReferences { + switch { + case repair: + content = strings.NewReplacer(`"proposal_ref":2`, `"proposal_ref":2e0`, `"id":2`, `"id":2.0`).Replace(content) + case !validation: + content = strings.Replace(content, `"id":2`, `"id":2.0`, 1) + } + } raw, _ := json.Marshal(content) w.Header().Set("Content-Type", "application/json") fmt.Fprintf(w, `{"model":"fixture","choices":[{"message":{"content":%s}}]}`, raw) @@ -172,11 +182,14 @@ func setupCorrectionFixture(t *testing.T, attempts int, replacement, wait bool) func TestSubprocessCorrectionRepairConversation(t *testing.T) { for _, tc := range []struct { - name string - debug, replacement bool - }{{"default prompt without debug", false, false}, {"replacement with debug", true, true}} { + name string + debug, replacement, decimalReferences bool + }{{"default prompt without debug", false, false, false}, {"replacement with debug and equivalent reference spellings", true, true, true}} { t.Run(tc.name, func(t *testing.T) { f, provider := setupCorrectionFixture(t, 2, tc.replacement, false) + provider.mu.Lock() + provider.decimalReferences = tc.decimalReferences + provider.mu.Unlock() args := []string{"--verbose"} if tc.debug { args = append(args, "--debug") diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index c332d88..899d5eb 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -295,6 +295,11 @@ available in the positive-budget case. ### R9 — Accept Schema-Valid Integer Spellings in Proposal and Repair References (P2) +**Complete.** Proposal and repair references use the same exact integer +decoder as transcript IDs. The CLI conversation test covers a usable sibling +and a repair revision with decimal or exponent references. The repair test +continues to reject a mismatched reference. + **Affected plan:** Stages 8, 12–14. The mismatch predates the sprint. Stage 9 already handles the corresponding validator references. diff --git a/internal/core/jsonnumber/integer.go b/internal/core/jsonnumber/integer.go index 59c02a5..c957d13 100644 --- a/internal/core/jsonnumber/integer.go +++ b/internal/core/jsonnumber/integer.go @@ -2,10 +2,22 @@ package jsonnumber import ( + "encoding/json" "strconv" "strings" ) +// Integer accepts exact JSON integer values within the native integer range. +type Integer int + +func (n *Integer) UnmarshalJSON(raw []byte) error { + if value, ok := Int(string(raw)); ok { + *n = Integer(value) + return nil + } + return json.Unmarshal(raw, (*int)(n)) +} + // Int accepts lexical integers and equivalent decimal and exponent spellings // in the native integer range. value must be a valid JSON number token. func Int(value string) (int, bool) { diff --git a/internal/core/schema/transcript.go b/internal/core/schema/transcript.go index d2b9390..21b2325 100644 --- a/internal/core/schema/transcript.go +++ b/internal/core/schema/transcript.go @@ -143,17 +143,7 @@ func extractSegments(raw []byte) (json.RawMessage, error) { } } -// sourceID decodes native integers without routing them through float64. -// Decimal and exponent spellings are accepted if they denote an exact integer. -type sourceID int - -func (id *sourceID) UnmarshalJSON(raw []byte) error { - if value, ok := jsonnumber.Int(string(raw)); ok { - *id = sourceID(value) - return nil - } - return json.Unmarshal(raw, (*int)(id)) -} +type sourceID = jsonnumber.Integer func validateSourceSegments(segments []SourceSegment) error { for i, s := range segments { diff --git a/internal/framework/proposal_generation/generate.go b/internal/framework/proposal_generation/generate.go index ec4fab4..fca1ff1 100644 --- a/internal/framework/proposal_generation/generate.go +++ b/internal/framework/proposal_generation/generate.go @@ -8,6 +8,7 @@ import ( "fmt" "strings" + "gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber" "gitea.maximumdirect.net/eric/audita/v3/internal/core/schema" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes" @@ -19,9 +20,9 @@ import ( // StructuredCorrectionProposal is one LLM response correction payload. type StructuredCorrectionProposal struct { - SegmentReference int `json:"id"` - OriginalText string `json:"original_text"` - CorrectedText string `json:"corrected_text"` + SegmentReference jsonnumber.Integer `json:"id"` + OriginalText string `json:"original_text"` + CorrectedText string `json:"corrected_text"` } // StructuredCorrectionSet is the reusable structured LLM response model for @@ -134,10 +135,11 @@ func GenerateCandidates(ctx context.Context, req Request) (Result, error) { references := make([]int, 0, len(response.Corrections)) corrections := make([]proposals.CorrectionProposal, 0, len(response.Corrections)) for _, raw := range response.Corrections { - references = append(references, raw.SegmentReference) + ref := int(raw.SegmentReference) + references = append(references, ref) // Unknown references remain invalid candidates for the existing domain validators. corrections = append(corrections, proposals.CorrectionProposal{ - TargetSegmentID: proposals.SegmentIDAtReference(req.WorkingTranscript, raw.SegmentReference), + TargetSegmentID: proposals.SegmentIDAtReference(req.WorkingTranscript, ref), OriginalText: raw.OriginalText, CorrectedText: raw.CorrectedText, }) diff --git a/internal/framework/proposal_generation/repair.go b/internal/framework/proposal_generation/repair.go index ae1f8cb..50f7d26 100644 --- a/internal/framework/proposal_generation/repair.go +++ b/internal/framework/proposal_generation/repair.go @@ -3,14 +3,15 @@ package proposal_generation import ( "encoding/json" + "gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput" ) // RepairResponse is one model response for an original proposal reference. type RepairResponse struct { - ProposalRef int `json:"proposal_ref"` - Decision RepairDecision `json:"decision"` + ProposalRef jsonnumber.Integer `json:"proposal_ref"` + Decision RepairDecision `json:"decision"` } // RepairDecision selects revision or withdrawal for the referenced proposal. @@ -26,7 +27,7 @@ func DecodeRepairResponse(content []byte, expectedRef int) (RepairResponse, erro if err := json.Unmarshal(content, &response); err != nil { return RepairResponse{}, invalidRepairResponse(err) } - if response.ProposalRef != expectedRef || (response.Decision.Action != "revise" || response.Decision.Correction == nil) && (response.Decision.Action != "withdraw" || response.Decision.Correction != nil) { + if int(response.ProposalRef) != expectedRef || (response.Decision.Action != "revise" || response.Decision.Correction == nil) && (response.Decision.Action != "withdraw" || response.Decision.Correction != nil) { return response, invalidRepairResponse(nil) } return response, nil diff --git a/internal/framework/proposal_generation/repair_test.go b/internal/framework/proposal_generation/repair_test.go index d6e1b03..86697f9 100644 --- a/internal/framework/proposal_generation/repair_test.go +++ b/internal/framework/proposal_generation/repair_test.go @@ -15,7 +15,7 @@ func TestDecodeRepairResponse(t *testing.T) { }{ {"revision with deletion", `{"proposal_ref":2,"decision":{"action":"revise","correction":{"id":1,"original_text":" ","corrected_text":""}}}`, true}, {"withdrawal", `{"proposal_ref":2,"decision":{"action":"withdraw","correction":null}}`, true}, - {"wrong reference", `{"proposal_ref":1,"decision":{"action":"withdraw","correction":null}}`, false}, + {"wrong reference", `{"proposal_ref":1e0,"decision":{"action":"withdraw","correction":null}}`, false}, {"missing revision", `{"proposal_ref":2,"decision":{"action":"revise","correction":null}}`, false}, {"withdrawal with revision", `{"proposal_ref":2,"decision":{"action":"withdraw","correction":{"id":1}}}`, false}, } { diff --git a/internal/framework/runner/repair.go b/internal/framework/runner/repair.go index ce05ced..f3c94e1 100644 --- a/internal/framework/runner/repair.go +++ b/internal/framework/runner/repair.go @@ -6,6 +6,7 @@ import ( "fmt" "slices" + "gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposal_generation" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals" @@ -144,7 +145,7 @@ func runRepairBranch(ctx context.Context, branchInput repairBranchInput) (repair buildRequest := func() (*structuredoutput.Request, error) { request := proposal_generation.RepairRequest{ ProposalRef: proposalRef, - Correction: proposal_generation.StructuredCorrectionProposal{SegmentReference: localRef, OriginalText: current.OriginalText, CorrectedText: current.CorrectedText}, + Correction: proposal_generation.StructuredCorrectionProposal{SegmentReference: jsonnumber.Integer(localRef), OriginalText: current.OriginalText, CorrectedText: current.CorrectedText}, Findings: findings, } if pos, ok := input.Section.Position(current.TargetSegmentID); ok { @@ -249,7 +250,7 @@ func runRepairBranch(ctx context.Context, branchInput repairBranchInput) (repair return nil, nil } revision := response.Decision.Correction - localRef = revision.SegmentReference + localRef = int(revision.SegmentReference) current = original current.CorrectionProposal = proposals.CorrectionProposal{TargetSegmentID: proposals.SegmentIDAtReference(input.WorkingTranscript, localRef), OriginalText: revision.OriginalText, CorrectedText: revision.CorrectedText} if debug { diff --git a/internal/framework/runner/repair_test.go b/internal/framework/runner/repair_test.go index 8ce4583..c707e41 100644 --- a/internal/framework/runner/repair_test.go +++ b/internal/framework/runner/repair_test.go @@ -157,7 +157,7 @@ func TestRepairRequestRefreshesPreviewAfterRetarget(t *testing.T) { if err := json.Unmarshal([]byte(client.calls[i].Variables["RepairRequestJSON"]), &payload); err != nil { t.Fatal(err) } - if payload.ProposalRef != 1 || payload.Correction.SegmentReference != i+1 || payload.OriginalSegmentText == nil || *payload.OriginalSegmentText != want.before || payload.CorrectedSegmentText == nil || *payload.CorrectedSegmentText != want.after { + if payload.ProposalRef != 1 || int(payload.Correction.SegmentReference) != i+1 || payload.OriginalSegmentText == nil || *payload.OriginalSegmentText != want.before || payload.CorrectedSegmentText == nil || *payload.CorrectedSegmentText != want.after { t.Fatalf("attempt %d did not describe its current proposal: %+v", i+1, payload) } } diff --git a/internal/modules/grammar/module_test.go b/internal/modules/grammar/module_test.go index 4f6f7be..c96aad0 100644 --- a/internal/modules/grammar/module_test.go +++ b/internal/modules/grammar/module_test.go @@ -9,6 +9,7 @@ import ( "testing" domain "gitea.maximumdirect.net/eric/audita/v3/internal/core/glossary" + "gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber" "gitea.maximumdirect.net/eric/audita/v3/internal/core/schema" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/llm" @@ -42,7 +43,7 @@ func (f *fakeLLMClient) CompleteStructured(ctx context.Context, req contracts.St items, _ := payload["corrections"].([]map[string]any) for _, item := range items { target.Corrections = append(target.Corrections, proposal_generation.StructuredCorrectionProposal{ - SegmentReference: item["id"].(int), + SegmentReference: jsonnumber.Integer(item["id"].(int)), OriginalText: item["original_text"].(string), CorrectedText: item["corrected_text"].(string), })