Use native prepared previews and correct diagnostic references
This commit is contained in:
@@ -21,7 +21,6 @@ func (n *NoopRunner) RunArtifact(ctx context.Context, req RunArtifactRequest) (A
|
||||
}
|
||||
return ArtifactResult{
|
||||
OutputPath: req.OutputPath,
|
||||
StdoutLogPath: req.StdoutLogPath,
|
||||
StderrLogPath: req.StderrLogPath,
|
||||
GeneratedConfigPath: req.GeneratedConfigPath,
|
||||
CommandMode: CommandModeRun,
|
||||
@@ -41,7 +40,6 @@ func (n *NoopRunner) RenderArtifact(ctx context.Context, req RenderArtifactReque
|
||||
}
|
||||
return ArtifactResult{
|
||||
OutputPath: req.OutputPath,
|
||||
StdoutLogPath: req.StdoutLogPath,
|
||||
StderrLogPath: req.StderrLogPath,
|
||||
GeneratedConfigPath: req.GeneratedConfigPath,
|
||||
CommandMode: CommandModeRender,
|
||||
@@ -78,9 +76,6 @@ func (f *FakeRunner) RunArtifact(ctx context.Context, req RunArtifactRequest) (A
|
||||
if res.OutputPath == "" {
|
||||
res.OutputPath = req.OutputPath
|
||||
}
|
||||
if res.StdoutLogPath == "" {
|
||||
res.StdoutLogPath = req.StdoutLogPath
|
||||
}
|
||||
if res.StderrLogPath == "" {
|
||||
res.StderrLogPath = req.StderrLogPath
|
||||
}
|
||||
@@ -119,9 +114,6 @@ func (f *FakeRunner) RenderArtifact(ctx context.Context, req RenderArtifactReque
|
||||
if res.OutputPath == "" {
|
||||
res.OutputPath = req.OutputPath
|
||||
}
|
||||
if res.StdoutLogPath == "" {
|
||||
res.StdoutLogPath = req.StdoutLogPath
|
||||
}
|
||||
if res.StderrLogPath == "" {
|
||||
res.StderrLogPath = req.StderrLogPath
|
||||
}
|
||||
@@ -161,11 +153,6 @@ func materializeRunPlaceholders(req RunArtifactRequest) error {
|
||||
return fmt.Errorf("write generated config %q: %w", req.GeneratedConfigPath, err)
|
||||
}
|
||||
}
|
||||
if req.StdoutLogPath != "" {
|
||||
if err := fileops.WriteFileAtomic(req.StdoutLogPath, []byte("scriptorium noop/fake run stdout placeholder\n"), fileops.WorkspaceFileMode); err != nil {
|
||||
return fmt.Errorf("write stdout log %q: %w", req.StdoutLogPath, err)
|
||||
}
|
||||
}
|
||||
if req.StderrLogPath != "" {
|
||||
if err := fileops.WriteFileAtomic(req.StderrLogPath, []byte("scriptorium noop/fake run stderr placeholder\n"), fileops.WorkspaceFileMode); err != nil {
|
||||
return fmt.Errorf("write stderr log %q: %w", req.StderrLogPath, err)
|
||||
@@ -176,7 +163,7 @@ func materializeRunPlaceholders(req RunArtifactRequest) error {
|
||||
|
||||
func materializeRenderPlaceholders(req RenderArtifactRequest) error {
|
||||
if req.OutputPath != "" {
|
||||
if err := fileops.WriteFileAtomic(req.OutputPath, []byte("{\"schema\":\"scriptorium.render.v1\",\"placeholder\":true}\n"), fileops.WorkspaceFileMode); err != nil {
|
||||
if err := fileops.WriteFileAtomic(req.OutputPath, []byte("{\"placeholder\":true}\n"), fileops.WorkspaceFileMode); err != nil {
|
||||
return fmt.Errorf("write render output %q: %w", req.OutputPath, err)
|
||||
}
|
||||
}
|
||||
@@ -192,11 +179,6 @@ func materializeRenderPlaceholders(req RenderArtifactRequest) error {
|
||||
return fmt.Errorf("write generated config %q: %w", req.GeneratedConfigPath, err)
|
||||
}
|
||||
}
|
||||
if req.StdoutLogPath != "" {
|
||||
if err := fileops.WriteFileAtomic(req.StdoutLogPath, []byte("scriptorium noop/fake render stdout placeholder\n"), fileops.WorkspaceFileMode); err != nil {
|
||||
return fmt.Errorf("write stdout log %q: %w", req.StdoutLogPath, err)
|
||||
}
|
||||
}
|
||||
if req.StderrLogPath != "" {
|
||||
if err := fileops.WriteFileAtomic(req.StderrLogPath, []byte("scriptorium noop/fake render stderr placeholder\n"), fileops.WorkspaceFileMode); err != nil {
|
||||
return fmt.Errorf("write stderr log %q: %w", req.StderrLogPath, err)
|
||||
|
||||
@@ -11,8 +11,8 @@ import (
|
||||
const (
|
||||
// CommandModeRun is the Scriptorium CLI mode for production generation.
|
||||
CommandModeRun = "run"
|
||||
// CommandModeRender is the Scriptorium CLI mode for debug/test rendering.
|
||||
CommandModeRender = "render"
|
||||
// CommandModeRender identifies the prepared request inspection command.
|
||||
CommandModeRender = "promptkit show prepared"
|
||||
)
|
||||
|
||||
// Runner is the adapter boundary for Scriptorium artifact invocations.
|
||||
|
||||
@@ -2,6 +2,7 @@ package scriptorium
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -121,7 +122,7 @@ func (r *SubprocessRunner) RunArtifact(ctx context.Context, req RunArtifactReque
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// RenderArtifact executes `scriptorium render` with JSON output.
|
||||
// RenderArtifact captures the native prepared request as JSON.
|
||||
func (r *SubprocessRunner) RenderArtifact(ctx context.Context, req RenderArtifactRequest) (ArtifactResult, error) {
|
||||
if r == nil {
|
||||
return ArtifactResult{}, fmt.Errorf("scriptorium subprocess runner is nil")
|
||||
@@ -158,6 +159,10 @@ func (r *SubprocessRunner) RenderArtifact(ctx context.Context, req RenderArtifac
|
||||
}
|
||||
}
|
||||
|
||||
output, err := openResultFile(req.OutputPath)
|
||||
if err != nil {
|
||||
return ArtifactResult{}, fmt.Errorf("open scriptorium preview %q: %w", req.OutputPath, err)
|
||||
}
|
||||
runRes, runErr := subprocess.Run(ctx, subprocess.RunRequest{
|
||||
Executable: req.Binary,
|
||||
Args: args,
|
||||
@@ -166,13 +171,17 @@ func (r *SubprocessRunner) RenderArtifact(ctx context.Context, req RenderArtifac
|
||||
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 preview %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,
|
||||
@@ -196,6 +205,13 @@ func (r *SubprocessRunner) RenderArtifact(ctx context.Context, req RenderArtifac
|
||||
if err := validateNonEmptyOutput(req.OutputPath); err != nil {
|
||||
return result, fmt.Errorf("validate scriptorium render output %q: %w", req.OutputPath, err)
|
||||
}
|
||||
data, err := fileops.ReadFileBounded(req.OutputPath, MaxOutputFileBytes)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("read scriptorium render output %q: %w", req.OutputPath, err)
|
||||
}
|
||||
if !json.Valid(data) {
|
||||
return result, fmt.Errorf("validate scriptorium render output %q: invalid JSON", req.OutputPath)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -281,7 +297,7 @@ func openResultFile(path string) (*os.File, error) {
|
||||
}
|
||||
|
||||
func buildRenderArgs(req RenderArtifactRequest) []string {
|
||||
args := []string{"render", "--prompt", strings.TrimSpace(req.PromptID)}
|
||||
args := []string{"promptkit", "show", "prepared", strings.TrimSpace(req.PromptID)}
|
||||
if cfgPath := strings.TrimSpace(req.ConfigPath); cfgPath != "" {
|
||||
args = append(args, "--config", cfgPath)
|
||||
}
|
||||
@@ -289,14 +305,12 @@ func buildRenderArgs(req RenderArtifactRequest) []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, "--format", "json")
|
||||
args = append(args, "--out", req.OutputPath)
|
||||
return args
|
||||
}
|
||||
|
||||
@@ -351,9 +365,7 @@ 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
|
||||
}
|
||||
data["stdout_destination"] = payload.OutputPath
|
||||
return writeGeneratedYAML(path, data)
|
||||
}
|
||||
|
||||
|
||||
@@ -182,6 +182,20 @@ func TestSubprocessRunnerRunExitCodeFourReturnsValidationFailureAndPreservesOutp
|
||||
assertFileNonEmpty(t, req.OutputPath)
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerRenderRejectsInvalidOrFailedPreview(t *testing.T) {
|
||||
for _, mode := range []string{"render_invalid", "render_failure"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
|
||||
t.Setenv("SCRIPTORIUM_HELPER_MODE", mode)
|
||||
dir := t.TempDir()
|
||||
req := RenderArtifactRequest{Binary: writeScriptoriumHelperWrapper(t), PromptID: "example", OutputPath: filepath.Join(dir, "preview.json"), StderrLogPath: filepath.Join(dir, "stderr.log"), Timeout: 2 * time.Second}
|
||||
if _, err := NewSubprocessRunner().RenderArtifact(context.Background(), req); err == nil {
|
||||
t.Fatal("expected preview error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerRunExitCodeTwoIsNotContentValidation(t *testing.T) {
|
||||
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
|
||||
t.Setenv("SCRIPTORIUM_HELPER_MODE", "fail_exit2_with_output")
|
||||
@@ -224,8 +238,11 @@ func TestSubprocessRunnerRenderSuccess(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
req := RenderArtifactRequest{
|
||||
Binary: wrapper,
|
||||
ConfigPath: "/config.yml",
|
||||
PromptID: "dnd.session_recap",
|
||||
ProfileID: "local-quality",
|
||||
InputPaths: map[string]string{"transcript": filepath.Join(dir, "polished.json")},
|
||||
Vars: map[string]string{"session_id": "test,session=value"},
|
||||
OutputPath: filepath.Join(dir, "artifacts", "session_recap.render.json"),
|
||||
StdoutLogPath: filepath.Join(dir, "logs", "scriptorium.render.stdout.log"),
|
||||
StderrLogPath: filepath.Join(dir, "logs", "scriptorium.render.stderr.log"),
|
||||
@@ -245,15 +262,21 @@ func TestSubprocessRunnerRenderSuccess(t *testing.T) {
|
||||
t.Fatalf("ExitCode = %d, want 0", res.ExitCode)
|
||||
}
|
||||
assertFileNonEmpty(t, req.OutputPath)
|
||||
if res.StdoutLogPath != "" {
|
||||
t.Fatalf("stdout log = %q", res.StdoutLogPath)
|
||||
}
|
||||
data, err := os.ReadFile(req.OutputPath)
|
||||
if err != nil || string(data) != "{\"prepared\":true}\n" {
|
||||
t.Fatalf("prepared output = %q, %v", data, err)
|
||||
}
|
||||
|
||||
rec := readScriptoriumHelperRecord(t, recordPath)
|
||||
wantArgs := []string{
|
||||
"render",
|
||||
"--prompt", "dnd.session_recap",
|
||||
"--input", "transcript=" + req.InputPaths["transcript"],
|
||||
"--timeout", "2s",
|
||||
"promptkit", "show", "prepared", "dnd.session_recap",
|
||||
"--config", "/config.yml", "--profile", "local-quality",
|
||||
"--input-file", "transcript=" + req.InputPaths["transcript"],
|
||||
"--var", "session_id=test,session=value",
|
||||
"--format", "json",
|
||||
"--out", req.OutputPath,
|
||||
}
|
||||
if strings.Join(rec.Args, "\n") != strings.Join(wantArgs, "\n") {
|
||||
t.Fatalf("args = %#v, want %#v", rec.Args, wantArgs)
|
||||
@@ -279,7 +302,6 @@ func TestScriptoriumSubprocessHelper(t *testing.T) {
|
||||
}
|
||||
cliArgs := args[start:]
|
||||
|
||||
outputPath := flagValue(cliArgs, "--out")
|
||||
recordPath := os.Getenv("SCRIPTORIUM_HELPER_RECORD_PATH")
|
||||
if strings.TrimSpace(recordPath) != "" {
|
||||
rec := scriptoriumHelperRecord{
|
||||
@@ -315,10 +337,15 @@ func TestScriptoriumSubprocessHelper(t *testing.T) {
|
||||
_, _ = os.Stderr.WriteString("scriptorium helper validation failure\n")
|
||||
os.Exit(4)
|
||||
case "render_success":
|
||||
writeScriptoriumHelperFile(outputPath, "{\"rendered\":true}\n")
|
||||
_, _ = os.Stdout.WriteString("scriptorium helper render stdout\n")
|
||||
_, _ = os.Stdout.WriteString("{\"prepared\":true}\n")
|
||||
_, _ = os.Stderr.WriteString("scriptorium helper render stderr\n")
|
||||
os.Exit(0)
|
||||
case "render_invalid":
|
||||
_, _ = os.Stdout.WriteString("{broken")
|
||||
os.Exit(0)
|
||||
case "render_failure":
|
||||
_, _ = os.Stderr.WriteString("preview failed")
|
||||
os.Exit(1)
|
||||
default:
|
||||
_, _ = os.Stderr.WriteString(fmt.Sprintf("unknown helper mode %q\n", mode))
|
||||
os.Exit(2)
|
||||
@@ -371,15 +398,6 @@ func mustParseScriptoriumDuration(t *testing.T, v string) time.Duration {
|
||||
return d
|
||||
}
|
||||
|
||||
func flagValue(args []string, name string) string {
|
||||
for i := 0; i < len(args)-1; i++ {
|
||||
if args[i] == name {
|
||||
return args[i+1]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func writeScriptoriumFile(t *testing.T, path, contents string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
@@ -390,14 +408,6 @@ func writeScriptoriumFile(t *testing.T, path, contents string) {
|
||||
}
|
||||
}
|
||||
|
||||
func writeScriptoriumHelperFile(path, contents string) {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return
|
||||
}
|
||||
_ = os.MkdirAll(filepath.Dir(path), 0o755)
|
||||
_ = os.WriteFile(path, []byte(contents), 0o644)
|
||||
}
|
||||
|
||||
func assertFileNonEmpty(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
info, err := os.Stat(path)
|
||||
|
||||
Reference in New Issue
Block a user