Repository navigation
fix(agent): don't start a second rebuild of a just-finished history - #1494
Merged
Merged
Conversation
The maintenance worker reads its rebuild queue outside run admission, then
starts a rebuild for each candidate. If a foreground open finished that
task's rebuild in between, begin_timeline_rebuild scheduled a fresh epoch
anyway. Its flip published HistoryReplaced with a newer epoch, which fails
any ACP session that just attached ("The task's history was replaced while
ACP was attached"). This is the intermittent failure of
acp::tests::session_load_prepares_retired_history_before_registering_and_can_prompt.
The foreground path already rechecks under admission. The worker now uses
begin_queued_timeline_rebuild, which issues a ticket only if the task still
matches the queue, checked in the same write transaction.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
acp::tests::session_load_prepares_retired_history_before_registering_and_can_promptfails now and then on Linuxagent-test:It hit run 41335978564 (Nov 1,
agent/python-tool) and run 42396584932 (Nov 7, #1492). Neither branch touches this code.Root cause
The test exposes a real race in the maintenance worker. It's not a bug in the test.
select_rebuildreadstimeline_rebuild_queueoutside run admission, then callsbegin_timeline_rebuildfor each candidate. If a foreground open finishes that task's rebuild in between,begin_timeline_rebuildstill schedules a fresh epoch, because it never asks whether the task needs one. The worker then flips to that epoch and publishesHistoryReplacedwith a newerrows_epoch. The ACP observer correctly fails any session that has just attached at the older epoch.The foreground path (
prepare_history_for_read) already rechecks under admission, with the comment "a finished task needs no new epoch". The worker didn't.Outside tests, this hits a user who opens an old-format task in an editor over ACP at the moment the worker picks the same task. They get the reload error and a needless second rebuild.
Fix
Store::begin_queued_timeline_rebuildjoins or schedules a rebuild only if the task still matches the queue's own condition (last_seq>0 AND (rebuild_epoch IS NOT NULL OR timeline_format<>current)). The check runs in the same write transaction. A pending epoch is still joined, so interrupted rebuilds still resume.select_rebuildintoenqueue_rebuild, which uses the new call. A finished task is skipped like a busy one.begin_timeline_rebuildkeeps its unconditional behaviour for its other callers.Testing
a_rebuild_finished_after_discovery_gets_no_new_epochreplays that interleaving deterministically at the worker level. It fails against the oldbegin_timeline_rebuildcall and passes with the fix.agent::maintenance,store::projectorandacp::tests pass (93), as doescargo fmt --check.MAPLE_HOOK_SKIP=1. The hook'sjust ci-lintpassed. Injust ci-test, all 868maple-agenttests passed and 1318 of 1319 app tests did. The exception wasui::chat::tests::state_tests::send_feedback_tests::send_button_preserves_first_draft_when_task_creation_fails("Local account is not ready"), which this change doesn't touch. It then passed 30 of 30 runs alone and 3 of 3 runs of its module. It looks like a separate flake under heavy local load; I found no CI failure or issue for it.🤖 Generated with Claude Code
Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.