Share prepared pipeline source validation
This commit is contained in:
@@ -118,6 +118,8 @@ extraction to Stage 2.
|
||||
|
||||
## Stage 2: Expose Shared Source And External-Reference Checks
|
||||
|
||||
**Status:** Complete.
|
||||
|
||||
**Goal:** Preflight can validate actual local inputs without running a pipeline.
|
||||
|
||||
**Inspect:** `internal/framework/pipeline/{prepare,references,runner_boundaries}.go`,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -10,6 +11,30 @@ import (
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
// ParseSource parses and validates input using the prepared input adapter.
|
||||
func (prepared *PreparedPipeline) ParseSource(ctx context.Context, request contracts.ParseRequest) (*source.SourceDocument, error) {
|
||||
metadata, err := cloneMetadata(request.Metadata)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("clone input adapter metadata: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request.Metadata = metadata
|
||||
request.LLMProfile = prepared.resolved.Input.LLMProfile
|
||||
doc, err := prepared.input.Parse(ctx, request)
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return nil, ctxErr
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse input with adapter %q: %w", prepared.input.Key(), err)
|
||||
}
|
||||
if err := source.ValidateDocument(doc); err != nil {
|
||||
return nil, fmt.Errorf("validate source document: %w", err)
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
// PreparedPipeline owns the constructed, run-local implementation set for one
|
||||
// resolved pipeline. Its implementation values are private so execution cannot
|
||||
// replace or reconfigure them after preparation.
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
func prepareRunSource(ctx context.Context, input *RunInput) (*source.SourceDocument, error) {
|
||||
debugRecorder := input.Debug
|
||||
adapter := input.Prepared.input
|
||||
var doc *source.SourceDocument
|
||||
sourceStarted := time.Now().UTC()
|
||||
if debugRecorder.Enabled() {
|
||||
if err := writeDebugTimed(debugRecorder, "source/input.json", debugTimedEnvelope{
|
||||
@@ -30,25 +29,12 @@ func prepareRunSource(ctx context.Context, input *RunInput) (*source.SourceDocum
|
||||
return nil, fmt.Errorf("write source debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
requestMetadata, err := cloneMetadata(input.Metadata)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("clone input adapter metadata: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
doc, err = adapter.Parse(ctx, contracts.ParseRequest{
|
||||
doc, err := input.Prepared.ParseSource(ctx, contracts.ParseRequest{
|
||||
SourceID: input.SourceID, Path: input.Path, Raw: input.RawInput,
|
||||
LLMProfile: input.pipeline.Input.LLMProfile, Metadata: requestMetadata,
|
||||
Metadata: input.Metadata,
|
||||
})
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return nil, ctxErr
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse input with adapter %q: %w", adapter.Key(), err)
|
||||
}
|
||||
if err := source.ValidateDocument(doc); err != nil {
|
||||
return nil, fmt.Errorf("validate source document: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
index := source.NewDocumentIndex(doc)
|
||||
input.sourceIndex = &index
|
||||
|
||||
48
internal/framework/pipeline/source_preparation_test.go
Normal file
48
internal/framework/pipeline/source_preparation_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
type sourcePreparationAdapter struct {
|
||||
doc *source.SourceDocument
|
||||
err error
|
||||
calls int
|
||||
}
|
||||
|
||||
func (*sourcePreparationAdapter) Key() string { return "test/input" }
|
||||
|
||||
func (adapter *sourcePreparationAdapter) Parse(context.Context, contracts.ParseRequest) (*source.SourceDocument, error) {
|
||||
adapter.calls++
|
||||
return adapter.doc, adapter.err
|
||||
}
|
||||
|
||||
func TestPreparedPipelineParsesAndValidatesSource(t *testing.T) {
|
||||
adapter := &sourcePreparationAdapter{doc: typedTestDocumentWithUnits(1)}
|
||||
prepared := &PreparedPipeline{input: adapter}
|
||||
doc, err := prepared.ParseSource(context.Background(), contracts.ParseRequest{})
|
||||
if err != nil || doc != adapter.doc || adapter.calls != 1 {
|
||||
t.Fatalf("ParseSource() = %v, %v; calls = %d", doc, err, adapter.calls)
|
||||
}
|
||||
adapter.doc = &source.SourceDocument{}
|
||||
if _, err := prepared.ParseSource(context.Background(), contracts.ParseRequest{}); err == nil || !strings.Contains(err.Error(), "validate source document") {
|
||||
t.Fatalf("invalid document error = %v", err)
|
||||
}
|
||||
cause := errors.New("adapter failure")
|
||||
adapter.err = cause
|
||||
if _, err := prepared.ParseSource(context.Background(), contracts.ParseRequest{}); !errors.Is(err, cause) {
|
||||
t.Fatalf("adapter error = %v, want original cause", err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
calls := adapter.calls
|
||||
if _, err := prepared.ParseSource(ctx, contracts.ParseRequest{}); !errors.Is(err, context.Canceled) || adapter.calls != calls {
|
||||
t.Fatalf("canceled parse error = %v; calls = %d", err, adapter.calls)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user