From 4f1e532dc1f452d8e268fed51f822501d9eeede0 Mon Sep 17 00:00:00 2001 From: Miguel Martinez Trivino Date: Sat, 5 Dec 2026 01:03:26 +0100 Subject: [PATCH] feat(trace): record AI coding session ids as an attestation annotation The trace push adds the chainloop.aisession.ids annotation to the session attestation. Its value is the data.session.id of each CHAINLOOP_AI_CODING_SESSION material in the attestation, sorted and comma-joined. The control plane stores attestation annotations on the attestation referrer, so discover returns the ids and a consumer no longer has to download the session materials to read them. Refs: PFM-8484 Assisted-by: Claude Code Signed-off-by: Miguel Martinez Trivino Chainloop-Trace-Sessions: ad02680a-2ab6-4980-aa71-d685c2c57fb5 --- app/cli/pkg/action/trace_attestation.go | 8 +- app/cli/pkg/action/trace_hook_handler.go | 5 +- .../pkg/action/trace_session_annotations.go | 68 ++++++++ .../action/trace_session_annotations_test.go | 159 ++++++++++++++++++ .../api/attestation/v1/crafting_state.go | 9 + 5 files changed, 244 insertions(+), 5 deletions(-) create mode 100644 app/cli/pkg/action/trace_session_annotations.go create mode 100644 app/cli/pkg/action/trace_session_annotations_test.go diff --git a/app/cli/pkg/action/trace_attestation.go b/app/cli/pkg/action/trace_attestation.go index dbc3d754a..619ba7fcb 100644 --- a/app/cli/pkg/action/trace_attestation.go +++ b/app/cli/pkg/action/trace_attestation.go @@ -260,8 +260,10 @@ func (e *AttestationExecutor) Reset(ctx context.Context, trigger, reason string) } // Push finalizes and pushes the attestation, returning the push result so -// callers can report where the evidence landed. -func (e *AttestationExecutor) Push(ctx context.Context) (*AttestationResult, error) { +// callers can report where the evidence landed. annotations are added to the +// attestation as runtime annotations; they cannot override a value that the +// contract sets. +func (e *AttestationExecutor) Push(ctx context.Context, annotations map[string]string) (*AttestationResult, error) { cliVersion, cliDigest, err := e.executableInfo() if err != nil { return nil, fmt.Errorf("resolve executable info: %w", err) @@ -280,7 +282,7 @@ func (e *AttestationExecutor) Push(ctx context.Context) (*AttestationResult, err return nil, fmt.Errorf("create attestation push action: %w", err) } - res, err := a.Run(ctx, "", nil, false) + res, err := a.Run(ctx, "", annotations, false) if err != nil { return nil, fmt.Errorf("attestation push: %w", err) } diff --git a/app/cli/pkg/action/trace_hook_handler.go b/app/cli/pkg/action/trace_hook_handler.go index 243e6d885..2aa977249 100644 --- a/app/cli/pkg/action/trace_hook_handler.go +++ b/app/cli/pkg/action/trace_hook_handler.go @@ -789,9 +789,10 @@ func RunTracePush(ctx context.Context, log zerolog.Logger, opts RunTracePushOpts return nil } - // Push attestation + // Push attestation, with the ids of its sessions as an attestation + // annotation so that discover returns them (PFM-8484). log.Debug().Msg("pushing attestation") - res, err := executor.Push(ctx) + res, err := pushSessionAttestation(ctx, executor, sessions, attestedSessions) if err != nil { return fmt.Errorf("attestation push: %w", err) } diff --git a/app/cli/pkg/action/trace_session_annotations.go b/app/cli/pkg/action/trace_session_annotations.go new file mode 100644 index 000000000..654dab69c --- /dev/null +++ b/app/cli/pkg/action/trace_session_annotations.go @@ -0,0 +1,68 @@ +// +// Copyright 2026 The Chainloop Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package action + +import ( + "context" + "slices" + "strings" + + api "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1" +) + +// attestationPusher pushes the attestation that is in progress, with the +// given runtime annotations added at the attestation level. +type attestationPusher interface { + Push(ctx context.Context, annotations map[string]string) (*AttestationResult, error) +} + +// pushSessionAttestation pushes the session attestation with the ids of its +// AI coding sessions as an attestation annotation (api.AnnotationAISessionIDs). +func pushSessionAttestation(ctx context.Context, pusher attestationPusher, sessions []sessionEvidence, attested []string) (*AttestationResult, error) { + return pusher.Push(ctx, aiSessionAnnotations(sessions, attested)) +} + +// aiSessionAnnotations returns the attestation annotations that identify the +// AI coding sessions added to the attestation. One attestation can hold more +// than one session material, so the value lists every id, sorted and +// comma-joined. Each id is the data.session.id of its session material, so +// it agrees with what a reader of the material gets. +// +// An id that is empty or that contains a comma is left out, because it cannot +// be read back from the list; a reader can still download that material. It +// returns nil when no id is left. +func aiSessionAnnotations(sessions []sessionEvidence, attested []string) map[string]string { + ids := make([]string, 0, len(attested)) + for _, se := range sessions { + if se.evidence == nil || !slices.Contains(attested, se.sessionID) { + continue + } + + id := se.evidence.Data.Session.ID + if id == "" || strings.Contains(id, ",") { + continue + } + ids = append(ids, id) + } + + if len(ids) == 0 { + return nil + } + + slices.Sort(ids) + + return map[string]string{api.AnnotationAISessionIDs: strings.Join(slices.Compact(ids), ",")} +} diff --git a/app/cli/pkg/action/trace_session_annotations_test.go b/app/cli/pkg/action/trace_session_annotations_test.go new file mode 100644 index 000000000..ba53acf43 --- /dev/null +++ b/app/cli/pkg/action/trace_session_annotations_test.go @@ -0,0 +1,159 @@ +// +// Copyright 2026 The Chainloop Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package action + +import ( + "context" + "errors" + "testing" + + api "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1" + "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/materials/aicodingsession" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// sessionWithEvidenceID returns the push entry of one session. The evidence +// carries its own id, which is the data.session.id of the session material. +func sessionWithEvidenceID(sessionID, evidenceID string) sessionEvidence { + return sessionEvidence{ + sessionID: sessionID, + evidence: &aicodingsession.Evidence{ + Data: aicodingsession.Data{Session: aicodingsession.Session{ID: evidenceID}}, + }, + } +} + +func TestAISessionAnnotations(t *testing.T) { + testCases := []struct { + name string + sessions []sessionEvidence + attested []string + want map[string]string + }{ + { + name: "one session", + sessions: []sessionEvidence{sessionWithEvidenceID("s1", "s1")}, + attested: []string{"s1"}, + want: map[string]string{api.AnnotationAISessionIDs: "s1"}, + }, + { + name: "more than one session is sorted and comma-joined", + sessions: []sessionEvidence{ + sessionWithEvidenceID("ses_b", "ses_b"), + sessionWithEvidenceID("ses_a", "ses_a"), + }, + attested: []string{"ses_b", "ses_a"}, + want: map[string]string{api.AnnotationAISessionIDs: "ses_a,ses_b"}, + }, + { + name: "the id comes from the session material, not from the trace state", + sessions: []sessionEvidence{sessionWithEvidenceID("state-id", "material-id")}, + attested: []string{"state-id"}, + want: map[string]string{api.AnnotationAISessionIDs: "material-id"}, + }, + { + name: "a session that was not added to the attestation is left out", + sessions: []sessionEvidence{ + sessionWithEvidenceID("s1", "s1"), + sessionWithEvidenceID("s2", "s2"), + }, + attested: []string{"s2"}, + want: map[string]string{api.AnnotationAISessionIDs: "s2"}, + }, + { + name: "a repeated id is recorded once", + sessions: []sessionEvidence{ + sessionWithEvidenceID("s1", "same"), + sessionWithEvidenceID("s2", "same"), + }, + attested: []string{"s1", "s2"}, + want: map[string]string{api.AnnotationAISessionIDs: "same"}, + }, + { + name: "an empty id or an id with a comma is left out", + sessions: []sessionEvidence{ + sessionWithEvidenceID("s1", ""), + sessionWithEvidenceID("s2", "a,b"), + sessionWithEvidenceID("s3", "s3"), + {sessionID: "s4"}, + }, + attested: []string{"s1", "s2", "s3", "s4"}, + want: map[string]string{api.AnnotationAISessionIDs: "s3"}, + }, + { + name: "no usable id gives no annotation", + sessions: []sessionEvidence{sessionWithEvidenceID("s1", "")}, + attested: []string{"s1"}, + want: nil, + }, + { + name: "no attested session gives no annotation", + sessions: []sessionEvidence{sessionWithEvidenceID("s1", "s1")}, + attested: nil, + want: nil, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, aiSessionAnnotations(tc.sessions, tc.attested)) + }) + } +} + +// fakeAttestationPusher records the annotations that a push was given. +type fakeAttestationPusher struct { + annotations map[string]string + calls int + err error +} + +func (f *fakeAttestationPusher) Push(_ context.Context, annotations map[string]string) (*AttestationResult, error) { + f.calls++ + f.annotations = annotations + if f.err != nil { + return nil, f.err + } + + return &AttestationResult{}, nil +} + +func TestPushSessionAttestation(t *testing.T) { + sessions := []sessionEvidence{ + sessionWithEvidenceID("ses_2", "ses_2"), + sessionWithEvidenceID("ses_1", "ses_1"), + sessionWithEvidenceID("ses_3", "ses_3"), + } + + t.Run("the pushed attestation carries the ids of the attested sessions", func(t *testing.T) { + pusher := &fakeAttestationPusher{} + + res, err := pushSessionAttestation(context.Background(), pusher, sessions, []string{"ses_2", "ses_1"}) + require.NoError(t, err) + require.NotNil(t, res) + + assert.Equal(t, 1, pusher.calls) + assert.Equal(t, map[string]string{api.AnnotationAISessionIDs: "ses_1,ses_2"}, pusher.annotations) + }) + + t.Run("a push error is returned", func(t *testing.T) { + pusher := &fakeAttestationPusher{err: errors.New("boom")} + + _, err := pushSessionAttestation(context.Background(), pusher, sessions, []string{"ses_1"}) + require.ErrorContains(t, err, "boom") + }) +} diff --git a/pkg/attestation/crafter/api/attestation/v1/crafting_state.go b/pkg/attestation/crafter/api/attestation/v1/crafting_state.go index 7adf1db84..8fb4bb178 100644 --- a/pkg/attestation/crafter/api/attestation/v1/crafting_state.go +++ b/pkg/attestation/crafter/api/attestation/v1/crafting_state.go @@ -84,6 +84,15 @@ var ( // redaction because the operator explicitly asked for it. Recorded so the // bypass is visible to policies rather than silent. AnnotationMaterialRedactionSkipped = CreateAnnotation("material.redaction.skipped") + + // AnnotationAISessionIDs is an attestation annotation, not a material + // annotation. It lists the ids of the AI coding sessions that the + // attestation holds, one for each CHAINLOOP_AI_CODING_SESSION material + // (the data.session.id of the material), sorted and comma-joined. The + // control plane stores attestation annotations on the attestation + // referrer, so a reader gets the ids from discover and does not download + // the session materials. + AnnotationAISessionIDs = CreateAnnotation("aisession.ids") ) type NormalizedMaterialOutput struct {