Share transcript envelope validation
This commit is contained in:
@@ -2,7 +2,6 @@ package audita
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -11,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/subprocess"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/contracts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
@@ -203,16 +203,10 @@ func validateProcessedOutput(path string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("audita processed transcript result exceeds or cannot be read within %d-byte limit: %w", MaxProcessedOutputBytes, err)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return fmt.Errorf("parse json: %w", err)
|
||||
err = contracts.ValidateTranscriptEnvelope(data)
|
||||
var decode *contracts.TranscriptDecodeError
|
||||
if errors.As(err, &decode) {
|
||||
return fmt.Errorf("parse json: %w", decode.Err)
|
||||
}
|
||||
segments, ok := payload["segments"]
|
||||
if !ok {
|
||||
return fmt.Errorf("top-level segments is required")
|
||||
}
|
||||
if _, ok := segments.([]any); !ok {
|
||||
return fmt.Errorf("top-level segments must be an array")
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package seriatim
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/subprocess"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/contracts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
)
|
||||
|
||||
@@ -659,19 +661,12 @@ func validateJSONFileWithSegments(path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return fmt.Errorf("parse json: %w", err)
|
||||
err = contracts.ValidateTranscriptEnvelope(data)
|
||||
var decode *contracts.TranscriptDecodeError
|
||||
if errors.As(err, &decode) {
|
||||
return fmt.Errorf("parse json: %w", decode.Err)
|
||||
}
|
||||
|
||||
segments, ok := payload["segments"]
|
||||
if !ok {
|
||||
return fmt.Errorf("top-level segments is required")
|
||||
}
|
||||
if _, ok := segments.([]any); !ok {
|
||||
return fmt.Errorf("top-level segments must be an array")
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
func validateNonEmptyTextFile(path string) error {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/contracts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
|
||||
@@ -404,18 +405,7 @@ func validateTranscriptSegmentsJSON(path string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return fmt.Errorf("decode json: %w", err)
|
||||
}
|
||||
segments, ok := payload["segments"]
|
||||
if !ok {
|
||||
return fmt.Errorf("top-level segments is required")
|
||||
}
|
||||
if _, ok := segments.([]any); !ok {
|
||||
return fmt.Errorf("top-level segments must be an array")
|
||||
}
|
||||
return nil
|
||||
return contracts.ValidateTranscriptEnvelope(data)
|
||||
}
|
||||
|
||||
func validateJSONContent(path string) error {
|
||||
|
||||
28
internal/contracts/transcript.go
Normal file
28
internal/contracts/transcript.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package contracts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// TranscriptDecodeError identifies invalid JSON or a non-object root.
|
||||
type TranscriptDecodeError struct{ Err error }
|
||||
|
||||
func (e *TranscriptDecodeError) Error() string { return "decode json: " + e.Err.Error() }
|
||||
func (e *TranscriptDecodeError) Unwrap() error { return e.Err }
|
||||
|
||||
// ValidateTranscriptEnvelope checks the shared minimal transcript JSON shape.
|
||||
func ValidateTranscriptEnvelope(data []byte) error {
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return &TranscriptDecodeError{Err: err}
|
||||
}
|
||||
segments, ok := payload["segments"]
|
||||
if !ok {
|
||||
return fmt.Errorf("top-level segments is required")
|
||||
}
|
||||
if _, ok := segments.([]any); !ok {
|
||||
return fmt.Errorf("top-level segments must be an array")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
23
internal/contracts/transcript_test.go
Normal file
23
internal/contracts/transcript_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package contracts
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateTranscriptEnvelope(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, input string
|
||||
valid bool
|
||||
}{
|
||||
{"empty segments", `{"segments":[]}`, true},
|
||||
{"other members", `{"extra":true,"segments":[null,{"anything":1}]}`, true},
|
||||
{"malformed", `{`, false}, {"missing", `{}`, false}, {"null segments", `{"segments":null}`, false},
|
||||
{"object segments", `{"segments":{}}`, false}, {"string segments", `{"segments":"x"}`, false},
|
||||
{"null root", `null`, false}, {"array root", `[]`, false}, {"string root", `"text"`, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := ValidateTranscriptEnvelope([]byte(tc.input))
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatalf("error = %v, valid = %t", err, tc.valid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,13 @@ package stage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/contracts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
@@ -177,16 +177,5 @@ func validateProcessedTranscriptOutput(path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return fmt.Errorf("decode json: %w", err)
|
||||
}
|
||||
segments, ok := payload["segments"]
|
||||
if !ok {
|
||||
return fmt.Errorf("top-level segments is required")
|
||||
}
|
||||
if _, ok := segments.([]any); !ok {
|
||||
return fmt.Errorf("top-level segments must be an array")
|
||||
}
|
||||
return nil
|
||||
return contracts.ValidateTranscriptEnvelope(data)
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ package stage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user