2026-05-08 17:08:44 +00:00
|
|
|
package stage
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-12-03 02:01:48 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/v4/internal/artifacts"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/v4/internal/config"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/v4/internal/fileops"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/v4/internal/manifest"
|
2026-05-08 17:08:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type trimStage struct{}
|
|
|
|
|
|
|
|
|
|
func (trimStage) Name() string { return "trim" }
|
|
|
|
|
|
|
|
|
|
func (trimStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*StageResult, error) {
|
|
|
|
|
if env == nil || env.Config == nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: stage environment config is required")
|
|
|
|
|
}
|
|
|
|
|
if env.ArtifactStore == nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: artifact store is required")
|
|
|
|
|
}
|
|
|
|
|
if env.Config.Pipeline == nil || env.Config.Session == nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: resolved config must include pipeline and session")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sessionID string
|
|
|
|
|
if m != nil {
|
|
|
|
|
sessionID = strings.TrimSpace(m.SessionID)
|
|
|
|
|
}
|
|
|
|
|
if sessionID == "" {
|
|
|
|
|
sessionID = strings.TrimSpace(env.Config.Session.SessionID)
|
|
|
|
|
}
|
|
|
|
|
if sessionID == "" {
|
|
|
|
|
return nil, fmt.Errorf("trim: session id is required")
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 20:57:27 +00:00
|
|
|
paths := sessionPathsForEnv(env, sessionID)
|
2026-05-18 00:55:35 +00:00
|
|
|
runLayout, err := resolveRunStageLayout(env, m, paths, sessionID, "trim")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: resolve run-stage layout: %w", err)
|
|
|
|
|
}
|
2026-05-10 21:26:53 +00:00
|
|
|
normalizedPath, normalizedSource, err := discoverNormalizedTranscript(m, paths)
|
2026-05-08 17:08:44 +00:00
|
|
|
if err != nil {
|
2026-05-10 21:26:53 +00:00
|
|
|
return nil, fmt.Errorf("trim: resolve normalized transcript: %w", err)
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-05-10 21:26:53 +00:00
|
|
|
if normalizedPath == "" {
|
2026-05-10 21:41:54 +00:00
|
|
|
return nil, fmt.Errorf("trim: normalized transcript input is required; run normalize stage first")
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-05-10 21:26:53 +00:00
|
|
|
if err := validateProcessedTranscriptOutput(normalizedPath); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: normalized transcript %q invalid: %w", normalizedPath, err)
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trimCfg := env.Config.Pipeline.Trim
|
2026-05-27 08:35:02 -05:00
|
|
|
enabled := trimCfg != nil && trimCfg.Enabled != nil && *trimCfg.Enabled
|
2026-05-08 17:08:44 +00:00
|
|
|
|
2026-05-18 00:55:35 +00:00
|
|
|
canonicalTrimmedPath, err := resolveTrimmedOutputPath(paths, trimCfg)
|
2026-05-08 17:08:44 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: resolve trimmed output path: %w", err)
|
|
|
|
|
}
|
2026-05-18 00:55:35 +00:00
|
|
|
trimmedPath, err := runLocalPathForCanonical(runLayout, paths, canonicalTrimmedPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: resolve run-local trimmed output path: %w", err)
|
|
|
|
|
}
|
2026-05-08 17:08:44 +00:00
|
|
|
|
|
|
|
|
metadata := map[string]any{
|
2026-12-02 21:42:06 +00:00
|
|
|
"stage": "trim", "trim_enabled": enabled,
|
|
|
|
|
"normalized_transcript_path": normalizedPath, "normalized_transcript_source": normalizedSource,
|
|
|
|
|
"run_trimmed_output_path": trimmedPath, "trimmed_output_path": canonicalTrimmedPath,
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
var bounds *trimBoundsResult
|
|
|
|
|
var transcript *trimTranscriptResult
|
2026-05-08 17:08:44 +00:00
|
|
|
if !enabled {
|
2026-05-10 21:26:53 +00:00
|
|
|
if err := copyTranscript(env.ArtifactStore, normalizedPath, trimmedPath); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: copy normalized transcript to trimmed output: %w", err)
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
|
|
|
|
metadata["trim_action"] = "copy_disabled"
|
2026-12-02 21:42:06 +00:00
|
|
|
} else {
|
|
|
|
|
if env.Scriptorium == nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: scriptorium adapter is required when trim is enabled")
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
if env.Seriatim == nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: seriatim adapter is required when trim is enabled")
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
if env.Config.Pipeline.Scriptorium == nil {
|
|
|
|
|
return nil, fmt.Errorf("trim: pipeline.scriptorium is required when trim is enabled")
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
generated, err := generateTrimBounds(ctx, env, paths, runLayout, sessionID, normalizedPath)
|
2026-08-10 21:57:54 +00:00
|
|
|
if err != nil {
|
2026-12-02 21:42:06 +00:00
|
|
|
return nil, err
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
generated, err = selectTrimBounds(generated, normalizedPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
bounds = &generated
|
|
|
|
|
transcript, err = runTrimTranscript(ctx, env, runLayout, normalizedPath, trimmedPath, bounds.selector, bounds.copyUnchanged)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
trimmed, err := acceptTrimTranscript(env, sessionID, trimmedPath, canonicalTrimmedPath)
|
2026-05-08 17:08:44 +00:00
|
|
|
if err != nil {
|
2026-12-02 21:42:06 +00:00
|
|
|
return nil, err
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
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})
|
2026-05-08 17:08:44 +00:00
|
|
|
if err != nil {
|
2026-12-02 21:42:06 +00:00
|
|
|
return nil, fmt.Errorf("trim: materialize canonical session bounds: %w", err)
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
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...)
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-12-02 21:42:06 +00:00
|
|
|
return result, nil
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resolveTrimmedOutputPath(paths artifacts.SessionPaths, cfg *config.TrimConfig) (string, error) {
|
|
|
|
|
configured := ""
|
|
|
|
|
if cfg != nil {
|
|
|
|
|
configured = strings.TrimSpace(cfg.OutputPath)
|
|
|
|
|
}
|
|
|
|
|
if configured == "" {
|
2026-05-22 19:05:23 -05:00
|
|
|
return filepath.Join(paths.TranscriptsDir, "final.trimmed.json"), nil
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
|
|
|
|
return resolveScriptoriumOutputPath(paths, configured)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func copyTranscript(store artifacts.Store, src, dst string) error {
|
2026-08-10 18:30:28 +00:00
|
|
|
data, err := readExternalResult(src, "seriatim trimmed transcript result")
|
2026-05-08 17:08:44 +00:00
|
|
|
if err != nil {
|
2026-08-10 18:30:28 +00:00
|
|
|
return err
|
2026-05-08 17:08:44 +00:00
|
|
|
}
|
2026-08-10 17:27:00 +00:00
|
|
|
if err := store.WriteFileAtomic(dst, data, fileops.WorkspaceFileMode); err != nil {
|
2026-05-08 17:08:44 +00:00
|
|
|
return fmt.Errorf("write destination transcript: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 08:34:23 -05:00
|
|
|
func resolveSeriatimStageTimeout(raw string) (time.Duration, error) {
|
2026-05-08 17:08:44 +00:00
|
|
|
trimmed := strings.TrimSpace(raw)
|
|
|
|
|
if trimmed == "" {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
d, err := time.ParseDuration(trimmed)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("parse duration %q: %w", raw, err)
|
|
|
|
|
}
|
|
|
|
|
if d <= 0 {
|
|
|
|
|
return 0, fmt.Errorf("duration must be > 0")
|
|
|
|
|
}
|
|
|
|
|
return d, nil
|
|
|
|
|
}
|