diff --git a/src/core/conversation_file_test.cpp b/src/core/conversation_file_test.cpp index b5c19f07..4503035c 100644 --- a/src/core/conversation_file_test.cpp +++ b/src/core/conversation_file_test.cpp @@ -127,7 +127,7 @@ void spit(const fs::path& p, const std::vector& d) { bool no_temp(const fs::path& dir) { for (const auto& e : fs::directory_iterator(dir)) { const std::string n = e.path().filename().string(); - if (n.size() > 4 && n.compare(n.size() - 4, 4, ".tmp") == 0 && n != "planted.bin.tmp") return false; + if (n.size() > 4 && n.compare(n.size() - 4, 4, ".tmp") == 0 && n != "keep.bin.tmp") return false; } return true; } @@ -424,10 +424,10 @@ int main() { check(session_file_write(p.string(), original, id, written, error, opt) && slurp(p) == image, "write with a small reserve succeeds"); } - // a failed write over an existing session keeps it, and touches no other file (a planted name of the old + // a failed write over an existing session keeps it, and touches no other file (a planted ".tmp", the old // fixed temporary pattern included) { - const fs::path p = dir / "keep.bin", planted = dir / "planted.bin.tmp"; + const fs::path p = dir / "keep.bin", planted = dir / "keep.bin.tmp"; check(session_file_write(p.string(), original, id, written, error), "keep: first write"); spit(planted, std::vector(5, 'p')); SavedConversation meta = original; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index c3072595..fe92c092 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -7627,6 +7627,27 @@ int main(int argc, char** argv) { // from the authoritative pools straight into the file, without a full host capture const auto disk_checks = strata::core::session_checkpoints_to_save(checks); kept = disk_checks.size(); + // RAM PREFLIGHT for the save (not a reservation): the copied running state, the checkpoint + // copies (this list and the one in `meta`) and the 16 MiB write buffer, above the parking floor + { + uint64_t need = (uint64_t) (17u << 20) + (uint64_t) live.size() * sizeof(int32_t); + strata::core::ConversationStateSizes z; + std::string why; + if (strata::core::conversation_session_sizes(g, ss, z, why)) + need += z.gdn + z.ple + (uint64_t) std::max(ss.qsa_alloc, 0) * + (z.tail + z.dead + z.block_pos); + for (const auto& c : disk_checks) need += 2 * (uint64_t) c.bytes(); + const uint64_t floor = (uint64_t) o.conversation_cache_min_free_mib << 20; + const auto avail = strata::core::conversation_available_memory(); + if (!strata::core::conversation_memory_admit(avail, need, floor)) { + refuse("not enough RAM to save the session (" + std::to_string(need >> 20) + + " MiB plus a floor of " + std::to_string((long long) o.conversation_cache_min_free_mib) + + " MiB needed, " + (avail ? std::to_string(*avail >> 20) + " MiB available)" + : "RAM telemetry unavailable)"), + strata::core::SessionError::memory); + continue; + } + } const strata::core::ConversationView view{live, live_imgs, disk_checks, cvec_cached}; strata::core::SavedConversation meta; std::vector sources;