From e3813355226c6fe80756afe5b647d82ee5582c42 Mon Sep 17 00:00:00 2001 From: Jose Ignacio Paris Prieto Date: Fri, 20 Nov 2026 11:56:10 +0100 Subject: [PATCH 1/4] fix(trace): re-record rebased commits from their trailer A rebase outside the clone with the Trace hooks gives the branch commits new SHAs with no commit record, so the next pre-push dropped the old records as orphans and sent none of the rebased commits. Before the orphan filter, record each branch commit that has a Chainloop-Trace-Sessions trailer and no record, with the trailer sessions that have a local session record. Merge commits are skipped. Fixes #3838 Refs PFM-8286 Assisted-by: Claude Code Signed-off-by: Jose Ignacio Paris Prieto Chainloop-Trace-Sessions: 7fa5c0ca-dc5c-428e-83d2-5f528e3bd327 --- app/cli/internal/trace/README.md | 1 + app/cli/internal/trace/git/exec.go | 16 ++ app/cli/internal/trace/git/exec_test.go | 27 ++ app/cli/internal/trace/git/git.go | 3 + app/cli/internal/trace/git/gogit.go | 15 ++ app/cli/pkg/action/trace_hook_handler.go | 91 ++++++- app/cli/pkg/action/trace_hook_handler_test.go | 3 + .../pkg/action/trace_rebased_commits_test.go | 239 ++++++++++++++++++ 8 files changed, 390 insertions(+), 5 deletions(-) create mode 100644 app/cli/pkg/action/trace_rebased_commits_test.go diff --git a/app/cli/internal/trace/README.md b/app/cli/internal/trace/README.md index 6876b2320..11d0117ff 100644 --- a/app/cli/internal/trace/README.md +++ b/app/cli/internal/trace/README.md @@ -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 | diff --git a/app/cli/internal/trace/git/exec.go b/app/cli/internal/trace/git/exec.go index 70be48ef0..a2dda96ae 100644 --- a/app/cli/internal/trace/git/exec.go +++ b/app/cli/internal/trace/git/exec.go @@ -278,6 +278,22 @@ func (c *ExecClient) CommitHeadInfo(repoRoot string) (sha, message string, err e return sha, strings.TrimSpace(message), nil } +// CommitInfo returns the message and the committer date of the commit at sha. +func (c *ExecClient) CommitInfo(repoRoot, sha string) (string, time.Time, error) { + out := gitOutput(repoRoot, "log", "-1", "--format=%ct%n%B", sha, "--") + if out == "" { + return "", time.Time{}, fmt.Errorf("git log %s: no output", sha) + } + + ts, message, _ := strings.Cut(out, "\n") + secs, err := strconv.ParseInt(ts, 10, 64) + if err != nil { + return "", time.Time{}, fmt.Errorf("git log %s committer date %q: %w", sha, ts, err) + } + + return strings.TrimSpace(message), time.Unix(secs, 0), nil +} + // HeadAuthorTime returns the author date of HEAD. func (c *ExecClient) HeadAuthorTime(repoRoot string) (time.Time, error) { out := gitOutput(repoRoot, "log", "-1", "--format=%at", "HEAD") diff --git a/app/cli/internal/trace/git/exec_test.go b/app/cli/internal/trace/git/exec_test.go index e8200a285..81a636fc4 100644 --- a/app/cli/internal/trace/git/exec_test.go +++ b/app/cli/internal/trace/git/exec_test.go @@ -246,3 +246,30 @@ func TestMapGitStatus(t *testing.T) { assert.Equal(t, "modified", mapGitStatus("M")) assert.Equal(t, "modified", mapGitStatus("")) } + +func TestClient_CommitInfo(t *testing.T) { + cwd, err := os.Getwd() + require.NoError(t, err) + + headSHA, headMessage, err := NewExecClient().CommitHeadInfo(cwd) + require.NoError(t, err) + + clients := []struct { + name string + client Client + }{ + {name: "exec", client: NewExecClient()}, + {name: "go-git", client: NewGoGitClient()}, + } + for _, tc := range clients { + t.Run(tc.name, func(t *testing.T) { + message, committedAt, err := tc.client.CommitInfo(cwd, headSHA) + require.NoError(t, err) + assert.Equal(t, headMessage, message) + assert.False(t, committedAt.IsZero()) + + _, _, err = tc.client.CommitInfo(cwd, "deadbeef00000000000000000000000000000000") + assert.Error(t, err) + }) + } +} diff --git a/app/cli/internal/trace/git/git.go b/app/cli/internal/trace/git/git.go index 5c537ec05..f7f131c89 100644 --- a/app/cli/internal/trace/git/git.go +++ b/app/cli/internal/trace/git/git.go @@ -113,6 +113,9 @@ type Client interface { CommitHeadInfo(repoRoot string) (sha, message string, err error) // HeadAuthorTime returns the author date of HEAD. HeadAuthorTime(repoRoot string) (time.Time, error) + // CommitInfo returns the message and the committer date of the commit + // identified by sha. + CommitInfo(repoRoot, sha string) (message string, committedAt time.Time, err error) // GeneratedMatcher returns a predicate that reports whether a repo-relative // path is marked linguist-generated=true in the repository's .gitattributes. // Implementations must return a no-op matcher (always false) when attributes diff --git a/app/cli/internal/trace/git/gogit.go b/app/cli/internal/trace/git/gogit.go index f7f41b0ee..2af50abc9 100644 --- a/app/cli/internal/trace/git/gogit.go +++ b/app/cli/internal/trace/git/gogit.go @@ -199,6 +199,21 @@ func (c *GoGitClient) CommitHeadInfo(repoRoot string) (sha, message string, err return head.Hash().String(), strings.TrimSpace(commit.Message), nil } +// CommitInfo returns the message and the committer date of the commit at sha. +func (c *GoGitClient) CommitInfo(repoRoot, sha string) (string, time.Time, error) { + repo, err := openRepo(repoRoot) + if err != nil { + return "", time.Time{}, fmt.Errorf("open repo: %w", err) + } + + commit, err := repo.CommitObject(plumbing.NewHash(sha)) + if err != nil { + return "", time.Time{}, fmt.Errorf("get commit %s: %w", sha, err) + } + + return strings.TrimSpace(commit.Message), commit.Committer.When, nil +} + func (c *GoGitClient) HeadAuthorTime(repoRoot string) (time.Time, error) { repo, err := openRepo(repoRoot) if err != nil { diff --git a/app/cli/pkg/action/trace_hook_handler.go b/app/cli/pkg/action/trace_hook_handler.go index 93efd21b1..505213200 100644 --- a/app/cli/pkg/action/trace_hook_handler.go +++ b/app/cli/pkg/action/trace_hook_handler.go @@ -528,6 +528,82 @@ 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 + } + + branchSHAs, err := client.BranchCommitSHAs(repoRoot) + 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{}{} + } + + for _, sha := range branchSHAs { + if _, ok := recorded[sha]; ok { + continue + } + + message, committedAt, err := client.CommitInfo(repoRoot, sha) + if err != nil { + log.Debug().Err(err).Str("sha", sha).Msg("could not read commit, skipping rebased commit recovery") + continue + } + + var sessionIDs []string + for _, sid := range state.ParseSessionIDsFromTrailer(message) { + if _, known := sessionRecords[sid]; known { + sessionIDs = append(sessionIDs, sid) + } + } + if len(sessionIDs) == 0 { + continue + } + + isMerge, err := client.IsMergeCommit(repoRoot, sha) + if err != nil || isMerge { + continue + } + + rec := &state.CommitRecord{ + SHA: sha, + Message: message, + SessionIDs: sessionIDs, + // The committer date keeps the branch order of the commits, which + // the time of this push would lose. + Timestamp: committedAt.UTC().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) + } + + return records +} + // RunTracePushOpts configures RunTracePush behaviour. type RunTracePushOpts struct { // AllowEmpty makes the push attest every recorded session even when @@ -812,6 +888,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) @@ -860,11 +946,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") diff --git a/app/cli/pkg/action/trace_hook_handler_test.go b/app/cli/pkg/action/trace_hook_handler_test.go index 6947fbc63..0137ec9b4 100644 --- a/app/cli/pkg/action/trace_hook_handler_test.go +++ b/app/cli/pkg/action/trace_hook_handler_test.go @@ -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) CommitInfo(string, string) (string, time.Time, error) { + return "", time.Time{}, nil +} func (f *fakeBranchClient) GeneratedMatcher(string) func(string) bool { return func(string) bool { return false } } diff --git a/app/cli/pkg/action/trace_rebased_commits_test.go b/app/cli/pkg/action/trace_rebased_commits_test.go new file mode 100644 index 000000000..22facd608 --- /dev/null +++ b/app/cli/pkg/action/trace_rebased_commits_test.go @@ -0,0 +1,239 @@ +// +// 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 ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + tracegit "github.com/chainloop-dev/chainloop/app/cli/internal/trace/git" + "github.com/chainloop-dev/chainloop/app/cli/internal/trace/state" + "github.com/rs/zerolog" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + knownSession = "11111111-1111-4111-8111-111111111111" + unknownSession = "22222222-2222-4222-8222-222222222222" + humanSubject = "human commit" +) + +// rebasedBranch is a feature branch whose commits were rebased without the +// hooks, as the GitHub "Update branch" button does. +type rebasedBranch struct { + dir string + store *state.Store + // oldSHAs are the SHAs that post-commit recorded before the rebase. + oldSHAs []string + // shaBySubject maps the subject of each commit to its SHA after the rebase. + shaBySubject map[string]string +} + +// setupRebasedBranch builds a branch with two recorded commits of +// knownSession, then rebases it onto a new main without running any hook. It +// also adds, before the rebase, commits with a trailer that post-commit never +// recorded: one for an unknown session, one for both sessions, and a merge. +func setupRebasedBranch(t *testing.T) *rebasedBranch { + t.Helper() + + dir, gitDir := initGitRepo(t) + store := state.NewGitStore(gitDir) + require.NoError(t, store.InitTraceDir()) + require.NoError(t, store.SaveSessionRecord(&state.SessionRecord{ + SessionID: knownSession, StartedAt: state.NowTimestamp(), + })) + runGit(t, dir, "update-ref", "refs/remotes/origin/main", "HEAD") + + commit := func(file, subject, trailer string) { + require.NoError(t, os.WriteFile(filepath.Join(dir, file), []byte(subject+"\n"), 0600)) + runGit(t, dir, "add", file) + msg := subject + if trailer != "" { + msg += "\n\n" + state.TrailerKey + ": " + trailer + "\n" + } + runGit(t, dir, "commit", "-m", msg) + } + + runGit(t, dir, "checkout", "-b", "feature") + oldSHAs := make([]string, 0, 2) + for _, f := range []string{"a", "b"} { + commit(f+".txt", "ai "+f, knownSession) + require.NoError(t, handlePostCommit(t.Context(), zerolog.Nop())) + oldSHAs = append(oldSHAs, headSHA(t, dir)) + } + commit("c.txt", "unknown session", unknownSession) + commit("d.txt", "mixed sessions", knownSession+", "+unknownSession) + commit("e.txt", humanSubject, "") + + // Upstream moves, then the branch is rebased with no hook. + runGit(t, dir, "checkout", "main") + commit("upstream.txt", "upstream", "") + runGit(t, dir, "update-ref", "refs/remotes/origin/main", "HEAD") + runGit(t, dir, "checkout", "feature") + runGit(t, dir, "-c", "core.hooksPath=/dev/null", "rebase", "main") + + return &rebasedBranch{dir: dir, store: store, oldSHAs: oldSHAs, shaBySubject: branchSubjects(t, dir)} +} + +// branchSubjects returns the SHA of each commit on HEAD that is not on main, +// by its subject. +func branchSubjects(t *testing.T, dir string) map[string]string { + t.Helper() + cmd := exec.Command("git", "log", "--format=%H %s", "main..HEAD") + cmd.Dir = dir + out, err := cmd.Output() + require.NoError(t, err) + + got := make(map[string]string) + for line := range strings.SplitSeq(strings.TrimSpace(string(out)), "\n") { + sha, subject, _ := strings.Cut(line, " ") + got[subject] = sha + } + + return got +} + +func recordsBySHA(records []*state.CommitRecord) map[string]*state.CommitRecord { + out := make(map[string]*state.CommitRecord, len(records)) + for _, r := range records { + out[r.SHA] = r + } + + return out +} + +func TestRecordRebasedCommits(t *testing.T) { + b := setupRebasedBranch(t) + + records, err := b.store.LoadAllCommitRecords() + require.NoError(t, err) + sessions, err := b.store.LoadAllSessionRecords() + require.NoError(t, err) + + got := recordsBySHA(recordRebasedCommits(tracegit.NewGitClient(), b.store, b.dir, records, sessions, zerolog.Nop())) + + testCases := []struct { + subject string + wantRecorded bool + wantSessions []string + }{ + {subject: "ai a", wantRecorded: true, wantSessions: []string{knownSession}}, + {subject: "ai b", wantRecorded: true, wantSessions: []string{knownSession}}, + {subject: "unknown session", wantRecorded: false}, + {subject: "mixed sessions", wantRecorded: true, wantSessions: []string{knownSession}}, + {subject: humanSubject, wantRecorded: false}, + } + for _, tc := range testCases { + t.Run(tc.subject, func(t *testing.T) { + sha := b.shaBySubject[tc.subject] + require.NotEmpty(t, sha) + + rec, ok := got[sha] + assert.Equal(t, tc.wantRecorded, ok) + if !tc.wantRecorded { + return + } + assert.Equal(t, tc.wantSessions, rec.SessionIDs) + assert.False(t, rec.Tracked) + assert.Equal(t, tc.wantSessions, mustLoadRecord(t, b.store, sha).SessionIDs, "record must be saved") + }) + } + + t.Run("keeps the existing records", func(t *testing.T) { + for _, sha := range b.oldSHAs { + assert.Contains(t, got, sha) + } + }) + + t.Run("does not overwrite an existing record", func(t *testing.T) { + sha := b.shaBySubject["ai a"] + existing := &state.CommitRecord{SHA: sha, Message: "kept", SessionIDs: []string{knownSession}, Tracked: true, Timestamp: state.NowTimestamp()} + require.NoError(t, b.store.SaveCommitRecord(existing)) + + all, err := b.store.LoadAllCommitRecords() + require.NoError(t, err) + again := recordsBySHA(recordRebasedCommits(tracegit.NewGitClient(), b.store, b.dir, all, sessions, zerolog.Nop())) + assert.True(t, again[sha].Tracked) + assert.Equal(t, "kept", mustLoadRecord(t, b.store, sha).Message) + }) +} + +func TestRecordRebasedCommits_SkipsMergeCommits(t *testing.T) { + dir, gitDir := initGitRepo(t) + store := state.NewGitStore(gitDir) + require.NoError(t, store.InitTraceDir()) + require.NoError(t, store.SaveSessionRecord(&state.SessionRecord{SessionID: knownSession, StartedAt: state.NowTimestamp()})) + runGit(t, dir, "update-ref", "refs/remotes/origin/main", "HEAD") + + runGit(t, dir, "checkout", "-b", "side") + require.NoError(t, os.WriteFile(filepath.Join(dir, "side.txt"), []byte("side\n"), 0600)) + runGit(t, dir, "add", "side.txt") + runGit(t, dir, "commit", "-m", "side") + runGit(t, dir, "checkout", "-b", "feature", "main") + require.NoError(t, os.WriteFile(filepath.Join(dir, "f.txt"), []byte("f\n"), 0600)) + runGit(t, dir, "add", "f.txt") + runGit(t, dir, "commit", "-m", "feature") + runGit(t, dir, "merge", "--no-ff", "side", "-m", "merge\n\n"+state.TrailerKey+": "+knownSession+"\n") + mergeSHA := headSHA(t, dir) + + got := recordsBySHA(recordRebasedCommits(tracegit.NewGitClient(), store, dir, nil, map[string]*state.SessionRecord{knownSession: {SessionID: knownSession}}, zerolog.Nop())) + assert.NotContains(t, got, mergeSHA) +} + +// TestGatherPushSessions_AfterRebaseOutsideClone covers the acceptance +// criteria of issue 3838: after a rebase that ran without the hooks, the next +// push sends the session with the rebased SHAs, with or without a new commit. +func TestGatherPushSessions_AfterRebaseOutsideClone(t *testing.T) { + testCases := []struct { + name string + newCommit bool + }{ + {name: "push with no new commit"}, + {name: "push with one new commit", newCommit: true}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + b := setupRebasedBranch(t) + want := []string{b.shaBySubject["ai a"], b.shaBySubject["ai b"], b.shaBySubject["mixed sessions"]} + + if tc.newCommit { + require.NoError(t, os.WriteFile(filepath.Join(b.dir, "new.txt"), []byte("new\n"), 0600)) + runGit(t, b.dir, "add", "new.txt") + runGit(t, b.dir, "commit", "-m", "new\n\n"+state.TrailerKey+": "+knownSession+"\n") + require.NoError(t, handlePostCommit(t.Context(), zerolog.Nop())) + want = append(want, headSHA(t, b.dir)) + } + + _, commits, err := gatherPushSessions(t.Context(), b.store, b.dir, "", false, zerolog.Nop()) + require.NoError(t, err) + + var got []string + for _, c := range commits { + if len(c.SessionIDs) > 0 { + got = append(got, c.SHA) + } + } + assert.ElementsMatch(t, want, got) + for _, sha := range b.oldSHAs { + assert.NotContains(t, got, sha, "old SHAs are no longer on the branch") + } + }) + } +} From a10583893c5adee931cf2472e49f81b1d3d3ec35 Mon Sep 17 00:00:00 2001 From: Jose Ignacio Paris Prieto Date: Fri, 20 Nov 2026 12:09:23 +0100 Subject: [PATCH 2/4] test(trace): reuse testClients in the CommitInfo test Fixes the goconst lint findings of the new test. Refs #3838 Assisted-by: Claude Code Signed-off-by: Jose Ignacio Paris Prieto Chainloop-Trace-Sessions: 7fa5c0ca-dc5c-428e-83d2-5f528e3bd327 --- app/cli/internal/trace/git/exec_test.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/app/cli/internal/trace/git/exec_test.go b/app/cli/internal/trace/git/exec_test.go index 81a636fc4..5e8a66976 100644 --- a/app/cli/internal/trace/git/exec_test.go +++ b/app/cli/internal/trace/git/exec_test.go @@ -254,21 +254,14 @@ func TestClient_CommitInfo(t *testing.T) { headSHA, headMessage, err := NewExecClient().CommitHeadInfo(cwd) require.NoError(t, err) - clients := []struct { - name string - client Client - }{ - {name: "exec", client: NewExecClient()}, - {name: "go-git", client: NewGoGitClient()}, - } - for _, tc := range clients { - t.Run(tc.name, func(t *testing.T) { - message, committedAt, err := tc.client.CommitInfo(cwd, headSHA) + for name, client := range testClients() { + t.Run(name, func(t *testing.T) { + message, committedAt, err := client.CommitInfo(cwd, headSHA) require.NoError(t, err) assert.Equal(t, headMessage, message) assert.False(t, committedAt.IsZero()) - _, _, err = tc.client.CommitInfo(cwd, "deadbeef00000000000000000000000000000000") + _, _, err = client.CommitInfo(cwd, "deadbeef00000000000000000000000000000000") assert.Error(t, err) }) } From e8f88487d86339dbdcf5d302bdaf10ddbcc1475d Mon Sep 17 00:00:00 2001 From: Jose Ignacio Paris Prieto Date: Fri, 20 Nov 2026 12:41:45 +0100 Subject: [PATCH 3/4] fix(trace): keep the branch order of re-recorded rebased commits A rebase replays its commits in the same second, so the committer dates of the re-recorded commits tie and the session sort could put them out of order. Walk the branch oldest first, give each new record a timestamp after the previous one, and sort the session commits with a stable sort. Refs #3838 Assisted-by: Claude Code Signed-off-by: Jose Ignacio Paris Prieto Chainloop-Trace-Sessions: 7fa5c0ca-dc5c-428e-83d2-5f528e3bd327 --- app/cli/internal/trace/git/exec.go | 2 +- app/cli/internal/trace/git/git.go | 3 ++- app/cli/pkg/action/trace_hook_handler.go | 20 ++++++++++++++----- .../pkg/action/trace_rebased_commits_test.go | 10 ++++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/cli/internal/trace/git/exec.go b/app/cli/internal/trace/git/exec.go index a2dda96ae..b528e2b5f 100644 --- a/app/cli/internal/trace/git/exec.go +++ b/app/cli/internal/trace/git/exec.go @@ -342,7 +342,7 @@ func (c *ExecClient) IsMergeCommit(repoRoot, sha string) (bool, error) { // `git rev-list HEAD ^` (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) } diff --git a/app/cli/internal/trace/git/git.go b/app/cli/internal/trace/git/git.go index f7f131c89..a76fd72b7 100644 --- a/app/cli/internal/trace/git/git.go +++ b/app/cli/internal/trace/git/git.go @@ -139,7 +139,8 @@ 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) // LocalReachableSHAs returns the union of SHAs reachable from every // local branch tip (refs/heads/*). Used by the post-push GC to identify diff --git a/app/cli/pkg/action/trace_hook_handler.go b/app/cli/pkg/action/trace_hook_handler.go index 505213200..609f9e364 100644 --- a/app/cli/pkg/action/trace_hook_handler.go +++ b/app/cli/pkg/action/trace_hook_handler.go @@ -558,7 +558,11 @@ func recordRebasedCommits(client tracegit.Client, store *state.Store, repoRoot s recorded[rec.SHA] = struct{}{} } - for _, sha := range branchSHAs { + // 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 _, sha := range slices.Backward(branchSHAs) { if _, ok := recorded[sha]; ok { continue } @@ -584,13 +588,19 @@ func recordRebasedCommits(client tracegit.Client, store *state.Store, repoRoot s continue } + // The committer date keeps the commits before a commit made after + // the pull, which the time of this push would not. + ts := committedAt.UTC().Truncate(time.Second) + if !ts.After(prev) { + ts = prev.Add(time.Second) + } + prev = ts + rec := &state.CommitRecord{ SHA: sha, Message: message, SessionIDs: sessionIDs, - // The committer date keeps the branch order of the commits, which - // the time of this push would lose. - Timestamp: committedAt.UTC().Format(time.RFC3339), + 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") @@ -853,7 +863,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 }) } diff --git a/app/cli/pkg/action/trace_rebased_commits_test.go b/app/cli/pkg/action/trace_rebased_commits_test.go index 22facd608..33b856861 100644 --- a/app/cli/pkg/action/trace_rebased_commits_test.go +++ b/app/cli/pkg/action/trace_rebased_commits_test.go @@ -156,6 +156,16 @@ func TestRecordRebasedCommits(t *testing.T) { }) } + t.Run("timestamps follow the branch order", func(t *testing.T) { + // The rebase replays all commits in the same second, so equal + // committer dates must not make the order ambiguous. + a := got[b.shaBySubject["ai a"]].Timestamp + bb := got[b.shaBySubject["ai b"]].Timestamp + mixed := got[b.shaBySubject["mixed sessions"]].Timestamp + assert.Less(t, a, bb) + assert.Less(t, bb, mixed) + }) + t.Run("keeps the existing records", func(t *testing.T) { for _, sha := range b.oldSHAs { assert.Contains(t, got, sha) From 2d887737f2d009fba26c4e3ed0dcdd718ea27e65 Mon Sep 17 00:00:00 2001 From: Jose Ignacio Paris Prieto Date: Fri, 20 Nov 2026 14:17:20 +0100 Subject: [PATCH 4/4] perf(trace): read the trailer commits of the branch in one pass Rebased commit recovery ran one git log per unrecorded branch commit on each push, which is the whole history when no base branch is found. Replace CommitInfo with BranchCommits, which lists in one read the branch commits whose message contains the trailer, with their parent count, so the merge check needs no extra call either. Refs #3838 Assisted-by: Claude Code Signed-off-by: Jose Ignacio Paris Prieto Chainloop-Trace-Sessions: 7fa5c0ca-dc5c-428e-83d2-5f528e3bd327 --- app/cli/internal/trace/git/exec.go | 69 +++++++++++++----- app/cli/internal/trace/git/exec_test.go | 67 ++++++++++++----- app/cli/internal/trace/git/git.go | 16 ++++- app/cli/internal/trace/git/gogit.go | 71 +++++++++++-------- app/cli/pkg/action/trace_hook_handler.go | 27 +++---- app/cli/pkg/action/trace_hook_handler_test.go | 4 +- 6 files changed, 172 insertions(+), 82 deletions(-) diff --git a/app/cli/internal/trace/git/exec.go b/app/cli/internal/trace/git/exec.go index b528e2b5f..6bb864c44 100644 --- a/app/cli/internal/trace/git/exec.go +++ b/app/cli/internal/trace/git/exec.go @@ -278,22 +278,6 @@ func (c *ExecClient) CommitHeadInfo(repoRoot string) (sha, message string, err e return sha, strings.TrimSpace(message), nil } -// CommitInfo returns the message and the committer date of the commit at sha. -func (c *ExecClient) CommitInfo(repoRoot, sha string) (string, time.Time, error) { - out := gitOutput(repoRoot, "log", "-1", "--format=%ct%n%B", sha, "--") - if out == "" { - return "", time.Time{}, fmt.Errorf("git log %s: no output", sha) - } - - ts, message, _ := strings.Cut(out, "\n") - secs, err := strconv.ParseInt(ts, 10, 64) - if err != nil { - return "", time.Time{}, fmt.Errorf("git log %s committer date %q: %w", sha, ts, err) - } - - return strings.TrimSpace(message), time.Unix(secs, 0), nil -} - // HeadAuthorTime returns the author date of HEAD. func (c *ExecClient) HeadAuthorTime(repoRoot string) (time.Time, error) { out := gitOutput(repoRoot, "log", "-1", "--format=%at", "HEAD") @@ -355,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") diff --git a/app/cli/internal/trace/git/exec_test.go b/app/cli/internal/trace/git/exec_test.go index 5e8a66976..c6c1be112 100644 --- a/app/cli/internal/trace/git/exec_test.go +++ b/app/cli/internal/trace/git/exec_test.go @@ -18,6 +18,7 @@ package git import ( "os" "path/filepath" + "slices" "testing" "github.com/stretchr/testify/assert" @@ -247,22 +248,56 @@ func TestMapGitStatus(t *testing.T) { assert.Equal(t, "modified", mapGitStatus("")) } -func TestClient_CommitInfo(t *testing.T) { - cwd, err := os.Getwd() - require.NoError(t, err) - - headSHA, headMessage, err := NewExecClient().CommitHeadInfo(cwd) - require.NoError(t, err) - +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() { - t.Run(name, func(t *testing.T) { - message, committedAt, err := client.CommitInfo(cwd, headSHA) - require.NoError(t, err) - assert.Equal(t, headMessage, message) - assert.False(t, committedAt.IsZero()) - - _, _, err = client.CommitInfo(cwd, "deadbeef00000000000000000000000000000000") - assert.Error(t, err) - }) + 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) + }) + } } } diff --git a/app/cli/internal/trace/git/git.go b/app/cli/internal/trace/git/git.go index a76fd72b7..ba857431f 100644 --- a/app/cli/internal/trace/git/git.go +++ b/app/cli/internal/trace/git/git.go @@ -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. +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) @@ -113,9 +122,6 @@ type Client interface { CommitHeadInfo(repoRoot string) (sha, message string, err error) // HeadAuthorTime returns the author date of HEAD. HeadAuthorTime(repoRoot string) (time.Time, error) - // CommitInfo returns the message and the committer date of the commit - // identified by sha. - CommitInfo(repoRoot, sha string) (message string, committedAt time.Time, err error) // GeneratedMatcher returns a predicate that reports whether a repo-relative // path is marked linguist-generated=true in the repository's .gitattributes. // Implementations must return a no-op matcher (always false) when attributes @@ -142,6 +148,10 @@ type Client interface { // 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 diff --git a/app/cli/internal/trace/git/gogit.go b/app/cli/internal/trace/git/gogit.go index 2af50abc9..2b0334fd1 100644 --- a/app/cli/internal/trace/git/gogit.go +++ b/app/cli/internal/trace/git/gogit.go @@ -199,21 +199,6 @@ func (c *GoGitClient) CommitHeadInfo(repoRoot string) (sha, message string, err return head.Hash().String(), strings.TrimSpace(commit.Message), nil } -// CommitInfo returns the message and the committer date of the commit at sha. -func (c *GoGitClient) CommitInfo(repoRoot, sha string) (string, time.Time, error) { - repo, err := openRepo(repoRoot) - if err != nil { - return "", time.Time{}, fmt.Errorf("open repo: %w", err) - } - - commit, err := repo.CommitObject(plumbing.NewHash(sha)) - if err != nil { - return "", time.Time{}, fmt.Errorf("get commit %s: %w", sha, err) - } - - return strings.TrimSpace(commit.Message), commit.Committer.When, nil -} - func (c *GoGitClient) HeadAuthorTime(repoRoot string) (time.Time, error) { repo, err := openRepo(repoRoot) if err != nil { @@ -427,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 @@ -459,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 diff --git a/app/cli/pkg/action/trace_hook_handler.go b/app/cli/pkg/action/trace_hook_handler.go index 609f9e364..243e6d885 100644 --- a/app/cli/pkg/action/trace_hook_handler.go +++ b/app/cli/pkg/action/trace_hook_handler.go @@ -546,7 +546,10 @@ func recordRebasedCommits(client tracegit.Client, store *state.Store, repoRoot s return records } - branchSHAs, err := client.BranchCommitSHAs(repoRoot) + // 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") @@ -562,19 +565,14 @@ func recordRebasedCommits(client tracegit.Client, store *state.Store, repoRoot s // 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 _, sha := range slices.Backward(branchSHAs) { - if _, ok := recorded[sha]; ok { - continue - } - - message, committedAt, err := client.CommitInfo(repoRoot, sha) - if err != nil { - log.Debug().Err(err).Str("sha", sha).Msg("could not read commit, skipping rebased commit recovery") + 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(message) { + for _, sid := range state.ParseSessionIDsFromTrailer(commit.Message) { if _, known := sessionRecords[sid]; known { sessionIDs = append(sessionIDs, sid) } @@ -583,14 +581,9 @@ func recordRebasedCommits(client tracegit.Client, store *state.Store, repoRoot s continue } - isMerge, err := client.IsMergeCommit(repoRoot, sha) - if err != nil || isMerge { - continue - } - // The committer date keeps the commits before a commit made after // the pull, which the time of this push would not. - ts := committedAt.UTC().Truncate(time.Second) + ts := commit.CommittedAt.UTC().Truncate(time.Second) if !ts.After(prev) { ts = prev.Add(time.Second) } @@ -598,7 +591,7 @@ func recordRebasedCommits(client tracegit.Client, store *state.Store, repoRoot s rec := &state.CommitRecord{ SHA: sha, - Message: message, + Message: commit.Message, SessionIDs: sessionIDs, Timestamp: ts.Format(time.RFC3339), } diff --git a/app/cli/pkg/action/trace_hook_handler_test.go b/app/cli/pkg/action/trace_hook_handler_test.go index 0137ec9b4..58b953e4b 100644 --- a/app/cli/pkg/action/trace_hook_handler_test.go +++ b/app/cli/pkg/action/trace_hook_handler_test.go @@ -312,8 +312,8 @@ 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) CommitInfo(string, string) (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 }