Keep raw runtime parameters out of early capture

This commit is contained in:
2026-11-28 16:12:52 +00:00
parent 63cdd7a7c5
commit 95d46c7da3
6 changed files with 127 additions and 3 deletions

View File

@@ -0,0 +1,86 @@
package cli
import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/promptkit"
)
func TestInvocationCaptureOmitsMarkedRuntimeParameters(t *testing.T) {
for _, command := range []string{"run", "preflight"} {
for _, earlyFailure := range []bool{false, true} {
name := command + "/success"
if earlyFailure {
name = command + "/early-failure"
}
t.Run(name, func(t *testing.T) {
probe := &runProbe{}
_, configPath, inputPath, _ := runFixture(t, probe, func(configuration string) string {
return strings.Replace(configuration, " prompts:", " extra_params: {token: configured-placeholder}\n prompts:", 1)
})
if earlyFailure {
inputPath += "-missing"
}
calls := 0
client := captureClient(func(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
calls++
if request.Target.ExtraParams["token"] != "runtime-private-placeholder" || request.Target.ExtraParams["caption"] != "ordinary-runtime-caption" {
t.Fatal("capture mutated execution parameters")
}
if productionSchemaHasProperty(request, "status") {
return &promptkit.GenerateResponse{Content: `{"status":"ok"}`}, nil
}
return probe.Generate(ctx, request)
})
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client, RunIDGenerator: func(time.Time) (string, error) { return "capture", nil }})
if err != nil {
t.Fatal(err)
}
root := t.TempDir()
var out, stderr bytes.Buffer
outcome := app.Execute(context.Background(), []string{command, "demo", "--config", configPath, "--input-file", inputPath, "--llm-profile", "chosen", "--llm-extra-params", `{"token":"runtime-private-placeholder","caption":"ordinary-runtime-caption"}`, "--cache", "bypass", "--debug", "--debug-dir", root, "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
if (outcome.Status != 0) != earlyFailure || earlyFailure && calls != 0 {
t.Fatalf("outcome=%+v calls=%d stderr=%s", outcome, calls, stderr.String())
}
if !earlyFailure {
var report map[string]any
if err := json.Unmarshal(out.Bytes(), &report); err != nil || calls == 0 {
t.Fatalf("report error=%v calls=%d", err, calls)
}
}
ordinaryRetained := false
err = filepath.WalkDir(root, func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err
}
if !entry.Type().IsRegular() {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
return err
}
if bytes.Contains(content, []byte("runtime-private-placeholder")) {
t.Errorf("marked runtime parameter retained in %s", path)
}
ordinaryRetained = ordinaryRetained || bytes.Contains(content, []byte("ordinary-runtime-caption"))
return nil
})
if err != nil {
t.Fatal(err)
}
if !earlyFailure && !ordinaryRetained {
t.Fatal("ordinary effective parameters were lost")
}
})
}
}
}

View File

@@ -104,7 +104,7 @@ func executePreflight(invocation commandkit.Invocation, input pipelineRunInput,
return finish(commandkit.Failure(err.Error()).WithCause(err))
}
add("debug", preflightScope{Path: capture.Path()}, "passed", "Debug capture is accessible.")
if err := capture.WriteJSON("context.json", map[string]any{"application_version": version, "pipeline": input.PipelineID, "input_path": input.InputFile.Value, "run_id": runID, "started_at": startedAt, "lanes": input.Lanes, "references": input.References, "unbinds": input.Unbinds, "resources": input.Overrides.Resources, "execution_overrides": input.Overrides.Execution}); err != nil {
if err := captureInvocationContext(capture, input, version, runID, startedAt); err != nil {
add("debug", preflightScope{Path: capture.Path()}, "failed", err.Error())
return finish(commandkit.Failure(err.Error()).WithCause(err))
}

View File

@@ -64,7 +64,7 @@ func (run *runInvocation) execute() commandkit.Outcome {
run.group = &promptkit.ExecutionGroup{}
ctx = promptkit.WithExecutionObserver(ctx, run.group.Observe)
}
if err := run.capture.WriteJSON("context.json", map[string]any{"application_version": version, "pipeline": input.PipelineID, "input_path": input.InputFile.Value, "run_id": runID, "started_at": startedAt, "lanes": input.Lanes, "resources": input.Overrides.Resources, "execution_overrides": input.Overrides.Execution}); err != nil {
if err := captureInvocationContext(run.capture, input, version, runID, startedAt); err != nil {
return run.finish(commandkit.Failure(err.Error()).WithCause(err))
}
if err := captureRunConfiguration(run.capture, result); err != nil {

View File

@@ -7,6 +7,7 @@ import (
"slices"
"strconv"
"strings"
"time"
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
frameworkdebug "gitea.maximumdirect.net/eric/notarius/internal/framework/debug"
@@ -15,6 +16,30 @@ import (
"go.maximumdirect.net/promptkit"
)
// Target-aware native evidence retains effective parameters with their secret
// marks. Early context records presence only, before a target is available.
func captureInvocationContext(recorder *frameworkdebug.Recorder, input pipelineRunInput, version, runID string, startedAt time.Time) error {
if !recorder.Enabled() {
return nil
}
execution := input.Overrides.Execution
extraParamsSupplied := false
if execution.Execution != nil {
extraParamsSupplied = execution.Execution.ExtraParams != nil
execution.Execution = captureExecutionOverride(execution.Execution)
}
return recorder.WriteJSON("context.json", map[string]any{"application_version": version, "pipeline": input.PipelineID, "input_path": input.InputFile.Value, "run_id": runID, "started_at": startedAt, "lanes": input.Lanes, "references": input.References, "unbinds": input.Unbinds, "resources": input.Overrides.Resources, "execution_overrides": execution, "extra_params_supplied": extraParamsSupplied})
}
func captureExecutionOverride(execution *promptkit.ExecutionTargetOverride) *promptkit.ExecutionTargetOverride {
if execution == nil {
return nil
}
copy := *execution
copy.ExtraParams = nil
return &copy
}
// captureRunConfiguration inspects the original, frozen application load. It
// neither reloads resources nor serializes typed settings containing credentials.
// Shared loaders remain independent of run storage.
@@ -107,6 +132,9 @@ func newRunEvidenceRecorder(recorder *frameworkdebug.Recorder, result *configkit
func (writer *runEvidenceRecorder) WriteJSON(name string, payload any) error {
switch value := payload.(type) {
case promptkit.RunRequest:
value.Execution = captureExecutionOverride(value.Execution)
payload = value
case *promptkit.PreparedRun:
copy := *value
copy.EffectiveModelParams = writer.target(copy.EffectiveModelParams, copy.SelectedProfileID, copy.SelectedBackendID)