Skip to content

fix(agent): don't start a second rebuild of a just-finished history - #1494

Merged
AnthonyRonning merged 1 commit into
masterfrom
claude/acp-load-flake
Nov 7, 2026
Merged

AnthonyRonning merged 1 commit into
masterfrom
claude/acp-load-flake

Conversation

@AnthonyRonning

@AnthonyRonning AnthonyRonning commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem

acp::tests::session_load_prepares_retired_history_before_registering_and_can_prompt fails now and then on Linux agent-test:

session/load failed: {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal error","data":"The task's history was replaced while ACP was attached. Reload this session; its task continues."}}

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_rebuild reads timeline_rebuild_queue outside run admission, then calls begin_timeline_rebuild for each candidate. If a foreground open finishes that task's rebuild in between, begin_timeline_rebuild still schedules a fresh epoch, because it never asks whether the task needs one. The worker then flips to that epoch and publishes HistoryReplaced with a newer rows_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_rebuild joins 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.
  • The worker's per-candidate step moves out of select_rebuild into enqueue_rebuild, which uses the new call. A finished task is skipped like a busy one.
  • begin_timeline_rebuild keeps its unconditional behaviour for its other callers.

Testing

  • Reproduced the race on purpose. I added a temporary hook (not in this PR) that holds the worker between discovery and enqueue until the foreground rebuild finishes, plus 0–20 ms of jitter. Unfixed: 33 of 60 runs failed, 25 with this exact message and the rest with sibling symptoms of the same second rebuild ("This task changed while its session was loading", "Resource not found"). Fixed, with the same hook: 60 of 60 passed.
  • New regression test a_rebuild_finished_after_discovery_gets_no_new_epoch replays that interleaving deterministically at the worker level. It fails against the old begin_timeline_rebuild call and passes with the fix.
  • The original ACP test passed 200 of 200 runs at the normal tick. agent::maintenance, store::projector and acp:: tests pass (93), as does cargo fmt --check.
  • Committed with MAPLE_HOOK_SKIP=1. The hook's just ci-lint passed. In just ci-test, all 868 maple-agent tests passed and 1318 of 1319 app tests did. The exception was ui::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


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

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>
@AnthonyRonning
AnthonyRonning merged commit 39d4dd6 into master Nov 7, 2026
23 checks passed
@AnthonyRonning
AnthonyRonning deleted the claude/acp-load-flake branch November 7, 2026 18:46
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.

1 participant