170 lines
6.1 KiB
Go
170 lines
6.1 KiB
Go
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
|
|
masksByPrefix := map[string][][]string{}
|
|
for _, field := range result.Metadata().HistoricalPaths() {
|
|
segments := strings.Split(strings.TrimPrefix(field.String(), "/"), "/")
|
|
extraParams := -1
|
|
// Match native root/collection resource paths before unescaping keys.
|
|
if len(segments) >= 5 && segments[0] == "promptkit" &&
|
|
(segments[1] == "profiles" || segments[1] == "backends") && segments[3] == "extra_params" {
|
|
extraParams = 3
|
|
} else if len(segments) >= 7 && segments[0] == "promptkit" && segments[1] == "collections" &&
|
|
(segments[3] == "profiles" || segments[3] == "backends") && segments[5] == "extra_params" {
|
|
extraParams = 5
|
|
}
|
|
if extraParams < 0 || !sensitive(field) {
|
|
continue
|
|
}
|
|
prefix := "/" + strings.Join(segments[:extraParams+1], "/")
|
|
suffix := segments[extraParams+1:]
|
|
for i, segment := range suffix {
|
|
suffix[i] = strings.ReplaceAll(strings.ReplaceAll(segment, "~1", "/"), "~0", "~")
|
|
}
|
|
masksByPrefix[prefix] = append(masksByPrefix[prefix], suffix)
|
|
}
|
|
register := func(masks map[string][][]string, id string, prefix configkit.Path) {
|
|
masks[id] = append(masks[id], masksByPrefix[prefix.String()]...)
|
|
}
|
|
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
|
|
}
|
|
}
|