Files
clice/tests/unit/server/pch_worker_tests.cpp
ykiko 3838bedcbf feat: persistent PCH/PCM cache across sessions (#391)
## Summary

PCH and PCM artifacts are now cached to disk at
`.clice/cache/{pch/,pcm/}` with content-addressed filenames, so they
survive server restarts. Dependency metadata is persisted in
`cache.json` (using eventide serde) with a shared path table for
deduplication.

### Key changes

- **protocol.h**: `output_path` field on `BuildPCHParams` /
`BuildPCMParams` so master specifies where workers write
- **stateless_worker.cpp**: Atomic write via `.tmp` + `fs::rename`;
`CompilationUnit` destroyed before rename to flush the file to disk;
fallback to temp file when `output_path` is empty (unit tests)
- **master_server.h**: `PCMState` struct, `pcm_states` map,
`load_cache()` / `save_cache()` / `cleanup_cache()` methods
- **master_server.cpp**: Cache lifecycle — load from `cache.json` on
startup, save after each PCH/PCM build and on exit; deterministic path
computation (`xxh3` preamble hash for PCH, module name + source path
hash for PCM); stale files (>7 days) cleaned on startup; `cache.json`
uses shared path table to avoid redundant storage of header paths across
entries
- **filesystem.h**: `fs::rename()` helper; `ThreadSafeFS` broadened to
match `.pch` extension instead of `preamble-` prefix
- **tests**: 11 new integration tests covering PCH/PCM persistence,
cross-session reuse, staleness detection, shared preamble dedup, and
restart survival; unit tests updated with `output_path`

### Naming scheme

- **PCH**: `.clice/cache/pch/<016x(xxh3(preamble))>.pch`
- **PCM**:
`.clice/cache/pcm/<module_name>-<016x(xxh3(source_path))>.pcm`

## Test plan

- [x] Unit tests — 448 passed
- [x] Integration tests — 92 passed (including 11 new persistent cache
tests)
- [x] Smoke tests — 1 passed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 16:29:21 +08:00

155 lines
4.4 KiB
C++

#include <string>
#include <vector>
#include "test/test.h"
#include "server/protocol.h"
#include "server/worker_test_helpers.h"
#include "syntax/scan.h"
namespace clice::testing {
namespace {
namespace et = eventide;
// ============================================================================
// End-to-end PCH compilation through real workers:
// 1. Stateless worker builds PCH for preamble headers
// 2. Stateful worker compiles a file using the PCH
// ============================================================================
TEST_SUITE(PCHWorker) {
TEST_CASE(BuildPCHThenCompile) {
TempDir tmp;
tmp.touch("common.h", R"cpp(struct Point { int x, y; };)cpp" "\n");
auto header = tmp.path("common.h");
std::string main_text = "#include \"common.h\"\nPoint p{1,2};\n";
tmp.touch("main.cpp", main_text);
auto main_file = tmp.path("main.cpp");
auto dir = std::string(tmp.root);
// --- Phase 1: Build PCH via stateless worker ---
WorkerHandle sl;
ASSERT_TRUE(sl.spawn("stateless-worker"));
std::string pch_path;
bool phase1_done = false;
sl.run([&]() -> et::task<> {
worker::BuildPCHParams params;
params.file = main_file;
params.directory = dir;
params.arguments = {"clang++",
"-resource-dir",
std::string(resource_dir()),
"-x",
"c++-header",
"-I",
dir,
main_file};
params.content = main_text;
params.output_path = tmp.path("preamble.pch");
auto result = co_await sl.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value());
CO_ASSERT_TRUE(result.value().success);
pch_path = result.value().pch_path;
EXPECT_FALSE(pch_path.empty());
phase1_done = true;
sl.peer->close_output();
});
ASSERT_TRUE(phase1_done);
ASSERT_FALSE(pch_path.empty());
// Verify the PCH file exists on disk.
ASSERT_TRUE(llvm::sys::fs::exists(pch_path));
// --- Phase 2: Compile with PCH via stateful worker ---
WorkerHandle sf;
ASSERT_TRUE(sf.spawn("stateful-worker"));
bool phase2_done = false;
auto preamble_bound = compute_preamble_bound(main_text);
sf.run([&]() -> et::task<> {
worker::CompileParams params;
params.path = main_file;
params.version = 1;
params.text = main_text;
params.directory = dir;
params.arguments = {"clang++",
"-resource-dir",
std::string(resource_dir()),
"-fsyntax-only",
"-I",
dir,
main_file};
params.pch = {pch_path, preamble_bound};
auto result = co_await sf.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value());
EXPECT_EQ(result.value().version, 1);
phase2_done = true;
sf.peer->close_output();
});
ASSERT_TRUE(phase2_done);
// Cleanup PCH temp file.
std::remove(pch_path.c_str());
}
TEST_CASE(CompileWithoutPCHStillWorks) {
TempDir tmp;
tmp.touch("common.h", R"cpp(struct Point { int x, y; };)cpp" "\n");
std::string main_text = "#include \"common.h\"\nPoint p{1,2};\n";
tmp.touch("main.cpp", main_text);
auto main_file = tmp.path("main.cpp");
auto dir = std::string(tmp.root);
WorkerHandle sf;
ASSERT_TRUE(sf.spawn("stateful-worker"));
bool compile_done = false;
sf.run([&]() -> et::task<> {
worker::CompileParams params;
params.path = main_file;
params.version = 1;
params.text = main_text;
params.directory = dir;
params.arguments = {"clang++",
"-resource-dir",
std::string(resource_dir()),
"-fsyntax-only",
"-I",
dir,
main_file};
// pch left as default (empty path, 0 bound).
auto result = co_await sf.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value());
EXPECT_EQ(result.value().version, 1);
compile_done = true;
sf.peer->close_output();
});
ASSERT_TRUE(compile_done);
}
}; // TEST_SUITE(PCHWorker)
} // namespace
} // namespace clice::testing