Skip to content

fix(trace): re-record rebased commits from their trailer - #3839

Merged
jiparis merged 4 commits into
chainloop-dev:mainfrom
jiparis:fix/trace-rebased-commits-from-trailer
Nov 20, 2026
Merged

jiparis merged 4 commits into
chainloop-dev:mainfrom
jiparis:fix/trace-rebased-commits-from-trailer

Conversation

@jiparis

@jiparis jiparis commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

A rebase that runs outside the clone with the Chainloop Trace hooks (for example the GitHub "Update branch" button, or a rebase in another clone) gives the branch commits new SHAs that have no commit record. The next pre-push then dropped the old records as orphans and sent none of the rebased commits, so the session lost their files and AI line attribution, or was not sent at all.

Before the orphan filter, the push now records each commit of the branch that carries a Chainloop-Trace-Sessions trailer and has no record. It keeps only the trailer sessions that have a local session record, because the commit author controls the trailer text. Merge commits are skipped, as in post-commit, and existing records are not changed. The next push then sends all the commits of the session with their new SHAs, with or without a new commit.

The git client gets a CommitInfo method to read the message and committer date of any commit. The Trace README feature table documents the new behavior.

This is the client part of PFM-8286. A rebase on GitHub that is not followed by a push from a clone with the hooks still needs the server-side fix.

Fixes #3838

AI disclosure: this change was produced with the assistance of Claude Code.

🤖 Generated with Claude Code

View guided diff

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 chainloop-dev#3838
Refs PFM-8286

Assisted-by: Claude Code
Signed-off-by: Jose Ignacio Paris Prieto <jiparis@chainloop.dev>

Chainloop-Trace-Sessions: 7fa5c0ca-dc5c-428e-83d2-5f528e3bd327
@chainloop-platform

chainloop-platform Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

PR validation — ✅ 3 passing

Status Policy Material Messages
✅ Passed pr-min-approvals pr-info -
✅ Passed pr-description-required pr-info -
✅ Passed pr-user-story-linked pr-info -

View attestation ↗

AI Session Checks — 🟡 84% · ✅ 0 failing

Avg score Sessions Failing policies Attribution Files Lines Total Duration
🟡 84% 1 ✅ 0 98% AI / 2% Human 8 +510 / -21 2h35m48s

🟡 84% — 98% AI — ✅ All policies passing

Nov 20, 2026 10:41 UTC · 2h35m48s · $9.62 · 310 in / 106.1k out · claude-code 2.1.329 (claude-opus-5-5)

View session details ↗

Change Summary

Recovers missing branch commit records from trailers before orphan filtering, retaining locally known sessions and skipping merges. Batches commit metadata reads, stabilizes record ordering, and adds recovery tests and documentation.

AI Session Overall Score

🟡 84% — Focused recovery fix with passing tests; ordering claims and code-change coverage need review.

AI Session Analysis Breakdown

🟢 96% · scope-discipline

No notes.

🟢 93% · user-trust-signal

No notes.

🟢 92% · context-and-planning

No notes.

🟢 90% · solution-quality

🟢 Persists missing records before orphan filtering instead of bypassing filtering or validation. · High Impact

🟡 74% · verification

🟢 Named recovery and merge-rejection tests passed against temporary repositories before the initial push. · High Impact

🟠 Acceptance tests assert recovered SHAs but discard session evidence, leaving combined code changes unasserted. · Medium Severity

💡 Assert gathered session files and code changes alongside recovered SHAs in both push cases.

🟡 72% · alignment

🟢 Explained recovery-gate tradeoffs and obtained approval before retaining existing trailer behavior. · High Impact

🟠 Initial summary claimed committer dates preserve branch order; review exposed same-second ties and prompted a fix. · Medium Severity

💡 Check timestamp ties before claiming that dates preserve commit order.


File Attribution

███████████████████░ 98% AI / 2% Human

Status Attribution File Lines
created ai app/cli/pkg/action/trace_rebased_commits_test.go +249 / -0
modified ai app/cli/pkg/action/trace_hook_handler.go +90 / -6
modified ai app/cli/internal/trace/git/gogit.go +43 / -13
modified ai app/cli/internal/trace/git/exec.go +54 / -1
modified ai app/cli/internal/trace/git/exec_test.go +55 / -0
modified ai app/cli/internal/trace/git/git.go +15 / -1
modified ai app/cli/pkg/action/trace_hook_handler_test.go +3 / -0
modified ai app/cli/internal/trace/README.md +1 / -0

Policies (4)

Status Policy Material Messages
✅ Passed ai-config-ai-agents-allowed ai-coding-session-7fa5c0 -
✅ Passed ai-config-no-dangerous-commands ai-coding-session-7fa5c0 -
✅ Passed ai-config-no-secrets ai-coding-session-7fa5c0 -
✅ Passed ai-config-mcp-servers-allowed ai-coding-session-7fa5c0 -

Security Checks — ⚠️ 1 failing

