Skip to content

refactor(p2p): tighten tests, share helpers, and guard test hooks - #77

Merged
trvon merged 1 commit into
fix/p2p-robustnessfrom
fix/p2p-quality
Aug 30, 2026
Merged

refactor(p2p): tighten tests, share helpers, and guard test hooks#77
trvon merged 1 commit into
fix/p2p-robustnessfrom
fix/p2p-quality

Conversation

@trvon

@trvon trvon commented Aug 30, 2026

Copy link
Copy Markdown
Owner

Summary

Stacked draft child of #76. Closes the remaining code-quality / test-quality findings from the deep review of the #68 stack that were not security-blocking. The large memory_sync.h body split is intentionally deferred to its own follow-up (it is a ~3,400-line mechanical move best reviewed in isolation). No merge or deploy.

  1. Test hooks compiled out of production (P2)gReconnectLoopHook storage, invokeReconnectLoopHook, and the reconnect-loop call site are now guarded by YAMS_DAEMON_TEST_HOOKS_ENABLED, matching the already-guarded testing_setReconnectLoopHook. Production builds no longer carry the test-hook machinery.
  2. Distinct writer keys per identity (P1 test-quality)testWriterAuthenticator previously signed every node with one shared key and trusted every node with that same key, masking origin-binding regressions. Each writer id (node/client-node/server-node) now has its own key pair, and each authenticator trusts every writer with that writer's own key, so a record claiming origin server-node but signed with client-node's key now fails verification. Constructor signature unchanged; zero call-site churn.
  3. Shared test helpers (P2) — the copy-pasted bytes/text/TempDir helpers are extracted to tests/common/p2p_test_helpers.h (namespace yams::p2p_test, inline) and reused by the transport/delta/manager suites.
  4. Timing-sensitive sleep removed (P2) — the transport session-expiry test no longer sleeps a fixed 600ms; it polls until a write fails (bounded 3s), accepting either the client-side Timeout or the peer-closed NotFound manifestation of the absolute session deadline.
  5. [stress] opt-in (P2) — the memory-sync-service concurrent-lifecycle race test now SKIPs unless YAMS_RUN_STRESS_TESTS=1, matching the repo's existing FTS5 stress pattern, so the default suite no longer runs stress coverage.
  6. Commitment-JSON dedup (P2) — the byte-identical commitments array serialization in MemorySyncLoop::coldBootstrapRoot and persistReplicationCheckpoint is now one static commitmentsJson() helper. Output is byte-identical (verified by the cold-bootstrap root-hash tests, 838/60 passing).

Validation

Focused normal matrix (green):

  • p2p_transport 261 / 18 (×3 runs, deterministic)
  • p2p_delta 301 / 10 · p2p_manager 180 / 12 · p2p_protocol 495 / 14 · peer_registry 91 / 6
  • memory_sync 838 / 60 (cold-bootstrap root hashes unchanged ⇒ byte-identical JSON)
  • memory_sync_service 260 assertions / 26 passed + 1 skipped (the [stress] case now skips by default)

Formatting, git diff --check, license, Windows-header, and portability checks pass. commitlint passes; the local dco default range includes 119 pre-DCO experimental commits (pre-existing; the pushed range is what the pre-push hook validates).

Ordinary pre-push gate passed without bypass: cross-compile smoke, ASAN 195/195, TSAN 280/280. Evidence: build/local-ci/pre-push-ci-gate/summary-20260830T141716Z-62734.md.

AI disclosure

Extensive AI assistance was used for implementation, test generation, and adversarial review. All changes were locally inspected and validated; the umbrella #68 remains blocked by the decision-grade live-performance gate.

Stack

Deferred follow-up: split the ~3,400-line inline MemorySyncLoop bodies from include/yams/memory_sync/memory_sync.h into src/memory_sync/memory_sync.cpp, and deduplicate the cross-file commitment parse sites (bounds differ per caller).

Do not merge or deploy this child independently.

Compile the direct-P2P test-only machinery out of production builds
(YAMS_DAEMON_TEST_HOOKS_ENABLED now wraps the reconnect-loop hook storage,
invoker, and call site, matching the already-guarded setter). Replace the
single shared writer key in p2p_manager tests with one distinct key pair per
writer identity so origin-binding regressions (a record claiming origin X but
signed by Y's key) fail verification. Extract the copy-pasted bytes/text/TempDir
helpers into tests/common/p2p_test_helpers.h and reuse them across the
transport/delta/manager suites. Replace a timing-sensitive fixed sleep in the
transport session-expiry test with a bounded poll that accepts both deadline
manifestations, and gate the memory-sync-service concurrent-lifecycle stress
case behind YAMS_RUN_STRESS_TESTS=1 per the existing FTS5 pattern. Deduplicate
the byte-identical commitment-JSON serialization inside MemorySyncLoop
(coldBootstrapRoot / persistReplicationCheckpoint) into one static helper.

Signed-off-by: trvon <git@trevon.dev>
@trvon
trvon marked this pull request as ready for review August 30, 2026 15:48
Copilot AI lite review requested due to automatic review settings August 30, 2026 15:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

There are a couple of concrete test-code issues to fix (helper header not self-contained due to missing <cstddef>, and stress gating semantics not matching the documented =1/true convention).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR tightens and refactors the direct-P2P and memory-sync test surface while ensuring daemon-side test-hook machinery is compiled out of production builds, and it deduplicates a byte-identical commitments JSON serialization helper in MemorySyncLoop.

Changes:

  • Compile-time guard reconnect-loop test hook storage/invocation behind YAMS_DAEMON_TEST_HOOKS_ENABLED.
  • Extract shared P2P unit-test helpers (bytes, text, TempDir) into tests/common/p2p_test_helpers.h and reuse across P2P suites; make the session-deadline test less timing-sensitive by polling rather than sleeping.
  • Add opt-in gating for a stress-tagged MemorySyncService test via YAMS_RUN_STRESS_TESTS, and deduplicate commitments JSON construction in MemorySyncLoop.
File summaries
File Description
tests/unit/memory_sync/memory_sync_service_catch2_test.cpp Adds env-gated skipping for a stress lifecycle/status race test.
tests/unit/daemon/p2p_transport_catch2_test.cpp Switches to shared helpers and replaces fixed sleep with bounded polling for session-expiry verification.
tests/unit/daemon/p2p_manager_catch2_test.cpp Switches to shared helpers and strengthens writer-auth test setup with per-writer key material.
tests/unit/daemon/p2p_delta_catch2_test.cpp Switches to shared helpers to remove duplicated byte/text/tempdir utilities.
tests/common/p2p_test_helpers.h Introduces shared helper utilities for P2P unit tests.
src/daemon/p2p/p2p_manager.cpp Guards reconnect-loop test hook storage + invocation behind YAMS_DAEMON_TEST_HOOKS_ENABLED.
include/yams/memory_sync/memory_sync.h Deduplicates commitments JSON serialization via a shared helper to keep output byte-identical.
Review details
  • Files reviewed: 7/7 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +9 to +14
#include <chrono>
#include <cstring>
#include <filesystem>
#include <string>
#include <string_view>
#include <vector>
Comment on lines +542 to +544
if (!yams::config::getenv_optional("YAMS_RUN_STRESS_TESTS").has_value()) {
SKIP("Stress tests disabled. Set YAMS_RUN_STRESS_TESTS=1 to enable.");
}
@trvon
trvon merged commit af609a1 into experimental Aug 30, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants