Bug Description
When a commit involves multiple AI agent sessions (e.g., Cursor + Codex), prompts inherited from the INITIAL state that have no new checkpoint data in the current working session get their total_additions reset to 0. This causes the reported total_ai_additions to be less than ai_accepted (or ai_additions), which is logically impossible since total_ai_additions represents all AI-generated lines throughout a session, while ai_accepted represents the subset that survived into the final commit.
Steps to Reproduce
- Start a coding session with Agent A (e.g., Cursor) — Agent A generates some code, creating checkpoint data with
line_stats.additions > 0
- Switch to Agent B (e.g., Codex) — Agent B generates additional code in a separate session, also creating checkpoint data
- Commit the changes — both agents' contributions are present in the final commit
Expected: Both agents have total_additions > 0 reflecting their checkpoint line_stats
Actual: Agent B's prompt (inherited from INITIAL) has total_additions = 0 because no new checkpoints exist for Agent B's session ID in the current working log
Root Cause
In VirtualAttributions::calculate_and_update_prompt_metrics() (src/authorship/virtual_attribution.rs), the code unconditionally overwrites total_additions / total_deletions from the session_additions / session_deletions maps:
// Current behavior (buggy):
prompt_record.total_additions = session_additions.get(session_id).copied().unwrap_or(0);
prompt_record.total_deletions = session_deletions.get(session_id).copied().unwrap_or(0);
When a prompt is inherited from INITIAL attributions (e.g., from a previous agent session), its PromptRecord already carries the correct total_additions value. However, if no new checkpoints exist for that session in the current working log, session_additions won't contain the session ID, and unwrap_or(0) resets the value to 0.
This affects all code paths that call calculate_and_update_prompt_metrics:
from_just_working_log()
from_working_log_snapshot()
from_persisted_working_log()
Proposed Fix
Only overwrite total_additions / total_deletions when checkpoint data actually exists for the session:
// Fixed behavior:
if let Some(&additions) = session_additions.get(session_id) {
prompt_record.total_additions = additions;
}
if let Some(&deletions) = session_deletions.get(session_id) {
prompt_record.total_deletions = deletions;
}
This preserves the existing total_additions value for prompts inherited from INITIAL that have no new checkpoint activity, while still correctly updating prompts that do have checkpoint data.
Impact
- Metric accuracy:
total_ai_additions (used for "AI generated lines" reporting) is under-reported
- Acceptance rate: When
total_ai_additions < ai_accepted, the acceptance rate exceeds 100%, which is logically impossible
- Scope: Affects any commit where multiple AI agent sessions contribute code and at least one session's prompt is inherited from INITIAL without new checkpoints
Environment
- Affected agents: any multi-agent workflow (e.g., Cursor + Codex)
@svarlamov
Bug Description
When a commit involves multiple AI agent sessions (e.g., Cursor + Codex), prompts inherited from the
INITIALstate that have no new checkpoint data in the current working session get theirtotal_additionsreset to 0. This causes the reportedtotal_ai_additionsto be less thanai_accepted(orai_additions), which is logically impossible sincetotal_ai_additionsrepresents all AI-generated lines throughout a session, whileai_acceptedrepresents the subset that survived into the final commit.Steps to Reproduce
line_stats.additions > 0Expected: Both agents have
total_additions > 0reflecting their checkpointline_statsActual: Agent B's prompt (inherited from INITIAL) has
total_additions = 0because no new checkpoints exist for Agent B's session ID in the current working logRoot Cause
In
VirtualAttributions::calculate_and_update_prompt_metrics()(src/authorship/virtual_attribution.rs), the code unconditionally overwritestotal_additions/total_deletionsfrom thesession_additions/session_deletionsmaps:When a prompt is inherited from INITIAL attributions (e.g., from a previous agent session), its
PromptRecordalready carries the correcttotal_additionsvalue. However, if no new checkpoints exist for that session in the current working log,session_additionswon't contain the session ID, andunwrap_or(0)resets the value to 0.This affects all code paths that call
calculate_and_update_prompt_metrics:from_just_working_log()from_working_log_snapshot()from_persisted_working_log()Proposed Fix
Only overwrite
total_additions/total_deletionswhen checkpoint data actually exists for the session:This preserves the existing
total_additionsvalue for prompts inherited from INITIAL that have no new checkpoint activity, while still correctly updating prompts that do have checkpoint data.Impact
total_ai_additions(used for "AI generated lines" reporting) is under-reportedtotal_ai_additions < ai_accepted, the acceptance rate exceeds 100%, which is logically impossibleEnvironment
@svarlamov