⛔ The AI Security Analysis check blocks this merge. An active finding at or above the high severity floor was found on the current head. Where this check is required in branch protection, the merge stays blocked until the finding is resolved or the required check is bypassed.

⚠️ AI Security Analysis

[1 finding · 1 high] The grounded audit checked this change against the project's security context and flagged code a past fix guards against.

P1 app/cli/pkg/action/trace_hook_handler.go:544 ▶ · fails-to-guard · logic flaw · CWE-840 · CVSS 6.4 · confidence: high

Trailer-only commit recovery bypasses session ownership checks

Why it matters — An attacker can include an unrelated commit and its code changes in a signed evidence document for an earlier AI session, falsely attributing that work to the session.

Precondition: An attacker needs local commit/push capability in a repository with Chainloop trace hooks enabled and residual trace state from an earlier AI session (or a stale session record). By touching a file that old session once edited, or by relying on a finished session that still looks active, they could trigger commit-msg/post-commit/pre-push to reuse that session ID on unrelated work.

Show detail — the change · how to fix · related fixes

The change

func recordRebasedCommits(client tracegit.Client, store *state.Store, repoRoot string, records []*state.CommitRecord, sessionRecords map[string]*state.SessionRecord, log zerolog.Logger) []*state.Commi …
	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")

How to fix — Require a verified replay match against a previously recorded session-owned commit, such as an orphan record, before saving recovered ownership. If equivalence cannot be verified, skip recovery rather than persisting ownership derived solely from a known session trailer.

Related fixes & locations

06ec131 — Only uncommitted, still-owned AI edits may credit a commit or session evidence document; once those edits land, are superseded, or the session has ended and nothing remains to attest, that session must stop being attached to later unrelated commits and pushes.

View in Chainloop ↗ · How this works ↗

✅ secret-scan

Status Policy Messages
✅ Passed secrets-detection -

✅ sast-scan

Status Policy Messages
✅ Passed owasp-top10-2025 -
✅ Passed sast -
✅ Passed cwe-top25 -
✅ Passed cwe-top26-40-cusp -
Scans not applied (3)
Scan Reason
vulnerability-scan no manifest/lockfile changed
github-actions-scan no workflow files changed
iac-scan no IaC files changed

View attestation ↗


Powered by Chainloop and Chainloop Trace

@jiparis
jiparis requested a review from a team November 20, 2026 10:57

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 8 files

Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack

View guided diff | Re-trigger cubic

Fixes the goconst lint findings of the new test.

Refs chainloop-dev#3838

Assisted-by: Claude Code
Signed-off-by: Jose Ignacio Paris Prieto <jiparis@chainloop.dev>

Chainloop-Trace-Sessions: 7fa5c0ca-dc5c-428e-83d2-5f528e3bd327
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 chainloop-dev#3838

Assisted-by: Claude Code
Signed-off-by: Jose Ignacio Paris Prieto <jiparis@chainloop.dev>

Chainloop-Trace-Sessions: 7fa5c0ca-dc5c-428e-83d2-5f528e3bd327

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

View guided diff | Re-trigger cubic

@jiparis

jiparis commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

On the AI Security Analysis finding at trace_hook_handler.go (recordRebasedCommits, "Unverified trailers attach unrelated commits to finished sessions"): we keep the current behaviour. Reasons:

  • The trailer cannot raise the AI line count. The AI/human split is based on content. An added line is AI only when it matches a line that an agent edit of the session wrote, and a removed line is AI only when an agent edit removed it. A commit that gets in with a forged trailer adds lines that count as human. The effect is limited to the scope of the session: its commit list, files and totals include the foreign commit, so the AI share goes down, not up.
  • The precondition already gives this capability. A forged trailer needs commit access to the clone with the hooks. The existing post-commit path trusts the trailer more than this change: it records the trailer sessions without a check for a local session record.
  • The proposed gates add little and lose real cases.
    • A per-session gate ("the session has an old record that is no longer on the branch") is usually open already: a local commit --amend leaves such a record.
    • Patch-id matching drops commits whose rebase resolved a conflict.
    • Both fail when the post-push cleanup has already removed the old records, for example after a push of another branch between the pull and the next push of this branch.

The server-side fix in PFM-8286 should validate the trailer against the repository and branch of the PR, which covers the cross-repository case.

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 chainloop-dev#3838

Assisted-by: Claude Code
Signed-off-by: Jose Ignacio Paris Prieto <jiparis@chainloop.dev>

Chainloop-Trace-Sessions: 7fa5c0ca-dc5c-428e-83d2-5f528e3bd327

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread app/cli/internal/trace/git/git.go Resolved
@jiparis
jiparis merged commit 679e033 into chainloop-dev:main Nov 20, 2026
20 of 22 checks passed
@jiparis
jiparis deleted the fix/trace-rebased-commits-from-trailer branch November 20, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trace: re-record rebased commits from their trailer when a rebase ran outside the clone

2 participants