Skip to content

Attribute files a late-joined session wrote before its first turn start - #3245

Open
peyton-alt wants to merge 1 commit into
mainfrom
fix/late-join-untracked-files
Open

peyton-alt wants to merge 1 commit into
mainfrom
fix/late-join-untracked-files

Conversation

@peyton-alt

@peyton-alt peyton-alt commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem

An agent session that is already running can write a new file into a repo before that repo sees its first turn start for the session. For example, the agent shell-copies client.js in from another folder. When the turn then starts, Entire records client.js as already untracked, so turn end treats it as pre-existing. The file is missing from files_touched and from the checkpoint.

This is more common under global enable (#2030), where one session moves between repos, but it happens on main as well.

Fix

At turn start, Entire now asks whether this session is joining the repo late. That is the case when:

  • the repo has no session state for it, and
  • the transcript already contains an earlier answered turn.

If so, untracked files modified at or after that earlier turn's start are not treated as pre-existing, so turn end reports them as new. Everything else keeps today's behaviour.

  • Adds the optional agent capability PreviousTurnStartReader, implemented for Claude Code (user prompts) and Codex (task_started events). Other agents are unchanged.
  • Only answered turns count, because the current prompt may or may not already be in the transcript when the hook fires.
  • On any error the existing baseline is kept. The debug log records counts only.

Not affected

  • A new session, including files the user creates before the first prompt. There is no earlier turn, so nothing changes.
  • A resumed session with an old transcript. Files older than the previous turn's start keep today's behaviour.
  • Agents without the capability.

Tests

  • Unit tests for the trimming rule and for each agent's reader.
  • Integration tests:
    • a late-joined shell copy is recorded;
    • a new session's pre-existing untracked file is not recorded.
  • Manual check with simulated Claude hooks (copy → turn start → Stop): client.js is now in files_touched. The other two event orders are unchanged.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XJeHKew3A4AUgjs8Habf7z


Note

Medium Risk
Turn-start baseline logic changes file attribution for sessions with prior transcript history; behavior is gated and fails open, but incorrect timestamps could misclassify files.

Overview
Fixes late-joined sessions missing files they created (e.g. shell copies) in files_touched because those untracked files were captured as pre-existing at the repo's first UserPromptSubmit.

At turn start, when the repo has no session state but the transcript already has an earlier answered turn, CapturePrePromptState now calls trimLateJoinBaseline: untracked files whose mod time is at or after that turn's start are dropped from the baseline so turn-end treats them as new.

Adds optional agent capability PreviousTurnStartReader (with AsPreviousTurnStartReader) and implements it for Claude Code (answered user prompts) and Codex (task_started + assistant). Failures keep the full baseline unchanged.

Includes unit tests for trimming and per-agent readers, plus integration tests for late-join shell copy vs. brand-new sessions.

Reviewed by Cursor Bugbot for commit 328ec12. Configure here.

A session already running elsewhere can write untracked files into a repo
during a turn before Entire sees its first TurnStart there. The pre-prompt
baseline recorded those files as pre-existing, so turn end never reported
them and files_touched and the checkpoint omitted them.

When no session state exists for the session and its transcript records an
earlier answered turn, drop untracked files modified since that turn started
from the baseline. Agents opt in through the new PreviousTurnStartReader
capability (Claude Code: last answered user prompt; Codex: last answered
task_started). Any failure keeps today's baseline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XJeHKew3A4AUgjs8Habf7z
@peyton-alt
peyton-alt requested a review from a team as a code owner November 28, 2026 06:37
Copilot AI balanced review requested due to automatic review settings November 28, 2026 06:37

Copilot AI 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.

🟡 Changes recommended

Turn-boundary detection mishandles sidechain replies and image-only prompts, producing incorrect baseline anchors that can miss copied files or attribute unrelated files.

2 open issues
What changed in this PR

This PR adjusts Entire’s file attribution for sessions that join a repository after working elsewhere, addressing a case that becomes more common with global enable.

Changes:

  • Adds an optional previous-turn reader for Claude Code and Codex.
  • Excludes recently modified untracked files from the first-turn baseline when prior answered-turn history exists, preserving existing behavior on errors.
  • Adds unit and integration coverage for late joins and new-session baseline preservation.
File Description
cmd/​entire/​cli/​state.go Applies late-join trimming during baseline capture.
cmd/​entire/​cli/​late_join_baseline.go Detects late joins and filters files by modification time.
cmd/​entire/​cli/​late_join_baseline_test.go Tests filtering and baseline-preservation conditions.
cmd/​entire/​cli/​integration_test/​shell_capture_test.go Tests late-copy attribution and new-session behavior.
cmd/​entire/​cli/​agent/​codex/​transcript.go Extracts a shared task_started event constant.
cmd/​entire/​cli/​agent/​codex/​previous_turn.go Reads the latest answered Codex turn’s start time.
cmd/​entire/​cli/​agent/​codex/​previous_turn_test.go Tests Codex turn selection.
cmd/​entire/​cli/​agent/​claudecode/​previous_turn.go Reads the latest answered Claude prompt’s timestamp.
cmd/​entire/​cli/​agent/​claudecode/​previous_turn_test.go Tests Claude turn selection and error handling.
cmd/​entire/​cli/​agent/​capabilities.go Adds the optional capability accessor.
cmd/​entire/​cli/​agent/​agent.go Defines the previous-turn reader interface.

Review effort: Balanced

Comment on lines +35 to +43
var header struct {
Type string `json:"type"`
Timestamp string `json:"timestamp"`
}
if json.Unmarshal(bytes.TrimSpace(raw), &header) != nil {
continue
}
if header.Type == transcriptlib.TypeAssistant && pending != "" {
answered, pending = pending, ""
Comment on lines +46 to +48
if _, ok := userPrompt(raw); ok {
pending = header.Timestamp
}

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 328ec12. Configure here.

}
kept = append(kept, f)
}
return kept, stats, dropped

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Late-join trim ignores change time

Medium Severity

dropFilesSince decides whether a late-joined untracked file is this session's work using only ModTime. UntrackedFileStat already records ChangeTime because agents often bring files in with timestamps preserved (mv across a sibling directory, cp -p/cp -a, rsync -a). Those files still look older than the previous turn start, stay in the baseline, and turn end omits them from files_touched and the checkpoint — the gap this change is meant to close.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 328ec12. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants