Skip to content
Merged
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
1 change: 1 addition & 0 deletions app/cli/internal/trace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ How the sessions are linked to the commits and attested.
| Feature | What it means | Claude Code | Cursor | OpenCode 1.x | OpenCode 2 | Pi | Codex | Copilot |
|---------|---------------|-------------|--------|--------------|------------|---|---|---|
| Commit trailer | Each commit gets a `Chainloop-Trace-Sessions` trailer with the sessions that contributed to it. | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
| Rebase outside the clone | A rebase that ran without the hooks, for example the GitHub "Update branch" button or a rebase in another clone, gives the commits new SHAs that have no commit record. At the next push from a clone with the hooks, each commit of the branch that has a trailer and no record is recorded again, with the sessions of the trailer that the clone knows. Then the push sends all the commits of the session with their new SHAs, also with no new commit. A merge commit and a session that the clone does not know are not recorded. A rewrite that drops the trailer is not covered ([issue 3838](https://github.com/chainloop-dev/chainloop/issues/3838)). | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
| Attestation at push | In the default control-plane storage mode, the `pre-push` hook attests the sessions of the pushed commits to the control plane. | Yes | Yes | Yes | Yes | Unknown | Unknown | Unknown |
| Local storage at push | In a repository with `traceStorage: LOCAL` in `.chainloop.yml`, the `pre-push` hook runs the same assembly as an attestation. It writes the session evidence set to the `local-sessions` folder of the trace directory and sends no attestation. It needs no control plane, login, or network. The stored set is equal to the upload of a push, except for the signature. A `statement.json` file indexes the set: the in-toto statement that a push would sign, with the `chainloop.dev/attestation/v0.2` predicate. It has no envelope and no signature, so it is not an attestation. `trace init --local` sets up the mode. `trace run --local` stores one wrapped session the same way, with no `.chainloop.yml` setting. Redaction is on by default. Set `CHAINLOOP_TRACE_NO_REDACT` to turn it off, for a trusted local store only (spec issue-3582). | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
| Shell commands in another checkout | A session can work in a checkout other than the one it started in through shell commands only, for example in a fresh clone. The checkout is found from the command text: the target of `cd` or `pushd`, the path of `git -C`, and the folder that `git clone` or `gh repo clone` creates. The git hooks are installed there and the session is recorded there, the same as for a file tool edit in that checkout. A clone gets the git hooks only after the command that created it, so a commit in that same command does not carry the session. For OpenCode, a plugin installed by an earlier version does not send the command text: install the hooks again to update it. | Yes | Partial | Yes | Yes | Yes | Yes | Yes |
Expand Down
55 changes: 54 additions & 1 deletion app/cli/internal/trace/git/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (c *ExecClient) IsMergeCommit(repoRoot, sha string) (bool, error) {
// `git rev-list HEAD ^<base>` (newest first). When no default remote branch
// is detected, returns every commit reachable from HEAD.
func (c *ExecClient) BranchCommitSHAs(repoRoot string) ([]string, error) {
args := []string{"rev-list", "HEAD"}
args := []string{"rev-list", "--topo-order", "HEAD"}
if base := detectBaseBranch(repoRoot); base != "" {
args = append(args, "^"+base)
}
Expand All @@ -339,6 +339,59 @@ func (c *ExecClient) BranchCommitSHAs(repoRoot string) ([]string, error) {
return strings.Split(out, "\n"), nil
}

// Separators of the BranchCommits output. A commit message cannot hold them in
// practice.
const (
fieldSep = "\x1f"
recordSep = "\x1e"
)

// BranchCommits returns the branch commits whose message contains grep, with
// one git log call.
func (c *ExecClient) BranchCommits(repoRoot, grep string) ([]BranchCommit, error) {
args := []string{"log", "--topo-order", "--format=%H" + fieldSep + "%ct" + fieldSep + "%P" + fieldSep + "%B" + recordSep}
if grep != "" {
args = append(args, "--fixed-strings", "--grep="+grep)
}
args = append(args, "HEAD")
if base := detectBaseBranch(repoRoot); base != "" {
args = append(args, "^"+base)
}

cmd := exec.Command("git", args...)
cmd.Dir = repoRoot
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("git log: %w", err)
}

var commits []BranchCommit
for record := range strings.SplitSeq(string(out), recordSep) {
record = strings.TrimLeft(record, "\n")
if record == "" {
continue
}

fields := strings.SplitN(record, fieldSep, 4)
if len(fields) != 4 {
return nil, fmt.Errorf("git log: unexpected record %q", record)
}
secs, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("git log %s committer date %q: %w", fields[0], fields[1], err)
}

commits = append(commits, BranchCommit{
SHA: fields[0],
Message: strings.TrimSpace(fields[3]),
CommittedAt: time.Unix(secs, 0),
Parents: len(strings.Fields(fields[2])),
})
}

return commits, nil
}

// detectBaseBranch tries to find the default remote branch (main, master, etc.)
func detectBaseBranch(repoRoot string) string {
ref := gitOutput(repoRoot, "symbolic-ref", "refs/remotes/origin/HEAD")
Expand Down
55 changes: 55 additions & 0 deletions app/cli/internal/trace/git/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package git
import (
"os"
"path/filepath"
"slices"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -246,3 +247,57 @@ func TestMapGitStatus(t *testing.T) {
assert.Equal(t, "modified", mapGitStatus("M"))
assert.Equal(t, "modified", mapGitStatus(""))
}

func TestClient_BranchCommits(t *testing.T) {
r := setupBranchedRepo(t)

testCases := []struct {
name string
grep string
wantSHAs []string
wantParents map[string]int
}{
{
name: "empty grep returns every branch commit",
wantSHAs: []string{r.mergeSHA, r.featureB, r.featureA, r.sideSHA},
wantParents: map[string]int{r.mergeSHA: 2, r.featureA: 1},
},
{
name: "grep keeps the matching messages",
grep: "feature",
wantSHAs: []string{r.featureB, r.featureA},
},
{
name: "grep with no match returns nothing",
grep: "Chainloop-Trace-Sessions:",
},
}
for name, client := range testClients() {
for _, tc := range testCases {
t.Run(name+"/"+tc.name, func(t *testing.T) {
commits, err := client.BranchCommits(r.dir, tc.grep)
require.NoError(t, err)

shas := make([]string, 0, len(commits))
byMessage := make(map[string]BranchCommit, len(commits))
for _, c := range commits {
shas = append(shas, c.SHA)
byMessage[c.Message] = c
assert.False(t, c.CommittedAt.IsZero())
if want, ok := tc.wantParents[c.SHA]; ok {
assert.Equal(t, want, c.Parents)
}
}
assert.ElementsMatch(t, tc.wantSHAs, shas)
if len(tc.wantSHAs) == 0 {
return
}

// HEAD first, and a commit before its parent.
assert.Equal(t, tc.wantSHAs[0], shas[0])
assert.Less(t, slices.Index(shas, r.featureB), slices.Index(shas, r.featureA))
assert.Equal(t, r.featureA, byMessage["feature a"].SHA)
})
}
}
}
16 changes: 15 additions & 1 deletion app/cli/internal/trace/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ type BlameLine struct {

// Client abstracts git operations so the implementation can be swapped
// (e.g. from exec-based to go-git).
// BranchCommit is a commit of the current branch.
Comment thread
jiparis marked this conversation as resolved.
Resolved
type BranchCommit struct {
SHA string
Message string
CommittedAt time.Time
// Parents is the number of parents. A merge commit has more than one.
Parents int
}

type Client interface {
// Context collects repository metadata: remote URL, branch, commits since base.
Context(repoRoot string) (*aicodingsession.GitContext, error)
Expand Down Expand Up @@ -136,8 +145,13 @@ type Client interface {
IsMergeCommit(repoRoot, sha string) (bool, error)
// BranchCommitSHAs returns the SHAs reachable from HEAD that are not
// reachable from the default remote branch (HEAD ^merge-base). The merge
// base itself is excluded. Order is unspecified.
// base itself is excluded. HEAD comes first, and on a linear branch each
// commit comes before its parents.
BranchCommitSHAs(repoRoot string) ([]string, error)
// BranchCommits returns the commits of BranchCommitSHAs, in the same
// order, whose message contains grep. An empty grep returns every commit.
// It reads all of them in one pass.
BranchCommits(repoRoot, grep string) ([]BranchCommit, error)
// LocalReachableSHAs returns the union of SHAs reachable from every
// local branch tip (refs/heads/*). Used by the post-push GC to identify
// CommitRecords that are still alive on at least one branch — anything
Expand Down
56 changes: 43 additions & 13 deletions app/cli/internal/trace/git/gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,22 +412,38 @@ func (c *GoGitClient) IsMergeCommit(repoRoot, sha string) (bool, error) {
}

// BranchCommitSHAs returns the SHAs reachable from HEAD that are not reachable
// from the default remote branch. The merge base is excluded. Order is
// unspecified.
// from the default remote branch. The merge base is excluded. HEAD comes
// first, and on a linear branch each commit comes before its parents.
func (c *GoGitClient) BranchCommitSHAs(repoRoot string) ([]string, error) {
var shas []string
err := walkBranch(repoRoot, func(commit *object.Commit) error {
shas = append(shas, commit.Hash.String())

return nil
})
if err != nil {
return nil, err
}

return shas, nil
}

// walkBranch calls fn for each commit reachable from HEAD and not from the
// merge base with the default remote branch, HEAD first.
func walkBranch(repoRoot string, fn func(*object.Commit) error) error {
repo, err := openRepo(repoRoot)
if err != nil {
return nil, fmt.Errorf("open repo: %w", err)
return fmt.Errorf("open repo: %w", err)
}

head, err := repo.Head()
if err != nil {
return nil, fmt.Errorf("get HEAD: %w", err)
return fmt.Errorf("get HEAD: %w", err)
}

headCommit, err := repo.CommitObject(head.Hash())
if err != nil {
return nil, fmt.Errorf("resolve HEAD commit: %w", err)
return fmt.Errorf("resolve HEAD commit: %w", err)
}

// Pre-seed the iterator's seen-set with the merge base(s). The preorder
Expand All @@ -444,17 +460,31 @@ func (c *GoGitClient) BranchCommitSHAs(repoRoot string) ([]string, error) {
}
}

var shas []string
iter := object.NewCommitPreorderIter(headCommit, seen, nil)
if err := iter.ForEach(func(c *object.Commit) error {
shas = append(shas, c.Hash.String())
if err := object.NewCommitPreorderIter(headCommit, seen, nil).ForEach(fn); err != nil {
return fmt.Errorf("walk commits: %w", err)
}

return nil
}

// BranchCommits returns the branch commits whose message contains grep.
func (c *GoGitClient) BranchCommits(repoRoot, grep string) ([]BranchCommit, error) {
var commits []BranchCommit
err := walkBranch(repoRoot, func(commit *object.Commit) error {
if grep != "" && !strings.Contains(commit.Message, grep) {
return nil
}
commits = append(commits, BranchCommit{
SHA: commit.Hash.String(),
Message: strings.TrimSpace(commit.Message),
CommittedAt: commit.Committer.When,
Parents: commit.NumParents(),
})

return nil
}); err != nil {
return nil, fmt.Errorf("walk commits: %w", err)
}
})

return shas, nil
return commits, err
}

// LocalReachableSHAs walks every local branch tip (refs/heads/*) and returns
Expand Down
96 changes: 90 additions & 6 deletions app/cli/pkg/action/trace_hook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,85 @@ func filterCurrentBranchCommits(client tracegit.Client, repoRoot string, records
return filtered
}

// recordRebasedCommits creates a commit record for each commit of the current
// branch that carries a Chainloop-Trace-Sessions trailer but has no record. A
// rebase that ran outside this clone (for example the GitHub "Update branch"
// button, or a rebase in another clone) gives the commits new SHAs without
// running post-commit, so no record exists for them. Without this step the
// orphan filter drops the old records and the push sends none of the rebased
// commits.
//
// Only sessions with a local session record are taken from the trailer: the
// trailer is text that the commit author controls. Merge commits are skipped,
// as post-commit does. An existing record is never changed. It returns records
// with the new records appended. When the branch commits cannot be listed, it
// returns records unchanged.
func recordRebasedCommits(client tracegit.Client, store *state.Store, repoRoot string, records []*state.CommitRecord, sessionRecords map[string]*state.SessionRecord, log zerolog.Logger) []*state.CommitRecord {
if len(sessionRecords) == 0 {
return records
}

// One read of the commits that carry a trailer: an unrecorded commit
// without one is never recorded, and the branch can hold the whole
// history when no base branch is found.
candidates, err := client.BranchCommits(repoRoot, state.TrailerKey+":")
if err != nil {
log.Debug().Err(err).Msg("could not enumerate branch commits, skipping rebased commit recovery")

return records
}

recorded := make(map[string]struct{}, len(records))
for _, rec := range records {
recorded[rec.SHA] = struct{}{}
}

// Walk the branch oldest first. A rebase replays its commits in the same
// second, so each new record gets a timestamp after the previous one: the
// timestamp is what orders the commits of a session.
var prev time.Time
for _, commit := range slices.Backward(candidates) {
sha := commit.SHA
if _, ok := recorded[sha]; ok || commit.Parents > 1 {
continue
}

var sessionIDs []string
for _, sid := range state.ParseSessionIDsFromTrailer(commit.Message) {
if _, known := sessionRecords[sid]; known {
sessionIDs = append(sessionIDs, sid)
}
}
if len(sessionIDs) == 0 {
continue
}

// The committer date keeps the commits before a commit made after
// the pull, which the time of this push would not.
ts := commit.CommittedAt.UTC().Truncate(time.Second)
if !ts.After(prev) {
ts = prev.Add(time.Second)
}
prev = ts

rec := &state.CommitRecord{
SHA: sha,
Message: commit.Message,
SessionIDs: sessionIDs,
Timestamp: ts.Format(time.RFC3339),
}
if err := store.SaveCommitRecord(rec); err != nil {
log.Debug().Err(err).Str("sha", sha).Msg("could not save rebased commit record")
continue
}

log.Debug().Str("sha", sha).Strs("session_ids", sessionIDs).Msg("recorded rebased commit from its trailer")
records = append(records, rec)
Comment thread
jiparis marked this conversation as resolved.
Resolved
}

return records
}

// RunTracePushOpts configures RunTracePush behaviour.
type RunTracePushOpts struct {
// AllowEmpty makes the push attest every recorded session even when
Expand Down Expand Up @@ -777,7 +856,7 @@ func sessionCommitGroups(aiCommits []*state.CommitRecord, sessionRecords map[str
delete(groups, sid)
continue
}
sort.Slice(commits, func(i, j int) bool {
sort.SliceStable(commits, func(i, j int) bool {
return commits[i].Timestamp < commits[j].Timestamp
})
}
Expand Down Expand Up @@ -812,6 +891,16 @@ func gatherPushSessions(ctx context.Context, store *state.Store, repoRoot, sessi

gitClient := tracegit.NewGitClient()

sessionRecords, err := store.LoadAllSessionRecords()
if err != nil {
log.Debug().Err(err).Msg("could not load session records")
}

// A rebase that ran without the hooks (for example the GitHub "Update
// branch" button) left branch commits with a trailer but no record.
// Record them again before the orphan filter drops their old records.
allCommits = recordRebasedCommits(gitClient, store, repoRoot, allCommits, sessionRecords, log)

// Drop orphan records (SHAs no longer in the branch — typically left over
// by a rebase) and any record that turns out to belong to a merge commit.
allCommits = filterCurrentBranchCommits(gitClient, repoRoot, allCommits, log)
Expand Down Expand Up @@ -860,11 +949,6 @@ func gatherPushSessions(ctx context.Context, store *state.Store, repoRoot, sessi
return nil, aiCommits, nil
}

sessionRecords, err := store.LoadAllSessionRecords()
if err != nil {
log.Debug().Err(err).Msg("could not load session records")
}

sessionCommits := sessionCommitGroups(aiCommits, sessionRecords, allowEmpty)
if len(sessionCommits) == 0 {
log.Debug().Msg("no AI coding sessions to record")
Expand Down
3 changes: 3 additions & 0 deletions app/cli/pkg/action/trace_hook_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ func (f *fakeBranchClient) Blame(string, string, string) ([]tracegit.BlameLine,

func (f *fakeBranchClient) CommitHeadInfo(string) (string, string, error) { return "", "", nil }
func (f *fakeBranchClient) HeadAuthorTime(string) (time.Time, error) { return time.Time{}, nil }
func (f *fakeBranchClient) BranchCommits(string, string) ([]tracegit.BranchCommit, error) {
return nil, nil
}
func (f *fakeBranchClient) GeneratedMatcher(string) func(string) bool {
return func(string) bool { return false }
}
Expand Down
Loading
Loading