Separate trim bounds and transcript acceptance
This commit is contained in:
@@ -7,11 +7,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/seriatim"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/contracts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
@@ -70,281 +67,59 @@ func (trimStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*Stag
|
||||
return nil, fmt.Errorf("trim: resolve run-local trimmed output path: %w", err)
|
||||
}
|
||||
|
||||
logPaths := []string{}
|
||||
generatedConfigs := []string{}
|
||||
metadata := map[string]any{
|
||||
"stage": "trim",
|
||||
"trim_enabled": enabled,
|
||||
"normalized_transcript_path": normalizedPath,
|
||||
"normalized_transcript_source": normalizedSource,
|
||||
"run_trimmed_output_path": trimmedPath,
|
||||
"trimmed_output_path": canonicalTrimmedPath,
|
||||
"stage": "trim", "trim_enabled": enabled,
|
||||
"normalized_transcript_path": normalizedPath, "normalized_transcript_source": normalizedSource,
|
||||
"run_trimmed_output_path": trimmedPath, "trimmed_output_path": canonicalTrimmedPath,
|
||||
}
|
||||
|
||||
var bounds *trimBoundsResult
|
||||
var transcript *trimTranscriptResult
|
||||
if !enabled {
|
||||
if err := copyTranscript(env.ArtifactStore, normalizedPath, trimmedPath); err != nil {
|
||||
return nil, fmt.Errorf("trim: copy normalized transcript to trimmed output: %w", err)
|
||||
}
|
||||
if err := validateProcessedTranscriptOutput(trimmedPath); err != nil {
|
||||
return nil, fmt.Errorf("trim: copied trimmed transcript %q invalid: %w", trimmedPath, err)
|
||||
}
|
||||
materializedTrimmed, err := materializeRunLocalOutput(env.ArtifactStore, trimmedPath, canonicalTrimmedPath, artifacts.Ref{
|
||||
Kind: "transcript_final_trimmed",
|
||||
Category: "transcripts",
|
||||
SessionID: sessionID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: materialize canonical final trimmed transcript: %w", err)
|
||||
}
|
||||
metadata["trim_action"] = "copy_disabled"
|
||||
return &StageResult{
|
||||
Outputs: []artifacts.Ref{materializedTrimmed},
|
||||
Metadata: metadata,
|
||||
}, nil
|
||||
}
|
||||
|
||||
if env.Scriptorium == nil {
|
||||
return nil, fmt.Errorf("trim: scriptorium adapter is required when trim is enabled")
|
||||
}
|
||||
if env.Seriatim == nil {
|
||||
return nil, fmt.Errorf("trim: seriatim adapter is required when trim is enabled")
|
||||
}
|
||||
if env.Config.Pipeline.Scriptorium == nil {
|
||||
return nil, fmt.Errorf("trim: pipeline.scriptorium is required when trim is enabled")
|
||||
}
|
||||
|
||||
boundsCfg := trimCfg.Bounds
|
||||
canonicalBoundsOutputPath, err := resolveScriptoriumOutputPath(paths, boundsCfg.OutputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: resolve bounds output path: %w", err)
|
||||
}
|
||||
boundsOutputPath, err := runLocalPathForCanonical(runLayout, paths, canonicalBoundsOutputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: resolve run-local bounds output path: %w", err)
|
||||
}
|
||||
boundsStderrLogPath := filepath.Join(runLayout.LogsDir, "scriptorium.bounds.stderr.log")
|
||||
boundsGeneratedConfigPath := filepath.Join(runLayout.LogsDir, "scriptorium.bounds.generated.yml")
|
||||
requestContext, err := TrimScriptoriumContext(env.Config, sessionID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: resolve bounds request: %w", err)
|
||||
}
|
||||
boundsTimeout := requestContext.Timeout
|
||||
|
||||
inputPaths := map[string]string{
|
||||
boundsCfg.TranscriptInputName: normalizedPath,
|
||||
}
|
||||
vars := requestContext.Vars
|
||||
|
||||
metadata["bounds_prompt_id"] = boundsCfg.PromptID
|
||||
metadata["bounds_profile_id"] = boundsCfg.ProfileID
|
||||
metadata["run_bounds_output_path"] = boundsOutputPath
|
||||
metadata["bounds_output_path"] = canonicalBoundsOutputPath
|
||||
metadata["bounds_timeout"] = boundsTimeout.String()
|
||||
metadata["bounds_input_name"] = boundsCfg.TranscriptInputName
|
||||
metadata["bounds_input_path"] = normalizedPath
|
||||
metadata["bounds_render_debug_enabled"] = boundsCfg.RenderDebug
|
||||
|
||||
renderOutputPath := ""
|
||||
if boundsCfg.RenderDebug {
|
||||
filename := filepath.Base(strings.TrimSpace(boundsCfg.RenderOutputPath))
|
||||
if filename == "." || filename == ".." || strings.ContainsAny(filename, `/\\`) {
|
||||
return nil, fmt.Errorf("trim: bounds render output must name a file")
|
||||
}
|
||||
renderOutputPath = filepath.Join(runLayout.DebugDir, filename)
|
||||
renderStderrLogPath := filepath.Join(runLayout.LogsDir, "scriptorium.bounds.render.stderr.log")
|
||||
renderGeneratedConfigPath := filepath.Join(runLayout.LogsDir, "scriptorium.bounds.render.generated.yml")
|
||||
|
||||
renderReq := scriptorium.RenderArtifactRequest{
|
||||
Binary: requestContext.Binary,
|
||||
ConfigPath: requestContext.ConfigPath,
|
||||
Credentials: requestContext.Credentials,
|
||||
PromptID: requestContext.PromptID,
|
||||
ProfileID: requestContext.ProfileID,
|
||||
InputPaths: inputPaths,
|
||||
Vars: vars,
|
||||
OutputPath: renderOutputPath,
|
||||
StderrLogPath: renderStderrLogPath,
|
||||
GeneratedConfigPath: renderGeneratedConfigPath,
|
||||
Timeout: boundsTimeout,
|
||||
}
|
||||
renderRes, renderErr := env.Scriptorium.RenderArtifact(ctx, renderReq)
|
||||
if renderErr != nil {
|
||||
return nil, fmt.Errorf("trim: scriptorium bounds render failed: %w", renderErr)
|
||||
}
|
||||
if renderRes.ValidationFailed {
|
||||
return nil, fmt.Errorf("trim: scriptorium bounds render returned validation_failed=true")
|
||||
}
|
||||
finalRenderOutputPath, err := authoritativeOutputPath(renderReq.OutputPath, renderRes.OutputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: %w", err)
|
||||
}
|
||||
if err := requireNonEmptyFile(finalRenderOutputPath, "bounds render output"); err != nil {
|
||||
return nil, fmt.Errorf("trim: %w", err)
|
||||
}
|
||||
if err := validateJSONFile(finalRenderOutputPath); err != nil {
|
||||
return nil, fmt.Errorf("trim: bounds render diagnostics %q invalid json: %w", finalRenderOutputPath, err)
|
||||
}
|
||||
|
||||
logPaths = append(logPaths, renderStderrLogPath, finalRenderOutputPath)
|
||||
generatedConfigs = append(generatedConfigs, renderGeneratedConfigPath)
|
||||
metadata["bounds_render_output_path"] = finalRenderOutputPath
|
||||
metadata["bounds_render_stderr_log_path"] = renderStderrLogPath
|
||||
metadata["bounds_render_generated_config_path"] = renderGeneratedConfigPath
|
||||
metadata["bounds_render_adapter_exit_code"] = renderRes.ExitCode
|
||||
metadata["bounds_render_adapter_duration_ms"] = renderRes.Duration.Milliseconds()
|
||||
metadata["bounds_render_adapter_command_mode"] = renderRes.CommandMode
|
||||
if renderRes.Metadata != nil {
|
||||
metadata["bounds_render_adapter_metadata"] = renderRes.Metadata
|
||||
}
|
||||
}
|
||||
|
||||
boundsReq := scriptorium.RunArtifactRequest{
|
||||
Binary: requestContext.Binary,
|
||||
ConfigPath: requestContext.ConfigPath,
|
||||
Credentials: requestContext.Credentials,
|
||||
PromptID: requestContext.PromptID,
|
||||
ProfileID: requestContext.ProfileID,
|
||||
InputPaths: inputPaths,
|
||||
Vars: vars,
|
||||
OutputPath: boundsOutputPath,
|
||||
StderrLogPath: boundsStderrLogPath,
|
||||
GeneratedConfigPath: boundsGeneratedConfigPath,
|
||||
Timeout: boundsTimeout,
|
||||
}
|
||||
|
||||
boundsRes, boundsRunErr := env.Scriptorium.RunArtifact(ctx, boundsReq)
|
||||
if boundsRunErr != nil {
|
||||
if boundsRes.ValidationFailed {
|
||||
return nil, fmt.Errorf(
|
||||
"trim: scriptorium bounds validation failed (prompt_id=%q, output_path=%q, exit_code=%d, stderr_log=%q): %w",
|
||||
boundsReq.PromptID,
|
||||
boundsReq.OutputPath,
|
||||
boundsRes.ExitCode,
|
||||
coalesceString(boundsRes.StderrLogPath, boundsReq.StderrLogPath),
|
||||
boundsRunErr,
|
||||
)
|
||||
}
|
||||
return nil, fmt.Errorf("trim: scriptorium bounds generation failed: %w", boundsRunErr)
|
||||
}
|
||||
if boundsRes.ValidationFailed {
|
||||
return nil, fmt.Errorf("trim: scriptorium bounds run returned validation_failed=true")
|
||||
}
|
||||
|
||||
finalBoundsOutputPath, err := authoritativeOutputPath(boundsReq.OutputPath, boundsRes.OutputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: %w", err)
|
||||
}
|
||||
if err := requireNonEmptyFile(finalBoundsOutputPath, "session bounds output"); err != nil {
|
||||
return nil, fmt.Errorf("trim: %w", err)
|
||||
}
|
||||
|
||||
boundsPayload, err := contracts.ParseSessionBoundsFile(finalBoundsOutputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: parse bounds output %q: %w", finalBoundsOutputPath, err)
|
||||
}
|
||||
if err := contracts.ValidateSessionBoundsAgainstTranscript(boundsPayload, normalizedPath); err != nil {
|
||||
return nil, fmt.Errorf("trim: validate bounds output %q against transcript %q: %w", finalBoundsOutputPath, normalizedPath, err)
|
||||
}
|
||||
|
||||
keepSelector, copyUnchanged, err := contracts.BuildSeriatimKeepSelector(boundsPayload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: build keep selector from bounds output: %w", err)
|
||||
}
|
||||
trimAction := strings.ToLower(strings.TrimSpace(boundsPayload.TrimAction))
|
||||
if trimAction == "" {
|
||||
trimAction = "trim"
|
||||
}
|
||||
|
||||
logPaths = append(logPaths, boundsStderrLogPath)
|
||||
generatedConfigs = append(generatedConfigs, boundsGeneratedConfigPath)
|
||||
metadata["bounds_confidence"] = boundsPayload.Confidence
|
||||
metadata["trim_action"] = trimAction
|
||||
metadata["start_segment_id"] = boundsPayload.StartSegmentID
|
||||
metadata["end_segment_id"] = boundsPayload.EndSegmentID
|
||||
metadata["warnings"] = boundsPayload.Warnings
|
||||
metadata["keep_selector"] = keepSelector
|
||||
metadata["run_bounds_output_path"] = finalBoundsOutputPath
|
||||
metadata["bounds_stderr_log_path"] = boundsStderrLogPath
|
||||
metadata["bounds_generated_config_path"] = boundsGeneratedConfigPath
|
||||
metadata["bounds_adapter_exit_code"] = boundsRes.ExitCode
|
||||
metadata["bounds_adapter_duration_ms"] = boundsRes.Duration.Milliseconds()
|
||||
metadata["bounds_adapter_command_mode"] = boundsRes.CommandMode
|
||||
metadata["bounds_adapter_prompt_id"] = boundsRes.PromptID
|
||||
metadata["bounds_adapter_profile_id"] = boundsRes.ProfileID
|
||||
metadata["bounds_adapter_output_path"] = boundsRes.OutputPath
|
||||
metadata["bounds_adapter_generated_config"] = boundsRes.GeneratedConfigPath
|
||||
metadata["bounds_adapter_stderr_log_path"] = boundsRes.StderrLogPath
|
||||
if boundsRes.Metadata != nil {
|
||||
metadata["bounds_adapter_metadata"] = boundsRes.Metadata
|
||||
}
|
||||
|
||||
if copyUnchanged {
|
||||
if err := copyTranscript(env.ArtifactStore, normalizedPath, trimmedPath); err != nil {
|
||||
return nil, fmt.Errorf("trim: copy normalized transcript to trimmed output: %w", err)
|
||||
}
|
||||
} else {
|
||||
trimStdoutLogPath := filepath.Join(runLayout.LogsDir, "seriatim.trim.stdout.log")
|
||||
trimStderrLogPath := filepath.Join(runLayout.LogsDir, "seriatim.trim.stderr.log")
|
||||
trimGeneratedConfigPath := filepath.Join(runLayout.LogsDir, "seriatim.trim.generated.yml")
|
||||
trimTimeout, err := resolveSeriatimStageTimeout(env.Config.Pipeline.Seriatim.Timeout)
|
||||
if env.Scriptorium == nil {
|
||||
return nil, fmt.Errorf("trim: scriptorium adapter is required when trim is enabled")
|
||||
}
|
||||
if env.Seriatim == nil {
|
||||
return nil, fmt.Errorf("trim: seriatim adapter is required when trim is enabled")
|
||||
}
|
||||
if env.Config.Pipeline.Scriptorium == nil {
|
||||
return nil, fmt.Errorf("trim: pipeline.scriptorium is required when trim is enabled")
|
||||
}
|
||||
generated, err := generateTrimBounds(ctx, env, paths, runLayout, sessionID, normalizedPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: resolve seriatim timeout: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
trimReq := seriatim.TrimRequest{
|
||||
Binary: env.Config.Pipeline.Seriatim.Binary,
|
||||
InputTranscriptPath: normalizedPath,
|
||||
OutputTrimmedPath: trimmedPath,
|
||||
KeepSelector: keepSelector,
|
||||
StdoutLogPath: trimStdoutLogPath,
|
||||
StderrLogPath: trimStderrLogPath,
|
||||
GeneratedConfigPath: trimGeneratedConfigPath,
|
||||
Timeout: trimTimeout,
|
||||
generated, err = selectTrimBounds(generated, normalizedPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
trimRes, trimErr := env.Seriatim.Trim(ctx, trimReq)
|
||||
if trimErr != nil {
|
||||
return nil, fmt.Errorf("trim: seriatim trim failed: %w", trimErr)
|
||||
}
|
||||
logPaths = append(logPaths, trimStdoutLogPath, trimStderrLogPath)
|
||||
generatedConfigs = append(generatedConfigs, trimGeneratedConfigPath)
|
||||
metadata["seriatim_trim_stdout_log_path"] = trimStdoutLogPath
|
||||
metadata["seriatim_trim_stderr_log_path"] = trimStderrLogPath
|
||||
metadata["seriatim_trim_generated_config_path"] = trimGeneratedConfigPath
|
||||
metadata["seriatim_trim_exit_code"] = trimRes.ExitCode
|
||||
metadata["seriatim_trim_duration_ms"] = trimRes.Duration.Milliseconds()
|
||||
metadata["seriatim_trim_invoked_binary"] = trimRes.InvokedBinary
|
||||
metadata["seriatim_trim_keep_selector"] = trimRes.KeepSelector
|
||||
if trimRes.Metadata != nil {
|
||||
metadata["seriatim_trim_metadata"] = trimRes.Metadata
|
||||
bounds = &generated
|
||||
transcript, err = runTrimTranscript(ctx, env, runLayout, normalizedPath, trimmedPath, bounds.selector, bounds.copyUnchanged)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateProcessedTranscriptOutput(trimmedPath); err != nil {
|
||||
return nil, fmt.Errorf("trim: trimmed transcript %q invalid: %w", trimmedPath, err)
|
||||
}
|
||||
|
||||
materializedTrimmed, err := materializeRunLocalOutput(env.ArtifactStore, trimmedPath, canonicalTrimmedPath, artifacts.Ref{
|
||||
Kind: "transcript_final_trimmed",
|
||||
Category: "transcripts",
|
||||
SessionID: sessionID,
|
||||
})
|
||||
trimmed, err := acceptTrimTranscript(env, sessionID, trimmedPath, canonicalTrimmedPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: materialize canonical final trimmed transcript: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
materializedBounds, err := materializeRunLocalOutput(env.ArtifactStore, finalBoundsOutputPath, canonicalBoundsOutputPath, artifacts.Ref{
|
||||
Kind: "session_bounds",
|
||||
Category: "artifacts",
|
||||
SessionID: sessionID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: materialize canonical session bounds: %w", err)
|
||||
result := &StageResult{Outputs: []artifacts.Ref{trimmed}, Metadata: metadata}
|
||||
if bounds != nil {
|
||||
materialized, err := materializeRunLocalOutput(env.ArtifactStore, bounds.outputPath, bounds.canonicalPath, artifacts.Ref{Kind: "session_bounds", Category: "artifacts", SessionID: sessionID})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: materialize canonical session bounds: %w", err)
|
||||
}
|
||||
result.Outputs = append(result.Outputs, materialized)
|
||||
result.Logs, result.GeneratedConfigs = bounds.appendDiagnostics(metadata)
|
||||
logs, configs := transcript.appendDiagnostics(metadata)
|
||||
result.Logs = append(result.Logs, logs...)
|
||||
result.GeneratedConfigs = append(result.GeneratedConfigs, configs...)
|
||||
}
|
||||
|
||||
return &StageResult{
|
||||
Outputs: []artifacts.Ref{materializedTrimmed, materializedBounds},
|
||||
Logs: logPaths,
|
||||
GeneratedConfigs: generatedConfigs,
|
||||
Metadata: metadata,
|
||||
}, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func resolveTrimmedOutputPath(paths artifacts.SessionPaths, cfg *config.TrimConfig) (string, error) {
|
||||
|
||||
213
internal/stage/trim_operations.go
Normal file
213
internal/stage/trim_operations.go
Normal file
@@ -0,0 +1,213 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/seriatim"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/contracts"
|
||||
)
|
||||
|
||||
type trimBoundsResult struct {
|
||||
canonicalPath, outputPath, stderrPath, generatedPath, selector, action string
|
||||
payload contracts.SessionBounds
|
||||
copyUnchanged bool
|
||||
request scriptorium.RunArtifactRequest
|
||||
result scriptorium.ArtifactResult
|
||||
render *trimRenderResult
|
||||
}
|
||||
type trimRenderResult struct {
|
||||
outputPath, stderrPath, generatedPath string
|
||||
result scriptorium.ArtifactResult
|
||||
}
|
||||
type trimTranscriptResult struct {
|
||||
request seriatim.TrimRequest
|
||||
result seriatim.TrimResult
|
||||
}
|
||||
|
||||
func generateTrimBounds(ctx context.Context, env *Env, paths artifacts.SessionPaths, layout runStageLayout, sessionID, normalized string) (trimBoundsResult, error) {
|
||||
cfg := env.Config.Pipeline.Trim.Bounds
|
||||
var out trimBoundsResult
|
||||
var err error
|
||||
out.canonicalPath, err = resolveScriptoriumOutputPath(paths, cfg.OutputPath)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("trim: resolve bounds output path: %w", err)
|
||||
}
|
||||
runPath, err := runLocalPathForCanonical(layout, paths, out.canonicalPath)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("trim: resolve run-local bounds output path: %w", err)
|
||||
}
|
||||
out.stderrPath = filepath.Join(layout.LogsDir, "scriptorium.bounds.stderr.log")
|
||||
out.generatedPath = filepath.Join(layout.LogsDir, "scriptorium.bounds.generated.yml")
|
||||
requestContext, err := TrimScriptoriumContext(env.Config, sessionID)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("trim: resolve bounds request: %w", err)
|
||||
}
|
||||
inputs := map[string]string{cfg.TranscriptInputName: normalized}
|
||||
if cfg.RenderDebug {
|
||||
filename := filepath.Base(strings.TrimSpace(cfg.RenderOutputPath))
|
||||
if filename == "." || filename == ".." || strings.ContainsAny(filename, `/\`) {
|
||||
return out, fmt.Errorf("trim: bounds render output must name a file")
|
||||
}
|
||||
render := &trimRenderResult{outputPath: filepath.Join(layout.DebugDir, filename), stderrPath: filepath.Join(layout.LogsDir, "scriptorium.bounds.render.stderr.log"), generatedPath: filepath.Join(layout.LogsDir, "scriptorium.bounds.render.generated.yml")}
|
||||
req := scriptorium.RenderArtifactRequest{Binary: requestContext.Binary, ConfigPath: requestContext.ConfigPath, Credentials: requestContext.Credentials, PromptID: requestContext.PromptID, ProfileID: requestContext.ProfileID, InputPaths: inputs, Vars: requestContext.Vars, OutputPath: render.outputPath, StderrLogPath: render.stderrPath, GeneratedConfigPath: render.generatedPath, Timeout: requestContext.Timeout}
|
||||
render.result, err = env.Scriptorium.RenderArtifact(ctx, req)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("trim: scriptorium bounds render failed: %w", err)
|
||||
}
|
||||
if render.result.ValidationFailed {
|
||||
return out, fmt.Errorf("trim: scriptorium bounds render returned validation_failed=true")
|
||||
}
|
||||
render.outputPath, err = authoritativeOutputPath(req.OutputPath, render.result.OutputPath)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("trim: %w", err)
|
||||
}
|
||||
if err := requireNonEmptyFile(render.outputPath, "bounds render output"); err != nil {
|
||||
return out, fmt.Errorf("trim: %w", err)
|
||||
}
|
||||
if err := validateJSONFile(render.outputPath); err != nil {
|
||||
return out, fmt.Errorf("trim: bounds render diagnostics %q invalid json: %w", render.outputPath, err)
|
||||
}
|
||||
out.render = render
|
||||
}
|
||||
out.request = scriptorium.RunArtifactRequest{Binary: requestContext.Binary, ConfigPath: requestContext.ConfigPath, Credentials: requestContext.Credentials, PromptID: requestContext.PromptID, ProfileID: requestContext.ProfileID, InputPaths: inputs, Vars: requestContext.Vars, OutputPath: runPath, StderrLogPath: out.stderrPath, GeneratedConfigPath: out.generatedPath, Timeout: requestContext.Timeout}
|
||||
out.result, err = env.Scriptorium.RunArtifact(ctx, out.request)
|
||||
if err != nil {
|
||||
if out.result.ValidationFailed {
|
||||
return out, fmt.Errorf("trim: scriptorium bounds validation failed (prompt_id=%q, output_path=%q, exit_code=%d, stderr_log=%q): %w", out.request.PromptID, out.request.OutputPath, out.result.ExitCode, coalesceString(out.result.StderrLogPath, out.request.StderrLogPath), err)
|
||||
}
|
||||
return out, fmt.Errorf("trim: scriptorium bounds generation failed: %w", err)
|
||||
}
|
||||
if out.result.ValidationFailed {
|
||||
return out, fmt.Errorf("trim: scriptorium bounds run returned validation_failed=true")
|
||||
}
|
||||
out.outputPath, err = authoritativeOutputPath(out.request.OutputPath, out.result.OutputPath)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("trim: %w", err)
|
||||
}
|
||||
if err := requireNonEmptyFile(out.outputPath, "session bounds output"); err != nil {
|
||||
return out, fmt.Errorf("trim: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func selectTrimBounds(out trimBoundsResult, normalized string) (trimBoundsResult, error) {
|
||||
var err error
|
||||
out.payload, err = contracts.ParseSessionBoundsFile(out.outputPath)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("trim: parse bounds output %q: %w", out.outputPath, err)
|
||||
}
|
||||
if err := contracts.ValidateSessionBoundsAgainstTranscript(out.payload, normalized); err != nil {
|
||||
return out, fmt.Errorf("trim: validate bounds output %q against transcript %q: %w", out.outputPath, normalized, err)
|
||||
}
|
||||
out.selector, out.copyUnchanged, err = contracts.BuildSeriatimKeepSelector(out.payload)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("trim: build keep selector from bounds output: %w", err)
|
||||
}
|
||||
out.action = strings.ToLower(strings.TrimSpace(out.payload.TrimAction))
|
||||
if out.action == "" {
|
||||
out.action = "trim"
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func runTrimTranscript(ctx context.Context, env *Env, layout runStageLayout, normalized, output, selector string, copyUnchanged bool) (*trimTranscriptResult, error) {
|
||||
if copyUnchanged {
|
||||
if err := copyTranscript(env.ArtifactStore, normalized, output); err != nil {
|
||||
return nil, fmt.Errorf("trim: copy normalized transcript to trimmed output: %w", err)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
timeout, err := resolveSeriatimStageTimeout(env.Config.Pipeline.Seriatim.Timeout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: resolve seriatim timeout: %w", err)
|
||||
}
|
||||
req := seriatim.TrimRequest{Binary: env.Config.Pipeline.Seriatim.Binary, InputTranscriptPath: normalized, OutputTrimmedPath: output, KeepSelector: selector, StdoutLogPath: filepath.Join(layout.LogsDir, "seriatim.trim.stdout.log"), StderrLogPath: filepath.Join(layout.LogsDir, "seriatim.trim.stderr.log"), GeneratedConfigPath: filepath.Join(layout.LogsDir, "seriatim.trim.generated.yml"), Timeout: timeout}
|
||||
result, err := env.Seriatim.Trim(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("trim: seriatim trim failed: %w", err)
|
||||
}
|
||||
return &trimTranscriptResult{request: req, result: result}, nil
|
||||
}
|
||||
|
||||
func acceptTrimTranscript(env *Env, sessionID, runPath, canonicalPath string) (artifacts.Ref, error) {
|
||||
if err := validateProcessedTranscriptOutput(runPath); err != nil {
|
||||
return artifacts.Ref{}, fmt.Errorf("trim: trimmed transcript %q invalid: %w", runPath, err)
|
||||
}
|
||||
result, err := materializeRunLocalOutput(env.ArtifactStore, runPath, canonicalPath, artifacts.Ref{Kind: "transcript_final_trimmed", Category: "transcripts", SessionID: sessionID})
|
||||
if err != nil {
|
||||
return artifacts.Ref{}, fmt.Errorf("trim: materialize canonical final trimmed transcript: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (b trimBoundsResult) appendDiagnostics(metadata map[string]any) ([]string, []string) {
|
||||
cfg := b.request
|
||||
metadata["bounds_prompt_id"] = cfg.PromptID
|
||||
metadata["bounds_profile_id"] = cfg.ProfileID
|
||||
metadata["run_bounds_output_path"] = b.outputPath
|
||||
metadata["bounds_output_path"] = b.canonicalPath
|
||||
metadata["bounds_timeout"] = cfg.Timeout.String()
|
||||
// The request contains exactly the configured transcript binding.
|
||||
for name, path := range cfg.InputPaths {
|
||||
metadata["bounds_input_name"], metadata["bounds_input_path"] = name, path
|
||||
}
|
||||
metadata["bounds_render_debug_enabled"] = b.render != nil
|
||||
var logs, generated []string
|
||||
if r := b.render; r != nil {
|
||||
logs = append(logs, r.stderrPath, r.outputPath)
|
||||
generated = append(generated, r.generatedPath)
|
||||
metadata["bounds_render_output_path"] = r.outputPath
|
||||
metadata["bounds_render_stderr_log_path"] = r.stderrPath
|
||||
metadata["bounds_render_generated_config_path"] = r.generatedPath
|
||||
metadata["bounds_render_adapter_exit_code"] = r.result.ExitCode
|
||||
metadata["bounds_render_adapter_duration_ms"] = r.result.Duration.Milliseconds()
|
||||
metadata["bounds_render_adapter_command_mode"] = r.result.CommandMode
|
||||
if r.result.Metadata != nil {
|
||||
metadata["bounds_render_adapter_metadata"] = r.result.Metadata
|
||||
}
|
||||
}
|
||||
logs = append(logs, b.stderrPath)
|
||||
generated = append(generated, b.generatedPath)
|
||||
metadata["bounds_confidence"] = b.payload.Confidence
|
||||
metadata["trim_action"] = b.action
|
||||
metadata["start_segment_id"] = b.payload.StartSegmentID
|
||||
metadata["end_segment_id"] = b.payload.EndSegmentID
|
||||
metadata["warnings"] = b.payload.Warnings
|
||||
metadata["keep_selector"] = b.selector
|
||||
metadata["bounds_stderr_log_path"] = b.stderrPath
|
||||
metadata["bounds_generated_config_path"] = b.generatedPath
|
||||
metadata["bounds_adapter_exit_code"] = b.result.ExitCode
|
||||
metadata["bounds_adapter_duration_ms"] = b.result.Duration.Milliseconds()
|
||||
metadata["bounds_adapter_command_mode"] = b.result.CommandMode
|
||||
metadata["bounds_adapter_prompt_id"] = b.result.PromptID
|
||||
metadata["bounds_adapter_profile_id"] = b.result.ProfileID
|
||||
metadata["bounds_adapter_output_path"] = b.result.OutputPath
|
||||
metadata["bounds_adapter_generated_config"] = b.result.GeneratedConfigPath
|
||||
metadata["bounds_adapter_stderr_log_path"] = b.result.StderrLogPath
|
||||
if b.result.Metadata != nil {
|
||||
metadata["bounds_adapter_metadata"] = b.result.Metadata
|
||||
}
|
||||
return logs, generated
|
||||
}
|
||||
|
||||
func (r *trimTranscriptResult) appendDiagnostics(metadata map[string]any) ([]string, []string) {
|
||||
if r == nil {
|
||||
return nil, nil
|
||||
}
|
||||
metadata["seriatim_trim_stdout_log_path"] = r.request.StdoutLogPath
|
||||
metadata["seriatim_trim_stderr_log_path"] = r.request.StderrLogPath
|
||||
metadata["seriatim_trim_generated_config_path"] = r.request.GeneratedConfigPath
|
||||
metadata["seriatim_trim_exit_code"] = r.result.ExitCode
|
||||
metadata["seriatim_trim_duration_ms"] = r.result.Duration.Milliseconds()
|
||||
metadata["seriatim_trim_invoked_binary"] = r.result.InvokedBinary
|
||||
metadata["seriatim_trim_keep_selector"] = r.result.KeepSelector
|
||||
if r.result.Metadata != nil {
|
||||
metadata["seriatim_trim_metadata"] = r.result.Metadata
|
||||
}
|
||||
return []string{r.request.StdoutLogPath, r.request.StderrLogPath}, []string{r.request.GeneratedConfigPath}
|
||||
}
|
||||
Reference in New Issue
Block a user