Use native Scriptorium generation and capture its output
This commit is contained in:
@@ -2,8 +2,10 @@ package scriptorium
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -60,6 +62,10 @@ func (r *SubprocessRunner) RunArtifact(ctx context.Context, req RunArtifactReque
|
||||
}
|
||||
}
|
||||
|
||||
output, err := openResultFile(req.OutputPath)
|
||||
if err != nil {
|
||||
return ArtifactResult{}, fmt.Errorf("open scriptorium output %q: %w", req.OutputPath, err)
|
||||
}
|
||||
runRes, runErr := subprocess.Run(ctx, subprocess.RunRequest{
|
||||
Executable: req.Binary,
|
||||
Args: args,
|
||||
@@ -68,13 +74,17 @@ func (r *SubprocessRunner) RunArtifact(ctx context.Context, req RunArtifactReque
|
||||
EnvOverrides: envOverrides,
|
||||
SensitiveEnvNames: sensitiveNames,
|
||||
DiagnosticOwner: "scriptorium",
|
||||
StdoutLogPath: req.StdoutLogPath,
|
||||
StdoutResult: output,
|
||||
StdoutResultLimit: MaxOutputFileBytes,
|
||||
StderrLogPath: req.StderrLogPath,
|
||||
})
|
||||
if closeErr := output.Close(); closeErr != nil {
|
||||
runErr = errors.Join(runErr, fmt.Errorf("close scriptorium output %q: %w", req.OutputPath, closeErr))
|
||||
}
|
||||
|
||||
result := ArtifactResult{
|
||||
OutputPath: req.OutputPath,
|
||||
StdoutLogPath: req.StdoutLogPath,
|
||||
StdoutLogPath: runRes.StdoutLogPath,
|
||||
StderrLogPath: req.StderrLogPath,
|
||||
GeneratedConfigPath: req.GeneratedConfigPath,
|
||||
ExitCode: runRes.ExitCode,
|
||||
@@ -92,14 +102,14 @@ func (r *SubprocessRunner) RunArtifact(ctx context.Context, req RunArtifactReque
|
||||
}
|
||||
|
||||
if runErr != nil {
|
||||
if runRes.ExitCode == 2 {
|
||||
if runRes.ExitCode == 4 {
|
||||
result.ValidationFailed = true
|
||||
exists, size, statErr := fileExistsWithSize(req.OutputPath)
|
||||
if statErr == nil {
|
||||
result.Metadata["output_exists"] = exists
|
||||
result.Metadata["output_size_bytes"] = size
|
||||
}
|
||||
return result, fmt.Errorf("scriptorium run exited with validation failure (exit code 2): %w", runErr)
|
||||
return result, fmt.Errorf("scriptorium run exited with validation failure (exit code 4): %w", runErr)
|
||||
}
|
||||
return result, fmt.Errorf("run scriptorium artifact (binary=%q): %w", req.Binary, runErr)
|
||||
}
|
||||
@@ -231,7 +241,7 @@ func validateCommonRunRequest(
|
||||
}
|
||||
|
||||
func buildRunArgs(req RunArtifactRequest) []string {
|
||||
args := []string{"run", "--prompt", strings.TrimSpace(req.PromptID)}
|
||||
args := []string{"run", strings.TrimSpace(req.PromptID)}
|
||||
if cfgPath := strings.TrimSpace(req.ConfigPath); cfgPath != "" {
|
||||
args = append(args, "--config", cfgPath)
|
||||
}
|
||||
@@ -239,16 +249,37 @@ func buildRunArgs(req RunArtifactRequest) []string {
|
||||
args = append(args, "--profile", profileID)
|
||||
}
|
||||
for _, kv := range sortedKeyValues(req.InputPaths) {
|
||||
args = append(args, "--input", kv)
|
||||
args = append(args, "--input-file", kv)
|
||||
}
|
||||
for _, kv := range sortedKeyValues(req.Vars) {
|
||||
args = append(args, "--var", kv)
|
||||
}
|
||||
args = append(args, "--timeout", req.Timeout.String())
|
||||
args = append(args, "--out", req.OutputPath)
|
||||
args = append(args, "--run-timeout", req.Timeout.String(), "--format", "text")
|
||||
return args
|
||||
}
|
||||
|
||||
func openResultFile(path string) (*os.File, error) {
|
||||
if err := fileops.EnsureWorkspaceDirectory(filepath.Dir(path)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
info, err := os.Lstat(path)
|
||||
if err == nil && !info.Mode().IsRegular() {
|
||||
return nil, fmt.Errorf("output is not a regular file")
|
||||
}
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileops.WorkspaceFileMode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := file.Chmod(fileops.WorkspaceFileMode); err != nil {
|
||||
_ = file.Close()
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func buildRenderArgs(req RenderArtifactRequest) []string {
|
||||
args := []string{"render", "--prompt", strings.TrimSpace(req.PromptID)}
|
||||
if cfgPath := strings.TrimSpace(req.ConfigPath); cfgPath != "" {
|
||||
@@ -320,6 +351,9 @@ func writeInvocationConfig(path string, payload invocationPayload) error {
|
||||
"render_format": payload.RenderFormat,
|
||||
"render_prompt_logged": payload.RenderPromptStore,
|
||||
}
|
||||
if payload.Mode == CommandModeRun {
|
||||
data["stdout_destination"] = payload.OutputPath
|
||||
}
|
||||
return writeGeneratedYAML(path, data)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user