diff --git a/docs/agent-workflow/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index 6211685..c90884b 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -61,6 +61,17 @@ start coordinate only, so ranges are zero-width until an end is available. LSP adapters and index/rename/debugging integration remain open. `vbs test` passed all 17 native suites; CMake passed the reflection and trace suites. +## December 6 index selector schema 3 + +`CURRENT` is now schema 3 (`{"schema":3,"file":...,"previous":...}`), so the +recovery generation name is recorded rather than hardcoded. Restore still +accepts schema 1 (bare name) and schema 2 (migrating it to 3), validates that the +recorded previous name is a plain file name (rejecting separators and `..`), and +recovers from it when the current file is missing. Registered `vls_index_tests` +grew to 29 cases, including recovery through a renamed previous generation and +rejection of an escaping name. All 18 native suites and all 18 CMake tests +passed. + ## December 5 persistent index snapshot migration The symbol-index snapshot is now schema 2 with a canonical payload digest diff --git a/src/index/SymbolIndex.cpp b/src/index/SymbolIndex.cpp index 371b49c..99fa29f 100644 --- a/src/index/SymbolIndex.cpp +++ b/src/index/SymbolIndex.cpp @@ -660,7 +660,8 @@ bool SymbolIndex::save_generation(const fs::path &root) { const auto temporary = root / "CURRENT.tmp"; { std::ofstream stream{temporary, std::ios::binary | std::ios::trunc}; - stream << "{\"schema\":2,\"file\":\"snapshot.json\"}\n"; + stream << "{\"schema\":3,\"file\":\"snapshot.json\"," + "\"previous\":\"snapshot.prev.json\"}\n"; } fs::rename(temporary, root / "CURRENT", error); return !error; @@ -675,6 +676,7 @@ SymbolIndex::restore_generation(const fs::path &root) { const auto legacy = root / "index.json"; fs::path selected; bool migrate_selector{}; + std::string previous_name{"snapshot.prev.json"}; if (fs::exists(current, error)) { std::ifstream stream{current, std::ios::binary}; std::string line(256, '\0'); @@ -693,9 +695,20 @@ SymbolIndex::restore_generation(const fs::path &root) { const auto *schema = fields ? P::find_member(*fields, "schema") : nullptr; const auto *name = fields ? P::as_string(P::find_member(*fields, "file")) : nullptr; const auto *value = schema ? std::get_if(&schema->value) : nullptr; - if (!fields || !name || !value || *value != 2) + if (!fields || !name || !value || (*value != 2 && *value != 3)) return std::unexpected("unsupported index generation selector"); file = *name; + migrate_selector = *value == 2; + if (*value == 3) { + if (const auto *recorded = P::find_member(*fields, "previous")) { + const auto *recorded_name = P::as_string(recorded); + if (!recorded_name || recorded_name->empty() || recorded_name->size() > 256 || + recorded_name->find('/') != std::string::npos || + recorded_name->find("..") != std::string::npos) + return std::unexpected("unsupported index generation selector"); + previous_name = *recorded_name; + } + } } if (file != "snapshot.json") return std::unexpected("unsupported index generation selector"); @@ -709,7 +722,7 @@ SymbolIndex::restore_generation(const fs::path &root) { if (!result) return result; if (result->status != LoadStatus::loaded) { - const auto previous = root / "snapshot.prev.json"; + const auto previous = root / previous_name; if (fs::exists(previous, error)) { result = restore(previous); // Keep CURRENT consistent: the recovered generation becomes current. @@ -721,7 +734,8 @@ SymbolIndex::restore_generation(const fs::path &root) { if (result && result->status == LoadStatus::loaded && (migrate_selector || result->migrated)) { const auto temporary = root / "CURRENT.tmp"; std::ofstream stream{temporary, std::ios::binary | std::ios::trunc}; - stream << "{\"schema\":2,\"file\":\"snapshot.json\"}\n"; + stream << "{\"schema\":3,\"file\":\"snapshot.json\"," + "\"previous\":\"snapshot.prev.json\"}\n"; stream.close(); fs::rename(temporary, current, error); // Persist the upgraded snapshot format; the previous generation is diff --git a/tests/index/SymbolIndexTests.cpp b/tests/index/SymbolIndexTests.cpp index 3232a3c..22c482d 100644 --- a/tests/index/SymbolIndexTests.cpp +++ b/tests/index/SymbolIndexTests.cpp @@ -305,7 +305,7 @@ VOID_TEST(SymbolIndex, generation_selector_migrates_from_the_legacy_format) { expect(restored->status == SymbolIndex::LoadStatus::loaded); std::ifstream selector{root / "CURRENT", std::ios::binary}; const std::string text{std::istreambuf_iterator{selector}, {}}; - expect(text.find("\"schema\":2") != std::string::npos); + expect(text.find("\"schema\":3") != std::string::npos); { std::ofstream stream{root / "CURRENT", std::ios::binary | std::ios::trunc}; stream << "future.json\n"; @@ -502,3 +502,30 @@ VOID_TEST(SymbolIndex, rejects_an_oversized_signature) { signed_record.signature = std::string(400, 'x'); expect(!index.publish(context("debug:unit", {signed_record}))); } + +VOID_TEST(SymbolIndex, selector_records_the_previous_generation_name) { + Void::FileSystem::TempDir temp{"vls_symbol_index_"}; + const auto root = temp.path() / "index"; + SymbolIndex index; + require(index.publish(context("debug:a", {record("a", "/project/a.cpp", 1)}))); + require(index.save_generation(root)); + require(index.publish(context("debug:b", {record("b", "/project/b.cpp", 2)}))); + require(index.save_generation(root)); + fs::rename(root / "snapshot.prev.json", root / "older.json"); + { + std::ofstream stream{root / "CURRENT", std::ios::binary | std::ios::trunc}; + stream << R"({"schema":3,"file":"snapshot.json","previous":"older.json"})"; + } + fs::remove(root / "snapshot.json"); + SymbolIndex recovered; + const auto restored = recovered.restore_generation(root); + require(restored.has_value()); + expect(restored->status == SymbolIndex::LoadStatus::loaded); + expect(count(recovered.find({.name = "a"})) == 1); + { + std::ofstream stream{root / "CURRENT", std::ios::binary | std::ios::trunc}; + stream << R"({"schema":3,"file":"snapshot.json","previous":"../escape.json"})"; + } + SymbolIndex rejected; + expect(!rejected.restore_generation(root).has_value()); +}