Move chunk planning into core operation execution
This commit is contained in:
@@ -71,10 +71,7 @@ func TestNewModuleSpecAndRegister(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConstructionRejectsMissingDependencyAndUnknownOptions(t *testing.T) {
|
||||
if _, err := New(nil, Options{}); err == nil || !strings.Contains(err.Error(), "native engine") {
|
||||
t.Fatalf("New(nil) error = %v, want native engine error", err)
|
||||
}
|
||||
func TestConstructionRejectsUnknownOptions(t *testing.T) {
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil || !strings.Contains(err.Error(), "unknown option") {
|
||||
t.Fatalf("DecodeOptions() error = %v, want unknown option error", err)
|
||||
}
|
||||
@@ -129,7 +126,7 @@ func TestPlanReturnsAnnotationFreeSceneRangesFromStructuredOutput(t *testing.T)
|
||||
request := chunkRequest()
|
||||
var prepared promptkit.PreparedRun
|
||||
request.Operation.Execution.OnPrepared = func(_ context.Context, value promptkit.PreparedRun) { prepared = value }
|
||||
result, err := newChunker(t, client).Plan(context.Background(), request)
|
||||
result, err := runChunk(context.Background(), newChunker(t, client), request)
|
||||
if err != nil {
|
||||
t.Fatalf("Plan() error = %v, want nil", err)
|
||||
}
|
||||
@@ -154,24 +151,24 @@ func TestPlanReturnsAnnotationFreeSceneRangesFromStructuredOutput(t *testing.T)
|
||||
t.Fatal("full source input missing from native prompt")
|
||||
}
|
||||
|
||||
if result.Plan.SourceDigest != "sha256:source" {
|
||||
t.Fatalf("SourceDigest = %q, want source digest", result.Plan.SourceDigest)
|
||||
if result.Value.SourceDigest != "sha256:source" {
|
||||
t.Fatalf("SourceDigest = %q, want source digest", result.Value.SourceDigest)
|
||||
}
|
||||
wantRanges := []source.ChunkRange{{StartUnitID: 1, EndUnitID: 2}, {StartUnitID: 3, EndUnitID: 4}}
|
||||
if len(result.Plan.Ranges) != len(wantRanges) {
|
||||
t.Fatalf("ranges = %#v, want two", result.Plan.Ranges)
|
||||
if len(result.Value.Ranges) != len(wantRanges) {
|
||||
t.Fatalf("ranges = %#v, want two", result.Value.Ranges)
|
||||
}
|
||||
for i, want := range wantRanges {
|
||||
got := result.Plan.Ranges[i]
|
||||
got := result.Value.Ranges[i]
|
||||
if got.StartUnitID != want.StartUnitID || got.EndUnitID != want.EndUnitID {
|
||||
t.Fatalf("range[%d] = %#v, want %#v", i, result.Plan.Ranges[i], want)
|
||||
t.Fatalf("range[%d] = %#v, want %#v", i, result.Value.Ranges[i], want)
|
||||
}
|
||||
if len(got.Annotations) != 0 {
|
||||
t.Fatalf("range[%d] annotations = %#v, want absent", i, got.Annotations)
|
||||
}
|
||||
}
|
||||
if len(result.Plan.Annotations) != 0 {
|
||||
t.Fatalf("plan annotations = %#v, want absent", result.Plan.Annotations)
|
||||
if len(result.Value.Annotations) != 0 {
|
||||
t.Fatalf("plan annotations = %#v, want absent", result.Value.Annotations)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -196,11 +193,11 @@ func TestPlanUsesDocumentOrderForNonconsecutiveUnitIDs(t *testing.T) {
|
||||
scene(10, 3),
|
||||
scene(20, 20),
|
||||
}}}
|
||||
result, err := newChunker(t, client).Plan(context.Background(), req)
|
||||
result, err := runChunk(context.Background(), newChunker(t, client), req)
|
||||
if err != nil {
|
||||
t.Fatalf("Plan() error = %v, want nil", err)
|
||||
}
|
||||
if got := result.Plan.Ranges; !reflect.DeepEqual(got, []source.ChunkRange{{StartUnitID: 10, EndUnitID: 3}, {StartUnitID: 20, EndUnitID: 20}}) {
|
||||
if got := result.Value.Ranges; !reflect.DeepEqual(got, []source.ChunkRange{{StartUnitID: 10, EndUnitID: 3}, {StartUnitID: 20, EndUnitID: 20}}) {
|
||||
t.Fatalf("ranges = %#v, want source-document order", got)
|
||||
}
|
||||
})
|
||||
@@ -210,7 +207,7 @@ func TestPlanUsesDocumentOrderForNonconsecutiveUnitIDs(t *testing.T) {
|
||||
scene(3, 10),
|
||||
scene(20, 20),
|
||||
}}}
|
||||
_, err := newChunker(t, client).Plan(context.Background(), req)
|
||||
_, err := runChunk(context.Background(), newChunker(t, client), req)
|
||||
if err == nil {
|
||||
t.Fatalf("Plan() error = %v, want document-position reversal", err)
|
||||
}
|
||||
@@ -247,7 +244,7 @@ func TestPlanPassesReferencesAsPromptInputs(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := newChunker(t, client).Plan(context.Background(), req); err != nil {
|
||||
if _, err := runChunk(context.Background(), newChunker(t, client), req); err != nil {
|
||||
t.Fatalf("Plan() error = %v, want nil", err)
|
||||
}
|
||||
var messages string
|
||||
@@ -294,7 +291,7 @@ func TestPlanRejectsInvalidRequests(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
chunker *Chunker
|
||||
chunker *chunkFixture
|
||||
ctx context.Context
|
||||
req contracts.ChunkRequest
|
||||
want string
|
||||
@@ -309,7 +306,7 @@ func TestPlanRejectsInvalidRequests(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := tt.chunker.Plan(tt.ctx, tt.req)
|
||||
_, err := runChunk(tt.ctx, tt.chunker, tt.req)
|
||||
if err == nil {
|
||||
t.Fatal("Plan() error = nil, want error")
|
||||
}
|
||||
@@ -383,11 +380,11 @@ func TestPlanFromResponseRejectsMalformedSceneRanges(t *testing.T) {
|
||||
func TestPlanWrapsLLMClientError(t *testing.T) {
|
||||
client := &fakeScenesLLMClient{err: errors.New("provider unavailable")}
|
||||
|
||||
_, err := newChunker(t, client).Plan(context.Background(), chunkRequest())
|
||||
_, err := runChunk(context.Background(), newChunker(t, client), chunkRequest())
|
||||
if err == nil {
|
||||
t.Fatal("Plan() error = nil, want LLM error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "dnd scenes") || !strings.Contains(err.Error(), "provider unavailable") {
|
||||
if !strings.Contains(err.Error(), "provider unavailable") {
|
||||
t.Fatalf("Plan() error = %q, want wrapped LLM context", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -414,7 +411,12 @@ func sceneSourceInput() contracts.LLMInputMaterial {
|
||||
return contracts.NewLLMInputMaterial("source", "application/json", []byte(sceneTranscriptJSON), "sha256:transcript", "file:///session-alpha.json")
|
||||
}
|
||||
|
||||
func newChunker(t *testing.T, client promptkit.LLMClient) *Chunker {
|
||||
type chunkFixture struct {
|
||||
*Chunker
|
||||
engine *promptkit.Engine
|
||||
}
|
||||
|
||||
func newChunker(t *testing.T, client promptkit.LLMClient) *chunkFixture {
|
||||
t.Helper()
|
||||
type settings struct {
|
||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||
@@ -438,11 +440,25 @@ func newChunker(t *testing.T, client promptkit.LLMClient) *Chunker {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chunker, err := New(engine, Options{})
|
||||
chunker, err := New(Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return chunker
|
||||
return &chunkFixture{Chunker: chunker, engine: engine}
|
||||
}
|
||||
|
||||
func runChunk(ctx context.Context, fixture *chunkFixture, req contracts.ChunkRequest) (pipeline.OperationResult[source.ChunkPlan], error) {
|
||||
var chunker *Chunker
|
||||
var engine *promptkit.Engine
|
||||
if fixture != nil {
|
||||
chunker, engine = fixture.Chunker, fixture.engine
|
||||
}
|
||||
op := req.Operation
|
||||
execution := pipeline.ExecutionContext[source.ChunkPlan]{Engine: engine, Controls: op.Controls, Assess: op.Assess, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Cache: op.Cache}
|
||||
input := contracts.ChunkInput{Source: req.Source, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata}
|
||||
return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[source.ChunkPlan], error) {
|
||||
return chunker.PrepareChunk(ctx, input)
|
||||
})
|
||||
}
|
||||
|
||||
func sceneSourceDocument() *source.SourceDocument {
|
||||
|
||||
Reference in New Issue
Block a user