Capture native execution facts and complete scoped debug evidence

This commit is contained in:
2026-11-22 03:19:43 +00:00
parent 8680060400
commit 9f66421f4f
24 changed files with 1688 additions and 543 deletions

157
internal/cli/run_capture.go Normal file
View File

@@ -0,0 +1,157 @@
package cli
import (
"bytes"
"fmt"
"maps"
"slices"
"strconv"
"strings"
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
frameworkdebug "gitea.maximumdirect.net/eric/notarius/internal/framework/debug"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
)
// captureRunConfiguration inspects the original, frozen application load. It
// neither reloads resources nor serializes typed settings containing credentials.
// Shared loaders remain independent of run storage.
func captureRunConfiguration(recorder *frameworkdebug.Recorder, result *configkit.Result[config.Document]) error {
if !recorder.Enabled() {
return nil
}
if result == nil {
return fmt.Errorf("capture configuration: application result is unavailable")
}
var application bytes.Buffer
if err := result.WriteEffective(&application); err != nil {
return fmt.Errorf("inspect application configuration: %w", err)
}
if err := recorder.WriteBytes("configuration/application.yml", application.Bytes()); err != nil {
return err
}
view, ok := result.InspectSubtree(configkit.Keys("promptkit"))
if !ok {
return fmt.Errorf("capture configuration: PromptKit inspection is unavailable")
}
var prompts bytes.Buffer
if err := view.WriteEffective(&prompts); err != nil {
return fmt.Errorf("inspect PromptKit configuration: %w", err)
}
if err := recorder.WriteBytes("configuration/promptkit.yml", prompts.Bytes()); err != nil {
return err
}
return recorder.WriteJSON("configuration/sources.json", result.Sources())
}
type runEvidenceRecorder struct {
*frameworkdebug.Recorder
profiles map[string][][]string
backends map[string][][]string
}
// Keep ConfigKit-marked provider settings omitted in native execution evidence,
// too. These are exact configuration paths from the adopted definition, not
// searches for credential-looking strings in application content.
func newRunEvidenceRecorder(recorder *frameworkdebug.Recorder, result *configkit.Result[config.Document]) pipeline.DebugRecorder {
writer := &runEvidenceRecorder{Recorder: recorder, profiles: map[string][][]string{}, backends: map[string][][]string{}}
if result == nil {
return writer
}
sensitive := config.Definition("").Sensitive
paths := result.Metadata().HistoricalPaths()
register := func(masks map[string][][]string, id string, prefix configkit.Path) {
for _, field := range paths {
if !strings.HasPrefix(field.String(), prefix.String()+"/") || !sensitive(field) {
continue
}
segments := strings.Split(strings.TrimPrefix(field.String(), prefix.String()+"/"), "/")
for index, segment := range segments {
segments[index] = strings.ReplaceAll(strings.ReplaceAll(segment, "~1", "/"), "~0", "~")
}
masks[id] = append(masks[id], segments)
}
}
root := configkit.Keys("promptkit")
for index, profile := range result.Config.PromptKit.Profiles {
register(writer.profiles, profile.ID, root.Key("profiles").Index(index).Key("extra_params"))
}
for index, backend := range result.Config.PromptKit.Backends {
register(writer.backends, backend.ID, root.Key("backends").Index(index).Key("extra_params"))
}
for namespace, collection := range result.Config.PromptKit.Collections {
prefix := root.Key("collections").Key(namespace)
for index, profile := range collection.Profiles {
register(writer.profiles, namespace+"/"+profile.ID, prefix.Key("profiles").Index(index).Key("extra_params"))
}
for index, backend := range collection.Backends {
register(writer.backends, namespace+"/"+backend.ID, prefix.Key("backends").Index(index).Key("extra_params"))
}
}
return writer
}
func (writer *runEvidenceRecorder) WriteJSON(name string, payload any) error {
switch value := payload.(type) {
case *promptkit.PreparedRun:
copy := *value
copy.EffectiveModelParams = writer.target(copy.EffectiveModelParams, copy.SelectedProfileID, copy.SelectedBackendID)
payload = copy
case *promptkit.RunResult:
copy := *value
copy.EffectiveModelParams = writer.target(copy.EffectiveModelParams, copy.SelectedProfileID, copy.SelectedBackendID)
payload = copy
}
return writer.Recorder.WriteJSON(name, payload)
}
func (writer *runEvidenceRecorder) target(target promptkit.ExecutionTarget, profile, backend string) promptkit.ExecutionTarget {
for _, segments := range writer.backends[backend] {
target.ExtraParams = omitEvidenceSetting(target.ExtraParams, segments)
}
for _, segments := range writer.profiles[profile] {
target.ExtraParams = omitEvidenceSetting(target.ExtraParams, segments)
}
return target
}
// Copy only changed map branches; never mutate the native snapshot delivered to
// another observer. Ordinary siblings and all application text remain untouched.
func omitEvidenceSetting(values map[string]any, segments []string) map[string]any {
if len(segments) == 0 || values == nil {
return values
}
value, found := values[segments[0]]
if !found {
return values
}
copy := maps.Clone(values)
if len(segments) == 1 {
copy[segments[0]] = "[REDACTED]"
return copy
}
copy[segments[0]] = omitEvidenceValue(value, segments[1:])
return copy
}
func omitEvidenceValue(value any, segments []string) any {
if len(segments) == 0 {
return "[REDACTED]"
}
switch value := value.(type) {
case map[string]any:
return omitEvidenceSetting(value, segments)
case []any:
index, err := strconv.Atoi(segments[0])
if err != nil || index < 0 || index >= len(value) {
return value
}
copy := slices.Clone(value)
copy[index] = omitEvidenceValue(copy[index], segments[1:])
return copy
default:
return value
}
}