From d613b73e1f9884df7e8cba001c895a2574465a8c Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Wed, 2 Dec 2026 23:34:58 +0300 Subject: [PATCH] Check source and capacity claims in authentic gathering and canceled work --- tests/custody_migration_tests.cpp | 60 ++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/tests/custody_migration_tests.cpp b/tests/custody_migration_tests.cpp index a6f4d87..adc6ad3 100644 --- a/tests/custody_migration_tests.cpp +++ b/tests/custody_migration_tests.cpp @@ -65,13 +65,30 @@ void reject(std::string bytes, std::string_view message="authenticated malformed require(!decode_checkpoint(bytes,*catalog,1,ExecutionMode::SerialReference), message); } struct CustodyOffsets { - std::size_t extension, agent_state, agent_timer, receipts; + std::size_t extension, agent_state, agent_timer, first_execution, receipts; + std::vector node_reserved, storage_reserved; std::vector> entries; }; CustodyOffsets offsets(const World& world, const std::string& bytes) { CustodyOffsets result{}; const auto body=body_begin(bytes); - result.extension=body+raw(world,WorldSaveFormat::Current14).size(); + Void::Binary::Reader prefix(std::as_bytes(std::span{bytes.data(),bytes.size()})); + const auto skip=[&](std::size_t n){require(prefix.skip(n).has_value(),"incomplete historical prefix");}; + const auto count=[&]{return prefix.readLittle().value();}; + const auto route_fields=[&]{const auto n=count();skip(static_cast(n)*8);}; + skip(body+109+static_cast(world.width())*world.height()); + const auto agents=count(); + for(std::uint32_t i=0;i(jobs)*25); + const auto nodes=count(); + for(std::uint32_t i=0;i(items)*9);} + const auto orders=count(); + for(std::uint32_t i=0;i(materials)*13);const auto stops=count();for(std::uint32_t j=0;j(world.width())*world.height()+4+16).has_value(),"missing agent state"); result.agent_state=reader.position(); @@ -85,8 +102,8 @@ CustodyOffsets offsets(const World& world, const std::string& bytes) { (agent.execution?43+47*agent.execution->reservation_count:0); result.receipts=result.extension+extension_size; Void::Binary::Reader receipts(std::as_bytes(std::span{bytes.data(),bytes.size()}).subspan(result.receipts)); - const auto count=receipts.readLittle().value(); - for(std::uint32_t i=0;i().value(); + for(std::uint32_t i=0;i().value();require(receipts.skip(request_size).has_value(),"bad request fixture"); const auto response=result.receipts+receipts.position(); @@ -128,6 +145,7 @@ struct [[= Void::Testing::name("CustodyMigrationTests")]] CustodyMigrationTests [[= Void::Testing::tag("custody-mutation")]] Test full_receipt_ring_preserves_exact_results_and_evicts_old_ordinals(); [[= Void::Testing::tag("custody-mutation")]] Test admission_result_reservations_are_distinct(); [[= Void::Testing::tag("custody-mutation")]] Test result_effects_are_unique_with_retired_and_distinct_owners(); + [[= Void::Testing::tag("custody-mutation")]] Test source_less_harvesting_and_reserved_return_destination_are_rejected(); }; Test CustodyMigrationTests::authentic_historical_phases_keep_eighty_tick_projection() { for (const auto& original : custody_v14::fixtures) { @@ -376,4 +394,38 @@ Test CustodyMigrationTests::result_effects_are_unique_with_retired_and_distinct_ auto duplicate=good;duplicate.replace(first+22,22,good.substr(first,22)); reject(std::move(duplicate),"duplicate historical effect accepted"); } +Test CustodyMigrationTests::source_less_harvesting_and_reserved_return_destination_are_rejected() { + auto gathering=load(unhex(fixture("harvest-gathering").checkpoint)); + const auto original=save(gathering); const auto location=offsets(*gathering.world,original); + require(save(load(original))==original,"authentic harvesting original was rejected"); + auto missing=original; + change(missing,location.agent_timer+4,std::uint64_t{0}); + change(missing,location.first_execution+17,std::uint64_t{0}); + change(missing,location.node_reserved.front(),std::uint32_t{0}); + // Remove the source claim together with its cached worker count, keeping the + // real destination capacity. A mere cache mismatch must not explain refusal. + require(gathering.world->agents().front().execution->reservations[0].kind==ReservationKind::Harvest,"source claim fixture changed"); + missing.replace(location.first_execution+43,47,47,'\0'); + reject(std::move(missing),"Gathering without source or source claim accepted"); + + auto returning=load(unhex(fixture("cabin-working").checkpoint)); + require(returning.world->cancel_production(returning.world->production_orders().front().id)==CommandStatus::Accepted,"return fixture cancellation failed"); + const auto good=save(returning);const auto cancelled=offsets(*returning.world,good); + require(save(load(good))==good,"authentic cancellation original was rejected"); + require(returning.world->agents().front().execution->reservation_count==0,"cancel did not release all reservations"); + Void::Binary::Reader counter(std::as_bytes(std::span{good.data(),good.size()}).subspan(cancelled.extension+8)); + const auto next=counter.readLittle().value(); + const auto destination=returning.world->storages().front().id; + auto reserved=good; + change(reserved,cancelled.extension+8,next+1); + change(reserved,cancelled.first_execution+25,destination); + change(reserved,cancelled.first_execution+42,std::uint8_t{1}); + change(reserved,cancelled.storage_reserved.front(),std::uint32_t{1}); + std::string capacity(47,'\0'); + change(capacity,0,next);change(capacity,8,ReservationKind::Capacity); + change(capacity,9,OwnerKind::Storage);change(capacity,10,destination);change(capacity,27,std::uint32_t{1}); + reserved.insert(cancelled.first_execution+43,capacity); + change(reserved,16,static_cast(reserved.size()-32-body_begin(reserved))); + reject(std::move(reserved),"Returning retained a destination and matching capacity reservation"); +} } // namespace