Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/cli/pkg/action/trace_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions app/cli/pkg/action/trace_hook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
68 changes: 68 additions & 0 deletions app/cli/pkg/action/trace_session_annotations.go
Original file line number Diff line number Diff line change
@@ -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), ",")}
}
159 changes: 159 additions & 0 deletions app/cli/pkg/action/trace_session_annotations_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}
9 changes: 9 additions & 0 deletions pkg/attestation/crafter/api/attestation/v1/crafting_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading