// rebuild merges added messages with the log of agent, in date order, and
// builds a new memory beside it. The caller holds the token lock. The model
// uses the original agent prompt and cache key. A failed or stopped build
// removes the new directory and leaves the original directory unchanged.
// Success copies the other agent files, adds this run's usage, and swaps the
// directories, with the original kept as a timestamped backup. Message IDs are
// rebuild merges added messages with the log of agent, in date order, and
// builds a new memory beside it. The caller holds the token lock. The model
// uses the original agent prompt and cache key. A failed or stopped build
// removes the new directory and leaves the original directory unchanged.
// Success copies the other agent files, adds this run's usage, and swaps the
// directories, with the original kept as a timestamped backup. Message IDs are