diff --git a/pkg/promptrun/record.go b/pkg/promptrun/record.go index c6775f99..70062a46 100644 --- a/pkg/promptrun/record.go +++ b/pkg/promptrun/record.go @@ -5,6 +5,8 @@ import ( "encoding/json" "errors" "fmt" + "maps" + "slices" "github.com/flanksource/captain/pkg/api" "github.com/flanksource/captain/pkg/database" @@ -46,6 +48,11 @@ type Recording struct { // Runtime is what the host asked for: its run mode, driver and requested // selection. The resolved selection is filled in when the run starts. Runtime database.PromptRunRuntime + // Metadata is the host's own facts about the run (e.g. the commit or event + // it acts on), stored in captain_prompt_runs.metadata beside Captain's + // resolution metadata. Values are stored as their JSON encoding. A key + // Captain records itself (see RunMetadata) is refused. + Metadata map[string]any // Link writes the host's own rows for the admitted run. It runs inside the // admission transaction; an error rolls back the sessions and the run, and // nothing is dispatched. @@ -129,6 +136,9 @@ func Admit(ctx context.Context, in Input) (*database.PromptRun, error) { if err != nil { return nil, err } + if err := mergeHostMetadata(metadata, rec.Metadata); err != nil { + return nil, err + } var out *database.PromptRun err = rec.DB.Transaction(ctx, func(tx *database.DB) error { session, err := rec.Placement.resolve(ctx, tx) @@ -192,6 +202,26 @@ func RunMetadata(resolved api.ResolvedSpec, presets *runtimeprofiles.PresetResol return jsonObject("run metadata", values) } +// runMetadataKeys are the metadata keys RunMetadata owns; a host may not set them. +var runMetadataKeys = []string{"specTrace", "specProvenance", "specWarnings", "runtimePresets", "runtimeProfile"} + +func mergeHostMetadata(metadata, host map[string]any) error { + if len(host) == 0 { + return nil + } + for key := range host { + if slices.Contains(runMetadataKeys, key) { + return fmt.Errorf("promptrun: recording metadata key %q is recorded by Captain", key) + } + } + encoded, err := jsonObject("recording metadata", host) + if err != nil { + return err + } + maps.Copy(metadata, encoded) + return nil +} + func jsonObject(what string, value any) (map[string]any, error) { raw, err := json.Marshal(value) if err != nil { diff --git a/pkg/promptrun/record_ginkgo_test.go b/pkg/promptrun/record_ginkgo_test.go index 9ea6eabe..59cd910c 100644 --- a/pkg/promptrun/record_ginkgo_test.go +++ b/pkg/promptrun/record_ginkgo_test.go @@ -512,6 +512,35 @@ var _ = Describe("a recorded prompt run", func() { Expect(err).To(MatchError(promptrun.ErrRunFinished)) }) + It("stores the host's metadata beside Captain's resolution metadata", func(ctx SpecContext) { + type event struct { + ID string `json:"id"` + } + rec := recording(nil) + rec.Metadata = map[string]any{"headSHA": "head-a", "context": event{ID: "conflict:head-a"}} + + run, err := promptrun.Admit(ctx, promptrun.Input{Resolved: resolved, Record: rec}) + + Expect(err).NotTo(HaveOccurred()) + stored, err := db.GetPromptRun(ctx, run.ID) + Expect(err).NotTo(HaveOccurred()) + Expect(stored.Metadata).To(HaveKeyWithValue("headSHA", "head-a")) + Expect(stored.Metadata).To(HaveKeyWithValue("context", map[string]any{"id": "conflict:head-a"})) + Expect(stored.Metadata["specTrace"]).To(HaveLen(2), "host metadata must not replace Captain's") + }) + + It("refuses host metadata that names a key Captain records, admitting nothing", func(ctx SpecContext) { + rec := recording(nil) + rec.PromptRunID = uuid.New() + rec.Metadata = map[string]any{"specTrace": "forged"} + + _, err := promptrun.Admit(ctx, promptrun.Input{Resolved: resolved, Record: rec}) + + Expect(err).To(MatchError(ContainSubstring(`"specTrace"`))) + _, err = db.GetPromptRun(ctx, rec.PromptRunID) + Expect(errors.Is(err, database.ErrPromptRunNotFound)).To(BeTrue(), "%v", err) + }) + It("requires a recording to admit", func(ctx SpecContext) { _, err := promptrun.Admit(ctx, promptrun.Input{Resolved: resolved}) Expect(err).To(MatchError(ContainSubstring("recording")))